Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: 5_Dragonfly_Cayenne_Sprint_IKS01A1
Fork of Cayenne-MQTT-mbed-MTSAS by
CayenneDataArray.h
00001 /* 00002 The MIT License(MIT) 00003 00004 Cayenne MQTT Client Library 00005 Copyright (c) 2016 myDevices 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 00008 documentation files(the "Software"), to deal in the Software without restriction, including without limitation 00009 the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, 00010 and to permit persons to whom the Software is furnished to do so, subject to the following conditions : 00011 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 00013 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR 00014 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00015 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00016 */ 00017 00018 #ifndef _CAYENNEDATAARRAY_h 00019 #define _CAYENNEDATAARRAY_h 00020 00021 #include "CayenneUtils.h" 00022 00023 #if defined(__cplusplus) 00024 00025 // C++ version of the data array. This is defined here so it requires no source file. 00026 00027 namespace CayenneMQTT 00028 { 00029 /** 00030 * @class DataArray 00031 * Class for manipulating a data array of unit/value pairs. 00032 * @param BUFFER_SIZE Maximum buffer size to use for data array, in bytes. 00033 * @param MAX_VALUES Maximum number of unit/value pairs in the array. 00034 */ 00035 template<int BUFFER_SIZE = CAYENNE_MAX_MESSAGE_SIZE, int MAX_VALUES = CAYENNE_MAX_MESSAGE_VALUES> 00036 class DataArray 00037 { 00038 public: 00039 /** 00040 * Construct an empty array. 00041 */ 00042 DataArray() { 00043 clear(); 00044 } 00045 00046 /** 00047 * Clear the array. 00048 */ 00049 void clear() { 00050 for (int i = 0; i < CAYENNE_MAX_MESSAGE_VALUES; ++i) { 00051 _values[i].unit = NULL; 00052 _values[i].value = NULL; 00053 } 00054 _valueCount = 0; 00055 _index = 0; 00056 } 00057 00058 /** 00059 * Add the specified unit/value pair to the array. 00060 * @param[in] unit The unit to add. 00061 * @param[in] value The value to add. 00062 * @param[in] unitInFlash If true the unit string is in flash memory, otherwise false. 00063 * @param[in] valueInFlash If true the value string is in flash memory, otherwise false. 00064 */ 00065 void add(const char* unit, const char* value, bool unitInFlash = false, bool valueInFlash = false) { 00066 if (_valueCount >= CAYENNE_MAX_MESSAGE_VALUES) 00067 return; 00068 00069 size_t unitLength = 0; 00070 if (unit) { 00071 unitLength = (unitInFlash ? CAYENNE_STRLEN(unit) : strlen(unit)) + 1; 00072 } 00073 size_t valueLength = 0; 00074 if (value) { 00075 valueLength = (valueInFlash ? CAYENNE_STRLEN(value) : strlen(value)) + 1; 00076 } 00077 if (_index + unitLength + valueLength > BUFFER_SIZE) 00078 return; 00079 00080 if (unit) { 00081 unitInFlash ? CAYENNE_MEMCPY(_buffer + _index, unit, unitLength) : memcpy(_buffer + _index, unit, unitLength); 00082 _values[_valueCount].unit = _buffer + _index; 00083 _index += unitLength; 00084 } 00085 else { 00086 _values[_valueCount].unit = NULL; 00087 } 00088 00089 if (value) { 00090 valueInFlash ? CAYENNE_MEMCPY(_buffer + _index, value, valueLength) : memcpy(_buffer + _index, value, valueLength); 00091 _values[_valueCount].value = _buffer + _index; 00092 _index += valueLength; 00093 } 00094 else { 00095 _values[_valueCount].value = NULL; 00096 } 00097 00098 _valueCount++; 00099 } 00100 00101 /** 00102 * Add the specified unit/value pair to the array. 00103 * @param[in] unit The unit to add. 00104 * @param[in] value The value to add. 00105 */ 00106 inline void add(const char* unit, const int value) { 00107 char str[2 + 8 * sizeof(value)]; 00108 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00109 itoa(value, str, 10); 00110 #else 00111 snprintf(str, sizeof(str), "%d", value); 00112 #endif 00113 add(unit, str); 00114 } 00115 00116 /** 00117 * Add the specified unit/value pair to the array. 00118 * @param[in] unit The unit to add. 00119 * @param[in] value The value to add. 00120 */ 00121 inline void add(const char* unit, const unsigned int value) { 00122 char str[1 + 8 * sizeof(value)]; 00123 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00124 utoa(value, str, 10); 00125 #else 00126 snprintf(str, sizeof(str), "%u", value); 00127 #endif 00128 add(unit, str); 00129 } 00130 00131 /** 00132 * Add the specified unit/value pair to the array. 00133 * @param[in] unit The unit to add. 00134 * @param[in] value The value to add. 00135 */ 00136 inline void add(const char* unit, const long value) { 00137 char str[2 + 8 * sizeof(value)]; 00138 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00139 ltoa(value, str, 10); 00140 #else 00141 snprintf(str, sizeof(str), "%ld", value); 00142 #endif 00143 add(unit, str); 00144 } 00145 00146 /** 00147 * Add the specified unit/value pair to the array. 00148 * @param[in] unit The unit to add. 00149 * @param[in] value The value to add. 00150 */ 00151 inline void add(const char* unit, const unsigned long value) { 00152 char str[1 + 8 * sizeof(value)]; 00153 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00154 ultoa(value, str, 10); 00155 #else 00156 snprintf(str, sizeof(str), "%lu", value); 00157 #endif 00158 add(unit, str); 00159 } 00160 00161 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00162 /** 00163 * Add the specified unit/value pair to the array. 00164 * @param[in] unit The unit to add. 00165 * @param[in] value The value to add. 00166 */ 00167 inline void add(const char* unit, const float value) { 00168 char str[33]; 00169 dtostrf(value, 5, 3, str); 00170 add(unit, str); 00171 } 00172 00173 /** 00174 * Add the specified unit/value pair to the array. 00175 * @param[in] unit The unit to add. 00176 * @param[in] value The value to add. 00177 */ 00178 inline void add(const char* unit, const double value) { 00179 char str[33]; 00180 dtostrf(value, 5, 3, str); 00181 add(unit, str); 00182 } 00183 00184 #else 00185 /** 00186 * Add the specified unit/value pair to the array. 00187 * @param[in] unit The unit to add. 00188 * @param[in] value The value to add. 00189 */ 00190 inline void add(const char* unit, const float value) { 00191 char str[33]; 00192 snprintf(str, 33, "%2.3f", value); 00193 add(unit, str); 00194 } 00195 00196 /** 00197 * Add the specified unit/value pair to the array. 00198 * @param[in] unit The unit to add. 00199 * @param[in] value The value to add. 00200 */ 00201 inline void add(const char* unit, const double value) { 00202 char str[33]; 00203 snprintf(str, 33, "%2.3f", value); 00204 add(unit, str); 00205 } 00206 00207 #endif 00208 00209 #ifdef CAYENNE_USING_PROGMEM 00210 /** 00211 * Add the specified unit/value pair to the array. 00212 * @param[in] unit The unit to add. 00213 * @param[in] value The value to add. 00214 */ 00215 void add(const char* unit, const __FlashStringHelper* value) { 00216 const char* valueString = reinterpret_cast<const char *>(value); 00217 add(unit, valueString, false, true); 00218 } 00219 00220 /** 00221 * Add the specified unit/value pair to the array. 00222 * @param[in] unit The unit to add. 00223 * @param[in] value The value to add. 00224 */ 00225 void add(const __FlashStringHelper* unit, const char* value) { 00226 const char* unitString = reinterpret_cast<const char *>(unit); 00227 add(unitString, value, true, false); 00228 } 00229 00230 /** 00231 * Add the specified unit/value pair to the array. 00232 * @param[in] unit The unit to add. 00233 * @param[in] value The value to add. 00234 */ 00235 void add(const __FlashStringHelper* unit, const __FlashStringHelper* value) { 00236 const char* unitString = reinterpret_cast<const char *>(unit); 00237 const char* valueString = reinterpret_cast<const char *>(value); 00238 add(unitString, valueString, true, true); 00239 } 00240 00241 /** 00242 * Add the specified unit/value pair to the array. 00243 * @param[in] unit The unit to add. 00244 * @param[in] value The value to add. 00245 */ 00246 inline void add(const __FlashStringHelper* unit, const int value) { 00247 char str[2 + 8 * sizeof(value)]; 00248 itoa(value, str, 10); 00249 add(unit, str); 00250 } 00251 00252 /** 00253 * Add the specified unit/value pair to the array. 00254 * @param[in] unit The unit to add. 00255 * @param[in] value The value to add. 00256 */ 00257 inline void add(const __FlashStringHelper* unit, const unsigned int value) { 00258 char str[1 + 8 * sizeof(value)]; 00259 utoa(value, str, 10); 00260 add(unit, str); 00261 } 00262 00263 /** 00264 * Add the specified unit/value pair to the array. 00265 * @param[in] unit The unit to add. 00266 * @param[in] value The value to add. 00267 */ 00268 inline void add(const __FlashStringHelper* unit, const long value) { 00269 char str[2 + 8 * sizeof(value)]; 00270 ltoa(value, str, 10); 00271 add(unit, str); 00272 } 00273 00274 /** 00275 * Add the specified unit/value pair to the array. 00276 * @param[in] unit The unit to add. 00277 * @param[in] value The value to add. 00278 */ 00279 inline void add(const __FlashStringHelper* unit, const unsigned long value) { 00280 char str[1 + 8 * sizeof(value)]; 00281 ultoa(value, str, 10); 00282 add(unit, str); 00283 } 00284 00285 /** 00286 * Add the specified unit/value pair to the array. 00287 * @param[in] unit The unit to add. 00288 * @param[in] value The value to add. 00289 */ 00290 inline void add(const __FlashStringHelper* unit, const float value) { 00291 char str[33]; 00292 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00293 dtostrf(value, 5, 3, str); 00294 #else 00295 snprintf(str, 33, "%2.3f", value); 00296 #endif 00297 add(unit, str); 00298 } 00299 00300 /** 00301 * Add the specified unit/value pair to the array. 00302 * @param[in] unit The unit to add. 00303 * @param[in] value The value to add. 00304 */ 00305 inline void add(const __FlashStringHelper* unit, const double value) { 00306 char str[33]; 00307 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00308 dtostrf(value, 5, 3, str); 00309 #else 00310 snprintf(str, 33, "%2.3f", value); 00311 #endif 00312 add(unit, str); 00313 } 00314 00315 #endif 00316 /** 00317 * Get the unit/value pair array. 00318 * @return Pointer to the array. 00319 */ 00320 const CayenneValuePair* getArray() const { 00321 return _values; 00322 } 00323 00324 /** 00325 * Get the number of items in the unit/value pair array. 00326 * @return Count of items. 00327 */ 00328 size_t getCount() const { 00329 return _valueCount; 00330 } 00331 00332 private: 00333 CayenneValuePair _values[MAX_VALUES]; 00334 size_t _valueCount; 00335 char _buffer[BUFFER_SIZE]; 00336 size_t _index; 00337 }; 00338 } 00339 00340 typedef CayenneMQTT::DataArray<> CayenneDataArray; 00341 00342 #else 00343 00344 // C version of the data array. Requires source file to be compiled and linked. 00345 00346 typedef struct CayenneDataArray 00347 { 00348 CayenneValuePair values[CAYENNE_MAX_MESSAGE_VALUES]; 00349 unsigned int valueCount; 00350 char* buffer; 00351 unsigned int bufferSize; 00352 unsigned int bufferIndex; 00353 } CayenneDataArray; 00354 00355 /** 00356 * Initialize a data array of unit/value pairs. 00357 * @param[out] dataArray The initialized data array 00358 * @param[in] buffer Buffer for storing unit/value pairs. This buffer should be available for as long as the data array is used. 00359 * @param[in] bufferSize Size of the buffer 00360 */ 00361 DLLExport void CayenneDataArrayInit(CayenneDataArray* dataArray, char* buffer, unsigned int bufferSize); 00362 00363 /** 00364 * Add the specified unit/value pair to the array. 00365 * @param[in] dataArray The data array to add values to 00366 * @param[in] unit The unit to add 00367 * @param[in] value The value to add 00368 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00369 */ 00370 DLLExport int CayenneDataArrayAdd(CayenneDataArray* dataArray, const char* unit, const char* value); 00371 00372 /** 00373 * Add the specified unit/value pair to the array. 00374 * @param[in] dataArray The data array to add values to 00375 * @param[in] unit The unit to add 00376 * @param[in] value The value to add 00377 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00378 */ 00379 DLLExport int CayenneDataArrayAddInt(CayenneDataArray* dataArray, const char* unit, int value); 00380 00381 /** 00382 * Add the specified unit/value pair to the array. 00383 * @param[in] dataArray The data array to add values to 00384 * @param[in] unit The unit to add 00385 * @param[in] value The value to add 00386 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00387 */ 00388 DLLExport int CayenneDataArrayAddUInt(CayenneDataArray* dataArray, const char* unit, unsigned int value); 00389 00390 /** 00391 * Add the specified unit/value pair to the array. 00392 * @param[in] dataArray The data array to add values to 00393 * @param[in] unit The unit to add 00394 * @param[in] value The value to add 00395 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00396 */ 00397 DLLExport int CayenneDataArrayAddLong(CayenneDataArray* dataArray, const char* unit, long value); 00398 00399 /** 00400 * Add the specified unit/value pair to the array. 00401 * @param[in] dataArray The data array to add values to 00402 * @param[in] unit The unit to add 00403 * @param[in] value The value to add 00404 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00405 */ 00406 DLLExport int CayenneDataArrayAddULong(CayenneDataArray* dataArray, const char* unit, unsigned long value); 00407 00408 /** 00409 * Add the specified unit/value pair to the array. 00410 * @param[in] dataArray The data array to add values to 00411 * @param[in] unit The unit to add 00412 * @param[in] value The value to add 00413 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00414 */ 00415 DLLExport int CayenneDataArrayAddDouble(CayenneDataArray* dataArray, const char* unit, double value); 00416 00417 /** 00418 * Add the specified unit/value pair to the array. 00419 * @param[in] dataArray The data array to add values to 00420 * @param[in] unit The unit to add 00421 * @param[in] value The value to add 00422 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00423 */ 00424 DLLExport int CayenneDataArrayAddFloat(CayenneDataArray* dataArray, const char* unit, float value); 00425 00426 /** 00427 * Clear the data array. 00428 * @param[in] dataArray The data array to clear 00429 */ 00430 DLLExport void CayenneDataArrayClear(CayenneDataArray* dataArray); 00431 00432 #endif 00433 00434 #endif
Generated on Wed Jul 13 2022 08:52:00 by
1.7.2
