blobs, signalers in element macros, and removing the big fucking if statement that was causing all the confusion

This commit is contained in:
Morgan 'ARR\!' Allen 2024-11-15 14:30:03 -08:00
parent 0cd6971e70
commit 85dc35dcc2
3 changed files with 64 additions and 51 deletions

View file

@ -49,27 +49,33 @@ struct cfglr_element {
cfglr_datatype_e datatype;
// TODO precalcualte sizes in an array and use datatype enum to look up
uint8_t datatype_size;
uint8_t default_size;
void *data;
void *default_data;
cfglr_handle_t *handle;
cfglr_signaler_t signalers[CFGLR_SIGNALERS_MAX];
};
#define CFGLR_ELEMENT(KEY, TYPE, DEFAULT, SIZE) (cfglr_element_t){\
#define CFGLR_ELEMENT(KEY, TYPE, SIZE, DEFAULT, DEF_SIZE, SIGNALERS...) (cfglr_element_t){\
.key = KEY,\
.datatype = TYPE,\
.datatype_size = SIZE,\
.default_data = (void*)DEFAULT,\
.default_size = DEF_SIZE,\
.signalers = {\
SIGNALERS,\
{ NULL }\
}\
}
#define CFGLR_ELEMENT_U8(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U8, DEFAULT, sizeof(uint8_t))
#define CFGLR_ELEMENT_I8(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I8, DEFAULT, sizeof(int8_t))
#define CFGLR_ELEMENT_U16(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U16, DEFAULT, sizeof(uint16_t))
#define CFGLR_ELEMENT_I16(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I16, DEFAULT, sizeof(int16_t))
#define CFGLR_ELEMENT_U32(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U32, DEFAULT, sizeof(uint32_t))
#define CFGLR_ELEMENT_I32(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I32, DEFAULT, sizeof(int32_t))
#define CFGLR_ELEMENT_U64(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U64, DEFAULT, sizeof(uint64_t))
#define CFGLR_ELEMENT_I64(KEY, DEFAULT) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I64, DEFAULT, sizeof(int64_t))
#define CFGLR_ELEMENT_U8(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U8, sizeof(uint8_t), DEFAULT, 1, SIGNALERS)
#define CFGLR_ELEMENT_I8(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I8, sizeof(int8_t), DEFAULT, 1, SIGNALERS)
#define CFGLR_ELEMENT_U16(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U16, sizeof(uint16_t), DEFAULT, 2, SIGNALERS)
#define CFGLR_ELEMENT_I16(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I16, sizeof(int16_t), DEFAULT, 2, SIGNALERS)
#define CFGLR_ELEMENT_U32(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U32, sizeof(uint32_t), DEFAULT, 4, SIGNALERS)
#define CFGLR_ELEMENT_I32(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I32, sizeof(int32_t), DEFAULT, 4, SIGNALERS)
#define CFGLR_ELEMENT_U64(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_U64, sizeof(uint64_t), DEFAULT, 8, SIGNALERS)
#define CFGLR_ELEMENT_I64(KEY, DEFAULT, SIGNALERS...) CFGLR_ELEMENT(KEY, CFGLR_DATATYPE_I64, sizeof(int64_t), DEFAULT, 8, SIGNALERS)
typedef struct cfglr_backend cfglr_backend_t;
@ -98,6 +104,7 @@ struct cfglr_backend {
struct cfglr_handle_struct {
const char *namespace;
// TODO cleanup, what is store_default for?
uint8_t store_default;
uint8_t backend_count;
uint8_t element_count;

View file

@ -17,7 +17,6 @@ void cfglr_backend_nvs_close(cfglr_backend_t *backend, cfglr_handle_t *handle) {
uint16_t cfglr_backend_nvs_get(cfglr_backend_t *backend, cfglr_element_t *element, cfglr_handle_t *handle) {
esp_err_t err = ESP_OK;
if(element->datatype <= CFGLR_DATATYPE_I32) {
switch(element->datatype) {
case CFGLR_DATATYPE_U8:
err = nvs_get_u8((nvs_handle_t)backend->handle, element->key, (uint8_t*)element->data);
@ -51,6 +50,17 @@ uint16_t cfglr_backend_nvs_get(cfglr_backend_t *backend, cfglr_element_t *elemen
err = nvs_get_i64((nvs_handle_t)backend->handle, element->key, (int64_t*)element->data);
break;
case CFGLR_DATATYPE_STR:
case CFGLR_DATATYPE_BIN:
uint8_t read_size = 0;
err = nvs_get_blob((nvs_handle_t)backend->handle, element->key, element->data, &read_size);
if(read_size != element->datatype_size) {
CFGLR_LOGW(TAG, "blob read size does not match declared size");
}
break;
default:
CFGLR_LOGI(TAG, "unhandlable datatype: %d", element->datatype);
}
@ -62,9 +72,6 @@ uint16_t cfglr_backend_nvs_get(cfglr_backend_t *backend, cfglr_element_t *elemen
} else if(err == ESP_OK) {
element->dirty = 0;
}
} else {
// handle [UI]64, STR and BIN datatype
}
return err;
}
@ -110,8 +117,7 @@ uint16_t cfglr_backend_nvs_set(cfglr_backend_t *backend, cfglr_element_t *elemen
case CFGLR_DATATYPE_STR:
case CFGLR_DATATYPE_BIN:
// determine length
// fetch data by length
err = nvs_set_blob((nvs_handle_t)backend->handle, element->key, element->data, element->datatype_size);
break;
default:

View file

@ -25,10 +25,10 @@ uint8_t cfglr_init(cfglr_handle_t *handle) {
CFGLR_LOGI(TAG, "Initializing element: %s", element->key);
CFGLR_LOGI(TAG, "size: %d", element->datatype_size);
CFGLR_LOGI(TAG, "default: %d", (int)element->default_data);
element->handle = handle;
element->data = (void*)malloc(element->datatype_size);
bzero(element->data, 0);
CFGLR_LOGW(TAG, "malloc: %d @ %p", element->datatype_size, element->data);
//CFGLR_LOGI(TAG, "element->data: %p", element->data);
@ -50,7 +50,7 @@ uint8_t cfglr_init(cfglr_handle_t *handle) {
} else {
CFGLR_LOGI(TAG, "failed to load %s, setting default", element->key);
memcpy((void*)element->data, (void*)&element->default_data, element->datatype_size);
memcpy((void*)&element->data, (void*)&element->default_data, element->default_size);
if(handle->backend.set(&handle->backend, element, handle) == 0) {
dispatch = true;
}