stm32: network WLAN(AP_IF).isconnected() always returns True
The documentation for network.WLAN.isconnected() states:
In AP mode returns
Truewhen a station is connected. ReturnsFalseotherwise.
ref: http://docs.micropython.org/en/v1.13/library/network.WLAN.html#network.WLAN.isconnected
However, when I run the following code on a PYBD_SF6W:
import utime as time
import network
from urandom import randint
import machine
# Access Point
def make_ap():
print("Access Point...")
ap = network.WLAN(network.AP_IF)
ap.config(essid='MyPyboard:{:04d}'.format(randint(1, 10000)))
ap.active(True)
while not ap.active():
time.sleep(0.1)
print(" AP_IF: {!r}".format(ap.ifconfig()))
return ap
# ----- Mainline
print('machine.reset_cause()', machine.reset_cause())
ap = make_ap() # network.WLAN in AP_IF mode
while True:
time.sleep(1)
print('isconnected()', ap.isconnected())
I get the output:
MicroPython v1.13-274-g49dd9ba1a on 2021-01-19; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== # pasted above code
machine.reset_cause() 1
Access Point...
AP_IF: ('192.168.4.1', '255.255.255.0', '192.168.4.1', '0.0.0.0')
isconnected() True
isconnected() True
isconnected() True
DHCPS: client connected: MAC=66:bc:1c:1a:af:f1 IP=192.168.4.16
isconnected() True
isconnected() True
isconnected() True
What I would expect is that before the DHCP connection is made, before any stations are connected to the AP, isconnected() should return False.
esp32/modnetwork.c: Fix for isconnected() for static IP config.
Currently <WLAN>.isconnected() always returns True if a static
IP is set, regardless of the state of the connection. This breaks
the commonly documented wifi connection workflow:
import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.ifconfig(
('192.168.1.101', '255.255.255.0', '192.168.1.1', '8.8.8.8'))
sta_if.connect('ssid', 'password')
while not sta_if.isconnected():
pass
This patch introduces a new flag wifi_sta_connected which is set in
event_handler() when GOT_IP event is received and reset when
DISCONNECTED event is received (unless re-connect is successful).
<WLAN>.isconnected() now simply returns the status of this flag (for STA_IF).
The pre-existing flag misleadingly named wifi_sta_connected is also
renamed to wifi_sta_connect_requested.
Fixes issue #3837