3 # Will list all devices found/lost within a time frame (timeout)
4 # Then Program will exit
5 ######### MAY NEED TO RUN AS SUDO #############
13 from dbus
.mainloop
.glib
import DBusGMainLoop
17 print " %s -i <interface_name> [-t <timeout>] \ " \
19 print " [-w <wpas_dbus_interface>]"
21 print " -i = interface name"
22 print " -t = timeout = 0s (infinite)"
23 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
25 print " %s -i wlan0 -t 10" % sys
.argv
[0]
28 def deviceFound(devicepath
):
29 print "Device found: %s" % (devicepath
)
31 def deviceLost(devicepath
):
32 print "Device lost: %s" % (devicepath
)
34 class P2P_Find (threading
.Thread
):
38 global interface_object
42 global wpas_dbus_interface
47 global wpas_dbus_opath
48 global wpas_dbus_interfaces_opath
49 global wpas_dbus_interfaces_interface
50 global wpas_dbus_interfaces_p2pdevice
53 def __init__(self
,interface_name
,wpas_dbus_interface
,timeout
):
54 # Initializes variables and threads
55 self
.timeout
= int(timeout
)
56 self
.interface_name
= interface_name
57 self
.wpas_dbus_interface
= wpas_dbus_interface
59 # Initializes thread and daemon allows for ctrl-c kill
60 threading
.Thread
.__init
__(self
)
63 # Generating interface/object paths
64 self
.wpas_dbus_opath
= "/" + \
65 self
.wpas_dbus_interface
.replace(".","/")
66 self
.wpas_wpas_dbus_interfaces_opath
= self
.wpas_dbus_opath
+ \
68 self
.wpas_dbus_interfaces_interface
= \
69 self
.wpas_dbus_interface
+ ".Interface"
70 self
.wpas_dbus_interfaces_p2pdevice
= \
71 self
.wpas_dbus_interfaces_interface \
74 # Getting interfaces and objects
75 DBusGMainLoop(set_as_default
=True)
76 self
.bus
= dbus
.SystemBus()
77 self
.wpas_object
= self
.bus
.get_object(
78 self
.wpas_dbus_interface
,
80 self
.wpas
= dbus
.Interface(self
.wpas_object
,
81 self
.wpas_dbus_interface
)
83 # Try to see if supplicant knows about interface
84 # If not, throw an exception
86 self
.path
= self
.wpas
.GetInterface(
88 except dbus
.DBusException
as exc
:
89 error
= 'Error:\n Interface ' + self
.interface_name \
95 self
.interface_object
= self
.bus
.get_object(
96 self
.wpas_dbus_interface
, self
.path
)
97 self
.p2p_interface
= dbus
.Interface(self
.interface_object
,
98 self
.wpas_dbus_interfaces_p2pdevice
)
100 #Adds listeners for find and lost
101 self
.bus
.add_signal_receiver(deviceFound
,
102 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
103 signal_name
="DeviceFound")
104 self
.bus
.add_signal_receiver(deviceLost
,
105 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
106 signal_name
="DeviceLost")
110 P2PFindDict
= dbus
.Dictionary(
111 {'Timeout':int(self
.timeout
)})
112 self
.p2p_interface
.Find(P2PFindDict
)
116 # Allows other threads to keep working while MainLoop runs
117 # Required for timeout implementation
118 gobject
.MainLoop().get_context().iteration(True)
119 gobject
.threads_init()
120 gobject
.MainLoop().run()
122 if __name__
== "__main__":
124 # Defaults for optional inputs
126 wpas_dbus_interface
= 'fi.w1.wpa_supplicant1'
128 # interface_name is required
129 interface_name
= None
131 # Using getopts to handle options
133 options
, args
= getopt
.getopt(sys
.argv
[1:],"hi:t:w:")
135 except getopt
.GetoptError
:
139 # If theres a switch, override default option
140 for key
, value
in options
:
147 interface_name
= value
150 if ( int(value
) >= 0):
153 print "Error:\n Timeout cannot be negative"
158 wpas_dbus_interface
= value
160 assert False, "unhandled option"
162 # Interface name is required and was not given
163 if (interface_name
== None):
164 print "Error:\n interface_name is required"
170 p2p_find_test
= P2P_Find(interface_name
, wpas_dbus_interface
, timeout
)
173 print "Error:\n Invalid wpas_dbus_interface"
178 p2p_find_test
.start()
181 # If timeout is 0, then run forever
185 # Else sleep for (timeout)
187 time
.sleep(p2p_find_test
.timeout
)