Merge branch 'feature/nvs_storage_copy_part_name' into 'master'

NVS: partition name is copied now

Closes IDF-1231

See merge request espressif/esp-idf!6933
This commit is contained in:
Angus Gratton 2020-01-09 10:42:25 +08:00
commit dba3c241a9
5 changed files with 91 additions and 20 deletions

View file

@ -64,6 +64,8 @@ typedef nvs_handle_t nvs_handle IDF_DEPRECATED("Replace with nvs_handle_t");
#define NVS_DEFAULT_PART_NAME "nvs" /*!< Default partition name of the NVS partition in the partition table */
#define NVS_PART_NAME_MAX_SIZE 16 /*!< maximum length of partition name (excluding null terminator) */
/**
* @brief Mode of opening the non-volatile storage
*/

View file

@ -50,8 +50,7 @@ esp_err_t nvs_flash_init(void);
/**
* @brief Initialize NVS flash storage for the specified partition.
*
* @param[in] partition_label Label of the partition. Note that internally a reference to
* passed value is kept and it should be accessible for future operations
* @param[in] partition_label Label of the partition. Must be no longer than 16 characters.
*
* @return
* - ESP_OK if storage was successfully initialized.

View file

@ -102,6 +102,8 @@ 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) {

View file

@ -15,7 +15,6 @@
#define nvs_storage_hpp
#include <memory>
#include <string>
#include <unordered_map>
#include "nvs.hpp"
#include "nvs_types.hpp"
@ -61,7 +60,10 @@ class Storage : public intrusive_list_node<Storage>
public:
~Storage();
Storage(const char *pName = NVS_DEFAULT_PART_NAME) : mPartitionName(pName) { };
Storage(const char *pName = NVS_DEFAULT_PART_NAME)
{
strncpy(mPartitionName, pName, NVS_PART_NAME_MAX_SIZE);
};
esp_err_t init(uint32_t baseSector, uint32_t sectorCount);
@ -143,7 +145,7 @@ protected:
esp_err_t findItem(uint8_t nsIndex, ItemType datatype, const char* key, Page* &page, Item& item, uint8_t chunkIdx = Page::CHUNK_ANY, VerOffset chunkStart = VerOffset::VER_ANY);
protected:
const char *mPartitionName;
char mPartitionName [NVS_PART_NAME_MAX_SIZE + 1];
size_t mPageCount;
PageManager mPageManager;
TNamespaces mNamespaces;

View file

@ -549,6 +549,72 @@ TEST_CASE("can erase items", "[nvs]")
CHECK(storage.readItem(3, "key00222", val) == ESP_ERR_NVS_NOT_FOUND);
}
TEST_CASE("partition name is deep copy", "[nvs]")
{
SpiFlashEmulator emu(10);
char partition_name[16];
strcpy(partition_name, "const_name");
nvs_handle_t handle_1;
nvs_handle_t handle_2;
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
TEST_ESP_OK(nvs_flash_init_custom(partition_name, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
strcpy(partition_name, "just_kidding");
TEST_ESP_OK(nvs_open_from_partition("const_name", "test", NVS_READWRITE, &handle_1));
CHECK(nvs_open_from_partition("just_kidding", "test", NVS_READWRITE, &handle_2) == ESP_ERR_NVS_PART_NOT_FOUND);
nvs_close(handle_1);
nvs_close(handle_2);
nvs_flash_deinit_partition("const_name");
nvs_flash_deinit_partition("just_kidding"); // just in case, try not to affect other tests
}
TEST_CASE("namespace name is deep copy", "[nvs]")
{
SpiFlashEmulator emu(10);
char ns_name[16];
strcpy(ns_name, "const_name");
nvs_handle_t handle_1;
nvs_handle_t handle_2;
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
TEST_ESP_OK(nvs_flash_init_custom(NVS_DEFAULT_PART_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN));
TEST_ESP_OK(nvs_open("const_name", NVS_READWRITE, &handle_1));
strcpy(ns_name, "just_kidding");
CHECK(nvs_open("just_kidding", NVS_READONLY, &handle_2) == ESP_ERR_NVS_NOT_FOUND);
nvs_close(handle_1);
nvs_close(handle_2);
nvs_flash_deinit_partition(NVS_DEFAULT_PART_NAME);
}
TEST_CASE("Partition name no longer than 16 characters", "[nvs]")
{
SpiFlashEmulator emu(10);
const char *TOO_LONG_NAME = "0123456789abcdefg";
const uint32_t NVS_FLASH_SECTOR = 6;
const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3;
emu.setBounds(NVS_FLASH_SECTOR, NVS_FLASH_SECTOR + NVS_FLASH_SECTOR_COUNT_MIN);
CHECK(nvs_flash_init_custom(TOO_LONG_NAME, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN)
== ESP_ERR_INVALID_ARG);
nvs_flash_deinit_partition(TOO_LONG_NAME); // just in case
}
TEST_CASE("readonly handle fails on writing", "[nvs]")
{
SpiFlashEmulator emu(10);