Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Revision:
117:5de54f09f754
Parent:
113:3872569be2af
--- a/DeviceBootstrap.cpp	Mon May 18 09:29:12 2015 +0000
+++ b/DeviceBootstrap.cpp	Wed May 20 09:55:49 2015 +0000
@@ -1,15 +1,12 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include "rtos.h"
 #include "DeviceBootstrap.h"
 #include "Storage.h"
 #include "LCDDisplay.h"
-#include "ComposedRecord.h"
-#include "CharValue.h"
-#include "IntegerValue.h"
-#include "ParsedRecord.h"
 #include "SmartRestConf.h"
+#include "SmartRestSocket.h"
+#include "lex.h"
 #include "logging.h"
 
 // Device bootstrap tenant, username and password
@@ -17,13 +14,6 @@
 #define BOOTSTRAP_USERNAME "devicebootstrap"
 #define BOOTSTRAP_PASSWORD "Fhdt1bb1f"
 
-DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
-    DeviceInfo& deviceInfo):
-    _client(client),
-    _deviceInfo(deviceInfo)
-{
-}
-
 bool DeviceBootstrap::bootstrap()
 {
     char tenant[CREDENTIAL_LENGTH];
@@ -44,48 +34,50 @@
 
 bool DeviceBootstrap::obtainFromPlatform()
 {
-    ComposedRecord record;
-    ParsedRecord recvdRecord;
+        char buf[SMARTREST_SIZE];
+        char buf2[SMARTREST_BODY_SIZE];
+        SmartRestSocket sock;
+        int l2 = snprintf(buf2, sizeof(buf2), "61,%s\r\n", deviceInfo.imei());
 
-    IntegerValue msgId(61);
-    CharValue identifier(_deviceInfo.imei());
-    if (!record.add(msgId) || !record.add(identifier))
-        return false;
-
-    LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei());
+        // set authorization for bootstrap
+        setAuth(BOOTSTRAP_TENANT, BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
+        LCDDisplay::inst().setLines("Bootstrap", deviceInfo.imei());
+        for (unsigned short i = 0; i < 255; ++i) {
+                int l = snprintf(buf, sizeof(buf), fmtSmartRest, "/s", l2, buf2);
+                l = sock.sendAndReceive(buf, l, sizeof(buf));
+                if (l <= 0) continue;
 
-    // set authorization for bootstrap
-    setAuth(BOOTSTRAP_TENANT, BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
-    for (uint8_t tries = 255; tries; --tries) {
-        if (_client.send(record, "") != SMARTREST_SUCCESS) {
-            _client.stop();
-            Thread::wait(2000);
-            continue;
-        }
-        if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) {
-            _client.stop();
-            Thread::wait(2000);
-            continue;
+                const char *p = skipHTTPHeader(buf);
+                if (p) {
+                        Token tok;
+                        p = lex(p, tok);
+                        if (tok.len==2 && strncmp(tok.p, "70", tok.len)==0) {
+                                for (unsigned short j = 0; *p && j < 3; ++j) {
+                                        p = lex(p, tok);
+                                }
+                                if (tok.type == Token::STRING) {
+                                        char tenant[CREDENTIAL_LENGTH] = {0};
+                                        char username[CREDENTIAL_LENGTH] = {0};
+                                        char password[CREDENTIAL_LENGTH] = {0};
+                                        strncpy(tenant, tok.p, tok.len);
+                                        p = lex(p, tok);
+                                        if (tok.type == Token::STRING)
+                                                strncpy(username, tok.p, tok.len);
+                                        else
+                                                return false;
+                                        p = lex(p, tok);
+                                        if (tok.type == Token::STRING)
+                                                strncpy(password, tok.p, tok.len);
+                                        else
+                                                return false;
+                                        setAuth(tenant, username, password);
+                                        LCDDisplay::inst().setLines("Bootstrap Success", srTenant, srUsername);
+                                        aInfo("%s/%s:%s\n", srTenant, srUsername, srPassword);
+                                        return true;
+                                } else
+                                        return false;
+                        }
+                }
         }
-        _client.stop();
-
-        if ((recvdRecord.values() < 1) ||
-            (recvdRecord.value(0).integerValue() == 50)) {
-            Thread::wait(2000);
-            continue;
-        }
-        if ((recvdRecord.value(0).integerValue() != 70) ||
-            (recvdRecord.values() != 6)) {
-            return false;
-        }
-
-        setAuth(recvdRecord.value(3).characterValue(),
-                recvdRecord.value(4).characterValue(),
-                recvdRecord.value(5).characterValue());
-        LCDDisplay::inst().setLines("Bootstrap Success", srTenant, srUsername);
-        aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr);
-        return true;
-    }
-    LCDDisplay::inst().setLines("Bootstrap Failure");
-    return false;
+        return false;
 }
\ No newline at end of file