ESPHome 2025.12.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 {
10namespace mdns {
11
12// Helper struct that identifies strings that may be stored in flash storage (similar to LogString)
13struct MDNSString;
14
15// Macro to cast string literals to MDNSString* (works on all platforms)
16#define MDNS_STR(name) (reinterpret_cast<const esphome::mdns::MDNSString *>(name))
17
18#ifdef USE_ESP8266
19#include <pgmspace.h>
20#define MDNS_STR_ARG(s) ((PGM_P) (s))
21#else
22#define MDNS_STR_ARG(s) (reinterpret_cast<const char *>(s))
23#endif
24
25// Service count is calculated at compile time by Python codegen
26// MDNS_SERVICE_COUNT will always be defined
27
29 const MDNSString *key;
30 const MDNSString *value;
31};
32
34 // service name _including_ underscore character prefix
35 // as defined in RFC6763 Section 7
36 const MDNSString *service_type;
37 // second label indicating protocol _including_ underscore character prefix
38 // as defined in RFC6763 Section 7, like "_tcp" or "_udp"
39 const MDNSString *proto;
42};
43
44class MDNSComponent : public Component {
45 public:
46 void setup() override;
47 void dump_config() override;
48
49#if (defined(USE_ESP8266) || defined(USE_RP2040)) && defined(USE_ARDUINO)
50 void loop() override;
51#endif
52 float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; }
53
54#ifdef USE_MDNS_EXTRA_SERVICES
55 void add_extra_service(MDNSService service) { this->services_.emplace_next() = std::move(service); }
56#endif
57
58#ifdef USE_MDNS_STORE_SERVICES
60#endif
61
62 void on_shutdown() override;
63
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
74
75 protected:
76#ifdef USE_MDNS_STORE_SERVICES
78#endif
80};
81
82} // namespace mdns
83} // namespace esphome
84#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 (MAC address, user lambdas) Pre-sized at compile time via MD...
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)
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:67
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
TemplatableValue< uint16_t > port
FixedVector< MDNSTXTRecord > txt_records
const MDNSString * proto
const MDNSString * service_type