From 8f7e33bb4c8ab2eaa7af4082474834070f5773a5 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Wed, 26 Mar 2014 23:36:49 +0000 Subject: [PATCH] Más refactoring --- Project/applications/smartcities/Makefile | 2 +- Project/applications/smartcities/httpClient.c | 4 ++-- Project/applications/smartcities/include/module.h | 2 +- Project/applications/smartcities/json.c | 39 +++++++++++++++++++++++++++++++++++---- Project/applications/smartcities/main.c | 30 +++++++----------------------- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/Project/applications/smartcities/Makefile b/Project/applications/smartcities/Makefile index 4b371da..3179a82 100644 --- a/Project/applications/smartcities/Makefile +++ b/Project/applications/smartcities/Makefile @@ -10,7 +10,7 @@ SDK_ROOT = ../../ PROJECT_OUT = smartcities -USER_SRC = main.c httpClient.c +USER_SRC = main.c httpClient.c callbacks.c USER_INCDIR = include/ # if you need to add build Defines options add to USER_DEFS define diff --git a/Project/applications/smartcities/httpClient.c b/Project/applications/smartcities/httpClient.c index 79cbccf..87efd2b 100644 --- a/Project/applications/smartcities/httpClient.c +++ b/Project/applications/smartcities/httpClient.c @@ -42,8 +42,8 @@ int httpRequest(struct httpHeaders head, char* content, int content_size) int request_size = head_size + content_size + 2*(sizeof ENDL) + sizeof '\0'; /* WARNING * - * 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 - * 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 + * 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. + * 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. * * --Imanol */ diff --git a/Project/applications/smartcities/include/module.h b/Project/applications/smartcities/include/module.h index 0323b3d..e0a2659 100644 --- a/Project/applications/smartcities/include/module.h +++ b/Project/applications/smartcities/include/module.h @@ -1,6 +1,6 @@ #ifndef MODULE_H #define MODULE_H -#define SERIAL_NUMBER 123456 +#define SERIAL_NUMBER "123456" #endif \ No newline at end of file diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 2f67f87..2564b95 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -21,8 +21,26 @@ char* prepare_json_observation_statement(char** data, uint32_t nObservations) char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type) { + //id, desc, type, unit + + unsigned int i, length, observation_length; + char *json_statement, *str_aux, *str_aux2; + length = 17; + str_aux = join_strings("{\"observations\":[","",length,0,JOIN_NO_FREE); + for(i = 0; i < nObservations; i++) + { + str_aux2 = prepare_observation(data[i],strlen(data[i])); + observation_length = strlen(str_aux2); + str_aux = join_strings(str_aux,str_aux2,length,observation_length,JOIN_FREE_MEM); + length += observation_length; + } + length--; //REMOVE LAST ',' + json_statement = join_strings(str_aux,"]}",length,2,JOIN_NO_FREE); + chHeapFree(str_aux); + return json_statement; + char *json_data, *str_aux, *str_aux2; - int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!! + 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); @@ -43,6 +61,13 @@ char* prepare_json_register_statement(char* module_ID, uint8_t sensor_type) char* prepare_observation(char* observation, uint32_t length) { char *json_data, *str_aux, *str_aux2; + /* TOPKEK + * + * ¿Gestión de errores? + * ¿PA QUÉ? + * + * --le trole + */ int value_length = find_next_index(observation,length,','); //EXPECTS FORMATTING!!! int timestamp_length = length - (value_length + 1); 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 strcpy(URL+16+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),"/"); strcpy(URL+17+SERVER_HOSTNAME_LENGTH+strlen(provider_ID),sensor_ID); } + + /* SELF-REMINDER + * + * Cambiar esto pa usar la función de Ferràn + * + * --Imanol + */ + response_code = http_post(URL,statement,URL_length,length); if(response code == 200) { @@ -128,6 +161,4 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free(str2); } return str; -} - -//id, desc, type, unit \ No newline at end of file +} \ No newline at end of file diff --git a/Project/applications/smartcities/main.c b/Project/applications/smartcities/main.c index 53c6800..61a4c3c 100644 --- a/Project/applications/smartcities/main.c +++ b/Project/applications/smartcities/main.c @@ -3,6 +3,7 @@ #include "lwip/inet.h" #include "globals.h" #include "httpClient.h" +#include "callbacks.h" #define WIFI_MODE WIFI_MODE_CLIENT #define NETWORK_SSID "linksys" @@ -16,29 +17,12 @@ void initLibwismart(void) libwismart_Init(hwif); } -void dhcp_connect_result_cb(int result) -{ - libwismart_ip_addr_t ip; - if(result==LIBWISMART_DHCP_ADDRESS_ASSIGNED) - { - libwismart_GetCurrentIP(&ip,NULL,NULL); - printf("IP: %d.%d.%d.%d \r\n",ip.addr[3],ip.addr[2],ip.addr[1],ip.addr[0]); - } - else if(result==LIBWISMART_DHCP_TIMEOUT) - { - printf("DHCP timeout\r\n"); - } - else - { - printf("DHCP error\r\n"); - } - -} - -void wifi_connect_result_cb(int result) -{ - printf("WiFi Connect indication: %s\r\n", (result == WISMART_WIFI_CONNECTED) ? "Connected": "Failed\r\n"); -} +/* erase me + * + * He movido los callbacks a un .c y .h aparte para mayor modularidad, conveniéntemente llamados callbacks.c y callbacks.h + * + * --Imanol + */ int main(void) { -- libgit2 0.22.2