Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Revision:
20:505d29d5bdfc
Parent:
19:81dfc04ce0bb
Child:
21:207549b3711e
--- a/MbedClient.cpp	Fri Mar 20 14:26:52 2015 +0000
+++ b/MbedClient.cpp	Mon Apr 13 14:24:44 2015 +0000
@@ -28,9 +28,11 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include "b64.h"
 #include "mbed.h"
+#include "rtos.h"
+
 #include "MbedClient.h"
+#include "SmartRestConf.h"
 #include "logging.h"
 
 #define STATE_INIT 0
@@ -44,11 +46,7 @@
 //#define MBCL_DBG(fmt, ...)
 #define DNS_ENTRY_DURATION 50
 
-MbedClient::MbedClient(const char* host, uint16_t port, MDMSerial& mdm, uint8_t tries) :
-    _host(host),
-    _username(NULL),
-    _password(NULL),
-    _port(port),
+MbedClient::MbedClient(MDMSerial& mdm, uint8_t tries) :
     _tries(tries),
     _state(STATE_INIT),
     _isStreamRequest(false),
@@ -65,23 +63,12 @@
 {
 }
 
-uint8_t MbedClient::setAuthorization(const char* username, const char* password)
-{
-    if (_state != STATE_INIT)
-        return internalError();
-
-    _username = username;
-    _password = password;
-    MBCL_DBG("\033[32mMbedClient:\033[39m Set authorization to %s:%s\r\n", username, password);
-    return CLIENT_OK;
-}
-
 uint8_t MbedClient::beginRequest()
 {
     if (_state != STATE_INIT)
         return internalError();
 
-    MBCL_DBG("\033[32mMbedClient:\033[39m Beginning SmartREST request.\r\n");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Begin SmartREST request.\n");
     _source.setTimeout(60000);
     if (!connect())
         return connectionError();
@@ -101,7 +88,7 @@
     // set stream request flag to later set the timeout right
     _isStreamRequest = true;
 
-    MBCL_DBG("\033[32mMbedClient:\033[39m Beginning SmartREST request.\r\n");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Begin SmartREST stream.\n");
     _source.setTimeout(60000);
     if (!connect())
         return connectionError();
@@ -115,10 +102,10 @@
 
 uint8_t MbedClient::sendIdentifier(const char* identifier)
 {
+    MBCL_DBG("\033[32mMbedClient:\033[39m Send identifier.\n");
     if (_state != STATE_IN_REQUEST)
         return internalError();
 
-    MBCL_DBG("\033[32mMbedClient:\033[39m Sending template identifier.\r\n");
     if ((identifier != NULL) && (strlen(identifier) != 0)) {
         if ((!send("X-Id: ")) ||
             (!send(identifier)) ||
@@ -131,13 +118,11 @@
 
 uint8_t MbedClient::sendData(const DataGenerator& generator)
 {
-    size_t len;
-    
-    if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
+    MBCL_DBG("\033[32mMbedClient:\033[39m Send payload.\n");
+    if (_state != STATE_IN_REQUEST && _state != STATE_SENT_ID)
         return internalError();
     
-    MBCL_DBG("\033[32mMbedClient:\033[39m Sending request payload.\r\n");
-    len = generator.writtenLength();
+    size_t len = generator.writtenLength();
     if ((!send("Content-Length: ")) ||
         (_sink.write((unsigned long)len) == 0) ||
         (!send("\r\n\r\n")))
@@ -151,14 +136,13 @@
 
 uint8_t MbedClient::endRequest()
 {
+    MBCL_DBG("\033[32mMbedClient:\033[39m End request.\n");
     if ((_state != STATE_IN_REQUEST) &&
         (_state != STATE_SENT_ID) &&
         (_state != STATE_SENT_DATA)) {
         return internalError();
     }
     
-    MBCL_DBG("\033[32mMbedClient:\033[39m Ending request.\r\n");
-
     if (_state != STATE_SENT_DATA) {
         // send end of headers
         if (!send("\r\n")) {
@@ -176,23 +160,19 @@
 
 uint8_t MbedClient::awaitResponse()
 {
+    aDebug("\033[32mThread %p:\033[39m Await response.\n", Thread::gettid());
     if (_state != STATE_REQ_COMPLETE) {
         return internalError();
     }
+    // set timeout to fifteen minutes if stream request flag set
+    if (_isStreamRequest) {
+        _source.setTimeout(300000);
+    }
     
-    MBCL_DBG("\033[32mMbedClient:\033[39m Awaiting response...\r\n");
-
     uint8_t status = _filter.readStatus();
-    MBCL_DBG("\033[32mMbedClient:\033[39m Status code: %u\r\n", status);
-
     if ((status != 200) || (!_filter.skipHeaders())) {
         return connectionError();
-    }
-    
-    // set timeout to fifteen minutes if stream request flag set
-//    if (_isStreamRequest) {
-//        _source.setTimeout(900000);
-//    }
+    }    
     
     _state = STATE_RECVD_RESPONSE;
     return CLIENT_OK;
@@ -205,8 +185,7 @@
 
 void MbedClient::stop()
 {
-    MBCL_DBG("\033[32mMbedClient:\033[39m Resetting client.\r\n");
-    MBCL_DBG("\033[32mMbedClient:\033[39m Bytes tramsmitted sofar: %zu\r\n", packetSize);
+    MBCL_DBG("\033[32mMbedClient:\033[39m Reset client, %zu bytes sent.\n", packetSize);
     _isStreamRequest = false;
     _sock.close();
     _source.reset();
@@ -217,25 +196,23 @@
 
 bool MbedClient::connect()
 {
-    uint8_t tries;
-
-    tries = _tries;
+    uint8_t tries = _tries;
     do {
         if (cachedIPValid == 0) {
-            MDMParser::IP ip = _mdm.gethostbyname(_host);
-            if (ip == NOIP) 
+            MDMParser::IP ip = _mdm.gethostbyname(getHost());
+            if (ip == NOIP)
                 continue;
             const unsigned char *c = (const unsigned char*)&ip;
             snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
-            MBCL_DBG("\033[32mMbedClient:\033[39m Connecting to %s:%u with resolved IP %s\r\n", _host, _port, cachedIP);
+            aInfo("\033[32mThread %p:\033[39m Connect to %s:%u (IP: %s)\n", Thread::gettid(), getHost(), getPort(), cachedIP);
         } else {
-            MBCL_DBG("\033[32mMbedClient:\033[39m Connecting to %s:%u\r\n", cachedIP, _port);
+            aDebug("\033[32mMThread %p:\033[39m Connect to %s:%u\n", Thread::gettid(), cachedIP, getPort());
         }
-        if (_sock.connect(cachedIP, _port) >= 0)
+        if (_sock.connect(cachedIP, getPort()) >= 0)
             break;
         cachedIPValid = 0;
         _sock.close();
-        MBCL_DBG("\033[32mMbedClient:\033[39m Connection attempt failed.\r\n");
+        aCritical("\033[32mThread %p:\033[39m Connect failed.\n", Thread::gettid());
     } while (--tries > 0);
 
     cachedIPValid = (cachedIPValid+1) % DNS_ENTRY_DURATION;
@@ -249,12 +226,12 @@
 
 bool MbedClient::sendRequestHeader(const char *uri)
 {
-    MBCL_DBG("\033[32mMbedClient:\033[39m Sending request header.\r\n");
+    MBCL_DBG("\033[32mMbedClient:\033[39m Send header.\n");
     if ((!send("POST ")) ||
         (!send(uri)) ||
         (!send(" HTTP/1.0\r\n")) ||
         (!send("Host: ")) ||
-        (!send(_host)) ||
+        (!send(getHost())) ||
         (!send("\r\n")))
         return false;
     
@@ -263,37 +240,19 @@
 
 bool MbedClient::sendBasicAuth()
 {
-    size_t ul, pl; unsigned char input[3]; unsigned char output[5];
-    int inputOffset = 0;
-
     // no need to send authorization if not specified
+    const char* _username = getUsername();
+    const char* _password = getPassword();
     if ((_username == NULL) || (strlen(_username) == 0) ||
         (_password == NULL) || (strlen(_password) == 0))
         return true;
 
     if (!send("Authorization: Basic "))
         return false;
+    
+    if (!send(getAuthStr()))
+        return false;
         
-    ul = strlen(_username);
-    pl = strlen(_password);
-
-    for (int i = 0; i < (ul+1+pl); i++) {
-        if (i < ul)
-            input[inputOffset++] = _username[i];
-        else if (i == ul)
-            input[inputOffset++] = ':';
-        else
-            input[inputOffset++] = _password[i-(ul+1)];
-
-        if ((inputOffset == 3) || (i == ul+pl)) {
-            b64_encode(input, inputOffset, output, 4);
-            output[4] = '\0';
-            if (!send((char*)output))
-                return false;
-            inputOffset = 0;
-        }
-    }
-    
     if (!send("\r\n"))
         return false;
     return true;
@@ -301,14 +260,14 @@
 
 uint8_t MbedClient::internalError()
 {
-    MBCL_DBG("\033[32mMbedClient:\033[39m Internal error occurred.\r\n");
+    aError("\033[32mMbedClient:\033[39m Internal error.\n");
     _state = STATE_INTERNAL_ERROR;
     return CLIENT_INTERNAL_ERROR;
 }
 
 uint8_t MbedClient::connectionError()
 {
-    MBCL_DBG("\033[32mMbedClient:\033[39m Connection error occurred.\r\n");
+    aError("\033[32mMbedClient:\033[39m Connect error.\n");
     _state = STATE_INTERNAL_ERROR;
     return CLIENT_CONNECTION_ERROR;
 }