1f05cddf9SRui Paulo#!/usr/bin/python 2f05cddf9SRui Paulo# Tests P2P_Flush 3f05cddf9SRui Paulo# Will flush the p2p interface 4f05cddf9SRui Paulo# Then Program will exit 5f05cddf9SRui Paulo######### MAY NEED TO RUN AS SUDO ############# 6f05cddf9SRui Paulo 7f05cddf9SRui Pauloimport dbus 8f05cddf9SRui Pauloimport sys, os 9f05cddf9SRui Pauloimport time 10f05cddf9SRui Pauloimport gobject 11f05cddf9SRui Pauloimport threading 12f05cddf9SRui Pauloimport getopt 13f05cddf9SRui Paulofrom dbus.mainloop.glib import DBusGMainLoop 14f05cddf9SRui Paulo 15f05cddf9SRui Paulodef usage(): 164bc52338SCy Schubert print("Usage:") 174bc52338SCy Schubert print(" %s -i <interface_name> \ " \ 184bc52338SCy Schubert % sys.argv[0]) 194bc52338SCy Schubert print(" [-w <wpas_dbus_interface>]") 204bc52338SCy Schubert print("Options:") 214bc52338SCy Schubert print(" -i = interface name") 224bc52338SCy Schubert print(" -w = wpas dbus interface = fi.w1.wpa_supplicant1") 234bc52338SCy Schubert print("Example:") 244bc52338SCy Schubert print(" %s -i wlan0" % sys.argv[0]) 25f05cddf9SRui Paulo 26f05cddf9SRui Paulo# Required Signals\ 27f05cddf9SRui Paulodef deviceLost(devicepath): 284bc52338SCy Schubert print("Device lost: %s" % (devicepath)) 29f05cddf9SRui Paulo 30f05cddf9SRui Pauloclass P2P_Flush (threading.Thread): 31f05cddf9SRui Paulo # Needed Variables 32f05cddf9SRui Paulo global bus 33f05cddf9SRui Paulo global wpas_object 34f05cddf9SRui Paulo global interface_object 35f05cddf9SRui Paulo global p2p_interface 36f05cddf9SRui Paulo global interface_name 37f05cddf9SRui Paulo global wpas 38f05cddf9SRui Paulo global wpas_dbus_interface 39f05cddf9SRui Paulo global path 40f05cddf9SRui Paulo global timeout 41f05cddf9SRui Paulo 42f05cddf9SRui Paulo # Dbus Paths 43f05cddf9SRui Paulo global wpas_dbus_opath 44f05cddf9SRui Paulo global wpas_dbus_interfaces_opath 45f05cddf9SRui Paulo global wpas_dbus_interfaces_interface 46f05cddf9SRui Paulo global wpas_dbus_interfaces_p2pdevice 47f05cddf9SRui Paulo 48f05cddf9SRui Paulo # Constructor 49f05cddf9SRui Paulo def __init__(self,interface_name,wpas_dbus_interface,timeout): 50f05cddf9SRui Paulo # Initializes variables and threads 51f05cddf9SRui Paulo self.interface_name = interface_name 52f05cddf9SRui Paulo self.wpas_dbus_interface = wpas_dbus_interface 53f05cddf9SRui Paulo self.timeout = timeout 54f05cddf9SRui Paulo 55f05cddf9SRui Paulo # Initializes thread and daemon allows for ctrl-c kill 56f05cddf9SRui Paulo threading.Thread.__init__(self) 57f05cddf9SRui Paulo self.daemon = True 58f05cddf9SRui Paulo 59f05cddf9SRui Paulo # Generating interface/object paths 60f05cddf9SRui Paulo self.wpas_dbus_opath = "/" + \ 61f05cddf9SRui Paulo self.wpas_dbus_interface.replace(".","/") 62f05cddf9SRui Paulo self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \ 63f05cddf9SRui Paulo "/Interfaces" 64f05cddf9SRui Paulo self.wpas_dbus_interfaces_interface = \ 65f05cddf9SRui Paulo self.wpas_dbus_interface + ".Interface" 66f05cddf9SRui Paulo self.wpas_dbus_interfaces_p2pdevice = \ 67f05cddf9SRui Paulo self.wpas_dbus_interfaces_interface \ 68f05cddf9SRui Paulo + ".P2PDevice" 69f05cddf9SRui Paulo 70f05cddf9SRui Paulo # Getting interfaces and objects 71f05cddf9SRui Paulo DBusGMainLoop(set_as_default=True) 72f05cddf9SRui Paulo self.bus = dbus.SystemBus() 73f05cddf9SRui Paulo self.wpas_object = self.bus.get_object( 74f05cddf9SRui Paulo self.wpas_dbus_interface, 75f05cddf9SRui Paulo self.wpas_dbus_opath) 76f05cddf9SRui Paulo self.wpas = dbus.Interface(self.wpas_object, 77f05cddf9SRui Paulo self.wpas_dbus_interface) 78f05cddf9SRui Paulo 79f05cddf9SRui Paulo # Try to see if supplicant knows about interface 80f05cddf9SRui Paulo # If not, throw an exception 81f05cddf9SRui Paulo try: 82f05cddf9SRui Paulo self.path = self.wpas.GetInterface( 83f05cddf9SRui Paulo self.interface_name) 844bc52338SCy Schubert except dbus.DBusException as exc: 85f05cddf9SRui Paulo error = 'Error:\n Interface ' + self.interface_name \ 86f05cddf9SRui Paulo + ' was not found' 874bc52338SCy Schubert print(error) 88f05cddf9SRui Paulo usage() 89f05cddf9SRui Paulo os._exit(0) 90f05cddf9SRui Paulo 91f05cddf9SRui Paulo self.interface_object = self.bus.get_object( 92f05cddf9SRui Paulo self.wpas_dbus_interface, self.path) 93f05cddf9SRui Paulo self.p2p_interface = dbus.Interface(self.interface_object, 94f05cddf9SRui Paulo self.wpas_dbus_interfaces_p2pdevice) 95f05cddf9SRui Paulo 96f05cddf9SRui Paulo # Signals 97f05cddf9SRui Paulo self.bus.add_signal_receiver(deviceLost, 98f05cddf9SRui Paulo dbus_interface=self.wpas_dbus_interfaces_p2pdevice, 99f05cddf9SRui Paulo signal_name="DeviceLost") 100f05cddf9SRui Paulo 101f05cddf9SRui Paulo # Runs p2p_flush 102f05cddf9SRui Paulo def run(self): 103f05cddf9SRui Paulo # Allows other threads to keep working while MainLoop runs 104f05cddf9SRui Paulo # Required for timeout implementation 105f05cddf9SRui Paulo gobject.MainLoop().get_context().iteration(True) 106f05cddf9SRui Paulo gobject.threads_init() 107f05cddf9SRui Paulo self.p2p_interface.Flush() 108f05cddf9SRui Paulo gobject.MainLoop().run() 109f05cddf9SRui Paulo 110f05cddf9SRui Paulo 111f05cddf9SRui Pauloif __name__ == "__main__": 112f05cddf9SRui Paulo # Needed to show which devices were lost 113f05cddf9SRui Paulo timeout = 5 114f05cddf9SRui Paulo # Defaults for optional inputs 115f05cddf9SRui Paulo wpas_dbus_interface = 'fi.w1.wpa_supplicant1' 116f05cddf9SRui Paulo 117f05cddf9SRui Paulo # interface_name is required 118f05cddf9SRui Paulo interface_name = None 119f05cddf9SRui Paulo 120f05cddf9SRui Paulo # Using getopts to handle options 121f05cddf9SRui Paulo try: 122f05cddf9SRui Paulo options, args = getopt.getopt(sys.argv[1:],"hi:w:") 123f05cddf9SRui Paulo 124f05cddf9SRui Paulo except getopt.GetoptError: 125f05cddf9SRui Paulo usage() 126f05cddf9SRui Paulo quit() 127f05cddf9SRui Paulo 128*c1d255d3SCy Schubert # If there's a switch, override default option 129f05cddf9SRui Paulo for key, value in options: 130f05cddf9SRui Paulo # Help 131f05cddf9SRui Paulo if (key == "-h"): 132f05cddf9SRui Paulo usage() 133f05cddf9SRui Paulo quit() 134f05cddf9SRui Paulo # Interface Name 135f05cddf9SRui Paulo elif (key == "-i"): 136f05cddf9SRui Paulo interface_name = value 137f05cddf9SRui Paulo # Dbus interface 138f05cddf9SRui Paulo elif (key == "-w"): 139f05cddf9SRui Paulo wpas_dbus_interface = value 140f05cddf9SRui Paulo else: 141f05cddf9SRui Paulo assert False, "unhandled option" 142f05cddf9SRui Paulo 143f05cddf9SRui Paulo # Interface name is required and was not given 144f05cddf9SRui Paulo if (interface_name == None): 1454bc52338SCy Schubert print("Error:\n interface_name is required") 146f05cddf9SRui Paulo usage() 147f05cddf9SRui Paulo quit() 148f05cddf9SRui Paulo 149f05cddf9SRui Paulo # Constructor 150f05cddf9SRui Paulo try: 151f05cddf9SRui Paulo p2p_flush_test = P2P_Flush(interface_name, wpas_dbus_interface,timeout) 152f05cddf9SRui Paulo 153f05cddf9SRui Paulo except: 1544bc52338SCy Schubert print("Error:\n Invalid wpas_dbus_interface") 155f05cddf9SRui Paulo usage() 156f05cddf9SRui Paulo quit() 157f05cddf9SRui Paulo 158f05cddf9SRui Paulo # Start P2P_Find 159f05cddf9SRui Paulo p2p_flush_test.start() 160f05cddf9SRui Paulo 161f05cddf9SRui Paulo try: 162f05cddf9SRui Paulo time.sleep(int(p2p_flush_test.timeout)) 163f05cddf9SRui Paulo 164f05cddf9SRui Paulo except: 165f05cddf9SRui Paulo pass 166f05cddf9SRui Paulo 1674bc52338SCy Schubert print("p2p_flush complete") 168f05cddf9SRui Paulo quit() 169