split NVS functionality into own code

This commit is contained in:
Morgan 'ARR\!' Allen 2024-07-31 12:51:12 -07:00
parent 9206858ea0
commit 57ce8614c4
6 changed files with 49 additions and 27 deletions

View file

@ -1,11 +1,13 @@
set(REQUIRES "") set(REQUIRES "")
set(SRCS "src/configulator.c")
if(${ESP_PLATFORM}) if(${ESP_PLATFORM})
list(APPEND REQUIRES nvs_flash) list(APPEND REQUIRES nvs_flash)
list(APPEND SRCS "src/cfglr_nvs.c")
endif() endif()
idf_component_register( idf_component_register(
SRCS src/configulator.c SRCS ${SRCS}
INCLUDE_DIRS "include" INCLUDE_DIRS "include"
# if platform esp... # if platform esp...
REQUIRES ${REQUIRES} REQUIRES ${REQUIRES}

14
include/cfglr_log.h Normal file
View file

@ -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

13
include/cfglr_nvs.h Normal file
View file

@ -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

View file

@ -1,9 +1,7 @@
#ifndef __CONFIGULATOR_H__ #ifndef __CONFIGULATOR_H__
#define __CONFIGULATOR_H__ #define __CONFIGULATOR_H__
#if ESP_PLATFORM #include <stdint.h>
#include "nvs_flash.h"
#endif
#define CFGLR_BACKENDS_MAX (04) #define CFGLR_BACKENDS_MAX (04)
#define CFGLR_ELEMENTS_MAX (04) #define CFGLR_ELEMENTS_MAX (04)
@ -22,7 +20,7 @@ typedef enum {
} cfglr_datatype_e; } cfglr_datatype_e;
typedef void (*cfglr_read_callback)(const char *key, void *out); 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 { typedef struct {
cfglr_read_callback read; cfglr_read_callback read;
@ -31,7 +29,7 @@ typedef struct {
typedef struct { typedef struct {
const char *key; const char *key;
bool dirty; uint8_t dirty;
cfglr_datatype_e datatype; cfglr_datatype_e datatype;
void *default_data; void *default_data;
void *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_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); 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 { struct cfglr_backend {
cfglr_backend_open_t open; cfglr_backend_open_t open;
cfglr_backend_close_t close; cfglr_backend_close_t close;
cfglr_backend_get_t get; cfglr_backend_get_t get;
#if ESP_PLATFORM void *handle;
nvs_handle_t nvs_handle;
#endif
}; };
#define CFGLR_BACKEND(OPEN, CLOSE) {\ #define CFGLR_BACKEND(OPEN, CLOSE) {\
@ -70,11 +63,9 @@ struct cfglr_backend {
.close = CLOSE,\ .close = CLOSE,\
} }
#define CFGLR_BACKEND_NVS() CFGLR_BACKEND(&cfglr_backend_nvs_open, &cfglr_backend_nvs_close)
struct cfglr_handle_struct { struct cfglr_handle_struct {
const char *namespace; const char *namespace;
bool store_defaults; uint8_t store_defaults;
cfglr_backend_t backends[CFGLR_BACKENDS_MAX]; cfglr_backend_t backends[CFGLR_BACKENDS_MAX];
cfglr_element_t elements[CFGLR_ELEMENTS_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_init(cfglr_handle_t *handle);
uint8_t cfglr_fetch_data(cfglr_backend_t *backend, cfglr_element_t *element, cfglr_handle_t *handler); uint8_t cfglr_fetch_data(cfglr_backend_t *backend, cfglr_element_t *element, cfglr_handle_t *handler);
#include "cfglr_nvs.h"
#endif//__CONFIGULATOR_H__ #endif//__CONFIGULATOR_H__

11
src/cfglr_nvs.c Normal file
View file

@ -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) {
}

View file

@ -1,19 +1,8 @@
#include "configulator.h" #include "configulator.h"
#ifdef ESP_PLATFORM #include "cfglr_log.h"
#include "nvs_flash.h"
#include "esp_log.h"
#define TAG "CFGLR" #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 cfglr_init(cfglr_handle_t *handle) {
uint8_t tick = 0; uint8_t tick = 0;
cfglr_backend_t *backend; cfglr_backend_t *backend;