ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
mdns_component.cpp
Go to the documentation of this file.
2#ifdef USE_MDNS
4#include "esphome/core/log.h"
6#include "mdns_component.h"
7
8#ifdef USE_ESP8266
9#include <pgmspace.h>
10// Macro to define strings in PROGMEM on ESP8266, regular memory on other platforms
11#define MDNS_STATIC_CONST_CHAR(name, value) static const char name[] PROGMEM = value
12#else
13// On non-ESP8266 platforms, use regular const char*
14#define MDNS_STATIC_CONST_CHAR(name, value) static constexpr const char name[] = value
15#endif
16
17#ifdef USE_API
19#endif
20#ifdef USE_DASHBOARD_IMPORT
22#endif
23
24namespace esphome::mdns {
25
26static const char *const TAG = "mdns";
27
28#ifndef USE_WEBSERVER_PORT
29#define USE_WEBSERVER_PORT 80 // NOLINT
30#endif
31
32#ifndef USE_SENDSPIN_PORT
33#define USE_SENDSPIN_PORT 8928 // NOLINT
34#endif
35
36// Define all constant strings using the macro
37MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp");
38
39// Wrap build-time defines into flash storage
40MDNS_STATIC_CONST_CHAR(VALUE_VERSION, ESPHOME_VERSION);
41
43#ifdef USE_MDNS_STORE_SERVICES
44 auto &services = this->services_;
45#else
47 auto &services = services_storage;
48#endif
49
50#ifdef USE_API
51#ifdef USE_MDNS_STORE_SERVICES
53 char *mac_ptr = this->mac_address_;
55 char *cfg_ptr = this->config_hash_str_;
56#else
57 char mac_address[MAC_ADDRESS_BUFFER_SIZE];
58 char config_hash_str[CONFIG_HASH_STR_SIZE];
59 get_mac_address_into_buffer(mac_address);
60 format_hex_to(config_hash_str, App.get_config_hash());
61 char *mac_ptr = mac_address;
62 char *cfg_ptr = config_hash_str;
63#endif
64#else
65 char *mac_ptr = nullptr;
66 char *cfg_ptr = nullptr;
67#endif
68
69 this->compile_records_(services, mac_ptr, cfg_ptr);
70 platform_register(this, services);
71}
72
74 char *config_hash_buf) {
75 // IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES
76 // in mdns/__init__.py. If you add a new service here, update both locations.
77
78#ifdef USE_API
79 MDNS_STATIC_CONST_CHAR(SERVICE_ESPHOMELIB, "_esphomelib");
80 MDNS_STATIC_CONST_CHAR(TXT_FRIENDLY_NAME, "friendly_name");
81 MDNS_STATIC_CONST_CHAR(TXT_VERSION, "version");
82 MDNS_STATIC_CONST_CHAR(TXT_CONFIG_HASH, "config_hash");
83 MDNS_STATIC_CONST_CHAR(TXT_MAC, "mac");
84 MDNS_STATIC_CONST_CHAR(TXT_PLATFORM, "platform");
85 MDNS_STATIC_CONST_CHAR(TXT_BOARD, "board");
86 MDNS_STATIC_CONST_CHAR(TXT_NETWORK, "network");
87 MDNS_STATIC_CONST_CHAR(VALUE_BOARD, ESPHOME_BOARD);
88
89 if (api::global_api_server != nullptr) {
90 auto &service = services.emplace_next();
91 service.service_type = MDNS_STR(SERVICE_ESPHOMELIB);
92 service.proto = MDNS_STR(SERVICE_TCP);
93 service.port = []() -> uint16_t { return api::global_api_server->get_port(); };
94
95 const auto &friendly_name = App.get_friendly_name();
96 bool friendly_name_empty = friendly_name.empty();
97
98 // Calculate exact capacity for txt_records
99 size_t txt_count = 4; // version, config_hash, mac, board (always present)
100 if (!friendly_name_empty) {
101 txt_count++; // friendly_name
102 }
103#if defined(USE_ESP8266) || defined(USE_ESP32) || defined(USE_RP2040) || defined(USE_LIBRETINY)
104 txt_count++; // platform
105#endif
106#if defined(USE_WIFI) || defined(USE_ETHERNET) || defined(USE_OPENTHREAD)
107 txt_count++; // network
108#endif
109#ifdef USE_API_NOISE
110 txt_count++; // api_encryption or api_encryption_supported
111#endif
112#ifdef ESPHOME_PROJECT_NAME
113 txt_count += 2; // project_name and project_version
114#endif
115#ifdef USE_DASHBOARD_IMPORT
116 txt_count++; // package_import_url
117#endif
118
119 auto &txt_records = service.txt_records;
120 txt_records.init(txt_count);
121
122 if (!friendly_name_empty) {
123 txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), MDNS_STR(friendly_name.c_str())});
124 }
125 txt_records.push_back({MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)});
126
127 // Config hash: passed from caller (either member buffer or stack buffer depending on USE_MDNS_STORE_SERVICES)
128 txt_records.push_back({MDNS_STR(TXT_CONFIG_HASH), MDNS_STR(config_hash_buf)});
129
130 // MAC address: passed from caller (either member buffer or stack buffer depending on USE_MDNS_STORE_SERVICES)
131 txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(mac_address_buf)});
132
133#ifdef USE_ESP8266
134 MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266");
135 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP8266)});
136#elif defined(USE_ESP32)
137 MDNS_STATIC_CONST_CHAR(PLATFORM_ESP32, "ESP32");
138 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP32)});
139#elif defined(USE_RP2040)
140 MDNS_STATIC_CONST_CHAR(PLATFORM_RP2040, "RP2040");
141 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_RP2040)});
142#elif defined(USE_LIBRETINY)
143 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(lt_cpu_get_model_name())});
144#endif
145
146 txt_records.push_back({MDNS_STR(TXT_BOARD), MDNS_STR(VALUE_BOARD)});
147
148#if defined(USE_WIFI)
149 MDNS_STATIC_CONST_CHAR(NETWORK_WIFI, "wifi");
150 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_WIFI)});
151#elif defined(USE_ETHERNET)
152 MDNS_STATIC_CONST_CHAR(NETWORK_ETHERNET, "ethernet");
153 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_ETHERNET)});
154#elif defined(USE_OPENTHREAD)
155 MDNS_STATIC_CONST_CHAR(NETWORK_THREAD, "thread");
156 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_THREAD)});
157#endif
158
159#ifdef USE_API_NOISE
160 MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION, "api_encryption");
161 MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION_SUPPORTED, "api_encryption_supported");
162 MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256");
163 bool has_psk = api::global_api_server->get_noise_ctx().has_psk();
164 const char *encryption_key = has_psk ? TXT_API_ENCRYPTION : TXT_API_ENCRYPTION_SUPPORTED;
165 txt_records.push_back({MDNS_STR(encryption_key), MDNS_STR(NOISE_ENCRYPTION)});
166#endif
167
168#ifdef ESPHOME_PROJECT_NAME
169 MDNS_STATIC_CONST_CHAR(TXT_PROJECT_NAME, "project_name");
170 MDNS_STATIC_CONST_CHAR(TXT_PROJECT_VERSION, "project_version");
171 MDNS_STATIC_CONST_CHAR(VALUE_PROJECT_NAME, ESPHOME_PROJECT_NAME);
172 MDNS_STATIC_CONST_CHAR(VALUE_PROJECT_VERSION, ESPHOME_PROJECT_VERSION);
173 txt_records.push_back({MDNS_STR(TXT_PROJECT_NAME), MDNS_STR(VALUE_PROJECT_NAME)});
174 txt_records.push_back({MDNS_STR(TXT_PROJECT_VERSION), MDNS_STR(VALUE_PROJECT_VERSION)});
175#endif // ESPHOME_PROJECT_NAME
176
177#ifdef USE_DASHBOARD_IMPORT
178 MDNS_STATIC_CONST_CHAR(TXT_PACKAGE_IMPORT_URL, "package_import_url");
179 txt_records.push_back({MDNS_STR(TXT_PACKAGE_IMPORT_URL), MDNS_STR(dashboard_import::get_package_import_url())});
180#endif
181 }
182#endif // USE_API
183
184#ifdef USE_PROMETHEUS
185 MDNS_STATIC_CONST_CHAR(SERVICE_PROMETHEUS, "_prometheus-http");
186
187 auto &prom_service = services.emplace_next();
188 prom_service.service_type = MDNS_STR(SERVICE_PROMETHEUS);
189 prom_service.proto = MDNS_STR(SERVICE_TCP);
190 prom_service.port = []() -> uint16_t { return USE_WEBSERVER_PORT; };
191#endif
192
193#ifdef USE_SENDSPIN
194 MDNS_STATIC_CONST_CHAR(SERVICE_SENDSPIN, "_sendspin");
195 MDNS_STATIC_CONST_CHAR(TXT_SENDSPIN_PATH, "path");
196 MDNS_STATIC_CONST_CHAR(VALUE_SENDSPIN_PATH, "/sendspin");
197
198 auto &sendspin_service = services.emplace_next();
199 sendspin_service.service_type = MDNS_STR(SERVICE_SENDSPIN);
200 sendspin_service.proto = MDNS_STR(SERVICE_TCP);
201 sendspin_service.port = []() -> uint16_t { return USE_SENDSPIN_PORT; };
202 sendspin_service.txt_records = {{MDNS_STR(TXT_SENDSPIN_PATH), MDNS_STR(VALUE_SENDSPIN_PATH)}};
203#endif
204
205#ifdef USE_WEBSERVER
206 MDNS_STATIC_CONST_CHAR(SERVICE_HTTP, "_http");
207
208 auto &web_service = services.emplace_next();
209 web_service.service_type = MDNS_STR(SERVICE_HTTP);
210 web_service.proto = MDNS_STR(SERVICE_TCP);
211 web_service.port = []() -> uint16_t { return USE_WEBSERVER_PORT; };
212#endif
213
214#if !defined(USE_API) && !defined(USE_PROMETHEUS) && !defined(USE_SENDSPIN) && !defined(USE_WEBSERVER) && \
215 !defined(USE_MDNS_EXTRA_SERVICES)
216 MDNS_STATIC_CONST_CHAR(SERVICE_HTTP, "_http");
217 MDNS_STATIC_CONST_CHAR(TXT_VERSION, "version");
218
219 // Publish "http" service if not using native API or any other services
220 // This is just to have *some* mDNS service so that .local resolution works
221 auto &fallback_service = services.emplace_next();
222 fallback_service.service_type = MDNS_STR(SERVICE_HTTP);
223 fallback_service.proto = MDNS_STR(SERVICE_TCP);
224 fallback_service.port = []() -> uint16_t { return USE_WEBSERVER_PORT; };
225 fallback_service.txt_records = {{MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)}};
226#endif
227}
228
230 ESP_LOGCONFIG(TAG,
231 "mDNS:\n"
232 " Hostname: %s",
233 App.get_name().c_str());
234#ifdef USE_MDNS_STORE_SERVICES
235 ESP_LOGV(TAG, " Services:");
236 for (const auto &service : this->services_) {
237 ESP_LOGV(TAG, " - %s, %s, %d", MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto),
238 service.port.value());
239 for (const auto &record : service.txt_records) {
240 ESP_LOGV(TAG, " TXT: %s = %s", MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
241 }
242 }
243#endif
244}
245
246} // namespace esphome::mdns
247#endif
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
const StringRef & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:217
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr bool empty() const
Definition string_ref.h:76
APINoiseContext & get_noise_ctx()
Definition api_server.h:76
uint16_t get_port() const
char config_hash_str_[CONFIG_HASH_STR_SIZE]
Fixed buffer for config hash hex string (only needed when services are stored)
static constexpr size_t CONFIG_HASH_STR_SIZE
Size of buffer required for config hash hex string (8 hex chars + null terminator)
void setup_buffers_and_register_(PlatformRegisterFn platform_register)
void compile_records_(StaticVector< MDNSService, MDNS_SERVICE_COUNT > &services, char *mac_address_buf, char *config_hash_buf)
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.
StaticVector< MDNSService, MDNS_SERVICE_COUNT > services_
APIServer * global_api_server
const char * get_package_import_url()
MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp")
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:745
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:335
Application App
Global storage of Application pointer - only one Application can exist.