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 56 #define LE_PACK_CONTEXT_PTR_REFERENCE 24 57 #define LE_PACK_ASYNC_HANDLER_REFERENCE 25 58 #define LE_PACK_FILESTREAM_ID 26 59 #define LE_PACK_FILESTREAM_FLAG 27 61 #define LE_PACK_SIZEOF_TAG_ID sizeof(TagID_t) 62 #define LE_PACK_SIZEOF_BOOL sizeof(bool) 63 #define LE_PACK_SIZEOF_CHAR sizeof(char) 64 #define LE_PACK_SIZEOF_UINT8 sizeof(uint8_t) 65 #define LE_PACK_SIZEOF_UINT16 sizeof(uint16_t) 66 #define LE_PACK_SIZEOF_UINT32 sizeof(uint32_t) 67 #define LE_PACK_SIZEOF_UINT64 sizeof(uint64_t) 68 #define LE_PACK_SIZEOF_INT8 sizeof(int8_t) 69 #define LE_PACK_SIZEOF_INT16 sizeof(int16_t) 70 #define LE_PACK_SIZEOF_INT32 sizeof(int32_t) 71 #define LE_PACK_SIZEOF_INT64 sizeof(int64_t) 72 #define LE_PACK_SIZEOF_DOUBLE sizeof(double) 73 #define LE_PACK_SIZEOF_RESULT LE_PACK_SIZEOF_UINT32 74 #define LE_PACK_SIZEOF_ONOFF LE_PACK_SIZEOF_UINT32 75 #define LE_PACK_SIZEOF_SIZE LE_PACK_SIZEOF_UINT32 76 #define LE_PACK_SIZEOF_REFERENCE LE_PACK_SIZEOF_UINT32 77 #define LE_PACK_SIZEOF_FILESTREAM_ID LE_PACK_SIZEOF_UINT16 78 #define LE_PACK_SIZEOF_FILESTREAM_FLAG LE_PACK_SIZEOF_UINT16 87 #define LE_PACK_PACK_SIMPLE_VALUE(value) \ 89 memcpy(*bufferPtr, &(value), sizeof(value)); \ 90 *bufferPtr = *bufferPtr + sizeof(value); \ 110 LE_PACK_PACK_SIMPLE_VALUE(value);
130 le_pack_PackTagID(bufferPtr, LE_PACK_UINT8);
132 LE_PACK_PACK_SIMPLE_VALUE(value);
153 le_pack_PackTagID(bufferPtr, tagId);
154 LE_PACK_PACK_SIMPLE_VALUE(value);
171 uint16_t newValue = htobe16(value);
172 le_pack_PackTagID(bufferPtr, LE_PACK_UINT16);
173 LE_PACK_PACK_SIMPLE_VALUE(newValue);
175 LE_PACK_PACK_SIMPLE_VALUE(value);
198 uint16_t newValue = htobe16(value);
199 le_pack_PackTagID(bufferPtr, tagId);
200 LE_PACK_PACK_SIMPLE_VALUE(newValue);
217 uint32_t newValue = htobe32(value);
218 le_pack_PackTagID(bufferPtr, LE_PACK_UINT32);
219 LE_PACK_PACK_SIMPLE_VALUE(newValue);
221 LE_PACK_PACK_SIMPLE_VALUE(value);
244 uint32_t newValue = htobe32(value);
245 le_pack_PackTagID(bufferPtr, tagId);
246 LE_PACK_PACK_SIMPLE_VALUE(newValue);
263 uint64_t newValue = htobe64(value);
264 le_pack_PackTagID(bufferPtr, LE_PACK_UINT64);
265 LE_PACK_PACK_SIMPLE_VALUE(newValue);
267 LE_PACK_PACK_SIMPLE_VALUE(value);
289 uint64_t newValue = htobe64(value);
290 le_pack_PackTagID(bufferPtr, tagId);
291 LE_PACK_PACK_SIMPLE_VALUE(newValue);
308 le_pack_PackTagID(bufferPtr, LE_PACK_INT8);
310 LE_PACK_PACK_SIMPLE_VALUE(value);
331 le_pack_PackTagID(bufferPtr, tagId);
332 LE_PACK_PACK_SIMPLE_VALUE(value);
349 int16_t newValue = htobe16(value);
350 le_pack_PackTagID(bufferPtr, LE_PACK_INT16);
351 LE_PACK_PACK_SIMPLE_VALUE(newValue);
353 LE_PACK_PACK_SIMPLE_VALUE(value);
375 int16_t newValue = htobe16(value);
376 le_pack_PackTagID(bufferPtr, tagId);
377 LE_PACK_PACK_SIMPLE_VALUE(newValue);
394 int32_t newValue = htobe32(value);
395 le_pack_PackTagID(bufferPtr, LE_PACK_INT32);
396 LE_PACK_PACK_SIMPLE_VALUE(newValue);
398 LE_PACK_PACK_SIMPLE_VALUE(value);
420 int32_t newValue = htobe32(value);
421 le_pack_PackTagID(bufferPtr, tagId);
422 LE_PACK_PACK_SIMPLE_VALUE(newValue);
439 int64_t newValue = htobe64(value);
440 le_pack_PackTagID(bufferPtr, LE_PACK_INT64);
441 LE_PACK_PACK_SIMPLE_VALUE(newValue);
443 LE_PACK_PACK_SIMPLE_VALUE(value);
465 int64_t newValue = htobe64(value);
466 le_pack_PackTagID(bufferPtr, tagId);
467 LE_PACK_PACK_SIMPLE_VALUE(newValue);
491 if (size > UINT32_MAX)
496 le_pack_PackTagID(bufferPtr, tagId);
498 uint32_t newSize = htobe32(size);
499 LE_PACK_PACK_SIMPLE_VALUE(newSize);
501 uint32_t newValue = htobe32(value);
502 LE_PACK_PACK_SIMPLE_VALUE(newValue);
525 if (size > UINT32_MAX)
530 le_pack_PackTagID(bufferPtr, tagId);
532 uint32_t newSize = htobe32(size);
533 LE_PACK_PACK_SIMPLE_VALUE(newSize);
535 uint64_t newValue = htobe64(value);
536 LE_PACK_PACK_SIMPLE_VALUE(newValue);
554 if (value > UINT32_MAX)
560 return le_pack_PackTaggedUint32(bufferPtr, value, LE_PACK_SIZE);
562 return le_pack_PackUint32(bufferPtr, value);
582 if (value > UINT32_MAX)
587 return le_pack_PackTaggedUint32(bufferPtr, value, tagId);
603 le_pack_PackTagID(bufferPtr, LE_PACK_BOOL);
608 uint8_t simpleValue = ((value)?1:0);
609 LE_PACK_PACK_SIMPLE_VALUE(simpleValue);
630 le_pack_PackTagID(bufferPtr, tagId);
634 uint8_t simpleValue = ((value)?1:0);
635 LE_PACK_PACK_SIMPLE_VALUE(simpleValue);
652 le_pack_PackTagID(bufferPtr, LE_PACK_CHAR);
654 LE_PACK_PACK_SIMPLE_VALUE(value);
675 le_pack_PackTagID(bufferPtr, tagId);
676 LE_PACK_PACK_SIMPLE_VALUE(value);
693 double newValue = htobe64(value);
694 le_pack_PackTagID(bufferPtr, LE_PACK_DOUBLE);
695 LE_PACK_PACK_SIMPLE_VALUE(newValue);
697 LE_PACK_PACK_SIMPLE_VALUE(value);
719 double newValue = htobe64(value);
720 le_pack_PackTagID(bufferPtr, tagId);
721 LE_PACK_PACK_SIMPLE_VALUE(newValue);
738 int32_t newValue = htobe32((int32_t) value);
739 le_pack_PackTagID(bufferPtr, LE_PACK_RESULT);
740 LE_PACK_PACK_SIMPLE_VALUE(newValue);
742 LE_PACK_PACK_SIMPLE_VALUE(value);
764 int32_t newValue = htobe32((int32_t) value);
765 le_pack_PackTagID(bufferPtr, tagId);
766 LE_PACK_PACK_SIMPLE_VALUE(newValue);
783 int32_t newValue = htobe32((int32_t) value);
784 le_pack_PackTagID(bufferPtr, LE_PACK_ONOFF);
785 LE_PACK_PACK_SIMPLE_VALUE(newValue);
787 LE_PACK_PACK_SIMPLE_VALUE(value);
809 int32_t newValue = htobe32((int32_t) value);
810 le_pack_PackTagID(bufferPtr, tagId);
811 LE_PACK_PACK_SIMPLE_VALUE(newValue);
816 #undef LE_PACK_PACK_SIMPLE_VALUE 829 size_t refAsInt = (size_t)ref;
834 if ((refAsInt <= UINT32_MAX) &&
835 ((refAsInt & 0x01) ||
839 return le_pack_PackTaggedUint32(bufferPtr, (uint32_t)refAsInt, LE_PACK_REFERENCE);
841 return le_pack_PackUint32(bufferPtr, (uint32_t)refAsInt);
864 size_t refAsInt = (size_t)ref;
869 if ((refAsInt <= UINT32_MAX) &&
870 ((refAsInt & 0x01) ||
873 return le_pack_PackTaggedUint32(bufferPtr, (uint32_t)refAsInt, tagId);
892 const char *stringPtr,
893 uint32_t maxStringCount
903 #if defined(__KLOCWORK__) 908 maxStringCount = strnlen(stringPtr, maxStringCount);
913 for (bytesCopied = 0;
914 (bytesCopied < maxStringCount) && (stringPtr[bytesCopied] !=
'\0');
918 (*bufferPtr)[bytesCopied +
sizeof(uint32_t) +
sizeof(TagID_t)] = stringPtr[bytesCopied];
920 (*bufferPtr)[bytesCopied +
sizeof(uint32_t)] = stringPtr[bytesCopied];
925 if (stringPtr[bytesCopied] !=
'\0')
933 bool packResult = le_pack_PackTaggedUint32(bufferPtr, bytesCopied, LE_PACK_STRING);
935 bool packResult = le_pack_PackUint32(bufferPtr, bytesCopied);
942 *bufferPtr = *bufferPtr + bytesCopied;
960 const char *stringPtr,
961 uint32_t maxStringCount,
972 #if defined(__KLOCWORK__) 977 maxStringCount = strnlen(stringPtr, maxStringCount);
982 for (bytesCopied = 0;
983 (bytesCopied < maxStringCount) && (stringPtr[bytesCopied] !=
'\0');
986 (*bufferPtr)[bytesCopied +
sizeof(uint32_t) +
sizeof(TagID_t)] = stringPtr[bytesCopied];
990 if (stringPtr[bytesCopied] !=
'\0')
997 bool packResult = le_pack_PackTaggedUint32(bufferPtr, bytesCopied, tagId);
1003 *bufferPtr = *bufferPtr + bytesCopied;
1019 uint8_t **bufferPtr,
1020 const void *arrayPtr,
1023 size_t arrayMaxCount
1029 if (arrayCount > arrayMaxCount)
1034 #ifdef LE_CONFIG_RPC 1035 LE_ASSERT(le_pack_PackTaggedSize(bufferPtr, arrayCount, LE_PACK_ARRAYHEADER));
1037 LE_ASSERT(le_pack_PackSize(bufferPtr, arrayCount));
1042 #ifdef LE_CONFIG_RPC 1054 uint8_t **bufferPtr,
1055 const void *arrayPtr,
1058 size_t arrayMaxCount,
1065 if (arrayCount > arrayMaxCount)
1070 LE_ASSERT(le_pack_PackTaggedSize(bufferPtr, arrayCount, tagId));
1082 #define LE_PACK_PACKARRAY(bufferPtr, \ 1089 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), \ 1090 (arrayPtr), sizeof((arrayPtr)[0]), \ 1091 (arrayCount), (arrayMaxCount)); \ 1095 for (i = 0; i < (arrayCount); ++i) \ 1097 LE_ASSERT(packFunc((bufferPtr), (arrayPtr)[i])); \ 1099 *(resultPtr) = true; \ 1110 #define LE_PACK_PACKSTRUCTARRAY(bufferPtr, \ 1117 *(resultPtr) = le_pack_PackArrayHeader((bufferPtr), \ 1118 (arrayPtr), sizeof((arrayPtr)[0]), \ 1119 (arrayCount), (arrayMaxCount)); \ 1123 for (i = 0; i < (arrayCount); ++i) \ 1125 LE_ASSERT(packFunc((bufferPtr), &((arrayPtr)[i]))); \ 1127 *(resultPtr) = true; \ 1135 #define LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr) \ 1137 memcpy((valuePtr), *bufferPtr, sizeof(*(valuePtr))); \ 1138 *bufferPtr = (*bufferPtr) + sizeof(*(valuePtr)); \ 1143 #ifdef LE_CONFIG_RPC 1155 uint8_t** bufferPtr,
1159 LE_PACK_UNPACK_SIMPLE_VALUE(tagIdPtr);
1171 uint8_t** bufferPtr,
1175 #ifdef LE_CONFIG_RPC 1177 le_pack_UnpackTagID(bufferPtr, &tag);
1179 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1190 uint8_t** bufferPtr,
1194 #ifdef LE_CONFIG_RPC 1196 le_pack_UnpackTagID(bufferPtr, &tag);
1197 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1198 *valuePtr = be16toh(*valuePtr);
1200 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1212 uint8_t** bufferPtr,
1216 #ifdef LE_CONFIG_RPC 1218 le_pack_UnpackTagID(bufferPtr, &tag);
1219 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1220 *valuePtr = be32toh(*valuePtr);
1222 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1234 uint8_t** bufferPtr,
1238 #ifdef LE_CONFIG_RPC 1240 le_pack_UnpackTagID(bufferPtr, &tag);
1241 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1242 *valuePtr = be64toh(*valuePtr);
1244 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1256 uint8_t** bufferPtr,
1260 #ifdef LE_CONFIG_RPC 1262 le_pack_UnpackTagID(bufferPtr, &tag);
1264 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1275 uint8_t** bufferPtr,
1279 #ifdef LE_CONFIG_RPC 1281 le_pack_UnpackTagID(bufferPtr, &tag);
1282 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1283 *valuePtr = be16toh(*valuePtr);
1285 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1297 uint8_t** bufferPtr,
1301 #ifdef LE_CONFIG_RPC 1303 le_pack_UnpackTagID(bufferPtr, &tag);
1304 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1305 *valuePtr = be32toh(*valuePtr);
1307 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1319 uint8_t** bufferPtr,
1323 #ifdef LE_CONFIG_RPC 1325 le_pack_UnpackTagID(bufferPtr, &tag);
1326 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1327 *valuePtr = be64toh(*valuePtr);
1329 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1334 #ifdef LE_CONFIG_RPC 1343 uint8_t** bufferPtr,
1351 le_pack_UnpackTagID(bufferPtr, &tag);
1352 LE_PACK_UNPACK_SIMPLE_VALUE(&rawSize);
1353 *sizePtr = be32toh(rawSize);
1355 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1356 *valuePtr = be32toh(*valuePtr);
1369 uint8_t** bufferPtr,
1377 le_pack_UnpackTagID(bufferPtr, &tag);
1378 LE_PACK_UNPACK_SIMPLE_VALUE(&rawSize);
1379 *sizePtr = be32toh(rawSize);
1381 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1382 *valuePtr = be64toh(*valuePtr);
1397 uint8_t **bufferPtr,
1403 if (!le_pack_UnpackUint32(bufferPtr, &rawValue))
1408 *valuePtr = rawValue;
1420 uint8_t** bufferPtr,
1426 uint8_t simpleValue;
1428 #ifdef LE_CONFIG_RPC 1430 le_pack_UnpackTagID(bufferPtr, &tag);
1432 memcpy(&simpleValue, *bufferPtr,
sizeof(simpleValue));
1434 *bufferPtr = ((uint8_t* )*bufferPtr) +
sizeof(simpleValue);
1437 *valuePtr = !!simpleValue;
1449 uint8_t** bufferPtr,
1453 #ifdef LE_CONFIG_RPC 1455 le_pack_UnpackTagID(bufferPtr, &tag);
1457 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1468 uint8_t** bufferPtr,
1472 #ifdef LE_CONFIG_RPC 1474 le_pack_UnpackTagID(bufferPtr, &tag);
1475 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1476 *valuePtr = be64toh(*valuePtr);
1478 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1490 uint8_t** bufferPtr,
1494 #ifdef LE_CONFIG_RPC 1497 le_pack_UnpackTagID(bufferPtr, &tag);
1498 LE_PACK_UNPACK_SIMPLE_VALUE(&value);
1501 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1513 uint8_t** bufferPtr,
1517 #ifdef LE_CONFIG_RPC 1520 le_pack_UnpackTagID(bufferPtr, &tag);
1521 LE_PACK_UNPACK_SIMPLE_VALUE(&value);
1524 LE_PACK_UNPACK_SIMPLE_VALUE(valuePtr);
1529 #undef LE_PACK_UNPACK_SIMPLE_VALUE 1538 uint8_t** bufferPtr,
1545 if (!le_pack_UnpackUint32(bufferPtr, &refAsInt))
1552 if ((refAsInt & 0x01) ||
1556 *(
void **)refPtr = (
void *)(size_t)refAsInt;
1572 uint8_t** bufferPtr,
1574 uint32_t bufferSize,
1575 uint32_t maxStringCount
1578 uint32_t stringSize;
1581 if (!le_pack_UnpackUint32(bufferPtr, &stringSize))
1586 if ((stringSize > maxStringCount) ||
1587 (stringSize > bufferSize))
1606 memcpy(stringPtr, *bufferPtr, stringSize);
1607 stringPtr[stringSize] =
'\0';
1609 *bufferPtr = *bufferPtr + stringSize;
1624 uint8_t **bufferPtr,
1625 const void *arrayPtr,
1627 size_t *arrayCountPtr,
1628 size_t arrayMaxCount
1633 LE_ASSERT(le_pack_UnpackSize(bufferPtr, arrayCountPtr));
1634 if (*arrayCountPtr > arrayMaxCount)
1641 return (*arrayCountPtr == 0);
1656 #define LE_PACK_UNPACKARRAY(bufferPtr, \ 1663 if (!le_pack_UnpackArrayHeader((bufferPtr), \ 1664 (arrayPtr), sizeof((arrayPtr)[0]), \ 1665 (arrayCountPtr), (arrayMaxCount))) \ 1667 *(resultPtr) = false; \ 1672 for (i = 0; i < *(arrayCountPtr); ++i) \ 1674 LE_ASSERT(unpackFunc((bufferPtr), &(arrayPtr)[i])); \ 1676 *(resultPtr) = true; \ 1687 #define LE_PACK_UNPACKSTRUCTARRAY(bufferPtr, \ 1693 LE_PACK_UNPACKARRAY((bufferPtr), (arrayPtr), (arrayCountPtr), \ 1694 (arrayMaxCount), (unpackFunc), (resultPtr)) le_result_t
Definition: le_basics.h:46
#define LE_UNUSED(v)
Definition: le_basics.h:382
#define LE_ASSERT(condition)
Definition: le_log.h:991
#define LE_DECLARE_INLINE
Definition: le_basics.h:333
le_onoff_t
Definition: le_basics.h:98