From e3d999269276ce7aaa289e610a01c4bb0018e616 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sat, 15 Mar 2014 18:38:26 +0000 Subject: [PATCH] JSON base finished --- Project/applications/smartcities/Makefile | 2 +- Project/applications/smartcities/include/json.h | 3 +++ Project/applications/smartcities/json.c | 36 ++++++++++++++++++++++-------------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Project/applications/smartcities/Makefile b/Project/applications/smartcities/Makefile index c941730..591610e 100644 --- a/Project/applications/smartcities/Makefile +++ b/Project/applications/smartcities/Makefile @@ -10,7 +10,7 @@ SDK_ROOT = ../../ PROJECT_OUT = smartcities -USER_SRC = main.c +USER_SRC = main.c json.c USER_INCDIR = include/ # if you need to add build Defines options add to USER_DEFS define diff --git a/Project/applications/smartcities/include/json.h b/Project/applications/smartcities/include/json.h index 2c53d93..2bc11e6 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -8,6 +8,9 @@ #define JSON_COMM_ERROR 1 #define JSON_OTHER_ERROR 2 +#define JOIN_NO_FREE 0 +#define JOIN_FREE_MEM 1 + char* prepare_json_statement(char** data, uint32_t nObservations); char* prepare_observation(char* observation, uint32_t length); uint8_t send_json(char* statement, uint32_t length); diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 03c9a5f..6c8214e 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -2,13 +2,27 @@ char* prepare_json_statement(char** data, uint32_t nObservations) { - + unsigned int i, length, observation_length; + char *json_statement, *str_aux, *str_aux2; + length = 17; + str_aux = join_strings("{\"observations\":[","",length,0,JOIN_NO_FREE); + for(i = 0; i < nObservations; i++) + { + str_aux2 = prepare_observation(data[i],strlen(data[i])); + observation_length = strlen(str_aux2); + str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); //FREES DATA FROM SENSORS!!! + length += observation_length; + } + length--; //REMOVE LAST ',' + json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE); + chHeapFree(str_aux); + return json_statement; } char* prepare_observation(char* observation, uint32_t length) { char *json_data, *str_aux, *str_aux2; - int value_length = find_next_index(observation,length,','); + int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!! int timestamp_length = length - (value_length + 1); char *value = (char*) chHeapAlloc(NULL,value_length+1); char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); @@ -16,14 +30,13 @@ char* prepare_observation(char* observation, uint32_t length) strncpy(timestamp,observation+(value_length+1),timestamp_length); value[value_length] = '\0'; timestamp[timestamp_length] = '\0'; - str_aux = join_strings("{\"value\":\"",value,10,value_length,0); - str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,0); - str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,1); - json_data = join_strings(str_aux2,"\"}",25+value_length+timestamp_length,2,0); + str_aux = join_strings("{\"value\":\"",value,10,value_length,JOIN_NO_FREE); + str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,JOIN_NO_FREE); + str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,JOIN_FREE_MEM); + json_data = join_strings(str_aux2,"\"},",25+value_length+timestamp_length,3,JOIN_NO_FREE); chHeapFree(str_aux); chHeapFree(str_aux2); chHeapFree(value); - chHeapFree(timestamp); return json_data; } @@ -42,7 +55,7 @@ uint32_t find_next_index(char* string, uint32_t length, char delimiter) unsigned int i; for(i = 0; i < length; i++) { - if(char[i] == delimiter) + if(string[i] == delimiter) { return i; } @@ -62,9 +75,4 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free(str2); } return str; -} - -/* -chHeapAlloc(NULL,size) -chHeapFree(ptr) -*/ \ No newline at end of file +} \ No newline at end of file -- libgit2 0.22.2