wpasupplicant.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 wpaspy
  13. logger = logging.getLogger(__name__)
  14. wpas_ctrl = '/var/run/wpa_supplicant'
  15. class WpaSupplicant:
  16. def __init__(self, ifname):
  17. self.ifname = ifname
  18. self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
  19. self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
  20. self.mon.attach()
  21. def request(self, cmd):
  22. logger.debug(self.ifname + ": CTRL: " + cmd)
  23. return self.ctrl.request(cmd)
  24. def ping(self):
  25. return "PONG" in self.request("PING")
  26. def reset(self):
  27. self.request("P2P_STOP_FIND")
  28. self.request("P2P_FLUSH")
  29. self.request("P2P_GROUP_REMOVE *")
  30. self.request("REMOVE_NETWORK *")
  31. self.request("REMOVE_CRED *")
  32. def get_status(self, field):
  33. res = self.request("STATUS")
  34. lines = res.splitlines()
  35. for l in lines:
  36. [name,value] = l.split('=', 1)
  37. if name == field:
  38. return value
  39. return None
  40. def p2p_dev_addr(self):
  41. return self.get_status("p2p_device_address")
  42. def p2p_listen(self):
  43. return self.request("P2P_LISTEN")
  44. def p2p_find(self, social=False):
  45. if social:
  46. return self.request("P2P_FIND type=social")
  47. return self.request("P2P_FIND")
  48. def wps_read_pin(self):
  49. #TODO: make this random
  50. self.pin = "12345670"
  51. return self.pin
  52. def peer_known(self, peer, full=True):
  53. res = self.request("P2P_PEER " + peer)
  54. if peer.lower() not in res.lower():
  55. return False
  56. if not full:
  57. return True
  58. return "[PROBE_REQ_ONLY]" not in res
  59. def discover_peer(self, peer, full=True, timeout=15):
  60. logger.info(self.ifname + ": Trying to discover peer " + peer)
  61. if self.peer_known(peer, full):
  62. return True
  63. self.p2p_find()
  64. count = 0
  65. while count < timeout:
  66. time.sleep(1)
  67. count = count + 1
  68. if self.peer_known(peer, full):
  69. return True
  70. return False
  71. def group_form_result(self, ev):
  72. exp = r'<.>(P2P-GROUP-STARTED) ([^ ]*) ([^ ]*) ssid="(.*)" freq=([0-9]*) ((?:psk=.*)|(?:passphrase=".*")) go_dev_addr=([0-9a-f:]*)'
  73. s = re.split(exp, ev)
  74. if len(s) < 8:
  75. raise Exception("Could not parse P2P-GROUP-STARTED")
  76. res = {}
  77. res['result'] = 'success'
  78. res['ifname'] = s[2]
  79. res['role'] = s[3]
  80. res['ssid'] = s[4]
  81. res['freq'] = s[5]
  82. p = re.match(r'psk=([0-9a-f]*)', s[6])
  83. if p:
  84. res['psk'] = p.group(1)
  85. p = re.match(r'passphrase="(.*)"', s[6])
  86. if p:
  87. res['passphrase'] = p.group(1)
  88. res['go_dev_addr'] = s[7]
  89. return res
  90. def p2p_go_neg_auth(self, peer, pin, method, go_intent=None):
  91. if not self.discover_peer(peer):
  92. raise Exception("Peer " + peer + " not found")
  93. self.dump_monitor()
  94. cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
  95. if go_intent:
  96. cmd = cmd + ' go_intent=' + str(go_intent)
  97. if "OK" in self.request(cmd):
  98. return None
  99. raise Exception("P2P_CONNECT (auth) failed")
  100. def p2p_go_neg_auth_result(self, timeout=1, expect_failure=False):
  101. ev = self.wait_event("P2P-GROUP-STARTED", timeout);
  102. if ev is None:
  103. if expect_failure:
  104. return None
  105. raise Exception("Group formation timed out")
  106. self.dump_monitor()
  107. if expect_failure:
  108. raise Exception("Group formation succeeded when expecting failure")
  109. return self.group_form_result(ev)
  110. def p2p_go_neg_init(self, peer, pin, method, timeout=0, go_intent=None, expect_failure=False):
  111. if not self.discover_peer(peer):
  112. raise Exception("Peer " + peer + " not found")
  113. self.dump_monitor()
  114. cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
  115. if go_intent:
  116. cmd = cmd + ' go_intent=' + str(go_intent)
  117. if "OK" in self.request(cmd):
  118. if timeout == 0:
  119. self.dump_monitor()
  120. return None
  121. ev = self.wait_event("P2P-GROUP-STARTED", timeout)
  122. if ev is None:
  123. if expect_failure:
  124. return None
  125. raise Exception("Group formation timed out")
  126. self.dump_monitor()
  127. if expect_failure:
  128. raise Exception("Group formation succeeded when expecting failure")
  129. return self.group_form_result(ev)
  130. raise Exception("P2P_CONNECT failed")
  131. def wait_event(self, event, timeout):
  132. count = 0
  133. while count < timeout * 2:
  134. count = count + 1
  135. time.sleep(0.1)
  136. while self.mon.pending():
  137. ev = self.mon.recv()
  138. logger.debug(self.ifname + ": " + ev)
  139. if event in ev:
  140. return ev
  141. return None
  142. def dump_monitor(self):
  143. while self.mon.pending():
  144. ev = self.mon.recv()
  145. logger.debug(self.ifname + ": " + ev)
  146. def remove_group(self, ifname=None):
  147. if ifname is None:
  148. ifname = self.ifname
  149. if "OK" not in self.request("P2P_GROUP_REMOVE " + ifname):
  150. raise Exception("Group could not be removed")