3 # Python class for controlling wpa_supplicant
4 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
14 logger = logging.getLogger(__name__)
15 wpas_ctrl = '/var/run/wpa_supplicant'
18 def __init__(self, ifname):
20 self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
21 self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
24 def request(self, cmd):
25 logger.debug(self.ifname + ": CTRL: " + cmd)
26 return self.ctrl.request(cmd)
29 return "PONG" in self.request("PING")
32 self.request("P2P_STOP_FIND")
33 self.request("P2P_FLUSH")
34 self.request("P2P_GROUP_REMOVE *")
35 self.request("REMOVE_NETWORK *")
36 self.request("REMOVE_CRED *")
38 def get_status(self, field):
39 res = self.request("STATUS")
40 lines = res.splitlines()
42 [name,value] = l.split('=', 1)
47 def p2p_dev_addr(self):
48 return self.get_status("p2p_device_address")
51 return self.request("P2P_LISTEN")
53 def p2p_find(self, social=False):
55 return self.request("P2P_FIND type=social")
56 return self.request("P2P_FIND")
58 def wps_read_pin(self):
59 #TODO: make this random
63 def peer_known(self, peer, full=True):
64 res = self.request("P2P_PEER " + peer)
65 if peer.lower() not in res.lower():
69 return "[PROBE_REQ_ONLY]" not in res
71 def discover_peer(self, peer, full=True, timeout=15):
72 logger.info(self.ifname + ": Trying to discover peer " + peer)
73 if self.peer_known(peer, full):
77 while count < timeout:
80 if self.peer_known(peer, full):
84 def p2p_go_neg_auth(self, peer, pin, method):
85 if not self.discover_peer(peer):
86 raise Exception("Peer " + peer + " not found")
88 cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
89 if "OK" in self.request(cmd):
91 raise Exception("P2P_CONNECT (auth) failed")
93 def p2p_go_neg_init(self, peer, pin, method, timeout=0):
94 if not self.discover_peer(peer):
95 raise Exception("Peer " + peer + " not found")
97 cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
98 if "OK" in self.request(cmd):
102 if self.wait_event("P2P-GROUP-STARTED", timeout):
105 raise Exception("Group formation timed out")
106 raise Exception("P2P_CONNECT failed")
108 def wait_event(self, event, timeout):
110 while count < timeout * 2:
113 while self.mon.pending():
119 def dump_monitor(self):
120 while self.mon.pending():
122 logger.debug(self.ifname + ": " + ev)
124 def remove_group(self, ifname=None):
127 if "OK" not in self.request("P2P_GROUP_REMOVE " + ifname):
128 raise Exception("Group could not be removed")