NVS: expose C++ API

Closes https://github.com/espressif/esp-idf/issues/3540
This commit is contained in:
Jakob Hasse
2019-11-25 11:32:40 +08:00
parent 6655fa8a93
commit d286876d34
26 changed files with 1674 additions and 227 deletions

View File

@@ -16,8 +16,10 @@
#include "nvs_storage.hpp"
#include "intrusive_list.h"
#include "nvs_platform.hpp"
#include "nvs_partition_manager.hpp"
#include "esp_partition.h"
#include "sdkconfig.h"
#include "nvs_handle_simple.hpp"
#ifdef CONFIG_NVS_ENCRYPTION
#include "nvs_encr.hpp"
#endif
@@ -34,6 +36,26 @@ static const char* TAG = "nvs";
#define ESP_LOGD(...)
#endif
class NVSHandleEntry : public intrusive_list_node<NVSHandleEntry> {
public:
NVSHandleEntry(nvs::NVSHandleSimple *handle, const char* part_name)
: nvs_handle(handle),
mHandle(++s_nvs_next_handle),
handle_part_name(part_name) { }
~NVSHandleEntry() {
delete nvs_handle;
}
nvs::NVSHandleSimple *nvs_handle;
nvs_handle_t mHandle;
const char* handle_part_name;
private:
static uint32_t s_nvs_next_handle;
};
uint32_t NVSHandleEntry::s_nvs_next_handle;
extern "C" void nvs_dump(const char *partName);
extern "C" esp_err_t nvs_flash_init_custom(const char *partName, uint32_t baseSector, uint32_t sectorCount);
@@ -41,26 +63,6 @@ extern "C" esp_err_t nvs_flash_init_custom(const char *partName, uint32_t baseSe
extern "C" esp_err_t nvs_flash_secure_init_custom(const char *partName, uint32_t baseSector, uint32_t sectorCount, nvs_sec_cfg_t* cfg);
#endif
class HandleEntry : public intrusive_list_node<HandleEntry>
{
static uint32_t s_nvs_next_handle;
public:
HandleEntry() {}
HandleEntry(bool readOnly, uint8_t nsIndex, nvs::Storage* StoragePtr) :
mHandle(++s_nvs_next_handle), // Begin the handle value with 1
mReadOnly(readOnly),
mNsIndex(nsIndex),
mStoragePtr(StoragePtr)
{
}
nvs_handle_t mHandle;
uint8_t mReadOnly;
uint8_t mNsIndex;
nvs::Storage* mStoragePtr;
};
#ifdef ESP_PLATFORM
SemaphoreHandle_t nvs::Lock::mSemaphore = NULL;
#endif
@@ -68,20 +70,11 @@ SemaphoreHandle_t nvs::Lock::mSemaphore = NULL;
using namespace std;
using namespace nvs;
static intrusive_list<HandleEntry> s_nvs_handles;
uint32_t HandleEntry::s_nvs_next_handle;
static intrusive_list<nvs::Storage> s_nvs_storage_list;
static intrusive_list<NVSHandleEntry> s_nvs_handles;
static nvs::Storage* lookup_storage_from_name(const char *name)
{
auto it = find_if(begin(s_nvs_storage_list), end(s_nvs_storage_list), [=](Storage& e) -> bool {
return (strcmp(e.getPartName(), name) == 0);
});
if (it == end(s_nvs_storage_list)) {
return NULL;
}
return it;
return NVSPartitionManager::get_instance()->lookup_storage_from_name(name);
}
extern "C" void nvs_dump(const char *partName)
@@ -102,24 +95,7 @@ extern "C" esp_err_t nvs_flash_init_custom(const char *partName, uint32_t baseSe
{
ESP_LOGD(TAG, "nvs_flash_init_custom partition=%s start=%d count=%d", partName, baseSector, sectorCount);
if (strlen(partName) > NVS_PART_NAME_MAX_SIZE) return ESP_ERR_INVALID_ARG;
nvs::Storage* new_storage = NULL;
nvs::Storage* storage = lookup_storage_from_name(partName);
if (storage == NULL) {
new_storage = new nvs::Storage((const char *)partName);
storage = new_storage;
}
esp_err_t err = storage->init(baseSector, sectorCount);
if (new_storage != NULL) {
if (err == ESP_OK) {
s_nvs_storage_list.push_back(new_storage);
} else {
delete new_storage;
}
}
return err;
return nvs::NVSPartitionManager::get_instance()->init_custom(partName, baseSector, sectorCount);
}
#ifdef CONFIG_NVS_ENCRYPTION
@@ -145,21 +121,8 @@ extern "C" esp_err_t nvs_flash_init_partition(const char *part_name)
{
Lock::init();
Lock lock;
nvs::Storage* mStorage;
mStorage = lookup_storage_from_name(part_name);
if (mStorage) {
return ESP_OK;
}
const esp_partition_t* partition = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, part_name);
if (partition == NULL) {
return ESP_ERR_NOT_FOUND;
}
return nvs_flash_init_custom(part_name, partition->address / SPI_FLASH_SEC_SIZE,
partition->size / SPI_FLASH_SEC_SIZE);
return nvs::NVSPartitionManager::get_instance()->init_partition(part_name);
}
extern "C" esp_err_t nvs_flash_init(void)
@@ -217,37 +180,11 @@ extern "C" esp_err_t nvs_flash_deinit_partition(const char* partition_name)
Lock::init();
Lock lock;
nvs::Storage* storage = lookup_storage_from_name(partition_name);
if (!storage) {
return ESP_ERR_NVS_NOT_INITIALIZED;
}
// Delete all corresponding open handles
s_nvs_handles.clearAndFreeNodes();
#ifdef CONFIG_NVS_ENCRYPTION
if(EncrMgr::isEncrActive()) {
auto encrMgr = EncrMgr::getInstance();
encrMgr->removeSecurityContext(storage->getBaseSector());
}
#endif
/* Clean up handles related to the storage being deinitialized */
auto it = s_nvs_handles.begin();
auto next = it;
while(it != s_nvs_handles.end()) {
next++;
if (it->mStoragePtr == storage) {
ESP_LOGD(TAG, "Deleting handle %d (ns=%d) related to partition \"%s\" (missing call to nvs_close?)",
it->mHandle, it->mNsIndex, partition_name);
s_nvs_handles.erase(it);
delete static_cast<HandleEntry*>(it);
}
it = next;
}
/* Finally delete the storage itself */
s_nvs_storage_list.erase(storage);
delete storage;
return ESP_OK;
// Deinit partition
return nvs::NVSPartitionManager::get_instance()->deinit_partition(partition_name);
}
extern "C" esp_err_t nvs_flash_deinit(void)
@@ -255,15 +192,15 @@ extern "C" esp_err_t nvs_flash_deinit(void)
return nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME);
}
static esp_err_t nvs_find_ns_handle(nvs_handle_t handle, HandleEntry& entry)
static esp_err_t nvs_find_ns_handle(nvs_handle_t c_handle, NVSHandleSimple** handle)
{
auto it = find_if(begin(s_nvs_handles), end(s_nvs_handles), [=](HandleEntry& e) -> bool {
return e.mHandle == handle;
auto it = find_if(begin(s_nvs_handles), end(s_nvs_handles), [=](NVSHandleEntry& e) -> bool {
return e.mHandle == c_handle;
});
if (it == end(s_nvs_handles)) {
return ESP_ERR_NVS_INVALID_HANDLE;
}
entry = *it;
*handle = it->nvs_handle;
return ESP_OK;
}
@@ -271,33 +208,25 @@ extern "C" esp_err_t nvs_open_from_partition(const char *part_name, const char*
{
Lock lock;
ESP_LOGD(TAG, "%s %s %d", __func__, name, open_mode);
uint8_t nsIndex;
nvs::Storage* sHandle;
sHandle = lookup_storage_from_name(part_name);
if (sHandle == NULL) {
return ESP_ERR_NVS_PART_NOT_FOUND;
NVSHandleSimple *handle;
esp_err_t result = nvs::NVSPartitionManager::get_instance()->open_handle(part_name, name, open_mode, &handle);
if (result == ESP_OK) {
NVSHandleEntry *entry = new NVSHandleEntry(handle, part_name);
if (entry) {
s_nvs_handles.push_back(entry);
*out_handle = entry->mHandle;
} else {
delete handle;
return ESP_ERR_NO_MEM;
}
}
esp_err_t err = sHandle->createOrOpenNamespace(name, open_mode == NVS_READWRITE, nsIndex);
if (err != ESP_OK) {
return err;
}
HandleEntry *handle_entry = new HandleEntry(open_mode==NVS_READONLY, nsIndex, sHandle);
s_nvs_handles.push_back(handle_entry);
*out_handle = handle_entry->mHandle;
return ESP_OK;
return result;
}
extern "C" esp_err_t nvs_open(const char* name, nvs_open_mode_t open_mode, nvs_handle_t *out_handle)
{
if (s_nvs_storage_list.size() == 0) {
return ESP_ERR_NVS_NOT_INITIALIZED;
}
return nvs_open_from_partition(NVS_DEFAULT_PART_NAME, name, open_mode, out_handle);
}
@@ -305,60 +234,54 @@ extern "C" void nvs_close(nvs_handle_t handle)
{
Lock lock;
ESP_LOGD(TAG, "%s %d", __func__, handle);
auto it = find_if(begin(s_nvs_handles), end(s_nvs_handles), [=](HandleEntry& e) -> bool {
auto it = find_if(begin(s_nvs_handles), end(s_nvs_handles), [=](NVSHandleEntry& e) -> bool {
return e.mHandle == handle;
});
if (it == end(s_nvs_handles)) {
return;
}
s_nvs_handles.erase(it);
delete static_cast<HandleEntry*>(it);
delete static_cast<NVSHandleEntry*>(it);
}
extern "C" esp_err_t nvs_erase_key(nvs_handle_t handle, const char* key)
extern "C" esp_err_t nvs_erase_key(nvs_handle_t c_handle, const char* key)
{
Lock lock;
ESP_LOGD(TAG, "%s %s\r\n", __func__, key);
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
if (entry.mReadOnly) {
return ESP_ERR_NVS_READ_ONLY;
}
return entry.mStoragePtr->eraseItem(entry.mNsIndex, key);
return handle->erase_item(key);
}
extern "C" esp_err_t nvs_erase_all(nvs_handle_t handle)
extern "C" esp_err_t nvs_erase_all(nvs_handle_t c_handle)
{
Lock lock;
ESP_LOGD(TAG, "%s\r\n", __func__);
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
if (entry.mReadOnly) {
return ESP_ERR_NVS_READ_ONLY;
}
return entry.mStoragePtr->eraseNamespace(entry.mNsIndex);
return handle->erase_all();
}
template<typename T>
static esp_err_t nvs_set(nvs_handle_t handle, const char* key, T value)
static esp_err_t nvs_set(nvs_handle_t c_handle, const char* key, T value)
{
Lock lock;
ESP_LOGD(TAG, "%s %s %d %d", __func__, key, sizeof(T), (uint32_t) value);
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
if (entry.mReadOnly) {
return ESP_ERR_NVS_READ_ONLY;
}
return entry.mStoragePtr->writeItem(entry.mNsIndex, key, value);
return handle->set_item(key, value);
}
extern "C" esp_err_t nvs_set_i8 (nvs_handle_t handle, const char* key, int8_t value)
@@ -401,110 +324,108 @@ extern "C" esp_err_t nvs_set_u64 (nvs_handle_t handle, const char* key, uint64_t
return nvs_set(handle, key, value);
}
extern "C" esp_err_t nvs_commit(nvs_handle_t handle)
extern "C" esp_err_t nvs_commit(nvs_handle_t c_handle)
{
Lock lock;
// no-op for now, to be used when intermediate cache is added
HandleEntry entry;
return nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
return handle->commit();
}
extern "C" esp_err_t nvs_set_str(nvs_handle_t handle, const char* key, const char* value)
extern "C" esp_err_t nvs_set_str(nvs_handle_t c_handle, const char* key, const char* value)
{
Lock lock;
ESP_LOGD(TAG, "%s %s %s", __func__, key, value);
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
if (entry.mReadOnly) {
return ESP_ERR_NVS_READ_ONLY;
}
return entry.mStoragePtr->writeItem(entry.mNsIndex, nvs::ItemType::SZ, key, value, strlen(value) + 1);
return handle->set_string(key, value);
}
extern "C" esp_err_t nvs_set_blob(nvs_handle_t handle, const char* key, const void* value, size_t length)
extern "C" esp_err_t nvs_set_blob(nvs_handle_t c_handle, const char* key, const void* value, size_t length)
{
Lock lock;
ESP_LOGD(TAG, "%s %s %d", __func__, key, length);
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
if (entry.mReadOnly) {
return ESP_ERR_NVS_READ_ONLY;
}
return entry.mStoragePtr->writeItem(entry.mNsIndex, nvs::ItemType::BLOB, key, value, length);
return handle->set_blob(key, value, length);
}
template<typename T>
static esp_err_t nvs_get(nvs_handle_t handle, const char* key, T* out_value)
static esp_err_t nvs_get(nvs_handle_t c_handle, const char* key, T* out_value)
{
Lock lock;
ESP_LOGD(TAG, "%s %s %d", __func__, key, sizeof(T));
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
return entry.mStoragePtr->readItem(entry.mNsIndex, key, *out_value);
return handle->get_item(key, *out_value);
}
extern "C" esp_err_t nvs_get_i8 (nvs_handle_t handle, const char* key, int8_t* out_value)
extern "C" esp_err_t nvs_get_i8 (nvs_handle_t c_handle, const char* key, int8_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_u8 (nvs_handle_t handle, const char* key, uint8_t* out_value)
extern "C" esp_err_t nvs_get_u8 (nvs_handle_t c_handle, const char* key, uint8_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_i16 (nvs_handle_t handle, const char* key, int16_t* out_value)
extern "C" esp_err_t nvs_get_i16 (nvs_handle_t c_handle, const char* key, int16_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_u16 (nvs_handle_t handle, const char* key, uint16_t* out_value)
extern "C" esp_err_t nvs_get_u16 (nvs_handle_t c_handle, const char* key, uint16_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_i32 (nvs_handle_t handle, const char* key, int32_t* out_value)
extern "C" esp_err_t nvs_get_i32 (nvs_handle_t c_handle, const char* key, int32_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_u32 (nvs_handle_t handle, const char* key, uint32_t* out_value)
extern "C" esp_err_t nvs_get_u32 (nvs_handle_t c_handle, const char* key, uint32_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_i64 (nvs_handle_t handle, const char* key, int64_t* out_value)
extern "C" esp_err_t nvs_get_i64 (nvs_handle_t c_handle, const char* key, int64_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
extern "C" esp_err_t nvs_get_u64 (nvs_handle_t handle, const char* key, uint64_t* out_value)
extern "C" esp_err_t nvs_get_u64 (nvs_handle_t c_handle, const char* key, uint64_t* out_value)
{
return nvs_get(handle, key, out_value);
return nvs_get(c_handle, key, out_value);
}
static esp_err_t nvs_get_str_or_blob(nvs_handle_t handle, nvs::ItemType type, const char* key, void* out_value, size_t* length)
static esp_err_t nvs_get_str_or_blob(nvs_handle_t c_handle, nvs::ItemType type, const char* key, void* out_value, size_t* length)
{
Lock lock;
ESP_LOGD(TAG, "%s %s", __func__, key);
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
size_t dataSize;
err = entry.mStoragePtr->getItemDataSize(entry.mNsIndex, type, key, dataSize);
err = handle->get_item_size(type, key, dataSize);
if (err != ESP_OK) {
return err;
}
@@ -520,17 +441,17 @@ static esp_err_t nvs_get_str_or_blob(nvs_handle_t handle, nvs::ItemType type, co
}
*length = dataSize;
return entry.mStoragePtr->readItem(entry.mNsIndex, type, key, out_value, dataSize);
return handle->get_typed_item(type, key, out_value, dataSize);
}
extern "C" esp_err_t nvs_get_str(nvs_handle_t handle, const char* key, char* out_value, size_t* length)
extern "C" esp_err_t nvs_get_str(nvs_handle_t c_handle, const char* key, char* out_value, size_t* length)
{
return nvs_get_str_or_blob(handle, nvs::ItemType::SZ, key, out_value, length);
return nvs_get_str_or_blob(c_handle, nvs::ItemType::SZ, key, out_value, length);
}
extern "C" esp_err_t nvs_get_blob(nvs_handle_t handle, const char* key, void* out_value, size_t* length)
extern "C" esp_err_t nvs_get_blob(nvs_handle_t c_handle, const char* key, void* out_value, size_t* length)
{
return nvs_get_str_or_blob(handle, nvs::ItemType::BLOB, key, out_value, length);
return nvs_get_str_or_blob(c_handle, nvs::ItemType::BLOB, key, out_value, length);
}
extern "C" esp_err_t nvs_get_stats(const char* part_name, nvs_stats_t* nvs_stats)
@@ -558,7 +479,7 @@ extern "C" esp_err_t nvs_get_stats(const char* part_name, nvs_stats_t* nvs_stats
return pStorage->fillStats(*nvs_stats);
}
extern "C" esp_err_t nvs_get_used_entry_count(nvs_handle_t handle, size_t* used_entries)
extern "C" esp_err_t nvs_get_used_entry_count(nvs_handle_t c_handle, size_t* used_entries)
{
Lock lock;
if(used_entries == NULL){
@@ -566,14 +487,14 @@ extern "C" esp_err_t nvs_get_used_entry_count(nvs_handle_t handle, size_t* used_
}
*used_entries = 0;
HandleEntry entry;
auto err = nvs_find_ns_handle(handle, entry);
NVSHandleSimple *handle;
auto err = nvs_find_ns_handle(c_handle, &handle);
if (err != ESP_OK) {
return err;
}
size_t used_entry_count;
err = entry.mStoragePtr->calcEntriesInNamespace(entry.mNsIndex, used_entry_count);
err = handle->get_used_entry_count(used_entry_count);
if(err == ESP_OK){
*used_entries = used_entry_count;
}