From f7cd72e58980afb7f99db838362ed02cbd976faf Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sat, 15 Mar 2014 08:10:02 +0000 Subject: [PATCH] JSON code in progress --- Project/applications/smartcities/include/json.h | 7 +++++-- Project/applications/smartcities/json.c | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Project/applications/smartcities/include/json.h b/Project/applications/smartcities/include/json.h index bb2627a..2c53d93 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -1,14 +1,17 @@ #ifndef JSON_H #define JSON_H +#include +#include + #define JSON_POST_OK 0 #define JSON_COMM_ERROR 1 #define JSON_OTHER_ERROR 2 -char* prepare_json_statement(char* data, uint32_t length); +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); uint32_t find_next_index(char* string, uint32_t length, char delimiter); -char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2) +char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, bool) #endif diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 76ee66e..03c9a5f 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -1,11 +1,30 @@ -char* prepare_json_statement(char* data, uint32_t length) +#include "json.h" + +char* prepare_json_statement(char** data, uint32_t nObservations) { } char* prepare_observation(char* observation, uint32_t length) { - + char *json_data, *str_aux, *str_aux2; + int value_length = find_next_index(observation,length,','); + int timestamp_length = length - (value_length + 1); + char *value = (char*) chHeapAlloc(NULL,value_length+1); + char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); + strncpy(value,observation,value_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); + chHeapFree(str_aux); + chHeapFree(str_aux2); + chHeapFree(value); + chHeapFree(timestamp); + return json_data; } uint8_t send_json(char* statement, uint32_t length) @@ -31,14 +50,18 @@ uint32_t find_next_index(char* string, uint32_t length, char delimiter) return -1; } -char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2) +char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free_mem) { char* str = (char*) chHeapAlloc(NULL,len1+len2+1); - /*strcat(str,str1); - strcat(str,str2); - USAR STRNCAT - */ - + strncpy(str,str1,len1); + str[len1] = '\0'; + strncat(str,str2,len2); + if(free_mem) + { + free(str1); + free(str2); + } + return str; } /* -- libgit2 0.22.2