From 91b92264d729d4c634561eb131217eaa679599e2 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Thu, 2 Jun 2022 08:19:55 +0200 Subject: [PATCH] Polishing get_type API entpoint --- CLC-qthing/SiliconTorch/NVSExplorer.cpp | 85 +++++++++++++++++++++++++ CLC-qthing/SiliconTorch/NVSExplorer.hpp | 5 ++ CLC-qthing/SiliconTorch/NVSExplorer.md | 5 ++ 3 files changed, 95 insertions(+) diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.cpp b/CLC-qthing/SiliconTorch/NVSExplorer.cpp index 963be75..0f3c3de 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.cpp +++ b/CLC-qthing/SiliconTorch/NVSExplorer.cpp @@ -603,6 +603,12 @@ namespace SiliconTorch { } )); + qthing::add_binary_message_callback(requestTopic("get_type/+/+"), wrapMessageHandler( + [&](std::vector<std::string>& topic, qthing::multipart_message_t msg) { + qthing::publish_message(responseTopic("get_type", topic[1], topic[2]), getTypeStr(topic[1], topic[2])); + } )); + + } @@ -1096,6 +1102,85 @@ namespace SiliconTorch { } + /** + * Converts IDF NVS types to strings + */ + std::string NVSExplorer::type2str(nvs_type_t typ) { + switch (typ) { + case NVS_TYPE_U8: return "u8"; + case NVS_TYPE_U16: return "u16"; + case NVS_TYPE_U32: return "u32"; + case NVS_TYPE_U64: return "u64"; + case NVS_TYPE_I8: return "i8"; + case NVS_TYPE_I16: return "i16"; + case NVS_TYPE_I32: return "i32"; + case NVS_TYPE_I64: return "i64"; + case NVS_TYPE_STR: return "str"; + case NVS_TYPE_BLOB: return "blob"; + default: return "any"; + } + } + + + /** + * Get the type of the entry, NVS_TYPE_ANY if not found + */ + nvs_type_t NVSExplorer::getType(const std::string& nameSpace, const std::string& key) { + + nvs_type_t entryType = NVS_TYPE_ANY; + + 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); + return entryType; + } + + it = nvs_entry_next(it); + } + + return entryType; + } + + /** + * Like getType but converts them to string + * non-existing keys are converted to `void` instead of `any` + */ + std::string NVSExplorer::getTypeStr(const std::string& nameSpace, const std::string& key) { + + bool entryFound = false; + nvs_type_t entryType = NVS_TYPE_ANY; + + 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) { + entryFound = true; + entryType = entry.type; + nvs_release_iterator(it); + break; + } + + it = nvs_entry_next(it); + } + + std::string typ; + + if (entryFound) typ = type2str(entryType); + else typ = "void"; + + return typ; + } + + 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 8728eb3..a281f6b 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.hpp +++ b/CLC-qthing/SiliconTorch/NVSExplorer.hpp @@ -88,6 +88,11 @@ namespace SiliconTorch { bool setString(const std::string& nameSpace, const std::string& key, const std::string& value); + std::string type2str(nvs_type_t typ); + nvs_type_t getType(const std::string& nameSpace, const std::string& key); + std::string getTypeStr(const std::string& nameSpace, const std::string& key); + + std::string deviceTopic(const std::string& suffix); std::string requestTopic(const std::string& cmd); diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.md b/CLC-qthing/SiliconTorch/NVSExplorer.md index 33ee37b..68103c1 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.md +++ b/CLC-qthing/SiliconTorch/NVSExplorer.md @@ -51,6 +51,11 @@ In the following list, `argument` stands for the message content received on the response: `RESPONSE/get/NS/KEY` description: reads the value and returns it in its natural form +- `get_type` - **get type of key** + request: `REQUEST/get_type/NS/KEY` + response: `RESPONSE/get_type/NS/KEY` + description: reads the type and converts it to `string`; returns `void` in case of non-existing `key` + - `get_raw` - **get generic value** request: `REQUEST/get_raw/NS/KEY` response: `RESPONSE/get_raw/NS/KEY` -- GitLab