wpa_supplicant: Fix AP mode frequency initialization
[mech_eap.git] / wpaspy / wpaspy.py
1 #!/usr/bin/python
2 #
3 # wpa_supplicant/hostapd control interface using Python
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
9 import os
10 import socket
11 import select
12
13 counter = 0
14
15 class Ctrl:
16     def __init__(self, path):
17         global counter
18         self.started = False
19         self.attached = False
20         self.s = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
21         self.dest = path
22         self.local = "/tmp/wpa_ctrl_" + str(os.getpid()) + '-' + str(counter)
23         counter += 1
24         self.s.bind(self.local)
25         self.s.connect(self.dest)
26         self.started = True
27
28     def __del__(self):
29         self.close()
30
31     def close(self):
32         if self.attached:
33             self.detach()
34         if self.started:
35             self.s.close()
36             os.unlink(self.local)
37             self.started = False
38
39     def request(self, cmd):
40         self.s.send(cmd)
41         [r, w, e] = select.select([self.s], [], [], 10)
42         if r:
43             return self.s.recv(4096)
44         raise Exception("Timeout on waiting response")
45
46     def attach(self):
47         if self.attached:
48             return None
49         res = self.request("ATTACH")
50         if "OK" in res:
51             return None
52         raise Exception("ATTACH failed")
53
54     def detach(self):
55         if not self.attached:
56             return None
57         res = self.request("DETACH")
58         if "OK" in res:
59             return None
60         raise Exception("DETACH failed")
61
62     def pending(self):
63         [r, w, e] = select.select([self.s], [], [], 0)
64         if r:
65             return True
66         return False
67
68     def recv(self):
69         res = self.s.recv(4096)
70         return res