A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

Committer:
vwochnik
Date:
Wed Apr 02 12:23:46 2014 +0000
Revision:
12:788dd934f283
Parent:
10:478414cc15a4
Child:
13:e76920d5e1ec
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 12:788dd934f283 52 puts("Send called.");
vwochnik 5:ab909221d22d 53 if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID))
vwochnik 5:ab909221d22d 54 return CLIENT_INTERNAL_ERROR;
vwochnik 12:788dd934f283 55 puts("Setting gen.");
vwochnik 5:ab909221d22d 56 _generator = new HTTPGeneratorWrapper(generator);
vwochnik 5:ab909221d22d 57 _state = STATE_SENT_DATA;
vwochnik 5:ab909221d22d 58 return CLIENT_OK;
vwochnik 3:ce2f116369bd 59 }
vwochnik 3:ce2f116369bd 60
vwochnik 3:ce2f116369bd 61 uint8_t MbedClient::endRequest()
vwochnik 3:ce2f116369bd 62 {
vwochnik 5:ab909221d22d 63 if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID) && (_state != STATE_SENT_DATA))
vwochnik 5:ab909221d22d 64 return CLIENT_INTERNAL_ERROR;
vwochnik 5:ab909221d22d 65 _state = STATE_REQ_COMPLETE;
vwochnik 7:26524a6a04a1 66 return CLIENT_OK;
vwochnik 3:ce2f116369bd 67 }
vwochnik 3:ce2f116369bd 68
vwochnik 3:ce2f116369bd 69 uint8_t MbedClient::awaitResponse()
vwochnik 3:ce2f116369bd 70 {
vwochnik 5:ab909221d22d 71 HTTPResult result;
vwochnik 5:ab909221d22d 72
vwochnik 12:788dd934f283 73 puts("Action");
vwochnik 5:ab909221d22d 74 if (_state != STATE_REQ_COMPLETE)
vwochnik 5:ab909221d22d 75 return CLIENT_INTERNAL_ERROR;
vwochnik 12:788dd934f283 76 puts("Calling");
vwochnik 5:ab909221d22d 77 result = _client.post(_url, *_generator, &_buffer);
vwochnik 5:ab909221d22d 78 if (result != 0)
vwochnik 5:ab909221d22d 79 return CLIENT_CONNECTION_ERROR;
vwochnik 10:478414cc15a4 80 char *p = _buffer._buf;
vwochnik 10:478414cc15a4 81 while (p != _buffer._wptr)
vwochnik 10:478414cc15a4 82 putchar(*p++);
vwochnik 5:ab909221d22d 83 _state = STATE_RECVD_RESPONSE;
vwochnik 5:ab909221d22d 84 return CLIENT_OK;
vwochnik 3:ce2f116369bd 85 }
vwochnik 3:ce2f116369bd 86
vwochnik 3:ce2f116369bd 87 AbstractDataSource& MbedClient::receiveData()
vwochnik 3:ce2f116369bd 88 {
vwochnik 3:ce2f116369bd 89 return _buffer;
vwochnik 3:ce2f116369bd 90 }
vwochnik 3:ce2f116369bd 91
vwochnik 3:ce2f116369bd 92 void MbedClient::stop()
vwochnik 3:ce2f116369bd 93 {
vwochnik 3:ce2f116369bd 94 _buffer.writeReset();
vwochnik 5:ab909221d22d 95 _headers[1] = NULL;
vwochnik 5:ab909221d22d 96 if (_generator != NULL)
vwochnik 5:ab909221d22d 97 delete _generator;
vwochnik 5:ab909221d22d 98 _generator = NULL;
vwochnik 7:26524a6a04a1 99 _state = STATE_INIT;
vwochnik 3:ce2f116369bd 100 }