ESPHome 2026.2.3
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
8namespace esphome {
9namespace host {
10namespace fs = std::filesystem;
11
12static const char *const TAG = "host.preferences";
13
15 if (this->setup_complete_)
16 return;
17 this->filename_.append(getenv("HOME"));
18 this->filename_.append("/.esphome");
19 this->filename_.append("/prefs");
20 fs::create_directories(this->filename_);
21 this->filename_.append("/");
22 this->filename_.append(App.get_name());
23 this->filename_.append(".prefs");
24 FILE *fp = fopen(this->filename_.c_str(), "rb");
25 if (fp != nullptr) {
26 while (!feof((fp))) {
27 uint32_t key;
28 uint8_t len;
29 if (fread(&key, sizeof(key), 1, fp) != 1)
30 break;
31 if (fread(&len, sizeof(len), 1, fp) != 1)
32 break;
33 uint8_t data[len];
34 if (fread(data, sizeof(uint8_t), len, fp) != len)
35 break;
36 std::vector vec(data, data + len);
37 this->data[key] = vec;
38 }
39 fclose(fp);
40 }
41 this->setup_complete_ = true;
42}
43
45 this->setup_();
46 FILE *fp = fopen(this->filename_.c_str(), "wb");
47 std::map<uint32_t, std::vector<uint8_t>>::iterator it;
48
49 for (it = this->data.begin(); it != this->data.end(); ++it) {
50 fwrite(&it->first, sizeof(uint32_t), 1, fp);
51 uint8_t len = it->second.size();
52 fwrite(&len, sizeof(len), 1, fp);
53 fwrite(it->second.data(), sizeof(uint8_t), it->second.size(), fp);
54 }
55 fclose(fp);
56 return true;
57}
58
60 host_preferences->data.clear();
61 return true;
62}
63
65 auto backend = new HostPreferenceBackend(type);
66 return ESPPreferenceObject(backend);
67};
68
69static HostPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
70
72 host_preferences = &s_preferences;
73 global_preferences = &s_preferences;
74}
75
76bool HostPreferenceBackend::save(const uint8_t *data, size_t len) {
77 return host_preferences->save(this->key_, data, len);
78}
79
80bool HostPreferenceBackend::load(uint8_t *data, size_t len) { return host_preferences->load(this->key_, data, len); }
81
83} // namespace host
84
85ESPPreferences *global_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
86} // namespace esphome
87
88#endif // USE_HOST
const std::string & get_name() const
Get the name of this Application set by pre_setup().
bool save(const uint8_t *data, size_t len) override
bool load(uint8_t *data, size_t len) override
bool load(uint32_t key, uint8_t *data, size_t len)
Definition preferences.h:41
bool save(uint32_t key, const uint8_t *data, size_t len)
Definition preferences.h:32
std::map< uint32_t, std::vector< uint8_t > > data
Definition preferences.h:59
ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash) override
uint16_t type
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:692
ESPPreferences * global_preferences
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t length
Definition tt21100.cpp:0