Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Wed Mar 04 09:35:25 2015 +0000
Revision:
17:b3a4b4bdfc59
Parent:
14:56478403e340
Child:
18:16192696c106
performance: increase data sink and source buffer size to 600, eliminated wait(0.5) when sending data.

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