ESPHome 2025.12.4
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// Define all constant strings using the macro
33MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp");
34
35// Wrap build-time defines into flash storage
36MDNS_STATIC_CONST_CHAR(VALUE_VERSION, ESPHOME_VERSION);
37
39 // IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES
40 // in mdns/__init__.py. If you add a new service here, update both locations.
41
42#ifdef USE_API
43 MDNS_STATIC_CONST_CHAR(SERVICE_ESPHOMELIB, "_esphomelib");
44 MDNS_STATIC_CONST_CHAR(TXT_FRIENDLY_NAME, "friendly_name");
45 MDNS_STATIC_CONST_CHAR(TXT_VERSION, "version");
46 MDNS_STATIC_CONST_CHAR(TXT_MAC, "mac");
47 MDNS_STATIC_CONST_CHAR(TXT_PLATFORM, "platform");
48 MDNS_STATIC_CONST_CHAR(TXT_BOARD, "board");
49 MDNS_STATIC_CONST_CHAR(TXT_NETWORK, "network");
50 MDNS_STATIC_CONST_CHAR(VALUE_BOARD, ESPHOME_BOARD);
51
52 if (api::global_api_server != nullptr) {
53 auto &service = services.emplace_next();
54 service.service_type = MDNS_STR(SERVICE_ESPHOMELIB);
55 service.proto = MDNS_STR(SERVICE_TCP);
56 service.port = api::global_api_server->get_port();
57
58 const std::string &friendly_name = App.get_friendly_name();
59 bool friendly_name_empty = friendly_name.empty();
60
61 // Calculate exact capacity for txt_records
62 size_t txt_count = 3; // version, mac, board (always present)
63 if (!friendly_name_empty) {
64 txt_count++; // friendly_name
65 }
66#if defined(USE_ESP8266) || defined(USE_ESP32) || defined(USE_RP2040) || defined(USE_LIBRETINY)
67 txt_count++; // platform
68#endif
69#if defined(USE_WIFI) || defined(USE_ETHERNET) || defined(USE_OPENTHREAD)
70 txt_count++; // network
71#endif
72#ifdef USE_API_NOISE
73 txt_count++; // api_encryption or api_encryption_supported
74#endif
75#ifdef ESPHOME_PROJECT_NAME
76 txt_count += 2; // project_name and project_version
77#endif
78#ifdef USE_DASHBOARD_IMPORT
79 txt_count++; // package_import_url
80#endif
81
82 auto &txt_records = service.txt_records;
83 txt_records.init(txt_count);
84
85 if (!friendly_name_empty) {
86 txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), MDNS_STR(friendly_name.c_str())});
87 }
88 txt_records.push_back({MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)});
89
90 // MAC address: passed from caller (either member buffer or stack buffer depending on USE_MDNS_STORE_SERVICES)
91 txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(mac_address_buf)});
92
93#ifdef USE_ESP8266
94 MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266");
95 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP8266)});
96#elif defined(USE_ESP32)
97 MDNS_STATIC_CONST_CHAR(PLATFORM_ESP32, "ESP32");
98 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP32)});
99#elif defined(USE_RP2040)
100 MDNS_STATIC_CONST_CHAR(PLATFORM_RP2040, "RP2040");
101 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_RP2040)});
102#elif defined(USE_LIBRETINY)
103 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(lt_cpu_get_model_name())});
104#endif
105
106 txt_records.push_back({MDNS_STR(TXT_BOARD), MDNS_STR(VALUE_BOARD)});
107
108#if defined(USE_WIFI)
109 MDNS_STATIC_CONST_CHAR(NETWORK_WIFI, "wifi");
110 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_WIFI)});
111#elif defined(USE_ETHERNET)
112 MDNS_STATIC_CONST_CHAR(NETWORK_ETHERNET, "ethernet");
113 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_ETHERNET)});
114#elif defined(USE_OPENTHREAD)
115 MDNS_STATIC_CONST_CHAR(NETWORK_THREAD, "thread");
116 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_THREAD)});
117#endif
118
119#ifdef USE_API_NOISE
120 MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION, "api_encryption");
121 MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION_SUPPORTED, "api_encryption_supported");
122 MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256");
123 bool has_psk = api::global_api_server->get_noise_ctx().has_psk();
124 const char *encryption_key = has_psk ? TXT_API_ENCRYPTION : TXT_API_ENCRYPTION_SUPPORTED;
125 txt_records.push_back({MDNS_STR(encryption_key), MDNS_STR(NOISE_ENCRYPTION)});
126#endif
127
128#ifdef ESPHOME_PROJECT_NAME
129 MDNS_STATIC_CONST_CHAR(TXT_PROJECT_NAME, "project_name");
130 MDNS_STATIC_CONST_CHAR(TXT_PROJECT_VERSION, "project_version");
131 MDNS_STATIC_CONST_CHAR(VALUE_PROJECT_NAME, ESPHOME_PROJECT_NAME);
132 MDNS_STATIC_CONST_CHAR(VALUE_PROJECT_VERSION, ESPHOME_PROJECT_VERSION);
133 txt_records.push_back({MDNS_STR(TXT_PROJECT_NAME), MDNS_STR(VALUE_PROJECT_NAME)});
134 txt_records.push_back({MDNS_STR(TXT_PROJECT_VERSION), MDNS_STR(VALUE_PROJECT_VERSION)});
135#endif // ESPHOME_PROJECT_NAME
136
137#ifdef USE_DASHBOARD_IMPORT
138 MDNS_STATIC_CONST_CHAR(TXT_PACKAGE_IMPORT_URL, "package_import_url");
139 txt_records.push_back({MDNS_STR(TXT_PACKAGE_IMPORT_URL), MDNS_STR(dashboard_import::get_package_import_url())});
140#endif
141 }
142#endif // USE_API
143
144#ifdef USE_PROMETHEUS
145 MDNS_STATIC_CONST_CHAR(SERVICE_PROMETHEUS, "_prometheus-http");
146
147 auto &prom_service = services.emplace_next();
148 prom_service.service_type = MDNS_STR(SERVICE_PROMETHEUS);
149 prom_service.proto = MDNS_STR(SERVICE_TCP);
150 prom_service.port = USE_WEBSERVER_PORT;
151#endif
152
153#ifdef USE_WEBSERVER
154 MDNS_STATIC_CONST_CHAR(SERVICE_HTTP, "_http");
155
156 auto &web_service = services.emplace_next();
157 web_service.service_type = MDNS_STR(SERVICE_HTTP);
158 web_service.proto = MDNS_STR(SERVICE_TCP);
159 web_service.port = USE_WEBSERVER_PORT;
160#endif
161
162#if !defined(USE_API) && !defined(USE_PROMETHEUS) && !defined(USE_WEBSERVER) && !defined(USE_MDNS_EXTRA_SERVICES)
163 MDNS_STATIC_CONST_CHAR(SERVICE_HTTP, "_http");
164 MDNS_STATIC_CONST_CHAR(TXT_VERSION, "version");
165
166 // Publish "http" service if not using native API or any other services
167 // This is just to have *some* mDNS service so that .local resolution works
168 auto &fallback_service = services.emplace_next();
169 fallback_service.service_type = MDNS_STR(SERVICE_HTTP);
170 fallback_service.proto = MDNS_STR(SERVICE_TCP);
171 fallback_service.port = USE_WEBSERVER_PORT;
172 fallback_service.txt_records = {{MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)}};
173#endif
174}
175
177 ESP_LOGCONFIG(TAG,
178 "mDNS:\n"
179 " Hostname: %s",
180 App.get_name().c_str());
181#ifdef USE_MDNS_STORE_SERVICES
182 ESP_LOGV(TAG, " Services:");
183 for (const auto &service : this->services_) {
184 ESP_LOGV(TAG, " - %s, %s, %d", MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto),
185 const_cast<TemplatableValue<uint16_t> &>(service.port).value());
186 for (const auto &record : service.txt_records) {
187 ESP_LOGV(TAG, " TXT: %s = %s", MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value));
188 }
189 }
190#endif
191}
192
193} // namespace esphome::mdns
194#endif
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:132
APINoiseContext & get_noise_ctx()
Definition api_server.h:81
uint16_t get_port() const
void compile_records_(StaticVector< MDNSService, MDNS_SERVICE_COUNT > &services, char *mac_address_buf)
StaticVector< MDNSService, MDNS_SERVICE_COUNT > services_
APIServer * global_api_server
const char * get_package_import_url()
MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp")
Application App
Global storage of Application pointer - only one Application can exist.