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