diff --git a/Firmware/idf_component.yml b/Firmware/idf_component.yml index 67cbc771..59314c81 100644 --- a/Firmware/idf_component.yml +++ b/Firmware/idf_component.yml @@ -84,5 +84,6 @@ dependencies: version: "1.1.4" rules: - if: "target in [esp32s3, esp32p4]" + espressif/mdns: "1.3.2" idf: '5.5.2' diff --git a/Platforms/platform-esp32/CMakeLists.txt b/Platforms/platform-esp32/CMakeLists.txt index 4543232e..fc6b66bf 100644 --- a/Platforms/platform-esp32/CMakeLists.txt +++ b/Platforms/platform-esp32/CMakeLists.txt @@ -6,7 +6,7 @@ idf_component_register( SRCS ${SOURCES} INCLUDE_DIRS "include/" PRIV_INCLUDE_DIRS "private/" - REQUIRES TactilityKernel driver esp_adc esp_driver_i2c esp_lcd vfs fatfs esp_wifi esp_netif esp_event + REQUIRES TactilityKernel driver esp_adc esp_driver_i2c esp_lcd vfs fatfs esp_wifi esp_netif esp_event mdns ) if (DEFINED ENV{ESP_IDF_VERSION}) diff --git a/Platforms/platform-esp32/source/drivers/esp32_wifi.cpp b/Platforms/platform-esp32/source/drivers/esp32_wifi.cpp index ccecbb59..2b41abb5 100644 --- a/Platforms/platform-esp32/source/drivers/esp32_wifi.cpp +++ b/Platforms/platform-esp32/source/drivers/esp32_wifi.cpp @@ -5,9 +5,11 @@ #if defined(CONFIG_SOC_WIFI_SUPPORTED) || defined(CONFIG_SLAVE_SOC_WIFI_SUPPORTED) #include +#include #include #include #include +#include #include #include @@ -151,6 +153,31 @@ error_t bring_up_wifi(Esp32WifiCtx* ctx) { return ERROR_RESOURCE; } + // mDNS + hostname: kidsOS-XXXX from MAC, for Bonjour discovery + { + uint8_t mac[6]; + char hostname[32]; + if (esp_wifi_get_mac(WIFI_IF_STA, mac) == ESP_OK) { + snprintf(hostname, sizeof(hostname), "kidsOS-%02X%02X", mac[4], mac[5]); + } else { + strncpy(hostname, "kidsOS", sizeof(hostname)); + } + LOG_I(TAG, "Setting DHCP hostname to '%s'", hostname); + esp_netif_set_hostname(ctx->netif, hostname); + + esp_err_t mdns_err = mdns_init(); + if (mdns_err == ESP_OK) { + LOG_I(TAG, "mDNS initialized, hostname '%s.local'", hostname); + mdns_hostname_set(hostname); + mdns_instance_name_set("kidsOS Tactility Device"); + mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0); + mdns_service_add(NULL, "_tactility", "_tcp", 6666, NULL, 0); + } else { + LOG_W(TAG, "mDNS init failed: %s", esp_err_to_name(mdns_err)); + } + } + + // Warning: this is the memory-intensive operation. It uses over 100kB of // RAM with default settings. wifi_init_config_t init_config = WIFI_INIT_CONFIG_DEFAULT(); diff --git a/Tactility/CMakeLists.txt b/Tactility/CMakeLists.txt index 43ff0f0e..03da4335 100644 --- a/Tactility/CMakeLists.txt +++ b/Tactility/CMakeLists.txt @@ -32,6 +32,7 @@ if (DEFINED ENV{ESP_IDF_VERSION}) vfs fatfs lwip + mdns spi_flash ) diff --git a/Tactility/Source/service/webserver/WebServerService.cpp b/Tactility/Source/service/webserver/WebServerService.cpp index 69e62595..19b10580 100644 --- a/Tactility/Source/service/webserver/WebServerService.cpp +++ b/Tactility/Source/service/webserver/WebServerService.cpp @@ -46,6 +46,8 @@ #include #include #include +#include +#include #include #include #include @@ -392,6 +394,27 @@ bool WebServerService::startApMode() { } LOG_I(TAG, "WiFi AP started - SSID: '%s', Channel: %u, IP: 192.168.4.1", settings.apSsid.c_str(), (unsigned)settings.apChannel); + + // mDNS for AP mode too - kidsOS-XXXX.local + { + uint8_t mac[6]; + char hostname[32]; + if (esp_wifi_get_mac(WIFI_IF_AP, mac) == ESP_OK) { + snprintf(hostname, sizeof(hostname), "kidsOS-%02X%02X", mac[4], mac[5]); + } else { + strncpy(hostname, "kidsOS", sizeof(hostname)); + } + esp_netif_set_hostname(apNetif, hostname); + esp_err_t mdns_err = mdns_init(); + if (mdns_err == ESP_OK) { + LOG_I(TAG, "mDNS AP initialized, hostname '%s.local'", hostname); + mdns_hostname_set(hostname); + mdns_instance_name_set("kidsOS Tactility Device"); + mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0); + mdns_service_add(NULL, "_tactility", "_tcp", 6666, NULL, 0); + } + } + return true; }