Thinger.io Client Library for ARM mbed platform. This is a generic library that provides a base class that can be used to other develop hardware specific libraries.

Fork of ThingerClient by Alvaro Luis Bustamante

Revision:
2:8c6f158b95c3
Parent:
0:b75d784c7c1a
Child:
3:58e0153dbb37
--- a/ThingerClient.h	Fri Dec 25 17:20:21 2015 +0000
+++ b/ThingerClient.h	Fri Dec 25 18:13:38 2015 +0000
@@ -36,31 +36,64 @@
 #define THINGER_PORT 25200
 #define RECONNECTION_TIMEOUT 5 // seconds
 
-class ThingerClient : public thinger::thinger {
+class ThingerClient : public thinger::thinger
+{
 public:
     ThingerClient(const char* user, const char* device, const char* device_credential) :
-            username_(user), device_id_(device), device_password_(device_credential),
-            temp_data_(NULL), out_size_(0)
+        username_(user), device_id_(device), device_password_(device_credential),
+        temp_data_(NULL), out_size_(0)
     {}
 
     virtual ~ThingerClient()
     {}
 
 protected:
-
+    /** Connect to the given host and port
+    * Initialize a socket and connect it to the given host and port
+    * \param host the host address to use
+    * \param port the host port to connect to
+    * \return true on success, or false on failure
+    */
     virtual bool    socket_start(const char* host, int port) = 0;
+    
+    /** Stop the current socket connection
+    * \return true on success, or false on failure
+    */
     virtual bool    socket_stop() = 0;
+    
+    /** Check if the socket is connected
+    * \return true on socket connected, or false on disconnected
+    */
     virtual bool    socket_connected() = 0;
+    
+    /** Read all specified bytes from socket to buffer
+    * \return the number of bytes read
+    */
     virtual size_t  socket_read(char* buffer, size_t size) = 0;
+    
+    ** Write all specified bytes from buffer to socket
+    * \return the number of bytes written
+    */
     virtual size_t  socket_write(char* buffer, size_t size) = 0;
+    
+    ** Check the total available data in the socket
+    * \return Ideally, the number of bytes available in the socket, or any positive number if there is pending data. 0 Otherwise.
+    */
     virtual size_t  socket_available() = 0;
+    
+    ** Initialize the network interface, i.e., initialize ethernet interface, get ip address, etc.
+    * \return the number of bytes read
+    */
     virtual bool    connect_network() = 0;
+    
+    ** Check if the network is still connected
+    * \return true if the network is connected, or false ortherwise
+    */
     virtual bool    network_connected() = 0;
 
-    virtual bool read(char *buffer, size_t size)
-    {
+    virtual bool read(char *buffer, size_t size) {
         size_t total_read = 0;
-        while(total_read<size){
+        while(total_read<size) {
             int read = socket_read(buffer, size-total_read);
             if(read<0) return false;
             total_read += read;
@@ -69,13 +102,13 @@
     }
 
     // TODO Allow removing this Nagle's algorithm implementation if the underlying device already implements it
-    virtual bool write(const char *buffer, size_t size, bool flush = false){
-        if(size>0){
+    virtual bool write(const char *buffer, size_t size, bool flush = false) {
+        if(size>0) {
             temp_data_ = (char*) realloc(temp_data_, out_size_ + size);
             memcpy(&temp_data_[out_size_], buffer, size);
             out_size_ += size;
         }
-        if(flush && out_size_>0){
+        if(flush && out_size_>0) {
             size_t written = socket_write(temp_data_, out_size_);
             bool success = written == out_size_;
             free(temp_data_);
@@ -86,13 +119,13 @@
         return true;
     }
 
-    virtual void disconnected(){
+    virtual void disconnected() {
         thinger_state_listener(SOCKET_TIMEOUT);
         socket_stop();
         thinger_state_listener(SOCKET_DISCONNECTED);
     }
-    
-    enum THINGER_STATE{
+
+    enum THINGER_STATE {
         NETWORK_CONNECTING,
         NETWORK_CONNECTED,
         NETWORK_CONNECT_ERROR,
@@ -106,9 +139,9 @@
         THINGER_AUTH_FAILED
     };
 
-    virtual void thinger_state_listener(THINGER_STATE state){
-        #ifdef _DEBUG_
-        switch(state){
+    virtual void thinger_state_listener(THINGER_STATE state) {
+#ifdef _DEBUG_
+        switch(state) {
             case NETWORK_CONNECTING:
                 printf("[NETWORK] Starting connection...\n");
                 break;
@@ -143,17 +176,16 @@
                 printf("[THINGER] Auth Failed! Check username, device id, or device credentials.\n");
                 break;
         }
-        #endif
+#endif
     }
 
-    bool handle_connection()
-    {
+    bool handle_connection() {
         bool network = network_connected();
 
-        if(!network){
+        if(!network) {
             thinger_state_listener(NETWORK_CONNECTING);
             network = connect_network();
-            if(!network){
+            if(!network) {
                 thinger_state_listener(NETWORK_CONNECT_ERROR);
                 return false;
             }
@@ -161,16 +193,16 @@
         }
 
         bool client = socket_connected();
-        if(!client){
+        if(!client) {
             client = connect_client();
-            if(!client){
+            if(!client) {
                 return false;
             }
         }
         return network && client;
     }
 
-    bool connect_client(){
+    bool connect_client() {
         bool connected = false;
         socket_stop(); // cleanup previous socket
         thinger_state_listener(SOCKET_CONNECTING);
@@ -178,16 +210,14 @@
             thinger_state_listener(SOCKET_CONNECTED);
             thinger_state_listener(THINGER_AUTHENTICATING);
             connected = thinger::thinger::connect(username_, device_id_, device_password_);
-            if(!connected){
+            if(!connected) {
                 thinger_state_listener(THINGER_AUTH_FAILED);
                 socket_stop();
                 thinger_state_listener(SOCKET_DISCONNECTED);
-            }
-            else{
+            } else {
                 thinger_state_listener(THINGER_AUTHENTICATED);
             }
-        }
-        else{
+        } else {
             thinger_state_listener(SOCKET_CONNECTION_ERROR);
         }
         return connected;
@@ -195,16 +225,16 @@
 
 public:
 
-    void handle(){
-        if(handle_connection()){
-            #ifdef _DEBUG_
+    void handle() {
+        if(handle_connection()) {
             int available = socket_available();
-            if(available>0){
+#ifdef _DEBUG_
+            if(available>0) {
                 printf("[THINGER] Available bytes: %d\n", available);
             }
-            #endif
+#endif
             thinger::thinger::handle(us_ticker_read()/1000, available>0);
-        }else{
+        } else {
             wait(RECONNECTION_TIMEOUT); // get some delay for a connection retry
         }
     }