3 # Will try to connect to another peer
5 ######### MAY NEED TO RUN AS SUDO #############
12 from dbus
.mainloop
.glib
import DBusGMainLoop
17 print " %s -i <interface_name> -m <wps_method> \ " \
19 print " -a <addr> [-p <pin>] [-g <go_intent>] \ "
20 print " [-w <wpas_dbus_interface>]"
22 print " -i = interface name"
23 print " -m = wps method"
24 print " -a = peer address"
25 print " -p = pin number (8 digits)"
26 print " -g = group owner intent"
27 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
29 print " %s -i wlan0 -a 0015008352c0 -m display -p 12345670" % sys
.argv
[0]
33 def GONegotiationSuccess(status
):
34 print "Go Negotiation Success"
36 def GONegotiationFailure(status
):
37 print 'Go Negotiation Failed. Status:'
41 def GroupStarted(properties
):
42 if properties
.has_key("group_object"):
43 print 'Group Formation Complete %s' \
44 % properties
["group_object"]
47 def WpsFailure(status
, etc
):
48 print "WPS Authentication Failure".format(status
)
56 global interface_object
60 global wpas_dbus_interface
69 global wpas_dbus_opath
70 global wpas_dbus_interfaces_opath
71 global wpas_dbus_interfaces_interface
72 global wpas_dbus_interfaces_p2pdevice
74 # Dictionary of Arguements
75 global p2p_connect_arguements
78 def __init__(self
,ifname
,wpas_dbus_interface
,addr
,
79 pin
,wps_method
,go_intent
):
80 # Initializes variables and threads
82 self
.wpas_dbus_interface
= wpas_dbus_interface
83 self
.wps_method
= wps_method
84 self
.go_intent
= go_intent
88 # Generating interface/object paths
89 self
.wpas_dbus_opath
= \
90 "/" + self
.wpas_dbus_interface
.replace(".","/")
91 self
.wpas_wpas_dbus_interfaces_opath
= \
92 self
.wpas_dbus_opath
+ "/Interfaces"
93 self
.wpas_dbus_interfaces_interface
= \
94 self
.wpas_dbus_interface
+ ".Interface"
95 self
.wpas_dbus_interfaces_p2pdevice
= \
96 self
.wpas_dbus_interfaces_interface
+ ".P2PDevice"
98 # Getting interfaces and objects
99 DBusGMainLoop(set_as_default
=True)
100 self
.bus
= dbus
.SystemBus()
101 self
.wpas_object
= self
.bus
.get_object(
102 self
.wpas_dbus_interface
,
103 self
.wpas_dbus_opath
)
104 self
.wpas
= dbus
.Interface(
105 self
.wpas_object
, self
.wpas_dbus_interface
)
107 # See if wpa_supplicant already knows about this interface
110 self
.path
= self
.wpas
.GetInterface(ifname
)
112 if not str(exc
).startswith(
113 self
.wpas_dbus_interface
+ \
114 ".InterfaceUnknown:"):
117 path
= self
.wpas
.CreateInterface(
118 {'Ifname': ifname
, 'Driver': 'test'})
121 except dbus
.DBusException
as exc
:
122 if not str(exc
).startswith(
123 self
.wpas_dbus_interface
+ \
124 ".InterfaceExists:"):
127 # Get Interface and objects
128 self
.interface_object
= self
.bus
.get_object(
129 self
.wpas_dbus_interface
,self
.path
)
130 self
.p2p_interface
= dbus
.Interface(
131 self
.interface_object
,
132 self
.wpas_dbus_interfaces_p2pdevice
)
135 self
.bus
.add_signal_receiver(GONegotiationSuccess
,
136 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
137 signal_name
="GONegotiationSuccess")
138 self
.bus
.add_signal_receiver(GONegotiationFailure
,
139 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
140 signal_name
="GONegotiationFailure")
141 self
.bus
.add_signal_receiver(GroupStarted
,
142 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
143 signal_name
="GroupStarted")
144 self
.bus
.add_signal_receiver(WpsFailure
,
145 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
146 signal_name
="WpsFailed")
149 #Constructing all the arguements needed to connect
150 def constructArguements(self
):
151 # Adding required arguements
152 self
.p2p_connect_arguements
= {'wps_method':self
.wps_method
,
153 'peer':dbus
.ObjectPath(self
.path
+'/Peers/'+self
.addr
)}
155 # Display requires a pin, and a go intent of 15
156 if (self
.wps_method
== 'display'):
157 if (self
.pin
!= None):
158 self
.p2p_connect_arguements
.update({'pin':self
.pin
})
160 print "Error:\n Pin required for wps_method=display"
164 if (self
.go_intent
!= None and int(self
.go_intent
) != 15):
165 print "go_intent overwritten to 15"
167 self
.go_intent
= '15'
169 # Keypad requires a pin, and a go intent of less than 15
170 elif (self
.wps_method
== 'keypad'):
171 if (self
.pin
!= None):
172 self
.p2p_connect_arguements
.update({'pin':self
.pin
})
174 print "Error:\n Pin required for wps_method=keypad"
178 if (self
.go_intent
!= None and int(self
.go_intent
) == 15):
179 error
= "Error :\n Group Owner intent cannot be" + \
180 " 15 for wps_method=keypad"
185 # Doesn't require pin
186 # for ./wpa_cli, p2p_connect [mac] [pin#], wps_method=keypad
187 elif (self
.wps_method
== 'pin'):
188 if (self
.pin
!= None):
191 # No pin is required for pbc so it is ignored
192 elif (self
.wps_method
== 'pbc'):
193 if (self
.pin
!= None):
197 print "Error:\n wps_method not supported or does not exist"
201 # Go_intent is optional for all arguements
202 if (self
.go_intent
!= None):
203 self
.p2p_connect_arguements
.update(
204 {'go_intent':dbus
.Int32(self
.go_intent
)})
206 # Running p2p_connect
209 result_pin
= self
.p2p_interface
.Connect(
210 self
.p2p_connect_arguements
)
212 except dbus
.DBusException
as exc
:
215 if (self
.wps_method
== 'pin' and \
216 not self
.p2p_connect_arguements
.has_key('pin') ):
217 print "Connect return with pin value of %d " % int(result_pin
)
218 gobject
.MainLoop().run()
220 if __name__
== "__main__":
223 interface_name
= None
227 # Conditionally optional
231 wpas_dbus_interface
= 'fi.w1.wpa_supplicant1'
234 # Using getopts to handle options
236 options
, args
= getopt
.getopt(sys
.argv
[1:],"hi:m:a:p:g:w:")
238 except getopt
.GetoptError
:
242 # If theres a switch, override default option
243 for key
, value
in options
:
250 interface_name
= value
265 wpas_dbus_interface
= value
267 assert False, "unhandled option"
269 # Required Arguements check
270 if (interface_name
== None or wps_method
== None or addr
== None):
271 print "Error:\n Required arguements not specified"
275 # Group Owner Intent Check
276 if (go_intent
!= None and (int(go_intent
) > 15 or int(go_intent
) < 0) ):
277 print "Error:\n Group Owner Intent must be between 0 and 15 inclusive"
282 if (pin
!= None and len(pin
) != 8):
283 print "Error:\n Pin is not 8 digits"
288 p2p_connect_test
= P2P_Connect(interface_name
,wpas_dbus_interface
,
289 addr
,pin
,wps_method
,go_intent
)
292 print "Error:\n Invalid Arguements"
296 p2p_connect_test
.constructArguements()
297 p2p_connect_test
.run()