From c9a709675a9a4228ed21679c02ddb1b2babe00c5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 15:24:42 -0500 Subject: [PATCH 1/4] simplify --- esphome/components/mdns/mdns_component.cpp | 61 ++++++++-------------- esphome/components/mdns/mdns_component.h | 4 +- esphome/components/mdns/mdns_esp8266.cpp | 4 +- esphome/components/mdns/mdns_libretiny.cpp | 4 +- esphome/components/mdns/mdns_rp2040.cpp | 4 +- 5 files changed, 30 insertions(+), 47 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index 3a97951d60..045addea36 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -45,24 +45,7 @@ static const char *const TAG = "mdns"; #define USE_WEBSERVER_PORT 80 // NOLINT #endif -// 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"); - +// Define constant strings for values (PROGMEM on ESP8266, regular flash on others) MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266"); MDNS_STATIC_CONST_CHAR(PLATFORM_ESP32, "ESP32"); MDNS_STATIC_CONST_CHAR(PLATFORM_RP2040, "RP2040"); @@ -80,8 +63,8 @@ void MDNSComponent::compile_records_() { #ifdef USE_API if (api::global_api_server != nullptr) { auto &service = this->services_.emplace_next(); - service.service_type = MDNS_STR(SERVICE_ESPHOMELIB); - service.proto = MDNS_STR(SERVICE_TCP); + service.service_type = "_esphomelib"; + service.proto = "_tcp"; service.port = api::global_api_server->get_port(); const std::string &friendly_name = App.get_friendly_name(); @@ -112,62 +95,62 @@ void MDNSComponent::compile_records_() { txt_records.reserve(txt_count); if (!friendly_name_empty) { - txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), friendly_name}); + txt_records.push_back({"friendly_name", friendly_name}); } - txt_records.push_back({MDNS_STR(TXT_VERSION), ESPHOME_VERSION}); - txt_records.push_back({MDNS_STR(TXT_MAC), get_mac_address()}); + txt_records.push_back({"version", ESPHOME_VERSION}); + txt_records.push_back({"mac", get_mac_address()}); #ifdef USE_ESP8266 - txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP8266)}); + txt_records.push_back({"platform", MDNS_STR(PLATFORM_ESP8266)}); #elif defined(USE_ESP32) - txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP32)}); + txt_records.push_back({"platform", MDNS_STR(PLATFORM_ESP32)}); #elif defined(USE_RP2040) - txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_RP2040)}); + txt_records.push_back({"platform", MDNS_STR(PLATFORM_RP2040)}); #elif defined(USE_LIBRETINY) txt_records.emplace_back(MDNSTXTRecord{"platform", lt_cpu_get_model_name()}); #endif - txt_records.push_back({MDNS_STR(TXT_BOARD), ESPHOME_BOARD}); + txt_records.push_back({"board", ESPHOME_BOARD}); #if defined(USE_WIFI) - txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_WIFI)}); + txt_records.push_back({"network", MDNS_STR(NETWORK_WIFI)}); #elif defined(USE_ETHERNET) - txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_ETHERNET)}); + txt_records.push_back({"network", MDNS_STR(NETWORK_ETHERNET)}); #elif defined(USE_OPENTHREAD) - txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_THREAD)}); + txt_records.push_back({"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({MDNS_STR(TXT_API_ENCRYPTION), MDNS_STR(NOISE_ENCRYPTION)}); + txt_records.push_back({"api_encryption", MDNS_STR(NOISE_ENCRYPTION)}); } else { - txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION_SUPPORTED), MDNS_STR(NOISE_ENCRYPTION)}); + txt_records.push_back({"api_encryption_supported", MDNS_STR(NOISE_ENCRYPTION)}); } #endif #ifdef ESPHOME_PROJECT_NAME - txt_records.push_back({MDNS_STR(TXT_PROJECT_NAME), ESPHOME_PROJECT_NAME}); - txt_records.push_back({MDNS_STR(TXT_PROJECT_VERSION), ESPHOME_PROJECT_VERSION}); + txt_records.push_back({"project_name", ESPHOME_PROJECT_NAME}); + txt_records.push_back({"project_version", ESPHOME_PROJECT_VERSION}); #endif // ESPHOME_PROJECT_NAME #ifdef USE_DASHBOARD_IMPORT - txt_records.push_back({MDNS_STR(TXT_PACKAGE_IMPORT_URL), dashboard_import::get_package_import_url()}); + txt_records.push_back({"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 = MDNS_STR(SERVICE_PROMETHEUS); - prom_service.proto = MDNS_STR(SERVICE_TCP); + prom_service.service_type = "_prometheus-http"; + prom_service.proto = "_tcp"; prom_service.port = USE_WEBSERVER_PORT; #endif #ifdef USE_WEBSERVER auto &web_service = this->services_.emplace_next(); - web_service.service_type = MDNS_STR(SERVICE_HTTP); - web_service.proto = MDNS_STR(SERVICE_TCP); + web_service.service_type = "_http"; + web_service.proto = "_tcp"; web_service.port = USE_WEBSERVER_PORT; #endif diff --git a/esphome/components/mdns/mdns_component.h b/esphome/components/mdns/mdns_component.h index 89cd5db48b..176240b828 100644 --- a/esphome/components/mdns/mdns_component.h +++ b/esphome/components/mdns/mdns_component.h @@ -20,10 +20,10 @@ struct MDNSTXTRecord { struct MDNSService { // service name _including_ underscore character prefix // as defined in RFC6763 Section 7 - std::string service_type; + const char *service_type; // second label indicating protocol _including_ underscore character prefix // as defined in RFC6763 Section 7, like "_tcp" or "_udp" - std::string proto; + const char *proto; TemplatableValue port; std::vector txt_records; }; diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index 3be75263f2..d348088444 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -21,11 +21,11 @@ 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.c_str(); + auto *proto = service.proto; while (*proto == '_') { proto++; } - auto *service_type = service.service_type.c_str(); + auto *service_type = service.service_type; while (*service_type == '_') { service_type++; } diff --git a/esphome/components/mdns/mdns_libretiny.cpp b/esphome/components/mdns/mdns_libretiny.cpp index e779e563c5..6edf9b329b 100644 --- a/esphome/components/mdns/mdns_libretiny.cpp +++ b/esphome/components/mdns/mdns_libretiny.cpp @@ -21,11 +21,11 @@ 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.c_str(); + auto *proto = service.proto; while (*proto == '_') { proto++; } - auto *service_type = service.service_type.c_str(); + auto *service_type = service.service_type; while (*service_type == '_') { service_type++; } diff --git a/esphome/components/mdns/mdns_rp2040.cpp b/esphome/components/mdns/mdns_rp2040.cpp index 66fda3e261..92d35e7e34 100644 --- a/esphome/components/mdns/mdns_rp2040.cpp +++ b/esphome/components/mdns/mdns_rp2040.cpp @@ -21,11 +21,11 @@ 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.c_str(); + auto *proto = service.proto; while (*proto == '_') { proto++; } - auto *service_type = service.service_type.c_str(); + auto *service_type = service.service_type; while (*service_type == '_') { service_type++; } From 2e4722104eae025779ee3e959a98027f67ca2e2b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 15:24:48 -0500 Subject: [PATCH 2/4] simplify --- esphome/components/mdns/mdns_esp32.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/esphome/components/mdns/mdns_esp32.cpp b/esphome/components/mdns/mdns_esp32.cpp index 85738814c1..53391382ef 100644 --- a/esphome/components/mdns/mdns_esp32.cpp +++ b/esphome/components/mdns/mdns_esp32.cpp @@ -36,8 +36,7 @@ void MDNSComponent::setup() { txt_records.push_back(it); } uint16_t port = const_cast &>(service.port).value(); - err = mdns_service_add(nullptr, service.service_type.c_str(), service.proto.c_str(), port, txt_records.data(), - txt_records.size()); + err = mdns_service_add(nullptr, service.service_type, service.proto, port, txt_records.data(), txt_records.size()); // free records for (const auto &it : txt_records) { @@ -45,7 +44,7 @@ void MDNSComponent::setup() { } if (err != ESP_OK) { - ESP_LOGW(TAG, "Failed to register service %s: %s", service.service_type.c_str(), esp_err_to_name(err)); + ESP_LOGW(TAG, "Failed to register service %s: %s", service.service_type, esp_err_to_name(err)); } } } From 711532465e050f64e0dc109bd4fafdc17ce90906 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 15:27:49 -0500 Subject: [PATCH 3/4] simplify --- esphome/components/mdns/mdns_component.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index 045addea36..1c82661ff0 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -173,7 +173,7 @@ 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.c_str(), service.proto.c_str(), + ESP_LOGV(TAG, " - %s, %s, %d", service.service_type, service.proto, const_cast &>(service.port).value()); for (const auto &record : service.txt_records) { ESP_LOGV(TAG, " TXT: %s = %s", record.key, From f0a7c6b0bb5fdf6c975f4d269e181ef854398eca Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 15:32:59 -0500 Subject: [PATCH 4/4] simplify --- esphome/components/mdns/mdns_component.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index 1c82661ff0..11c1d1fa67 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -45,7 +45,7 @@ static const char *const TAG = "mdns"; #define USE_WEBSERVER_PORT 80 // NOLINT #endif -// Define constant strings for values (PROGMEM on ESP8266, regular flash on others) +// Define constant strings for values (PROGMEM on ESP8266, static pointers on others) MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266"); MDNS_STATIC_CONST_CHAR(PLATFORM_ESP32, "ESP32"); MDNS_STATIC_CONST_CHAR(PLATFORM_RP2040, "RP2040");