mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.

Dependents:   MbedSmartRestMain MbedSmartRestMain

Committer:
Cumulocity
Date:
Mon Jul 07 16:14:51 2014 +0200
Revision:
1:9a11a331e340
Parent:
0:099f76422485
Child:
2:45a6e44a4fb4
Updated from revision 06e71f31df1c

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