2016-08-17 15:08:22 +00:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-10-31 11:48:28 +00:00
|
|
|
//
|
2016-08-17 15:08:22 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
#include "nvs_page.hpp"
|
|
|
|
#if defined(ESP_PLATFORM)
|
|
|
|
#include <rom/crc.h>
|
|
|
|
#else
|
|
|
|
#include "crc.h"
|
|
|
|
#endif
|
2016-08-22 03:46:44 +00:00
|
|
|
#include <cstdio>
|
2016-08-23 07:14:13 +00:00
|
|
|
#include <cstring>
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2018-07-02 11:10:43 +00:00
|
|
|
#include "nvs_ops.hpp"
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
namespace nvs
|
|
|
|
{
|
|
|
|
|
|
|
|
uint32_t Page::Header::calculateCrc32()
|
|
|
|
{
|
|
|
|
return crc32_le(0xffffffff,
|
|
|
|
reinterpret_cast<uint8_t*>(this) + offsetof(Header, mSeqNumber),
|
|
|
|
offsetof(Header, mCrc32) - offsetof(Header, mSeqNumber));
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t Page::load(uint32_t sectorNumber)
|
|
|
|
{
|
|
|
|
mBaseAddress = sectorNumber * SEC_SIZE;
|
|
|
|
mUsedEntryCount = 0;
|
|
|
|
mErasedEntryCount = 0;
|
|
|
|
|
|
|
|
Header header;
|
2016-10-26 16:46:33 +00:00
|
|
|
auto rc = spi_flash_read(mBaseAddress, &header, sizeof(header));
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (header.mState == PageState::UNINITIALIZED) {
|
|
|
|
mState = header.mState;
|
|
|
|
// check if the whole page is really empty
|
|
|
|
// reading the whole page takes ~40 times less than erasing it
|
2018-12-18 16:12:35 +00:00
|
|
|
const int BLOCK_SIZE = 128;
|
|
|
|
uint32_t* block = new uint32_t[BLOCK_SIZE];
|
|
|
|
for (uint32_t i = 0; i < SPI_FLASH_SEC_SIZE; i += 4 * BLOCK_SIZE) {
|
|
|
|
rc = spi_flash_read(mBaseAddress + i, block, 4 * BLOCK_SIZE);
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
2018-12-18 16:12:35 +00:00
|
|
|
delete[] block;
|
2016-08-17 15:08:22 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2018-12-18 16:12:35 +00:00
|
|
|
if (std::any_of(block, block + BLOCK_SIZE, [](uint32_t val) -> bool { return val != 0xffffffff; })) {
|
2016-08-17 15:08:22 +00:00
|
|
|
// page isn't as empty after all, mark it as corrupted
|
|
|
|
mState = PageState::CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 16:12:35 +00:00
|
|
|
delete[] block;
|
2016-09-22 13:05:47 +00:00
|
|
|
} else if (header.mCrc32 != header.calculateCrc32()) {
|
2016-08-17 15:08:22 +00:00
|
|
|
header.mState = PageState::CORRUPT;
|
2016-09-22 13:05:47 +00:00
|
|
|
} else {
|
2016-08-17 15:08:22 +00:00
|
|
|
mState = header.mState;
|
|
|
|
mSeqNumber = header.mSeqNumber;
|
2018-07-25 15:11:09 +00:00
|
|
|
if(header.mVersion < NVS_VERSION) {
|
|
|
|
return ESP_ERR_NVS_NEW_VERSION_FOUND;
|
|
|
|
} else {
|
|
|
|
mVersion = header.mVersion;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
switch (mState) {
|
|
|
|
case PageState::UNINITIALIZED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PageState::FULL:
|
|
|
|
case PageState::ACTIVE:
|
|
|
|
case PageState::FREEING:
|
|
|
|
mLoadEntryTable();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mState = PageState::CORRUPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t Page::writeEntry(const Item& item)
|
|
|
|
{
|
2018-07-02 11:10:43 +00:00
|
|
|
esp_err_t err;
|
|
|
|
|
|
|
|
err = nvs_flash_write(getEntryAddress(mNextFreeEntry), &item, sizeof(item));
|
|
|
|
|
|
|
|
if (err != ESP_OK) {
|
2016-08-17 15:08:22 +00:00
|
|
|
mState = PageState::INVALID;
|
2018-07-02 11:10:43 +00:00
|
|
|
return err;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 11:10:43 +00:00
|
|
|
err = alterEntryState(mNextFreeEntry, EntryState::WRITTEN);
|
2016-08-17 15:08:22 +00:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-08-22 04:11:32 +00:00
|
|
|
if (mFirstUsedEntry == INVALID_ENTRY) {
|
|
|
|
mFirstUsedEntry = mNextFreeEntry;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++mUsedEntryCount;
|
2016-08-22 04:11:32 +00:00
|
|
|
++mNextFreeEntry;
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-09-22 03:42:55 +00:00
|
|
|
esp_err_t Page::writeEntryData(const uint8_t* data, size_t size)
|
|
|
|
{
|
|
|
|
assert(size % ENTRY_SIZE == 0);
|
|
|
|
assert(mNextFreeEntry != INVALID_ENTRY);
|
|
|
|
assert(mFirstUsedEntry != INVALID_ENTRY);
|
|
|
|
const uint16_t count = size / ENTRY_SIZE;
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-11-18 11:17:13 +00:00
|
|
|
const uint8_t* buf = data;
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-11-18 11:17:13 +00:00
|
|
|
#ifdef ESP_PLATFORM
|
|
|
|
/* On the ESP32, data can come from DROM, which is not accessible by spi_flash_write
|
|
|
|
* function. To work around this, we copy the data to heap if it came from DROM.
|
|
|
|
* Hopefully this won't happen very often in practice. For data from DRAM, we should
|
|
|
|
* still be able to write it to flash directly.
|
|
|
|
* TODO: figure out how to make this platform-specific check nicer (probably by introducing
|
|
|
|
* a platform-specific flash layer).
|
|
|
|
*/
|
|
|
|
if ((uint32_t) data < 0x3ff00000) {
|
|
|
|
buf = (uint8_t*) malloc(size);
|
|
|
|
if (!buf) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
memcpy((void*)buf, data, size);
|
|
|
|
}
|
|
|
|
#endif //ESP_PLATFORM
|
2018-07-02 11:10:43 +00:00
|
|
|
|
|
|
|
auto rc = nvs_flash_write(getEntryAddress(mNextFreeEntry), buf, size);
|
|
|
|
|
2016-11-18 11:17:13 +00:00
|
|
|
#ifdef ESP_PLATFORM
|
|
|
|
if (buf != data) {
|
|
|
|
free((void*)buf);
|
|
|
|
}
|
|
|
|
#endif //ESP_PLATFORM
|
2016-09-22 03:42:55 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
auto err = alterEntryRangeState(mNextFreeEntry, mNextFreeEntry + count, EntryState::WRITTEN);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
mUsedEntryCount += count;
|
|
|
|
mNextFreeEntry += count;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
esp_err_t Page::writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize, uint8_t chunkIdx)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
Item item;
|
|
|
|
esp_err_t err;
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-10-31 11:48:28 +00:00
|
|
|
if (mState == PageState::INVALID) {
|
|
|
|
return ESP_ERR_NVS_INVALID_STATE;
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
if (mState == PageState::UNINITIALIZED) {
|
|
|
|
err = initialize();
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == PageState::FULL) {
|
|
|
|
return ESP_ERR_NVS_PAGE_FULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t keySize = strlen(key);
|
|
|
|
if (keySize > Item::MAX_KEY_LENGTH) {
|
|
|
|
return ESP_ERR_NVS_KEY_TOO_LONG;
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
|
|
|
if (dataSize > Page::CHUNK_MAX_SIZE) {
|
2017-05-31 04:59:24 +00:00
|
|
|
return ESP_ERR_NVS_VALUE_TOO_LONG;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
size_t totalSize = ENTRY_SIZE;
|
|
|
|
size_t entriesCount = 1;
|
2018-04-02 10:44:59 +00:00
|
|
|
if (isVariableLengthType(datatype)) {
|
2016-08-17 15:08:22 +00:00
|
|
|
size_t roundedSize = (dataSize + ENTRY_SIZE - 1) & ~(ENTRY_SIZE - 1);
|
|
|
|
totalSize += roundedSize;
|
|
|
|
entriesCount += roundedSize / ENTRY_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// primitive types should fit into one entry
|
2018-04-02 10:44:59 +00:00
|
|
|
assert(totalSize == ENTRY_SIZE ||
|
|
|
|
isVariableLengthType(datatype));
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2016-08-22 04:11:32 +00:00
|
|
|
if (mNextFreeEntry == INVALID_ENTRY || mNextFreeEntry + entriesCount > ENTRY_COUNT) {
|
2016-08-17 15:08:22 +00:00
|
|
|
// page will not fit this amount of data
|
|
|
|
return ESP_ERR_NVS_PAGE_FULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write first item
|
2016-09-20 14:40:12 +00:00
|
|
|
size_t span = (totalSize + ENTRY_SIZE - 1) / ENTRY_SIZE;
|
2018-04-02 10:44:59 +00:00
|
|
|
item = Item(nsIndex, datatype, span, key, chunkIdx);
|
2016-09-20 14:40:12 +00:00
|
|
|
mHashList.insert(item, mNextFreeEntry);
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
if (!isVariableLengthType(datatype)) {
|
2016-08-17 15:08:22 +00:00
|
|
|
memcpy(item.data, data, dataSize);
|
|
|
|
item.crc32 = item.calculateCrc32();
|
|
|
|
err = writeEntry(item);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const uint8_t* src = reinterpret_cast<const uint8_t*>(data);
|
|
|
|
item.varLength.dataCrc32 = Item::calculateCrc32(src, dataSize);
|
|
|
|
item.varLength.dataSize = dataSize;
|
2018-04-02 10:44:59 +00:00
|
|
|
item.varLength.reserved = 0xffff;
|
2016-08-17 15:08:22 +00:00
|
|
|
item.crc32 = item.calculateCrc32();
|
|
|
|
err = writeEntry(item);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-09-22 03:42:55 +00:00
|
|
|
size_t left = dataSize / ENTRY_SIZE * ENTRY_SIZE;
|
|
|
|
if (left > 0) {
|
|
|
|
err = writeEntryData(static_cast<const uint8_t*>(data), left);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-09-22 03:42:55 +00:00
|
|
|
size_t tail = dataSize - left;
|
|
|
|
if (tail > 0) {
|
2018-07-02 11:10:43 +00:00
|
|
|
std::fill_n(item.rawData, ENTRY_SIZE, 0xff);
|
2016-09-22 03:42:55 +00:00
|
|
|
memcpy(item.rawData, static_cast<const uint8_t*>(data) + left, tail);
|
2016-08-17 15:08:22 +00:00
|
|
|
err = writeEntry(item);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
esp_err_t Page::readItem(uint8_t nsIndex, ItemType datatype, const char* key, void* data, size_t dataSize, uint8_t chunkIdx, VerOffset chunkStart)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
size_t index = 0;
|
|
|
|
Item item;
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-10-31 11:48:28 +00:00
|
|
|
if (mState == PageState::INVALID) {
|
|
|
|
return ESP_ERR_NVS_INVALID_STATE;
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
|
|
|
esp_err_t rc = findItem(nsIndex, datatype, key, index, item, chunkIdx, chunkStart);
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
if (!isVariableLengthType(datatype)) {
|
2016-08-17 15:08:22 +00:00
|
|
|
if (dataSize != getAlignmentForType(datatype)) {
|
|
|
|
return ESP_ERR_NVS_TYPE_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, item.data, dataSize);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataSize < static_cast<size_t>(item.varLength.dataSize)) {
|
|
|
|
return ESP_ERR_NVS_INVALID_LENGTH;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* dst = reinterpret_cast<uint8_t*>(data);
|
|
|
|
size_t left = item.varLength.dataSize;
|
|
|
|
for (size_t i = index + 1; i < index + item.span; ++i) {
|
|
|
|
Item ditem;
|
|
|
|
rc = readEntry(i, ditem);
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
size_t willCopy = ENTRY_SIZE;
|
|
|
|
willCopy = (left < willCopy)?left:willCopy;
|
|
|
|
memcpy(dst, ditem.rawData, willCopy);
|
|
|
|
left -= willCopy;
|
|
|
|
dst += willCopy;
|
|
|
|
}
|
|
|
|
if (Item::calculateCrc32(reinterpret_cast<uint8_t*>(data), item.varLength.dataSize) != item.varLength.dataCrc32) {
|
|
|
|
rc = eraseEntryAndSpan(index);
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return ESP_ERR_NVS_NOT_FOUND;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
esp_err_t Page::eraseItem(uint8_t nsIndex, ItemType datatype, const char* key, uint8_t chunkIdx, VerOffset chunkStart)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
size_t index = 0;
|
|
|
|
Item item;
|
2018-04-02 10:44:59 +00:00
|
|
|
esp_err_t rc = findItem(nsIndex, datatype, key, index, item, chunkIdx, chunkStart);
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return eraseEntryAndSpan(index);
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
esp_err_t Page::findItem(uint8_t nsIndex, ItemType datatype, const char* key, uint8_t chunkIdx, VerOffset chunkStart)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
size_t index = 0;
|
|
|
|
Item item;
|
2018-04-02 10:44:59 +00:00
|
|
|
return findItem(nsIndex, datatype, key, index, item, chunkIdx, chunkStart);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t Page::eraseEntryAndSpan(size_t index)
|
|
|
|
{
|
|
|
|
auto state = mEntryTable.get(index);
|
|
|
|
assert(state == EntryState::WRITTEN || state == EntryState::EMPTY);
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
size_t span = 1;
|
|
|
|
if (state == EntryState::WRITTEN) {
|
|
|
|
Item item;
|
|
|
|
auto rc = readEntry(index, item);
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (item.calculateCrc32() != item.crc32) {
|
2018-03-11 22:35:21 +00:00
|
|
|
mHashList.erase(index, false);
|
2016-08-17 15:08:22 +00:00
|
|
|
rc = alterEntryState(index, EntryState::ERASED);
|
2016-10-31 13:10:47 +00:00
|
|
|
--mUsedEntryCount;
|
|
|
|
++mErasedEntryCount;
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
} else {
|
2018-03-11 22:35:21 +00:00
|
|
|
mHashList.erase(index);
|
2016-08-17 15:08:22 +00:00
|
|
|
span = item.span;
|
|
|
|
for (ptrdiff_t i = index + span - 1; i >= static_cast<ptrdiff_t>(index); --i) {
|
2016-08-22 04:11:32 +00:00
|
|
|
if (mEntryTable.get(i) == EntryState::WRITTEN) {
|
|
|
|
--mUsedEntryCount;
|
|
|
|
}
|
|
|
|
++mErasedEntryCount;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
2016-09-22 03:42:55 +00:00
|
|
|
if (span == 1) {
|
|
|
|
rc = alterEntryState(index, EntryState::ERASED);
|
|
|
|
} else {
|
|
|
|
rc = alterEntryRangeState(index, index + span, EntryState::ERASED);
|
|
|
|
}
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
} else {
|
2016-08-17 15:08:22 +00:00
|
|
|
auto rc = alterEntryState(index, EntryState::ERASED);
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index == mFirstUsedEntry) {
|
|
|
|
updateFirstUsedEntry(index, span);
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-22 04:11:32 +00:00
|
|
|
if (index + span > mNextFreeEntry) {
|
|
|
|
mNextFreeEntry = index + span;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Page::updateFirstUsedEntry(size_t index, size_t span)
|
|
|
|
{
|
|
|
|
assert(index == mFirstUsedEntry);
|
|
|
|
mFirstUsedEntry = INVALID_ENTRY;
|
2016-08-22 04:11:32 +00:00
|
|
|
size_t end = mNextFreeEntry;
|
|
|
|
if (end > ENTRY_COUNT) {
|
|
|
|
end = ENTRY_COUNT;
|
|
|
|
}
|
|
|
|
for (size_t i = index + span; i < end; ++i) {
|
2016-08-17 15:08:22 +00:00
|
|
|
if (mEntryTable.get(i) == EntryState::WRITTEN) {
|
|
|
|
mFirstUsedEntry = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2018-04-27 07:10:29 +00:00
|
|
|
esp_err_t Page::copyItems(Page& other)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
if (mFirstUsedEntry == INVALID_ENTRY) {
|
|
|
|
return ESP_ERR_NVS_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (other.mState == PageState::UNINITIALIZED) {
|
|
|
|
auto err = other.initialize();
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Item entry;
|
2018-04-27 07:10:29 +00:00
|
|
|
size_t readEntryIndex = mFirstUsedEntry;
|
2016-08-17 15:08:22 +00:00
|
|
|
|
2018-04-27 07:10:29 +00:00
|
|
|
while (readEntryIndex < ENTRY_COUNT) {
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2018-04-27 07:10:29 +00:00
|
|
|
if (mEntryTable.get(readEntryIndex) != EntryState::WRITTEN) {
|
|
|
|
assert(readEntryIndex != mFirstUsedEntry);
|
|
|
|
readEntryIndex++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto err = readEntry(readEntryIndex, entry);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2018-04-27 07:10:29 +00:00
|
|
|
other.mHashList.insert(entry, other.mNextFreeEntry);
|
2016-08-17 15:08:22 +00:00
|
|
|
err = other.writeEntry(entry);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2018-04-27 07:10:29 +00:00
|
|
|
size_t span = entry.span;
|
|
|
|
size_t end = readEntryIndex + span;
|
|
|
|
|
|
|
|
assert(end <= ENTRY_COUNT);
|
|
|
|
|
|
|
|
for (size_t i = readEntryIndex + 1; i < end; ++i) {
|
|
|
|
readEntry(i, entry);
|
|
|
|
err = other.writeEntry(entry);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
readEntryIndex = end;
|
|
|
|
|
2016-08-22 04:11:32 +00:00
|
|
|
}
|
2018-04-27 07:10:29 +00:00
|
|
|
return ESP_OK;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t Page::mLoadEntryTable()
|
|
|
|
{
|
|
|
|
// for states where we actually care about data in the page, read entry state table
|
|
|
|
if (mState == PageState::ACTIVE ||
|
|
|
|
mState == PageState::FULL ||
|
|
|
|
mState == PageState::FREEING) {
|
2016-10-26 16:46:33 +00:00
|
|
|
auto rc = spi_flash_read(mBaseAddress + ENTRY_TABLE_OFFSET, mEntryTable.data(),
|
2016-10-21 09:28:50 +00:00
|
|
|
mEntryTable.byteSize());
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mErasedEntryCount = 0;
|
|
|
|
mUsedEntryCount = 0;
|
|
|
|
for (size_t i = 0; i < ENTRY_COUNT; ++i) {
|
|
|
|
auto s = mEntryTable.get(i);
|
|
|
|
if (s == EntryState::WRITTEN) {
|
|
|
|
if (mFirstUsedEntry == INVALID_ENTRY) {
|
|
|
|
mFirstUsedEntry = i;
|
|
|
|
}
|
|
|
|
++mUsedEntryCount;
|
|
|
|
} else if (s == EntryState::ERASED) {
|
|
|
|
++mErasedEntryCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for PageState::ACTIVE, we may have more data written to this page
|
|
|
|
// as such, we need to figure out where the first unused entry is
|
|
|
|
if (mState == PageState::ACTIVE) {
|
|
|
|
for (size_t i = 0; i < ENTRY_COUNT; ++i) {
|
|
|
|
if (mEntryTable.get(i) == EntryState::EMPTY) {
|
|
|
|
mNextFreeEntry = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// however, if power failed after some data was written into the entry.
|
|
|
|
// but before the entry state table was altered, the entry locacted via
|
|
|
|
// entry state table may actually be half-written.
|
|
|
|
// this is easy to check by reading EntryHeader (i.e. first word)
|
2016-09-22 03:42:55 +00:00
|
|
|
while (mNextFreeEntry < ENTRY_COUNT) {
|
2016-08-22 04:11:32 +00:00
|
|
|
uint32_t entryAddress = getEntryAddress(mNextFreeEntry);
|
|
|
|
uint32_t header;
|
2016-10-26 16:46:33 +00:00
|
|
|
auto rc = spi_flash_read(entryAddress, &header, sizeof(header));
|
2016-08-22 04:11:32 +00:00
|
|
|
if (rc != ESP_OK) {
|
2016-08-17 15:08:22 +00:00
|
|
|
mState = PageState::INVALID;
|
2016-08-22 04:11:32 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
if (header != 0xffffffff) {
|
2016-09-22 03:42:55 +00:00
|
|
|
auto oldState = mEntryTable.get(mNextFreeEntry);
|
2016-08-22 04:11:32 +00:00
|
|
|
auto err = alterEntryState(mNextFreeEntry, EntryState::ERASED);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
++mNextFreeEntry;
|
2016-09-22 03:42:55 +00:00
|
|
|
if (oldState == EntryState::WRITTEN) {
|
|
|
|
--mUsedEntryCount;
|
|
|
|
}
|
|
|
|
++mErasedEntryCount;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that all variable-length items are written or erased fully
|
2016-08-22 04:11:32 +00:00
|
|
|
Item item;
|
|
|
|
size_t lastItemIndex = INVALID_ENTRY;
|
|
|
|
size_t end = mNextFreeEntry;
|
|
|
|
if (end > ENTRY_COUNT) {
|
|
|
|
end = ENTRY_COUNT;
|
|
|
|
}
|
2016-10-31 13:10:47 +00:00
|
|
|
size_t span;
|
|
|
|
for (size_t i = 0; i < end; i += span) {
|
|
|
|
span = 1;
|
2016-08-17 15:08:22 +00:00
|
|
|
if (mEntryTable.get(i) == EntryState::ERASED) {
|
2016-08-22 04:11:32 +00:00
|
|
|
lastItemIndex = INVALID_ENTRY;
|
2016-08-17 15:08:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-22 04:11:32 +00:00
|
|
|
lastItemIndex = i;
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
auto err = readEntry(i, item);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return err;
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
if (item.crc32 != item.calculateCrc32()) {
|
|
|
|
err = eraseEntryAndSpan(i);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2018-03-11 22:35:21 +00:00
|
|
|
mHashList.insert(item, i);
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2018-03-11 22:35:21 +00:00
|
|
|
// search for potential duplicate item
|
|
|
|
size_t duplicateIndex = mHashList.find(0, item);
|
2018-04-02 10:44:59 +00:00
|
|
|
|
|
|
|
if (isVariableLengthType(item.datatype)) {
|
2016-10-31 13:10:47 +00:00
|
|
|
span = item.span;
|
|
|
|
bool needErase = false;
|
|
|
|
for (size_t j = i; j < i + span; ++j) {
|
|
|
|
if (mEntryTable.get(j) != EntryState::WRITTEN) {
|
|
|
|
needErase = true;
|
|
|
|
lastItemIndex = INVALID_ENTRY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (needErase) {
|
|
|
|
eraseEntryAndSpan(i);
|
|
|
|
continue;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
|
|
|
/* Note that logic for duplicate detections works fine even
|
|
|
|
* when old-format blob is present along with new-format blob-index
|
|
|
|
* for same key on active page. Since datatype is not used in hash calculation,
|
|
|
|
* old-format blob will be removed.*/
|
2016-10-31 13:10:47 +00:00
|
|
|
if (duplicateIndex < i) {
|
|
|
|
eraseEntryAndSpan(duplicateIndex);
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-22 04:11:32 +00:00
|
|
|
// check that last item is not duplicate
|
|
|
|
if (lastItemIndex != INVALID_ENTRY) {
|
|
|
|
size_t findItemIndex = 0;
|
|
|
|
Item dupItem;
|
|
|
|
if (findItem(item.nsIndex, item.datatype, item.key, findItemIndex, dupItem) == ESP_OK) {
|
|
|
|
if (findItemIndex < lastItemIndex) {
|
|
|
|
auto err = eraseEntryAndSpan(findItemIndex);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
} else if (mState == PageState::FULL || mState == PageState::FREEING) {
|
2016-09-20 14:40:12 +00:00
|
|
|
// We have already filled mHashList for page in active state.
|
|
|
|
// Do the same for the case when page is in full or freeing state.
|
|
|
|
Item item;
|
|
|
|
for (size_t i = mFirstUsedEntry; i < ENTRY_COUNT; ++i) {
|
|
|
|
if (mEntryTable.get(i) != EntryState::WRITTEN) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-09-20 14:40:12 +00:00
|
|
|
auto err = readEntry(i, item);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return err;
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2017-09-04 03:54:18 +00:00
|
|
|
if (item.crc32 != item.calculateCrc32()) {
|
|
|
|
err = eraseEntryAndSpan(i);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-11 22:35:21 +00:00
|
|
|
|
2017-09-04 03:54:18 +00:00
|
|
|
assert(item.span > 0);
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2018-03-11 22:35:21 +00:00
|
|
|
mHashList.insert(item, i);
|
|
|
|
|
2016-09-20 14:40:12 +00:00
|
|
|
size_t span = item.span;
|
2018-04-13 06:59:06 +00:00
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
if (isVariableLengthType(item.datatype)) {
|
2018-04-13 06:59:06 +00:00
|
|
|
for (size_t j = i + 1; j < i + span; ++j) {
|
|
|
|
if (mEntryTable.get(j) != EntryState::WRITTEN) {
|
|
|
|
eraseEntryAndSpan(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:40:12 +00:00
|
|
|
i += span - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t Page::initialize()
|
|
|
|
{
|
|
|
|
assert(mState == PageState::UNINITIALIZED);
|
|
|
|
mState = PageState::ACTIVE;
|
|
|
|
Header header;
|
|
|
|
header.mState = mState;
|
|
|
|
header.mSeqNumber = mSeqNumber;
|
2018-07-25 15:11:09 +00:00
|
|
|
header.mVersion = mVersion;
|
2016-08-17 15:08:22 +00:00
|
|
|
header.mCrc32 = header.calculateCrc32();
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-10-26 16:46:33 +00:00
|
|
|
auto rc = spi_flash_write(mBaseAddress, &header, sizeof(header));
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
mNextFreeEntry = 0;
|
|
|
|
std::fill_n(mEntryTable.data(), mEntryTable.byteSize() / sizeof(uint32_t), 0xffffffff);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t Page::alterEntryState(size_t index, EntryState state)
|
|
|
|
{
|
|
|
|
assert(index < ENTRY_COUNT);
|
|
|
|
mEntryTable.set(index, state);
|
|
|
|
size_t wordToWrite = mEntryTable.getWordIndex(index);
|
|
|
|
uint32_t word = mEntryTable.data()[wordToWrite];
|
2016-10-21 09:28:50 +00:00
|
|
|
auto rc = spi_flash_write(mBaseAddress + ENTRY_TABLE_OFFSET + static_cast<uint32_t>(wordToWrite) * 4,
|
2016-10-26 16:46:33 +00:00
|
|
|
&word, sizeof(word));
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-22 03:42:55 +00:00
|
|
|
esp_err_t Page::alterEntryRangeState(size_t begin, size_t end, EntryState state)
|
|
|
|
{
|
|
|
|
assert(end <= ENTRY_COUNT);
|
|
|
|
assert(end > begin);
|
|
|
|
size_t wordIndex = mEntryTable.getWordIndex(end - 1);
|
|
|
|
for (ptrdiff_t i = end - 1; i >= static_cast<ptrdiff_t>(begin); --i) {
|
|
|
|
mEntryTable.set(i, state);
|
|
|
|
size_t nextWordIndex;
|
|
|
|
if (i == static_cast<ptrdiff_t>(begin)) {
|
|
|
|
nextWordIndex = (size_t) -1;
|
|
|
|
} else {
|
|
|
|
nextWordIndex = mEntryTable.getWordIndex(i - 1);
|
|
|
|
}
|
|
|
|
if (nextWordIndex != wordIndex) {
|
|
|
|
uint32_t word = mEntryTable.data()[wordIndex];
|
2016-10-21 09:28:50 +00:00
|
|
|
auto rc = spi_flash_write(mBaseAddress + ENTRY_TABLE_OFFSET + static_cast<uint32_t>(wordIndex) * 4,
|
2016-10-26 16:46:33 +00:00
|
|
|
&word, 4);
|
2016-09-22 03:42:55 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wordIndex = nextWordIndex;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t Page::alterPageState(PageState state)
|
|
|
|
{
|
2016-10-21 09:28:50 +00:00
|
|
|
uint32_t state_val = static_cast<uint32_t>(state);
|
2016-10-26 16:46:33 +00:00
|
|
|
auto rc = spi_flash_write(mBaseAddress, &state_val, sizeof(state));
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
mState = (PageState) state;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-22 02:30:35 +00:00
|
|
|
esp_err_t Page::readEntry(size_t index, Item& dst) const
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
2018-07-02 11:10:43 +00:00
|
|
|
auto rc = nvs_flash_read(getEntryAddress(index), &dst, sizeof(dst));
|
2016-08-17 15:08:22 +00:00
|
|
|
if (rc != ESP_OK) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
esp_err_t Page::findItem(uint8_t nsIndex, ItemType datatype, const char* key, size_t &itemIndex, Item& item, uint8_t chunkIdx, VerOffset chunkStart)
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
|
|
|
if (mState == PageState::CORRUPT || mState == PageState::INVALID || mState == PageState::UNINITIALIZED) {
|
|
|
|
return ESP_ERR_NVS_NOT_FOUND;
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2016-11-03 12:18:30 +00:00
|
|
|
size_t findBeginIndex = itemIndex;
|
|
|
|
if (findBeginIndex >= ENTRY_COUNT) {
|
2016-08-22 04:11:32 +00:00
|
|
|
return ESP_ERR_NVS_NOT_FOUND;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
size_t start = mFirstUsedEntry;
|
2016-11-03 12:18:30 +00:00
|
|
|
if (findBeginIndex > mFirstUsedEntry && findBeginIndex < ENTRY_COUNT) {
|
|
|
|
start = findBeginIndex;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-22 04:11:32 +00:00
|
|
|
size_t end = mNextFreeEntry;
|
|
|
|
if (end > ENTRY_COUNT) {
|
|
|
|
end = ENTRY_COUNT;
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-09-20 14:40:12 +00:00
|
|
|
if (nsIndex != NS_ANY && datatype != ItemType::ANY && key != NULL) {
|
2018-04-02 10:44:59 +00:00
|
|
|
size_t cachedIndex = mHashList.find(start, Item(nsIndex, datatype, 0, key, chunkIdx));
|
2016-09-20 14:40:12 +00:00
|
|
|
if (cachedIndex < ENTRY_COUNT) {
|
|
|
|
start = cachedIndex;
|
2016-09-22 13:05:47 +00:00
|
|
|
} else {
|
2016-09-20 14:40:12 +00:00
|
|
|
return ESP_ERR_NVS_NOT_FOUND;
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
size_t next;
|
2016-08-22 04:11:32 +00:00
|
|
|
for (size_t i = start; i < end; i = next) {
|
2016-08-17 15:08:22 +00:00
|
|
|
next = i + 1;
|
|
|
|
if (mEntryTable.get(i) != EntryState::WRITTEN) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rc = readEntry(i, item);
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto crc32 = item.calculateCrc32();
|
|
|
|
if (item.crc32 != crc32) {
|
2018-03-11 22:35:21 +00:00
|
|
|
rc = eraseEntryAndSpan(i);
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-02 10:44:59 +00:00
|
|
|
if (isVariableLengthType(item.datatype)) {
|
2016-08-17 15:08:22 +00:00
|
|
|
next = i + item.span;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsIndex != NS_ANY && item.nsIndex != nsIndex) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key != nullptr && strncmp(key, item.key, Item::MAX_KEY_LENGTH) != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
/* For blob data, chunkIndex should match*/
|
|
|
|
if (chunkIdx != CHUNK_ANY
|
|
|
|
&& datatype == ItemType::BLOB_DATA
|
|
|
|
&& item.chunkIndex != chunkIdx) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Blob-index will match the <ns,key> with blob data.
|
|
|
|
* Skip data chunks when searching for blob index*/
|
|
|
|
if (datatype == ItemType::BLOB_IDX
|
|
|
|
&& item.chunkIndex != CHUNK_ANY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Match the version for blob-index*/
|
|
|
|
if (datatype == ItemType::BLOB_IDX
|
|
|
|
&& chunkStart != VerOffset::VER_ANY
|
|
|
|
&& item.blobIndex.chunkStart != chunkStart) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
|
|
|
|
if (datatype != ItemType::ANY && item.datatype != datatype) {
|
2018-04-02 10:44:59 +00:00
|
|
|
if (key == nullptr && nsIndex == NS_ANY && chunkIdx == CHUNK_ANY) {
|
|
|
|
continue; // continue for bruteforce search on blob indices.
|
|
|
|
}
|
2016-08-17 15:08:22 +00:00
|
|
|
return ESP_ERR_NVS_TYPE_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemIndex = i;
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_ERR_NVS_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t Page::getSeqNumber(uint32_t& seqNumber) const
|
|
|
|
{
|
|
|
|
if (mState != PageState::UNINITIALIZED && mState != PageState::INVALID && mState != PageState::CORRUPT) {
|
|
|
|
seqNumber = mSeqNumber;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
return ESP_ERR_NVS_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
esp_err_t Page::setSeqNumber(uint32_t seqNumber)
|
|
|
|
{
|
|
|
|
if (mState != PageState::UNINITIALIZED) {
|
|
|
|
return ESP_ERR_NVS_INVALID_STATE;
|
|
|
|
}
|
|
|
|
mSeqNumber = seqNumber;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:11:09 +00:00
|
|
|
esp_err_t Page::setVersion(uint8_t ver)
|
|
|
|
{
|
|
|
|
if (mState != PageState::UNINITIALIZED) {
|
|
|
|
return ESP_ERR_NVS_INVALID_STATE;
|
|
|
|
}
|
|
|
|
mVersion = ver;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
esp_err_t Page::erase()
|
|
|
|
{
|
|
|
|
auto sector = mBaseAddress / SPI_FLASH_SEC_SIZE;
|
|
|
|
auto rc = spi_flash_erase_sector(sector);
|
|
|
|
if (rc != ESP_OK) {
|
|
|
|
mState = PageState::INVALID;
|
|
|
|
return rc;
|
|
|
|
}
|
2016-09-20 10:00:49 +00:00
|
|
|
mUsedEntryCount = 0;
|
|
|
|
mErasedEntryCount = 0;
|
|
|
|
mFirstUsedEntry = INVALID_ENTRY;
|
|
|
|
mNextFreeEntry = INVALID_ENTRY;
|
|
|
|
mState = PageState::UNINITIALIZED;
|
2016-10-26 04:25:53 +00:00
|
|
|
mHashList.clear();
|
2016-09-20 10:00:49 +00:00
|
|
|
return ESP_OK;
|
2016-08-17 15:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t Page::markFreeing()
|
|
|
|
{
|
|
|
|
if (mState != PageState::FULL && mState != PageState::ACTIVE) {
|
|
|
|
return ESP_ERR_NVS_INVALID_STATE;
|
|
|
|
}
|
|
|
|
return alterPageState(PageState::FREEING);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t Page::markFull()
|
|
|
|
{
|
|
|
|
if (mState != PageState::ACTIVE) {
|
|
|
|
return ESP_ERR_NVS_INVALID_STATE;
|
|
|
|
}
|
|
|
|
return alterPageState(PageState::FULL);
|
|
|
|
}
|
2018-04-02 10:44:59 +00:00
|
|
|
|
|
|
|
size_t Page::getVarDataTailroom() const
|
|
|
|
{
|
|
|
|
if (mState == PageState::UNINITIALIZED) {
|
|
|
|
return CHUNK_MAX_SIZE;
|
|
|
|
} else if (mState == PageState::FULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Skip one entry for header*/
|
|
|
|
return ((mNextFreeEntry < (ENTRY_COUNT-1)) ? ((ENTRY_COUNT - mNextFreeEntry - 1) * ENTRY_SIZE): 0);
|
|
|
|
}
|
|
|
|
|
2017-03-15 08:51:52 +00:00
|
|
|
const char* Page::pageStateToName(PageState ps)
|
|
|
|
{
|
|
|
|
switch (ps) {
|
|
|
|
case PageState::CORRUPT:
|
|
|
|
return "CORRUPT";
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2017-03-15 08:51:52 +00:00
|
|
|
case PageState::ACTIVE:
|
|
|
|
return "ACTIVE";
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2017-03-15 08:51:52 +00:00
|
|
|
case PageState::FREEING:
|
|
|
|
return "FREEING";
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2017-03-15 08:51:52 +00:00
|
|
|
case PageState::FULL:
|
|
|
|
return "FULL";
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2017-03-15 08:51:52 +00:00
|
|
|
case PageState::INVALID:
|
|
|
|
return "INVALID";
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2017-03-15 08:51:52 +00:00
|
|
|
case PageState::UNINITIALIZED:
|
|
|
|
return "UNINITIALIZED";
|
2018-04-02 10:44:59 +00:00
|
|
|
|
2017-03-15 08:51:52 +00:00
|
|
|
default:
|
|
|
|
assert(0 && "invalid state value");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
|
2016-08-22 02:30:35 +00:00
|
|
|
void Page::debugDump() const
|
2016-08-17 15:08:22 +00:00
|
|
|
{
|
2017-03-15 08:51:52 +00:00
|
|
|
printf("state=%x (%s) addr=%x seq=%d\nfirstUsed=%d nextFree=%d used=%d erased=%d\n", (uint32_t) mState, pageStateToName(mState), mBaseAddress, mSeqNumber, static_cast<int>(mFirstUsedEntry), static_cast<int>(mNextFreeEntry), mUsedEntryCount, mErasedEntryCount);
|
2016-08-17 15:08:22 +00:00
|
|
|
size_t skip = 0;
|
|
|
|
for (size_t i = 0; i < ENTRY_COUNT; ++i) {
|
|
|
|
printf("%3d: ", static_cast<int>(i));
|
|
|
|
EntryState state = mEntryTable.get(i);
|
|
|
|
if (state == EntryState::EMPTY) {
|
|
|
|
printf("E\n");
|
2016-09-22 13:05:47 +00:00
|
|
|
} else if (state == EntryState::ERASED) {
|
2016-08-17 15:08:22 +00:00
|
|
|
printf("X\n");
|
2016-09-22 13:05:47 +00:00
|
|
|
} else if (state == EntryState::WRITTEN) {
|
2016-08-17 15:08:22 +00:00
|
|
|
Item item;
|
|
|
|
readEntry(i, item);
|
|
|
|
if (skip == 0) {
|
2018-04-02 10:44:59 +00:00
|
|
|
printf("W ns=%2u type=%2u span=%3u key=\"%s\" chunkIdx=%d len=%d\n", item.nsIndex, static_cast<unsigned>(item.datatype), item.span, item.key, item.chunkIndex, (item.span != 1)?((int)item.varLength.dataSize):-1);
|
2016-10-31 11:48:28 +00:00
|
|
|
if (item.span > 0 && item.span <= ENTRY_COUNT - i) {
|
|
|
|
skip = item.span - 1;
|
|
|
|
} else {
|
|
|
|
skip = 0;
|
|
|
|
}
|
2016-09-22 13:05:47 +00:00
|
|
|
} else {
|
2016-08-17 15:08:22 +00:00
|
|
|
printf("D\n");
|
|
|
|
skip--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 07:11:56 +00:00
|
|
|
esp_err_t Page::calcEntries(nvs_stats_t &nvsStats)
|
|
|
|
{
|
|
|
|
assert(mState != PageState::FREEING);
|
|
|
|
|
|
|
|
nvsStats.total_entries += ENTRY_COUNT;
|
|
|
|
|
|
|
|
switch (mState) {
|
|
|
|
case PageState::UNINITIALIZED:
|
|
|
|
case PageState::CORRUPT:
|
|
|
|
nvsStats.free_entries += ENTRY_COUNT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PageState::FULL:
|
|
|
|
case PageState::ACTIVE:
|
|
|
|
nvsStats.used_entries += mUsedEntryCount;
|
|
|
|
nvsStats.free_entries += ENTRY_COUNT - mUsedEntryCount; // it's equivalent free + erase entries.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PageState::INVALID:
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Unhandled state");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-17 15:08:22 +00:00
|
|
|
} // namespace nvs
|