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@67:c360a2b2c948, 2014-11-30 (annotated)
- Committer:
- vwochnik
- Date:
- Sun Nov 30 19:34:49 2014 +0000
- Revision:
- 67:c360a2b2c948
- Parent:
- 55:a0f7295ed6b6
- Child:
- 77:f6717e4eccc4
refactor credentials persistence, add factory reset upon fire press button
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 | 67:c360a2b2c948 | 62 | uint8_t RtosSmartRest::stream(const char *uri, const Record& record, const char *overrideIdentifier) |
vwochnik | 67:c360a2b2c948 | 63 | { |
vwochnik | 67:c360a2b2c948 | 64 | MbedSmartRest *inst; |
vwochnik | 67:c360a2b2c948 | 65 | |
vwochnik | 67:c360a2b2c948 | 66 | if ((inst = instance()) == NULL) |
vwochnik | 67:c360a2b2c948 | 67 | return SMARTREST_INTERNAL_ERROR; |
vwochnik | 67:c360a2b2c948 | 68 | |
vwochnik | 67:c360a2b2c948 | 69 | return inst->stream(uri, record, overrideIdentifier); |
vwochnik | 67:c360a2b2c948 | 70 | } |
vwochnik | 67:c360a2b2c948 | 71 | |
vwochnik | 55:a0f7295ed6b6 | 72 | uint8_t RtosSmartRest::receive(ParsedRecord& record) |
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 SMARTREST_INTERNAL_ERROR; |
vwochnik | 55:a0f7295ed6b6 | 78 | |
vwochnik | 55:a0f7295ed6b6 | 79 | return inst->receive(record); |
vwochnik | 55:a0f7295ed6b6 | 80 | } |
vwochnik | 55:a0f7295ed6b6 | 81 | |
vwochnik | 55:a0f7295ed6b6 | 82 | void RtosSmartRest::stop() |
vwochnik | 55:a0f7295ed6b6 | 83 | { |
vwochnik | 55:a0f7295ed6b6 | 84 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 85 | |
vwochnik | 55:a0f7295ed6b6 | 86 | if ((inst = instance()) == NULL) |
vwochnik | 55:a0f7295ed6b6 | 87 | return; |
vwochnik | 55:a0f7295ed6b6 | 88 | |
vwochnik | 55:a0f7295ed6b6 | 89 | inst->stop(); |
vwochnik | 55:a0f7295ed6b6 | 90 | } |
vwochnik | 55:a0f7295ed6b6 | 91 | |
vwochnik | 55:a0f7295ed6b6 | 92 | const char * RtosSmartRest::getIdentifier() |
vwochnik | 55:a0f7295ed6b6 | 93 | { |
vwochnik | 55:a0f7295ed6b6 | 94 | return _identifier; |
vwochnik | 55:a0f7295ed6b6 | 95 | } |
vwochnik | 55:a0f7295ed6b6 | 96 | |
vwochnik | 55:a0f7295ed6b6 | 97 | MbedSmartRest * RtosSmartRest::instance() |
vwochnik | 55:a0f7295ed6b6 | 98 | { |
vwochnik | 55:a0f7295ed6b6 | 99 | size_t i; |
vwochnik | 55:a0f7295ed6b6 | 100 | osThreadId tid; |
vwochnik | 55:a0f7295ed6b6 | 101 | MbedSmartRest *inst; |
vwochnik | 55:a0f7295ed6b6 | 102 | RtosSmartRest::Slot slot; |
vwochnik | 55:a0f7295ed6b6 | 103 | |
vwochnik | 55:a0f7295ed6b6 | 104 | tid = Thread::gettid(); |
vwochnik | 55:a0f7295ed6b6 | 105 | |
vwochnik | 55:a0f7295ed6b6 | 106 | for (i = 0; i < _count; i++) { |
vwochnik | 55:a0f7295ed6b6 | 107 | if (tid == _slots[i].tid) |
vwochnik | 55:a0f7295ed6b6 | 108 | return _slots[i].inst; |
vwochnik | 55:a0f7295ed6b6 | 109 | } |
vwochnik | 55:a0f7295ed6b6 | 110 | |
vwochnik | 55:a0f7295ed6b6 | 111 | if (_count == RTOS_SMARTREST_SLOTS) |
vwochnik | 55:a0f7295ed6b6 | 112 | return NULL; |
vwochnik | 55:a0f7295ed6b6 | 113 | |
vwochnik | 55:a0f7295ed6b6 | 114 | inst = new MbedSmartRest(_host, _port, _identifier, _tries); |
vwochnik | 55:a0f7295ed6b6 | 115 | inst->setAuthorization(_username, _password); |
vwochnik | 55:a0f7295ed6b6 | 116 | |
vwochnik | 55:a0f7295ed6b6 | 117 | slot.tid = tid; |
vwochnik | 55:a0f7295ed6b6 | 118 | slot.inst = inst; |
vwochnik | 55:a0f7295ed6b6 | 119 | |
vwochnik | 55:a0f7295ed6b6 | 120 | _slots[_count++] = slot; |
vwochnik | 55:a0f7295ed6b6 | 121 | return inst; |
vwochnik | 55:a0f7295ed6b6 | 122 | } |