wpas-dbus-new.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 import dbus
00004 import sys, os
00005 import time
00006 import gobject
00007 from dbus.mainloop.glib import DBusGMainLoop
00008 
00009 WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"
00010 WPAS_DBUS_INTERFACE = "fi.w1.wpa_supplicant1"
00011 WPAS_DBUS_OPATH = "/fi/w1/wpa_supplicant1"
00012 
00013 WPAS_DBUS_INTERFACES_INTERFACE = "fi.w1.wpa_supplicant1.Interface"
00014 WPAS_DBUS_INTERFACES_OPATH = "/fi/w1/wpa_supplicant1/Interfaces"
00015 WPAS_DBUS_BSS_INTERFACE = "fi.w1.wpa_supplicant1.BSS"
00016 
00017 def byte_array_to_string(s):
00018         import urllib
00019         r = ""    
00020         for c in s:
00021                 if c >= 32 and c < 127:
00022                         r += "%c" % c
00023                 else:
00024                         r += urllib.quote(chr(c))
00025         return r
00026 
00027 def list_interfaces(wpas_obj):
00028         ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
00029                               dbus_interface=dbus.PROPERTIES_IFACE)
00030         for path in ifaces:
00031                 if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
00032                 ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname',
00033                               dbus_interface=dbus.PROPERTIES_IFACE)
00034                 print ifname
00035 
00036 def propertiesChanged(properties):
00037         if properties.has_key("State"):
00038                 print "PropertiesChanged: State: %s" % (properties["State"])
00039 
00040 def showBss(bss):
00041         net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss)
00042         net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
00043 
00044         # Convert the byte-array for SSID and BSSID to printable strings
00045         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID',
00046                           dbus_interface=dbus.PROPERTIES_IFACE)
00047         bssid = ""
00048         for item in val:
00049                 bssid = bssid + ":%02x" % item
00050         bssid = bssid[1:]
00051         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID',
00052                           dbus_interface=dbus.PROPERTIES_IFACE)
00053         ssid = byte_array_to_string(val)
00054 
00055         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPA',
00056                           dbus_interface=dbus.PROPERTIES_IFACE)
00057         wpa = "no"
00058         if len(val["KeyMgmt"]) > 0:
00059                 wpa = "yes"
00060         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSN',
00061                           dbus_interface=dbus.PROPERTIES_IFACE)
00062         wpa2 = "no"
00063         if len(val["KeyMgmt"]) > 0:
00064                 wpa2 = "yes"
00065         freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency',
00066                            dbus_interface=dbus.PROPERTIES_IFACE)
00067         signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal',
00068                              dbus_interface=dbus.PROPERTIES_IFACE)
00069         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates',
00070                           dbus_interface=dbus.PROPERTIES_IFACE)
00071         if len(val) > 0:
00072                 maxrate = val[0] / 1000000
00073         else:
00074                 maxrate = 0
00075 
00076         print "  %s  ::  ssid='%s'  wpa=%s  wpa2=%s  signal=%d  rate=%d  freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq)
00077 
00078 def scanDone(success):
00079         print "Scan done: success=%s" % success
00080         
00081         res = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'BSSs',
00082                          dbus_interface=dbus.PROPERTIES_IFACE)
00083 
00084         print "Scanned wireless networks:"
00085         for opath in res:
00086                 print opath
00087                 showBss(opath)
00088 
00089 def bssAdded(bss, properties):
00090         print "BSS added: %s" % (bss)
00091         showBss(bss)
00092 
00093 def bssRemoved(bss):
00094         print "BSS removed: %s" % (bss)
00095 
00096 def main():
00097         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
00098         global bus
00099         bus = dbus.SystemBus()
00100         wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
00101 
00102         if len(sys.argv) != 2:
00103                 list_interfaces(wpas_obj)
00104                 os._exit(1)
00105 
00106         wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
00107         bus.add_signal_receiver(scanDone,
00108                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00109                                 signal_name="ScanDone")
00110         bus.add_signal_receiver(bssAdded,
00111                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00112                                 signal_name="BSSAdded")
00113         bus.add_signal_receiver(bssRemoved,
00114                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00115                                 signal_name="BSSRemoved")
00116         bus.add_signal_receiver(propertiesChanged,
00117                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00118                                 signal_name="PropertiesChanged")
00119 
00120         ifname = sys.argv[1]
00121 
00122         # See if wpa_supplicant already knows about this interface
00123         path = None
00124         try:
00125                 path = wpas.GetInterface(ifname)
00126         except dbus.DBusException, exc:
00127                 if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceUnknown:"):
00128                         raise exc
00129                 try:
00130                         path = wpas.CreateInterface({'Ifname': ifname, 'Driver': 'test'})
00131                         time.sleep(1)
00132 
00133                 except dbus.DBusException, exc:
00134                         if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceExists:"):
00135                                 raise exc
00136 
00137         global if_obj
00138         if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
00139         global iface
00140         iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
00141         iface.Scan({'Type': 'active'})
00142 
00143         gobject.MainLoop().run()
00144 
00145         wpas.RemoveInterface(dbus.ObjectPath(path))
00146 
00147 if __name__ == "__main__":
00148         main()
00149 


wpa_supplicant_node
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:33:21