ALL YOUR CONFIGULATORS ARE BELONG TO US
This commit is contained in:
commit
9206858ea0
3 changed files with 139 additions and 0 deletions
12
CMakeLists.txt
Normal file
12
CMakeLists.txt
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
set(REQUIRES "")
|
||||||
|
|
||||||
|
if(${ESP_PLATFORM})
|
||||||
|
list(APPEND REQUIRES nvs_flash)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
idf_component_register(
|
||||||
|
SRCS src/configulator.c
|
||||||
|
INCLUDE_DIRS "include"
|
||||||
|
# if platform esp...
|
||||||
|
REQUIRES ${REQUIRES}
|
||||||
|
)
|
85
include/configulator.h
Normal file
85
include/configulator.h
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
#ifndef __CONFIGULATOR_H__
|
||||||
|
#define __CONFIGULATOR_H__
|
||||||
|
|
||||||
|
#if ESP_PLATFORM
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CFGLR_BACKENDS_MAX (04)
|
||||||
|
#define CFGLR_ELEMENTS_MAX (04)
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
CFGLR_DATATYPE_U8,
|
||||||
|
CFGLR_DATATYPE_I8,
|
||||||
|
CFGLR_DATATYPE_U16,
|
||||||
|
CFGLR_DATATYPE_I16,
|
||||||
|
CFGLR_DATATYPE_U32,
|
||||||
|
CFGLR_DATATYPE_I32,
|
||||||
|
CFGLR_DATATYPE_U64,
|
||||||
|
CFGLR_DATATYPE_I64,
|
||||||
|
CFGLR_DATATYPE_STR,
|
||||||
|
CFGLR_DATATYPE_BIN,
|
||||||
|
} 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 struct {
|
||||||
|
cfglr_read_callback read;
|
||||||
|
cfglr_write_callback write;
|
||||||
|
} cfglr_data_handler_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *key;
|
||||||
|
bool dirty;
|
||||||
|
cfglr_datatype_e datatype;
|
||||||
|
void *default_data;
|
||||||
|
void *data;
|
||||||
|
} cfglr_element_t;
|
||||||
|
|
||||||
|
#define CFGLR_ELEMENT(KEY, TYPE, DEFAULT) (cfglr_element_t){\
|
||||||
|
.key = KEY,\
|
||||||
|
.dirty = 1,\
|
||||||
|
.default_data = (void*)DEFAULT,\
|
||||||
|
.datatype = TYPE\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define CFGLR_ELEMENT_U8(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U8, DEFAULT)
|
||||||
|
|
||||||
|
typedef struct cfglr_backend cfglr_backend_t;
|
||||||
|
typedef struct cfglr_handle_struct cfglr_handle_t;
|
||||||
|
|
||||||
|
typedef void (*cfglr_backend_open_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);
|
||||||
|
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
#define CFGLR_BACKEND(OPEN, CLOSE) {\
|
||||||
|
.open = OPEN,\
|
||||||
|
.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;
|
||||||
|
cfglr_backend_t backends[CFGLR_BACKENDS_MAX];
|
||||||
|
cfglr_element_t elements[CFGLR_ELEMENTS_MAX];
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
#endif//__CONFIGULATOR_H__
|
42
src/configulator.c
Normal file
42
src/configulator.c
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#include "configulator.h"
|
||||||
|
#ifdef ESP_PLATFORM
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "esp_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;
|
||||||
|
cfglr_element_t *element;
|
||||||
|
|
||||||
|
while((backend = &handle->backends[tick])) {
|
||||||
|
if(backend->open == NULL) break;
|
||||||
|
|
||||||
|
backend->open(backend, handle);
|
||||||
|
|
||||||
|
++tick;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick = 0;
|
||||||
|
|
||||||
|
while((element = &handle->elements[tick])) {
|
||||||
|
if(element->key == NULL) break;
|
||||||
|
|
||||||
|
// all elements should start out dirty, =
|
||||||
|
// they should be fetched, or defaulted
|
||||||
|
if(element->dirty) {
|
||||||
|
backend->get(backend, element, handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in a new issue