diff --git a/Project/applications/smartcities/callbacks.c b/Project/applications/smartcities/callbacks.c index c44fee6..9716f58 100644 --- a/Project/applications/smartcities/callbacks.c +++ b/Project/applications/smartcities/callbacks.c @@ -7,6 +7,7 @@ void dhcp_connect_result_cb(int result) { 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]); + connected = 1; } else if(result==LIBWISMART_DHCP_TIMEOUT) { diff --git a/Project/applications/smartcities/include/callbacks.h b/Project/applications/smartcities/include/callbacks.h index 89b8ca1..d9e24fb 100644 --- a/Project/applications/smartcities/include/callbacks.h +++ b/Project/applications/smartcities/include/callbacks.h @@ -6,4 +6,6 @@ void dhcp_connect_result_cb(int result); void wifi_connect_result_cb(int result); +extern uint8_t connected; + #endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/json.h b/Project/applications/smartcities/include/json.h index 1b472b3..f225f57 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -9,6 +9,7 @@ #include #include #include "libwismart.h" +#include "httpClient.h" #define JSON_POST_OK 0 #define JSON_COMM_ERROR 1 @@ -17,7 +18,7 @@ #define JOIN_NO_FREE 0 #define JOIN_FREE_MEM 1 -char* prepare_json_statement(char** data, uint32_t nObservations); +char* prepare_json_observation_statement(char** data, uint32_t nObservations); char* prepare_json_register_statement(module mod, sensor sens); char* prepare_observation(char* observation, uint32_t length); uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID); diff --git a/Project/applications/smartcities/json.c b/Project/applications/smartcities/json.c index 2285787..1ce006c 100644 --- a/Project/applications/smartcities/json.c +++ b/Project/applications/smartcities/json.c @@ -89,8 +89,7 @@ char* prepare_observation(char* observation, uint32_t length) uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID) { int connectivity, response_code; - char* URL; - int server_hostname_length = strlen(SERVER_HOSTNAME); + char *URL, *PATH; connectivity = libwismart_IsConnected(); if(connectivity != WISMART_CONNECTED) { @@ -98,32 +97,31 @@ uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sen } if(sensor_ID == NULL) //Register sensor { - URL = (char*) chHeapAlloc(NULL,17+server_hostname_length+strlen(provider_ID)); + URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME)); strcpy(URL,"http://"); strcpy(URL+7,SERVER_HOSTNAME); - strcpy(URL+7+server_hostname_length,"/catalog/"); - strcpy(URL+16+server_hostname_length,provider_ID); + PATH = (char*) chHeapAlloc(NULL,10+strlen(provider_ID)); + strcpy(PATH,"/catalog/"); + strcpy(PATH+9,provider_ID); } else //Post data { - URL = (char*) chHeapAlloc(NULL,18+server_hostname_length+strlen(provider_ID)+strlen(sensor_ID)); + URL = (char*) chHeapAlloc(NULL,8+strlen(SERVER_HOSTNAME)); strcpy(URL,"http://"); strcpy(URL+7,SERVER_HOSTNAME); - strcpy(URL+7+server_hostname_length,"/data/"); - strcpy(URL+16+server_hostname_length,provider_ID); - strcpy(URL+16+server_hostname_length+strlen(provider_ID),"/"); - strcpy(URL+17+server_hostname_length+strlen(provider_ID),sensor_ID); + URL[7+strlen(SERVER_HOSTNAME)] = '\0'; + PATH = (char*) chHeapAlloc(NULL,8+strlen(provider_ID)+strlen(sensor_ID)); + strcpy(PATH,"/data/"); + strcpy(PATH+6,provider_ID); + strcpy(PATH+6+strlen(provider_ID),sensor_ID); + PATH[7+strlen(provider_ID)+strlen(sensor_ID)] = '\0'; } - - /* SELF-REMINDER - * - * Cambiar esto pa usar la función de Ferràn - * - * --Imanol - */ - - //response_code = http_post(URL,statement,URL_length,length); - response_code = 200; //placeholder + printf("%s: %d\n\r",PATH,strlen(PATH)); + printf("%s: %d\n\r",URL,strlen(URL)); + struct httpHeaders server = { put,PATH,strlen(PATH),URL,strlen(URL)}; + response_code = httpRequest(server, statement, length); + chHeapFree(PATH); + chHeapFree(URL); if(response_code == 200) { return JSON_POST_OK; @@ -159,11 +157,8 @@ char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t strncat(str,str2,len2); if(free_mem) { - free(str1); - free(str2); + chHeapFree(str1); + chHeapFree(str2); } return str; -} - -//check if exists -//subscribe \ No newline at end of file +} \ No newline at end of file