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.c
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 #include "CayenneDataArray.h" 00019 #include <string.h> 00020 #include <stdio.h> 00021 00022 00023 /** 00024 * Initialize a data array of unit/value pairs. 00025 * @param[out] dataArray The initialized data array 00026 * @param[in] buffer Buffer for storing unit/value pairs. This buffer should be available for as long as the data array is used. 00027 * @param[in] bufferSize Size of the buffer 00028 */ 00029 void CayenneDataArrayInit(CayenneDataArray* dataArray, char* buffer, unsigned int bufferSize) 00030 { 00031 dataArray->valueCount = 0; 00032 dataArray->buffer = buffer; 00033 dataArray->bufferSize = bufferSize; 00034 dataArray->bufferIndex = 0; 00035 } 00036 00037 /** 00038 * Add the specified unit/value pair to the array. 00039 * @param[in] dataArray The data array to add values to 00040 * @param[in] unit The unit to add 00041 * @param[in] value The value to add 00042 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00043 */ 00044 int CayenneDataArrayAdd(CayenneDataArray* dataArray, const char* unit, const char* value) 00045 { 00046 if (dataArray->valueCount >= CAYENNE_MAX_MESSAGE_VALUES) 00047 return CAYENNE_FAILURE; 00048 00049 size_t unitLength = unit ? strlen(unit) + 1 : 0; 00050 size_t valueLength = value ? strlen(value) + 1 : 0; 00051 if (dataArray->bufferIndex + unitLength + valueLength > dataArray->bufferSize) 00052 return CAYENNE_FAILURE; 00053 00054 if (unit) { 00055 memcpy(dataArray->buffer + dataArray->bufferIndex, unit, unitLength); 00056 dataArray->values[dataArray->valueCount].unit = dataArray->buffer + dataArray->bufferIndex; 00057 dataArray->bufferIndex += unitLength; 00058 } 00059 else { 00060 dataArray->values[dataArray->valueCount].unit = NULL; 00061 } 00062 00063 if (value) { 00064 memcpy(dataArray->buffer + dataArray->bufferIndex, value, valueLength); 00065 dataArray->values[dataArray->valueCount].value = dataArray->buffer + dataArray->bufferIndex; 00066 dataArray->bufferIndex += valueLength; 00067 } 00068 else { 00069 dataArray->values[dataArray->valueCount].value = NULL; 00070 } 00071 00072 dataArray->valueCount++; 00073 return CAYENNE_SUCCESS; 00074 } 00075 00076 /** 00077 * Add the specified unit/value pair to the array. 00078 * @param[in] dataArray The data array to add values to 00079 * @param[in] unit The unit to add 00080 * @param[in] value The value to add 00081 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00082 */ 00083 int CayenneDataArrayAddInt(CayenneDataArray* dataArray, const char* unit, int value) 00084 { 00085 char str[2 + 8 * sizeof(value)]; 00086 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00087 itoa(value, str, 10); 00088 #else 00089 snprintf(str, sizeof(str), "%d", value); 00090 #endif 00091 return CayenneDataArrayAdd(dataArray, unit, str); 00092 } 00093 00094 /** 00095 * Add the specified unit/value pair to the array. 00096 * @param[in] dataArray The data array to add values to 00097 * @param[in] unit The unit to add 00098 * @param[in] value The value to add 00099 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00100 */ 00101 int CayenneDataArrayAddUInt(CayenneDataArray* dataArray, const char* unit, unsigned int value) 00102 { 00103 char str[1 + 8 * sizeof(value)]; 00104 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00105 utoa(value, str, 10); 00106 #else 00107 snprintf(str, sizeof(str), "%u", value); 00108 #endif 00109 return CayenneDataArrayAdd(dataArray, unit, str); 00110 } 00111 00112 /** 00113 * Add the specified unit/value pair to the array. 00114 * @param[in] dataArray The data array to add values to 00115 * @param[in] unit The unit to add 00116 * @param[in] value The value to add 00117 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00118 */ 00119 int CayenneDataArrayAddLong(CayenneDataArray* dataArray, const char* unit, long value) 00120 { 00121 char str[2 + 8 * sizeof(value)]; 00122 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00123 ltoa(value, str, 10); 00124 #else 00125 snprintf(str, sizeof(str), "%ld", value); 00126 #endif 00127 return CayenneDataArrayAdd(dataArray, unit, str); 00128 } 00129 00130 /** 00131 * Add the specified unit/value pair to the array. 00132 * @param[in] dataArray The data array to add values to 00133 * @param[in] unit The unit to add 00134 * @param[in] value The value to add 00135 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00136 */ 00137 int CayenneDataArrayAddULong(CayenneDataArray* dataArray, const char* unit, unsigned long value) 00138 { 00139 char str[1 + 8 * sizeof(value)]; 00140 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00141 ultoa(value, str, 10); 00142 #else 00143 snprintf(str, sizeof(str), "%lu", value); 00144 #endif 00145 return CayenneDataArrayAdd(dataArray, unit, str); 00146 } 00147 00148 /** 00149 * Add the specified unit/value pair to the array. 00150 * @param[in] dataArray The data array to add values to 00151 * @param[in] unit The unit to add 00152 * @param[in] value The value to add 00153 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00154 */ 00155 int CayenneDataArrayAddDouble(CayenneDataArray* dataArray, const char* unit, double value) 00156 { 00157 char str[33]; 00158 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00159 dtostrf(value, 5, 3, str); 00160 #else 00161 snprintf(str, 33, "%2.3f", value); 00162 #endif 00163 return CayenneDataArrayAdd(dataArray, unit, str); 00164 } 00165 00166 /** 00167 * Add the specified unit/value pair to the array. 00168 * @param[in] dataArray The data array to add values to 00169 * @param[in] unit The unit to add 00170 * @param[in] value The value to add 00171 * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise 00172 */ 00173 int CayenneDataArrayAddFloat(CayenneDataArray* dataArray, const char* unit, float value) 00174 { 00175 char str[33]; 00176 #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) 00177 dtostrf(value, 5, 3, str); 00178 #else 00179 snprintf(str, 33, "%2.3f", value); 00180 #endif 00181 return CayenneDataArrayAdd(dataArray, unit, str); 00182 } 00183 00184 /** 00185 * Clear the data array. 00186 * @param[in] dataArray The data array to clear 00187 */ 00188 void CayenneDataArrayClear(CayenneDataArray* dataArray) 00189 { 00190 dataArray->valueCount = 0; 00191 dataArray->bufferIndex = 0; 00192 memset(dataArray->buffer, 0, dataArray->bufferSize); 00193 }
Generated on Wed Jul 13 2022 08:52:00 by
1.7.2
