mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.
Dependents: MbedSmartRestMain MbedSmartRestMain
Revision 11:e1bee9a77652, committed 2014-11-15
- Comitter:
- Cumulocity
- Date:
- Sat Nov 15 11:21:01 2014 +0100
- Parent:
- 10:97077cfa13fc
- Child:
- 12:6634f9814235
- Commit message:
- Updated from revision ce16ad78546a
Changed in this revision
--- a/AbstractClient.h Thu Oct 23 14:41:58 2014 +0200 +++ b/AbstractClient.h Sat Nov 15 11:21:01 2014 +0100 @@ -51,9 +51,9 @@ * * All methods have to be called in the following order: * - setAuthorization() - * - beginRequest() + * - beginRequest() or beginStream() * - sendIdentifier() (optional) - * - sendData() (optional) + * - sendData() (optional if in request mode) * - endRequest() * - awaitResponse() * - receiveData() (optional) @@ -66,68 +66,76 @@ */ class AbstractClient { -public: - virtual ~AbstractClient() { }; + public: + virtual ~AbstractClient() { }; - /** - * Sets authorization username and password. This method can only be - * called when no request is processing. Values set using this method - * must be kept in memory until either new values are set or the object - * is being destroyed. - * @param username the username used for further request authorization - * @param password the password used for further request authorization - */ - virtual uint8_t setAuthorization(const char*, const char*) = 0; + /** + * Sets authorization username and password. This method can only be + * called when no request is processing. Values set using this method + * must be kept in memory until either new values are set or the object + * is being destroyed. + * @param username the username used for further request authorization + * @param password the password used for further request authorization + */ + virtual uint8_t setAuthorization(const char*, const char*) = 0; - /** - * Begins a new request. This method has to be the first call for - * any request. A connection to the server will be established - * and the start of a HTTP post request will be sent over the - * connection including Host and Authorization headers. - */ - virtual uint8_t beginRequest() = 0; + /** + * Begins a new request. This method has to be the first call for + * any request. A connection to the server will be established + * and the start of a HTTP post request will be sent over the + * connection including Host and Authorization headers. + */ + virtual uint8_t beginRequest() = 0; - /** - * Sends the X-Id device identifier header. - * @param identifier the identifier to be sent. The value has to remain - * valid for the entire request. If null or empty, no - * identifier is sent. - */ - virtual uint8_t sendIdentifier(const char*) = 0; + /** + * Begins a new stream. This method has to be the first call for + * any request. A connection to the server will be established + * and the start of a HTTP post request will be sent over the + * connection including Host and Authorization headers. + */ + virtual uint8_t beginStream(const char*) = 0; - /** - * Sends POST data over the connection. Before the data is sent, - * a Content-Length header is also sent over the connection. if the - * estimated length does not match the actual conten length, - * unexpected behavior will occur. - * @param generator the data generator for the data to be sent - */ - virtual uint8_t sendData(const DataGenerator&) = 0; + /** + * Sends the X-Id device identifier header. + * @param identifier the identifier to be sent. The value has to remain + * valid for the entire request. If null or empty, no + * identifier is sent. + */ + virtual uint8_t sendIdentifier(const char*) = 0; - /** - * Finishes the request. In case no data has been sent over the - * connection, this method will send twice carriage-return new-line. - */ - virtual uint8_t endRequest() = 0; + /** + * Sends POST data over the connection. Before the data is sent, + * a Content-Length header is also sent over the connection. if the + * estimated length does not match the actual conten length, + * unexpected behavior will occur. + * @param generator the data generator for the data to be sent + */ + virtual uint8_t sendData(const DataGenerator&) = 0; - /** - * Blocks until the start of a response has been received. - */ - virtual uint8_t awaitResponse() = 0; + /** + * Finishes the request. In case no data has been sent over the + * connection, this method will send twice carriage-return new-line. + */ + virtual uint8_t endRequest() = 0; + + /** + * Blocks until the start of a response has been received. + */ + virtual uint8_t awaitResponse() = 0; - /** - * Returns a data source for reading data. - * When no data can be read for whatever reason, - * a data source with an error state may be returned. - * @return the data source to read from - */ - virtual AbstractDataSource& receiveData() = 0; + /** + * Returns a data source for reading data. + * When no data can be read for whatever reason, + * a data source with an error state may be returned. + * @return the data source to read from + */ + virtual AbstractDataSource& receiveData() = 0; - /** - * Resets the connection. This method can be called at any time - * and resets the state of this instance. - */ - virtual void stop() = 0; + /** + * Resets the connection. This method can be called at any time + * and resets the state of this instance. + */ + virtual void stop() = 0; }; #endif
--- a/AbstractDataSink.h Thu Oct 23 14:41:58 2014 +0200 +++ b/AbstractDataSink.h Sat Nov 15 11:21:01 2014 +0100 @@ -50,37 +50,37 @@ */ class AbstractDataSink { -public: - virtual ~AbstractDataSink() { }; + public: + virtual ~AbstractDataSink() { }; - /** - * Writes a single character. - * @param c the character to write - * @return the number of characters written, in this case 1 or 0 - */ - virtual size_t write(char) = 0; + /** + * Writes a single character. + * @param c the character to write + * @return the number of characters written, in this case 1 or 0 + */ + virtual size_t write(char) = 0; - /** - * Writes a buffer. - * @param buf the buffer to write - * @param length the length of the buffer in bytes - * @return the number of characters written - */ - virtual size_t write(void*, size_t) = 0; + /** + * Writes a buffer. + * @param buf the buffer to write + * @param length the length of the buffer in bytes + * @return the number of characters written + */ + virtual size_t write(void*, size_t) = 0; - /** - * Writes a character string. - * @param str the string to write - * @return the number of characters written - */ - virtual size_t write(const char*) = 0; + /** + * Writes a character string. + * @param str the string to write + * @return the number of characters written + */ + virtual size_t write(const char*) = 0; - /** - * Writes a number with base 10. - * @param number the number to write - * @return the number of digits written - */ - virtual size_t write(unsigned long) = 0; + /** + * Writes a number with base 10. + * @param number the number to write + * @return the number of digits written + */ + virtual size_t write(unsigned long) = 0; }; #endif
--- a/AbstractDataSource.h Thu Oct 23 14:41:58 2014 +0200 +++ b/AbstractDataSource.h Sat Nov 15 11:21:01 2014 +0100 @@ -74,28 +74,28 @@ */ class AbstractDataSource { -public: - virtual ~AbstractDataSource() { }; + public: + virtual ~AbstractDataSource() { }; - /** - * Reads one character. When successful, the returned value - * represents the character. Otherwise, status() will return a - * non-zero value. This function is blocking. - * @return the caracter read or zero. If the return value is - * non-zero, status() is zero. Otherwise, check status() for a - * non-zero value. - */ - virtual char read() = 0; + /** + * Reads one character. When successful, the returned value + * represents the character. Otherwise, status() will return a + * non-zero value. This function is blocking. + * @return the caracter read or zero. If the return value is + * non-zero, status() is zero. Otherwise, check status() for a + * non-zero value. + */ + virtual char read() = 0; - /** - * Returns the current read state. If a non-zero value is returned, - * there is an error. Otherwise, everything is ok. - * DS_STATUS_TIMEOUT means that there has been a read timeout. - * DS_STATUS_CLOSED means that the connection has been closed - * by either this or the foreign host. - * @return the status value - */ - virtual uint8_t status() = 0; + /** + * Returns the current read state. If a non-zero value is returned, + * there is an error. Otherwise, everything is ok. + * DS_STATUS_TIMEOUT means that there has been a read timeout. + * DS_STATUS_CLOSED means that the connection has been closed + * by either this or the foreign host. + * @return the status value + */ + virtual uint8_t status() = 0; }; #endif
--- a/AbstractSmartRest.h Thu Oct 23 14:41:58 2014 +0200 +++ b/AbstractSmartRest.h Sat Nov 15 11:21:01 2014 +0100 @@ -59,89 +59,89 @@ */ class AbstractSmartRest { -public: - virtual ~AbstractSmartRest() { }; + public: + virtual ~AbstractSmartRest() { }; - /** - * Sets the client authorization. - * @param username the authorization username - * @param password the authorization password - */ - virtual uint8_t setAuthorization(const char*, const char*) = 0; + /** + * Sets the client authorization. + * @param username the authorization username + * @param password the authorization password + */ + virtual uint8_t setAuthorization(const char*, const char*) = 0; #ifdef SMARTREST_TRANSACTIONAL - /** - * Performs a SmartRest request without receiving any data from the - * server. - * @param generator the generator which will generate the data to be - * sent as a template. - * @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 - */ - virtual uint8_t request(const DataGenerator&, const char* = NULL) = 0; + /** + * Performs a SmartRest request without receiving any data from the + * server. + * @param generator the generator which will generate the data to be + * sent as a template. + * @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 + */ + virtual uint8_t request(const DataGenerator&, const char* = NULL) = 0; - /** - * Performs a SmartRest request. - * @param generator the generator which will generate the data to be - * sent as a template. - * @param aggregator the aggregator where all received records will - * be stored. The aggregator must be managed, - * otherwise an error will be returned. - * @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 - */ - virtual uint8_t request(const DataGenerator&, Aggregator&, const char* = NULL) = 0; + /** + * Performs a SmartRest request. + * @param generator the generator which will generate the data to be + * sent as a template. + * @param aggregator the aggregator where all received records will + * be stored. The aggregator must be managed, + * otherwise an error will be returned. + * @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 + */ + virtual uint8_t request(const DataGenerator&, Aggregator&, const char* = NULL) = 0; #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 - */ - virtual uint8_t bootstrap(const DataGenerator&) = 0; + /* + * 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 + */ + virtual uint8_t bootstrap(const DataGenerator&) = 0; #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 - */ - virtual uint8_t send(const DataGenerator&, const char* = NULL) = 0; + /** + * 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 + */ + virtual uint8_t send(const DataGenerator&, const char* = NULL) = 0; - /** - * 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 - */ - virtual uint8_t receive(ParsedRecord&) = 0; + /** + * 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 + */ + virtual uint8_t receive(ParsedRecord&) = 0; - /* - * Closes the connection. - */ - virtual void stop() = 0; + /* + * Closes the connection. + */ + virtual void stop() = 0; #endif - /* - * Retrieves the template identifier. - * @return template identifier - */ - virtual const char * getIdentifier() = 0; + /* + * Retrieves the template identifier. + * @return template identifier + */ + virtual const char * getIdentifier() = 0; }; #endif
--- a/Aggregator.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/Aggregator.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -29,147 +29,154 @@ #include "Aggregator.h" #include <stdlib.h> +/*-------------------------------------------------------------------------*/ #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE Aggregator::Aggregator(size_t capacity, bool growing, bool managed) : - _initial(capacity), - _growing(growing), - _managed(managed) + _initial(capacity), + _growing(growing), + _managed(managed) { - _capacity = 0; - _list = NULL; - _length = 0; + _capacity = 0; + _list = NULL; + _length = 0; } #else Aggregator::Aggregator(bool managed) : - _managed(managed) + _managed(managed) { - _length = 0; + _length = 0; } #endif - +/*-------------------------------------------------------------------------*/ Aggregator::~Aggregator() { - if (_managed) { - for (size_t n = 0; n < _length; n++) - delete _list[n]; - } - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - if (_list != NULL) - free(_list); - #endif + if (_managed) + { + for (size_t n = 0; n < _length; n++) + delete _list[n]; + } +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + if (_list != NULL) + free(_list); +#endif } - +/*-------------------------------------------------------------------------*/ bool Aggregator::add(const Record& record) { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - if (_length == _capacity) { - size_t capacity; +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + if (_length == _capacity) + { + size_t capacity; - if ((!_growing) && (_capacity != 0)) - return false; + if ((!_growing) && (_capacity != 0)) + return false; - if (_capacity == 0) - capacity = _initial; - else - capacity = _capacity + SMARTREST_AGGREGATOR_MEMORY_INCREMENT; + if (_capacity == 0) + capacity = _initial; + else + capacity = _capacity + SMARTREST_AGGREGATOR_MEMORY_INCREMENT; - const Record **list = (const Record**)realloc(_list, - capacity*sizeof(Record*)); - if (list == NULL) - return false; - _list = list; - _capacity = capacity; - } - #else - if (_length == SMARTREST_AGGREGATOR_FIXED_SIZE) - return false; - #endif + const Record **list = (const Record**)realloc(_list, + capacity*sizeof(Record*)); + if (list == NULL) + return false; + _list = list; + _capacity = capacity; + } +#else + if (_length == SMARTREST_AGGREGATOR_FIXED_SIZE) + return false; +#endif - if (_managed) { - Record *copy = record.copy(); - if (copy == NULL) - return false; - _list[_length++] = copy; - } else { - _list[_length++] = &record; - } - return true; + if (_managed) + { + Record *copy = record.copy(); + if (copy == NULL) + return false; + _list[_length++] = copy; + } + else + { + _list[_length++] = &record; + } + return true; } - +/*-------------------------------------------------------------------------*/ const Record& Aggregator::get(size_t index) const { - return *_list[index]; + return *_list[index]; } - +/*-------------------------------------------------------------------------*/ void Aggregator::clear() { - if (_managed) { - for (size_t n = 0; n < _length; n++) - delete _list[n]; - } - _length = 0; + if (_managed) + { + for (size_t n = 0; n < _length; n++) + delete _list[n]; + } + _length = 0; - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - if (_list != NULL) - free(_list); - _list = NULL; - _capacity = 0; - #endif +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + if (_list != NULL) + free(_list); + _list = NULL; + _capacity = 0; +#endif } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::length() const { - return _length; + return _length; } - +/*-------------------------------------------------------------------------*/ bool Aggregator::full() const { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - return (_growing) ? false : (_length == _capacity); - #else - return (_length == SMARTREST_AGGREGATOR_FIXED_SIZE); - #endif +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + return (_growing) ? false : (_length == _capacity); +#else + return (_length == SMARTREST_AGGREGATOR_FIXED_SIZE); +#endif } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::capacity() const { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - return (_growing) ? 0 : _capacity; - #else - return SMARTREST_AGGREGATOR_FIXED_SIZE; - #endif +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + return (_growing) ? 0 : _capacity; +#else + return SMARTREST_AGGREGATOR_FIXED_SIZE; +#endif } - +/*-------------------------------------------------------------------------*/ bool Aggregator::managed() const { - return _managed; + return _managed; } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::writeTo(AbstractDataSink& sink) const { - size_t len = 0; - for (size_t n = 0; n < _length; n++) - len += _list[n]->writeTo(sink); - return len; + size_t len = 0; + for (size_t n = 0; n < _length; n++) + len += _list[n]->writeTo(sink); + return len; } - +/*-------------------------------------------------------------------------*/ size_t Aggregator::writtenLength() const { - size_t len = 0; - for (size_t n = 0; n < _length; n++) - len += _list[n]->writtenLength(); - return len; + size_t len = 0; + for (size_t n = 0; n < _length; n++) + len += _list[n]->writtenLength(); + return len; } - +/*-------------------------------------------------------------------------*/ Aggregator* Aggregator::copy() const { - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - Aggregator *copy = new Aggregator(_length, _growing, true); - #else - Aggregator *copy = new Aggregator(true); - #endif - for (size_t n = 0; n < _length; n++) - copy->add(*_list[n]); - return copy; +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + Aggregator *copy = new Aggregator(_length, _growing, true); +#else + Aggregator *copy = new Aggregator(true); +#endif + for (size_t n = 0; n < _length; n++) + copy->add(*_list[n]); + return copy; } - +/*-------------------------------------------------------------------------*/
--- a/Aggregator.h Thu Oct 23 14:41:58 2014 +0200 +++ b/Aggregator.h Sat Nov 15 11:21:01 2014 +0100 @@ -79,88 +79,88 @@ */ class Aggregator : public DataGenerator { -public: - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - /** - * Creates a new Aggregator instance. - * @param capacity the initial capacity of the instance - * @param growing specifies the capability of this instance to grow - * @param managed specifies whether internal memory management shall be - * used. If true, all added records will be copied to - * the heap and freed accordingly. - */ - Aggregator(size_t = SMARTREST_AGGREGATOR_INITIAL_CAPACITY, bool = true, bool = false); - #else - /** - * Creates a new Aggregator instance. - * @param managed specifies whether internal memory management shall be - * used. If true, all added records will be copied to - * the heap and freed accordingly. - */ - Aggregator(bool = false); - #endif - ~Aggregator(); + public: +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + /** + * Creates a new Aggregator instance. + * @param capacity the initial capacity of the instance + * @param growing specifies the capability of this instance to grow + * @param managed specifies whether internal memory management shall be + * used. If true, all added records will be copied to + * the heap and freed accordingly. + */ + Aggregator(size_t = SMARTREST_AGGREGATOR_INITIAL_CAPACITY, bool = true, bool = false); +#else + /** + * Creates a new Aggregator instance. + * @param managed specifies whether internal memory management shall be + * used. If true, all added records will be copied to + * the heap and freed accordingly. + */ + Aggregator(bool = false); +#endif + ~Aggregator(); - /** - * Adds a record to the aggregator. - * @param record the record to add - * @return true if added, false otherwise. - */ - bool add(const Record&); + /** + * Adds a record to the aggregator. + * @param record the record to add + * @return true if added, false otherwise. + */ + bool add(const Record&); - /** - * Retrieves the nth record from the aggregator. - * @param index the index of the nth element to retrieve - */ - const Record& get(size_t) const; + /** + * Retrieves the nth record from the aggregator. + * @param index the index of the nth element to retrieve + */ + const Record& get(size_t) const; - /** - * Clears the aggregator. The capacity will shrink to it's initial - * size. - */ - void clear(); + /** + * Clears the aggregator. The capacity will shrink to it's initial + * size. + */ + void clear(); - /** - * Returns the number of records aggregated by this instance. - * @return the number of records aggregated - */ - size_t length() const; + /** + * Returns the number of records aggregated by this instance. + * @return the number of records aggregated + */ + size_t length() const; - /** - * Returns whether the aggregator is full. If growing, this will - * always return false. - * @return whether the aggregator is full - */ - bool full() const; + /** + * Returns whether the aggregator is full. If growing, this will + * always return false. + * @return whether the aggregator is full + */ + bool full() const; - /** - * Returns the capacity of the aggregator. This will always return zero - * if the aggregator is growing. - */ - size_t capacity() const; + /** + * Returns the capacity of the aggregator. This will always return zero + * if the aggregator is growing. + */ + size_t capacity() const; - /** - * Returns whether this aggregator is using internal memory management. - * @return whether internal memory management is being used - */ - bool managed() const; + /** + * Returns whether this aggregator is using internal memory management. + * @return whether internal memory management is being used + */ + bool managed() const; - size_t writeTo(AbstractDataSink&) const; - size_t writtenLength() const; - Aggregator* copy() const; + size_t writeTo(AbstractDataSink&) const; + size_t writtenLength() const; + Aggregator* copy() const; -private: - #ifdef SMARTREST_AGGREGATOR_FIXED_SIZE - const Record *_list[SMARTREST_AGGREGATOR_FIXED_SIZE]; - #else - const Record **_list; - #endif - size_t _length; - #ifndef SMARTREST_AGGREGATOR_FIXED_SIZE - size_t _capacity, _initial; - bool _growing; - #endif - bool _managed; + private: +#ifdef SMARTREST_AGGREGATOR_FIXED_SIZE + const Record *_list[SMARTREST_AGGREGATOR_FIXED_SIZE]; +#else + const Record **_list; +#endif + size_t _length; +#ifndef SMARTREST_AGGREGATOR_FIXED_SIZE + size_t _capacity, _initial; + bool _growing; +#endif + bool _managed; }; #endif
--- a/CharValue.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/CharValue.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -32,122 +32,133 @@ #include <string.h> #include <ctype.h> + +/*-------------------------------------------------------------------------*/ CharValue::CharValue(const char *str, bool copy) { - if ((str == NULL) || (*str == '\0')) { - _alloc = false; - _str = NULL; - } else { - _alloc = copy; - if (copy) { - _str = (const char*)malloc(strlen(str)*sizeof(char)); - if (_str == NULL) - return; - strcpy((char*)_str, str); - _escape = escapeCheck(); - } else { - _str = str; - _escape = escapeCheck(); - } - } + if ((str == NULL) || (*str == '\0')) + { + _alloc = false; + _str = NULL; + } + else + { + _alloc = copy; + if (copy) + { + _str = (const char*)malloc(strlen(str)*sizeof(char)); + if (_str == NULL) + return; + strcpy((char*)_str, str); + _escape = escapeCheck(); + } + else + { + _str = str; + _escape = escapeCheck(); + } + } } - +/*-------------------------------------------------------------------------*/ CharValue::~CharValue() { - if (_alloc) - free((void*)_str); + if (_alloc) + free((void*)_str); } - +/*-------------------------------------------------------------------------*/ uint8_t CharValue::valueType() const { - if (_str == NULL) - return VALUE_NULL; - return VALUE_CHARACTER; + if (_str == NULL) + return VALUE_NULL; + return VALUE_CHARACTER; } - +/*-------------------------------------------------------------------------*/ long CharValue::integerValue() const { - return 0; + return 0; } - +/*-------------------------------------------------------------------------*/ double CharValue::floatValue() const { - return 0.0; + return 0.0; } - +/*-------------------------------------------------------------------------*/ const char * CharValue::characterValue() const { - return _str; + return _str; } - +/*-------------------------------------------------------------------------*/ size_t CharValue::write(AbstractDataSink& sink) const { - size_t n; - const char *ptr; char c; + size_t n; + const char *ptr; char c; - if (_str == NULL) - return 0; + if (_str == NULL) + return 0; - n = 0; - if (_escape) - n += sink.write('"'); + n = 0; + if (_escape) + n += sink.write('"'); - for (ptr = _str; (c = *ptr) != '\0'; ptr++) { - if ((_escape) && (c == '"')) - n += sink.write('"'); - n += sink.write(c); - } + for (ptr = _str; (c = *ptr) != '\0'; ptr++) + { + if ((_escape) && (c == '"')) + n += sink.write('"'); + n += sink.write(c); + } - if (_escape) - n += sink.write('"'); - return n; + if (_escape) + n += sink.write('"'); + return n; } - +/*-------------------------------------------------------------------------*/ size_t CharValue::length() const { - size_t n; - const char *ptr; char c; + size_t n; + const char *ptr; char c; - if (_str == NULL) - return 0; + if (_str == NULL) + return 0; - n = 0; - if (_escape) - n += 2; + n = 0; + if (_escape) + n += 2; - for (ptr = _str; (c = *ptr) != '\0'; ptr++) { - if ((_escape) && (c == '"')) - n++; - n++; - } + for (ptr = _str; (c = *ptr) != '\0'; ptr++) + { + if ((_escape) && (c == '"')) + n++; + n++; + } - return n; + return n; } - +/*-------------------------------------------------------------------------*/ Value* CharValue::copy() const { - if (_str == NULL) - return new NullValue(); - return new CharValue(_str, true); + if (_str == NULL) + return new NullValue(); + return new CharValue(_str, true); } - +/*-------------------------------------------------------------------------*/ bool CharValue::escapeCheck() { - const char *ptr; char c; + const char *ptr; char c; - ptr = _str; - while (*ptr != '\0') { - c = *ptr; - if ((isspace(c)) && ((ptr == _str) || (c != ' '))) - return true; - else if ((c == '"') || (c == ',')) - return true; - ptr++; - } + ptr = _str; + while (*ptr != '\0') + { + c = *ptr; + if ((isspace(c)) && ((ptr == _str) || (c != ' '))) + return true; + else if ((c == '"') || (c == ',')) + return true; + ptr++; + } - if (isspace(c)) - return true; + if (isspace(c)) + return true; - return false; + return false; } - +/*-------------------------------------------------------------------------*/
--- a/CharValue.h Thu Oct 23 14:41:58 2014 +0200 +++ b/CharValue.h Sat Nov 15 11:21:01 2014 +0100 @@ -50,25 +50,25 @@ */ class CharValue : public Value { -public: - CharValue(const char*, bool = false); - ~CharValue(); + public: + CharValue(const char*, bool = false); + ~CharValue(); - uint8_t valueType() const; - long integerValue() const; - double floatValue() const; - const char * characterValue() const; + uint8_t valueType() const; + long integerValue() const; + double floatValue() const; + const char * characterValue() const; - size_t write(AbstractDataSink&) const; - size_t length() const; - Value* copy() const; + size_t write(AbstractDataSink&) const; + size_t length() const; + Value* copy() const; -protected: - bool escapeCheck(); + protected: + bool escapeCheck(); -private: - const char *_str; - bool _escape, _alloc; + private: + const char *_str; + bool _escape, _alloc; }; #endif
--- a/ComposedRecord.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/ComposedRecord.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -30,92 +30,100 @@ #include <stdlib.h> #include <string.h> + +/*-------------------------------------------------------------------------*/ ComposedRecord::ComposedRecord(bool copy) { - _alloc = copy; - #ifndef SMARTREST_COMPOSED_FIXED_SIZE - _capacity = 0; - _values = NULL; - #endif - _count = 0; + _alloc = copy; +#ifndef SMARTREST_COMPOSED_FIXED_SIZE + _capacity = 0; + _values = NULL; +#endif + _count = 0; } - +/*-------------------------------------------------------------------------*/ ComposedRecord::~ComposedRecord() { - if (_alloc) { - for (size_t n = 0; n < _count; n++) - delete _values[n]; - } - #ifndef SMARTREST_COMPOSED_FIXED_SIZE - if (_values != NULL) - free(_values); - #endif + if (_alloc) + { + for (size_t n = 0; n < _count; n++) + delete _values[n]; + } +#ifndef SMARTREST_COMPOSED_FIXED_SIZE + if (_values != NULL) + free(_values); +#endif } - +/*-------------------------------------------------------------------------*/ bool ComposedRecord::add(const Value& value) { - #ifndef SMARTREST_COMPOSED_FIXED_SIZE - if (_capacity == _count) { - size_t capacity; - if (_capacity == 0) - capacity = SMARTREST_COMPOSED_INITIAL_CAPACITY; - else - capacity = _capacity + SMARTREST_COMPOSED_MEMORY_INCREMENT; +#ifndef SMARTREST_COMPOSED_FIXED_SIZE + if (_capacity == _count) + { + size_t capacity; + if (_capacity == 0) + capacity = SMARTREST_COMPOSED_INITIAL_CAPACITY; + else + capacity = _capacity + SMARTREST_COMPOSED_MEMORY_INCREMENT; - const Value **values = (const Value**)realloc(_values, - capacity*sizeof(Value*)); - if (values == NULL) - return false; - _values = values; - _capacity = capacity; - } - #else - if (_count == SMARTREST_COMPOSED_FIXED_SIZE) - return false; - #endif + const Value **values = (const Value**)realloc(_values, + capacity*sizeof(Value*)); + if (values == NULL) + return false; + _values = values; + _capacity = capacity; + } +#else + if (_count == SMARTREST_COMPOSED_FIXED_SIZE) + return false; +#endif - if (_alloc) { - Value *copy = value.copy(); - if (copy == NULL) - return false; - _values[_count++] = copy; - } else { - _values[_count++] = &value; - } - return true; + if (_alloc) + { + Value *copy = value.copy(); + if (copy == NULL) + return false; + _values[_count++] = copy; + } + else + { + _values[_count++] = &value; + } + return true; } - +/*-------------------------------------------------------------------------*/ void ComposedRecord::clear() { - if (_alloc) { - for (size_t n = 0; n < _count; n++) - delete _values[n]; - } - _count = 0; + if (_alloc) + { + for (size_t n = 0; n < _count; n++) + delete _values[n]; + } + _count = 0; - #ifndef SMARTREST_COMPOSED_FIXED_SIZE - if (_values != NULL) - free(_values); - _values = NULL; - _capacity = 0; - #endif +#ifndef SMARTREST_COMPOSED_FIXED_SIZE + if (_values != NULL) + free(_values); + _values = NULL; + _capacity = 0; +#endif } - +/*-------------------------------------------------------------------------*/ size_t ComposedRecord::values() const { - return _count; + return _count; } - +/*-------------------------------------------------------------------------*/ const Value& ComposedRecord::value(size_t index) const { - return *_values[index]; + return *_values[index]; } - +/*-------------------------------------------------------------------------*/ ComposedRecord* ComposedRecord::copy() const { - ComposedRecord *copy = new ComposedRecord(true); - for (size_t n = 0; n < _count; n++) - copy->add(*_values[n]); - return copy; + ComposedRecord *copy = new ComposedRecord(true); + for (size_t n = 0; n < _count; n++) + copy->add(*_values[n]); + return copy; } - +/*-------------------------------------------------------------------------*/
--- a/ComposedRecord.h Thu Oct 23 14:41:58 2014 +0200 +++ b/ComposedRecord.h Sat Nov 15 11:21:01 2014 +0100 @@ -68,27 +68,27 @@ */ class ComposedRecord : public Record { -public: - ComposedRecord(bool = false); - ~ComposedRecord(); + public: + ComposedRecord(bool = false); + ~ComposedRecord(); - bool add(const Value&); - void clear(); + bool add(const Value&); + void clear(); - size_t values() const; - const Value& value(size_t) const; + size_t values() const; + const Value& value(size_t) const; - ComposedRecord* copy() const; + ComposedRecord* copy() const; -private: - #ifndef SMARTREST_COMPOSED_FIXED_SIZE - const Value **_values; - size_t _capacity; - #else - const Value *_values[SMARTREST_COMPOSED_FIXED_SIZE]; - #endif - size_t _count; - bool _alloc; + private: +#ifndef SMARTREST_COMPOSED_FIXED_SIZE + const Value **_values; + size_t _capacity; +#else + const Value *_values[SMARTREST_COMPOSED_FIXED_SIZE]; +#endif + size_t _count; + bool _alloc; }; #endif
--- a/DataGenerator.h Thu Oct 23 14:41:58 2014 +0200 +++ b/DataGenerator.h Sat Nov 15 11:21:01 2014 +0100 @@ -39,31 +39,31 @@ */ class DataGenerator { -public: - virtual ~DataGenerator() - { - } + public: + virtual ~DataGenerator() + { + } - /** - * Writes the object to the specified destination. - * @param s the destination - * @return the number of bytes written - */ - virtual size_t writeTo(AbstractDataSink&) const = 0; + /** + * Writes the object to the specified destination. + * @param s the destination + * @return the number of bytes written + */ + virtual size_t writeTo(AbstractDataSink&) const = 0; - /** - * Returns the estimated number of bytes which will be written. - * @return the estimated number of bytes printed - */ - virtual size_t writtenLength() const = 0; + /** + * Returns the estimated number of bytes which will be written. + * @return the estimated number of bytes printed + */ + virtual size_t writtenLength() const = 0; - /** - * Creates a deep copy on the heap. The class type may be different. - * The only thing guaranteed is that the copy behaves exactly - * the same. - * @return cloned object with no memory dependencies - */ - virtual DataGenerator* copy() const = 0; + /** + * Creates a deep copy on the heap. The class type may be different. + * The only thing guaranteed is that the copy behaves exactly + * the same. + * @return cloned object with no memory dependencies + */ + virtual DataGenerator* copy() const = 0; }; #endif
--- a/FloatValue.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/FloatValue.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -30,155 +30,167 @@ #include <math.h> #include <stdio.h> + +/*-------------------------------------------------------------------------*/ FloatValue::FloatValue(double number, uint8_t digits, bool zflag) { - _negative = (number < 0.0); - _zflag = zflag; - if (_negative) - _number = -number; - else - _number = number; - if (digits == SMARTREST_FLOATVALUE_DETECTPREC) - _digits = detectPrecision(_number); - else - _digits = digits; + _negative = (number < 0.0); + _zflag = zflag; + if (_negative) + _number = -number; + else + _number = number; + if (digits == SMARTREST_FLOATVALUE_DETECTPREC) + _digits = detectPrecision(_number); + else + _digits = digits; } - +/*-------------------------------------------------------------------------*/ uint8_t FloatValue::valueType() const { - return VALUE_FLOAT; + return VALUE_FLOAT; } - +/*-------------------------------------------------------------------------*/ long FloatValue::integerValue() const { - return 0L; + return 0L; } - +/*-------------------------------------------------------------------------*/ double FloatValue::floatValue() const { - return (_negative) ? -_number : _number; + return (_negative) ? -_number : _number; } - +/*-------------------------------------------------------------------------*/ const char * FloatValue::characterValue() const { - return 0; + return 0; } - +/*-------------------------------------------------------------------------*/ size_t FloatValue::write(AbstractDataSink& sink) const { - size_t n = 0; - double number, rounding; - uint8_t i; - unsigned long int_part; - double remainder; + size_t n = 0; + double number, rounding; + uint8_t i; + unsigned long int_part; + double remainder; + + if (isnan(_number)) + return sink.write("nan"); - if (isnan(_number)) - return sink.write("nan"); + if (_negative) + n += sink.write('-'); - if (_negative) - n += sink.write('-'); - - if (isinf(_number)) { - n += sink.write("inf"); - return n; - } + if (isinf(_number)) + { + n += sink.write("inf"); + return n; + } - number = _number; - rounding = 0.5; - for (i = 0; i < _digits; ++i) - rounding /= 10.0; - number += rounding; + number = _number; + rounding = 0.5; + for (i = 0; i < _digits; ++i) + rounding /= 10.0; + number += rounding; - int_part = (unsigned long)number; - remainder = number - (double)int_part; + int_part = (unsigned long)number; + remainder = number - (double)int_part; - if ((_zflag) || (int_part)) - n += sink.write(int_part); + if ((_zflag) || (int_part)) + n += sink.write(int_part); - if (_digits == 0) - return n; - n += sink.write("."); + if (_digits == 0) + return n; + n += sink.write("."); - uint8_t digits = _digits; - while (digits-- > 0) { - remainder *= 10.0; - unsigned long l = (unsigned long)remainder; - n += sink.write(l); - remainder -= l; - } + uint8_t digits = _digits; + while (digits-- > 0) + { + remainder *= 10.0; + unsigned long l = (unsigned long)remainder; + n += sink.write(l); + remainder -= l; + } - return n; + return n; } - +/*-------------------------------------------------------------------------*/ size_t FloatValue::length() const { - size_t l = 0; - double number, rounding; - unsigned long n; - uint8_t i; + size_t l = 0; + double number, rounding; + unsigned long n; + uint8_t i; - if (isnan(_number)) - return 3; + if (isnan(_number)) + return 3; - if (_negative) - l++; + if (_negative) + l++; - if (isinf(_number)) { - l += 3; - return l; - } + if (isinf(_number)) + { + l += 3; + return l; + } - number = _number; - rounding = 0.5; - for (i = 0; i < _digits; ++i) - rounding /= 10.0; - number += rounding; + number = _number; + rounding = 0.5; + for (i = 0; i < _digits; ++i) + rounding /= 10.0; + number += rounding; - n = (unsigned long)number; - if ((_zflag) || (n)) { - do { - n /= 10; - l++; - } while(n); - } + n = (unsigned long)number; + if ((_zflag) || (n)) + { + do + { + n /= 10; + l++; + } while(n); + } - if (_digits > 0) - l += 1 + _digits; + if (_digits > 0) + l += 1 + _digits; - return l; + return l; } - +/*-------------------------------------------------------------------------*/ Value* FloatValue::copy() const { - double number; + double number; - number = (_negative) ? -_number : _number; - return new FloatValue(number, _digits, _zflag); + number = (_negative) ? -_number : _number; + return new FloatValue(number, _digits, _zflag); } - +/*-------------------------------------------------------------------------*/ uint8_t FloatValue::detectPrecision(double n) { - uint8_t prec = 0, count = 0; - int8_t d; + uint8_t prec = 0, count = 0; + int8_t d; - if ((isinf(n)) || (isnan(n))) - return 0; + if ((isinf(n)) || (isnan(n))) + return 0; - n -= (long)n; + n -= (long)n; - while ((prec < 6) && (count < 3)) { - n *= 10; - d = (uint8_t)n; + while ((prec < 6) && (count < 3)) + { + n *= 10; + d = (uint8_t)n; - if (count == 0) { - if ((d <= 0) || (d >= 9)) - count++; - } else { - count++; - } - prec++; - n -= d; - } + if (count == 0) + { + if ((d <= 0) || (d >= 9)) + count++; + } + else + { + count++; + } + prec++; + n -= d; + } - return --prec; + return --prec; } +/*-------------------------------------------------------------------------*/
--- a/FloatValue.h Thu Oct 23 14:41:58 2014 +0200 +++ b/FloatValue.h Sat Nov 15 11:21:01 2014 +0100 @@ -50,32 +50,32 @@ */ class FloatValue : public Value { -public: - /** - * Creates a new instance of a floating-point value. - * @param number the floating-point number - * @param digits precision in number of digits - * @param zflag whether to write a zero before the floating point - */ - FloatValue(double, uint8_t = SMARTREST_FLOATVALUE_DETECTPREC, bool = true); + public: + /** + * Creates a new instance of a floating-point value. + * @param number the floating-point number + * @param digits precision in number of digits + * @param zflag whether to write a zero before the floating point + */ + FloatValue(double, uint8_t = SMARTREST_FLOATVALUE_DETECTPREC, bool = true); - uint8_t valueType() const; - long integerValue() const; - double floatValue() const; - const char * characterValue() const; + uint8_t valueType() const; + long integerValue() const; + double floatValue() const; + const char * characterValue() const; - size_t write(AbstractDataSink&) const; - size_t length() const; - Value* copy() const; + size_t write(AbstractDataSink&) const; + size_t length() const; + Value* copy() const; -protected: - uint8_t detectPrecision(double); + protected: + uint8_t detectPrecision(double); -private: - double _number; - bool _negative; - uint8_t _digits; - bool _zflag; + private: + double _number; + bool _negative; + uint8_t _digits; + bool _zflag; }; #endif
--- a/IntegerValue.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/IntegerValue.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -28,65 +28,68 @@ #include "IntegerValue.h" + +/*-------------------------------------------------------------------------*/ IntegerValue::IntegerValue(long number) { - _negative = (number < 0L); - if (_negative) - _number = -number; - else - _number = number; + _negative = (number < 0L); + if (_negative) + _number = -number; + else + _number = number; } - +/*-------------------------------------------------------------------------*/ uint8_t IntegerValue::valueType() const { - return VALUE_INTEGER; + return VALUE_INTEGER; } - +/*-------------------------------------------------------------------------*/ long IntegerValue::integerValue() const { - return (_negative) ? -_number : _number; + return (_negative) ? -_number : _number; } - +/*-------------------------------------------------------------------------*/ double IntegerValue::floatValue() const { - return 0.0; + return 0.0; } - +/*-------------------------------------------------------------------------*/ const char * IntegerValue::characterValue() const { - return 0; + return 0; } - +/*-------------------------------------------------------------------------*/ size_t IntegerValue::write(AbstractDataSink& sink) const { - size_t n = 0; - if (_negative) - n += sink.write('-'); - n += sink.write(_number); - return n; + size_t n = 0; + if (_negative) + n += sink.write('-'); + n += sink.write(_number); + return n; } - +/*-------------------------------------------------------------------------*/ size_t IntegerValue::length() const { - size_t l = 0; + size_t l = 0; - if (_negative) - l++; + if (_negative) + l++; - unsigned long n = _number; + unsigned long n = _number; - do { - n /= 10; - l++; - } while(n); - return l; + do + { + n /= 10; + l++; + } while(n); + return l; } - +/*-------------------------------------------------------------------------*/ Value* IntegerValue::copy() const { - long number; + long number; - number = (_negative) ? -_number : _number; - return new IntegerValue(number); + number = (_negative) ? -_number : _number; + return new IntegerValue(number); } - +/*-------------------------------------------------------------------------*/
--- a/IntegerValue.h Thu Oct 23 14:41:58 2014 +0200 +++ b/IntegerValue.h Sat Nov 15 11:21:01 2014 +0100 @@ -42,21 +42,21 @@ */ class IntegerValue : public Value { -public: - IntegerValue(long); + public: + IntegerValue(long); - uint8_t valueType() const; - long integerValue() const; - double floatValue() const; - const char * characterValue() const; + uint8_t valueType() const; + long integerValue() const; + double floatValue() const; + const char * characterValue() const; - size_t write(AbstractDataSink&) const; - size_t length() const; - Value* copy() const; + size_t write(AbstractDataSink&) const; + size_t length() const; + Value* copy() const; -private: - unsigned long _number; - bool _negative; + private: + unsigned long _number; + bool _negative; }; #endif
--- a/MbedClient.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/MbedClient.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -47,6 +47,7 @@ _port(port), _tries(tries), _state(STATE_INIT), + _isStreamRequest(false), _filter(_source), _source(_sock), _sink(_sock), @@ -71,36 +72,36 @@ uint8_t MbedClient::beginRequest() { - uint8_t tries; - if (_state != STATE_INIT) return internalError(); - tries = _tries; - do { - MBCL_DBG("Connecting to %s:%u", _host, _port); - if (_sock.connect(_host, _port) >= 0) - break; - _sock.close(); - MBCL_DBG("Connection atempt failed."); - } while (--tries > 0); + MBCL_DBG("Beginning SmartREST request."); + _source.setTimeout(60000); + if (!connect()) + return connectionError(); - if (tries == 0) + if (!sendRequestHeader("/s")) return connectionError(); - MBCL_DBG("Sending request header."); + _state = STATE_IN_REQUEST; + return CLIENT_OK; +} - if ((!send("POST /s HTTP/1.0\r\n")) || - (!send("Host: ")) || - (!send(_host)) || - (!send("\r\n"))) +uint8_t MbedClient::beginStream(const char *uri) +{ + if (_state != STATE_INIT) + return internalError(); + + // set stream request flag to later set the timeout right + _isStreamRequest = true; + + MBCL_DBG("Beginning SmartREST request."); + _source.setTimeout(60000); + if (!connect()) return connectionError(); - - if ((_username != NULL) && (strlen(_username) > 0) && - (_password != NULL) && (strlen(_password) > 0)) { - if (!sendBasicAuth()) - return connectionError(); - } + + if (!sendRequestHeader(uri)) + return connectionError(); _state = STATE_IN_REQUEST; return CLIENT_OK; @@ -180,6 +181,10 @@ if ((status != 200) || (!_filter.skipHeaders())) return connectionError(); + + // set timeout to fifteen minutes if stream request flag set + if (_isStreamRequest) + _source.setTimeout(900000); _state = STATE_RECVD_RESPONSE; return CLIENT_OK; @@ -193,6 +198,7 @@ void MbedClient::stop() { MBCL_DBG("Resetting client."); + _isStreamRequest = false; _sock.close(); _source.reset(); _sink.reset(); @@ -200,16 +206,51 @@ _state = STATE_INIT; } +bool MbedClient::connect() +{ + uint8_t tries; + + tries = _tries; + do { + MBCL_DBG("Connecting to %s:%u", _host, _port); + if (_sock.connect(_host, _port) >= 0) + break; + _sock.close(); + MBCL_DBG("Connection atempt failed."); + } while (--tries > 0); + + return (tries > 0); +} + bool MbedClient::send(const char *str) { return (_sink.write(str) == strlen(str)); } +bool MbedClient::sendRequestHeader(const char *uri) +{ + MBCL_DBG("Sending request header."); + if ((!send("POST ")) || + (!send(uri)) || + (!send("HTTP/1.0\r\n")) || + (!send("Host: ")) || + (!send(_host)) || + (!send("\r\n"))) + return false; + + return sendBasicAuth(); +} + bool MbedClient::sendBasicAuth() { size_t ul, pl; unsigned char input[3]; unsigned char output[5]; int inputOffset = 0; + // no need to send authorization if not specified + if ((_username == NULL) || (strlen(_username) == 0) || + (_password == NULL) || (strlen(_password) == 0)) + return true; + if (!send("Authorization: Basic ")) return false;
--- a/MbedClient.h Thu Oct 23 14:41:58 2014 +0200 +++ b/MbedClient.h Sat Nov 15 11:21:01 2014 +0100 @@ -46,6 +46,7 @@ uint8_t setAuthorization(const char*, const char*); uint8_t beginRequest(); + uint8_t beginStream(const char*); uint8_t sendIdentifier(const char*); uint8_t sendData(const DataGenerator& generator); uint8_t endRequest(); @@ -54,7 +55,9 @@ void stop(); protected: + bool connect(); bool send(const char *str); + bool sendRequestHeader(const char*); bool sendBasicAuth(); private: @@ -64,6 +67,7 @@ private: const char *_host, *_username, *_password; uint16_t _port, _tries, _state; + bool _isStreamRequest; TCPSocketConnection _sock; MbedDataSource _source; MbedDataSink _sink;
--- a/MbedDataSource.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/MbedDataSource.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -29,10 +29,12 @@ #include "MbedDataSource.h" #include "stdio.h" -MbedDataSource::MbedDataSource(TCPSocketConnection& sock) : _sock(sock) +MbedDataSource::MbedDataSource(TCPSocketConnection& sock) : + _sock(sock), + _timeout(0), + _isTimedOut(false) { _offset = _len = 0; - _timeout = false; } MbedDataSource::~MbedDataSource() @@ -54,12 +56,17 @@ if (!_sock.is_connected()) return DS_STATUS_CLOSED; - if (_timeout) + if (_isTimedOut) return DS_STATUS_TIMEOUT; return DS_STATUS_OK; } +void MbedDataSource::setTimeout(size_t timeout) +{ + _timeout = timeout; +} + bool MbedDataSource::receive() { int ret; @@ -67,11 +74,11 @@ if (status() != DS_STATUS_OK) return false; - _sock.set_blocking(true, 60000); + _sock.set_blocking(true, _timeout); ret = _sock.receive(_buf, MBED_SOURCE_BUFFER_SIZE); if (ret < 0) { - _timeout = true; + _isTimedOut = true; return false; } @@ -84,5 +91,5 @@ void MbedDataSource::reset() { _len = _offset = 0; - _timeout = false; + _isTimedOut = false; }
--- a/MbedDataSource.h Thu Oct 23 14:41:58 2014 +0200 +++ b/MbedDataSource.h Sat Nov 15 11:21:01 2014 +0100 @@ -47,14 +47,15 @@ uint8_t status(); protected: + void setTimeout(size_t); bool receive(); void reset(); private: TCPSocketConnection& _sock; char _buf[MBED_SOURCE_BUFFER_SIZE]; - size_t _len, _offset; - bool _timeout; + size_t _len, _offset, _timeout; + bool _isTimedOut; friend class MbedClient; };
--- a/NullValue.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/NullValue.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -28,44 +28,46 @@ #include "NullValue.h" + NullValue aNullValue; +/*-------------------------------------------------------------------------*/ NullValue::NullValue() { } - +/*-------------------------------------------------------------------------*/ uint8_t NullValue::valueType() const { - return VALUE_NULL; + return VALUE_NULL; } - +/*-------------------------------------------------------------------------*/ long NullValue::integerValue() const { - return 0; + return 0; } - +/*-------------------------------------------------------------------------*/ double NullValue::floatValue() const { - return 0.0; + return 0.0; } - +/*-------------------------------------------------------------------------*/ char * NullValue::characterValue() const { - return NULL; + return NULL; } - +/*-------------------------------------------------------------------------*/ size_t NullValue::write(AbstractDataSink&) const { - return 0; + return 0; } - +/*-------------------------------------------------------------------------*/ size_t NullValue::length() const { - return 0; + return 0; } - +/*-------------------------------------------------------------------------*/ Value* NullValue::copy() const { - return new NullValue(); + return new NullValue(); } - +/*-------------------------------------------------------------------------*/
--- a/NullValue.h Thu Oct 23 14:41:58 2014 +0200 +++ b/NullValue.h Sat Nov 15 11:21:01 2014 +0100 @@ -45,17 +45,17 @@ */ class NullValue : public Value { -public: - NullValue(); + public: + NullValue(); - uint8_t valueType() const; - long integerValue() const; - double floatValue() const; - char * characterValue() const; + uint8_t valueType() const; + long integerValue() const; + double floatValue() const; + char * characterValue() const; - size_t write(AbstractDataSink&) const; - size_t length() const; - Value* copy() const; + size_t write(AbstractDataSink&) const; + size_t length() const; + Value* copy() const; }; extern NullValue aNullValue;
--- a/ParsedRecord.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/ParsedRecord.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -32,98 +32,106 @@ #include <stdlib.h> #include <string.h> + +/*-------------------------------------------------------------------------*/ ParsedRecord::ParsedRecord(bool copy) : - _copy(copy) + _copy(copy) { - _count = 0; + _count = 0; #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC - _values = NULL; + _values = NULL; #endif - _buffer = NULL; + _buffer = NULL; } - +/*-------------------------------------------------------------------------*/ ParsedRecord::~ParsedRecord() { - clear(); + clear(); #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC - if (_values != NULL) - free(_values); + if (_values != NULL) + free(_values); #endif } - +/*-------------------------------------------------------------------------*/ size_t ParsedRecord::values() const { - return _count; + return _count; } - +/*-------------------------------------------------------------------------*/ const Value& ParsedRecord::value(size_t index) const { - return *_values[index]; + return *_values[index]; } - +/*-------------------------------------------------------------------------*/ ParsedRecord* ParsedRecord::copy() const { - ParsedRecord *copy = new ParsedRecord(true); - copy->set(_buffer, _count); - return copy; + ParsedRecord *copy = new ParsedRecord(true); + copy->set(_buffer, _count); + return copy; } - +/*-------------------------------------------------------------------------*/ const char * ParsedRecord::rawValue(size_t index) const { - if (index >= _count) - return NULL; + if (index >= _count) + return NULL; - const char *ptr = _buffer; - for (size_t n = 0; n < index; n++) { - ptr += strlen(ptr) + 1; - } + const char *ptr = _buffer; + for (size_t n = 0; n < index; n++) + { + ptr += strlen(ptr) + 1; + } - return ptr; + return ptr; } - +/*-------------------------------------------------------------------------*/ bool ParsedRecord::set(const char *buffer, size_t count) { - clear(); + clear(); #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC - const Value **values = (const Value**)realloc(_values, count*sizeof(Value*)); - if (values == NULL) - return false; - _values = values; + const Value **values = (const Value**)realloc(_values, count*sizeof(Value*)); + if (values == NULL) + return false; + _values = values; #else - if (count > SMARTREST_PARSED_FIXED_SIZE) - count = SMARTREST_PARSED_FIXED_SIZE; + if (count > SMARTREST_PARSED_FIXED_SIZE) + count = SMARTREST_PARSED_FIXED_SIZE; #endif - if (_copy) { - const char *ptr = buffer; - for (size_t n = 0; n < count; n++) - ptr += strlen(ptr) + 1; - _buffer = (const char*)malloc(ptr-buffer); - if (_buffer == NULL) - return false; - memcpy((char*)_buffer, buffer, ptr-buffer); - } else { - _buffer = buffer; - } - _count = count; + if (_copy) + { + const char *ptr = buffer; + for (size_t n = 0; n < count; n++) + ptr += strlen(ptr) + 1; + _buffer = (const char*)malloc(ptr-buffer); + if (_buffer == NULL) + return false; + memcpy((char*)_buffer, buffer, ptr-buffer); + } + else + { + _buffer = buffer; + } + _count = count; - const char *ptr = _buffer; - for (size_t n = 0; n < count; n++) { - _values[n] = new ParsedValue(ptr, false); - ptr += strlen(ptr) + 1; - } + const char *ptr = _buffer; + for (size_t n = 0; n < count; n++) + { + _values[n] = new ParsedValue(ptr, false); + ptr += strlen(ptr) + 1; + } - return true; + return true; } - +/*-------------------------------------------------------------------------*/ void ParsedRecord::clear() { - for (size_t n = 0; n < _count; n++) - delete _values[n]; - if ((_copy) && (_buffer != NULL)) - free((char*)_buffer); - _buffer = NULL; - _count = 0; + for (size_t n = 0; n < _count; n++) + delete _values[n]; + if ((_copy) && (_buffer != NULL)) + free((char*)_buffer); + _buffer = NULL; + _count = 0; } +/*-------------------------------------------------------------------------*/
--- a/ParsedRecord.h Thu Oct 23 14:41:58 2014 +0200 +++ b/ParsedRecord.h Sat Nov 15 11:21:01 2014 +0100 @@ -74,37 +74,37 @@ */ class ParsedRecord : public Record { -public: - ParsedRecord(bool = false); - ~ParsedRecord(); + public: + ParsedRecord(bool = false); + ~ParsedRecord(); - size_t values() const; - const Value& value(size_t) const; + size_t values() const; + const Value& value(size_t) const; - ParsedRecord* copy() const; + ParsedRecord* copy() const; - /** - * Returns the raw value string by index - * @param index the index of the value - * @return a pointer to the raw value - */ - const char * rawValue(size_t) const; + /** + * Returns the raw value string by index + * @param index the index of the value + * @return a pointer to the raw value + */ + const char * rawValue(size_t) const; -protected: - bool set(const char*, size_t); - void clear(); + protected: + bool set(const char*, size_t); + void clear(); -private: + private: #ifdef SMARTREST_PARSED_DYNAMIC_ALLOC - const Value **_values; + const Value **_values; #else - const Value *_values[SMARTREST_PARSED_FIXED_SIZE]; + const Value *_values[SMARTREST_PARSED_FIXED_SIZE]; #endif - const char *_buffer; - size_t _count; - bool _copy; + const char *_buffer; + size_t _count; + bool _copy; -friend class Parser; + friend class Parser; };
--- a/ParsedValue.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/ParsedValue.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -32,75 +32,81 @@ #include <ctype.h> #include <errno.h> + +/*-------------------------------------------------------------------------*/ ParsedValue::ParsedValue(const char *str, bool copy) : - _value(str, copy), - _float(0.0), - _integer(0l) + _value(str, copy), + _float(0.0), + _integer(0l) { - if (_value.valueType() != VALUE_NULL) { - _type = VALUE_CHARACTER; - extractValue(); - } else { - _type = VALUE_NULL; - } + if (_value.valueType() != VALUE_NULL) + { + _type = VALUE_CHARACTER; + extractValue(); + } + else + { + _type = VALUE_NULL; + } } - +/*-------------------------------------------------------------------------*/ uint8_t ParsedValue::valueType() const { - return _type; + return _type; } - +/*-------------------------------------------------------------------------*/ long ParsedValue::integerValue() const { - return _integer; + return _integer; } - +/*-------------------------------------------------------------------------*/ double ParsedValue::floatValue() const { - return _float; + return _float; } - +/*-------------------------------------------------------------------------*/ const char * ParsedValue::characterValue() const { - if (_type != VALUE_CHARACTER) - return NULL; - return _value.characterValue(); + if (_type != VALUE_CHARACTER) + return NULL; + return _value.characterValue(); } - +/*-------------------------------------------------------------------------*/ size_t ParsedValue::write(AbstractDataSink& sink) const { - return _value.write(sink); + return _value.write(sink); } - +/*-------------------------------------------------------------------------*/ size_t ParsedValue::length() const { - return _value.length(); + return _value.length(); } - +/*-------------------------------------------------------------------------*/ Value* ParsedValue::copy() const { - return new ParsedValue(_value.characterValue(), true); + return new ParsedValue(_value.characterValue(), true); } - +/*-------------------------------------------------------------------------*/ void ParsedValue::extractValue() { - const char *str; char *ptr; - - str = _value.characterValue(); - if ((*str == '\0') || (isspace(*str))) - return; + const char *str; char *ptr; + + str = _value.characterValue(); + if ((*str == '\0') || (isspace(*str))) + return; - errno = 0; - if ((((_integer = strtol(str, &ptr, 10)) != 0) || (errno == 0)) && - (*ptr == '\0')) { - _type = VALUE_INTEGER; - return; - } + errno = 0; + if ((((_integer = strtol(str, &ptr, 10)) != 0) || (errno == 0)) && + (*ptr == '\0')) { + _type = VALUE_INTEGER; + return; + } - errno = 0; - if ((((_float = strtod(str, &ptr)) != 0.0) || (errno == 0)) && - (*ptr == '\0')) { - _type = VALUE_FLOAT; - return; - } + errno = 0; + if ((((_float = strtod(str, &ptr)) != 0.0) || (errno == 0)) && + (*ptr == '\0')) { + _type = VALUE_FLOAT; + return; + } } +/*-------------------------------------------------------------------------*/
--- a/ParsedValue.h Thu Oct 23 14:41:58 2014 +0200 +++ b/ParsedValue.h Sat Nov 15 11:21:01 2014 +0100 @@ -65,26 +65,26 @@ */ class ParsedValue : public Value { -public: - ParsedValue(const char*, bool = false); + public: + ParsedValue(const char*, bool = false); - uint8_t valueType() const; - long integerValue() const; - double floatValue() const; - const char * characterValue() const; + uint8_t valueType() const; + long integerValue() const; + double floatValue() const; + const char * characterValue() const; - size_t write(AbstractDataSink&) const; - size_t length() const; - Value* copy() const; + size_t write(AbstractDataSink&) const; + size_t length() const; + Value* copy() const; -private: - void extractValue(); + private: + void extractValue(); -private: - CharValue _value; - uint8_t _type; - double _float; - long _integer; + private: + CharValue _value; + uint8_t _type; + double _float; + long _integer; }; #endif
--- a/Parser.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/Parser.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -27,6 +27,7 @@ */ #include "Parser.h" +#include <assert.h> #define STATE_BLANK 0 #define STATE_STRUCTURE 1 @@ -36,152 +37,172 @@ #define STATE_COMPLETE 5 #define STATE_ERROR 6 + +/*-------------------------------------------------------------------------*/ Parser::Parser() { - reset(); + reset(); } - +/*-------------------------------------------------------------------------*/ uint8_t Parser::readFrom(AbstractDataSource& source, ParsedRecord& record) { - uint8_t status; char read; + int status = -1; + char read; - reset(); - record.clear(); + reset(); + record.clear(); - while ((_state < STATE_COMPLETE) && - (((read = source.read()) > 0) || - ((status = source.status()) == DS_STATUS_OK))) - parse(read); + while ((_state < STATE_COMPLETE) && + (((read = source.read()) > 0) || + ((status = source.status()) == DS_STATUS_OK))) + parse(read); - // successfully read record - if (_state == STATE_COMPLETE) { - if (!record.set(_buffer, _count)) - return PARSER_INTERNAL_ERROR; - return PARSER_SUCCESS; - } + // successfully read record + if (_state == STATE_COMPLETE) + { + if (!record.set(_buffer, _count)) + return PARSER_INTERNAL_ERROR; + return PARSER_SUCCESS; + } - if (_state == STATE_BLANK) { - if (status == DS_STATUS_CLOSED) - return PARSER_END_OF_RESPONSE; - else if (status == DS_STATUS_TIMEOUT) - return PARSER_TIMEOUT_ERROR; - else - return PARSER_INTERNAL_ERROR; - } + if (_state == STATE_BLANK) + { + assert(status >= 0); + if (status == DS_STATUS_CLOSED) + return PARSER_END_OF_RESPONSE; + else if (status == DS_STATUS_TIMEOUT) + return PARSER_TIMEOUT_ERROR; + else + return PARSER_INTERNAL_ERROR; + } - if (status == DS_STATUS_TIMEOUT) - return PARSER_TIMEOUT_ERROR; - else if (status == DS_STATUS_ERROR) - return PARSER_INTERNAL_ERROR; - else - return PARSER_PARSE_ERROR; + assert(status >= 0); + if (status == DS_STATUS_TIMEOUT) + return PARSER_TIMEOUT_ERROR; + else if (status == DS_STATUS_ERROR) + return PARSER_INTERNAL_ERROR; + else + return PARSER_PARSE_ERROR; } - +/*-------------------------------------------------------------------------*/ void Parser::parse(char c) { - if (_ptr-_buffer >= SMARTREST_PARSER_BUFFER_SIZE) { - _state = STATE_ERROR; - return; - } + if (_ptr-_buffer >= SMARTREST_PARSER_BUFFER_SIZE) + { + _state = STATE_ERROR; + return; + } - switch (_state) { - case STATE_BLANK: - _state = STATE_STRUCTURE; - case STATE_STRUCTURE: - switch (c) { - case ' ': - case '\t': - case '\r': - break; - case '"': - _state = STATE_INQUOTES; - break; - case ',': - close(); - break; - case '\0': - case '\n': - if ((_count == 0) && (_length == 0)) { - _state = STATE_BLANK; - } else { - close(); - _state = STATE_COMPLETE; - } - break; - default: - if (_length > 0) { - _state = STATE_ERROR; - } else { - _state = STATE_VALUE; - parse(c); - } - break; - } - break; - case STATE_VALUE: - switch (c) { - case ',': - case '\n': - _state = STATE_STRUCTURE; - parse(c); - break; - case '"': - _state = STATE_ERROR; - break; - default: - if ((c == ' ') || (c == '\t') || (c == '\r')) - _trailing++; - else - _trailing = 0; - append(c); - break; - } - break; - case STATE_INQUOTES: - switch (c) { - case '"': - _state = STATE_AFTERQUOTE; - break; - default: - append(c); - break; - } - break; - case STATE_AFTERQUOTE: - switch (c) { - case '"': - append(c); - _state = STATE_INQUOTES; - break; - default: - _state = STATE_STRUCTURE; - parse(c); - break; - } - break; - } + switch (_state) + { + case STATE_BLANK: + _state = STATE_STRUCTURE; + case STATE_STRUCTURE: + switch (c) + { + case ' ': + case '\t': + case '\r': + break; + case '"': + _state = STATE_INQUOTES; + break; + case ',': + close(); + break; + case '\0': + case '\n': + if ((_count == 0) && (_length == 0)) + { + _state = STATE_BLANK; + } + else + { + close(); + _state = STATE_COMPLETE; + } + break; + default: + if (_length > 0) + { + _state = STATE_ERROR; + } + else + { + _state = STATE_VALUE; + parse(c); + } + break; + } + break; + case STATE_VALUE: + switch (c) + { + case ',': + case '\n': + _state = STATE_STRUCTURE; + parse(c); + break; + case '"': + _state = STATE_ERROR; + break; + default: + if ((c == ' ') || (c == '\t') || (c == '\r')) + _trailing++; + else + _trailing = 0; + append(c); + break; + } + break; + case STATE_INQUOTES: + switch (c) + { + case '"': + _state = STATE_AFTERQUOTE; + break; + default: + append(c); + break; + } + break; + case STATE_AFTERQUOTE: + switch (c) + { + case '"': + append(c); + _state = STATE_INQUOTES; + break; + default: + _state = STATE_STRUCTURE; + parse(c); + break; + } + break; + } } - +/*-------------------------------------------------------------------------*/ void Parser::append(char c) { - *_ptr++ = c; - _length++; + *_ptr++ = c; + _length++; } - +/*-------------------------------------------------------------------------*/ void Parser::close() { - _ptr -= _trailing; - *_ptr++ = 0; - _trailing = 0; - _length = 0; - _count++; + _ptr -= _trailing; + *_ptr++ = 0; + _trailing = 0; + _length = 0; + _count++; } - +/*-------------------------------------------------------------------------*/ void Parser::reset() { - _state = STATE_BLANK; - _ptr = _buffer; - _count = 0; - _trailing = 0; - _length = 0; + _state = STATE_BLANK; + _ptr = _buffer; + _count = 0; + _trailing = 0; + _length = 0; } +/*-------------------------------------------------------------------------*/
--- a/Parser.h Thu Oct 23 14:41:58 2014 +0200 +++ b/Parser.h Sat Nov 15 11:21:01 2014 +0100 @@ -89,31 +89,31 @@ */ class Parser { -public: - /* - * Creates a new Parser instance. - */ - Parser(); + public: + /* + * Creates a new Parser instance. + */ + Parser(); - /* - * Parses a row from a stream. - * @param source the source to read from - * @param row the object written to - * @return a non-zero value if and only if an error occured. - */ - uint8_t readFrom(AbstractDataSource&,ParsedRecord&); + /* + * Parses a row from a stream. + * @param source the source to read from + * @param row the object written to + * @return a non-zero value if and only if an error occured. + */ + uint8_t readFrom(AbstractDataSource&,ParsedRecord&); -private: - void parse(char); - void append(char); - void close(); - void reset(); + private: + void parse(char); + void append(char); + void close(); + void reset(); -private: - char _buffer[SMARTREST_PARSER_BUFFER_SIZE]; - uint8_t _state; - char *_ptr; - size_t _count, _length, _trailing; + private: + char _buffer[SMARTREST_PARSER_BUFFER_SIZE]; + uint8_t _state; + char *_ptr; + size_t _count, _length, _trailing; }; #endif
--- a/Record.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/Record.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -28,32 +28,36 @@ #include "Record.h" + +/*-------------------------------------------------------------------------*/ size_t Record::writeTo(AbstractDataSink& sink) const { - size_t written = 0; - for (size_t n = values(), i = 0; i < n; i++) { - written += value(i).write(sink); - if (i < n-1) - written += sink.write(','); - } - written += sink.write('\r'); - written += sink.write('\n'); - return written; + size_t written = 0; + for (size_t n = values(), i = 0; i < n; i++) + { + written += value(i).write(sink); + if (i < n-1) + written += sink.write(','); + } + written += sink.write('\r'); + written += sink.write('\n'); + return written; } - +/*-------------------------------------------------------------------------*/ size_t Record::writtenLength() const { - size_t written = 2; - for (size_t n = values(), i = 0; i < n; i++) { - written += value(i).length(); - if (i < n-1) - written++; - } - return written; + size_t written = 2; + for (size_t n = values(), i = 0; i < n; i++) + { + written += value(i).length(); + if (i < n-1) + written++; + } + return written; } - +/*-------------------------------------------------------------------------*/ Record::operator bool() const { - return values() > 0; + return values() > 0; } - +/*-------------------------------------------------------------------------*/
--- a/Record.h Thu Oct 23 14:41:58 2014 +0200 +++ b/Record.h Sat Nov 15 11:21:01 2014 +0100 @@ -38,31 +38,31 @@ */ class Record : public DataGenerator { -public: - virtual ~Record() { }; + public: + virtual ~Record() { }; - /** - * Returns the number of values in this record. - * @return number of values in this record - */ - virtual size_t values() const = 0; + /** + * Returns the number of values in this record. + * @return number of values in this record + */ + virtual size_t values() const = 0; - /** - * Returns the value object by index - * @param index the index of the value - * @return a copy of a value object - */ - virtual const Value& value(size_t) const = 0; + /** + * Returns the value object by index + * @param index the index of the value + * @return a copy of a value object + */ + virtual const Value& value(size_t) const = 0; - size_t writeTo(AbstractDataSink&) const; - size_t writtenLength() const; + size_t writeTo(AbstractDataSink&) const; + size_t writtenLength() const; - virtual Record* copy() const = 0; + virtual Record* copy() const = 0; - /** - * Test if record contains any values. - */ - operator bool() const; + /** + * Test if record contains any values. + */ + operator bool() const; }; #endif
--- a/SmartRest.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/SmartRest.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -30,190 +30,205 @@ #include <stdlib.h> #include <string.h> + +/*-------------------------------------------------------------------------*/ SmartRest::SmartRest(AbstractClient& client, const char *identifier) : - _client(client), - _identifier(identifier) + _client(client), + _identifier(identifier) { - _source = NULL; - _mogid[0] = 0; + _source = NULL; + _mogid[0] = 0; } - +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::setAuthorization(const char *username, const char *password) { - uint8_t res; + uint8_t res; - res = _client.setAuthorization(username, password); - if (res != CLIENT_OK) - return SMARTREST_INTERNAL_ERROR; - return SMARTREST_SUCCESS; + res = _client.setAuthorization(username, password); + if (res != CLIENT_OK) + return SMARTREST_INTERNAL_ERROR; + return SMARTREST_SUCCESS; } - +/*-------------------------------------------------------------------------*/ #ifdef SMARTREST_TRANSACTIONAL +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::request(const DataGenerator& generator, const char *overrideIdentifier) { - uint8_t res; + uint8_t res; - res = send(generator, overrideIdentifier); - stop(); - return res; + res = send(generator, overrideIdentifier); + stop(); + return res; } - +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::request(const DataGenerator& generator, Aggregator& aggregator, const char *overrideIdentifier) { - uint8_t res; + uint8_t res; - if (!aggregator.managed()) - return SMARTREST_INTERNAL_ERROR; + if (!aggregator.managed()) + return SMARTREST_INTERNAL_ERROR; - res = send(generator, overrideIdentifier); - if (res != SMARTREST_SUCCESS) { - stop(); - return res; - } + res = send(generator, overrideIdentifier); + if (res != SMARTREST_SUCCESS) + { + stop(); + return res; + } - ParsedRecord recvd; - while ((res = receive(recvd)) == SMARTREST_SUCCESS) { - if (!aggregator.add(recvd)) - return SMARTREST_INTERNAL_ERROR; - } + ParsedRecord recvd; + while ((res = receive(recvd)) == SMARTREST_SUCCESS) + { + if (!aggregator.add(recvd)) + return SMARTREST_INTERNAL_ERROR; + } - stop(); - if (res == SMARTREST_END_OF_RESPONSE) - return SMARTREST_SUCCESS; - return res; + stop(); + if (res == SMARTREST_END_OF_RESPONSE) + return SMARTREST_SUCCESS; + return res; } -#endif - +/*-------------------------------------------------------------------------*/ +#endif // SMARTREST_TRANSACTIONAL +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::bootstrap(const DataGenerator& generator) { - ParsedRecord record; - int8_t ret; + ParsedRecord record; + int8_t ret; - ret = beginRequest(NULL); - if (ret != SMARTREST_SUCCESS) - return ret; - ret = awaitResponse(); - if (ret != SMARTREST_SUCCESS) - return ret; - ret = receive(record); - if (ret != SMARTREST_SUCCESS) - return ret; - if (!record) { - return SMARTREST_INTERNAL_ERROR; - } - stop(); + ret = beginRequest(NULL); + if (ret != SMARTREST_SUCCESS) + return ret; + ret = awaitResponse(); + if (ret != SMARTREST_SUCCESS) + return ret; + ret = receive(record); + if (ret != SMARTREST_SUCCESS) + return ret; + if (!record) + { + return SMARTREST_INTERNAL_ERROR; + } + stop(); - if (setMoGid(record)) - return SMARTREST_SUCCESS; + if (setMoGid(record)) + return SMARTREST_SUCCESS; - if (record.value(0).integerValue() != 40) - return SMARTREST_INTERNAL_ERROR; + if (record.value(0).integerValue() != 40) + return SMARTREST_INTERNAL_ERROR; - ret = send(generator, NULL); - if (ret != SMARTREST_SUCCESS) - return ret; - ret = receive(record); - if (ret != SMARTREST_SUCCESS) - return ret; - stop(); + ret = send(generator, NULL); + if (ret != SMARTREST_SUCCESS) + return ret; + ret = receive(record); + if (ret != SMARTREST_SUCCESS) + return ret; + stop(); - if (!setMoGid(record)) - return SMARTREST_INTERNAL_ERROR; + if (!setMoGid(record)) + return SMARTREST_INTERNAL_ERROR; - return SMARTREST_SUCCESS; + return SMARTREST_SUCCESS; } - +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::send(const DataGenerator& generator, const char *overrideIdentifier) { - uint8_t res; + uint8_t res; - res = beginRequest(overrideIdentifier); - if (res != SMARTREST_SUCCESS) - return res; + res = beginRequest(overrideIdentifier); + if (res != SMARTREST_SUCCESS) + return res; - _client.sendData(generator); - return awaitResponse(); + _client.sendData(generator); + return awaitResponse(); } - +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::receive(ParsedRecord& record) { - uint8_t res; - - if (_source == NULL) - return SMARTREST_INTERNAL_ERROR; + uint8_t res; - res = _parser.readFrom(*_source, record); + if (_source == NULL) + return SMARTREST_INTERNAL_ERROR; + + res = _parser.readFrom(*_source, record); - switch (res) { - case PARSER_SUCCESS: - return SMARTREST_SUCCESS; - case PARSER_END_OF_RESPONSE: - return SMARTREST_END_OF_RESPONSE; - case PARSER_TIMEOUT_ERROR: - return SMARTREST_TIMEOUT_ERROR; - } - return SMARTREST_INTERNAL_ERROR; + switch (res) + { + case PARSER_SUCCESS: + return SMARTREST_SUCCESS; + case PARSER_END_OF_RESPONSE: + return SMARTREST_END_OF_RESPONSE; + case PARSER_TIMEOUT_ERROR: + return SMARTREST_TIMEOUT_ERROR; + } + return SMARTREST_INTERNAL_ERROR; } - +/*-------------------------------------------------------------------------*/ void SmartRest::stop() { - _source = NULL; - _client.stop(); + _source = NULL; + _client.stop(); } - +/*-------------------------------------------------------------------------*/ const char * SmartRest::getIdentifier() { - if (*_mogid) - return _mogid; - return _identifier; + if (*_mogid) + return _mogid; + return _identifier; } - +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::beginRequest(const char *overrideIdentifier) { - int res; + int res; - res = _client.beginRequest(); - if (res == CLIENT_CONNECTION_ERROR) { - return SMARTREST_CONNECTION_FAILED; - } else if (res != CLIENT_OK) { - return SMARTREST_INTERNAL_ERROR; - } - if (overrideIdentifier != NULL) { - if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK) - return SMARTREST_INTERNAL_ERROR; - } else { - if (_client.sendIdentifier(getIdentifier()) != CLIENT_OK) - return SMARTREST_INTERNAL_ERROR; - } - return SMARTREST_SUCCESS; + res = _client.beginRequest(); + if (res == CLIENT_CONNECTION_ERROR) + { + return SMARTREST_CONNECTION_FAILED; + } + else if (res != CLIENT_OK) + { + return SMARTREST_INTERNAL_ERROR; + } + if (overrideIdentifier != NULL) + { + if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK) + return SMARTREST_INTERNAL_ERROR; + } + else + { + if (_client.sendIdentifier(getIdentifier()) != CLIENT_OK) + return SMARTREST_INTERNAL_ERROR; + } + return SMARTREST_SUCCESS; } - +/*-------------------------------------------------------------------------*/ uint8_t SmartRest::awaitResponse() { - if ((_client.endRequest() != CLIENT_OK) || - (_client.awaitResponse() != CLIENT_OK)) - return SMARTREST_INTERNAL_ERROR; - _source = &_client.receiveData(); - return SMARTREST_SUCCESS; + if ((_client.endRequest() != CLIENT_OK) || + (_client.awaitResponse() != CLIENT_OK)) + return SMARTREST_INTERNAL_ERROR; + _source = &_client.receiveData(); + return SMARTREST_SUCCESS; } - +/*-------------------------------------------------------------------------*/ bool SmartRest::setMoGid(ParsedRecord& record) { - const char *mogid; + const char *mogid; - *_mogid = 0; - if ((record.values() < 2) || (record.value(0).integerValue() != 20)) - return false; + *_mogid = 0; + if ((record.values() < 2) || (record.value(0).integerValue() != 20)) + return false; + + if ((record.value(1).valueType() != VALUE_INTEGER) || + (record.value(1).integerValue() <= 0)) + return false; - if ((record.value(1).valueType() != VALUE_INTEGER) || - (record.value(1).integerValue() <= 0)) - return false; + mogid = record.rawValue(1); + if (strlen(mogid)+1 > sizeof(_mogid)) + return false; - mogid = record.rawValue(1); - if (strlen(mogid)+1 > sizeof(_mogid)) - return false; + strcpy(_mogid, mogid); - strcpy(_mogid, mogid); - - return true; + return true; } +/*-------------------------------------------------------------------------*/
--- a/SmartRest.h Thu Oct 23 14:41:58 2014 +0200 +++ b/SmartRest.h Sat Nov 15 11:21:01 2014 +0100 @@ -47,43 +47,43 @@ */ class SmartRest : public AbstractSmartRest { -public: - /** - * Creates a new generic SmartRest object. - * @param client the abstract client to use - * @param identifier the device identifier - */ - SmartRest(AbstractClient&, const char*); + public: + /** + * Creates a new generic SmartRest object. + * @param client the abstract client to use + * @param identifier the device identifier + */ + SmartRest(AbstractClient&, const char*); - uint8_t setAuthorization(const char*, const char*); + 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); + uint8_t request(const DataGenerator&, const char* = NULL); + uint8_t request(const DataGenerator&, Aggregator&, const char* = NULL); #endif - uint8_t bootstrap(const DataGenerator&); + uint8_t bootstrap(const DataGenerator&); #ifndef SMARTREST_TRANSACTIONAL - uint8_t send(const DataGenerator&, const char* = NULL); - uint8_t receive(ParsedRecord&); - void stop(); + uint8_t send(const DataGenerator&, const char* = NULL); + uint8_t receive(ParsedRecord&); + void stop(); #endif - const char * getIdentifier(); + const char * getIdentifier(); -protected: + protected: #ifdef SMARTREST_TRANSACTIONAL - uint8_t send(const DataGenerator&, const char*); - uint8_t receive(ParsedRecord&); - void stop(); + 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&); + 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]; + private: + AbstractClient& _client; + AbstractDataSource *_source; + Parser _parser; + const char *_identifier; + char _mogid[8 * sizeof(long) + 1]; }; #endif
--- a/StaticData.cpp Thu Oct 23 14:41:58 2014 +0200 +++ b/StaticData.cpp Sat Nov 15 11:21:01 2014 +0100 @@ -30,49 +30,57 @@ #include <stdlib.h> #include <string.h> + +/*-------------------------------------------------------------------------*/ StaticData::StaticData(void* buffer, size_t length, bool copy) { - _alloc = copy; - _len = length; - if (copy) { - _buf = malloc(length); - memcpy(_buf, buffer, length); - } else { - _buf = buffer; - } + _alloc = copy; + _len = length; + if (copy) + { + _buf = malloc(length); + memcpy(_buf, buffer, length); + } + else + { + _buf = buffer; + } } - +/*-------------------------------------------------------------------------*/ StaticData::StaticData(const char* string, bool copy) { - _alloc = copy; - if (copy) { - _len = strlen(string)*sizeof(char); - _buf = malloc(_len); - memcpy(_buf, string, _len); - } else { - _buf = (void*)string; - _len = strlen(string)*sizeof(char); - } + _alloc = copy; + if (copy) + { + _len = strlen(string)*sizeof(char); + _buf = malloc(_len); + memcpy(_buf, string, _len); + } + else + { + _buf = (void*)string; + _len = strlen(string)*sizeof(char); + } } - +/*-------------------------------------------------------------------------*/ StaticData::~StaticData() { - if (_alloc) - free(_buf); + if (_alloc) + free(_buf); } - +/*-------------------------------------------------------------------------*/ size_t StaticData::writeTo(AbstractDataSink& sink) const { - return sink.write((void*)_buf, _len*sizeof(char)); + return sink.write((void*)_buf, _len*sizeof(char)); } - +/*-------------------------------------------------------------------------*/ size_t StaticData::writtenLength() const { - return _len; + return _len; } - +/*-------------------------------------------------------------------------*/ DataGenerator* StaticData::copy() const { - return new StaticData(_buf, _len, true); + return new StaticData(_buf, _len, true); } - +/*-------------------------------------------------------------------------*/
--- a/StaticData.h Thu Oct 23 14:41:58 2014 +0200 +++ b/StaticData.h Sat Nov 15 11:21:01 2014 +0100 @@ -47,18 +47,18 @@ */ class StaticData : public DataGenerator { -public: - StaticData(void*, size_t, bool = false); - StaticData(const char*, bool = false); - ~StaticData(); - size_t writeTo(AbstractDataSink&) const; - size_t writtenLength() const; - DataGenerator* copy() const; + public: + StaticData(void*, size_t, bool = false); + StaticData(const char*, bool = false); + ~StaticData(); + size_t writeTo(AbstractDataSink&) const; + size_t writtenLength() const; + DataGenerator* copy() const; -private: - void *_buf; - size_t _len; - bool _alloc; + private: + void *_buf; + size_t _len; + bool _alloc; }; #endif
--- a/Value.h Thu Oct 23 14:41:58 2014 +0200 +++ b/Value.h Sat Nov 15 11:21:01 2014 +0100 @@ -54,54 +54,54 @@ */ class Value { -public: - virtual ~Value() { }; + public: + virtual ~Value() { }; - /** - * Returns the value type which can be VALUE_NULL, VALUE_INTEGER, - * VALUE_FLOAT or VALUE_CHARACTER. - * @return the value type - */ - virtual uint8_t valueType() const = 0; + /** + * Returns the value type which can be VALUE_NULL, VALUE_INTEGER, + * VALUE_FLOAT or VALUE_CHARACTER. + * @return the value type + */ + virtual uint8_t valueType() const = 0; - /** - * Returns the integer value if an integer type, otherwise zero. - * @return integer value - */ - virtual long integerValue() const = 0; + /** + * Returns the integer value if an integer type, otherwise zero. + * @return integer value + */ + virtual long integerValue() const = 0; - /** - * Returns the float value if a float type, otherwise zero. - * @return float value - */ - virtual double floatValue() const = 0; + /** + * Returns the float value if a float type, otherwise zero. + * @return float value + */ + virtual double floatValue() const = 0; - /** - * Returns the character value if a character type, otherwise zero. - * @return character value - */ - virtual const char * characterValue() const = 0; + /** + * Returns the character value if a character type, otherwise zero. + * @return character value + */ + virtual const char * characterValue() const = 0; - /** - * Writes the value to a sink. - * If necessary, quotes and escape characters may be added. - * @param sink the sink to write the value to - * @return the number of bytes written - */ - virtual size_t write(AbstractDataSink&) const = 0; + /** + * Writes the value to a sink. + * If necessary, quotes and escape characters may be added. + * @param sink the sink to write the value to + * @return the number of bytes written + */ + virtual size_t write(AbstractDataSink&) const = 0; - /** - * Returns the number of bytes required to write this value to a sink. - * @return number of bytes required to write this value - */ - virtual size_t length() const = 0; + /** + * Returns the number of bytes required to write this value to a sink. + * @return number of bytes required to write this value + */ + virtual size_t length() const = 0; - /** - * Copies this value to the heap. Referenced contents must also be - * copied. - * @return a new value instance allocated on the heap - */ - virtual Value* copy() const = 0; + /** + * Copies this value to the heap. Referenced contents must also be + * copied. + * @return a new value instance allocated on the heap + */ + virtual Value* copy() const = 0; }; #endif