Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
xinlei
Date:
Fri Mar 20 14:26:52 2015 +0000
Revision:
19:81dfc04ce0bb
Parent:
18:16192696c106
Child:
20:505d29d5bdfc
Get rid of all annoying warning messages

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),
xinlei 19:81dfc04ce0bb 55 _sock(),
Cumulocity 2:45a6e44a4fb4 56 _source(_sock),
Cumulocity 2:45a6e44a4fb4 57 _sink(_sock),
xinlei 19:81dfc04ce0bb 58 _filter(_source),
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) &&
xinlei 18:16192696c106 156 (_state != STATE_SENT_DATA)) {
Cumulocity 6:cd7ba1ddb664 157 return internalError();
xinlei 18:16192696c106 158 }
Cumulocity 0:099f76422485 159
xinlei 17:b3a4b4bdfc59 160 MBCL_DBG("\033[32mMbedClient:\033[39m Ending request.\r\n");
Cumulocity 8:3a4dba260b71 161
Cumulocity 6:cd7ba1ddb664 162 if (_state != STATE_SENT_DATA) {
Cumulocity 0:099f76422485 163 // send end of headers
xinlei 18:16192696c106 164 if (!send("\r\n")) {
Cumulocity 6:cd7ba1ddb664 165 return connectionError();
xinlei 18:16192696c106 166 }
Cumulocity 0:099f76422485 167 }
Cumulocity 0:099f76422485 168
xinlei 18:16192696c106 169 if (!_sink.flush()) {
Cumulocity 6:cd7ba1ddb664 170 return connectionError();
xinlei 18:16192696c106 171 }
Cumulocity 0:099f76422485 172
Cumulocity 6:cd7ba1ddb664 173 _state = STATE_REQ_COMPLETE;
Cumulocity 0:099f76422485 174 return CLIENT_OK;
Cumulocity 0:099f76422485 175 }
Cumulocity 0:099f76422485 176
Cumulocity 0:099f76422485 177 uint8_t MbedClient::awaitResponse()
Cumulocity 0:099f76422485 178 {
xinlei 18:16192696c106 179 if (_state != STATE_REQ_COMPLETE) {
Cumulocity 6:cd7ba1ddb664 180 return internalError();
xinlei 18:16192696c106 181 }
Cumulocity 0:099f76422485 182
xinlei 17:b3a4b4bdfc59 183 MBCL_DBG("\033[32mMbedClient:\033[39m Awaiting response...\r\n");
Cumulocity 8:3a4dba260b71 184
xinlei 18:16192696c106 185 uint8_t status = _filter.readStatus();
xinlei 17:b3a4b4bdfc59 186 MBCL_DBG("\033[32mMbedClient:\033[39m Status code: %u\r\n", status);
Cumulocity 8:3a4dba260b71 187
xinlei 18:16192696c106 188 if ((status != 200) || (!_filter.skipHeaders())) {
Cumulocity 6:cd7ba1ddb664 189 return connectionError();
xinlei 18:16192696c106 190 }
xinlei 18:16192696c106 191
Cumulocity 11:e1bee9a77652 192 // set timeout to fifteen minutes if stream request flag set
xinlei 18:16192696c106 193 // if (_isStreamRequest) {
xinlei 18:16192696c106 194 // _source.setTimeout(900000);
xinlei 18:16192696c106 195 // }
Cumulocity 0:099f76422485 196
Cumulocity 6:cd7ba1ddb664 197 _state = STATE_RECVD_RESPONSE;
Cumulocity 0:099f76422485 198 return CLIENT_OK;
Cumulocity 0:099f76422485 199 }
Cumulocity 0:099f76422485 200
Cumulocity 0:099f76422485 201 AbstractDataSource& MbedClient::receiveData()
Cumulocity 0:099f76422485 202 {
Cumulocity 0:099f76422485 203 return _filter;
Cumulocity 0:099f76422485 204 }
Cumulocity 0:099f76422485 205
Cumulocity 0:099f76422485 206 void MbedClient::stop()
Cumulocity 0:099f76422485 207 {
xinlei 17:b3a4b4bdfc59 208 MBCL_DBG("\033[32mMbedClient:\033[39m Resetting client.\r\n");
xinlei 17:b3a4b4bdfc59 209 MBCL_DBG("\033[32mMbedClient:\033[39m Bytes tramsmitted sofar: %zu\r\n", packetSize);
Cumulocity 11:e1bee9a77652 210 _isStreamRequest = false;
Cumulocity 0:099f76422485 211 _sock.close();
Cumulocity 0:099f76422485 212 _source.reset();
Cumulocity 0:099f76422485 213 _sink.reset();
Cumulocity 0:099f76422485 214 _filter.reset();
Cumulocity 6:cd7ba1ddb664 215 _state = STATE_INIT;
Cumulocity 0:099f76422485 216 }
Cumulocity 0:099f76422485 217
Cumulocity 11:e1bee9a77652 218 bool MbedClient::connect()
Cumulocity 11:e1bee9a77652 219 {
Cumulocity 11:e1bee9a77652 220 uint8_t tries;
Cumulocity 11:e1bee9a77652 221
Cumulocity 11:e1bee9a77652 222 tries = _tries;
Cumulocity 11:e1bee9a77652 223 do {
xinlei 17:b3a4b4bdfc59 224 if (cachedIPValid == 0) {
xinlei 17:b3a4b4bdfc59 225 MDMParser::IP ip = _mdm.gethostbyname(_host);
xinlei 17:b3a4b4bdfc59 226 if (ip == NOIP)
xinlei 17:b3a4b4bdfc59 227 continue;
xinlei 17:b3a4b4bdfc59 228 const unsigned char *c = (const unsigned char*)&ip;
xinlei 17:b3a4b4bdfc59 229 snprintf(cachedIP, sizeof(cachedIP), "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);
xinlei 17:b3a4b4bdfc59 230 MBCL_DBG("\033[32mMbedClient:\033[39m Connecting to %s:%u with resolved IP %s\r\n", _host, _port, cachedIP);
xinlei 17:b3a4b4bdfc59 231 } else {
xinlei 17:b3a4b4bdfc59 232 MBCL_DBG("\033[32mMbedClient:\033[39m Connecting to %s:%u\r\n", cachedIP, _port);
xinlei 17:b3a4b4bdfc59 233 }
xinlei 17:b3a4b4bdfc59 234 if (_sock.connect(cachedIP, _port) >= 0)
Cumulocity 11:e1bee9a77652 235 break;
xinlei 17:b3a4b4bdfc59 236 cachedIPValid = 0;
Cumulocity 11:e1bee9a77652 237 _sock.close();
xinlei 17:b3a4b4bdfc59 238 MBCL_DBG("\033[32mMbedClient:\033[39m Connection attempt failed.\r\n");
Cumulocity 11:e1bee9a77652 239 } while (--tries > 0);
Cumulocity 11:e1bee9a77652 240
xinlei 17:b3a4b4bdfc59 241 cachedIPValid = (cachedIPValid+1) % DNS_ENTRY_DURATION;
Cumulocity 11:e1bee9a77652 242 return (tries > 0);
Cumulocity 11:e1bee9a77652 243 }
Cumulocity 11:e1bee9a77652 244
Cumulocity 0:099f76422485 245 bool MbedClient::send(const char *str)
Cumulocity 0:099f76422485 246 {
Cumulocity 7:8159a2d12e4e 247 return (_sink.write(str) == strlen(str));
Cumulocity 0:099f76422485 248 }
Cumulocity 0:099f76422485 249
Cumulocity 11:e1bee9a77652 250 bool MbedClient::sendRequestHeader(const char *uri)
Cumulocity 11:e1bee9a77652 251 {
xinlei 17:b3a4b4bdfc59 252 MBCL_DBG("\033[32mMbedClient:\033[39m Sending request header.\r\n");
Cumulocity 11:e1bee9a77652 253 if ((!send("POST ")) ||
Cumulocity 11:e1bee9a77652 254 (!send(uri)) ||
Cumulocity 12:6634f9814235 255 (!send(" HTTP/1.0\r\n")) ||
Cumulocity 11:e1bee9a77652 256 (!send("Host: ")) ||
Cumulocity 11:e1bee9a77652 257 (!send(_host)) ||
Cumulocity 11:e1bee9a77652 258 (!send("\r\n")))
Cumulocity 11:e1bee9a77652 259 return false;
Cumulocity 11:e1bee9a77652 260
Cumulocity 11:e1bee9a77652 261 return sendBasicAuth();
Cumulocity 11:e1bee9a77652 262 }
Cumulocity 11:e1bee9a77652 263
Cumulocity 0:099f76422485 264 bool MbedClient::sendBasicAuth()
Cumulocity 0:099f76422485 265 {
Cumulocity 0:099f76422485 266 size_t ul, pl; unsigned char input[3]; unsigned char output[5];
Cumulocity 0:099f76422485 267 int inputOffset = 0;
Cumulocity 0:099f76422485 268
Cumulocity 11:e1bee9a77652 269 // no need to send authorization if not specified
Cumulocity 11:e1bee9a77652 270 if ((_username == NULL) || (strlen(_username) == 0) ||
Cumulocity 11:e1bee9a77652 271 (_password == NULL) || (strlen(_password) == 0))
Cumulocity 11:e1bee9a77652 272 return true;
Cumulocity 11:e1bee9a77652 273
Cumulocity 0:099f76422485 274 if (!send("Authorization: Basic "))
Cumulocity 0:099f76422485 275 return false;
Cumulocity 0:099f76422485 276
Cumulocity 0:099f76422485 277 ul = strlen(_username);
Cumulocity 0:099f76422485 278 pl = strlen(_password);
Cumulocity 0:099f76422485 279
Cumulocity 0:099f76422485 280 for (int i = 0; i < (ul+1+pl); i++) {
Cumulocity 0:099f76422485 281 if (i < ul)
Cumulocity 0:099f76422485 282 input[inputOffset++] = _username[i];
Cumulocity 0:099f76422485 283 else if (i == ul)
Cumulocity 0:099f76422485 284 input[inputOffset++] = ':';
Cumulocity 0:099f76422485 285 else
Cumulocity 0:099f76422485 286 input[inputOffset++] = _password[i-(ul+1)];
Cumulocity 0:099f76422485 287
Cumulocity 0:099f76422485 288 if ((inputOffset == 3) || (i == ul+pl)) {
Cumulocity 0:099f76422485 289 b64_encode(input, inputOffset, output, 4);
Cumulocity 0:099f76422485 290 output[4] = '\0';
Cumulocity 0:099f76422485 291 if (!send((char*)output))
Cumulocity 0:099f76422485 292 return false;
Cumulocity 0:099f76422485 293 inputOffset = 0;
Cumulocity 0:099f76422485 294 }
Cumulocity 0:099f76422485 295 }
Cumulocity 0:099f76422485 296
Cumulocity 0:099f76422485 297 if (!send("\r\n"))
Cumulocity 0:099f76422485 298 return false;
Cumulocity 0:099f76422485 299 return true;
Cumulocity 0:099f76422485 300 }
Cumulocity 6:cd7ba1ddb664 301
Cumulocity 6:cd7ba1ddb664 302 uint8_t MbedClient::internalError()
Cumulocity 6:cd7ba1ddb664 303 {
xinlei 17:b3a4b4bdfc59 304 MBCL_DBG("\033[32mMbedClient:\033[39m Internal error occurred.\r\n");
Cumulocity 6:cd7ba1ddb664 305 _state = STATE_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 306 return CLIENT_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 307 }
Cumulocity 6:cd7ba1ddb664 308
Cumulocity 6:cd7ba1ddb664 309 uint8_t MbedClient::connectionError()
Cumulocity 6:cd7ba1ddb664 310 {
xinlei 17:b3a4b4bdfc59 311 MBCL_DBG("\033[32mMbedClient:\033[39m Connection error occurred.\r\n");
Cumulocity 6:cd7ba1ddb664 312 _state = STATE_INTERNAL_ERROR;
Cumulocity 6:cd7ba1ddb664 313 return CLIENT_CONNECTION_ERROR;
Cumulocity 6:cd7ba1ddb664 314 }
Cumulocity 6:cd7ba1ddb664 315