Refactor NVS and WiFi handlers for improved memory management and logging
- Updated KVStorageHandler interface to use std::string instead of char* for key-value operations. - Enhanced NVSStorageHandler to utilize ESP_LOG for error and info messages instead of printf. - Refactored WifiHandler to manage WiFi credentials using JSON format for better structure and storage. - Replaced raw pointers with std::unique_ptr in WifiHandler and NetworkHandler for automatic memory management. - Removed unused TouchHandler and EInkTouchHandler classes to clean up the codebase. - Adjusted CMakeLists.txt to remove unnecessary include directories. - Updated lv_conf.h to enable FreeRTOS and gesture recognition features.
This commit is contained in:
15
main/io/io.h
15
main/io/io.h
@@ -3,8 +3,8 @@
|
||||
#include "freertos/event_groups.h"
|
||||
#include <memory>
|
||||
|
||||
typedef bool(*FilterFunc)(const char* const& key);
|
||||
typedef void (*KeyValueProcessor)(void* arg, const char* const& key, const char* const& value);
|
||||
typedef bool(*FilterFunc)(const std::string& key);
|
||||
typedef void (*KeyValueProcessor)(void* arg, const std::string& key, const std::string& value);
|
||||
|
||||
class KVStorageHandler {
|
||||
public:
|
||||
@@ -13,15 +13,14 @@ public:
|
||||
virtual void init(const EventGroupHandle_t& system_event_group) = 0;
|
||||
|
||||
// Store a key-value pair
|
||||
virtual void put(const char* const& key, const char* const& value) = 0;
|
||||
virtual void put(const std::string& key, const std::string& value) = 0;
|
||||
|
||||
// Retrieve a value by key, returns nullptr if key not found
|
||||
// The caller is responsible for freeing the returned memory
|
||||
virtual std::unique_ptr<char[]> get(const char* const& key) const = 0;
|
||||
// Retrieve a value by key, returns empty string if key not found
|
||||
virtual std::string get(const std::string& key) const = 0;
|
||||
virtual esp_err_t process_all(KeyValueProcessor processor, void* arg) const = 0;
|
||||
virtual esp_err_t process_filtered(const char* const& key_prefix, KeyValueProcessor processor, void* arg) const = 0;
|
||||
virtual esp_err_t process_filtered(const std::string& key_prefix, KeyValueProcessor processor, void* arg) const = 0;
|
||||
virtual esp_err_t process_filtered(FilterFunc filter_func, KeyValueProcessor processor, void* arg) const = 0;
|
||||
|
||||
// Delete a key-value pair
|
||||
virtual void remove(const char* const& key) = 0;
|
||||
virtual void remove(const std::string& key) = 0;
|
||||
};
|
||||
@@ -2,6 +2,9 @@
|
||||
#include "io/nvs_handler.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "string.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
#define TAG "NVSStorageHandler"
|
||||
|
||||
NVSStorageHandler::NVSStorageHandler(
|
||||
const char* name_space
|
||||
@@ -24,49 +27,51 @@ void NVSStorageHandler::init(const EventGroupHandle_t& system_event_group) {
|
||||
|
||||
err = nvs_open(this->name_space, NVS_READWRITE, &this->nvsHandle);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) opening NVS handle!", esp_err_to_name(err));
|
||||
} else {
|
||||
xEventGroupSetBits(system_event_group, STORAGE_READY_BIT);
|
||||
printf("NVS Storage initialized.\n");
|
||||
if (system_event_group != nullptr) {
|
||||
xEventGroupSetBits(system_event_group, STORAGE_READY_BIT);
|
||||
}
|
||||
ESP_LOGI(TAG, "NVS Storage initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
void NVSStorageHandler::put(const char* const& key, const char* const& value) {
|
||||
void NVSStorageHandler::put(const std::string& key, const std::string& value) {
|
||||
if (this->nvsHandle == 0) {
|
||||
printf("NVS handle is not initialized.\n");
|
||||
ESP_LOGE(TAG, "NVS handle is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t err = nvs_set_str(this->nvsHandle, key, value);
|
||||
esp_err_t err = nvs_set_str(this->nvsHandle, key.c_str(), value.c_str());
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) setting key-value pair in NVS!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) setting key-value pair in NVS!", esp_err_to_name(err));
|
||||
} else {
|
||||
nvs_commit(this->nvsHandle);
|
||||
printf("Key-value pair (%s, %s) stored in NVS.\n", key, value);
|
||||
ESP_LOGI(TAG, "Key-value pair (%s, %s) stored in NVS.", key.c_str(), value.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<char[]> NVSStorageHandler::get(const char* const& key) const {
|
||||
std::string NVSStorageHandler::get(const std::string& key) const {
|
||||
if (this->nvsHandle == 0) {
|
||||
printf("NVS handle is not initialized.\n");
|
||||
return nullptr;
|
||||
ESP_LOGE(TAG, "NVS handle is not initialized.");
|
||||
return "";
|
||||
}
|
||||
|
||||
size_t required_size = 0;
|
||||
esp_err_t err = nvs_get_str(this->nvsHandle, key, nullptr, &required_size);
|
||||
esp_err_t err = nvs_get_str(this->nvsHandle, key.c_str(), nullptr, &required_size);
|
||||
if (err == ESP_ERR_NVS_NOT_FOUND) {
|
||||
printf("Key %s not found in NVS.\n", key);
|
||||
return nullptr;
|
||||
ESP_LOGW(TAG, "Key %s not found in NVS.", key.c_str());
|
||||
return "";
|
||||
} else if (err != ESP_OK) {
|
||||
printf("Error (%s) getting size for key %s from NVS!\n", esp_err_to_name(err), key);
|
||||
return nullptr;
|
||||
ESP_LOGE(TAG, "Error (%s) getting size for key %s from NVS!", esp_err_to_name(err), key.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
std::unique_ptr<char[]> value(new char[required_size]);
|
||||
err = nvs_get_str(this->nvsHandle, key, value.get(), &required_size);
|
||||
std::string value;
|
||||
err = nvs_get_str(this->nvsHandle, key.c_str(), value.data(), &required_size);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) getting value for key %s from NVS!\n", esp_err_to_name(err), key);
|
||||
return nullptr;
|
||||
ESP_LOGE(TAG, "Error (%s) getting value for key %s from NVS!", esp_err_to_name(err), key.c_str());
|
||||
return "";
|
||||
}
|
||||
|
||||
return value;
|
||||
@@ -76,7 +81,7 @@ NVSIteratorGuard NVSStorageHandler::create_iterator() const {
|
||||
nvs_iterator_t it = nullptr;
|
||||
esp_err_t err = nvs_entry_find(NVS_DEFAULT_PART_NAME, this->name_space, NVS_TYPE_ANY, &it);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) creating NVS iterator!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) creating NVS iterator!", esp_err_to_name(err));
|
||||
return NVSIteratorGuard(nullptr, err);
|
||||
}
|
||||
|
||||
@@ -94,22 +99,23 @@ esp_err_t NVSStorageHandler::process_all(KeyValueProcessor processor, void* arg)
|
||||
nvs_entry_info_t info;
|
||||
esp_err_t err = nvs_entry_info(it, &info);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) getting NVS entry info!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) getting NVS entry info!", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
nvs_handle_t temp_handle;
|
||||
err = nvs_open(this->name_space, NVS_READONLY, &temp_handle);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) opening NVS handle for reading!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) opening NVS handle for reading!", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
// call the processor with the key and value
|
||||
processor(arg, info.key, this->get(info.key).get());
|
||||
std::string key_str = info.key;
|
||||
processor(arg, key_str, this->get(key_str));
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t NVSStorageHandler::process_filtered(const char* const& key_prefix, KeyValueProcessor processor, void* arg) const {
|
||||
esp_err_t NVSStorageHandler::process_filtered(const std::string& key_prefix, KeyValueProcessor processor, void* arg) const {
|
||||
NVSIteratorGuard iterator_guard = this->create_iterator();
|
||||
if (!iterator_guard.is_valid()) {
|
||||
return iterator_guard.get_error();
|
||||
@@ -120,19 +126,19 @@ esp_err_t NVSStorageHandler::process_filtered(const char* const& key_prefix, Key
|
||||
nvs_entry_info_t info;
|
||||
esp_err_t err = nvs_entry_info(it, &info);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) getting NVS entry info!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) getting NVS entry info!", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
// check if the key matches the prefix
|
||||
if (strncmp(info.key, key_prefix, strlen(key_prefix)) == 0) {
|
||||
if (strncmp(info.key, key_prefix.c_str(), key_prefix.length()) == 0) {
|
||||
nvs_handle_t temp_handle;
|
||||
err = nvs_open(this->name_space, NVS_READONLY, &temp_handle);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) opening NVS handle for reading!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) opening NVS handle for reading!", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
// call the processor with the key and value
|
||||
processor(arg, info.key, this->get(info.key).get());
|
||||
processor(arg, std::string(info.key), this->get(std::string(info.key)));
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
@@ -149,35 +155,36 @@ esp_err_t NVSStorageHandler::process_filtered(FilterFunc filter_func, KeyValuePr
|
||||
nvs_entry_info_t info;
|
||||
esp_err_t err = nvs_entry_info(it, &info);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) getting NVS entry info!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) getting NVS entry info!", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
// check if the key matches the filter function
|
||||
if (filter_func(info.key)) {
|
||||
std::string key_str(info.key);
|
||||
if (filter_func(key_str)) {
|
||||
nvs_handle_t temp_handle;
|
||||
err = nvs_open(this->name_space, NVS_READONLY, &temp_handle);
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) opening NVS handle for reading!\n", esp_err_to_name(err));
|
||||
ESP_LOGE(TAG, "Error (%s) opening NVS handle for reading!", esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
// call the processor with the key and value
|
||||
processor(arg, info.key, this->get(info.key).get());
|
||||
processor(arg, key_str, this->get(key_str));
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void NVSStorageHandler::remove(const char* const& key) {
|
||||
void NVSStorageHandler::remove(const std::string& key) {
|
||||
if (this->nvsHandle == 0) {
|
||||
printf("NVS handle is not initialized.\n");
|
||||
ESP_LOGE(TAG, "NVS handle is not initialized.");
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t err = nvs_erase_key(this->nvsHandle, key);
|
||||
esp_err_t err = nvs_erase_key(this->nvsHandle, key.c_str());
|
||||
if (err != ESP_OK) {
|
||||
printf("Error (%s) deleting key %s from NVS!\n", esp_err_to_name(err), key);
|
||||
ESP_LOGE(TAG, "Error (%s) deleting key %s from NVS!", esp_err_to_name(err), key.c_str());
|
||||
} else {
|
||||
nvs_commit(this->nvsHandle);
|
||||
printf("Key %s deleted from NVS.\n", key);
|
||||
ESP_LOGI(TAG, "Key %s deleted from NVS.", key.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,14 +53,14 @@ public:
|
||||
|
||||
void init(const EventGroupHandle_t& system_event_group) override;
|
||||
|
||||
void put(const char* const& key, const char* const& value) override;
|
||||
void put(const std::string& key, const std::string& value) override;
|
||||
|
||||
std::unique_ptr<char[]> get(const char* const& key) const override;
|
||||
std::string get(const std::string& key) const override;
|
||||
esp_err_t process_all(KeyValueProcessor processor, void* arg) const override;
|
||||
esp_err_t process_filtered(const char* const& key_prefix, KeyValueProcessor processor, void* arg) const override;
|
||||
esp_err_t process_filtered(const std::string& key_prefix, KeyValueProcessor processor, void* arg) const override;
|
||||
esp_err_t process_filtered(FilterFunc filter_func, KeyValueProcessor processor, void* arg) const override;
|
||||
|
||||
void remove(const char* const& key) override;
|
||||
void remove(const std::string& key) override;
|
||||
|
||||
private:
|
||||
NVSIteratorGuard create_iterator() const;
|
||||
|
||||
Reference in New Issue
Block a user