wpas-dbus-new-signals.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/usr/bin/python
  2. import dbus
  3. import sys, os
  4. import time
  5. import gobject
  6. from dbus.mainloop.glib import DBusGMainLoop
  7. WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"
  8. WPAS_DBUS_INTERFACE = "fi.w1.wpa_supplicant1"
  9. WPAS_DBUS_OPATH = "/fi/w1/wpa_supplicant1"
  10. WPAS_DBUS_INTERFACES_INTERFACE = "fi.w1.wpa_supplicant1.Interface"
  11. WPAS_DBUS_INTERFACES_OPATH = "/fi/w1/wpa_supplicant1/Interfaces"
  12. WPAS_DBUS_BSS_INTERFACE = "fi.w1.wpa_supplicant1.BSS"
  13. WPAS_DBUS_NETWORK_INTERFACE = "fi.w1.wpa_supplicant1.Network"
  14. def byte_array_to_string(s):
  15. import urllib
  16. r = ""
  17. for c in s:
  18. if c >= 32 and c < 127:
  19. r += "%c" % c
  20. else:
  21. r += urllib.quote(chr(c))
  22. return r
  23. def list_interfaces(wpas_obj):
  24. ifaces = wpas_obj.Get(WPAS_DBUS_INTERFACE, 'Interfaces',
  25. dbus_interface=dbus.PROPERTIES_IFACE)
  26. for path in ifaces:
  27. if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
  28. ifname = if_obj.Get(WPAS_DBUS_INTERFACES_INTERFACE, 'Ifname',
  29. dbus_interface=dbus.PROPERTIES_IFACE)
  30. print ifname
  31. def interfaceAdded(interface, properties):
  32. print "InterfaceAdded(%s): Ifname=%s" % (interface, properties['Ifname'])
  33. def interfaceRemoved(interface):
  34. print "InterfaceRemoved(%s)" % (interface)
  35. def propertiesChanged(properties):
  36. for i in properties:
  37. print "PropertiesChanged: %s=%s" % (i, properties[i])
  38. def showBss(bss):
  39. net_obj = bus.get_object(WPAS_DBUS_SERVICE, bss)
  40. net = dbus.Interface(net_obj, WPAS_DBUS_BSS_INTERFACE)
  41. # Convert the byte-array for SSID and BSSID to printable strings
  42. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'BSSID',
  43. dbus_interface=dbus.PROPERTIES_IFACE)
  44. bssid = ""
  45. for item in val:
  46. bssid = bssid + ":%02x" % item
  47. bssid = bssid[1:]
  48. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'SSID',
  49. dbus_interface=dbus.PROPERTIES_IFACE)
  50. ssid = byte_array_to_string(val)
  51. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'WPA',
  52. dbus_interface=dbus.PROPERTIES_IFACE)
  53. wpa = "no"
  54. if val != None:
  55. wpa = "yes"
  56. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'RSN',
  57. dbus_interface=dbus.PROPERTIES_IFACE)
  58. wpa2 = "no"
  59. if val != None:
  60. wpa2 = "yes"
  61. freq = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Frequency',
  62. dbus_interface=dbus.PROPERTIES_IFACE)
  63. signal = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Signal',
  64. dbus_interface=dbus.PROPERTIES_IFACE)
  65. val = net_obj.Get(WPAS_DBUS_BSS_INTERFACE, 'Rates',
  66. dbus_interface=dbus.PROPERTIES_IFACE)
  67. if len(val) > 0:
  68. maxrate = val[0] / 1000000
  69. else:
  70. maxrate = 0
  71. print " %s :: ssid='%s' wpa=%s wpa2=%s signal=%d rate=%d freq=%d" % (bssid, ssid, wpa, wpa2, signal, maxrate, freq)
  72. def scanDone(success):
  73. gobject.MainLoop().quit()
  74. print "Scan done: success=%s" % success
  75. def scanDone2(success, path=None):
  76. print "Scan done: success=%s [path=%s]" % (success, path)
  77. def bssAdded(bss, properties):
  78. print "BSS added: %s" % (bss)
  79. showBss(bss)
  80. def bssRemoved(bss):
  81. print "BSS removed: %s" % (bss)
  82. def blobAdded(blob):
  83. print "BlobAdded(%s)" % (blob)
  84. def blobRemoved(blob):
  85. print "BlobRemoved(%s)" % (blob)
  86. def networkAdded(network, properties):
  87. print "NetworkAdded(%s)" % (network)
  88. def networkRemoved(network):
  89. print "NetworkRemoved(%s)" % (network)
  90. def networkSelected(network):
  91. print "NetworkSelected(%s)" % (network)
  92. def propertiesChangedInterface(properties):
  93. for i in properties:
  94. print "PropertiesChanged(interface): %s=%s" % (i, properties[i])
  95. def propertiesChangedBss(properties):
  96. for i in properties:
  97. print "PropertiesChanged(BSS): %s=%s" % (i, properties[i])
  98. def propertiesChangedNetwork(properties):
  99. for i in properties:
  100. print "PropertiesChanged(Network): %s=%s" % (i, properties[i])
  101. def main():
  102. dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  103. global bus
  104. bus = dbus.SystemBus()
  105. wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_OPATH)
  106. if len(sys.argv) != 2:
  107. list_interfaces(wpas_obj)
  108. os._exit(1)
  109. wpas = dbus.Interface(wpas_obj, WPAS_DBUS_INTERFACE)
  110. bus.add_signal_receiver(interfaceAdded,
  111. dbus_interface=WPAS_DBUS_INTERFACE,
  112. signal_name="InterfaceAdded")
  113. bus.add_signal_receiver(interfaceRemoved,
  114. dbus_interface=WPAS_DBUS_INTERFACE,
  115. signal_name="InterfaceRemoved")
  116. bus.add_signal_receiver(propertiesChanged,
  117. dbus_interface=WPAS_DBUS_INTERFACE,
  118. signal_name="PropertiesChanged")
  119. ifname = sys.argv[1]
  120. path = wpas.GetInterface(ifname)
  121. if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
  122. iface = dbus.Interface(if_obj, WPAS_DBUS_INTERFACES_INTERFACE)
  123. iface.connect_to_signal("ScanDone", scanDone2,
  124. path_keyword='path')
  125. bus.add_signal_receiver(scanDone,
  126. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  127. signal_name="ScanDone",
  128. path=path)
  129. bus.add_signal_receiver(bssAdded,
  130. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  131. signal_name="BSSAdded",
  132. path=path)
  133. bus.add_signal_receiver(bssRemoved,
  134. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  135. signal_name="BSSRemoved",
  136. path=path)
  137. bus.add_signal_receiver(blobAdded,
  138. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  139. signal_name="BlobAdded",
  140. path=path)
  141. bus.add_signal_receiver(blobRemoved,
  142. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  143. signal_name="BlobRemoved",
  144. path=path)
  145. bus.add_signal_receiver(networkAdded,
  146. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  147. signal_name="NetworkAdded",
  148. path=path)
  149. bus.add_signal_receiver(networkRemoved,
  150. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  151. signal_name="NetworkRemoved",
  152. path=path)
  153. bus.add_signal_receiver(networkSelected,
  154. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  155. signal_name="NetworkSelected",
  156. path=path)
  157. bus.add_signal_receiver(propertiesChangedInterface,
  158. dbus_interface=WPAS_DBUS_INTERFACES_INTERFACE,
  159. signal_name="PropertiesChanged",
  160. path=path)
  161. bus.add_signal_receiver(propertiesChangedBss,
  162. dbus_interface=WPAS_DBUS_BSS_INTERFACE,
  163. signal_name="PropertiesChanged")
  164. bus.add_signal_receiver(propertiesChangedNetwork,
  165. dbus_interface=WPAS_DBUS_NETWORK_INTERFACE,
  166. signal_name="PropertiesChanged")
  167. gobject.MainLoop().run()
  168. if __name__ == "__main__":
  169. main()