From 940f38cc99a6f7effb239025a694afe2fc7d6a4b Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Fri, 27 May 2022 09:01:07 +0200
Subject: [PATCH] Generic getters for integer types

---
 CLC-qthing/SiliconTorch/NVSExplorer.cpp | 55 +++++++++++++++++++++----
 CLC-qthing/SiliconTorch/NVSExplorer.hpp |  4 +-
 2 files changed, 50 insertions(+), 9 deletions(-)

diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.cpp b/CLC-qthing/SiliconTorch/NVSExplorer.cpp
index 79093a4..8bc4fb7 100644
--- a/CLC-qthing/SiliconTorch/NVSExplorer.cpp
+++ b/CLC-qthing/SiliconTorch/NVSExplorer.cpp
@@ -262,21 +262,62 @@ namespace SiliconTorch {
       return namespaces;
     }
 
-    void NVSExplorer::switchNS(const std::string& newNS) {
-      mutex.run([&]() {
-        // if (currentNS != NULL && currentNS.compare(newNS) != 0) {
 
-          // currentNS = newNS;
+    int64_t  getSignedInt(const std::string& nameSpace, const std::string& key, int64_t defaultValue) {
 
+      // Lets first test & evaluate the other function before duplication………
 
+      return -1337LL;
+    }
+
+
+    uint64_t getUnsignedInt(const std::string& nameSpace, const std::string& key, uint64_t defaultValue) {
+
+      uint64_t value = defaultValue;
+      nvs_type_t entryType = NVS_TYPE_ANY;
+
+      // search for the key to get its type
+      nvs_iterator_t it = nvs_entry_find("nvs", nameSpace.c_str(), NVS_TYPE_ANY);
+
+      while (it != NULL) {
+        nvs_entry_info_t entry;
+        nvs_entry_info(it, &entry);
+
+        if (key.compare(entry.key) == 0) {
+          entryType = entry.type;
+          nvs_release_iterator(it);
+          break;
+        }
 
-          // esp_err_t err = nvs_open("__test__", NVS_READWRITE, &nvs);
+        it = nvs_entry_next(it);
+      }
 
-        // }
-      });
+      if (entryType == NVS_TYPE_ANY)  // key not found
+        return defaultValue;
+
+      nvs_handle_t nvs;
+      if ( nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs) != ESP_OK )  // namespace not found
+        return defaultValue;
+
+      uint64_t* valptr = &value;
+      switch (entryType) {
+        case NVS_TYPE_U8:  { nvs_get_u8(nvs,  key.c_str(), ( uint8_t*)valptr); break; }
+        case NVS_TYPE_I8:  { nvs_get_i8(nvs,  key.c_str(), (  int8_t*)valptr); break; }
+        case NVS_TYPE_U16: { nvs_get_u16(nvs, key.c_str(), (uint16_t*)valptr); break; }
+        case NVS_TYPE_I16: { nvs_get_i16(nvs, key.c_str(), ( int16_t*)valptr); break; }
+        case NVS_TYPE_U32: { nvs_get_u32(nvs, key.c_str(), (uint32_t*)valptr); break; }
+        case NVS_TYPE_I32: { nvs_get_i32(nvs, key.c_str(), ( int32_t*)valptr); break; }
+        case NVS_TYPE_U64: { nvs_get_u64(nvs, key.c_str(), (uint64_t*)valptr); break; }
+        case NVS_TYPE_I64: { nvs_get_i64(nvs, key.c_str(), ( int64_t*)valptr); break; }
+        default: {}
+      }
+
+      nvs_close(nvs);
+      return value;
     }
 
 
+
     std::string NVSExplorer::deviceTopic(const std::string& suffix) {
       return std::string(DEVICE_NAMESPACE + "NVS/") + suffix;
     }
diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.hpp b/CLC-qthing/SiliconTorch/NVSExplorer.hpp
index 02adb91..4573eeb 100644
--- a/CLC-qthing/SiliconTorch/NVSExplorer.hpp
+++ b/CLC-qthing/SiliconTorch/NVSExplorer.hpp
@@ -40,8 +40,8 @@ namespace SiliconTorch {
 
 
         // Gets the key regardless of its type in flash
-        int64_t  getSignedInt(const std::string& nameSpace, const std::string& key);
-        uint64_t getUnsignedInt(const std::string& nameSpace, const std::string& key);
+        int64_t  getSignedInt(const std::string& nameSpace, const std::string& key, int64_t defaultValue = 0);
+        uint64_t getUnsignedInt(const std::string& nameSpace, const std::string& key, uint64_t defaultValue = 0);
 
         float  getFloat(const std::string& nameSpace, const std::string& key);
         double getDouble(const std::string& nameSpace, const std::string& key);
-- 
GitLab