Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * AbstractClient.h
Cumulocity 0:099f76422485 3 *
Cumulocity 0:099f76422485 4 * Created on: Nov 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 /**
Cumulocity 0:099f76422485 30 * @file AbstractClient.h
Cumulocity 0:099f76422485 31 * An abstraction layer for the SmartREST http client.
Cumulocity 0:099f76422485 32 */
Cumulocity 0:099f76422485 33
Cumulocity 0:099f76422485 34 #ifndef ABSTRACTCLIENT_H
Cumulocity 0:099f76422485 35 #define ABSTRACTCLIENT_H
Cumulocity 0:099f76422485 36
Cumulocity 0:099f76422485 37 #include "config.h"
Cumulocity 0:099f76422485 38 #include "DataGenerator.h"
Cumulocity 0:099f76422485 39 #include "AbstractDataSource.h"
Cumulocity 0:099f76422485 40 #include "AbstractDataSink.h"
Cumulocity 0:099f76422485 41
Cumulocity 0:099f76422485 42 /** Return value indicating that the operation has been successful. */
Cumulocity 0:099f76422485 43 #define CLIENT_OK 0
Cumulocity 0:099f76422485 44 /** Return value indicating that there has been a connection error. */
Cumulocity 0:099f76422485 45 #define CLIENT_CONNECTION_ERROR 1
Cumulocity 0:099f76422485 46 /** Return value indicating that an internal error occurred. */
Cumulocity 0:099f76422485 47 #define CLIENT_INTERNAL_ERROR 2
Cumulocity 0:099f76422485 48
Cumulocity 0:099f76422485 49 /*
Cumulocity 0:099f76422485 50 * An abstraction layer for the SmartREST http client.
Cumulocity 0:099f76422485 51 *
Cumulocity 0:099f76422485 52 * All methods have to be called in the following order:
Cumulocity 2:45a6e44a4fb4 53 * - setAuthorization()
Cumulocity 0:099f76422485 54 * - beginRequest()
Cumulocity 0:099f76422485 55 * - sendIdentifier() (optional)
Cumulocity 0:099f76422485 56 * - sendData() (optional)
Cumulocity 0:099f76422485 57 * - endRequest()
Cumulocity 0:099f76422485 58 * - awaitResponse()
Cumulocity 0:099f76422485 59 * - receiveData() (optional)
Cumulocity 0:099f76422485 60 * - stop()
Cumulocity 0:099f76422485 61 *
Cumulocity 0:099f76422485 62 * Additionally, stop() can be called at any time to reset the state of
Cumulocity 0:099f76422485 63 * this instance.
Cumulocity 0:099f76422485 64 */
Cumulocity 0:099f76422485 65 class AbstractClient
Cumulocity 0:099f76422485 66 {
Cumulocity 0:099f76422485 67 public:
Cumulocity 0:099f76422485 68 virtual ~AbstractClient() { };
Cumulocity 0:099f76422485 69
Cumulocity 0:099f76422485 70 /**
Cumulocity 2:45a6e44a4fb4 71 * Sets authorization username and password. This method can only be
Cumulocity 2:45a6e44a4fb4 72 * called when no request is processing. Values set using this method
Cumulocity 2:45a6e44a4fb4 73 * must be kept in memory until either new values are set or the object
Cumulocity 2:45a6e44a4fb4 74 * is being destroyed.
Cumulocity 2:45a6e44a4fb4 75 * @param username the username used for further request authorization
Cumulocity 2:45a6e44a4fb4 76 * @param password the password used for further request authorization
Cumulocity 2:45a6e44a4fb4 77 */
Cumulocity 2:45a6e44a4fb4 78 virtual uint8_t setAuthorization(const char*, const char*) = 0;
Cumulocity 2:45a6e44a4fb4 79
Cumulocity 2:45a6e44a4fb4 80 /**
Cumulocity 0:099f76422485 81 * Begins a new request. This method has to be the first call for
Cumulocity 0:099f76422485 82 * any request. A connection to the server will be established
Cumulocity 0:099f76422485 83 * and the start of a HTTP post request will be sent over the
Cumulocity 0:099f76422485 84 * connection including Host and Authorization headers.
Cumulocity 0:099f76422485 85 */
Cumulocity 0:099f76422485 86 virtual uint8_t beginRequest() = 0;
Cumulocity 0:099f76422485 87
Cumulocity 0:099f76422485 88 /**
Cumulocity 0:099f76422485 89 * Sends the X-Id device identifier header.
Cumulocity 0:099f76422485 90 * @param identifier the identifier to be sent. The value has to remain
Cumulocity 1:9a11a331e340 91 * valid for the entire request. If null or empty, no
Cumulocity 1:9a11a331e340 92 * identifier is sent.
Cumulocity 0:099f76422485 93 */
Cumulocity 0:099f76422485 94 virtual uint8_t sendIdentifier(const char*) = 0;
Cumulocity 0:099f76422485 95
Cumulocity 0:099f76422485 96 /**
Cumulocity 0:099f76422485 97 * Sends POST data over the connection. Before the data is sent,
Cumulocity 0:099f76422485 98 * a Content-Length header is also sent over the connection. if the
Cumulocity 0:099f76422485 99 * estimated length does not match the actual conten length,
Cumulocity 0:099f76422485 100 * unexpected behavior will occur.
Cumulocity 0:099f76422485 101 * @param generator the data generator for the data to be sent
Cumulocity 0:099f76422485 102 */
Cumulocity 1:9a11a331e340 103 virtual uint8_t sendData(const DataGenerator& generator) = 0;
Cumulocity 0:099f76422485 104
Cumulocity 0:099f76422485 105 /**
Cumulocity 0:099f76422485 106 * Finishes the request. In case no data has been sent over the
Cumulocity 0:099f76422485 107 * connection, this method will send twice carriage-return new-line.
Cumulocity 0:099f76422485 108 */
Cumulocity 0:099f76422485 109 virtual uint8_t endRequest() = 0;
Cumulocity 0:099f76422485 110
Cumulocity 0:099f76422485 111 /**
Cumulocity 0:099f76422485 112 * Blocks until the start of a response has been received.
Cumulocity 0:099f76422485 113 */
Cumulocity 0:099f76422485 114 virtual uint8_t awaitResponse() = 0;
Cumulocity 0:099f76422485 115
Cumulocity 0:099f76422485 116 /**
Cumulocity 0:099f76422485 117 * Returns a data source for reading data.
Cumulocity 0:099f76422485 118 * When no data can be read for whatever reason,
Cumulocity 0:099f76422485 119 * a data source with an error state may be returned.
Cumulocity 0:099f76422485 120 * @return the data source to read from
Cumulocity 0:099f76422485 121 */
Cumulocity 0:099f76422485 122 virtual AbstractDataSource& receiveData() = 0;
Cumulocity 0:099f76422485 123
Cumulocity 0:099f76422485 124 /**
Cumulocity 0:099f76422485 125 * Resets the connection. This method can be called at any time
Cumulocity 0:099f76422485 126 * and resets the state of this instance.
Cumulocity 0:099f76422485 127 */
Cumulocity 0:099f76422485 128 virtual void stop() = 0;
Cumulocity 0:099f76422485 129 };
Cumulocity 0:099f76422485 130
Cumulocity 0:099f76422485 131 #endif