diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index cdd24015f5..9bf27e71e4 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -39,6 +39,37 @@ MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp"); // Wrap build-time defines into flash storage MDNS_STATIC_CONST_CHAR(VALUE_VERSION, ESPHOME_VERSION); +void MDNSComponent::setup_buffers_and_register_(PlatformRegisterFn platform_register) { +#ifdef USE_MDNS_STORE_SERVICES + auto &services = this->services_; +#else + StaticVector services_storage; + auto &services = services_storage; +#endif + +#ifdef USE_API +#ifdef USE_MDNS_STORE_SERVICES + get_mac_address_into_buffer(this->mac_address_); + char *mac_ptr = this->mac_address_; + format_hex_to(this->config_hash_str_, App.get_config_hash()); + char *cfg_ptr = this->config_hash_str_; +#else + char mac_address[MAC_ADDRESS_BUFFER_SIZE]; + char config_hash_str[CONFIG_HASH_STR_SIZE]; + get_mac_address_into_buffer(mac_address); + format_hex_to(config_hash_str, App.get_config_hash()); + char *mac_ptr = mac_address; + char *cfg_ptr = config_hash_str; +#endif +#else + char *mac_ptr = nullptr; + char *cfg_ptr = nullptr; +#endif + + this->compile_records_(services, mac_ptr, cfg_ptr); + platform_register(this, services); +} + void MDNSComponent::compile_records_(StaticVector &services, char *mac_address_buf, char *config_hash_buf) { // IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES diff --git a/esphome/components/mdns/mdns_component.h b/esphome/components/mdns/mdns_component.h index a4aedab5fe..e981a65941 100644 --- a/esphome/components/mdns/mdns_component.h +++ b/esphome/components/mdns/mdns_component.h @@ -1,10 +1,7 @@ #pragma once #include "esphome/core/defines.h" #ifdef USE_MDNS -#include -#include #include -#include "esphome/core/application.h" #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/helpers.h" @@ -74,7 +71,7 @@ class MDNSComponent final : public Component void dump_config() override; /// Size of buffer required for config hash hex string (8 hex chars + null terminator) - static constexpr size_t CONFIG_HASH_STR_SIZE = 9; + static constexpr size_t CONFIG_HASH_STR_SIZE = format_hex_size(sizeof(uint32_t)); #ifdef USE_MDNS_EVENT_DRIVEN_POLLING // LEAmDNS has meaningful work only during the probe+announce phase (3×250ms probes + @@ -130,35 +127,7 @@ class MDNSComponent final : public Component /// Helper to set up services and MAC buffers, then call platform-specific registration using PlatformRegisterFn = void (*)(MDNSComponent *, StaticVector &); - void setup_buffers_and_register_(PlatformRegisterFn platform_register) { -#ifdef USE_MDNS_STORE_SERVICES - auto &services = this->services_; -#else - StaticVector services_storage; - auto &services = services_storage; -#endif - -#ifdef USE_API -#ifdef USE_MDNS_STORE_SERVICES - get_mac_address_into_buffer(this->mac_address_); - char *mac_ptr = this->mac_address_; - char *cfg_ptr = this->config_hash_str_; -#else - char mac_address[MAC_ADDRESS_BUFFER_SIZE]; - char config_hash_str[CONFIG_HASH_STR_SIZE]; - get_mac_address_into_buffer(mac_address); - char *mac_ptr = mac_address; - char *cfg_ptr = config_hash_str; -#endif - snprintf(cfg_ptr, CONFIG_HASH_STR_SIZE, "%08" PRIx32, App.get_config_hash()); -#else - char *mac_ptr = nullptr; - char *cfg_ptr = nullptr; -#endif - - this->compile_records_(services, mac_ptr, cfg_ptr); - platform_register(this, services); - } + void setup_buffers_and_register_(PlatformRegisterFn platform_register); #ifdef USE_MDNS_DYNAMIC_TXT /// Storage for runtime-generated TXT values from user lambdas