ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mdns_rp2040.cpp
Go to the documentation of this file.
2#if defined(USE_RP2040) && defined(USE_MDNS)
3
7#include "esphome/core/log.h"
8#include "mdns_component.h"
9// wifi_component.h / ethernet_component.h are pulled in transitively by
10// mdns_component.h when their respective listener defines are active.
11
12// Arduino-Pico's PolledTimeout.h (pulled in by ESP8266mDNS.h) redefines IRAM_ATTR to empty.
13#pragma push_macro("IRAM_ATTR")
14#undef IRAM_ATTR
15#include <ESP8266mDNS.h>
16#pragma pop_macro("IRAM_ATTR")
17
18namespace esphome::mdns {
19
20static void register_rp2040(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
21 MDNS.begin(App.get_name().c_str());
22
23 for (const auto &service : services) {
24 // ESP8266mDNS always adds the leading underscore itself, so strip it here.
25 auto *proto = MDNS_STR_ARG(service.proto);
26 while (*proto == '_') {
27 proto++;
28 }
29 auto *service_type = MDNS_STR_ARG(service.service_type);
30 while (*service_type == '_') {
31 service_type++;
32 }
33 uint16_t port = service.port.value();
34 MDNS.addService(service_type, proto, port);
35 for (const auto &record : service.txt_records) {
36 MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
37 }
38 }
39}
40
41#ifdef USE_MDNS_EVENT_DRIVEN_POLLING
43 // uint32_t-ID set_interval/set_timeout already does atomic cancel-and-add.
44 this->set_interval(MDNS_POLL_ID, MDNS_UPDATE_INTERVAL_MS, []() { MDNS.update(); });
46}
47#endif
48
50 // arduino-pico stubs out LwipIntf::stateUpCB (the netif status callback LEAmDNS uses
51 // on ESP8266 for auto-restart), so we must drive begin()/notifyAPChange() from our
52 // own IP state listener. Both WiFi and Ethernet have the same listener signature —
53 // one on_ip_state() override serves both.
54#ifdef USE_MDNS_WIFI_LISTENER
56 // AFTER_CONNECTION priority means the network may already be up; the listener only
57 // fires on subsequent changes, so seed the current state.
58 {
60 if (ips[0].is_set()) {
61 this->on_ip_state(ips, wifi::global_wifi_component->get_dns_address(0),
62 wifi::global_wifi_component->get_dns_address(1));
63 }
64 }
65#endif
66#ifdef USE_MDNS_ETHERNET_LISTENER
70 if (ips[0].is_set()) {
71 this->on_ip_state(ips, network::IPAddress{}, network::IPAddress{});
72 }
73 }
74#endif
75}
76
77#ifdef USE_MDNS_EVENT_DRIVEN_POLLING
78void MDNSComponent::on_ip_state(const network::IPAddresses &ips, const network::IPAddress &,
79 const network::IPAddress &) {
80 // Listener only fires on IP acquisition (not loss); every event is a fresh IP.
81 if (!ips[0].is_set()) {
82 return;
83 }
84 if (!this->initialized_) {
85 this->setup_buffers_and_register_(register_rp2040);
86 this->initialized_ = true;
87 } else {
88 MDNS.notifyAPChange();
89 }
91}
92#endif
93
95 MDNS.close();
96 delay(40);
97}
98
99} // namespace esphome::mdns
100
101#endif
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.h:400
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_interval(const std boo cancel_interval)(const char *name)
Cancel an interval function.
Definition component.h:422
constexpr const char * c_str() const
Definition string_ref.h:73
void add_ip_state_listener(EthernetIPStateListener *listener)
static constexpr uint32_t MDNS_POLL_STOP_ID
void start_polling_window_()
Arm a fresh MDNS_POLL_WINDOW_MS polling window.
void on_ip_state(const network::IPAddresses &ips, const network::IPAddress &dns1, const network::IPAddress &dns2) override
static constexpr uint32_t MDNS_POLL_WINDOW_MS
void setup_buffers_and_register_(PlatformRegisterFn platform_register)
static constexpr uint32_t MDNS_POLL_ID
static constexpr uint32_t MDNS_UPDATE_INTERVAL_MS
void add_ip_state_listener(WiFiIPStateListener *listener)
Add a listener for IP state changes.
network::IPAddresses wifi_sta_ip_addresses()
EthernetComponent * global_eth_component
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:223
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:27
WiFiComponent * global_wifi_component
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.