From 63a94df74fb0ffb164f675fa254414c449650811 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 15:47:19 -0500 Subject: [PATCH 1/9] tidy --- esphome/components/mdns/mdns_esp32.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/components/mdns/mdns_esp32.cpp b/esphome/components/mdns/mdns_esp32.cpp index 53391382ef..2d05b69f28 100644 --- a/esphome/components/mdns/mdns_esp32.cpp +++ b/esphome/components/mdns/mdns_esp32.cpp @@ -40,7 +40,9 @@ void MDNSComponent::setup() { // free records for (const auto &it : txt_records) { - free((void *) it.value); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-cstyle-cast) + free( + (void *) it + .value); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-no-malloc) } if (err != ESP_OK) { From b49f60569e3375f0ef34f13a0875b32793b149ce Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 15:50:51 -0500 Subject: [PATCH 2/9] tidy --- esphome/components/mdns/mdns_esp32.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/esphome/components/mdns/mdns_esp32.cpp b/esphome/components/mdns/mdns_esp32.cpp index 2d05b69f28..38d52139a8 100644 --- a/esphome/components/mdns/mdns_esp32.cpp +++ b/esphome/components/mdns/mdns_esp32.cpp @@ -40,9 +40,7 @@ void MDNSComponent::setup() { // free records for (const auto &it : txt_records) { - free( - (void *) it - .value); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-no-malloc) + free((void *) it.value); // NOLINT(cppcoreguidelines-no-malloc) } if (err != ESP_OK) { From 1a6aaedbb76371d9bf620966604d77882b03f6e2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 16:16:36 -0500 Subject: [PATCH 3/9] preen --- esphome/components/mdns/mdns_component.cpp | 69 ++++++++++++++-------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index 11c1d1fa67..33cd50174a 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -45,7 +45,24 @@ static const char *const TAG = "mdns"; #define USE_WEBSERVER_PORT 80 // NOLINT #endif -// Define constant strings for values (PROGMEM on ESP8266, static pointers on others) +// Define all constant strings using the macro +MDNS_STATIC_CONST_CHAR(SERVICE_ESPHOMELIB, "_esphomelib"); +MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp"); +MDNS_STATIC_CONST_CHAR(SERVICE_PROMETHEUS, "_prometheus-http"); +MDNS_STATIC_CONST_CHAR(SERVICE_HTTP, "_http"); + +MDNS_STATIC_CONST_CHAR(TXT_FRIENDLY_NAME, "friendly_name"); +MDNS_STATIC_CONST_CHAR(TXT_VERSION, "version"); +MDNS_STATIC_CONST_CHAR(TXT_MAC, "mac"); +MDNS_STATIC_CONST_CHAR(TXT_PLATFORM, "platform"); +MDNS_STATIC_CONST_CHAR(TXT_BOARD, "board"); +MDNS_STATIC_CONST_CHAR(TXT_NETWORK, "network"); +MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION, "api_encryption"); +MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION_SUPPORTED, "api_encryption_supported"); +MDNS_STATIC_CONST_CHAR(TXT_PROJECT_NAME, "project_name"); +MDNS_STATIC_CONST_CHAR(TXT_PROJECT_VERSION, "project_version"); +MDNS_STATIC_CONST_CHAR(TXT_PACKAGE_IMPORT_URL, "package_import_url"); + MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266"); MDNS_STATIC_CONST_CHAR(PLATFORM_ESP32, "ESP32"); MDNS_STATIC_CONST_CHAR(PLATFORM_RP2040, "RP2040"); @@ -63,8 +80,8 @@ void MDNSComponent::compile_records_() { #ifdef USE_API if (api::global_api_server != nullptr) { auto &service = this->services_.emplace_next(); - service.service_type = "_esphomelib"; - service.proto = "_tcp"; + service.service_type = MDNS_STR(SERVICE_ESPHOMELIB); + service.proto = MDNS_STR(SERVICE_TCP); service.port = api::global_api_server->get_port(); const std::string &friendly_name = App.get_friendly_name(); @@ -95,62 +112,62 @@ void MDNSComponent::compile_records_() { txt_records.reserve(txt_count); if (!friendly_name_empty) { - txt_records.push_back({"friendly_name", friendly_name}); + txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), friendly_name}); } - txt_records.push_back({"version", ESPHOME_VERSION}); - txt_records.push_back({"mac", get_mac_address()}); + txt_records.push_back({MDNS_STR(TXT_VERSION), ESPHOME_VERSION}); + txt_records.push_back({MDNS_STR(TXT_MAC), get_mac_address()}); #ifdef USE_ESP8266 - txt_records.push_back({"platform", MDNS_STR(PLATFORM_ESP8266)}); + txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP8266)}); #elif defined(USE_ESP32) - txt_records.push_back({"platform", MDNS_STR(PLATFORM_ESP32)}); + txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP32)}); #elif defined(USE_RP2040) - txt_records.push_back({"platform", MDNS_STR(PLATFORM_RP2040)}); + txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_RP2040)}); #elif defined(USE_LIBRETINY) - txt_records.emplace_back(MDNSTXTRecord{"platform", lt_cpu_get_model_name()}); + txt_records.push_back({MDNS_STR(TXT_PLATFORM), lt_cpu_get_model_name()}); #endif - txt_records.push_back({"board", ESPHOME_BOARD}); + txt_records.push_back({MDNS_STR(TXT_BOARD), ESPHOME_BOARD}); #if defined(USE_WIFI) - txt_records.push_back({"network", MDNS_STR(NETWORK_WIFI)}); + txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_WIFI)}); #elif defined(USE_ETHERNET) - txt_records.push_back({"network", MDNS_STR(NETWORK_ETHERNET)}); + txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_ETHERNET)}); #elif defined(USE_OPENTHREAD) - txt_records.push_back({"network", MDNS_STR(NETWORK_THREAD)}); + txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_THREAD)}); #endif #ifdef USE_API_NOISE MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256"); if (api::global_api_server->get_noise_ctx()->has_psk()) { - txt_records.push_back({"api_encryption", MDNS_STR(NOISE_ENCRYPTION)}); + txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION), MDNS_STR(NOISE_ENCRYPTION)}); } else { - txt_records.push_back({"api_encryption_supported", MDNS_STR(NOISE_ENCRYPTION)}); + txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION_SUPPORTED), MDNS_STR(NOISE_ENCRYPTION)}); } #endif #ifdef ESPHOME_PROJECT_NAME - txt_records.push_back({"project_name", ESPHOME_PROJECT_NAME}); - txt_records.push_back({"project_version", ESPHOME_PROJECT_VERSION}); + txt_records.push_back({MDNS_STR(TXT_PROJECT_NAME), ESPHOME_PROJECT_NAME}); + txt_records.push_back({MDNS_STR(TXT_PROJECT_VERSION), ESPHOME_PROJECT_VERSION}); #endif // ESPHOME_PROJECT_NAME #ifdef USE_DASHBOARD_IMPORT - txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()}); + txt_records.push_back({MDNS_STR(TXT_PACKAGE_IMPORT_URL), dashboard_import::get_package_import_url()}); #endif } #endif // USE_API #ifdef USE_PROMETHEUS auto &prom_service = this->services_.emplace_next(); - prom_service.service_type = "_prometheus-http"; - prom_service.proto = "_tcp"; + prom_service.service_type = MDNS_STR(SERVICE_PROMETHEUS); + prom_service.proto = MDNS_STR(SERVICE_TCP); prom_service.port = USE_WEBSERVER_PORT; #endif #ifdef USE_WEBSERVER auto &web_service = this->services_.emplace_next(); - web_service.service_type = "_http"; - web_service.proto = "_tcp"; + web_service.service_type = MDNS_STR(SERVICE_HTTP); + web_service.proto = MDNS_STR(SERVICE_TCP); web_service.port = USE_WEBSERVER_PORT; #endif @@ -158,10 +175,10 @@ void MDNSComponent::compile_records_() { // Publish "http" service if not using native API or any other services // This is just to have *some* mDNS service so that .local resolution works auto &fallback_service = this->services_.emplace_next(); - fallback_service.service_type = "_http"; - fallback_service.proto = "_tcp"; + fallback_service.service_type = MDNS_STR(SERVICE_HTTP); + fallback_service.proto = MDNS_STR(SERVICE_TCP); fallback_service.port = USE_WEBSERVER_PORT; - fallback_service.txt_records.emplace_back(MDNSTXTRecord{"version", ESPHOME_VERSION}); + fallback_service.txt_records.push_back({MDNS_STR(TXT_VERSION), ESPHOME_VERSION}); #endif } From 87a1040285887182a3f3fa94cd189f1ef8c0d80b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 16:29:10 -0500 Subject: [PATCH 4/9] keep all 8266 in flash --- esphome/components/mdns/mdns_component.cpp | 40 +++++++++----------- esphome/components/mdns/mdns_component.h | 16 ++++++-- esphome/components/mdns/mdns_esp32.cpp | 5 ++- esphome/components/mdns/mdns_esp8266.cpp | 6 +-- esphome/components/mdns/mdns_libretiny.cpp | 6 +-- esphome/components/mdns/mdns_rp2040.cpp | 6 +-- esphome/components/openthread/openthread.cpp | 4 +- 7 files changed, 45 insertions(+), 38 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index 33cd50174a..5d7d2caf7e 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -9,24 +9,20 @@ #include // Macro to define strings in PROGMEM on ESP8266, regular memory on other platforms #define MDNS_STATIC_CONST_CHAR(name, value) static const char name[] PROGMEM = value -// Helper to get string from PROGMEM - returns a temporary std::string -// Only define this function if we have services that will use it -#if defined(USE_API) || defined(USE_PROMETHEUS) || defined(USE_WEBSERVER) || defined(USE_MDNS_EXTRA_SERVICES) -static std::string mdns_string_p(const char *src) { +#define MDNS_STR(name) (reinterpret_cast(name)) +// Helper to convert PROGMEM string to std::string for TemplatableValue +static std::string mdns_str_value(PGM_P str) { char buf[64]; - strncpy_P(buf, src, sizeof(buf) - 1); + strncpy_P(buf, str, sizeof(buf) - 1); buf[sizeof(buf) - 1] = '\0'; return std::string(buf); } -#define MDNS_STR(name) mdns_string_p(name) -#else -// If no services are configured, we still need the fallback service but it uses string literals -#define MDNS_STR(name) std::string(name) -#endif +#define MDNS_STR_VALUE(name) mdns_str_value(name) #else // On non-ESP8266 platforms, use regular const char* -#define MDNS_STATIC_CONST_CHAR(name, value) static constexpr const char *name = value -#define MDNS_STR(name) name +#define MDNS_STATIC_CONST_CHAR(name, value) static constexpr const char name[] = value +#define MDNS_STR(name) (reinterpret_cast(name)) +#define MDNS_STR_VALUE(name) std::string(name) #endif #ifdef USE_API @@ -118,11 +114,11 @@ void MDNSComponent::compile_records_() { txt_records.push_back({MDNS_STR(TXT_MAC), get_mac_address()}); #ifdef USE_ESP8266 - txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP8266)}); + txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR_VALUE(PLATFORM_ESP8266)}); #elif defined(USE_ESP32) - txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP32)}); + txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR_VALUE(PLATFORM_ESP32)}); #elif defined(USE_RP2040) - txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_RP2040)}); + txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR_VALUE(PLATFORM_RP2040)}); #elif defined(USE_LIBRETINY) txt_records.push_back({MDNS_STR(TXT_PLATFORM), lt_cpu_get_model_name()}); #endif @@ -130,19 +126,19 @@ void MDNSComponent::compile_records_() { txt_records.push_back({MDNS_STR(TXT_BOARD), ESPHOME_BOARD}); #if defined(USE_WIFI) - txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_WIFI)}); + txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR_VALUE(NETWORK_WIFI)}); #elif defined(USE_ETHERNET) - txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_ETHERNET)}); + txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR_VALUE(NETWORK_ETHERNET)}); #elif defined(USE_OPENTHREAD) - txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_THREAD)}); + txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR_VALUE(NETWORK_THREAD)}); #endif #ifdef USE_API_NOISE MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256"); if (api::global_api_server->get_noise_ctx()->has_psk()) { - txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION), MDNS_STR(NOISE_ENCRYPTION)}); + txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION), MDNS_STR_VALUE(NOISE_ENCRYPTION)}); } else { - txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION_SUPPORTED), MDNS_STR(NOISE_ENCRYPTION)}); + txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION_SUPPORTED), MDNS_STR_VALUE(NOISE_ENCRYPTION)}); } #endif @@ -190,10 +186,10 @@ void MDNSComponent::dump_config() { #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE ESP_LOGV(TAG, " Services:"); for (const auto &service : this->services_) { - ESP_LOGV(TAG, " - %s, %s, %d", service.service_type, service.proto, + ESP_LOGV(TAG, " - %s, %s, %d", MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto), const_cast &>(service.port).value()); for (const auto &record : service.txt_records) { - ESP_LOGV(TAG, " TXT: %s = %s", record.key, + ESP_LOGV(TAG, " TXT: %s = %s", MDNS_STR_ARG(record.key), const_cast &>(record.value).value().c_str()); } } diff --git a/esphome/components/mdns/mdns_component.h b/esphome/components/mdns/mdns_component.h index 176240b828..ef366cd31a 100644 --- a/esphome/components/mdns/mdns_component.h +++ b/esphome/components/mdns/mdns_component.h @@ -9,21 +9,31 @@ namespace esphome { namespace mdns { +// Helper struct that identifies strings that may be stored in flash storage (similar to LogString) +struct MDNSString; + +#ifdef USE_ESP8266 +#include +#define MDNS_STR_ARG(s) ((PGM_P) (s)) +#else +#define MDNS_STR_ARG(s) (reinterpret_cast(s)) +#endif + // Service count is calculated at compile time by Python codegen // MDNS_SERVICE_COUNT will always be defined struct MDNSTXTRecord { - const char *key; + const MDNSString *key; TemplatableValue value; }; struct MDNSService { // service name _including_ underscore character prefix // as defined in RFC6763 Section 7 - const char *service_type; + const MDNSString *service_type; // second label indicating protocol _including_ underscore character prefix // as defined in RFC6763 Section 7, like "_tcp" or "_udp" - const char *proto; + const MDNSString *proto; TemplatableValue port; std::vector txt_records; }; diff --git a/esphome/components/mdns/mdns_esp32.cpp b/esphome/components/mdns/mdns_esp32.cpp index 38d52139a8..a471051401 100644 --- a/esphome/components/mdns/mdns_esp32.cpp +++ b/esphome/components/mdns/mdns_esp32.cpp @@ -30,13 +30,14 @@ void MDNSComponent::setup() { for (const auto &record : service.txt_records) { mdns_txt_item_t it{}; // key is a compile-time string literal in flash, no need to strdup - it.key = record.key; + it.key = MDNS_STR_ARG(record.key); // value is a temporary from TemplatableValue, must strdup to keep it alive it.value = strdup(const_cast &>(record.value).value().c_str()); txt_records.push_back(it); } uint16_t port = const_cast &>(service.port).value(); - err = mdns_service_add(nullptr, service.service_type, service.proto, port, txt_records.data(), txt_records.size()); + err = mdns_service_add(nullptr, MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto), port, + txt_records.data(), txt_records.size()); // free records for (const auto &it : txt_records) { diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index d348088444..6f0e50c1e2 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -21,18 +21,18 @@ void MDNSComponent::setup() { // part of the wire protocol to have an underscore, and for example ESP-IDF // expects the underscore to be there, the ESP8266 implementation always adds // the underscore itself. - auto *proto = service.proto; + auto *proto = MDNS_STR_ARG(service.proto); while (*proto == '_') { proto++; } - auto *service_type = service.service_type; + auto *service_type = MDNS_STR_ARG(service.service_type); while (*service_type == '_') { service_type++; } uint16_t port = const_cast &>(service.port).value(); MDNS.addService(service_type, proto, port); for (const auto &record : service.txt_records) { - MDNS.addServiceTxt(service_type, proto, record.key, + MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), const_cast &>(record.value).value().c_str()); } } diff --git a/esphome/components/mdns/mdns_libretiny.cpp b/esphome/components/mdns/mdns_libretiny.cpp index 6edf9b329b..9010ca2bc6 100644 --- a/esphome/components/mdns/mdns_libretiny.cpp +++ b/esphome/components/mdns/mdns_libretiny.cpp @@ -21,18 +21,18 @@ void MDNSComponent::setup() { // part of the wire protocol to have an underscore, and for example ESP-IDF // expects the underscore to be there, the ESP8266 implementation always adds // the underscore itself. - auto *proto = service.proto; + auto *proto = MDNS_STR_ARG(service.proto); while (*proto == '_') { proto++; } - auto *service_type = service.service_type; + auto *service_type = MDNS_STR_ARG(service.service_type); while (*service_type == '_') { service_type++; } uint16_t port_ = const_cast &>(service.port).value(); MDNS.addService(service_type, proto, port_); for (const auto &record : service.txt_records) { - MDNS.addServiceTxt(service_type, proto, record.key, + MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), const_cast &>(record.value).value().c_str()); } } diff --git a/esphome/components/mdns/mdns_rp2040.cpp b/esphome/components/mdns/mdns_rp2040.cpp index 92d35e7e34..039453f501 100644 --- a/esphome/components/mdns/mdns_rp2040.cpp +++ b/esphome/components/mdns/mdns_rp2040.cpp @@ -21,18 +21,18 @@ void MDNSComponent::setup() { // part of the wire protocol to have an underscore, and for example ESP-IDF // expects the underscore to be there, the ESP8266 implementation always adds // the underscore itself. - auto *proto = service.proto; + auto *proto = MDNS_STR_ARG(service.proto); while (*proto == '_') { proto++; } - auto *service_type = service.service_type; + auto *service_type = MDNS_STR_ARG(service.service_type); while (*service_type == '_') { service_type++; } uint16_t port = const_cast &>(service.port).value(); MDNS.addService(service_type, proto, port); for (const auto &record : service.txt_records) { - MDNS.addServiceTxt(service_type, proto, record.key, + MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), const_cast &>(record.value).value().c_str()); } } diff --git a/esphome/components/openthread/openthread.cpp b/esphome/components/openthread/openthread.cpp index d1fe677a93..bc5dcadef6 100644 --- a/esphome/components/openthread/openthread.cpp +++ b/esphome/components/openthread/openthread.cpp @@ -155,7 +155,7 @@ void OpenThreadSrpComponent::setup() { // Set service name char *string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size); - std::string full_service = service.service_type + "." + service.proto; + std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) + "." + MDNS_STR_ARG(service.proto); if (full_service.size() > size) { ESP_LOGW(TAG, "Service name too long: %s", full_service.c_str()); continue; @@ -181,7 +181,7 @@ void OpenThreadSrpComponent::setup() { for (size_t i = 0; i < service.txt_records.size(); i++) { const auto &txt = service.txt_records[i]; auto value = const_cast &>(txt.value).value(); - txt_entries[i].mKey = txt.key; // Compile-time string literal in flash + txt_entries[i].mKey = MDNS_STR_ARG(txt.key); txt_entries[i].mValue = reinterpret_cast(strdup(value.c_str())); txt_entries[i].mValueLength = value.size(); } From 2e1d5662ea08cd4461e490a2f0ffd96a73dad097 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 16:34:51 -0500 Subject: [PATCH 5/9] tidy --- esphome/components/mdns/mdns_component.cpp | 3 +++ esphome/components/mdns/mdns_esp8266.cpp | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index 5d7d2caf7e..5dfaa7fd4b 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -11,6 +11,8 @@ #define MDNS_STATIC_CONST_CHAR(name, value) static const char name[] PROGMEM = value #define MDNS_STR(name) (reinterpret_cast(name)) // Helper to convert PROGMEM string to std::string for TemplatableValue +// Only define this function if we have services that will use it +#if defined(USE_API) || defined(USE_PROMETHEUS) || defined(USE_WEBSERVER) || defined(USE_MDNS_EXTRA_SERVICES) static std::string mdns_str_value(PGM_P str) { char buf[64]; strncpy_P(buf, str, sizeof(buf) - 1); @@ -18,6 +20,7 @@ static std::string mdns_str_value(PGM_P str) { return std::string(buf); } #define MDNS_STR_VALUE(name) mdns_str_value(name) +#endif #else // On non-ESP8266 platforms, use regular const char* #define MDNS_STATIC_CONST_CHAR(name, value) static constexpr const char name[] = value diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index 6f0e50c1e2..fbf78a68fc 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -22,11 +22,11 @@ void MDNSComponent::setup() { // expects the underscore to be there, the ESP8266 implementation always adds // the underscore itself. auto *proto = MDNS_STR_ARG(service.proto); - while (*proto == '_') { + while (pgm_read_byte(proto) == '_') { proto++; } auto *service_type = MDNS_STR_ARG(service.service_type); - while (*service_type == '_') { + while (pgm_read_byte(service_type) == '_') { service_type++; } uint16_t port = const_cast &>(service.port).value(); From 95ecacc5f7f7d3f714f0a5ccc80884e8812a8402 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 16:39:40 -0500 Subject: [PATCH 6/9] tidy --- esphome/components/mdns/mdns_esp8266.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index fbf78a68fc..f0d11ca3dc 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -30,9 +30,9 @@ void MDNSComponent::setup() { service_type++; } uint16_t port = const_cast &>(service.port).value(); - MDNS.addService(service_type, proto, port); + MDNS.addService(FPSTR(service_type), FPSTR(proto), port); for (const auto &record : service.txt_records) { - MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), + MDNS.addServiceTxt(FPSTR(service_type), FPSTR(proto), FPSTR(MDNS_STR_ARG(record.key)), const_cast &>(record.value).value().c_str()); } } From 57bd6ec68c2a247851ad46d19c4af8dd53256287 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 16:46:26 -0500 Subject: [PATCH 7/9] tidy --- esphome/components/mdns/mdns_esp8266.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index f0d11ca3dc..88b4c89505 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -22,11 +22,11 @@ void MDNSComponent::setup() { // expects the underscore to be there, the ESP8266 implementation always adds // the underscore itself. auto *proto = MDNS_STR_ARG(service.proto); - while (pgm_read_byte(proto) == '_') { + while (progmem_read_byte(proto) == '_') { proto++; } auto *service_type = MDNS_STR_ARG(service.service_type); - while (pgm_read_byte(service_type) == '_') { + while (progmem_read_byte(service_type) == '_') { service_type++; } uint16_t port = const_cast &>(service.port).value(); From 7446c87267e0fa91aad989ea33507f42f0365d4f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 16:58:19 -0500 Subject: [PATCH 8/9] tidy --- esphome/components/mdns/mdns_esp8266.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index 88b4c89505..f1c8909807 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -22,11 +22,11 @@ void MDNSComponent::setup() { // expects the underscore to be there, the ESP8266 implementation always adds // the underscore itself. auto *proto = MDNS_STR_ARG(service.proto); - while (progmem_read_byte(proto) == '_') { + while (progmem_read_byte((const uint8_t *) proto) == '_') { proto++; } auto *service_type = MDNS_STR_ARG(service.service_type); - while (progmem_read_byte(service_type) == '_') { + while (progmem_read_byte((const uint8_t *) service_type) == '_') { service_type++; } uint16_t port = const_cast &>(service.port).value(); From fa66b3235ddbb86fec99c3fa0ba2fad680054f98 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 16:58:59 -0500 Subject: [PATCH 9/9] tidy --- esphome/components/mdns/mdns_esp32.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/mdns/mdns_esp32.cpp b/esphome/components/mdns/mdns_esp32.cpp index a471051401..40d305a1e6 100644 --- a/esphome/components/mdns/mdns_esp32.cpp +++ b/esphome/components/mdns/mdns_esp32.cpp @@ -45,7 +45,7 @@ void MDNSComponent::setup() { } if (err != ESP_OK) { - ESP_LOGW(TAG, "Failed to register service %s: %s", service.service_type, esp_err_to_name(err)); + ESP_LOGW(TAG, "Failed to register service %s: %s", MDNS_STR_ARG(service.service_type), esp_err_to_name(err)); } } }