xref: /freebsd/contrib/wpa/wpa_supplicant/examples/p2p/p2p_stop_find.py (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
1f05cddf9SRui Paulo#!/usr/bin/python
2f05cddf9SRui Paulo# Tests p2p_stop_find
3f05cddf9SRui Paulo######### MAY NEED TO RUN AS SUDO #############
4f05cddf9SRui Paulo
5f05cddf9SRui Pauloimport dbus
6f05cddf9SRui Pauloimport sys, os
7f05cddf9SRui Pauloimport time
8f05cddf9SRui Pauloimport gobject
9f05cddf9SRui Pauloimport threading
10f05cddf9SRui Pauloimport getopt
11f05cddf9SRui Paulofrom dbus.mainloop.glib import DBusGMainLoop
12f05cddf9SRui Paulo
13f05cddf9SRui Paulodef usage():
144bc52338SCy Schubert	print("Usage:")
154bc52338SCy Schubert	print("  %s -i <interface_name> \ " \
164bc52338SCy Schubert		% sys.argv[0])
174bc52338SCy Schubert	print("  		[-w <wpas_dbus_interface>]")
184bc52338SCy Schubert	print("Options:")
194bc52338SCy Schubert	print("  -i = interface name")
204bc52338SCy Schubert	print("  -w = wpas dbus interface = fi.w1.wpa_supplicant1")
214bc52338SCy Schubert	print("Example:")
224bc52338SCy Schubert	print("  %s -i wlan0" % sys.argv[0])
23f05cddf9SRui Paulo
24f05cddf9SRui Paulo# Required Signals
25f05cddf9SRui Paulodef deviceLost(devicepath):
264bc52338SCy Schubert	print("Device lost: %s" % (devicepath))
27f05cddf9SRui Paulo
28f05cddf9SRui Paulodef p2pStateChange(status):
294bc52338SCy Schubert	print(status)
30f05cddf9SRui Paulo	os._exit(0)
31f05cddf9SRui Paulo
32f05cddf9SRui Pauloclass P2P_Stop_Find (threading.Thread):
33f05cddf9SRui Paulo	# Needed Variables
34f05cddf9SRui Paulo	global bus
35f05cddf9SRui Paulo	global wpas_object
36f05cddf9SRui Paulo	global interface_object
37f05cddf9SRui Paulo	global p2p_interface
38f05cddf9SRui Paulo	global interface_name
39f05cddf9SRui Paulo	global wpas
40f05cddf9SRui Paulo	global wpas_dbus_interface
41f05cddf9SRui Paulo	global path
42f05cddf9SRui Paulo	global timeout
43f05cddf9SRui Paulo
44f05cddf9SRui Paulo	# Dbus Paths
45f05cddf9SRui Paulo	global wpas_dbus_opath
46f05cddf9SRui Paulo	global wpas_dbus_interfaces_opath
47f05cddf9SRui Paulo	global wpas_dbus_interfaces_interface
48f05cddf9SRui Paulo	global wpas_dbus_interfaces_p2pdevice
49f05cddf9SRui Paulo
50f05cddf9SRui Paulo	# Constructor
51f05cddf9SRui Paulo	def __init__(self,interface_name,wpas_dbus_interface,timeout):
52f05cddf9SRui Paulo		# Initializes variables and threads
53f05cddf9SRui Paulo		self.interface_name = interface_name
54f05cddf9SRui Paulo		self.wpas_dbus_interface = wpas_dbus_interface
55f05cddf9SRui Paulo		self.timeout = timeout
56f05cddf9SRui Paulo
57f05cddf9SRui Paulo		# Initializes thread and daemon allows for ctrl-c kill
58f05cddf9SRui Paulo		threading.Thread.__init__(self)
59f05cddf9SRui Paulo		self.daemon = True
60f05cddf9SRui Paulo
61f05cddf9SRui Paulo		# Generating interface/object paths
62f05cddf9SRui Paulo		self.wpas_dbus_opath = "/" + \
63f05cddf9SRui Paulo				self.wpas_dbus_interface.replace(".","/")
64f05cddf9SRui Paulo		self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \
65f05cddf9SRui Paulo				"/Interfaces"
66f05cddf9SRui Paulo		self.wpas_dbus_interfaces_interface = \
67f05cddf9SRui Paulo				self.wpas_dbus_interface + ".Interface"
68f05cddf9SRui Paulo		self.wpas_dbus_interfaces_p2pdevice = \
69f05cddf9SRui Paulo				self.wpas_dbus_interfaces_interface \
70f05cddf9SRui Paulo				+ ".P2PDevice"
71f05cddf9SRui Paulo
72f05cddf9SRui Paulo		# Getting interfaces and objects
73f05cddf9SRui Paulo		DBusGMainLoop(set_as_default=True)
74f05cddf9SRui Paulo		self.bus = dbus.SystemBus()
75f05cddf9SRui Paulo		self.wpas_object = self.bus.get_object(
76f05cddf9SRui Paulo				self.wpas_dbus_interface,
77f05cddf9SRui Paulo				self.wpas_dbus_opath)
78f05cddf9SRui Paulo		self.wpas = dbus.Interface(self.wpas_object,
79f05cddf9SRui Paulo				self.wpas_dbus_interface)
80f05cddf9SRui Paulo
81f05cddf9SRui Paulo		# Try to see if supplicant knows about interface
82f05cddf9SRui Paulo		# If not, throw an exception
83f05cddf9SRui Paulo		try:
84f05cddf9SRui Paulo			self.path = self.wpas.GetInterface(
85f05cddf9SRui Paulo					self.interface_name)
864bc52338SCy Schubert		except dbus.DBusException as exc:
87f05cddf9SRui Paulo			error = 'Error:\n  Interface ' + self.interface_name \
88f05cddf9SRui Paulo				+ ' was not found'
894bc52338SCy Schubert			print(error)
90f05cddf9SRui Paulo			usage()
91f05cddf9SRui Paulo			os._exit(0)
92f05cddf9SRui Paulo
93f05cddf9SRui Paulo		self.interface_object = self.bus.get_object(
94f05cddf9SRui Paulo				self.wpas_dbus_interface, self.path)
95f05cddf9SRui Paulo		self.p2p_interface = dbus.Interface(self.interface_object,
96f05cddf9SRui Paulo				self.wpas_dbus_interfaces_p2pdevice)
97f05cddf9SRui Paulo
98f05cddf9SRui Paulo		# Signals
99f05cddf9SRui Paulo		self.bus.add_signal_receiver(deviceLost,
100f05cddf9SRui Paulo			dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
101f05cddf9SRui Paulo			signal_name="DeviceLost")
102f05cddf9SRui Paulo		self.bus.add_signal_receiver(p2pStateChange,
103f05cddf9SRui Paulo			dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
104f05cddf9SRui Paulo			signal_name="P2PStateChanged")
105f05cddf9SRui Paulo
106f05cddf9SRui Paulo	# Runs p2p_stop_find
107f05cddf9SRui Paulo	def run(self):
108f05cddf9SRui Paulo		# Allows other threads to keep working while MainLoop runs
109f05cddf9SRui Paulo		# Required for timeout implementation
110f05cddf9SRui Paulo		gobject.MainLoop().get_context().iteration(True)
111f05cddf9SRui Paulo		gobject.threads_init()
112f05cddf9SRui Paulo		self.p2p_interface.StopFind()
113f05cddf9SRui Paulo		gobject.MainLoop().run()
114f05cddf9SRui Paulo
115f05cddf9SRui Paulo
116f05cddf9SRui Pauloif __name__ == "__main__":
117f05cddf9SRui Paulo	# Needed because P2PStateChanged signal is not caught
118f05cddf9SRui Paulo	timeout = 5
119f05cddf9SRui Paulo	# Defaults for optional inputs
120f05cddf9SRui Paulo	wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
121f05cddf9SRui Paulo
122f05cddf9SRui Paulo	# interface_name is required
123f05cddf9SRui Paulo	interface_name = None
124f05cddf9SRui Paulo
125f05cddf9SRui Paulo	# Using getopts to handle options
126f05cddf9SRui Paulo	try:
127f05cddf9SRui Paulo		options, args = getopt.getopt(sys.argv[1:],"ht:i:w:")
128f05cddf9SRui Paulo
129f05cddf9SRui Paulo	except getopt.GetoptError:
130f05cddf9SRui Paulo		usage()
131f05cddf9SRui Paulo		quit()
132f05cddf9SRui Paulo
133*c1d255d3SCy Schubert	# If there's a switch, override default option
134f05cddf9SRui Paulo	for key, value in options:
135f05cddf9SRui Paulo		# Help
136f05cddf9SRui Paulo		if (key == "-h"):
137f05cddf9SRui Paulo			usage()
138f05cddf9SRui Paulo			quit()
139f05cddf9SRui Paulo		# Interface Name
140f05cddf9SRui Paulo		elif (key == "-i"):
141f05cddf9SRui Paulo			interface_name = value
142f05cddf9SRui Paulo		# Dbus interface
143f05cddf9SRui Paulo		elif (key == "-w"):
144f05cddf9SRui Paulo			wpas_dbus_interface = value
145f05cddf9SRui Paulo		else:
146f05cddf9SRui Paulo			assert False, "unhandled option"
147f05cddf9SRui Paulo
148f05cddf9SRui Paulo	# Interface name is required and was not given
149f05cddf9SRui Paulo	if (interface_name == None):
1504bc52338SCy Schubert		print("Error:\n  interface_name is required")
151f05cddf9SRui Paulo		usage()
152f05cddf9SRui Paulo		quit()
153f05cddf9SRui Paulo
154f05cddf9SRui Paulo	# Constructor
155f05cddf9SRui Paulo	try:
156f05cddf9SRui Paulo		p2p_stop_find_test = P2P_Stop_Find(interface_name,
157f05cddf9SRui Paulo						wpas_dbus_interface,timeout)
158f05cddf9SRui Paulo
159f05cddf9SRui Paulo	except:
1604bc52338SCy Schubert		print("Error:\n  Invalid wpas_dbus_interface")
161f05cddf9SRui Paulo		usage()
162f05cddf9SRui Paulo		quit()
163f05cddf9SRui Paulo
164f05cddf9SRui Paulo	# Start P2P_Find
165f05cddf9SRui Paulo	p2p_stop_find_test.start()
166f05cddf9SRui Paulo
167f05cddf9SRui Paulo	try:
168f05cddf9SRui Paulo		time.sleep(int(p2p_stop_find_test.timeout))
169f05cddf9SRui Paulo
170f05cddf9SRui Paulo	except:
171f05cddf9SRui Paulo		pass
172f05cddf9SRui Paulo
1734bc52338SCy Schubert	print("p2p find stopped")
174f05cddf9SRui Paulo	quit()
175