diff options
-rw-r--r-- | calibrate_client.c | 18 | ||||
-rw-r--r-- | redist/json_helpers.c | 114 | ||||
-rw-r--r-- | redist/json_helpers.h | 6 | ||||
-rw-r--r-- | src/survive_cal.c | 2 | ||||
-rw-r--r-- | src/survive_config.c | 57 | ||||
-rw-r--r-- | src/survive_config.h | 3 |
6 files changed, 188 insertions, 12 deletions
diff --git a/calibrate_client.c b/calibrate_client.c index 08f0715..927f40f 100644 --- a/calibrate_client.c +++ b/calibrate_client.c @@ -142,6 +142,17 @@ void * GuiThread( void * v ) int main() { + + config_init(); + config_read("config.json"); + config_set_str(&global_config_values, "Hello","World!"); + const char *s = config_read_str(&global_config_values, "test123","default"); + printf("%s\n", s); + + +// config_save("config.json"); + + ctx = survive_init( 1 ); survive_install_light_fn( ctx, my_light_process ); @@ -159,13 +170,6 @@ int main() CNFGSetup( "Survive GUI Debug", 640, 480 ); OGCreateThread( GuiThread, 0 ); - config_init(); - config_set_str(&global_config_values, "Hello","World!"); - const char *s = config_read_str(&global_config_values, "TestStr","This is a test."); - printf("%s\n", s); - config_save("config.json"); - - if( !ctx ) { fprintf( stderr, "Fatal. Could not start\n" ); diff --git a/redist/json_helpers.c b/redist/json_helpers.c index fd04203..b7ccb40 100644 --- a/redist/json_helpers.c +++ b/redist/json_helpers.c @@ -6,6 +6,8 @@ #include <stdlib.h> #include <string.h> #include "json_helpers.h" +#include <jsmn.h> + void json_write_float_array(FILE* f, const char* tag, float* v, uint8_t count) { uint8_t i = 0; @@ -14,10 +16,10 @@ void json_write_float_array(FILE* f, const char* tag, float* v, uint8_t count) { asprintf(&str1,"\"%s\":[", tag); for (i=0;i<count;++i) { - if (i<(count-1)) { - asprintf(&str2, "%s\"%f\",", str1,v[i]); - } else { + if ( (i+1) < count) { asprintf(&str2, "%s\"%f\"", str1,v[i]); + } else { + asprintf(&str2, "%s\"%f\",", str1,v[i]); } free(str1); str1=str2; @@ -62,3 +64,109 @@ void json_write_float(FILE* f, const char* tag, float v) { void json_write_str(FILE* f, const char* tag, const char* v) { fprintf(f, "\"%s\":\"%s\"", tag, v); } + + + + +void (*json_begin_object)(char* tag) = NULL; +void (*json_end_object)() = NULL; +void (*json_tag_value)(char* tag, char** values, uint16_t count) = NULL; + +uint32_t JSON_STRING_LEN; + +char* load_file_to_mem(const char* path) { + FILE * f = fopen( path, "r" ); + fseek( f, 0, SEEK_END ); + int len = ftell( f ); + fseek( f, 0, SEEK_SET ); + char * JSON_STRING = malloc( len ); + fread( JSON_STRING, len, 1, f ); + fclose( f ); + return JSON_STRING; +} + +static char* substr(const char* str, uint32_t start, uint32_t end, uint32_t npos) { + uint32_t l = end-start+1; + + if (end<=start || end>=npos) return NULL; + + char* x = malloc(l); + memcpy(x,str+start,l); + x[l-1] = '\0'; + return x; +} + +static uint16_t json_load_array(const char* JSON_STRING, jsmntok_t* tokens, uint16_t size, char* tag) { + jsmntok_t* t = tokens; + uint16_t i = 0; + + char* values[size]; + + for (i=0;i<size;++i) { + t = tokens+i; + values[i] = substr(JSON_STRING, t->start, t->end, JSON_STRING_LEN); + } + + if (json_tag_value != NULL) json_tag_value(tag, values, i); + + for (i=0;i<size;++i) free(values[i]); + + return size; +} + +void json_load_file(const char* path) { + uint32_t i = 0; + char* JSON_STRING = load_file_to_mem(path); + JSON_STRING_LEN = strlen(JSON_STRING); + + jsmn_parser parser; + jsmn_init(&parser); + + uint32_t items = jsmn_parse(&parser, JSON_STRING, JSON_STRING_LEN, NULL, 0); + jsmntok_t* tokens = malloc(items * sizeof(jsmntok_t)); + + jsmn_init(&parser); + items = jsmn_parse(&parser, JSON_STRING, JSON_STRING_LEN, tokens, items); + + int16_t children = -1; + + for (i=0; i<items; i+=2) + { + //increment i on each successful tag + values combination, not individual tokens + jsmntok_t* tag_t = tokens+i; + jsmntok_t* value_t = tokens+i+1; + + char* tag = substr(JSON_STRING, tag_t->start, tag_t->end, JSON_STRING_LEN); + char* value = substr(JSON_STRING, value_t->start, value_t->end, JSON_STRING_LEN); + + printf("%d %d c:%d %d %s \n", tag_t->start, tag_t->end, tag_t->size, tag_t->type, tag); + + + if (value_t->type == JSMN_ARRAY) { + i += json_load_array(JSON_STRING, tokens+i+2,value_t->size, tag); //look at array children + } else if (value_t->type == JSMN_OBJECT) { + printf("Begin Object\n"); + if (json_begin_object != NULL) json_begin_object(tag); + children = value_t->size +1; //+1 to account for this loop where we are not yed parsing children +// i += decode_jsmn_object(JSON_STRING, tokens+i+2,value_t->size); + } + else { + if (json_tag_value != NULL) json_tag_value(tag, &value, 1); + } + + if (children>=0) children--; + if (children == 0) { + children = -1; + printf("End Object\n"); + if (json_end_object!=NULL) json_end_object(); + } + +// printf("%d %s \n", value_t->type, tag); + + free(tag); + free(value); + } + + free(JSON_STRING); +} + diff --git a/redist/json_helpers.h b/redist/json_helpers.h index 8d6eb64..1670058 100644 --- a/redist/json_helpers.h +++ b/redist/json_helpers.h @@ -11,4 +11,10 @@ void json_write_uint32(FILE* f, const char* tag, uint32_t v); void json_write_float(FILE* f, const char* tag, float v); void json_write_str(FILE* f, const char* tag, const char* v); +void json_load_file(const char* path); +extern void (*json_begin_object)(char* tag); +extern void (*json_end_object)(); +extern void (*json_tag_value)(char* tag, char** values, uint16_t count); + + #endif
\ No newline at end of file diff --git a/src/survive_cal.c b/src/survive_cal.c index f372309..33d1faa 100644 --- a/src/survive_cal.c +++ b/src/survive_cal.c @@ -53,7 +53,7 @@ void ootx_packet_clbk_d(ootx_decoder_context *ct, ootx_packet* packet) b->OOTXSet = 1; config_set_lighthouse(b,id); - config_save("config.json"); +// config_save("config.json"); } int survive_cal_get_status( struct SurviveContext * ctx, char * description, int description_length ) diff --git a/src/survive_config.c b/src/survive_config.c index c46e300..d91596f 100644 --- a/src/survive_config.c +++ b/src/survive_config.c @@ -109,6 +109,14 @@ FLT config_read_float(config_group *cg, const char *tag, const FLT def) { config_entry* next_unused_entry(config_group *cg) { config_entry *cv = cg->config_entries + cg->used_entries; assert(cg->used_entries < cg->max_entries); + +/* + if (cg->used_entries >= cg->max_entries) { + cg->max_entries+=10; + cg->config_entries = realloc(cg->config_entries, sizeof(config_entry)*cg->max_entries); + } +*/ + cg->used_entries++; return cv; } @@ -213,3 +221,52 @@ void config_save(const char* path) { fclose(f); } +void print_json_value(char* tag, char** values, uint16_t count) { + uint16_t i = 0; + for (i=0;i<count; ++i) { + printf("%s:%s \n", tag, values[i]); + } +} + +config_group* cg_stack[10]; //handle 10 nested objects deep +uint8_t cg_stack_head = 0; + +void handle_config_group(char* tag) { + cg_stack_head++; + if (strcmp("lighthouse0",tag) == 0) { + cg_stack[cg_stack_head] = lh_config; + } else if (strcmp("lighthouse1",tag) == 0) { + cg_stack[cg_stack_head] = lh_config+1; + } else { + cg_stack[cg_stack_head] = &global_config_values; + } +} + +void pop_config_group() { + cg_stack_head--; +} + +void handle_tag_value(char* tag, char** values, uint16_t count) { + print_json_value(tag,values,count); + config_group* cg = cg_stack[cg_stack_head]; + + //parse out numeric values + + if (count == 1) config_set_str(cg,tag,values[0]); +// else if (count>1) config_set_str +} + +void config_read(const char* path) { + json_begin_object = handle_config_group; + json_end_object = pop_config_group; + json_tag_value = handle_tag_value; + + cg_stack[0] = &global_config_values; + + json_load_file(path); + + json_begin_object = NULL; + json_end_object = NULL; + json_tag_value = NULL; +} + diff --git a/src/survive_config.h b/src/survive_config.h index 14e2fc6..2610e2e 100644 --- a/src/survive_config.h +++ b/src/survive_config.h @@ -37,7 +37,8 @@ extern config_group lh_config[2]; //lighthouse configs void config_init(); -void config_open(const char* path, const char* mode); +//void config_open(const char* path, const char* mode); +void config_read(const char* path); void config_close(); //void config_write_lighthouse(struct BaseStationData* bsd, uint8_t length); void config_set_lighthouse(struct BaseStationData* bsd, uint8_t idx); |