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.
This commit is contained in:
Scott Shawcroft 2024-10-08 15:01:47 -07:00
parent 6ca0380d26
commit ed0e640fb8
No known key found for this signature in database
5 changed files with 18 additions and 10 deletions

3
.gitmodules vendored
View file

@ -146,7 +146,8 @@
branch = circuitpython-v5.3.1 branch = circuitpython-v5.3.1
[submodule "ports/espressif/esp-protocols"] [submodule "ports/espressif/esp-protocols"]
path = 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"] [submodule "ports/espressif/esp-camera"]
path = ports/espressif/esp-camera path = ports/espressif/esp-camera
url = https://github.com/adafruit/esp32-camera.git url = https://github.com/adafruit/esp32-camera.git

View file

@ -33,10 +33,9 @@ void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
} }
_active_object = self; _active_object = self;
uint8_t mac[6]; // Match the netif hostname set when `import wifi` was called.
esp_netif_get_mac(common_hal_wifi_radio_obj.netif, mac); esp_netif_get_hostname(common_hal_wifi_radio_obj.netif, &self->hostname);
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->hostname);
common_hal_mdns_server_set_hostname(self, self->default_hostname);
self->inited = true; self->inited = true;

View file

@ -12,7 +12,6 @@ typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
const char *hostname; const char *hostname;
const char *instance_name; const char *instance_name;
char default_hostname[sizeof("cpy-XXXXXX")];
// Track if this object owns access to the underlying MDNS service. // Track if this object owns access to the underlying MDNS service.
bool inited; bool inited;
} mdns_server_obj_t; } mdns_server_obj_t;

View file

@ -199,11 +199,20 @@ void common_hal_wifi_init(bool user_initiated) {
ESP_LOGE(TAG, "WiFi error code: %x", result); ESP_LOGE(TAG, "WiFi error code: %x", result);
return; return;
} }
// set the default lwip_local_hostname // Set the default lwip_local_hostname capped at 32 characters. We trim off
char cpy_default_hostname[strlen(CIRCUITPY_BOARD_ID) + (MAC_ADDRESS_LENGTH * 2) + 6]; // 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]; uint8_t mac[MAC_ADDRESS_LENGTH];
esp_wifi_get_mac(ESP_IF_WIFI_STA, mac); 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; const char *default_lwip_local_hostname = cpy_default_hostname;
ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname)); ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname));
// set station mode to avoid the default SoftAP // set station mode to avoid the default SoftAP

@ -1 +1 @@
Subproject commit ea54eef0d0fe59bd53a49c916f87065518b957eb Subproject commit 2f492c02289015ecbb36851dd1bd04e0eb0a9937