1#!/usr/bin/python
2# Tests P2P_Disconnect
3# Will perform disconnect on interface_name
4######### MAY NEED TO RUN AS SUDO #############
5
6import dbus
7import sys, os
8import time
9import gobject
10import threading
11import getopt
12from dbus.mainloop.glib import DBusGMainLoop
13
14def usage():
15	print "Usage:"
16	print "  %s -i <interface_name> \ " \
17		% sys.argv[0]
18	print "  		[-w <wpas_dbus_interface>]"
19	print "Options:"
20	print "  -i = interface name"
21	print "  -w = wpas dbus interface = fi.w1.wpa_supplicant1"
22	print "Example:"
23	print "  %s -i p2p-wlan0-0" % sys.argv[0]
24
25# Required Signals
26def GroupFinished(status, etc):
27	print "Disconnected"
28	os._exit(0)
29
30class P2P_Disconnect (threading.Thread):
31	# Needed Variables
32	global bus
33	global wpas_object
34	global interface_object
35	global p2p_interface
36	global interface_name
37	global wpas
38	global wpas_dbus_interface
39	global path
40	global timeout
41
42	# Dbus Paths
43	global wpas_dbus_opath
44	global wpas_dbus_interfaces_opath
45	global wpas_dbus_interfaces_interface
46	global wpas_dbus_interfaces_p2pdevice
47
48	# Constructor
49	def __init__(self,interface_name,wpas_dbus_interface,timeout):
50		# Initializes variables and threads
51		self.interface_name = interface_name
52		self.wpas_dbus_interface = wpas_dbus_interface
53		self.timeout = timeout
54
55		# Initializes thread and daemon allows for ctrl-c kill
56		threading.Thread.__init__(self)
57		self.daemon = True
58
59		# Generating interface/object paths
60		self.wpas_dbus_opath = "/" + \
61				self.wpas_dbus_interface.replace(".","/")
62		self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \
63				"/Interfaces"
64		self.wpas_dbus_interfaces_interface = \
65				self.wpas_dbus_interface + ".Interface"
66		self.wpas_dbus_interfaces_p2pdevice = \
67				self.wpas_dbus_interfaces_interface \
68				+ ".P2PDevice"
69
70		# Getting interfaces and objects
71		DBusGMainLoop(set_as_default=True)
72		self.bus = dbus.SystemBus()
73		self.wpas_object = self.bus.get_object(
74				self.wpas_dbus_interface,
75				self.wpas_dbus_opath)
76		self.wpas = dbus.Interface(self.wpas_object,
77				self.wpas_dbus_interface)
78
79		# Try to see if supplicant knows about interface
80		# If not, throw an exception
81		try:
82			self.path = self.wpas.GetInterface(
83					self.interface_name)
84		except dbus.DBusException, exc:
85			error = 'Error:\n  Interface ' + self.interface_name \
86				+ ' was not found'
87			print error
88			usage()
89			os._exit(0)
90
91		self.interface_object = self.bus.get_object(
92				self.wpas_dbus_interface, self.path)
93		self.p2p_interface = dbus.Interface(self.interface_object,
94				self.wpas_dbus_interfaces_p2pdevice)
95
96		# Signals
97		self.bus.add_signal_receiver(GroupFinished,
98			dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
99			signal_name="GroupFinished")
100
101	# Runs p2p_disconnect
102	def run(self):
103		# Allows other threads to keep working while MainLoop runs
104		# Required for timeout implementation
105		gobject.MainLoop().get_context().iteration(True)
106		gobject.threads_init()
107		self.p2p_interface.Disconnect()
108		gobject.MainLoop().run()
109
110
111if __name__ == "__main__":
112
113	timeout = 5
114	# Defaults for optional inputs
115	wpas_dbus_interface = 'fi.w1.wpa_supplicant1'
116
117	# interface_name is required
118	interface_name = None
119
120	# Using getopts to handle options
121	try:
122		options, args = getopt.getopt(sys.argv[1:],"hi:w:")
123
124	except getopt.GetoptError:
125		usage()
126		quit()
127
128	# If theres a switch, override default option
129	for key, value in options:
130		# Help
131		if (key == "-h"):
132			usage()
133			quit()
134		# Interface Name
135		elif (key == "-i"):
136			interface_name = value
137		# Dbus interface
138		elif (key == "-w"):
139			wpas_dbus_interface = value
140		else:
141			assert False, "unhandled option"
142
143	# Interface name is required and was not given
144	if (interface_name == None):
145		print "Error:\n  interface_name is required"
146		usage()
147		quit()
148
149	# Constructor
150	try:
151		p2p_disconnect_test = P2P_Disconnect(interface_name,
152						wpas_dbus_interface,timeout)
153
154	except:
155		print "Error:\n  Invalid wpas_dbus_interface"
156		usage()
157		quit()
158
159	# Start P2P_Disconnect
160	p2p_disconnect_test.start()
161
162	try:
163		time.sleep(int(p2p_disconnect_test.timeout))
164
165	except:
166		pass
167
168	print "Disconnect timed out"
169	quit()
170