Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
SmartRest.cpp@13:aba98ad2ac1b, 2014-11-15 (annotated)
- Committer:
- Cumulocity
- Date:
- Sat Nov 15 12:21:41 2014 +0100
- Revision:
- 13:aba98ad2ac1b
- Parent:
- 11:e1bee9a77652
- Child:
- 18:16192696c106
Updated from revision 0b898f0efc6d
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 0:099f76422485 | 1 | /* |
Cumulocity | 0:099f76422485 | 2 | * SmartRest.cpp |
Cumulocity | 0:099f76422485 | 3 | * |
Cumulocity | 0:099f76422485 | 4 | * Created on: Nov 1, 2013 |
Cumulocity | 0:099f76422485 | 5 | * * Authors: Vincent Wochnik <v.wochnik@gmail.com> |
Cumulocity | 0:099f76422485 | 6 | * |
Cumulocity | 0:099f76422485 | 7 | * Copyright (c) 2013 Cumulocity GmbH |
Cumulocity | 0:099f76422485 | 8 | * |
Cumulocity | 0:099f76422485 | 9 | * Permission is hereby granted, free of charge, to any person obtaining |
Cumulocity | 0:099f76422485 | 10 | * a copy of this software and associated documentation files (the |
Cumulocity | 0:099f76422485 | 11 | * "Software"), to deal in the Software without restriction, including |
Cumulocity | 0:099f76422485 | 12 | * without limitation the rights to use, copy, modify, merge, publish, |
Cumulocity | 0:099f76422485 | 13 | * distribute, sublicense, and/or sell copies of the Software, and to |
Cumulocity | 0:099f76422485 | 14 | * permit persons to whom the Software is furnished to do so, subject to |
Cumulocity | 0:099f76422485 | 15 | * the following conditions: |
Cumulocity | 0:099f76422485 | 16 | * |
Cumulocity | 0:099f76422485 | 17 | * The above copyright notice and this permission notice shall be |
Cumulocity | 0:099f76422485 | 18 | * included in all copies or substantial portions of the Software. |
Cumulocity | 0:099f76422485 | 19 | * |
Cumulocity | 0:099f76422485 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
Cumulocity | 0:099f76422485 | 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Cumulocity | 0:099f76422485 | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Cumulocity | 0:099f76422485 | 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
Cumulocity | 0:099f76422485 | 24 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
Cumulocity | 0:099f76422485 | 25 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
Cumulocity | 0:099f76422485 | 26 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Cumulocity | 0:099f76422485 | 27 | */ |
Cumulocity | 0:099f76422485 | 28 | |
Cumulocity | 0:099f76422485 | 29 | #include "SmartRest.h" |
Cumulocity | 0:099f76422485 | 30 | #include <stdlib.h> |
Cumulocity | 0:099f76422485 | 31 | #include <string.h> |
Cumulocity | 0:099f76422485 | 32 | |
Cumulocity | 11:e1bee9a77652 | 33 | |
Cumulocity | 11:e1bee9a77652 | 34 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 35 | SmartRest::SmartRest(AbstractClient& client, const char *identifier) : |
Cumulocity | 11:e1bee9a77652 | 36 | _client(client), |
Cumulocity | 11:e1bee9a77652 | 37 | _identifier(identifier) |
Cumulocity | 0:099f76422485 | 38 | { |
Cumulocity | 11:e1bee9a77652 | 39 | _source = NULL; |
Cumulocity | 11:e1bee9a77652 | 40 | _mogid[0] = 0; |
Cumulocity | 0:099f76422485 | 41 | } |
Cumulocity | 11:e1bee9a77652 | 42 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 43 | uint8_t SmartRest::setAuthorization(const char *username, const char *password) |
Cumulocity | 2:45a6e44a4fb4 | 44 | { |
Cumulocity | 11:e1bee9a77652 | 45 | uint8_t res; |
Cumulocity | 2:45a6e44a4fb4 | 46 | |
Cumulocity | 11:e1bee9a77652 | 47 | res = _client.setAuthorization(username, password); |
Cumulocity | 11:e1bee9a77652 | 48 | if (res != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 49 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 50 | return SMARTREST_SUCCESS; |
Cumulocity | 2:45a6e44a4fb4 | 51 | } |
Cumulocity | 11:e1bee9a77652 | 52 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 53 | #ifdef SMARTREST_TRANSACTIONAL |
Cumulocity | 11:e1bee9a77652 | 54 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 55 | uint8_t SmartRest::request(const DataGenerator& generator, const char *overrideIdentifier) |
Cumulocity | 5:2b74510900da | 56 | { |
Cumulocity | 11:e1bee9a77652 | 57 | uint8_t res; |
Cumulocity | 5:2b74510900da | 58 | |
Cumulocity | 11:e1bee9a77652 | 59 | res = send(generator, overrideIdentifier); |
Cumulocity | 11:e1bee9a77652 | 60 | stop(); |
Cumulocity | 11:e1bee9a77652 | 61 | return res; |
Cumulocity | 5:2b74510900da | 62 | } |
Cumulocity | 11:e1bee9a77652 | 63 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 64 | uint8_t SmartRest::request(const DataGenerator& generator, Aggregator& aggregator, const char *overrideIdentifier) |
Cumulocity | 5:2b74510900da | 65 | { |
Cumulocity | 11:e1bee9a77652 | 66 | uint8_t res; |
Cumulocity | 5:2b74510900da | 67 | |
Cumulocity | 11:e1bee9a77652 | 68 | if (!aggregator.managed()) |
Cumulocity | 11:e1bee9a77652 | 69 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 5:2b74510900da | 70 | |
Cumulocity | 11:e1bee9a77652 | 71 | res = send(generator, overrideIdentifier); |
Cumulocity | 11:e1bee9a77652 | 72 | if (res != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 73 | { |
Cumulocity | 11:e1bee9a77652 | 74 | stop(); |
Cumulocity | 11:e1bee9a77652 | 75 | return res; |
Cumulocity | 11:e1bee9a77652 | 76 | } |
Cumulocity | 5:2b74510900da | 77 | |
Cumulocity | 11:e1bee9a77652 | 78 | ParsedRecord recvd; |
Cumulocity | 11:e1bee9a77652 | 79 | while ((res = receive(recvd)) == SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 80 | { |
Cumulocity | 11:e1bee9a77652 | 81 | if (!aggregator.add(recvd)) |
Cumulocity | 11:e1bee9a77652 | 82 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 83 | } |
Cumulocity | 5:2b74510900da | 84 | |
Cumulocity | 11:e1bee9a77652 | 85 | stop(); |
Cumulocity | 11:e1bee9a77652 | 86 | if (res == SMARTREST_END_OF_RESPONSE) |
Cumulocity | 11:e1bee9a77652 | 87 | return SMARTREST_SUCCESS; |
Cumulocity | 11:e1bee9a77652 | 88 | return res; |
Cumulocity | 5:2b74510900da | 89 | } |
Cumulocity | 11:e1bee9a77652 | 90 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 11:e1bee9a77652 | 91 | #endif // SMARTREST_TRANSACTIONAL |
Cumulocity | 11:e1bee9a77652 | 92 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 93 | uint8_t SmartRest::bootstrap(const DataGenerator& generator) |
Cumulocity | 5:2b74510900da | 94 | { |
Cumulocity | 11:e1bee9a77652 | 95 | ParsedRecord record; |
Cumulocity | 11:e1bee9a77652 | 96 | int8_t ret; |
Cumulocity | 5:2b74510900da | 97 | |
Cumulocity | 13:aba98ad2ac1b | 98 | ret = beginRequest(NULL, NULL); |
Cumulocity | 11:e1bee9a77652 | 99 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 100 | return ret; |
Cumulocity | 11:e1bee9a77652 | 101 | ret = awaitResponse(); |
Cumulocity | 11:e1bee9a77652 | 102 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 103 | return ret; |
Cumulocity | 11:e1bee9a77652 | 104 | ret = receive(record); |
Cumulocity | 11:e1bee9a77652 | 105 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 106 | return ret; |
Cumulocity | 11:e1bee9a77652 | 107 | if (!record) |
Cumulocity | 11:e1bee9a77652 | 108 | { |
Cumulocity | 11:e1bee9a77652 | 109 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 110 | } |
Cumulocity | 11:e1bee9a77652 | 111 | stop(); |
Cumulocity | 5:2b74510900da | 112 | |
Cumulocity | 11:e1bee9a77652 | 113 | if (setMoGid(record)) |
Cumulocity | 11:e1bee9a77652 | 114 | return SMARTREST_SUCCESS; |
Cumulocity | 5:2b74510900da | 115 | |
Cumulocity | 11:e1bee9a77652 | 116 | if (record.value(0).integerValue() != 40) |
Cumulocity | 11:e1bee9a77652 | 117 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 5:2b74510900da | 118 | |
Cumulocity | 11:e1bee9a77652 | 119 | ret = send(generator, NULL); |
Cumulocity | 11:e1bee9a77652 | 120 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 121 | return ret; |
Cumulocity | 11:e1bee9a77652 | 122 | ret = receive(record); |
Cumulocity | 11:e1bee9a77652 | 123 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 124 | return ret; |
Cumulocity | 11:e1bee9a77652 | 125 | stop(); |
Cumulocity | 5:2b74510900da | 126 | |
Cumulocity | 11:e1bee9a77652 | 127 | if (!setMoGid(record)) |
Cumulocity | 11:e1bee9a77652 | 128 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 5:2b74510900da | 129 | |
Cumulocity | 11:e1bee9a77652 | 130 | return SMARTREST_SUCCESS; |
Cumulocity | 5:2b74510900da | 131 | } |
Cumulocity | 11:e1bee9a77652 | 132 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 133 | uint8_t SmartRest::send(const DataGenerator& generator, const char *overrideIdentifier) |
Cumulocity | 0:099f76422485 | 134 | { |
Cumulocity | 11:e1bee9a77652 | 135 | uint8_t res; |
Cumulocity | 0:099f76422485 | 136 | |
Cumulocity | 13:aba98ad2ac1b | 137 | res = beginRequest(NULL, overrideIdentifier); |
Cumulocity | 11:e1bee9a77652 | 138 | if (res != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 139 | return res; |
Cumulocity | 0:099f76422485 | 140 | |
Cumulocity | 11:e1bee9a77652 | 141 | _client.sendData(generator); |
Cumulocity | 11:e1bee9a77652 | 142 | return awaitResponse(); |
Cumulocity | 0:099f76422485 | 143 | } |
Cumulocity | 11:e1bee9a77652 | 144 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 13:aba98ad2ac1b | 145 | uint8_t SmartRest::stream(const char *uri, const Record& record, const char *overrideIdentifier) |
Cumulocity | 13:aba98ad2ac1b | 146 | { |
Cumulocity | 13:aba98ad2ac1b | 147 | uint8_t res; |
Cumulocity | 13:aba98ad2ac1b | 148 | |
Cumulocity | 13:aba98ad2ac1b | 149 | res = beginRequest(uri, overrideIdentifier); |
Cumulocity | 13:aba98ad2ac1b | 150 | if (res != SMARTREST_SUCCESS) |
Cumulocity | 13:aba98ad2ac1b | 151 | return res; |
Cumulocity | 13:aba98ad2ac1b | 152 | |
Cumulocity | 13:aba98ad2ac1b | 153 | _client.sendData(record); |
Cumulocity | 13:aba98ad2ac1b | 154 | return awaitResponse(); |
Cumulocity | 13:aba98ad2ac1b | 155 | } |
Cumulocity | 13:aba98ad2ac1b | 156 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 157 | uint8_t SmartRest::receive(ParsedRecord& record) |
Cumulocity | 0:099f76422485 | 158 | { |
Cumulocity | 11:e1bee9a77652 | 159 | uint8_t res; |
Cumulocity | 0:099f76422485 | 160 | |
Cumulocity | 11:e1bee9a77652 | 161 | if (_source == NULL) |
Cumulocity | 11:e1bee9a77652 | 162 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 163 | |
Cumulocity | 11:e1bee9a77652 | 164 | res = _parser.readFrom(*_source, record); |
Cumulocity | 0:099f76422485 | 165 | |
Cumulocity | 11:e1bee9a77652 | 166 | switch (res) |
Cumulocity | 11:e1bee9a77652 | 167 | { |
Cumulocity | 11:e1bee9a77652 | 168 | case PARSER_SUCCESS: |
Cumulocity | 11:e1bee9a77652 | 169 | return SMARTREST_SUCCESS; |
Cumulocity | 11:e1bee9a77652 | 170 | case PARSER_END_OF_RESPONSE: |
Cumulocity | 11:e1bee9a77652 | 171 | return SMARTREST_END_OF_RESPONSE; |
Cumulocity | 11:e1bee9a77652 | 172 | case PARSER_TIMEOUT_ERROR: |
Cumulocity | 11:e1bee9a77652 | 173 | return SMARTREST_TIMEOUT_ERROR; |
Cumulocity | 11:e1bee9a77652 | 174 | } |
Cumulocity | 11:e1bee9a77652 | 175 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 0:099f76422485 | 176 | } |
Cumulocity | 11:e1bee9a77652 | 177 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 178 | void SmartRest::stop() |
Cumulocity | 0:099f76422485 | 179 | { |
Cumulocity | 11:e1bee9a77652 | 180 | _source = NULL; |
Cumulocity | 11:e1bee9a77652 | 181 | _client.stop(); |
Cumulocity | 0:099f76422485 | 182 | } |
Cumulocity | 11:e1bee9a77652 | 183 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 7:8159a2d12e4e | 184 | const char * SmartRest::getIdentifier() |
Cumulocity | 7:8159a2d12e4e | 185 | { |
Cumulocity | 11:e1bee9a77652 | 186 | if (*_mogid) |
Cumulocity | 11:e1bee9a77652 | 187 | return _mogid; |
Cumulocity | 11:e1bee9a77652 | 188 | return _identifier; |
Cumulocity | 7:8159a2d12e4e | 189 | } |
Cumulocity | 11:e1bee9a77652 | 190 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 13:aba98ad2ac1b | 191 | uint8_t SmartRest::beginRequest(const char *uri, const char *overrideIdentifier) |
Cumulocity | 0:099f76422485 | 192 | { |
Cumulocity | 11:e1bee9a77652 | 193 | int res; |
Cumulocity | 0:099f76422485 | 194 | |
Cumulocity | 13:aba98ad2ac1b | 195 | if (uri != NULL) |
Cumulocity | 13:aba98ad2ac1b | 196 | { |
Cumulocity | 13:aba98ad2ac1b | 197 | res = _client.beginStream(uri); |
Cumulocity | 13:aba98ad2ac1b | 198 | } |
Cumulocity | 13:aba98ad2ac1b | 199 | else |
Cumulocity | 13:aba98ad2ac1b | 200 | { |
Cumulocity | 13:aba98ad2ac1b | 201 | res = _client.beginRequest(); |
Cumulocity | 13:aba98ad2ac1b | 202 | } |
Cumulocity | 13:aba98ad2ac1b | 203 | |
Cumulocity | 11:e1bee9a77652 | 204 | if (res == CLIENT_CONNECTION_ERROR) |
Cumulocity | 11:e1bee9a77652 | 205 | { |
Cumulocity | 11:e1bee9a77652 | 206 | return SMARTREST_CONNECTION_FAILED; |
Cumulocity | 11:e1bee9a77652 | 207 | } |
Cumulocity | 11:e1bee9a77652 | 208 | else if (res != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 209 | { |
Cumulocity | 11:e1bee9a77652 | 210 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 211 | } |
Cumulocity | 11:e1bee9a77652 | 212 | if (overrideIdentifier != NULL) |
Cumulocity | 11:e1bee9a77652 | 213 | { |
Cumulocity | 11:e1bee9a77652 | 214 | if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 215 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 216 | } |
Cumulocity | 11:e1bee9a77652 | 217 | else |
Cumulocity | 11:e1bee9a77652 | 218 | { |
Cumulocity | 11:e1bee9a77652 | 219 | if (_client.sendIdentifier(getIdentifier()) != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 220 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 221 | } |
Cumulocity | 11:e1bee9a77652 | 222 | return SMARTREST_SUCCESS; |
Cumulocity | 0:099f76422485 | 223 | } |
Cumulocity | 11:e1bee9a77652 | 224 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 225 | uint8_t SmartRest::awaitResponse() |
Cumulocity | 0:099f76422485 | 226 | { |
Cumulocity | 11:e1bee9a77652 | 227 | if ((_client.endRequest() != CLIENT_OK) || |
Cumulocity | 11:e1bee9a77652 | 228 | (_client.awaitResponse() != CLIENT_OK)) |
Cumulocity | 11:e1bee9a77652 | 229 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 230 | _source = &_client.receiveData(); |
Cumulocity | 11:e1bee9a77652 | 231 | return SMARTREST_SUCCESS; |
Cumulocity | 0:099f76422485 | 232 | } |
Cumulocity | 11:e1bee9a77652 | 233 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 234 | bool SmartRest::setMoGid(ParsedRecord& record) |
Cumulocity | 0:099f76422485 | 235 | { |
Cumulocity | 11:e1bee9a77652 | 236 | const char *mogid; |
Cumulocity | 0:099f76422485 | 237 | |
Cumulocity | 11:e1bee9a77652 | 238 | *_mogid = 0; |
Cumulocity | 11:e1bee9a77652 | 239 | if ((record.values() < 2) || (record.value(0).integerValue() != 20)) |
Cumulocity | 11:e1bee9a77652 | 240 | return false; |
Cumulocity | 11:e1bee9a77652 | 241 | |
Cumulocity | 11:e1bee9a77652 | 242 | if ((record.value(1).valueType() != VALUE_INTEGER) || |
Cumulocity | 11:e1bee9a77652 | 243 | (record.value(1).integerValue() <= 0)) |
Cumulocity | 11:e1bee9a77652 | 244 | return false; |
Cumulocity | 0:099f76422485 | 245 | |
Cumulocity | 11:e1bee9a77652 | 246 | mogid = record.rawValue(1); |
Cumulocity | 11:e1bee9a77652 | 247 | if (strlen(mogid)+1 > sizeof(_mogid)) |
Cumulocity | 11:e1bee9a77652 | 248 | return false; |
Cumulocity | 0:099f76422485 | 249 | |
Cumulocity | 11:e1bee9a77652 | 250 | strcpy(_mogid, mogid); |
Cumulocity | 0:099f76422485 | 251 | |
Cumulocity | 11:e1bee9a77652 | 252 | return true; |
Cumulocity | 0:099f76422485 | 253 | } |
Cumulocity | 11:e1bee9a77652 | 254 | /*-------------------------------------------------------------------------*/ |