ESPHome 2025.12.0-dev
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 {
25namespace mdns {
26
27static const char *const TAG = "mdns";
28
29#ifndef USE_WEBSERVER_PORT
30#define USE_WEBSERVER_PORT 80 // NOLINT
31#endif
32
33// Define all constant strings using the macro
34MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp");
35
36// Wrap build-time defines into flash storage
37MDNS_STATIC_CONST_CHAR(VALUE_VERSION, ESPHOME_VERSION);
38
40 // IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES
41 // in mdns/__init__.py. If you add a new service here, update both locations.
42
43#ifdef USE_API
44 MDNS_STATIC_CONST_CHAR(SERVICE_ESPHOMELIB, "_esphomelib");
45 MDNS_STATIC_CONST_CHAR(TXT_FRIENDLY_NAME, "friendly_name");
46 MDNS_STATIC_CONST_CHAR(TXT_VERSION, "version");
47 MDNS_STATIC_CONST_CHAR(TXT_MAC, "mac");
48 MDNS_STATIC_CONST_CHAR(TXT_PLATFORM, "platform");
49 MDNS_STATIC_CONST_CHAR(TXT_BOARD, "board");
50 MDNS_STATIC_CONST_CHAR(TXT_NETWORK, "network");
51 MDNS_STATIC_CONST_CHAR(VALUE_BOARD, ESPHOME_BOARD);
52
53 if (api::global_api_server != nullptr) {
54 auto &service = services.emplace_next();
55 service.service_type = MDNS_STR(SERVICE_ESPHOMELIB);
56 service.proto = MDNS_STR(SERVICE_TCP);
57 service.port = api::global_api_server->get_port();
58
59 const std::string &friendly_name = App.get_friendly_name();
60 bool friendly_name_empty = friendly_name.empty();
61
62 // Calculate exact capacity for txt_records
63 size_t txt_count = 3; // version, mac, board (always present)
64 if (!friendly_name_empty) {
65 txt_count++; // friendly_name
66 }
67#if defined(USE_ESP8266) || defined(USE_ESP32) || defined(USE_RP2040) || defined(USE_LIBRETINY)
68 txt_count++; // platform
69#endif
70#if defined(USE_WIFI) || defined(USE_ETHERNET) || defined(USE_OPENTHREAD)
71 txt_count++; // network
72#endif
73#ifdef USE_API_NOISE
74 txt_count++; // api_encryption or api_encryption_supported
75#endif
76#ifdef ESPHOME_PROJECT_NAME
77 txt_count += 2; // project_name and project_version
78#endif
79#ifdef USE_DASHBOARD_IMPORT
80 txt_count++; // package_import_url
81#endif
82
83 auto &txt_records = service.txt_records;
84 txt_records.init(txt_count);
85
86 if (!friendly_name_empty) {
87 txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), MDNS_STR(friendly_name.c_str())});
88 }
89 txt_records.push_back({MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)});
90 txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(this->add_dynamic_txt_value(get_mac_address()))});
91
92#ifdef USE_ESP8266
93 MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266");
94 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP8266)});
95#elif defined(USE_ESP32)
96 MDNS_STATIC_CONST_CHAR(PLATFORM_ESP32, "ESP32");
97 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP32)});
98#elif defined(USE_RP2040)
99 MDNS_STATIC_CONST_CHAR(PLATFORM_RP2040, "RP2040");
100 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_RP2040)});
101#elif defined(USE_LIBRETINY)
102 txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(lt_cpu_get_model_name())});
103#endif
104
105 txt_records.push_back({MDNS_STR(TXT_BOARD), MDNS_STR(VALUE_BOARD)});
106
107#if defined(USE_WIFI)
108 MDNS_STATIC_CONST_CHAR(NETWORK_WIFI, "wifi");
109 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_WIFI)});
110#elif defined(USE_ETHERNET)
111 MDNS_STATIC_CONST_CHAR(NETWORK_ETHERNET, "ethernet");
112 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_ETHERNET)});
113#elif defined(USE_OPENTHREAD)
114 MDNS_STATIC_CONST_CHAR(NETWORK_THREAD, "thread");
115 txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_THREAD)});
116#endif
117
118#ifdef USE_API_NOISE
119 MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION, "api_encryption");
120 MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION_SUPPORTED, "api_encryption_supported");
121 MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256");
122 bool has_psk = api::global_api_server->get_noise_ctx()->has_psk();
123 const char *encryption_key = has_psk ? TXT_API_ENCRYPTION : TXT_API_ENCRYPTION_SUPPORTED;
124 txt_records.push_back({MDNS_STR(encryption_key), MDNS_STR(NOISE_ENCRYPTION)});
125#endif
126
127#ifdef ESPHOME_PROJECT_NAME
128 MDNS_STATIC_CONST_CHAR(TXT_PROJECT_NAME, "project_name");
129 MDNS_STATIC_CONST_CHAR(TXT_PROJECT_VERSION, "project_version");
130 MDNS_STATIC_CONST_CHAR(VALUE_PROJECT_NAME, ESPHOME_PROJECT_NAME);
131 MDNS_STATIC_CONST_CHAR(VALUE_PROJECT_VERSION, ESPHOME_PROJECT_VERSION);
132 txt_records.push_back({MDNS_STR(TXT_PROJECT_NAME), MDNS_STR(VALUE_PROJECT_NAME)});
133 txt_records.push_back({MDNS_STR(TXT_PROJECT_VERSION), MDNS_STR(VALUE_PROJECT_VERSION)});
134#endif // ESPHOME_PROJECT_NAME
135
136#ifdef USE_DASHBOARD_IMPORT
137 MDNS_STATIC_CONST_CHAR(TXT_PACKAGE_IMPORT_URL, "package_import_url");
138 txt_records.push_back(
139 {MDNS_STR(TXT_PACKAGE_IMPORT_URL), MDNS_STR(dashboard_import::get_package_import_url().c_str())});
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 mdns
194} // namespace esphome
195#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
std::shared_ptr< APINoiseContext > get_noise_ctx()
Definition api_server.h:58
uint16_t get_port() const
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)
StaticVector< MDNSService, MDNS_SERVICE_COUNT > services_
APIServer * global_api_server
const std::string & get_package_import_url()
MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp")
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition helpers.cpp:632
Application App
Global storage of Application pointer - only one Application can exist.