mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.
Dependents: MbedSmartRestMain MbedSmartRestMain
MbedClient.cpp@1:9a11a331e340, 2014-07-07 (annotated)
- Committer:
- Cumulocity
- Date:
- Mon Jul 07 16:14:51 2014 +0200
- Revision:
- 1:9a11a331e340
- Parent:
- 0:099f76422485
- Child:
- 2:45a6e44a4fb4
Updated from revision 06e71f31df1c
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 | 0:099f76422485 | 35 | MbedClient::MbedClient(const char* host, uint16_t port, const char* username, const char* password) |
Cumulocity | 0:099f76422485 | 36 | : _host(host), _port(port), _username(username), _password(password), _filter(_source), _source(_sock), _sink(_sock), _sock() |
Cumulocity | 0:099f76422485 | 37 | { |
Cumulocity | 0:099f76422485 | 38 | _state = MBED_STATE_INIT; |
Cumulocity | 0:099f76422485 | 39 | } |
Cumulocity | 0:099f76422485 | 40 | |
Cumulocity | 0:099f76422485 | 41 | MbedClient::~MbedClient() |
Cumulocity | 0:099f76422485 | 42 | { |
Cumulocity | 0:099f76422485 | 43 | } |
Cumulocity | 0:099f76422485 | 44 | |
Cumulocity | 0:099f76422485 | 45 | uint8_t MbedClient::beginRequest() |
Cumulocity | 0:099f76422485 | 46 | { |
Cumulocity | 0:099f76422485 | 47 | if (_state != MBED_STATE_INIT) |
Cumulocity | 0:099f76422485 | 48 | return CLIENT_INTERNAL_ERROR; |
Cumulocity | 0:099f76422485 | 49 | |
Cumulocity | 0:099f76422485 | 50 | if (_sock.connect(_host, _port) < 0) { |
Cumulocity | 0:099f76422485 | 51 | stop(); |
Cumulocity | 0:099f76422485 | 52 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 53 | } |
Cumulocity | 0:099f76422485 | 54 | |
Cumulocity | 0:099f76422485 | 55 | if ((!send("POST /s HTTP/1.0\r\n")) || |
Cumulocity | 0:099f76422485 | 56 | (!send("Host: ")) || |
Cumulocity | 0:099f76422485 | 57 | (!send(_host)) || |
Cumulocity | 0:099f76422485 | 58 | (!send("\r\n"))) |
Cumulocity | 0:099f76422485 | 59 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 60 | |
Cumulocity | 0:099f76422485 | 61 | if ((strlen(_username) > 0) && (strlen(_password) > 0)) { |
Cumulocity | 0:099f76422485 | 62 | if (!sendBasicAuth()) |
Cumulocity | 0:099f76422485 | 63 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 64 | } |
Cumulocity | 0:099f76422485 | 65 | |
Cumulocity | 0:099f76422485 | 66 | _state = MBED_STATE_IN_REQUEST; |
Cumulocity | 0:099f76422485 | 67 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 68 | } |
Cumulocity | 0:099f76422485 | 69 | |
Cumulocity | 0:099f76422485 | 70 | uint8_t MbedClient::sendIdentifier(const char* identifier) |
Cumulocity | 0:099f76422485 | 71 | { |
Cumulocity | 0:099f76422485 | 72 | if (_state != MBED_STATE_IN_REQUEST) |
Cumulocity | 0:099f76422485 | 73 | return CLIENT_INTERNAL_ERROR; |
Cumulocity | 0:099f76422485 | 74 | |
Cumulocity | 0:099f76422485 | 75 | if ((identifier != NULL) && (strlen(identifier) != 0)) { |
Cumulocity | 0:099f76422485 | 76 | if ((!send("X-Id: ")) || |
Cumulocity | 0:099f76422485 | 77 | (!send(identifier)) || |
Cumulocity | 0:099f76422485 | 78 | (!send("\r\n"))) |
Cumulocity | 0:099f76422485 | 79 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 80 | } |
Cumulocity | 0:099f76422485 | 81 | |
Cumulocity | 0:099f76422485 | 82 | _state = MBED_STATE_SENT_ID; |
Cumulocity | 0:099f76422485 | 83 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 84 | } |
Cumulocity | 0:099f76422485 | 85 | |
Cumulocity | 1:9a11a331e340 | 86 | uint8_t MbedClient::sendData(const DataGenerator& generator) |
Cumulocity | 0:099f76422485 | 87 | { |
Cumulocity | 0:099f76422485 | 88 | size_t len; char lenstr[8]; |
Cumulocity | 0:099f76422485 | 89 | |
Cumulocity | 0:099f76422485 | 90 | if ((_state != MBED_STATE_IN_REQUEST) && (_state != MBED_STATE_SENT_ID)) |
Cumulocity | 0:099f76422485 | 91 | return CLIENT_INTERNAL_ERROR; |
Cumulocity | 0:099f76422485 | 92 | |
Cumulocity | 0:099f76422485 | 93 | len = generator.writtenLength(); |
Cumulocity | 0:099f76422485 | 94 | snprintf(lenstr, 8, "%ld", len); |
Cumulocity | 0:099f76422485 | 95 | |
Cumulocity | 0:099f76422485 | 96 | if ((!send("Content-Length: ")) || |
Cumulocity | 0:099f76422485 | 97 | (!send(lenstr)) || |
Cumulocity | 0:099f76422485 | 98 | (!send("\r\n\r\n"))) |
Cumulocity | 0:099f76422485 | 99 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 100 | |
Cumulocity | 0:099f76422485 | 101 | if (generator.writeTo(_sink) != len) { |
Cumulocity | 0:099f76422485 | 102 | stop(); |
Cumulocity | 0:099f76422485 | 103 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 104 | } |
Cumulocity | 0:099f76422485 | 105 | |
Cumulocity | 0:099f76422485 | 106 | _state = MBED_STATE_SENT_DATA; |
Cumulocity | 0:099f76422485 | 107 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 108 | } |
Cumulocity | 0:099f76422485 | 109 | |
Cumulocity | 0:099f76422485 | 110 | uint8_t MbedClient::endRequest() |
Cumulocity | 0:099f76422485 | 111 | { |
Cumulocity | 0:099f76422485 | 112 | if ((_state != MBED_STATE_IN_REQUEST) && (_state != MBED_STATE_SENT_ID) && (_state != MBED_STATE_SENT_DATA)) |
Cumulocity | 0:099f76422485 | 113 | return CLIENT_INTERNAL_ERROR; |
Cumulocity | 0:099f76422485 | 114 | |
Cumulocity | 0:099f76422485 | 115 | if (_state != MBED_STATE_SENT_DATA) { |
Cumulocity | 0:099f76422485 | 116 | // send end of headers |
Cumulocity | 0:099f76422485 | 117 | if (!send("\r\n")) |
Cumulocity | 0:099f76422485 | 118 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 119 | } |
Cumulocity | 0:099f76422485 | 120 | |
Cumulocity | 0:099f76422485 | 121 | if (!_sink.flush()) { |
Cumulocity | 0:099f76422485 | 122 | stop(); |
Cumulocity | 0:099f76422485 | 123 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 124 | } |
Cumulocity | 0:099f76422485 | 125 | |
Cumulocity | 0:099f76422485 | 126 | _state = MBED_STATE_REQ_COMPLETE; |
Cumulocity | 0:099f76422485 | 127 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 128 | } |
Cumulocity | 0:099f76422485 | 129 | |
Cumulocity | 0:099f76422485 | 130 | uint8_t MbedClient::awaitResponse() |
Cumulocity | 0:099f76422485 | 131 | { |
Cumulocity | 0:099f76422485 | 132 | if (_state != MBED_STATE_REQ_COMPLETE) |
Cumulocity | 0:099f76422485 | 133 | return CLIENT_INTERNAL_ERROR; |
Cumulocity | 0:099f76422485 | 134 | |
Cumulocity | 0:099f76422485 | 135 | if ((_filter.readStatus() != 200) || (!_filter.skipHeaders())) { |
Cumulocity | 0:099f76422485 | 136 | stop(); |
Cumulocity | 0:099f76422485 | 137 | return CLIENT_CONNECTION_ERROR; |
Cumulocity | 0:099f76422485 | 138 | } |
Cumulocity | 0:099f76422485 | 139 | |
Cumulocity | 0:099f76422485 | 140 | _state = MBED_STATE_RECVD_RESPONSE; |
Cumulocity | 0:099f76422485 | 141 | return CLIENT_OK; |
Cumulocity | 0:099f76422485 | 142 | } |
Cumulocity | 0:099f76422485 | 143 | |
Cumulocity | 0:099f76422485 | 144 | AbstractDataSource& MbedClient::receiveData() |
Cumulocity | 0:099f76422485 | 145 | { |
Cumulocity | 0:099f76422485 | 146 | return _filter; |
Cumulocity | 0:099f76422485 | 147 | } |
Cumulocity | 0:099f76422485 | 148 | |
Cumulocity | 0:099f76422485 | 149 | void MbedClient::stop() |
Cumulocity | 0:099f76422485 | 150 | { |
Cumulocity | 0:099f76422485 | 151 | _sock.close(); |
Cumulocity | 0:099f76422485 | 152 | _source.reset(); |
Cumulocity | 0:099f76422485 | 153 | _sink.reset(); |
Cumulocity | 0:099f76422485 | 154 | _filter.reset(); |
Cumulocity | 0:099f76422485 | 155 | _state = MBED_STATE_INIT; |
Cumulocity | 0:099f76422485 | 156 | } |
Cumulocity | 0:099f76422485 | 157 | |
Cumulocity | 0:099f76422485 | 158 | bool MbedClient::send(const char *str) |
Cumulocity | 0:099f76422485 | 159 | { |
Cumulocity | 0:099f76422485 | 160 | if (_sink.write(str) != strlen(str)) { |
Cumulocity | 0:099f76422485 | 161 | stop(); |
Cumulocity | 0:099f76422485 | 162 | return false; |
Cumulocity | 0:099f76422485 | 163 | } |
Cumulocity | 0:099f76422485 | 164 | return true; |
Cumulocity | 0:099f76422485 | 165 | } |
Cumulocity | 0:099f76422485 | 166 | |
Cumulocity | 0:099f76422485 | 167 | bool MbedClient::sendBasicAuth() |
Cumulocity | 0:099f76422485 | 168 | { |
Cumulocity | 0:099f76422485 | 169 | size_t ul, pl; unsigned char input[3]; unsigned char output[5]; |
Cumulocity | 0:099f76422485 | 170 | int inputOffset = 0; |
Cumulocity | 0:099f76422485 | 171 | |
Cumulocity | 0:099f76422485 | 172 | if (!send("Authorization: Basic ")) |
Cumulocity | 0:099f76422485 | 173 | return false; |
Cumulocity | 0:099f76422485 | 174 | |
Cumulocity | 0:099f76422485 | 175 | ul = strlen(_username); |
Cumulocity | 0:099f76422485 | 176 | pl = strlen(_password); |
Cumulocity | 0:099f76422485 | 177 | |
Cumulocity | 0:099f76422485 | 178 | for (int i = 0; i < (ul+1+pl); i++) { |
Cumulocity | 0:099f76422485 | 179 | if (i < ul) |
Cumulocity | 0:099f76422485 | 180 | input[inputOffset++] = _username[i]; |
Cumulocity | 0:099f76422485 | 181 | else if (i == ul) |
Cumulocity | 0:099f76422485 | 182 | input[inputOffset++] = ':'; |
Cumulocity | 0:099f76422485 | 183 | else |
Cumulocity | 0:099f76422485 | 184 | input[inputOffset++] = _password[i-(ul+1)]; |
Cumulocity | 0:099f76422485 | 185 | |
Cumulocity | 0:099f76422485 | 186 | if ((inputOffset == 3) || (i == ul+pl)) { |
Cumulocity | 0:099f76422485 | 187 | b64_encode(input, inputOffset, output, 4); |
Cumulocity | 0:099f76422485 | 188 | output[4] = '\0'; |
Cumulocity | 0:099f76422485 | 189 | if (!send((char*)output)) |
Cumulocity | 0:099f76422485 | 190 | return false; |
Cumulocity | 0:099f76422485 | 191 | inputOffset = 0; |
Cumulocity | 0:099f76422485 | 192 | } |
Cumulocity | 0:099f76422485 | 193 | } |
Cumulocity | 0:099f76422485 | 194 | |
Cumulocity | 0:099f76422485 | 195 | if (!send("\r\n")) |
Cumulocity | 0:099f76422485 | 196 | return false; |
Cumulocity | 0:099f76422485 | 197 | return true; |
Cumulocity | 0:099f76422485 | 198 | } |