1#!/usr/bin/python 2# Tests P2P_Find 3# Will listen 4# Then Program will exit 5######### MAY NEED TO RUN AS SUDO ############# 6 7import dbus 8import sys, os 9import time 10import gobject 11import threading 12import getopt 13from dbus.mainloop.glib import DBusGMainLoop 14 15def usage(): 16 print "Usage:" 17 print " %s -i <interface_name> [-t <timeout>] \ " \ 18 % sys.argv[0] 19 print " [-w <wpas_dbus_interface>]" 20 print "Options:" 21 print " -i = interface name" 22 print " -t = timeout = 0s (infinite)" 23 print " -w = wpas dbus interface = fi.w1.wpa_supplicant1" 24 print "Example:" 25 print " %s -i wlan0 -t 5" % sys.argv[0] 26 27# Required Signals 28def p2pStateChange(status): 29 print status 30 31class P2P_Listen(threading.Thread): 32 # Needed Variables 33 global bus 34 global wpas_object 35 global interface_object 36 global p2p_interface 37 global interface_name 38 global wpas 39 global wpas_dbus_interface 40 global path 41 global timeout 42 43 # Dbus Paths 44 global wpas_dbus_opath 45 global wpas_dbus_interfaces_opath 46 global wpas_dbus_interfaces_interface 47 global wpas_dbus_interfaces_p2pdevice 48 49 # Constructor 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 55 56 # Initializes thread and daemon allows for ctrl-c kill 57 threading.Thread.__init__(self) 58 self.daemon = True 59 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 + \ 64 "/Interfaces" 65 self.wpas_dbus_interfaces_interface = \ 66 self.wpas_dbus_interface + ".Interface" 67 self.wpas_dbus_interfaces_p2pdevice = \ 68 self.wpas_dbus_interfaces_interface \ 69 + ".P2PDevice" 70 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, 76 self.wpas_dbus_opath) 77 self.wpas = dbus.Interface(self.wpas_object, 78 self.wpas_dbus_interface) 79 80 # Try to see if supplicant knows about interface 81 # If not, throw an exception 82 try: 83 self.path = self.wpas.GetInterface( 84 self.interface_name) 85 except dbus.DBusException, exc: 86 error = 'Error:\n Interface ' + self.interface_name \ 87 + ' was not found' 88 print error 89 usage() 90 os._exit(0) 91 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) 96 97 self.bus.add_signal_receiver(p2pStateChange, 98 dbus_interface=self.wpas_dbus_interfaces_p2pdevice, 99 signal_name="P2PStateChanged") 100 101 # Run p2p_find 102 def run(self): 103 # Sets up p2p_listen 104 self.p2p_interface.Listen(int(self.timeout)) 105 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() 111 112if __name__ == "__main__": 113 114 # Defaults for optional inputs 115 timeout = 0 116 wpas_dbus_interface = 'fi.w1.wpa_supplicant1' 117 118 # interface_name is required 119 interface_name = None 120 121 # Using getopts to handle options 122 try: 123 options, args = getopt.getopt(sys.argv[1:],"hi:t:w:") 124 125 except getopt.GetoptError: 126 usage() 127 quit() 128 129 # If theres a switch, override default option 130 for key, value in options: 131 # Help 132 if (key == "-h"): 133 usage() 134 quit() 135 # Interface Name 136 elif (key == "-i"): 137 interface_name = value 138 # Timeout 139 elif (key == "-t"): 140 if ( int(value) >= 0): 141 timeout = value 142 else: 143 print "Error:\n Timeout cannot be negative" 144 usage() 145 quit() 146 # Dbus interface 147 elif (key == "-w"): 148 wpas_dbus_interface = value 149 else: 150 assert False, "unhandled option" 151 152 # Interface name is required and was not given 153 if (interface_name == None): 154 print "Error:\n interface_name is required" 155 usage() 156 quit() 157 158 # Constructor 159 try: 160 p2p_listen_test = P2P_Listen(interface_name, wpas_dbus_interface, timeout) 161 162 except: 163 print "Error:\n Invalid wpas_dbus_interface" 164 usage() 165 quit() 166 167 # Start P2P_Find 168 p2p_listen_test.start() 169 170 try: 171 # If timeout is 0, then run forever 172 if (int(p2p_listen_test.timeout) == 0): 173 while(True): 174 pass 175 # Else sleep for (timeout) 176 else: 177 time.sleep(int(p2p_listen_test.timeout)) 178 179 except: 180 pass 181 182 quit() 183