Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Thu Jul 10 16:20:31 2014 +0200
Revision:
5:2b74510900da
Parent:
4:059b8b90e0d9
Child:
6:cd7ba1ddb664
Updated from revision 131b98d7d1aa

Who changed what in which revision?

UserRevisionLine numberNew 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 2:45a6e44a4fb4 33 SmartRest::SmartRest(AbstractClient& client, const char *identifier) :
Cumulocity 2:45a6e44a4fb4 34 _client(client),
Cumulocity 2:45a6e44a4fb4 35 _identifier(identifier)
Cumulocity 0:099f76422485 36 {
Cumulocity 0:099f76422485 37 _source = NULL;
Cumulocity 0:099f76422485 38 _mogid[0] = 0;
Cumulocity 0:099f76422485 39 }
Cumulocity 0:099f76422485 40
Cumulocity 2:45a6e44a4fb4 41 uint8_t SmartRest::setAuthorization(const char *username, const char *password)
Cumulocity 2:45a6e44a4fb4 42 {
Cumulocity 2:45a6e44a4fb4 43 uint8_t res;
Cumulocity 2:45a6e44a4fb4 44
Cumulocity 2:45a6e44a4fb4 45 res = _client.setAuthorization(username, password);
Cumulocity 2:45a6e44a4fb4 46 if (res != CLIENT_OK)
Cumulocity 2:45a6e44a4fb4 47 return SMARTREST_INTERNAL_ERROR;
Cumulocity 2:45a6e44a4fb4 48 return SMARTREST_SUCCESS;
Cumulocity 2:45a6e44a4fb4 49 }
Cumulocity 2:45a6e44a4fb4 50
Cumulocity 5:2b74510900da 51 #ifdef SMARTREST_TRANSACTIONAL
Cumulocity 5:2b74510900da 52 uint8_t SmartRest::request(const DataGenerator& generator, const char *overrideIdentifier)
Cumulocity 5:2b74510900da 53 {
Cumulocity 5:2b74510900da 54 uint8_t res;
Cumulocity 5:2b74510900da 55
Cumulocity 5:2b74510900da 56 res = send(generator, overrideIdentifier);
Cumulocity 5:2b74510900da 57 stop();
Cumulocity 5:2b74510900da 58 return res;
Cumulocity 5:2b74510900da 59 }
Cumulocity 5:2b74510900da 60
Cumulocity 5:2b74510900da 61 uint8_t SmartRest::request(const DataGenerator& generator, Aggregator& aggregator, const char *overrideIdentifier)
Cumulocity 5:2b74510900da 62 {
Cumulocity 5:2b74510900da 63 uint8_t res;
Cumulocity 5:2b74510900da 64
Cumulocity 5:2b74510900da 65 if (!aggregator.isManaged())
Cumulocity 5:2b74510900da 66 return SMARTREST_INTERNAL_ERROR;
Cumulocity 5:2b74510900da 67
Cumulocity 5:2b74510900da 68 res = send(generator, overrideIdentifier);
Cumulocity 5:2b74510900da 69 if (res != SMARTREST_SUCCESS) {
Cumulocity 5:2b74510900da 70 stop();
Cumulocity 5:2b74510900da 71 return res;
Cumulocity 5:2b74510900da 72 }
Cumulocity 5:2b74510900da 73
Cumulocity 5:2b74510900da 74 ParsedRecord recvd;
Cumulocity 5:2b74510900da 75 while ((res = receive(recvd)) == SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 76 aggregator.add(recvd);
Cumulocity 5:2b74510900da 77
Cumulocity 5:2b74510900da 78 stop();
Cumulocity 5:2b74510900da 79 if (res == SMARTREST_END_OF_RESPONSE)
Cumulocity 5:2b74510900da 80 return SMARTREST_SUCCESS;
Cumulocity 5:2b74510900da 81 return res;
Cumulocity 5:2b74510900da 82 }
Cumulocity 5:2b74510900da 83 #endif
Cumulocity 5:2b74510900da 84
Cumulocity 5:2b74510900da 85 uint8_t SmartRest::bootstrap(const DataGenerator& generator)
Cumulocity 5:2b74510900da 86 {
Cumulocity 5:2b74510900da 87 ParsedRecord record;
Cumulocity 5:2b74510900da 88 int8_t ret;
Cumulocity 5:2b74510900da 89
Cumulocity 5:2b74510900da 90 ret = beginRequest(NULL);
Cumulocity 5:2b74510900da 91 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 92 return ret;
Cumulocity 5:2b74510900da 93 ret = awaitResponse();
Cumulocity 5:2b74510900da 94 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 95 return ret;
Cumulocity 5:2b74510900da 96 ret = receive(record);
Cumulocity 5:2b74510900da 97 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 98 return ret;
Cumulocity 5:2b74510900da 99 if (!record) {
Cumulocity 5:2b74510900da 100 return SMARTREST_INTERNAL_ERROR;
Cumulocity 5:2b74510900da 101 }
Cumulocity 5:2b74510900da 102 stop();
Cumulocity 5:2b74510900da 103
Cumulocity 5:2b74510900da 104 if (setMoGid(record))
Cumulocity 5:2b74510900da 105 return SMARTREST_SUCCESS;
Cumulocity 5:2b74510900da 106
Cumulocity 5:2b74510900da 107 if (record.value(0).integerValue() != 40)
Cumulocity 5:2b74510900da 108 return SMARTREST_INTERNAL_ERROR;
Cumulocity 5:2b74510900da 109
Cumulocity 5:2b74510900da 110 ret = send(generator, NULL);
Cumulocity 5:2b74510900da 111 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 112 return ret;
Cumulocity 5:2b74510900da 113 ret = receive(record);
Cumulocity 5:2b74510900da 114 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 115 return ret;
Cumulocity 5:2b74510900da 116 stop();
Cumulocity 5:2b74510900da 117
Cumulocity 5:2b74510900da 118 if (!setMoGid(record))
Cumulocity 5:2b74510900da 119 return SMARTREST_INTERNAL_ERROR;
Cumulocity 5:2b74510900da 120
Cumulocity 5:2b74510900da 121 return SMARTREST_SUCCESS;
Cumulocity 5:2b74510900da 122 }
Cumulocity 5:2b74510900da 123
Cumulocity 2:45a6e44a4fb4 124 uint8_t SmartRest::send(const DataGenerator& generator, const char *overrideIdentifier)
Cumulocity 0:099f76422485 125 {
Cumulocity 0:099f76422485 126 uint8_t res;
Cumulocity 0:099f76422485 127
Cumulocity 1:9a11a331e340 128 res = beginRequest(overrideIdentifier);
Cumulocity 0:099f76422485 129 if (res != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 130 return res;
Cumulocity 0:099f76422485 131
Cumulocity 0:099f76422485 132 _client.sendData(generator);
Cumulocity 0:099f76422485 133 return awaitResponse();
Cumulocity 0:099f76422485 134 }
Cumulocity 0:099f76422485 135
Cumulocity 2:45a6e44a4fb4 136 uint8_t SmartRest::receive(ParsedRecord& record)
Cumulocity 0:099f76422485 137 {
Cumulocity 0:099f76422485 138 uint8_t res;
Cumulocity 0:099f76422485 139
Cumulocity 0:099f76422485 140 if (_source == NULL)
Cumulocity 0:099f76422485 141 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 142
Cumulocity 0:099f76422485 143 res = _parser.readFrom(*_source, record);
Cumulocity 0:099f76422485 144
Cumulocity 0:099f76422485 145 switch (res) {
Cumulocity 0:099f76422485 146 case PARSER_SUCCESS:
Cumulocity 0:099f76422485 147 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 148 case PARSER_END_OF_RESPONSE:
Cumulocity 0:099f76422485 149 return SMARTREST_END_OF_RESPONSE;
Cumulocity 0:099f76422485 150 case PARSER_TIMEOUT_ERROR:
Cumulocity 0:099f76422485 151 return SMARTREST_TIMEOUT_ERROR;
Cumulocity 0:099f76422485 152 }
Cumulocity 0:099f76422485 153 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 154 }
Cumulocity 0:099f76422485 155
Cumulocity 0:099f76422485 156 void SmartRest::stop()
Cumulocity 0:099f76422485 157 {
Cumulocity 0:099f76422485 158 _source = NULL;
Cumulocity 0:099f76422485 159 _client.stop();
Cumulocity 0:099f76422485 160 }
Cumulocity 0:099f76422485 161
Cumulocity 1:9a11a331e340 162 uint8_t SmartRest::beginRequest(const char *overrideIdentifier)
Cumulocity 0:099f76422485 163 {
Cumulocity 0:099f76422485 164 int res;
Cumulocity 0:099f76422485 165
Cumulocity 0:099f76422485 166 res = _client.beginRequest();
Cumulocity 0:099f76422485 167 if (res == CLIENT_CONNECTION_ERROR) {
Cumulocity 0:099f76422485 168 return SMARTREST_CONNECTION_FAILED;
Cumulocity 0:099f76422485 169 } else if (res != CLIENT_OK) {
Cumulocity 0:099f76422485 170 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 171 }
Cumulocity 1:9a11a331e340 172 if (overrideIdentifier != NULL) {
Cumulocity 1:9a11a331e340 173 if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK)
Cumulocity 1:9a11a331e340 174 return SMARTREST_INTERNAL_ERROR;
Cumulocity 1:9a11a331e340 175 } else if (strlen(_mogid)) {
Cumulocity 0:099f76422485 176 if (_client.sendIdentifier(_mogid) != CLIENT_OK)
Cumulocity 0:099f76422485 177 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 178 } else {
Cumulocity 0:099f76422485 179 if (_client.sendIdentifier(_identifier) != CLIENT_OK)
Cumulocity 0:099f76422485 180 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 181 }
Cumulocity 0:099f76422485 182 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 183 }
Cumulocity 0:099f76422485 184
Cumulocity 0:099f76422485 185 uint8_t SmartRest::awaitResponse()
Cumulocity 0:099f76422485 186 {
Cumulocity 0:099f76422485 187 if ((_client.endRequest() != CLIENT_OK) ||
Cumulocity 0:099f76422485 188 (_client.awaitResponse() != CLIENT_OK))
Cumulocity 0:099f76422485 189 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 190 _source = &_client.receiveData();
Cumulocity 0:099f76422485 191 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 192 }
Cumulocity 0:099f76422485 193
Cumulocity 0:099f76422485 194 bool SmartRest::setMoGid(ParsedRecord& record)
Cumulocity 0:099f76422485 195 {
Cumulocity 0:099f76422485 196 const char *mogid;
Cumulocity 0:099f76422485 197
Cumulocity 0:099f76422485 198 *_mogid = 0;
Cumulocity 0:099f76422485 199 if ((record.values() < 2) || (record.value(0).integerValue() != 20))
Cumulocity 0:099f76422485 200 return false;
Cumulocity 0:099f76422485 201
Cumulocity 0:099f76422485 202 if ((record.value(1).valueType() != VALUE_INTEGER) ||
Cumulocity 0:099f76422485 203 (record.value(1).integerValue() <= 0))
Cumulocity 0:099f76422485 204 return false;
Cumulocity 0:099f76422485 205
Cumulocity 0:099f76422485 206 mogid = record.rawValue(1);
Cumulocity 0:099f76422485 207 if (strlen(mogid)+1 > sizeof(_mogid))
Cumulocity 0:099f76422485 208 return false;
Cumulocity 0:099f76422485 209
Cumulocity 0:099f76422485 210 strcpy(_mogid, mogid);
Cumulocity 0:099f76422485 211
Cumulocity 0:099f76422485 212 return true;
Cumulocity 0:099f76422485 213 }