Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Wed Oct 22 16:17:22 2014 +0200
Revision:
7:8159a2d12e4e
Parent:
6:cd7ba1ddb664
Child:
11:e1bee9a77652
Updated from revision aaae61ab8a05

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 6:cd7ba1ddb664 65 if (!aggregator.managed())
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 6:cd7ba1ddb664 75 while ((res = receive(recvd)) == SMARTREST_SUCCESS) {
Cumulocity 6:cd7ba1ddb664 76 if (!aggregator.add(recvd))
Cumulocity 6:cd7ba1ddb664 77 return SMARTREST_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 78 }
Cumulocity 5:2b74510900da 79
Cumulocity 5:2b74510900da 80 stop();
Cumulocity 5:2b74510900da 81 if (res == SMARTREST_END_OF_RESPONSE)
Cumulocity 5:2b74510900da 82 return SMARTREST_SUCCESS;
Cumulocity 5:2b74510900da 83 return res;
Cumulocity 5:2b74510900da 84 }
Cumulocity 5:2b74510900da 85 #endif
Cumulocity 5:2b74510900da 86
Cumulocity 5:2b74510900da 87 uint8_t SmartRest::bootstrap(const DataGenerator& generator)
Cumulocity 5:2b74510900da 88 {
Cumulocity 5:2b74510900da 89 ParsedRecord record;
Cumulocity 5:2b74510900da 90 int8_t ret;
Cumulocity 5:2b74510900da 91
Cumulocity 5:2b74510900da 92 ret = beginRequest(NULL);
Cumulocity 5:2b74510900da 93 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 94 return ret;
Cumulocity 5:2b74510900da 95 ret = awaitResponse();
Cumulocity 5:2b74510900da 96 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 97 return ret;
Cumulocity 5:2b74510900da 98 ret = receive(record);
Cumulocity 5:2b74510900da 99 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 100 return ret;
Cumulocity 5:2b74510900da 101 if (!record) {
Cumulocity 5:2b74510900da 102 return SMARTREST_INTERNAL_ERROR;
Cumulocity 5:2b74510900da 103 }
Cumulocity 5:2b74510900da 104 stop();
Cumulocity 5:2b74510900da 105
Cumulocity 5:2b74510900da 106 if (setMoGid(record))
Cumulocity 5:2b74510900da 107 return SMARTREST_SUCCESS;
Cumulocity 5:2b74510900da 108
Cumulocity 5:2b74510900da 109 if (record.value(0).integerValue() != 40)
Cumulocity 5:2b74510900da 110 return SMARTREST_INTERNAL_ERROR;
Cumulocity 5:2b74510900da 111
Cumulocity 5:2b74510900da 112 ret = send(generator, NULL);
Cumulocity 5:2b74510900da 113 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 114 return ret;
Cumulocity 5:2b74510900da 115 ret = receive(record);
Cumulocity 5:2b74510900da 116 if (ret != SMARTREST_SUCCESS)
Cumulocity 5:2b74510900da 117 return ret;
Cumulocity 5:2b74510900da 118 stop();
Cumulocity 5:2b74510900da 119
Cumulocity 5:2b74510900da 120 if (!setMoGid(record))
Cumulocity 5:2b74510900da 121 return SMARTREST_INTERNAL_ERROR;
Cumulocity 5:2b74510900da 122
Cumulocity 5:2b74510900da 123 return SMARTREST_SUCCESS;
Cumulocity 5:2b74510900da 124 }
Cumulocity 5:2b74510900da 125
Cumulocity 2:45a6e44a4fb4 126 uint8_t SmartRest::send(const DataGenerator& generator, const char *overrideIdentifier)
Cumulocity 0:099f76422485 127 {
Cumulocity 0:099f76422485 128 uint8_t res;
Cumulocity 0:099f76422485 129
Cumulocity 1:9a11a331e340 130 res = beginRequest(overrideIdentifier);
Cumulocity 0:099f76422485 131 if (res != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 132 return res;
Cumulocity 0:099f76422485 133
Cumulocity 0:099f76422485 134 _client.sendData(generator);
Cumulocity 0:099f76422485 135 return awaitResponse();
Cumulocity 0:099f76422485 136 }
Cumulocity 0:099f76422485 137
Cumulocity 2:45a6e44a4fb4 138 uint8_t SmartRest::receive(ParsedRecord& record)
Cumulocity 0:099f76422485 139 {
Cumulocity 0:099f76422485 140 uint8_t res;
Cumulocity 0:099f76422485 141
Cumulocity 0:099f76422485 142 if (_source == NULL)
Cumulocity 0:099f76422485 143 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 144
Cumulocity 0:099f76422485 145 res = _parser.readFrom(*_source, record);
Cumulocity 0:099f76422485 146
Cumulocity 0:099f76422485 147 switch (res) {
Cumulocity 0:099f76422485 148 case PARSER_SUCCESS:
Cumulocity 0:099f76422485 149 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 150 case PARSER_END_OF_RESPONSE:
Cumulocity 0:099f76422485 151 return SMARTREST_END_OF_RESPONSE;
Cumulocity 0:099f76422485 152 case PARSER_TIMEOUT_ERROR:
Cumulocity 0:099f76422485 153 return SMARTREST_TIMEOUT_ERROR;
Cumulocity 0:099f76422485 154 }
Cumulocity 0:099f76422485 155 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 156 }
Cumulocity 0:099f76422485 157
Cumulocity 0:099f76422485 158 void SmartRest::stop()
Cumulocity 0:099f76422485 159 {
Cumulocity 0:099f76422485 160 _source = NULL;
Cumulocity 0:099f76422485 161 _client.stop();
Cumulocity 0:099f76422485 162 }
Cumulocity 0:099f76422485 163
Cumulocity 7:8159a2d12e4e 164 const char * SmartRest::getIdentifier()
Cumulocity 7:8159a2d12e4e 165 {
Cumulocity 7:8159a2d12e4e 166 if (*_mogid)
Cumulocity 7:8159a2d12e4e 167 return _mogid;
Cumulocity 7:8159a2d12e4e 168 return _identifier;
Cumulocity 7:8159a2d12e4e 169 }
Cumulocity 7:8159a2d12e4e 170
Cumulocity 1:9a11a331e340 171 uint8_t SmartRest::beginRequest(const char *overrideIdentifier)
Cumulocity 0:099f76422485 172 {
Cumulocity 0:099f76422485 173 int res;
Cumulocity 0:099f76422485 174
Cumulocity 0:099f76422485 175 res = _client.beginRequest();
Cumulocity 0:099f76422485 176 if (res == CLIENT_CONNECTION_ERROR) {
Cumulocity 0:099f76422485 177 return SMARTREST_CONNECTION_FAILED;
Cumulocity 0:099f76422485 178 } else if (res != CLIENT_OK) {
Cumulocity 0:099f76422485 179 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 180 }
Cumulocity 1:9a11a331e340 181 if (overrideIdentifier != NULL) {
Cumulocity 1:9a11a331e340 182 if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK)
Cumulocity 1:9a11a331e340 183 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 184 } else {
Cumulocity 7:8159a2d12e4e 185 if (_client.sendIdentifier(getIdentifier()) != CLIENT_OK)
Cumulocity 0:099f76422485 186 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 187 }
Cumulocity 0:099f76422485 188 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 189 }
Cumulocity 0:099f76422485 190
Cumulocity 0:099f76422485 191 uint8_t SmartRest::awaitResponse()
Cumulocity 0:099f76422485 192 {
Cumulocity 0:099f76422485 193 if ((_client.endRequest() != CLIENT_OK) ||
Cumulocity 0:099f76422485 194 (_client.awaitResponse() != CLIENT_OK))
Cumulocity 0:099f76422485 195 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 196 _source = &_client.receiveData();
Cumulocity 0:099f76422485 197 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 198 }
Cumulocity 0:099f76422485 199
Cumulocity 0:099f76422485 200 bool SmartRest::setMoGid(ParsedRecord& record)
Cumulocity 0:099f76422485 201 {
Cumulocity 0:099f76422485 202 const char *mogid;
Cumulocity 0:099f76422485 203
Cumulocity 0:099f76422485 204 *_mogid = 0;
Cumulocity 0:099f76422485 205 if ((record.values() < 2) || (record.value(0).integerValue() != 20))
Cumulocity 0:099f76422485 206 return false;
Cumulocity 0:099f76422485 207
Cumulocity 0:099f76422485 208 if ((record.value(1).valueType() != VALUE_INTEGER) ||
Cumulocity 0:099f76422485 209 (record.value(1).integerValue() <= 0))
Cumulocity 0:099f76422485 210 return false;
Cumulocity 0:099f76422485 211
Cumulocity 0:099f76422485 212 mogid = record.rawValue(1);
Cumulocity 0:099f76422485 213 if (strlen(mogid)+1 > sizeof(_mogid))
Cumulocity 0:099f76422485 214 return false;
Cumulocity 0:099f76422485 215
Cumulocity 0:099f76422485 216 strcpy(_mogid, mogid);
Cumulocity 0:099f76422485 217
Cumulocity 0:099f76422485 218 return true;
Cumulocity 0:099f76422485 219 }