SmartREST client reference implementation for the u-blox C027 mbed compatible device.

Dependencies:   C027 C027_Support mbed mbed-rtos MbedSmartRest LM75B MMA7660 C12832

Fork of MbedSmartRestTest by Vincent Wochnik

Revision:
33:b7d7e6fc421e
Parent:
32:56804dd00193
Child:
34:8a0556f39161
--- a/program.cpp	Wed May 28 17:51:12 2014 +0000
+++ b/program.cpp	Thu Jun 12 14:33:04 2014 +0000
@@ -7,6 +7,23 @@
 #include "FloatValue.h"
 #include "Aggregator.h"
 
+/************* CONFIGURATION *************/
+
+/* Credentials for device bootstrap authentification.
+ * Contact cumulocity to get credentials. */
+#define DEVICEBOOTSTRAP_USERNAME ""
+#define DEVICEBOOTSTRAP_PASSWORD ""
+
+/* Uncomment and fill in credentials to turn off device bootstrapping. */
+//#define CREDENTIALS_USERNAME ""
+//#define CREDENTIALS_PASSWORD ""
+
+/* Template device identifier */
+#define TEMPLATE_DEVICE_IDENTIFIER "com_u-blox_C027_REV-A_0.10_Test1233123"
+
+/*****************************************/
+
+bool deviceBootstrap();
 long existing();
 long create();
 bool identify(long deviceId);
@@ -20,7 +37,6 @@
 credentials_t credentials = {};
 char cDeviceIdentifier[48];
 
-const char *srtplIdentifier = "com_u-blox_C027_REV-A_0.10_Test1233123";
 StaticData srtpl(
 // get device by identity
 // Usage: 100,<SERIAL/NR>
@@ -62,14 +78,23 @@
 {
     long deviceId = 0; Timer timer;
 
-    // copy credentials and identifier into fields
-    //TODO: implement bootstrap process
+#ifndef CREDENTIALS_USERNAME
+    // read credentials from modem or make a device bootstrapping
     if (!credentials_read(&credentials)) {
-        puts("Could not read credentials. Stop.");
-        return 1;
+        puts("Could not read credentials. Starting bootstrapping process.");
+        if (!deviceBootstrap()) {
+            puts("Device bootstrap failed.");
+            return 1;
+        }
     }
-
-    strcpy(cDeviceIdentifier, srtplIdentifier);
+#else
+    // copy hardcoded credentials
+    strcpy(credentials.username, CREDENTIALS_USERNAME);
+    strcpy(credentials.password, CREDENTIALS_PASSWORD);
+#endif
+    
+    // copy identifier into variable
+    strcpy(cDeviceIdentifier, TEMPLATE_DEVICE_IDENTIFIER);
 
     lcd_tenant(credentials.username);
     puts("Hello!");
@@ -106,6 +131,91 @@
     }
 }
 
+bool deviceBootstrap()
+{
+    uint8_t ret;
+    
+    ComposedRecord record(false);
+    ParsedRecord received;
+    CharValue deviceId(imei());
+    IntegerValue connectMsgId(60);
+    IntegerValue pollMsgId(61);
+
+    // copy credentials
+    strcpy(credentials.username, DEVICEBOOTSTRAP_USERNAME);
+    strcpy(credentials.password, DEVICEBOOTSTRAP_PASSWORD);
+    
+    record.add(connectMsgId);
+    record.add(deviceId);
+    
+    if (client.send(record) != SMARTREST_SUCCESS) {
+        puts("Could not connect to platform.");
+        client.stop();
+        return false;
+    }
+    client.stop();
+    
+    ret = client.receive(received);
+    if (ret == SMARTREST_SUCCESS) {
+        if ((received.values() > 0) && (received.value(0).integerValue() == 50))
+            puts("Server error. Make sure to register the device before attempting a device bootstrap.");
+        else
+            puts("Unknown error.");
+        return false;
+    }
+    record.clear();
+    
+    record.add(pollMsgId);
+    record.add(deviceId);
+    
+    while (true) {
+        if (client.send(record) != SMARTREST_SUCCESS) {
+            puts("Connection unsuccessful. Retrying.");
+            client.stop();
+            Thread::wait(2000);
+            continue;
+        }
+        
+        if (client.receive(received) != SMARTREST_SUCCESS) {
+            puts("Not received anything. Retrying.");
+            client.stop();
+            Thread::wait(2000);
+            continue;
+        }
+        client.stop();
+        
+        if (received.values() < 1) {
+            puts("Bad received values. Retrying.");
+            Thread::wait(2000);
+            continue;
+        }
+        
+        if (received.value(0).integerValue() == 50) {
+            puts("No values. Retrying.");
+            Thread::wait(2000);
+            continue;
+        }
+        
+        if (received.value(0).integerValue() != 70) {
+            puts("Unknown received message identifier.");
+            return false;
+        }
+        
+        if (received.values() != 6) {
+            puts("Bad credentials received.");
+            return false;
+        }
+        
+        credentials_set(&credentials, received.value(3).characterValue(), received.value(4).characterValue(), received.value(5).characterValue());
+        
+        printf("Username: %s\nPassword: %s\n", credentials.username, credentials.password);
+        
+        credentials_write(&credentials);
+        
+        return true;
+    }
+}
+
 long existing()
 {
     ComposedRecord newMoRec(true); // set copy=true b/c tmp objects