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