From db19e574c009b361ea02b63d5ec860f6f24df49c Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sat, 18 Dec 2021 23:06:06 +0100 Subject: [PATCH] Implemented getter for qr codes --- main/qrimport.cpp | 62 ++++++++++++++++++++++++++++++---------- main/qrimport.h | 5 ++-- main/settingspersister.h | 2 ++ 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/main/qrimport.cpp b/main/qrimport.cpp index 6e84f7d..634558d 100644 --- a/main/qrimport.cpp +++ b/main/qrimport.cpp @@ -1,42 +1,74 @@ #include "qrimport.h" +// esp-idf includes +#include +#include // 3rd party includes -#include #include #include #include #include // local includes +#include "globals.h" namespace qrimport { namespace { +constexpr const char * const TAG = "QRIMPORT"; + cpputils::DelayedConstruction http_request; } // namespace // nvs -tl::expected get_qr_code(std::string_view key) -{ - return ""; -} - -tl::expected set_qr_code(std::string_view qrcode, std::string_view key) -{ - return{}; -} - bool has_qr_code(std::string_view key) { - if (const auto result = get_qr_code(key); !result) + const auto handle = settingsPersister.getCommonHandle(); + + size_t length; + if (const esp_err_t result = nvs_get_str(handle, key.data(), nullptr, &length); result != ESP_OK) { - return (result.error() == ESP_ERR_NVS_NOT_FOUND); + if (result != ESP_ERR_NVS_NOT_FOUND) + ESP_LOGW(TAG, "nvs_get_str() size-only for key %.*s failed with %s", key.size(), key.data(), esp_err_to_name(result)); + return false; } - else + + return length; +} + +tl::expected get_qr_code(std::string_view key) +{ + const auto handle = settingsPersister.getCommonHandle(); + + size_t length; + if (const esp_err_t result = nvs_get_str(handle, key.data(), nullptr, &length); result != ESP_OK) { - return true; + ESP_LOGW(TAG, "nvs_get_str() size-only for key %.*s failed with %s", key.size(), key.data(), esp_err_to_name(result)); + return tl::make_unexpected(result); } + + // empty string optimization + if (!length) + return {}; + + std::string buf; + buf.resize(length); + if (const esp_err_t result = nvs_get_str(handle, key.data(), buf.data(), &length); result != ESP_OK) + { + ESP_LOGW(TAG, "nvs_get_str() for key %.*s failed with %s", key.size(), key.data(), esp_err_to_name(result)); + return tl::make_unexpected(result); + } + + if (buf.back() == '\n') + buf.resize(buf.size() - 1); + + return buf; // no std::move needed as return is optimized +} + +tl::expected set_qr_code(std::string_view key, std::string_view qrcode) +{ + return{}; } // web request diff --git a/main/qrimport.h b/main/qrimport.h index 3bd0911..f941a2b 100644 --- a/main/qrimport.h +++ b/main/qrimport.h @@ -6,6 +6,7 @@ */ // system includes +//#include #include // 3rd party includes @@ -16,9 +17,9 @@ namespace qrimport { // nvs - tl::expected get_qr_code(std::string_view key); - tl::expected set_qr_code(std::string_view qrcode, std::string_view key); bool has_qr_code(std::string_view key); + tl::expected get_qr_code(std::string_view key); + tl::expected set_qr_code(std::string_view key, std::string_view qrcode); // web request void setup_request(); diff --git a/main/settingspersister.h b/main/settingspersister.h index 02dfe92..19f5254 100644 --- a/main/settingspersister.h +++ b/main/settingspersister.h @@ -22,6 +22,8 @@ public: std::optional currentlyOpenProfileIndex() const; + nvs_handle getCommonHandle() { return m_handle; } + private: // for common settings nvs_handle m_handle{};