DataStore.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #pragma once
  2. #include <helpers/IdentityStore.h>
  3. #include <helpers/ContactInfo.h>
  4. #include <helpers/ChannelDetails.h>
  5. #include "NodePrefs.h"
  6. class DataStoreHost {
  7. public:
  8. virtual bool onContactLoaded(const ContactInfo& contact) =0;
  9. virtual bool getContactForSave(uint32_t idx, ContactInfo& contact) =0;
  10. virtual bool onChannelLoaded(uint8_t channel_idx, const ChannelDetails& ch) =0;
  11. virtual bool getChannelForSave(uint8_t channel_idx, ChannelDetails& ch) =0;
  12. };
  13. class DataStore {
  14. FILESYSTEM* _fs;
  15. FILESYSTEM* _fsExtra;
  16. mesh::RTCClock* _clock;
  17. IdentityStore identity_store;
  18. void loadPrefsInt(const char *filename, NodePrefs& prefs, double& node_lat, double& node_lon);
  19. #if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
  20. void checkAdvBlobFile();
  21. #endif
  22. public:
  23. DataStore(FILESYSTEM& fs, mesh::RTCClock& clock);
  24. DataStore(FILESYSTEM& fs, FILESYSTEM& fsExtra, mesh::RTCClock& clock);
  25. void begin();
  26. bool formatFileSystem();
  27. FILESYSTEM* getPrimaryFS() const { return _fs; }
  28. FILESYSTEM* getSecondaryFS() const { return _fsExtra; }
  29. bool loadMainIdentity(mesh::LocalIdentity &identity);
  30. bool saveMainIdentity(const mesh::LocalIdentity &identity);
  31. void loadPrefs(NodePrefs& prefs, double& node_lat, double& node_lon);
  32. void savePrefs(const NodePrefs& prefs, double node_lat, double node_lon);
  33. void loadContacts(DataStoreHost* host);
  34. void saveContacts(DataStoreHost* host, bool (*filter)(const ContactInfo& c) = NULL);
  35. void loadChannels(DataStoreHost* host);
  36. void saveChannels(DataStoreHost* host);
  37. void migrateToSecondaryFS();
  38. uint8_t getBlobByKey(const uint8_t key[], int key_len, uint8_t dest_buf[]);
  39. bool putBlobByKey(const uint8_t key[], int key_len, const uint8_t src_buf[], uint8_t len);
  40. bool deleteBlobByKey(const uint8_t key[], int key_len);
  41. File openRead(const char* filename);
  42. File openRead(FILESYSTEM* fs, const char* filename);
  43. bool removeFile(const char* filename);
  44. bool removeFile(FILESYSTEM* fs, const char* filename);
  45. uint32_t getStorageUsedKb() const;
  46. uint32_t getStorageTotalKb() const;
  47. private:
  48. FILESYSTEM* _getContactsChannelsFS() const { if (_fsExtra) return _fsExtra; return _fs;};
  49. };