Added WirelessManager, a port of wpa_supplicant.
[AROS.git] / workbench / network / WirelessManager / wpa_supplicant / examples / wpas-dbus-new-signals.py
blobb040e0a6070379208bf660d7fa9364847d68b414
1 #!/usr/bin/python
3 import dbus
4 import sys, os
5 import time
6 import gobject
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):
19 import urllib
20 r = ""
21 for c in s:
22 if c >= 32 and c < 127:
23 r += "%c" % c
24 else:
25 r += urllib.quote(chr(c))
26 return r
28 def list_interfaces(wpas_obj):
29 ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
30 dbus_interface=dbus.PROPERTIES_IFACE)
31 for path in ifaces:
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)
35 print ifname
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):
44 for i in properties:
45 print "PropertiesChanged: %s=%s" % (i, properties[i])
47 def showBss(bss):
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)
54 bssid = ""
55 for item in val:
56 bssid = bssid + ":%02x" % item
57 bssid = bssid[1:]
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)
64 wpa = "no"
65 if val != None:
66 wpa = "yes"
67 val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSNIE',
68 dbus_interface=dbus.PROPERTIES_IFACE)
69 wpa2 = "no"
70 if val != None:
71 wpa2 = "yes"
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)
78 if len(val) > 0:
79 maxrate = val[0] / 1000000
80 else:
81 maxrate = 0
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)
94 showBss(bss)
96 def bssRemoved(bss):
97 print "BSS removed: %s" % (bss)
99 def blobAdded(blob):
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):
115 for i in properties:
116 print "PropertiesChanged(interface): %s=%s" % (i, properties[i])
118 def propertiesChangedBss(properties):
119 for i in properties:
120 print "PropertiesChanged(BSS): %s=%s" % (i, properties[i])
122 def propertiesChangedNetwork(properties):
123 for i in properties:
124 print "PropertiesChanged(Network): %s=%s" % (i, properties[i])
126 def main():
127 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
128 global bus
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)
134 os._exit(1)
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")
147 ifname = sys.argv[1]
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,
152 path_keyword='path')
154 bus.add_signal_receiver(scanDone,
155 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
156 signal_name="ScanDone",
157 path=path)
158 bus.add_signal_receiver(bssAdded,
159 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
160 signal_name="BSSAdded",
161 path=path)
162 bus.add_signal_receiver(bssRemoved,
163 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
164 signal_name="BSSRemoved",
165 path=path)
166 bus.add_signal_receiver(blobAdded,
167 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
168 signal_name="BlobAdded",
169 path=path)
170 bus.add_signal_receiver(blobRemoved,
171 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
172 signal_name="BlobRemoved",
173 path=path)
174 bus.add_signal_receiver(networkAdded,
175 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
176 signal_name="NetworkAdded",
177 path=path)
178 bus.add_signal_receiver(networkRemoved,
179 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
180 signal_name="NetworkRemoved",
181 path=path)
182 bus.add_signal_receiver(networkSelected,
183 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
184 signal_name="NetworkSelected",
185 path=path)
186 bus.add_signal_receiver(propertiesChangedInterface,
187 dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
188 signal_name="PropertiesChanged",
189 path=path)
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__":
202 main()