ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
mdns_esp8266.cpp
Go to the documentation of this file.
2#if defined(USE_ESP8266) && defined(USE_ARDUINO) && defined(USE_MDNS)
3
4#include <ESP8266mDNS.h>
8#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
10#include "mdns_component.h"
11// wifi_component.h is pulled in transitively by mdns_component.h when
12// USE_MDNS_WIFI_LISTENER is defined.
13
14namespace esphome::mdns {
15
16static void register_esp8266(MDNSComponent *, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
17 MDNS.begin(App.get_name().c_str());
18
19 for (const auto &service : services) {
20 // Strip the leading underscore from the proto and service_type. While it is
21 // part of the wire protocol to have an underscore, and for example ESP-IDF
22 // expects the underscore to be there, the ESP8266 implementation always adds
23 // the underscore itself.
24 auto *proto = MDNS_STR_ARG(service.proto);
25 while (progmem_read_byte((const uint8_t *) proto) == '_') {
26 proto++;
27 }
28 auto *service_type = MDNS_STR_ARG(service.service_type);
29 while (progmem_read_byte((const uint8_t *) service_type) == '_') {
30 service_type++;
31 }
32 uint16_t port = service.port.value();
33 MDNS.addService(FPSTR(service_type), FPSTR(proto), port);
34 for (const auto &record : service.txt_records) {
35 MDNS.addServiceTxt(FPSTR(service_type), FPSTR(proto), FPSTR(MDNS_STR_ARG(record.key)),
36 FPSTR(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 this->setup_buffers_and_register_(register_esp8266);
51#ifdef USE_MDNS_WIFI_LISTENER
52 // LEAmDNS's own LwipIntf::statusChangeCB drives _restart() on netif changes; we just
53 // arm the window around the initial probe/announce and each reconnect. Unconditional
54 // here is safe: setup_priority::AFTER_CONNECTION guarantees the network is up.
57#endif
58}
59
60#ifdef USE_MDNS_WIFI_LISTENER
62 const network::IPAddress &) {
63 // IP listener only fires on acquisition (not loss), so any notification is a fresh
64 // IP worth re-arming for. start_polling_window_() is idempotent.
65 if (ips[0].is_set()) {
67 }
68}
69#endif
70
72 MDNS.close();
73 delay(10);
74}
75
76} // namespace esphome::mdns
77
78#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
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.
std::array< IPAddress, 5 > IPAddresses
Definition ip_address.h:223
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.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:43