Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
SmartRest.cpp@18:16192696c106, 2015-03-06 (annotated)
- Committer:
- xinlei
- Date:
- Fri Mar 06 10:37:09 2015 +0000
- Revision:
- 18:16192696c106
- Parent:
- 13:aba98ad2ac1b
- Child:
- 20:505d29d5bdfc
bugfix: workaround for nasty connection loss after 6 minutes
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 <stdlib.h> |
Cumulocity | 0:099f76422485 | 30 | #include <string.h> |
xinlei | 18:16192696c106 | 31 | #include <stdio.h> |
xinlei | 18:16192696c106 | 32 | #include "SmartRest.h" |
Cumulocity | 0:099f76422485 | 33 | |
Cumulocity | 11:e1bee9a77652 | 34 | |
Cumulocity | 11:e1bee9a77652 | 35 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 36 | SmartRest::SmartRest(AbstractClient& client, const char *identifier) : |
Cumulocity | 11:e1bee9a77652 | 37 | _client(client), |
Cumulocity | 11:e1bee9a77652 | 38 | _identifier(identifier) |
Cumulocity | 0:099f76422485 | 39 | { |
Cumulocity | 11:e1bee9a77652 | 40 | _source = NULL; |
Cumulocity | 11:e1bee9a77652 | 41 | _mogid[0] = 0; |
Cumulocity | 0:099f76422485 | 42 | } |
Cumulocity | 11:e1bee9a77652 | 43 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 44 | uint8_t SmartRest::setAuthorization(const char *username, const char *password) |
Cumulocity | 2:45a6e44a4fb4 | 45 | { |
Cumulocity | 11:e1bee9a77652 | 46 | uint8_t res; |
Cumulocity | 2:45a6e44a4fb4 | 47 | |
Cumulocity | 11:e1bee9a77652 | 48 | res = _client.setAuthorization(username, password); |
Cumulocity | 11:e1bee9a77652 | 49 | if (res != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 50 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 51 | return SMARTREST_SUCCESS; |
Cumulocity | 2:45a6e44a4fb4 | 52 | } |
Cumulocity | 11:e1bee9a77652 | 53 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 54 | #ifdef SMARTREST_TRANSACTIONAL |
Cumulocity | 11:e1bee9a77652 | 55 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 56 | uint8_t SmartRest::request(const DataGenerator& generator, const char *overrideIdentifier) |
Cumulocity | 5:2b74510900da | 57 | { |
Cumulocity | 11:e1bee9a77652 | 58 | uint8_t res; |
Cumulocity | 5:2b74510900da | 59 | |
Cumulocity | 11:e1bee9a77652 | 60 | res = send(generator, overrideIdentifier); |
Cumulocity | 11:e1bee9a77652 | 61 | stop(); |
Cumulocity | 11:e1bee9a77652 | 62 | return res; |
Cumulocity | 5:2b74510900da | 63 | } |
Cumulocity | 11:e1bee9a77652 | 64 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 65 | uint8_t SmartRest::request(const DataGenerator& generator, Aggregator& aggregator, const char *overrideIdentifier) |
Cumulocity | 5:2b74510900da | 66 | { |
Cumulocity | 11:e1bee9a77652 | 67 | uint8_t res; |
Cumulocity | 5:2b74510900da | 68 | |
Cumulocity | 11:e1bee9a77652 | 69 | if (!aggregator.managed()) |
Cumulocity | 11:e1bee9a77652 | 70 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 5:2b74510900da | 71 | |
Cumulocity | 11:e1bee9a77652 | 72 | res = send(generator, overrideIdentifier); |
Cumulocity | 11:e1bee9a77652 | 73 | if (res != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 74 | { |
Cumulocity | 11:e1bee9a77652 | 75 | stop(); |
Cumulocity | 11:e1bee9a77652 | 76 | return res; |
Cumulocity | 11:e1bee9a77652 | 77 | } |
Cumulocity | 5:2b74510900da | 78 | |
Cumulocity | 11:e1bee9a77652 | 79 | ParsedRecord recvd; |
Cumulocity | 11:e1bee9a77652 | 80 | while ((res = receive(recvd)) == SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 81 | { |
Cumulocity | 11:e1bee9a77652 | 82 | if (!aggregator.add(recvd)) |
Cumulocity | 11:e1bee9a77652 | 83 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 84 | } |
Cumulocity | 5:2b74510900da | 85 | |
Cumulocity | 11:e1bee9a77652 | 86 | stop(); |
Cumulocity | 11:e1bee9a77652 | 87 | if (res == SMARTREST_END_OF_RESPONSE) |
Cumulocity | 11:e1bee9a77652 | 88 | return SMARTREST_SUCCESS; |
Cumulocity | 11:e1bee9a77652 | 89 | return res; |
Cumulocity | 5:2b74510900da | 90 | } |
Cumulocity | 11:e1bee9a77652 | 91 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 11:e1bee9a77652 | 92 | #endif // SMARTREST_TRANSACTIONAL |
Cumulocity | 11:e1bee9a77652 | 93 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 5:2b74510900da | 94 | uint8_t SmartRest::bootstrap(const DataGenerator& generator) |
Cumulocity | 5:2b74510900da | 95 | { |
Cumulocity | 11:e1bee9a77652 | 96 | ParsedRecord record; |
Cumulocity | 11:e1bee9a77652 | 97 | int8_t ret; |
Cumulocity | 5:2b74510900da | 98 | |
Cumulocity | 13:aba98ad2ac1b | 99 | ret = beginRequest(NULL, NULL); |
Cumulocity | 11:e1bee9a77652 | 100 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 101 | return ret; |
Cumulocity | 11:e1bee9a77652 | 102 | ret = awaitResponse(); |
Cumulocity | 11:e1bee9a77652 | 103 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 104 | return ret; |
Cumulocity | 11:e1bee9a77652 | 105 | ret = receive(record); |
Cumulocity | 11:e1bee9a77652 | 106 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 107 | return ret; |
Cumulocity | 11:e1bee9a77652 | 108 | if (!record) |
Cumulocity | 11:e1bee9a77652 | 109 | { |
Cumulocity | 11:e1bee9a77652 | 110 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 111 | } |
Cumulocity | 11:e1bee9a77652 | 112 | stop(); |
Cumulocity | 5:2b74510900da | 113 | |
Cumulocity | 11:e1bee9a77652 | 114 | if (setMoGid(record)) |
Cumulocity | 11:e1bee9a77652 | 115 | return SMARTREST_SUCCESS; |
Cumulocity | 5:2b74510900da | 116 | |
Cumulocity | 11:e1bee9a77652 | 117 | if (record.value(0).integerValue() != 40) |
Cumulocity | 11:e1bee9a77652 | 118 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 5:2b74510900da | 119 | |
Cumulocity | 11:e1bee9a77652 | 120 | ret = send(generator, NULL); |
Cumulocity | 11:e1bee9a77652 | 121 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 122 | return ret; |
Cumulocity | 11:e1bee9a77652 | 123 | ret = receive(record); |
Cumulocity | 11:e1bee9a77652 | 124 | if (ret != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 125 | return ret; |
Cumulocity | 11:e1bee9a77652 | 126 | stop(); |
Cumulocity | 5:2b74510900da | 127 | |
Cumulocity | 11:e1bee9a77652 | 128 | if (!setMoGid(record)) |
Cumulocity | 11:e1bee9a77652 | 129 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 5:2b74510900da | 130 | |
Cumulocity | 11:e1bee9a77652 | 131 | return SMARTREST_SUCCESS; |
Cumulocity | 5:2b74510900da | 132 | } |
Cumulocity | 11:e1bee9a77652 | 133 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 134 | uint8_t SmartRest::send(const DataGenerator& generator, const char *overrideIdentifier) |
Cumulocity | 0:099f76422485 | 135 | { |
Cumulocity | 11:e1bee9a77652 | 136 | uint8_t res; |
Cumulocity | 0:099f76422485 | 137 | |
Cumulocity | 13:aba98ad2ac1b | 138 | res = beginRequest(NULL, overrideIdentifier); |
Cumulocity | 11:e1bee9a77652 | 139 | if (res != SMARTREST_SUCCESS) |
Cumulocity | 11:e1bee9a77652 | 140 | return res; |
Cumulocity | 0:099f76422485 | 141 | |
Cumulocity | 11:e1bee9a77652 | 142 | _client.sendData(generator); |
Cumulocity | 11:e1bee9a77652 | 143 | return awaitResponse(); |
Cumulocity | 0:099f76422485 | 144 | } |
Cumulocity | 11:e1bee9a77652 | 145 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 13:aba98ad2ac1b | 146 | uint8_t SmartRest::stream(const char *uri, const Record& record, const char *overrideIdentifier) |
Cumulocity | 13:aba98ad2ac1b | 147 | { |
Cumulocity | 13:aba98ad2ac1b | 148 | uint8_t res; |
Cumulocity | 13:aba98ad2ac1b | 149 | |
Cumulocity | 13:aba98ad2ac1b | 150 | res = beginRequest(uri, overrideIdentifier); |
Cumulocity | 13:aba98ad2ac1b | 151 | if (res != SMARTREST_SUCCESS) |
Cumulocity | 13:aba98ad2ac1b | 152 | return res; |
Cumulocity | 13:aba98ad2ac1b | 153 | |
Cumulocity | 13:aba98ad2ac1b | 154 | _client.sendData(record); |
Cumulocity | 13:aba98ad2ac1b | 155 | return awaitResponse(); |
Cumulocity | 13:aba98ad2ac1b | 156 | } |
Cumulocity | 13:aba98ad2ac1b | 157 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 2:45a6e44a4fb4 | 158 | uint8_t SmartRest::receive(ParsedRecord& record) |
Cumulocity | 0:099f76422485 | 159 | { |
Cumulocity | 11:e1bee9a77652 | 160 | uint8_t res; |
Cumulocity | 0:099f76422485 | 161 | |
Cumulocity | 11:e1bee9a77652 | 162 | if (_source == NULL) |
Cumulocity | 11:e1bee9a77652 | 163 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 164 | |
Cumulocity | 11:e1bee9a77652 | 165 | res = _parser.readFrom(*_source, record); |
Cumulocity | 0:099f76422485 | 166 | |
Cumulocity | 11:e1bee9a77652 | 167 | switch (res) |
Cumulocity | 11:e1bee9a77652 | 168 | { |
Cumulocity | 11:e1bee9a77652 | 169 | case PARSER_SUCCESS: |
Cumulocity | 11:e1bee9a77652 | 170 | return SMARTREST_SUCCESS; |
Cumulocity | 11:e1bee9a77652 | 171 | case PARSER_END_OF_RESPONSE: |
Cumulocity | 11:e1bee9a77652 | 172 | return SMARTREST_END_OF_RESPONSE; |
Cumulocity | 11:e1bee9a77652 | 173 | case PARSER_TIMEOUT_ERROR: |
Cumulocity | 11:e1bee9a77652 | 174 | return SMARTREST_TIMEOUT_ERROR; |
Cumulocity | 11:e1bee9a77652 | 175 | } |
Cumulocity | 11:e1bee9a77652 | 176 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 0:099f76422485 | 177 | } |
Cumulocity | 11:e1bee9a77652 | 178 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 179 | void SmartRest::stop() |
Cumulocity | 0:099f76422485 | 180 | { |
Cumulocity | 11:e1bee9a77652 | 181 | _source = NULL; |
Cumulocity | 11:e1bee9a77652 | 182 | _client.stop(); |
Cumulocity | 0:099f76422485 | 183 | } |
Cumulocity | 11:e1bee9a77652 | 184 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 7:8159a2d12e4e | 185 | const char * SmartRest::getIdentifier() |
Cumulocity | 7:8159a2d12e4e | 186 | { |
Cumulocity | 11:e1bee9a77652 | 187 | if (*_mogid) |
Cumulocity | 11:e1bee9a77652 | 188 | return _mogid; |
Cumulocity | 11:e1bee9a77652 | 189 | return _identifier; |
Cumulocity | 7:8159a2d12e4e | 190 | } |
Cumulocity | 11:e1bee9a77652 | 191 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 13:aba98ad2ac1b | 192 | uint8_t SmartRest::beginRequest(const char *uri, const char *overrideIdentifier) |
Cumulocity | 0:099f76422485 | 193 | { |
Cumulocity | 11:e1bee9a77652 | 194 | int res; |
Cumulocity | 0:099f76422485 | 195 | |
Cumulocity | 13:aba98ad2ac1b | 196 | if (uri != NULL) |
Cumulocity | 13:aba98ad2ac1b | 197 | { |
Cumulocity | 13:aba98ad2ac1b | 198 | res = _client.beginStream(uri); |
Cumulocity | 13:aba98ad2ac1b | 199 | } |
Cumulocity | 13:aba98ad2ac1b | 200 | else |
Cumulocity | 13:aba98ad2ac1b | 201 | { |
Cumulocity | 13:aba98ad2ac1b | 202 | res = _client.beginRequest(); |
Cumulocity | 13:aba98ad2ac1b | 203 | } |
Cumulocity | 13:aba98ad2ac1b | 204 | |
Cumulocity | 11:e1bee9a77652 | 205 | if (res == CLIENT_CONNECTION_ERROR) |
Cumulocity | 11:e1bee9a77652 | 206 | { |
Cumulocity | 11:e1bee9a77652 | 207 | return SMARTREST_CONNECTION_FAILED; |
Cumulocity | 11:e1bee9a77652 | 208 | } |
Cumulocity | 11:e1bee9a77652 | 209 | else if (res != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 210 | { |
Cumulocity | 11:e1bee9a77652 | 211 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 212 | } |
Cumulocity | 11:e1bee9a77652 | 213 | if (overrideIdentifier != NULL) |
Cumulocity | 11:e1bee9a77652 | 214 | { |
Cumulocity | 11:e1bee9a77652 | 215 | if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 216 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 217 | } |
Cumulocity | 11:e1bee9a77652 | 218 | else |
Cumulocity | 11:e1bee9a77652 | 219 | { |
Cumulocity | 11:e1bee9a77652 | 220 | if (_client.sendIdentifier(getIdentifier()) != CLIENT_OK) |
Cumulocity | 11:e1bee9a77652 | 221 | return SMARTREST_INTERNAL_ERROR; |
Cumulocity | 11:e1bee9a77652 | 222 | } |
Cumulocity | 11:e1bee9a77652 | 223 | return SMARTREST_SUCCESS; |
Cumulocity | 0:099f76422485 | 224 | } |
Cumulocity | 11:e1bee9a77652 | 225 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 226 | uint8_t SmartRest::awaitResponse() |
Cumulocity | 0:099f76422485 | 227 | { |
Cumulocity | 11:e1bee9a77652 | 228 | if ((_client.endRequest() != CLIENT_OK) || |
xinlei | 18:16192696c106 | 229 | (_client.awaitResponse() != CLIENT_OK)) { |
Cumulocity | 11:e1bee9a77652 | 230 | return SMARTREST_INTERNAL_ERROR; |
xinlei | 18:16192696c106 | 231 | } |
Cumulocity | 11:e1bee9a77652 | 232 | _source = &_client.receiveData(); |
Cumulocity | 11:e1bee9a77652 | 233 | return SMARTREST_SUCCESS; |
Cumulocity | 0:099f76422485 | 234 | } |
Cumulocity | 11:e1bee9a77652 | 235 | /*-------------------------------------------------------------------------*/ |
Cumulocity | 0:099f76422485 | 236 | bool SmartRest::setMoGid(ParsedRecord& record) |
Cumulocity | 0:099f76422485 | 237 | { |
Cumulocity | 11:e1bee9a77652 | 238 | const char *mogid; |
Cumulocity | 0:099f76422485 | 239 | |
Cumulocity | 11:e1bee9a77652 | 240 | *_mogid = 0; |
Cumulocity | 11:e1bee9a77652 | 241 | if ((record.values() < 2) || (record.value(0).integerValue() != 20)) |
Cumulocity | 11:e1bee9a77652 | 242 | return false; |
Cumulocity | 11:e1bee9a77652 | 243 | |
Cumulocity | 11:e1bee9a77652 | 244 | if ((record.value(1).valueType() != VALUE_INTEGER) || |
Cumulocity | 11:e1bee9a77652 | 245 | (record.value(1).integerValue() <= 0)) |
Cumulocity | 11:e1bee9a77652 | 246 | return false; |
Cumulocity | 0:099f76422485 | 247 | |
Cumulocity | 11:e1bee9a77652 | 248 | mogid = record.rawValue(1); |
Cumulocity | 11:e1bee9a77652 | 249 | if (strlen(mogid)+1 > sizeof(_mogid)) |
Cumulocity | 11:e1bee9a77652 | 250 | return false; |
Cumulocity | 0:099f76422485 | 251 | |
Cumulocity | 11:e1bee9a77652 | 252 | strcpy(_mogid, mogid); |
Cumulocity | 0:099f76422485 | 253 | |
Cumulocity | 11:e1bee9a77652 | 254 | return true; |
Cumulocity | 0:099f76422485 | 255 | } |
Cumulocity | 11:e1bee9a77652 | 256 | /*-------------------------------------------------------------------------*/ |