Skip to content

Added Preferences::begin overload to select non-default NVS partition #4718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions libraries/Preferences/src/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion libraries/Preferences/src/Preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down