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"
16 WPAS_DBUS_NETWORK_INTERFACE
= "fi.w1.wpa_supplicant1.Network"
18 def byte_array_to_string(s
):
22 if c
>= 32 and c
< 127:
25 r
+= urllib
.quote(chr(c
))
28 def list_interfaces(wpas_obj
):
29 ifaces
= wpas_obj
.Get(WPAS_DBUS_INTERFACE
, 'Interfaces',
30 dbus_interface
=dbus
.PROPERTIES_IFACE
)
32 if_obj
= bus
.get_object(WPAS_DBUS_SERVICE
, path
)
33 ifname
= if_obj
.Get(WPAS_DBUS_INTERFACES_INTERFACE
, 'Ifname',
34 dbus_interface
=dbus
.PROPERTIES_IFACE
)
37 def interfaceAdded(interface
, properties
):
38 print "InterfaceAdded(%s): Ifname=%s" % (interface
, properties
['Ifname'])
40 def interfaceRemoved(interface
):
41 print "InterfaceRemoved(%s)" % (interface
)
43 def propertiesChanged(properties
):
45 print "PropertiesChanged: %s=%s" % (i
, properties
[i
])
48 net_obj
= bus
.get_object(WPAS_DBUS_SERVICE
, bss
)
49 net
= dbus
.Interface(net_obj
, WPAS_DBUS_BSS_INTERFACE
)
51 # Convert the byte-array for SSID and BSSID to printable strings
52 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'BSSID',
53 dbus_interface
=dbus
.PROPERTIES_IFACE
)
56 bssid
= bssid
+ ":%02x" % item
58 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'SSID',
59 dbus_interface
=dbus
.PROPERTIES_IFACE
)
60 ssid
= byte_array_to_string(val
)
62 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'WPAIE',
63 dbus_interface
=dbus
.PROPERTIES_IFACE
)
67 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'RSNIE',
68 dbus_interface
=dbus
.PROPERTIES_IFACE
)
72 freq
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'Frequency',
73 dbus_interface
=dbus
.PROPERTIES_IFACE
)
74 signal
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'Signal',
75 dbus_interface
=dbus
.PROPERTIES_IFACE
)
76 val
= net_obj
.Get(WPAS_DBUS_BSS_INTERFACE
, 'Rates',
77 dbus_interface
=dbus
.PROPERTIES_IFACE
)
79 maxrate
= val
[0] / 1000000
83 print " %s :: ssid='%s' wpa=%s wpa2=%s signal=%d rate=%d freq=%d" % (bssid
, ssid
, wpa
, wpa2
, signal
, maxrate
, freq
)
85 def scanDone(success
):
86 gobject
.MainLoop().quit()
87 print "Scan done: success=%s" % success
89 def scanDone2(success
, path
=None):
90 print "Scan done: success=%s [path=%s]" % (success
, path
)
92 def bssAdded(bss
, properties
):
93 print "BSS added: %s" % (bss
)
97 print "BSS removed: %s" % (bss
)
100 print "BlobAdded(%s)" % (blob
)
102 def blobRemoved(blob
):
103 print "BlobRemoved(%s)" % (blob
)
105 def networkAdded(network
, properties
):
106 print "NetworkAdded(%s)" % (network
)
108 def networkRemoved(network
):
109 print "NetworkRemoved(%s)" % (network
)
111 def networkSelected(network
):
112 print "NetworkSelected(%s)" % (network
)
114 def propertiesChangedInterface(properties
):
116 print "PropertiesChanged(interface): %s=%s" % (i
, properties
[i
])
118 def propertiesChangedBss(properties
):
120 print "PropertiesChanged(BSS): %s=%s" % (i
, properties
[i
])
122 def propertiesChangedNetwork(properties
):
124 print "PropertiesChanged(Network): %s=%s" % (i
, properties
[i
])
127 dbus
.mainloop
.glib
.DBusGMainLoop(set_as_default
=True)
129 bus
= dbus
.SystemBus()
130 wpas_obj
= bus
.get_object(WPAS_DBUS_SERVICE
, WPAS_DBUS_OPATH
)
132 if len(sys
.argv
) != 2:
133 list_interfaces(wpas_obj
)
136 wpas
= dbus
.Interface(wpas_obj
, WPAS_DBUS_INTERFACE
)
137 bus
.add_signal_receiver(interfaceAdded
,
138 dbus_interface
=WPAS_DBUS_INTERFACE
,
139 signal_name
="InterfaceAdded")
140 bus
.add_signal_receiver(interfaceRemoved
,
141 dbus_interface
=WPAS_DBUS_INTERFACE
,
142 signal_name
="InterfaceRemoved")
143 bus
.add_signal_receiver(propertiesChanged
,
144 dbus_interface
=WPAS_DBUS_INTERFACE
,
145 signal_name
="PropertiesChanged")
148 path
= wpas
.GetInterface(ifname
)
149 if_obj
= bus
.get_object(WPAS_DBUS_SERVICE
, path
)
150 iface
= dbus
.Interface(if_obj
, WPAS_DBUS_INTERFACES_INTERFACE
)
151 iface
.connect_to_signal("ScanDone", scanDone2
,
154 bus
.add_signal_receiver(scanDone
,
155 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
156 signal_name
="ScanDone",
158 bus
.add_signal_receiver(bssAdded
,
159 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
160 signal_name
="BSSAdded",
162 bus
.add_signal_receiver(bssRemoved
,
163 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
164 signal_name
="BSSRemoved",
166 bus
.add_signal_receiver(blobAdded
,
167 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
168 signal_name
="BlobAdded",
170 bus
.add_signal_receiver(blobRemoved
,
171 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
172 signal_name
="BlobRemoved",
174 bus
.add_signal_receiver(networkAdded
,
175 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
176 signal_name
="NetworkAdded",
178 bus
.add_signal_receiver(networkRemoved
,
179 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
180 signal_name
="NetworkRemoved",
182 bus
.add_signal_receiver(networkSelected
,
183 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
184 signal_name
="NetworkSelected",
186 bus
.add_signal_receiver(propertiesChangedInterface
,
187 dbus_interface
=WPAS_DBUS_INTERFACES_INTERFACE
,
188 signal_name
="PropertiesChanged",
191 bus
.add_signal_receiver(propertiesChangedBss
,
192 dbus_interface
=WPAS_DBUS_BSS_INTERFACE
,
193 signal_name
="PropertiesChanged")
195 bus
.add_signal_receiver(propertiesChangedNetwork
,
196 dbus_interface
=WPAS_DBUS_NETWORK_INTERFACE
,
197 signal_name
="PropertiesChanged")
199 gobject
.MainLoop().run()
201 if __name__
== "__main__":