Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
MbedClient.cpp@8:3a4dba260b71, 2014-10-23 (annotated)
- Committer:
- Cumulocity
- Date:
- Thu Oct 23 13:17:04 2014 +0200
- Revision:
- 8:3a4dba260b71
- Parent:
- 7:8159a2d12e4e
- Child:
- 11:e1bee9a77652
Updated from revision 01696ee8dfde
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:45a6e44a4fb4 | 50 | _filter(_source), |
Cumulocity | 2:45a6e44a4fb4 | 51 | _source(_sock), |
Cumulocity | 2:45a6e44a4fb4 | 52 | _sink(_sock), |
Cumulocity | 2:45a6e44a4fb4 | 53 | _sock() |
Cumulocity | 0:099f76422485 | 54 | { |
Cumulocity | 0:099f76422485 | 55 | } |
Cumulocity | 0:099f76422485 | 56 | |
Cumulocity | 0:099f76422485 | 57 | MbedClient::~MbedClient() |
Cumulocity | 0:099f76422485 | 58 | { |
Cumulocity | 0:099f76422485 | 59 | } |
Cumulocity | 0:099f76422485 | 60 | |
Cumulocity | 2:45a6e44a4fb4 | 61 | uint8_t MbedClient::setAuthorization(const char* username, const char* password) |
Cumulocity | 2:45a6e44a4fb4 | 62 | { |
Cumulocity | 6:cd7ba1ddb664 | 63 | if (_state != STATE_INIT) |
Cumulocity | 6:cd7ba1ddb664 | 64 | return internalError(); |
Cumulocity | 2:45a6e44a4fb4 | 65 | |
Cumulocity | 2:45a6e44a4fb4 | 66 | _username = username; |
Cumulocity | 2:45a6e44a4fb4 | 67 | _password = password; |
Cumulocity | 8:3a4dba260b71 | 68 | MBCL_DBG("Set authorization to %s:%s", username, password); |
Cumulocity | 2:45a6e44a4fb4 | 69 | return CLIENT_OK; |
Cumulocity | 2:45a6e44a4fb4 | 70 | } |
Cumulocity | 2:45a6e44a4fb4 | 71 | |
Cumulocity | 0:099f76422485 | 72 | uint8_t MbedClient::beginRequest() |
Cumulocity | 0:099f76422485 | 73 | { |
Cumulocity | 6:cd7ba1ddb664 | 74 | uint8_t tries; |
Cumulocity | 6:cd7ba1ddb664 | 75 | |
Cumulocity | 6:cd7ba1ddb664 | 76 | if (_state != STATE_INIT) |
Cumulocity | 6:cd7ba1ddb664 | 77 | return internalError(); |
Cumulocity | 0:099f76422485 | 78 | |
Cumulocity | 6:cd7ba1ddb664 | 79 | tries = _tries; |
Cumulocity | 6:cd7ba1ddb664 | 80 | do { |
Cumulocity | 8:3a4dba260b71 | 81 | MBCL_DBG("Connecting to %s:%u", _host, _port); |
Cumulocity | 6:cd7ba1ddb664 | 82 | if (_sock.connect(_host, _port) >= 0) |
Cumulocity | 6:cd7ba1ddb664 | 83 | break; |
Cumulocity | 6:cd7ba1ddb664 | 84 | _sock.close(); |
Cumulocity | 8:3a4dba260b71 | 85 | MBCL_DBG("Connection atempt failed."); |
Cumulocity | 6:cd7ba1ddb664 | 86 | } while (--tries > 0); |
Cumulocity | 6:cd7ba1ddb664 | 87 | |
Cumulocity | 6:cd7ba1ddb664 | 88 | if (tries == 0) |
Cumulocity | 6:cd7ba1ddb664 | 89 | return connectionError(); |
Cumulocity | 0:099f76422485 | 90 | |
Cumulocity | 8:3a4dba260b71 | 91 | MBCL_DBG("Sending request header."); |
Cumulocity | 8:3a4dba260b71 | 92 | |
Cumulocity | 0:099f76422485 | 93 | if ((!send("POST /s HTTP/1.0\r\n")) || |
Cumulocity | 0:099f76422485 | 94 | (!send("Host: ")) || |
Cumulocity | 0:099f76422485 | 95 | (!send(_host)) || |
Cumulocity | 0:099f76422485 | 96 | (!send("\r\n"))) |
Cumulocity | 6:cd7ba1ddb664 | 97 | return connectionError(); |
Cumulocity | 0:099f76422485 | 98 | |
Cumulocity | 2:45a6e44a4fb4 | 99 | if ((_username != NULL) && (strlen(_username) > 0) && |
Cumulocity | 2:45a6e44a4fb4 | 100 | (_password != NULL) && (strlen(_password) > 0)) { |
Cumulocity | 0:099f76422485 | 101 | if (!sendBasicAuth()) |
Cumulocity | 6:cd7ba1ddb664 | 102 | return connectionError(); |
Cumulocity | 0:099f76422485 | 103 | } |
Cumulocity | 0:099f76422485 | 104 | |
Cumulocity | 6:cd7ba1ddb664 | 105 | _state = STATE_IN_REQUEST; |
Cumulocity | 0:099f76422485 | 106 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 107 | } |
Cumulocity | 0:099f76422485 | 108 | |
Cumulocity | 0:099f76422485 | 109 | uint8_t MbedClient::sendIdentifier(const char* identifier) |
Cumulocity | 0:099f76422485 | 110 | { |
Cumulocity | 6:cd7ba1ddb664 | 111 | if (_state != STATE_IN_REQUEST) |
Cumulocity | 6:cd7ba1ddb664 | 112 | return internalError(); |
Cumulocity | 0:099f76422485 | 113 | |
Cumulocity | 8:3a4dba260b71 | 114 | MBCL_DBG("Sending template identifier."); |
Cumulocity | 0:099f76422485 | 115 | if ((identifier != NULL) && (strlen(identifier) != 0)) { |
Cumulocity | 0:099f76422485 | 116 | if ((!send("X-Id: ")) || |
Cumulocity | 0:099f76422485 | 117 | (!send(identifier)) || |
Cumulocity | 0:099f76422485 | 118 | (!send("\r\n"))) |
Cumulocity | 6:cd7ba1ddb664 | 119 | return connectionError(); |
Cumulocity | 0:099f76422485 | 120 | } |
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 | 0:099f76422485 | 142 | |
Cumulocity | 6:cd7ba1ddb664 | 143 | _state = STATE_SENT_DATA; |
Cumulocity | 0:099f76422485 | 144 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 145 | } |
Cumulocity | 0:099f76422485 | 146 | |
Cumulocity | 0:099f76422485 | 147 | uint8_t MbedClient::endRequest() |
Cumulocity | 0:099f76422485 | 148 | { |
Cumulocity | 6:cd7ba1ddb664 | 149 | if ((_state != STATE_IN_REQUEST) && |
Cumulocity | 6:cd7ba1ddb664 | 150 | (_state != STATE_SENT_ID) && |
Cumulocity | 6:cd7ba1ddb664 | 151 | (_state != STATE_SENT_DATA)) |
Cumulocity | 6:cd7ba1ddb664 | 152 | return internalError(); |
Cumulocity | 0:099f76422485 | 153 | |
Cumulocity | 8:3a4dba260b71 | 154 | MBCL_DBG("Ending request. request."); |
Cumulocity | 8:3a4dba260b71 | 155 | |
Cumulocity | 6:cd7ba1ddb664 | 156 | if (_state != STATE_SENT_DATA) { |
Cumulocity | 0:099f76422485 | 157 | // send end of headers |
Cumulocity | 0:099f76422485 | 158 | if (!send("\r\n")) |
Cumulocity | 6:cd7ba1ddb664 | 159 | return connectionError(); |
Cumulocity | 0:099f76422485 | 160 | } |
Cumulocity | 0:099f76422485 | 161 | |
Cumulocity | 6:cd7ba1ddb664 | 162 | if (!_sink.flush()) |
Cumulocity | 6:cd7ba1ddb664 | 163 | return connectionError(); |
Cumulocity | 0:099f76422485 | 164 | |
Cumulocity | 6:cd7ba1ddb664 | 165 | _state = STATE_REQ_COMPLETE; |
Cumulocity | 0:099f76422485 | 166 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 167 | } |
Cumulocity | 0:099f76422485 | 168 | |
Cumulocity | 0:099f76422485 | 169 | uint8_t MbedClient::awaitResponse() |
Cumulocity | 0:099f76422485 | 170 | { |
Cumulocity | 8:3a4dba260b71 | 171 | uint8_t status; |
Cumulocity | 8:3a4dba260b71 | 172 | |
Cumulocity | 6:cd7ba1ddb664 | 173 | if (_state != STATE_REQ_COMPLETE) |
Cumulocity | 6:cd7ba1ddb664 | 174 | return internalError(); |
Cumulocity | 0:099f76422485 | 175 | |
Cumulocity | 8:3a4dba260b71 | 176 | MBCL_DBG("Awaiting response..."); |
Cumulocity | 8:3a4dba260b71 | 177 | |
Cumulocity | 8:3a4dba260b71 | 178 | status = _filter.readStatus(); |
Cumulocity | 8:3a4dba260b71 | 179 | MBCL_DBG("Status code: %u", status); |
Cumulocity | 8:3a4dba260b71 | 180 | |
Cumulocity | 8:3a4dba260b71 | 181 | if ((status != 200) || (!_filter.skipHeaders())) |
Cumulocity | 6:cd7ba1ddb664 | 182 | return connectionError(); |
Cumulocity | 0:099f76422485 | 183 | |
Cumulocity | 6:cd7ba1ddb664 | 184 | _state = STATE_RECVD_RESPONSE; |
Cumulocity | 0:099f76422485 | 185 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 186 | } |
Cumulocity | 0:099f76422485 | 187 | |
Cumulocity | 0:099f76422485 | 188 | AbstractDataSource& MbedClient::receiveData() |
Cumulocity | 0:099f76422485 | 189 | { |
Cumulocity | 0:099f76422485 | 190 | return _filter; |
Cumulocity | 0:099f76422485 | 191 | } |
Cumulocity | 0:099f76422485 | 192 | |
Cumulocity | 0:099f76422485 | 193 | void MbedClient::stop() |
Cumulocity | 0:099f76422485 | 194 | { |
Cumulocity | 8:3a4dba260b71 | 195 | MBCL_DBG("Resetting client."); |
Cumulocity | 0:099f76422485 | 196 | _sock.close(); |
Cumulocity | 0:099f76422485 | 197 | _source.reset(); |
Cumulocity | 0:099f76422485 | 198 | _sink.reset(); |
Cumulocity | 0:099f76422485 | 199 | _filter.reset(); |
Cumulocity | 6:cd7ba1ddb664 | 200 | _state = STATE_INIT; |
Cumulocity | 0:099f76422485 | 201 | } |
Cumulocity | 0:099f76422485 | 202 | |
Cumulocity | 0:099f76422485 | 203 | bool MbedClient::send(const char *str) |
Cumulocity | 0:099f76422485 | 204 | { |
Cumulocity | 7:8159a2d12e4e | 205 | return (_sink.write(str) == strlen(str)); |
Cumulocity | 0:099f76422485 | 206 | } |
Cumulocity | 0:099f76422485 | 207 | |
Cumulocity | 0:099f76422485 | 208 | bool MbedClient::sendBasicAuth() |
Cumulocity | 0:099f76422485 | 209 | { |
Cumulocity | 0:099f76422485 | 210 | size_t ul, pl; unsigned char input[3]; unsigned char output[5]; |
Cumulocity | 0:099f76422485 | 211 | int inputOffset = 0; |
Cumulocity | 0:099f76422485 | 212 | |
Cumulocity | 0:099f76422485 | 213 | if (!send("Authorization: Basic ")) |
Cumulocity | 0:099f76422485 | 214 | return false; |
Cumulocity | 0:099f76422485 | 215 | |
Cumulocity | 0:099f76422485 | 216 | ul = strlen(_username); |
Cumulocity | 0:099f76422485 | 217 | pl = strlen(_password); |
Cumulocity | 0:099f76422485 | 218 | |
Cumulocity | 0:099f76422485 | 219 | for (int i = 0; i < (ul+1+pl); i++) { |
Cumulocity | 0:099f76422485 | 220 | if (i < ul) |
Cumulocity | 0:099f76422485 | 221 | input[inputOffset++] = _username[i]; |
Cumulocity | 0:099f76422485 | 222 | else if (i == ul) |
Cumulocity | 0:099f76422485 | 223 | input[inputOffset++] = ':'; |
Cumulocity | 0:099f76422485 | 224 | else |
Cumulocity | 0:099f76422485 | 225 | input[inputOffset++] = _password[i-(ul+1)]; |
Cumulocity | 0:099f76422485 | 226 | |
Cumulocity | 0:099f76422485 | 227 | if ((inputOffset == 3) || (i == ul+pl)) { |
Cumulocity | 0:099f76422485 | 228 | b64_encode(input, inputOffset, output, 4); |
Cumulocity | 0:099f76422485 | 229 | output[4] = '\0'; |
Cumulocity | 0:099f76422485 | 230 | if (!send((char*)output)) |
Cumulocity | 0:099f76422485 | 231 | return false; |
Cumulocity | 0:099f76422485 | 232 | inputOffset = 0; |
Cumulocity | 0:099f76422485 | 233 | } |
Cumulocity | 0:099f76422485 | 234 | } |
Cumulocity | 0:099f76422485 | 235 | |
Cumulocity | 0:099f76422485 | 236 | if (!send("\r\n")) |
Cumulocity | 0:099f76422485 | 237 | return false; |
Cumulocity | 0:099f76422485 | 238 | return true; |
Cumulocity | 0:099f76422485 | 239 | } |
Cumulocity | 6:cd7ba1ddb664 | 240 | |
Cumulocity | 6:cd7ba1ddb664 | 241 | uint8_t MbedClient::internalError() |
Cumulocity | 6:cd7ba1ddb664 | 242 | { |
Cumulocity | 8:3a4dba260b71 | 243 | MBCL_DBG("Internal error occurred."); |
Cumulocity | 6:cd7ba1ddb664 | 244 | _state = STATE_INTERNAL_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 245 | return CLIENT_INTERNAL_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 246 | } |
Cumulocity | 6:cd7ba1ddb664 | 247 | |
Cumulocity | 6:cd7ba1ddb664 | 248 | uint8_t MbedClient::connectionError() |
Cumulocity | 6:cd7ba1ddb664 | 249 | { |
Cumulocity | 8:3a4dba260b71 | 250 | MBCL_DBG("Connection error occurred."); |
Cumulocity | 6:cd7ba1ddb664 | 251 | _state = STATE_INTERNAL_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 252 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 253 | } |
Cumulocity | 6:cd7ba1ddb664 | 254 |