Add NVS storage handler and integrate with main application logic

This commit is contained in:
GW_MC
2026-01-18 14:46:25 +08:00
parent e458256193
commit d339a1f4c3
7 changed files with 135 additions and 2 deletions

View File

@@ -0,0 +1,20 @@
#pragma once
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
class KVStorageHandler {
public:
virtual ~KVStorageHandler() = default;
virtual void init(const EventGroupHandle_t& system_event_group) = 0;
// Store a key-value pair
virtual void put(const char*& key, const char*& value) = 0;
// Retrieve a value by key, returns nullptr if key not found
// The caller is responsible for freeing the returned memory
virtual char* get(const char*& key) const = 0;
// Delete a key-value pair
virtual void remove(const char*& key) = 0;
};

84
main/io/nvs_handler.cpp Normal file
View File

@@ -0,0 +1,84 @@
#include "common/constants.h"
#include "io/nvs_handler.h"
#include "nvs_flash.h"
NVSStorageHandler::~NVSStorageHandler() {
if (this->nvsHandle != 0) {
nvs_close(this->nvsHandle);
this->nvsHandle = 0;
}
}
void NVSStorageHandler::init(const EventGroupHandle_t& system_event_group) {
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
nvs_flash_erase();
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
err = nvs_open("storage", NVS_READWRITE, &this->nvsHandle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
} else {
xEventGroupSetBits(system_event_group, STORAGE_READY_BIT);
printf("NVS Storage initialized.\n");
}
}
void NVSStorageHandler::put(const char*& key, const char*& value) {
if (this->nvsHandle == 0) {
printf("NVS handle is not initialized.\n");
return;
}
esp_err_t err = nvs_set_str(this->nvsHandle, key, value);
if (err != ESP_OK) {
printf("Error (%s) setting key-value pair in NVS!\n", esp_err_to_name(err));
} else {
nvs_commit(this->nvsHandle);
printf("Key-value pair (%s, %s) stored in NVS.\n", key, value);
}
}
char* NVSStorageHandler::get(const char*& key) const {
if (this->nvsHandle == 0) {
printf("NVS handle is not initialized.\n");
return nullptr;
}
size_t required_size = 0;
esp_err_t err = nvs_get_str(this->nvsHandle, key, nullptr, &required_size);
if (err == ESP_ERR_NVS_NOT_FOUND) {
printf("Key %s not found in NVS.\n", key);
return nullptr;
} else if (err != ESP_OK) {
printf("Error (%s) getting size for key %s from NVS!\n", esp_err_to_name(err), key);
return nullptr;
}
char* value = (char*)malloc(required_size);
err = nvs_get_str(this->nvsHandle, key, value, &required_size);
if (err != ESP_OK) {
printf("Error (%s) getting value for key %s from NVS!\n", esp_err_to_name(err), key);
free(value);
return nullptr;
}
return value;
}
void NVSStorageHandler::remove(const char*& key) {
if (this->nvsHandle == 0) {
printf("NVS handle is not initialized.\n");
return;
}
esp_err_t err = nvs_erase_key(this->nvsHandle, key);
if (err != ESP_OK) {
printf("Error (%s) deleting key %s from NVS!\n", esp_err_to_name(err), key);
} else {
nvs_commit(this->nvsHandle);
printf("Key %s deleted from NVS.\n", key);
}
}

21
main/io/nvs_handler.h Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include "io/io.h"
#include "freertos/FreeRTOS.h"
#include "nvs.h"
class NVSStorageHandler : public KVStorageHandler {
public:
NVSStorageHandler() = default;
~NVSStorageHandler() override;
void init(const EventGroupHandle_t& system_event_group) override;
void put(const char*& key, const char*& value) override;
char* get(const char*& key) const override;
void remove(const char*& key) override;
private:
nvs_handle_t nvsHandle = 0;
};