Skip to content

Commit 4563239

Browse files
authored
Merge pull request #9699 from tannewt/esp_mdns
MDNS hostname match DHCP. Fix collision mangling
2 parents 0509ed2 + fb0ff7e commit 4563239

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

.gitmodules

+2-1
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

+3-4
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

-1
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

+13-3
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,21 @@ 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-%02x%02x%02x%02x%02x%02x", CIRCUITPY_BOARD_ID + board_trim, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
216+
207217
const char *default_lwip_local_hostname = cpy_default_hostname;
208218
ESP_ERROR_CHECK(esp_netif_set_hostname(self->netif, default_lwip_local_hostname));
209219
// set station mode to avoid the default SoftAP

ports/espressif/esp-protocols

Submodule esp-protocols updated 667 files

ports/espressif/mpconfigport.mk

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0
136136
CIRCUITPY_USB_DEVICE = 0
137137
CIRCUITPY_ESP_USB_SERIAL_JTAG ?= 1
138138

139+
# No room in flash.
140+
CIRCUITPY_AESIO = 0
141+
CIRCUITPY_KEYPAD_DEMUX = 0
142+
139143
else ifeq ($(IDF_TARGET),esp32c6)
140144
# Modules
141145
CIRCUITPY_ESPCAMERA = 0

0 commit comments

Comments
 (0)