wpasupplicant.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 wpaspy
  12. logger = logging.getLogger(__name__)
  13. wpas_ctrl = '/var/run/wpa_supplicant'
  14. class WpaSupplicant:
  15. def __init__(self, ifname):
  16. self.ifname = ifname
  17. self.ctrl = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
  18. self.mon = wpaspy.Ctrl(os.path.join(wpas_ctrl, ifname))
  19. self.mon.attach()
  20. def request(self, cmd):
  21. logger.debug(self.ifname + ": CTRL: " + cmd)
  22. return self.ctrl.request(cmd)
  23. def ping(self):
  24. return "PONG" in self.request("PING")
  25. def reset(self):
  26. self.request("P2P_STOP_FIND")
  27. self.request("P2P_FLUSH")
  28. self.request("P2P_GROUP_REMOVE *")
  29. self.request("REMOVE_NETWORK *")
  30. self.request("REMOVE_CRED *")
  31. def get_status(self, field):
  32. res = self.request("STATUS")
  33. lines = res.splitlines()
  34. for l in lines:
  35. [name,value] = l.split('=', 1)
  36. if name == field:
  37. return value
  38. return None
  39. def p2p_dev_addr(self):
  40. return self.get_status("p2p_device_address")
  41. def p2p_listen(self):
  42. return self.request("P2P_LISTEN")
  43. def p2p_find(self, social=False):
  44. if social:
  45. return self.request("P2P_FIND type=social")
  46. return self.request("P2P_FIND")
  47. def wps_read_pin(self):
  48. #TODO: make this random
  49. self.pin = "12345670"
  50. return self.pin
  51. def peer_known(self, peer, full=True):
  52. res = self.request("P2P_PEER " + peer)
  53. if peer.lower() not in res.lower():
  54. return False
  55. if not full:
  56. return True
  57. return "[PROBE_REQ_ONLY]" not in res
  58. def discover_peer(self, peer, full=True, timeout=15):
  59. logger.info(self.ifname + ": Trying to discover peer " + peer)
  60. if self.peer_known(peer, full):
  61. return True
  62. self.p2p_find()
  63. count = 0
  64. while count < timeout:
  65. time.sleep(1)
  66. count = count + 1
  67. if self.peer_known(peer, full):
  68. return True
  69. return False
  70. def p2p_go_neg_auth(self, peer, pin, method):
  71. if not self.discover_peer(peer):
  72. raise Exception("Peer " + peer + " not found")
  73. self.dump_monitor()
  74. cmd = "P2P_CONNECT " + peer + " " + pin + " " + method + " auth"
  75. if "OK" in self.request(cmd):
  76. return None
  77. raise Exception("P2P_CONNECT (auth) failed")
  78. def p2p_go_neg_init(self, peer, pin, method, timeout=0):
  79. if not self.discover_peer(peer):
  80. raise Exception("Peer " + peer + " not found")
  81. self.dump_monitor()
  82. cmd = "P2P_CONNECT " + peer + " " + pin + " " + method
  83. if "OK" in self.request(cmd):
  84. if timeout == 0:
  85. self.dump_monitor()
  86. return None
  87. if self.wait_event("P2P-GROUP-STARTED", timeout):
  88. self.dump_monitor()
  89. return None
  90. raise Exception("Group formation timed out")
  91. raise Exception("P2P_CONNECT failed")
  92. def wait_event(self, event, timeout):
  93. count = 0
  94. while count < timeout * 2:
  95. count = count + 1
  96. time.sleep(0.5)
  97. while self.mon.pending():
  98. ev = self.mon.recv()
  99. if event in ev:
  100. return True
  101. return False
  102. def dump_monitor(self):
  103. while self.mon.pending():
  104. ev = self.mon.recv()
  105. logger.debug(self.ifname + ": " + ev)
  106. def remove_group(self, ifname):
  107. if "OK" not in self.request("P2P_GROUP_REMOVE " + ifname):
  108. raise Exception("Group could not be removed")