p2p_group_add.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/usr/bin/python
  2. # Tests p2p_group_add
  3. ######### MAY NEED TO RUN AS SUDO #############
  4. import dbus
  5. import sys, os
  6. import time
  7. import gobject
  8. import getopt
  9. import threading
  10. from dbus.mainloop.glib import DBusGMainLoop
  11. def usage():
  12. print "Usage:"
  13. print " %s -i <interface_name> [-p <persistent>] \ " \
  14. % sys.argv[0]
  15. print " [-f <frequency>] [-o <group_object_path>] \ "
  16. print " [-w <wpas_dbus_interface>]"
  17. print "Options:"
  18. print " -i = interface name"
  19. print " -p = persistant group = 0 (0=false, 1=true)"
  20. print " -f = frequency"
  21. print " -o = persistent group object path"
  22. print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
  23. print "Example:"
  24. print " %s -i wlan0" % sys.argv[0]
  25. # Required Signals
  26. def GroupStarted(properties):
  27. if properties.has_key("group_object"):
  28. print 'Group Formation Complete %s' \
  29. % properties["group_object"]
  30. os._exit(0)
  31. def WpsFailure(status, etc):
  32. print "WPS Authentication Failure".format(status)
  33. print etc
  34. os._exit(0)
  35. class P2P_Group_Add (threading.Thread):
  36. # Needed Variables
  37. global bus
  38. global wpas_object
  39. global interface_object
  40. global p2p_interface
  41. global interface_name
  42. global wpas
  43. global wpas_dbus_interface
  44. global path
  45. global persistent
  46. global frequency
  47. global persistent_group_object
  48. # Dbus Paths
  49. global wpas_dbus_opath
  50. global wpas_dbus_interfaces_opath
  51. global wpas_dbus_interfaces_interface
  52. global wpas_dbus_interfaces_p2pdevice
  53. # Arguements
  54. global P2PDictionary
  55. # Constructor
  56. def __init__(self,interface_name,wpas_dbus_interface,persistent,frequency,
  57. persistent_group_object):
  58. # Initializes variables and threads
  59. self.interface_name = interface_name
  60. self.wpas_dbus_interface = wpas_dbus_interface
  61. self.persistent = persistent
  62. self.frequency = frequency
  63. self.persistent_group_object = persistent_group_object
  64. # Initializes thread and daemon allows for ctrl-c kill
  65. threading.Thread.__init__(self)
  66. self.daemon = True
  67. # Generating interface/object paths
  68. self.wpas_dbus_opath = "/" + \
  69. self.wpas_dbus_interface.replace(".","/")
  70. self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \
  71. "/Interfaces"
  72. self.wpas_dbus_interfaces_interface = \
  73. self.wpas_dbus_interface + ".Interface"
  74. self.wpas_dbus_interfaces_p2pdevice = \
  75. self.wpas_dbus_interfaces_interface \
  76. + ".P2PDevice"
  77. # Getting interfaces and objects
  78. DBusGMainLoop(set_as_default=True)
  79. self.bus = dbus.SystemBus()
  80. self.wpas_object = self.bus.get_object(
  81. self.wpas_dbus_interface,
  82. self.wpas_dbus_opath)
  83. self.wpas = dbus.Interface(self.wpas_object,
  84. self.wpas_dbus_interface)
  85. # Try to see if supplicant knows about interface
  86. # If not, throw an exception
  87. try:
  88. self.path = self.wpas.GetInterface(
  89. self.interface_name)
  90. except dbus.DBusException, exc:
  91. error = 'Error:\n Interface ' + self.interface_name \
  92. + ' was not found'
  93. print error
  94. usage()
  95. os._exit(0)
  96. self.interface_object = self.bus.get_object(
  97. self.wpas_dbus_interface, self.path)
  98. self.p2p_interface = dbus.Interface(self.interface_object,
  99. self.wpas_dbus_interfaces_p2pdevice)
  100. #Adds listeners
  101. self.bus.add_signal_receiver(GroupStarted,
  102. dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
  103. signal_name="GroupStarted")
  104. self.bus.add_signal_receiver(WpsFailure,
  105. dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
  106. signal_name="WpsFailed")
  107. # Sets up p2p_group_add dictionary
  108. def constructArguements(self):
  109. self.P2PDictionary = {'persistent':self.persistent}
  110. if (self.frequency != None):
  111. if (int(self.frequency) > 0):
  112. self.P2PDictionary.update({'frequency':int(self.frequency)})
  113. else:
  114. print "Error:\n Frequency must be greater than 0"
  115. usage()
  116. os._exit(0)
  117. if (self.persistent_group_object != None):
  118. self.P2PDictionary.update({'persistent_group_object':
  119. self.persistent_group_object})
  120. # Run p2p_group_remove
  121. def run(self):
  122. try:
  123. self.p2p_interface.GroupAdd(self.P2PDictionary)
  124. except:
  125. print "Error:\n Could not preform group add"
  126. usage()
  127. os._exit(0)
  128. # Allows other threads to keep working while MainLoop runs
  129. # Required for timeout implementation
  130. gobject.MainLoop().get_context().iteration(True)
  131. gobject.threads_init()
  132. gobject.MainLoop().run()
  133. if __name__ == "__main__":
  134. # Defaults for optional inputs
  135. # 0 = false, 1 = true
  136. persistent = False
  137. frequency = None
  138. persistent_group_object = None
  139. wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
  140. # interface_name is required
  141. interface_name = None
  142. # Using getopts to handle options
  143. try:
  144. options, args = getopt.getopt(sys.argv[1:],"hi:p:f:o:w:")
  145. except getopt.GetoptError:
  146. usage()
  147. quit()
  148. # If theres a switch, override default option
  149. for key, value in options:
  150. # Help
  151. if (key == "-h"):
  152. usage()
  153. quit()
  154. # Interface Name
  155. elif (key == "-i"):
  156. interface_name = value
  157. # Timeout
  158. elif (key == "-p"):
  159. if (value == '0'):
  160. persistent = False
  161. elif (value == '1'):
  162. persistent = True
  163. else:
  164. print "Error:\n Persistent can only be 1 or 0"
  165. usage()
  166. os._exit(0)
  167. # Frequency
  168. elif (key == "-f"):
  169. frequency = value
  170. # Persistent group object path
  171. elif (key == "-o"):
  172. persistent_group_object = value
  173. # Dbus interface
  174. elif (key == "-w"):
  175. wpas_dbus_interface = value
  176. else:
  177. assert False, "unhandled option"
  178. # Interface name is required and was not given
  179. if (interface_name == None):
  180. print "Error:\n interface_name is required"
  181. usage()
  182. quit()
  183. try:
  184. p2p_group_add_test = P2P_Group_Add(interface_name,wpas_dbus_interface,
  185. persistent,frequency,persistent_group_object)
  186. except:
  187. print "Error:\n Invalid Arguements"
  188. p2p_group_add_test.constructArguements()
  189. p2p_group_add_test.start()
  190. time.sleep(5)
  191. print "Error:\n Group formation timed out"
  192. os._exit(0)