ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
preferences.cpp
Go to the documentation of this file.
1#ifdef USE_HOST
2
3#include <filesystem>
4#include <fstream>
5#include "preferences.h"
7#include "esphome/core/log.h"
8
9namespace esphome::host {
10namespace fs = std::filesystem;
11
12static const char *const TAG = "preferences";
13
15 if (this->setup_complete_)
16 return;
17 const char *home = getenv("HOME");
18 if (home == nullptr) {
19 ESP_LOGE(TAG, "HOME environment variable is not set");
20 abort();
21 }
22 this->filename_.append(home);
23 this->filename_.append("/.esphome");
24 this->filename_.append("/prefs");
25 fs::create_directories(this->filename_);
26 this->filename_.append("/");
27 this->filename_.append(App.get_name());
28 this->filename_.append(".prefs");
29 FILE *fp = fopen(this->filename_.c_str(), "rb");
30 if (fp != nullptr) {
31 while (!feof((fp))) {
32 uint32_t key;
33 uint8_t len;
34 if (fread(&key, sizeof(key), 1, fp) != 1)
35 break;
36 if (fread(&len, sizeof(len), 1, fp) != 1)
37 break;
38 uint8_t data[len];
39 if (fread(data, sizeof(uint8_t), len, fp) != len)
40 break;
41 std::vector vec(data, data + len);
42 this->data[key] = vec;
43 }
44 fclose(fp);
45 }
46 this->setup_complete_ = true;
47}
48
50 this->setup_();
51 FILE *fp = fopen(this->filename_.c_str(), "wb");
52 if (fp == nullptr) {
53 ESP_LOGE(TAG, "Failed to open preferences file for writing: %s", this->filename_.c_str());
54 return false;
55 }
56
57 for (auto it = this->data.begin(); it != this->data.end(); ++it) {
58 fwrite(&it->first, sizeof(uint32_t), 1, fp);
59 uint8_t len = it->second.size();
60 fwrite(&len, sizeof(len), 1, fp);
61 fwrite(it->second.data(), sizeof(uint8_t), it->second.size(), fp);
62 }
63 fclose(fp);
64 return true;
65}
66
68 host_preferences->data.clear();
69 return true;
70}
71
73 auto backend = new HostPreferenceBackend(type);
74 return ESPPreferenceObject(backend);
75};
76
77static HostPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
78
79HostPreferences *get_preferences() { return &s_preferences; }
80
82 host_preferences = &s_preferences;
83 global_preferences = &s_preferences;
84}
85
86bool HostPreferenceBackend::save(const uint8_t *data, size_t len) {
87 return host_preferences->save(this->key_, data, len);
88}
89
90bool HostPreferenceBackend::load(uint8_t *data, size_t len) { return host_preferences->load(this->key_, data, len); }
91
92HostPreferences *host_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
93
94} // namespace esphome::host
95
96namespace esphome {
97ESPPreferences *global_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
98} // namespace esphome
99
100#endif // USE_HOST
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
bool load(uint8_t *data, size_t len)
bool save(const uint8_t *data, size_t len)
bool load(uint32_t key, uint8_t *data, size_t len)
Definition preferences.h:30
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, bool in_flash)
std::map< uint32_t, std::vector< uint8_t > > data
Definition preferences.h:48
uint16_t type
HostPreferences * get_preferences()
void setup_preferences()
HostPreferences * host_preferences
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:1045
ESPPreferences * global_preferences
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint16_t length
Definition tt21100.cpp:0