Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / WirelessManager / wpa_supplicant / examples / wpas-dbus-new.py
blob25072ce9a2dfb82872f5a166f40064710401e776
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"
17 def byte_array_to_string(s):
18 import urllib
19 r = ""
20 for c in s:
21 if c >= 32 and c < 127:
22 r += "%c" % c
23 else:
24 r += urllib.quote(chr(c))
25 return r
27 def list_interfaces(wpas_obj):
28 ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
29 dbus_interface=dbus.PROPERTIES_IFACE)
30 for path in ifaces:
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)
34 print ifname
36 def propertiesChanged(properties):
37 if properties.has_key("State"):
38 print "PropertiesChanged: State: %s" % (properties["State"])
40 def showBss(bss):
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)
47 bssid = ""
48 for item in val:
49 bssid = bssid + ":%02x" % item
50 bssid = bssid[1:]
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)
57 wpa = "no"
58 if len(val["KeyMgmt"]) > 0:
59 wpa = "yes"
60 val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSN',
61 dbus_interface=dbus.PROPERTIES_IFACE)
62 wpa2 = "no"
63 if len(val["KeyMgmt"]) > 0:
64 wpa2 = "yes"
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)
71 if len(val) > 0:
72 maxrate = val[0] / 1000000
73 else:
74 maxrate = 0
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:"
85 for opath in res:
86 print opath
87 showBss(opath)
89 def bssAdded(bss, properties):
90 print "BSS added: %s" % (bss)
91 showBss(bss)
93 def bssRemoved(bss):
94 print "BSS removed: %s" % (bss)
96 def main():
97 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
98 global bus
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)
104 os._exit(1)
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")
120 ifname = sys.argv[1]
122 # See if wpa_supplicant already knows about this interface
123 path = None
124 try:
125 path = wpas.GetInterface(ifname)
126 except dbus.DBusException, exc:
127 if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceUnknown:"):
128 raise exc
129 try:
130 path = wpas.CreateInterface({'Ifname': ifname, 'Driver': 'test'})
131 time.sleep(1)
133 except dbus.DBusException, exc:
134 if not str(exc).startswith("fi.w1.wpa_supplicant1.InterfaceExists:"):
135 raise exc
137 global if_obj
138 if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
139 global iface
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__":
148 main()