ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
mdns_component.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_MDNS
4#include <string>
8
9namespace esphome::mdns {
10
11// Helper struct that identifies strings that may be stored in flash storage (similar to LogString)
12struct MDNSString;
13
14// Macro to cast string literals to MDNSString* (works on all platforms)
15#define MDNS_STR(name) (reinterpret_cast<const esphome::mdns::MDNSString *>(name))
16
17#ifdef USE_ESP8266
18#include <pgmspace.h>
19#define MDNS_STR_ARG(s) ((PGM_P) (s))
20#else
21#define MDNS_STR_ARG(s) (reinterpret_cast<const char *>(s))
22#endif
23
24// Service count is calculated at compile time by Python codegen
25// MDNS_SERVICE_COUNT will always be defined
26
28 const MDNSString *key;
29 const MDNSString *value;
30};
31
33 // service name _including_ underscore character prefix
34 // as defined in RFC6763 Section 7
35 const MDNSString *service_type;
36 // second label indicating protocol _including_ underscore character prefix
37 // as defined in RFC6763 Section 7, like "_tcp" or "_udp"
38 const MDNSString *proto;
41};
42
43class MDNSComponent : public Component {
44 public:
45 void setup() override;
46 void dump_config() override;
47
48#if (defined(USE_ESP8266) || defined(USE_RP2040)) && defined(USE_ARDUINO)
49 void loop() override;
50#endif
51 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
52
53#ifdef USE_MDNS_EXTRA_SERVICES
54 void add_extra_service(MDNSService service) { this->services_.emplace_next() = std::move(service); }
55#endif
56
57#ifdef USE_MDNS_STORE_SERVICES
59#endif
60
61 void on_shutdown() override;
62
63#ifdef USE_MDNS_DYNAMIC_TXT
65 const char *add_dynamic_txt_value(const std::string &value) {
66 this->dynamic_txt_values_.push_back(value);
67 return this->dynamic_txt_values_[this->dynamic_txt_values_.size() - 1].c_str();
68 }
69#endif
70
71 protected:
74
76#ifdef USE_MDNS_STORE_SERVICES
77 auto &services = this->services_;
78#else
80 auto &services = services_storage;
81#endif
82
83#ifdef USE_API
84#ifdef USE_MDNS_STORE_SERVICES
86 char *mac_ptr = this->mac_address_;
87#else
88 char mac_address[MAC_ADDRESS_BUFFER_SIZE];
89 get_mac_address_into_buffer(mac_address);
90 char *mac_ptr = mac_address;
91#endif
92#else
93 char *mac_ptr = nullptr;
94#endif
95
96 this->compile_records_(services, mac_ptr);
97 platform_register(this, services);
98 }
99
100#ifdef USE_MDNS_DYNAMIC_TXT
105#endif
106
107#if defined(USE_API) && defined(USE_MDNS_STORE_SERVICES)
109 char mac_address_[MAC_ADDRESS_BUFFER_SIZE];
110#endif
111#ifdef USE_MDNS_STORE_SERVICES
113#endif
114 void compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services, char *mac_address_buf);
115};
116
117} // namespace esphome::mdns
118#endif
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:184
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:132
size_t size() const
Definition helpers.h:162
void push_back(const T &value)
Definition helpers.h:146
StaticVector< std::string, MDNS_DYNAMIC_TXT_COUNT > dynamic_txt_values_
Storage for runtime-generated TXT values from user lambdas Pre-sized at compile time via MDNS_DYNAMIC...
float get_setup_priority() const override
const char * add_dynamic_txt_value(const std::string &value)
Add a dynamic TXT value and return pointer to it for use in MDNSTXTRecord.
void compile_records_(StaticVector< MDNSService, MDNS_SERVICE_COUNT > &services, char *mac_address_buf)
void setup_buffers_and_register_(PlatformRegisterFn platform_register)
char mac_address_[MAC_ADDRESS_BUFFER_SIZE]
Fixed buffer for MAC address (only needed when services are stored)
void(*)(MDNSComponent *, StaticVector< MDNSService, MDNS_SERVICE_COUNT > &) PlatformRegisterFn
Helper to set up services and MAC buffers, then call platform-specific registration.
void add_extra_service(MDNSService service)
const StaticVector< MDNSService, MDNS_SERVICE_COUNT > & get_services() const
StaticVector< MDNSService, MDNS_SERVICE_COUNT > services_
const float AFTER_CONNECTION
For components that should be initialized after a data connection (API/MQTT) is connected.
Definition component.cpp:89
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
Definition helpers.cpp:704
TemplatableValue< uint16_t > port
FixedVector< MDNSTXTRecord > txt_records
const MDNSString * proto
const MDNSString * service_type