Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Revision:
8:3a4dba260b71
Parent:
7:8159a2d12e4e
Child:
11:e1bee9a77652
--- a/MbedClient.cpp	Wed Oct 22 16:17:22 2014 +0200
+++ b/MbedClient.cpp	Thu Oct 23 13:17:04 2014 +0200
@@ -65,6 +65,7 @@
 
     _username = username;
     _password = password;
+    MBCL_DBG("Set authorization to %s:%s", username, password);
     return CLIENT_OK;
 }
 
@@ -77,14 +78,18 @@
 
     tries = _tries;
     do {
+        MBCL_DBG("Connecting to %s:%u", _host, _port);
         if (_sock.connect(_host, _port) >= 0)
             break;
         _sock.close();
+        MBCL_DBG("Connection atempt failed.");
     } while (--tries > 0);
 
     if (tries == 0)
         return connectionError();
 
+    MBCL_DBG("Sending request header.");
+
     if ((!send("POST /s HTTP/1.0\r\n")) ||
         (!send("Host: ")) ||
         (!send(_host)) ||
@@ -106,6 +111,7 @@
     if (_state != STATE_IN_REQUEST)
         return internalError();
 
+    MBCL_DBG("Sending template identifier.");
     if ((identifier != NULL) && (strlen(identifier) != 0)) {
         if ((!send("X-Id: ")) ||
             (!send(identifier)) ||
@@ -124,8 +130,8 @@
     if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
         return internalError();
     
+    MBCL_DBG("Sending request payload.");
     len = generator.writtenLength();
-
     if ((!send("Content-Length: ")) ||
         (_sink.write((unsigned long)len) == 0) ||
         (!send("\r\n\r\n")))
@@ -145,6 +151,8 @@
         (_state != STATE_SENT_DATA))
         return internalError();
     
+    MBCL_DBG("Ending request. request.");
+
     if (_state != STATE_SENT_DATA) {
         // send end of headers
         if (!send("\r\n"))
@@ -160,10 +168,17 @@
 
 uint8_t MbedClient::awaitResponse()
 {
+    uint8_t status;
+
     if (_state != STATE_REQ_COMPLETE)
         return internalError();
     
-    if ((_filter.readStatus() != 200) || (!_filter.skipHeaders()))
+    MBCL_DBG("Awaiting response...");
+
+    status = _filter.readStatus();
+    MBCL_DBG("Status code: %u", status);
+
+    if ((status != 200) || (!_filter.skipHeaders()))
         return connectionError();
     
     _state = STATE_RECVD_RESPONSE;
@@ -177,6 +192,7 @@
 
 void MbedClient::stop()
 {
+    MBCL_DBG("Resetting client.");
     _sock.close();
     _source.reset();
     _sink.reset();
@@ -224,12 +240,14 @@
 
 uint8_t MbedClient::internalError()
 {
+    MBCL_DBG("Internal error occurred.");
     _state = STATE_INTERNAL_ERROR;
     return CLIENT_INTERNAL_ERROR;
 }
 
 uint8_t MbedClient::connectionError()
 {
+    MBCL_DBG("Connection error occurred.");
     _state = STATE_INTERNAL_ERROR;
     return CLIENT_CONNECTION_ERROR;
 }