nvs.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "kbf/nvs.h"
  2. #include <esp_log.h>
  3. #include <nvs_flash.h>
  4. #include "kbf/macros.h"
  5. using namespace kbf;
  6. static constexpr const char *TAG = "nvs";
  7. static std::atomic<bool> initialized = {false};
  8. void nvs::init() {
  9. ESP_LOGD(TAG, "init()");
  10. if (initialized) {
  11. ESP_LOGW(TAG, "NVS already initialized");
  12. return;
  13. }
  14. esp_err_t err = nvs_flash_init();
  15. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  16. erase();
  17. err = nvs_flash_init();
  18. }
  19. CHECK(err);
  20. initialized = true;
  21. }
  22. void nvs::erase() {
  23. ESP_LOGW(TAG, "erasing flash");
  24. CHECK(nvs_flash_erase());
  25. initialized = false;
  26. }
  27. nvs::NVS::NVS(const string &name) : name(name) {
  28. ESP_LOGD(TAG, "NVS(\"%s\")", name.c_str());
  29. if (!initialized) {
  30. init();
  31. }
  32. CHECK(nvs_open(name.c_str(), NVS_READWRITE, &handle));
  33. }
  34. nvs::NVS::~NVS() {
  35. ESP_LOGD(TAG, "~NVS(\"%s\")", name.c_str());
  36. nvs_close(handle);
  37. }
  38. static bool checkReadError(const string &key, const string &value, esp_err_t err) {
  39. switch (err) {
  40. case ESP_OK:
  41. ESP_LOGD(TAG, "read %s from %s", value.c_str(), key.c_str());
  42. return true;
  43. case ESP_ERR_NVS_NOT_FOUND:
  44. ESP_LOGW(TAG, "key not found: %s", key.c_str());
  45. return false;
  46. default:
  47. CHECK(err); // this will always fail
  48. return false; // but the compiler cries anyway
  49. }
  50. }
  51. template<>
  52. bool nvs::NVS::read<int8_t>(const string &key, int8_t &value) {
  53. esp_err_t err = nvs_get_i8(handle, key.c_str(), &value);
  54. return checkReadError(key, std::to_string(value), err);
  55. }
  56. template<>
  57. bool nvs::NVS::read<uint8_t>(const string &key, uint8_t &value) {
  58. esp_err_t err = nvs_get_u8(handle, key.c_str(), &value);
  59. return checkReadError(key, std::to_string(value), err);
  60. }
  61. template<>
  62. bool nvs::NVS::read<int16_t>(const string &key, int16_t &value) {
  63. esp_err_t err = nvs_get_i16(handle, key.c_str(), &value);
  64. return checkReadError(key, std::to_string(value), err);
  65. }
  66. template<>
  67. bool nvs::NVS::read<uint16_t>(const string &key, uint16_t &value) {
  68. esp_err_t err = nvs_get_u16(handle, key.c_str(), &value);
  69. return checkReadError(key, std::to_string(value), err);
  70. }
  71. template<>
  72. bool nvs::NVS::read<int32_t>(const string &key, int32_t &value) {
  73. esp_err_t err = nvs_get_i32(handle, key.c_str(), &value);
  74. return checkReadError(key, std::to_string(value), err);
  75. }
  76. template<>
  77. bool nvs::NVS::read<uint32_t>(const string &key, uint32_t &value) {
  78. esp_err_t err = nvs_get_u32(handle, key.c_str(), &value);
  79. return checkReadError(key, std::to_string(value), err);
  80. }
  81. template<>
  82. void nvs::NVS::write<int8_t>(const string &key, int8_t &value) {
  83. ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
  84. CHECK(nvs_set_i8(handle, key.c_str(), value));
  85. CHECK(nvs_commit(handle));
  86. }
  87. template<>
  88. void nvs::NVS::write<uint8_t>(const string &key, uint8_t &value) {
  89. ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
  90. CHECK(nvs_set_u8(handle, key.c_str(), value));
  91. CHECK(nvs_commit(handle));
  92. }
  93. template<>
  94. void nvs::NVS::write<int16_t>(const string &key, int16_t &value) {
  95. ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
  96. CHECK(nvs_set_i16(handle, key.c_str(), value));
  97. CHECK(nvs_commit(handle));
  98. }
  99. template<>
  100. void nvs::NVS::write<uint16_t>(const string &key, uint16_t &value) {
  101. ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
  102. CHECK(nvs_set_u16(handle, key.c_str(), value));
  103. CHECK(nvs_commit(handle));
  104. }
  105. template<>
  106. void nvs::NVS::write<int32_t>(const string &key, int32_t &value) {
  107. ESP_LOGD(TAG, "writing %d to %s", value, key.c_str());
  108. CHECK(nvs_set_i32(handle, key.c_str(), value));
  109. CHECK(nvs_commit(handle));
  110. }
  111. template<>
  112. void nvs::NVS::write<uint32_t>(const string &key, uint32_t &value) {
  113. ESP_LOGD(TAG, "writing %u to %s", value, key.c_str());
  114. CHECK(nvs_set_u32(handle, key.c_str(), value));
  115. CHECK(nvs_commit(handle));
  116. }