Generic SmartRest library
Dependents: SmartRestUnitTest MbedSmartRest MbedSmartRestStreaming
SmartRest.h@1:3e7b4c9e0821, 2014-04-16 (annotated)
- Committer:
- vwochnik
- Date:
- Wed Apr 16 08:30:49 2014 +0000
- Revision:
- 1:3e7b4c9e0821
- Parent:
- 0:744801d5734d
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vwochnik | 0:744801d5734d | 1 | /* |
vwochnik | 0:744801d5734d | 2 | * SmartRest.h |
vwochnik | 0:744801d5734d | 3 | * |
vwochnik | 0:744801d5734d | 4 | * Created on: Nov 1, 2013 |
vwochnik | 0:744801d5734d | 5 | * * Authors: Vincent Wochnik <v.wochnik@gmail.com> |
vwochnik | 0:744801d5734d | 6 | * |
vwochnik | 0:744801d5734d | 7 | * Copyright (c) 2013 Cumulocity GmbH |
vwochnik | 0:744801d5734d | 8 | * |
vwochnik | 0:744801d5734d | 9 | * Permission is hereby granted, free of charge, to any person obtaining |
vwochnik | 0:744801d5734d | 10 | * a copy of this software and associated documentation files (the |
vwochnik | 0:744801d5734d | 11 | * "Software"), to deal in the Software without restriction, including |
vwochnik | 0:744801d5734d | 12 | * without limitation the rights to use, copy, modify, merge, publish, |
vwochnik | 0:744801d5734d | 13 | * distribute, sublicense, and/or sell copies of the Software, and to |
vwochnik | 0:744801d5734d | 14 | * permit persons to whom the Software is furnished to do so, subject to |
vwochnik | 0:744801d5734d | 15 | * the following conditions: |
vwochnik | 0:744801d5734d | 16 | * |
vwochnik | 0:744801d5734d | 17 | * The above copyright notice and this permission notice shall be |
vwochnik | 0:744801d5734d | 18 | * included in all copies or substantial portions of the Software. |
vwochnik | 0:744801d5734d | 19 | * |
vwochnik | 0:744801d5734d | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
vwochnik | 0:744801d5734d | 21 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
vwochnik | 0:744801d5734d | 22 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
vwochnik | 0:744801d5734d | 23 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
vwochnik | 0:744801d5734d | 24 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
vwochnik | 0:744801d5734d | 25 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
vwochnik | 0:744801d5734d | 26 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
vwochnik | 0:744801d5734d | 27 | */ |
vwochnik | 0:744801d5734d | 28 | |
vwochnik | 0:744801d5734d | 29 | /** |
vwochnik | 0:744801d5734d | 30 | * @file SmartRest.h |
vwochnik | 0:744801d5734d | 31 | * The main SmartRest facade class. This class is abstract, because the |
vwochnik | 0:744801d5734d | 32 | * actual implementation has to supply a client. |
vwochnik | 0:744801d5734d | 33 | */ |
vwochnik | 0:744801d5734d | 34 | |
vwochnik | 0:744801d5734d | 35 | #ifndef SMARTREST_H |
vwochnik | 0:744801d5734d | 36 | #define SMARTREST_H |
vwochnik | 0:744801d5734d | 37 | |
vwochnik | 0:744801d5734d | 38 | #ifdef HAVE_CONFIG_H |
vwochnik | 0:744801d5734d | 39 | #include <config.h> |
vwochnik | 0:744801d5734d | 40 | #endif |
vwochnik | 0:744801d5734d | 41 | |
vwochnik | 0:744801d5734d | 42 | #include <stddef.h> |
vwochnik | 0:744801d5734d | 43 | #include <stdint.h> |
vwochnik | 0:744801d5734d | 44 | #include "AbstractClient.h" |
vwochnik | 0:744801d5734d | 45 | #include "ParsedRecord.h" |
vwochnik | 0:744801d5734d | 46 | #include "Parser.h" |
vwochnik | 0:744801d5734d | 47 | |
vwochnik | 0:744801d5734d | 48 | #ifndef SMARTREST_MOGID_BUFFER_SIZE |
vwochnik | 0:744801d5734d | 49 | #define SMARTREST_MOGID_BUFFER_SIZE 11 |
vwochnik | 0:744801d5734d | 50 | #endif |
vwochnik | 0:744801d5734d | 51 | |
vwochnik | 0:744801d5734d | 52 | /** Return value indicating that no error occurred. */ |
vwochnik | 0:744801d5734d | 53 | #define SMARTREST_SUCCESS 0 |
vwochnik | 0:744801d5734d | 54 | /** Return value indicating that the connection has been closed during |
vwochnik | 0:744801d5734d | 55 | * data transmission. */ |
vwochnik | 0:744801d5734d | 56 | #define SMARTREST_CONNECTION_FAILED 1 |
vwochnik | 0:744801d5734d | 57 | /** Return value indicating an internal state error. */ |
vwochnik | 0:744801d5734d | 58 | #define SMARTREST_INTERNAL_ERROR 2 |
vwochnik | 0:744801d5734d | 59 | /** Return value indicating a transmission timeout. */ |
vwochnik | 0:744801d5734d | 60 | #define SMARTREST_TIMEOUT_ERROR 3 |
vwochnik | 0:744801d5734d | 61 | /** Return value indicating an end of response indicated by the |
vwochnik | 0:744801d5734d | 62 | * Content-Length header. */ |
vwochnik | 0:744801d5734d | 63 | #define SMARTREST_END_OF_RESPONSE 4 |
vwochnik | 0:744801d5734d | 64 | /** Return value indicating that the connection has been closed. */ |
vwochnik | 0:744801d5734d | 65 | #define SMARTREST_CONNECTION_CLOSED 5 |
vwochnik | 0:744801d5734d | 66 | |
vwochnik | 0:744801d5734d | 67 | /** |
vwochnik | 0:744801d5734d | 68 | * SmartRest Arduino client implementation. |
vwochnik | 0:744801d5734d | 69 | * This class provides methods to send a request and receive a response |
vwochnik | 0:744801d5734d | 70 | * from the server. |
vwochnik | 0:744801d5734d | 71 | */ |
vwochnik | 0:744801d5734d | 72 | class SmartRest |
vwochnik | 0:744801d5734d | 73 | { |
vwochnik | 0:744801d5734d | 74 | protected: |
vwochnik | 0:744801d5734d | 75 | /** |
vwochnik | 0:744801d5734d | 76 | * Creates a new GenericSmartRest object. |
vwochnik | 0:744801d5734d | 77 | * @param client the abstract client to use |
vwochnik | 0:744801d5734d | 78 | * @param identifier the device identifier |
vwochnik | 0:744801d5734d | 79 | */ |
vwochnik | 0:744801d5734d | 80 | SmartRest(AbstractClient&, const char*); |
vwochnik | 0:744801d5734d | 81 | |
vwochnik | 0:744801d5734d | 82 | public: |
vwochnik | 0:744801d5734d | 83 | virtual ~SmartRest() { }; |
vwochnik | 0:744801d5734d | 84 | |
vwochnik | 0:744801d5734d | 85 | /** |
vwochnik | 0:744801d5734d | 86 | * Sends a smart request. |
vwochnik | 0:744801d5734d | 87 | * @param generator the generator which will generate the data to be |
vwochnik | 0:744801d5734d | 88 | * sent. |
vwochnik | 0:744801d5734d | 89 | * @return a non-zero value if and only if an error occured |
vwochnik | 0:744801d5734d | 90 | */ |
vwochnik | 0:744801d5734d | 91 | int8_t send(DataGenerator&); |
vwochnik | 0:744801d5734d | 92 | |
vwochnik | 0:744801d5734d | 93 | /** |
vwochnik | 0:744801d5734d | 94 | * Tries to receive a parsed response row. |
vwochnik | 0:744801d5734d | 95 | * When the function succeeds, but the row pointer is NULL, there are |
vwochnik | 0:744801d5734d | 96 | * no more rows to be read. |
vwochnik | 0:744801d5734d | 97 | * @param record an instance to where the parsed row is written |
vwochnik | 0:744801d5734d | 98 | * @return a non-zero value if and only if an error occured |
vwochnik | 0:744801d5734d | 99 | */ |
vwochnik | 0:744801d5734d | 100 | int8_t receive(ParsedRecord&); |
vwochnik | 0:744801d5734d | 101 | |
vwochnik | 0:744801d5734d | 102 | /* |
vwochnik | 0:744801d5734d | 103 | * Initiates the SmartRest bootstrap process. |
vwochnik | 0:744801d5734d | 104 | * When successful, the template identifier will be replaced by the |
vwochnik | 0:744801d5734d | 105 | * global managed object ID in future requests. |
vwochnik | 0:744801d5734d | 106 | * @param generator the generator which will generate the data to be |
vwochnik | 0:744801d5734d | 107 | * sent as a template. |
vwochnik | 0:744801d5734d | 108 | * @return a non-zero value if and only if an error occured |
vwochnik | 0:744801d5734d | 109 | */ |
vwochnik | 0:744801d5734d | 110 | int8_t bootstrap(DataGenerator&); |
vwochnik | 0:744801d5734d | 111 | |
vwochnik | 0:744801d5734d | 112 | /* |
vwochnik | 0:744801d5734d | 113 | * Closes the connection. |
vwochnik | 0:744801d5734d | 114 | */ |
vwochnik | 0:744801d5734d | 115 | void stop(); |
vwochnik | 0:744801d5734d | 116 | |
vwochnik | 0:744801d5734d | 117 | private: |
vwochnik | 0:744801d5734d | 118 | uint8_t beginRequest(); |
vwochnik | 0:744801d5734d | 119 | uint8_t awaitResponse(); |
vwochnik | 0:744801d5734d | 120 | bool setMoGid(Record&); |
vwochnik | 0:744801d5734d | 121 | |
vwochnik | 0:744801d5734d | 122 | private: |
vwochnik | 0:744801d5734d | 123 | AbstractClient& _client; |
vwochnik | 0:744801d5734d | 124 | AbstractDataSource *_source; |
vwochnik | 0:744801d5734d | 125 | Parser _parser; |
vwochnik | 0:744801d5734d | 126 | const char *_identifier; |
vwochnik | 0:744801d5734d | 127 | char _mogid[SMARTREST_MOGID_BUFFER_SIZE]; |
vwochnik | 0:744801d5734d | 128 | }; |
vwochnik | 0:744801d5734d | 129 | |
vwochnik | 0:744801d5734d | 130 | #endif |