Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
MbedClient.cpp@6:cd7ba1ddb664, 2014-07-28 (annotated)
- Committer:
- Cumulocity
- Date:
- Mon Jul 28 12:11:02 2014 +0200
- Revision:
- 6:cd7ba1ddb664
- Parent:
- 2:45a6e44a4fb4
- Child:
- 7:8159a2d12e4e
Updated from revision 908949dc5088
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 | 6:cd7ba1ddb664 | 55 | _state = STATE_INIT; |
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 | 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 | 6:cd7ba1ddb664 | 81 | if (_sock.connect(_host, _port) >= 0) |
Cumulocity | 6:cd7ba1ddb664 | 82 | break; |
Cumulocity | 6:cd7ba1ddb664 | 83 | _sock.close(); |
Cumulocity | 6:cd7ba1ddb664 | 84 | } while (--tries > 0); |
Cumulocity | 6:cd7ba1ddb664 | 85 | |
Cumulocity | 6:cd7ba1ddb664 | 86 | if (tries == 0) |
Cumulocity | 6:cd7ba1ddb664 | 87 | return connectionError(); |
Cumulocity | 0:099f76422485 | 88 | |
Cumulocity | 0:099f76422485 | 89 | if ((!send("POST /s HTTP/1.0\r\n")) || |
Cumulocity | 0:099f76422485 | 90 | (!send("Host: ")) || |
Cumulocity | 0:099f76422485 | 91 | (!send(_host)) || |
Cumulocity | 0:099f76422485 | 92 | (!send("\r\n"))) |
Cumulocity | 6:cd7ba1ddb664 | 93 | return connectionError(); |
Cumulocity | 0:099f76422485 | 94 | |
Cumulocity | 2:45a6e44a4fb4 | 95 | if ((_username != NULL) && (strlen(_username) > 0) && |
Cumulocity | 2:45a6e44a4fb4 | 96 | (_password != NULL) && (strlen(_password) > 0)) { |
Cumulocity | 0:099f76422485 | 97 | if (!sendBasicAuth()) |
Cumulocity | 6:cd7ba1ddb664 | 98 | return connectionError(); |
Cumulocity | 0:099f76422485 | 99 | } |
Cumulocity | 0:099f76422485 | 100 | |
Cumulocity | 6:cd7ba1ddb664 | 101 | _state = STATE_IN_REQUEST; |
Cumulocity | 0:099f76422485 | 102 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 103 | } |
Cumulocity | 0:099f76422485 | 104 | |
Cumulocity | 0:099f76422485 | 105 | uint8_t MbedClient::sendIdentifier(const char* identifier) |
Cumulocity | 0:099f76422485 | 106 | { |
Cumulocity | 6:cd7ba1ddb664 | 107 | if (_state != STATE_IN_REQUEST) |
Cumulocity | 6:cd7ba1ddb664 | 108 | return internalError(); |
Cumulocity | 0:099f76422485 | 109 | |
Cumulocity | 0:099f76422485 | 110 | if ((identifier != NULL) && (strlen(identifier) != 0)) { |
Cumulocity | 0:099f76422485 | 111 | if ((!send("X-Id: ")) || |
Cumulocity | 0:099f76422485 | 112 | (!send(identifier)) || |
Cumulocity | 0:099f76422485 | 113 | (!send("\r\n"))) |
Cumulocity | 6:cd7ba1ddb664 | 114 | return connectionError(); |
Cumulocity | 0:099f76422485 | 115 | } |
Cumulocity | 0:099f76422485 | 116 | |
Cumulocity | 6:cd7ba1ddb664 | 117 | _state = STATE_SENT_ID; |
Cumulocity | 0:099f76422485 | 118 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 119 | } |
Cumulocity | 0:099f76422485 | 120 | |
Cumulocity | 1:9a11a331e340 | 121 | uint8_t MbedClient::sendData(const DataGenerator& generator) |
Cumulocity | 0:099f76422485 | 122 | { |
Cumulocity | 0:099f76422485 | 123 | size_t len; char lenstr[8]; |
Cumulocity | 0:099f76422485 | 124 | |
Cumulocity | 6:cd7ba1ddb664 | 125 | if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID)) |
Cumulocity | 6:cd7ba1ddb664 | 126 | return internalError(); |
Cumulocity | 0:099f76422485 | 127 | |
Cumulocity | 0:099f76422485 | 128 | len = generator.writtenLength(); |
Cumulocity | 0:099f76422485 | 129 | snprintf(lenstr, 8, "%ld", len); |
Cumulocity | 0:099f76422485 | 130 | |
Cumulocity | 0:099f76422485 | 131 | if ((!send("Content-Length: ")) || |
Cumulocity | 0:099f76422485 | 132 | (!send(lenstr)) || |
Cumulocity | 0:099f76422485 | 133 | (!send("\r\n\r\n"))) |
Cumulocity | 6:cd7ba1ddb664 | 134 | return connectionError(); |
Cumulocity | 0:099f76422485 | 135 | |
Cumulocity | 6:cd7ba1ddb664 | 136 | if (generator.writeTo(_sink) != len) |
Cumulocity | 6:cd7ba1ddb664 | 137 | return connectionError(); |
Cumulocity | 0:099f76422485 | 138 | |
Cumulocity | 6:cd7ba1ddb664 | 139 | _state = STATE_SENT_DATA; |
Cumulocity | 0:099f76422485 | 140 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 141 | } |
Cumulocity | 0:099f76422485 | 142 | |
Cumulocity | 0:099f76422485 | 143 | uint8_t MbedClient::endRequest() |
Cumulocity | 0:099f76422485 | 144 | { |
Cumulocity | 6:cd7ba1ddb664 | 145 | if ((_state != STATE_IN_REQUEST) && |
Cumulocity | 6:cd7ba1ddb664 | 146 | (_state != STATE_SENT_ID) && |
Cumulocity | 6:cd7ba1ddb664 | 147 | (_state != STATE_SENT_DATA)) |
Cumulocity | 6:cd7ba1ddb664 | 148 | return internalError(); |
Cumulocity | 0:099f76422485 | 149 | |
Cumulocity | 6:cd7ba1ddb664 | 150 | if (_state != STATE_SENT_DATA) { |
Cumulocity | 0:099f76422485 | 151 | // send end of headers |
Cumulocity | 0:099f76422485 | 152 | if (!send("\r\n")) |
Cumulocity | 6:cd7ba1ddb664 | 153 | return connectionError(); |
Cumulocity | 0:099f76422485 | 154 | } |
Cumulocity | 0:099f76422485 | 155 | |
Cumulocity | 6:cd7ba1ddb664 | 156 | if (!_sink.flush()) |
Cumulocity | 6:cd7ba1ddb664 | 157 | return connectionError(); |
Cumulocity | 0:099f76422485 | 158 | |
Cumulocity | 6:cd7ba1ddb664 | 159 | _state = STATE_REQ_COMPLETE; |
Cumulocity | 0:099f76422485 | 160 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 161 | } |
Cumulocity | 0:099f76422485 | 162 | |
Cumulocity | 0:099f76422485 | 163 | uint8_t MbedClient::awaitResponse() |
Cumulocity | 0:099f76422485 | 164 | { |
Cumulocity | 6:cd7ba1ddb664 | 165 | if (_state != STATE_REQ_COMPLETE) |
Cumulocity | 6:cd7ba1ddb664 | 166 | return internalError(); |
Cumulocity | 0:099f76422485 | 167 | |
Cumulocity | 6:cd7ba1ddb664 | 168 | if ((_filter.readStatus() != 200) || (!_filter.skipHeaders())) |
Cumulocity | 6:cd7ba1ddb664 | 169 | return connectionError(); |
Cumulocity | 0:099f76422485 | 170 | |
Cumulocity | 6:cd7ba1ddb664 | 171 | _state = STATE_RECVD_RESPONSE; |
Cumulocity | 0:099f76422485 | 172 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 173 | } |
Cumulocity | 0:099f76422485 | 174 | |
Cumulocity | 0:099f76422485 | 175 | AbstractDataSource& MbedClient::receiveData() |
Cumulocity | 0:099f76422485 | 176 | { |
Cumulocity | 0:099f76422485 | 177 | return _filter; |
Cumulocity | 0:099f76422485 | 178 | } |
Cumulocity | 0:099f76422485 | 179 | |
Cumulocity | 0:099f76422485 | 180 | void MbedClient::stop() |
Cumulocity | 0:099f76422485 | 181 | { |
Cumulocity | 0:099f76422485 | 182 | _sock.close(); |
Cumulocity | 0:099f76422485 | 183 | _source.reset(); |
Cumulocity | 0:099f76422485 | 184 | _sink.reset(); |
Cumulocity | 0:099f76422485 | 185 | _filter.reset(); |
Cumulocity | 6:cd7ba1ddb664 | 186 | _state = STATE_INIT; |
Cumulocity | 0:099f76422485 | 187 | } |
Cumulocity | 0:099f76422485 | 188 | |
Cumulocity | 0:099f76422485 | 189 | bool MbedClient::send(const char *str) |
Cumulocity | 0:099f76422485 | 190 | { |
Cumulocity | 6:cd7ba1ddb664 | 191 | if (_sink.write(str) != strlen(str)) |
Cumulocity | 0:099f76422485 | 192 | return false; |
Cumulocity | 0:099f76422485 | 193 | return true; |
Cumulocity | 0:099f76422485 | 194 | } |
Cumulocity | 0:099f76422485 | 195 | |
Cumulocity | 0:099f76422485 | 196 | bool MbedClient::sendBasicAuth() |
Cumulocity | 0:099f76422485 | 197 | { |
Cumulocity | 0:099f76422485 | 198 | size_t ul, pl; unsigned char input[3]; unsigned char output[5]; |
Cumulocity | 0:099f76422485 | 199 | int inputOffset = 0; |
Cumulocity | 0:099f76422485 | 200 | |
Cumulocity | 0:099f76422485 | 201 | if (!send("Authorization: Basic ")) |
Cumulocity | 0:099f76422485 | 202 | return false; |
Cumulocity | 0:099f76422485 | 203 | |
Cumulocity | 0:099f76422485 | 204 | ul = strlen(_username); |
Cumulocity | 0:099f76422485 | 205 | pl = strlen(_password); |
Cumulocity | 0:099f76422485 | 206 | |
Cumulocity | 0:099f76422485 | 207 | for (int i = 0; i < (ul+1+pl); i++) { |
Cumulocity | 0:099f76422485 | 208 | if (i < ul) |
Cumulocity | 0:099f76422485 | 209 | input[inputOffset++] = _username[i]; |
Cumulocity | 0:099f76422485 | 210 | else if (i == ul) |
Cumulocity | 0:099f76422485 | 211 | input[inputOffset++] = ':'; |
Cumulocity | 0:099f76422485 | 212 | else |
Cumulocity | 0:099f76422485 | 213 | input[inputOffset++] = _password[i-(ul+1)]; |
Cumulocity | 0:099f76422485 | 214 | |
Cumulocity | 0:099f76422485 | 215 | if ((inputOffset == 3) || (i == ul+pl)) { |
Cumulocity | 0:099f76422485 | 216 | b64_encode(input, inputOffset, output, 4); |
Cumulocity | 0:099f76422485 | 217 | output[4] = '\0'; |
Cumulocity | 0:099f76422485 | 218 | if (!send((char*)output)) |
Cumulocity | 0:099f76422485 | 219 | return false; |
Cumulocity | 0:099f76422485 | 220 | inputOffset = 0; |
Cumulocity | 0:099f76422485 | 221 | } |
Cumulocity | 0:099f76422485 | 222 | } |
Cumulocity | 0:099f76422485 | 223 | |
Cumulocity | 0:099f76422485 | 224 | if (!send("\r\n")) |
Cumulocity | 0:099f76422485 | 225 | return false; |
Cumulocity | 0:099f76422485 | 226 | return true; |
Cumulocity | 0:099f76422485 | 227 | } |
Cumulocity | 6:cd7ba1ddb664 | 228 | |
Cumulocity | 6:cd7ba1ddb664 | 229 | uint8_t MbedClient::internalError() |
Cumulocity | 6:cd7ba1ddb664 | 230 | { |
Cumulocity | 6:cd7ba1ddb664 | 231 | _state = STATE_INTERNAL_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 232 | return CLIENT_INTERNAL_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 233 | } |
Cumulocity | 6:cd7ba1ddb664 | 234 | |
Cumulocity | 6:cd7ba1ddb664 | 235 | uint8_t MbedClient::connectionError() |
Cumulocity | 6:cd7ba1ddb664 | 236 | { |
Cumulocity | 6:cd7ba1ddb664 | 237 | _state = STATE_INTERNAL_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 238 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 6:cd7ba1ddb664 | 239 | } |
Cumulocity | 6:cd7ba1ddb664 | 240 |