Commit e3d999269276ce7aaa289e610a01c4bb0018e616
1 parent
f7cd72e5
JSON base finished
Showing
3 changed files
with
26 additions
and
15 deletions
Project/applications/smartcities/Makefile
Project/applications/smartcities/include/json.h
... | ... | @@ -8,6 +8,9 @@ |
8 | 8 | #define JSON_COMM_ERROR 1 |
9 | 9 | #define JSON_OTHER_ERROR 2 |
10 | 10 | |
11 | +#define JOIN_NO_FREE 0 | |
12 | +#define JOIN_FREE_MEM 1 | |
13 | + | |
11 | 14 | char* prepare_json_statement(char** data, uint32_t nObservations); |
12 | 15 | char* prepare_observation(char* observation, uint32_t length); |
13 | 16 | uint8_t send_json(char* statement, uint32_t length); | ... | ... |
Project/applications/smartcities/json.c
... | ... | @@ -2,13 +2,27 @@ |
2 | 2 | |
3 | 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 | 22 | char* prepare_observation(char* observation, uint32_t length) |
9 | 23 | { |
10 | 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 | 26 | int timestamp_length = length - (value_length + 1); |
13 | 27 | char *value = (char*) chHeapAlloc(NULL,value_length+1); |
14 | 28 | char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); |
... | ... | @@ -16,14 +30,13 @@ char* prepare_observation(char* observation, uint32_t length) |
16 | 30 | strncpy(timestamp,observation+(value_length+1),timestamp_length); |
17 | 31 | value[value_length] = '\0'; |
18 | 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 | 37 | chHeapFree(str_aux); |
24 | 38 | chHeapFree(str_aux2); |
25 | 39 | chHeapFree(value); |
26 | - chHeapFree(timestamp); | |
27 | 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 | 55 | unsigned int i; |
43 | 56 | for(i = 0; i < length; i++) |
44 | 57 | { |
45 | - if(char[i] == delimiter) | |
58 | + if(string[i] == delimiter) | |
46 | 59 | { |
47 | 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 | 75 | free(str2); |
63 | 76 | } |
64 | 77 | return str; |
65 | -} | |
66 | - | |
67 | -/* | |
68 | -chHeapAlloc(NULL,size) | |
69 | -chHeapFree(ptr) | |
70 | -*/ | |
71 | 78 | \ No newline at end of file |
79 | +} | |
72 | 80 | \ No newline at end of file | ... | ... |