Merge branch 'integration' into memory_api

This commit is contained in:
J. Nick Koston
2025-11-23 22:43:50 -06:00
9 changed files with 74 additions and 29 deletions

View File

@@ -35,7 +35,7 @@ MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp");
// Wrap build-time defines into flash storage
MDNS_STATIC_CONST_CHAR(VALUE_VERSION, ESPHOME_VERSION);
void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services, char *mac_address_buf) {
// IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES
// in mdns/__init__.py. If you add a new service here, update both locations.
@@ -86,7 +86,9 @@ void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUN
txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), MDNS_STR(friendly_name.c_str())});
}
txt_records.push_back({MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)});
txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(this->mac_address_)});
// MAC address: passed from caller (either member buffer or stack buffer depending on USE_MDNS_STORE_SERVICES)
txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(mac_address_buf)});
#ifdef USE_ESP8266
MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266");

View File

@@ -70,14 +70,15 @@ class MDNSComponent : public Component {
protected:
/// Common setup logic called by all platform-specific setup() implementations
void on_setup_() {
#ifdef USE_API
// Populate MAC address buffer once during setup
get_mac_address_into_buffer(std::span<char, 13>(this->mac_address_));
#endif
void on_setup_(char *mac_address_buf) {
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
#ifdef USE_API
// Copy to member buffer for storage
std::memcpy(this->mac_address_, mac_address_buf, MAC_ADDRESS_BUFFER_SIZE);
this->compile_records_(this->services_, this->mac_address_);
#else
this->compile_records_(this->services_, mac_address_buf);
#endif
#endif
}
@@ -88,14 +89,14 @@ class MDNSComponent : public Component {
StaticVector<std::string, MDNS_DYNAMIC_TXT_COUNT> dynamic_txt_values_;
#endif
#ifdef USE_API
/// Fixed buffer for MAC address (populated once in setup())
char mac_address_[13]; // 12 hex chars + null terminator
#if defined(USE_API) && defined(USE_MDNS_STORE_SERVICES)
/// Fixed buffer for MAC address (only needed when services are stored)
char mac_address_[MAC_ADDRESS_BUFFER_SIZE];
#endif
#ifdef USE_MDNS_STORE_SERVICES
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services_{};
#endif
void compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services);
void compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services, char *mac_address_buf);
};
} // namespace esphome::mdns

View File

@@ -12,13 +12,20 @@ namespace esphome::mdns {
static const char *const TAG = "mdns";
void MDNSComponent::setup() {
this->on_setup_();
#ifdef USE_API
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE>(mac_address));
#else
char *mac_address = nullptr;
#endif
this->on_setup_(mac_address);
#ifdef USE_MDNS_STORE_SERVICES
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
this->compile_records_(services, mac_address);
#endif
esp_err_t err = mdns_init();

View File

@@ -12,13 +12,20 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup_();
#ifdef USE_API
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE>(mac_address));
#else
char *mac_address = nullptr;
#endif
this->on_setup_(mac_address);
#ifdef USE_MDNS_STORE_SERVICES
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
this->compile_records_(services, mac_address);
#endif
MDNS.begin(App.get_name().c_str());

View File

@@ -9,7 +9,14 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup_();
#ifdef USE_API
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE>(mac_address));
#else
char *mac_address = nullptr;
#endif
this->on_setup_(mac_address);
// Host platform doesn't have actual mDNS implementation
}

View File

@@ -12,13 +12,20 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup_();
#ifdef USE_API
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE>(mac_address));
#else
char *mac_address = nullptr;
#endif
this->on_setup_(mac_address);
#ifdef USE_MDNS_STORE_SERVICES
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
this->compile_records_(services, mac_address);
#endif
MDNS.begin(App.get_name().c_str());

View File

@@ -12,13 +12,20 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup_();
#ifdef USE_API
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE>(mac_address));
#else
char *mac_address = nullptr;
#endif
this->on_setup_(mac_address);
#ifdef USE_MDNS_STORE_SERVICES
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
this->compile_records_(services);
this->compile_records_(services, mac_address);
#endif
MDNS.begin(App.get_name().c_str());

View File

@@ -640,17 +640,17 @@ std::string get_mac_address() {
}
std::string get_mac_address_pretty() {
char buf[18];
char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
return std::string(get_mac_address_pretty_into_buffer(buf));
}
void get_mac_address_into_buffer(std::span<char, 13> buf) {
void get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE> buf) {
uint8_t mac[6];
get_mac_address_raw(mac);
format_mac_addr_lower_no_sep(mac, buf.data());
}
const char *get_mac_address_pretty_into_buffer(std::span<char, 18> buf) {
const char *get_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf) {
uint8_t mac[6];
get_mac_address_raw(mac);
format_mac_addr_upper(mac, buf.data());

View File

@@ -1123,6 +1123,12 @@ class HighFrequencyLoopRequester {
/// Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
void get_mac_address_raw(uint8_t *mac); // NOLINT(readability-non-const-parameter)
/// Buffer size for MAC address in lowercase hex notation (12 hex chars + null terminator)
constexpr size_t MAC_ADDRESS_BUFFER_SIZE = 13;
/// Buffer size for MAC address in colon-separated uppercase hex notation (17 chars + null terminator)
constexpr size_t MAC_ADDRESS_PRETTY_BUFFER_SIZE = 18;
/// Get the device MAC address as a string, in lowercase hex notation.
std::string get_mac_address();
@@ -1130,13 +1136,14 @@ std::string get_mac_address();
std::string get_mac_address_pretty();
/// Get the device MAC address into the given buffer, in lowercase hex notation.
/// Assumes buffer length is 13 (12 digits for hexadecimal representation followed by null terminator).
void get_mac_address_into_buffer(std::span<char, 13> buf);
/// Assumes buffer length is MAC_ADDRESS_BUFFER_SIZE (12 digits for hexadecimal representation followed by null
/// terminator).
void get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE> buf);
/// Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.
/// Buffer must be exactly 18 bytes (17 for "XX:XX:XX:XX:XX:XX" + null terminator).
/// Buffer must be exactly MAC_ADDRESS_PRETTY_BUFFER_SIZE bytes (17 for "XX:XX:XX:XX:XX:XX" + null terminator).
/// Returns pointer to the buffer for convenience.
const char *get_mac_address_pretty_into_buffer(std::span<char, 18> buf);
const char *get_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
#ifdef USE_ESP32
/// Set the MAC address to use from the provided byte array (6 bytes).