ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
progmem.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <new>
7
8#include "esphome/core/hal.h" // For PROGMEM definition
9
10// Platform-agnostic macros for PROGMEM string handling
11// On ESP8266/Arduino: Use Arduino's F() macro for PROGMEM strings
12// On other platforms: Use plain strings (no PROGMEM)
13
14#ifdef USE_ESP8266
15// ESP8266 uses Arduino macros
16#define ESPHOME_F(string_literal) F(string_literal)
17#define ESPHOME_PGM_P PGM_P
18#define ESPHOME_PSTR(s) PSTR(s)
19#define ESPHOME_strncpy_P strncpy_P
20#define ESPHOME_strncat_P strncat_P
21#define ESPHOME_snprintf_P snprintf_P
22#define ESPHOME_strcmp_P strcmp_P
23#define ESPHOME_strcasecmp_P strcasecmp_P
24#define ESPHOME_strncmp_P strncmp_P
25#define ESPHOME_strncasecmp_P strncasecmp_P
26// Type for pointers to PROGMEM strings (for use with ESPHOME_F return values)
27using ProgmemStr = const __FlashStringHelper *;
28// Storage class for PROGMEM_STRING_TABLE data. Mirrors the logger's choice of
29// LOG_STR_ARG: when LOG_STR_ARG treats the LogString as PROGMEM (PGM_P), the
30// table data must actually be in flash; when LOG_STR_ARG treats it as a plain
31// const char* (assumes RAM), the table data must live in RAM or non-logger
32// consumers (ArduinoJson, Print, MQTT publish) crash on unaligned flash reads.
33#ifdef USE_STORE_LOG_STR_IN_FLASH
34#define ESPHOME_PROGMEM_STRING_TABLE_STORAGE PROGMEM
35#else
36#define ESPHOME_PROGMEM_STRING_TABLE_STORAGE
37#endif
38#else
39#define ESPHOME_F(string_literal) (string_literal)
40#define ESPHOME_PGM_P const char *
41#define ESPHOME_PSTR(s) (s)
42#define ESPHOME_strncpy_P strncpy
43#define ESPHOME_strncat_P strncat
44#define ESPHOME_snprintf_P snprintf
45#define ESPHOME_strcmp_P strcmp
46#define ESPHOME_strcasecmp_P strcasecmp
47#define ESPHOME_strncmp_P strncmp
48#define ESPHOME_strncasecmp_P strncasecmp
49// Type for pointers to strings (no PROGMEM on non-ESP8266 platforms)
50using ProgmemStr = const char *;
51// No-op on non-ESP8266 platforms where PROGMEM itself is a no-op.
52#define ESPHOME_PROGMEM_STRING_TABLE_STORAGE
53#endif
54
55namespace esphome {
56
58template<size_t N> struct FixedString {
59 char data[N]{};
60 constexpr FixedString(const char (&str)[N]) {
61 for (size_t i = 0; i < N; ++i)
62 data[i] = str[i];
63 }
64 constexpr size_t size() const { return N - 1; } // exclude null terminator
65};
66
75template<FixedString... Strs> struct ProgmemStringTable {
76 static constexpr size_t COUNT = sizeof...(Strs);
77 static constexpr size_t BLOB_SIZE = (0 + ... + (Strs.size() + 1));
78
80 static constexpr auto make_blob() {
81 std::array<char, BLOB_SIZE> result{};
82 size_t pos = 0;
83 auto copy = [&](const auto &str) {
84 for (size_t i = 0; i <= str.size(); ++i)
85 result[pos++] = str.data[i];
86 };
87 (copy(Strs), ...);
88 return result;
89 }
90
92 static constexpr auto make_offsets() {
93 static_assert(COUNT > 0, "PROGMEM_STRING_TABLE must contain at least one string");
94 static_assert(COUNT <= 255, "PROGMEM_STRING_TABLE supports at most 255 strings with uint8_t indices");
95 static_assert(BLOB_SIZE <= 255, "PROGMEM_STRING_TABLE blob exceeds 255 bytes; use fewer/shorter strings");
96 std::array<uint8_t, COUNT> result{};
97 size_t pos = 0, idx = 0;
98 ((result[idx++] = static_cast<uint8_t>(pos), pos += Strs.size() + 1), ...);
99 return result;
100 }
101};
102
103// Forward declaration for LogString (defined in log.h)
104struct LogString;
105
109#define PROGMEM_STRING_TABLE(Name, ...) \
110 struct Name { \
111 using Table = ::esphome::ProgmemStringTable<__VA_ARGS__>; \
112 static constexpr size_t COUNT = Table::COUNT; \
113 static constexpr uint8_t LAST_INDEX = COUNT - 1; \
114 static constexpr size_t BLOB_SIZE = Table::BLOB_SIZE; \
115 static constexpr auto BLOB ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_blob(); \
116 static constexpr auto OFFSETS ESPHOME_PROGMEM_STRING_TABLE_STORAGE = Table::make_offsets(); \
117 static const char *get_(uint8_t idx, uint8_t fallback) { \
118 if (idx >= COUNT) \
119 idx = fallback; \
120 /* std::launder is used here to prevent the inter-procedural analysis that */ \
121 /* causes the false positive that the string is not null terminated */ \
122 return std::launder(&BLOB[::esphome::progmem_read_byte(&OFFSETS[idx])]); \
123 } \
124 static ::ProgmemStr get_progmem_str(uint8_t idx, uint8_t fallback) { \
125 return reinterpret_cast<::ProgmemStr>(get_(idx, fallback)); \
126 } \
127 static const ::esphome::LogString *get_log_str(uint8_t idx, uint8_t fallback) { \
128 return reinterpret_cast<const ::esphome::LogString *>(get_(idx, fallback)); \
129 } \
130 }
131
132} // namespace esphome
size_t size_t pos
Definition helpers.h:1038
const __FlashStringHelper * ProgmemStr
Definition progmem.h:27
Helper for C++20 string literal template arguments.
Definition progmem.h:58
constexpr size_t size() const
Definition progmem.h:64
constexpr FixedString(const char(&str)[N])
Definition progmem.h:60
Compile-time string table that packs strings into a single blob with offset lookup.
Definition progmem.h:75
static constexpr size_t COUNT
Definition progmem.h:76
static constexpr size_t BLOB_SIZE
Definition progmem.h:77
static constexpr auto make_blob()
Generate packed string blob at compile time.
Definition progmem.h:80
static constexpr auto make_offsets()
Generate offset table at compile time (uint8_t limits blob to 255 bytes)
Definition progmem.h:92