ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
preference_backend.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <cstdint>
5
8
9// Include the concrete preference backend for the active platform.
10// Each header defines its backend class, forward-declares its manager class,
11// declares get_preferences(), and provides the PreferenceBackend alias.
12#ifdef USE_ESP32
14#elif defined(USE_ESP8266)
16#elif defined(USE_RP2)
18#elif defined(USE_LIBRETINY)
20#elif defined(USE_HOST)
22#elif defined(USE_ZEPHYR) && defined(CONFIG_SETTINGS)
24#endif
25
26// Key-lookup preference backends find stored data by key; their platforms add the
27// USE_PREFERENCE_KEY_LOOKUP define from Python codegen, which enables preference key
28// migration. Slot-based backends (ESP8266, RP2040) instead allocate a storage slot for
29// every make_preference() call and use the key only as a validity tag on that slot;
30// migration is not possible there, and key collisions cannot corrupt data.
31
32namespace esphome {
33
34// The PreferenceBackend method surface, asserted on the alias each platform
35// header binds. save() persists len bytes; load() fills dest only when the
36// stored data exists and matches len. Both report success as their return.
37template<typename T>
38concept PreferenceBackendContract = requires(T backend, const uint8_t *src, uint8_t *dest, size_t len) {
39 { backend.save(src, len) } -> std::same_as<bool>;
40 { backend.load(dest, len) } -> std::same_as<bool>;
41 { backend.sync() } -> std::same_as<bool>;
42};
43
44#if !defined(USE_ESP32) && !defined(USE_ESP8266) && !defined(USE_RP2) && !defined(USE_LIBRETINY) && \
45 !defined(USE_HOST) && !(defined(USE_ZEPHYR) && defined(CONFIG_SETTINGS))
46// Stub for static analysis when no platform is defined.
48 bool save(const uint8_t *, size_t) { return false; }
49 bool load(uint8_t *, size_t) { return false; }
50 bool sync() { return false; }
51};
52#endif
53
56 "The platform's preference backend is missing part of the PreferenceBackend surface");
57
59 public:
61 explicit ESPPreferenceObject(PreferenceBackend *backend) : backend_(backend) {}
62
63 template<typename T> bool save(const T *src) { return this->save(reinterpret_cast<const uint8_t *>(src), sizeof(T)); }
64
65 template<typename T> bool load(T *dest) { return this->load(reinterpret_cast<uint8_t *>(dest), sizeof(T)); }
66
68 bool save(const uint8_t *src, size_t len) {
69 if (this->backend_ == nullptr)
70 return false;
71 return this->backend_->save(src, len);
72 }
73
75 bool load(uint8_t *dest, size_t len) {
76 if (this->backend_ == nullptr)
77 return false;
78 return this->backend_->load(dest, len);
79 }
80
84 bool sync() { return this->backend_ != nullptr && this->backend_->sync(); }
85
86 protected:
88};
89
90// The preferences manager method surface, asserted in esphome/core/preferences.h
91// on the ESPPreferences alias each platform's preferences.h binds through
92// DECLARE_PREFERENCE_ALIASES. Semantics beyond the signatures:
93// - make_preference: the two-argument form applies the platform's historic
94// default storage; in_flash=false may fall back to flash where the platform
95// has no faster storage.
96// - sync: commit pending writes to flash, true on success.
97// - reset: forget unsaved changes and re-initialize the permanent storage
98// (usually followed by a restart), true on success.
99// The template forms are what component call sites use; PreferencesMixin
100// supplies them, but the derived class's non-template overloads hide them
101// unless it also declares `using PreferencesMixin<X>::make_preference;`, so
102// the concept pins those too.
103template<typename T>
104concept PreferencesContract = requires(T prefs, size_t len, uint32_t type, bool in_flash) {
105 { prefs.make_preference(len, type, in_flash) } -> std::same_as<ESPPreferenceObject>;
106 { prefs.make_preference(len, type) } -> std::same_as<ESPPreferenceObject>;
107 { prefs.template make_preference<uint32_t>(type, in_flash) } -> std::same_as<ESPPreferenceObject>;
108 { prefs.template make_preference<uint32_t>(type) } -> std::same_as<ESPPreferenceObject>;
109 { prefs.sync() } -> std::same_as<bool>;
110 { prefs.reset() } -> std::same_as<bool>;
111};
112
113// Key-lookup platforms additionally provide load_from_key(), a one-shot read
114// of a stored preference by key that migrate_preference() relies on; see the
115// key-lookup note at the top of this file. Not part of PreferencesContract,
116// so it is asserted in preferences.h only where USE_PREFERENCE_KEY_LOOKUP
117// is set.
118template<typename T>
119concept PreferencesKeyLookupContract = requires(T prefs, uint32_t type, uint8_t *data, size_t len) {
120 { prefs.load_from_key(type, data, len) } -> std::same_as<bool>;
121};
122
125template<typename Derived> class PreferencesMixin {
126 public:
127 template<typename T, enable_if_t<is_trivially_copyable<T>::value, bool> = true>
129 return static_cast<Derived *>(this)->make_preference(sizeof(T), type, in_flash);
130 }
131
132 template<typename T, enable_if_t<is_trivially_copyable<T>::value, bool> = true>
134 return static_cast<Derived *>(this)->make_preference(sizeof(T), type);
135 }
136
137 private:
138 PreferencesMixin() = default;
139 friend Derived;
140};
141
142// Macro for platform preferences.h headers to declare the standard aliases.
143// Must be used at file scope (outside any namespace).
144#define DECLARE_PREFERENCE_ALIASES(platform_class) \
145 namespace esphome { \
146 using Preferences = platform_class; \
147 using ESPPreferences = Preferences; \
148 extern ESPPreferences *global_preferences; /* NOLINT(cppcoreguidelines-avoid-non-const-global-variables) */ \
149 }
150
151} // namespace esphome
bool sync()
Write this preference to flash now; ESP8266, RP2040, LibreTiny and host also write the other pending ...
ESPPreferenceObject(PreferenceBackend *backend)
bool save(const uint8_t *src, size_t len)
Raw save with explicit length, for callers that only know the size at runtime.
bool load(uint8_t *dest, size_t len)
Raw load with explicit length, for callers that only know the size at runtime.
CRTP mixin providing type-safe template make_preference<T>() helpers.
ESPPreferenceObject make_preference(uint32_t type)
ESPPreferenceObject make_preference(uint32_t type, bool in_flash)
uint8_t type
const void size_t len
Definition hal.h:68
esp32::ESP32PreferenceBackend PreferenceBackend
const void * src
Definition hal.h:68
static void uint32_t
bool save(const uint8_t *, size_t)
bool load(uint8_t *, size_t)