nvs_storage.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef nvs_storage_hpp
  14. #define nvs_storage_hpp
  15. #include <memory>
  16. #include <string>
  17. #include <unordered_map>
  18. #include "nvs.hpp"
  19. #include "nvs_types.hpp"
  20. #include "nvs_page.hpp"
  21. #include "nvs_pagemanager.hpp"
  22. //extern void dumpBytes(const uint8_t* data, size_t count);
  23. namespace nvs
  24. {
  25. class Storage : public intrusive_list_node<Storage>
  26. {
  27. enum class StorageState : uint32_t {
  28. INVALID,
  29. ACTIVE,
  30. };
  31. struct NamespaceEntry : public intrusive_list_node<NamespaceEntry> {
  32. public:
  33. char mName[Item::MAX_KEY_LENGTH + 1];
  34. uint8_t mIndex;
  35. };
  36. typedef intrusive_list<NamespaceEntry> TNamespaces;
  37. struct UsedPageNode: public intrusive_list_node<UsedPageNode> {
  38. public: Page* mPage;
  39. };
  40. typedef intrusive_list<UsedPageNode> TUsedPageList;
  41. struct BlobIndexNode: public intrusive_list_node<BlobIndexNode> {
  42. public:
  43. char key[Item::MAX_KEY_LENGTH + 1];
  44. uint8_t nsIndex;
  45. uint8_t chunkCount;
  46. VerOffset chunkStart;
  47. };
  48. typedef intrusive_list<BlobIndexNode> TBlobIndexList;
  49. public:
  50. ~Storage();
  51. Storage(const char *pName = NVS_DEFAULT_PART_NAME) : mPartitionName(pName) { };
  52. esp_err_t init(uint32_t baseSector, uint32_t sectorCount);
  53. bool isValid() const;
  54. esp_err_t createOrOpenNamespace(const char* nsName, bool canCreate, uint8_t& nsIndex);
  55. esp_err_t writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize);
  56. esp_err_t readItem(uint8_t nsIndex, ItemType datatype, const char* key, void* data, size_t dataSize);
  57. esp_err_t getItemDataSize(uint8_t nsIndex, ItemType datatype, const char* key, size_t& dataSize);
  58. esp_err_t eraseItem(uint8_t nsIndex, ItemType datatype, const char* key);
  59. template<typename T>
  60. esp_err_t writeItem(uint8_t nsIndex, const char* key, const T& value)
  61. {
  62. return writeItem(nsIndex, itemTypeOf(value), key, &value, sizeof(value));
  63. }
  64. template<typename T>
  65. esp_err_t readItem(uint8_t nsIndex, const char* key, T& value)
  66. {
  67. return readItem(nsIndex, itemTypeOf(value), key, &value, sizeof(value));
  68. }
  69. esp_err_t eraseItem(uint8_t nsIndex, const char* key)
  70. {
  71. return eraseItem(nsIndex, ItemType::ANY, key);
  72. }
  73. esp_err_t eraseNamespace(uint8_t nsIndex);
  74. const char *getPartName() const
  75. {
  76. return mPartitionName;
  77. }
  78. uint32_t getBaseSector()
  79. {
  80. return mPageManager.getBaseSector();
  81. }
  82. esp_err_t writeMultiPageBlob(uint8_t nsIndex, const char* key, const void* data, size_t dataSize, VerOffset chunkStart);
  83. esp_err_t readMultiPageBlob(uint8_t nsIndex, const char* key, void* data, size_t dataSize);
  84. esp_err_t cmpMultiPageBlob(uint8_t nsIndex, const char* key, const void* data, size_t dataSize);
  85. esp_err_t eraseMultiPageBlob(uint8_t nsIndex, const char* key, VerOffset chunkStart = VerOffset::VER_ANY);
  86. void debugDump();
  87. void debugCheck();
  88. esp_err_t fillStats(nvs_stats_t& nvsStats);
  89. esp_err_t calcEntriesInNamespace(uint8_t nsIndex, size_t& usedEntries);
  90. bool findEntry(nvs_opaque_iterator_t*, const char* name);
  91. bool nextEntry(nvs_opaque_iterator_t* it);
  92. protected:
  93. Page& getCurrentPage()
  94. {
  95. return mPageManager.back();
  96. }
  97. void clearNamespaces();
  98. void populateBlobIndices(TBlobIndexList&);
  99. void eraseOrphanDataBlobs(TBlobIndexList&);
  100. void fillEntryInfo(Item &item, nvs_entry_info_t &info);
  101. esp_err_t findItem(uint8_t nsIndex, ItemType datatype, const char* key, Page* &page, Item& item, uint8_t chunkIdx = Page::CHUNK_ANY, VerOffset chunkStart = VerOffset::VER_ANY);
  102. protected:
  103. const char *mPartitionName;
  104. size_t mPageCount;
  105. PageManager mPageManager;
  106. TNamespaces mNamespaces;
  107. CompressedEnumTable<bool, 1, 256> mNamespaceUsage;
  108. StorageState mState = StorageState::INVALID;
  109. };
  110. } // namespace nvs
  111. struct nvs_opaque_iterator_t
  112. {
  113. nvs_type_t type;
  114. uint8_t nsIndex;
  115. size_t entryIndex;
  116. nvs::Storage *storage;
  117. intrusive_list<nvs::Page>::iterator page;
  118. nvs_entry_info_t entry_info;
  119. };
  120. #endif /* nvs_storage_hpp */