]> git.ipfire.org Git - thirdparty/hostap.git/blob - tests/hwsim/test_dfs.py
tests: Pass wpas/hapd instance to test_connectivity()
[thirdparty/hostap.git] / tests / hwsim / test_dfs.py
1 # Test cases for DFS
2 # Copyright (c) 2013, Jouni Malinen <j@w1.fi>
3 #
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
6
7 import os
8 import subprocess
9 import time
10 import logging
11 logger = logging.getLogger()
12
13 import hwsim_utils
14 import hostapd
15
16 def wait_dfs_event(hapd, event, timeout):
17 dfs_events = [ "DFS-RADAR-DETECTED", "DFS-NEW-CHANNEL",
18 "DFS-CAC-START", "DFS-CAC-COMPLETED",
19 "DFS-NOP-FINISHED", "AP-ENABLED" ]
20 ev = hapd.wait_event(dfs_events, timeout=timeout)
21 if not ev:
22 raise Exception("DFS event timed out")
23 if event and event not in ev:
24 raise Exception("Unexpected DFS event")
25 return ev
26
27 def start_dfs_ap(ap, allow_failure=False):
28 ifname = ap['ifname']
29 logger.info("Starting AP " + ifname + " on DFS channel")
30 hapd_global = hostapd.HostapdGlobal()
31 hapd_global.remove(ifname)
32 hapd_global.add(ifname)
33 hapd = hostapd.Hostapd(ifname)
34 if not hapd.ping():
35 raise Exception("Could not ping hostapd")
36 hapd.set_defaults()
37 hapd.set("ssid", "dfs")
38 hapd.set("country_code", "FI")
39 hapd.set("ieee80211d", "1")
40 hapd.set("ieee80211h", "1")
41 hapd.set("hw_mode", "a")
42 hapd.set("channel", "52")
43 hapd.enable()
44
45 ev = wait_dfs_event(hapd, "DFS-CAC-START", 5)
46 if "DFS-CAC-START" not in ev:
47 raise Exception("Unexpected DFS event")
48
49 state = hapd.get_status_field("state")
50 if state != "DFS":
51 if allow_failure:
52 logger.info("Interface state not DFS: " + state)
53 return None
54 raise Exception("Unexpected interface state: " + state)
55
56 return hapd
57
58 def test_dfs(dev, apdev):
59 """DFS CAC functionality on clear channel"""
60 try:
61 hapd = start_dfs_ap(apdev[0], allow_failure=True)
62 if hapd is None:
63 if not os.path.exists("dfs"):
64 return "skip"
65 raise Exception("Failed to start DFS AP")
66
67 ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
68 if "success=1" not in ev:
69 raise Exception("CAC failed")
70 if "freq=5260" not in ev:
71 raise Exception("Unexpected DFS freq result")
72
73 ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
74 if not ev:
75 raise Exception("AP setup timed out")
76
77 state = hapd.get_status_field("state")
78 if state != "ENABLED":
79 raise Exception("Unexpected interface state")
80
81 freq = hapd.get_status_field("freq")
82 if freq != "5260":
83 raise Exception("Unexpected frequency")
84
85 dev[0].connect("dfs", key_mgmt="NONE")
86 hwsim_utils.test_connectivity(dev[0], hapd)
87
88 hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
89 ev = hapd.wait_event(["DFS-RADAR-DETECTED"], timeout=10)
90 if ev is None:
91 raise Exception("DFS-RADAR-DETECTED event not reported")
92 if "freq=5260" not in ev:
93 raise Exception("Incorrect frequency in radar detected event: " + ev);
94 ev = hapd.wait_event(["DFS-NEW-CHANNEL"], timeout=70)
95 if ev is None:
96 raise Exception("DFS-NEW-CHANNEL event not reported")
97 if "freq=5260" in ev:
98 raise Exception("Channel did not change after radar was detected");
99
100 ev = hapd.wait_event(["AP-CSA-FINISHED"], timeout=70)
101 if ev is None:
102 raise Exception("AP-CSA-FINISHED event not reported")
103 if "freq=5260" in ev:
104 raise Exception("Channel did not change after radar was detected(2)");
105 time.sleep(1)
106 hwsim_utils.test_connectivity(dev[0], hapd)
107 finally:
108 subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
109
110 def test_dfs_radar(dev, apdev):
111 """DFS CAC functionality with radar detected"""
112 try:
113 hapd = start_dfs_ap(apdev[0])
114 if hapd is None:
115 if not os.path.exists("dfs"):
116 return "skip"
117 raise Exception("Failed to start DFS AP")
118 time.sleep(1)
119
120 phyname = hapd.get_driver_status_field("phyname")
121 radar_file = '/sys/kernel/debug/ieee80211/' + phyname + '/hwsim/dfs_simulate_radar'
122 cmd = subprocess.Popen(["sudo", "tee", radar_file],
123 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
124 cmd.stdin.write("1")
125 cmd.stdin.close()
126 cmd.stdout.read()
127 cmd.stdout.close()
128
129 ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 5)
130 if ev is None:
131 raise Exception("Timeout on DFS aborted event")
132 if "success=0 freq=5260" not in ev:
133 raise Exception("Unexpected DFS aborted event contents: " + ev)
134
135 ev = wait_dfs_event(hapd, "DFS-RADAR-DETECTED", 5)
136 if "freq=5260" not in ev:
137 raise Exception("Unexpected DFS radar detection freq")
138
139 ev = wait_dfs_event(hapd, "DFS-NEW-CHANNEL", 5)
140 if "freq=5260" in ev:
141 raise Exception("Unexpected DFS new freq")
142
143 ev = wait_dfs_event(hapd, None, 5)
144 if "AP-ENABLED" in ev:
145 logger.info("Started AP on non-DFS channel")
146 else:
147 logger.info("Trying to start AP on another DFS channel")
148 if "DFS-CAC-START" not in ev:
149 raise Exception("Unexpected DFS event")
150 if "freq=5260" in ev:
151 raise Exception("Unexpected DFS CAC freq")
152
153 ev = wait_dfs_event(hapd, "DFS-CAC-COMPLETED", 70)
154 if "success=1" not in ev:
155 raise Exception("CAC failed")
156 if "freq=5260" in ev:
157 raise Exception("Unexpected DFS freq result - radar channel")
158
159 ev = hapd.wait_event(["AP-ENABLED"], timeout=5)
160 if not ev:
161 raise Exception("AP setup timed out")
162
163 state = hapd.get_status_field("state")
164 if state != "ENABLED":
165 raise Exception("Unexpected interface state")
166
167 freq = hapd.get_status_field("freq")
168 if freq == "5260":
169 raise Exception("Unexpected frequency: " + freq)
170
171 dev[0].connect("dfs", key_mgmt="NONE")
172 finally:
173 subprocess.call(['sudo', 'iw', 'reg', 'set', '00'])
174
175 def test_dfs_radar_on_non_dfs_channel(dev, apdev):
176 """DFS radar detection test code on non-DFS channel"""
177 params = { "ssid": "radar" }
178 hapd = hostapd.add_ap(apdev[0]['ifname'], params)
179
180 hapd.request("RADAR DETECTED freq=5260 ht_enabled=1 chan_width=1")
181 hapd.request("RADAR DETECTED freq=2412 ht_enabled=1 chan_width=1")