A client for the SmartREST protocol from Cumulocity.

Dependencies:   SmartRest

Fork of MbedSmartRest by Vincent Wochnik

Committer:
vwochnik
Date:
Thu Jan 23 14:36:46 2014 +0000
Revision:
0:f76673e7f275
initial commit, only abstraction

Who changed what in which revision?

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