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:
Fri Oct 24 15:00:36 2014 +0000
Parent:
59:f96be79feccd
Child:
61:15719dbe8820
Commit message:
operation thread + funcs

Changed in this revision

DeviceBootstrap.cpp Show annotated file Show diff for this revision Revisions of this file
GPSTracker.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
operation/OperationStore.h 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
--- a/DeviceBootstrap.cpp	Fri Oct 24 11:30:43 2014 +0000
+++ b/DeviceBootstrap.cpp	Fri Oct 24 15:00:36 2014 +0000
@@ -19,6 +19,9 @@
 
 bool DeviceBootstrap::setUpCredentials()
 {
+    strcpy(_username, "vaillant/admin");
+    strcpy(_password, "klanpi");
+
     if (((*_username == '\0') || (*_password == '\0')) &&
         (!obtainFromStorage())) {
         if (!obtainFromPlatform())
--- a/GPSTracker.cpp	Fri Oct 24 11:30:43 2014 +0000
+++ b/GPSTracker.cpp	Fri Oct 24 15:00:36 2014 +0000
@@ -25,7 +25,7 @@
     
     return result;
 }
-    
+
 void GPSTracker::thread()
 {
     char buf[256], chr; // needs to be that big otherwise mdm isn't working
--- a/MbedAgent.cpp	Fri Oct 24 11:30:43 2014 +0000
+++ b/MbedAgent.cpp	Fri Oct 24 15:00:36 2014 +0000
@@ -62,14 +62,14 @@
     while (true) {
         timer.reset();
         
-        _signalQualityMeasurement.run();
+        /*_signalQualityMeasurement.run();
         _temperatureMeasurement.run();
         _accelerationMeasurement.run();
         _analogMeasurement.run();
-        _locationUpdate.run();
+        _locationUpdate.run();*/
         _operationSupport.run();
         
-        while (timer.read() < MBED_AGENT_INTERVAL) {
+        while (timer.read() < 10 /*MBED_AGENT_INTERVAL*/) {
             Thread::yield();
         }
     }
--- a/operation/OperationStore.h	Fri Oct 24 11:30:43 2014 +0000
+++ b/operation/OperationStore.h	Fri Oct 24 15:00:36 2014 +0000
@@ -10,7 +10,7 @@
 {
 public:
     struct Operation {
-        unsigned long identifier;
+        long identifier;
         const char *description;
         uint8_t state;
     };
--- a/operation/OperationSupport.cpp	Fri Oct 24 11:30:43 2014 +0000
+++ b/operation/OperationSupport.cpp	Fri Oct 24 15:00:36 2014 +0000
@@ -3,6 +3,7 @@
 #include "ComposedRecord.h"
 #include "CharValue.h"
 #include "IntegerValue.h"
+#include <stdio.h>
 
 CharValue aOperationStatePending("PENDING");
 CharValue aOperationStateExecuting("EXECUTING");
@@ -12,7 +13,8 @@
 OperationSupport::OperationSupport(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId) :
     _client(client),
     _tpl(tpl),
-    _deviceId(deviceId)
+    _deviceId(deviceId),
+    _thread(OperationSupport::thread_func, this)
 {
     _init = false;
 }
@@ -48,6 +50,10 @@
 
 bool OperationSupport::run()
 {
+    uint8_t ret;
+    OperationStore::Operation ops[100];
+    size_t nops;
+
     ComposedRecord record;
     ParsedRecord received;
     
@@ -55,29 +61,49 @@
     IntegerValue deviceId(_deviceId);
     if ((!record.add(msgId)) || (!record.add(deviceId)))
         return false;
-        
+
+    puts("Operation support.");        
     if (_client.send(record) != SMARTREST_SUCCESS) {
         _client.stop();
         return false;
     }
+    
+    nops = 0;
+    while ((ret = _client.receive(received)) == SMARTREST_SUCCESS) {
+        puts("Received operation.");
+        if (!operationFromRecord(received, ops[nops++]))
+            puts("Operation conversion failed.");
+    }
     _client.stop();
+
+    if ((ret != SMARTREST_END_OF_RESPONSE) &&
+        (ret != SMARTREST_CONNECTION_CLOSED)) {
+        puts("Failed receive.");
+        return false;
+    }
     
+    for (size_t i = 0; i < nops; i++) {
+        ops[i].state = STATE_SUCCESSFUL;
+        if (!updateOperation(ops[i]))
+            puts("Operation update failed.");
+    }
+
+    return true;
 }
 
-bool operationFromRecord(ParsedRecord& received, OperationStore::Operation& op)
+bool OperationSupport::operationFromRecord(ParsedRecord& received, OperationStore::Operation& op)
 {
     const char *tmp;
 
     if ((received.values() < 4) ||
         (received.value(0).valueType() != VALUE_INTEGER) ||
-        (received.value(0).integerValue() != 211) ||
+        (received.value(0).integerValue() != 210) ||
         (received.value(2).valueType() != VALUE_INTEGER) ||
         (received.value(3).valueType() != VALUE_CHARACTER))
         return false;
     
+    op.identifier = received.value(2).integerValue();
     tmp = received.value(3).characterValue();
-    op.identifier = received.value(2).integerValue();
-
     if (strcmp(tmp, "EXECUTING") == 0)
         op.state = STATE_EXECUTING;
     else if (strcmp(tmp, "SUCCESSFUL") == 0)
@@ -130,3 +156,18 @@
         return aOperationStatePending;
     }
 }
+
+void OperationSupport::thread()
+{
+    while (!_init)
+        Thread::yield();
+    
+    
+}
+
+void OperationSupport::thread_func(void const *arg)
+{
+    OperationSupport *that;
+    that = (OperationSupport*)arg;
+    that->thread();
+}
--- a/operation/OperationSupport.h	Fri Oct 24 11:30:43 2014 +0000
+++ b/operation/OperationSupport.h	Fri Oct 24 15:00:36 2014 +0000
@@ -1,6 +1,7 @@
 #ifndef OPERATIONSUPPORT_H
 #define OPERATIONSUPPORT_H
 
+#include "rtos.h"
 #include "AbstractSmartRest.h"
 #include "SmartRestTemplate.h"
 #include "ComposedRecord.h"
@@ -21,11 +22,15 @@
     bool updateOperation(OperationStore::Operation&);
     CharValue& operationStateValue(OperationStore::Operation&);
 
+    void thread();
+    static void thread_func(void const*);
+
 private:
     bool _init;
     long& _deviceId;
     SmartRestTemplate& _tpl;
     AbstractSmartRest& _client;
+    Thread _thread;
 };
 
 extern CharValue aOperationStatePending;