Home | History | Annotate | Line # | Download | only in examples
wpas-dbus-new-signals.py revision 1.1.1.2.32.1
      1           1.1  christos #!/usr/bin/python
      2           1.1  christos 
      3           1.1  christos import dbus
      4           1.1  christos import sys, os
      5           1.1  christos import time
      6           1.1  christos import gobject
      7           1.1  christos from dbus.mainloop.glib import DBusGMainLoop
      8           1.1  christos 
      9           1.1  christos WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"
     10           1.1  christos WPAS_DBUS_INTERFACE = "fi.w1.wpa_supplicant1"
     11           1.1  christos WPAS_DBUS_OPATH = "/fi/w1/wpa_supplicant1"
     12           1.1  christos 
     13           1.1  christos WPAS_DBUS_INTERFACES_INTERFACE = "fi.w1.wpa_supplicant1.Interface"
     14           1.1  christos WPAS_DBUS_INTERFACES_OPATH = "/fi/w1/wpa_supplicant1/Interfaces"
     15           1.1  christos WPAS_DBUS_BSS_INTERFACE = "fi.w1.wpa_supplicant1.BSS"
     16           1.1  christos WPAS_DBUS_NETWORK_INTERFACE = "fi.w1.wpa_supplicant1.Network"
     17           1.1  christos 
     18           1.1  christos def byte_array_to_string(s):
     19           1.1  christos 	import urllib
     20           1.1  christos 	r = ""    
     21           1.1  christos 	for c in s:
     22           1.1  christos 		if c >= 32 and c < 127:
     23           1.1  christos 			r += "%c" % c
     24           1.1  christos 		else:
     25           1.1  christos 			r += urllib.quote(chr(c))
     26           1.1  christos 	return r
     27           1.1  christos 
     28           1.1  christos def list_interfaces(wpas_obj):
     29           1.1  christos 	ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
     30           1.1  christos 			      dbus_interface=dbus.PROPERTIES_IFACE)
     31           1.1  christos 	for path in ifaces:
     32           1.1  christos 		if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
     33           1.1  christos 		ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname',
     34           1.1  christos 			      dbus_interface=dbus.PROPERTIES_IFACE)
     35  1.1.1.2.32.1    martin 		print(ifname)
     36           1.1  christos 
     37           1.1  christos def interfaceAdded(interface, properties):
     38  1.1.1.2.32.1    martin 	print("InterfaceAdded(%s): Ifname=%s" % (interface, properties['Ifname']))
     39           1.1  christos 
     40           1.1  christos def interfaceRemoved(interface):
     41  1.1.1.2.32.1    martin 	print("InterfaceRemoved(%s)" % (interface))
     42           1.1  christos 
     43           1.1  christos def propertiesChanged(properties):
     44           1.1  christos 	for i in properties:
     45  1.1.1.2.32.1    martin 		print("PropertiesChanged: %s=%s" % (i, properties[i]))
     46           1.1  christos 
     47           1.1  christos def showBss(bss):
     48           1.1  christos 	net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss)
     49           1.1  christos 	net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
     50           1.1  christos 
     51           1.1  christos 	# Convert the byte-array for SSID and BSSID to printable strings
     52           1.1  christos 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID',
     53           1.1  christos 			  dbus_interface=dbus.PROPERTIES_IFACE)
     54           1.1  christos 	bssid = ""
     55           1.1  christos 	for item in val:
     56           1.1  christos 		bssid = bssid + ":%02x" % item
     57           1.1  christos 	bssid = bssid[1:]
     58           1.1  christos 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID',
     59           1.1  christos 			  dbus_interface=dbus.PROPERTIES_IFACE)
     60           1.1  christos 	ssid = byte_array_to_string(val)
     61           1.1  christos 
     62       1.1.1.2  christos 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPA',
     63           1.1  christos 			  dbus_interface=dbus.PROPERTIES_IFACE)
     64           1.1  christos 	wpa = "no"
     65           1.1  christos 	if val != None:
     66           1.1  christos 		wpa = "yes"
     67       1.1.1.2  christos 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSN',
     68           1.1  christos 			  dbus_interface=dbus.PROPERTIES_IFACE)
     69           1.1  christos 	wpa2 = "no"
     70           1.1  christos 	if val != None:
     71           1.1  christos 		wpa2 = "yes"
     72           1.1  christos 	freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency',
     73           1.1  christos 			   dbus_interface=dbus.PROPERTIES_IFACE)
     74           1.1  christos 	signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal',
     75           1.1  christos 			     dbus_interface=dbus.PROPERTIES_IFACE)
     76           1.1  christos 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates',
     77           1.1  christos 			  dbus_interface=dbus.PROPERTIES_IFACE)
     78           1.1  christos 	if len(val) > 0:
     79           1.1  christos 		maxrate = val[0] / 1000000
     80           1.1  christos 	else:
     81           1.1  christos 		maxrate = 0
     82           1.1  christos 
     83  1.1.1.2.32.1    martin 	print("  %s  ::  ssid='%s'  wpa=%s  wpa2=%s  signal=%d  rate=%d  freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq))
     84           1.1  christos 
     85           1.1  christos def scanDone(success):
     86           1.1  christos 	gobject.MainLoop().quit()
     87  1.1.1.2.32.1    martin 	print("Scan done: success=%s" % success)
     88           1.1  christos 
     89           1.1  christos def scanDone2(success, path=None):
     90  1.1.1.2.32.1    martin 	print("Scan done: success=%s [path=%s]" % (success, path))
     91           1.1  christos 
     92           1.1  christos def bssAdded(bss, properties):
     93  1.1.1.2.32.1    martin 	print("BSS added: %s" % (bss))
     94           1.1  christos 	showBss(bss)
     95           1.1  christos 
     96           1.1  christos def bssRemoved(bss):
     97  1.1.1.2.32.1    martin 	print("BSS removed: %s" % (bss))
     98           1.1  christos 
     99           1.1  christos def blobAdded(blob):
    100  1.1.1.2.32.1    martin 	print("BlobAdded(%s)" % (blob))
    101           1.1  christos 
    102           1.1  christos def blobRemoved(blob):
    103  1.1.1.2.32.1    martin 	print("BlobRemoved(%s)" % (blob))
    104           1.1  christos 
    105           1.1  christos def networkAdded(network, properties):
    106  1.1.1.2.32.1    martin 	print("NetworkAdded(%s)" % (network))
    107           1.1  christos 
    108           1.1  christos def networkRemoved(network):
    109  1.1.1.2.32.1    martin 	print("NetworkRemoved(%s)" % (network))
    110           1.1  christos 
    111           1.1  christos def networkSelected(network):
    112  1.1.1.2.32.1    martin 	print("NetworkSelected(%s)" % (network))
    113           1.1  christos 
    114           1.1  christos def propertiesChangedInterface(properties):
    115           1.1  christos 	for i in properties:
    116  1.1.1.2.32.1    martin 		print("PropertiesChanged(interface): %s=%s" % (i, properties[i]))
    117           1.1  christos 
    118           1.1  christos def propertiesChangedBss(properties):
    119           1.1  christos 	for i in properties:
    120  1.1.1.2.32.1    martin 		print("PropertiesChanged(BSS): %s=%s" % (i, properties[i]))
    121           1.1  christos 
    122           1.1  christos def propertiesChangedNetwork(properties):
    123           1.1  christos 	for i in properties:
    124  1.1.1.2.32.1    martin 		print("PropertiesChanged(Network): %s=%s" % (i, properties[i]))
    125           1.1  christos 
    126           1.1  christos def main():
    127           1.1  christos 	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    128           1.1  christos 	global bus
    129           1.1  christos 	bus = dbus.SystemBus()
    130           1.1  christos 	wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
    131           1.1  christos 
    132           1.1  christos 	if len(sys.argv) != 2:
    133           1.1  christos 		list_interfaces(wpas_obj)
    134           1.1  christos 		os._exit(1)
    135           1.1  christos 
    136           1.1  christos 	wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
    137           1.1  christos 	bus.add_signal_receiver(interfaceAdded,
    138           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACE,
    139           1.1  christos 				signal_name="InterfaceAdded")
    140           1.1  christos 	bus.add_signal_receiver(interfaceRemoved,
    141           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACE,
    142           1.1  christos 				signal_name="InterfaceRemoved")
    143           1.1  christos 	bus.add_signal_receiver(propertiesChanged,
    144           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACE,
    145           1.1  christos 				signal_name="PropertiesChanged")
    146           1.1  christos 
    147           1.1  christos 	ifname = sys.argv[1]
    148           1.1  christos 	path = wpas.GetInterface(ifname)
    149           1.1  christos 	if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
    150           1.1  christos 	iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
    151           1.1  christos 	iface.connect_to_signal("ScanDone", scanDone2,
    152           1.1  christos 				path_keyword='path')
    153           1.1  christos 
    154           1.1  christos 	bus.add_signal_receiver(scanDone,
    155           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    156           1.1  christos 				signal_name="ScanDone",
    157           1.1  christos 				path=path)
    158           1.1  christos 	bus.add_signal_receiver(bssAdded,
    159           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    160           1.1  christos 				signal_name="BSSAdded",
    161           1.1  christos 				path=path)
    162           1.1  christos 	bus.add_signal_receiver(bssRemoved,
    163           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    164           1.1  christos 				signal_name="BSSRemoved",
    165           1.1  christos 				path=path)
    166           1.1  christos 	bus.add_signal_receiver(blobAdded,
    167           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    168           1.1  christos 				signal_name="BlobAdded",
    169           1.1  christos 				path=path)
    170           1.1  christos 	bus.add_signal_receiver(blobRemoved,
    171           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    172           1.1  christos 				signal_name="BlobRemoved",
    173           1.1  christos 				path=path)
    174           1.1  christos 	bus.add_signal_receiver(networkAdded,
    175           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    176           1.1  christos 				signal_name="NetworkAdded",
    177           1.1  christos 				path=path)
    178           1.1  christos 	bus.add_signal_receiver(networkRemoved,
    179           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    180           1.1  christos 				signal_name="NetworkRemoved",
    181           1.1  christos 				path=path)
    182           1.1  christos 	bus.add_signal_receiver(networkSelected,
    183           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    184           1.1  christos 				signal_name="NetworkSelected",
    185           1.1  christos 				path=path)
    186           1.1  christos 	bus.add_signal_receiver(propertiesChangedInterface,
    187           1.1  christos 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    188           1.1  christos 				signal_name="PropertiesChanged",
    189           1.1  christos 				path=path)
    190           1.1  christos 
    191           1.1  christos 	bus.add_signal_receiver(propertiesChangedBss,
    192           1.1  christos 				dbus_interface=WPAS_DBUS_BSS_INTERFACE,
    193           1.1  christos 				signal_name="PropertiesChanged")
    194           1.1  christos 
    195           1.1  christos 	bus.add_signal_receiver(propertiesChangedNetwork,
    196           1.1  christos 				dbus_interface=WPAS_DBUS_NETWORK_INTERFACE,
    197           1.1  christos 				signal_name="PropertiesChanged")
    198           1.1  christos 
    199           1.1  christos 	gobject.MainLoop().run()
    200           1.1  christos 
    201           1.1  christos if __name__ == "__main__":
    202           1.1  christos 	main()
    203           1.1  christos 
    204