1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-13 15:23:49 +01:00

Merge branch 'mdns_esp32_cleanup' into integration

This commit is contained in:
J. Nick Koston
2025-10-07 11:14:43 -05:00

View File

@@ -26,24 +26,16 @@ void MDNSComponent::setup() {
mdns_instance_name_set(this->hostname_.c_str()); mdns_instance_name_set(this->hostname_.c_str());
for (const auto &service : this->services_) { for (const auto &service : this->services_) {
std::vector<mdns_txt_item_t> txt_records; std::vector<mdns_txt_item_t> txt_records(service.txt_records.size());
for (const auto &record : service.txt_records) { for (size_t i = 0; i < service.txt_records.size(); i++) {
mdns_txt_item_t it{}; // mdns_service_add copies the strings internally, no need to strdup
// dup strings to ensure the pointer is valid even after the record loop txt_records[i].key = service.txt_records[i].key.c_str();
it.key = strdup(record.key.c_str()); txt_records[i].value = const_cast<TemplatableValue<std::string> &>(service.txt_records[i].value).value().c_str();
it.value = strdup(const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
txt_records.push_back(it);
} }
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value(); uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
err = mdns_service_add(nullptr, service.service_type.c_str(), service.proto.c_str(), port, txt_records.data(), err = mdns_service_add(nullptr, service.service_type.c_str(), service.proto.c_str(), port, txt_records.data(),
txt_records.size()); txt_records.size());
// free records
for (const auto &it : txt_records) {
delete it.key; // NOLINT(cppcoreguidelines-owning-memory)
delete it.value; // NOLINT(cppcoreguidelines-owning-memory)
}
if (err != ESP_OK) { 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.c_str(), esp_err_to_name(err));
} }