Commit e7bbd55405a1f9ebfad2dc6305b4af98e55f3acd
1 parent
746b99d5
JSON ready
Showing
6 changed files
with
37 additions
and
24 deletions
Project/applications/smartcities/include/json.h
@@ -3,9 +3,11 @@ | @@ -3,9 +3,11 @@ | ||
3 | 3 | ||
4 | #include <string.h> | 4 | #include <string.h> |
5 | #include <stdint.h> | 5 | #include <stdint.h> |
6 | -#include "httpClient.h" | ||
7 | -#include "libwismart.h" | ||
8 | -#include "ch.h" | 6 | +#include "globals.h" |
7 | +#include "sensors.h" | ||
8 | +#include "module.h" | ||
9 | +#include <string.h> | ||
10 | +#include <stdio.h> | ||
9 | 11 | ||
10 | #define JSON_POST_OK 0 | 12 | #define JSON_POST_OK 0 |
11 | #define JSON_COMM_ERROR 1 | 13 | #define JSON_COMM_ERROR 1 |
@@ -15,10 +17,10 @@ | @@ -15,10 +17,10 @@ | ||
15 | #define JOIN_FREE_MEM 1 | 17 | #define JOIN_FREE_MEM 1 |
16 | 18 | ||
17 | char* prepare_json_statement(char** data, uint32_t nObservations); | 19 | char* prepare_json_statement(char** data, uint32_t nObservations); |
18 | -char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type); | 20 | +char* prepare_json_register_statement(module mod, sensor sens); |
19 | char* prepare_observation(char* observation, uint32_t length); | 21 | char* prepare_observation(char* observation, uint32_t length); |
20 | uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, char* sensor_ID); | 22 | uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, char* sensor_ID); |
21 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); | 23 | uint32_t find_next_index(char* string, uint32_t length, char delimiter); |
22 | -char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, bool) | 24 | +char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free_mem); |
23 | 25 | ||
24 | -#endif | 26 | -#endif |
27 | +#endif | ||
25 | \ No newline at end of file | 28 | \ No newline at end of file |
Project/applications/smartcities/include/module.h
1 | #ifndef MODULE_H | 1 | #ifndef MODULE_H |
2 | #define MODULE_H | 2 | #define MODULE_H |
3 | 3 | ||
4 | -typedef struct module_info { | 4 | +typedef struct module_info module; |
5 | +struct module_info { | ||
5 | char* ID; | 6 | char* ID; |
6 | char* geoloc; | 7 | char* geoloc; |
7 | -} module_info; | 8 | +}; |
8 | 9 | ||
9 | -module_info module = {"123456", "41.39479 2.148768"} /* append 0/1 flag for read write */ | 10 | +extern module mod; |
10 | 11 | ||
11 | -#endif | ||
12 | \ No newline at end of file | 12 | \ No newline at end of file |
13 | +#endif |
Project/applications/smartcities/include/sensors.h
1 | #ifndef SENSORS_H | 1 | #ifndef SENSORS_H |
2 | #define SENSORS_H | 2 | #define SENSORS_H |
3 | 3 | ||
4 | -typedef struct sensor_info { | 4 | +#include <stdint.h> |
5 | + | ||
6 | + | ||
7 | +typedef struct sensor_info sensor; | ||
8 | +struct sensor_info { | ||
5 | uint8_t ID; | 9 | uint8_t ID; |
6 | char* description; | 10 | char* description; |
7 | char* type; | 11 | char* type; |
8 | char* unit; | 12 | char* unit; |
9 | -} sensor_info; | 13 | +}; |
10 | 14 | ||
11 | -sensor_info humidity_sensor = {ID = 0x77, "Humidity sensor", "humidity", "%"} /* append 0/1 flag for read write */ | 15 | +extern sensor humidity_sensor; |
12 | 16 | ||
13 | 17 | ||
14 | #endif | 18 | #endif |
15 | \ No newline at end of file | 19 | \ No newline at end of file |
Project/applications/smartcities/json.c
@@ -19,40 +19,40 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) | @@ -19,40 +19,40 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) | ||
19 | return json_statement; | 19 | return json_statement; |
20 | } | 20 | } |
21 | 21 | ||
22 | -char* prepare_json_register_statement(module_info module, sensor_info sensor) | 22 | +char* prepare_json_register_statement(module mod, sensor sens) |
23 | { | 23 | { |
24 | unsigned int i, length, observation_length; | 24 | unsigned int i, length, observation_length; |
25 | char *json_statement, *str_aux, *str_aux2; | 25 | char *json_statement, *str_aux, *str_aux2; |
26 | + char ID[MODULE_ID_LENGTH+3]; | ||
26 | length = 23; | 27 | length = 23; |
27 | - char* ID[MODULE_ID_LENGTH+3]; | ||
28 | - strcpy(ID,module.ID); | ||
29 | - sprintf(ID+MODULE_ID_LENGTH,"%d",sensor.ID); | 28 | + strcpy(ID,mod.ID); |
29 | + sprintf(ID+MODULE_ID_LENGTH,"%d",sens.ID); | ||
30 | str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE); | 30 | str_aux = join_strings("{\"sensors\":[{\"sensor\":\"",ID,length,MODULE_ID_LENGTH+2,JOIN_NO_FREE); |
31 | length += MODULE_ID_LENGTH+2; | 31 | length += MODULE_ID_LENGTH+2; |
32 | str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE); | 32 | str_aux2 = join_strings(str_aux,"\",\"description\":\"",length,17,JOIN_NO_FREE); |
33 | chHeapFree(str_aux); | 33 | chHeapFree(str_aux); |
34 | length += 17; | 34 | length += 17; |
35 | - str_aux = join_strings(str_aux2,sensor.description,length,strlen(sensor.description),JOIN_NO_FREE); | 35 | + str_aux = join_strings(str_aux2,sens.description,length,strlen(sens.description),JOIN_NO_FREE); |
36 | chHeapFree(str_aux2); | 36 | chHeapFree(str_aux2); |
37 | - length += strlen(sensor.description); | 37 | + length += strlen(sens.description); |
38 | str_aux2 = join_strings(str_aux,"\",\"type\":\"",length,10,JOIN_NO_FREE); | 38 | str_aux2 = join_strings(str_aux,"\",\"type\":\"",length,10,JOIN_NO_FREE); |
39 | chHeapFree(str_aux); | 39 | chHeapFree(str_aux); |
40 | length += 10; | 40 | length += 10; |
41 | - str_aux = join_strings(str_aux2,sensor.type,length,strlen(sensors.type),JOIN_NO_FREE); | 41 | + str_aux = join_strings(str_aux2,sens.type,length,strlen(sens.type),JOIN_NO_FREE); |
42 | chHeapFree(str_aux2); | 42 | chHeapFree(str_aux2); |
43 | - length += strlen(sensors.type); | 43 | + length += strlen(sens.type); |
44 | str_aux2 = join_strings(str_aux,"\",\"unit\":\"",length,10,JOIN_NO_FREE); | 44 | str_aux2 = join_strings(str_aux,"\",\"unit\":\"",length,10,JOIN_NO_FREE); |
45 | chHeapFree(str_aux); | 45 | chHeapFree(str_aux); |
46 | length += 10; | 46 | length += 10; |
47 | - str_aux = join_strings(str_aux2,sensor.unit,length,strlen(sensor.unit),JOIN_NO_FREE); | 47 | + str_aux = join_strings(str_aux2,sens.unit,length,strlen(sens.unit),JOIN_NO_FREE); |
48 | chHeapFree(str_aux2); | 48 | chHeapFree(str_aux2); |
49 | length += strlen(sensor.unit); | 49 | length += strlen(sensor.unit); |
50 | str_aux2 = join_strings(str_aux,"\",\"location\":\"",length,14,JOIN_NO_FREE); | 50 | str_aux2 = join_strings(str_aux,"\",\"location\":\"",length,14,JOIN_NO_FREE); |
51 | chHeapFree(str_aux); | 51 | chHeapFree(str_aux); |
52 | length += 14; | 52 | length += 14; |
53 | - str_aux = join_strings(str_aux2,module.geoloc,length,strlen(module.geoloc),JOIN_NO_FREE); | 53 | + str_aux = join_strings(str_aux2,mod.geoloc,length,strlen(mod.geoloc),JOIN_NO_FREE); |
54 | chHeapFree(str_aux2); | 54 | chHeapFree(str_aux2); |
55 | - length += strlen(module.geoloc); | 55 | + length += strlen(mod.geoloc); |
56 | json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); | 56 | json_statement = join_strings(str_aux,"\"}]}\0",length,5,JOIN_NO_FREE); |
57 | chHeapFree(str_aux); | 57 | chHeapFree(str_aux); |
58 | return json_statement; | 58 | return json_statement; |
Project/applications/smartcities/module.c
0 → 100644
Project/applications/smartcities/sensors.c
0 → 100644