|
@@ -1,16 +1,18 @@
|
|
|
-#include "kbf_nvs.h"
|
|
|
+#include "kbf/nvs.h"
|
|
|
|
|
|
#include <esp_log.h>
|
|
|
#include <nvs_flash.h>
|
|
|
|
|
|
-#include "kbf_assert.h"
|
|
|
+#include "kbf/macros.h"
|
|
|
|
|
|
-static constexpr const char *TAG = "kbf::nvs";
|
|
|
+using namespace kbf;
|
|
|
+
|
|
|
+static constexpr const char *TAG = "nvs";
|
|
|
|
|
|
static std::atomic<bool> initialized = {false};
|
|
|
|
|
|
-void kbf::nvs::init() {
|
|
|
- ESP_LOGI(TAG, "init()");
|
|
|
+void nvs::init() {
|
|
|
+ ESP_LOGD(TAG, "init()");
|
|
|
|
|
|
if (initialized) {
|
|
|
ESP_LOGW(TAG, "NVS already initialized");
|
|
@@ -27,30 +29,31 @@ void kbf::nvs::init() {
|
|
|
initialized = true;
|
|
|
}
|
|
|
|
|
|
-void kbf::nvs::erase() {
|
|
|
+void nvs::erase() {
|
|
|
ESP_LOGW(TAG, "erasing flash");
|
|
|
CHECK(nvs_flash_erase());
|
|
|
initialized = false;
|
|
|
}
|
|
|
|
|
|
-kbf::nvs::NVS::NVS(const string &name) : name(name) {
|
|
|
+nvs::NVS::NVS(const string &name) : name(name) {
|
|
|
+ ESP_LOGD(TAG, "NVS(\"%s\")", name.c_str());
|
|
|
+
|
|
|
if (!initialized) {
|
|
|
init();
|
|
|
}
|
|
|
|
|
|
- ESP_LOGI(TAG, "opening handle for namespace \"%s\"", name.c_str());
|
|
|
CHECK(nvs_open(name.c_str(), NVS_READWRITE, &handle));
|
|
|
}
|
|
|
|
|
|
-kbf::nvs::NVS::~NVS() {
|
|
|
- ESP_LOGI(TAG, "closing handle for namespace \"%s\"", name.c_str());
|
|
|
+nvs::NVS::~NVS() {
|
|
|
+ ESP_LOGD(TAG, "~NVS(\"%s\")", name.c_str());
|
|
|
nvs_close(handle);
|
|
|
}
|
|
|
|
|
|
static bool checkReadError(const string &key, const string &value, esp_err_t err) {
|
|
|
switch (err) {
|
|
|
case ESP_OK:
|
|
|
- ESP_LOGI(TAG, "read %s from %s", value.c_str(), key.c_str());
|
|
|
+ ESP_LOGD(TAG, "read %s from %s", value.c_str(), key.c_str());
|
|
|
return true;
|
|
|
case ESP_ERR_NVS_NOT_FOUND:
|
|
|
ESP_LOGW(TAG, "key not found: %s", key.c_str());
|
|
@@ -62,79 +65,79 @@ static bool checkReadError(const string &key, const string &value, esp_err_t err
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-bool kbf::nvs::NVS::read<int8_t>(const string &key, int8_t &value) {
|
|
|
+bool nvs::NVS::read<int8_t>(const string &key, int8_t &value) {
|
|
|
esp_err_t err = nvs_get_i8(handle, key.c_str(), &value);
|
|
|
return checkReadError(key, std::to_string(value), err);
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-bool kbf::nvs::NVS::read<uint8_t>(const string &key, uint8_t &value) {
|
|
|
+bool nvs::NVS::read<uint8_t>(const string &key, uint8_t &value) {
|
|
|
esp_err_t err = nvs_get_u8(handle, key.c_str(), &value);
|
|
|
return checkReadError(key, std::to_string(value), err);
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-bool kbf::nvs::NVS::read<int16_t>(const string &key, int16_t &value) {
|
|
|
+bool nvs::NVS::read<int16_t>(const string &key, int16_t &value) {
|
|
|
esp_err_t err = nvs_get_i16(handle, key.c_str(), &value);
|
|
|
return checkReadError(key, std::to_string(value), err);
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-bool kbf::nvs::NVS::read<uint16_t>(const string &key, uint16_t &value) {
|
|
|
+bool nvs::NVS::read<uint16_t>(const string &key, uint16_t &value) {
|
|
|
esp_err_t err = nvs_get_u16(handle, key.c_str(), &value);
|
|
|
return checkReadError(key, std::to_string(value), err);
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-bool kbf::nvs::NVS::read<int32_t>(const string &key, int32_t &value) {
|
|
|
+bool nvs::NVS::read<int32_t>(const string &key, int32_t &value) {
|
|
|
esp_err_t err = nvs_get_i32(handle, key.c_str(), &value);
|
|
|
return checkReadError(key, std::to_string(value), err);
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-bool kbf::nvs::NVS::read<uint32_t>(const string &key, uint32_t &value) {
|
|
|
+bool nvs::NVS::read<uint32_t>(const string &key, uint32_t &value) {
|
|
|
esp_err_t err = nvs_get_u32(handle, key.c_str(), &value);
|
|
|
return checkReadError(key, std::to_string(value), err);
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-void kbf::nvs::NVS::write<int8_t>(const string &key, int8_t &value) {
|
|
|
- ESP_LOGI(TAG, "writing %d to %s", value, key.c_str());
|
|
|
+void nvs::NVS::write<int8_t>(const string &key, int8_t &value) {
|
|
|
+ ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
|
|
|
CHECK(nvs_set_i8(handle, key.c_str(), value));
|
|
|
CHECK(nvs_commit(handle));
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-void kbf::nvs::NVS::write<uint8_t>(const string &key, uint8_t &value) {
|
|
|
- ESP_LOGI(TAG, "writing %d to %s", value, key.c_str());
|
|
|
+void nvs::NVS::write<uint8_t>(const string &key, uint8_t &value) {
|
|
|
+ ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
|
|
|
CHECK(nvs_set_u8(handle, key.c_str(), value));
|
|
|
CHECK(nvs_commit(handle));
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-void kbf::nvs::NVS::write<int16_t>(const string &key, int16_t &value) {
|
|
|
- ESP_LOGI(TAG, "writing %d to %s", value, key.c_str());
|
|
|
+void nvs::NVS::write<int16_t>(const string &key, int16_t &value) {
|
|
|
+ ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
|
|
|
CHECK(nvs_set_i16(handle, key.c_str(), value));
|
|
|
CHECK(nvs_commit(handle));
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-void kbf::nvs::NVS::write<uint16_t>(const string &key, uint16_t &value) {
|
|
|
- ESP_LOGI(TAG, "writing %d to %s", value, key.c_str());
|
|
|
+void nvs::NVS::write<uint16_t>(const string &key, uint16_t &value) {
|
|
|
+ ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
|
|
|
CHECK(nvs_set_u16(handle, key.c_str(), value));
|
|
|
CHECK(nvs_commit(handle));
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-void kbf::nvs::NVS::write<int32_t>(const string &key, int32_t &value) {
|
|
|
- ESP_LOGI(TAG, "writing %d to %s", value, key.c_str());
|
|
|
+void nvs::NVS::write<int32_t>(const string &key, int32_t &value) {
|
|
|
+ ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
|
|
|
CHECK(nvs_set_i32(handle, key.c_str(), value));
|
|
|
CHECK(nvs_commit(handle));
|
|
|
}
|
|
|
|
|
|
template<>
|
|
|
-void kbf::nvs::NVS::write<uint32_t>(const string &key, uint32_t &value) {
|
|
|
- ESP_LOGI(TAG, "writing %u to %s", value, key.c_str());
|
|
|
+void nvs::NVS::write<uint32_t>(const string &key, uint32_t &value) {
|
|
|
+ ESP_LOGD(TAG, "writing %u to %s", value, key.c_str());
|
|
|
CHECK(nvs_set_u32(handle, key.c_str(), value));
|
|
|
CHECK(nvs_commit(handle));
|
|
|
}
|