Merge branch 'mdns_arduino' into integration

This commit is contained in:
J. Nick Koston
2025-11-24 15:34:43 -06:00
3 changed files with 48 additions and 62 deletions

View File

@@ -0,0 +1,42 @@
#pragma once
#include "esphome/core/defines.h"
// Common Arduino mDNS registration for RP2040 and LibreTiny
#if defined(USE_MDNS) && (defined(USE_RP2040) || defined(USE_LIBRETINY))
#include "esphome/core/application.h"
#include "mdns_component.h"
namespace esphome::mdns {
/// Register mDNS services using Arduino-style mDNS library (RP2040/LibreTiny)
/// @param services The services to register
inline void register_arduino_mdns(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
MDNS.begin(App.get_name().c_str());
for (const auto &service : services) {
// Strip the leading underscore from the proto and service_type. While it is
// part of the wire protocol to have an underscore, and for example ESP-IDF
// expects the underscore to be there, the RP2040/LibreTiny mDNS
// implementations always add the underscore themselves.
auto *proto = MDNS_STR_ARG(service.proto);
while (*proto == '_') {
proto++;
}
auto *service_type = MDNS_STR_ARG(service.service_type);
while (*service_type == '_') {
service_type++;
}
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
MDNS.addService(service_type, proto, port);
for (const auto &record : service.txt_records) {
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
}
}
}
} // namespace esphome::mdns
#endif

View File

@@ -1,41 +1,13 @@
#include "esphome/core/defines.h"
#if defined(USE_LIBRETINY) && defined(USE_MDNS)
#include "esphome/components/network/ip_address.h"
#include "esphome/components/network/util.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include "mdns_component.h"
#include <mDNS.h>
#include "mdns_arduino.h"
namespace esphome::mdns {
static void register_libretiny(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
MDNS.begin(App.get_name().c_str());
for (const auto &service : services) {
// Strip the leading underscore from the proto and service_type. While it is
// part of the wire protocol to have an underscore, and for example ESP-IDF
// expects the underscore to be there, the ESP8266 implementation always adds
// the underscore itself.
auto *proto = MDNS_STR_ARG(service.proto);
while (*proto == '_') {
proto++;
}
auto *service_type = MDNS_STR_ARG(service.service_type);
while (*service_type == '_') {
service_type++;
}
uint16_t port_ = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
MDNS.addService(service_type, proto, port_);
for (const auto &record : service.txt_records) {
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
}
}
}
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_libretiny); }
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_arduino_mdns); }
void MDNSComponent::on_shutdown() {}

View File

@@ -1,41 +1,13 @@
#include "esphome/core/defines.h"
#if defined(USE_RP2040) && defined(USE_MDNS)
#include "esphome/components/network/ip_address.h"
#include "esphome/components/network/util.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include "mdns_component.h"
#include <ESP8266mDNS.h>
#include "mdns_arduino.h"
namespace esphome::mdns {
static void register_rp2040(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
MDNS.begin(App.get_name().c_str());
for (const auto &service : services) {
// Strip the leading underscore from the proto and service_type. While it is
// part of the wire protocol to have an underscore, and for example ESP-IDF
// expects the underscore to be there, the ESP8266 implementation always adds
// the underscore itself.
auto *proto = MDNS_STR_ARG(service.proto);
while (*proto == '_') {
proto++;
}
auto *service_type = MDNS_STR_ARG(service.service_type);
while (*service_type == '_') {
service_type++;
}
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
MDNS.addService(service_type, proto, port);
for (const auto &record : service.txt_records) {
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
}
}
}
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_rp2040); }
void MDNSComponent::setup() { this->setup_buffers_and_register_(register_arduino_mdns); }
void MDNSComponent::loop() { MDNS.update(); }