From 0c4352fb526b4e106dc736b0d1b174c0791f755c Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 21 Aug 2026 07:38:20 -0700 Subject: [PATCH 01/10] zephyr-cp/wifi: subscribe to NET_EVENT_WIFI_SCAN_RESULT The event handler has a NET_EVENT_WIFI_SCAN_RESULT case that queues each AP as it arrives, but that event was never in the subscription mask, so the case never ran and scans always returned zero networks. RAW_SCAN_RESULT is in the mask but is not a substitute. It carries raw beacon frames and only fires when CONFIG_WIFI_MGMT_RAW_SCAN_RESULTS is enabled, which it is not by default. Measured on a Raspberry Pi Pico 2 W running raspberrypi_rpi_pico2_w_zephyr, built from 069144c66 and flashed over SWD with pyOCD: len([1 for n in wifi.radio.start_scanning_networks()]) before 0 after 204 Same board, same probe, same script, with only this change reverted for the before run. --- ports/zephyr-cp/common-hal/wifi/__init__.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/zephyr-cp/common-hal/wifi/__init__.c b/ports/zephyr-cp/common-hal/wifi/__init__.c index 4b967bc2780..81ebdfdc955 100644 --- a/ports/zephyr-cp/common-hal/wifi/__init__.c +++ b/ports/zephyr-cp/common-hal/wifi/__init__.c @@ -278,6 +278,7 @@ void common_hal_wifi_init(bool user_initiated) { // self->ap_mode = 0; net_mgmt_init_event_callback(&wifi_cb, _event_handler, + NET_EVENT_WIFI_SCAN_RESULT | NET_EVENT_WIFI_SCAN_DONE | NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | From dfe806d68e1a66eddbdde12cbb3b03f37adbfd33 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Sat, 22 Aug 2026 13:57:45 -0700 Subject: [PATCH 02/10] zephyr-cp/wifi: route diagnostic output through the log subsystem The Wi-Fi common-hal printed on every net event with raw printk. That output is unconditional, so it corrupts the serial handshake that raw-REPL tooling relies on, and it cannot be turned down per module. Register a cp_wifi log module and route the existing calls through it, at CONFIG_LOG_DEFAULT_LEVEL as supervisor/usb.c already does. Two printks in start_scanning_networks() only restated the message raised on the following line, so they are dropped rather than converted. Also fixes two defects the conversion exposed: - The unhandled-event print passed a uint64_t mgmt_event to %x, truncating to 32 bits. Since the layer lives in the high bits, every unhandled Wi-Fi event aliased to the same value. - NET_EVENT_IPV4_ADDR_ADD was already subscribed but had no case, so it fell through to the unhandled-event path and the status bar kept reading "No IP" after DHCP bound, while wifi.radio.ipv4_address returned the real lease. --- ports/zephyr-cp/common-hal/wifi/Radio.c | 15 ++-- .../common-hal/wifi/ScannedNetworks.c | 7 +- ports/zephyr-cp/common-hal/wifi/__init__.c | 84 +++++++++++-------- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index 35a0b76a362..9ae19b5392a 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -25,6 +25,7 @@ #include "bindings/zephyr_kernel/__init__.h" #include +#include #include #include #include @@ -33,6 +34,8 @@ #include "common-hal/mdns/Server.h" #endif +LOG_MODULE_DECLARE(cp_wifi); + #define MAC_ADDRESS_LENGTH 6 // static void set_mode_station(wifi_radio_obj_t *self, bool state) { @@ -85,7 +88,7 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) { // #if CIRCUITPY_MDNS // mdns_server_deinit_singleton(); // #endif - printk("net_if_down\n"); + LOG_DBG("net_if_down"); int res = net_if_down(self->sta_netif); if (res < 0 && res != -EALREADY) { raise_zephyr_error(res); @@ -94,7 +97,7 @@ void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled) { return; } if (!self->started && enabled) { - printk("net_if_up\n"); + LOG_DBG("net_if_up"); int res = net_if_up(self->sta_netif); if (res < 0 && res != -EALREADY) { raise_zephyr_error(res); @@ -214,13 +217,11 @@ void common_hal_wifi_radio_set_mac_address_ap(wifi_radio_obj_t *self, const uint } mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self, uint8_t start_channel, uint8_t stop_channel) { - printk("common_hal_wifi_radio_start_scanning_networks\n"); + LOG_DBG("common_hal_wifi_radio_start_scanning_networks"); if (self->current_scan != NULL) { - printk("Already scanning for wifi networks\n"); mp_raise_RuntimeError(MP_ERROR_TEXT("Already scanning for wifi networks")); } if (!common_hal_wifi_radio_get_enabled(self)) { - printk("WiFi is not enabled\n"); mp_raise_RuntimeError(MP_ERROR_TEXT("WiFi is not enabled")); } @@ -246,12 +247,12 @@ mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self, u K_POLL_MODE_NOTIFY_ONLY, &scan->msgq); wifi_scannednetworks_scan_next_channel(scan); - printk("common_hal_wifi_radio_start_scanning_networks done %p\n", scan); + LOG_DBG("common_hal_wifi_radio_start_scanning_networks done %p", scan); return scan; } void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) { - printk("common_hal_wifi_radio_stop_scanning_networks\n"); + LOG_DBG("common_hal_wifi_radio_stop_scanning_networks"); // Return early if self->current_scan is NULL to avoid hang if (self->current_scan == NULL) { return; diff --git a/ports/zephyr-cp/common-hal/wifi/ScannedNetworks.c b/ports/zephyr-cp/common-hal/wifi/ScannedNetworks.c index 725bf1fa7cb..d7df14ffa5c 100644 --- a/ports/zephyr-cp/common-hal/wifi/ScannedNetworks.c +++ b/ports/zephyr-cp/common-hal/wifi/ScannedNetworks.c @@ -19,12 +19,15 @@ #include "bindings/zephyr_kernel/__init__.h" #include +#include #include +LOG_MODULE_DECLARE(cp_wifi); + void wifi_scannednetworks_scan_result(wifi_scannednetworks_obj_t *self, struct wifi_scan_result *result) { if (k_msgq_put(&self->msgq, result, K_NO_WAIT) != 0) { - printk("Dropping scan result!\n"); + LOG_WRN("Dropping scan result"); } } @@ -104,7 +107,7 @@ void wifi_scannednetworks_scan_next_channel(wifi_scannednetworks_obj_t *self) { } else { int res = net_mgmt(NET_REQUEST_WIFI_SCAN, self->netif, ¶ms, sizeof(params)); if (res != 0) { - printk("Failed to start wifi scan %d\n", res); + LOG_ERR("Failed to start wifi scan %d", res); raise_zephyr_error(res); wifi_scannednetworks_done(self); } else { diff --git a/ports/zephyr-cp/common-hal/wifi/__init__.c b/ports/zephyr-cp/common-hal/wifi/__init__.c index 81ebdfdc955..213ef7f618f 100644 --- a/ports/zephyr-cp/common-hal/wifi/__init__.c +++ b/ports/zephyr-cp/common-hal/wifi/__init__.c @@ -31,12 +31,15 @@ wifi_radio_obj_t common_hal_wifi_radio_obj; #endif #include +#include #include #include #define MAC_ADDRESS_LENGTH 6 +LOG_MODULE_REGISTER(cp_wifi, CONFIG_LOG_DEFAULT_LEVEL); + static void schedule_background_on_cp_core(void *arg) { #if CIRCUITPY_STATUS_BAR supervisor_status_bar_request_update(false); @@ -56,7 +59,7 @@ static void _event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_eve switch (mgmt_event) { case NET_EVENT_WIFI_SCAN_RESULT: { - printk("NET_EVENT_WIFI_SCAN_RESULT\n"); + LOG_DBG("NET_EVENT_WIFI_SCAN_RESULT"); const struct wifi_scan_result *result = cb->info; if (result != NULL && self->current_scan != NULL) { wifi_scannednetworks_scan_result(self->current_scan, result); @@ -64,7 +67,7 @@ static void _event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_eve break; } case NET_EVENT_WIFI_SCAN_DONE: - printk("NET_EVENT_WIFI_SCAN_DONE (thread: %s prio=%d)\n", + LOG_DBG("NET_EVENT_WIFI_SCAN_DONE (thread: %s prio=%d)", k_thread_name_get(k_current_get()), k_thread_priority_get(k_current_get())); if (self->current_scan != NULL) { @@ -72,46 +75,55 @@ static void _event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_eve } break; case NET_EVENT_WIFI_CONNECT_RESULT: - printk("NET_EVENT_WIFI_CONNECT_RESULT\n"); + LOG_DBG("NET_EVENT_WIFI_CONNECT_RESULT"); break; case NET_EVENT_WIFI_DISCONNECT_RESULT: - printk("NET_EVENT_WIFI_DISCONNECT_RESULT\n"); + LOG_DBG("NET_EVENT_WIFI_DISCONNECT_RESULT"); break; case NET_EVENT_WIFI_IFACE_STATUS: - printk("NET_EVENT_WIFI_IFACE_STATUS\n"); + LOG_DBG("NET_EVENT_WIFI_IFACE_STATUS"); break; case NET_EVENT_WIFI_TWT: - printk("NET_EVENT_WIFI_TWT\n"); + LOG_DBG("NET_EVENT_WIFI_TWT"); break; case NET_EVENT_WIFI_TWT_SLEEP_STATE: - printk("NET_EVENT_WIFI_TWT_SLEEP_STATE\n"); + LOG_DBG("NET_EVENT_WIFI_TWT_SLEEP_STATE"); break; case NET_EVENT_WIFI_RAW_SCAN_RESULT: - printk("NET_EVENT_WIFI_RAW_SCAN_RESULT\n"); + LOG_DBG("NET_EVENT_WIFI_RAW_SCAN_RESULT"); break; case NET_EVENT_WIFI_DISCONNECT_COMPLETE: - printk("NET_EVENT_WIFI_DISCONNECT_COMPLETE\n"); + LOG_DBG("NET_EVENT_WIFI_DISCONNECT_COMPLETE"); break; case NET_EVENT_WIFI_SIGNAL_CHANGE: - printk("NET_EVENT_WIFI_SIGNAL_CHANGE\n"); + LOG_DBG("NET_EVENT_WIFI_SIGNAL_CHANGE"); break; case NET_EVENT_WIFI_NEIGHBOR_REP_COMP: - printk("NET_EVENT_WIFI_NEIGHBOR_REP_COMP\n"); + LOG_DBG("NET_EVENT_WIFI_NEIGHBOR_REP_COMP"); break; case NET_EVENT_WIFI_AP_ENABLE_RESULT: - printk("NET_EVENT_WIFI_AP_ENABLE_RESULT\n"); + LOG_DBG("NET_EVENT_WIFI_AP_ENABLE_RESULT"); break; case NET_EVENT_WIFI_AP_DISABLE_RESULT: - printk("NET_EVENT_WIFI_AP_DISABLE_RESULT\n"); + LOG_DBG("NET_EVENT_WIFI_AP_DISABLE_RESULT"); break; case NET_EVENT_WIFI_AP_STA_CONNECTED: - printk("NET_EVENT_WIFI_AP_STA_CONNECTED\n"); + LOG_DBG("NET_EVENT_WIFI_AP_STA_CONNECTED"); break; case NET_EVENT_WIFI_AP_STA_DISCONNECTED: - printk("NET_EVENT_WIFI_AP_STA_DISCONNECTED\n"); + LOG_DBG("NET_EVENT_WIFI_AP_STA_DISCONNECTED"); + break; + case NET_EVENT_IPV4_ADDR_ADD: + // DHCP bound, or a static address was configured. The address is read + // live by the ipv4_address getter, so nothing is stored here; the + // status bar just needs a refresh or it keeps showing "No IP". + LOG_DBG("NET_EVENT_IPV4_ADDR_ADD"); + schedule_background_on_cp_core(NULL); break; default: - printk("unhandled net event %x\n", mgmt_event); + // Print all 64 bits. The layer lives in the high bits, so a 32-bit + // print collapses every unhandled event in a layer to one value. + LOG_DBG("unhandled net event %llx", (unsigned long long)mgmt_event); break; } } @@ -196,7 +208,7 @@ static bool wifi_user_initiated; void common_hal_wifi_init(bool user_initiated) { wifi_radio_obj_t *self = &common_hal_wifi_radio_obj; - printk("common_hal_wifi_init\n"); + LOG_DBG("common_hal_wifi_init"); if (wifi_inited) { if (user_initiated && !wifi_user_initiated) { @@ -223,8 +235,8 @@ void common_hal_wifi_init(bool user_initiated) { // } self->sta_netif = net_if_get_wifi_sta(); self->ap_netif = net_if_get_wifi_sap(); - printk("sta_netif %p\n", self->sta_netif); - printk("ap_netif %p\n", self->ap_netif); + LOG_DBG("sta_netif %p", self->sta_netif); + LOG_DBG("ap_netif %p", self->ap_netif); struct wifi_iface_status status = { 0 }; @@ -232,39 +244,39 @@ void common_hal_wifi_init(bool user_initiated) { CHECK_ZEPHYR_RESULT(net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, self->sta_netif, &status, sizeof(struct wifi_iface_status))); if (net_if_is_up(self->sta_netif)) { - printk("STA is up\n"); + LOG_DBG("STA is up"); } else { - printk("STA is down\n"); + LOG_DBG("STA is down"); } if (net_if_is_carrier_ok(self->sta_netif)) { - printk("STA carrier is ok\n"); + LOG_DBG("STA carrier is ok"); } else { - printk("STA carrier is not ok\n"); + LOG_DBG("STA carrier is not ok"); } if (net_if_is_dormant(self->sta_netif)) { - printk("STA is dormant\n"); + LOG_DBG("STA is dormant"); } else { - printk("STA is not dormant\n"); + LOG_DBG("STA is not dormant"); } } if (self->ap_netif != NULL) { int res = net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, self->ap_netif, &status, sizeof(struct wifi_iface_status)); - printk("AP status request response %d\n", res); + LOG_DBG("AP status request response %d", res); if (net_if_is_up(self->ap_netif)) { - printk("AP is up\n"); + LOG_DBG("AP is up"); } else { - printk("AP is down\n"); + LOG_DBG("AP is down"); } if (net_if_is_carrier_ok(self->ap_netif)) { - printk("AP carrier is ok\n"); + LOG_DBG("AP carrier is ok"); } else { - printk("AP carrier is not ok\n"); + LOG_DBG("AP carrier is not ok"); } if (net_if_is_dormant(self->ap_netif)) { - printk("AP is dormant\n"); + LOG_DBG("AP is dormant"); } else { - printk("AP is not dormant\n"); + LOG_DBG("AP is not dormant"); } } @@ -318,21 +330,21 @@ void common_hal_wifi_init(bool user_initiated) { char cpy_default_hostname[board_len + (MAC_ADDRESS_LENGTH * 2) + 6]; struct net_linkaddr *mac = net_if_get_link_addr(self->sta_netif); if (mac->len < MAC_ADDRESS_LENGTH) { - printk("MAC address too short"); + LOG_ERR("MAC address too short"); } snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%02x%02x%02x%02x%02x%02x", CIRCUITPY_BOARD_ID + board_trim, mac->addr[0], mac->addr[1], mac->addr[2], mac->addr[3], mac->addr[4], mac->addr[5]); CHECK_ZEPHYR_RESULT(net_hostname_set(cpy_default_hostname, strlen(cpy_default_hostname))); } #else - printk("Hostname support disabled in Zephyr config\n"); + LOG_WRN("Hostname support disabled in Zephyr config"); #endif // set station mode to avoid the default SoftAP common_hal_wifi_radio_start_station(self); // start wifi common_hal_wifi_radio_set_enabled(self, true); - printk("common_hal_wifi_init done\n"); + LOG_DBG("common_hal_wifi_init done"); } void wifi_user_reset(void) { @@ -343,7 +355,7 @@ void wifi_user_reset(void) { } void wifi_reset(void) { - printk("wifi_reset\n"); + LOG_DBG("wifi_reset"); if (!wifi_inited) { return; } From 8b2553e781d2900fcef92cba28a4b834f451ff1c Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Fri, 31 Jul 2026 13:51:54 -0700 Subject: [PATCH 03/10] zephyr-cp/wifi: implement station connect common_hal_wifi_radio_connect() was a stub: the body was commented-out ESP-IDF code and it returned WIFI_RADIO_ERROR_NONE without attempting anything, so connect() silently "succeeded" while never associating. get_connected() returned a hardcoded false and the IPv4 getters returned None. No zephyr-cp board could join a network. Implement connect() with NET_REQUEST_WIFI_CONNECT: - build wifi_connect_req_params from ssid/password/channel/bssid - wait on a semaphore signalled from CONNECT_RESULT (or DISCONNECT_RESULT, which is how a failed attempt reports), honouring the timeout argument and staying interruptible - map wifi_conn_status to the CircuitPython error codes so a wrong password raises AUTH_FAIL instead of appearing to succeed - start DHCPv4 and wait for an address Also implement get_connected(), get_ipv4_address() and get_ipv4_gateway() from the Zephyr net_if state. get_mac_address() returned an uninitialized stack buffer; read the real address from net_if_get_link_addr() instead. Track the associated SSID so a repeat connect() to the same network returns without tearing down a working link, on both the normal and the -EALREADY path. Security is fixed at WIFI_SECURITY_TYPE_PSK here. Transition-mode APs negotiate up from there; per-network selection follows in the next commit. --- ports/zephyr-cp/common-hal/wifi/Radio.c | 160 +++++++++++++++++++-- ports/zephyr-cp/common-hal/wifi/Radio.h | 12 ++ ports/zephyr-cp/common-hal/wifi/__init__.c | 23 ++- 3 files changed, 176 insertions(+), 19 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index 9ae19b5392a..b79085ef021 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -121,8 +122,13 @@ void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *host } mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) { - uint8_t mac[MAC_ADDRESS_LENGTH]; - // esp_wifi_get_mac(ESP_IF_WIFI_STA, mac); + uint8_t mac[MAC_ADDRESS_LENGTH] = { 0 }; + if (self->sta_netif != NULL) { + struct net_linkaddr *addr = net_if_get_link_addr(self->sta_netif); + if (addr != NULL && addr->len >= MAC_ADDRESS_LENGTH) { + memcpy(mac, addr->addr, MAC_ADDRESS_LENGTH); + } + } return mp_obj_new_bytes(mac, MAC_ADDRESS_LENGTH); } @@ -457,12 +463,128 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t // // We're connected, allow us to retry if we get disconnected. // self->retries_left = self->starting_retries; // } + + struct wifi_connect_req_params params = { 0 }; + + params.ssid = ssid; + params.ssid_length = ssid_len; + params.band = WIFI_FREQ_BAND_2_4_GHZ; + params.channel = channel == 0 ? WIFI_CHANNEL_ANY : channel; + params.mfp = WIFI_MFP_OPTIONAL; + params.timeout = SYS_FOREVER_MS; + + if (password_len > 0) { + params.psk = password; + params.psk_length = password_len; + // WPA2-PSK. Drivers that support a WPA2/WPA3 transition AP will + // negotiate up from here; a WPA3-only network needs + // WIFI_SECURITY_TYPE_SAE, which we cannot infer without a prior scan. + params.security = WIFI_SECURITY_TYPE_PSK; + } else { + params.security = WIFI_SECURITY_TYPE_NONE; + } + + if (bssid_len == WIFI_MAC_ADDR_LEN) { + memcpy(params.bssid, bssid, WIFI_MAC_ADDR_LEN); + } + + // Already associated to the network being asked for: leave the link alone. + // supervisor_start_web_workflow() calls connect() on every invocation, so + // tearing the association down here would churn the link continuously. + if (self->connected && + ssid_len == self->current_ssid_len && + memcmp(ssid, self->current_ssid, ssid_len) == 0) { + return WIFI_RADIO_ERROR_NONE; + } + + // Switching networks. Connecting while associated returns -EALREADY and the + // failure path takes the interface down, so disconnect first. + if (self->connected) { + // A failure here is tolerated on purpose: if the interface really is + // unusable, the connect below returns a proper error to the caller. + (void)net_mgmt(NET_REQUEST_WIFI_DISCONNECT, self->sta_netif, NULL, 0); + // Give the controller a moment to tear the association down. + for (int i = 0; i < 40 && self->connected; i++) { + k_msleep(50); + } + self->connected = false; + } + + self->connected = false; + self->last_connect_status = -1; + self->last_disconnect_reason = 0; + k_sem_reset(&self->connect_sem); + + int res = net_mgmt(NET_REQUEST_WIFI_CONNECT, self->sta_netif, ¶ms, sizeof(params)); + if (res == -EALREADY) { + // Record the SSID as the success path does, so the early return above + // matches on a later connect() to the same network. + self->connected = true; + self->current_ssid_len = MIN(ssid_len, sizeof(self->current_ssid)); + memcpy(self->current_ssid, ssid, self->current_ssid_len); + return WIFI_RADIO_ERROR_NONE; + } + if (res < 0) { + return WIFI_RADIO_ERROR_UNSPECIFIED; + } + + // Wait for NET_EVENT_WIFI_CONNECT_RESULT (or a DISCONNECT_RESULT standing + // in for a failed attempt), staying responsive to ctrl-C. + mp_float_t timeout_s = timeout <= 0 ? (mp_float_t)10 : timeout; + int64_t deadline = k_uptime_get() + (int64_t)(timeout_s * 1000); + bool signalled = false; + while (k_uptime_get() < deadline) { + if (k_sem_take(&self->connect_sem, K_MSEC(50)) == 0) { + signalled = true; + break; + } + if (mp_hal_is_interrupted()) { + return WIFI_RADIO_ERROR_UNSPECIFIED; + } + } + + if (!signalled) { + return WIFI_RADIO_ERROR_HANDSHAKE_TIMEOUT; + } + if (!self->connected) { + switch (self->last_connect_status) { + case WIFI_STATUS_CONN_WRONG_PASSWORD: + return WIFI_RADIO_ERROR_AUTH_FAIL; + case WIFI_STATUS_CONN_AP_NOT_FOUND: + return WIFI_RADIO_ERROR_NO_AP_FOUND; + case WIFI_STATUS_CONN_TIMEOUT: + return WIFI_RADIO_ERROR_HANDSHAKE_TIMEOUT; + default: + return WIFI_RADIO_ERROR_CONNECTION_FAIL; + } + } + + // Remember which network this association is for, so a later connect() for + // the same SSID can return without disturbing it. + self->current_ssid_len = MIN(ssid_len, sizeof(self->current_ssid)); + memcpy(self->current_ssid, ssid, self->current_ssid_len); + + // Associated. Ask for an address; the AP side of DHCP can take a moment. + #if defined(CONFIG_NET_DHCPV4) + net_dhcpv4_start(self->sta_netif); + int64_t ip_deadline = k_uptime_get() + 15000; + while (k_uptime_get() < ip_deadline) { + if (net_if_ipv4_get_global_addr(self->sta_netif, NET_ADDR_PREFERRED) != NULL) { + break; + } + if (mp_hal_is_interrupted()) { + break; + } + k_msleep(50); + } + #endif + return WIFI_RADIO_ERROR_NONE; } bool common_hal_wifi_radio_get_connected(wifi_radio_obj_t *self) { - // return self->sta_mode && esp_netif_is_netif_up(self->netif); - return false; + return self->connected && self->sta_netif != NULL && + net_if_is_up(self->sta_netif); } mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) { @@ -500,11 +622,17 @@ mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) { } mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) { - // if (!esp_netif_is_netif_up(self->netif)) { - return mp_const_none; - // } - // esp_netif_get_ip_info(self->netif, &self->ip_info); - // return common_hal_ipaddress_new_ipv4address(self->ip_info.gw.addr); + if (self->sta_netif == NULL || !net_if_is_up(self->sta_netif)) { + return mp_const_none; + } + const struct net_if_config *cfg = net_if_get_config(self->sta_netif); + if (cfg == NULL || cfg->ip.ipv4 == NULL) { + return mp_const_none; + } + if (cfg->ip.ipv4->gw.s_addr == 0) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(cfg->ip.ipv4->gw.s_addr); } mp_obj_t common_hal_wifi_radio_get_ipv4_gateway_ap(wifi_radio_obj_t *self) { @@ -582,12 +710,14 @@ uint32_t wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) { } mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) { - // if (!esp_netif_is_netif_up(self->netif)) { - // return mp_const_none; - // } - // esp_netif_get_ip_info(self->netif, &self->ip_info); - // return common_hal_ipaddress_new_ipv4address(self->ip_info.ip.addr); - return mp_const_none; + if (self->sta_netif == NULL || !net_if_is_up(self->sta_netif)) { + return mp_const_none; + } + struct in_addr *addr = net_if_ipv4_get_global_addr(self->sta_netif, NET_ADDR_PREFERRED); + if (addr == NULL) { + return mp_const_none; + } + return common_hal_ipaddress_new_ipv4address(addr->s_addr); } mp_obj_t common_hal_wifi_radio_get_ipv4_address_ap(wifi_radio_obj_t *self) { diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.h b/ports/zephyr-cp/common-hal/wifi/Radio.h index f177f493685..2500079df09 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.h +++ b/ports/zephyr-cp/common-hal/wifi/Radio.h @@ -11,7 +11,9 @@ #include "shared-bindings/wifi/ScannedNetworks.h" #include "shared-bindings/wifi/Network.h" +#include #include +#include // Event bits for the Radio event group. #define WIFI_SCAN_DONE_BIT BIT0 @@ -38,6 +40,16 @@ typedef struct { uint8_t retries_left; uint8_t starting_retries; uint8_t last_disconnect_reason; + // Signalled from the net_mgmt event handler when a connect attempt + // finishes, so common_hal_wifi_radio_connect() can wait on the result. + struct k_sem connect_sem; + // Latest wifi_conn_status from NET_EVENT_WIFI_CONNECT_RESULT. + int last_connect_status; + bool connected; + // SSID of the association that `connected` refers to, so that a connect() + // for the network we are already on can return without touching the link. + uint8_t current_ssid[WIFI_SSID_MAX_LEN]; + size_t current_ssid_len; } wifi_radio_obj_t; extern void common_hal_wifi_radio_gc_collect(wifi_radio_obj_t *self); diff --git a/ports/zephyr-cp/common-hal/wifi/__init__.c b/ports/zephyr-cp/common-hal/wifi/__init__.c index 213ef7f618f..182ae5a3c30 100644 --- a/ports/zephyr-cp/common-hal/wifi/__init__.c +++ b/ports/zephyr-cp/common-hal/wifi/__init__.c @@ -74,12 +74,24 @@ static void _event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_eve k_poll_signal_raise(&self->current_scan->channel_done, 0); } break; - case NET_EVENT_WIFI_CONNECT_RESULT: - LOG_DBG("NET_EVENT_WIFI_CONNECT_RESULT"); + case NET_EVENT_WIFI_CONNECT_RESULT: { + const struct wifi_status *status = cb->info; + self->last_connect_status = status != NULL ? status->status : -1; + self->connected = self->last_connect_status == WIFI_STATUS_CONN_SUCCESS; + LOG_DBG("NET_EVENT_WIFI_CONNECT_RESULT status %d", self->last_connect_status); + k_sem_give(&self->connect_sem); break; - case NET_EVENT_WIFI_DISCONNECT_RESULT: - LOG_DBG("NET_EVENT_WIFI_DISCONNECT_RESULT"); + } + case NET_EVENT_WIFI_DISCONNECT_RESULT: { + const struct wifi_status *status = cb->info; + self->last_disconnect_reason = status != NULL ? (uint8_t)status->status : 0; + self->connected = false; + LOG_DBG("NET_EVENT_WIFI_DISCONNECT_RESULT reason %d", self->last_disconnect_reason); + // A disconnect can also be the failure result of a connect attempt, + // so release any waiter rather than letting it sit until timeout. + k_sem_give(&self->connect_sem); break; + } case NET_EVENT_WIFI_IFACE_STATUS: LOG_DBG("NET_EVENT_WIFI_IFACE_STATUS"); break; @@ -219,6 +231,9 @@ void common_hal_wifi_init(bool user_initiated) { wifi_inited = true; wifi_user_initiated = user_initiated; self->base.type = &wifi_radio_type; + k_sem_init(&self->connect_sem, 0, 1); + self->connected = false; + self->last_connect_status = -1; // struct net_if *default_iface = net_if_get_default(); // printk("default interface %p\n", default_iface); From 9f5b9e824804de59e5096dd200cdf1b4f4eef901 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 3 Aug 2026 17:35:53 -0700 Subject: [PATCH 04/10] zephyr-cp/wifi: per-AP security selection, real authmode, IPv4 getters Security type has to be chosen per network. The SiWx91x driver maps WIFI_SECURITY_TYPE_PSK to SL_WIFI_WPA2 and WPA_AUTO_PERSONAL to SL_WIFI_WPA3_TRANSITION, and neither works everywhere: a WPA2-PSK AP rejects WPA3 transition and a WPA3-SAE AP rejects WPA2, both surfacing identically as "Authentication failure". So cache the most recent scan (24 entries, same-SSID replace) and look the SSID up in connect(), falling back to WPA2-PSK when it was not seen. Known limit: that fallback is silently wrong for a WPA3-only hidden AP. get_authmode() built its mask from a switch that was entirely commented out (ESP-IDF leftover) and always returned an empty list, which reads as an open network. Translate Zephyr's wifi_security_type instead. The EAP and OWE arms are taken from the header and are not exercised on hardware. Adds the ipv4_subnet and ipv4_dns getters alongside the address and gateway getters from the previous commit. Co-Authored-By: Claude Fable 5 --- ports/zephyr-cp/common-hal/wifi/Network.c | 65 ++++++++++-------- ports/zephyr-cp/common-hal/wifi/Radio.c | 78 ++++++++++++++++------ ports/zephyr-cp/common-hal/wifi/__init__.c | 41 +++++++++++- ports/zephyr-cp/common-hal/wifi/__init__.h | 6 ++ 4 files changed, 139 insertions(+), 51 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Network.c b/ports/zephyr-cp/common-hal/wifi/Network.c index 44c049f88c1..510f8b9f5c2 100644 --- a/ports/zephyr-cp/common-hal/wifi/Network.c +++ b/ports/zephyr-cp/common-hal/wifi/Network.c @@ -34,35 +34,44 @@ mp_obj_t common_hal_wifi_network_get_country(wifi_network_obj_t *self) { } mp_obj_t common_hal_wifi_network_get_authmode(wifi_network_obj_t *self) { + // Translate Zephyr's wifi_security_type. An empty list would read as an + // open network to any caller checking for AUTHMODE_OPEN. uint32_t authmode_mask = 0; - // switch (self->record.authmode) { - // case WIFI_AUTH_OPEN: - // authmode_mask = AUTHMODE_OPEN; - // break; - // case WIFI_AUTH_WEP: - // authmode_mask = AUTHMODE_WEP; - // break; - // case WIFI_AUTH_WPA_PSK: - // authmode_mask = AUTHMODE_WPA | AUTHMODE_PSK; - // break; - // case WIFI_AUTH_WPA2_PSK: - // authmode_mask = AUTHMODE_WPA2 | AUTHMODE_PSK; - // break; - // case WIFI_AUTH_WPA_WPA2_PSK: - // authmode_mask = AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_PSK; - // break; - // case WIFI_AUTH_WPA2_ENTERPRISE: - // authmode_mask = AUTHMODE_WPA2 | AUTHMODE_ENTERPRISE; - // break; - // case WIFI_AUTH_WPA3_PSK: - // authmode_mask = AUTHMODE_WPA3 | AUTHMODE_PSK; - // break; - // case WIFI_AUTH_WPA2_WPA3_PSK: - // authmode_mask = AUTHMODE_WPA2 | AUTHMODE_WPA3 | AUTHMODE_PSK; - // break; - // default: - // break; - // } + switch (self->scan_result.security) { + case WIFI_SECURITY_TYPE_NONE: + authmode_mask = AUTHMODE_OPEN; + break; + case WIFI_SECURITY_TYPE_WEP: + authmode_mask = AUTHMODE_WEP; + break; + case WIFI_SECURITY_TYPE_WPA_PSK: + authmode_mask = AUTHMODE_WPA | AUTHMODE_PSK; + break; + case WIFI_SECURITY_TYPE_PSK: + case WIFI_SECURITY_TYPE_PSK_SHA256: + authmode_mask = AUTHMODE_WPA2 | AUTHMODE_PSK; + break; + case WIFI_SECURITY_TYPE_SAE: // == WIFI_SECURITY_TYPE_SAE_HNP (alias) + case WIFI_SECURITY_TYPE_SAE_H2E: + case WIFI_SECURITY_TYPE_SAE_AUTO: + case WIFI_SECURITY_TYPE_SAE_EXT_KEY: + case WIFI_SECURITY_TYPE_FT_SAE: + authmode_mask = AUTHMODE_WPA3 | AUTHMODE_PSK; + break; + case WIFI_SECURITY_TYPE_WPA_AUTO_PERSONAL: + authmode_mask = AUTHMODE_WPA | AUTHMODE_WPA2 | AUTHMODE_WPA3 | AUTHMODE_PSK; + break; + case WIFI_SECURITY_TYPE_EAP: // == WIFI_SECURITY_TYPE_EAP_TLS (alias) + case WIFI_SECURITY_TYPE_EAP_PEAP_MSCHAPV2: + case WIFI_SECURITY_TYPE_EAP_PEAP_GTC: + case WIFI_SECURITY_TYPE_EAP_TTLS_MSCHAPV2: + case WIFI_SECURITY_TYPE_EAP_PEAP_TLS: + case WIFI_SECURITY_TYPE_FT_EAP: + authmode_mask = AUTHMODE_WPA2 | AUTHMODE_ENTERPRISE; + break; + default: + break; + } mp_obj_t authmode_list = mp_obj_new_list(0, NULL); if (authmode_mask != 0) { for (uint8_t i = 0; i < 32; i++) { diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index b79085ef021..f3de72f24bf 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -27,6 +27,8 @@ #include #include #include +// dns_resolve_get_default() for radio.ipv4_dns. +#include #include #include #include @@ -476,10 +478,28 @@ wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t if (password_len > 0) { params.psk = password; params.psk_length = password_len; - // WPA2-PSK. Drivers that support a WPA2/WPA3 transition AP will - // negotiate up from here; a WPA3-only network needs - // WIFI_SECURITY_TYPE_SAE, which we cannot infer without a prior scan. + // The security type must match what the AP advertises: this driver maps + // PSK to WPA2 and WPA_AUTO_PERSONAL to WPA3-transition, and rejects the + // wrong one with a generic "Authentication failure". So take it from the + // last scan, falling back to WPA2-PSK when the SSID was not seen. That + // fallback is wrong for a WPA3-only hidden AP. params.security = WIFI_SECURITY_TYPE_PSK; + struct wifi_scan_result *cached = wifi_cached_scan_lookup(ssid, ssid_len); + if (cached != NULL) { + switch (cached->security) { + case WIFI_SECURITY_TYPE_SAE: + case WIFI_SECURITY_TYPE_SAE_H2E: + case WIFI_SECURITY_TYPE_SAE_AUTO: + params.security = WIFI_SECURITY_TYPE_WPA_AUTO_PERSONAL; + break; + case WIFI_SECURITY_TYPE_WPA_PSK: + params.security = WIFI_SECURITY_TYPE_WPA_PSK; + break; + default: + params.security = WIFI_SECURITY_TYPE_PSK; + break; + } + } } else { params.security = WIFI_SECURITY_TYPE_NONE; } @@ -644,11 +664,21 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_gateway_ap(wifi_radio_obj_t *self) { } mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) { - // if (!esp_netif_is_netif_up(self->netif)) { + if (self->sta_netif == NULL || !net_if_is_up(self->sta_netif)) { + return mp_const_none; + } + struct net_if_ipv4 *ipv4 = self->sta_netif->config.ip.ipv4; + if (ipv4 == NULL) { + return mp_const_none; + } + for (int i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) { + if (ipv4->unicast[i].ipv4.is_used && + ipv4->unicast[i].ipv4.addr_state == NET_ADDR_PREFERRED) { + return common_hal_ipaddress_new_ipv4address( + ipv4->unicast[i].netmask.s_addr); + } + } return mp_const_none; - // } - // esp_netif_get_ip_info(self->netif, &self->ip_info); - // return common_hal_ipaddress_new_ipv4address(self->ip_info.netmask.addr); } mp_obj_t common_hal_wifi_radio_get_ipv4_subnet_ap(wifi_radio_obj_t *self) { @@ -730,20 +760,26 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_address_ap(wifi_radio_obj_t *self) { } mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self) { - // if (!esp_netif_is_netif_up(self->netif)) { - // return mp_const_none; - // } - - // esp_netif_get_dns_info(self->netif, ESP_NETIF_DNS_MAIN, &self->dns_info); - - // if (self->dns_info.ip.type != ESP_IPADDR_TYPE_V4) { - // return mp_const_none; - // } - // // dns_info is of type esp_netif_dns_info_t, which is just ever so slightly - // // different than esp_netif_ip_info_t used for - // // common_hal_wifi_radio_get_ipv4_address (includes both ipv4 and 6), - // // so some extra jumping is required to get to the actual address - // return common_hal_ipaddress_new_ipv4address(self->dns_info.ip.u_addr.ip4.addr); + // Zephyr keeps resolver state in the DNS resolve context rather than on + // the interface, so read it there. + #if defined(CONFIG_DNS_RESOLVER) + if (self->sta_netif == NULL || !net_if_is_up(self->sta_netif)) { + return mp_const_none; + } + struct dns_resolve_context *ctx = dns_resolve_get_default(); + if (ctx == NULL) { + return mp_const_none; + } + for (int i = 0; i < CONFIG_DNS_RESOLVER_MAX_SERVERS; i++) { + if (ctx->servers[i].dns_server.sa_family == AF_INET) { + struct sockaddr_in *addr = + (struct sockaddr_in *)&ctx->servers[i].dns_server; + if (addr->sin_addr.s_addr != 0) { + return common_hal_ipaddress_new_ipv4address(addr->sin_addr.s_addr); + } + } + } + #endif return mp_const_none; } diff --git a/ports/zephyr-cp/common-hal/wifi/__init__.c b/ports/zephyr-cp/common-hal/wifi/__init__.c index 182ae5a3c30..33f1ad13ed8 100644 --- a/ports/zephyr-cp/common-hal/wifi/__init__.c +++ b/ports/zephyr-cp/common-hal/wifi/__init__.c @@ -4,6 +4,8 @@ // // SPDX-License-Identifier: MIT +#include + #include "common-hal/wifi/__init__.h" #include "shared-bindings/wifi/__init__.h" @@ -53,6 +55,36 @@ static void schedule_background_on_cp_core(void *arg) { static struct net_mgmt_event_callback wifi_cb; static struct net_mgmt_event_callback ipv4_cb; +// Small cache of the most recent scan, used by common_hal_wifi_radio_connect() +// to pick the right security type per AP. +#define WIFI_SCAN_CACHE_LEN 24 +static struct wifi_scan_result scan_cache[WIFI_SCAN_CACHE_LEN]; +static size_t scan_cache_count; + +struct wifi_scan_result *wifi_cached_scan_lookup(const uint8_t *ssid, size_t ssid_len) { + for (size_t i = 0; i < scan_cache_count; i++) { + if (scan_cache[i].ssid_length == ssid_len && + memcmp(scan_cache[i].ssid, ssid, ssid_len) == 0) { + return &scan_cache[i]; + } + } + return NULL; +} + +static void wifi_scan_cache_add(const struct wifi_scan_result *result) { + // Replace an existing entry for the same SSID so the cache tracks the + // latest reading rather than filling up with duplicate BSSIDs. + struct wifi_scan_result *existing = + wifi_cached_scan_lookup(result->ssid, result->ssid_length); + if (existing != NULL) { + *existing = *result; + return; + } + if (scan_cache_count < WIFI_SCAN_CACHE_LEN) { + scan_cache[scan_cache_count++] = *result; + } +} + static void _event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, struct net_if *iface) { wifi_radio_obj_t *self = &common_hal_wifi_radio_obj; (void)iface; @@ -61,8 +93,13 @@ static void _event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_eve case NET_EVENT_WIFI_SCAN_RESULT: { LOG_DBG("NET_EVENT_WIFI_SCAN_RESULT"); const struct wifi_scan_result *result = cb->info; - if (result != NULL && self->current_scan != NULL) { - wifi_scannednetworks_scan_result(self->current_scan, result); + if (result != NULL) { + // Remember the authmode so connect() can request the matching + // security type later. + wifi_scan_cache_add(result); + if (self->current_scan != NULL) { + wifi_scannednetworks_scan_result(self->current_scan, result); + } } break; } diff --git a/ports/zephyr-cp/common-hal/wifi/__init__.h b/ports/zephyr-cp/common-hal/wifi/__init__.h index dab519b1a5f..9f730003878 100644 --- a/ports/zephyr-cp/common-hal/wifi/__init__.h +++ b/ports/zephyr-cp/common-hal/wifi/__init__.h @@ -8,10 +8,16 @@ #include "py/obj.h" +#include + struct sockaddr_storage; void wifi_reset(void); +// Look up an SSID in the cache of the most recent scan. Returns NULL if the +// network was not seen. +struct wifi_scan_result *wifi_cached_scan_lookup(const uint8_t *ssid, size_t ssid_len); + // void ipaddress_ipaddress_to_esp_idf(mp_obj_t ip_address, ip_addr_t *esp_ip_address); // void ipaddress_ipaddress_to_esp_idf_ip4(mp_obj_t ip_address, esp_ip4_addr_t *esp_ip_address); From c5b74ac63268f4f0a6179532637b0986a86faed8 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 3 Aug 2026 20:46:28 -0700 Subject: [PATCH 05/10] zephyr-cp/wifi: fix the "ip" field in version.json wifi_radio_get_ipv4_address(), the raw uint32_t getter that supervisor/shared/web_workflow/web_workflow.c uses for the status bar and for /cp/version.json's "ip" field, was a leftover ESP-IDF stub that returned 0 unconditionally. It is a separate entry point from common_hal_wifi_radio_get_ipv4_address(), the Python-facing getter: one underlying address, two functions, only one of them implemented. board_name and hostname in version.json stay empty, for an unrelated reason: both come from the mDNS responder, and zephyr-cp has no common-hal/mdns, so CIRCUITPY_MDNS never reaches web_workflow.c. That is a new component rather than a bug fix, so it is left out of this series. Co-Authored-By: Claude Sonnet 5 --- ports/zephyr-cp/common-hal/wifi/Radio.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index f3de72f24bf..60df2d55e31 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -731,12 +731,16 @@ mp_obj_t common_hal_wifi_radio_get_addresses_ap(wifi_radio_obj_t *self) { } uint32_t wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) { - // if (!esp_netif_is_netif_up(self->netif)) { - // return 0; - // } - // esp_netif_get_ip_info(self->netif, &self->ip_info); - // return self->ip_info.ip.addr; - return 0; + // Raw uint32_t sibling of common_hal_wifi_radio_get_ipv4_address(), + // used internally by supervisor/shared/web_workflow/web_workflow.c. + if (self->sta_netif == NULL || !net_if_is_up(self->sta_netif)) { + return 0; + } + struct in_addr *addr = net_if_ipv4_get_global_addr(self->sta_netif, NET_ADDR_PREFERRED); + if (addr == NULL) { + return 0; + } + return addr->s_addr; } mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) { From be7b0b46a52ce24b40937a0c76e91958fd3cd785 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 3 Aug 2026 20:58:48 -0700 Subject: [PATCH 06/10] zephyr-cp/wifi: fix wifi.radio.addresses common_hal_wifi_radio_get_addresses() returned mp_const_none unconditionally, which is the wrong type in both states for the shared-bindings contract ("addresses: Sequence[str] ... Empty sequence when not connected"): None instead of a tuple when connected, None instead of an empty tuple when not. Reuse wifi_radio_get_ipv4_address() and format it as a string, which is what the espressif and raspberrypi ports return here rather than IPv4Address objects. get_addresses_ap() had the same problem and is corrected to mp_const_empty_tuple, without claiming AP mode works. Co-Authored-By: Claude Sonnet 5 --- ports/zephyr-cp/common-hal/wifi/Radio.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index 60df2d55e31..007c90f5b46 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -721,13 +721,24 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_subnet_ap(wifi_radio_obj_t *self) { // } mp_obj_t common_hal_wifi_radio_get_addresses(wifi_radio_obj_t *self) { - // return common_hal_wifi_radio_get_addresses_netif(self, self->netif); - return mp_const_none; + // shared-bindings documents this as Sequence[str], empty when not + // connected, so format as a dotted-quad string rather than returning an + // IPv4Address object. Same address as wifi_radio_get_ipv4_address(). + uint32_t ipv4_address = wifi_radio_get_ipv4_address(self); + if (ipv4_address == 0) { + return mp_const_empty_tuple; + } + uint8_t *octets = (uint8_t *)&ipv4_address; + char buf[16]; + snprintf(buf, sizeof(buf), "%d.%d.%d.%d", octets[0], octets[1], octets[2], octets[3]); + mp_obj_t args[] = { mp_obj_new_str(buf, strlen(buf)) }; + return mp_obj_new_tuple(MP_ARRAY_SIZE(args), args); } mp_obj_t common_hal_wifi_radio_get_addresses_ap(wifi_radio_obj_t *self) { - // return common_hal_wifi_radio_get_addresses_netif(self, self->ap_netif); - return mp_const_none; + // AP mode is unimplemented here, but mp_const_none is still the wrong type + // for the Sequence[str] contract. + return mp_const_empty_tuple; } uint32_t wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) { From d16ed053bfb6e8dc997b285fb4e58f9bdbdb1980 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Tue, 4 Aug 2026 06:28:11 -0700 Subject: [PATCH 07/10] zephyr-cp/wifi: expose raw MAC getter, guard net_if_ip.ipv4 access wifi_radio_get_mac_address(self, uint8_t *) is declared in shared-bindings/wifi/Radio.h but was never implemented for this port; only the Python-facing common_hal_wifi_radio_get_mac_address() existed. Add the raw helper and have the existing function call it rather than duplicating the netif read. common_hal_wifi_radio_get_ipv4_gateway() and _subnet() read net_if_ip.ipv4 unconditionally, but that struct member only exists when CONFIG_NET_IPV4 is set. This file builds for every Wi-Fi board in the port's CI matrix, and nrf7002dk does not enable IPv4, so the unguarded access breaks that build. Guard both. --- ports/zephyr-cp/common-hal/wifi/Radio.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index 007c90f5b46..a615c424259 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -123,14 +123,19 @@ void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *host } } -mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) { - uint8_t mac[MAC_ADDRESS_LENGTH] = { 0 }; +void wifi_radio_get_mac_address(wifi_radio_obj_t *self, uint8_t *mac) { + memset(mac, 0, MAC_ADDRESS_LENGTH); if (self->sta_netif != NULL) { struct net_linkaddr *addr = net_if_get_link_addr(self->sta_netif); if (addr != NULL && addr->len >= MAC_ADDRESS_LENGTH) { memcpy(mac, addr->addr, MAC_ADDRESS_LENGTH); } } +} + +mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self) { + uint8_t mac[MAC_ADDRESS_LENGTH]; + wifi_radio_get_mac_address(self, mac); return mp_obj_new_bytes(mac, MAC_ADDRESS_LENGTH); } @@ -645,6 +650,9 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) { if (self->sta_netif == NULL || !net_if_is_up(self->sta_netif)) { return mp_const_none; } + // net_if_ip.ipv4 only exists with CONFIG_NET_IPV4, and this file builds for + // every Wi-Fi board in the port, not just ones that enable it. + #if defined(CONFIG_NET_IPV4) const struct net_if_config *cfg = net_if_get_config(self->sta_netif); if (cfg == NULL || cfg->ip.ipv4 == NULL) { return mp_const_none; @@ -653,6 +661,9 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self) { return mp_const_none; } return common_hal_ipaddress_new_ipv4address(cfg->ip.ipv4->gw.s_addr); + #else + return mp_const_none; + #endif } mp_obj_t common_hal_wifi_radio_get_ipv4_gateway_ap(wifi_radio_obj_t *self) { @@ -667,6 +678,8 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) { if (self->sta_netif == NULL || !net_if_is_up(self->sta_netif)) { return mp_const_none; } + // See get_ipv4_gateway: net_if_ip.ipv4 needs CONFIG_NET_IPV4. + #if defined(CONFIG_NET_IPV4) struct net_if_ipv4 *ipv4 = self->sta_netif->config.ip.ipv4; if (ipv4 == NULL) { return mp_const_none; @@ -678,6 +691,7 @@ mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self) { ipv4->unicast[i].netmask.s_addr); } } + #endif return mp_const_none; } From cf88c4870e2271919928530eb3e6c0e02771cca5 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Sat, 1 Aug 2026 15:35:16 -0700 Subject: [PATCH 08/10] zephyr-cp/wifi: implement radio.ap_info common_hal_wifi_radio_get_ap_info() returned mp_const_none unconditionally, with the espressif implementation left commented out beneath it. So there was no way to read the RSSI, BSSID or channel of the AP actually associated with. The only workaround was a full scan matched against the connected SSID, which costs a scan, briefly takes the radio away from the association being asked about, and cannot distinguish the connected AP from another radio broadcasting the same SSID. Zephyr already exposes this through NET_REQUEST_WIFI_IFACE_STATUS. Translate the resulting wifi_iface_status into the wifi_scan_result that wifi.Network wraps, and return None when there is nothing to report. Two details worth keeping: - Guarded on WIFI_STATE_ASSOCIATED rather than a connected flag alone. Associated is the weakest state in which BSSID and RSSI are meaningful. - status.rssi is int, scan_result.rssi is int8_t dBm. Clamped rather than truncated: a wrapped value would surface as a positive dBm, which is the same class of bug as the driver's unsigned-magnitude RSSI fixed in siwx917/fix-scan-rssi-sign. Verified on BRD2605A against the scan-based workaround it replaces: ap_info ('foreverrun', 'b0:19:21:df:d4:03', -43, 5) scan ('foreverrun', 'b0:19:21:df:d4:03', -42, 5) bssid match True | channel match True | rssi delta -1 Same BSSID and channel; the 1 dBm difference is the two samples being taken a scan apart. The BSSID is also distinct from wifi.radio.mac_address, confirming it reports the access point rather than the station. Depends on siwx917/feat-wifi-station-connect: the guard needs self->connected to be maintained, which is what that branch fixes. Co-Authored-By: Claude Opus 5 (1M context) (cherry picked from commit 098263a8eb81119ec682df54b05873fa5609c8a6) --- ports/zephyr-cp/common-hal/wifi/Radio.c | 37 +++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index a615c424259..06576d50522 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -613,8 +613,41 @@ bool common_hal_wifi_radio_get_connected(wifi_radio_obj_t *self) { } mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self) { - // if (!esp_netif_is_netif_up(self->netif)) { - return mp_const_none; + if (self->sta_netif == NULL || !self->connected) { + return mp_const_none; + } + + // NET_REQUEST_WIFI_IFACE_STATUS carries everything a wifi.Network needs, so + // this reports the live association without spending a scan on it. + struct wifi_iface_status status = { 0 }; + if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, self->sta_netif, + &status, sizeof(status)) != 0) { + return mp_const_none; + } + + // Associated is the weakest state that has a meaningful BSSID and RSSI. + if (status.state < WIFI_STATE_ASSOCIATED) { + return mp_const_none; + } + + // wifi.Network wraps a scan result, so translate the status into one. + wifi_network_obj_t *ap_info = mp_obj_malloc(wifi_network_obj_t, &wifi_network_type); + size_t ssid_len = MIN(status.ssid_len, sizeof(ap_info->scan_result.ssid) - 1); + memcpy(ap_info->scan_result.ssid, status.ssid, ssid_len); + ap_info->scan_result.ssid[ssid_len] = '\0'; + ap_info->scan_result.ssid_length = ssid_len; + memcpy(ap_info->scan_result.mac, status.bssid, WIFI_MAC_ADDR_LEN); + ap_info->scan_result.mac_length = WIFI_MAC_ADDR_LEN; + ap_info->scan_result.band = status.band; + ap_info->scan_result.channel = status.channel; + ap_info->scan_result.security = status.security; + ap_info->scan_result.wpa3_ent_type = status.wpa3_ent_type; + ap_info->scan_result.mfp = status.mfp; + // status.rssi is int, scan_result.rssi is int8_t. Clamp, since a truncated + // value would wrap to a positive dBm. + ap_info->scan_result.rssi = (int8_t)MIN(MAX(status.rssi, INT8_MIN), INT8_MAX); + return MP_OBJ_FROM_PTR(ap_info); + // } // // Make sure the interface is in STA mode From d7c3710adce9671e7edf782cbfcfd75ec4fa66d0 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Thu, 13 Aug 2026 20:29:03 -0700 Subject: [PATCH 09/10] zephyr-cp/wifi: implement radio.ping() on Zephyr's ICMP API common_hal_wifi_radio_ping() was a stub: the ESP-IDF body was commented out and it ended `return 0;`. The binding treats -1 and only -1 as failure (shared-bindings/wifi/Radio.c), so 0 was handed to Python as a successful 0 ms round trip. Callers written as `if result is None` read every failed ping as a success, including pings to unreachable addresses and pings issued while the radio was not even associated. Fixes mikeysklar/circuitpython#46. Implemented on net_icmp_init_ctx() / net_icmp_send_echo_request(). No Kconfig change is needed: there is no CONFIG_NET_ICMPV4 symbol in this Zephyr revision, ICMP gates on NET_IP/NET_IPV4, and NET_IPV4 is already set for this board. Notes on the implementation: - Returns elapsed milliseconds, and -1 for every failure path: bad context, send failure, timeout, and interruption. Never 0 except for a genuine sub-millisecond round trip. - Per-call state lives on the caller's stack and reaches the reply handler as the ICMP context's user_data, so there are no globals. This is safe in both directions: icmp.c assigns ctx->user_data before handing the packet to the stack, so a racing reply cannot see a stale pointer, and net_icmp_cleanup_ctx() takes the same lock the stack holds while dispatching handlers, so teardown cannot race a handler mid-dereference. - The handler matches on identifier and sequence and returns NET_CONTINUE on a mismatch. Without it, back-to-back pings report each other's timings. It reads the header with net_pkt_get_data(), which leaves the packet cursor alone, because NET_CONTINUE passes the packet to the next handler. - The wait polls with k_sem_take clamped to the time actually remaining, so ctrl-C stays responsive at 50 ms granularity while a sub-50 ms timeout cannot report a round trip that exceeded it. - Arrival is timestamped inside the handler rather than after the semaphore wakes, so the measurement excludes scheduling delay. - This Zephyr renamed the socket address types, so the destination is a struct net_sockaddr_in with NET_AF_INET. Code copied from older ICMP examples will not compile. Verified on BRD2605A: ping(gateway 192.168.0.1) 0.02 (float), wall 0.023 s ping(192.0.2.1, timeout=2) None, wall 2.003 s ping("not-an-address") ValueError: Only IPv4 addresses supported interleaved: 192.168.0.1 0.014 -> 1.1.1.1 0.024 192.168.0.1 0.010 -> 8.8.8.8 0.022 The interleaved run exercises the sequence guard: the local gateway stays at 10-14 ms across both visits while the two internet hosts sit at 22-24 ms, and every reported value tracks its own wall-clock measurement to within 3 ms. Costs 1,392 B of flash. Known limitation: an unroutable address and a routable but absent one are indistinguishable from Python, since both return None after the timeout. Destination-unreachable replies are not observed either, as the context is registered for NET_ICMPV4_ECHO_REPLY only. Built and tested on top of siwx917/fix-dns-zvfs-poll-max, though it does not depend on it. Radio.c is identical at both bases. Co-Authored-By: Claude Opus 5 (1M context) (cherry picked from commit 6fc0583d2ea4d2445843388e1e96b4da23e69485) --- ports/zephyr-cp/common-hal/wifi/Radio.c | 211 +++++++++++++++++++----- 1 file changed, 168 insertions(+), 43 deletions(-) diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index 06576d50522..25892747d18 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -30,8 +30,11 @@ // dns_resolve_get_default() for radio.ipv4_dns. #include #include +// net_icmp_* for radio.ping(). +#include #include #include +#include #if CIRCUITPY_MDNS #include "common-hal/mdns/Server.h" @@ -914,57 +917,179 @@ void common_hal_wifi_radio_set_ipv4_address_ap(wifi_radio_obj_t *self, mp_obj_t // common_hal_wifi_radio_start_dhcp_server(self); // restart access point DHCP } -// static void ping_success_cb(esp_ping_handle_t hdl, void *args) { -// wifi_radio_obj_t *self = (wifi_radio_obj_t *)args; -// esp_ping_get_profile(hdl, ESP_PING_PROF_TIMEGAP, &self->ping_elapsed_time, sizeof(self->ping_elapsed_time)); -// } +// Zephyr delivers the echo reply on the network RX thread, not the caller's, so +// the two halves share this state through the ICMP context's user_data. It lives +// on the calling thread's stack, hence the cleanup discipline in ping() below. +typedef struct { + struct k_sem reply_sem; + int64_t sent_ms; + int64_t elapsed_ms; + uint16_t identifier; + uint16_t sequence; +} ping_session_t; + +// Zephyr keeps struct net_icmpv4_echo_req in subsys/net/ip/icmpv4.h, a private +// header that code outside the net stack cannot include, so mirror the four +// bytes that follow the ICMP header here. +struct ping_echo_hdr { + uint16_t identifier; + uint16_t sequence; +} __packed; + +// An echo carries no port number, so the sequence is the only thing telling two +// back-to-back requests apart; without it a late reply would be reported as the +// next call's round trip. The identifier is randomized once per boot. +static uint16_t ping_identifier; +static uint16_t ping_sequence; + +static enum net_verdict ping_reply_handler(struct net_icmp_ctx *ctx, + struct net_pkt *pkt, + struct net_icmp_ip_hdr *ip_hdr, + struct net_icmp_hdr *icmp_hdr, + void *user_data) { + NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(echo_access, struct ping_echo_hdr); + ping_session_t *session = user_data; + struct ping_echo_hdr *echo; + + (void)ctx; + (void)ip_hdr; + (void)icmp_hdr; + + if (session == NULL) { + return NET_CONTINUE; + } + + // net_pkt_get_data() leaves the cursor where it found it, which the + // NET_CONTINUE below relies on: the next handler starts at the echo header. + echo = (struct ping_echo_hdr *)net_pkt_get_data(pkt, &echo_access); + if (echo == NULL) { + return NET_CONTINUE; + } + + if (net_ntohs(echo->identifier) != session->identifier || + net_ntohs(echo->sequence) != session->sequence) { + // A reply to an earlier ping of ours, or to somebody else's. Leave it + // alone rather than waking the caller with a round trip time that + // belongs to a different request. + return NET_CONTINUE; + } + + // Stamp arrival here rather than after k_sem_take() returns, so the + // measurement does not absorb the woken thread's scheduling delay. + session->elapsed_ms = k_uptime_get() - session->sent_ms; + k_sem_give(&session->reply_sem); + + return NET_OK; +} mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) { - // esp_ping_config_t ping_config = ESP_PING_DEFAULT_CONFIG(); - // ipaddress_ipaddress_to_esp_idf(ip_address, &ping_config.target_addr); - // ping_config.count = 1; - - // // We must fetch ping information using the callback mechanism, because the session storage is freed when - // // the ping session is done, even before esp_ping_delete_session(). - // esp_ping_callbacks_t ping_callbacks = { - // .on_ping_success = ping_success_cb, - // .cb_args = (void *)self, - // }; + // radio.ping() is documented to take an ipaddress.IPv4Address. + if (!mp_obj_is_type(ip_address, &ipaddress_ipv4address_type)) { + mp_raise_ValueError(MP_ERROR_TEXT("Only IPv4 addresses supported")); + } - // size_t timeout_ms = timeout * 1000; + // get_packed() takes a concrete ipaddress_ipv4address_obj_t *, so the + // MP_OBJ_TO_PTR is needed under object representations C and D. + ipaddress_ipv4address_obj_t *addr_obj = MP_OBJ_TO_PTR(ip_address); + size_t packed_len; + const char *packed = mp_obj_str_get_data( + common_hal_ipaddress_ipv4address_get_packed(addr_obj), &packed_len); + if (packed_len != sizeof(struct net_in_addr)) { + mp_raise_ValueError(MP_ERROR_TEXT("Only IPv4 addresses supported")); + } - // // ESP-IDF creates a task to do the ping session. It shuts down when done, but only after a one second delay. - // // Calling common_hal_wifi_radio_ping() too fast will cause resource exhaustion. - // esp_ping_handle_t ping; - // if (esp_ping_new_session(&ping_config, &ping_callbacks, &ping) != ESP_OK) { - // // Wait for old task to go away and then try again. - // // Empirical testing shows we have to wait at least two seconds, despite the task - // // having a one-second timeout. - // common_hal_time_delay_ms(2000); - // // Return if interrupted now, to show the interruption as KeyboardInterrupt instead of the - // // IDF error. - // if (mp_hal_is_interrupted()) { - // return (uint32_t)(-1); - // } - // CHECK_ESP_RESULT(esp_ping_new_session(&ping_config, &ping_callbacks, &ping)); - // } + // This Zephyr renamed the socket address types, so the destination is a + // struct net_sockaddr_in carrying NET_AF_INET, not a sockaddr_in/AF_INET. + struct net_sockaddr_in dst = { + .sin_family = NET_AF_INET, + }; + memcpy(&dst.sin_addr, packed, sizeof(dst.sin_addr)); + + if (ping_identifier == 0) { + // Seeded lazily. sys_rand16_get() may legitimately return 0, in which + // case we simply reseed on the next call. + ping_identifier = sys_rand16_get(); + } - // // Use all ones as a flag that the elapsed time was not set (ping failed or timed out). - // self->ping_elapsed_time = (uint32_t)(-1); + ping_session_t session = { + .identifier = ping_identifier, + .sequence = ++ping_sequence, + .elapsed_ms = -1, + }; + k_sem_init(&session.reply_sem, 0, 1); - // esp_ping_start(ping); + struct net_icmp_ctx icmp_ctx; + int res = net_icmp_init_ctx(&icmp_ctx, NET_AF_INET, NET_ICMPV4_ECHO_REPLY, 0, + ping_reply_handler); + if (res < 0) { + LOG_DBG("ping: net_icmp_init_ctx failed (%d)", res); + return -1; + } - // uint32_t start_time = common_hal_time_monotonic_ms(); - // while ((self->ping_elapsed_time == (uint32_t)(-1)) && - // (common_hal_time_monotonic_ms() - start_time < timeout_ms) && - // !mp_hal_is_interrupted()) { - // RUN_BACKGROUND_TASKS; - // } - // esp_ping_stop(ping); - // esp_ping_delete_session(ping); + struct net_icmp_ping_params params = { + .identifier = session.identifier, + .sequence = session.sequence, + .tc_tos = 0, + // A negative priority leaves the packet at the stack default and lets + // tc_tos drive the DSCP/ECN bits instead. + .priority = -1, + .data = NULL, + .data_size = 0, + }; + + session.sent_ms = k_uptime_get(); + + // A NULL sta_netif is fine; the stack picks an interface from the + // destination. This is the blocking send, as Zephyr's net shell uses: it + // waits up to a second for a buffer, so a ping can take timeout + 1s. + res = net_icmp_send_echo_request(&icmp_ctx, self->sta_netif, + (struct net_sockaddr *)&dst, ¶ms, &session); + if (res < 0) { + LOG_DBG("ping: send failed (%d)", res); + (void)net_icmp_cleanup_ctx(&icmp_ctx); + return -1; + } + + // Wait for ping_reply_handler() to signal, staying responsive to ctrl-C at + // the same 50 ms granularity as the association wait in + // common_hal_wifi_radio_connect(). + mp_float_t timeout_s = timeout <= 0 ? (mp_float_t)0.5 : timeout; + int64_t deadline = k_uptime_get() + (int64_t)(timeout_s * 1000); + bool replied = false; + while (true) { + int64_t remaining_ms = deadline - k_uptime_get(); + if (remaining_ms <= 0) { + break; + } + + // Poll in 50 ms slices so ctrl-C stays responsive, clamped to the + // caller's deadline so a reply arriving after the timeout is not + // reported as a success. MIN() is avoided here because py/misc.h and + // zephyr/sys/util.h both define it and this file includes both. + int64_t wait_ms = remaining_ms < 50 ? remaining_ms : 50; + + if (k_sem_take(&session.reply_sem, K_MSEC(wait_ms)) == 0) { + replied = true; + break; + } + if (mp_hal_is_interrupted()) { + break; + } + } + + // Unregister before returning. net_icmp_cleanup_ctx() takes the same mutex + // the stack holds while dispatching handlers, so once it returns no handler + // can still be looking at session, which lives on this stack frame. This + // has to happen on every exit path, including the error paths above. + (void)net_icmp_cleanup_ctx(&icmp_ctx); + + if (!replied || session.elapsed_ms < 0) { + return -1; + } - // return (mp_int_t)self->ping_elapsed_time; - return 0; + // shared-bindings turns exactly -1 into None and divides anything else by + // 1000, so every failure path must return -1, not 0. + return (mp_int_t)session.elapsed_ms; } void common_hal_wifi_radio_gc_collect(wifi_radio_obj_t *self) { From a1b18ef92ea93d7453af3aa312511ca57ee7541e Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Sun, 23 Aug 2026 09:45:22 -0700 Subject: [PATCH 10/10] zephyr-cp/wifi: let boards turn radio.ping() off to save flash Adds CIRCUITPY_WIFI_PING to the port Kconfig, default y, and sets it to n for nrf7002dk_nrf5340_cpuapp, which is at 99.85% of flash before this series and overflows by 516 bytes once the ICMP ping code is in. With the option off, common_hal_wifi_radio_ping() returns -1, so radio.ping() reports None the same way it does for an unreachable host. Built for nordic_nrf7002dk with the option off: links at 966388 of 966656 bytes with the Homebrew arm-none-eabi toolchain, which is about 1 KB larger than the Zephyr SDK build CI uses. --- ports/zephyr-cp/Kconfig | 9 +++++++++ ports/zephyr-cp/boards/nrf7002dk_nrf5340_cpuapp.conf | 3 +++ ports/zephyr-cp/common-hal/wifi/Radio.c | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/ports/zephyr-cp/Kconfig b/ports/zephyr-cp/Kconfig index 015864100ec..b202b53d53f 100644 --- a/ports/zephyr-cp/Kconfig +++ b/ports/zephyr-cp/Kconfig @@ -25,6 +25,15 @@ config UART_LINE_CTRL config ENTROPY_GENERATOR default y +# ===== CircuitPython feature defaults — enabled by default, boards can disable ===== + +config CIRCUITPY_WIFI_PING + bool "wifi.radio.ping() on Zephyr's ICMP API" + default y + help + Boards that are out of flash can set this to n. radio.ping() then + returns None, as it does for an unreachable host. + # ===== Bluetooth defaults ===== # Use a variable for the chosen name so the comma isn't parsed as an argument separator diff --git a/ports/zephyr-cp/boards/nrf7002dk_nrf5340_cpuapp.conf b/ports/zephyr-cp/boards/nrf7002dk_nrf5340_cpuapp.conf index 2255bd760d3..a4da934009a 100644 --- a/ports/zephyr-cp/boards/nrf7002dk_nrf5340_cpuapp.conf +++ b/ports/zephyr-cp/boards/nrf7002dk_nrf5340_cpuapp.conf @@ -8,3 +8,6 @@ CONFIG_LOG=n CONFIG_ASSERT=n CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_BT=n + +# Out of flash; radio.ping() alone overflows it. +CONFIG_CIRCUITPY_WIFI_PING=n diff --git a/ports/zephyr-cp/common-hal/wifi/Radio.c b/ports/zephyr-cp/common-hal/wifi/Radio.c index 25892747d18..af4bce1e9cf 100644 --- a/ports/zephyr-cp/common-hal/wifi/Radio.c +++ b/ports/zephyr-cp/common-hal/wifi/Radio.c @@ -30,8 +30,10 @@ // dns_resolve_get_default() for radio.ipv4_dns. #include #include +#if defined(CONFIG_CIRCUITPY_WIFI_PING) // net_icmp_* for radio.ping(). #include +#endif #include #include #include @@ -917,6 +919,7 @@ void common_hal_wifi_radio_set_ipv4_address_ap(wifi_radio_obj_t *self, mp_obj_t // common_hal_wifi_radio_start_dhcp_server(self); // restart access point DHCP } +#if defined(CONFIG_CIRCUITPY_WIFI_PING) // Zephyr delivers the echo reply on the network RX thread, not the caller's, so // the two halves share this state through the ICMP context's user_data. It lives // on the calling thread's stack, hence the cleanup discipline in ping() below. @@ -1091,6 +1094,12 @@ mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, // 1000, so every failure path must return -1, not 0. return (mp_int_t)session.elapsed_ms; } +#else +mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout) { + // Boards that turn ping off to save flash report no reply. + return -1; +} +#endif void common_hal_wifi_radio_gc_collect(wifi_radio_obj_t *self) { // Only bother to scan the actual object references.