wpasupplicant.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #!/usr/bin/python
  2. #
  3. # Python class for controlling wpa_supplicant
  4. # Copyright (c) 2013, 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. import os
  9. import time
  10. import logging
  11. import re
  12. import subprocess
  13. import wpaspy
  14. logger = logging.getLogger()
  15. wpas_ctrl = '/var/run/wpa_supplicant'
  16. class WpaSupplicant:
  17. def __init__(self, ifname, global_iface=None):
  18. self.ifname = ifname
  19. self.group_ifname = None
  20. self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
  21. self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
  22. self.mon.attach()
  23. self.global_iface = global_iface
  24. if global_iface:
  25. self.global_ctrl = wpaspy.Ctrl(global_iface)
  26. self.global_mon = wpaspy.Ctrl(global_iface)
  27. self.global_mon.attach()
  28. def request(self, cmd):
  29. logger.debug(self.ifname + ": CTRL: " + cmd)
  30. return self.ctrl.request(cmd)
  31. def global_request(self, cmd):
  32. if self.global_iface is None:
  33. self.request(cmd)
  34. else:
  35. logger.debug(self.ifname + ": CTRL: " + cmd)
  36. return self.global_ctrl.request(cmd)
  37. def group_request(self, cmd):
  38. if self.group_ifname and self.group_ifname != self.ifname:
  39. logger.debug(self.group_ifname + ": CTRL: " + cmd)
  40. gctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, self.group_ifname))
  41. return gctrl.request(cmd)
  42. return self.request(cmd)
  43. def ping(self):
  44. return "PONG" in self.request("PING")
  45. def reset(self):
  46. res = self.request("FLUSH")
  47. if not "OK" in res:
  48. logger.info("FLUSH to " + self.ifname + " failed: " + res)
  49. self.request("SET ignore_old_scan_res 0")
  50. self.request("SET external_sim 0")
  51. self.request("SET p2p_add_cli_chan 0")
  52. self.request("SET p2p_no_go_freq ")
  53. self.request("SET p2p_pref_chan ")
  54. self.request("SET disallow_aps ")
  55. self.request("SET p2p_no_group_iface 1")
  56. self.request("P2P_SET per_sta_psk 0")
  57. self.request("P2P_SET disabled 0")
  58. self.request("P2P_SERVICE_FLUSH")
  59. self.group_ifname = None
  60. self.dump_monitor()
  61. iter = 0
  62. while iter < 60:
  63. state = self.get_driver_status_field("scan_state")
  64. if "SCAN_STARTED" in state or "SCAN_REQUESTED" in state:
  65. logger.info(self.ifname + ": Waiting for scan operation to complete before continuing")
  66. time.sleep(1)
  67. else:
  68. break
  69. iter = iter + 1
  70. if iter == 60:
  71. logger.error(self.ifname + ": Driver scan state did not clear")
  72. print "Trying to clear cfg80211/mac80211 scan state"
  73. try:
  74. cmd = ["sudo", "ifconfig", self.ifname, "down"]
  75. subprocess.call(cmd)
  76. except subprocess.CalledProcessError, e:
  77. logger.info("ifconfig failed: " + str(e.returncode))
  78. logger.info(e.output)
  79. try:
  80. cmd = ["sudo", "ifconfig", self.ifname, "up"]
  81. subprocess.call(cmd)
  82. except subprocess.CalledProcessError, e:
  83. logger.info("ifconfig failed: " + str(e.returncode))
  84. logger.info(e.output)
  85. if not self.ping():
  86. logger.info("No PING response from " + self.ifname + " after reset")
  87. def add_network(self):
  88. id = self.request("ADD_NETWORK")
  89. if "FAIL" in id:
  90. raise Exception("ADD_NETWORK failed")
  91. return int(id)
  92. def remove_network(self, id):
  93. id = self.request("REMOVE_NETWORK " + str(id))
  94. if "FAIL" in id:
  95. raise Exception("REMOVE_NETWORK failed")
  96. return None
  97. def set_network(self, id, field, value):
  98. res = self.request("SET_NETWORK " + str(id) + " " + field + " " + value)
  99. if "FAIL" in res:
  100. raise Exception("SET_NETWORK failed")
  101. return None
  102. def set_network_quoted(self, id, field, value):
  103. res = self.request("SET_NETWORK " + str(id) + " " + field + ' "' + value + '"')
  104. if "FAIL" in res:
  105. raise Exception("SET_NETWORK failed")
  106. return None
  107. def list_networks(self):
  108. res = self.request("LIST_NETWORKS")
  109. lines = res.splitlines()
  110. networks = []
  111. for l in lines:
  112. if "network id" in l:
  113. continue
  114. [id,ssid,bssid,flags] = l.split('\t')
  115. network = {}
  116. network['id'] = id
  117. network['ssid'] = ssid
  118. network['bssid'] = bssid
  119. network['flags'] = flags
  120. networks.append(network)
  121. return networks
  122. def hs20_enable(self):
  123. self.request("SET interworking 1")
  124. self.request("SET hs20 1")
  125. def add_cred(self):
  126. id = self.request("ADD_CRED")
  127. if "FAIL" in id:
  128. raise Exception("ADD_CRED failed")
  129. return int(id)
  130. def remove_cred(self, id):
  131. id = self.request("REMOVE_CRED " + str(id))
  132. if "FAIL" in id:
  133. raise Exception("REMOVE_CRED failed")
  134. return None
  135. def set_cred(self, id, field, value):
  136. res = self.request("SET_CRED " + str(id) + " " + field + " " + value)
  137. if "FAIL" in res:
  138. raise Exception("SET_CRED failed")
  139. return None
  140. def set_cred_quoted(self, id, field, value):
  141. res = self.request("SET_CRED " + str(id) + " " + field + ' "' + value + '"')
  142. if "FAIL" in res:
  143. raise Exception("SET_CRED failed")
  144. return None
  145. def add_cred_values(self, params):
  146. id = self.add_cred()
  147. quoted = [ "realm", "username", "password", "domain", "imsi",
  148. "excluded_ssid" ]
  149. for field in quoted:
  150. if field in params:
  151. self.set_cred_quoted(id, field, params[field])
  152. not_quoted = [ "eap", "required_roaming_consortium" ]
  153. for field in not_quoted:
  154. if field in params:
  155. self.set_cred(id, field, params[field])
  156. return id;
  157. def select_network(self, id):
  158. id = self.request("SELECT_NETWORK " + str(id))
  159. if "FAIL" in id:
  160. raise Exception("SELECT_NETWORK failed")
  161. return None
  162. def connect_network(self, id):
  163. self.dump_monitor()
  164. self.select_network(id)
  165. ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
  166. if ev is None:
  167. raise Exception("Association with the AP timed out")
  168. self.dump_monitor()
  169. def get_status(self):
  170. res = self.request("STATUS")
  171. lines = res.splitlines()
  172. vals = dict()
  173. for l in lines:
  174. [name,value] = l.split('=', 1)
  175. vals[name] = value
  176. return vals
  177. def get_status_field(self, field):
  178. vals = self.get_status()
  179. if field in vals:
  180. return vals[field]
  181. return None
  182. def get_group_status(self):
  183. res = self.group_request("STATUS")
  184. lines = res.splitlines()
  185. vals = dict()
  186. for l in lines:
  187. [name,value] = l.split('=', 1)
  188. vals[name] = value
  189. return vals
  190. def get_group_status_field(self, field):
  191. vals = self.get_group_status()
  192. if field in vals:
  193. return vals[field]
  194. return None
  195. def get_driver_status(self):
  196. res = self.request("STATUS-DRIVER")
  197. lines = res.splitlines()
  198. vals = dict()
  199. for l in lines:
  200. [name,value] = l.split('=', 1)
  201. vals[name] = value
  202. return vals
  203. def get_driver_status_field(self, field):
  204. vals = self.get_driver_status()
  205. if field in vals:
  206. return vals[field]
  207. return None
  208. def p2p_dev_addr(self):
  209. return self.get_status_field("p2p_device_address")
  210. def p2p_interface_addr(self):
  211. return self.get_group_status_field("address")
  212. def p2p_listen(self):
  213. return self.global_request("P2P_LISTEN")
  214. def p2p_find(self, social=False):
  215. if social:
  216. return self.global_request("P2P_FIND type=social")
  217. return self.global_request("P2P_FIND")
  218. def p2p_stop_find(self):
  219. return self.global_request("P2P_STOP_FIND")
  220. def wps_read_pin(self):
  221. #TODO: make this random
  222. self.pin = "12345670"
  223. return self.pin
  224. def peer_known(self, peer, full=True):
  225. res = self.global_request("P2P_PEER " + peer)
  226. if peer.lower() not in res.lower():
  227. return False
  228. if not full:
  229. return True
  230. return "[PROBE_REQ_ONLY]" not in res
  231. def discover_peer(self, peer, full=True, timeout=15, social=True):
  232. logger.info(self.ifname + ": Trying to discover peer " + peer)
  233. if self.peer_known(peer, full):
  234. return True
  235. self.p2p_find(social)
  236. count = 0
  237. while count < timeout:
  238. time.sleep(1)
  239. count = count + 1
  240. if self.peer_known(peer, full):
  241. return True
  242. return False
  243. def get_peer(self, peer):
  244. res = self.global_request("P2P_PEER " + peer)
  245. if peer.lower() not in res.lower():
  246. raise Exception("Peer information not available")
  247. lines = res.splitlines()
  248. vals = dict()
  249. for l in lines:
  250. if '=' in l:
  251. [name,value] = l.split('=', 1)
  252. vals[name] = value
  253. return vals
  254. def group_form_result(self, ev, expect_failure=False, go_neg_res=None):
  255. if expect_failure:
  256. if "P2P-GROUP-STARTED" in ev:
  257. raise Exception("Group formation succeeded when expecting failure")
  258. exp = r'<.>(P2P-GO-NEG-FAILURE) status=([0-9]*)'
  259. s = re.split(exp, ev)
  260. if len(s) < 3:
  261. return None
  262. res = {}
  263. res['result'] = 'go-neg-failed'
  264. res['status'] = int(s[2])
  265. return res
  266. if "P2P-GROUP-STARTED" not in ev:
  267. raise Exception("No P2P-GROUP-STARTED event seen")
  268. exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
  269. s = re.split(exp, ev)
  270. if len(s) < 8:
  271. raise Exception("Could not parse P2P-GROUP-STARTED")
  272. res = {}
  273. res['result'] = 'success'
  274. res['ifname'] = s[2]
  275. self.group_ifname = s[2]
  276. res['role'] = s[3]
  277. res['ssid'] = s[4]
  278. res['freq'] = s[5]
  279. if "[PERSISTENT]" in ev:
  280. res['persistent'] = True
  281. else:
  282. res['persistent'] = False
  283. p = re.match(r'psk=([0-9a-f]*)', s[6])
  284. if p:
  285. res['psk'] = p.group(1)
  286. p = re.match(r'passphrase="(.*)"', s[6])
  287. if p:
  288. res['passphrase'] = p.group(1)
  289. res['go_dev_addr'] = s[7]
  290. if go_neg_res:
  291. exp = r'<.>(P2P-GO-NEG-SUCCESS) role=(GO|client) freq=([0-9]*)'
  292. s = re.split(exp, go_neg_res)
  293. if len(s) < 4:
  294. raise Exception("Could not parse P2P-GO-NEG-SUCCESS")
  295. res['go_neg_role'] = s[2]
  296. res['go_neg_freq'] = s[3]
  297. return res
  298. def p2p_go_neg_auth(self, peer, pin, method, go_intent=None, persistent=False, freq=None):
  299. if not self.discover_peer(peer):
  300. raise Exception("Peer " + peer + " not found")
  301. self.dump_monitor()
  302. cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
  303. if go_intent:
  304. cmd = cmd + ' go_intent=' + str(go_intent)
  305. if freq:
  306. cmd = cmd + ' freq=' + str(freq)
  307. if persistent:
  308. cmd = cmd + " persistent"
  309. if "OK" in self.global_request(cmd):
  310. return None
  311. raise Exception("P2P_CONNECT (auth) failed")
  312. def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
  313. go_neg_res = None
  314. ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
  315. "P2P-GO-NEG-FAILURE"], timeout);
  316. if ev is None:
  317. if expect_failure:
  318. return None
  319. raise Exception("Group formation timed out")
  320. if "P2P-GO-NEG-SUCCESS" in ev:
  321. go_neg_res = ev
  322. ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout);
  323. if ev is None:
  324. if expect_failure:
  325. return None
  326. raise Exception("Group formation timed out")
  327. self.dump_monitor()
  328. return self.group_form_result(ev, expect_failure, go_neg_res)
  329. def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None, expect_failure=False, persistent=False, freq=None):
  330. if not self.discover_peer(peer):
  331. raise Exception("Peer " + peer + " not found")
  332. self.dump_monitor()
  333. if pin:
  334. cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
  335. else:
  336. cmd = "P2P_CONNECT " + peer + " " + method
  337. if go_intent:
  338. cmd = cmd + ' go_intent=' + str(go_intent)
  339. if freq:
  340. cmd = cmd + ' freq=' + str(freq)
  341. if persistent:
  342. cmd = cmd + " persistent"
  343. if "OK" in self.global_request(cmd):
  344. if timeout == 0:
  345. self.dump_monitor()
  346. return None
  347. go_neg_res = None
  348. ev = self.wait_global_event(["P2P-GO-NEG-SUCCESS",
  349. "P2P-GO-NEG-FAILURE"], timeout)
  350. if ev is None:
  351. if expect_failure:
  352. return None
  353. raise Exception("Group formation timed out")
  354. if "P2P-GO-NEG-SUCCESS" in ev:
  355. go_neg_res = ev
  356. ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
  357. if ev is None:
  358. if expect_failure:
  359. return None
  360. raise Exception("Group formation timed out")
  361. self.dump_monitor()
  362. return self.group_form_result(ev, expect_failure, go_neg_res)
  363. raise Exception("P2P_CONNECT failed")
  364. def wait_event(self, events, timeout):
  365. count = 0
  366. while count < timeout * 10:
  367. count = count + 1
  368. time.sleep(0.1)
  369. while self.mon.pending():
  370. ev = self.mon.recv()
  371. logger.debug(self.ifname + ": " + ev)
  372. for event in events:
  373. if event in ev:
  374. return ev
  375. return None
  376. def wait_global_event(self, events, timeout):
  377. if self.global_iface is None:
  378. self.wait_event(events, timeout)
  379. else:
  380. count = 0
  381. while count < timeout * 10:
  382. count = count + 1
  383. time.sleep(0.1)
  384. while self.global_mon.pending():
  385. ev = self.global_mon.recv()
  386. logger.debug(self.ifname + "(global): " + ev)
  387. for event in events:
  388. if event in ev:
  389. return ev
  390. return None
  391. def wait_go_ending_session(self):
  392. ev = self.wait_event(["P2P-GROUP-REMOVED"], timeout=3)
  393. if ev is None:
  394. raise Exception("Group removal event timed out")
  395. if "reason=GO_ENDING_SESSION" not in ev:
  396. raise Exception("Unexpected group removal reason")
  397. def dump_monitor(self):
  398. while self.mon.pending():
  399. ev = self.mon.recv()
  400. logger.debug(self.ifname + ": " + ev)
  401. while self.global_mon.pending():
  402. ev = self.global_mon.recv()
  403. logger.debug(self.ifname + "(global): " + ev)
  404. def remove_group(self, ifname=None):
  405. if ifname is None:
  406. ifname = self.group_ifname if self.group_ifname else self.ifname
  407. if "OK" not in self.global_request("P2P_GROUP_REMOVE " + ifname):
  408. raise Exception("Group could not be removed")
  409. self.group_ifname = None
  410. def p2p_start_go(self, persistent=None, freq=None):
  411. self.dump_monitor()
  412. cmd = "P2P_GROUP_ADD"
  413. if persistent is None:
  414. pass
  415. elif persistent is True:
  416. cmd = cmd + " persistent"
  417. else:
  418. cmd = cmd + " persistent=" + str(persistent)
  419. if freq:
  420. cmd = cmd + " freq=" + str(freq)
  421. if "OK" in self.global_request(cmd):
  422. ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout=5)
  423. if ev is None:
  424. raise Exception("GO start up timed out")
  425. self.dump_monitor()
  426. return self.group_form_result(ev)
  427. raise Exception("P2P_GROUP_ADD failed")
  428. def p2p_go_authorize_client(self, pin):
  429. cmd = "WPS_PIN any " + pin
  430. if "FAIL" in self.group_request(cmd):
  431. raise Exception("Failed to authorize client connection on GO")
  432. return None
  433. def p2p_go_authorize_client_pbc(self):
  434. cmd = "WPS_PBC"
  435. if "FAIL" in self.group_request(cmd):
  436. raise Exception("Failed to authorize client connection on GO")
  437. return None
  438. def p2p_connect_group(self, go_addr, pin, timeout=0, social=False):
  439. self.dump_monitor()
  440. if not self.discover_peer(go_addr, social=social):
  441. raise Exception("GO " + go_addr + " not found")
  442. self.dump_monitor()
  443. cmd = "P2P_CONNECT " + go_addr + " " + pin + " join"
  444. if "OK" in self.global_request(cmd):
  445. if timeout == 0:
  446. self.dump_monitor()
  447. return None
  448. ev = self.wait_global_event(["P2P-GROUP-STARTED"], timeout)
  449. if ev is None:
  450. raise Exception("Joining the group timed out")
  451. self.dump_monitor()
  452. return self.group_form_result(ev)
  453. raise Exception("P2P_CONNECT(join) failed")
  454. def tdls_setup(self, peer):
  455. cmd = "TDLS_SETUP " + peer
  456. if "FAIL" in self.group_request(cmd):
  457. raise Exception("Failed to request TDLS setup")
  458. return None
  459. def tdls_teardown(self, peer):
  460. cmd = "TDLS_TEARDOWN " + peer
  461. if "FAIL" in self.group_request(cmd):
  462. raise Exception("Failed to request TDLS teardown")
  463. return None
  464. def connect(self, ssid, psk=None, proto=None, key_mgmt=None, wep_key0=None,
  465. ieee80211w=None, pairwise=None, group=None, scan_freq=None,
  466. eap=None, identity=None, anonymous_identity=None,
  467. password=None, phase1=None, phase2=None, ca_cert=None,
  468. domain_suffix_match=None, password_hex=None,
  469. client_cert=None, private_key=None,
  470. wait_connect=True):
  471. logger.info("Connect STA " + self.ifname + " to AP")
  472. id = self.add_network()
  473. self.set_network_quoted(id, "ssid", ssid)
  474. if psk:
  475. self.set_network_quoted(id, "psk", psk)
  476. if proto:
  477. self.set_network(id, "proto", proto)
  478. if key_mgmt:
  479. self.set_network(id, "key_mgmt", key_mgmt)
  480. if ieee80211w:
  481. self.set_network(id, "ieee80211w", ieee80211w)
  482. if pairwise:
  483. self.set_network(id, "pairwise", pairwise)
  484. if group:
  485. self.set_network(id, "group", group)
  486. if wep_key0:
  487. self.set_network(id, "wep_key0", wep_key0)
  488. if scan_freq:
  489. self.set_network(id, "scan_freq", scan_freq)
  490. if eap:
  491. self.set_network(id, "eap", eap)
  492. if identity:
  493. self.set_network_quoted(id, "identity", identity)
  494. if anonymous_identity:
  495. self.set_network_quoted(id, "anonymous_identity",
  496. anonymous_identity)
  497. if password:
  498. self.set_network_quoted(id, "password", password)
  499. if password_hex:
  500. self.set_network(id, "password", password_hex)
  501. if ca_cert:
  502. self.set_network_quoted(id, "ca_cert", ca_cert)
  503. if client_cert:
  504. self.set_network_quoted(id, "client_cert", client_cert)
  505. if private_key:
  506. self.set_network_quoted(id, "private_key", private_key)
  507. if phase1:
  508. self.set_network_quoted(id, "phase1", phase1)
  509. if phase2:
  510. self.set_network_quoted(id, "phase2", phase2)
  511. if domain_suffix_match:
  512. self.set_network_quoted(id, "domain_suffix_match",
  513. domain_suffix_match)
  514. if wait_connect:
  515. self.connect_network(id)
  516. else:
  517. self.dump_monitor()
  518. self.select_network(id)
  519. return id
  520. def scan(self, type=None):
  521. if type:
  522. cmd = "SCAN TYPE=" + type
  523. else:
  524. cmd = "SCAN"
  525. self.dump_monitor()
  526. if not "OK" in self.request(cmd):
  527. raise Exception("Failed to trigger scan")
  528. ev = self.wait_event(["CTRL-EVENT-SCAN-RESULTS"], 15)
  529. if ev is None:
  530. raise Exception("Scan timed out")
  531. def roam(self, bssid):
  532. self.dump_monitor()
  533. self.request("ROAM " + bssid)
  534. ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=10)
  535. if ev is None:
  536. raise Exception("Roaming with the AP timed out")
  537. self.dump_monitor()
  538. def wps_reg(self, bssid, pin, new_ssid=None, key_mgmt=None, cipher=None,
  539. new_passphrase=None):
  540. self.dump_monitor()
  541. if new_ssid:
  542. self.request("WPS_REG " + bssid + " " + pin + " " +
  543. new_ssid.encode("hex") + " " + key_mgmt + " " +
  544. cipher + " " + new_passphrase.encode("hex"))
  545. ev = self.wait_event(["WPS-SUCCESS"], timeout=15)
  546. else:
  547. self.request("WPS_REG " + bssid + " " + pin)
  548. ev = self.wait_event(["WPS-CRED-RECEIVED"], timeout=15)
  549. if ev is None:
  550. raise Exception("WPS cred timed out")
  551. ev = self.wait_event(["WPS-FAIL"], timeout=15)
  552. if ev is None:
  553. raise Exception("WPS timed out")
  554. ev = self.wait_event(["CTRL-EVENT-CONNECTED"], timeout=15)
  555. if ev is None:
  556. raise Exception("Association with the AP timed out")
  557. def relog(self):
  558. self.request("RELOG")
  559. def wait_completed(self, timeout=10):
  560. for i in range(0, timeout * 2):
  561. if self.get_status_field("wpa_state") == "COMPLETED":
  562. return
  563. time.sleep(0.5)
  564. raise Exception("Timeout while waiting for COMPLETED state")