Commit e3d999269276ce7aaa289e610a01c4bb0018e616

Authored by Imanol-Mikel Barba Sabariego
1 parent f7cd72e5

JSON base finished

Project/applications/smartcities/Makefile
@@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
10 SDK_ROOT = ../../ 10 SDK_ROOT = ../../
11 PROJECT_OUT = smartcities 11 PROJECT_OUT = smartcities
12 12
13 -USER_SRC = main.c 13 +USER_SRC = main.c json.c
14 USER_INCDIR = include/ 14 USER_INCDIR = include/
15 15
16 # if you need to add build Defines options add to USER_DEFS define 16 # if you need to add build Defines options add to USER_DEFS define
Project/applications/smartcities/include/json.h
@@ -8,6 +8,9 @@ @@ -8,6 +8,9 @@
8 #define JSON_COMM_ERROR 1 8 #define JSON_COMM_ERROR 1
9 #define JSON_OTHER_ERROR 2 9 #define JSON_OTHER_ERROR 2
10 10
  11 +#define JOIN_NO_FREE 0
  12 +#define JOIN_FREE_MEM 1
  13 +
11 char* prepare_json_statement(char** data, uint32_t nObservations); 14 char* prepare_json_statement(char** data, uint32_t nObservations);
12 char* prepare_observation(char* observation, uint32_t length); 15 char* prepare_observation(char* observation, uint32_t length);
13 uint8_t send_json(char* statement, uint32_t length); 16 uint8_t send_json(char* statement, uint32_t length);
Project/applications/smartcities/json.c
@@ -2,13 +2,27 @@ @@ -2,13 +2,27 @@
2 2
3 char* prepare_json_statement(char** data, uint32_t nObservations) 3 char* prepare_json_statement(char** data, uint32_t nObservations)
4 { 4 {
5 - 5 + unsigned int i, length, observation_length;
  6 + char *json_statement, *str_aux, *str_aux2;
  7 + length = 17;
  8 + str_aux = join_strings("{\"observations\":[","",length,0,JOIN_NO_FREE);
  9 + for(i = 0; i < nObservations; i++)
  10 + {
  11 + str_aux2 = prepare_observation(data[i],strlen(data[i]));
  12 + observation_length = strlen(str_aux2);
  13 + str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); //FREES DATA FROM SENSORS!!!
  14 + length += observation_length;
  15 + }
  16 + length--; //REMOVE LAST ','
  17 + json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE);
  18 + chHeapFree(str_aux);
  19 + return json_statement;
6 } 20 }
7 21
8 char* prepare_observation(char* observation, uint32_t length) 22 char* prepare_observation(char* observation, uint32_t length)
9 { 23 {
10 char *json_data, *str_aux, *str_aux2; 24 char *json_data, *str_aux, *str_aux2;
11 - int value_length = find_next_index(observation,length,','); 25 + int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!!
12 int timestamp_length = length - (value_length + 1); 26 int timestamp_length = length - (value_length + 1);
13 char *value = (char*) chHeapAlloc(NULL,value_length+1); 27 char *value = (char*) chHeapAlloc(NULL,value_length+1);
14 char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); 28 char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1);
@@ -16,14 +30,13 @@ char* prepare_observation(char* observation, uint32_t length) @@ -16,14 +30,13 @@ char* prepare_observation(char* observation, uint32_t length)
16 strncpy(timestamp,observation+(value_length+1),timestamp_length); 30 strncpy(timestamp,observation+(value_length+1),timestamp_length);
17 value[value_length] = '\0'; 31 value[value_length] = '\0';
18 timestamp[timestamp_length] = '\0'; 32 timestamp[timestamp_length] = '\0';
19 - str_aux = join_strings("{\"value\":\"",value,10,value_length,0);  
20 - str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,0);  
21 - str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,1);  
22 - json_data = join_strings(str_aux2,"\"}",25+value_length+timestamp_length,2,0); 33 + str_aux = join_strings("{\"value\":\"",value,10,value_length,JOIN_NO_FREE);
  34 + str_aux2 = join_strings(str_aux,"\",\"timestamp\":\"",10+value_length,15,JOIN_NO_FREE);
  35 + str_aux2 = join_strings(str_aux2,timestamp,25+value_length,timestamp_length,JOIN_FREE_MEM);
  36 + json_data = join_strings(str_aux2,"\"},",25+value_length+timestamp_length,3,JOIN_NO_FREE);
23 chHeapFree(str_aux); 37 chHeapFree(str_aux);
24 chHeapFree(str_aux2); 38 chHeapFree(str_aux2);
25 chHeapFree(value); 39 chHeapFree(value);
26 - chHeapFree(timestamp);  
27 return json_data; 40 return json_data;
28 } 41 }
29 42
@@ -42,7 +55,7 @@ uint32_t find_next_index(char* string, uint32_t length, char delimiter) @@ -42,7 +55,7 @@ uint32_t find_next_index(char* string, uint32_t length, char delimiter)
42 unsigned int i; 55 unsigned int i;
43 for(i = 0; i < length; i++) 56 for(i = 0; i < length; i++)
44 { 57 {
45 - if(char[i] == delimiter) 58 + if(string[i] == delimiter)
46 { 59 {
47 return i; 60 return i;
48 } 61 }
@@ -62,9 +75,4 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t @@ -62,9 +75,4 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t
62 free(str2); 75 free(str2);
63 } 76 }
64 return str; 77 return str;
65 -}  
66 -  
67 -/*  
68 -chHeapAlloc(NULL,size)  
69 -chHeapFree(ptr)  
70 -*/  
71 \ No newline at end of file 78 \ No newline at end of file
  79 +}
72 \ No newline at end of file 80 \ No newline at end of file