A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

Revision:
14:dc3f8dd5c02b
Parent:
13:e76920d5e1ec
Child:
18:f76f9ae79195
--- a/MbedClient.cpp	Fri Apr 11 09:33:45 2014 +0000
+++ b/MbedClient.cpp	Mon Apr 14 11:23:50 2014 +0000
@@ -1,14 +1,11 @@
 #include "MbedClient.h"
 #include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include "b64.h"
 #include "mbed.h"
 
-const char *cExpectedStatus = "HTTP/1.* 200 OK";
-
 MbedClient::MbedClient(const char* host, uint16_t port, const char* username, const char* password)
-    : _host(host), _port(port), _username(username), _password(password), _source(_sock), _sink(_sock), _sock()
+    : _host(host), _port(port), _username(username), _password(password), _filter(_source), _source(_sock), _sink(_sock), _sock()
 {
     _state = MBED_STATE_INIT;
 }
@@ -56,19 +53,20 @@
 
 uint8_t MbedClient::sendData(DataGenerator& generator)
 {
-    char len[8];
+    size_t len; char lenstr[8];
     
     if ((_state != MBED_STATE_IN_REQUEST) && (_state != MBED_STATE_SENT_ID))
         return CLIENT_INTERNAL_ERROR;
     
-    snprintf(len, 8, "%ld", generator.writtenLength());
-
+    len = generator.writtenLength();
+    snprintf(lenstr, 8, "%ld", len);
+    
     if ((!send("Content-Length: ")) ||
-        (!send(len)) ||
+        (!send(lenstr)) ||
         (!send("\r\n\r\n")))
         return CLIENT_CONNECTION_ERROR;
 
-    if (generator.writeTo(_sink) != generator.writtenLength()) {
+    if (generator.writeTo(_sink) != len) {
         stop();
         return CLIENT_CONNECTION_ERROR;
     }
@@ -99,47 +97,10 @@
 
 uint8_t MbedClient::awaitResponse()
 {
-    int8_t state = 0; char c; size_t offset = 0;
-
     if (_state != MBED_STATE_REQ_COMPLETE)
         return CLIENT_INTERNAL_ERROR;
     
-    while ((state >= 0) && (state < 20) && (((c = _source.read()) > 0) || (_source.status() == DS_STATUS_OK))) {
-        switch (state) {
-        case 0: // read expected status line
-            if ((cExpectedStatus[offset] != c) && (cExpectedStatus[offset] != '*'))
-                state = -1;
-            offset++;
-            if (offset == strlen(cExpectedStatus))
-                state = 1;
-            break;
-        linebrk:
-        case 1:
-            if (c == '\n')
-                state = 2;
-            else if (c != '\r')
-                state = -1;
-            break;
-        case 2:
-            if (c == '\n') {
-                state = 20;
-                break;
-            } else if (c == '\r') {
-                break;
-            } else {
-                state = 3;
-                goto random;
-            }
-        random:
-        case 3:
-            if ((c == '\r') || (c == '\n')) {
-                state = 1;
-                goto linebrk;
-            }
-        }
-    }
-    
-    if (state != 20) {
+    if ((_filter.readStatus() != 200) || (!_filter.skipHeaders())) {
         stop();
         return CLIENT_CONNECTION_ERROR;
     }
@@ -150,7 +111,7 @@
 
 AbstractDataSource& MbedClient::receiveData()
 {
-    return _source;
+    return _filter;
 }
 
 void MbedClient::stop()
@@ -158,6 +119,7 @@
     _sock.close();
     _source.reset();
     _sink.reset();
+    _filter.reset();
     _state = MBED_STATE_INIT;
 }