nvs_storage.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include "nvs_storage.hpp"
  14. #ifndef ESP_PLATFORM
  15. #include <map>
  16. #include <sstream>
  17. #endif
  18. namespace nvs
  19. {
  20. Storage::~Storage()
  21. {
  22. clearNamespaces();
  23. }
  24. void Storage::clearNamespaces()
  25. {
  26. for (auto it = std::begin(mNamespaces); it != std::end(mNamespaces); ) {
  27. auto tmp = it;
  28. ++it;
  29. mNamespaces.erase(tmp);
  30. delete static_cast<NamespaceEntry*>(tmp);
  31. }
  32. }
  33. esp_err_t Storage::init(uint32_t baseSector, uint32_t sectorCount)
  34. {
  35. auto err = mPageManager.load(baseSector, sectorCount);
  36. if (err != ESP_OK) {
  37. mState = StorageState::INVALID;
  38. return err;
  39. }
  40. // load namespaces list
  41. clearNamespaces();
  42. std::fill_n(mNamespaceUsage.data(), mNamespaceUsage.byteSize() / 4, 0);
  43. for (auto it = mPageManager.begin(); it != mPageManager.end(); ++it) {
  44. Page& p = *it;
  45. size_t itemIndex = 0;
  46. Item item;
  47. while (p.findItem(Page::NS_INDEX, ItemType::U8, nullptr, itemIndex, item) == ESP_OK) {
  48. NamespaceEntry* entry = new NamespaceEntry;
  49. item.getKey(entry->mName, sizeof(entry->mName) - 1);
  50. item.getValue(entry->mIndex);
  51. mNamespaces.push_back(entry);
  52. mNamespaceUsage.set(entry->mIndex, true);
  53. itemIndex += item.span;
  54. }
  55. }
  56. mNamespaceUsage.set(0, true);
  57. mNamespaceUsage.set(255, true);
  58. mState = StorageState::ACTIVE;
  59. #ifndef ESP_PLATFORM
  60. debugCheck();
  61. #endif
  62. return ESP_OK;
  63. }
  64. esp_err_t Storage::findItem(uint8_t nsIndex, ItemType datatype, const char* key, Page* &page, Item& item)
  65. {
  66. size_t itemIndex = 0;
  67. for (auto it = std::begin(mPageManager); it != std::end(mPageManager); ++it) {
  68. auto err = it->findItem(nsIndex, datatype, key, itemIndex, item);
  69. if (err == ESP_OK) {
  70. page = it;
  71. return ESP_OK;
  72. }
  73. }
  74. return ESP_ERR_NVS_NOT_FOUND;
  75. }
  76. esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize)
  77. {
  78. if (mState != StorageState::ACTIVE) {
  79. return ESP_ERR_NVS_NOT_INITIALIZED;
  80. }
  81. Page* findPage = nullptr;
  82. Item item;
  83. auto err = findItem(nsIndex, datatype, key, findPage, item);
  84. if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) {
  85. return err;
  86. }
  87. Page& page = getCurrentPage();
  88. err = page.writeItem(nsIndex, datatype, key, data, dataSize);
  89. if (err == ESP_ERR_NVS_PAGE_FULL) {
  90. if (page.state() != Page::PageState::FULL) {
  91. err = page.markFull();
  92. if (err != ESP_OK) {
  93. return err;
  94. }
  95. }
  96. err = mPageManager.requestNewPage();
  97. if (err != ESP_OK) {
  98. return err;
  99. }
  100. err = getCurrentPage().writeItem(nsIndex, datatype, key, data, dataSize);
  101. if (err == ESP_ERR_NVS_PAGE_FULL) {
  102. return ESP_ERR_NVS_NOT_ENOUGH_SPACE;
  103. }
  104. if (err != ESP_OK) {
  105. return err;
  106. }
  107. } else if (err != ESP_OK) {
  108. return err;
  109. }
  110. if (findPage) {
  111. if (findPage->state() == Page::PageState::UNINITIALIZED ||
  112. findPage->state() == Page::PageState::INVALID) {
  113. ESP_ERROR_CHECK( findItem(nsIndex, datatype, key, findPage, item) );
  114. }
  115. err = findPage->eraseItem(nsIndex, datatype, key);
  116. if (err == ESP_ERR_FLASH_OP_FAIL) {
  117. return ESP_ERR_NVS_REMOVE_FAILED;
  118. }
  119. if (err != ESP_OK) {
  120. return err;
  121. }
  122. }
  123. #ifndef ESP_PLATFORM
  124. debugCheck();
  125. #endif
  126. return ESP_OK;
  127. }
  128. esp_err_t Storage::createOrOpenNamespace(const char* nsName, bool canCreate, uint8_t& nsIndex)
  129. {
  130. if (mState != StorageState::ACTIVE) {
  131. return ESP_ERR_NVS_NOT_INITIALIZED;
  132. }
  133. auto it = std::find_if(mNamespaces.begin(), mNamespaces.end(), [=] (const NamespaceEntry& e) -> bool {
  134. return strncmp(nsName, e.mName, sizeof(e.mName) - 1) == 0;
  135. });
  136. if (it == std::end(mNamespaces)) {
  137. if (!canCreate) {
  138. return ESP_ERR_NVS_NOT_FOUND;
  139. }
  140. uint8_t ns;
  141. for (ns = 1; ns < 255; ++ns) {
  142. if (mNamespaceUsage.get(ns) == false) {
  143. break;
  144. }
  145. }
  146. if (ns == 255) {
  147. return ESP_ERR_NVS_NOT_ENOUGH_SPACE;
  148. }
  149. auto err = writeItem(Page::NS_INDEX, ItemType::U8, nsName, &ns, sizeof(ns));
  150. if (err != ESP_OK) {
  151. return err;
  152. }
  153. mNamespaceUsage.set(ns, true);
  154. nsIndex = ns;
  155. NamespaceEntry* entry = new NamespaceEntry;
  156. entry->mIndex = ns;
  157. strncpy(entry->mName, nsName, sizeof(entry->mName) - 1);
  158. entry->mName[sizeof(entry->mName) - 1] = 0;
  159. mNamespaces.push_back(entry);
  160. } else {
  161. nsIndex = it->mIndex;
  162. }
  163. return ESP_OK;
  164. }
  165. esp_err_t Storage::readItem(uint8_t nsIndex, ItemType datatype, const char* key, void* data, size_t dataSize)
  166. {
  167. if (mState != StorageState::ACTIVE) {
  168. return ESP_ERR_NVS_NOT_INITIALIZED;
  169. }
  170. Item item;
  171. Page* findPage = nullptr;
  172. auto err = findItem(nsIndex, datatype, key, findPage, item);
  173. if (err != ESP_OK) {
  174. return err;
  175. }
  176. return findPage->readItem(nsIndex, datatype, key, data, dataSize);
  177. }
  178. esp_err_t Storage::eraseItem(uint8_t nsIndex, ItemType datatype, const char* key)
  179. {
  180. if (mState != StorageState::ACTIVE) {
  181. return ESP_ERR_NVS_NOT_INITIALIZED;
  182. }
  183. Item item;
  184. Page* findPage = nullptr;
  185. auto err = findItem(nsIndex, datatype, key, findPage, item);
  186. if (err != ESP_OK) {
  187. return err;
  188. }
  189. return findPage->eraseItem(nsIndex, datatype, key);
  190. }
  191. esp_err_t Storage::eraseNamespace(uint8_t nsIndex)
  192. {
  193. if (mState != StorageState::ACTIVE) {
  194. return ESP_ERR_NVS_NOT_INITIALIZED;
  195. }
  196. for (auto it = std::begin(mPageManager); it != std::end(mPageManager); ++it) {
  197. while (true) {
  198. auto err = it->eraseItem(nsIndex, ItemType::ANY, nullptr);
  199. if (err == ESP_ERR_NVS_NOT_FOUND) {
  200. break;
  201. }
  202. else if (err != ESP_OK) {
  203. return err;
  204. }
  205. }
  206. }
  207. return ESP_OK;
  208. }
  209. esp_err_t Storage::getItemDataSize(uint8_t nsIndex, ItemType datatype, const char* key, size_t& dataSize)
  210. {
  211. if (mState != StorageState::ACTIVE) {
  212. return ESP_ERR_NVS_NOT_INITIALIZED;
  213. }
  214. Item item;
  215. Page* findPage = nullptr;
  216. auto err = findItem(nsIndex, datatype, key, findPage, item);
  217. if (err != ESP_OK) {
  218. return err;
  219. }
  220. dataSize = item.varLength.dataSize;
  221. return ESP_OK;
  222. }
  223. void Storage::debugDump()
  224. {
  225. for (auto p = mPageManager.begin(); p != mPageManager.end(); ++p) {
  226. p->debugDump();
  227. }
  228. }
  229. #ifndef ESP_PLATFORM
  230. void Storage::debugCheck()
  231. {
  232. std::map<std::string, Page*> keys;
  233. for (auto p = mPageManager.begin(); p != mPageManager.end(); ++p) {
  234. size_t itemIndex = 0;
  235. size_t usedCount = 0;
  236. Item item;
  237. while (p->findItem(Page::NS_ANY, ItemType::ANY, nullptr, itemIndex, item) == ESP_OK) {
  238. std::stringstream keyrepr;
  239. keyrepr << static_cast<unsigned>(item.nsIndex) << "_" << static_cast<unsigned>(item.datatype) << "_" << item.key;
  240. std::string keystr = keyrepr.str();
  241. if (keys.find(keystr) != std::end(keys)) {
  242. printf("Duplicate key: %s\n", keystr.c_str());
  243. debugDump();
  244. assert(0);
  245. }
  246. keys.insert(std::make_pair(keystr, static_cast<Page*>(p)));
  247. itemIndex += item.span;
  248. usedCount += item.span;
  249. }
  250. assert(usedCount == p->getUsedEntryCount());
  251. }
  252. }
  253. #endif //ESP_PLATFORM
  254. }