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
util/RtosSmartRest.cpp@55:a0f7295ed6b6, 2014-10-23 (annotated)
- Committer:
- vwochnik
- Date:
- Thu Oct 23 12:50:07 2014 +0000
- Revision:
- 55:a0f7295ed6b6
- Child:
- 67:c360a2b2c948
thread safe smartrest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vwochnik | 55:a0f7295ed6b6 | 1 | #include "RtosSmartRest.h" |
vwochnik | 55:a0f7295ed6b6 | 2 | |
vwochnik | 55:a0f7295ed6b6 | 3 | RtosSmartRest::RtosSmartRest(const char *host, uint16_t port, const char *identifier, uint8_t tries) : |
vwochnik | 55:a0f7295ed6b6 | 4 | _host(host), |
vwochnik | 55:a0f7295ed6b6 | 5 | _port(port), |
vwochnik | 55:a0f7295ed6b6 | 6 | _identifier(identifier), |
vwochnik | 55:a0f7295ed6b6 | 7 | _tries(tries), |
vwochnik | 55:a0f7295ed6b6 | 8 | _username(NULL), |
vwochnik | 55:a0f7295ed6b6 | 9 | _password(NULL), |
vwochnik | 55:a0f7295ed6b6 | 10 | _count(0) |
vwochnik | 55:a0f7295ed6b6 | 11 | { |
vwochnik | 55:a0f7295ed6b6 | 12 | } |
vwochnik | 55:a0f7295ed6b6 | 13 | |
vwochnik | 55:a0f7295ed6b6 | 14 | RtosSmartRest::~RtosSmartRest() |
vwochnik | 55:a0f7295ed6b6 | 15 | { |
vwochnik | 55:a0f7295ed6b6 | 16 | size_t i; |
vwochnik | 55:a0f7295ed6b6 | 17 | |
vwochnik | 55:a0f7295ed6b6 | 18 | for (i = 0; i < _count; i++) |
vwochnik | 55:a0f7295ed6b6 | 19 | delete _slots[i].inst; |
vwochnik | 55:a0f7295ed6b6 | 20 | } |
vwochnik | 55:a0f7295ed6b6 | 21 | |
vwochnik | 55:a0f7295ed6b6 | 22 | uint8_t RtosSmartRest::setAuthorization(const char *username, const char *password) |
vwochnik | 55:a0f7295ed6b6 | 23 | { |
vwochnik | 55:a0f7295ed6b6 | 24 | uint8_t ret; |
vwochnik | 55:a0f7295ed6b6 | 25 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 26 | |
vwochnik | 55:a0f7295ed6b6 | 27 | if ((inst = instance()) == NULL) |
vwochnik | 55:a0f7295ed6b6 | 28 | return SMARTREST_INTERNAL_ERROR; |
vwochnik | 55:a0f7295ed6b6 | 29 | |
vwochnik | 55:a0f7295ed6b6 | 30 | if ((ret = inst->setAuthorization(username, password)) == SMARTREST_SUCCESS) { |
vwochnik | 55:a0f7295ed6b6 | 31 | _username = username; |
vwochnik | 55:a0f7295ed6b6 | 32 | _password = password; |
vwochnik | 55:a0f7295ed6b6 | 33 | } |
vwochnik | 55:a0f7295ed6b6 | 34 | |
vwochnik | 55:a0f7295ed6b6 | 35 | return ret; |
vwochnik | 55:a0f7295ed6b6 | 36 | } |
vwochnik | 55:a0f7295ed6b6 | 37 | |
vwochnik | 55:a0f7295ed6b6 | 38 | uint8_t RtosSmartRest::bootstrap(const DataGenerator& generator) |
vwochnik | 55:a0f7295ed6b6 | 39 | { |
vwochnik | 55:a0f7295ed6b6 | 40 | uint8_t ret; |
vwochnik | 55:a0f7295ed6b6 | 41 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 42 | |
vwochnik | 55:a0f7295ed6b6 | 43 | if ((inst = instance()) == NULL) |
vwochnik | 55:a0f7295ed6b6 | 44 | return SMARTREST_INTERNAL_ERROR; |
vwochnik | 55:a0f7295ed6b6 | 45 | |
vwochnik | 55:a0f7295ed6b6 | 46 | if ((ret = inst->bootstrap(generator)) == SMARTREST_SUCCESS) |
vwochnik | 55:a0f7295ed6b6 | 47 | _identifier = inst->getIdentifier(); |
vwochnik | 55:a0f7295ed6b6 | 48 | |
vwochnik | 55:a0f7295ed6b6 | 49 | return ret; |
vwochnik | 55:a0f7295ed6b6 | 50 | } |
vwochnik | 55:a0f7295ed6b6 | 51 | |
vwochnik | 55:a0f7295ed6b6 | 52 | uint8_t RtosSmartRest::send(const DataGenerator& generator, const char *overrideIdentifier) |
vwochnik | 55:a0f7295ed6b6 | 53 | { |
vwochnik | 55:a0f7295ed6b6 | 54 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 55 | |
vwochnik | 55:a0f7295ed6b6 | 56 | if ((inst = instance()) == NULL) |
vwochnik | 55:a0f7295ed6b6 | 57 | return SMARTREST_INTERNAL_ERROR; |
vwochnik | 55:a0f7295ed6b6 | 58 | |
vwochnik | 55:a0f7295ed6b6 | 59 | return inst->send(generator, overrideIdentifier); |
vwochnik | 55:a0f7295ed6b6 | 60 | } |
vwochnik | 55:a0f7295ed6b6 | 61 | |
vwochnik | 55:a0f7295ed6b6 | 62 | uint8_t RtosSmartRest::receive(ParsedRecord& record) |
vwochnik | 55:a0f7295ed6b6 | 63 | { |
vwochnik | 55:a0f7295ed6b6 | 64 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 65 | |
vwochnik | 55:a0f7295ed6b6 | 66 | if ((inst = instance()) == NULL) |
vwochnik | 55:a0f7295ed6b6 | 67 | return SMARTREST_INTERNAL_ERROR; |
vwochnik | 55:a0f7295ed6b6 | 68 | |
vwochnik | 55:a0f7295ed6b6 | 69 | return inst->receive(record); |
vwochnik | 55:a0f7295ed6b6 | 70 | } |
vwochnik | 55:a0f7295ed6b6 | 71 | |
vwochnik | 55:a0f7295ed6b6 | 72 | void RtosSmartRest::stop() |
vwochnik | 55:a0f7295ed6b6 | 73 | { |
vwochnik | 55:a0f7295ed6b6 | 74 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 75 | |
vwochnik | 55:a0f7295ed6b6 | 76 | if ((inst = instance()) == NULL) |
vwochnik | 55:a0f7295ed6b6 | 77 | return; |
vwochnik | 55:a0f7295ed6b6 | 78 | |
vwochnik | 55:a0f7295ed6b6 | 79 | inst->stop(); |
vwochnik | 55:a0f7295ed6b6 | 80 | } |
vwochnik | 55:a0f7295ed6b6 | 81 | |
vwochnik | 55:a0f7295ed6b6 | 82 | const char * RtosSmartRest::getIdentifier() |
vwochnik | 55:a0f7295ed6b6 | 83 | { |
vwochnik | 55:a0f7295ed6b6 | 84 | return _identifier; |
vwochnik | 55:a0f7295ed6b6 | 85 | } |
vwochnik | 55:a0f7295ed6b6 | 86 | |
vwochnik | 55:a0f7295ed6b6 | 87 | MbedSmartRest * RtosSmartRest::instance() |
vwochnik | 55:a0f7295ed6b6 | 88 | { |
vwochnik | 55:a0f7295ed6b6 | 89 | size_t i; |
vwochnik | 55:a0f7295ed6b6 | 90 | osThreadId tid; |
vwochnik | 55:a0f7295ed6b6 | 91 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 92 | RtosSmartRest::Slot slot; |
vwochnik | 55:a0f7295ed6b6 | 93 | |
vwochnik | 55:a0f7295ed6b6 | 94 | tid = Thread::gettid(); |
vwochnik | 55:a0f7295ed6b6 | 95 | |
vwochnik | 55:a0f7295ed6b6 | 96 | for (i = 0; i < _count; i++) { |
vwochnik | 55:a0f7295ed6b6 | 97 | if (tid == _slots[i].tid) |
vwochnik | 55:a0f7295ed6b6 | 98 | return _slots[i].inst; |
vwochnik | 55:a0f7295ed6b6 | 99 | } |
vwochnik | 55:a0f7295ed6b6 | 100 | |
vwochnik | 55:a0f7295ed6b6 | 101 | if (_count == RTOS_SMARTREST_SLOTS) |
vwochnik | 55:a0f7295ed6b6 | 102 | return NULL; |
vwochnik | 55:a0f7295ed6b6 | 103 | |
vwochnik | 55:a0f7295ed6b6 | 104 | inst = new MbedSmartRest(_host, _port, _identifier, _tries); |
vwochnik | 55:a0f7295ed6b6 | 105 | inst->setAuthorization(_username, _password); |
vwochnik | 55:a0f7295ed6b6 | 106 | |
vwochnik | 55:a0f7295ed6b6 | 107 | slot.tid = tid; |
vwochnik | 55:a0f7295ed6b6 | 108 | slot.inst = inst; |
vwochnik | 55:a0f7295ed6b6 | 109 | |
vwochnik | 55:a0f7295ed6b6 | 110 | _slots[_count++] = slot; |
vwochnik | 55:a0f7295ed6b6 | 111 | return inst; |
vwochnik | 55:a0f7295ed6b6 | 112 | } |