wpasupplicant.py 23 KB

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