ESPHome 2025.12.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
12namespace esphome {
13namespace mdns {
14
16#ifdef USE_MDNS_STORE_SERVICES
17 this->compile_records_(this->services_);
18 const auto &services = this->services_;
19#else
20 StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;
21 this->compile_records_(services);
22#endif
23
24 MDNS.begin(App.get_name().c_str());
25
26 for (const auto &service : services) {
27 // Strip the leading underscore from the proto and service_type. While it is
28 // part of the wire protocol to have an underscore, and for example ESP-IDF
29 // expects the underscore to be there, the ESP8266 implementation always adds
30 // the underscore itself.
31 auto *proto = MDNS_STR_ARG(service.proto);
32 while (progmem_read_byte((const uint8_t *) proto) == '_') {
33 proto++;
34 }
35 auto *service_type = MDNS_STR_ARG(service.service_type);
36 while (progmem_read_byte((const uint8_t *) service_type) == '_') {
37 service_type++;
38 }
39 uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
40 MDNS.addService(FPSTR(service_type), FPSTR(proto), port);
41 for (const auto &record : service.txt_records) {
42 MDNS.addServiceTxt(FPSTR(service_type), FPSTR(proto), FPSTR(MDNS_STR_ARG(record.key)),
43 FPSTR(MDNS_STR_ARG(record.value)));
44 }
45 }
46}
47
48void MDNSComponent::loop() { MDNS.update(); }
49
51 MDNS.close();
52 delay(10);
53}
54
55} // namespace mdns
56} // namespace esphome
57
58#endif
const std::string & get_name() const
Get the name of this Application set by pre_setup().
void compile_records_(StaticVector< MDNSService, MDNS_SERVICE_COUNT > &services)
StaticVector< MDNSService, MDNS_SERVICE_COUNT > services_
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:31
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:70