tests: Add basic wlan test.

Includes adding some ESP8266 port output to the ignored output list for the
multitest runner.

This test passes on ESP8266 and various ESP32s (including talking to each
other). Without the fix in the parent commit, ESP32 AP will fail if the
station can report its channel (i.e. channel is wrong).

Testing with a CYW43 (RPI_PICO_W) currently fails but I have some fixes
to submit so it can pass as well.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
This commit is contained in:
Angus Gratton
2024-11-13 17:40:02 +11:00
committed by Angus Gratton
parent 951a10e707
commit 0e383a31b9
3 changed files with 135 additions and 6 deletions

View File

@@ -0,0 +1,117 @@
# Basic Wi-Fi MAC layer test where one device creates an Access Point and the
# other device connects to it as a Station. Also tests channel assignment (where
# possible) and disconnection.
try:
import network
network.WLAN
except (ImportError, NameError):
print("SKIP")
raise SystemExit
import os
import sys
import time
CHANNEL = 8
# Note that on slower Wi-Fi stacks this bumps up against the run-multitests.py
# timeout which expects <10s between lines of output. We work around this by
# logging something half way through the wait_for loop...
CONNECT_TIMEOUT = 15000
def wait_for(test_func):
has_printed = False
start = time.ticks_ms()
while not test_func():
time.sleep(0.1)
delta = time.ticks_diff(time.ticks_ms(), start)
if not has_printed and delta > CONNECT_TIMEOUT / 2:
print("...")
has_printed = True
elif delta > CONNECT_TIMEOUT:
break
if not has_printed:
print("...") # keep the output consistent
return test_func()
# AP
def instance0():
ap = network.WLAN(network.WLAN.IF_AP)
ssid = "MP-test-" + os.urandom(6).hex()
psk = "Secret-" + os.urandom(6).hex()
# stop any previous activity
network.WLAN(network.WLAN.IF_STA).active(False)
ap.active(False)
ap.active(True)
ap.config(ssid=ssid, key=psk, channel=CHANNEL, security=network.WLAN.SEC_WPA_WPA2)
# print("AP setup", ssid, psk)
print("AP started")
multitest.globals(SSID=ssid, PSK=psk)
multitest.next()
# Wait for station
if not wait_for(ap.isconnected):
raise RuntimeError("Timed out waiting for station, status ", ap.status())
print("AP got station")
time.sleep(
3
) # depending on port, may still need to negotiate DHCP lease for STA to see connection
print("AP disabling...")
ap.active(False)
# STA
def instance1():
sta = network.WLAN(network.WLAN.IF_STA)
# stop any previous activity
network.WLAN(network.WLAN.IF_AP).active(False)
sta.active(False)
multitest.next()
ssid = SSID
psk = PSK
# print("STA setup", ssid, psk)
sta.active(True)
sta.connect(ssid, psk)
print("STA connecting...")
if not wait_for(sta.isconnected):
raise RuntimeError("Timed out waiting to connect, status ", sta.status())
print("STA connected")
# Print the current channel, if the port support this
try:
print("channel", sta.config("channel"))
except OSError as e:
if "AP" in str(e):
# ESP8266 only supports reading channel on the AP interface, so fake this result
print("channel", CHANNEL)
else:
raise
print("STA waiting for disconnect...")
# Expect the AP to disconnect us immediately
if not wait_for(lambda: not sta.isconnected()):
raise RuntimeError("Timed out waiting for AP to disconnect us, status ", sta.status())
print("STA disconnected")
sta.active(False)

View File

@@ -0,0 +1,13 @@
--- instance0 ---
AP started
...
AP got station
AP disabling...
--- instance1 ---
STA connecting...
...
STA connected
channel 8
STA waiting for disconnect...
...
STA disconnected

View File

@@ -105,15 +105,14 @@ instance{}()
multitest.flush()
"""
# The btstack implementation on Unix generates some spurious output that we
# can't control. Also other platforms may output certain warnings/errors that
# can be safely ignored.
# Some ports generate output we can't control, and that can be safely ignored.
IGNORE_OUTPUT_MATCHES = (
"libusb: error ", # It tries to open devices that it doesn't have access to (libusb prints unconditionally).
"libusb: error ", # unix btstack tries to open devices that it doesn't have access to (libusb prints unconditionally).
"hci_transport_h2_libusb.c", # Same issue. We enable LOG_ERROR in btstack.
"USB Path: ", # Hardcoded in btstack's libusb transport.
"hci_number_completed_packet", # Warning from btstack.
"USB Path: ", # Hardcoded in unix btstack's libusb transport.
"hci_number_completed_packet", # Warning from unix btstack.
"lld_pdu_get_tx_flush_nb HCI packet count mismatch (", # From ESP-IDF, see https://github.com/espressif/esp-idf/issues/5105
" ets_task(", # ESP8266 port debug output
)