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: util/RtosSmartRest.cpp
- Revision:
- 93:0acd11870c6a
- Parent:
- 92:48069375dffa
--- a/util/RtosSmartRest.cpp Fri Mar 20 14:27:10 2015 +0000 +++ b/util/RtosSmartRest.cpp Mon Apr 13 14:24:58 2015 +0000 @@ -1,13 +1,11 @@ #include "RtosSmartRest.h" +#include "SmartRestConf.h" +#include "logging.h" -RtosSmartRest::RtosSmartRest(const char *host, uint16_t port, const char *identifier, MDMSerial& mdm, uint8_t tries) : - _host(host), - _identifier(identifier), - _port(port), +RtosSmartRest::RtosSmartRest(MDMSerial& mdm, uint8_t tries) : _tries(tries), - _username(NULL), - _password(NULL), _count(0), + slotsLock(), _mdm(mdm) { } @@ -18,104 +16,81 @@ delete _slots[i].inst; } -uint8_t RtosSmartRest::setAuthorization(const char *username, const char *password) +uint8_t RtosSmartRest::bootstrap(const DataGenerator& generator) { - uint8_t ret; - MbedSmartRest *inst; - - if ((inst = instance()) == NULL) + MbedSmartRest *inst = instance(); + if (inst == NULL) return SMARTREST_INTERNAL_ERROR; - - if ((ret = inst->setAuthorization(username, password)) == SMARTREST_SUCCESS) { - _username = username; - _password = password; - } - + + uint8_t ret = inst->bootstrap(generator); + if (ret == SMARTREST_SUCCESS) + setIdentifier(inst->getIdentifier()); return ret; } -uint8_t RtosSmartRest::bootstrap(const DataGenerator& generator) -{ - uint8_t ret; - MbedSmartRest *inst; - - if ((inst = instance()) == NULL) - return SMARTREST_INTERNAL_ERROR; - - if ((ret = inst->bootstrap(generator)) == SMARTREST_SUCCESS) - _identifier = inst->getIdentifier(); - - return ret; -} - uint8_t RtosSmartRest::send(const DataGenerator& generator, const char *overrideIdentifier) { - MbedSmartRest *inst; - - if ((inst = instance()) == NULL) + MbedSmartRest *inst = instance(); + if (inst) + return inst->send(generator, overrideIdentifier); + else return SMARTREST_INTERNAL_ERROR; +} - return inst->send(generator, overrideIdentifier); +uint8_t RtosSmartRest::sendAndClose(const DataGenerator& generator, const char *overrideIdentifier) +{ + MbedSmartRest *inst = instance(); + if (inst) + return inst->sendAndClose(generator, overrideIdentifier); + else + return SMARTREST_INTERNAL_ERROR; } uint8_t RtosSmartRest::stream(const char *uri, const Record& record, const char *overrideIdentifier) { - MbedSmartRest *inst; - - if ((inst = instance()) == NULL) + MbedSmartRest *inst = instance(); + if (inst) + return inst->stream(uri, record, overrideIdentifier); + else return SMARTREST_INTERNAL_ERROR; - return inst->stream(uri, record, overrideIdentifier); } uint8_t RtosSmartRest::receive(ParsedRecord& record) { - MbedSmartRest *inst; - - if ((inst = instance()) == NULL) + MbedSmartRest *inst = instance(); + if (inst) + return inst->receive(record); + else return SMARTREST_INTERNAL_ERROR; - - return inst->receive(record); } void RtosSmartRest::stop() { - MbedSmartRest *inst; - - if ((inst = instance()) == NULL) - return; - - inst->stop(); -} - -const char * RtosSmartRest::getIdentifier() -{ - return _identifier; + MbedSmartRest *inst = instance(); + if (inst) + inst->stop(); } MbedSmartRest * RtosSmartRest::instance() { - size_t i; - osThreadId tid; - MbedSmartRest *inst; - RtosSmartRest::Slot slot; - - tid = Thread::gettid(); - - for (i = 0; i < _count; i++) { - if (tid == _slots[i].tid) + osThreadId tid = Thread::gettid(); + for (size_t i = 0; i < _count; ++i) { + if (tid == _slots[i].tid) { return _slots[i].inst; + } } - + if (_count == RTOS_SMARTREST_SLOTS) return NULL; - - inst = new MbedSmartRest(_host, _port, _identifier, _mdm, _tries); - inst->setAuthorization(_username, _password); + else { + MbedSmartRest *inst = new MbedSmartRest(_mdm, _tries); + slotsLock.lock(); + _slots[_count].tid = tid; + _slots[_count].inst = inst; + ++_count; + slotsLock.unlock(); - slot.tid = tid; - slot.inst = inst; - - _slots[_count++] = slot; - return inst; + return inst; + } }