ESPHome  2024.10.2
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 
8 namespace esphome {
9 namespace host {
10 namespace fs = std::filesystem;
11 
12 static 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 
70  auto *pref = new HostPreferences(); // NOLINT(cppcoreguidelines-owning-memory)
71  host_preferences = pref;
72  global_preferences = pref;
73 }
74 
75 bool HostPreferenceBackend::save(const uint8_t *data, size_t len) {
76  return host_preferences->save(this->key_, data, len);
77 }
78 
79 bool HostPreferenceBackend::load(uint8_t *data, size_t len) { return host_preferences->load(this->key_, data, len); }
80 
82 } // namespace host
83 
84 ESPPreferences *global_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
85 } // namespace esphome
86 
87 #endif // USE_HOST
std::map< uint32_t, std::vector< uint8_t > > data
Definition: preferences.h:58
bool load(uint32_t key, uint8_t *data, size_t len)
Definition: preferences.h:41
void setup_preferences()
Definition: preferences.cpp:69
bool save(const uint8_t *data, size_t len) override
Definition: preferences.cpp:75
ESPPreferences * global_preferences
HostPreferences * host_preferences
Definition: preferences.cpp:81
ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash) override
Definition: preferences.cpp:64
uint8_t type
Application App
Global storage of Application pointer - only one Application can exist.
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:202
bool load(uint8_t *data, size_t len) override
Definition: preferences.cpp:79
bool save(uint32_t key, const uint8_t *data, size_t len)
Definition: preferences.h:32
std::string size_t len
Definition: helpers.h:292
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7