Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Thu Jul 03 20:38:04 2014 +0200
Revision:
0:099f76422485
Child:
1:9a11a331e340
Updated from revision 0413a0179eb6

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