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

Revision:
70:f489ca11f254
Parent:
68:0dc778a16d0d
Child:
77:f6717e4eccc4
--- a/operation/OperationSupport.cpp	Sat Dec 13 16:07:07 2014 +0000
+++ b/operation/OperationSupport.cpp	Tue Feb 10 20:52:13 2015 +0000
@@ -59,11 +59,9 @@
 {
     if (_firstRun) {
         _firstRun = false;
-        //TODO: only request pending until push operation works
+        if (!requestPendingOperations())
+            return false;
     }
-
-    if (!requestPendingOperations())
-        return false;
         
     return true;
 }
@@ -89,8 +87,7 @@
     while ((ret = _client.receive(received)) == SMARTREST_SUCCESS) {
         if (!operationFromRecord(received, op))
             continue;
-        if (!_store.enqueue(op))
-            continue;
+        _store.enqueue(op);
     }
     _client.stop();
 
@@ -108,7 +105,8 @@
 
     if ((received.values() < 4) ||
         (received.value(0).valueType() != VALUE_INTEGER) ||
-        (received.value(0).integerValue() != 210) ||
+//TODO: error here since streaming op does not have 210 msg id
+//        (received.value(0).integerValue() != 210) ||
         (received.value(2).valueType() != VALUE_INTEGER) ||
         (received.value(3).valueType() != VALUE_CHARACTER))
         return false;
@@ -121,8 +119,10 @@
         op.state = OPERATION_SUCCESSFUL;
     else if (strcmp(tmp, "FAILED") == 0)
         op.state = OPERATION_FAILED;
+    else if (strcmp(tmp, "PENDING") == 0)
+        op.state = OPERATION_PENDING;
     else
-        op.state = OPERATION_PENDING;
+        return false;
     
     return true;
 }
@@ -229,36 +229,86 @@
 
 void OperationSupport::thread3()
 {
-/*    char bayeuxId[33];
+    char bayeuxId[33];
 
     ComposedRecord record;
     ParsedRecord received;
+    OperationStore::Operation op;
     
     while ((!_init) || (_firstRun))
         Thread::yield();
     
     // request Bayeux ID
     {
+        const char *str;
+
         IntegerValue msgId(80);
         if (!record.add(msgId))
             return;
         
-        if ((_client.stream("/devicecontrol/notifications", record) != SMARTREST_SUCCESS)) {
-            puts("Sending stream failed.");
+        if ((_client.stream("/devicecontrol/notifications", record) != SMARTREST_SUCCESS) ||
+            (_client.receive(received) != SMARTREST_SUCCESS)) {
             _client.stop();
             return;
         }
-        if ((_client.receive(received) != SMARTREST_SUCCESS)) {
-            puts("Receiving stream failed.");
+
+        _client.stop();
+
+        str = received.value(0).characterValue();
+        if ((str == NULL) || (strlen(str) > sizeof(bayeuxId)))
+            return;
+
+        strcpy(bayeuxId, str);
+        record.clear();
+    }
+    
+    // set channel
+    {
+        char chn[16]; int len;
+        
+        len = 0;
+        snprintf(chn, sizeof(chn), "/%ld%n", _deviceId, &len);
+        if ((len == 0) || (len == sizeof(chn)))
+            return;
+        
+        IntegerValue msgId(81);
+        CharValue bid(bayeuxId);
+        CharValue channel(chn);
+        if ((!record.add(msgId)) || (!record.add(bid)) || (!record.add(channel)))
+            return;
+        
+        if ((_client.stream("/devicecontrol/notifications", record) != SMARTREST_SUCCESS) ||
+            (_client.receive(received) != SMARTREST_END_OF_RESPONSE)) {
             _client.stop();
+            return;
         }
+
         _client.stop();
-        
-        puts(received.value(0).characterValue());
+        record.clear();
+    }
+    
+    {
+        IntegerValue msgId(83);
+        CharValue bid(bayeuxId);
+        if ((!record.add(msgId)) || (!record.add(bid)))
+            return;
     }
 
     while (true) {
-    }*/
+        if (_client.stream("/devicecontrol/notifications", record) != SMARTREST_SUCCESS) {
+            _client.stop();
+            continue;
+        }
+
+        while (_client.receive(received) == SMARTREST_SUCCESS) {
+            if (!operationFromRecord(received, op))
+                continue;
+            _store.enqueue(op);
+        }
+        
+        //TODO: error checking
+        _client.stop();
+    }
 }
 
 void OperationSupport::thread1_func(void const *arg)