1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-02 19:32:19 +01:00

mdns services array

This commit is contained in:
J. Nick Koston
2025-08-15 13:11:13 -05:00
parent af9ecf3429
commit 1b20f508b3
5 changed files with 54 additions and 21 deletions

View File

@@ -90,12 +90,38 @@ async def to_code(config):
cg.add_define("USE_MDNS") cg.add_define("USE_MDNS")
# Count how many services we'll have at compile time
service_count = 0
# Check if API is enabled (USE_API is defined by api component)
if "api" in CORE.config:
service_count += 1
# Check if Prometheus is enabled (USE_PROMETHEUS is defined by prometheus component)
if "prometheus" in CORE.config:
service_count += 1
# Check if Web Server is enabled (USE_WEBSERVER is defined by web_server component)
if "web_server" in CORE.config:
service_count += 1
# Count extra services from config
extra_service_count = len(config.get(CONF_SERVICES, []))
if extra_service_count > 0:
service_count += extra_service_count
cg.add_define("USE_MDNS_EXTRA_SERVICES")
cg.add_define("MDNS_EXTRA_SERVICE_COUNT", extra_service_count)
# If no services, we'll add a fallback HTTP service
if service_count == 0:
service_count = 1
# Define the total service count
cg.add_define("MDNS_SERVICE_COUNT", service_count)
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)
if config[CONF_SERVICES]:
cg.add_define("USE_MDNS_EXTRA_SERVICES")
for service in config[CONF_SERVICES]: for service in config[CONF_SERVICES]:
txt = [ txt = [
cg.StructInitializer( cg.StructInitializer(

View File

@@ -24,7 +24,8 @@ static const char *const TAG = "mdns";
void MDNSComponent::compile_records_() { void MDNSComponent::compile_records_() {
this->hostname_ = App.get_name(); this->hostname_ = App.get_name();
this->services_.clear(); this->service_count_ = 0;
#ifdef USE_API #ifdef USE_API
if (api::global_api_server != nullptr) { if (api::global_api_server != nullptr) {
MDNSService service{}; MDNSService service{};
@@ -80,7 +81,7 @@ void MDNSComponent::compile_records_() {
service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()}); service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()});
#endif #endif
this->services_.push_back(service); this->services_[this->service_count_++] = service;
} }
#endif // USE_API #endif // USE_API
@@ -90,7 +91,7 @@ void MDNSComponent::compile_records_() {
service.service_type = "_prometheus-http"; service.service_type = "_prometheus-http";
service.proto = "_tcp"; service.proto = "_tcp";
service.port = USE_WEBSERVER_PORT; service.port = USE_WEBSERVER_PORT;
this->services_.push_back(service); this->services_[this->service_count_++] = service;
} }
#endif #endif
@@ -100,15 +101,17 @@ void MDNSComponent::compile_records_() {
service.service_type = "_http"; service.service_type = "_http";
service.proto = "_tcp"; service.proto = "_tcp";
service.port = USE_WEBSERVER_PORT; service.port = USE_WEBSERVER_PORT;
this->services_.push_back(service); this->services_[this->service_count_++] = service;
} }
#endif #endif
#ifdef USE_MDNS_EXTRA_SERVICES #ifdef USE_MDNS_EXTRA_SERVICES
this->services_.insert(this->services_.end(), this->services_extra_.begin(), this->services_extra_.end()); for (const auto &extra_service : this->services_extra_) {
this->services_[this->service_count_++] = extra_service;
}
#endif #endif
if (this->services_.empty()) { if (this->service_count_ == 0) {
// Publish "http" service if not using native API // Publish "http" service if not using native API
// This is just to have *some* mDNS service so that .local resolution works // This is just to have *some* mDNS service so that .local resolution works
MDNSService service{}; MDNSService service{};
@@ -116,7 +119,7 @@ void MDNSComponent::compile_records_() {
service.proto = "_tcp"; service.proto = "_tcp";
service.port = USE_WEBSERVER_PORT; service.port = USE_WEBSERVER_PORT;
service.txt_records.push_back({"version", ESPHOME_VERSION}); service.txt_records.push_back({"version", ESPHOME_VERSION});
this->services_.push_back(service); this->services_[this->service_count_++] = service;
} }
} }
@@ -126,7 +129,8 @@ void MDNSComponent::dump_config() {
" Hostname: %s", " Hostname: %s",
this->hostname_.c_str()); this->hostname_.c_str());
ESP_LOGV(TAG, " Services:"); ESP_LOGV(TAG, " Services:");
for (const auto &service : this->services_) { for (size_t i = 0; i < this->service_count_; i++) {
const auto &service = this->services_[i];
ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(), ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(),
const_cast<TemplatableValue<uint16_t> &>(service.port).value()); const_cast<TemplatableValue<uint16_t> &>(service.port).value());
for (const auto &record : service.txt_records) { for (const auto &record : service.txt_records) {
@@ -136,8 +140,6 @@ void MDNSComponent::dump_config() {
} }
} }
std::vector<MDNSService> MDNSComponent::get_services() { return this->services_; }
} // namespace mdns } // namespace mdns
} // namespace esphome } // namespace esphome
#endif #endif

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "esphome/core/defines.h" #include "esphome/core/defines.h"
#ifdef USE_MDNS #ifdef USE_MDNS
#include <array>
#include <string> #include <string>
#include <vector> #include <vector>
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
@@ -39,7 +40,8 @@ class MDNSComponent : public Component {
void add_extra_service(MDNSService service) { services_extra_.push_back(std::move(service)); } void add_extra_service(MDNSService service) { services_extra_.push_back(std::move(service)); }
#endif #endif
std::vector<MDNSService> get_services(); const std::array<MDNSService, MDNS_SERVICE_COUNT> &get_services() const { return this->services_; }
size_t get_service_count() const { return this->service_count_; }
void on_shutdown() override; void on_shutdown() override;
@@ -47,7 +49,8 @@ class MDNSComponent : public Component {
#ifdef USE_MDNS_EXTRA_SERVICES #ifdef USE_MDNS_EXTRA_SERVICES
std::vector<MDNSService> services_extra_{}; std::vector<MDNSService> services_extra_{};
#endif #endif
std::vector<MDNSService> services_{}; std::array<MDNSService, MDNS_SERVICE_COUNT> services_{};
size_t service_count_{0}; // Track how many services are actually populated
std::string hostname_; std::string hostname_;
void compile_records_(); void compile_records_();
}; };

View File

@@ -125,11 +125,14 @@ void OpenThreadSrpComponent::setup() {
return; return;
} }
// Copy the mdns services to our local instance so that the c_str pointers remain valid for the lifetime of this // Get the mdns services directly from the component
// component // No lifetime concerns since OpenThread runs after MDNS component (enforced by get_setup_priority)
this->mdns_services_ = this->mdns_->get_services(); // and the services array in MDNS component lives for the entire application lifetime
ESP_LOGD(TAG, "Setting up SRP services. count = %d\n", this->mdns_services_.size()); const auto &services = this->mdns_->get_services();
for (const auto &service : this->mdns_services_) { size_t service_count = this->mdns_->get_service_count();
ESP_LOGD(TAG, "Setting up SRP services. count = %d\n", service_count);
for (size_t i = 0; i < service_count; i++) {
const auto &service = services[i];
otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance); otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance);
if (!entry) { if (!entry) {
ESP_LOGW(TAG, "Failed to allocate service entry"); ESP_LOGW(TAG, "Failed to allocate service entry");

View File

@@ -46,7 +46,6 @@ class OpenThreadSrpComponent : public Component {
protected: protected:
esphome::mdns::MDNSComponent *mdns_{nullptr}; esphome::mdns::MDNSComponent *mdns_{nullptr};
std::vector<esphome::mdns::MDNSService> mdns_services_;
std::vector<std::unique_ptr<uint8_t[]>> memory_pool_; std::vector<std::unique_ptr<uint8_t[]>> memory_pool_;
void *pool_alloc_(size_t size); void *pool_alloc_(size_t size);
}; };