diff --git a/firmware/components/esp_persistence/include/esp_crud.h b/firmware/components/esp_persistence/include/esp_crud.h index c1a59db4..a76cbd42 100644 --- a/firmware/components/esp_persistence/include/esp_crud.h +++ b/firmware/components/esp_persistence/include/esp_crud.h @@ -58,27 +58,30 @@ class EspCrud final : public persistence::Crud> : part_name{a_part_name}, namespace_name{a_namespace_name} { + ESP_ERROR_CHECK(nvs_open(namespace_name, NVS_READWRITE, &nvs_handle)); + + ESP_LOGI(TAG, + "Started EspCrud for partition %s namespace %s with handle " + "%" PRIu32, + part_name, + namespace_name, + nvs_handle); } + ~EspCrud() final { nvs_close(nvs_handle); } + [[nodiscard]] int create(const std::span &data, uint32_t &id_out) override { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; // start at 0 in case last ID has not been saved to NVS uint32_t id = 0; - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(namespace_name, NVS_READWRITE, &nvs_handle)); - if(err != ESP_OK) - goto out; - ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_get_u32(nvs_handle, last_id_name, &id)); if(err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) - goto out; + return -1; id++; id_out = id; @@ -87,43 +90,32 @@ class EspCrud final : public persistence::Crud> err = nvs_set_blob( nvs_handle, id_to_key(id).c_str(), data.data(), data.size())); if(err != ESP_OK) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_set_u32(nvs_handle, last_id_name, id)); if(err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_commit(nvs_handle)); if(err != ESP_OK) - goto out; + return -1; - rc = 0; - - out: - nvs_close(nvs_handle); - return rc; + return 0; } /// \return non-zero on error [[nodiscard]] int read(uint32_t id, std::span &buffer) override { - nvs_handle_t nvs_handle; esp_err_t err; std::size_t required_size = 0; - int rc = -1; - - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(namespace_name, NVS_READONLY, &nvs_handle)); - if(err != ESP_OK) - goto out; // query the required size ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_get_blob( nvs_handle, id_to_key(id).c_str(), nullptr, &required_size)); if(err != ESP_OK) - goto out; + return -1; if(required_size > buffer.size()) { @@ -131,7 +123,7 @@ class EspCrud final : public persistence::Crud> "Not enough space in the output buffer. Need %zu, got %zu", required_size, buffer.size()); - goto out; + return -1; } ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_get_blob(nvs_handle, @@ -139,71 +131,46 @@ class EspCrud final : public persistence::Crud> buffer.data(), &required_size)); if(err != ESP_OK) - goto out; + return -1; buffer = buffer.subspan(0, required_size); - rc = 0; - out: - nvs_close(nvs_handle); - return rc; + return 0; }; /// \return non-zero on error [[nodiscard]] int update(uint32_t id, const std::span &data) override { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; - - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(namespace_name, NVS_READWRITE, &nvs_handle)); - if(err != ESP_OK) - goto out; ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_set_blob( nvs_handle, id_to_key(id).c_str(), data.data(), data.size())); if(err != ESP_OK) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_commit(nvs_handle)); if(err != ESP_OK) - goto out; - - rc = 0; + return -1; - out: - nvs_close(nvs_handle); - return rc; + return 0; }; /// \return non-zero on error [[nodiscard]] int destroy(uint32_t id) override { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; - - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(namespace_name, NVS_READWRITE, &nvs_handle)); - if(err != ESP_OK) - goto out; ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_erase_key(nvs_handle, id_to_key(id).c_str())); if(err != ESP_OK) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_commit(nvs_handle)); if(err != ESP_OK) - goto out; - - rc = 0; + return -1; - out: - nvs_close(nvs_handle); - return rc; + return 0; }; void for_each(etl::delegate &)> @@ -401,6 +368,7 @@ class EspCrud final : public persistence::Crud> const char *const part_name; const char *const namespace_name; + nvs_handle_t nvs_handle = 0; static constexpr char last_id_name[] = "last_id"; static constexpr char TAG[] = "esp_crud"; diff --git a/firmware/components/esp_persistence/include/esp_persistence.h b/firmware/components/esp_persistence/include/esp_persistence.h index cff669b2..2527ad24 100644 --- a/firmware/components/esp_persistence/include/esp_persistence.h +++ b/firmware/components/esp_persistence/include/esp_persistence.h @@ -20,6 +20,7 @@ #pragma once #include "persistence.h" +#include namespace shrapnel::persistence { @@ -30,15 +31,21 @@ namespace shrapnel::persistence { class EspStorage final : public Storage { public: + EspStorage(); + ~EspStorage(); + [[nodiscard]] int save(const char *key, std::span data) override; [[nodiscard]] int save(const char *key, etl::string_view data) override; [[nodiscard]] int save(const char *key, uint32_t data) override; - [[nodiscard]] virtual int save(const char *key, float data) override; + [[nodiscard]] int save(const char *key, float data) override; [[nodiscard]] int load(const char *key, std::span &data) override; [[nodiscard]] int load(const char *key, etl::istring &data) override; [[nodiscard]] int load(const char *key, uint32_t &data) override; [[nodiscard]] int load(const char *key, float &data) override; + +private: + nvs_handle_t nvs_handle = 0; }; } // namespace shrapnel::persistence diff --git a/firmware/components/esp_persistence/src/esp_persistence.cpp b/firmware/components/esp_persistence/src/esp_persistence.cpp index ffcf74d1..8c9296f4 100644 --- a/firmware/components/esp_persistence/src/esp_persistence.cpp +++ b/firmware/components/esp_persistence/src/esp_persistence.cpp @@ -26,140 +26,98 @@ namespace shrapnel::persistence { +EspStorage::EspStorage() +{ + ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &nvs_handle)); + + ESP_LOGI(TAG, "Started EspStorage for with handle %" PRIu32, nvs_handle); +} + +EspStorage::~EspStorage() { nvs_close(nvs_handle); } + int EspStorage::save(const char *key, std::span data) { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; - - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &nvs_handle)); - if(err != ESP_OK) - goto out; ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_set_blob(nvs_handle, key, data.data(), data.size())); if(err != ESP_OK) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_commit(nvs_handle)); if(err != ESP_OK) - goto out; - - rc = 0; + return -1; -out: - nvs_close(nvs_handle); - return rc; + return 0; } int EspStorage::save(const char *key, etl::string_view data) { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; etl::string<15> truncated_key{key}; - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &nvs_handle)); - if(err != ESP_OK) - goto out; - ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_set_blob( nvs_handle, truncated_key.data(), data.data(), data.size())); if(err != ESP_OK) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_commit(nvs_handle)); if(err != ESP_OK) - goto out; - - rc = 0; + return -1; -out: - nvs_close(nvs_handle); - return rc; + return 0; } int EspStorage::save(const char *key, uint32_t data) { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; etl::string<15> truncated_key{key}; - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &nvs_handle)); - if(err != ESP_OK) - goto out; - ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_set_u32(nvs_handle, truncated_key.data(), data)); if(err != ESP_OK) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_commit(nvs_handle)); if(err != ESP_OK) - goto out; - - rc = 0; + return -1; -out: - nvs_close(nvs_handle); - return rc; + return 0; } int EspStorage::save(const char *key, float data) { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; etl::string<15> truncated_key{key}; - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &nvs_handle)); - if(err != ESP_OK) - goto out; - uint32_t value; memcpy(&value, &data, sizeof value); ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_set_u32(nvs_handle, truncated_key.data(), value)); if(err != ESP_OK) - goto out; + return -1; ESP_ERROR_CHECK_WITHOUT_ABORT(err = nvs_commit(nvs_handle)); if(err != ESP_OK) - goto out; - - rc = 0; + return -1; -out: - nvs_close(nvs_handle); - return rc; + return 0; } int EspStorage::load(const char *key, std::span &buffer) { - nvs_handle_t nvs_handle; esp_err_t err; std::size_t required_size = 0; - int rc = -1; - - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READONLY, &nvs_handle)); - if(err != ESP_OK) - goto out; // query the required size ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_get_blob(nvs_handle, key, nullptr, &required_size)); if(err != ESP_OK) - goto out; + return -1; if(required_size > buffer.size()) { @@ -167,41 +125,31 @@ int EspStorage::load(const char *key, std::span &buffer) "Not enough space in the output buffer. Need %zu, got %zu", required_size, buffer.size()); - goto out; + return -1; } ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_get_blob(nvs_handle, key, buffer.data(), &required_size)); if(err != ESP_OK) - goto out; + return -1; buffer = buffer.subspan(0, required_size); - rc = 0; -out: - nvs_close(nvs_handle); - return rc; + return 0; } int EspStorage::load(const char *key, etl::istring &data) { - nvs_handle_t nvs_handle; esp_err_t err; std::size_t required_size = 0; - int rc = -1; etl::string<15> truncated_key{key}; - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READONLY, &nvs_handle)); - if(err != ESP_OK) - goto out; - // query the required size ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_get_blob( nvs_handle, truncated_key.data(), nullptr, &required_size)); if(err != ESP_OK) - goto out; + return -1; data.initialize_free_space(); data.uninitialized_resize(required_size); @@ -219,63 +167,40 @@ int EspStorage::load(const char *key, etl::istring &data) err = nvs_get_blob( nvs_handle, truncated_key.data(), data.data(), &required_size)); if(err != ESP_OK) - goto out; + return -1; - rc = 0; -out: - nvs_close(nvs_handle); - return rc; + return 0; } int EspStorage::load(const char *key, uint32_t &data) { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; etl::string<15> truncated_key{key}; - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READONLY, &nvs_handle)); - if(err != ESP_OK) - goto out; - ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_get_u32(nvs_handle, truncated_key.data(), &data)); if(err != ESP_OK) - goto out; + return -1; - rc = 0; -out: - nvs_close(nvs_handle); - return rc; + return 0; } int EspStorage::load(const char *key, float &data) { - nvs_handle_t nvs_handle; esp_err_t err; - int rc = -1; etl::string<15> truncated_key{key}; - ESP_ERROR_CHECK_WITHOUT_ABORT( - err = nvs_open(STORAGE_NAMESPACE, NVS_READONLY, &nvs_handle)); - if(err != ESP_OK) - goto out; - uint32_t value; ESP_ERROR_CHECK_WITHOUT_ABORT( err = nvs_get_u32(nvs_handle, truncated_key.data(), &value)); if(err != ESP_OK) - goto out; + return -1; memcpy(&data, &value, sizeof value); - rc = 0; -out: - nvs_close(nvs_handle); - return rc; + return 0; } } // namespace shrapnel::persistence \ No newline at end of file diff --git a/firmware/main/main.cpp b/firmware/main/main.cpp index 21454990..a180b411 100644 --- a/firmware/main/main.cpp +++ b/firmware/main/main.cpp @@ -297,7 +297,7 @@ extern "C" void app_main(void) /* Start the mdns service */ start_mdns(); -#if 1 +#if 0 rc = xTaskCreate(profiling_task, "i2s profiling", 2000,