ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
preferences.h
Go to the documentation of this file.
1#pragma once
2#ifdef USE_HOST
3
5#include <cstring>
6#include <map>
7#include <string>
8#include <vector>
9
10namespace esphome::host {
11
12class HostPreferences final : public PreferencesMixin<HostPreferences> {
13 public:
15 bool sync();
16 bool reset();
17
20
21 bool save(uint32_t key, const uint8_t *data, size_t len) {
22 if (len > 255)
23 return false;
24 this->setup_();
25 std::vector vec(data, data + len);
26 this->data_[key] = vec;
27 return true;
28 }
29
31 bool load_from_key(uint32_t type, uint8_t *data, size_t len) { return this->load(type, data, len); }
32
33 bool load(uint32_t key, uint8_t *data, size_t len) {
34 if (len > 255)
35 return false;
36 this->setup_();
37 auto it = this->data_.find(key);
38 if (it == this->data_.end())
39 return false;
40 const auto &vec = it->second;
41 if (vec.size() != len)
42 return false;
43 memcpy(data, vec.data(), len);
44 return true;
45 }
46
47 protected:
48 void setup_();
50 std::string filename_{};
51 std::map<uint32_t, std::vector<uint8_t>> data_{};
52};
53
55extern HostPreferences *host_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
56
57} // namespace esphome::host
58
59DECLARE_PREFERENCE_ALIASES(esphome::host::HostPreferences)
60
61#endif // USE_HOST
CRTP mixin providing type-safe template make_preference<T>() helpers.
bool load_from_key(uint32_t type, uint8_t *data, size_t len)
One-shot read of a stored preference by key, without allocating a backend.
Definition preferences.h:31
bool load(uint32_t key, uint8_t *data, size_t len)
Definition preferences.h:33
std::map< uint32_t, std::vector< uint8_t > > data_
Definition preferences.h:51
bool save(uint32_t key, const uint8_t *data, size_t len)
Definition preferences.h:21
ESPPreferenceObject make_preference(size_t length, uint32_t type)
Definition preferences.h:19
ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)
uint16_t type
void setup_preferences()
HostPreferences * host_preferences
const void size_t len
Definition hal.h:64
static void uint32_t
uint16_t length
Definition tt21100.cpp:0