From ed0e640fb8eb00fcac455034fe3eea18d07da81b Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 8 Oct 2024 15:01:47 -0700 Subject: [PATCH 1/4] MDNS hostname match DHCP. Fix collision mangling MDNS defaults to the hostname used by DHCP/LWIP since it is now unique. This makes it the same either way. This also updates esp-protocols (used for mdns) with a patch to ensure that mangled names are the ones that collide. (circuitpython.local collides but was causing cpy- to be mangled.) Fixes #6869 again. --- .gitmodules | 3 ++- ports/espressif/common-hal/mdns/Server.c | 7 +++---- ports/espressif/common-hal/mdns/Server.h | 1 - ports/espressif/common-hal/wifi/__init__.c | 15 ++++++++++++--- ports/espressif/esp-protocols | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.gitmodules b/.gitmodules index cf0e554e26bad..c01f23dbe6e1e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -146,7 +146,8 @@ branch = circuitpython-v5.3.1 [submodule "ports/espressif/esp-protocols"] path = ports/espressif/esp-protocols - url = https://github.com/espressif/esp-protocols.git + url = https://github.com/adafruit/esp-protocols.git + branch = circuitpython [submodule "ports/espressif/esp-camera"] path = ports/espressif/esp-camera url = https://github.com/adafruit/esp32-camera.git diff --git a/ports/espressif/common-hal/mdns/Server.c b/ports/espressif/common-hal/mdns/Server.c index 81a4415cecd86..e8c34ee0885ce 100644 --- a/ports/espressif/common-hal/mdns/Server.c +++ b/ports/espressif/common-hal/mdns/Server.c @@ -33,10 +33,9 @@ void mdns_server_construct(mdns_server_obj_t *self, bool workflow) { } _active_object = self; - uint8_t mac[6]; - esp_netif_get_mac(common_hal_wifi_radio_obj.netif, mac); - snprintf(self->default_hostname, sizeof(self->default_hostname), "cpy-%02x%02x%02x", mac[3], mac[4], mac[5]); - common_hal_mdns_server_set_hostname(self, self->default_hostname); + // Match the netif hostname set when `import wifi` was called. + esp_netif_get_hostname(common_hal_wifi_radio_obj.netif, &self->hostname); + common_hal_mdns_server_set_hostname(self, self->hostname); self->inited = true; diff --git a/ports/espressif/common-hal/mdns/Server.h b/ports/espressif/common-hal/mdns/Server.h index 42ffd878d13ae..f364a539c9179 100644 --- a/ports/espressif/common-hal/mdns/Server.h +++ b/ports/espressif/common-hal/mdns/Server.h @@ -12,7 +12,6 @@ typedef struct { mp_obj_base_t base; const char *hostname; const char *instance_name; - char default_hostname[sizeof("cpy-XXXXXX")]; // Track if this object owns access to the underlying MDNS service. bool inited; } mdns_server_obj_t; diff --git a/ports/espressif/common-hal/wifi/__init__.c b/ports/espressif/common-hal/wifi/__init__.c index c0d547cc08fe4..710688b9d31c9 100644 --- a/ports/espressif/common-hal/wifi/__init__.c +++ b/ports/espressif/common-hal/wifi/__init__.c @@ -199,11 +199,20 @@ void common_hal_wifi_init(bool user_initiated) { ESP_LOGE(TAG, "WiFi error code: %x", result); return; } - // set the default lwip_local_hostname - char cpy_default_hostname[strlen(CIRCUITPY_BOARD_ID) + (MAC_ADDRESS_LENGTH * 2) + 6]; + // Set the default lwip_local_hostname capped at 32 characters. We trim off + // the start of the board name (likely manufacturer) because the end is + // often more unique to the board. + size_t board_len = MIN(32 - ((MAC_ADDRESS_LENGTH * 2) + 6), strlen(CIRCUITPY_BOARD_ID)); + size_t board_trim = strlen(CIRCUITPY_BOARD_ID) - board_len; + // Avoid double _ in the hostname. + if (CIRCUITPY_BOARD_ID[board_trim] == '_') { + board_trim++; + } + + char cpy_default_hostname[board_len + (MAC_ADDRESS_LENGTH * 2) + 6]; uint8_t mac[MAC_ADDRESS_LENGTH]; esp_wifi_get_mac(ESP_IF_WIFI_STA, mac); - sprintf(cpy_default_hostname, "cpy_%s_%x", CIRCUITPY_BOARD_ID, (unsigned int)mac); + snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%x", CIRCUITPY_BOARD_ID + board_trim, (unsigned int)mac); const char *default_lwip_local_hostname = cpy_default_hostname; ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname)); // set station mode to avoid the default SoftAP diff --git a/ports/espressif/esp-protocols b/ports/espressif/esp-protocols index ea54eef0d0fe5..2f492c0228901 160000 --- a/ports/espressif/esp-protocols +++ b/ports/espressif/esp-protocols @@ -1 +1 @@ -Subproject commit ea54eef0d0fe59bd53a49c916f87065518b957eb +Subproject commit 2f492c02289015ecbb36851dd1bd04e0eb0a9937 From 390c280d2b28c3ab3bb12437c98be59562dd8a86 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 8 Oct 2024 15:49:19 -0700 Subject: [PATCH 2/4] Turn off keypad demux on ESP32-C3 --- ports/espressif/mpconfigport.mk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 4bc24a720ca58..e80c9654040be 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -136,6 +136,9 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0 CIRCUITPY_USB_DEVICE = 0 CIRCUITPY_ESP_USB_SERIAL_JTAG ?= 1 +# No room in flash. +CIRCUITPY_KEYPAD_DEMUX = 0 + else ifeq ($(IDF_TARGET),esp32c6) # Modules CIRCUITPY_ESPCAMERA = 0 From 1543642dc2721f1804504ffe2a9024ef990917c2 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 9 Oct 2024 14:22:16 -0700 Subject: [PATCH 3/4] Fix mac address formatting and disable aesio for ESP --- ports/espressif/common-hal/wifi/__init__.c | 3 ++- ports/espressif/mpconfigport.mk | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/espressif/common-hal/wifi/__init__.c b/ports/espressif/common-hal/wifi/__init__.c index 710688b9d31c9..1a4c014bfbc5b 100644 --- a/ports/espressif/common-hal/wifi/__init__.c +++ b/ports/espressif/common-hal/wifi/__init__.c @@ -212,7 +212,8 @@ void common_hal_wifi_init(bool user_initiated) { char cpy_default_hostname[board_len + (MAC_ADDRESS_LENGTH * 2) + 6]; uint8_t mac[MAC_ADDRESS_LENGTH]; esp_wifi_get_mac(ESP_IF_WIFI_STA, mac); - snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%x", CIRCUITPY_BOARD_ID + board_trim, (unsigned int)mac); + snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%02x%02x%02x%02x%02x%02x", CIRCUITPY_BOARD_ID + board_trim, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + const char *default_lwip_local_hostname = cpy_default_hostname; ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname)); // set station mode to avoid the default SoftAP diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index e80c9654040be..ffca0a0574368 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -58,6 +58,9 @@ CIRCUITPY_WATCHDOG ?= 1 CIRCUITPY_WIFI ?= 1 CIRCUITPY_SOCKETPOOL_IPV6 ?= 1 +# Turn off aesio. It is a custom API that no one uses. +CIRCUITPY_AESIO = 0 + # Enable _eve module CIRCUITPY__EVE ?= 1 From fb0ff7e5bdb4725bdb3363cc6d8a5f148e504bad Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 10 Oct 2024 10:22:15 -0700 Subject: [PATCH 4/4] Only disable aesio on C3 --- ports/espressif/mpconfigport.mk | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index ffca0a0574368..7f4b7600e364f 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -58,9 +58,6 @@ CIRCUITPY_WATCHDOG ?= 1 CIRCUITPY_WIFI ?= 1 CIRCUITPY_SOCKETPOOL_IPV6 ?= 1 -# Turn off aesio. It is a custom API that no one uses. -CIRCUITPY_AESIO = 0 - # Enable _eve module CIRCUITPY__EVE ?= 1 @@ -140,6 +137,7 @@ CIRCUITPY_USB_DEVICE = 0 CIRCUITPY_ESP_USB_SERIAL_JTAG ?= 1 # No room in flash. +CIRCUITPY_AESIO = 0 CIRCUITPY_KEYPAD_DEMUX = 0 else ifeq ($(IDF_TARGET),esp32c6)