|
| 1 | +# Basic Wi-Fi MAC layer test where one device creates an Access Point and the |
| 2 | +# other device connects to it as a Station. Also tests channel assignment (where |
| 3 | +# possible) and disconnection. |
| 4 | + |
| 5 | +try: |
| 6 | + import network |
| 7 | + |
| 8 | + network.WLAN |
| 9 | +except (ImportError, NameError): |
| 10 | + print("SKIP") |
| 11 | + raise SystemExit |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import time |
| 16 | + |
| 17 | +CHANNEL = 8 |
| 18 | + |
| 19 | +# Note that on slower Wi-Fi stacks this bumps up against the run-multitests.py |
| 20 | +# timeout which expects <10s between lines of output. We work around this by |
| 21 | +# logging something half way through the wait_for loop... |
| 22 | +CONNECT_TIMEOUT = 15000 |
| 23 | + |
| 24 | + |
| 25 | +def wait_for(test_func): |
| 26 | + has_printed = False |
| 27 | + start = time.ticks_ms() |
| 28 | + while not test_func(): |
| 29 | + time.sleep(0.1) |
| 30 | + delta = time.ticks_diff(time.ticks_ms(), start) |
| 31 | + if not has_printed and delta > CONNECT_TIMEOUT / 2: |
| 32 | + print("...") |
| 33 | + has_printed = True |
| 34 | + elif delta > CONNECT_TIMEOUT: |
| 35 | + break |
| 36 | + |
| 37 | + if not has_printed: |
| 38 | + print("...") # keep the output consistent |
| 39 | + |
| 40 | + return test_func() |
| 41 | + |
| 42 | + |
| 43 | +# AP |
| 44 | +def instance0(): |
| 45 | + ap = network.WLAN(network.WLAN.IF_AP) |
| 46 | + ssid = "MP-test-" + os.urandom(6).hex() |
| 47 | + psk = "Secret-" + os.urandom(6).hex() |
| 48 | + |
| 49 | + # stop any previous activity |
| 50 | + network.WLAN(network.WLAN.IF_STA).active(False) |
| 51 | + ap.active(False) |
| 52 | + |
| 53 | + ap.active(True) |
| 54 | + ap.config(ssid=ssid, key=psk, channel=CHANNEL, security=network.WLAN.SEC_WPA_WPA2) |
| 55 | + |
| 56 | + # print("AP setup", ssid, psk) |
| 57 | + print("AP started") |
| 58 | + |
| 59 | + multitest.globals(SSID=ssid, PSK=psk) |
| 60 | + multitest.next() |
| 61 | + |
| 62 | + # Wait for station |
| 63 | + if not wait_for(ap.isconnected): |
| 64 | + raise RuntimeError("Timed out waiting for station, status ", ap.status()) |
| 65 | + |
| 66 | + print("AP got station") |
| 67 | + time.sleep( |
| 68 | + 3 |
| 69 | + ) # depending on port, may still need to negotiate DHCP lease for STA to see connection |
| 70 | + |
| 71 | + print("AP disabling...") |
| 72 | + ap.active(False) |
| 73 | + |
| 74 | + |
| 75 | +# STA |
| 76 | +def instance1(): |
| 77 | + sta = network.WLAN(network.WLAN.IF_STA) |
| 78 | + |
| 79 | + # stop any previous activity |
| 80 | + network.WLAN(network.WLAN.IF_AP).active(False) |
| 81 | + sta.active(False) |
| 82 | + |
| 83 | + multitest.next() |
| 84 | + ssid = SSID |
| 85 | + psk = PSK |
| 86 | + |
| 87 | + # print("STA setup", ssid, psk) |
| 88 | + |
| 89 | + sta.active(True) |
| 90 | + sta.connect(ssid, psk) |
| 91 | + |
| 92 | + print("STA connecting...") |
| 93 | + |
| 94 | + if not wait_for(sta.isconnected): |
| 95 | + raise RuntimeError("Timed out waiting to connect, status ", sta.status()) |
| 96 | + |
| 97 | + print("STA connected") |
| 98 | + |
| 99 | + # Print the current channel, if the port support this |
| 100 | + try: |
| 101 | + print("channel", sta.config("channel")) |
| 102 | + except OSError as e: |
| 103 | + if "AP" in str(e): |
| 104 | + # ESP8266 only supports reading channel on the AP interface, so fake this result |
| 105 | + print("channel", CHANNEL) |
| 106 | + else: |
| 107 | + raise |
| 108 | + |
| 109 | + print("STA waiting for disconnect...") |
| 110 | + |
| 111 | + # Expect the AP to disconnect us immediately |
| 112 | + if not wait_for(lambda: not sta.isconnected()): |
| 113 | + raise RuntimeError("Timed out waiting for AP to disconnect us, status ", sta.status()) |
| 114 | + |
| 115 | + print("STA disconnected") |
| 116 | + |
| 117 | + sta.active(False) |
0 commit comments