mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
components/nvs: add debugging facilities and runtime checks
Extra runtime sanity checks run when compiled for the host (i.e. with no ESP_PLATFORM define)
This commit is contained in:
parent
3ce433cfd1
commit
f04c894123
@ -45,6 +45,12 @@ static intrusive_list<HandleEntry> s_nvs_handles;
|
||||
static uint32_t s_nvs_next_handle = 1;
|
||||
static nvs::Storage s_nvs_storage;
|
||||
|
||||
extern "C" void nvs_dump()
|
||||
{
|
||||
Lock lock;
|
||||
s_nvs_storage.debugDump();
|
||||
}
|
||||
|
||||
extern "C" esp_err_t nvs_flash_init(uint32_t baseSector, uint32_t sectorCount)
|
||||
{
|
||||
Lock::init();
|
||||
|
@ -17,6 +17,7 @@
|
||||
#else
|
||||
#include "crc.h"
|
||||
#endif
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
namespace nvs
|
||||
|
@ -91,6 +91,7 @@ esp_err_t PageManager::requestNewPage()
|
||||
Page* newPage = &mPageList.back();
|
||||
|
||||
Page* erasedPage = maxErasedItemsPageIt;
|
||||
size_t usedEntries = erasedPage->getUsedEntryCount();
|
||||
err = erasedPage->markFreeing();
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
@ -109,6 +110,8 @@ esp_err_t PageManager::requestNewPage()
|
||||
return err;
|
||||
}
|
||||
|
||||
assert(usedEntries == newPage->getUsedEntryCount());
|
||||
|
||||
mPageList.erase(maxErasedItemsPageIt);
|
||||
mFreePageList.push_back(erasedPage);
|
||||
|
||||
|
@ -13,6 +13,11 @@
|
||||
// limitations under the License.
|
||||
#include "nvs_storage.hpp"
|
||||
|
||||
#ifndef ESP_PLATFORM
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#endif
|
||||
|
||||
namespace nvs
|
||||
{
|
||||
|
||||
@ -56,6 +61,9 @@ esp_err_t Storage::init(uint32_t baseSector, uint32_t sectorCount)
|
||||
mNamespaceUsage.set(0, true);
|
||||
mNamespaceUsage.set(255, true);
|
||||
mState = StorageState::ACTIVE;
|
||||
#ifndef ESP_PLATFORM
|
||||
debugCheck();
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -75,29 +83,46 @@ esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key
|
||||
Page& page = getCurrentPage();
|
||||
err = page.writeItem(nsIndex, datatype, key, data, dataSize);
|
||||
if (err == ESP_ERR_NVS_PAGE_FULL) {
|
||||
page.markFull();
|
||||
if (page.state() != Page::PageState::FULL) {
|
||||
err = page.markFull();
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
err = mPageManager.requestNewPage();
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = getCurrentPage().writeItem(nsIndex, datatype, key, data, dataSize);
|
||||
if (err == ESP_ERR_NVS_PAGE_FULL) {
|
||||
return ESP_ERR_NVS_NOT_ENOUGH_SPACE;
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
else if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (findPage) {
|
||||
if (findPage->state() == Page::PageState::UNINITIALIZED) {
|
||||
if (findPage->state() == Page::PageState::UNINITIALIZED ||
|
||||
findPage->state() == Page::PageState::INVALID) {
|
||||
auto err = findItem(nsIndex, datatype, key, findPage, item);
|
||||
assert(err == ESP_OK);
|
||||
}
|
||||
err = findPage->eraseItem(nsIndex, datatype, key);
|
||||
if (err == ESP_ERR_FLASH_OP_FAIL) {
|
||||
return ESP_ERR_NVS_REMOVE_FAILED;
|
||||
}
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef ESP_PLATFORM
|
||||
debugCheck();
|
||||
#endif
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@ -192,4 +217,35 @@ esp_err_t Storage::getItemDataSize(uint8_t nsIndex, ItemType datatype, const cha
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void Storage::debugDump()
|
||||
{
|
||||
for (auto p = mPageManager.begin(); p != mPageManager.end(); ++p) {
|
||||
p->debugDump();
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef ESP_PLATFORM
|
||||
void Storage::debugCheck()
|
||||
{
|
||||
std::map<std::string, Page*> keys;
|
||||
|
||||
for (auto p = mPageManager.begin(); p != mPageManager.end(); ++p) {
|
||||
size_t itemIndex = 0;
|
||||
Item item;
|
||||
while (p->findItem(Page::NS_ANY, ItemType::ANY, nullptr, itemIndex, item) == ESP_OK) {
|
||||
std::stringstream keyrepr;
|
||||
keyrepr << static_cast<unsigned>(item.nsIndex) << "_" << static_cast<unsigned>(item.datatype) << "_" << item.key;
|
||||
std::string keystr = keyrepr.str();
|
||||
if (keys.find(keystr) != std::end(keys)) {
|
||||
printf("Duplicate key: %s\n", keystr.c_str());
|
||||
debugDump();
|
||||
assert(0);
|
||||
}
|
||||
keys.insert(std::make_pair(keystr, static_cast<Page*>(p)));
|
||||
itemIndex += item.span;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif //ESP_PLATFORM
|
||||
|
||||
}
|
@ -75,6 +75,9 @@ public:
|
||||
return eraseItem(nsIndex, itemTypeOf<T>(), key);
|
||||
}
|
||||
|
||||
void debugDump();
|
||||
void debugCheck();
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user