3 # Will perform disconnect on interface_name
4 ######### MAY NEED TO RUN AS SUDO #############
12 from dbus.mainloop.glib import DBusGMainLoop
16 print " %s -i <interface_name> \ " \
18 print " [-w <wpas_dbus_interface>]"
20 print " -i = interface name"
21 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1"
23 print " %s -i p2p-wlan0-0" % sys.argv[0]
26 def GroupFinished(status, etc):
30 class P2P_Disconnect (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, 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(GroupFinished,
98 dbus_interface=self.wpas_dbus_interfaces_p2pdevice,
99 signal_name="GroupFinished")
101 # Runs p2p_disconnect
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.Disconnect()
108 gobject.MainLoop().run()
111 if __name__ == "__main__":
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_disconnect_test = P2P_Disconnect(interface_name,
152 wpas_dbus_interface,timeout)
155 print "Error:\n Invalid wpas_dbus_interface"
159 # Start P2P_Disconnect
160 p2p_disconnect_test.start()
163 time.sleep(int(p2p_disconnect_test.timeout))
168 print "Disconnect timed out"