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

Dependents:   MbedSmartRestMain MbedSmartRestMain

Committer:
Cumulocity
Date:
Sat Nov 15 12:21:41 2014 +0100
Revision:
13:aba98ad2ac1b
Parent:
12:6634f9814235
Updated from revision 0b898f0efc6d

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * MbedClient.cpp
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Feb 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 "MbedClient.h"
Cumulocity 0:099f76422485 30 #include <stdlib.h>
Cumulocity 0:099f76422485 31 #include <string.h>
Cumulocity 0:099f76422485 32 #include "b64.h"
Cumulocity 0:099f76422485 33 #include "mbed.h"
Cumulocity 0:099f76422485 34
Cumulocity 6:cd7ba1ddb664 35 #define STATE_INIT 0
Cumulocity 6:cd7ba1ddb664 36 #define STATE_IN_REQUEST 1
Cumulocity 6:cd7ba1ddb664 37 #define STATE_SENT_ID 2
Cumulocity 6:cd7ba1ddb664 38 #define STATE_SENT_DATA 3
Cumulocity 6:cd7ba1ddb664 39 #define STATE_REQ_COMPLETE 4
Cumulocity 6:cd7ba1ddb664 40 #define STATE_RECVD_RESPONSE 5
Cumulocity 6:cd7ba1ddb664 41 #define STATE_INTERNAL_ERROR 6
Cumulocity 6:cd7ba1ddb664 42
Cumulocity 6:cd7ba1ddb664 43 MbedClient::MbedClient(const char* host, uint16_t port, uint8_t tries) :
Cumulocity 2:45a6e44a4fb4 44 _host(host),
Cumulocity 6:cd7ba1ddb664 45 _username(NULL),
Cumulocity 6:cd7ba1ddb664 46 _password(NULL),
Cumulocity 2:45a6e44a4fb4 47 _port(port),
Cumulocity 6:cd7ba1ddb664 48 _tries(tries),
Cumulocity 6:cd7ba1ddb664 49 _state(STATE_INIT),
Cumulocity 11:e1bee9a77652 50 _isStreamRequest(false),
Cumulocity 2:45a6e44a4fb4 51 _filter(_source),
Cumulocity 2:45a6e44a4fb4 52 _source(_sock),
Cumulocity 2:45a6e44a4fb4 53 _sink(_sock),
Cumulocity 2:45a6e44a4fb4 54 _sock()
Cumulocity 0:099f76422485 55 {
Cumulocity 0:099f76422485 56 }
Cumulocity 0:099f76422485 57
Cumulocity 0:099f76422485 58 MbedClient::~MbedClient()
Cumulocity 0:099f76422485 59 {
Cumulocity 0:099f76422485 60 }
Cumulocity 0:099f76422485 61
Cumulocity 2:45a6e44a4fb4 62 uint8_t MbedClient::setAuthorization(const char* username, const char* password)
Cumulocity 2:45a6e44a4fb4 63 {
Cumulocity 6:cd7ba1ddb664 64 if (_state != STATE_INIT)
Cumulocity 6:cd7ba1ddb664 65 return internalError();
Cumulocity 2:45a6e44a4fb4 66
Cumulocity 2:45a6e44a4fb4 67 _username = username;
Cumulocity 2:45a6e44a4fb4 68 _password = password;
Cumulocity 8:3a4dba260b71 69 MBCL_DBG("Set authorization to %s:%s", username, password);
Cumulocity 2:45a6e44a4fb4 70 return CLIENT_OK;
Cumulocity 2:45a6e44a4fb4 71 }
Cumulocity 2:45a6e44a4fb4 72
Cumulocity 0:099f76422485 73 uint8_t MbedClient::beginRequest()
Cumulocity 0:099f76422485 74 {
Cumulocity 6:cd7ba1ddb664 75 if (_state != STATE_INIT)
Cumulocity 6:cd7ba1ddb664 76 return internalError();
Cumulocity 0:099f76422485 77
Cumulocity 11:e1bee9a77652 78 MBCL_DBG("Beginning SmartREST request.");
Cumulocity 11:e1bee9a77652 79 _source.setTimeout(60000);
Cumulocity 11:e1bee9a77652 80 if (!connect())
Cumulocity 11:e1bee9a77652 81 return connectionError();
Cumulocity 6:cd7ba1ddb664 82
Cumulocity 11:e1bee9a77652 83 if (!sendRequestHeader("/s"))
Cumulocity 6:cd7ba1ddb664 84 return connectionError();
Cumulocity 0:099f76422485 85
Cumulocity 11:e1bee9a77652 86 _state = STATE_IN_REQUEST;
Cumulocity 11:e1bee9a77652 87 return CLIENT_OK;
Cumulocity 11:e1bee9a77652 88 }
Cumulocity 8:3a4dba260b71 89
Cumulocity 11:e1bee9a77652 90 uint8_t MbedClient::beginStream(const char *uri)
Cumulocity 11:e1bee9a77652 91 {
Cumulocity 11:e1bee9a77652 92 if (_state != STATE_INIT)
Cumulocity 11:e1bee9a77652 93 return internalError();
Cumulocity 11:e1bee9a77652 94
Cumulocity 11:e1bee9a77652 95 // set stream request flag to later set the timeout right
Cumulocity 11:e1bee9a77652 96 _isStreamRequest = true;
Cumulocity 11:e1bee9a77652 97
Cumulocity 11:e1bee9a77652 98 MBCL_DBG("Beginning SmartREST request.");
Cumulocity 11:e1bee9a77652 99 _source.setTimeout(60000);
Cumulocity 11:e1bee9a77652 100 if (!connect())
Cumulocity 6:cd7ba1ddb664 101 return connectionError();
Cumulocity 11:e1bee9a77652 102
Cumulocity 11:e1bee9a77652 103 if (!sendRequestHeader(uri))
Cumulocity 11:e1bee9a77652 104 return connectionError();
Cumulocity 0:099f76422485 105
Cumulocity 6:cd7ba1ddb664 106 _state = STATE_IN_REQUEST;
Cumulocity 0:099f76422485 107 return CLIENT_OK;
Cumulocity 0:099f76422485 108 }
Cumulocity 0:099f76422485 109
Cumulocity 0:099f76422485 110 uint8_t MbedClient::sendIdentifier(const char* identifier)
Cumulocity 0:099f76422485 111 {
Cumulocity 6:cd7ba1ddb664 112 if (_state != STATE_IN_REQUEST)
Cumulocity 6:cd7ba1ddb664 113 return internalError();
Cumulocity 0:099f76422485 114
Cumulocity 8:3a4dba260b71 115 MBCL_DBG("Sending template identifier.");
Cumulocity 0:099f76422485 116 if ((identifier != NULL) && (strlen(identifier) != 0)) {
Cumulocity 0:099f76422485 117 if ((!send("X-Id: ")) ||
Cumulocity 0:099f76422485 118 (!send(identifier)) ||
Cumulocity 0:099f76422485 119 (!send("\r\n")))
Cumulocity 6:cd7ba1ddb664 120 return connectionError();
Cumulocity 0:099f76422485 121 }
Cumulocity 0:099f76422485 122
Cumulocity 6:cd7ba1ddb664 123 _state = STATE_SENT_ID;
Cumulocity 0:099f76422485 124 return CLIENT_OK;
Cumulocity 0:099f76422485 125 }
Cumulocity 0:099f76422485 126
Cumulocity 1:9a11a331e340 127 uint8_t MbedClient::sendData(const DataGenerator& generator)
Cumulocity 0:099f76422485 128 {
Cumulocity 7:8159a2d12e4e 129 size_t len;
Cumulocity 0:099f76422485 130
Cumulocity 6:cd7ba1ddb664 131 if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
Cumulocity 6:cd7ba1ddb664 132 return internalError();
Cumulocity 0:099f76422485 133
Cumulocity 8:3a4dba260b71 134 MBCL_DBG("Sending request payload.");
Cumulocity 0:099f76422485 135 len = generator.writtenLength();
Cumulocity 0:099f76422485 136 if ((!send("Content-Length: ")) ||
Cumulocity 7:8159a2d12e4e 137 (_sink.write((unsigned long)len) == 0) ||
Cumulocity 0:099f76422485 138 (!send("\r\n\r\n")))
Cumulocity 6:cd7ba1ddb664 139 return connectionError();
Cumulocity 0:099f76422485 140
Cumulocity 6:cd7ba1ddb664 141 if (generator.writeTo(_sink) != len)
Cumulocity 6:cd7ba1ddb664 142 return connectionError();
Cumulocity 0:099f76422485 143
Cumulocity 6:cd7ba1ddb664 144 _state = STATE_SENT_DATA;
Cumulocity 0:099f76422485 145 return CLIENT_OK;
Cumulocity 0:099f76422485 146 }
Cumulocity 0:099f76422485 147
Cumulocity 0:099f76422485 148 uint8_t MbedClient::endRequest()
Cumulocity 0:099f76422485 149 {
Cumulocity 6:cd7ba1ddb664 150 if ((_state != STATE_IN_REQUEST) &&
Cumulocity 6:cd7ba1ddb664 151 (_state != STATE_SENT_ID) &&
Cumulocity 6:cd7ba1ddb664 152 (_state != STATE_SENT_DATA))
Cumulocity 6:cd7ba1ddb664 153 return internalError();
Cumulocity 0:099f76422485 154
Cumulocity 12:6634f9814235 155 MBCL_DBG("Ending request.");
Cumulocity 8:3a4dba260b71 156
Cumulocity 6:cd7ba1ddb664 157 if (_state != STATE_SENT_DATA) {
Cumulocity 0:099f76422485 158 // send end of headers
Cumulocity 0:099f76422485 159 if (!send("\r\n"))
Cumulocity 6:cd7ba1ddb664 160 return connectionError();
Cumulocity 0:099f76422485 161 }
Cumulocity 0:099f76422485 162
Cumulocity 6:cd7ba1ddb664 163 if (!_sink.flush())
Cumulocity 6:cd7ba1ddb664 164 return connectionError();
Cumulocity 0:099f76422485 165
Cumulocity 6:cd7ba1ddb664 166 _state = STATE_REQ_COMPLETE;
Cumulocity 0:099f76422485 167 return CLIENT_OK;
Cumulocity 0:099f76422485 168 }
Cumulocity 0:099f76422485 169
Cumulocity 0:099f76422485 170 uint8_t MbedClient::awaitResponse()
Cumulocity 0:099f76422485 171 {
Cumulocity 8:3a4dba260b71 172 uint8_t status;
Cumulocity 8:3a4dba260b71 173
Cumulocity 6:cd7ba1ddb664 174 if (_state != STATE_REQ_COMPLETE)
Cumulocity 6:cd7ba1ddb664 175 return internalError();
Cumulocity 0:099f76422485 176
Cumulocity 8:3a4dba260b71 177 MBCL_DBG("Awaiting response...");
Cumulocity 8:3a4dba260b71 178
Cumulocity 8:3a4dba260b71 179 status = _filter.readStatus();
Cumulocity 8:3a4dba260b71 180 MBCL_DBG("Status code: %u", status);
Cumulocity 8:3a4dba260b71 181
Cumulocity 8:3a4dba260b71 182 if ((status != 200) || (!_filter.skipHeaders()))
Cumulocity 6:cd7ba1ddb664 183 return connectionError();
Cumulocity 11:e1bee9a77652 184
Cumulocity 11:e1bee9a77652 185 // set timeout to fifteen minutes if stream request flag set
Cumulocity 11:e1bee9a77652 186 if (_isStreamRequest)
Cumulocity 11:e1bee9a77652 187 _source.setTimeout(900000);
Cumulocity 0:099f76422485 188
Cumulocity 6:cd7ba1ddb664 189 _state = STATE_RECVD_RESPONSE;
Cumulocity 0:099f76422485 190 return CLIENT_OK;
Cumulocity 0:099f76422485 191 }
Cumulocity 0:099f76422485 192
Cumulocity 0:099f76422485 193 AbstractDataSource& MbedClient::receiveData()
Cumulocity 0:099f76422485 194 {
Cumulocity 0:099f76422485 195 return _filter;
Cumulocity 0:099f76422485 196 }
Cumulocity 0:099f76422485 197
Cumulocity 0:099f76422485 198 void MbedClient::stop()
Cumulocity 0:099f76422485 199 {
Cumulocity 8:3a4dba260b71 200 MBCL_DBG("Resetting client.");
Cumulocity 11:e1bee9a77652 201 _isStreamRequest = false;
Cumulocity 0:099f76422485 202 _sock.close();
Cumulocity 0:099f76422485 203 _source.reset();
Cumulocity 0:099f76422485 204 _sink.reset();
Cumulocity 0:099f76422485 205 _filter.reset();
Cumulocity 6:cd7ba1ddb664 206 _state = STATE_INIT;
Cumulocity 0:099f76422485 207 }
Cumulocity 0:099f76422485 208
Cumulocity 11:e1bee9a77652 209 bool MbedClient::connect()
Cumulocity 11:e1bee9a77652 210 {
Cumulocity 11:e1bee9a77652 211 uint8_t tries;
Cumulocity 11:e1bee9a77652 212
Cumulocity 11:e1bee9a77652 213 tries = _tries;
Cumulocity 11:e1bee9a77652 214 do {
Cumulocity 11:e1bee9a77652 215 MBCL_DBG("Connecting to %s:%u", _host, _port);
Cumulocity 11:e1bee9a77652 216 if (_sock.connect(_host, _port) >= 0)
Cumulocity 11:e1bee9a77652 217 break;
Cumulocity 11:e1bee9a77652 218 _sock.close();
Cumulocity 11:e1bee9a77652 219 MBCL_DBG("Connection atempt failed.");
Cumulocity 11:e1bee9a77652 220 } while (--tries > 0);
Cumulocity 11:e1bee9a77652 221
Cumulocity 11:e1bee9a77652 222 return (tries > 0);
Cumulocity 11:e1bee9a77652 223 }
Cumulocity 11:e1bee9a77652 224
Cumulocity 0:099f76422485 225 bool MbedClient::send(const char *str)
Cumulocity 0:099f76422485 226 {
Cumulocity 7:8159a2d12e4e 227 return (_sink.write(str) == strlen(str));
Cumulocity 0:099f76422485 228 }
Cumulocity 0:099f76422485 229
Cumulocity 11:e1bee9a77652 230 bool MbedClient::sendRequestHeader(const char *uri)
Cumulocity 11:e1bee9a77652 231 {
Cumulocity 11:e1bee9a77652 232 MBCL_DBG("Sending request header.");
Cumulocity 11:e1bee9a77652 233 if ((!send("POST ")) ||
Cumulocity 11:e1bee9a77652 234 (!send(uri)) ||
Cumulocity 12:6634f9814235 235 (!send(" HTTP/1.0\r\n")) ||
Cumulocity 11:e1bee9a77652 236 (!send("Host: ")) ||
Cumulocity 11:e1bee9a77652 237 (!send(_host)) ||
Cumulocity 11:e1bee9a77652 238 (!send("\r\n")))
Cumulocity 11:e1bee9a77652 239 return false;
Cumulocity 11:e1bee9a77652 240
Cumulocity 11:e1bee9a77652 241 return sendBasicAuth();
Cumulocity 11:e1bee9a77652 242 }
Cumulocity 11:e1bee9a77652 243
Cumulocity 0:099f76422485 244 bool MbedClient::sendBasicAuth()
Cumulocity 0:099f76422485 245 {
Cumulocity 0:099f76422485 246 size_t ul, pl; unsigned char input[3]; unsigned char output[5];
Cumulocity 0:099f76422485 247 int inputOffset = 0;
Cumulocity 0:099f76422485 248
Cumulocity 11:e1bee9a77652 249 // no need to send authorization if not specified
Cumulocity 11:e1bee9a77652 250 if ((_username == NULL) || (strlen(_username) == 0) ||
Cumulocity 11:e1bee9a77652 251 (_password == NULL) || (strlen(_password) == 0))
Cumulocity 11:e1bee9a77652 252 return true;
Cumulocity 11:e1bee9a77652 253
Cumulocity 0:099f76422485 254 if (!send("Authorization: Basic "))
Cumulocity 0:099f76422485 255 return false;
Cumulocity 0:099f76422485 256
Cumulocity 0:099f76422485 257 ul = strlen(_username);
Cumulocity 0:099f76422485 258 pl = strlen(_password);
Cumulocity 0:099f76422485 259
Cumulocity 0:099f76422485 260 for (int i = 0; i < (ul+1+pl); i++) {
Cumulocity 0:099f76422485 261 if (i < ul)
Cumulocity 0:099f76422485 262 input[inputOffset++] = _username[i];
Cumulocity 0:099f76422485 263 else if (i == ul)
Cumulocity 0:099f76422485 264 input[inputOffset++] = ':';
Cumulocity 0:099f76422485 265 else
Cumulocity 0:099f76422485 266 input[inputOffset++] = _password[i-(ul+1)];
Cumulocity 0:099f76422485 267
Cumulocity 0:099f76422485 268 if ((inputOffset == 3) || (i == ul+pl)) {
Cumulocity 0:099f76422485 269 b64_encode(input, inputOffset, output, 4);
Cumulocity 0:099f76422485 270 output[4] = '\0';
Cumulocity 0:099f76422485 271 if (!send((char*)output))
Cumulocity 0:099f76422485 272 return false;
Cumulocity 0:099f76422485 273 inputOffset = 0;
Cumulocity 0:099f76422485 274 }
Cumulocity 0:099f76422485 275 }
Cumulocity 0:099f76422485 276
Cumulocity 0:099f76422485 277 if (!send("\r\n"))
Cumulocity 0:099f76422485 278 return false;
Cumulocity 0:099f76422485 279 return true;
Cumulocity 0:099f76422485 280 }
Cumulocity 6:cd7ba1ddb664 281
Cumulocity 6:cd7ba1ddb664 282 uint8_t MbedClient::internalError()
Cumulocity 6:cd7ba1ddb664 283 {
Cumulocity 8:3a4dba260b71 284 MBCL_DBG("Internal error occurred.");
Cumulocity 6:cd7ba1ddb664 285 _state = STATE_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 286 return CLIENT_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 287 }
Cumulocity 6:cd7ba1ddb664 288
Cumulocity 6:cd7ba1ddb664 289 uint8_t MbedClient::connectionError()
Cumulocity 6:cd7ba1ddb664 290 {
Cumulocity 8:3a4dba260b71 291 MBCL_DBG("Connection error occurred.");
Cumulocity 6:cd7ba1ddb664 292 _state = STATE_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 293 return CLIENT_CONNECTION_ERROR;
Cumulocity 6:cd7ba1ddb664 294 }
Cumulocity 6:cd7ba1ddb664 295