3 ######### MAY NEED TO RUN AS SUDO #############
11 from dbus
.mainloop
.glib
import DBusGMainLoop
15 print " %s -i <interface_name> \ " \
17 print " [-w <wpas_dbus_interface>]"
19 print " -i = interface name"
20 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
22 print " %s -i wlan0" % sys
.argv
[0]
25 def deviceLost(devicepath
):
26 print "Device lost: %s" % (devicepath
)
28 def p2pStateChange(status
):
32 class P2P_Stop_Find (threading
.Thread
):
36 global interface_object
40 global wpas_dbus_interface
45 global wpas_dbus_opath
46 global wpas_dbus_interfaces_opath
47 global wpas_dbus_interfaces_interface
48 global wpas_dbus_interfaces_p2pdevice
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
57 # Initializes thread and daemon allows for ctrl-c kill
58 threading
.Thread
.__init
__(self
)
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
+ \
66 self
.wpas_dbus_interfaces_interface
= \
67 self
.wpas_dbus_interface
+ ".Interface"
68 self
.wpas_dbus_interfaces_p2pdevice
= \
69 self
.wpas_dbus_interfaces_interface \
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
,
78 self
.wpas
= dbus
.Interface(self
.wpas_object
,
79 self
.wpas_dbus_interface
)
81 # Try to see if supplicant knows about interface
82 # If not, throw an exception
84 self
.path
= self
.wpas
.GetInterface(
86 except dbus
.DBusException
as exc
:
87 error
= 'Error:\n Interface ' + self
.interface_name \
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
)
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")
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()
116 if __name__
== "__main__":
117 # Needed because P2PStateChanged signal is not caught
119 # Defaults for optional inputs
120 wpas_dbus_interface
= 'fi.w1.wpa_supplicant1'
122 # interface_name is required
123 interface_name
= None
125 # Using getopts to handle options
127 options
, args
= getopt
.getopt(sys
.argv
[1:],"ht:i:w:")
129 except getopt
.GetoptError
:
133 # If theres a switch, override default option
134 for key
, value
in options
:
141 interface_name
= value
144 wpas_dbus_interface
= value
146 assert False, "unhandled option"
148 # Interface name is required and was not given
149 if (interface_name
== None):
150 print "Error:\n interface_name is required"
156 p2p_stop_find_test
= P2P_Stop_Find(interface_name
,
157 wpas_dbus_interface
,timeout
)
160 print "Error:\n Invalid wpas_dbus_interface"
165 p2p_stop_find_test
.start()
168 time
.sleep(int(p2p_stop_find_test
.timeout
))
173 print "p2p find stopped"