21 #ifndef LE_PACK_H_INCLUDE_GUARD 22 #define LE_PACK_H_INCLUDE_GUARD 30 typedef uint8_t TagID_t;
32 #define LE_PACK_UINT8 1 33 #define LE_PACK_UINT16 2 34 #define LE_PACK_UINT32 3 35 #define LE_PACK_UINT64 4 36 #define LE_PACK_INT8 5 37 #define LE_PACK_INT16 6 38 #define LE_PACK_INT32 7 39 #define LE_PACK_INT64 8 40 #define LE_PACK_SIZE 9 41 #define LE_PACK_BOOL 10 42 #define LE_PACK_CHAR 11 43 #define LE_PACK_DOUBLE 12 44 #define LE_PACK_RESULT 13 45 #define LE_PACK_ONOFF 14 46 #define LE_PACK_REFERENCE 15 47 #define LE_PACK_STRING 16 48 #define LE_PACK_ARRAYHEADER 17 49 #define LE_PACK_IN_STRING_POINTER 18 50 #define LE_PACK_OUT_STRING_POINTER 19 51 #define LE_PACK_IN_ARRAY_POINTER 20 52 #define LE_PACK_OUT_ARRAY_POINTER 21 54 #define LE_PACK_SIZEOF_TAG_ID sizeof(TagID_t) 55 #define LE_PACK_SIZEOF_BOOL sizeof(bool) 56 #define LE_PACK_SIZEOF_CHAR sizeof(char) 57 #define LE_PACK_SIZEOF_UINT8 sizeof(uint8_t) 58 #define LE_PACK_SIZEOF_UINT16 sizeof(uint16_t) 59 #define LE_PACK_SIZEOF_UINT32 sizeof(uint32_t) 60 #define LE_PACK_SIZEOF_UINT64 sizeof(uint64_t) 61 #define LE_PACK_SIZEOF_INT8 sizeof(int8_t) 62 #define LE_PACK_SIZEOF_INT16 sizeof(int16_t) 63 #define LE_PACK_SIZEOF_INT32 sizeof(int32_t) 64 #define LE_PACK_SIZEOF_INT64 sizeof(int64_t) 65 #define LE_PACK_SIZEOF_DOUBLE sizeof(double) 66 #define LE_PACK_SIZEOF_RESULT LE_PACK_SIZEOF_UINT32 67 #define LE_PACK_SIZEOF_ONOFF LE_PACK_SIZEOF_UINT32 68 #define LE_PACK_SIZEOF_SIZE LE_PACK_SIZEOF_UINT32 69 #define LE_PACK_SIZEOF_REFERENCE LE_PACK_SIZEOF_UINT32 78 #define LE_PACK_PACK_SIMPLE_VALUE(value) \ 80 memcpy(*bufferPtr, &(value), sizeof(value)); \ 81 *bufferPtr = *bufferPtr + sizeof(value); \ 101 LE_PACK_PACK_SIMPLE_VALUE(value);
121 le_pack_PackTagID(bufferPtr, LE_PACK_UINT8);
123 LE_PACK_PACK_SIMPLE_VALUE(value);
144 le_pack_PackTagID(bufferPtr, tagId);
145 LE_PACK_PACK_SIMPLE_VALUE(value);
162 uint16_t newValue = htobe16(value);
163 le_pack_PackTagID(bufferPtr, LE_PACK_UINT16);
164 LE_PACK_PACK_SIMPLE_VALUE(newValue);
166 LE_PACK_PACK_SIMPLE_VALUE(value);
189 uint16_t newValue = htobe16(value);
190 le_pack_PackTagID(bufferPtr, tagId);
191 LE_PACK_PACK_SIMPLE_VALUE(newValue);
208 uint32_t newValue = htobe32(value);
209 le_pack_PackTagID(bufferPtr, LE_PACK_UINT32);
210 LE_PACK_PACK_SIMPLE_VALUE(newValue);
212 LE_PACK_PACK_SIMPLE_VALUE(value);
235 uint32_t newValue = htobe32(value);
236 le_pack_PackTagID(bufferPtr, tagId);
237 LE_PACK_PACK_SIMPLE_VALUE(newValue);
254 uint64_t newValue = htobe64(value);
255 le_pack_PackTagID(bufferPtr, LE_PACK_UINT64);
256 LE_PACK_PACK_SIMPLE_VALUE(newValue);
258 LE_PACK_PACK_SIMPLE_VALUE(value);
280 uint64_t newValue = htobe64(value);
281 le_pack_PackTagID(bufferPtr, tagId);
282 LE_PACK_PACK_SIMPLE_VALUE(newValue);
299 le_pack_PackTagID(bufferPtr, LE_PACK_INT8);
301 LE_PACK_PACK_SIMPLE_VALUE(value);
322 le_pack_PackTagID(bufferPtr, tagId);
323 LE_PACK_PACK_SIMPLE_VALUE(value);
340 int16_t newValue = htobe16(value);
341 le_pack_PackTagID(bufferPtr, LE_PACK_INT16);
342 LE_PACK_PACK_SIMPLE_VALUE(newValue);
344 LE_PACK_PACK_SIMPLE_VALUE(value);
366 int16_t newValue = htobe16(value);
367 le_pack_PackTagID(bufferPtr, tagId);
368 LE_PACK_PACK_SIMPLE_VALUE(newValue);
385 int32_t newValue = htobe32(value);
386 le_pack_PackTagID(bufferPtr, LE_PACK_INT32);
387 LE_PACK_PACK_SIMPLE_VALUE(newValue);
389 LE_PACK_PACK_SIMPLE_VALUE(value);
411 int32_t newValue = htobe32(value);
412 le_pack_PackTagID(bufferPtr, tagId);
413 LE_PACK_PACK_SIMPLE_VALUE(newValue);
430 int64_t newValue = htobe64(value);
431 le_pack_PackTagID(bufferPtr, LE_PACK_INT64);
432 LE_PACK_PACK_SIMPLE_VALUE(newValue);
434 LE_PACK_PACK_SIMPLE_VALUE(value);
456 int64_t newValue = htobe64(value);
457 le_pack_PackTagID(bufferPtr, tagId);
458 LE_PACK_PACK_SIMPLE_VALUE(newValue);
482 le_pack_PackTagID(bufferPtr, tagId);
483 if (size > UINT32_MAX)
487 uint32_t newSize = htobe32(size);
488 LE_PACK_PACK_SIMPLE_VALUE(newSize);
490 uint32_t newValue = htobe32(value);
491 LE_PACK_PACK_SIMPLE_VALUE(newValue);
514 le_pack_PackTagID(bufferPtr, tagId);
515 if (size > UINT32_MAX)
519 uint32_t newSize = htobe32(size);
520 LE_PACK_PACK_SIMPLE_VALUE(newSize);
522 uint64_t newValue = htobe64(value);
523 LE_PACK_PACK_SIMPLE_VALUE(newValue);
541 if (value > UINT32_MAX)
547 return le_pack_PackTaggedUint32(bufferPtr, value, LE_PACK_SIZE);
549 return le_pack_PackUint32(bufferPtr, value);
569 if (value > UINT32_MAX)
574 return le_pack_PackTaggedUint32(bufferPtr, value, tagId);
590 le_pack_PackTagID(bufferPtr, LE_PACK_BOOL);
595 uint8_t simpleValue = ((value)?1:0);
596 LE_PACK_PACK_SIMPLE_VALUE(simpleValue);
617 le_pack_PackTagID(bufferPtr, tagId);
621 uint8_t simpleValue = ((value)?1:0);
622 LE_PACK_PACK_SIMPLE_VALUE(simpleValue);
639 le_pack_PackTagID(bufferPtr, LE_PACK_CHAR);
641 LE_PACK_PACK_SIMPLE_VALUE(value);
662 le_pack_PackTagID(bufferPtr, tagId);
663 LE_PACK_PACK_SIMPLE_VALUE(value);
680 double newValue = htobe64(value);
681 le_pack_PackTagID(bufferPtr, LE_PACK_DOUBLE);
682 LE_PACK_PACK_SIMPLE_VALUE(newValue);
684 LE_PACK_PACK_SIMPLE_VALUE(value);
706 double newValue = htobe64(value);
707 le_pack_PackTagID(bufferPtr, tagId);
708 LE_PACK_PACK_SIMPLE_VALUE(newValue);
725 int32_t newValue = htobe32((int32_t) value);
726 le_pack_PackTagID(bufferPtr, LE_PACK_RESULT);
727 LE_PACK_PACK_SIMPLE_VALUE(newValue);
729 LE_PACK_PACK_SIMPLE_VALUE(value);
751 int32_t newValue = htobe32((int32_t) value);
752 le_pack_PackTagID(bufferPtr, tagId);
753 LE_PACK_PACK_SIMPLE_VALUE(newValue);
770 int32_t newValue = htobe32((int32_t) value);
771 le_pack_PackTagID(bufferPtr, LE_PACK_ONOFF);
772 LE_PACK_PACK_SIMPLE_VALUE(newValue);
774 LE_PACK_PACK_SIMPLE_VALUE(value);
796 int32_t newValue = htobe32((int32_t) value);
797 le_pack_PackTagID(bufferPtr, tagId);
798 LE_PACK_PACK_SIMPLE_VALUE(newValue);
803 #undef LE_PACK_PACK_SIMPLE_VALUE 816 size_t refAsInt = (size_t)ref;
821 if ((refAsInt <= UINT32_MAX) &&
822 ((refAsInt & 0x01) ||
826 return le_pack_PackTaggedUint32(bufferPtr, (uint32_t)refAsInt, LE_PACK_REFERENCE);
828 return le_pack_PackUint32(bufferPtr, (uint32_t)refAsInt);
851 size_t refAsInt = (size_t)ref;
856 if ((refAsInt <= UINT32_MAX) &&
857 ((refAsInt & 0x01) ||
860 return le_pack_PackTaggedUint32(bufferPtr, (uint32_t)refAsInt, tagId);
879 const char *stringPtr,
880 uint32_t maxStringCount
890 #if defined(__KLOCWORK__) 895 maxStringCount = strnlen(stringPtr, maxStringCount);
900 for (bytesCopied = 0;
901 (bytesCopied < maxStringCount) && (stringPtr[bytesCopied] !=
'\0');
905 (*bufferPtr)[bytesCopied +
sizeof(uint32_t) +
sizeof(TagID_t)] = stringPtr[bytesCopied];
907 (*bufferPtr)[bytesCopied +
sizeof(uint32_t)] = stringPtr[bytesCopied];
912 if (stringPtr[bytesCopied] !=
'\0')
920 bool packResult = le_pack_PackTaggedUint32(bufferPtr, bytesCopied, LE_PACK_STRING);
922 bool packResult = le_pack_PackUint32(bufferPtr, bytesCopied);
929 *bufferPtr = *bufferPtr + bytesCopied;
947 const char *stringPtr,
948 uint32_t maxStringCount,
959 #if defined(__KLOCWORK__) 964 maxStringCount = strnlen(stringPtr, maxStringCount);
969 for (bytesCopied = 0;
970 (bytesCopied < maxStringCount) && (stringPtr[bytesCopied] !=
'\0');
973 (*bufferPtr)[bytesCopied +
sizeof(uint32_t) +
sizeof(TagID_t)] = stringPtr[bytesCopied];
977 if (stringPtr[bytesCopied] !=
'\0')
984 bool packResult = le_pack_PackTaggedUint32(bufferPtr, bytesCopied, tagId);
990 *bufferPtr = *bufferPtr + bytesCopied;
1006 uint8_t **bufferPtr,
1007 const void *arrayPtr,
1010 size_t arrayMaxCount
1013 if (arrayCount > arrayMaxCount)
1018 #ifdef LE_CONFIG_RPC 1019 LE_ASSERT(le_pack_PackTaggedSize(bufferPtr, arrayCount, LE_PACK_ARRAYHEADER));
1021 LE_ASSERT(le_pack_PackSize(bufferPtr, arrayCount));
1026 #ifdef LE_CONFIG_RPC 1038 uint8_t **bufferPtr,
1039 const void *arrayPtr,
1042 size_t arrayMaxCount,
1046 if (arrayCount > arrayMaxCount)
1051 LE_ASSERT(le_pack_PackTaggedSize(bufferPtr, arrayCount, tagId));
1063 #define LE_PACK_PACKARRAY(bufferPtr, \ 1070 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), \ 1071 (arrayPtr), sizeof((arrayPtr)[0]), \ 1072 (arrayCount), (arrayMaxCount)); \ 1076 for (i = 0; i < (arrayCount); ++i) \ 1078 LE_ASSERT(packFunc((bufferPtr), (arrayPtr)[i])); \ 1080 *(resultPtr) = true; \ 1091 #define LE_PACK_PACKSTRUCTARRAY(bufferPtr, \ 1098 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), \ 1099 (arrayPtr), sizeof((arrayPtr)[0]), \ 1100 (arrayCount), (arrayMaxCount)); \ 1104 for (i = 0; i < (arrayCount); ++i) \ 1106 LE_ASSERT(packFunc((bufferPtr), &((arrayPtr)[i]))); \ 1108 *(resultPtr) = true; \ 1116 #define LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr) \ 1118 memcpy((valuePtr), *bufferPtr, sizeof(*(valuePtr))); \ 1119 *bufferPtr = (*bufferPtr) + sizeof(*(valuePtr)); \ 1124 #ifdef LE_CONFIG_RPC 1136 uint8_t** bufferPtr,
1140 LE_PACK_UNPACK_SIMPLE_VALUE(tagIdPtr);
1152 uint8_t** bufferPtr,
1156 #ifdef LE_CONFIG_RPC 1158 le_pack_UnpackTagID(bufferPtr, &tag);
1160 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1171 uint8_t** bufferPtr,
1175 #ifdef LE_CONFIG_RPC 1177 le_pack_UnpackTagID(bufferPtr, &tag);
1178 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1179 *valuePtr = be16toh(*valuePtr);
1181 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1193 uint8_t** bufferPtr,
1197 #ifdef LE_CONFIG_RPC 1199 le_pack_UnpackTagID(bufferPtr, &tag);
1200 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1201 *valuePtr = be32toh(*valuePtr);
1203 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1215 uint8_t** bufferPtr,
1219 #ifdef LE_CONFIG_RPC 1221 le_pack_UnpackTagID(bufferPtr, &tag);
1222 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1223 *valuePtr = be64toh(*valuePtr);
1225 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1237 uint8_t** bufferPtr,
1241 #ifdef LE_CONFIG_RPC 1243 le_pack_UnpackTagID(bufferPtr, &tag);
1245 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1256 uint8_t** bufferPtr,
1260 #ifdef LE_CONFIG_RPC 1262 le_pack_UnpackTagID(bufferPtr, &tag);
1263 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1264 *valuePtr = be16toh(*valuePtr);
1266 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1278 uint8_t** bufferPtr,
1282 #ifdef LE_CONFIG_RPC 1284 le_pack_UnpackTagID(bufferPtr, &tag);
1285 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1286 *valuePtr = be32toh(*valuePtr);
1288 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1300 uint8_t** bufferPtr,
1304 #ifdef LE_CONFIG_RPC 1306 le_pack_UnpackTagID(bufferPtr, &tag);
1307 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1308 *valuePtr = be64toh(*valuePtr);
1310 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1315 #ifdef LE_CONFIG_RPC 1324 uint8_t** bufferPtr,
1332 le_pack_UnpackTagID(bufferPtr, &tag);
1333 LE_PACK_UNPACK_SIMPLE_VALUE(&rawSize);
1334 *sizePtr = be32toh(rawSize);
1336 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1337 *valuePtr = be32toh(*valuePtr);
1350 uint8_t** bufferPtr,
1358 le_pack_UnpackTagID(bufferPtr, &tag);
1359 LE_PACK_UNPACK_SIMPLE_VALUE(&rawSize);
1360 *sizePtr = be32toh(rawSize);
1362 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1363 *valuePtr = be64toh(*valuePtr);
1378 uint8_t **bufferPtr,
1384 if (!le_pack_UnpackUint32(bufferPtr, &rawValue))
1389 *valuePtr = rawValue;
1401 uint8_t** bufferPtr,
1407 uint8_t simpleValue;
1409 #ifdef LE_CONFIG_RPC 1411 le_pack_UnpackTagID(bufferPtr, &tag);
1413 memcpy(&simpleValue, *bufferPtr,
sizeof(simpleValue));
1415 *bufferPtr = ((uint8_t* )*bufferPtr) +
sizeof(simpleValue);
1418 *valuePtr = !!simpleValue;
1430 uint8_t** bufferPtr,
1434 #ifdef LE_CONFIG_RPC 1436 le_pack_UnpackTagID(bufferPtr, &tag);
1438 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1449 uint8_t** bufferPtr,
1453 #ifdef LE_CONFIG_RPC 1455 le_pack_UnpackTagID(bufferPtr, &tag);
1456 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1457 *valuePtr = be64toh(*valuePtr);
1459 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1471 uint8_t** bufferPtr,
1475 #ifdef LE_CONFIG_RPC 1478 le_pack_UnpackTagID(bufferPtr, &tag);
1479 LE_PACK_UNPACK_SIMPLE_VALUE(&value);
1482 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1494 uint8_t** bufferPtr,
1498 #ifdef LE_CONFIG_RPC 1501 le_pack_UnpackTagID(bufferPtr, &tag);
1502 LE_PACK_UNPACK_SIMPLE_VALUE(&value);
1505 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1510 #undef LE_PACK_UNPACK_SIMPLE_VALUE 1519 uint8_t** bufferPtr,
1526 if (!le_pack_UnpackUint32(bufferPtr, &refAsInt))
1533 if ((refAsInt & 0x01) ||
1537 *(
void **)refPtr = (
void *)(size_t)refAsInt;
1553 uint8_t** bufferPtr,
1555 uint32_t bufferSize,
1556 uint32_t maxStringCount
1559 uint32_t stringSize;
1562 if (!le_pack_UnpackUint32(bufferPtr, &stringSize))
1567 if ((stringSize > maxStringCount) ||
1568 (stringSize > bufferSize))
1587 memcpy(stringPtr, *bufferPtr, stringSize);
1588 stringPtr[stringSize] =
'\0';
1590 *bufferPtr = *bufferPtr + stringSize;
1605 uint8_t **bufferPtr,
1606 const void *arrayPtr,
1608 size_t *arrayCountPtr,
1609 size_t arrayMaxCount
1612 LE_ASSERT(le_pack_UnpackSize(bufferPtr, arrayCountPtr));
1613 if (*arrayCountPtr > arrayMaxCount)
1620 return (*arrayCountPtr == 0);
1635 #define LE_PACK_UNPACKARRAY(bufferPtr, \ 1642 if (!le_pack_UnpackArrayHeader((bufferPtr), \ 1643 (arrayPtr), sizeof((arrayPtr)[0]), \ 1644 (arrayCountPtr), (arrayMaxCount))) \ 1646 *(resultPtr) = false; \ 1651 for (i = 0; i < *(arrayCountPtr); ++i) \ 1653 LE_ASSERT(unpackFunc((bufferPtr), &(arrayPtr)[i])); \ 1655 *(resultPtr) = true; \ 1666 #define LE_PACK_UNPACKSTRUCTARRAY(bufferPtr, \ 1672 LE_PACK_UNPACKARRAY((bufferPtr), (arrayPtr), (arrayCountPtr), \ 1673 (arrayMaxCount), (unpackFunc), (resultPtr)) le_result_t
Definition: le_basics.h:35
#define LE_ASSERT(condition)
Definition: le_log.h:878
#define LE_DECLARE_INLINE
Definition: le_basics.h:274
le_onoff_t
Definition: le_basics.h:71