mirror of
https://github.com/esphome/esphome.git
synced 2026-02-20 16:35:37 -07:00
dry
This commit is contained in:
@@ -69,17 +69,32 @@ class MDNSComponent : public Component {
|
||||
#endif
|
||||
|
||||
protected:
|
||||
/// Common setup logic called by all platform-specific setup() implementations
|
||||
void on_setup_(char *mac_address_buf) {
|
||||
/// 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
|
||||
#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_);
|
||||
auto &services = this->services_;
|
||||
#else
|
||||
this->compile_records_(this->services_, mac_address_buf);
|
||||
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_;
|
||||
#else
|
||||
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
|
||||
get_mac_address_into_buffer(mac_address);
|
||||
char *mac_ptr = mac_address;
|
||||
#endif
|
||||
#else
|
||||
char *mac_ptr = nullptr;
|
||||
#endif
|
||||
|
||||
this->compile_records_(services, mac_ptr);
|
||||
platform_register(this, services);
|
||||
}
|
||||
|
||||
#ifdef USE_MDNS_DYNAMIC_TXT
|
||||
|
||||
@@ -11,27 +11,11 @@ namespace esphome::mdns {
|
||||
|
||||
static const char *const TAG = "mdns";
|
||||
|
||||
void MDNSComponent::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, mac_address);
|
||||
#endif
|
||||
|
||||
static void register_esp32(MDNSComponent *comp, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
|
||||
esp_err_t err = mdns_init();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Init failed: %s", esp_err_to_name(err));
|
||||
this->mark_failed();
|
||||
comp->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -58,6 +42,8 @@ void MDNSComponent::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_esp32); }
|
||||
|
||||
void MDNSComponent::on_shutdown() {
|
||||
mdns_free();
|
||||
delay(40); // Allow the mdns packets announcing service removal to be sent
|
||||
|
||||
@@ -11,23 +11,7 @@
|
||||
|
||||
namespace esphome::mdns {
|
||||
|
||||
void MDNSComponent::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, mac_address);
|
||||
#endif
|
||||
|
||||
static void register_esp8266(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
|
||||
MDNS.begin(App.get_name().c_str());
|
||||
|
||||
for (const auto &service : services) {
|
||||
@@ -52,6 +36,8 @@ void MDNSComponent::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_esp8266); }
|
||||
|
||||
void MDNSComponent::loop() { MDNS.update(); }
|
||||
|
||||
void MDNSComponent::on_shutdown() {
|
||||
|
||||
@@ -9,14 +9,15 @@
|
||||
namespace esphome::mdns {
|
||||
|
||||
void MDNSComponent::setup() {
|
||||
#ifdef USE_MDNS_STORE_SERVICES
|
||||
#ifdef USE_API
|
||||
char mac_address[MAC_ADDRESS_BUFFER_SIZE];
|
||||
get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE>(mac_address));
|
||||
get_mac_address_into_buffer(this->mac_address_);
|
||||
char *mac_ptr = this->mac_address_;
|
||||
#else
|
||||
char *mac_address = nullptr;
|
||||
char *mac_ptr = nullptr;
|
||||
#endif
|
||||
this->compile_records_(this->services_, mac_ptr);
|
||||
#endif
|
||||
|
||||
this->on_setup_(mac_address);
|
||||
// Host platform doesn't have actual mDNS implementation
|
||||
}
|
||||
|
||||
|
||||
@@ -11,23 +11,7 @@
|
||||
|
||||
namespace esphome::mdns {
|
||||
|
||||
void MDNSComponent::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, mac_address);
|
||||
#endif
|
||||
|
||||
static void register_libretiny(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
|
||||
MDNS.begin(App.get_name().c_str());
|
||||
|
||||
for (const auto &service : services) {
|
||||
@@ -51,6 +35,8 @@ void MDNSComponent::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_libretiny); }
|
||||
|
||||
void MDNSComponent::on_shutdown() {}
|
||||
|
||||
} // namespace esphome::mdns
|
||||
|
||||
@@ -11,23 +11,7 @@
|
||||
|
||||
namespace esphome::mdns {
|
||||
|
||||
void MDNSComponent::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, mac_address);
|
||||
#endif
|
||||
|
||||
static void register_rp2040(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
|
||||
MDNS.begin(App.get_name().c_str());
|
||||
|
||||
for (const auto &service : services) {
|
||||
@@ -51,6 +35,8 @@ void MDNSComponent::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_rp2040); }
|
||||
|
||||
void MDNSComponent::loop() { MDNS.update(); }
|
||||
|
||||
void MDNSComponent::on_shutdown() {
|
||||
|
||||
Reference in New Issue
Block a user