1f05cddf9SRui Paulo#!/usr/bin/python 2f05cddf9SRui Paulo# Tests p2p_find 3f05cddf9SRui Paulo# Will list all devices found/lost within a time frame (timeout) 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> [-t <timeout>] \ " \ 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(" -t = timeout = 0s (infinite)") 234bc52338SCy Schubert print(" -w = wpas dbus interface = fi.w1.wpa_supplicant1") 244bc52338SCy Schubert print("Example:") 254bc52338SCy Schubert print(" %s -i wlan0 -t 10" % sys.argv[0]) 26f05cddf9SRui Paulo 27f05cddf9SRui Paulo# Required Signals 28f05cddf9SRui Paulodef deviceFound(devicepath): 294bc52338SCy Schubert print("Device found: %s" % (devicepath)) 30f05cddf9SRui Paulo 31f05cddf9SRui Paulodef deviceLost(devicepath): 324bc52338SCy Schubert print("Device lost: %s" % (devicepath)) 33f05cddf9SRui Paulo 34f05cddf9SRui Pauloclass P2P_Find (threading.Thread): 35f05cddf9SRui Paulo # Needed Variables 36f05cddf9SRui Paulo global bus 37f05cddf9SRui Paulo global wpas_object 38f05cddf9SRui Paulo global interface_object 39f05cddf9SRui Paulo global p2p_interface 40f05cddf9SRui Paulo global interface_name 41f05cddf9SRui Paulo global wpas 42f05cddf9SRui Paulo global wpas_dbus_interface 43f05cddf9SRui Paulo global timeout 44f05cddf9SRui Paulo global path 45f05cddf9SRui Paulo 46f05cddf9SRui Paulo # Dbus Paths 47f05cddf9SRui Paulo global wpas_dbus_opath 48f05cddf9SRui Paulo global wpas_dbus_interfaces_opath 49f05cddf9SRui Paulo global wpas_dbus_interfaces_interface 50f05cddf9SRui Paulo global wpas_dbus_interfaces_p2pdevice 51f05cddf9SRui Paulo 52f05cddf9SRui Paulo # Constructor 53f05cddf9SRui Paulo def __init__(self,interface_name,wpas_dbus_interface,timeout): 54f05cddf9SRui Paulo # Initializes variables and threads 55f05cddf9SRui Paulo self.timeout = int(timeout) 56f05cddf9SRui Paulo self.interface_name = interface_name 57f05cddf9SRui Paulo self.wpas_dbus_interface = wpas_dbus_interface 58f05cddf9SRui Paulo 59f05cddf9SRui Paulo # Initializes thread and daemon allows for ctrl-c kill 60f05cddf9SRui Paulo threading.Thread.__init__(self) 61f05cddf9SRui Paulo self.daemon = True 62f05cddf9SRui Paulo 63f05cddf9SRui Paulo # Generating interface/object paths 64f05cddf9SRui Paulo self.wpas_dbus_opath = "/" + \ 65f05cddf9SRui Paulo self.wpas_dbus_interface.replace(".","/") 66f05cddf9SRui Paulo self.wpas_wpas_dbus_interfaces_opath = self.wpas_dbus_opath + \ 67f05cddf9SRui Paulo "/Interfaces" 68f05cddf9SRui Paulo self.wpas_dbus_interfaces_interface = \ 69f05cddf9SRui Paulo self.wpas_dbus_interface + ".Interface" 70f05cddf9SRui Paulo self.wpas_dbus_interfaces_p2pdevice = \ 71f05cddf9SRui Paulo self.wpas_dbus_interfaces_interface \ 72f05cddf9SRui Paulo + ".P2PDevice" 73f05cddf9SRui Paulo 74f05cddf9SRui Paulo # Getting interfaces and objects 75f05cddf9SRui Paulo DBusGMainLoop(set_as_default=True) 76f05cddf9SRui Paulo self.bus = dbus.SystemBus() 77f05cddf9SRui Paulo self.wpas_object = self.bus.get_object( 78f05cddf9SRui Paulo self.wpas_dbus_interface, 79f05cddf9SRui Paulo self.wpas_dbus_opath) 80f05cddf9SRui Paulo self.wpas = dbus.Interface(self.wpas_object, 81f05cddf9SRui Paulo self.wpas_dbus_interface) 82f05cddf9SRui Paulo 83f05cddf9SRui Paulo # Try to see if supplicant knows about interface 84f05cddf9SRui Paulo # If not, throw an exception 85f05cddf9SRui Paulo try: 86f05cddf9SRui Paulo self.path = self.wpas.GetInterface( 87f05cddf9SRui Paulo self.interface_name) 884bc52338SCy Schubert except dbus.DBusException as exc: 89f05cddf9SRui Paulo error = 'Error:\n Interface ' + self.interface_name \ 90f05cddf9SRui Paulo + ' was not found' 914bc52338SCy Schubert print(error) 92f05cddf9SRui Paulo usage() 93f05cddf9SRui Paulo os._exit(0) 94f05cddf9SRui Paulo 95f05cddf9SRui Paulo self.interface_object = self.bus.get_object( 96f05cddf9SRui Paulo self.wpas_dbus_interface, self.path) 97f05cddf9SRui Paulo self.p2p_interface = dbus.Interface(self.interface_object, 98f05cddf9SRui Paulo self.wpas_dbus_interfaces_p2pdevice) 99f05cddf9SRui Paulo 100f05cddf9SRui Paulo #Adds listeners for find and lost 101f05cddf9SRui Paulo self.bus.add_signal_receiver(deviceFound, 102f05cddf9SRui Paulo dbus_interface=self.wpas_dbus_interfaces_p2pdevice, 103f05cddf9SRui Paulo signal_name="DeviceFound") 104f05cddf9SRui Paulo self.bus.add_signal_receiver(deviceLost, 105f05cddf9SRui Paulo dbus_interface=self.wpas_dbus_interfaces_p2pdevice, 106f05cddf9SRui Paulo signal_name="DeviceLost") 107f05cddf9SRui Paulo 108f05cddf9SRui Paulo 109f05cddf9SRui Paulo # Sets up p2p_find 110f05cddf9SRui Paulo P2PFindDict = dbus.Dictionary( 111f05cddf9SRui Paulo {'Timeout':int(self.timeout)}) 112f05cddf9SRui Paulo self.p2p_interface.Find(P2PFindDict) 113f05cddf9SRui Paulo 114f05cddf9SRui Paulo # Run p2p_find 115f05cddf9SRui Paulo def run(self): 116f05cddf9SRui Paulo # Allows other threads to keep working while MainLoop runs 117f05cddf9SRui Paulo # Required for timeout implementation 118f05cddf9SRui Paulo gobject.MainLoop().get_context().iteration(True) 119f05cddf9SRui Paulo gobject.threads_init() 120f05cddf9SRui Paulo gobject.MainLoop().run() 121f05cddf9SRui Paulo 122f05cddf9SRui Pauloif __name__ == "__main__": 123f05cddf9SRui Paulo 124f05cddf9SRui Paulo # Defaults for optional inputs 125f05cddf9SRui Paulo timeout = 0 126f05cddf9SRui Paulo wpas_dbus_interface = 'fi.w1.wpa_supplicant1' 127f05cddf9SRui Paulo 128f05cddf9SRui Paulo # interface_name is required 129f05cddf9SRui Paulo interface_name = None 130f05cddf9SRui Paulo 131f05cddf9SRui Paulo # Using getopts to handle options 132f05cddf9SRui Paulo try: 133f05cddf9SRui Paulo options, args = getopt.getopt(sys.argv[1:],"hi:t:w:") 134f05cddf9SRui Paulo 135f05cddf9SRui Paulo except getopt.GetoptError: 136f05cddf9SRui Paulo usage() 137f05cddf9SRui Paulo quit() 138f05cddf9SRui Paulo 139*c1d255d3SCy Schubert # If there's a switch, override default option 140f05cddf9SRui Paulo for key, value in options: 141f05cddf9SRui Paulo # Help 142f05cddf9SRui Paulo if (key == "-h"): 143f05cddf9SRui Paulo usage() 144f05cddf9SRui Paulo quit() 145f05cddf9SRui Paulo # Interface Name 146f05cddf9SRui Paulo elif (key == "-i"): 147f05cddf9SRui Paulo interface_name = value 148f05cddf9SRui Paulo # Timeout 149f05cddf9SRui Paulo elif (key == "-t"): 150f05cddf9SRui Paulo if ( int(value) >= 0): 151f05cddf9SRui Paulo timeout = value 152f05cddf9SRui Paulo else: 1534bc52338SCy Schubert print("Error:\n Timeout cannot be negative") 154f05cddf9SRui Paulo usage() 155f05cddf9SRui Paulo quit() 156f05cddf9SRui Paulo # Dbus interface 157f05cddf9SRui Paulo elif (key == "-w"): 158f05cddf9SRui Paulo wpas_dbus_interface = value 159f05cddf9SRui Paulo else: 160f05cddf9SRui Paulo assert False, "unhandled option" 161f05cddf9SRui Paulo 162f05cddf9SRui Paulo # Interface name is required and was not given 163f05cddf9SRui Paulo if (interface_name == None): 1644bc52338SCy Schubert print("Error:\n interface_name is required") 165f05cddf9SRui Paulo usage() 166f05cddf9SRui Paulo quit() 167f05cddf9SRui Paulo 168f05cddf9SRui Paulo # Constructor 169f05cddf9SRui Paulo try: 170f05cddf9SRui Paulo p2p_find_test = P2P_Find(interface_name, wpas_dbus_interface, timeout) 171f05cddf9SRui Paulo 172f05cddf9SRui Paulo except: 1734bc52338SCy Schubert print("Error:\n Invalid wpas_dbus_interface") 174f05cddf9SRui Paulo usage() 175f05cddf9SRui Paulo quit() 176f05cddf9SRui Paulo 177f05cddf9SRui Paulo # Start P2P_Find 178f05cddf9SRui Paulo p2p_find_test.start() 179f05cddf9SRui Paulo 180f05cddf9SRui Paulo try: 181f05cddf9SRui Paulo # If timeout is 0, then run forever 182f05cddf9SRui Paulo if (timeout == 0): 183f05cddf9SRui Paulo while(True): 184f05cddf9SRui Paulo pass 185f05cddf9SRui Paulo # Else sleep for (timeout) 186f05cddf9SRui Paulo else: 187f05cddf9SRui Paulo time.sleep(p2p_find_test.timeout) 188f05cddf9SRui Paulo 189f05cddf9SRui Paulo except: 190f05cddf9SRui Paulo pass 191f05cddf9SRui Paulo 192f05cddf9SRui Paulo quit() 193