p2p_stop_find.py 4.2 KB

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