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:
Sun Nov 30 19:34:49 2014 +0000
Parent:
66:31c754c36ed7
Child:
68:0dc778a16d0d
Commit message:
refactor credentials persistence, add factory reset upon fire press button

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
MbedAgent.cpp Show annotated file Show diff for this revision Revisions of this file
MbedAgent.h Show annotated file Show diff for this revision Revisions of this file
MbedSmartRest.lib Show annotated file Show diff for this revision Revisions of this file
io/DeviceFeedback.cpp Show annotated file Show diff for this revision Revisions of this file
io/DeviceFeedback.h Show annotated file Show diff for this revision Revisions of this file
io/DeviceMemory.cpp Show annotated file Show diff for this revision Revisions of this file
io/DeviceMemory.h 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
operation/OperationSupport.cpp Show annotated file Show diff for this revision Revisions of this file
operation/OperationSupport.h Show annotated file Show diff for this revision Revisions of this file
util/RtosSmartRest.cpp Show annotated file Show diff for this revision Revisions of this file
util/RtosSmartRest.h Show annotated file Show diff for this revision Revisions of this file
--- a/DeviceBootstrap.cpp	Thu Oct 30 14:46:22 2014 +0000
+++ b/DeviceBootstrap.cpp	Sun Nov 30 19:34:49 2014 +0000
@@ -8,11 +8,11 @@
 #include "IntegerValue.h"
 #include "ParsedRecord.h"
 
-DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, MDMSerial& mdm, DeviceIO& io, DeviceInfo& deviceInfo) :
+DeviceBootstrap::DeviceBootstrap(AbstractSmartRest& client, DeviceIO& io, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
     _client(client),
-    _mdm(mdm),
     _io(io),
-    _deviceInfo(deviceInfo)
+    _deviceInfo(deviceInfo),
+    _deviceMemory(deviceMemory)
 {
     *_username = *_password = '\0';
 }
@@ -44,22 +44,7 @@
 
 bool DeviceBootstrap::obtainFromStorage()
 {
-    char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr;
-    
-    int res = _mdm.readFile(CREDENTIALS_FILE, buf, sizeof(buf));
-    if (res < 0)
-        return false;
-        
-    buf[res] = '\0';
-    if ((ptr = strchr(buf, '\n')) == NULL)
-        return false;
-    *ptr = '\0';
-    
-    ptr = buf;
-    strcpy(_username, ptr);
-    ptr += strlen(ptr)+1;
-    strcpy(_password, ptr);
-    return true;
+    return _deviceMemory.loadPlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
 }
 
 bool DeviceBootstrap::obtainFromPlatform()
@@ -125,23 +110,7 @@
 
 bool DeviceBootstrap::writeToStorage()
 {
-    char buf[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH*2+2], *ptr;
-    size_t len;
-
-    ptr = buf;
-    len = strlen(_username);
-    strcpy(ptr, _username);
-    ptr += len;
-    
-    *ptr++ = '\n';
-    len++;
-    
-    len += strlen(_password);
-    strcpy(ptr, _password);
-    
-    _mdm.delFile(CREDENTIALS_FILE);
-    _mdm.writeFile(CREDENTIALS_FILE, buf, len);
-    return true;
+    return _deviceMemory.savePlatformCredentials(_username, _password, DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH);
 }
 
 void DeviceBootstrap::setCredentials(const char *tenant, const char *username, const char *password)
--- a/DeviceBootstrap.h	Thu Oct 30 14:46:22 2014 +0000
+++ b/DeviceBootstrap.h	Sun Nov 30 19:34:49 2014 +0000
@@ -2,8 +2,8 @@
 #define DEVICEBOOTSTRAP_H
 
 #include <stddef.h>
-#include "MDM.h"
 #include "DeviceIO.h"
+#include "DeviceMemory.h"
 #include "AbstractSmartRest.h"
 #include "DeviceInfo.h"
 
@@ -15,12 +15,10 @@
 
 #define DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH 256
 
-#define CREDENTIALS_FILE "001_CREDENTIALS"
-
 class DeviceBootstrap
 {
 public:
-    DeviceBootstrap(AbstractSmartRest&, MDMSerial&, DeviceIO&, DeviceInfo&);
+    DeviceBootstrap(AbstractSmartRest&, DeviceIO&, DeviceInfo&, DeviceMemory&);
     
     bool setUpCredentials();
     const char * username();
@@ -36,8 +34,8 @@
 
 private:
     AbstractSmartRest& _client;
-    MDMSerial& _mdm;
     DeviceInfo& _deviceInfo;
+    DeviceMemory& _deviceMemory;
     DeviceIO& _io;
     char _username[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH],                    
          _password[DEVICE_BOOTSTRAP_CREDENTIALS_LENGTH];
--- a/MbedAgent.cpp	Thu Oct 30 14:46:22 2014 +0000
+++ b/MbedAgent.cpp	Sun Nov 30 19:34:49 2014 +0000
@@ -1,12 +1,13 @@
 #include "MbedAgent.h"
 #include "rtos.h"
 
-MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo) :
+MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
     _io(io),
     _mdm(mdm),
     _deviceInfo(deviceInfo),
+    _deviceMemory(deviceMemory),
     _client(MBED_AGENT_HOST, MBED_AGENT_PORT, MBED_AGENT_DEVICE_IDENTIFIER),
-    _bootstrap(_client, _mdm, _io, _deviceInfo),
+    _bootstrap(_client, _io, _deviceInfo, _deviceMemory),
     _integration(_client, _tpl, _deviceId, _deviceInfo),
     _signalQualityMeasurement(_client, _tpl, _deviceId, _deviceInfo),
     _temperatureMeasurement(_client, _tpl, _deviceId, _io.temperatureSensor()),
--- a/MbedAgent.h	Thu Oct 30 14:46:22 2014 +0000
+++ b/MbedAgent.h	Sun Nov 30 19:34:49 2014 +0000
@@ -7,6 +7,7 @@
 #include "RtosSmartRest.h"
 #include "SmartRestTemplate.h"
 #include "DeviceInfo.h"
+#include "DeviceMemory.h"
 #include "DeviceBootstrap.h"
 #include "DeviceIntegration.h"
 #include "SignalQualityMeasurement.h"
@@ -24,7 +25,7 @@
 class MbedAgent
 {
 public:
-    MbedAgent(DeviceIO&, MDMSerial&, DeviceInfo&);
+    MbedAgent(DeviceIO&, MDMSerial&, DeviceInfo&, DeviceMemory&);
     
     bool init();
     bool run();
@@ -36,6 +37,7 @@
     DeviceIO& _io;
     MDMSerial& _mdm;
     DeviceInfo& _deviceInfo;
+    DeviceMemory& _deviceMemory;
     RtosSmartRest _client;
     SmartRestTemplate _tpl;
     DeviceBootstrap _bootstrap;
--- a/MbedSmartRest.lib	Thu Oct 30 14:46:22 2014 +0000
+++ b/MbedSmartRest.lib	Sun Nov 30 19:34:49 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/Cumulocity/code/MbedSmartRest/#97077cfa13fc
+http://mbed.org/users/Cumulocity/code/MbedSmartRest/#aba98ad2ac1b
--- a/io/DeviceFeedback.cpp	Thu Oct 30 14:46:22 2014 +0000
+++ b/io/DeviceFeedback.cpp	Sun Nov 30 19:34:49 2014 +0000
@@ -2,8 +2,10 @@
 #include <stdlib.h>
 #include <string.h>
 
-#define MSG_CLOSE_RELAY 1
-#define MSG_OPEN_RELAY 2
+#define MSG_BEEP_SUCCESS 1
+#define MSG_BEEP_FAILURE 2
+#define MSG_CLOSE_RELAY 3
+#define MSG_OPEN_RELAY 4
 
 DeviceFeedback::DeviceFeedback(PwmOut speaker) :
     _speaker(speaker),
@@ -11,22 +13,33 @@
 {
 }
 
+void DeviceFeedback::beepSuccess()
+{
+    sendMessage(MSG_BEEP_SUCCESS);
+}
+
+void DeviceFeedback::beepFailure()
+{
+    sendMessage(MSG_BEEP_FAILURE);
+}
+
 void DeviceFeedback::closeRelay()
 {
-    uint8_t *msg;
-    
-    msg = _mail.alloc();
-    *msg = MSG_CLOSE_RELAY;
-    _mail.put(msg);
+    sendMessage(MSG_CLOSE_RELAY);
 }
 
 void DeviceFeedback::openRelay()
 {
-    uint8_t *msg;
+    sendMessage(MSG_OPEN_RELAY);
+}
+
+void DeviceFeedback::sendMessage(uint8_t msg)
+{
+    uint8_t *msgPtr;
     
-    msg = _mail.alloc();
-    *msg = MSG_OPEN_RELAY;
-    _mail.put(msg);
+    msgPtr = _mail.alloc();
+    *msgPtr = msg;
+    _mail.put(msgPtr);
 }
 
 void DeviceFeedback::thread()
@@ -38,6 +51,24 @@
         if ((evt = _mail.get(1000)).status == osEventMail) {
             msg = (uint8_t*)evt.value.p;
             switch (*msg) {
+            case MSG_BEEP_SUCCESS:
+                for (float i=2000.0; i<10000.0; i+=2000.0) {
+                    _speaker.period(1.0/i);
+                    _speaker = 0.5;
+                    Thread::wait(200);
+                    _speaker = 0.0;
+                    Thread::wait(50);
+                }
+                break;
+            case MSG_BEEP_FAILURE:
+                for (float i=10000.0; i>2000.0; i-=2000.0) {
+                    _speaker.period(1.0/i);
+                    _speaker = 0.5;
+                    Thread::wait(200);
+                    _speaker = 0.0;
+                    Thread::wait(50);
+                }
+                break;
             case MSG_CLOSE_RELAY:
                 if (!relayState) {
                     relayState = true;
--- a/io/DeviceFeedback.h	Thu Oct 30 14:46:22 2014 +0000
+++ b/io/DeviceFeedback.h	Sun Nov 30 19:34:49 2014 +0000
@@ -10,10 +10,13 @@
 public:
     DeviceFeedback(PwmOut speaker);
     
+    void beepSuccess();
+    void beepFailure();
     void closeRelay();
     void openRelay();
     
 protected:
+    void sendMessage(uint8_t);
     void thread();
     static void thread_func(void const*);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/io/DeviceMemory.cpp	Sun Nov 30 19:34:49 2014 +0000
@@ -0,0 +1,39 @@
+#include "DeviceMemory.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#define PLATFORM_CREDENTIALS_FILE "001_CREDENTIALS"
+
+DeviceMemory::DeviceMemory(MDMSerial& mdm) :
+    _mdm(mdm)
+{
+}
+
+bool DeviceMemory::loadPlatformCredentials(char *username, char *password, size_t len)
+{
+    char buffer[len*2+3]; int res, len2;
+
+    if ((res = _mdm.readFile(PLATFORM_CREDENTIALS_FILE, buffer, sizeof(buffer))) < 0)
+        return false;
+        
+    buffer[(size_t)res] = '\0';
+    sscanf(buffer, "%s\n%s\n%n", username, password, &len2);
+    return res == len2;
+}
+
+bool DeviceMemory::savePlatformCredentials(char *username, char *password, size_t len)
+{
+    char buffer[len*2+3]; int res;
+    
+    res = snprintf(buffer, sizeof(buffer), "%s\n%s\n", username, password);
+    if ((res < 0) || (res >= sizeof(buffer)))
+        return false;
+
+    return res == _mdm.writeFile(PLATFORM_CREDENTIALS_FILE, buffer, res);
+}
+
+bool DeviceMemory::resetPlatformCredentials()
+{
+    return _mdm.delFile(PLATFORM_CREDENTIALS_FILE);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/io/DeviceMemory.h	Sun Nov 30 19:34:49 2014 +0000
@@ -0,0 +1,28 @@
+#ifndef DEVICEMEMORY_H
+#define DEVICEMEMORY_H
+
+#include <stddef.h>
+#include "MDM.h"
+
+/**
+ * Device Memory storage handler
+ */
+class DeviceMemory
+{
+public:
+    DeviceMemory(MDMSerial&);
+    
+    /** loads credentials from persistent memory */
+    bool loadPlatformCredentials(char*, char*, size_t);
+    
+    /** saves credentials to persistent memory */
+    bool savePlatformCredentials(char*, char*, size_t);
+    
+    /** removes credentials from persistent memory */
+    bool resetPlatformCredentials();
+    
+private:
+    MDMSerial& _mdm;
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Thu Oct 30 14:46:22 2014 +0000
+++ b/main.cpp	Sun Nov 30 19:34:49 2014 +0000
@@ -3,9 +3,12 @@
 #include "MDM.h"
 #include "GPS.h"
 #include "DeviceInfo.h"
+#include "DeviceMemory.h"
 #include "MbedAgent.h"
 #include "GPSTracker.h"
 
+#include <stdio.h>
+
 /**
  * SIM PIN. Null for no pin.
  */
@@ -27,7 +30,7 @@
     MDMRtos<MDMSerial> mdm;
     GPSI2C gps;
     
-    //mdm.setDebug(4);
+    mdm.setDebug(4);
 
     if (!mdm.init(SIM_PIN, &devStatus))
         status = 1;
@@ -50,19 +53,6 @@
     
     io.lcdPrint("DEVICE INIT");
     
-    /*if (io.resetButtonPressed()) {
-        puts("Resetting program.");
-        res = mdm.delFile("001_CREDENTIALS");
-        if (res < 0) {
-            puts("Credential reset failed.");
-            io.lcdPrint("CREDENTIAL RESET", "FAILURE", "PLEASE RESTART DEVICE");
-        } else {
-            puts("Credential reset successful.");
-            io.lcdPrint("CREDENTIAL RESET", "SUCCESS", "PLEASE RESTART DEVICE");
-        }
-        return 0;
-    }*/
-
     io.lcdPrint("REGISTER NETWORK", "IMEI", devStatus.imei);
 
     if (!mdm.registerNet()) {
@@ -83,7 +73,18 @@
     {
         uint8_t tries;
         DeviceInfo deviceInfo(mdm, devStatus);
-        MbedAgent agent(io, mdm, deviceInfo);
+        DeviceMemory deviceMemory(mdm);
+        
+        if (io.resetButtonPressed()) {
+            if (deviceMemory.resetPlatformCredentials())
+                io.deviceFeedback().beepSuccess();
+            else
+                io.deviceFeedback().beepFailure();
+            Thread::wait(1000);
+            return;
+        }
+
+        MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
     
         io.lcdPrint("AGENT INIT");
         if (!agent.init()) {
--- a/operation/OperationSupport.cpp	Thu Oct 30 14:46:22 2014 +0000
+++ b/operation/OperationSupport.cpp	Sun Nov 30 19:34:49 2014 +0000
@@ -16,9 +16,11 @@
     _deviceId(deviceId),
     _executor(client, tpl, deviceId, io),
     _thread1(OperationSupport::thread1_func, this),
-    _thread2(OperationSupport::thread2_func, this)
+    _thread2(OperationSupport::thread2_func, this),
+    _thread3(OperationSupport::thread3_func, this)
 {
     _init = false;
+    _firstRun = true;
 }
 
 bool OperationSupport::init()
@@ -55,7 +57,9 @@
 
 bool OperationSupport::run()
 {
-    if (!_store.hasPending()) {
+    if (_firstRun) {
+        _firstRun = false;
+        return true;
         if (!requestPendingOperations())
             return false;
     }
@@ -222,6 +226,40 @@
     }
 }
 
+void OperationSupport::thread3()
+{
+    char bayeuxId[33];
+
+    ComposedRecord record;
+    ParsedRecord received;
+    
+    while ((!_init) || (_firstRun))
+        Thread::yield();
+    
+    // request Bayeux ID
+    {
+        IntegerValue msgId(80);
+        if (!record.add(msgId))
+            return;
+        
+        if ((_client.stream("/devicecontrol/notifications", record) != SMARTREST_SUCCESS)) {
+            puts("Sending stream failed.");
+            _client.stop();
+            return;
+        }
+        if ((_client.receive(received) != SMARTREST_SUCCESS)) {
+            puts("Receiving stream failed.");
+            _client.stop();
+        }
+        _client.stop();
+        
+        puts(received.value(0).characterValue());
+    }
+
+    while (true) {
+    }
+}
+
 void OperationSupport::thread1_func(void const *arg)
 {
     OperationSupport *that;
@@ -235,3 +273,10 @@
     that = (OperationSupport*)arg;
     that->thread2();
 }
+
+void OperationSupport::thread3_func(void const *arg)
+{
+    OperationSupport *that;
+    that = (OperationSupport*)arg;
+    that->thread3();
+}
--- a/operation/OperationSupport.h	Thu Oct 30 14:46:22 2014 +0000
+++ b/operation/OperationSupport.h	Sun Nov 30 19:34:49 2014 +0000
@@ -27,18 +27,20 @@
 
     void thread1();
     void thread2();
+    void thread3();
 
     static void thread1_func(void const*);
     static void thread2_func(void const*);
+    static void thread3_func(void const*);
 
 private:
-    bool _init;
+    bool _init, _firstRun;
     long& _deviceId;
     SmartRestTemplate& _tpl;
     AbstractSmartRest& _client;
     OperationStore _store;
     OperationExecutor _executor;
-    Thread _thread1, _thread2;
+    Thread _thread1, _thread2, _thread3;
 };
 
 extern CharValue aOperationStatePending;
--- a/util/RtosSmartRest.cpp	Thu Oct 30 14:46:22 2014 +0000
+++ b/util/RtosSmartRest.cpp	Sun Nov 30 19:34:49 2014 +0000
@@ -59,6 +59,16 @@
     return inst->send(generator, overrideIdentifier);
 }
 
+uint8_t RtosSmartRest::stream(const char *uri, const Record& record, const char *overrideIdentifier)
+{
+    MbedSmartRest *inst;
+    
+    if ((inst = instance()) == NULL)
+        return SMARTREST_INTERNAL_ERROR;
+
+    return inst->stream(uri, record, overrideIdentifier);
+}
+
 uint8_t RtosSmartRest::receive(ParsedRecord& record)
 {
     MbedSmartRest *inst;
--- a/util/RtosSmartRest.h	Thu Oct 30 14:46:22 2014 +0000
+++ b/util/RtosSmartRest.h	Sun Nov 30 19:34:49 2014 +0000
@@ -21,6 +21,7 @@
     uint8_t setAuthorization(const char*, const char*);
     uint8_t bootstrap(const DataGenerator&);
     uint8_t send(const DataGenerator&, const char* = NULL);
+    uint8_t stream(const char*, const Record&, const char* = NULL);
     uint8_t receive(ParsedRecord&);
     void stop();
     const char * getIdentifier();