21 #ifndef LE_PACK_H_INCLUDE_GUARD 22 #define LE_PACK_H_INCLUDE_GUARD 30 typedef uint8_t TagID_t;
33 #define LE_PACK_UINT8 1 34 #define LE_PACK_UINT16 2 35 #define LE_PACK_UINT32 3 36 #define LE_PACK_UINT64 4 37 #define LE_PACK_INT8 5 38 #define LE_PACK_INT16 6 39 #define LE_PACK_INT32 7 40 #define LE_PACK_INT64 8 41 #define LE_PACK_SIZE 9 42 #define LE_PACK_BOOL 10 43 #define LE_PACK_CHAR 11 44 #define LE_PACK_DOUBLE 12 45 #define LE_PACK_RESULT 13 46 #define LE_PACK_ONOFF 14 47 #define LE_PACK_REFERENCE 15 48 #define LE_PACK_STRING 16 49 #define LE_PACK_ARRAYHEADER 17 50 #define LE_PACK_IN_STRING_POINTER 18 51 #define LE_PACK_OUT_STRING_POINTER 19 52 #define LE_PACK_IN_ARRAY_POINTER 20 53 #define LE_PACK_OUT_ARRAY_POINTER 21 54 #define LE_PACK_STRING_RESPONSE_SIZE 22 55 #define LE_PACK_ARRAY_RESPONSE_SIZE 23 57 #define LE_PACK_SIZEOF_TAG_ID sizeof(TagID_t) 58 #define LE_PACK_SIZEOF_BOOL sizeof(bool) 59 #define LE_PACK_SIZEOF_CHAR sizeof(char) 60 #define LE_PACK_SIZEOF_UINT8 sizeof(uint8_t) 61 #define LE_PACK_SIZEOF_UINT16 sizeof(uint16_t) 62 #define LE_PACK_SIZEOF_UINT32 sizeof(uint32_t) 63 #define LE_PACK_SIZEOF_UINT64 sizeof(uint64_t) 64 #define LE_PACK_SIZEOF_INT8 sizeof(int8_t) 65 #define LE_PACK_SIZEOF_INT16 sizeof(int16_t) 66 #define LE_PACK_SIZEOF_INT32 sizeof(int32_t) 67 #define LE_PACK_SIZEOF_INT64 sizeof(int64_t) 68 #define LE_PACK_SIZEOF_DOUBLE sizeof(double) 69 #define LE_PACK_SIZEOF_RESULT LE_PACK_SIZEOF_UINT32 70 #define LE_PACK_SIZEOF_ONOFF LE_PACK_SIZEOF_UINT32 71 #define LE_PACK_SIZEOF_SIZE LE_PACK_SIZEOF_UINT32 72 #define LE_PACK_SIZEOF_REFERENCE LE_PACK_SIZEOF_UINT32 81 #define LE_PACK_PACK_SIMPLE_VALUE(value) \ 83 memcpy(*bufferPtr, &(value), sizeof(value)); \ 84 *bufferPtr = *bufferPtr + sizeof(value); \ 104 LE_PACK_PACK_SIMPLE_VALUE(value);
124 le_pack_PackTagID(bufferPtr, LE_PACK_UINT8);
126 LE_PACK_PACK_SIMPLE_VALUE(value);
147 le_pack_PackTagID(bufferPtr, tagId);
148 LE_PACK_PACK_SIMPLE_VALUE(value);
165 uint16_t newValue = htobe16(value);
166 le_pack_PackTagID(bufferPtr, LE_PACK_UINT16);
167 LE_PACK_PACK_SIMPLE_VALUE(newValue);
169 LE_PACK_PACK_SIMPLE_VALUE(value);
192 uint16_t newValue = htobe16(value);
193 le_pack_PackTagID(bufferPtr, tagId);
194 LE_PACK_PACK_SIMPLE_VALUE(newValue);
211 uint32_t newValue = htobe32(value);
212 le_pack_PackTagID(bufferPtr, LE_PACK_UINT32);
213 LE_PACK_PACK_SIMPLE_VALUE(newValue);
215 LE_PACK_PACK_SIMPLE_VALUE(value);
238 uint32_t newValue = htobe32(value);
239 le_pack_PackTagID(bufferPtr, tagId);
240 LE_PACK_PACK_SIMPLE_VALUE(newValue);
257 uint64_t newValue = htobe64(value);
258 le_pack_PackTagID(bufferPtr, LE_PACK_UINT64);
259 LE_PACK_PACK_SIMPLE_VALUE(newValue);
261 LE_PACK_PACK_SIMPLE_VALUE(value);
283 uint64_t newValue = htobe64(value);
284 le_pack_PackTagID(bufferPtr, tagId);
285 LE_PACK_PACK_SIMPLE_VALUE(newValue);
302 le_pack_PackTagID(bufferPtr, LE_PACK_INT8);
304 LE_PACK_PACK_SIMPLE_VALUE(value);
325 le_pack_PackTagID(bufferPtr, tagId);
326 LE_PACK_PACK_SIMPLE_VALUE(value);
343 int16_t newValue = htobe16(value);
344 le_pack_PackTagID(bufferPtr, LE_PACK_INT16);
345 LE_PACK_PACK_SIMPLE_VALUE(newValue);
347 LE_PACK_PACK_SIMPLE_VALUE(value);
369 int16_t newValue = htobe16(value);
370 le_pack_PackTagID(bufferPtr, tagId);
371 LE_PACK_PACK_SIMPLE_VALUE(newValue);
388 int32_t newValue = htobe32(value);
389 le_pack_PackTagID(bufferPtr, LE_PACK_INT32);
390 LE_PACK_PACK_SIMPLE_VALUE(newValue);
392 LE_PACK_PACK_SIMPLE_VALUE(value);
414 int32_t newValue = htobe32(value);
415 le_pack_PackTagID(bufferPtr, tagId);
416 LE_PACK_PACK_SIMPLE_VALUE(newValue);
433 int64_t newValue = htobe64(value);
434 le_pack_PackTagID(bufferPtr, LE_PACK_INT64);
435 LE_PACK_PACK_SIMPLE_VALUE(newValue);
437 LE_PACK_PACK_SIMPLE_VALUE(value);
459 int64_t newValue = htobe64(value);
460 le_pack_PackTagID(bufferPtr, tagId);
461 LE_PACK_PACK_SIMPLE_VALUE(newValue);
485 if (size > UINT32_MAX)
490 le_pack_PackTagID(bufferPtr, tagId);
492 uint32_t newSize = htobe32(size);
493 LE_PACK_PACK_SIMPLE_VALUE(newSize);
495 uint32_t newValue = htobe32(value);
496 LE_PACK_PACK_SIMPLE_VALUE(newValue);
519 if (size > UINT32_MAX)
524 le_pack_PackTagID(bufferPtr, tagId);
526 uint32_t newSize = htobe32(size);
527 LE_PACK_PACK_SIMPLE_VALUE(newSize);
529 uint64_t newValue = htobe64(value);
530 LE_PACK_PACK_SIMPLE_VALUE(newValue);
548 if (value > UINT32_MAX)
554 return le_pack_PackTaggedUint32(bufferPtr, value, LE_PACK_SIZE);
556 return le_pack_PackUint32(bufferPtr, value);
576 if (value > UINT32_MAX)
581 return le_pack_PackTaggedUint32(bufferPtr, value, tagId);
597 le_pack_PackTagID(bufferPtr, LE_PACK_BOOL);
602 uint8_t simpleValue = ((value)?1:0);
603 LE_PACK_PACK_SIMPLE_VALUE(simpleValue);
624 le_pack_PackTagID(bufferPtr, tagId);
628 uint8_t simpleValue = ((value)?1:0);
629 LE_PACK_PACK_SIMPLE_VALUE(simpleValue);
646 le_pack_PackTagID(bufferPtr, LE_PACK_CHAR);
648 LE_PACK_PACK_SIMPLE_VALUE(value);
669 le_pack_PackTagID(bufferPtr, tagId);
670 LE_PACK_PACK_SIMPLE_VALUE(value);
687 double newValue = htobe64(value);
688 le_pack_PackTagID(bufferPtr, LE_PACK_DOUBLE);
689 LE_PACK_PACK_SIMPLE_VALUE(newValue);
691 LE_PACK_PACK_SIMPLE_VALUE(value);
713 double newValue = htobe64(value);
714 le_pack_PackTagID(bufferPtr, tagId);
715 LE_PACK_PACK_SIMPLE_VALUE(newValue);
732 int32_t newValue = htobe32((int32_t) value);
733 le_pack_PackTagID(bufferPtr, LE_PACK_RESULT);
734 LE_PACK_PACK_SIMPLE_VALUE(newValue);
736 LE_PACK_PACK_SIMPLE_VALUE(value);
758 int32_t newValue = htobe32((int32_t) value);
759 le_pack_PackTagID(bufferPtr, tagId);
760 LE_PACK_PACK_SIMPLE_VALUE(newValue);
777 int32_t newValue = htobe32((int32_t) value);
778 le_pack_PackTagID(bufferPtr, LE_PACK_ONOFF);
779 LE_PACK_PACK_SIMPLE_VALUE(newValue);
781 LE_PACK_PACK_SIMPLE_VALUE(value);
803 int32_t newValue = htobe32((int32_t) value);
804 le_pack_PackTagID(bufferPtr, tagId);
805 LE_PACK_PACK_SIMPLE_VALUE(newValue);
810 #undef LE_PACK_PACK_SIMPLE_VALUE 823 size_t refAsInt = (size_t)ref;
828 if ((refAsInt <= UINT32_MAX) &&
829 ((refAsInt & 0x01) ||
833 return le_pack_PackTaggedUint32(bufferPtr, (uint32_t)refAsInt, LE_PACK_REFERENCE);
835 return le_pack_PackUint32(bufferPtr, (uint32_t)refAsInt);
858 size_t refAsInt = (size_t)ref;
863 if ((refAsInt <= UINT32_MAX) &&
864 ((refAsInt & 0x01) ||
867 return le_pack_PackTaggedUint32(bufferPtr, (uint32_t)refAsInt, tagId);
886 const char *stringPtr,
887 uint32_t maxStringCount
897 #if defined(__KLOCWORK__) 902 maxStringCount = strnlen(stringPtr, maxStringCount);
907 for (bytesCopied = 0;
908 (bytesCopied < maxStringCount) && (stringPtr[bytesCopied] !=
'\0');
912 (*bufferPtr)[bytesCopied +
sizeof(uint32_t) +
sizeof(TagID_t)] = stringPtr[bytesCopied];
914 (*bufferPtr)[bytesCopied +
sizeof(uint32_t)] = stringPtr[bytesCopied];
919 if (stringPtr[bytesCopied] !=
'\0')
927 bool packResult = le_pack_PackTaggedUint32(bufferPtr, bytesCopied, LE_PACK_STRING);
929 bool packResult = le_pack_PackUint32(bufferPtr, bytesCopied);
936 *bufferPtr = *bufferPtr + bytesCopied;
954 const char *stringPtr,
955 uint32_t maxStringCount,
966 #if defined(__KLOCWORK__) 971 maxStringCount = strnlen(stringPtr, maxStringCount);
976 for (bytesCopied = 0;
977 (bytesCopied < maxStringCount) && (stringPtr[bytesCopied] !=
'\0');
980 (*bufferPtr)[bytesCopied +
sizeof(uint32_t) +
sizeof(TagID_t)] = stringPtr[bytesCopied];
984 if (stringPtr[bytesCopied] !=
'\0')
991 bool packResult = le_pack_PackTaggedUint32(bufferPtr, bytesCopied, tagId);
997 *bufferPtr = *bufferPtr + bytesCopied;
1013 uint8_t **bufferPtr,
1014 const void *arrayPtr,
1017 size_t arrayMaxCount
1023 if (arrayCount > arrayMaxCount)
1028 #ifdef LE_CONFIG_RPC 1029 LE_ASSERT(le_pack_PackTaggedSize(bufferPtr, arrayCount, LE_PACK_ARRAYHEADER));
1031 LE_ASSERT(le_pack_PackSize(bufferPtr, arrayCount));
1036 #ifdef LE_CONFIG_RPC 1048 uint8_t **bufferPtr,
1049 const void *arrayPtr,
1052 size_t arrayMaxCount,
1059 if (arrayCount > arrayMaxCount)
1064 LE_ASSERT(le_pack_PackTaggedSize(bufferPtr, arrayCount, tagId));
1076 #define LE_PACK_PACKARRAY(bufferPtr, \ 1083 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), \ 1084 (arrayPtr), sizeof((arrayPtr)[0]), \ 1085 (arrayCount), (arrayMaxCount)); \ 1089 for (i = 0; i < (arrayCount); ++i) \ 1091 LE_ASSERT(packFunc((bufferPtr), (arrayPtr)[i])); \ 1093 *(resultPtr) = true; \ 1104 #define LE_PACK_PACKSTRUCTARRAY(bufferPtr, \ 1111 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), \ 1112 (arrayPtr), sizeof((arrayPtr)[0]), \ 1113 (arrayCount), (arrayMaxCount)); \ 1117 for (i = 0; i < (arrayCount); ++i) \ 1119 LE_ASSERT(packFunc((bufferPtr), &((arrayPtr)[i]))); \ 1121 *(resultPtr) = true; \ 1129 #define LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr) \ 1131 memcpy((valuePtr), *bufferPtr, sizeof(*(valuePtr))); \ 1132 *bufferPtr = (*bufferPtr) + sizeof(*(valuePtr)); \ 1137 #ifdef LE_CONFIG_RPC 1149 uint8_t** bufferPtr,
1153 LE_PACK_UNPACK_SIMPLE_VALUE(tagIdPtr);
1165 uint8_t** bufferPtr,
1169 #ifdef LE_CONFIG_RPC 1171 le_pack_UnpackTagID(bufferPtr, &tag);
1173 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1184 uint8_t** bufferPtr,
1188 #ifdef LE_CONFIG_RPC 1190 le_pack_UnpackTagID(bufferPtr, &tag);
1191 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1192 *valuePtr = be16toh(*valuePtr);
1194 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1206 uint8_t** bufferPtr,
1210 #ifdef LE_CONFIG_RPC 1212 le_pack_UnpackTagID(bufferPtr, &tag);
1213 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1214 *valuePtr = be32toh(*valuePtr);
1216 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1228 uint8_t** bufferPtr,
1232 #ifdef LE_CONFIG_RPC 1234 le_pack_UnpackTagID(bufferPtr, &tag);
1235 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1236 *valuePtr = be64toh(*valuePtr);
1238 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1250 uint8_t** bufferPtr,
1254 #ifdef LE_CONFIG_RPC 1256 le_pack_UnpackTagID(bufferPtr, &tag);
1258 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1269 uint8_t** bufferPtr,
1273 #ifdef LE_CONFIG_RPC 1275 le_pack_UnpackTagID(bufferPtr, &tag);
1276 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1277 *valuePtr = be16toh(*valuePtr);
1279 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1291 uint8_t** bufferPtr,
1295 #ifdef LE_CONFIG_RPC 1297 le_pack_UnpackTagID(bufferPtr, &tag);
1298 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1299 *valuePtr = be32toh(*valuePtr);
1301 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1313 uint8_t** bufferPtr,
1317 #ifdef LE_CONFIG_RPC 1319 le_pack_UnpackTagID(bufferPtr, &tag);
1320 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1321 *valuePtr = be64toh(*valuePtr);
1323 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1328 #ifdef LE_CONFIG_RPC 1337 uint8_t** bufferPtr,
1345 le_pack_UnpackTagID(bufferPtr, &tag);
1346 LE_PACK_UNPACK_SIMPLE_VALUE(&rawSize);
1347 *sizePtr = be32toh(rawSize);
1349 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1350 *valuePtr = be32toh(*valuePtr);
1363 uint8_t** bufferPtr,
1371 le_pack_UnpackTagID(bufferPtr, &tag);
1372 LE_PACK_UNPACK_SIMPLE_VALUE(&rawSize);
1373 *sizePtr = be32toh(rawSize);
1375 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1376 *valuePtr = be64toh(*valuePtr);
1391 uint8_t **bufferPtr,
1397 if (!le_pack_UnpackUint32(bufferPtr, &rawValue))
1402 *valuePtr = rawValue;
1414 uint8_t** bufferPtr,
1420 uint8_t simpleValue;
1422 #ifdef LE_CONFIG_RPC 1424 le_pack_UnpackTagID(bufferPtr, &tag);
1426 memcpy(&simpleValue, *bufferPtr,
sizeof(simpleValue));
1428 *bufferPtr = ((uint8_t* )*bufferPtr) +
sizeof(simpleValue);
1431 *valuePtr = !!simpleValue;
1443 uint8_t** bufferPtr,
1447 #ifdef LE_CONFIG_RPC 1449 le_pack_UnpackTagID(bufferPtr, &tag);
1451 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1462 uint8_t** bufferPtr,
1466 #ifdef LE_CONFIG_RPC 1468 le_pack_UnpackTagID(bufferPtr, &tag);
1469 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1470 *valuePtr = be64toh(*valuePtr);
1472 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1484 uint8_t** bufferPtr,
1488 #ifdef LE_CONFIG_RPC 1491 le_pack_UnpackTagID(bufferPtr, &tag);
1492 LE_PACK_UNPACK_SIMPLE_VALUE(&value);
1495 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1507 uint8_t** bufferPtr,
1511 #ifdef LE_CONFIG_RPC 1514 le_pack_UnpackTagID(bufferPtr, &tag);
1515 LE_PACK_UNPACK_SIMPLE_VALUE(&value);
1518 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1523 #undef LE_PACK_UNPACK_SIMPLE_VALUE 1532 uint8_t** bufferPtr,
1539 if (!le_pack_UnpackUint32(bufferPtr, &refAsInt))
1546 if ((refAsInt & 0x01) ||
1550 *(
void **)refPtr = (
void *)(size_t)refAsInt;
1566 uint8_t** bufferPtr,
1568 uint32_t bufferSize,
1569 uint32_t maxStringCount
1572 uint32_t stringSize;
1575 if (!le_pack_UnpackUint32(bufferPtr, &stringSize))
1580 if ((stringSize > maxStringCount) ||
1581 (stringSize > bufferSize))
1600 memcpy(stringPtr, *bufferPtr, stringSize);
1601 stringPtr[stringSize] =
'\0';
1603 *bufferPtr = *bufferPtr + stringSize;
1618 uint8_t **bufferPtr,
1619 const void *arrayPtr,
1621 size_t *arrayCountPtr,
1622 size_t arrayMaxCount
1627 LE_ASSERT(le_pack_UnpackSize(bufferPtr, arrayCountPtr));
1628 if (*arrayCountPtr > arrayMaxCount)
1635 return (*arrayCountPtr == 0);
1650 #define LE_PACK_UNPACKARRAY(bufferPtr, \ 1657 if (!le_pack_UnpackArrayHeader((bufferPtr), \ 1658 (arrayPtr), sizeof((arrayPtr)[0]), \ 1659 (arrayCountPtr), (arrayMaxCount))) \ 1661 *(resultPtr) = false; \ 1666 for (i = 0; i < *(arrayCountPtr); ++i) \ 1668 LE_ASSERT(unpackFunc((bufferPtr), &(arrayPtr)[i])); \ 1670 *(resultPtr) = true; \ 1681 #define LE_PACK_UNPACKSTRUCTARRAY(bufferPtr, \ 1687 LE_PACK_UNPACKARRAY((bufferPtr), (arrayPtr), (arrayCountPtr), \ 1688 (arrayMaxCount), (unpackFunc), (resultPtr)) le_result_t
Definition: le_basics.h:35
#define LE_UNUSED(v)
Definition: le_basics.h:355
#define LE_ASSERT(condition)
Definition: le_log.h:916
#define LE_DECLARE_INLINE
Definition: le_basics.h:306
le_onoff_t
Definition: le_basics.h:71