Commit 8f7e33bb4c8ab2eaa7af4082474834070f5773a5
1 parent
410c7b66
Más refactoring
Showing
5 changed files
with
46 additions
and
31 deletions
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 httpClient.c | 13 | +USER_SRC = main.c httpClient.c callbacks.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/httpClient.c
@@ -42,8 +42,8 @@ int httpRequest(struct httpHeaders head, char* content, int content_size) | @@ -42,8 +42,8 @@ int httpRequest(struct httpHeaders head, char* content, int content_size) | ||
42 | int request_size = head_size + content_size + 2*(sizeof ENDL) + sizeof '\0'; | 42 | int request_size = head_size + content_size + 2*(sizeof ENDL) + sizeof '\0'; |
43 | /* WARNING | 43 | /* WARNING |
44 | * | 44 | * |
45 | - * 1. Te has olvidado el free de liberar memoria. Es SÚPER IMPORTANTE no dejar memoria sin liberar por que genera memory leaks y no vamos sobraos de ram | ||
46 | - * 2. Recuerda cambiarlo luego a chHeapAlloc y chHeapFree, yo suelo hacerme unos .c's aparte en mi carpeta para no mezclar código de prueba con código de la repo, pero mientras luego lo cambies cap problema | 45 | + * 1. Te has olvidado el free de liberar memoria. Es SÚPER IMPORTANTE no dejar memoria sin liberar por que genera memory leaks y no vamos sobraos de ram. Pa más infor sobre memory leaks, consulte al Manol más cercano. |
46 | + * 2. Recuerda cambiarlo luego a chHeapAlloc y chHeapFree, yo suelo hacerme unos .c's aparte en mi carpeta para no mezclar código de prueba con código de la repo, pero mientras luego lo cambies cap problema. | ||
47 | * | 47 | * |
48 | * --Imanol | 48 | * --Imanol |
49 | */ | 49 | */ |
Project/applications/smartcities/include/module.h
Project/applications/smartcities/json.c
@@ -21,8 +21,26 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) | @@ -21,8 +21,26 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) | ||
21 | 21 | ||
22 | char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type) | 22 | char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type) |
23 | { | 23 | { |
24 | + //id, desc, type, unit | ||
25 | + | ||
26 | + unsigned int i, length, observation_length; | ||
27 | + char *json_statement, *str_aux, *str_aux2; | ||
28 | + length = 17; | ||
29 | + str_aux = join_strings("{\"observations\":[","",length,0,JOIN_NO_FREE); | ||
30 | + for(i = 0; i < nObservations; i++) | ||
31 | + { | ||
32 | + str_aux2 = prepare_observation(data[i],strlen(data[i])); | ||
33 | + observation_length = strlen(str_aux2); | ||
34 | + str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); | ||
35 | + length += observation_length; | ||
36 | + } | ||
37 | + length--; //REMOVE LAST ',' | ||
38 | + json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE); | ||
39 | + chHeapFree(str_aux); | ||
40 | + return json_statement; | ||
41 | + | ||
24 | char *json_data, *str_aux, *str_aux2; | 42 | char *json_data, *str_aux, *str_aux2; |
25 | - int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!! | 43 | + int value_length = find_next_index(observation,length,','); |
26 | int timestamp_length = length - (value_length + 1); | 44 | int timestamp_length = length - (value_length + 1); |
27 | char *value = (char*) chHeapAlloc(NULL,value_length+1); | 45 | char *value = (char*) chHeapAlloc(NULL,value_length+1); |
28 | char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); | 46 | char *timestamp = (char*) chHeapAlloc(NULL,timestamp_length+1); |
@@ -43,6 +61,13 @@ char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type) | @@ -43,6 +61,13 @@ char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type) | ||
43 | char* prepare_observation(char* observation, uint32_t length) | 61 | char* prepare_observation(char* observation, uint32_t length) |
44 | { | 62 | { |
45 | char *json_data, *str_aux, *str_aux2; | 63 | char *json_data, *str_aux, *str_aux2; |
64 | + /* TOPKEK | ||
65 | + * | ||
66 | + * ¿Gestión de errores? | ||
67 | + * ¿PA QUÉ? | ||
68 | + * | ||
69 | + * --le trole | ||
70 | + */ | ||
46 | int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!! | 71 | int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!! |
47 | int timestamp_length = length - (value_length + 1); | 72 | int timestamp_length = length - (value_length + 1); |
48 | char *value = (char*) chHeapAlloc(NULL,value_length+1); | 73 | char *value = (char*) chHeapAlloc(NULL,value_length+1); |
@@ -88,6 +113,14 @@ uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, ch | @@ -88,6 +113,14 @@ uint8_t send_observation(char* statement, uint32_t length, char* provider_ID, ch | ||
88 | strcpy(URL+16+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),"/"); | 113 | strcpy(URL+16+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),"/"); |
89 | strcpy(URL+17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),sensor_ID); | 114 | strcpy(URL+17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),sensor_ID); |
90 | } | 115 | } |
116 | + | ||
117 | + /* SELF-REMINDER | ||
118 | + * | ||
119 | + * Cambiar esto pa usar la función de Ferràn | ||
120 | + * | ||
121 | + * --Imanol | ||
122 | + */ | ||
123 | + | ||
91 | response_code = http_post(URL,statement,URL_length,length); | 124 | response_code = http_post(URL,statement,URL_length,length); |
92 | if(response code == 200) | 125 | if(response code == 200) |
93 | { | 126 | { |
@@ -128,6 +161,4 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t | @@ -128,6 +161,4 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t | ||
128 | free(str2); | 161 | free(str2); |
129 | } | 162 | } |
130 | return str; | 163 | return str; |
131 | -} | ||
132 | - | ||
133 | -//id, desc, type, unit | ||
134 | \ No newline at end of file | 164 | \ No newline at end of file |
165 | +} | ||
135 | \ No newline at end of file | 166 | \ No newline at end of file |
Project/applications/smartcities/main.c
@@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
3 | #include "lwip/inet.h" | 3 | #include "lwip/inet.h" |
4 | #include "globals.h" | 4 | #include "globals.h" |
5 | #include "httpClient.h" | 5 | #include "httpClient.h" |
6 | +#include "callbacks.h" | ||
6 | 7 | ||
7 | #define WIFI_MODE WIFI_MODE_CLIENT | 8 | #define WIFI_MODE WIFI_MODE_CLIENT |
8 | #define NETWORK_SSID "linksys" | 9 | #define NETWORK_SSID "linksys" |
@@ -16,29 +17,12 @@ void initLibwismart(void) | @@ -16,29 +17,12 @@ void initLibwismart(void) | ||
16 | libwismart_Init(hwif); | 17 | libwismart_Init(hwif); |
17 | } | 18 | } |
18 | 19 | ||
19 | -void dhcp_connect_result_cb(int result) | ||
20 | -{ | ||
21 | - libwismart_ip_addr_t ip; | ||
22 | - if(result==LIBWISMART_DHCP_ADDRESS_ASSIGNED) | ||
23 | - { | ||
24 | - libwismart_GetCurrentIP(&ip,NULL,NULL); | ||
25 | - printf("IP: %d.%d.%d.%d \r\n",ip.addr[3],ip.addr[2],ip.addr[1],ip.addr[0]); | ||
26 | - } | ||
27 | - else if(result==LIBWISMART_DHCP_TIMEOUT) | ||
28 | - { | ||
29 | - printf("DHCP timeout\r\n"); | ||
30 | - } | ||
31 | - else | ||
32 | - { | ||
33 | - printf("DHCP error\r\n"); | ||
34 | - } | ||
35 | - | ||
36 | -} | ||
37 | - | ||
38 | -void wifi_connect_result_cb(int result) | ||
39 | -{ | ||
40 | - printf("WiFi Connect indication: %s\r\n", (result == WISMART_WIFI_CONNECTED) ? "Connected": "Failed\r\n"); | ||
41 | -} | 20 | +/* erase me |
21 | + * | ||
22 | + * He movido los callbacks a un .c y .h aparte para mayor modularidad, conveniéntemente llamados callbacks.c y callbacks.h | ||
23 | + * | ||
24 | + * --Imanol | ||
25 | + */ | ||
42 | 26 | ||
43 | int main(void) | 27 | int main(void) |
44 | { | 28 | { |