1# autoscan tests
2# Copyright (c) 2014, 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
7from remotehost import remote_compatible
8import time
9import logging
10logger = logging.getLogger()
11import os
12
13import hostapd
14
15def test_autoscan_periodic(dev, apdev):
16    """autoscan_periodic"""
17    hostapd.add_ap(apdev[0], {"ssid": "autoscan"})
18
19    try:
20        if "OK" not in dev[0].request("AUTOSCAN periodic:1"):
21            raise Exception("Failed to set autoscan")
22        id = dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
23                            wait_connect=False)
24        times = {}
25        for i in range(0, 3):
26            logger.info("Waiting for scan to start")
27            start = os.times()[4]
28            ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
29            if ev is None:
30                raise Exception("did not start a scan")
31            stop = os.times()[4]
32            times[i] = stop - start
33            logger.info("Waiting for scan to complete")
34            ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
35            if ev is None:
36                raise Exception("did not complete a scan")
37        if times[0] > 1 or times[1] < 0.5 or times[1] > 1.5 or times[2] < 0.5 or times[2] > 1.5:
38            raise Exception("Unexpected scan timing: " + str(times))
39
40        # scan some more channels to allow some more time for reseting AUTOSCAN
41        # while a scan is in progress
42        dev[0].set_network(id, "scan_freq", "2412 2437 2462 5180 5200 5220 5240")
43        dev[0].dump_monitor()
44        ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
45        if ev is None:
46            raise Exception("did not start a scan")
47        if "OK" not in dev[0].request("AUTOSCAN periodic:2"):
48            raise Exception("Failed to (re)set autoscan")
49        ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
50        if ev is None:
51            raise Exception("did not complete a scan")
52    finally:
53        dev[0].request("AUTOSCAN ")
54
55@remote_compatible
56def test_autoscan_exponential(dev, apdev):
57    """autoscan_exponential"""
58    hostapd.add_ap(apdev[0], {"ssid": "autoscan"})
59
60    try:
61        if "OK" not in dev[0].request("AUTOSCAN exponential:2:10"):
62            raise Exception("Failed to set autoscan")
63        dev[0].connect("not-used", key_mgmt="NONE", scan_freq="2412",
64                       wait_connect=False)
65        times = {}
66        for i in range(0, 3):
67            logger.info("Waiting for scan to start")
68            start = os.times()[4]
69            ev = dev[0].wait_event(["CTRL-EVENT-SCAN-STARTED"], timeout=5)
70            if ev is None:
71                raise Exception("did not start a scan")
72            stop = os.times()[4]
73            times[i] = stop - start
74            logger.info("Waiting for scan to complete")
75            ev = dev[0].wait_event(["CTRL-EVENT-SCAN-RESULTS"], 10)
76            if ev is None:
77                raise Exception("did not complete a scan")
78        if times[0] > 1 or times[1] < 1 or times[1] > 3 or times[2] < 3 or times[2] > 5:
79            raise Exception("Unexpected scan timing: " + str(times))
80    finally:
81        dev[0].request("AUTOSCAN ")
82