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
Diff: operation/OperationSupport.cpp
- Revision:
- 60:3c822f97fc73
- Parent:
- 59:f96be79feccd
- Child:
- 61:15719dbe8820
--- 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(); +}