Skip to content

Commit 233cf7f

Browse files
authored
Merge pull request #8326 from bill88t/picow-stop-ap
Pico W wifi code improvements.
2 parents 14b26b5 + e3314ef commit 233cf7f

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ msgstr ""
447447
msgid "ADC2 is being used by WiFi"
448448
msgstr ""
449449

450+
#: ports/raspberrypi/common-hal/wifi/Radio.c
451+
msgid "AP could not be started"
452+
msgstr ""
453+
450454
#: shared-bindings/_bleio/Address.c shared-bindings/ipaddress/IPv4Address.c
451455
#, c-format
452456
msgid "Address must be %d bytes long"
@@ -1959,10 +1963,6 @@ msgstr ""
19591963
msgid "Stereo right must be on PWM channel B"
19601964
msgstr ""
19611965

1962-
#: ports/raspberrypi/common-hal/wifi/Radio.c
1963-
msgid "Stopping AP is not supported."
1964-
msgstr ""
1965-
19661966
#: shared-bindings/alarm/time/TimeAlarm.c
19671967
msgid "Supply one of monotonic_time or epoch_time"
19681968
msgstr ""
@@ -2337,14 +2337,6 @@ msgstr ""
23372337
msgid "Wi-Fi: "
23382338
msgstr ""
23392339

2340-
#: ports/raspberrypi/common-hal/wifi/Radio.c
2341-
msgid "Wifi is in access point mode."
2342-
msgstr ""
2343-
2344-
#: ports/raspberrypi/common-hal/wifi/Radio.c
2345-
msgid "Wifi is in station mode."
2346-
msgstr ""
2347-
23482340
#: ports/raspberrypi/common-hal/wifi/Radio.c
23492341
msgid "Wifi is not enabled"
23502342
msgstr ""

ports/raspberrypi/common-hal/wifi/Radio.c

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) {
157157
void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) {
158158

159159
cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
160-
// This is wrong, but without this call the state of ITF_STA is still
161-
// reported as CYW43_LINK_JOIN (by wifi_link_status) and CYW43_LINK_UP
162-
// (by tcpip_link_status). However since ap disconnection isn't working
163-
// either, this is not an issue.
164-
cyw43_wifi_leave(&cyw43_state, CYW43_ITF_AP);
165160
const size_t timeout_ms = 500;
166161
uint64_t start = port_get_raw_ticks(NULL);
167162
uint64_t deadline = start + timeout_ms;
@@ -179,20 +174,45 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_
179174
mp_raise_RuntimeError(translate("Wifi is not enabled"));
180175
}
181176

182-
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_DOWN) {
183-
mp_raise_RuntimeError(translate("Wifi is in station mode."));
184-
}
177+
/* TODO: If the AP is stopped once it cannot be restarted.
178+
* This means that if if the user does:
179+
*
180+
* wifi.radio.start_ap(...)
181+
* wifi.radio.stop_ap()
182+
* wifi.radio.start_ap(...)
183+
*
184+
* The second start_ap will fail.
185+
*/
185186

186187
common_hal_wifi_radio_stop_ap(self);
187188

188189
// Channel can only be changed after initial powerup and config of ap.
189190
// Defaults to 1 if not set or invalid (i.e. 13)
190191
cyw43_wifi_ap_set_channel(&cyw43_state, (const uint32_t)channel);
191192

192-
cyw43_arch_enable_ap_mode((const char *)ssid, (const char *)password, CYW43_AUTH_WPA2_AES_PSK);
193+
if (password_len) {
194+
cyw43_arch_enable_ap_mode((const char *)ssid, (const char *)password, CYW43_AUTH_WPA2_AES_PSK);
195+
} else {
196+
cyw43_arch_enable_ap_mode((const char *)ssid, NULL, CYW43_AUTH_OPEN);
197+
}
193198

194199
// TODO: Implement authmode check like in espressif
195200
bindings_cyw43_wifi_enforce_pm();
201+
202+
const size_t timeout_ms = 500;
203+
uint64_t start = port_get_raw_ticks(NULL);
204+
uint64_t deadline = start + timeout_ms;
205+
while (port_get_raw_ticks(NULL) < deadline && (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_UP)) {
206+
RUN_BACKGROUND_TASKS;
207+
if (mp_hal_is_interrupted()) {
208+
break;
209+
}
210+
}
211+
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_UP) {
212+
common_hal_wifi_radio_stop_ap(self);
213+
// This is needed since it leaves a broken AP up.
214+
mp_raise_RuntimeError(translate("AP could not be started"));
215+
}
196216
}
197217

198218
bool common_hal_wifi_radio_get_ap_active(wifi_radio_obj_t *self) {
@@ -204,19 +224,19 @@ void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) {
204224
mp_raise_RuntimeError(translate("wifi is not enabled"));
205225
}
206226

207-
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_DOWN) {
208-
mp_raise_NotImplementedError(translate("Stopping AP is not supported."));
227+
cyw43_arch_disable_ap_mode();
228+
229+
const size_t timeout_ms = 500;
230+
uint64_t start = port_get_raw_ticks(NULL);
231+
uint64_t deadline = start + timeout_ms;
232+
while (port_get_raw_ticks(NULL) < deadline && (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_DOWN)) {
233+
RUN_BACKGROUND_TASKS;
234+
if (mp_hal_is_interrupted()) {
235+
break;
236+
}
209237
}
210238

211-
/*
212-
* AP cannot be disconnected. cyw43_wifi_leave is broken.
213-
* This code snippet should work, but doesn't.
214-
*
215-
* cyw43_wifi_leave(&cyw43_state, CYW43_ITF_AP);
216-
* cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA);
217-
*
218-
* bindings_cyw43_wifi_enforce_pm();
219-
*/
239+
bindings_cyw43_wifi_enforce_pm();
220240
}
221241

222242
static bool connection_unchanged(wifi_radio_obj_t *self, const uint8_t *ssid, size_t ssid_len) {
@@ -237,10 +257,6 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t
237257
mp_raise_RuntimeError(translate("Wifi is not enabled"));
238258
}
239259

240-
if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_DOWN) {
241-
mp_raise_RuntimeError(translate("Wifi is in access point mode."));
242-
}
243-
244260
if (ssid_len > 32) {
245261
return WIFI_RADIO_ERROR_CONNECTION_FAIL;
246262
}

shared-bindings/wifi/Radio.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,9 @@ MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_station_obj, wifi_radio_stop_station);
332332
//|
333333
//| **Limitations:** On Espressif, ``authmode`` with a non-empty password must include
334334
//| `wifi.AuthMode.PSK`, and one or both of `wifi.AuthMode.WPA` and `wifi.AuthMode.WPA2`.
335-
//| On Pi Pico W, ``authmode`` is ignored; it is always ``(wifi.AuthMode.WPA2, wifi.AuthMode.PSK)`
336-
//| with a non-empty password, or ``(wifi.AuthMode.OPEN,)`` when no password is given.
335+
//| On Pi Pico W, ``authmode`` is ignored; it is always ``(wifi.AuthMode.WPA2, wifi.AuthMode.PSK)``
336+
//| with a non-empty password, or ``(wifi.AuthMode.OPEN)``, when no password is given.
337+
//| On Pi Pico W, the AP can be started and stopped only once per reboot.
337338
//|
338339
//| The length of ``password`` must be 8-63 characters if it is ASCII,
339340
//| or exactly 64 hexadecimal characters if it is the hex form of the 256-bit key.

0 commit comments

Comments
 (0)