Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Revision:
112:3872569be2af
Parent:
109:b7a403dbceb6
Child:
116:5de54f09f754
--- a/DeviceBootstrap.cpp	Fri May 15 10:14:05 2015 +0000
+++ b/DeviceBootstrap.cpp	Fri May 15 14:07:51 2015 +0000
@@ -1,8 +1,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include "rtos.h"
 #include "DeviceBootstrap.h"
-#include "rtos.h"
 #include "Storage.h"
 #include "LCDDisplay.h"
 #include "ComposedRecord.h"
@@ -12,27 +12,30 @@
 #include "SmartRestConf.h"
 #include "logging.h"
 
-/** The username used for device bootstrapping. */
-#define BOOTSTRAP_USERNAME "management/devicebootstrap"
-
-/** The password used for device bootstrapping. */
+// Device bootstrap tenant, username and password
+#define BOOTSTRAP_TENANT "management"
+#define BOOTSTRAP_USERNAME "devicebootstrap"
 #define BOOTSTRAP_PASSWORD "Fhdt1bb1f"
 
 DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
-    DeviceInfo& deviceInfo) :
+    DeviceInfo& deviceInfo):
     _client(client),
     _deviceInfo(deviceInfo)
 {
-    *_username = *_password = '\0';
 }
 
-bool DeviceBootstrap::setUpCredentials()
+bool DeviceBootstrap::bootstrap()
 {
-    if (loadCredential(_username, _password, CREDENTIAL_LENGTH)) {
+    char tenant[CREDENTIAL_LENGTH];
+    char username[CREDENTIAL_LENGTH];
+    char password[CREDENTIAL_LENGTH];
+    if (loadCredential(tenant, username, password, CREDENTIAL_LENGTH)) {
+        setAuth(tenant, username, password);
+        aInfo("Set auth: %s/%s:%s(%s)\n", srTenant, srUsername, srPassword, srAuthStr);
         return true;
     } else if (obtainFromPlatform()) {
-        if (!saveCredential(_username, _password, CREDENTIAL_LENGTH))
-            aError("Write credentials!\n");
+        if (!saveCredential(srTenant, srUsername, srPassword, CREDENTIAL_LENGTH))
+            aError("Save credentials!\n");
         return true;
     } else {
         return false;
@@ -49,13 +52,11 @@
     if (!record.add(msgId) || !record.add(identifier))
         return false;
 
-    // set authorization for bootstrap
-    setAuth(BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
-
     LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei());
 
-    uint8_t tries = 255;
-    do {
+    // 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);
@@ -77,30 +78,14 @@
             (recvdRecord.values() != 6)) {
             return false;
         }
-        
-        setCredentials(recvdRecord.value(3).characterValue(),
-                       recvdRecord.value(4).characterValue(),
-                       recvdRecord.value(5).characterValue());        
-        LCDDisplay::inst().setLines("Bootstrap Success", _username, _password);
+
+        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;
-    } while (--tries > 0);
-
+    }
     LCDDisplay::inst().setLines("Bootstrap Failure");
     return false;
-}
-
-void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
-{
-    *_username = '\0';
-    if (tenant != NULL) {
-        strncpy(_username, tenant, CREDENTIAL_LENGTH);
-        _username[CREDENTIAL_LENGTH-1] = '\0';
-        if (strlen(_username)+1 < CREDENTIAL_LENGTH)
-            strcat(_username, "/");
-    }
-    strncat(_username, username, CREDENTIAL_LENGTH-strlen(_username));
-    _username[CREDENTIAL_LENGTH-1] = '\0';
-
-    strncpy(_password, password, CREDENTIAL_LENGTH);
-    _password[CREDENTIAL_LENGTH-1] = '\0';
-}
+}
\ No newline at end of file