From e06393ed9cfefb07584ee666ef7ced63c3994540 Mon Sep 17 00:00:00 2001 From: "Yu Ang, Tan" Date: Thu, 14 Jan 2021 17:54:47 +1100 Subject: [PATCH 1/2] Added Preferences::begin overload to select non-default NVS partition --- libraries/Preferences/src/Preferences.cpp | 20 ++++++++++++++++++++ libraries/Preferences/src/Preferences.h | 1 + 2 files changed, 21 insertions(+) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index 6b45d2c7db1..5c4868bcc37 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -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]) @@ -42,6 +43,25 @@ bool Preferences::begin(const char * name, bool readOnly){ return true; } +bool Preferences::begin(const char* partition_label, const char* name, bool readOnly) { + if (_started) { + return false; + } + _readOnly = readOnly; + esp_err_t 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); + if (err) { + log_e("nvs_open failed: %s", nvs_error(err)); + return false; + } + _started = true; + return true; +} + void Preferences::end(){ if(!_started){ return; diff --git a/libraries/Preferences/src/Preferences.h b/libraries/Preferences/src/Preferences.h index 1b8be270dd4..0d71151d1bd 100644 --- a/libraries/Preferences/src/Preferences.h +++ b/libraries/Preferences/src/Preferences.h @@ -30,6 +30,7 @@ class Preferences { ~Preferences(); bool begin(const char * name, bool readOnly=false); + bool begin(const char* partition_label, const char* name, bool readOnly = false); void end(); bool clear(); From 23ddd8cf86d8593baccfe623e630a7b7a0ce4c02 Mon Sep 17 00:00:00 2001 From: "Yu Ang, Tan" Date: Fri, 15 Jan 2021 20:56:09 +1100 Subject: [PATCH 2/2] Partition selection added as an optional argument --- libraries/Preferences/src/Preferences.cpp | 33 +++++++++-------------- libraries/Preferences/src/Preferences.h | 3 +-- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index 5c4868bcc37..3c1928b820f 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -29,32 +29,23 @@ 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); - if(err){ - log_e("nvs_open failed: %s", nvs_error(err)); - return false; + 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); } - _started = true; - return true; -} - -bool Preferences::begin(const char* partition_label, const char* name, bool readOnly) { - if (_started) { - return false; - } - _readOnly = readOnly; - esp_err_t 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); - if (err) { + if(err){ log_e("nvs_open failed: %s", nvs_error(err)); return false; } diff --git a/libraries/Preferences/src/Preferences.h b/libraries/Preferences/src/Preferences.h index 0d71151d1bd..5dbcbd4cb4f 100644 --- a/libraries/Preferences/src/Preferences.h +++ b/libraries/Preferences/src/Preferences.h @@ -29,8 +29,7 @@ class Preferences { Preferences(); ~Preferences(); - bool begin(const char * name, bool readOnly=false); - bool begin(const char* partition_label, const char* name, bool readOnly = false); + bool begin(const char * name, bool readOnly=false, const char* partition_label=NULL); void end(); bool clear();