Added parameter to Preferences::begin to select non-default NVS partition (#4718)

This commit is contained in:
bot1131357 2021-01-16 04:06:51 +11:00 committed by GitHub
parent d2530850a3
commit 8d0e68db4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -14,6 +14,7 @@
#include "Preferences.h"
#include "nvs.h"
#include "nvs_flash.h"
const char * nvs_errors[] = { "OTHER", "NOT_INITIALIZED", "NOT_FOUND", "TYPE_MISMATCH", "READ_ONLY", "NOT_ENOUGH_SPACE", "INVALID_NAME", "INVALID_HANDLE", "REMOVE_FAILED", "KEY_TOO_LONG", "PAGE_FULL", "INVALID_STATE", "INVALID_LENGTH"};
#define nvs_error(e) (((e)>ESP_ERR_NVS_BASE)?nvs_errors[(e)&~(ESP_ERR_NVS_BASE)]:nvs_errors[0])
@ -28,12 +29,22 @@ Preferences::~Preferences(){
end();
}
bool Preferences::begin(const char * name, bool readOnly){
bool Preferences::begin(const char * name, bool readOnly, const char* partition_label){
if(_started){
return false;
}
_readOnly = readOnly;
esp_err_t err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
esp_err_t err = ESP_OK;
if (partition_label != NULL) {
err = nvs_flash_init_partition(partition_label);
if (err) {
log_e("nvs_flash_init_partition failed: %s", nvs_error(err));
return false;
}
err = nvs_open_from_partition(partition_label, name, readOnly ? NVS_READONLY : NVS_READWRITE, &_handle);
} else {
err = nvs_open(name, readOnly?NVS_READONLY:NVS_READWRITE, &_handle);
}
if(err){
log_e("nvs_open failed: %s", nvs_error(err));
return false;

View File

@ -29,7 +29,7 @@ class Preferences {
Preferences();
~Preferences();
bool begin(const char * name, bool readOnly=false);
bool begin(const char * name, bool readOnly=false, const char* partition_label=NULL);
void end();
bool clear();