ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
progmem.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <limits>
8#include <new>
9#include <string>
10#include <type_traits>
11
12#include "esphome/core/hal.h" // For PROGMEM definition
13
14// Platform-agnostic macros for PROGMEM string handling
15// On ESP8266/Arduino: Use Arduino's F() macro for PROGMEM strings
16// On other platforms: Use plain strings (no PROGMEM)
17
18#ifdef USE_ESP8266
19// ESP8266 uses Arduino macros
20#define ESPHOME_F(string_literal) F(string_literal)
21#define ESPHOME_PGM_P PGM_P
22#define ESPHOME_PSTR(s) PSTR(s)
23#define ESPHOME_strncpy_P strncpy_P
24#define ESPHOME_strncat_P strncat_P
25#define ESPHOME_snprintf_P snprintf_P
26#define ESPHOME_strcmp_P strcmp_P
27#define ESPHOME_strcasecmp_P strcasecmp_P
28#define ESPHOME_strncmp_P strncmp_P
29#define ESPHOME_strncasecmp_P strncasecmp_P
30#define ESPHOME_strlen_P strlen_P
31#define ESPHOME_strstr_P strstr_P
32// Type for pointers to PROGMEM strings (for use with ESPHOME_F return values)
33using ProgmemStr = const __FlashStringHelper *;
34// Storage class for PROGMEM_STRING_TABLE data; LOG_STR_ARG treats a LogString as PGM_P
35// on ESP8266, so the table data must be in flash to match.
36#define ESPHOME_PROGMEM_STRING_TABLE_STORAGE PROGMEM
37#else
38#define ESPHOME_F(string_literal) (string_literal)
39#define ESPHOME_PGM_P const char *
40#define ESPHOME_PSTR(s) (s)
41#define ESPHOME_strncpy_P strncpy
42#define ESPHOME_strncat_P strncat
43#define ESPHOME_snprintf_P snprintf
44#define ESPHOME_strcmp_P strcmp
45#define ESPHOME_strcasecmp_P strcasecmp
46#define ESPHOME_strncmp_P strncmp
47#define ESPHOME_strncasecmp_P strncasecmp
48#define ESPHOME_strlen_P strlen
49#define ESPHOME_strstr_P strstr
50// Type for pointers to strings (no PROGMEM on non-ESP8266 platforms)
51using ProgmemStr = const char *;
52// No-op on non-ESP8266 platforms where PROGMEM itself is a no-op.
53#define ESPHOME_PROGMEM_STRING_TABLE_STORAGE
54#endif
55
56namespace esphome {
57
59#ifdef USE_ESP8266
60std::string progmem_string(ProgmemStr str);
61#else
62inline std::string progmem_string(ProgmemStr str) { return std::string(str); }
63#endif
64
66template<size_t N> struct FixedString {
67 char data[N]{};
68 constexpr FixedString(const char (&str)[N]) {
69 for (size_t i = 0; i < N; ++i)
70 data[i] = str[i];
71 }
72 constexpr size_t size() const { return N - 1; } // exclude null terminator
73};
74
84template<FixedString... Strs> struct ProgmemStringTable {
85 static constexpr size_t COUNT = sizeof...(Strs);
86 static constexpr size_t BLOB_SIZE = (0 + ... + (Strs.size() + 1));
88 static constexpr size_t MAX_LENGTH = std::max({Strs.size()...});
91 static constexpr bool INDEXED = BLOB_SIZE <= UINT8_MAX;
92
94 static constexpr auto make_blob() {
95 std::array<char, BLOB_SIZE> result{};
96 size_t pos = 0;
97 auto copy = [&](const auto &str) {
98 for (size_t i = 0; i <= str.size(); ++i)
99 result[pos++] = str.data[i];
100 };
101 (copy(Strs), ...);
102 return result;
103 }
104
106 static constexpr auto make_offsets() {
107 static_assert(COUNT > 0, "PROGMEM_STRING_TABLE must contain at least one string");
108 static_assert(COUNT <= 255, "PROGMEM_STRING_TABLE supports at most 255 strings with uint8_t indices");
109 if constexpr (INDEXED) {
110 std::array<uint8_t, COUNT> result{};
111 size_t pos = 0, idx = 0;
112 ((result[idx++] = static_cast<uint8_t>(pos), pos += Strs.size() + 1), ...);
113 return result;
114 } else {
115 return std::array<uint8_t, 0>{};
116 }
117 }
118};
119
121inline const char *progmem_skip_strings(const char *blob, uint8_t idx) {
122 while (idx-- > 0)
123 blob += ESPHOME_strlen_P(blob) + 1;
124 return blob;
125}
126
127namespace progmem_detail {
130template<typename D = size_t, typename T> constexpr D table_index(T value) {
131 if constexpr (std::is_enum_v<T>) {
132 return table_index<D>(static_cast<std::underlying_type_t<T>>(value));
133 } else {
134 static_assert(std::is_integral_v<T> && !std::is_same_v<T, bool>, "index must be an integer or an enum");
135 if constexpr (sizeof(T) > sizeof(D)) {
136 const auto v = static_cast<std::make_unsigned_t<T>>(value);
137 return v > std::numeric_limits<D>::max() ? std::numeric_limits<D>::max() : static_cast<D>(v);
138 } else if constexpr (std::is_signed_v<T>) {
139 return static_cast<D>(static_cast<std::make_signed_t<D>>(value)); // sign-extend first
140 } else {
141 return value;
142 }
143 }
144}
145} // namespace progmem_detail
146
147// Forward declaration for LogString (defined in log.h)
148struct LogString;
149
154#define PROGMEM_STRING_TABLE(Name, ...) \
155 struct Name { \
156 using Table = ::esphome::ProgmemStringTable<__VA_ARGS__>; \
157 static constexpr size_t COUNT = Table::COUNT; \
158 static constexpr uint8_t LAST_INDEX = COUNT - 1; \
159 static constexpr size_t BLOB_SIZE = Table::BLOB_SIZE; \
160 static constexpr size_t MAX_LENGTH = Table::MAX_LENGTH; \
161 static constexpr auto BLOB ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_blob(); \
162 static constexpr auto OFFSETS ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_offsets(); \
163 static const char *get_(size_t idx, uint8_t fallback) { \
164 if (idx >= COUNT) \
165 idx = fallback; \
166 /* std::launder is used here to prevent the inter-procedural analysis that */ \
167 /* causes the false positive that the string is not null terminated */ \
168 if constexpr (Table::INDEXED) \
169 return std::launder(&BLOB[::esphome::progmem_read_byte(&OFFSETS[idx])]); \
170 else \
171 return std::launder(::esphome::progmem_skip_strings(BLOB.data(), static_cast<uint8_t>(idx))); \
172 } \
173 template<typename T, typename F = uint8_t> static ::ProgmemStr get_progmem_str(T idx, F fallback = LAST_INDEX) { \
174 return reinterpret_cast<::ProgmemStr>( \
175 get_(::esphome::progmem_detail::table_index(idx), \
176 static_cast<uint8_t>(::esphome::progmem_detail::table_index(fallback)))); \
177 } \
178 template<typename T, typename F = uint8_t> \
179 static const ::esphome::LogString *get_log_str(T idx, F fallback = LAST_INDEX) { \
180 return reinterpret_cast<const ::esphome::LogString *>( \
181 get_(::esphome::progmem_detail::table_index(idx), \
182 static_cast<uint8_t>(::esphome::progmem_detail::table_index(fallback)))); \
183 } \
184 }
185
188template<typename Table> int16_t progmem_string_table_find(const char *value, size_t len) {
189 const char *entry = Table::BLOB.data();
190 for (uint8_t i = 0; i < Table::COUNT; i++) {
191 const size_t entry_len = ESPHOME_strlen_P(entry);
192 if (entry_len == len && ESPHOME_strncmp_P(value, entry, len) == 0) {
193 return i;
194 }
195 entry += entry_len + 1;
196 }
197 return -1;
198}
199
200} // namespace esphome
constexpr D table_index(T value)
Widen an enum or integer to a table index of type D; negative and out-of-range values become huge one...
Definition progmem.h:130
std::string progmem_string(ProgmemStr str)
Copies a string stored with ESPHOME_F into a std::string.
Definition helpers.cpp:24
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
int16_t progmem_string_table_find(const char *value, size_t len)
The index of the string value (len bytes, in RAM) in a PROGMEM_STRING_TABLE, or -1 when it is not one...
Definition progmem.h:188
const char * progmem_skip_strings(const char *blob, uint8_t idx)
Skip idx strings in a packed NUL-separated blob that may live in flash.
Definition progmem.h:121
size_t size_t pos
Definition helpers.h:1530
const __FlashStringHelper * ProgmemStr
Definition progmem.h:33
Helper for C++20 string literal template arguments.
Definition progmem.h:66
constexpr size_t size() const
Definition progmem.h:72
constexpr FixedString(const char(&str)[N])
Definition progmem.h:68
Compile-time string table that packs strings into a single blob with offset lookup.
Definition progmem.h:84
static constexpr size_t COUNT
Definition progmem.h:85
static constexpr size_t MAX_LENGTH
Length of the longest string, for sizing a RAM copy on ESP8266.
Definition progmem.h:88
static constexpr size_t BLOB_SIZE
Definition progmem.h:86
static constexpr auto make_blob()
Generate packed string blob at compile time.
Definition progmem.h:94
static constexpr bool INDEXED
One-byte offsets address up to 255 bytes; a larger blob has no offset table and is scanned instead,...
Definition progmem.h:91
static constexpr auto make_offsets()
Generate offset table at compile time.
Definition progmem.h:106
esp_err_t result