mirror of
https://github.com/esphome/esphome.git
synced 2025-10-13 07:13:47 +01:00
keep all 8266 in flash
This commit is contained in:
@@ -9,24 +9,20 @@
|
|||||||
#include <pgmspace.h>
|
#include <pgmspace.h>
|
||||||
// Macro to define strings in PROGMEM on ESP8266, regular memory on other platforms
|
// Macro to define strings in PROGMEM on ESP8266, regular memory on other platforms
|
||||||
#define MDNS_STATIC_CONST_CHAR(name, value) static const char name[] PROGMEM = value
|
#define MDNS_STATIC_CONST_CHAR(name, value) static const char name[] PROGMEM = value
|
||||||
// Helper to get string from PROGMEM - returns a temporary std::string
|
#define MDNS_STR(name) (reinterpret_cast<const MDNSString *>(name))
|
||||||
// Only define this function if we have services that will use it
|
// Helper to convert PROGMEM string to std::string for TemplatableValue
|
||||||
#if defined(USE_API) || defined(USE_PROMETHEUS) || defined(USE_WEBSERVER) || defined(USE_MDNS_EXTRA_SERVICES)
|
static std::string mdns_str_value(PGM_P str) {
|
||||||
static std::string mdns_string_p(const char *src) {
|
|
||||||
char buf[64];
|
char buf[64];
|
||||||
strncpy_P(buf, src, sizeof(buf) - 1);
|
strncpy_P(buf, str, sizeof(buf) - 1);
|
||||||
buf[sizeof(buf) - 1] = '\0';
|
buf[sizeof(buf) - 1] = '\0';
|
||||||
return std::string(buf);
|
return std::string(buf);
|
||||||
}
|
}
|
||||||
#define MDNS_STR(name) mdns_string_p(name)
|
#define MDNS_STR_VALUE(name) mdns_str_value(name)
|
||||||
#else
|
|
||||||
// If no services are configured, we still need the fallback service but it uses string literals
|
|
||||||
#define MDNS_STR(name) std::string(name)
|
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
// On non-ESP8266 platforms, use regular const char*
|
// On non-ESP8266 platforms, use regular const char*
|
||||||
#define MDNS_STATIC_CONST_CHAR(name, value) static constexpr const char *name = value
|
#define MDNS_STATIC_CONST_CHAR(name, value) static constexpr const char name[] = value
|
||||||
#define MDNS_STR(name) name
|
#define MDNS_STR(name) (reinterpret_cast<const MDNSString *>(name))
|
||||||
|
#define MDNS_STR_VALUE(name) std::string(name)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_API
|
#ifdef USE_API
|
||||||
@@ -118,11 +114,11 @@ void MDNSComponent::compile_records_() {
|
|||||||
txt_records.push_back({MDNS_STR(TXT_MAC), get_mac_address()});
|
txt_records.push_back({MDNS_STR(TXT_MAC), get_mac_address()});
|
||||||
|
|
||||||
#ifdef USE_ESP8266
|
#ifdef USE_ESP8266
|
||||||
txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP8266)});
|
txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR_VALUE(PLATFORM_ESP8266)});
|
||||||
#elif defined(USE_ESP32)
|
#elif defined(USE_ESP32)
|
||||||
txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_ESP32)});
|
txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR_VALUE(PLATFORM_ESP32)});
|
||||||
#elif defined(USE_RP2040)
|
#elif defined(USE_RP2040)
|
||||||
txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR(PLATFORM_RP2040)});
|
txt_records.push_back({MDNS_STR(TXT_PLATFORM), MDNS_STR_VALUE(PLATFORM_RP2040)});
|
||||||
#elif defined(USE_LIBRETINY)
|
#elif defined(USE_LIBRETINY)
|
||||||
txt_records.push_back({MDNS_STR(TXT_PLATFORM), lt_cpu_get_model_name()});
|
txt_records.push_back({MDNS_STR(TXT_PLATFORM), lt_cpu_get_model_name()});
|
||||||
#endif
|
#endif
|
||||||
@@ -130,19 +126,19 @@ void MDNSComponent::compile_records_() {
|
|||||||
txt_records.push_back({MDNS_STR(TXT_BOARD), ESPHOME_BOARD});
|
txt_records.push_back({MDNS_STR(TXT_BOARD), ESPHOME_BOARD});
|
||||||
|
|
||||||
#if defined(USE_WIFI)
|
#if defined(USE_WIFI)
|
||||||
txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_WIFI)});
|
txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR_VALUE(NETWORK_WIFI)});
|
||||||
#elif defined(USE_ETHERNET)
|
#elif defined(USE_ETHERNET)
|
||||||
txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_ETHERNET)});
|
txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR_VALUE(NETWORK_ETHERNET)});
|
||||||
#elif defined(USE_OPENTHREAD)
|
#elif defined(USE_OPENTHREAD)
|
||||||
txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR(NETWORK_THREAD)});
|
txt_records.push_back({MDNS_STR(TXT_NETWORK), MDNS_STR_VALUE(NETWORK_THREAD)});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256");
|
MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256");
|
||||||
if (api::global_api_server->get_noise_ctx()->has_psk()) {
|
if (api::global_api_server->get_noise_ctx()->has_psk()) {
|
||||||
txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION), MDNS_STR(NOISE_ENCRYPTION)});
|
txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION), MDNS_STR_VALUE(NOISE_ENCRYPTION)});
|
||||||
} else {
|
} else {
|
||||||
txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION_SUPPORTED), MDNS_STR(NOISE_ENCRYPTION)});
|
txt_records.push_back({MDNS_STR(TXT_API_ENCRYPTION_SUPPORTED), MDNS_STR_VALUE(NOISE_ENCRYPTION)});
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -190,10 +186,10 @@ void MDNSComponent::dump_config() {
|
|||||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||||
ESP_LOGV(TAG, " Services:");
|
ESP_LOGV(TAG, " Services:");
|
||||||
for (const auto &service : this->services_) {
|
for (const auto &service : this->services_) {
|
||||||
ESP_LOGV(TAG, " - %s, %s, %d", service.service_type, service.proto,
|
ESP_LOGV(TAG, " - %s, %s, %d", MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto),
|
||||||
const_cast<TemplatableValue<uint16_t> &>(service.port).value());
|
const_cast<TemplatableValue<uint16_t> &>(service.port).value());
|
||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
ESP_LOGV(TAG, " TXT: %s = %s", record.key,
|
ESP_LOGV(TAG, " TXT: %s = %s", MDNS_STR_ARG(record.key),
|
||||||
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -9,21 +9,31 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace mdns {
|
namespace mdns {
|
||||||
|
|
||||||
|
// Helper struct that identifies strings that may be stored in flash storage (similar to LogString)
|
||||||
|
struct MDNSString;
|
||||||
|
|
||||||
|
#ifdef USE_ESP8266
|
||||||
|
#include <pgmspace.h>
|
||||||
|
#define MDNS_STR_ARG(s) ((PGM_P) (s))
|
||||||
|
#else
|
||||||
|
#define MDNS_STR_ARG(s) (reinterpret_cast<const char *>(s))
|
||||||
|
#endif
|
||||||
|
|
||||||
// Service count is calculated at compile time by Python codegen
|
// Service count is calculated at compile time by Python codegen
|
||||||
// MDNS_SERVICE_COUNT will always be defined
|
// MDNS_SERVICE_COUNT will always be defined
|
||||||
|
|
||||||
struct MDNSTXTRecord {
|
struct MDNSTXTRecord {
|
||||||
const char *key;
|
const MDNSString *key;
|
||||||
TemplatableValue<std::string> value;
|
TemplatableValue<std::string> value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MDNSService {
|
struct MDNSService {
|
||||||
// service name _including_ underscore character prefix
|
// service name _including_ underscore character prefix
|
||||||
// as defined in RFC6763 Section 7
|
// as defined in RFC6763 Section 7
|
||||||
const char *service_type;
|
const MDNSString *service_type;
|
||||||
// second label indicating protocol _including_ underscore character prefix
|
// second label indicating protocol _including_ underscore character prefix
|
||||||
// as defined in RFC6763 Section 7, like "_tcp" or "_udp"
|
// as defined in RFC6763 Section 7, like "_tcp" or "_udp"
|
||||||
const char *proto;
|
const MDNSString *proto;
|
||||||
TemplatableValue<uint16_t> port;
|
TemplatableValue<uint16_t> port;
|
||||||
std::vector<MDNSTXTRecord> txt_records;
|
std::vector<MDNSTXTRecord> txt_records;
|
||||||
};
|
};
|
||||||
|
@@ -30,13 +30,14 @@ void MDNSComponent::setup() {
|
|||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
mdns_txt_item_t it{};
|
mdns_txt_item_t it{};
|
||||||
// key is a compile-time string literal in flash, no need to strdup
|
// key is a compile-time string literal in flash, no need to strdup
|
||||||
it.key = record.key;
|
it.key = MDNS_STR_ARG(record.key);
|
||||||
// value is a temporary from TemplatableValue, must strdup to keep it alive
|
// value is a temporary from TemplatableValue, must strdup to keep it alive
|
||||||
it.value = strdup(const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
it.value = strdup(const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
||||||
txt_records.push_back(it);
|
txt_records.push_back(it);
|
||||||
}
|
}
|
||||||
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||||
err = mdns_service_add(nullptr, service.service_type, service.proto, port, txt_records.data(), txt_records.size());
|
err = mdns_service_add(nullptr, MDNS_STR_ARG(service.service_type), MDNS_STR_ARG(service.proto), port,
|
||||||
|
txt_records.data(), txt_records.size());
|
||||||
|
|
||||||
// free records
|
// free records
|
||||||
for (const auto &it : txt_records) {
|
for (const auto &it : txt_records) {
|
||||||
|
@@ -21,18 +21,18 @@ void MDNSComponent::setup() {
|
|||||||
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||||
// expects the underscore to be there, the ESP8266 implementation always adds
|
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||||
// the underscore itself.
|
// the underscore itself.
|
||||||
auto *proto = service.proto;
|
auto *proto = MDNS_STR_ARG(service.proto);
|
||||||
while (*proto == '_') {
|
while (*proto == '_') {
|
||||||
proto++;
|
proto++;
|
||||||
}
|
}
|
||||||
auto *service_type = service.service_type;
|
auto *service_type = MDNS_STR_ARG(service.service_type);
|
||||||
while (*service_type == '_') {
|
while (*service_type == '_') {
|
||||||
service_type++;
|
service_type++;
|
||||||
}
|
}
|
||||||
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||||
MDNS.addService(service_type, proto, port);
|
MDNS.addService(service_type, proto, port);
|
||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
MDNS.addServiceTxt(service_type, proto, record.key,
|
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key),
|
||||||
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -21,18 +21,18 @@ void MDNSComponent::setup() {
|
|||||||
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||||
// expects the underscore to be there, the ESP8266 implementation always adds
|
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||||
// the underscore itself.
|
// the underscore itself.
|
||||||
auto *proto = service.proto;
|
auto *proto = MDNS_STR_ARG(service.proto);
|
||||||
while (*proto == '_') {
|
while (*proto == '_') {
|
||||||
proto++;
|
proto++;
|
||||||
}
|
}
|
||||||
auto *service_type = service.service_type;
|
auto *service_type = MDNS_STR_ARG(service.service_type);
|
||||||
while (*service_type == '_') {
|
while (*service_type == '_') {
|
||||||
service_type++;
|
service_type++;
|
||||||
}
|
}
|
||||||
uint16_t port_ = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
uint16_t port_ = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||||
MDNS.addService(service_type, proto, port_);
|
MDNS.addService(service_type, proto, port_);
|
||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
MDNS.addServiceTxt(service_type, proto, record.key,
|
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key),
|
||||||
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -21,18 +21,18 @@ void MDNSComponent::setup() {
|
|||||||
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||||
// expects the underscore to be there, the ESP8266 implementation always adds
|
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||||
// the underscore itself.
|
// the underscore itself.
|
||||||
auto *proto = service.proto;
|
auto *proto = MDNS_STR_ARG(service.proto);
|
||||||
while (*proto == '_') {
|
while (*proto == '_') {
|
||||||
proto++;
|
proto++;
|
||||||
}
|
}
|
||||||
auto *service_type = service.service_type;
|
auto *service_type = MDNS_STR_ARG(service.service_type);
|
||||||
while (*service_type == '_') {
|
while (*service_type == '_') {
|
||||||
service_type++;
|
service_type++;
|
||||||
}
|
}
|
||||||
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
uint16_t port = const_cast<TemplatableValue<uint16_t> &>(service.port).value();
|
||||||
MDNS.addService(service_type, proto, port);
|
MDNS.addService(service_type, proto, port);
|
||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
MDNS.addServiceTxt(service_type, proto, record.key,
|
MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key),
|
||||||
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -155,7 +155,7 @@ void OpenThreadSrpComponent::setup() {
|
|||||||
|
|
||||||
// Set service name
|
// Set service name
|
||||||
char *string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
|
char *string = otSrpClientBuffersGetServiceEntryServiceNameString(entry, &size);
|
||||||
std::string full_service = service.service_type + "." + service.proto;
|
std::string full_service = std::string(MDNS_STR_ARG(service.service_type)) + "." + MDNS_STR_ARG(service.proto);
|
||||||
if (full_service.size() > size) {
|
if (full_service.size() > size) {
|
||||||
ESP_LOGW(TAG, "Service name too long: %s", full_service.c_str());
|
ESP_LOGW(TAG, "Service name too long: %s", full_service.c_str());
|
||||||
continue;
|
continue;
|
||||||
@@ -181,7 +181,7 @@ void OpenThreadSrpComponent::setup() {
|
|||||||
for (size_t i = 0; i < service.txt_records.size(); i++) {
|
for (size_t i = 0; i < service.txt_records.size(); i++) {
|
||||||
const auto &txt = service.txt_records[i];
|
const auto &txt = service.txt_records[i];
|
||||||
auto value = const_cast<TemplatableValue<std::string> &>(txt.value).value();
|
auto value = const_cast<TemplatableValue<std::string> &>(txt.value).value();
|
||||||
txt_entries[i].mKey = txt.key; // Compile-time string literal in flash
|
txt_entries[i].mKey = MDNS_STR_ARG(txt.key);
|
||||||
txt_entries[i].mValue = reinterpret_cast<const uint8_t *>(strdup(value.c_str()));
|
txt_entries[i].mValue = reinterpret_cast<const uint8_t *>(strdup(value.c_str()));
|
||||||
txt_entries[i].mValueLength = value.size();
|
txt_entries[i].mValueLength = value.size();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user