Own fork of MbedSmartRest

Dependents:   MbedSmartRestMain MbedSmartRestMain

Fork of MbedSmartRest by Cumulocity Official

Revision:
0:099f76422485
Child:
1:9a11a331e340
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SmartRest.h	Thu Jul 03 20:38:04 2014 +0200
@@ -0,0 +1,150 @@
+/*
+ * 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"
+
+/** 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 GenericSmartRest object.
+     * @param client the abstract client to use
+     * @param identifier the device identifier
+     */
+    SmartRest(AbstractClient&, const char*);
+
+public:
+    virtual ~SmartRest() { };
+
+    /**
+     * Sends a smart request.
+     * @param generator the generator which will generate the data to be
+     *                  sent.
+     * @return a non-zero value if and only if an error occured
+     */
+    int8_t send(DataGenerator&);
+
+    /**
+     * 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
+     */
+    int8_t receive(ParsedRecord&);
+
+    /*
+     * 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
+     */
+    int8_t bootstrap(DataGenerator&);
+
+    /*
+     * Closes the connection.
+     */
+    void stop();
+
+protected:
+    uint8_t beginRequest();
+    uint8_t awaitResponse();
+    bool setMoGid(ParsedRecord&);
+
+private:
+    AbstractClient& _client;
+    AbstractDataSource *_source;
+    Parser _parser;
+    const char *_identifier;
+    char _mogid[8 * sizeof(long) + 1];
+};
+
+#endif