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 5" % sys
.argv
[0]
28 def p2pStateChange(status
):
31 class P2P_Listen(threading
.Thread
):
35 global interface_object
39 global wpas_dbus_interface
44 global wpas_dbus_opath
45 global wpas_dbus_interfaces_opath
46 global wpas_dbus_interfaces_interface
47 global wpas_dbus_interfaces_p2pdevice
50 def __init__(self
,interface_name
,wpas_dbus_interface
,timeout
):
51 # Initializes variables and threads
52 self
.timeout
= int(timeout
)
53 self
.interface_name
= interface_name
54 self
.wpas_dbus_interface
= wpas_dbus_interface
56 # Initializes thread and daemon allows for ctrl-c kill
57 threading
.Thread
.__init
__(self
)
60 # Generating interface/object paths
61 self
.wpas_dbus_opath
= "/" + \
62 self
.wpas_dbus_interface
.replace(".","/")
63 self
.wpas_wpas_dbus_interfaces_opath
= self
.wpas_dbus_opath
+ \
65 self
.wpas_dbus_interfaces_interface
= \
66 self
.wpas_dbus_interface
+ ".Interface"
67 self
.wpas_dbus_interfaces_p2pdevice
= \
68 self
.wpas_dbus_interfaces_interface \
71 # Getting interfaces and objects
72 DBusGMainLoop(set_as_default
=True)
73 self
.bus
= dbus
.SystemBus()
74 self
.wpas_object
= self
.bus
.get_object(
75 self
.wpas_dbus_interface
,
77 self
.wpas
= dbus
.Interface(self
.wpas_object
,
78 self
.wpas_dbus_interface
)
80 # Try to see if supplicant knows about interface
81 # If not, throw an exception
83 self
.path
= self
.wpas
.GetInterface(
85 except dbus
.DBusException
as exc
:
86 error
= 'Error:\n Interface ' + self
.interface_name \
92 self
.interface_object
= self
.bus
.get_object(
93 self
.wpas_dbus_interface
, self
.path
)
94 self
.p2p_interface
= dbus
.Interface(self
.interface_object
,
95 self
.wpas_dbus_interfaces_p2pdevice
)
97 self
.bus
.add_signal_receiver(p2pStateChange
,
98 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
99 signal_name
="P2PStateChanged")
104 self
.p2p_interface
.Listen(int(self
.timeout
))
106 # Allows other threads to keep working while MainLoop runs
107 # Required for timeout implementation
108 gobject
.MainLoop().get_context().iteration(True)
109 gobject
.threads_init()
110 gobject
.MainLoop().run()
112 if __name__
== "__main__":
114 # Defaults for optional inputs
116 wpas_dbus_interface
= 'fi.w1.wpa_supplicant1'
118 # interface_name is required
119 interface_name
= None
121 # Using getopts to handle options
123 options
, args
= getopt
.getopt(sys
.argv
[1:],"hi:t:w:")
125 except getopt
.GetoptError
:
129 # If theres a switch, override default option
130 for key
, value
in options
:
137 interface_name
= value
140 if ( int(value
) >= 0):
143 print "Error:\n Timeout cannot be negative"
148 wpas_dbus_interface
= value
150 assert False, "unhandled option"
152 # Interface name is required and was not given
153 if (interface_name
== None):
154 print "Error:\n interface_name is required"
160 p2p_listen_test
= P2P_Listen(interface_name
, wpas_dbus_interface
, timeout
)
163 print "Error:\n Invalid wpas_dbus_interface"
168 p2p_listen_test
.start()
171 # If timeout is 0, then run forever
172 if (int(p2p_listen_test
.timeout
) == 0):
175 # Else sleep for (timeout)
177 time
.sleep(int(p2p_listen_test
.timeout
))