7 from dbus
.mainloop
.glib
import DBusGMainLoop
9 WPAS_DBUS_SERVICE
= "fi.w1.wpa_supplicant1"
10 WPAS_DBUS_INTERFACE
= "fi.w1.wpa_supplicant1"
11 WPAS_DBUS_OPATH
= "/fi/w1/wpa_supplicant1"
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"
17 def byte_array_to_string(s
):
21 if c
>= 32 and c
< 127:
24 r
+= urllib
.quote(chr(c
))
27 def list_interfaces(wpas_obj
):
28 ifaces
= wpas_obj
.Get(WPAS_DBUS_INTERFACE
, 'Interfaces',
29 dbus_interface
=dbus
.PROPERTIES_IFACE
)
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
)
36 def propertiesChanged(properties
):
37 if properties
.has_key("State"):
38 print "PropertiesChanged: State: %s" % (properties
["State"])
41 net_obj
= bus
.get_object(WPAS_DBUS_SERVICE
, bss
)
42 net
= dbus
.Interface(net_obj
, WPAS_DBUS_BSS_INTERFACE
)
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
)
49 bssid
= bssid
+ ":%02x" % item
51 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'SSID',
52 dbus_interface
=dbus
.PROPERTIES_IFACE
)
53 ssid
= byte_array_to_string(val
)
55 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'WPA',
56 dbus_interface
=dbus
.PROPERTIES_IFACE
)
58 if len(val
["KeyMgmt"]) > 0:
60 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'RSN',
61 dbus_interface
=dbus
.PROPERTIES_IFACE
)
63 if len(val
["KeyMgmt"]) > 0:
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
)
72 maxrate
= val
[0] / 1000000
76 print " %s :: ssid='%s' wpa=%s wpa2=%s signal=%d rate=%d freq=%d" % (bssid
, ssid
, wpa
, wpa2
, signal
, maxrate
, freq
)
78 def scanDone(success
):
79 print "Scan done: success=%s" % success
81 res
= if_obj
.Get(WPAS_DBUS_INTERFACES_INTERFACE
, 'BSSs',
82 dbus_interface
=dbus
.PROPERTIES_IFACE
)
84 print "Scanned wireless networks:"
89 def bssAdded(bss
, properties
):
90 print "BSS added: %s" % (bss
)
94 print "BSS removed: %s" % (bss
)
97 dbus
.mainloop
.glib
.DBusGMainLoop(set_as_default
=True)
99 bus
= dbus
.SystemBus()
100 wpas_obj
= bus
.get_object(WPAS_DBUS_SERVICE
, WPAS_DBUS_OPATH
)
102 if len(sys
.argv
) != 2:
103 list_interfaces(wpas_obj
)
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")
122 # See if wpa_supplicant already knows about this interface
125 path
= wpas
.GetInterface(ifname
)
126 except dbus
.DBusException
, exc
:
127 if not str(exc
).startswith("fi.w1.wpa_supplicant1.InterfaceUnknown:"):
130 path
= wpas
.CreateInterface({'Ifname': ifname
, 'Driver': 'test'})
133 except dbus
.DBusException
, exc
:
134 if not str(exc
).startswith("fi.w1.wpa_supplicant1.InterfaceExists:"):
138 if_obj
= bus
.get_object(WPAS_DBUS_SERVICE
, path
)
140 iface
= dbus
.Interface(if_obj
, WPAS_DBUS_INTERFACES_INTERFACE
)
141 iface
.Scan({'Type': 'active'})
143 gobject
.MainLoop().run()
145 wpas
.RemoveInterface(dbus
.ObjectPath(path
))
147 if __name__
== "__main__":