[core] Add ESPHOME_strlen_P to the progmem helpers (#19574)

This commit is contained in:
J. Nick Koston
2026-09-24 10:03:33 +01:00
committed by GitHub
parent e1126a152e
commit 698eb71862
2 changed files with 6 additions and 16 deletions
+4 -16
View File
@@ -2,16 +2,13 @@
#ifdef USE_NOISE
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
#include <algorithm>
#include <cstring>
#include <noise/protocol.h>
#ifdef USE_ESP8266
#include <pgmspace.h>
#endif
namespace esphome::noise {
static const char *const TAG = "noise";
@@ -74,22 +71,13 @@ size_t format_reject_payload(uint8_t *buf, size_t capacity, const LogString *rea
return 0;
}
buf[0] = HANDSHAKE_STATUS_REJECT;
#ifdef USE_STORE_LOG_STR_IN_FLASH
// On ESP8266 with flash strings, we need to use PROGMEM-aware functions
size_t reason_len = strlen_P(reinterpret_cast<PGM_P>(reason));
reason_len = std::min(reason_len, capacity - 1);
if (reason_len > 0) {
memcpy_P(buf + 1, reinterpret_cast<PGM_P>(reason), reason_len);
}
#else
// The reason may live in PROGMEM on ESP8266; the progmem helpers read RAM and flash alike
const char *reason_str = LOG_STR_ARG(reason);
size_t reason_len = strlen(reason_str);
reason_len = std::min(reason_len, capacity - 1);
size_t reason_len = std::min(ESPHOME_strlen_P(reason_str), capacity - 1);
if (reason_len > 0) {
// NOLINTNEXTLINE(bugprone-not-null-terminated-result) - binary protocol, not a C string
std::memcpy(buf + 1, reason_str, reason_len);
progmem_memcpy(buf + 1, reason_str, reason_len);
}
#endif
return reason_len + 1;
}
+2
View File
@@ -24,6 +24,7 @@
#define ESPHOME_strcasecmp_P strcasecmp_P
#define ESPHOME_strncmp_P strncmp_P
#define ESPHOME_strncasecmp_P strncasecmp_P
#define ESPHOME_strlen_P strlen_P
// Type for pointers to PROGMEM strings (for use with ESPHOME_F return values)
using ProgmemStr = const __FlashStringHelper *;
// Storage class for PROGMEM_STRING_TABLE data. Mirrors the logger's choice of
@@ -47,6 +48,7 @@ using ProgmemStr = const __FlashStringHelper *;
#define ESPHOME_strcasecmp_P strcasecmp
#define ESPHOME_strncmp_P strncmp
#define ESPHOME_strncasecmp_P strncasecmp
#define ESPHOME_strlen_P strlen
// Type for pointers to strings (no PROGMEM on non-ESP8266 platforms)
using ProgmemStr = const char *;
// No-op on non-ESP8266 platforms where PROGMEM itself is a no-op.