From 3443598feba06a4f63408fe4407c1f2ddd261f89 Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Wed, 1 Jun 2022 06:41:30 +0200 Subject: [PATCH] Adding string API endpoints --- CLC-qthing/SiliconTorch/NVSExplorer.cpp | 142 +++++++++++++++++++++++- 1 file changed, 141 insertions(+), 1 deletion(-) diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.cpp b/CLC-qthing/SiliconTorch/NVSExplorer.cpp index cdff703..86d87d3 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.cpp +++ b/CLC-qthing/SiliconTorch/NVSExplorer.cpp @@ -536,7 +536,78 @@ namespace SiliconTorch { ESP_LOGI(TAG, "Set namespace[ %s ] key[ %s ] to i64[ %lld ]", topic[1].c_str(), topic[2].c_str(), value); else ESP_LOGW(TAG, "Setting namespace[ %s ] key[ %s ] to i64[ %lld ] FAILED", topic[1].c_str(), topic[2].c_str(), value); // publish error…? - }; + + + qthing::add_binary_message_callback(requestTopic("get_strLen/+/+"), wrapMessageHandler( + [&](std::vector<std::string>& topic, qthing::multipart_message_t msg) { + + size_t testValue = 1337000; // way too big for NVS + size_t length = getStringLength(topic[1], topic[2], testValue); + + if (length < testValue) { // key exists and is of type string + + char buffer[32]; + std::snprintf(&buffer[0], 32, "%d", length); + + std::string value(&buffer[0]); + qthing::publish_message(responseTopic("get_strLen", topic[1], topic[2]), value); + } else { + // publish error…? + + ESP_LOGW(TAG, "Retrieving |str|[] from namespace[ %s ] key[ %s ] FAILED", topic[1].c_str(), topic[2].c_str()); + } + } )); + + + + qthing::add_binary_message_callback(requestTopic("get_str/+/+"), wrapMessageHandler( + [&](std::vector<std::string>& topic, qthing::multipart_message_t msg) { + + size_t testValue = 1337000; // way too big for NVS + + if ( getStringLength(topic[1], topic[2], testValue) < testValue ) { // key exists and is of type string + + std::string value = getString(topic[1], topic[2]); + + qthing::publish_message(responseTopic("get_str", topic[1], topic[2]), value); + } else { + // publish error…? + + ESP_LOGW(TAG, "Retrieving str[] from namespace[ %s ] key[ %s ] FAILED", topic[1].c_str(), topic[2].c_str()); + } + } )); + + + + // ####################################### + // ### TODO: remove after testing!! ### + // ####################################### + + qthing::add_binary_message_callback(requestTopic("get_LONGstr/+/+"), wrapMessageHandler( + [&](std::vector<std::string>& topic, qthing::multipart_message_t msg) { + + size_t testValue = 1337000; // way too big for NVS + + if ( getStringLength(topic[1], topic[2], testValue) < testValue ) { // key exists and is of type string + + char buffer[256]; + std::memset(&buffer[0], 0x00, 256); + + getLongString(topic[1], topic[2], &buffer[0]); + + std::string value(&buffer[0]); + qthing::publish_message(responseTopic("get_LONGstr", topic[1], topic[2]), value); + } else { + // publish error…? + + ESP_LOGW(TAG, "Retrieving str[] from namespace[ %s ] key[ %s ] FAILED", topic[1].c_str(), topic[2].c_str()); + } + } )); + + // ####################################### + // ####################################### + + qthing::add_binary_message_callback(requestTopic("set_i64/+/+"), wrapMessageHandler(setI64)); @@ -842,6 +913,75 @@ namespace SiliconTorch { } + size_t NVSExplorer::getStringLength(const std::string& nameSpace, const std::string& key, size_t defaultValue) { + + size_t value = defaultValue; + + nvs_handle_t nvs; + if ( nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs) == ESP_OK ) { + nvs_get_str(nvs, key.c_str(), NULL, &value); + nvs_close(nvs); + } + + return value; + } + + + std::string NVSExplorer::getString(const std::string& nameSpace, const std::string& key, const std::string& defaultValue) { + + nvs_handle_t nvs; + if ( nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs) == ESP_OK ) { + + size_t size; + if ( nvs_get_str(nvs, key.c_str(), NULL, &size) == ESP_OK ) { + + char buffer[size + 1]; + std::memset(&buffer[0], 0x00, size + 1); + + nvs_get_str(nvs, key.c_str(), &buffer[0], &size); + nvs_close(nvs); + + std::string out(&buffer[0]); + return out; + } + } + + return defaultValue; + } + + + size_t NVSExplorer::getLongString(const std::string& nameSpace, const std::string& key, char* const buffer) { + + size_t size = 0; + + nvs_handle_t nvs; + if ( nvs_open(nameSpace.c_str(), NVS_READONLY, &nvs) == ESP_OK ) { + nvs_get_str(nvs, key.c_str(), buffer, &size); + nvs_close(nvs); + } + + return size; + } + + + bool NVSExplorer::setString(const std::string& nameSpace, const std::string& key, const std::string& value) { + + bool success = true; + + nvs_handle_t nvs; + if ( nvs_open(nameSpace.c_str(), NVS_READWRITE, &nvs) == ESP_OK ) { + success &= nvs_set_str(nvs, key.c_str(), value.c_str()) == ESP_OK; + success &= nvs_commit(nvs) == ESP_OK; + nvs_close(nvs); + + return success; + } else { + return false; + } + } + + + void NVSExplorer::rmKey(const std::string& nameSpace, const std::string& key) { nvs_handle_t _nvs; ESP_LOGW(TAG, "rmKey() -> %s / %s", nameSpace.c_str(), key.c_str()); -- GitLab