From b8cd6c093fa25ba650a79a64ce83491607f80691 Mon Sep 17 00:00:00 2001 From: Bill Sideris Date: Thu, 17 Nov 2022 21:47:39 +0200 Subject: [PATCH] picow-ap progress --- locale/circuitpython.pot | 18 ++++++++-- ports/raspberrypi/common-hal/wifi/Radio.c | 40 ++++++++++++++++++----- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 044900bd30..2d759d8cbe 100755 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -106,7 +106,7 @@ msgstr "" msgid "%q in use" msgstr "" -#: py/obj.c py/objstr.c py/objstrunicode.c +#: py/objstr.c py/objstrunicode.c msgid "%q index out of range" msgstr "" @@ -171,7 +171,7 @@ msgstr "" msgid "%q must be an int" msgstr "" -#: py/argcheck.c +#: py/argcheck.c py/obj.c msgid "%q must be of type %q" msgstr "" @@ -397,6 +397,10 @@ msgstr "" msgid "ADC2 is being used by WiFi" msgstr "" +#: ports/raspberrypi/common-hal/wifi/Radio.c +msgid "AP cannot be stopped." +msgstr "" + #: shared-bindings/_bleio/Address.c shared-bindings/ipaddress/IPv4Address.c #, c-format msgid "Address must be %d bytes long" @@ -478,10 +482,18 @@ msgstr "" msgid "Already advertising." msgstr "" +#: ports/raspberrypi/common-hal/wifi/Radio.c +msgid "Already connected to station." +msgstr "" + #: ports/atmel-samd/common-hal/canio/Listener.c msgid "Already have all-matches listener" msgstr "" +#: ports/raspberrypi/common-hal/wifi/Radio.c +msgid "Already in access point mode." +msgstr "" + #: ports/espressif/common-hal/coproc/__init__.c #: shared-module/memorymonitor/AllocationAlarm.c #: shared-module/memorymonitor/AllocationSize.c @@ -3153,7 +3165,7 @@ msgid "index is out of bounds" msgstr "" #: extmod/ulab/code/numpy/numerical.c extmod/ulab/code/ulab_tools.c -#: ports/espressif/common-hal/pulseio/PulseIn.c py/obj.c +#: ports/espressif/common-hal/pulseio/PulseIn.c #: shared-bindings/bitmaptools/__init__.c msgid "index out of range" msgstr "" diff --git a/ports/raspberrypi/common-hal/wifi/Radio.c b/ports/raspberrypi/common-hal/wifi/Radio.c index b70543a43c..ea0cde78bc 100644 --- a/ports/raspberrypi/common-hal/wifi/Radio.c +++ b/ports/raspberrypi/common-hal/wifi/Radio.c @@ -155,12 +155,11 @@ void common_hal_wifi_radio_start_station(wifi_radio_obj_t *self) { } void common_hal_wifi_radio_stop_station(wifi_radio_obj_t *self) { + + // This is wrong This is fine. cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA); - // This is wrong, but without this call the state of ITF_STA is still - // reported as CYW43_LINK_JOIN (by wifi_link_status) and CYW43_LINK_UP - // (by tcpip_link_status). Until AP support is added, we can ignore the - // problem. cyw43_wifi_leave(&cyw43_state, CYW43_ITF_AP); + bindings_cyw43_wifi_enforce_pm(); } @@ -168,21 +167,41 @@ void common_hal_wifi_radio_start_ap(wifi_radio_obj_t *self, uint8_t *ssid, size_ if (!common_hal_wifi_radio_get_enabled(self)) { mp_raise_RuntimeError(translate("wifi is not enabled")); } - // Is there a better way? - common_hal_wifi_radio_stop_station(self); + + if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA) != CYW43_LINK_DOWN) { + mp_raise_RuntimeError(translate("Already connected to station.")); + } + + common_hal_wifi_radio_stop_ap(self); // Channel can only be changed after inital powerup and config of ap. // Defaults to 1 if not set or invalid (i.e. 13) cyw43_wifi_ap_set_channel(&cyw43_state, (const uint32_t)channel); cyw43_arch_enable_ap_mode((const char *)ssid, (const char *)password, CYW43_AUTH_WPA2_AES_PSK); + // TODO: Implement authmode check like in espressif bindings_cyw43_wifi_enforce_pm(); } void common_hal_wifi_radio_stop_ap(wifi_radio_obj_t *self) { - common_hal_wifi_radio_stop_station(self); - // I mean, since it already does both.. + if (!common_hal_wifi_radio_get_enabled(self)) { + mp_raise_RuntimeError(translate("wifi is not enabled")); + } + + if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_DOWN) { + mp_raise_NotImplementedError(translate("AP cannot be stopped.")); + } + + /* + * AP cannot be disconnected. cyw43_wifi_leave is broken. + * This code snippet should work, but doesn't. + * + * cyw43_wifi_leave(&cyw43_state, CYW43_ITF_AP); + * cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA); + * + * bindings_cyw43_wifi_enforce_pm(); + */ } wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t *ssid, size_t ssid_len, uint8_t *password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t *bssid, size_t bssid_len) { @@ -190,6 +209,11 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t mp_raise_RuntimeError(translate("wifi is not enabled")); } + if (cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_AP) != CYW43_LINK_DOWN) { + mp_raise_RuntimeError(translate("Already in access point mode.")); + } + + size_t timeout_ms = timeout <= 0 ? 8000 : (size_t)MICROPY_FLOAT_C_FUN(ceil)(timeout * 1000); uint64_t start = port_get_raw_ticks(NULL); uint64_t deadline = start + timeout_ms;