Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Committer:
Cumulocity
Date:
Wed Jul 09 15:37:19 2014 +0200
Revision:
4:059b8b90e0d9
Parent:
2:45a6e44a4fb4
Child:
5:2b74510900da
Updated from revision b9c81426d0e6

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 1:9a11a331e340 96 * Creates a new generic SmartRest 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 2:45a6e44a4fb4 105 uint8_t setAuthorization(const char*, const char*);
Cumulocity 2:45a6e44a4fb4 106
Cumulocity 0:099f76422485 107 /**
Cumulocity 0:099f76422485 108 * Sends a smart request.
Cumulocity 0:099f76422485 109 * @param generator the generator which will generate the data to be
Cumulocity 0:099f76422485 110 * sent.
Cumulocity 1:9a11a331e340 111 * @param overrideIdentifier a device identifier which gets sent instead
Cumulocity 1:9a11a331e340 112 * of the identifier specified in the
Cumulocity 1:9a11a331e340 113 * constructor. If an empty string is
Cumulocity 1:9a11a331e340 114 * specified, no identifier is sent at all.
Cumulocity 0:099f76422485 115 * @return a non-zero value if and only if an error occured
Cumulocity 0:099f76422485 116 */
Cumulocity 2:45a6e44a4fb4 117 uint8_t send(const DataGenerator& generator, const char *overrideIdentifier=NULL);
Cumulocity 0:099f76422485 118
Cumulocity 0:099f76422485 119 /**
Cumulocity 0:099f76422485 120 * Tries to receive a parsed response row.
Cumulocity 0:099f76422485 121 * When the function succeeds, but the row pointer is NULL, there are
Cumulocity 0:099f76422485 122 * no more rows to be read.
Cumulocity 0:099f76422485 123 * @param record an instance to where the parsed row is written
Cumulocity 0:099f76422485 124 * @return a non-zero value if and only if an error occured
Cumulocity 0:099f76422485 125 */
Cumulocity 2:45a6e44a4fb4 126 uint8_t receive(ParsedRecord&);
Cumulocity 0:099f76422485 127
Cumulocity 0:099f76422485 128 /*
Cumulocity 0:099f76422485 129 * Initiates the SmartRest bootstrap process.
Cumulocity 0:099f76422485 130 * When successful, the template identifier will be replaced by the
Cumulocity 0:099f76422485 131 * global managed object ID in future requests.
Cumulocity 0:099f76422485 132 * @param generator the generator which will generate the data to be
Cumulocity 0:099f76422485 133 * sent as a template.
Cumulocity 0:099f76422485 134 * @return a non-zero value if and only if an error occured
Cumulocity 0:099f76422485 135 */
Cumulocity 4:059b8b90e0d9 136 uint8_t bootstrap(const DataGenerator&);
Cumulocity 0:099f76422485 137
Cumulocity 0:099f76422485 138 /*
Cumulocity 0:099f76422485 139 * Closes the connection.
Cumulocity 0:099f76422485 140 */
Cumulocity 0:099f76422485 141 void stop();
Cumulocity 0:099f76422485 142
Cumulocity 0:099f76422485 143 protected:
Cumulocity 1:9a11a331e340 144 uint8_t beginRequest(const char*);
Cumulocity 0:099f76422485 145 uint8_t awaitResponse();
Cumulocity 0:099f76422485 146 bool setMoGid(ParsedRecord&);
Cumulocity 0:099f76422485 147
Cumulocity 0:099f76422485 148 private:
Cumulocity 0:099f76422485 149 AbstractClient& _client;
Cumulocity 0:099f76422485 150 AbstractDataSource *_source;
Cumulocity 0:099f76422485 151 Parser _parser;
Cumulocity 0:099f76422485 152 const char *_identifier;
Cumulocity 0:099f76422485 153 char _mogid[8 * sizeof(long) + 1];
Cumulocity 0:099f76422485 154 };
Cumulocity 0:099f76422485 155
Cumulocity 0:099f76422485 156 #endif