Home | History | Annotate | Download | only in examples
      1 #!/usr/bin/python
      2 
      3 import dbus
      4 import sys, os
      5 import time
      6 import gobject
      7 from dbus.mainloop.glib import DBusGMainLoop
      8 
      9 WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"
     10 WPAS_DBUS_INTERFACE = "fi.w1.wpa_supplicant1"
     11 WPAS_DBUS_OPATH = "/fi/w1/wpa_supplicant1"
     12 
     13 WPAS_DBUS_INTERFACES_INTERFACE = "fi.w1.wpa_supplicant1.Interface"
     14 WPAS_DBUS_INTERFACES_OPATH = "/fi/w1/wpa_supplicant1/Interfaces"
     15 WPAS_DBUS_BSS_INTERFACE = "fi.w1.wpa_supplicant1.BSS"
     16 
     17 def byte_array_to_string(s):
     18 	import urllib
     19 	r = ""    
     20 	for c in s:
     21 		if c >= 32 and c < 127:
     22 			r += "%c" % c
     23 		else:
     24 			r += urllib.quote(chr(c))
     25 	return r
     26 
     27 def list_interfaces(wpas_obj):
     28 	ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
     29 			      dbus_interface=dbus.PROPERTIES_IFACE)
     30 	for path in ifaces:
     31 		if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
     32 		ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname',
     33 			      dbus_interface=dbus.PROPERTIES_IFACE)
     34 		print ifname
     35 
     36 def propertiesChanged(properties):
     37 	if properties.has_key("State"):
     38 		print "PropertiesChanged: State: %s" % (properties["State"])
     39 
     40 def showBss(bss):
     41 	net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss)
     42 	net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
     43 
     44 	# Convert the byte-array for SSID and BSSID to printable strings
     45 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID',
     46 			  dbus_interface=dbus.PROPERTIES_IFACE)
     47 	bssid = ""
     48 	for item in val:
     49 		bssid = bssid + ":%02x" % item
     50 	bssid = bssid[1:]
     51 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID',
     52 			  dbus_interface=dbus.PROPERTIES_IFACE)
     53 	ssid = byte_array_to_string(val)
     54 
     55 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPA',
     56 			  dbus_interface=dbus.PROPERTIES_IFACE)
     57 	wpa = "no"
     58 	if len(val["KeyMgmt"]) > 0:
     59 		wpa = "yes"
     60 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSN',
     61 			  dbus_interface=dbus.PROPERTIES_IFACE)
     62 	wpa2 = "no"
     63 	if len(val["KeyMgmt"]) > 0:
     64 		wpa2 = "yes"
     65 	freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency',
     66 			   dbus_interface=dbus.PROPERTIES_IFACE)
     67 	signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal',
     68 			     dbus_interface=dbus.PROPERTIES_IFACE)
     69 	val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates',
     70 			  dbus_interface=dbus.PROPERTIES_IFACE)
     71 	if len(val) > 0:
     72 		maxrate = val[0] / 1000000
     73 	else:
     74 		maxrate = 0
     75 
     76 	print "  %s  ::  ssid='%s'  wpa=%s  wpa2=%s  signal=%d  rate=%d  freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq)
     77 
     78 def scanDone(success):
     79 	print "Scan done: success=%s" % success
     80 	
     81 	res = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'BSSs',
     82 			 dbus_interface=dbus.PROPERTIES_IFACE)
     83 
     84 	print "Scanned wireless networks:"
     85 	for opath in res:
     86 		print opath
     87 		showBss(opath)
     88 
     89 def bssAdded(bss, properties):
     90 	print "BSS added: %s" % (bss)
     91 	showBss(bss)
     92 
     93 def bssRemoved(bss):
     94 	print "BSS removed: %s" % (bss)
     95 
     96 def main():
     97 	dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
     98 	global bus
     99 	bus = dbus.SystemBus()
    100 	wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
    101 
    102 	if len(sys.argv) != 2:
    103 		list_interfaces(wpas_obj)
    104 		os._exit(1)
    105 
    106 	wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
    107 	bus.add_signal_receiver(scanDone,
    108 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    109 				signal_name="ScanDone")
    110 	bus.add_signal_receiver(bssAdded,
    111 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    112 				signal_name="BSSAdded")
    113 	bus.add_signal_receiver(bssRemoved,
    114 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    115 				signal_name="BSSRemoved")
    116 	bus.add_signal_receiver(propertiesChanged,
    117 				dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
    118 				signal_name="PropertiesChanged")
    119 
    120 	ifname = sys.argv[1]
    121 
    122 	# See if wpa_supplicant already knows about this interface
    123 	path = None
    124 	try:
    125 		path = wpas.GetInterface(ifname)
    126 	except dbus.DBusException, exc:
    127 		if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceUnknown:"):
    128 			raise exc
    129 		try:
    130 			path = wpas.CreateInterface({'Ifname': ifname, 'Driver': 'test'})
    131 			time.sleep(1)
    132 
    133 		except dbus.DBusException, exc:
    134 			if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceExists:"):
    135 				raise exc
    136 
    137 	global if_obj
    138 	if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
    139 	global iface
    140 	iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
    141 	iface.Scan({'Type': 'active'})
    142 
    143 	gobject.MainLoop().run()
    144 
    145 	wpas.RemoveInterface(dbus.ObjectPath(path))
    146 
    147 if __name__ == "__main__":
    148 	main()
    149 
    150