[mdns] Move setup helper to .cpp and use format_hex_to

This commit is contained in:
J. Nick Koston
2026-04-29 16:44:03 -05:00
parent 7543a356c8
commit 5a59e99016
2 changed files with 33 additions and 33 deletions
@@ -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<MDNSService, MDNS_SERVICE_COUNT> 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<MDNSService, MDNS_SERVICE_COUNT> &services, char *mac_address_buf,
char *config_hash_buf) {
// IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES
+2 -33
View File
@@ -1,10 +1,7 @@
#pragma once
#include "esphome/core/defines.h"
#ifdef USE_MDNS
#include <cinttypes>
#include <cstdio>
#include <string>
#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<MDNSService, MDNS_SERVICE_COUNT> &);
void setup_buffers_and_register_(PlatformRegisterFn platform_register) {
#ifdef USE_MDNS_STORE_SERVICES
auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> 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