Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Mon Jul 07 17:08:02 2014 +0200
Revision:
2:45a6e44a4fb4
Parent:
1:9a11a331e340
Child:
4:059b8b90e0d9
Updated from revision 98c2d7e29dea

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 2:45a6e44a4fb4 51 uint8_t SmartRest::send(const DataGenerator& generator, const char *overrideIdentifier)
Cumulocity 0:099f76422485 52 {
Cumulocity 0:099f76422485 53 uint8_t res;
Cumulocity 0:099f76422485 54
Cumulocity 1:9a11a331e340 55 res = beginRequest(overrideIdentifier);
Cumulocity 0:099f76422485 56 if (res != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 57 return res;
Cumulocity 0:099f76422485 58
Cumulocity 0:099f76422485 59 _client.sendData(generator);
Cumulocity 0:099f76422485 60 return awaitResponse();
Cumulocity 0:099f76422485 61 }
Cumulocity 0:099f76422485 62
Cumulocity 2:45a6e44a4fb4 63 uint8_t SmartRest::receive(ParsedRecord& record)
Cumulocity 0:099f76422485 64 {
Cumulocity 0:099f76422485 65 uint8_t res;
Cumulocity 0:099f76422485 66
Cumulocity 0:099f76422485 67 if (_source == NULL)
Cumulocity 0:099f76422485 68 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 69
Cumulocity 0:099f76422485 70 res = _parser.readFrom(*_source, record);
Cumulocity 0:099f76422485 71
Cumulocity 0:099f76422485 72 switch (res) {
Cumulocity 0:099f76422485 73 case PARSER_SUCCESS:
Cumulocity 0:099f76422485 74 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 75 case PARSER_END_OF_RESPONSE:
Cumulocity 0:099f76422485 76 return SMARTREST_END_OF_RESPONSE;
Cumulocity 0:099f76422485 77 case PARSER_TIMEOUT_ERROR:
Cumulocity 0:099f76422485 78 return SMARTREST_TIMEOUT_ERROR;
Cumulocity 0:099f76422485 79 }
Cumulocity 0:099f76422485 80 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 81 }
Cumulocity 0:099f76422485 82
Cumulocity 2:45a6e44a4fb4 83 uint8_t SmartRest::bootstrap(DataGenerator& generator)
Cumulocity 0:099f76422485 84 {
Cumulocity 0:099f76422485 85 ParsedRecord record;
Cumulocity 0:099f76422485 86 int8_t ret;
Cumulocity 0:099f76422485 87
Cumulocity 1:9a11a331e340 88 ret = beginRequest(NULL);
Cumulocity 0:099f76422485 89 if (ret != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 90 return ret;
Cumulocity 0:099f76422485 91 ret = awaitResponse();
Cumulocity 0:099f76422485 92 if (ret != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 93 return ret;
Cumulocity 0:099f76422485 94 ret = receive(record);
Cumulocity 0:099f76422485 95 if (ret != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 96 return ret;
Cumulocity 0:099f76422485 97 if (!record) {
Cumulocity 0:099f76422485 98 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 99 }
Cumulocity 0:099f76422485 100 stop();
Cumulocity 0:099f76422485 101
Cumulocity 0:099f76422485 102 if (setMoGid(record))
Cumulocity 0:099f76422485 103 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 104
Cumulocity 0:099f76422485 105 if (record.value(0).integerValue() != 40)
Cumulocity 0:099f76422485 106 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 107
Cumulocity 0:099f76422485 108 ret = send(generator);
Cumulocity 0:099f76422485 109 if (ret != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 110 return ret;
Cumulocity 0:099f76422485 111 ret = receive(record);
Cumulocity 0:099f76422485 112 if (ret != SMARTREST_SUCCESS)
Cumulocity 0:099f76422485 113 return ret;
Cumulocity 0:099f76422485 114 stop();
Cumulocity 0:099f76422485 115
Cumulocity 0:099f76422485 116 if (!setMoGid(record))
Cumulocity 0:099f76422485 117 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 118
Cumulocity 0:099f76422485 119 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 120 }
Cumulocity 0:099f76422485 121
Cumulocity 0:099f76422485 122 void SmartRest::stop()
Cumulocity 0:099f76422485 123 {
Cumulocity 0:099f76422485 124 _source = NULL;
Cumulocity 0:099f76422485 125 _client.stop();
Cumulocity 0:099f76422485 126 }
Cumulocity 0:099f76422485 127
Cumulocity 1:9a11a331e340 128 uint8_t SmartRest::beginRequest(const char *overrideIdentifier)
Cumulocity 0:099f76422485 129 {
Cumulocity 0:099f76422485 130 int res;
Cumulocity 0:099f76422485 131
Cumulocity 0:099f76422485 132 res = _client.beginRequest();
Cumulocity 0:099f76422485 133 if (res == CLIENT_CONNECTION_ERROR) {
Cumulocity 0:099f76422485 134 return SMARTREST_CONNECTION_FAILED;
Cumulocity 0:099f76422485 135 } else if (res != CLIENT_OK) {
Cumulocity 0:099f76422485 136 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 137 }
Cumulocity 1:9a11a331e340 138 if (overrideIdentifier != NULL) {
Cumulocity 1:9a11a331e340 139 if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK)
Cumulocity 1:9a11a331e340 140 return SMARTREST_INTERNAL_ERROR;
Cumulocity 1:9a11a331e340 141 } else if (strlen(_mogid)) {
Cumulocity 0:099f76422485 142 if (_client.sendIdentifier(_mogid) != CLIENT_OK)
Cumulocity 0:099f76422485 143 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 144 } else {
Cumulocity 0:099f76422485 145 if (_client.sendIdentifier(_identifier) != CLIENT_OK)
Cumulocity 0:099f76422485 146 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 147 }
Cumulocity 0:099f76422485 148 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 149 }
Cumulocity 0:099f76422485 150
Cumulocity 0:099f76422485 151 uint8_t SmartRest::awaitResponse()
Cumulocity 0:099f76422485 152 {
Cumulocity 0:099f76422485 153 if ((_client.endRequest() != CLIENT_OK) ||
Cumulocity 0:099f76422485 154 (_client.awaitResponse() != CLIENT_OK))
Cumulocity 0:099f76422485 155 return SMARTREST_INTERNAL_ERROR;
Cumulocity 0:099f76422485 156 _source = &_client.receiveData();
Cumulocity 0:099f76422485 157 return SMARTREST_SUCCESS;
Cumulocity 0:099f76422485 158 }
Cumulocity 0:099f76422485 159
Cumulocity 0:099f76422485 160 bool SmartRest::setMoGid(ParsedRecord& record)
Cumulocity 0:099f76422485 161 {
Cumulocity 0:099f76422485 162 const char *mogid;
Cumulocity 0:099f76422485 163
Cumulocity 0:099f76422485 164 *_mogid = 0;
Cumulocity 0:099f76422485 165 if ((record.values() < 2) || (record.value(0).integerValue() != 20))
Cumulocity 0:099f76422485 166 return false;
Cumulocity 0:099f76422485 167
Cumulocity 0:099f76422485 168 if ((record.value(1).valueType() != VALUE_INTEGER) ||
Cumulocity 0:099f76422485 169 (record.value(1).integerValue() <= 0))
Cumulocity 0:099f76422485 170 return false;
Cumulocity 0:099f76422485 171
Cumulocity 0:099f76422485 172 mogid = record.rawValue(1);
Cumulocity 0:099f76422485 173 if (strlen(mogid)+1 > sizeof(_mogid))
Cumulocity 0:099f76422485 174 return false;
Cumulocity 0:099f76422485 175
Cumulocity 0:099f76422485 176 strcpy(_mogid, mogid);
Cumulocity 0:099f76422485 177
Cumulocity 0:099f76422485 178 return true;
Cumulocity 0:099f76422485 179 }