Prechádzať zdrojové kódy

DK-4 implement NVS::write for strings

Bence Balint 3 rokov pred
rodič
commit
fbab0bb9b6
3 zmenil súbory, kde vykonal 57 pridanie a 12 odobranie
  1. 8 8
      include/kbf/nvs.h
  2. 22 0
      src/nvs.cpp
  3. 27 4
      test/test_nvs.cpp

+ 8 - 8
include/kbf/nvs.h

@@ -7,8 +7,6 @@
 #include <esp_log.h>
 #include <nvs_flash.h>
 
-using std::string;
-
 namespace kbf::nvs {
     /**
      * @brief Non-Volatile Storage read / write functions.
@@ -20,12 +18,12 @@ namespace kbf::nvs {
          *
          * @param name NVS namespace name; default is "storage"
          */
-        explicit NVS(const string &name = "storage");
+        explicit NVS(const std::string &name = "storage");
 
         ~NVS();
 
         /**
-         * @brief Reads a numeric value from NVS.
+         * @brief Reads a value from NVS.
          *
          * @tparam T value type
          * @param key name
@@ -33,20 +31,22 @@ namespace kbf::nvs {
          * @return true if successful; false if key not found
          */
         template<typename T>
-        bool read(const string &key, T &value);
+        bool read(const std::string &key, T &value);
 
         /**
-         * @brief Writes a numeric value to NVS.
+         * @brief Writes a value to NVS.
+         *
+         * @warning writing string values that contain '\0' doesn't work properly
          *
          * @tparam T value type
          * @param key name
          * @param value value
          */
         template<typename T>
-        void write(const string &key, T &value);
+        void write(const std::string &key, T &value);
 
         /** @brief NVS namespace name */
-        const string name;
+        const std::string name;
 
     private:
         nvs_handle_t handle{};

+ 22 - 0
src/nvs.cpp

@@ -6,6 +6,7 @@
 #include "kbf/macros.h"
 
 using namespace kbf;
+using std::string;
 
 static constexpr const char *TAG = "nvs";
 
@@ -100,6 +101,20 @@ bool nvs::NVS::read<uint32_t>(const string &key, uint32_t &value) {
     return checkReadError(key, std::to_string(value), err);
 }
 
+template<>
+bool nvs::NVS::read<string>(const string &key, string &value) {
+    size_t size = 0;
+    esp_err_t err = nvs_get_blob(handle, key.c_str(), nullptr, &size);
+    if (!checkReadError(key, value, err)) return false;
+    if (size == 0) return false;
+
+    char data[size];
+    err = nvs_get_blob(handle, key.c_str(), data, &size);
+    if (!checkReadError(key, value, err)) return false;
+    value = string(data, size - 1);
+    return true;
+}
+
 template<>
 void nvs::NVS::write<int8_t>(const string &key, int8_t &value) {
     ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
@@ -141,3 +156,10 @@ void nvs::NVS::write<uint32_t>(const string &key, uint32_t &value) {
     CHECK(nvs_set_u32(handle, key.c_str(), value));
     CHECK(nvs_commit(handle));
 }
+
+template<>
+void nvs::NVS::write<string>(const string &key, string &value) {
+    ESP_LOGD(TAG, "writing %s to %s", value.c_str(), key.c_str());
+    CHECK(nvs_set_blob(handle, key.c_str(), value.data(), value.size() + 1));
+    CHECK(nvs_commit(handle));
+}

+ 27 - 4
test/test_nvs.cpp

@@ -2,9 +2,11 @@
 
 #include <kbf/nvs.h>
 
-TEST_CASE("NVS write/read numeric values", "[kbf_nvs]") {
-    static constexpr const char *KEY = "kbf_nvs_test";
+static constexpr const char *NAMESPACE = "kbf_nvs_test";
+
+using std::string;
 
+TEST_CASE("NVS write/read numeric values", "[kbf_nvs]") {
     int      TEST_INT = -2147483648;
     int8_t   TEST_I8  = -128;
     uint8_t  TEST_U8  = 255;
@@ -16,7 +18,7 @@ TEST_CASE("NVS write/read numeric values", "[kbf_nvs]") {
 //    kbf::nvs::init();
 //    kbf::nvs::erase();
 
-    auto nvs = kbf::nvs::NVS(KEY);
+    auto nvs = kbf::nvs::NVS(NAMESPACE);
 
     int readInt = 0;
     nvs.write("int", TEST_INT);
@@ -49,4 +51,25 @@ TEST_CASE("NVS write/read numeric values", "[kbf_nvs]") {
     nvs.write("u32", TEST_U32);
     TEST_ASSERT_TRUE(nvs.read("u32", readU32));
     TEST_ASSERT_EQUAL(TEST_U32, readU32);
-};
+}
+
+TEST_CASE("NVS write/read string values", "[kbf_nvs]") {
+    string readString;
+    string TEST_STRING = "string_value";
+
+    auto nvs = kbf::nvs::NVS(NAMESPACE);
+
+    nvs.write("string", TEST_STRING);
+    TEST_ASSERT_TRUE(nvs.read("string", readString))
+    TEST_ASSERT_EQUAL(TEST_STRING.length(), readString.length());
+    TEST_ASSERT_EQUAL_STRING(TEST_STRING.c_str(), readString.c_str());
+
+    string TEST_BIN_WITH_NULL = "\x13\x37\0foo"; // NOLINT(bugprone-string-literal-with-embedded-nul)
+    nvs.write("binary", TEST_BIN_WITH_NULL);
+    TEST_ASSERT_TRUE(nvs.read("binary", readString))
+//    TEST_ASSERT_EQUAL(6, readString.size());
+    TEST_ASSERT_EQUAL(2, readString.size()); // TODO should be 6
+    for (int i = 0; i < TEST_BIN_WITH_NULL.size(); i++) {
+        TEST_ASSERT_EQUAL(TEST_BIN_WITH_NULL[i], readString[i]);
+    }
+}