wpas-dbus-new-signals.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 WPAS_DBUS_NETWORK_INTERFACE = "fi.w1.wpa_supplicant1.Network"
00017 
00018 def byte_array_to_string(s):
00019         import urllib
00020         r = ""    
00021         for c in s:
00022                 if c >= 32 and c < 127:
00023                         r += "%c" % c
00024                 else:
00025                         r += urllib.quote(chr(c))
00026         return r
00027 
00028 def list_interfaces(wpas_obj):
00029         ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
00030                               dbus_interface=dbus.PROPERTIES_IFACE)
00031         for path in ifaces:
00032                 if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
00033                 ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname',
00034                               dbus_interface=dbus.PROPERTIES_IFACE)
00035                 print ifname
00036 
00037 def interfaceAdded(interface, properties):
00038         print "InterfaceAdded(%s): Ifname=%s" % (interface, properties['Ifname'])
00039 
00040 def interfaceRemoved(interface):
00041         print "InterfaceRemoved(%s)" % (interface)
00042 
00043 def propertiesChanged(properties):
00044         for i in properties:
00045                 print "PropertiesChanged: %s=%s" % (i, properties[i])
00046 
00047 def showBss(bss):
00048         net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss)
00049         net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
00050 
00051         # Convert the byte-array for SSID and BSSID to printable strings
00052         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID',
00053                           dbus_interface=dbus.PROPERTIES_IFACE)
00054         bssid = ""
00055         for item in val:
00056                 bssid = bssid + ":%02x" % item
00057         bssid = bssid[1:]
00058         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID',
00059                           dbus_interface=dbus.PROPERTIES_IFACE)
00060         ssid = byte_array_to_string(val)
00061 
00062         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPAIE',
00063                           dbus_interface=dbus.PROPERTIES_IFACE)
00064         wpa = "no"
00065         if val != None:
00066                 wpa = "yes"
00067         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSNIE',
00068                           dbus_interface=dbus.PROPERTIES_IFACE)
00069         wpa2 = "no"
00070         if val != None:
00071                 wpa2 = "yes"
00072         freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency',
00073                            dbus_interface=dbus.PROPERTIES_IFACE)
00074         signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal',
00075                              dbus_interface=dbus.PROPERTIES_IFACE)
00076         val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates',
00077                           dbus_interface=dbus.PROPERTIES_IFACE)
00078         if len(val) > 0:
00079                 maxrate = val[0] / 1000000
00080         else:
00081                 maxrate = 0
00082 
00083         print "  %s  ::  ssid='%s'  wpa=%s  wpa2=%s  signal=%d  rate=%d  freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq)
00084 
00085 def scanDone(success):
00086         gobject.MainLoop().quit()
00087         print "Scan done: success=%s" % success
00088 
00089 def scanDone2(success, path=None):
00090         print "Scan done: success=%s [path=%s]" % (success, path)
00091 
00092 def bssAdded(bss, properties):
00093         print "BSS added: %s" % (bss)
00094         showBss(bss)
00095 
00096 def bssRemoved(bss):
00097         print "BSS removed: %s" % (bss)
00098 
00099 def blobAdded(blob):
00100         print "BlobAdded(%s)" % (blob)
00101 
00102 def blobRemoved(blob):
00103         print "BlobRemoved(%s)" % (blob)
00104 
00105 def networkAdded(network, properties):
00106         print "NetworkAdded(%s)" % (network)
00107 
00108 def networkRemoved(network):
00109         print "NetworkRemoved(%s)" % (network)
00110 
00111 def networkSelected(network):
00112         print "NetworkSelected(%s)" % (network)
00113 
00114 def propertiesChangedInterface(properties):
00115         for i in properties:
00116                 print "PropertiesChanged(interface): %s=%s" % (i, properties[i])
00117 
00118 def propertiesChangedBss(properties):
00119         for i in properties:
00120                 print "PropertiesChanged(BSS): %s=%s" % (i, properties[i])
00121 
00122 def propertiesChangedNetwork(properties):
00123         for i in properties:
00124                 print "PropertiesChanged(Network): %s=%s" % (i, properties[i])
00125 
00126 def main():
00127         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
00128         global bus
00129         bus = dbus.SystemBus()
00130         wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
00131 
00132         if len(sys.argv) != 2:
00133                 list_interfaces(wpas_obj)
00134                 os._exit(1)
00135 
00136         wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
00137         bus.add_signal_receiver(interfaceAdded,
00138                                 dbus_interface=WPAS_DBUS_INTERFACE,
00139                                 signal_name="InterfaceAdded")
00140         bus.add_signal_receiver(interfaceRemoved,
00141                                 dbus_interface=WPAS_DBUS_INTERFACE,
00142                                 signal_name="InterfaceRemoved")
00143         bus.add_signal_receiver(propertiesChanged,
00144                                 dbus_interface=WPAS_DBUS_INTERFACE,
00145                                 signal_name="PropertiesChanged")
00146 
00147         ifname = sys.argv[1]
00148         path = wpas.GetInterface(ifname)
00149         if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
00150         iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
00151         iface.connect_to_signal("ScanDone", scanDone2,
00152                                 path_keyword='path')
00153 
00154         bus.add_signal_receiver(scanDone,
00155                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00156                                 signal_name="ScanDone",
00157                                 path=path)
00158         bus.add_signal_receiver(bssAdded,
00159                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00160                                 signal_name="BSSAdded",
00161                                 path=path)
00162         bus.add_signal_receiver(bssRemoved,
00163                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00164                                 signal_name="BSSRemoved",
00165                                 path=path)
00166         bus.add_signal_receiver(blobAdded,
00167                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00168                                 signal_name="BlobAdded",
00169                                 path=path)
00170         bus.add_signal_receiver(blobRemoved,
00171                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00172                                 signal_name="BlobRemoved",
00173                                 path=path)
00174         bus.add_signal_receiver(networkAdded,
00175                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00176                                 signal_name="NetworkAdded",
00177                                 path=path)
00178         bus.add_signal_receiver(networkRemoved,
00179                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00180                                 signal_name="NetworkRemoved",
00181                                 path=path)
00182         bus.add_signal_receiver(networkSelected,
00183                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00184                                 signal_name="NetworkSelected",
00185                                 path=path)
00186         bus.add_signal_receiver(propertiesChangedInterface,
00187                                 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
00188                                 signal_name="PropertiesChanged",
00189                                 path=path)
00190 
00191         bus.add_signal_receiver(propertiesChangedBss,
00192                                 dbus_interface=WPAS_DBUS_BSS_INTERFACE,
00193                                 signal_name="PropertiesChanged")
00194 
00195         bus.add_signal_receiver(propertiesChangedNetwork,
00196                                 dbus_interface=WPAS_DBUS_NETWORK_INTERFACE,
00197                                 signal_name="PropertiesChanged")
00198 
00199         gobject.MainLoop().run()
00200 
00201 if __name__ == "__main__":
00202         main()
00203 


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:36