A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

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