p2p_find.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/python
  2. # Tests p2p_find
  3. # Will list all devices found/lost within a time frame (timeout)
  4. # Then Program will exit
  5. ######### MAY NEED TO RUN AS SUDO #############
  6. import dbus
  7. import sys, os
  8. import time
  9. import gobject
  10. import threading
  11. import getopt
  12. from dbus.mainloop.glib import DBusGMainLoop
  13. def usage():
  14. print "Usage:"
  15. print " %s -i <interface_name> [-t <timeout>] \ " \
  16. % sys.argv[0]
  17. print " [-w <wpas_dbus_interface>]"
  18. print "Options:"
  19. print " -i = interface name"
  20. print " -t = timeout = 0s (infinite)"
  21. print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
  22. print "Example:"
  23. print " %s -i wlan0 -t 10" % sys.argv[0]
  24. # Required Signals
  25. def deviceFound(devicepath):
  26. print "Device found: %s" % (devicepath)
  27. def deviceLost(devicepath):
  28. print "Device lost: %s" % (devicepath)
  29. class P2P_Find (threading.Thread):
  30. # Needed Variables
  31. global bus
  32. global wpas_object
  33. global interface_object
  34. global p2p_interface
  35. global interface_name
  36. global wpas
  37. global wpas_dbus_interface
  38. global timeout
  39. global path
  40. # Dbus Paths
  41. global wpas_dbus_opath
  42. global wpas_dbus_interfaces_opath
  43. global wpas_dbus_interfaces_interface
  44. global wpas_dbus_interfaces_p2pdevice
  45. # Constructor
  46. def __init__(self,interface_name,wpas_dbus_interface,timeout):
  47. # Initializes variables and threads
  48. self.timeout = int(timeout)
  49. self.interface_name = interface_name
  50. self.wpas_dbus_interface = wpas_dbus_interface
  51. # Initializes thread and daemon allows for ctrl-c kill
  52. threading.Thread.__init__(self)
  53. self.daemon = True
  54. # Generating interface/object paths
  55. self.wpas_dbus_opath = "/" + \
  56. self.wpas_dbus_interface.replace(".","/")
  57. self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \
  58. "/Interfaces"
  59. self.wpas_dbus_interfaces_interface = \
  60. self.wpas_dbus_interface + ".Interface"
  61. self.wpas_dbus_interfaces_p2pdevice = \
  62. self.wpas_dbus_interfaces_interface \
  63. + ".P2PDevice"
  64. # Getting interfaces and objects
  65. DBusGMainLoop(set_as_default=True)
  66. self.bus = dbus.SystemBus()
  67. self.wpas_object = self.bus.get_object(
  68. self.wpas_dbus_interface,
  69. self.wpas_dbus_opath)
  70. self.wpas = dbus.Interface(self.wpas_object,
  71. self.wpas_dbus_interface)
  72. # Try to see if supplicant knows about interface
  73. # If not, throw an exception
  74. try:
  75. self.path = self.wpas.GetInterface(
  76. self.interface_name)
  77. except dbus.DBusException, exc:
  78. error = 'Error:\n Interface ' + self.interface_name \
  79. + ' was not found'
  80. print error
  81. usage()
  82. os._exit(0)
  83. self.interface_object = self.bus.get_object(
  84. self.wpas_dbus_interface, self.path)
  85. self.p2p_interface = dbus.Interface(self.interface_object,
  86. self.wpas_dbus_interfaces_p2pdevice)
  87. #Adds listeners for find and lost
  88. self.bus.add_signal_receiver(deviceFound,
  89. dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
  90. signal_name="DeviceFound")
  91. self.bus.add_signal_receiver(deviceLost,
  92. dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
  93. signal_name="DeviceLost")
  94. # Sets up p2p_find
  95. P2PFindDict = dbus.Dictionary(
  96. {'Timeout':int(self.timeout)})
  97. self.p2p_interface.Find(P2PFindDict)
  98. # Run p2p_find
  99. def run(self):
  100. # Allows other threads to keep working while MainLoop runs
  101. # Required for timeout implementation
  102. gobject.MainLoop().get_context().iteration(True)
  103. gobject.threads_init()
  104. gobject.MainLoop().run()
  105. if __name__ == "__main__":
  106. # Defaults for optional inputs
  107. timeout = 0
  108. wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
  109. # interface_name is required
  110. interface_name = None
  111. # Using getopts to handle options
  112. try:
  113. options, args = getopt.getopt(sys.argv[1:],"hi:t:w:")
  114. except getopt.GetoptError:
  115. usage()
  116. quit()
  117. # If theres a switch, override default option
  118. for key, value in options:
  119. # Help
  120. if (key == "-h"):
  121. usage()
  122. quit()
  123. # Interface Name
  124. elif (key == "-i"):
  125. interface_name = value
  126. # Timeout
  127. elif (key == "-t"):
  128. if ( int(value) >= 0):
  129. timeout = value
  130. else:
  131. print "Error:\n Timeout cannot be negative"
  132. usage()
  133. quit()
  134. # Dbus interface
  135. elif (key == "-w"):
  136. wpas_dbus_interface = value
  137. else:
  138. assert False, "unhandled option"
  139. # Interface name is required and was not given
  140. if (interface_name == None):
  141. print "Error:\n interface_name is required"
  142. usage()
  143. quit()
  144. # Constructor
  145. try:
  146. p2p_find_test = P2P_Find(interface_name, wpas_dbus_interface, timeout)
  147. except:
  148. print "Error:\n Invalid wpas_dbus_interface"
  149. usage()
  150. quit()
  151. # Start P2P_Find
  152. p2p_find_test.start()
  153. try:
  154. # If timeout is 0, then run forever
  155. if (timeout == 0):
  156. while(True):
  157. pass
  158. # Else sleep for (timeout)
  159. else:
  160. time.sleep(p2p_find_test.timeout)
  161. except:
  162. pass
  163. quit()