3 # Will flush the p2p interface
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> \ " \
19 print " [-w <wpas_dbus_interface>]"
21 print " -i = interface name"
22 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
24 print " %s -i wlan0" % sys
.argv
[0]
27 def deviceLost(devicepath
):
28 print "Device lost: %s" % (devicepath
)
30 class P2P_Flush (threading
.Thread
):
34 global interface_object
38 global wpas_dbus_interface
43 global wpas_dbus_opath
44 global wpas_dbus_interfaces_opath
45 global wpas_dbus_interfaces_interface
46 global wpas_dbus_interfaces_p2pdevice
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
55 # Initializes thread and daemon allows for ctrl-c kill
56 threading
.Thread
.__init
__(self
)
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
+ \
64 self
.wpas_dbus_interfaces_interface
= \
65 self
.wpas_dbus_interface
+ ".Interface"
66 self
.wpas_dbus_interfaces_p2pdevice
= \
67 self
.wpas_dbus_interfaces_interface \
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
,
76 self
.wpas
= dbus
.Interface(self
.wpas_object
,
77 self
.wpas_dbus_interface
)
79 # Try to see if supplicant knows about interface
80 # If not, throw an exception
82 self
.path
= self
.wpas
.GetInterface(
84 except dbus
.DBusException
as exc
:
85 error
= 'Error:\n Interface ' + self
.interface_name \
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
)
97 self
.bus
.add_signal_receiver(deviceLost
,
98 dbus_interface
=self
.wpas_dbus_interfaces_p2pdevice
,
99 signal_name
="DeviceLost")
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
.Flush()
108 gobject
.MainLoop().run()
111 if __name__
== "__main__":
112 # Needed to show which devices were lost
114 # Defaults for optional inputs
115 wpas_dbus_interface
= 'fi.w1.wpa_supplicant1'
117 # interface_name is required
118 interface_name
= None
120 # Using getopts to handle options
122 options
, args
= getopt
.getopt(sys
.argv
[1:],"hi:w:")
124 except getopt
.GetoptError
:
128 # If theres a switch, override default option
129 for key
, value
in options
:
136 interface_name
= value
139 wpas_dbus_interface
= value
141 assert False, "unhandled option"
143 # Interface name is required and was not given
144 if (interface_name
== None):
145 print "Error:\n interface_name is required"
151 p2p_flush_test
= P2P_Flush(interface_name
, wpas_dbus_interface
,timeout
)
154 print "Error:\n Invalid wpas_dbus_interface"
159 p2p_flush_test
.start()
162 time
.sleep(int(p2p_flush_test
.timeout
))
167 print "p2p_flush complete"