mbed.org implementation of the abstract SmartREST library for the Cumulocity Platform SmartREST protocol.
Dependents: MbedSmartRestMain MbedSmartRestMain
Revision 7:8159a2d12e4e, committed 2014-10-22
- Comitter:
- Cumulocity
- Date:
- Wed Oct 22 16:17:22 2014 +0200
- Parent:
- 6:cd7ba1ddb664
- Child:
- 8:3a4dba260b71
- Commit message:
- Updated from revision aaae61ab8a05
Changed in this revision
--- a/AbstractSmartRest.h Mon Jul 28 12:11:02 2014 +0200 +++ b/AbstractSmartRest.h Wed Oct 22 16:17:22 2014 +0200 @@ -136,6 +136,12 @@ */ virtual void stop() = 0; #endif + + /* + * Retrieves the template identifier. + * @return template identifier + */ + virtual const char * getIdentifier() = 0; }; #endif
--- a/CharValue.cpp Mon Jul 28 12:11:02 2014 +0200 +++ b/CharValue.cpp Wed Oct 22 16:17:22 2014 +0200 @@ -30,8 +30,7 @@ #include "NullValue.h" #include <stdlib.h> #include <string.h> - -inline int8_t __charvalue_need_escape(const char *str); +#include <ctype.h> CharValue::CharValue(const char *str, bool copy) { @@ -42,9 +41,13 @@ _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(); } } } @@ -79,55 +82,48 @@ size_t CharValue::write(AbstractDataSink& sink) const { + size_t n; + const char *ptr; char c; + if (_str == NULL) return 0; - size_t n = 0; - int8_t esc = __charvalue_need_escape(_str) ? 1 : 0; - if (esc) n += sink.write('"'); - for (char *q = (char*)_str, c; (c = *q) != 0; q++) { - if ((esc) && (c == '"')) + + n = 0; + if (_escape) + n += sink.write('"'); + + for (ptr = _str; (c = *ptr) != '\0'; ptr++) { + if ((_escape) && (c == '"')) n += sink.write('"'); n += sink.write(c); } - if (esc) n += sink.write('"'); + + if (_escape) + n += sink.write('"'); return n; } size_t CharValue::length() const { + size_t n; + const char *ptr; char c; + if (_str == NULL) return 0; - size_t n = 0; - int8_t esc = __charvalue_need_escape(_str) ? 1 : 0; - if (esc) + + n = 0; + if (_escape) n += 2; - for (char *q = (char*)_str, c; (c = *q) != 0; q++) { - if ((esc) && (c == '"')) + + for (ptr = _str; (c = *ptr) != '\0'; ptr++) { + if ((_escape) && (c == '"')) n++; n++; } + return n; } -int8_t __charvalue_need_escape(const char *str) -{ - int8_t w = 0; - for (char c; (c = *str) != 0; str++) { - if ((c == ' ') || (c == '\t')) { - if (w == 0) - return 1; - w = 2; - } else { - w = 1; - if ((c == '"') || (c == ',') || (c == '\r') || (c == '\n')) - return 1; - } - } - if (w == 2) - return 1; - return 0; -} - Value* CharValue::copy() const { if (_str == NULL) @@ -135,3 +131,23 @@ return new CharValue(_str, true); } +bool CharValue::escapeCheck() +{ + 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++; + } + + if (isspace(c)) + return true; + + return false; +} +
--- a/CharValue.h Mon Jul 28 12:11:02 2014 +0200 +++ b/CharValue.h Wed Oct 22 16:17:22 2014 +0200 @@ -63,9 +63,12 @@ size_t length() const; Value* copy() const; +protected: + bool escapeCheck(); + private: const char *_str; - bool _alloc; + bool _escape, _alloc; }; #endif
--- a/MbedClient.cpp Mon Jul 28 12:11:02 2014 +0200 +++ b/MbedClient.cpp Wed Oct 22 16:17:22 2014 +0200 @@ -52,7 +52,6 @@ _sink(_sock), _sock() { - _state = STATE_INIT; } MbedClient::~MbedClient() @@ -120,16 +119,15 @@ uint8_t MbedClient::sendData(const DataGenerator& generator) { - size_t len; char lenstr[8]; + size_t len; if ((_state != STATE_IN_REQUEST) && (_state != STATE_SENT_ID)) return internalError(); len = generator.writtenLength(); - snprintf(lenstr, 8, "%ld", len); - + if ((!send("Content-Length: ")) || - (!send(lenstr)) || + (_sink.write((unsigned long)len) == 0) || (!send("\r\n\r\n"))) return connectionError(); @@ -188,9 +186,7 @@ bool MbedClient::send(const char *str) { - if (_sink.write(str) != strlen(str)) - return false; - return true; + return (_sink.write(str) == strlen(str)); } bool MbedClient::sendBasicAuth()
--- a/ParsedValue.cpp Mon Jul 28 12:11:02 2014 +0200 +++ b/ParsedValue.cpp Wed Oct 22 16:17:22 2014 +0200 @@ -31,13 +31,11 @@ #include <string.h> #include <ctype.h> #include <errno.h> -#include <math.h> -#include "NullValue.h" -#include "IntegerValue.h" -#include "FloatValue.h" -#include "CharValue.h" -ParsedValue::ParsedValue(const char *str, bool copy) : _value(str, copy), _float(0.0), _digits(0), _zflag(false), _integer(0l) +ParsedValue::ParsedValue(const char *str, bool copy) : + _value(str, copy), + _float(0.0), + _integer(0l) { if (_value.valueType() != VALUE_NULL) { _type = VALUE_CHARACTER; @@ -81,80 +79,28 @@ Value* ParsedValue::copy() const { - if (_type == VALUE_NULL) - return new NullValue(); - if (_type == VALUE_INTEGER) - return new IntegerValue(_integer); - if (_type == VALUE_FLOAT) - return new FloatValue(_float, _digits, _zflag); - return new CharValue(_value.characterValue(), true); + return new ParsedValue(_value.characterValue(), true); } void ParsedValue::extractValue() { - const char *str; char *ptr, c; - bool floating = false, zflag = false, negative = false; - uint8_t digits = 0; + const char *str; char *ptr; str = _value.characterValue(); - - if (isspace(*str)) + if ((*str == '\0') || (isspace(*str))) return; - // easy check for nan - if (strcmp(str, "nan") == 0) { - _float = NAN; - _type = VALUE_FLOAT; - return; - } - - if (*str == '-') { - negative = true; - str++; - } - - if (strcmp(str, "inf") == 0) { - _float = (negative) ? -INFINITY : INFINITY; - _type = VALUE_FLOAT; + errno = 0; + if ((((_integer = strtol(str, &ptr, 10)) != 0) || (errno == 0)) && + (*ptr == '\0')) { + _type = VALUE_INTEGER; return; } - for (ptr = (char*)str; (c = *ptr) != 0; ptr++) { - if (floating) { - if (!isdigit(c)) - return; - digits++; - } else { - if (c == '.') { - if (*(ptr+1) == '\0') - return; - floating = true; - } else if (isdigit(c)) { - _zflag = true; - } else { - return; - } - } - } - - if (floating) { - errno = 0; - if ((((_float = strtod(str, &ptr)) != 0.0) || (errno == 0)) && - (*ptr == '\0')) { - if (negative) - _float = -_float; - _zflag = zflag; - _digits = digits; - _type = VALUE_FLOAT; - } - } else { - errno = 0; - if ((((_integer = strtol(str, &ptr, 10)) != 0) || (errno == 0)) && - (*ptr == '\0')) { - if (negative) - _integer = -_integer; - _type = VALUE_INTEGER; - return; - } + errno = 0; + if ((((_float = strtod(str, &ptr)) != 0.0) || (errno == 0)) && + (*ptr == '\0')) { + _type = VALUE_FLOAT; + return; } }
--- a/ParsedValue.h Mon Jul 28 12:11:02 2014 +0200 +++ b/ParsedValue.h Wed Oct 22 16:17:22 2014 +0200 @@ -84,8 +84,6 @@ CharValue _value; uint8_t _type; double _float; - uint8_t _digits; - bool _zflag; long _integer; };
--- a/SmartRest.cpp Mon Jul 28 12:11:02 2014 +0200 +++ b/SmartRest.cpp Wed Oct 22 16:17:22 2014 +0200 @@ -161,6 +161,13 @@ _client.stop(); } +const char * SmartRest::getIdentifier() +{ + if (*_mogid) + return _mogid; + return _identifier; +} + uint8_t SmartRest::beginRequest(const char *overrideIdentifier) { int res; @@ -174,11 +181,8 @@ if (overrideIdentifier != NULL) { if (_client.sendIdentifier(overrideIdentifier) != CLIENT_OK) return SMARTREST_INTERNAL_ERROR; - } else if (strlen(_mogid)) { - if (_client.sendIdentifier(_mogid) != CLIENT_OK) - return SMARTREST_INTERNAL_ERROR; } else { - if (_client.sendIdentifier(_identifier) != CLIENT_OK) + if (_client.sendIdentifier(getIdentifier()) != CLIENT_OK) return SMARTREST_INTERNAL_ERROR; } return SMARTREST_SUCCESS;
--- a/SmartRest.h Mon Jul 28 12:11:02 2014 +0200 +++ b/SmartRest.h Wed Oct 22 16:17:22 2014 +0200 @@ -73,6 +73,7 @@ uint8_t receive(ParsedRecord&); void stop(); #endif + const char * getIdentifier(); uint8_t beginRequest(const char*); uint8_t awaitResponse(); bool setMoGid(ParsedRecord&);