Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Mon Feb 16 09:15:55 2015 +0000
Revision:
14:56478403e340
Parent:
12:6634f9814235
Child:
17:b3a4b4bdfc59
Various minor bug fixes

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 6:cd7ba1ddb664 122 _state = STATE_SENT_ID;
Cumulocity 0:099f76422485 123 return CLIENT_OK;
Cumulocity 0:099f76422485 124 }
Cumulocity 0:099f76422485 125
Cumulocity 1:9a11a331e340 126 uint8_t MbedClient::sendData(const DataGenerator& generator)
Cumulocity 0:099f76422485 127 {
Cumulocity 7:8159a2d12e4e 128 size_t len;
Cumulocity 0:099f76422485 129
Cumulocity 6:cd7ba1ddb664 130 if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
Cumulocity 6:cd7ba1ddb664 131 return internalError();
Cumulocity 0:099f76422485 132
Cumulocity 8:3a4dba260b71 133 MBCL_DBG("Sending request payload.");
Cumulocity 0:099f76422485 134 len = generator.writtenLength();
Cumulocity 0:099f76422485 135 if ((!send("Content-Length: ")) ||
Cumulocity 7:8159a2d12e4e 136 (_sink.write((unsigned long)len) == 0) ||
Cumulocity 0:099f76422485 137 (!send("\r\n\r\n")))
Cumulocity 6:cd7ba1ddb664 138 return connectionError();
Cumulocity 0:099f76422485 139
Cumulocity 6:cd7ba1ddb664 140 if (generator.writeTo(_sink) != len)
Cumulocity 6:cd7ba1ddb664 141 return connectionError();
Cumulocity 6:cd7ba1ddb664 142 _state = STATE_SENT_DATA;
Cumulocity 0:099f76422485 143 return CLIENT_OK;
Cumulocity 0:099f76422485 144 }
Cumulocity 0:099f76422485 145
Cumulocity 0:099f76422485 146 uint8_t MbedClient::endRequest()
Cumulocity 0:099f76422485 147 {
Cumulocity 6:cd7ba1ddb664 148 if ((_state != STATE_IN_REQUEST) &&
Cumulocity 6:cd7ba1ddb664 149 (_state != STATE_SENT_ID) &&
Cumulocity 6:cd7ba1ddb664 150 (_state != STATE_SENT_DATA))
Cumulocity 6:cd7ba1ddb664 151 return internalError();
Cumulocity 0:099f76422485 152
Cumulocity 12:6634f9814235 153 MBCL_DBG("Ending request.");
Cumulocity 8:3a4dba260b71 154
Cumulocity 6:cd7ba1ddb664 155 if (_state != STATE_SENT_DATA) {
Cumulocity 0:099f76422485 156 // send end of headers
Cumulocity 0:099f76422485 157 if (!send("\r\n"))
Cumulocity 6:cd7ba1ddb664 158 return connectionError();
Cumulocity 0:099f76422485 159 }
Cumulocity 0:099f76422485 160
Cumulocity 6:cd7ba1ddb664 161 if (!_sink.flush())
Cumulocity 6:cd7ba1ddb664 162 return connectionError();
Cumulocity 0:099f76422485 163
Cumulocity 6:cd7ba1ddb664 164 _state = STATE_REQ_COMPLETE;
Cumulocity 0:099f76422485 165 return CLIENT_OK;
Cumulocity 0:099f76422485 166 }
Cumulocity 0:099f76422485 167
Cumulocity 0:099f76422485 168 uint8_t MbedClient::awaitResponse()
Cumulocity 0:099f76422485 169 {
Cumulocity 8:3a4dba260b71 170 uint8_t status;
Cumulocity 8:3a4dba260b71 171
Cumulocity 6:cd7ba1ddb664 172 if (_state != STATE_REQ_COMPLETE)
Cumulocity 6:cd7ba1ddb664 173 return internalError();
Cumulocity 0:099f76422485 174
Cumulocity 8:3a4dba260b71 175 MBCL_DBG("Awaiting response...");
Cumulocity 8:3a4dba260b71 176
Cumulocity 8:3a4dba260b71 177 status = _filter.readStatus();
Cumulocity 8:3a4dba260b71 178 MBCL_DBG("Status code: %u", status);
Cumulocity 8:3a4dba260b71 179
Cumulocity 8:3a4dba260b71 180 if ((status != 200) || (!_filter.skipHeaders()))
Cumulocity 6:cd7ba1ddb664 181 return connectionError();
Cumulocity 11:e1bee9a77652 182
Cumulocity 11:e1bee9a77652 183 // set timeout to fifteen minutes if stream request flag set
Cumulocity 11:e1bee9a77652 184 if (_isStreamRequest)
Cumulocity 11:e1bee9a77652 185 _source.setTimeout(900000);
Cumulocity 0:099f76422485 186
Cumulocity 6:cd7ba1ddb664 187 _state = STATE_RECVD_RESPONSE;
Cumulocity 0:099f76422485 188 return CLIENT_OK;
Cumulocity 0:099f76422485 189 }
Cumulocity 0:099f76422485 190
Cumulocity 0:099f76422485 191 AbstractDataSource& MbedClient::receiveData()
Cumulocity 0:099f76422485 192 {
Cumulocity 0:099f76422485 193 return _filter;
Cumulocity 0:099f76422485 194 }
Cumulocity 0:099f76422485 195
Cumulocity 0:099f76422485 196 void MbedClient::stop()
Cumulocity 0:099f76422485 197 {
Cumulocity 8:3a4dba260b71 198 MBCL_DBG("Resetting client.");
xinlei 14:56478403e340 199 MBCL_DBG("Bytes tramsmitted sofar: %zu", packetSize);
Cumulocity 11:e1bee9a77652 200 _isStreamRequest = false;
Cumulocity 0:099f76422485 201 _sock.close();
Cumulocity 0:099f76422485 202 _source.reset();
Cumulocity 0:099f76422485 203 _sink.reset();
Cumulocity 0:099f76422485 204 _filter.reset();
Cumulocity 6:cd7ba1ddb664 205 _state = STATE_INIT;
Cumulocity 0:099f76422485 206 }
Cumulocity 0:099f76422485 207
Cumulocity 11:e1bee9a77652 208 bool MbedClient::connect()
Cumulocity 11:e1bee9a77652 209 {
Cumulocity 11:e1bee9a77652 210 uint8_t tries;
Cumulocity 11:e1bee9a77652 211
Cumulocity 11:e1bee9a77652 212 tries = _tries;
Cumulocity 11:e1bee9a77652 213 do {
Cumulocity 11:e1bee9a77652 214 MBCL_DBG("Connecting to %s:%u", _host, _port);
Cumulocity 11:e1bee9a77652 215 if (_sock.connect(_host, _port) >= 0)
Cumulocity 11:e1bee9a77652 216 break;
Cumulocity 11:e1bee9a77652 217 _sock.close();
Cumulocity 11:e1bee9a77652 218 MBCL_DBG("Connection atempt failed.");
Cumulocity 11:e1bee9a77652 219 } while (--tries > 0);
Cumulocity 11:e1bee9a77652 220
Cumulocity 11:e1bee9a77652 221 return (tries > 0);
Cumulocity 11:e1bee9a77652 222 }
Cumulocity 11:e1bee9a77652 223
Cumulocity 0:099f76422485 224 bool MbedClient::send(const char *str)
Cumulocity 0:099f76422485 225 {
Cumulocity 7:8159a2d12e4e 226 return (_sink.write(str) == strlen(str));
Cumulocity 0:099f76422485 227 }
Cumulocity 0:099f76422485 228
Cumulocity 11:e1bee9a77652 229 bool MbedClient::sendRequestHeader(const char *uri)
Cumulocity 11:e1bee9a77652 230 {
Cumulocity 11:e1bee9a77652 231 MBCL_DBG("Sending request header.");
Cumulocity 11:e1bee9a77652 232 if ((!send("POST ")) ||
Cumulocity 11:e1bee9a77652 233 (!send(uri)) ||
Cumulocity 12:6634f9814235 234 (!send(" HTTP/1.0\r\n")) ||
Cumulocity 11:e1bee9a77652 235 (!send("Host: ")) ||
Cumulocity 11:e1bee9a77652 236 (!send(_host)) ||
Cumulocity 11:e1bee9a77652 237 (!send("\r\n")))
Cumulocity 11:e1bee9a77652 238 return false;
Cumulocity 11:e1bee9a77652 239
Cumulocity 11:e1bee9a77652 240 return sendBasicAuth();
Cumulocity 11:e1bee9a77652 241 }
Cumulocity 11:e1bee9a77652 242
Cumulocity 0:099f76422485 243 bool MbedClient::sendBasicAuth()
Cumulocity 0:099f76422485 244 {
Cumulocity 0:099f76422485 245 size_t ul, pl; unsigned char input[3]; unsigned char output[5];
Cumulocity 0:099f76422485 246 int inputOffset = 0;
Cumulocity 0:099f76422485 247
Cumulocity 11:e1bee9a77652 248 // no need to send authorization if not specified
Cumulocity 11:e1bee9a77652 249 if ((_username == NULL) || (strlen(_username) == 0) ||
Cumulocity 11:e1bee9a77652 250 (_password == NULL) || (strlen(_password) == 0))
Cumulocity 11:e1bee9a77652 251 return true;
Cumulocity 11:e1bee9a77652 252
Cumulocity 0:099f76422485 253 if (!send("Authorization: Basic "))
Cumulocity 0:099f76422485 254 return false;
Cumulocity 0:099f76422485 255
Cumulocity 0:099f76422485 256 ul = strlen(_username);
Cumulocity 0:099f76422485 257 pl = strlen(_password);
Cumulocity 0:099f76422485 258
Cumulocity 0:099f76422485 259 for (int i = 0; i < (ul+1+pl); i++) {
Cumulocity 0:099f76422485 260 if (i < ul)
Cumulocity 0:099f76422485 261 input[inputOffset++] = _username[i];
Cumulocity 0:099f76422485 262 else if (i == ul)
Cumulocity 0:099f76422485 263 input[inputOffset++] = ':';
Cumulocity 0:099f76422485 264 else
Cumulocity 0:099f76422485 265 input[inputOffset++] = _password[i-(ul+1)];
Cumulocity 0:099f76422485 266
Cumulocity 0:099f76422485 267 if ((inputOffset == 3) || (i == ul+pl)) {
Cumulocity 0:099f76422485 268 b64_encode(input, inputOffset, output, 4);
Cumulocity 0:099f76422485 269 output[4] = '\0';
Cumulocity 0:099f76422485 270 if (!send((char*)output))
Cumulocity 0:099f76422485 271 return false;
Cumulocity 0:099f76422485 272 inputOffset = 0;
Cumulocity 0:099f76422485 273 }
Cumulocity 0:099f76422485 274 }
Cumulocity 0:099f76422485 275
Cumulocity 0:099f76422485 276 if (!send("\r\n"))
Cumulocity 0:099f76422485 277 return false;
Cumulocity 0:099f76422485 278 return true;
Cumulocity 0:099f76422485 279 }
Cumulocity 6:cd7ba1ddb664 280
Cumulocity 6:cd7ba1ddb664 281 uint8_t MbedClient::internalError()
Cumulocity 6:cd7ba1ddb664 282 {
Cumulocity 8:3a4dba260b71 283 MBCL_DBG("Internal error occurred.");
Cumulocity 6:cd7ba1ddb664 284 _state = STATE_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 285 return CLIENT_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 286 }
Cumulocity 6:cd7ba1ddb664 287
Cumulocity 6:cd7ba1ddb664 288 uint8_t MbedClient::connectionError()
Cumulocity 6:cd7ba1ddb664 289 {
Cumulocity 8:3a4dba260b71 290 MBCL_DBG("Connection error occurred.");
Cumulocity 6:cd7ba1ddb664 291 _state = STATE_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 292 return CLIENT_CONNECTION_ERROR;
Cumulocity 6:cd7ba1ddb664 293 }
Cumulocity 6:cd7ba1ddb664 294