3 ######### MAY NEED TO RUN AS SUDO #############
11 from dbus.mainloop.glib import DBusGMainLoop
15 print " %s -i <interface_name> [-p <persistent>] \ " \
17 print " [-f <frequency>] [-o <group_object_path>] \ "
18 print " [-w <wpas_dbus_interface>]"
20 print " -i = interface name"
21 print " -p = persistant group = 0 (0=false, 1=true)"
22 print " -f = frequency"
23 print " -o = persistent group object path"
24 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
26 print " %s -i wlan0" % sys.argv[0]
29 def GroupStarted(properties):
30 if properties.has_key("group_object"):
31 print 'Group Formation Complete %s' \
32 % properties["group_object"]
35 def WpsFailure(status, etc):
36 print "WPS Authentication Failure".format(status)
40 class P2P_Group_Add (threading.Thread):
44 global interface_object
48 global wpas_dbus_interface
52 global persistent_group_object
55 global wpas_dbus_opath
56 global wpas_dbus_interfaces_opath
57 global wpas_dbus_interfaces_interface
58 global wpas_dbus_interfaces_p2pdevice
64 def __init__(self,interface_name,wpas_dbus_interface,persistent,frequency,
65 persistent_group_object):
66 # Initializes variables and threads
67 self.interface_name = interface_name
68 self.wpas_dbus_interface = wpas_dbus_interface
69 self.persistent = persistent
70 self.frequency = frequency
71 self.persistent_group_object = persistent_group_object
73 # Initializes thread and daemon allows for ctrl-c kill
74 threading.Thread.__init__(self)
77 # Generating interface/object paths
78 self.wpas_dbus_opath = "/" + \
79 self.wpas_dbus_interface.replace(".","/")
80 self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \
82 self.wpas_dbus_interfaces_interface = \
83 self.wpas_dbus_interface + ".Interface"
84 self.wpas_dbus_interfaces_p2pdevice = \
85 self.wpas_dbus_interfaces_interface \
88 # Getting interfaces and objects
89 DBusGMainLoop(set_as_default=True)
90 self.bus = dbus.SystemBus()
91 self.wpas_object = self.bus.get_object(
92 self.wpas_dbus_interface,
94 self.wpas = dbus.Interface(self.wpas_object,
95 self.wpas_dbus_interface)
97 # Try to see if supplicant knows about interface
98 # If not, throw an exception
100 self.path = self.wpas.GetInterface(
102 except dbus.DBusException, exc:
103 error = 'Error:\n Interface ' + self.interface_name \
109 self.interface_object = self.bus.get_object(
110 self.wpas_dbus_interface, self.path)
111 self.p2p_interface = dbus.Interface(self.interface_object,
112 self.wpas_dbus_interfaces_p2pdevice)
115 self.bus.add_signal_receiver(GroupStarted,
116 dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
117 signal_name="GroupStarted")
118 self.bus.add_signal_receiver(WpsFailure,
119 dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
120 signal_name="WpsFailed")
122 # Sets up p2p_group_add dictionary
123 def constructArguements(self):
124 self.P2PDictionary = {'persistent':self.persistent}
126 if (self.frequency != None):
127 if (int(self.frequency) > 0):
128 self.P2PDictionary.update({'frequency':int(self.frequency)})
130 print "Error:\n Frequency must be greater than 0"
134 if (self.persistent_group_object != None):
135 self.P2PDictionary.update({'persistent_group_object':
136 self.persistent_group_object})
138 # Run p2p_group_remove
141 self.p2p_interface.GroupAdd(self.P2PDictionary)
144 print "Error:\n Could not preform group add"
148 # Allows other threads to keep working while MainLoop runs
149 # Required for timeout implementation
150 gobject.MainLoop().get_context().iteration(True)
151 gobject.threads_init()
152 gobject.MainLoop().run()
155 if __name__ == "__main__":
157 # Defaults for optional inputs
158 # 0 = false, 1 = true
161 persistent_group_object = None
162 wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
164 # interface_name is required
165 interface_name = None
167 # Using getopts to handle options
169 options, args = getopt.getopt(sys.argv[1:],"hi:p:f:o:w:")
171 except getopt.GetoptError:
175 # If theres a switch, override default option
176 for key, value in options:
183 interface_name = value
191 print "Error:\n Persistent can only be 1 or 0"
197 # Persistent group object path
199 persistent_group_object = value
202 wpas_dbus_interface = value
204 assert False, "unhandled option"
206 # Interface name is required and was not given
207 if (interface_name == None):
208 print "Error:\n interface_name is required"
213 p2p_group_add_test = P2P_Group_Add(interface_name,wpas_dbus_interface,
214 persistent,frequency,persistent_group_object)
216 print "Error:\n Invalid Arguements"
218 p2p_group_add_test.constructArguements()
219 p2p_group_add_test.start()
221 print "Error:\n Group formation timed out"