The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.
Fork of Cayenne-MQTT-mbed by
src/CayenneUtils/CayenneDataArray.h@23:1a9aed5e77c9, 2017-01-25 (annotated)
- Committer:
- jburhenn
- Date:
- Wed Jan 25 11:10:39 2017 -0700
- Branch:
- feature/multivalue
- Revision:
- 23:1a9aed5e77c9
- Parent:
- 15:1f4d5dbf1928
CayenneDataArray updates for multivalue support.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:09ef59d2d0f7 | 1 | /* |
jburhenn | 0:09ef59d2d0f7 | 2 | The MIT License(MIT) |
jburhenn | 0:09ef59d2d0f7 | 3 | |
jburhenn | 0:09ef59d2d0f7 | 4 | Cayenne MQTT Client Library |
jburhenn | 0:09ef59d2d0f7 | 5 | Copyright (c) 2016 myDevices |
jburhenn | 0:09ef59d2d0f7 | 6 | |
jburhenn | 0:09ef59d2d0f7 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
jburhenn | 0:09ef59d2d0f7 | 8 | documentation files(the "Software"), to deal in the Software without restriction, including without limitation |
jburhenn | 0:09ef59d2d0f7 | 9 | the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, |
jburhenn | 0:09ef59d2d0f7 | 10 | and to permit persons to whom the Software is furnished to do so, subject to the following conditions : |
jburhenn | 0:09ef59d2d0f7 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
jburhenn | 0:09ef59d2d0f7 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
jburhenn | 0:09ef59d2d0f7 | 13 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR |
jburhenn | 0:09ef59d2d0f7 | 14 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
jburhenn | 0:09ef59d2d0f7 | 15 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jburhenn | 0:09ef59d2d0f7 | 16 | */ |
jburhenn | 0:09ef59d2d0f7 | 17 | |
jburhenn | 0:09ef59d2d0f7 | 18 | #ifndef _CAYENNEDATAARRAY_h |
jburhenn | 0:09ef59d2d0f7 | 19 | #define _CAYENNEDATAARRAY_h |
jburhenn | 0:09ef59d2d0f7 | 20 | |
jburhenn | 0:09ef59d2d0f7 | 21 | #include "CayenneUtils.h" |
jburhenn | 0:09ef59d2d0f7 | 22 | |
jburhenn | 0:09ef59d2d0f7 | 23 | #if defined(__cplusplus) |
jburhenn | 0:09ef59d2d0f7 | 24 | |
jburhenn | 0:09ef59d2d0f7 | 25 | // C++ version of the data array. This is defined here so it requires no source file. |
jburhenn | 0:09ef59d2d0f7 | 26 | |
jburhenn | 8:aec9cfdd4c8e | 27 | namespace CayenneMQTT |
jburhenn | 0:09ef59d2d0f7 | 28 | { |
jburhenn | 1:90dccf306268 | 29 | /** |
jburhenn | 1:90dccf306268 | 30 | * @class DataArray |
jburhenn | 23:1a9aed5e77c9 | 31 | * Class for manipulating a data array. |
jburhenn | 1:90dccf306268 | 32 | * @param BUFFER_SIZE Maximum buffer size to use for data array, in bytes. |
jburhenn | 1:90dccf306268 | 33 | */ |
jburhenn | 23:1a9aed5e77c9 | 34 | template<int BUFFER_SIZE = CAYENNE_MAX_PAYLOAD_VALUE_SIZE> |
jburhenn | 0:09ef59d2d0f7 | 35 | class DataArray |
jburhenn | 0:09ef59d2d0f7 | 36 | { |
jburhenn | 0:09ef59d2d0f7 | 37 | public: |
jburhenn | 1:90dccf306268 | 38 | /** |
jburhenn | 1:90dccf306268 | 39 | * Construct an empty array. |
jburhenn | 1:90dccf306268 | 40 | */ |
jburhenn | 0:09ef59d2d0f7 | 41 | DataArray() { |
jburhenn | 0:09ef59d2d0f7 | 42 | clear(); |
jburhenn | 0:09ef59d2d0f7 | 43 | } |
jburhenn | 0:09ef59d2d0f7 | 44 | |
jburhenn | 1:90dccf306268 | 45 | /** |
jburhenn | 1:90dccf306268 | 46 | * Clear the array. |
jburhenn | 1:90dccf306268 | 47 | */ |
jburhenn | 0:09ef59d2d0f7 | 48 | void clear() { |
jburhenn | 23:1a9aed5e77c9 | 49 | _buffer[0] = '['; // Opening bracket for array |
jburhenn | 23:1a9aed5e77c9 | 50 | _buffer[1] = '\0'; |
jburhenn | 0:09ef59d2d0f7 | 51 | _valueCount = 0; |
jburhenn | 23:1a9aed5e77c9 | 52 | _index = 1; |
jburhenn | 0:09ef59d2d0f7 | 53 | } |
jburhenn | 0:09ef59d2d0f7 | 54 | |
jburhenn | 1:90dccf306268 | 55 | /** |
jburhenn | 23:1a9aed5e77c9 | 56 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 57 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 58 | * @param[in] valueInFlash If true the value string is in flash memory, otherwise false. |
jburhenn | 1:90dccf306268 | 59 | */ |
jburhenn | 23:1a9aed5e77c9 | 60 | void add(const char* value, bool valueInFlash = false) { |
jburhenn | 0:09ef59d2d0f7 | 61 | size_t valueLength = 0; |
jburhenn | 0:09ef59d2d0f7 | 62 | if (value) { |
jburhenn | 0:09ef59d2d0f7 | 63 | valueLength = (valueInFlash ? CAYENNE_STRLEN(value) : strlen(value)) + 1; |
jburhenn | 23:1a9aed5e77c9 | 64 | // Make sure the value string along with comma & array bracket will fit in buffer. |
jburhenn | 23:1a9aed5e77c9 | 65 | if (_index + valueLength + 2 > BUFFER_SIZE) |
jburhenn | 23:1a9aed5e77c9 | 66 | return; |
jburhenn | 23:1a9aed5e77c9 | 67 | if (_valueCount > 0 && _index > 1) { |
jburhenn | 23:1a9aed5e77c9 | 68 | if (_buffer[--_index - 1] == ']') |
jburhenn | 23:1a9aed5e77c9 | 69 | --_index; |
jburhenn | 23:1a9aed5e77c9 | 70 | _buffer[_index++] = ','; |
jburhenn | 23:1a9aed5e77c9 | 71 | } |
jburhenn | 23:1a9aed5e77c9 | 72 | valueInFlash ? CAYENNE_MEMCPY(_buffer + _index, value, valueLength) : memcpy(_buffer + _index, value, valueLength); |
jburhenn | 23:1a9aed5e77c9 | 73 | _index += valueLength; |
jburhenn | 23:1a9aed5e77c9 | 74 | if (_valueCount > 0 && _index > 1) { |
jburhenn | 23:1a9aed5e77c9 | 75 | // Only append the closing bracket if multiple items are added. That way the getString() function can be used get |
jburhenn | 23:1a9aed5e77c9 | 76 | // the string value without a closing bracket if this class is being used to convert single values to strings. |
jburhenn | 23:1a9aed5e77c9 | 77 | _buffer[_index - 1] = ']'; |
jburhenn | 23:1a9aed5e77c9 | 78 | _buffer[_index] = '\0'; |
jburhenn | 23:1a9aed5e77c9 | 79 | } |
jburhenn | 23:1a9aed5e77c9 | 80 | ++_valueCount; |
jburhenn | 0:09ef59d2d0f7 | 81 | } |
jburhenn | 0:09ef59d2d0f7 | 82 | } |
jburhenn | 0:09ef59d2d0f7 | 83 | |
jburhenn | 1:90dccf306268 | 84 | /** |
jburhenn | 23:1a9aed5e77c9 | 85 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 86 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 87 | */ |
jburhenn | 23:1a9aed5e77c9 | 88 | inline void add(const int value) { |
jburhenn | 0:09ef59d2d0f7 | 89 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 90 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 91 | itoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 92 | #else |
jburhenn | 0:09ef59d2d0f7 | 93 | snprintf(str, sizeof(str), "%d", value); |
jburhenn | 0:09ef59d2d0f7 | 94 | #endif |
jburhenn | 23:1a9aed5e77c9 | 95 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 96 | } |
jburhenn | 0:09ef59d2d0f7 | 97 | |
jburhenn | 1:90dccf306268 | 98 | /** |
jburhenn | 23:1a9aed5e77c9 | 99 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 100 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 101 | */ |
jburhenn | 23:1a9aed5e77c9 | 102 | inline void add(const unsigned int value) { |
jburhenn | 0:09ef59d2d0f7 | 103 | char str[1 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 104 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 105 | utoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 106 | #else |
jburhenn | 0:09ef59d2d0f7 | 107 | snprintf(str, sizeof(str), "%u", value); |
jburhenn | 0:09ef59d2d0f7 | 108 | #endif |
jburhenn | 23:1a9aed5e77c9 | 109 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 110 | } |
jburhenn | 0:09ef59d2d0f7 | 111 | |
jburhenn | 1:90dccf306268 | 112 | /** |
jburhenn | 23:1a9aed5e77c9 | 113 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 114 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 115 | */ |
jburhenn | 23:1a9aed5e77c9 | 116 | inline void add(const long value) { |
jburhenn | 0:09ef59d2d0f7 | 117 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 118 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 119 | ltoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 120 | #else |
jburhenn | 0:09ef59d2d0f7 | 121 | snprintf(str, sizeof(str), "%ld", value); |
jburhenn | 0:09ef59d2d0f7 | 122 | #endif |
jburhenn | 23:1a9aed5e77c9 | 123 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 124 | } |
jburhenn | 0:09ef59d2d0f7 | 125 | |
jburhenn | 1:90dccf306268 | 126 | /** |
jburhenn | 23:1a9aed5e77c9 | 127 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 128 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 129 | */ |
jburhenn | 23:1a9aed5e77c9 | 130 | inline void add(const unsigned long value) { |
jburhenn | 0:09ef59d2d0f7 | 131 | char str[1 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 132 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 133 | ultoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 134 | #else |
jburhenn | 0:09ef59d2d0f7 | 135 | snprintf(str, sizeof(str), "%lu", value); |
jburhenn | 0:09ef59d2d0f7 | 136 | #endif |
jburhenn | 23:1a9aed5e77c9 | 137 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 138 | } |
jburhenn | 0:09ef59d2d0f7 | 139 | |
jburhenn | 23:1a9aed5e77c9 | 140 | #if defined (__AVR__) || defined (ARDUINO_ARCH_ARC32) || defined (ESP8266) |
jburhenn | 1:90dccf306268 | 141 | /** |
jburhenn | 23:1a9aed5e77c9 | 142 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 143 | * @param[in] value The value to add. |
jburhenn | 23:1a9aed5e77c9 | 144 | * @param[in] precision Number of digits after the decimal. |
jburhenn | 1:90dccf306268 | 145 | */ |
jburhenn | 23:1a9aed5e77c9 | 146 | inline void add(const float value, unsigned char precision = 3) { |
jburhenn | 0:09ef59d2d0f7 | 147 | char str[33]; |
jburhenn | 23:1a9aed5e77c9 | 148 | dtostrf(value, 1, precision, str); |
jburhenn | 23:1a9aed5e77c9 | 149 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 150 | } |
jburhenn | 0:09ef59d2d0f7 | 151 | |
jburhenn | 1:90dccf306268 | 152 | /** |
jburhenn | 23:1a9aed5e77c9 | 153 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 154 | * @param[in] value The value to add. |
jburhenn | 23:1a9aed5e77c9 | 155 | * @param[in] precision Number of digits after the decimal. |
jburhenn | 1:90dccf306268 | 156 | */ |
jburhenn | 23:1a9aed5e77c9 | 157 | inline void add(const double value, unsigned char precision = 3) { |
jburhenn | 0:09ef59d2d0f7 | 158 | char str[33]; |
jburhenn | 23:1a9aed5e77c9 | 159 | dtostrf(value, 1, precision, str); |
jburhenn | 23:1a9aed5e77c9 | 160 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 161 | } |
jburhenn | 0:09ef59d2d0f7 | 162 | |
jburhenn | 0:09ef59d2d0f7 | 163 | #else |
jburhenn | 1:90dccf306268 | 164 | /** |
jburhenn | 23:1a9aed5e77c9 | 165 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 166 | * @param[in] value The value to add. |
jburhenn | 23:1a9aed5e77c9 | 167 | * @param[in] precision Number of digits after the decimal. |
jburhenn | 1:90dccf306268 | 168 | */ |
jburhenn | 23:1a9aed5e77c9 | 169 | inline void add(const float value, unsigned char precision = 3) { |
jburhenn | 0:09ef59d2d0f7 | 170 | char str[33]; |
jburhenn | 23:1a9aed5e77c9 | 171 | snprintf(str, 33, "%.*f", precision, value); |
jburhenn | 23:1a9aed5e77c9 | 172 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 173 | } |
jburhenn | 0:09ef59d2d0f7 | 174 | |
jburhenn | 1:90dccf306268 | 175 | /** |
jburhenn | 23:1a9aed5e77c9 | 176 | * Add the specified value to the array. |
jburhenn | 23:1a9aed5e77c9 | 177 | * @param[in] precision Number of digits after the decimal. |
jburhenn | 1:90dccf306268 | 178 | */ |
jburhenn | 23:1a9aed5e77c9 | 179 | inline void add(const double value, unsigned char precision = 3) { |
jburhenn | 0:09ef59d2d0f7 | 180 | char str[33]; |
jburhenn | 23:1a9aed5e77c9 | 181 | snprintf(str, 33, "%.*f", precision, value); |
jburhenn | 23:1a9aed5e77c9 | 182 | add(str); |
jburhenn | 0:09ef59d2d0f7 | 183 | } |
jburhenn | 0:09ef59d2d0f7 | 184 | |
jburhenn | 0:09ef59d2d0f7 | 185 | #endif |
jburhenn | 0:09ef59d2d0f7 | 186 | |
jburhenn | 0:09ef59d2d0f7 | 187 | #ifdef CAYENNE_USING_PROGMEM |
jburhenn | 1:90dccf306268 | 188 | /** |
jburhenn | 23:1a9aed5e77c9 | 189 | * Add the specified value to the array. |
jburhenn | 1:90dccf306268 | 190 | * @param[in] value The value to add. |
jburhenn | 1:90dccf306268 | 191 | */ |
jburhenn | 23:1a9aed5e77c9 | 192 | void add(const __FlashStringHelper* value) { |
jburhenn | 23:1a9aed5e77c9 | 193 | const char* valueString = reinterpret_cast<const char *>(value); |
jburhenn | 23:1a9aed5e77c9 | 194 | add(valueString, true); |
jburhenn | 0:09ef59d2d0f7 | 195 | } |
jburhenn | 0:09ef59d2d0f7 | 196 | |
jburhenn | 0:09ef59d2d0f7 | 197 | #endif |
jburhenn | 1:90dccf306268 | 198 | /** |
jburhenn | 23:1a9aed5e77c9 | 199 | * Get the value string. |
jburhenn | 23:1a9aed5e77c9 | 200 | * @return If there are multiple values this will return the full array string with brackets, otherwise just the single value string without brackets. |
jburhenn | 1:90dccf306268 | 201 | */ |
jburhenn | 23:1a9aed5e77c9 | 202 | const char* getString() const { |
jburhenn | 23:1a9aed5e77c9 | 203 | if (_valueCount > 1) |
jburhenn | 23:1a9aed5e77c9 | 204 | return _buffer; |
jburhenn | 23:1a9aed5e77c9 | 205 | return &_buffer[1]; |
jburhenn | 0:09ef59d2d0f7 | 206 | } |
jburhenn | 0:09ef59d2d0f7 | 207 | |
jburhenn | 1:90dccf306268 | 208 | /** |
jburhenn | 23:1a9aed5e77c9 | 209 | * Get the number of items in the value array. |
jburhenn | 1:90dccf306268 | 210 | * @return Count of items. |
jburhenn | 1:90dccf306268 | 211 | */ |
jburhenn | 0:09ef59d2d0f7 | 212 | size_t getCount() const { |
jburhenn | 0:09ef59d2d0f7 | 213 | return _valueCount; |
jburhenn | 0:09ef59d2d0f7 | 214 | } |
jburhenn | 0:09ef59d2d0f7 | 215 | |
jburhenn | 0:09ef59d2d0f7 | 216 | private: |
jburhenn | 0:09ef59d2d0f7 | 217 | size_t _valueCount; |
jburhenn | 0:09ef59d2d0f7 | 218 | char _buffer[BUFFER_SIZE]; |
jburhenn | 0:09ef59d2d0f7 | 219 | size_t _index; |
jburhenn | 0:09ef59d2d0f7 | 220 | }; |
jburhenn | 0:09ef59d2d0f7 | 221 | } |
jburhenn | 0:09ef59d2d0f7 | 222 | |
jburhenn | 8:aec9cfdd4c8e | 223 | typedef CayenneMQTT::DataArray<> CayenneDataArray; |
jburhenn | 0:09ef59d2d0f7 | 224 | |
jburhenn | 0:09ef59d2d0f7 | 225 | #endif |
jburhenn | 0:09ef59d2d0f7 | 226 | |
jburhenn | 0:09ef59d2d0f7 | 227 | #endif |