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

Files at this revision

API Documentation at this revision

Comitter:
vwochnik
Date:
Thu Jun 12 14:33:04 2014 +0000
Parent:
32:56804dd00193
Child:
34:8a0556f39161
Commit message:
device bootstrapping

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
program.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed May 28 17:51:12 2014 +0000
+++ b/main.cpp	Thu Jun 12 14:33:04 2014 +0000
@@ -19,6 +19,7 @@
     puts("IO initialized.");
 
     MDMSerial mdm;
+    mdm.setDebug(4);
     pMdm = &mdm;
 
     if (!mdm.init(NULL, &devStatus)) {
@@ -125,7 +126,8 @@
 
     if (res < 0)
         return false;
-
+        
+    buf[res] = '\0';
     if ((ptr = strchr(buf, '\n')) == NULL)
         return false;
     *ptr = '\0';
--- 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