3 ######### MAY NEED TO RUN AS SUDO #############
11 from dbus.mainloop.glib import DBusGMainLoop
15 print " %s -i <interface_name> -a <addr> \ " \
17 print " [-o <persistent_group_object>] [-w <wpas_dbus_interface>]"
19 print " -i = interface name"
20 print " -a = address of peer"
21 print " -o = persistent group object path"
22 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
24 print " %s -i p2p-wlan0-0 -a 00150083523c" % sys.argv[0]
27 def InvitationResult(invite_result):
28 print "Inviation Result signal :"
29 status = invite_result['status']
30 print "status = ", status
31 if invite_result.has_key('BSSID'):
32 bssid = invite_result['BSSID']
33 print "BSSID = ", hex(bssid[0]) , ":" , \
34 hex(bssid[1]) , ":" , hex(bssid[2]) , ":", \
35 hex(bssid[3]) , ":" , hex(bssid[4]) , ":" , \
39 class P2P_Invite (threading.Thread):
43 global interface_object
47 global wpas_dbus_interface
50 global persistent_group_object
53 global wpas_dbus_opath
54 global wpas_dbus_interfaces_opath
55 global wpas_dbus_interfaces_interface
56 global wpas_dbus_interfaces_p2pdevice
62 def __init__(self,interface_name,wpas_dbus_interface,addr,
63 persistent_group_object):
64 # Initializes variables and threads
65 self.interface_name = interface_name
66 self.wpas_dbus_interface = wpas_dbus_interface
68 self.persistent_group_object = persistent_group_object
70 # Initializes thread and daemon allows for ctrl-c kill
71 threading.Thread.__init__(self)
74 # Generating interface/object paths
75 self.wpas_dbus_opath = "/" + \
76 self.wpas_dbus_interface.replace(".","/")
77 self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \
79 self.wpas_dbus_interfaces_interface = \
80 self.wpas_dbus_interface + ".Interface"
81 self.wpas_dbus_interfaces_p2pdevice = \
82 self.wpas_dbus_interfaces_interface \
85 # Getting interfaces and objects
86 DBusGMainLoop(set_as_default=True)
87 self.bus = dbus.SystemBus()
88 self.wpas_object = self.bus.get_object(
89 self.wpas_dbus_interface,
91 self.wpas = dbus.Interface(self.wpas_object,
92 self.wpas_dbus_interface)
94 # Try to see if supplicant knows about interface
95 # If not, throw an exception
97 self.path = self.wpas.GetInterface(
99 except dbus.DBusException, exc:
100 error = 'Error:\n Interface ' + self.interface_name \
106 self.interface_object = self.bus.get_object(
107 self.wpas_dbus_interface, self.path)
108 self.p2p_interface = dbus.Interface(self.interface_object,
109 self.wpas_dbus_interfaces_p2pdevice)
112 self.bus.add_signal_receiver(InvitationResult,
113 dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
114 signal_name="InvitationResult")
116 # Sets up p2p_invite dictionary
117 def constructArguements(self):
118 self.P2PDictionary = \
119 {'peer':dbus.ObjectPath(self.path+'/Peers/'+self.addr)}
120 if (self.persistent_group_object != None):
121 self.P2PDictionary.update({"persistent_group_object":
122 self.persistent_group_object})
127 self.p2p_interface.Invite(self.P2PDictionary)
130 print "Error:\n Invalid Arguements"
134 # Allows other threads to keep working while MainLoop runs
135 # Required for timeout implementation
136 gobject.MainLoop().get_context().iteration(True)
137 gobject.threads_init()
138 gobject.MainLoop().run()
140 if __name__ == "__main__":
141 # Defaults for optional inputs
143 persistent_group_object = None
144 wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
146 # interface_name is required
147 interface_name = None
149 # Using getopts to handle options
151 options, args = getopt.getopt(sys.argv[1:],"hi:o:w:a:")
153 except getopt.GetoptError:
157 # If theres a switch, override default option
158 for key, value in options:
165 interface_name = value
168 # Persistent group object path
170 persistent_group_object = value
173 wpas_dbus_interface = value
175 assert False, "unhandled option"
177 # Interface name is required and was not given
178 if (interface_name == None):
179 print "Error:\n interface_name is required"
184 print "Error:\n peer address is required"
190 P2P_Invite(interface_name,wpas_dbus_interface,
191 addr,persistent_group_object)
193 print "Error:\n Invalid Arguements"
197 p2p_invite_test.constructArguements()
198 p2p_invite_test.start()
200 print "Error:\n p2p_invite timed out"