A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

Committer:
vwochnik
Date:
Thu Jan 30 11:11:39 2014 +0000
Revision:
5:ab909221d22d
Parent:
3:ce2f116369bd
Child:
7:26524a6a04a1
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vwochnik 3:ce2f116369bd 1 #include "MbedClient.h"
vwochnik 5:ab909221d22d 2 #include <stdlib.h>
vwochnik 5:ab909221d22d 3
vwochnik 5:ab909221d22d 4 #define STATE_INIT 0
vwochnik 5:ab909221d22d 5 #define STATE_IN_REQUEST 1
vwochnik 5:ab909221d22d 6 #define STATE_SENT_ID 2
vwochnik 5:ab909221d22d 7 #define STATE_SENT_DATA 3
vwochnik 5:ab909221d22d 8 #define STATE_REQ_COMPLETE 4
vwochnik 5:ab909221d22d 9 #define STATE_RECVD_RESPONSE 5
vwochnik 5:ab909221d22d 10 #define STATE_RECV_DATA 6
vwochnik 5:ab909221d22d 11
vwochnik 5:ab909221d22d 12 const char * const cXidHeader = "X-Id";
vwochnik 3:ce2f116369bd 13
vwochnik 3:ce2f116369bd 14 MbedClient::MbedClient(const char* url, const char* username, const char* password)
vwochnik 3:ce2f116369bd 15 : _url(url), _username(username), _password(password)
vwochnik 3:ce2f116369bd 16 {
vwochnik 5:ab909221d22d 17 _state = STATE_INIT;
vwochnik 5:ab909221d22d 18 _headers[0] = cXidHeader;
vwochnik 5:ab909221d22d 19 _headers[1] = NULL;
vwochnik 5:ab909221d22d 20 }
vwochnik 5:ab909221d22d 21
vwochnik 5:ab909221d22d 22 MbedClient::~MbedClient()
vwochnik 5:ab909221d22d 23 {
vwochnik 5:ab909221d22d 24 if (_generator != NULL)
vwochnik 5:ab909221d22d 25 delete _generator;
vwochnik 3:ce2f116369bd 26 }
vwochnik 3:ce2f116369bd 27
vwochnik 3:ce2f116369bd 28 uint8_t MbedClient::beginRequest()
vwochnik 3:ce2f116369bd 29 {
vwochnik 5:ab909221d22d 30 if (_state != STATE_INIT)
vwochnik 5:ab909221d22d 31 return CLIENT_INTERNAL_ERROR;
vwochnik 5:ab909221d22d 32 _client.basicAuth(_username, _password);
vwochnik 5:ab909221d22d 33 _client.customHeaders(NULL, 0);
vwochnik 5:ab909221d22d 34 _state = STATE_IN_REQUEST;
vwochnik 5:ab909221d22d 35 return CLIENT_OK;
vwochnik 3:ce2f116369bd 36 }
vwochnik 3:ce2f116369bd 37
vwochnik 5:ab909221d22d 38 uint8_t MbedClient::sendIdentifier(const char* identifier)
vwochnik 3:ce2f116369bd 39 {
vwochnik 5:ab909221d22d 40 if (_state != STATE_IN_REQUEST)
vwochnik 5:ab909221d22d 41 return CLIENT_INTERNAL_ERROR;
vwochnik 5:ab909221d22d 42 _headers[1] = identifier;
vwochnik 5:ab909221d22d 43 _client.customHeaders(_headers, 1);
vwochnik 5:ab909221d22d 44 _state = STATE_SENT_ID;
vwochnik 5:ab909221d22d 45 return CLIENT_OK;
vwochnik 3:ce2f116369bd 46 }
vwochnik 3:ce2f116369bd 47
vwochnik 3:ce2f116369bd 48 uint8_t MbedClient::sendData(DataGenerator& generator)
vwochnik 3:ce2f116369bd 49 {
vwochnik 5:ab909221d22d 50 if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
vwochnik 5:ab909221d22d 51 return CLIENT_INTERNAL_ERROR;
vwochnik 5:ab909221d22d 52 _generator = new HTTPGeneratorWrapper(generator);
vwochnik 5:ab909221d22d 53 _state = STATE_SENT_DATA;
vwochnik 5:ab909221d22d 54 return CLIENT_OK;
vwochnik 3:ce2f116369bd 55 }
vwochnik 3:ce2f116369bd 56
vwochnik 3:ce2f116369bd 57 uint8_t MbedClient::endRequest()
vwochnik 3:ce2f116369bd 58 {
vwochnik 5:ab909221d22d 59 if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID) && (_state != STATE_SENT_DATA))
vwochnik 5:ab909221d22d 60 return CLIENT_INTERNAL_ERROR;
vwochnik 5:ab909221d22d 61 _state = STATE_REQ_COMPLETE;
vwochnik 3:ce2f116369bd 62 }
vwochnik 3:ce2f116369bd 63
vwochnik 3:ce2f116369bd 64 uint8_t MbedClient::awaitResponse()
vwochnik 3:ce2f116369bd 65 {
vwochnik 5:ab909221d22d 66 HTTPResult result;
vwochnik 5:ab909221d22d 67
vwochnik 5:ab909221d22d 68 if (_state != STATE_REQ_COMPLETE)
vwochnik 5:ab909221d22d 69 return CLIENT_INTERNAL_ERROR;
vwochnik 5:ab909221d22d 70 result = _client.post(_url, *_generator, &_buffer);
vwochnik 5:ab909221d22d 71 if (result != 0)
vwochnik 5:ab909221d22d 72 return CLIENT_CONNECTION_ERROR;
vwochnik 5:ab909221d22d 73 _state = STATE_RECVD_RESPONSE;
vwochnik 5:ab909221d22d 74 return CLIENT_OK;
vwochnik 3:ce2f116369bd 75 }
vwochnik 3:ce2f116369bd 76
vwochnik 3:ce2f116369bd 77 AbstractDataSource& MbedClient::receiveData()
vwochnik 3:ce2f116369bd 78 {
vwochnik 3:ce2f116369bd 79 return _buffer;
vwochnik 3:ce2f116369bd 80 }
vwochnik 3:ce2f116369bd 81
vwochnik 3:ce2f116369bd 82 void MbedClient::stop()
vwochnik 3:ce2f116369bd 83 {
vwochnik 3:ce2f116369bd 84 _buffer.writeReset();
vwochnik 5:ab909221d22d 85 _headers[1] = NULL;
vwochnik 5:ab909221d22d 86 if (_generator != NULL)
vwochnik 5:ab909221d22d 87 delete _generator;
vwochnik 5:ab909221d22d 88 _generator = NULL;
vwochnik 3:ce2f116369bd 89 }