p2p_disconnect.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/python
  2. # Tests P2P_Disconnect
  3. # Will perform disconnect on interface_name
  4. ######### MAY NEED TO RUN AS SUDO #############
  5. import dbus
  6. import sys, os
  7. import time
  8. import gobject
  9. import threading
  10. import getopt
  11. from dbus.mainloop.glib import DBusGMainLoop
  12. def usage():
  13. print "Usage:"
  14. print " %s -i <interface_name> \ " \
  15. % sys.argv[0]
  16. print " [-w <wpas_dbus_interface>]"
  17. print "Options:"
  18. print " -i = interface name"
  19. print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
  20. print "Example:"
  21. print " %s -i p2p-wlan0-0" % sys.argv[0]
  22. # Required Signals
  23. def GroupFinished(status, etc):
  24. print "Disconnected"
  25. os._exit(0)
  26. class P2P_Disconnect (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(GroupFinished,
  86. dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
  87. signal_name="GroupFinished")
  88. # Runs p2p_disconnect
  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.Disconnect()
  95. gobject.MainLoop().run()
  96. if __name__ == "__main__":
  97. timeout = 5
  98. # Defaults for optional inputs
  99. wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
  100. # interface_name is required
  101. interface_name = None
  102. # Using getopts to handle options
  103. try:
  104. options, args = getopt.getopt(sys.argv[1:],"hi:w:")
  105. except getopt.GetoptError:
  106. usage()
  107. quit()
  108. # If theres a switch, override default option
  109. for key, value in options:
  110. # Help
  111. if (key == "-h"):
  112. usage()
  113. quit()
  114. # Interface Name
  115. elif (key == "-i"):
  116. interface_name = value
  117. # Dbus interface
  118. elif (key == "-w"):
  119. wpas_dbus_interface = value
  120. else:
  121. assert False, "unhandled option"
  122. # Interface name is required and was not given
  123. if (interface_name == None):
  124. print "Error:\n interface_name is required"
  125. usage()
  126. quit()
  127. # Constructor
  128. try:
  129. p2p_disconnect_test = P2P_Disconnect(interface_name,
  130. wpas_dbus_interface,timeout)
  131. except:
  132. print "Error:\n Invalid wpas_dbus_interface"
  133. usage()
  134. quit()
  135. # Start P2P_Disconnect
  136. p2p_disconnect_test.start()
  137. try:
  138. time.sleep(int(p2p_disconnect_test.timeout))
  139. except:
  140. pass
  141. print "Disconnect timed out"
  142. quit()