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:
Wed Aug 13 10:55:11 2014 +0000
Parent:
51:e193db6ac7cb
Child:
54:7dcc8898d87d
Commit message:
minor refactoring and improvement

Changed in this revision

DeviceBootstrap.cpp Show annotated file Show diff for this revision Revisions of this file
DeviceBootstrap.h Show annotated file Show diff for this revision Revisions of this file
DeviceIO.cpp Show annotated file Show diff for this revision Revisions of this file
DeviceIntegration.cpp Show annotated file Show diff for this revision Revisions of this file
MbedAgent.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/DeviceBootstrap.cpp	Tue Jul 29 17:53:43 2014 +0000
+++ b/DeviceBootstrap.cpp	Wed Aug 13 10:55:11 2014 +0000
@@ -8,9 +8,10 @@
 #include "IntegerValue.h"
 #include "ParsedRecord.h"
 
-DeviceBootstrap::DeviceBootstrap(SmartRest& client, MDMSerial& mdm, DeviceInfo& deviceInfo) :
+DeviceBootstrap::DeviceBootstrap(SmartRest& client, MDMSerial& mdm, DeviceIO& io, DeviceInfo& deviceInfo) :
     _client(client),
     _mdm(mdm),
+    _io(io),
     _deviceInfo(deviceInfo)
 {
     *_username = *_password = '\0';
@@ -26,7 +27,6 @@
             puts("Warning: Could not write credentials to file!");
     }
 
-    printf("Credentials: %s : %s\n", _username, _password);
     if (_client.setAuthorization(_username, _password) != SMARTREST_SUCCESS)
         return false;
     return true;
@@ -65,16 +65,13 @@
 bool DeviceBootstrap::obtainFromPlatform()
 {
     uint8_t ret;
-    const char *id;
-    
-    id = _deviceInfo.imei();
-    printf("Starting device bootstrap with '%s'\n", id);
+    uint8_t tries;
     
     ComposedRecord record;
     ParsedRecord recvdRecord;
 
     IntegerValue msgId(61);
-    CharValue identifier(id);
+    CharValue identifier(_deviceInfo.imei());
     if ((!record.add(msgId)) || (!record.add(identifier)))
         return false;
 
@@ -82,16 +79,17 @@
     if (_client.setAuthorization(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD) != SMARTREST_SUCCESS)
         return false;
 
-    while (true) {
+    _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei());
+    
+    tries = 255;
+    do {
         if (_client.send(record, "") != SMARTREST_SUCCESS) {
-            puts("Connection unsuccessful. Retrying.");
             _client.stop();
             Thread::wait(2000);
             continue;
         }
         
         if (_client.receive(recvdRecord) != SMARTREST_SUCCESS) {
-            puts("Receiving failure.");
             _client.stop();
             Thread::wait(2000);
             continue;
@@ -103,14 +101,12 @@
             
         if ((recvdRecord.values() < 1) ||
             (recvdRecord.value(0).integerValue() == 50)) {
-            puts("No credentials available yet. Retrying.");
             Thread::wait(2000);
             continue;
         }
         
         if ((recvdRecord.value(0).integerValue() != 70) ||
             (recvdRecord.values() != 6)) {
-            puts("Bad credentials received.");
             return false;
         }
         
@@ -118,8 +114,12 @@
                        recvdRecord.value(4).characterValue(),
                        recvdRecord.value(5).characterValue());
         
+        _io.lcdPrint("BOOTSTRAP SUCCESSFUL", _username, _password);
+
         return true;
-    }
+    } while (--tries > 0);
+
+    _io.lcdPrint("BOOTSTRAP FAILURE");
     return false;
 }
 
--- a/DeviceBootstrap.h	Tue Jul 29 17:53:43 2014 +0000
+++ b/DeviceBootstrap.h	Wed Aug 13 10:55:11 2014 +0000
@@ -3,6 +3,7 @@
 
 #include <stddef.h>
 #include "MDM.h"
+#include "DeviceIO.h"
 #include "SmartRest.h"
 #include "DeviceInfo.h"
 
@@ -19,7 +20,7 @@
 class DeviceBootstrap
 {
 public:
-    DeviceBootstrap(SmartRest&, MDMSerial&, DeviceInfo&);
+    DeviceBootstrap(SmartRest&, MDMSerial&, DeviceIO&, DeviceInfo&);
     
     bool setUpCredentials();
     const char * username();
@@ -37,6 +38,7 @@
     SmartRest& _client;
     MDMSerial& _mdm;
     DeviceInfo& _deviceInfo;
+    DeviceIO& _io;
     char _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH],                    
          _password[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH];
 };
--- a/DeviceIO.cpp	Tue Jul 29 17:53:43 2014 +0000
+++ b/DeviceIO.cpp	Wed Aug 13 10:55:11 2014 +0000
@@ -1,5 +1,9 @@
 #include "DeviceIO.h"
 
+#define DEF "\033[39m"
+#define GRE "\033[32m"
+#define CYA "\033[36m"
+
 DeviceIO::DeviceIO(GPSI2C& gps) :
     _gpsTracker(gps),
     _resetButton(D4), // fire button on arduino app shield
@@ -42,12 +46,20 @@
 
 void DeviceIO::lcdPrint(const char *line1, const char *line2, const char *line3)
 {
+    printf(GRE "io::lcdPrint" DEF "\r\n");
     _lcd.cls();
     _lcd.locate(0, 0);
+
     _lcd.printf("%s\n", line1);
+    printf(GRE "> " CYA "%s\r\n" DEF, line1);
+
     if (line2 != NULL) {
         _lcd.printf("%s\n", line2);
-        if (line3 != NULL)
+        printf(GRE "> " CYA "%s\r\n" DEF, line2);
+
+        if (line3 != NULL) {
             _lcd.printf("%s\n", line3);
+            printf(GRE "> " CYA "%s\r\n" DEF, line3);
+        }
     }
 }
--- a/DeviceIntegration.cpp	Tue Jul 29 17:53:43 2014 +0000
+++ b/DeviceIntegration.cpp	Wed Aug 13 10:55:11 2014 +0000
@@ -64,25 +64,11 @@
         return false;
     }
 
-    puts("Hello.");
-    if (!deviceExisting()) {
-        puts("Creating device.");
-        if (!createDevice()) {
-            puts("Failed.");
-            return false;
-        }
-        puts("Adding global identifier.");
-        if (!addGlobalIdentifier()) {
-            puts("Failed.");
-            return false;
-        }
-    }
+    if ((!deviceExisting()) && ((!createDevice()) || (!addGlobalIdentifier())))
+        return false;
 
-    puts("Updating device data.");
-    if (!updateDevice()) {
-        puts("Failed.");
+    if (!updateDevice())
         return false;
-    }
 
     return true;
 }
--- a/MbedAgent.cpp	Tue Jul 29 17:53:43 2014 +0000
+++ b/MbedAgent.cpp	Wed Aug 13 10:55:11 2014 +0000
@@ -6,7 +6,7 @@
     _mdm(mdm),
     _deviceInfo(deviceInfo),
     _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER),
-    _bootstrap(_client, _mdm, _deviceInfo),
+    _bootstrap(_client, _mdm, _io, _deviceInfo),
     _integration(_client, _tpl, _deviceId, _deviceInfo),
     _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo),
     _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor()),
@@ -34,25 +34,19 @@
 bool MbedAgent::run()
 {
     // device bootstrapping process
-    _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei());
-    if (!_bootstrap.setUpCredentials()) {
-        _io.lcdPrint("BOOTSTRAP FAILURE");
-        puts("Could not obtain credentials.");
+    if (!_bootstrap.setUpCredentials())
         return false;
-    }
-    _io.lcdPrint("BOOTSTRAP SUCCESSFUL", _bootstrap.username(), _bootstrap.password());
+
     Thread::wait(5000);
 
     _io.lcdPrint("INTEGRATION");
     if (!_integration.integrate()) {
-        puts("Device integration process failed.");
         return false;
     }
     
     char status[60];
     snprintf(status, sizeof(status), "ID: %ld", _deviceId);
     _io.lcdPrint("INTEGRATED", status);
-    puts(status);
 
     loop();
     return true;
@@ -66,9 +60,7 @@
     while (true) {
         timer.reset();
         
-        puts("Sending signal quality.");
-        if (!_signalQualityMeasurement.run())
-            puts("Failed signal str.");
+        _signalQualityMeasurement.run();
         _temperatureMeasurement.run();
         _accelerationMeasurement.run();
         _analogMeasurement.run();
--- a/main.cpp	Tue Jul 29 17:53:43 2014 +0000
+++ b/main.cpp	Wed Aug 13 10:55:11 2014 +0000
@@ -7,28 +7,47 @@
 #include "apndb.h"
 #include "GPSTracker.h"
 
+/**
+ * SIM PIN. Null for no pin.
+ */
+#define SIM_PIN NULL
+
+/**
+ * SIM GPRS login data. Leave commented out for automatic setting.
+ */
+//#define SIM_APN ""
+//#define SIM_USER ""
+//#define SIM_PASS ""
+
 int main()
 {
     MDMParser::DevStatus devStatus;
     int res;
+    uint8_t status = 0;
+    apndb_t *apn;
 
     MDMSerial mdm;
     GPSI2C gps;
-    //mdm.setDebug(4);
 
-    if (!mdm.init(NULL, &devStatus)) {
-        puts("Modem initialization failed. Check your PIN number.");
-        return 1;
-    }
-    puts("Modem initialized");
-
-    if (!gps.init()) {
-        puts("GPS initialization failed.");
-        return 1;
-    }
-    puts("Gps initialized.");
+    if (!mdm.init(SIM_PIN, &devStatus))
+        status = 1;
+    else if (!gps.init())
+        status = 2;
     
     DeviceIO io(gps);
+
+    switch (status) {
+    case 1:
+        io.lcdPrint("MODEM INIT FAILURE", "REMOVE SIM PIN");
+        break;
+    case 2:
+        io.lcdPrint("GPS INIT FAILURE");
+        break;
+    }
+    
+    if (status != 0)
+        goto error;
+    
     io.lcdPrint("DEVICE INIT");
     
     /*if (io.resetButtonPressed()) {
@@ -46,59 +65,61 @@
 
     io.lcdPrint("IMEI", devStatus.imei);
 
-    // print out basic device data
-    printf("IMEI: %s\n", devStatus.imei);
-    printf("IMSI: %s\n", devStatus.imsi);
-
-    puts("Searching for login...");
-    apndb_t *apn = apndb_get(devStatus.imsi);
+#ifndef SIM_APN
+    apn = apndb_get(devStatus.imsi);
     if (apn == NULL) {
-        puts("No APN found. Stop.");
-        io.lcdPrint("NO APN FOUND", "IMEI:", devStatus.imsi);
-        return 1;
+        io.lcdPrint("NO CARRIER FOUND", "CHECK IMSI", devStatus.imsi);
+        goto error;
     }
-    puts("Connected.");
+#endif
     
     if (!mdm.registerNet()) {
-        puts("Network registration failed.");
         io.lcdPrint("NETWORK REG ERROR");
-        return 1;
+        goto error;
     }
 
-    printf("Carrier: %s\n", apn->carrier);
-    puts("Joining Network.");
+#ifdef SIM_APN
+    if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
+        io.lcdPrint("NETWORK JOIN FAILURE");
+        goto error;
+    }
+#else
     io.lcdPrint("JOINING CARRIER", apn->carrier);
     if (mdm.join(apn->apn) == NOIP) {
         io.lcdPrint("NETWORK JOIN FAILURE");
-        puts("Could not join network. Make sure chosen carrier is correct and no credentials are required.");
-        return 1;
-    }
-    
-    DeviceInfo deviceInfo(mdm, devStatus);
-    MbedAgent agent(io, mdm, deviceInfo);
-
-    puts("Starting agent ...");
-    if (!agent.init()) {
-        io.lcdPrint("AGENT INIT FAILURE");
-        puts("Initialization failure.");
-        mdm.disconnect();
-        return 1;
+        goto error;
     }
+#endif
     
-    size_t tries = 3;
-
-    do {
-        io.lcdPrint("RUN AGENT");
-        puts("Running agent ...");
+    {
+        uint8_t tries;
+        DeviceInfo deviceInfo(mdm, devStatus);
+        MbedAgent agent(io, mdm, deviceInfo);
+    
+        io.lcdPrint("AGENT INIT");
+        if (!agent.init()) {
+            io.lcdPrint("AGENT INIT FAILURE");
+            goto error;
+        }
+        
+        tries = 3;
+        do {
+            io.lcdPrint("AGENT RUN");
+            if (agent.run())
+                break;
+        } while (--tries > 0);
 
-        if (!agent.run()) {
-            puts("Agent failure.");
-            continue;
-        } else {
-            break;
+        if (tries == 0) {
+            io.lcdPrint("AGENT RUN FAILURE");
+            goto error;
         }
-    } while (--tries > 0);
+    }
     
     mdm.disconnect();
     return 0;
+
+error:
+    io.lcdPrint("DISCONNECTING");
+    mdm.disconnect();
+    return 1;
 }