From 1e4f2c7fcbb4f19604937eb502f952c9ef91db31 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Sat, 24 Oct 2020 07:53:12 -0600 Subject: [PATCH 1/5] Added isKey method to Preferences --- libraries/Preferences/src/Preferences.cpp | 22 +++++++++++++++++++++- libraries/Preferences/src/Preferences.h | 3 ++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index f06e9d0862a..8e8c37452e5 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -15,7 +15,7 @@ #include "nvs.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_LENGHT"}; +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]) Preferences::Preferences() @@ -280,6 +280,26 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len){ return len; } +bool Preferences::isKey(const char* key) { + if(!_started || !key){ + return false; + } + int8_t mt1; uint8_t mt2; int16_t mt3; uint16_t mt4; + int32_t mt5; uint32_t mt6; int64_t mt7; uint64_t mt8; + size_t len = 0; + if(!nvs_get_i8(_handle, key, &mt1)) return true; + if(!nvs_get_u8(_handle, key, &mt2)) return true; + if(!nvs_get_i16(_handle, key, &mt3)) return true; + if(!nvs_get_u16(_handle, key, &mt4)) return true; + if(!nvs_get_i32(_handle, key, &mt5)) return true; + if(!nvs_get_u32(_handle, key, &mt6)) return true; + if(!nvs_get_i64(_handle, key, &mt7)) return true; + if(!nvs_get_u64(_handle, key, &mt8)) return true; + if(!nvs_get_str(_handle, key, NULL, &len)) return true; + if(!nvs_get_blob(_handle, key, NULL, &len)) return true; + return false; +} + /* * Get a key value * */ diff --git a/libraries/Preferences/src/Preferences.h b/libraries/Preferences/src/Preferences.h index 0ad94afbbad..91b2709be7a 100644 --- a/libraries/Preferences/src/Preferences.h +++ b/libraries/Preferences/src/Preferences.h @@ -48,6 +48,7 @@ class Preferences { size_t putString(const char* key, String value); size_t putBytes(const char* key, const void* value, size_t len); + bool isKey(const char* key); int8_t getChar(const char* key, int8_t defaultValue = 0); uint8_t getUChar(const char* key, uint8_t defaultValue = 0); int16_t getShort(const char* key, int16_t defaultValue = 0); @@ -63,7 +64,7 @@ class Preferences { bool getBool(const char* key, bool defaultValue = false); size_t getString(const char* key, char* value, size_t maxLen); String getString(const char* key, String defaultValue = String()); - size_t getBytesLength(const char* key); + size_t getBytesLength(const char* key); size_t getBytes(const char* key, void * buf, size_t maxLen); size_t freeEntries(); }; From 754f6294a0f4c1e50b54e1d03989a9588036d49d Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Sun, 25 Oct 2020 22:14:20 -0600 Subject: [PATCH 2/5] Changed to assert against ESP_OK --- libraries/Preferences/src/Preferences.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index 8e8c37452e5..9d83926acd1 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -287,16 +287,16 @@ bool Preferences::isKey(const char* key) { int8_t mt1; uint8_t mt2; int16_t mt3; uint16_t mt4; int32_t mt5; uint32_t mt6; int64_t mt7; uint64_t mt8; size_t len = 0; - if(!nvs_get_i8(_handle, key, &mt1)) return true; - if(!nvs_get_u8(_handle, key, &mt2)) return true; - if(!nvs_get_i16(_handle, key, &mt3)) return true; - if(!nvs_get_u16(_handle, key, &mt4)) return true; - if(!nvs_get_i32(_handle, key, &mt5)) return true; - if(!nvs_get_u32(_handle, key, &mt6)) return true; - if(!nvs_get_i64(_handle, key, &mt7)) return true; - if(!nvs_get_u64(_handle, key, &mt8)) return true; - if(!nvs_get_str(_handle, key, NULL, &len)) return true; - if(!nvs_get_blob(_handle, key, NULL, &len)) return true; + if(nvs_get_i8(_handle, key, &mt1) == ESP_OK) return true; + if(nvs_get_u8(_handle, key, &mt2) == ESP_OK) return true; + if(nvs_get_i16(_handle, key, &mt3) == ESP_OK) return true; + if(nvs_get_u16(_handle, key, &mt4) == ESP_OK) return true; + if(nvs_get_i32(_handle, key, &mt5) == ESP_OK) return true; + if(nvs_get_u32(_handle, key, &mt6) == ESP_OK) return true; + if(nvs_get_i64(_handle, key, &mt7) == ESP_OK) return true; + if(nvs_get_u64(_handle, key, &mt8) == ESP_OK) return true; + if(nvs_get_str(_handle, key, NULL, &len) == ESP_OK) return true; + if(nvs_get_blob(_handle, key, NULL, &len) == ESP_OK) return true; return false; } From 0cf8fbbdc03067a13a9c072be9e7fe892cfd34ab Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Tue, 3 Nov 2020 11:56:51 +0200 Subject: [PATCH 3/5] Update Preferences.h --- libraries/Preferences/src/Preferences.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/Preferences/src/Preferences.h b/libraries/Preferences/src/Preferences.h index 91b2709be7a..1b8be270dd4 100644 --- a/libraries/Preferences/src/Preferences.h +++ b/libraries/Preferences/src/Preferences.h @@ -16,6 +16,10 @@ #include "Arduino.h" +typedef enum { + PT_I8, PT_U8, PT_I16, PT_U16, PT_I32, PT_U32, PT_I64, PT_U64, PT_STR, PT_BLOB, PT_INVALID +} PreferenceType; + class Preferences { protected: uint32_t _handle; @@ -49,6 +53,7 @@ class Preferences { size_t putBytes(const char* key, const void* value, size_t len); bool isKey(const char* key); + PreferenceType getType(const char* key); int8_t getChar(const char* key, int8_t defaultValue = 0); uint8_t getUChar(const char* key, uint8_t defaultValue = 0); int16_t getShort(const char* key, int16_t defaultValue = 0); From 947ea9cc738b4947e42dddedb29d29fa5a4d03c7 Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Tue, 3 Nov 2020 12:00:55 +0200 Subject: [PATCH 4/5] Update Preferences.cpp --- libraries/Preferences/src/Preferences.cpp | 30 +++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index 9d83926acd1..85a23ef8e2c 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -280,24 +280,28 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len){ return len; } -bool Preferences::isKey(const char* key) { +PreferenceType Preferences::getType(const char* key) { if(!_started || !key){ - return false; + return PT_INVALID; } int8_t mt1; uint8_t mt2; int16_t mt3; uint16_t mt4; int32_t mt5; uint32_t mt6; int64_t mt7; uint64_t mt8; size_t len = 0; - if(nvs_get_i8(_handle, key, &mt1) == ESP_OK) return true; - if(nvs_get_u8(_handle, key, &mt2) == ESP_OK) return true; - if(nvs_get_i16(_handle, key, &mt3) == ESP_OK) return true; - if(nvs_get_u16(_handle, key, &mt4) == ESP_OK) return true; - if(nvs_get_i32(_handle, key, &mt5) == ESP_OK) return true; - if(nvs_get_u32(_handle, key, &mt6) == ESP_OK) return true; - if(nvs_get_i64(_handle, key, &mt7) == ESP_OK) return true; - if(nvs_get_u64(_handle, key, &mt8) == ESP_OK) return true; - if(nvs_get_str(_handle, key, NULL, &len) == ESP_OK) return true; - if(nvs_get_blob(_handle, key, NULL, &len) == ESP_OK) return true; - return false; + if(nvs_get_i8(_handle, key, &mt1) == ESP_OK) return PT_I8; + if(nvs_get_u8(_handle, key, &mt2) == ESP_OK) return PT_U8; + if(nvs_get_i16(_handle, key, &mt3) == ESP_OK) return PT_I16; + if(nvs_get_u16(_handle, key, &mt4) == ESP_OK) return PT_U16; + if(nvs_get_i32(_handle, key, &mt5) == ESP_OK) return PT_I32; + if(nvs_get_u32(_handle, key, &mt6) == ESP_OK) return PT_U32; + if(nvs_get_i64(_handle, key, &mt7) == ESP_OK) return PT_I64; + if(nvs_get_u64(_handle, key, &mt8) == ESP_OK) return PT_U64; + if(nvs_get_str(_handle, key, NULL, &len) == ESP_OK) return PT_STR; + if(nvs_get_blob(_handle, key, NULL, &len) == ESP_OK) return PT_BLOB; + return PT_INVALID; +} + +bool Preferences::isKey(const char* key) { + return getType(key) != PT_INVALID; } /* From 1f7f2505cd7c2b2e4bd95f063823efa10a931757 Mon Sep 17 00:00:00 2001 From: Larry Bernstone Date: Tue, 3 Nov 2020 07:39:04 -0700 Subject: [PATCH 5/5] Added a check that key length is <16 --- libraries/Preferences/src/Preferences.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Preferences/src/Preferences.cpp b/libraries/Preferences/src/Preferences.cpp index 85a23ef8e2c..6b45d2c7db1 100644 --- a/libraries/Preferences/src/Preferences.cpp +++ b/libraries/Preferences/src/Preferences.cpp @@ -281,7 +281,7 @@ size_t Preferences::putBytes(const char* key, const void* value, size_t len){ } PreferenceType Preferences::getType(const char* key) { - if(!_started || !key){ + if(!_started || !key || strlen(key)>15){ return PT_INVALID; } int8_t mt1; uint8_t mt2; int16_t mt3; uint16_t mt4;