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

Dependents:   MbedSmartRestMain MbedSmartRestMain

Committer:
Cumulocity
Date:
Thu Jul 10 16:20:31 2014 +0200
Revision:
5:2b74510900da
Parent:
4:059b8b90e0d9
Child:
6:cd7ba1ddb664
Updated from revision 131b98d7d1aa

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Cumulocity 0:099f76422485 1 /*
Cumulocity 0:099f76422485 2 * SmartRest.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 SmartRest.h
Cumulocity 0:099f76422485 31 * The main SmartRest facade class. This class is abstract, because the
Cumulocity 0:099f76422485 32 * actual implementation has to supply a client.
Cumulocity 0:099f76422485 33 */
Cumulocity 0:099f76422485 34
Cumulocity 0:099f76422485 35 #ifndef SMARTREST_H
Cumulocity 0:099f76422485 36 #define SMARTREST_H
Cumulocity 0:099f76422485 37
Cumulocity 0:099f76422485 38 #include "config.h"
Cumulocity 0:099f76422485 39 #include <stddef.h>
Cumulocity 0:099f76422485 40 #include <stdint.h>
Cumulocity 0:099f76422485 41 #include "AbstractClient.h"
Cumulocity 0:099f76422485 42 #include "ParsedRecord.h"
Cumulocity 0:099f76422485 43 #include "Parser.h"
Cumulocity 5:2b74510900da 44 #include "Aggregator.h"
Cumulocity 0:099f76422485 45
Cumulocity 0:099f76422485 46 /** Return value indicating that no error occurred. */
Cumulocity 0:099f76422485 47 #define SMARTREST_SUCCESS 0
Cumulocity 0:099f76422485 48 /** Return value indicating that the connection has been closed during
Cumulocity 0:099f76422485 49 * data transmission. */
Cumulocity 0:099f76422485 50 #define SMARTREST_CONNECTION_FAILED 1
Cumulocity 0:099f76422485 51 /** Return value indicating an internal state error. */
Cumulocity 0:099f76422485 52 #define SMARTREST_INTERNAL_ERROR 2
Cumulocity 0:099f76422485 53 /** Return value indicating a transmission timeout. */
Cumulocity 0:099f76422485 54 #define SMARTREST_TIMEOUT_ERROR 3
Cumulocity 0:099f76422485 55 /** Return value indicating an end of response indicated by the
Cumulocity 0:099f76422485 56 * Content-Length header. */
Cumulocity 0:099f76422485 57 #define SMARTREST_END_OF_RESPONSE 4
Cumulocity 0:099f76422485 58 /** Return value indicating that the connection has been closed. */
Cumulocity 0:099f76422485 59 #define SMARTREST_CONNECTION_CLOSED 5
Cumulocity 0:099f76422485 60
Cumulocity 0:099f76422485 61 /**
Cumulocity 0:099f76422485 62 * SmartRest client implementation.
Cumulocity 0:099f76422485 63 * This class provides methods to send a request and receive a response
Cumulocity 0:099f76422485 64 * from the server.
Cumulocity 0:099f76422485 65 *
Cumulocity 0:099f76422485 66 * Example:
Cumulocity 0:099f76422485 67 * @code
Cumulocity 0:099f76422485 68 * // given a concrete SmartRest implementation and a template
Cumulocity 0:099f76422485 69 * SmartRest client;
Cumulocity 0:099f76422485 70 * StaticData template;
Cumulocity 0:099f76422485 71 *
Cumulocity 0:099f76422485 72 * // bootstrap
Cumulocity 0:099f76422485 73 * if (client.bootstrap(template) != SMARTREST_SUCCESS) {
Cumulocity 0:099f76422485 74 * // error handling
Cumulocity 0:099f76422485 75 * return;
Cumulocity 0:099f76422485 76 * }
Cumulocity 0:099f76422485 77 *
Cumulocity 0:099f76422485 78 * if (client.send(StaticData("100,Hello")) != SMARTREST_SUCCESS) {
Cumulocity 0:099f76422485 79 * // error handling
Cumulocity 0:099f76422485 80 * }
Cumulocity 0:099f76422485 81 *
Cumulocity 0:099f76422485 82 * uint8_t ret;
Cumulocity 0:099f76422485 83 * while ((ret = client.receive(record)) == SMARTREST_SUCCESS) {
Cumulocity 0:099f76422485 84 * // work with data
Cumulocity 0:099f76422485 85 * }
Cumulocity 0:099f76422485 86 *
Cumulocity 0:099f76422485 87 * // error handling
Cumulocity 0:099f76422485 88 *
Cumulocity 0:099f76422485 89 * // call after every request.
Cumulocity 0:099f76422485 90 * client.stop();
Cumulocity 0:099f76422485 91 * @encode
Cumulocity 0:099f76422485 92 */
Cumulocity 0:099f76422485 93 class SmartRest
Cumulocity 0:099f76422485 94 {
Cumulocity 0:099f76422485 95 protected:
Cumulocity 0:099f76422485 96 /**
Cumulocity 1:9a11a331e340 97 * Creates a new generic SmartRest object.
Cumulocity 0:099f76422485 98 * @param client the abstract client to use
Cumulocity 0:099f76422485 99 * @param identifier the device identifier
Cumulocity 0:099f76422485 100 */
Cumulocity 0:099f76422485 101 SmartRest(AbstractClient&, const char*);
Cumulocity 0:099f76422485 102
Cumulocity 0:099f76422485 103 public:
Cumulocity 0:099f76422485 104 virtual ~SmartRest() { };
Cumulocity 0:099f76422485 105
Cumulocity 2:45a6e44a4fb4 106 uint8_t setAuthorization(const char*, const char*);
Cumulocity 2:45a6e44a4fb4 107
Cumulocity 5:2b74510900da 108 #ifdef SMARTREST_TRANSACTIONAL
Cumulocity 5:2b74510900da 109 uint8_t request(const DataGenerator&, const char* = NULL);
Cumulocity 5:2b74510900da 110 uint8_t request(const DataGenerator&, Aggregator&, const char* = NULL);
Cumulocity 5:2b74510900da 111 #endif
Cumulocity 5:2b74510900da 112
Cumulocity 5:2b74510900da 113 /*
Cumulocity 5:2b74510900da 114 * Initiates the SmartRest bootstrap process.
Cumulocity 5:2b74510900da 115 * When successful, the template identifier will be replaced by the
Cumulocity 5:2b74510900da 116 * global managed object ID in future requests.
Cumulocity 5:2b74510900da 117 * @param generator the generator which will generate the data to be
Cumulocity 5:2b74510900da 118 * sent as a template.
Cumulocity 5:2b74510900da 119 * @return a non-zero value if and only if an error occured
Cumulocity 5:2b74510900da 120 */
Cumulocity 5:2b74510900da 121 uint8_t bootstrap(const DataGenerator&);
Cumulocity 5:2b74510900da 122
Cumulocity 5:2b74510900da 123 #ifndef SMARTREST_TRANSACTIONAL
Cumulocity 0:099f76422485 124 /**
Cumulocity 0:099f76422485 125 * Sends a smart request.
Cumulocity 0:099f76422485 126 * @param generator the generator which will generate the data to be
Cumulocity 0:099f76422485 127 * sent.
Cumulocity 1:9a11a331e340 128 * @param overrideIdentifier a device identifier which gets sent instead
Cumulocity 1:9a11a331e340 129 * of the identifier specified in the
Cumulocity 1:9a11a331e340 130 * constructor. If an empty string is
Cumulocity 1:9a11a331e340 131 * specified, no identifier is sent at all.
Cumulocity 0:099f76422485 132 * @return a non-zero value if and only if an error occured
Cumulocity 0:099f76422485 133 */
Cumulocity 5:2b74510900da 134 uint8_t send(const DataGenerator&, const char* = NULL);
Cumulocity 0:099f76422485 135
Cumulocity 0:099f76422485 136 /**
Cumulocity 0:099f76422485 137 * Tries to receive a parsed response row.
Cumulocity 0:099f76422485 138 * When the function succeeds, but the row pointer is NULL, there are
Cumulocity 0:099f76422485 139 * no more rows to be read.
Cumulocity 0:099f76422485 140 * @param record an instance to where the parsed row is written
Cumulocity 0:099f76422485 141 * @return a non-zero value if and only if an error occured
Cumulocity 0:099f76422485 142 */
Cumulocity 2:45a6e44a4fb4 143 uint8_t receive(ParsedRecord&);
Cumulocity 0:099f76422485 144
Cumulocity 0:099f76422485 145 /*
Cumulocity 0:099f76422485 146 * Closes the connection.
Cumulocity 0:099f76422485 147 */
Cumulocity 0:099f76422485 148 void stop();
Cumulocity 5:2b74510900da 149 #endif
Cumulocity 0:099f76422485 150
Cumulocity 0:099f76422485 151 protected:
Cumulocity 5:2b74510900da 152 #ifdef SMARTREST_TRANSACTIONAL
Cumulocity 5:2b74510900da 153 uint8_t send(const DataGenerator&, const char*);
Cumulocity 5:2b74510900da 154 uint8_t receive(ParsedRecord&);
Cumulocity 5:2b74510900da 155 void stop();
Cumulocity 5:2b74510900da 156 #endif
Cumulocity 1:9a11a331e340 157 uint8_t beginRequest(const char*);
Cumulocity 0:099f76422485 158 uint8_t awaitResponse();
Cumulocity 0:099f76422485 159 bool setMoGid(ParsedRecord&);
Cumulocity 0:099f76422485 160
Cumulocity 0:099f76422485 161 private:
Cumulocity 0:099f76422485 162 AbstractClient& _client;
Cumulocity 0:099f76422485 163 AbstractDataSource *_source;
Cumulocity 0:099f76422485 164 Parser _parser;
Cumulocity 0:099f76422485 165 const char *_identifier;
Cumulocity 0:099f76422485 166 char _mogid[8 * sizeof(long) + 1];
Cumulocity 0:099f76422485 167 };
Cumulocity 0:099f76422485 168
Cumulocity 0:099f76422485 169 #endif