Skip to content

Commit

Permalink
Merge pull request #9699 from tannewt/esp_mdns
Browse files Browse the repository at this point in the history
MDNS hostname match DHCP. Fix collision mangling
  • Loading branch information
dhalbert authored Oct 10, 2024
2 parents 0509ed2 + fb0ff7e commit 4563239
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions ports/espressif/common-hal/mdns/Server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 0 additions & 1 deletion ports/espressif/common-hal/mdns/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 13 additions & 3 deletions ports/espressif/common-hal/wifi/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,21 @@ 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-%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
Expand Down
2 changes: 1 addition & 1 deletion ports/espressif/esp-protocols
Submodule esp-protocols updated 667 files
4 changes: 4 additions & 0 deletions ports/espressif/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0
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)
# Modules
CIRCUITPY_ESPCAMERA = 0
Expand Down

0 comments on commit 4563239

Please sign in to comment.