From 3f393e23d5c4b6fbac2346d57811c118ebff35e1 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Sun, 8 Jun 2014 21:47:17 +0000 Subject: [PATCH] --- Project/applications/smartcities/configServer.c | 7 ------- Project/applications/smartcities/include/adc.h | 12 ++++++------ Project/applications/smartcities/include/buffer.h | 6 +++--- Project/applications/smartcities/include/callbacks.h | 40 ++++++++++++++++++++++++++++++---------- Project/applications/smartcities/include/configServer.h | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- Project/applications/smartcities/include/globals.h | 28 ++++++++++++++++++++++------ Project/applications/smartcities/include/i2c.h | 4 ++-- Project/applications/smartcities/include/json.h | 45 ++++++++++++++++++++++++++++++++++++++------- Project/applications/smartcities/include/module.h | 12 ++++++++++++ Project/applications/smartcities/include/timer-loop.h | 25 ++++++++++++++++++++++--- Project/applications/smartcities/timer-loop.c | 14 +------------- 11 files changed, 183 insertions(+), 62 deletions(-) diff --git a/Project/applications/smartcities/configServer.c b/Project/applications/smartcities/configServer.c index b05ea75..512b646 100644 --- a/Project/applications/smartcities/configServer.c +++ b/Project/applications/smartcities/configServer.c @@ -348,13 +348,6 @@ int configServer_hex2bin(char *hex, char *buf, size_t hexLen) { size_t i, j = 0; uint8_t byte; - /*if( (hexLen%2) != 0){ - return -1; - }* - - for (i = 0; i < hexLen; i += 2) { - *opos++ = (ipos[i] << 4) | (ipos[i + 1]); - }*/ for(i = 0; i < strlen(hex); i += 2) { diff --git a/Project/applications/smartcities/include/adc.h b/Project/applications/smartcities/include/adc.h index 5b3b77a..ea41277 100644 --- a/Project/applications/smartcities/include/adc.h +++ b/Project/applications/smartcities/include/adc.h @@ -77,23 +77,23 @@ #define ADCsound_GPIO_STR "PA2" //! Battery ADC initialization function -/*! This function initializes the base HW to start battery ADC conversion */ +/*! This function initializes the base HW to start battery ADC conversion. */ void adc_batt_init(void); //! Battery ADC processing function -/*! This function calls adc_batt_read and converts the raw digital value to the analog scale defined for the battery ADC */ +/*! This function calls adc_batt_read and converts the raw digital value to the analog scale defined for the battery ADC. Returns the analog value. */ uint32_t adc_batt_process(void); //! Battery ADC raw read function -/*! This function starts an ADC conversion and returns the raw 12 bit digital value */ +/*! This function starts an ADC conversion and returns the raw 12 bit digital value. */ uint16_t adc_batt_read(void); //! Battery ADC initialization function -/*! This function initializes the base HW to start sound ADC conversion */ +/*! This function initializes the base HW to start sound ADC conversion. */ void adc_sound_init(void); //! Battery ADC processing function -/*! This function calls adc_batt_read and converts the raw digital value to the analog scale defined for the sound ADC */ +/*! This function calls adc_batt_read and converts the raw digital value to the analog scale defined for the sound ADC. Returns the analog value. */ uint32_t adc_sound_process(void); //! Battery ADC raw read function -/*! This function starts an ADC conversion and returns the raw 12 bit digital value */ +/*! This function starts an ADC conversion and returns the raw 12 bit digital value. */ uint16_t adc_sound_read(void); #endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/buffer.h b/Project/applications/smartcities/include/buffer.h index e4761bc..f71a54a 100644 --- a/Project/applications/smartcities/include/buffer.h +++ b/Project/applications/smartcities/include/buffer.h @@ -37,15 +37,15 @@ char** put_message(char* info /*! Data to be stored */, char** buf /*! Old buffer */ ,uint32_t *index /*! Buffer index to store the data */, uint32_t *buf_len /*! Old buffer length */); //! Memory check function -/*! This function checks the current used memory and returns the memory status definitions according to the amount of memory used. */ +/*! This function checks the current used memory and returns the memory status definitions according to the amount of memory used. Returns one of the Memory Status codes. */ int check_memory(void); //! Buffer data send function -/*! This function sends the data in a specified buffer to the server. If successful, it frees the memory used by it. */ +/*! This function sends the data in a specified buffer to the server. If successful, it frees the memory used by it. Returns one of the JSON status codes (see json.h). */ int send(char** buf /*! Buffer to send */, uint32_t *index /*! Current buffer index */, uint32_t *size_buf /*! Buffer size */, char *provider_ID /*! Current module ID */, char *sensor_ID /*! Sensor ID for the data stored in that buffer */); //! Buffer join function -/*! This function returns a copy of the buffer passed by argument with one more slot to store another piece of data from the sensors */ +/*! This function returns a copy of the buffer passed by argument with one more slot to store another measurement from the sensors */ char** join_buf(char** buf /*! Old buffer */, uint32_t *buf_len /*! Old buffer length*/); #endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/callbacks.h b/Project/applications/smartcities/include/callbacks.h index b71f16e..9c93f91 100644 --- a/Project/applications/smartcities/include/callbacks.h +++ b/Project/applications/smartcities/include/callbacks.h @@ -4,7 +4,7 @@ * @author Imanol Barba Sabariego, Maria Jorba Brosa * @date 08/06/2014 * - * The functions declared in this file are used as callbacks by the network functions in the libwismart library. + * The functions declared in this file are used as callbacks by the network functions in the libwismart library. */ #ifndef CALLBACKS_H @@ -14,22 +14,42 @@ #include "lwip/inet.h" #include "configServer.h" +//! Wi-Fi client mode definition #define WIFI_MODE_CLIENT 1 +//! Wi-Fi Host AP mode definition #define WIFI_MODE_SOFTAP 2 -#define MAX_RETRIES 5 +//! Maximum connection retries +/*! This defines how many Association or DHCP requests retries will take place before considering that network connectivity is not available at the moment. */ +#define MAX_RETRIES 5 +//! Connection state definition (Connected in the past) +/*! This definition tells that the device had connectivity at some point in the past with the current Wi-Fi configuration. It doesn't define if it has or has not Wi-Fi connectivity at the moment. */ #define CONNECTED_PAST 0x02 +//! Connection state definition (Connected) +/*! This definition tells that the device is currently connected to the Wi-Fi network. */ #define CONNECTED_NOW 0x01 +//! Connection state definition (Connected in the past) +/*! This definition tells that the device is not connected to the Wi-Fi network. */ #define NOT_CONNECTED 0x00 -void dhcp_connect_result_cb(int result); -void wifi_connect_result_cb(int result); -void wifi_connect_ap_result_cb(int result); -void wifi_connect_result_cb(int result); -void softapMode_clientIndicationCb(wismart_softap_cb_t reason, const uint8_t *mac, const libwismart_ip_addr_t *ip); -void softapMode_apStartedCb(int result); -void printWifiInfo(uint8_t wifiMode); - +//! DHCP callback +/*! This function is called whenever the libwismart DHCP acquirement function returns a result, and prints if it has been successful, and if it was, which IP has obtained. */ +void dhcp_connect_result_cb(int result /*! Callee function result */); +//! Wi-Fi connect callback +/*! This function is called whenever the libwismart Wi-Fi association function returns a result, and prints if it has been successful. */ +void wifi_connect_result_cb(int result /*! Callee function result */); +//! AP client callback +/*! This function is called whenever the some client connects to the created AP, and prints which is its MAC address and if it which IP has the DHCP server assigned to it (if applicable). */ +void softapMode_clientIndicationCb(wismart_softap_cb_t reason /*! Client disconnect reason (if applicable) */, const uint8_t *mac /*! Client MAC address */, const libwismart_ip_addr_t *ip /*! DHCP assigned IP address of the client */); +//! Host AP callback +/*! This function is called whenever the libwismart Host AP setup function returns a result, and prints if it has been successful and if it was, the parameters of the created AP. */ +void softapMode_apStartedCb(int result /*! Callee function result */); +//! Print AP info +/*! This function prints information about the AP created by the device when this happens. */ +void printWifiInfo(uint8_t wifiMode /*! Current Wi-Fi mode */); + +//! Debug printing definition +/*! This definition is used for debugging purposes, it is a shorthand for printing debug information */ #define DBG_CALLBACKS(fmt,...) printf("%c[1;35mcallbacks.c:%c[1;00m "fmt,0x1B,0x1B, ##__VA_ARGS__) //#define DBG_CALLBACKS(fmt,...) printf("") diff --git a/Project/applications/smartcities/include/configServer.h b/Project/applications/smartcities/include/configServer.h index f352ab2..ddb42fe 100644 --- a/Project/applications/smartcities/include/configServer.h +++ b/Project/applications/smartcities/include/configServer.h @@ -1,3 +1,12 @@ + +/**@file + * @brief Declaration for the HTTP configuration server functions + * @author Imanol Barba Sabariego + * @date 08/06/2014 + * + * The functions declared in this file are used to set up and manage the HTTP configuration server. + */ + #ifndef CONFIG_SERVER_H #define CONFIG_SERVER_H @@ -10,43 +19,76 @@ #include "ch.h" #include "fsdata.c" +//! Debug printing definition +/*! This definition is used for debugging purposes, it is a shorthand for printing debug information */ #define CONFIG_SERVER_DBG(fmt,...) printf("%c[1;35mconfigServer.c:%c[1;00m "fmt,0x1B,0x1B, ##__VA_ARGS__) +//! Debug printing definition +/*! This definition is used for debugging purposes, it is a shorthand for printing debug information */ #define CONFIG_SERVER_DBG_WARNING(fmt,...) printf("%c[1;35mconfigServer.c:%c[1;33m[WARNING] "fmt"%c[1;00m",0x1B,0x1B,0x1B, ##__VA_ARGS__) //#define CONFIG_SERVER_DBG(fmt,...) printf("") //#define CONFIG_SERVER_DBG_WARNING(fmt,...) printf("") -void configServer_start(uint8_t enableApScan); +//! HTTP configuration server start function +/*! This function starts the HTTP server to configure the device. */ +void configServer_start(uint8_t enableApScan /*! Wether or not to enable AP scan */); +//! HTTP server connection handler +/*! This function handles an incoming connection to the HTTP server. */ void configServer_connect(void); -uint32_t condigServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllocType); +//! HTTP resource building function void configServer_buildResources(void); +//! HTTP client parameters function +/*! This function reads the parameters that the client POSTed to the HTTP server and writes them to the internal configuration register. */ void configServer_setClientParameters(void); +//! Device reboot function void configServer_reboot(void); +//! Device reboot timer handler void configServer_rebootTimerHandler(void *arg); -int configServer_hex2bin(char *hex, char *buf, size_t hexLen); -uint32_t configServer_dynamicCb(char* varName, char** varValue, uint8_t* varAllocType); +//! Hex to Binary conversion function +/*! This function converts a hexadecimal wep key to a binary string. Returns 0 if successful.*/ +int configServer_hex2bin(char *hex /*! Input hex string */, char *buf /*! Output binary string */, size_t hexLen /*! Hex string length */); +//! HTTP Dynamic content callback function +/*! This function is called whenever a client requests dynamic content to the server, and sends the requested data through the socket. */ +uint32_t configServer_dynamicCb(char* varName /*! Requested variable name */, char** varValue /*! Output pointer for the requested data */, uint8_t* varAllocType /*! Pointer to indicate wether the requested data was dynamically allocated or not */); +//! Configuration registry parameters typedef struct { - char ssid[33]; // ssid clave red usuario contrasenya tipicript + //! Wi-Fi network SSID + char ssid[33]; + //! Wi-Fi network WEP key char wepkey[14]; + //! Wi-Fi network WPA passphrase char passphrase[64]; + //! Wi-Fi network security uint16_t security; + //! RADIUS user char user[64]; + //! RADIUS password char password[64]; + //! Geolocalization coordinates (i.e: 41.557255 2.096183) char localization[32]; } config_params_t; extern wismart_registryKey_t geo; +//! Wi-Fi Open network security definition #define SECURITY_TYPE_OPEN "open" +//! Wi-Fi WPA network security definition #define SECURITY_TYPE_WPA "wpa" +//! Wi-Fi WEP network security definition (Index 1) #define SECURITY_TYPE_WEP1 "wep1" +//! Wi-Fi WEP network security definition (Index 2) #define SECURITY_TYPE_WEP2 "wep2" +//! Wi-Fi WEP network security definition (Index 3) #define SECURITY_TYPE_WEP3 "wep3" +//! Wi-Fi WEP network security definition (Index 4) #define SECURITY_TYPE_WEP4 "wep4" +//! HTTP server HTML MIME type string #define CONFIG_SERVER_MIME_TYPE_HTML "text/html; charset=UTF-8" +//! HTTP server CSS MIME type string #define CONFIG_SERVER_MIME_TYPE_CSS "text/css; charset=UTF-8" +//! HTTP server PNG MIME type string #define CONFIG_SERVER_MIME_TYPE_PNG "image/png" diff --git a/Project/applications/smartcities/include/globals.h b/Project/applications/smartcities/include/globals.h index 0740e72..2926899 100644 --- a/Project/applications/smartcities/include/globals.h +++ b/Project/applications/smartcities/include/globals.h @@ -1,21 +1,37 @@ + +/**@file + * @brief Definition of global parameters + * @author Imanol Barba Sabariego + * @date 08/06/2014 + * + * Here are defined several parameters that affect the current device configuration + */ + #ifndef GLOBALS_H #define GLOBALS_H +//! SENTILO platform server IP #define SERVER_IP "147.83.39.240" +//! SENTILO platform server hostname #define SERVER_HOSTNAME SERVER_IP +//! Module ID length #define MODULE_ID_LENGTH 6 -#define BUFFER_LENGTH 30 +//! Maximum RAM available #define MAX_RAM 34704 +//! Current device module ID #define MODULE_ID "000001" +//! Default Wi-Fi mode #define WIFI_MODE WIFI_MODE_CLIENT +//! Hosted AP name #define NETWORK_SSID_AP "modularsense" +//! Hosted AP encryption key #define NETWORK_KEY_AP NULL +//! Hosted AP channel #define NETWORK_CHANNEL_AP 1 +//! Wait time for each retry when in hard limit #define HARD_LIMIT_WAIT_TIME 10*1000 //5*60*1000 +//! NTP remote server IP #define SNTP_SERVER_ADDRESS ipaddr_addr("81.184.154.182") /* your ntp server */ - +//! Type of server used (lewis.upc.es) #define SERVER_LEWIS -#endif - -// printf("%c[1;31m[FREE] Freed bytes from %08x%c[1;00m\r\n",0x1B,p,0x1B); -// printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,size,(hp),0x1B); +#endif \ No newline at end of file diff --git a/Project/applications/smartcities/include/i2c.h b/Project/applications/smartcities/include/i2c.h index 918e77a..6d9f37d 100644 --- a/Project/applications/smartcities/include/i2c.h +++ b/Project/applications/smartcities/include/i2c.h @@ -42,11 +42,11 @@ void I2C_stop(I2C_TypeDef* I2Cx /*! I2C device to use */); void I2C_write(I2C_TypeDef* I2Cx /*! I2C device to use */, uint8_t data /*! Byte to write */); //! I2C read byte function (with master ACK) -/*! This function fetches a byte from the I2C bus that has been stored into the internal register and sends and ACK afterwards. */ +/*! This function returns a byte from the I2C bus that has been stored into the internal register and sends and ACK afterwards. */ uint8_t I2C_read_ack(I2C_TypeDef* I2Cx /*! I2C device to use */); //! I2C read byte function (without master ACK) -/*! This function fetches a byte from the I2C bus that has been stored into the internal register without sending and ACK afterwards. */ +/*! This function returns a byte from the I2C bus that has been stored into the internal register without sending and ACK afterwards. */ uint8_t I2C_read_nack(I2C_TypeDef* I2Cx /*! I2C device to use */); //! I2C bus scan function diff --git a/Project/applications/smartcities/include/json.h b/Project/applications/smartcities/include/json.h index 0df065c..d3c838c 100644 --- a/Project/applications/smartcities/include/json.h +++ b/Project/applications/smartcities/include/json.h @@ -1,3 +1,12 @@ + +/**@file + * @brief Declaration for the JSON related functions + * @author Imanol Barba Sabariego + * @date 08/06/2014 + * + * The functions declared in this file are used to generate and send the different JSON-formatted strings to communicate with the SENTILO platform. + */ + #ifndef JSON_H #define JSON_H @@ -11,22 +20,44 @@ #include "libwismart.h" #include "httpClient.h" +//! JSON data send status definition (OK) #define JSON_POST_OK 0 +//! JSON data send status definition (Communication error occurred) #define JSON_COMM_ERROR 1 +//! JSON data send status definition (Undefined error occurred) #define JSON_OTHER_ERROR 2 +//! Free memory from the strings to be joined in join_strings #define JOIN_NO_FREE 0 +//! Do not free memory from the strings to be joined in join_strings #define JOIN_FREE_MEM 1 +//! Debug printing definition +/*! This definition is used for debugging purposes, it is a shorthand for printing debug information */ #define DBG_JSON(fmt,...) printf("%c[1;35mjson.c:%c[1;00m "fmt,0x1B,0x1B, ##__VA_ARGS__) //#define DBG_JSON(fmt,...) printf("") -uint8_t register_sensor(sensor sens); -char* prepare_json_observation_statement(char** data, uint32_t nObservations); -char* prepare_json_register_statement(module mod, sensor sens, char *additional_info); -char* prepare_observation(char* observation, uint32_t length); -uint8_t send_json(char* statement, uint32_t length, char* provider_ID, char* sensor_ID); -uint32_t find_next_index(char* string, uint32_t length, char delimiter); -char* join_strings(char* str1, char* str2, uint32_t len1, uint32_t len2, uint8_t free_mem); +//! Sensor register function +/*! This function generates a JSON string and sends it to the SENTILO server to register the sensor in the platform. Returns one of the JSON status codes.*/ +uint8_t register_sensor(sensor sens /*! Sensor to register */); + +//! Observation statement generation function. +/*! Returns the generated JSON string for the buffer passed by argument. */ +char* prepare_json_observation_statement(char** data /*! Observation buffer */, uint32_t nObservations /*! Number of measurements inside the buffer */); +//! Register statement generation function. +/*! Returns the generated JSON string for the sensor passed by argument. */ +char* prepare_json_register_statement(module mod /*! Current device module data */, sensor sens /*! Sensor for which the statement is generated */, char *additional_info /*! Additional registration information */); +//! Observation generation function +/*! Returns the generated JSON string for the observation passed by argument. */ +char* prepare_observation(char* observation /*! Observation for which the statement is generated */, uint32_t length /*! Length of the observation string */); +//! JSON send function +/*! This function sends the JSON statement passed by argument to the SENTILO server. Returns one of the JSON status codes. */ +uint8_t send_json(char* statement /*! Statement to send */, uint32_t length /*! Statement string length */, char* provider_ID /*! Current device module ID*/ , char* sensor_ID /*! Sensor ID of the current statement */); +//! String parsing function +/*! This function finds the next ocurrence of the character passed by argument and returns the index in which that ocurrence can be found. */ +uint32_t find_next_index(char* string /*! String where to search for ocurrences */, uint32_t length /*! Length of the string */, char delimiter /*! Character to search in the string */); +//! Join two stings +/*! This function joins two strings passed by argument an returns the new string. Optionally, it may free the memory of the two original strings. */ +char* join_strings(char* str1 /*! First string */, char* str2 /*! Second string */, uint32_t len1 /*! First string length */, uint32_t len2 /*! Second string length */, uint8_t free_mem /*! Wether to free the memory from the original strings or not. Possible values are:
JOIN_NO_FREE
JOIN_FREE_MEM */); #endif diff --git a/Project/applications/smartcities/include/module.h b/Project/applications/smartcities/include/module.h index 71d5c4d..e792294 100644 --- a/Project/applications/smartcities/include/module.h +++ b/Project/applications/smartcities/include/module.h @@ -1,10 +1,22 @@ + +/**@file + * @brief Definition for the module structure and current module + * @author Imanol Barba Sabariego + * @date 08/06/2014 + * + * Here is the current module and the module information structure definitions + */ + #ifndef MODULE_H #define MODULE_H #include "globals.h" +//! Module definition struct typedef struct { + //! Module ID string char ID[MODULE_ID_LENGTH +1]; + //! Module geolocalization coordinates string char geoloc[20]; } module; diff --git a/Project/applications/smartcities/include/timer-loop.h b/Project/applications/smartcities/include/timer-loop.h index 60d4581..6c40b01 100644 --- a/Project/applications/smartcities/include/timer-loop.h +++ b/Project/applications/smartcities/include/timer-loop.h @@ -1,23 +1,42 @@ + +/**@file + * @brief Declaration for the timing control functions + * @author Ferràn Quer i Guerrero + * @date 08/06/2014 + * + * The functions declared in this file are used to synchronize the different actions taken by the device in a timely fashion. + */ #ifndef TIMER_LOOP_H #define TIMER_LOOP_H #include "libwismart.h" #include "lwip/inet.h" +//! Half hour time definition #define HALF_HOUR 60*30 // time in seconds +//! Five minutes time definition #define FIVE_MIN 60*5 // time in seconds -//#define LONG_PERIOD HALF_HOUR //60*3 // for testing +//#define LONG_PERIOD HALF_HOUR //60*3 // for testing //#define SHORT_PERIOD FIVE_MIN //60*1 // for testing +//! Long period time definition +/*! This defines how often will the device try to send data to the server. */ #define LONG_PERIOD 60*3 //60*3 // for testing +//! Short period time definition +/*! This defines how often will the device try to fetch data from the sensors. */ #define SHORT_PERIOD 60*1 //60*1 // for testing +//! Debug printing definition +/*! This definition is used for debugging purposes, it is a shorthand for printing debug information */ #define DBG_TIMER(fmt,...) printf("%c[1;35mtimer-loop.c:%c[1;00m "fmt,0x1B,0x1B, ##__VA_ARGS__) //#define DBG_TIMER(fmt,...) printf("") +//! Returns elapsed time since device startup in seconds unsigned long getSystemTime(void); -unsigned long getElapsedTime(unsigned long t); +//! Returns elapsed time since the timestamp passed by argument +unsigned long getElapsedTime(unsigned long t /*! Time from which calculate the difference */); -void sleep_thread(unsigned long seconds); +//! Sleep thread function +void sleep_thread(unsigned long seconds /*! Seconds to sleep */); #endif \ No newline at end of file diff --git a/Project/applications/smartcities/timer-loop.c b/Project/applications/smartcities/timer-loop.c index 49dfadd..564f13f 100644 --- a/Project/applications/smartcities/timer-loop.c +++ b/Project/applications/smartcities/timer-loop.c @@ -8,24 +8,12 @@ #include "timer-loop.h" - -/*************************************************** - ** CATALAN **************************************** - * Agafem el temps des de l'NTP el primer cop. * - * Mostres síncrones amb els altres nodes modsense * - * Cada mitja hora (long period) asíncrona amb els * - * altres nodes, NTP i enviament de mostres. * - ** ENGLISH **************************************** - * Get NTP time the first time. * - * Values sync'd with other modularsense nodes * - * Every half an hour (long period), async'd to * - * other nodes, get NTP and send all data. * - ***************************************************/ /* * unsigned int representa: * - segons: 2^32 -1 = 4.294.967.295 segons // 136.1 average Gregorian years * - milisegons:ans/1000 = 4.294.967,295 segons // 49 days 17 hours 2 minutes 47.3 seconds */ + unsigned long getSystemTime() { return (long)libwismart_GetTime()/1000; -- libgit2 0.22.2