Own fork of MbedSmartRest
Dependents: MbedSmartRestMain MbedSmartRestMain
Fork of MbedSmartRest by
SmartRest.h
- Committer:
- Cumulocity
- Date:
- 2014-07-10
- Revision:
- 5:2b74510900da
- Parent:
- 4:059b8b90e0d9
- Child:
- 6:cd7ba1ddb664
File content as of revision 5:2b74510900da:
/* * SmartRest.h * * Created on: Nov 1, 2013 * * Authors: Vincent Wochnik <v.wochnik@gmail.com> * * Copyright (c) 2013 Cumulocity GmbH * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** * @file SmartRest.h * The main SmartRest facade class. This class is abstract, because the * actual implementation has to supply a client. */ #ifndef SMARTREST_H #define SMARTREST_H #include "config.h" #include <stddef.h> #include <stdint.h> #include "AbstractClient.h" #include "ParsedRecord.h" #include "Parser.h" #include "Aggregator.h" /** Return value indicating that no error occurred. */ #define SMARTREST_SUCCESS 0 /** Return value indicating that the connection has been closed during * data transmission. */ #define SMARTREST_CONNECTION_FAILED 1 /** Return value indicating an internal state error. */ #define SMARTREST_INTERNAL_ERROR 2 /** Return value indicating a transmission timeout. */ #define SMARTREST_TIMEOUT_ERROR 3 /** Return value indicating an end of response indicated by the * Content-Length header. */ #define SMARTREST_END_OF_RESPONSE 4 /** Return value indicating that the connection has been closed. */ #define SMARTREST_CONNECTION_CLOSED 5 /** * SmartRest client implementation. * This class provides methods to send a request and receive a response * from the server. * * Example: * @code * // given a concrete SmartRest implementation and a template * SmartRest client; * StaticData template; * * // bootstrap * if (client.bootstrap(template) != SMARTREST_SUCCESS) { * // error handling * return; * } * * if (client.send(StaticData("100,Hello")) != SMARTREST_SUCCESS) { * // error handling * } * * uint8_t ret; * while ((ret = client.receive(record)) == SMARTREST_SUCCESS) { * // work with data * } * * // error handling * * // call after every request. * client.stop(); * @encode */ class SmartRest { protected: /** * Creates a new generic SmartRest object. * @param client the abstract client to use * @param identifier the device identifier */ SmartRest(AbstractClient&, const char*); public: virtual ~SmartRest() { }; uint8_t setAuthorization(const char*, const char*); #ifdef SMARTREST_TRANSACTIONAL uint8_t request(const DataGenerator&, const char* = NULL); uint8_t request(const DataGenerator&, Aggregator&, const char* = NULL); #endif /* * Initiates the SmartRest bootstrap process. * When successful, the template identifier will be replaced by the * global managed object ID in future requests. * @param generator the generator which will generate the data to be * sent as a template. * @return a non-zero value if and only if an error occured */ uint8_t bootstrap(const DataGenerator&); #ifndef SMARTREST_TRANSACTIONAL /** * Sends a smart request. * @param generator the generator which will generate the data to be * sent. * @param overrideIdentifier a device identifier which gets sent instead * of the identifier specified in the * constructor. If an empty string is * specified, no identifier is sent at all. * @return a non-zero value if and only if an error occured */ uint8_t send(const DataGenerator&, const char* = NULL); /** * Tries to receive a parsed response row. * When the function succeeds, but the row pointer is NULL, there are * no more rows to be read. * @param record an instance to where the parsed row is written * @return a non-zero value if and only if an error occured */ uint8_t receive(ParsedRecord&); /* * Closes the connection. */ void stop(); #endif protected: #ifdef SMARTREST_TRANSACTIONAL uint8_t send(const DataGenerator&, const char*); uint8_t receive(ParsedRecord&); void stop(); #endif uint8_t beginRequest(const char*); uint8_t awaitResponse(); bool setMoGid(ParsedRecord&); private: AbstractClient& _client; AbstractDataSource *_source; Parser _parser; const char *_identifier; char _mogid[8 * sizeof(long) + 1]; }; #endif