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.c@24:686d9ed1d192, 2017-04-25 (annotated)
- Committer:
- pferland
- Date:
- Tue Apr 25 15:32:32 2017 +0000
- Revision:
- 24:686d9ed1d192
- Parent:
- 0:09ef59d2d0f7
Small changes for compatibility with MTSAS socket library
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 | #include "CayenneDataArray.h" |
jburhenn | 0:09ef59d2d0f7 | 19 | #include <string.h> |
jburhenn | 0:09ef59d2d0f7 | 20 | #include <stdio.h> |
jburhenn | 0:09ef59d2d0f7 | 21 | |
jburhenn | 0:09ef59d2d0f7 | 22 | |
jburhenn | 0:09ef59d2d0f7 | 23 | /** |
jburhenn | 0:09ef59d2d0f7 | 24 | * Initialize a data array of unit/value pairs. |
jburhenn | 0:09ef59d2d0f7 | 25 | * @param[out] dataArray The initialized data array |
jburhenn | 0:09ef59d2d0f7 | 26 | * @param[in] buffer Buffer for storing unit/value pairs. This buffer should be available for as long as the data array is used. |
jburhenn | 0:09ef59d2d0f7 | 27 | * @param[in] bufferSize Size of the buffer |
jburhenn | 0:09ef59d2d0f7 | 28 | */ |
jburhenn | 0:09ef59d2d0f7 | 29 | void CayenneDataArrayInit(CayenneDataArray* dataArray, char* buffer, unsigned int bufferSize) |
jburhenn | 0:09ef59d2d0f7 | 30 | { |
jburhenn | 0:09ef59d2d0f7 | 31 | dataArray->valueCount = 0; |
jburhenn | 0:09ef59d2d0f7 | 32 | dataArray->buffer = buffer; |
jburhenn | 0:09ef59d2d0f7 | 33 | dataArray->bufferSize = bufferSize; |
jburhenn | 0:09ef59d2d0f7 | 34 | dataArray->bufferIndex = 0; |
jburhenn | 0:09ef59d2d0f7 | 35 | } |
jburhenn | 0:09ef59d2d0f7 | 36 | |
jburhenn | 0:09ef59d2d0f7 | 37 | /** |
jburhenn | 0:09ef59d2d0f7 | 38 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 39 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 40 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 41 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 42 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 43 | */ |
jburhenn | 0:09ef59d2d0f7 | 44 | int CayenneDataArrayAdd(CayenneDataArray* dataArray, const char* unit, const char* value) |
jburhenn | 0:09ef59d2d0f7 | 45 | { |
jburhenn | 0:09ef59d2d0f7 | 46 | if (dataArray->valueCount >= CAYENNE_MAX_MESSAGE_VALUES) |
jburhenn | 0:09ef59d2d0f7 | 47 | return CAYENNE_FAILURE; |
jburhenn | 0:09ef59d2d0f7 | 48 | |
jburhenn | 0:09ef59d2d0f7 | 49 | size_t unitLength = unit ? strlen(unit) + 1 : 0; |
jburhenn | 0:09ef59d2d0f7 | 50 | size_t valueLength = value ? strlen(value) + 1 : 0; |
jburhenn | 0:09ef59d2d0f7 | 51 | if (dataArray->bufferIndex + unitLength + valueLength > dataArray->bufferSize) |
jburhenn | 0:09ef59d2d0f7 | 52 | return CAYENNE_FAILURE; |
jburhenn | 0:09ef59d2d0f7 | 53 | |
jburhenn | 0:09ef59d2d0f7 | 54 | if (unit) { |
jburhenn | 0:09ef59d2d0f7 | 55 | memcpy(dataArray->buffer + dataArray->bufferIndex, unit, unitLength); |
jburhenn | 0:09ef59d2d0f7 | 56 | dataArray->values[dataArray->valueCount].unit = dataArray->buffer + dataArray->bufferIndex; |
jburhenn | 0:09ef59d2d0f7 | 57 | dataArray->bufferIndex += unitLength; |
jburhenn | 0:09ef59d2d0f7 | 58 | } |
jburhenn | 0:09ef59d2d0f7 | 59 | else { |
jburhenn | 0:09ef59d2d0f7 | 60 | dataArray->values[dataArray->valueCount].unit = NULL; |
jburhenn | 0:09ef59d2d0f7 | 61 | } |
jburhenn | 0:09ef59d2d0f7 | 62 | |
jburhenn | 0:09ef59d2d0f7 | 63 | if (value) { |
jburhenn | 0:09ef59d2d0f7 | 64 | memcpy(dataArray->buffer + dataArray->bufferIndex, value, valueLength); |
jburhenn | 0:09ef59d2d0f7 | 65 | dataArray->values[dataArray->valueCount].value = dataArray->buffer + dataArray->bufferIndex; |
jburhenn | 0:09ef59d2d0f7 | 66 | dataArray->bufferIndex += valueLength; |
jburhenn | 0:09ef59d2d0f7 | 67 | } |
jburhenn | 0:09ef59d2d0f7 | 68 | else { |
jburhenn | 0:09ef59d2d0f7 | 69 | dataArray->values[dataArray->valueCount].value = NULL; |
jburhenn | 0:09ef59d2d0f7 | 70 | } |
jburhenn | 0:09ef59d2d0f7 | 71 | |
jburhenn | 0:09ef59d2d0f7 | 72 | dataArray->valueCount++; |
jburhenn | 0:09ef59d2d0f7 | 73 | return CAYENNE_SUCCESS; |
jburhenn | 0:09ef59d2d0f7 | 74 | } |
jburhenn | 0:09ef59d2d0f7 | 75 | |
jburhenn | 0:09ef59d2d0f7 | 76 | /** |
jburhenn | 0:09ef59d2d0f7 | 77 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 78 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 79 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 80 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 81 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 82 | */ |
jburhenn | 0:09ef59d2d0f7 | 83 | int CayenneDataArrayAddInt(CayenneDataArray* dataArray, const char* unit, int value) |
jburhenn | 0:09ef59d2d0f7 | 84 | { |
jburhenn | 0:09ef59d2d0f7 | 85 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 86 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 87 | itoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 88 | #else |
jburhenn | 0:09ef59d2d0f7 | 89 | snprintf(str, sizeof(str), "%d", value); |
jburhenn | 0:09ef59d2d0f7 | 90 | #endif |
jburhenn | 0:09ef59d2d0f7 | 91 | return CayenneDataArrayAdd(dataArray, unit, str); |
jburhenn | 0:09ef59d2d0f7 | 92 | } |
jburhenn | 0:09ef59d2d0f7 | 93 | |
jburhenn | 0:09ef59d2d0f7 | 94 | /** |
jburhenn | 0:09ef59d2d0f7 | 95 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 96 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 97 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 98 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 99 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 100 | */ |
jburhenn | 0:09ef59d2d0f7 | 101 | int CayenneDataArrayAddUInt(CayenneDataArray* dataArray, const char* unit, unsigned int value) |
jburhenn | 0:09ef59d2d0f7 | 102 | { |
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 | 0:09ef59d2d0f7 | 109 | return CayenneDataArrayAdd(dataArray, unit, str); |
jburhenn | 0:09ef59d2d0f7 | 110 | } |
jburhenn | 0:09ef59d2d0f7 | 111 | |
jburhenn | 0:09ef59d2d0f7 | 112 | /** |
jburhenn | 0:09ef59d2d0f7 | 113 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 114 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 115 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 116 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 117 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 118 | */ |
jburhenn | 0:09ef59d2d0f7 | 119 | int CayenneDataArrayAddLong(CayenneDataArray* dataArray, const char* unit, long value) |
jburhenn | 0:09ef59d2d0f7 | 120 | { |
jburhenn | 0:09ef59d2d0f7 | 121 | char str[2 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 122 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 123 | ltoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 124 | #else |
jburhenn | 0:09ef59d2d0f7 | 125 | snprintf(str, sizeof(str), "%ld", value); |
jburhenn | 0:09ef59d2d0f7 | 126 | #endif |
jburhenn | 0:09ef59d2d0f7 | 127 | return CayenneDataArrayAdd(dataArray, unit, str); |
jburhenn | 0:09ef59d2d0f7 | 128 | } |
jburhenn | 0:09ef59d2d0f7 | 129 | |
jburhenn | 0:09ef59d2d0f7 | 130 | /** |
jburhenn | 0:09ef59d2d0f7 | 131 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 132 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 133 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 134 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 135 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 136 | */ |
jburhenn | 0:09ef59d2d0f7 | 137 | int CayenneDataArrayAddULong(CayenneDataArray* dataArray, const char* unit, unsigned long value) |
jburhenn | 0:09ef59d2d0f7 | 138 | { |
jburhenn | 0:09ef59d2d0f7 | 139 | char str[1 + 8 * sizeof(value)]; |
jburhenn | 0:09ef59d2d0f7 | 140 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 141 | ultoa(value, str, 10); |
jburhenn | 0:09ef59d2d0f7 | 142 | #else |
jburhenn | 0:09ef59d2d0f7 | 143 | snprintf(str, sizeof(str), "%lu", value); |
jburhenn | 0:09ef59d2d0f7 | 144 | #endif |
jburhenn | 0:09ef59d2d0f7 | 145 | return CayenneDataArrayAdd(dataArray, unit, str); |
jburhenn | 0:09ef59d2d0f7 | 146 | } |
jburhenn | 0:09ef59d2d0f7 | 147 | |
jburhenn | 0:09ef59d2d0f7 | 148 | /** |
jburhenn | 0:09ef59d2d0f7 | 149 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 150 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 151 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 152 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 153 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 154 | */ |
jburhenn | 0:09ef59d2d0f7 | 155 | int CayenneDataArrayAddDouble(CayenneDataArray* dataArray, const char* unit, double value) |
jburhenn | 0:09ef59d2d0f7 | 156 | { |
jburhenn | 0:09ef59d2d0f7 | 157 | char str[33]; |
jburhenn | 0:09ef59d2d0f7 | 158 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 159 | dtostrf(value, 5, 3, str); |
jburhenn | 0:09ef59d2d0f7 | 160 | #else |
jburhenn | 0:09ef59d2d0f7 | 161 | snprintf(str, 33, "%2.3f", value); |
jburhenn | 0:09ef59d2d0f7 | 162 | #endif |
jburhenn | 0:09ef59d2d0f7 | 163 | return CayenneDataArrayAdd(dataArray, unit, str); |
jburhenn | 0:09ef59d2d0f7 | 164 | } |
jburhenn | 0:09ef59d2d0f7 | 165 | |
jburhenn | 0:09ef59d2d0f7 | 166 | /** |
jburhenn | 0:09ef59d2d0f7 | 167 | * Add the specified unit/value pair to the array. |
jburhenn | 0:09ef59d2d0f7 | 168 | * @param[in] dataArray The data array to add values to |
jburhenn | 0:09ef59d2d0f7 | 169 | * @param[in] unit The unit to add |
jburhenn | 0:09ef59d2d0f7 | 170 | * @param[in] value The value to add |
jburhenn | 0:09ef59d2d0f7 | 171 | * @return CAYENNE_SUCCESS if unit/value pair was add, CAYENNE_FAILURE otherwise |
jburhenn | 0:09ef59d2d0f7 | 172 | */ |
jburhenn | 0:09ef59d2d0f7 | 173 | int CayenneDataArrayAddFloat(CayenneDataArray* dataArray, const char* unit, float value) |
jburhenn | 0:09ef59d2d0f7 | 174 | { |
jburhenn | 0:09ef59d2d0f7 | 175 | char str[33]; |
jburhenn | 0:09ef59d2d0f7 | 176 | #if defined(__AVR__) || defined (ARDUINO_ARCH_ARC32) |
jburhenn | 0:09ef59d2d0f7 | 177 | dtostrf(value, 5, 3, str); |
jburhenn | 0:09ef59d2d0f7 | 178 | #else |
jburhenn | 0:09ef59d2d0f7 | 179 | snprintf(str, 33, "%2.3f", value); |
jburhenn | 0:09ef59d2d0f7 | 180 | #endif |
jburhenn | 0:09ef59d2d0f7 | 181 | return CayenneDataArrayAdd(dataArray, unit, str); |
jburhenn | 0:09ef59d2d0f7 | 182 | } |
jburhenn | 0:09ef59d2d0f7 | 183 | |
jburhenn | 0:09ef59d2d0f7 | 184 | /** |
jburhenn | 0:09ef59d2d0f7 | 185 | * Clear the data array. |
jburhenn | 0:09ef59d2d0f7 | 186 | * @param[in] dataArray The data array to clear |
jburhenn | 0:09ef59d2d0f7 | 187 | */ |
jburhenn | 0:09ef59d2d0f7 | 188 | void CayenneDataArrayClear(CayenneDataArray* dataArray) |
jburhenn | 0:09ef59d2d0f7 | 189 | { |
jburhenn | 0:09ef59d2d0f7 | 190 | dataArray->valueCount = 0; |
jburhenn | 0:09ef59d2d0f7 | 191 | dataArray->bufferIndex = 0; |
jburhenn | 0:09ef59d2d0f7 | 192 | memset(dataArray->buffer, 0, dataArray->bufferSize); |
jburhenn | 0:09ef59d2d0f7 | 193 | } |