split NVS functionality into own code
This commit is contained in:
parent
9206858ea0
commit
57ce8614c4
6 changed files with 49 additions and 27 deletions
|
@ -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}
|
||||
|
|
14
include/cfglr_log.h
Normal file
14
include/cfglr_log.h
Normal 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
13
include/cfglr_nvs.h
Normal 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
|
|
@ -1,9 +1,7 @@
|
|||
#ifndef __CONFIGULATOR_H__
|
||||
#define __CONFIGULATOR_H__
|
||||
|
||||
#if ESP_PLATFORM
|
||||
#include "nvs_flash.h"
|
||||
#endif
|
||||
#include <stdint.h>
|
||||
|
||||
#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__
|
||||
|
|
11
src/cfglr_nvs.c
Normal file
11
src/cfglr_nvs.c
Normal 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) {
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue