Skip to content

Commit ed0e640

Browse files
committed
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.
1 parent 6ca0380 commit ed0e640

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

.gitmodules

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@
146146
branch = circuitpython-v5.3.1
147147
[submodule "ports/espressif/esp-protocols"]
148148
path = ports/espressif/esp-protocols
149-
url = https://github.com/espressif/esp-protocols.git
149+
url = https://github.com/adafruit/esp-protocols.git
150+
branch = circuitpython
150151
[submodule "ports/espressif/esp-camera"]
151152
path = ports/espressif/esp-camera
152153
url = https://github.com/adafruit/esp32-camera.git

ports/espressif/common-hal/mdns/Server.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ void mdns_server_construct(mdns_server_obj_t *self, bool workflow) {
3333
}
3434
_active_object = self;
3535

36-
uint8_t mac[6];
37-
esp_netif_get_mac(common_hal_wifi_radio_obj.netif, mac);
38-
snprintf(self->default_hostname, sizeof(self->default_hostname), "cpy-%02x%02x%02x", mac[3], mac[4], mac[5]);
39-
common_hal_mdns_server_set_hostname(self, self->default_hostname);
36+
// Match the netif hostname set when `import wifi` was called.
37+
esp_netif_get_hostname(common_hal_wifi_radio_obj.netif, &self->hostname);
38+
common_hal_mdns_server_set_hostname(self, self->hostname);
4039

4140
self->inited = true;
4241

ports/espressif/common-hal/mdns/Server.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ typedef struct {
1212
mp_obj_base_t base;
1313
const char *hostname;
1414
const char *instance_name;
15-
char default_hostname[sizeof("cpy-XXXXXX")];
1615
// Track if this object owns access to the underlying MDNS service.
1716
bool inited;
1817
} mdns_server_obj_t;

ports/espressif/common-hal/wifi/__init__.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,20 @@ void common_hal_wifi_init(bool user_initiated) {
199199
ESP_LOGE(TAG, "WiFi error code: %x", result);
200200
return;
201201
}
202-
// set the default lwip_local_hostname
203-
char cpy_default_hostname[strlen(CIRCUITPY_BOARD_ID) + (MAC_ADDRESS_LENGTH * 2) + 6];
202+
// Set the default lwip_local_hostname capped at 32 characters. We trim off
203+
// the start of the board name (likely manufacturer) because the end is
204+
// often more unique to the board.
205+
size_t board_len = MIN(32 - ((MAC_ADDRESS_LENGTH * 2) + 6), strlen(CIRCUITPY_BOARD_ID));
206+
size_t board_trim = strlen(CIRCUITPY_BOARD_ID) - board_len;
207+
// Avoid double _ in the hostname.
208+
if (CIRCUITPY_BOARD_ID[board_trim] == '_') {
209+
board_trim++;
210+
}
211+
212+
char cpy_default_hostname[board_len + (MAC_ADDRESS_LENGTH * 2) + 6];
204213
uint8_t mac[MAC_ADDRESS_LENGTH];
205214
esp_wifi_get_mac(ESP_IF_WIFI_STA, mac);
206-
sprintf(cpy_default_hostname, "cpy_%s_%x", CIRCUITPY_BOARD_ID, (unsigned int)mac);
215+
snprintf(cpy_default_hostname, sizeof(cpy_default_hostname), "cpy-%s-%x", CIRCUITPY_BOARD_ID + board_trim, (unsigned int)mac);
207216
const char *default_lwip_local_hostname = cpy_default_hostname;
208217
ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname));
209218
// set station mode to avoid the default SoftAP

ports/espressif/esp-protocols

Submodule esp-protocols updated 667 files

0 commit comments

Comments
 (0)