p2p_flush.py 4.0 KB

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