tests: Add test cases for P2P+NFC
[mech_eap.git] / tests / hwsim / wpasupplicant.py
1 #!/usr/bin/python
2 #
3 # Python class for controlling wpa_supplicant
4 # Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
5 #
6 # This software may be distributed under the terms of the BSD license.
7 # See README for more details.
8
9 import os
10 import time
11 import logging
12 import re
13 import subprocess
14 import wpaspy
15
16 logger = logging.getLogger()
17 wpas_ctrl = '/var/run/wpa_supplicant'
18
19 class WpaSupplicant:
20     def __init__(self, ifname=None, global_iface=None):
21         self.group_ifname = None
22         if ifname:
23             self.set_ifname(ifname)
24         else:
25             self.ifname = None
26
27         self.global_iface = global_iface
28         if global_iface:
29             self.global_ctrl = wpaspy.Ctrl(global_iface)
30             self.global_mon = wpaspy.Ctrl(global_iface)
31             self.global_mon.attach()
32
33     def set_ifname(self, ifname):
34         self.ifname = ifname
35         self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
36         self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
37         self.mon.attach()
38
39     def remove_ifname(self):
40         if self.ifname:
41             self.mon.detach()
42             self.mon = None
43             self.ctrl = None
44             self.ifname = None
45
46     def interface_add(self, ifname, driver="nl80211", drv_params=None):
47         try:
48             groups = subprocess.check_output(["id"])
49             group = "admin" if "(admin)" in groups else "adm"
50         except Exception, e:
51             group = "admin"
52         cmd = "INTERFACE_ADD " + ifname + "\t\t" + driver + "\tDIR=/var/run/wpa_supplicant GROUP=" + group
53         if drv_params:
54             cmd = cmd + '\t' + drv_params
55         if "FAIL" in self.global_request(cmd):
56             raise Exception("Failed to add a dynamic wpa_supplicant interface")
57         self.set_ifname(ifname)
58
59     def interface_remove(self, ifname):
60         self.remove_ifname()
61         self.global_request("INTERFACE_REMOVE " + ifname)
62
63     def request(self, cmd):
64         logger.debug(self.ifname + ": CTRL: " + cmd)
65         return self.ctrl.request(cmd)
66
67     def global_request(self, cmd):
68         if self.global_iface is None:
69             self.request(cmd)
70         else:
71             ifname = self.ifname or self.global_iface
72             logger.debug(ifname + ": CTRL: " + cmd)
73             return self.global_ctrl.request(cmd)
74
75     def group_request(self, cmd):
76         if self.group_ifname and self.group_ifname != self.ifname:
77             logger.debug(self.group_ifname + ": CTRL: " + cmd)
78             gctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
79             return gctrl.request(cmd)
80         return self.request(cmd)
81
82     def ping(self):
83         return "PONG" in self.request("PING")
84
85     def reset(self):
86         res = self.request("FLUSH")
87         if not "OK" in res:
88             logger.info("FLUSH to " + self.ifname + " failed: " + res)
89         self.request("SET external_sim 0")
90         self.request("SET hessid 00:00:00:00:00:00")
91         self.request("SET access_network_type 15")
92         self.request("SET p2p_add_cli_chan 0")
93         self.request("SET p2p_no_go_freq ")
94         self.request("SET p2p_pref_chan ")
95         self.request("SET p2p_no_group_iface 1")
96         self.request("SET p2p_go_intent 7")
97         self.group_ifname = None
98         self.dump_monitor()
99
100         iter = 0
101         while iter < 60:
102             state = self.get_driver_status_field("scan_state")
103             if "SCAN_STARTED" in state or "SCAN_REQUESTED" in state:
104                 logger.info(self.ifname + ": Waiting for scan operation to complete before continuing")
105                 time.sleep(1)
106             else:
107                 break
108             iter = iter + 1
109         if iter == 60:
110             logger.error(self.ifname + ": Driver scan state did not clear")
111             print "Trying to clear cfg80211/mac80211 scan state"
112             try:
113                 cmd = ["sudo", "ifconfig", self.ifname, "down"]
114                 subprocess.call(cmd)
115             except subprocess.CalledProcessError, e:
116                 logger.info("ifconfig failed: " + str(e.returncode))
117                 logger.info(e.output)
118             try:
119                 cmd = ["sudo", "ifconfig", self.ifname, "up"]
120                 subprocess.call(cmd)
121             except subprocess.CalledProcessError, e:
122                 logger.info("ifconfig failed: " + str(e.returncode))
123                 logger.info(e.output)
124         if iter > 0:
125             # The ongoing scan could have discovered BSSes or P2P peers
126             logger.info("Run FLUSH again since scan was in progress")
127             self.request("FLUSH")
128             self.dump_monitor()
129
130         if not self.ping():
131             logger.info("No PING response from " + self.ifname + " after reset")
132
133     def add_network(self):
134         id = self.request("ADD_NETWORK")
135         if "FAIL" in id:
136             raise Exception("ADD_NETWORK failed")
137         return int(id)
138
139     def remove_network(self, id):
140         id = self.request("REMOVE_NETWORK " + str(id))
141         if "FAIL" in id:
142             raise Exception("REMOVE_NETWORK failed")
143         return None
144
145     def set_network(self, id, field, value):
146         res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
147         if "FAIL" in res:
148             raise Exception("SET_NETWORK failed")
149         return None
150
151     def set_network_quoted(self, id, field, value):
152         res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
153         if "FAIL" in res:
154             raise Exception("SET_NETWORK failed")
155         return None
156
157     def list_networks(self):
158         res = self.request("LIST_NETWORKS")
159         lines = res.splitlines()
160         networks = []
161         for l in lines:
162             if "network id" in l:
163                 continue
164             [id,ssid,bssid,flags] = l.split('\t')
165             network = {}
166             network['id'] = id
167             network['ssid'] = ssid
168             network['bssid'] = bssid
169             network['flags'] = flags
170             networks.append(network)
171         return networks
172
173     def hs20_enable(self):
174         self.request("SET interworking 1")
175         self.request("SET hs20 1")
176
177     def add_cred(self):
178         id = self.request("ADD_CRED")
179         if "FAIL" in id:
180             raise Exception("ADD_CRED failed")
181         return int(id)
182
183     def remove_cred(self, id):
184         id = self.request("REMOVE_CRED " + str(id))
185         if "FAIL" in id:
186             raise Exception("REMOVE_CRED failed")
187         return None
188
189     def set_cred(self, id, field, value):
190         res = self.request("SET_CRED " + str(id) + " " + field + " " + value)
191         if "FAIL" in res:
192             raise Exception("SET_CRED failed")
193         return None
194
195     def set_cred_quoted(self, id, field, value):
196         res = self.request("SET_CRED " + str(id) + " " + field + ' "' + value + '"')
197         if "FAIL" in res:
198             raise Exception("SET_CRED failed")
199         return None
200
201     def add_cred_values(self, params):
202         id = self.add_cred()
203
204         quoted = [ "realm", "username", "password", "domain", "imsi",
205                    "excluded_ssid", "milenage", "ca_cert", "client_cert",
206                    "private_key" ]
207         for field in quoted:
208             if field in params:
209                 self.set_cred_quoted(id, field, params[field])
210
211         not_quoted = [ "eap", "roaming_consortium",
212                        "required_roaming_consortium" ]
213         for field in not_quoted:
214             if field in params:
215                 self.set_cred(id, field, params[field])
216
217         return id;
218
219     def select_network(self, id):
220         id = self.request("SELECT_NETWORK " + str(id))
221         if "FAIL" in id:
222             raise Exception("SELECT_NETWORK failed")
223         return None
224
225     def connect_network(self, id, timeout=10):
226         self.dump_monitor()
227         self.select_network(id)
228         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=timeout)
229         if ev is None:
230             raise Exception("Association with the AP timed out")
231         self.dump_monitor()
232
233     def get_status(self):
234         res = self.request("STATUS")
235         lines = res.splitlines()
236         vals = dict()
237         for l in lines:
238             [name,value] = l.split('=', 1)
239             vals[name] = value
240         return vals
241
242     def get_status_field(self, field):
243         vals = self.get_status()
244         if field in vals:
245             return vals[field]
246         return None
247
248     def get_group_status(self):
249         res = self.group_request("STATUS")
250         lines = res.splitlines()
251         vals = dict()
252         for l in lines:
253             [name,value] = l.split('=', 1)
254             vals[name] = value
255         return vals
256
257     def get_group_status_field(self, field):
258         vals = self.get_group_status()
259         if field in vals:
260             return vals[field]
261         return None
262
263     def get_driver_status(self):
264         res = self.request("STATUS-DRIVER")
265         lines = res.splitlines()
266         vals = dict()
267         for l in lines:
268             [name,value] = l.split('=', 1)
269             vals[name] = value
270         return vals
271
272     def get_driver_status_field(self, field):
273         vals = self.get_driver_status()
274         if field in vals:
275             return vals[field]
276         return None
277
278     def p2p_dev_addr(self):
279         return self.get_status_field("p2p_device_address")
280
281     def p2p_interface_addr(self):
282         return self.get_group_status_field("address")
283
284     def p2p_listen(self):
285         return self.global_request("P2P_LISTEN")
286
287     def p2p_find(self, social=False, dev_id=None, dev_type=None):
288         cmd = "P2P_FIND"
289         if social:
290             cmd = cmd + " type=social"
291         if dev_id:
292             cmd = cmd + " dev_id=" + dev_id
293         if dev_type:
294             cmd = cmd + " dev_type=" + dev_type
295         return self.global_request(cmd)
296
297     def p2p_stop_find(self):
298         return self.global_request("P2P_STOP_FIND")
299
300     def wps_read_pin(self):
301         #TODO: make this random
302         self.pin = "12345670"
303         return self.pin
304
305     def peer_known(self, peer, full=True):
306         res = self.global_request("P2P_PEER " + peer)
307         if peer.lower() not in res.lower():
308             return False
309         if not full:
310             return True
311         return "[PROBE_REQ_ONLY]" not in res
312
313     def discover_peer(self, peer, full=True, timeout=15, social=True, force_find=False):
314         logger.info(self.ifname + ": Trying to discover peer " + peer)
315         if not force_find and self.peer_known(peer, full):
316             return True
317         self.p2p_find(social)
318         count = 0
319         while count < timeout:
320             time.sleep(1)
321             count = count + 1
322             if self.peer_known(peer, full):
323                 return True
324         return False
325
326     def get_peer(self, peer):
327         res = self.global_request("P2P_PEER " + peer)
328         if peer.lower() not in res.lower():
329             raise Exception("Peer information not available")
330         lines = res.splitlines()
331         vals = dict()
332         for l in lines:
333             if '=' in l:
334                 [name,value] = l.split('=', 1)
335                 vals[name] = value
336         return vals
337
338     def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
339         if expect_failure:
340             if "P2P-GROUP-STARTED" in ev:
341                 raise Exception("Group formation succeeded when expecting failure")
342             exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
343             s = re.split(exp, ev)
344             if len(s) < 3:
345                 return None
346             res = {}
347             res['result'] = 'go-neg-failed'
348             res['status'] = int(s[2])
349             return res
350
351         if "P2P-GROUP-STARTED" not in ev:
352             raise Exception("No P2P-GROUP-STARTED event seen")
353
354         exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*) ip_addr=([0-9.]*) ip_mask=([0-9.]*) go_ip_addr=([0-9.]*)'
355         s = re.split(exp, ev)
356         if len(s) < 11:
357             exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
358             s = re.split(exp, ev)
359             if len(s) < 8:
360                 raise Exception("Could not parse P2P-GROUP-STARTED")
361         res = {}
362         res['result'] = 'success'
363         res['ifname'] = s[2]
364         self.group_ifname = s[2]
365         res['role'] = s[3]
366         res['ssid'] = s[4]
367         res['freq'] = s[5]
368         if "[PERSISTENT]" in ev:
369             res['persistent'] = True
370         else:
371             res['persistent'] = False
372         p = re.match(r'psk=([0-9a-f]*)', s[6])
373         if p:
374             res['psk'] = p.group(1)
375         p = re.match(r'passphrase="(.*)"', s[6])
376         if p:
377             res['passphrase'] = p.group(1)
378         res['go_dev_addr'] = s[7]
379
380         if len(s) > 8 and len(s[8]) > 0:
381             res['ip_addr'] = s[8]
382         if len(s) > 9:
383             res['ip_mask'] = s[9]
384         if len(s) > 10:
385             res['go_ip_addr'] = s[10]
386
387         if go_neg_res:
388             exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
389             s = re.split(exp, go_neg_res)
390             if len(s) < 4:
391                 raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
392             res['go_neg_role'] = s[2]
393             res['go_neg_freq'] = s[3]
394
395         return res
396
397     def p2p_go_neg_auth(self, peer, pin, method, go_intent=None, persistent=False, freq=None):
398         if not self.discover_peer(peer):
399             raise Exception("Peer " + peer + " not found")
400         self.dump_monitor()
401         cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
402         if go_intent:
403             cmd = cmd + ' go_intent=' + str(go_intent)
404         if freq:
405             cmd = cmd + ' freq=' + str(freq)
406         if persistent:
407             cmd = cmd + " persistent"
408         if "OK" in self.global_request(cmd):
409             return None
410         raise Exception("P2P_CONNECT (auth) failed")
411
412     def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
413         go_neg_res = None
414         ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
415                                      "P2P-GO-NEG-FAILURE"], timeout);
416         if ev is None:
417             if expect_failure:
418                 return None
419             raise Exception("Group formation timed out")
420         if "P2P-GO-NEG-SUCCESS" in ev:
421             go_neg_res = ev
422             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
423             if ev is None:
424                 if expect_failure:
425                     return None
426                 raise Exception("Group formation timed out")
427         self.dump_monitor()
428         return self.group_form_result(ev, expect_failure, go_neg_res)
429
430     def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None, expect_failure=False, persistent=False, freq=None):
431         if not self.discover_peer(peer):
432             raise Exception("Peer " + peer + " not found")
433         self.dump_monitor()
434         if pin:
435             cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
436         else:
437             cmd = "P2P_CONNECT " + peer + " " + method
438         if go_intent:
439             cmd = cmd + ' go_intent=' + str(go_intent)
440         if freq:
441             cmd = cmd + ' freq=' + str(freq)
442         if persistent:
443             cmd = cmd + " persistent"
444         if "OK" in self.global_request(cmd):
445             if timeout == 0:
446                 self.dump_monitor()
447                 return None
448             go_neg_res = None
449             ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
450                                          "P2P-GO-NEG-FAILURE"], timeout)
451             if ev is None:
452                 if expect_failure:
453                     return None
454                 raise Exception("Group formation timed out")
455             if "P2P-GO-NEG-SUCCESS" in ev:
456                 go_neg_res = ev
457                 ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
458                 if ev is None:
459                     if expect_failure:
460                         return None
461                     raise Exception("Group formation timed out")
462             self.dump_monitor()
463             return self.group_form_result(ev, expect_failure, go_neg_res)
464         raise Exception("P2P_CONNECT failed")
465
466     def wait_event(self, events, timeout=10):
467         start = os.times()[4]
468         while True:
469             while self.mon.pending():
470                 ev = self.mon.recv()
471                 logger.debug(self.ifname + ": " + ev)
472                 for event in events:
473                     if event in ev:
474                         return ev
475             now = os.times()[4]
476             remaining = start + timeout - now
477             if remaining <= 0:
478                 break
479             if not self.mon.pending(timeout=remaining):
480                 break
481         return None
482
483     def wait_global_event(self, events, timeout):
484         if self.global_iface is None:
485             self.wait_event(events, timeout)
486         else:
487             start = os.times()[4]
488             while True:
489                 while self.global_mon.pending():
490                     ev = self.global_mon.recv()
491                     logger.debug(self.ifname + "(global): " + ev)
492                     for event in events:
493                         if event in ev:
494                             return ev
495                 now = os.times()[4]
496                 remaining = start + timeout - now
497                 if remaining <= 0:
498                     break
499                 if not self.global_mon.pending(timeout=remaining):
500                     break
501         return None
502
503     def wait_go_ending_session(self):
504         ev = self.wait_event(["P2P-GROUP-REMOVED"], timeout=3)
505         if ev is None:
506             raise Exception("Group removal event timed out")
507         if "reason=GO_ENDING_SESSION" not in ev:
508             raise Exception("Unexpected group removal reason")
509
510     def dump_monitor(self):
511         while self.mon.pending():
512             ev = self.mon.recv()
513             logger.debug(self.ifname + ": " + ev)
514         while self.global_mon.pending():
515             ev = self.global_mon.recv()
516             logger.debug(self.ifname + "(global): " + ev)
517
518     def remove_group(self, ifname=None):
519         if ifname is None:
520             ifname = self.group_ifname if self.group_ifname else self.ifname
521         if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
522             raise Exception("Group could not be removed")
523         self.group_ifname = None
524
525     def p2p_start_go(self, persistent=None, freq=None):
526         self.dump_monitor()
527         cmd = "P2P_GROUP_ADD"
528         if persistent is None:
529             pass
530         elif persistent is True:
531             cmd = cmd + " persistent"
532         else:
533             cmd = cmd + " persistent=" + str(persistent)
534         if freq:
535             cmd = cmd + " freq=" + str(freq)
536         if "OK" in self.global_request(cmd):
537             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
538             if ev is None:
539                 raise Exception("GO start up timed out")
540             self.dump_monitor()
541             return self.group_form_result(ev)
542         raise Exception("P2P_GROUP_ADD failed")
543
544     def p2p_go_authorize_client(self, pin):
545         cmd = "WPS_PIN any " + pin
546         if "FAIL" in self.group_request(cmd):
547             raise Exception("Failed to authorize client connection on GO")
548         return None
549
550     def p2p_go_authorize_client_pbc(self):
551         cmd = "WPS_PBC"
552         if "FAIL" in self.group_request(cmd):
553             raise Exception("Failed to authorize client connection on GO")
554         return None
555
556     def p2p_connect_group(self, go_addr, pin, timeout=0, social=False):
557         self.dump_monitor()
558         if not self.discover_peer(go_addr, social=social):
559             raise Exception("GO " + go_addr + " not found")
560         self.dump_monitor()
561         cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
562         if "OK" in self.global_request(cmd):
563             if timeout == 0:
564                 self.dump_monitor()
565                 return None
566             ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
567             if ev is None:
568                 raise Exception("Joining the group timed out")
569             self.dump_monitor()
570             return self.group_form_result(ev)
571         raise Exception("P2P_CONNECT(join) failed")
572
573     def tdls_setup(self, peer):
574         cmd = "TDLS_SETUP " + peer
575         if "FAIL" in self.group_request(cmd):
576             raise Exception("Failed to request TDLS setup")
577         return None
578
579     def tdls_teardown(self, peer):
580         cmd = "TDLS_TEARDOWN " + peer
581         if "FAIL" in self.group_request(cmd):
582             raise Exception("Failed to request TDLS teardown")
583         return None
584
585     def connect(self, ssid=None, ssid2=None, psk=None, proto=None,
586                 key_mgmt=None, wep_key0=None,
587                 ieee80211w=None, pairwise=None, group=None, scan_freq=None,
588                 eap=None, identity=None, anonymous_identity=None,
589                 password=None, phase1=None, phase2=None, ca_cert=None,
590                 domain_suffix_match=None, password_hex=None,
591                 client_cert=None, private_key=None, peerkey=False, okc=False,
592                 eapol_flags=None, fragment_size=None,
593                 wait_connect=True, only_add_network=False,
594                 ca_cert2=None, client_cert2=None, private_key2=None,
595                 scan_ssid=None, raw_psk=None, pac_file=None,
596                 subject_match=None, altsubject_match=None,
597                 private_key_passwd=None, ocsp=None):
598         logger.info("Connect STA " + self.ifname + " to AP")
599         id = self.add_network()
600         if ssid:
601             self.set_network_quoted(id, "ssid", ssid)
602         elif ssid2:
603             self.set_network(id, "ssid", ssid2)
604         if psk:
605             self.set_network_quoted(id, "psk", psk)
606         if raw_psk:
607             self.set_network(id, "psk", raw_psk)
608         if proto:
609             self.set_network(id, "proto", proto)
610         if key_mgmt:
611             self.set_network(id, "key_mgmt", key_mgmt)
612         if ieee80211w:
613             self.set_network(id, "ieee80211w", ieee80211w)
614         if pairwise:
615             self.set_network(id, "pairwise", pairwise)
616         if group:
617             self.set_network(id, "group", group)
618         if wep_key0:
619             self.set_network(id, "wep_key0", wep_key0)
620         if scan_freq:
621             self.set_network(id, "scan_freq", scan_freq)
622         if eap:
623             self.set_network(id, "eap", eap)
624         if identity:
625             self.set_network_quoted(id, "identity", identity)
626         if anonymous_identity:
627             self.set_network_quoted(id, "anonymous_identity",
628                                     anonymous_identity)
629         if password:
630             self.set_network_quoted(id, "password", password)
631         if password_hex:
632             self.set_network(id, "password", password_hex)
633         if ca_cert:
634             self.set_network_quoted(id, "ca_cert", ca_cert)
635         if client_cert:
636             self.set_network_quoted(id, "client_cert", client_cert)
637         if private_key:
638             self.set_network_quoted(id, "private_key", private_key)
639         if private_key_passwd:
640             self.set_network_quoted(id, "private_key_passwd",
641                                     private_key_passwd)
642         if ca_cert2:
643             self.set_network_quoted(id, "ca_cert2", ca_cert2)
644         if client_cert2:
645             self.set_network_quoted(id, "client_cert2", client_cert2)
646         if private_key2:
647             self.set_network_quoted(id, "private_key2", private_key2)
648         if phase1:
649             self.set_network_quoted(id, "phase1", phase1)
650         if phase2:
651             self.set_network_quoted(id, "phase2", phase2)
652         if domain_suffix_match:
653             self.set_network_quoted(id, "domain_suffix_match",
654                                     domain_suffix_match)
655         if altsubject_match:
656             self.set_network_quoted(id, "altsubject_match",
657                                     altsubject_match)
658         if subject_match:
659             self.set_network_quoted(id, "subject_match",
660                                     subject_match)
661         if peerkey:
662             self.set_network(id, "peerkey", "1")
663         if okc:
664             self.set_network(id, "proactive_key_caching", "1")
665         if eapol_flags:
666             self.set_network(id, "eapol_flags", eapol_flags)
667         if fragment_size:
668             self.set_network(id, "fragment_size", fragment_size)
669         if scan_ssid:
670             self.set_network(id, "scan_ssid", scan_ssid)
671         if pac_file:
672             self.set_network_quoted(id, "pac_file", pac_file)
673         if ocsp:
674             self.set_network(id, "ocsp", str(ocsp))
675         if only_add_network:
676             return id
677         if wait_connect:
678             if eap:
679                 self.connect_network(id, timeout=20)
680             else:
681                 self.connect_network(id)
682         else:
683             self.dump_monitor()
684             self.select_network(id)
685         return id
686
687     def scan(self, type=None, freq=None, no_wait=False):
688         if type:
689             cmd = "SCAN TYPE=" + type
690         else:
691             cmd = "SCAN"
692         if freq:
693             cmd = cmd + " freq=" + freq
694         if not no_wait:
695             self.dump_monitor()
696         if not "OK" in self.request(cmd):
697             raise Exception("Failed to trigger scan")
698         if no_wait:
699             return
700         ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
701         if ev is None:
702             raise Exception("Scan timed out")
703
704     def roam(self, bssid):
705         self.dump_monitor()
706         self.request("ROAM " + bssid)
707         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
708         if ev is None:
709             raise Exception("Roaming with the AP timed out")
710         self.dump_monitor()
711
712     def roam_over_ds(self, bssid):
713         self.dump_monitor()
714         self.request("FT_DS " + bssid)
715         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
716         if ev is None:
717             raise Exception("Roaming with the AP timed out")
718         self.dump_monitor()
719
720     def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
721                 new_passphrase=None, no_wait=False):
722         self.dump_monitor()
723         if new_ssid:
724             self.request("WPS_REG " + bssid + " " + pin + " " +
725                          new_ssid.encode("hex") + " " + key_mgmt + " " +
726                          cipher + " " + new_passphrase.encode("hex"))
727             if no_wait:
728                 return
729             ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
730         else:
731             self.request("WPS_REG " + bssid + " " + pin)
732             if no_wait:
733                 return
734             ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
735             if ev is None:
736                 raise Exception("WPS cred timed out")
737             ev = self.wait_event(["WPS-FAIL"], timeout=15)
738         if ev is None:
739             raise Exception("WPS timed out")
740         ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
741         if ev is None:
742             raise Exception("Association with the AP timed out")
743
744     def relog(self):
745         self.request("RELOG")
746
747     def wait_completed(self, timeout=10):
748         for i in range(0, timeout * 2):
749             if self.get_status_field("wpa_state") == "COMPLETED":
750                 return
751             time.sleep(0.5)
752         raise Exception("Timeout while waiting for COMPLETED state")
753
754     def get_capability(self, field):
755         res = self.request("GET_CAPABILITY " + field)
756         if "FAIL" in res:
757             return None
758         return res.split(' ')
759
760     def get_bss(self, bssid):
761         res = self.request("BSS " + bssid)
762         lines = res.splitlines()
763         vals = dict()
764         for l in lines:
765             [name,value] = l.split('=', 1)
766             vals[name] = value
767         return vals
768
769     def get_pmksa(self, bssid):
770         res = self.request("PMKSA")
771         lines = res.splitlines()
772         for l in lines:
773             if bssid not in l:
774                 continue
775             vals = dict()
776             [index,aa,pmkid,expiration,opportunistic] = l.split(' ')
777             vals['index'] = index
778             vals['pmkid'] = pmkid
779             vals['expiration'] = expiration
780             vals['opportunistic'] = opportunistic
781             return vals
782         return None