From 85dc35dcc27486da31e7e1d55341a5267c19b910 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Fri, 15 Nov 2024 14:30:03 -0800 Subject: [PATCH] blobs, signalers in element macros, and removing the big fucking if statement that was causing all the confusion --- include/configulator.h | 25 +++++++----- src/cfglr_nvs.c | 86 ++++++++++++++++++++++-------------------- src/configulator.c | 4 +- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/include/configulator.h b/include/configulator.h index 78a6c7e..fe5638c 100644 --- a/include/configulator.h +++ b/include/configulator.h @@ -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; diff --git a/src/cfglr_nvs.c b/src/cfglr_nvs.c index 8857b60..08d6d93 100644 --- a/src/cfglr_nvs.c +++ b/src/cfglr_nvs.c @@ -17,53 +17,60 @@ 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); - break; + switch(element->datatype) { + case CFGLR_DATATYPE_U8: + err = nvs_get_u8((nvs_handle_t)backend->handle, element->key, (uint8_t*)element->data); + break; - case CFGLR_DATATYPE_I8: - err = nvs_get_i8((nvs_handle_t)backend->handle, element->key, (int8_t*)element->data); - break; + case CFGLR_DATATYPE_I8: + err = nvs_get_i8((nvs_handle_t)backend->handle, element->key, (int8_t*)element->data); + break; - case CFGLR_DATATYPE_U16: - err = nvs_get_u16((nvs_handle_t)backend->handle, element->key, (uint16_t*)element->data); - break; + case CFGLR_DATATYPE_U16: + err = nvs_get_u16((nvs_handle_t)backend->handle, element->key, (uint16_t*)element->data); + break; - case CFGLR_DATATYPE_I16: - err = nvs_get_i16((nvs_handle_t)backend->handle, element->key, (int16_t*)element->data); - break; + case CFGLR_DATATYPE_I16: + err = nvs_get_i16((nvs_handle_t)backend->handle, element->key, (int16_t*)element->data); + break; - case CFGLR_DATATYPE_U32: - err = nvs_get_u32((nvs_handle_t)backend->handle, element->key, (uint32_t*)element->data); - break; + case CFGLR_DATATYPE_U32: + err = nvs_get_u32((nvs_handle_t)backend->handle, element->key, (uint32_t*)element->data); + break; - case CFGLR_DATATYPE_I32: - err = nvs_get_i32((nvs_handle_t)backend->handle, element->key, (int32_t*)element->data); - break; + case CFGLR_DATATYPE_I32: + err = nvs_get_i32((nvs_handle_t)backend->handle, element->key, (int32_t*)element->data); + break; - case CFGLR_DATATYPE_U64: - err = nvs_get_u64((nvs_handle_t)backend->handle, element->key, (uint64_t*)element->data); - break; + case CFGLR_DATATYPE_U64: + err = nvs_get_u64((nvs_handle_t)backend->handle, element->key, (uint64_t*)element->data); + break; - case CFGLR_DATATYPE_I64: - err = nvs_get_i64((nvs_handle_t)backend->handle, element->key, (int64_t*)element->data); - break; + case CFGLR_DATATYPE_I64: + err = nvs_get_i64((nvs_handle_t)backend->handle, element->key, (int64_t*)element->data); + break; - default: - CFGLR_LOGI(TAG, "unhandlable datatype: %d", element->datatype); - } + case CFGLR_DATATYPE_STR: + case CFGLR_DATATYPE_BIN: + uint8_t read_size = 0; - if(err == ESP_ERR_NVS_NOT_FOUND) { - CFGLR_LOGI(TAG, "NOT FOUN!!!!"); - } else if(err != ESP_OK) { - ESP_ERROR_CHECK(err); - } else if(err == ESP_OK) { - element->dirty = 0; - } - } else { - // handle [UI]64, STR and BIN datatype + 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); + } + + if(err == ESP_ERR_NVS_NOT_FOUND) { + CFGLR_LOGI(TAG, "NOT FOUN!!!!"); + } else if(err != ESP_OK) { + ESP_ERROR_CHECK(err); + } else if(err == ESP_OK) { + element->dirty = 0; } 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: diff --git a/src/configulator.c b/src/configulator.c index 173f140..6432d8c 100644 --- a/src/configulator.c +++ b/src/configulator.c @@ -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; }