From 57ce8614c48dd9b2d98300dc80121839ec37c7fc Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Wed, 31 Jul 2024 12:51:12 -0700 Subject: [PATCH] split NVS functionality into own code --- CMakeLists.txt | 4 +++- include/cfglr_log.h | 14 ++++++++++++++ include/cfglr_nvs.h | 13 +++++++++++++ include/configulator.h | 21 +++++++-------------- src/cfglr_nvs.c | 11 +++++++++++ src/configulator.c | 13 +------------ 6 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 include/cfglr_log.h create mode 100644 include/cfglr_nvs.h create mode 100644 src/cfglr_nvs.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 1baa7db..11653e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,13 @@ set(REQUIRES "") +set(SRCS "src/configulator.c") if(${ESP_PLATFORM}) list(APPEND REQUIRES nvs_flash) + list(APPEND SRCS "src/cfglr_nvs.c") endif() idf_component_register( - SRCS src/configulator.c + SRCS ${SRCS} INCLUDE_DIRS "include" # if platform esp... REQUIRES ${REQUIRES} diff --git a/include/cfglr_log.h b/include/cfglr_log.h new file mode 100644 index 0000000..5a3defd --- /dev/null +++ b/include/cfglr_log.h @@ -0,0 +1,14 @@ +#ifndef _CFGLR_LOG_H_ +#define _CFGLR_LOG_H_ + +#ifdef ESP_PLATFORM +#include "esp_log.h" +#include "cfglr_nvs.h" + +#define CFGLR_LOGI(TAG, STR, ...) ESP_LOGI(TAG, STR, ##__VA_ARGS__) + +#else +#define CFGLR_LOGI(TAG, STR, ...) print(TAG); printf(STR, ##__VA_ARGS__); +#endif + +#endif diff --git a/include/cfglr_nvs.h b/include/cfglr_nvs.h new file mode 100644 index 0000000..1772812 --- /dev/null +++ b/include/cfglr_nvs.h @@ -0,0 +1,13 @@ +#ifndef __CFGLR_NVS_H__ +#define __CFGLR_NVS_H__ +#if ESP_PLATFORM +#include "nvs_flash.h" +#include "configulator.h" + +void cfglr_backend_nvs_open(cfglr_backend_t *backend, cfglr_handle_t *handle); +void cfglr_backend_nvs_close(cfglr_backend_t *backend, cfglr_handle_t *handle); + +#define CFGLR_BACKEND_NVS() CFGLR_BACKEND(&cfglr_backend_nvs_open, &cfglr_backend_nvs_close) + +#endif +#endif diff --git a/include/configulator.h b/include/configulator.h index e74c74a..33b27c1 100644 --- a/include/configulator.h +++ b/include/configulator.h @@ -1,9 +1,7 @@ #ifndef __CONFIGULATOR_H__ #define __CONFIGULATOR_H__ -#if ESP_PLATFORM -#include "nvs_flash.h" -#endif +#include #define CFGLR_BACKENDS_MAX (04) #define CFGLR_ELEMENTS_MAX (04) @@ -22,7 +20,7 @@ typedef enum { } cfglr_datatype_e; typedef void (*cfglr_read_callback)(const char *key, void *out); -typedef void (*cfglr_write_callback)(const char *key, void *valoue, size_t length); +typedef void (*cfglr_write_callback)(const char *key, void *valoue, uint8_t length); typedef struct { cfglr_read_callback read; @@ -31,7 +29,7 @@ typedef struct { typedef struct { const char *key; - bool dirty; + uint8_t dirty; cfglr_datatype_e datatype; void *default_data; void *data; @@ -53,16 +51,11 @@ typedef void (*cfglr_backend_open_t)(cfglr_backend_t *backend, cfglr_handle_t *h typedef void (*cfglr_backend_close_t)(cfglr_backend_t *backend, cfglr_handle_t *handle); typedef void (*cfglr_backend_get_t)(cfglr_backend_t *backend, cfglr_element_t *element, cfglr_handle_t *handle); -void cfglr_backend_nvs_open(cfglr_backend_t *backend, cfglr_handle_t *handle); -void cfglr_backend_nvs_close(cfglr_backend_t *backend, cfglr_handle_t *handle); - struct cfglr_backend { cfglr_backend_open_t open; cfglr_backend_close_t close; cfglr_backend_get_t get; -#if ESP_PLATFORM - nvs_handle_t nvs_handle; -#endif + void *handle; }; #define CFGLR_BACKEND(OPEN, CLOSE) {\ @@ -70,11 +63,9 @@ struct cfglr_backend { .close = CLOSE,\ } -#define CFGLR_BACKEND_NVS() CFGLR_BACKEND(&cfglr_backend_nvs_open, &cfglr_backend_nvs_close) - struct cfglr_handle_struct { const char *namespace; - bool store_defaults; + uint8_t store_defaults; cfglr_backend_t backends[CFGLR_BACKENDS_MAX]; cfglr_element_t elements[CFGLR_ELEMENTS_MAX]; }; @@ -82,4 +73,6 @@ struct cfglr_handle_struct { uint8_t cfglr_init(cfglr_handle_t *handle); uint8_t cfglr_fetch_data(cfglr_backend_t *backend, cfglr_element_t *element, cfglr_handle_t *handler); +#include "cfglr_nvs.h" + #endif//__CONFIGULATOR_H__ diff --git a/src/cfglr_nvs.c b/src/cfglr_nvs.c new file mode 100644 index 0000000..298bda7 --- /dev/null +++ b/src/cfglr_nvs.c @@ -0,0 +1,11 @@ +#include "cfglr_log.h" + +#define TAG "CFGLR_NVS" + +void cfglr_backend_nvs_open(cfglr_backend_t *backend, cfglr_handle_t *handle) { + CFGLR_LOGI(TAG, "opening NVS partition: %s", handle->namespace); + esp_err_t err = nvs_open(handle->namespace, NVS_READWRITE, (nvs_handle_t*)&backend->handle); +} + +void cfglr_backend_nvs_close(cfglr_backend_t *backend, cfglr_handle_t *handle) { +} diff --git a/src/configulator.c b/src/configulator.c index acb9dae..fd1fcd8 100644 --- a/src/configulator.c +++ b/src/configulator.c @@ -1,19 +1,8 @@ #include "configulator.h" -#ifdef ESP_PLATFORM -#include "nvs_flash.h" -#include "esp_log.h" +#include "cfglr_log.h" #define TAG "CFGLR" -void cfglr_backend_nvs_open(cfglr_backend_t *backend, cfglr_handle_t *handle) { - ESP_LOGI(TAG, "opening NVS partition: %s", handle->namespace); - esp_err_t err = nvs_open(handle->namespace, NVS_READWRITE, &backend->nvs_handle); -} - -void cfglr_backend_nvs_close(cfglr_backend_t *backend, cfglr_handle_t *handle) { -} -#endif - uint8_t cfglr_init(cfglr_handle_t *handle) { uint8_t tick = 0; cfglr_backend_t *backend;