add: Kconfig assert or errorcode option
add: private include header add: macro encapsulation for assertion or error check add: ESP_FAIL return code documentation in public headers change: replaced all assertions by NVS_ASSERT_OR_RETURN macro change: few internal function return values from void to esp_err_t change: ESP_ERR_NVS_VALUE_TOO_LONG macro comment
This commit is contained in:
committed by
Martin Vychodil
parent
1f6fad6f48
commit
58cca07104
@@ -110,14 +110,23 @@ esp_err_t Storage::init(uint32_t baseSector, uint32_t sectorCount)
|
||||
}
|
||||
|
||||
item.getKey(entry->mName, sizeof(entry->mName));
|
||||
item.getValue(entry->mIndex);
|
||||
err = item.getValue(entry->mIndex);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
mNamespaces.push_back(entry);
|
||||
mNamespaceUsage.set(entry->mIndex, true);
|
||||
if (mNamespaceUsage.set(entry->mIndex, true) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
itemIndex += item.span;
|
||||
}
|
||||
}
|
||||
mNamespaceUsage.set(0, true);
|
||||
mNamespaceUsage.set(255, true);
|
||||
if (mNamespaceUsage.set(0, true) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (mNamespaceUsage.set(255, true) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
mState = StorageState::ACTIVE;
|
||||
|
||||
// Populate list of multi-page index entries.
|
||||
@@ -204,15 +213,17 @@ esp_err_t Storage::writeMultiPageBlob(uint8_t nsIndex, const char* key, const vo
|
||||
}
|
||||
|
||||
/* Split the blob into two and store the chunk of available size onto the current page */
|
||||
assert(tailroom != 0);
|
||||
NVS_ASSERT_OR_RETURN(tailroom != 0, ESP_FAIL);
|
||||
|
||||
chunkSize = (remainingSize > tailroom)? tailroom : remainingSize;
|
||||
remainingSize -= chunkSize;
|
||||
|
||||
err = page.writeItem(nsIndex, ItemType::BLOB_DATA, key,
|
||||
static_cast<const uint8_t*> (data) + offset, chunkSize, static_cast<uint8_t> (chunkStart) + chunkCount);
|
||||
chunkCount++;
|
||||
assert(err != ESP_ERR_NVS_PAGE_FULL);
|
||||
|
||||
if (err != ESP_OK) {
|
||||
NVS_ASSERT_OR_RETURN(err != ESP_ERR_NVS_PAGE_FULL, err);
|
||||
break;
|
||||
} else {
|
||||
UsedPageNode* node = new (std::nothrow) UsedPageNode();
|
||||
@@ -245,7 +256,7 @@ esp_err_t Storage::writeMultiPageBlob(uint8_t nsIndex, const char* key, const vo
|
||||
item.blobIndex.chunkStart = chunkStart;
|
||||
|
||||
err = getCurrentPage().writeItem(nsIndex, ItemType::BLOB_IDX, key, item.data, sizeof(item.data));
|
||||
assert(err != ESP_ERR_NVS_PAGE_FULL);
|
||||
NVS_ASSERT_OR_RETURN(err != ESP_ERR_NVS_PAGE_FULL, err);
|
||||
break;
|
||||
}
|
||||
} while (1);
|
||||
@@ -298,7 +309,8 @@ esp_err_t Storage::writeItem(uint8_t nsIndex, ItemType datatype, const char* key
|
||||
}
|
||||
/* Get the version of the previous index with same <ns,key> */
|
||||
prevStart = item.blobIndex.chunkStart;
|
||||
assert(prevStart == VerOffset::VER_0_OFFSET || prevStart == VerOffset::VER_1_OFFSET);
|
||||
NVS_ASSERT_OR_RETURN(prevStart == VerOffset::VER_0_OFFSET || prevStart == VerOffset::VER_1_OFFSET, ESP_FAIL);
|
||||
|
||||
|
||||
/* Toggle the version by changing the offset */
|
||||
nextStart
|
||||
@@ -401,8 +413,12 @@ esp_err_t Storage::createOrOpenNamespace(const char* nsName, bool canCreate, uin
|
||||
}
|
||||
|
||||
uint8_t ns;
|
||||
bool ns_state;
|
||||
for (ns = 1; ns < 255; ++ns) {
|
||||
if (mNamespaceUsage.get(ns) == false) {
|
||||
if (mNamespaceUsage.get(ns, &ns_state) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
if (!ns_state) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -415,7 +431,9 @@ esp_err_t Storage::createOrOpenNamespace(const char* nsName, bool canCreate, uin
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
mNamespaceUsage.set(ns, true);
|
||||
if (mNamespaceUsage.set(ns, true) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
nsIndex = ns;
|
||||
|
||||
NamespaceEntry* entry = new (std::nothrow) NamespaceEntry;
|
||||
@@ -449,7 +467,7 @@ esp_err_t Storage::readMultiPageBlob(uint8_t nsIndex, const char* key, void* dat
|
||||
VerOffset chunkStart = item.blobIndex.chunkStart;
|
||||
size_t offset = 0;
|
||||
|
||||
assert(dataSize == item.blobIndex.dataSize);
|
||||
NVS_ASSERT_OR_RETURN(dataSize == item.blobIndex.dataSize, ESP_FAIL);
|
||||
|
||||
/* Now read corresponding chunks */
|
||||
for (uint8_t chunkNum = 0; chunkNum < chunkCount; chunkNum++) {
|
||||
@@ -464,12 +482,12 @@ esp_err_t Storage::readMultiPageBlob(uint8_t nsIndex, const char* key, void* dat
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
assert(static_cast<uint8_t> (chunkStart) + chunkNum == item.chunkIndex);
|
||||
NVS_ASSERT_OR_RETURN(static_cast<uint8_t> (chunkStart) + chunkNum == item.chunkIndex, ESP_FAIL);
|
||||
|
||||
offset += item.varLength.dataSize;
|
||||
}
|
||||
if (err == ESP_OK) {
|
||||
assert(offset == dataSize);
|
||||
}
|
||||
NVS_ASSERT_OR_RETURN(offset == dataSize, ESP_FAIL);
|
||||
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND) {
|
||||
eraseMultiPageBlob(nsIndex, key); // cleanup if a chunk is not found
|
||||
}
|
||||
@@ -509,12 +527,12 @@ esp_err_t Storage::cmpMultiPageBlob(uint8_t nsIndex, const char* key, const void
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
assert(static_cast<uint8_t> (chunkStart) + chunkNum == item.chunkIndex);
|
||||
NVS_ASSERT_OR_RETURN(static_cast<uint8_t> (chunkStart) + chunkNum == item.chunkIndex, ESP_FAIL);
|
||||
|
||||
offset += item.varLength.dataSize;
|
||||
}
|
||||
if (err == ESP_OK) {
|
||||
assert(offset == dataSize);
|
||||
}
|
||||
NVS_ASSERT_OR_RETURN(offset == dataSize, ESP_FAIL);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -564,7 +582,7 @@ esp_err_t Storage::eraseMultiPageBlob(uint8_t nsIndex, const char* key, VerOffse
|
||||
if (chunkStart == VerOffset::VER_ANY) {
|
||||
chunkStart = item.blobIndex.chunkStart;
|
||||
} else {
|
||||
assert(chunkStart == item.blobIndex.chunkStart);
|
||||
NVS_ASSERT_OR_RETURN(chunkStart == item.blobIndex.chunkStart, ESP_FAIL);
|
||||
}
|
||||
|
||||
/* Now erase corresponding chunks*/
|
||||
|
||||
Reference in New Issue
Block a user