Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Revision:
100:dbcd3bc51758
Parent:
97:9f2de96941c4
Child:
104:fd3571349e5d
--- a/DeviceBootstrap.cpp	Thu May 07 13:56:19 2015 +0000
+++ b/DeviceBootstrap.cpp	Fri May 08 12:19:57 2015 +0000
@@ -12,6 +12,12 @@
 #include "SmartRestConf.h"
 #include "logging.h"
 
+/** The username used for device bootstrapping. */
+#define BOOTSTRAP_USERNAME "management/devicebootstrap"
+
+/** The password used for device bootstrapping. */
+#define BOOTSTRAP_PASSWORD "Fhdt1bb1f"
+
 DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client,
     DeviceInfo& deviceInfo) :
     _client(client),
@@ -22,14 +28,22 @@
 
 bool DeviceBootstrap::setUpCredentials()
 {
-    if ((*_username == '\0' || *_password == '\0') &&
-        (!obtainFromStorage())) {
-        if (!obtainFromPlatform())
-            return false;
-        if (!writeToStorage())
-            aWarning("Can not write credentials!\n");
+//    if ((*_username == '\0' || *_password == '\0') &&
+//        (!obtainFromStorage())) {
+//        if (!obtainFromPlatform())
+//            return false;
+//        if (!writeToStorage())
+//            aError("Can not write credentials!\n");
+//    }
+    if (loadCredential(_username, _password, CREDENTIAL_LENGTH)) {
+        return true;
+    } else if (obtainFromPlatform()) {
+        if (!saveCredential(_username, _password, CREDENTIAL_LENGTH))
+            aError("Write credentials!\n");
+        return true;
+    } else {
+        return false;
     }
-    return true;
 }
 
 const char * DeviceBootstrap::username()
@@ -44,11 +58,11 @@
 
 bool DeviceBootstrap::obtainFromStorage()
 {
-    return loadPlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
+    return loadCredential(_username, _password, CREDENTIAL_LENGTH);
 }
 
 bool DeviceBootstrap::obtainFromPlatform()
-{    
+{
     ComposedRecord record;
     ParsedRecord recvdRecord;
 
@@ -58,7 +72,7 @@
         return false;
 
     // set authorization for bootstrap
-    setAuth(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD);
+    setAuth(BOOTSTRAP_USERNAME, BOOTSTRAP_PASSWORD);
 
     LCDDisplay::inst().setLines("Bootstrap", _deviceInfo.imei());
 
@@ -99,21 +113,21 @@
 
 bool DeviceBootstrap::writeToStorage()
 {
-    return savePlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
+    return saveCredential(_username, _password, CREDENTIAL_LENGTH);
 }
 
 void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
 {
     *_username = '\0';
     if (tenant != NULL) {
-        strncpy(_username, tenant, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
-        _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
-        if (strlen(_username)+1 < DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH)
+        strncpy(_username, tenant, CREDENTIAL_LENGTH);
+        _username[CREDENTIAL_LENGTH-1] = '\0';
+        if (strlen(_username)+1 < CREDENTIAL_LENGTH)
             strcat(_username, "/");
     }
-    strncat(_username, username, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-strlen(_username));
-    _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
+    strncat(_username, username, CREDENTIAL_LENGTH-strlen(_username));
+    _username[CREDENTIAL_LENGTH-1] = '\0';
 
-    strncpy(_password, password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
-    _password[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH-1] = '\0';
+    strncpy(_password, password, CREDENTIAL_LENGTH);
+    _password[CREDENTIAL_LENGTH-1] = '\0';
 }