mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.

Dependents:   MbedSmartRestMain MbedSmartRestMain

Committer:
Cumulocity
Date:
Mon Jul 07 17:08:02 2014 +0200
Revision:
2:45a6e44a4fb4
Parent:
1:9a11a331e340
Child:
6:cd7ba1ddb664
Updated from revision 98c2d7e29dea

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