p2p_invite.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #!/usr/bin/python
  2. # Tests p2p_invite
  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> -a <addr> \ " \
  14. % sys.argv[0]
  15. print " [-o <persistent_group_object>] [-w <wpas_dbus_interface>]"
  16. print "Options:"
  17. print " -i = interface name"
  18. print " -a = address of peer"
  19. print " -o = persistent group object path"
  20. print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
  21. print "Example:"
  22. print " %s -i p2p-wlan0-0 -a 00150083523c" % sys.argv[0]
  23. # Required Signals
  24. def InvitationResult(invite_result):
  25. print "Inviation Result signal :"
  26. status = invite_result['status']
  27. print "status = ", status
  28. if invite_result.has_key('BSSID'):
  29. bssid = invite_result['BSSID']
  30. print "BSSID = ", hex(bssid[0]) , ":" , \
  31. hex(bssid[1]) , ":" , hex(bssid[2]) , ":", \
  32. hex(bssid[3]) , ":" , hex(bssid[4]) , ":" , \
  33. hex(bssid[5])
  34. os._exit(0)
  35. class P2P_Invite (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 addr
  46. global persistent_group_object
  47. # Dbus Paths
  48. global wpas_dbus_opath
  49. global wpas_dbus_interfaces_opath
  50. global wpas_dbus_interfaces_interface
  51. global wpas_dbus_interfaces_p2pdevice
  52. # Arguements
  53. global P2PDictionary
  54. # Constructor
  55. def __init__(self,interface_name,wpas_dbus_interface,addr,
  56. persistent_group_object):
  57. # Initializes variables and threads
  58. self.interface_name = interface_name
  59. self.wpas_dbus_interface = wpas_dbus_interface
  60. self.addr = addr
  61. self.persistent_group_object = persistent_group_object
  62. # Initializes thread and daemon allows for ctrl-c kill
  63. threading.Thread.__init__(self)
  64. self.daemon = True
  65. # Generating interface/object paths
  66. self.wpas_dbus_opath = "/" + \
  67. self.wpas_dbus_interface.replace(".","/")
  68. self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \
  69. "/Interfaces"
  70. self.wpas_dbus_interfaces_interface = \
  71. self.wpas_dbus_interface + ".Interface"
  72. self.wpas_dbus_interfaces_p2pdevice = \
  73. self.wpas_dbus_interfaces_interface \
  74. + ".P2PDevice"
  75. # Getting interfaces and objects
  76. DBusGMainLoop(set_as_default=True)
  77. self.bus = dbus.SystemBus()
  78. self.wpas_object = self.bus.get_object(
  79. self.wpas_dbus_interface,
  80. self.wpas_dbus_opath)
  81. self.wpas = dbus.Interface(self.wpas_object,
  82. self.wpas_dbus_interface)
  83. # Try to see if supplicant knows about interface
  84. # If not, throw an exception
  85. try:
  86. self.path = self.wpas.GetInterface(
  87. self.interface_name)
  88. except dbus.DBusException, exc:
  89. error = 'Error:\n Interface ' + self.interface_name \
  90. + ' was not found'
  91. print error
  92. usage()
  93. os._exit(0)
  94. self.interface_object = self.bus.get_object(
  95. self.wpas_dbus_interface, self.path)
  96. self.p2p_interface = dbus.Interface(self.interface_object,
  97. self.wpas_dbus_interfaces_p2pdevice)
  98. #Adds listeners
  99. self.bus.add_signal_receiver(InvitationResult,
  100. dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
  101. signal_name="InvitationResult")
  102. # Sets up p2p_invite dictionary
  103. def constructArguements(self):
  104. self.P2PDictionary = \
  105. {'peer':dbus.ObjectPath(self.path+'/Peers/'+self.addr)}
  106. if (self.persistent_group_object != None):
  107. self.P2PDictionary.update({"persistent_group_object":
  108. self.persistent_group_object})
  109. # Run p2p_invite
  110. def run(self):
  111. try:
  112. self.p2p_interface.Invite(self.P2PDictionary)
  113. except:
  114. print "Error:\n Invalid Arguements"
  115. usage()
  116. os._exit(0)
  117. # Allows other threads to keep working while MainLoop runs
  118. # Required for timeout implementation
  119. gobject.MainLoop().get_context().iteration(True)
  120. gobject.threads_init()
  121. gobject.MainLoop().run()
  122. if __name__ == "__main__":
  123. # Defaults for optional inputs
  124. addr = None
  125. persistent_group_object = None
  126. wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
  127. # interface_name is required
  128. interface_name = None
  129. # Using getopts to handle options
  130. try:
  131. options, args = getopt.getopt(sys.argv[1:],"hi:o:w:a:")
  132. except getopt.GetoptError:
  133. usage()
  134. quit()
  135. # If theres a switch, override default option
  136. for key, value in options:
  137. # Help
  138. if (key == "-h"):
  139. usage()
  140. quit()
  141. # Interface Name
  142. elif (key == "-i"):
  143. interface_name = value
  144. elif (key == "-a"):
  145. addr = value
  146. # Persistent group object path
  147. elif (key == "-o"):
  148. persistent_group_object = value
  149. # Dbus interface
  150. elif (key == "-w"):
  151. wpas_dbus_interface = value
  152. else:
  153. assert False, "unhandled option"
  154. # Interface name is required and was not given
  155. if (interface_name == None):
  156. print "Error:\n interface_name is required"
  157. usage()
  158. quit()
  159. if (addr == None):
  160. print "Error:\n peer address is required"
  161. usage()
  162. quit()
  163. try:
  164. p2p_invite_test = \
  165. P2P_Invite(interface_name,wpas_dbus_interface,
  166. addr,persistent_group_object)
  167. except:
  168. print "Error:\n Invalid Arguements"
  169. usage()
  170. os._exit(1)
  171. p2p_invite_test.constructArguements()
  172. p2p_invite_test.start()
  173. time.sleep(10)
  174. print "Error:\n p2p_invite timed out"
  175. os._exit(0)