tlvCommon.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <cstdio>
5 
6 namespace Tlv
7 {
8 // Type (T of TLV) is encoded on 4 bytes
9 typedef uint32_t type_t;
10 
11 // Can be used passed as size argument to API function
12 constexpr size_t UNLIMITED = UINT32_MAX;
13 
14 // TLV is padded to 4-octet alignment
15 constexpr size_t alignmentSize = 4;
16 
17 // TLV header (TL part)
18 #pragma pack(push, 1)
19 struct __attribute__((packed)) header_s {
20  type_t type;
21  uint32_t length;
22 };
23 #pragma pack(pop)
24 
25 // TLV stucture (warning: the sizeof will _not_ include value, which is of variable size)
26 #pragma pack(push, 1)
27 struct __attribute__((packed)) tlv_s {
28  private:
29  header_s header;
30  uint8_t value[]; // Number of bytes is specified by header.length
31 
32  public:
33  // Return type of TLV
34  inline type_t getType() const
35  {
36  return header.type;
37  }
38 
39  // Return length of TLV
40  inline size_t getLength() const
41  {
42  return header.length;
43  }
44 
45  // Return buffer of TLV
46  inline const uint8_t *getValue() const
47  {
48  return value;
49  }
50 };
51 #pragma pack(pop)
52 
53 // Returns number of padding bytes to be added depending on non-padded size
54 static inline size_t getPadding(size_t size)
55 {
56  size_t n = size % alignmentSize;
57  return (n == 0) ? 0 : alignmentSize - n;
58 }
59 
60 // Return size of TLV, it differs from length since it includes header size and padding
61 static inline size_t getSize(const tlv_s &tlv)
62 {
63  size_t unpadded_size = sizeof(header_s) + tlv.getLength();
64  return unpadded_size + getPadding(unpadded_size);
65 }
66 
67 } // namespace Tlv
Tlv::getSize
static size_t getSize(const tlv_s &tlv)
Definition: tlvCommon.h:61
Tlv
Definition: tlvCommon.h:6
Tlv::getPadding
static size_t getPadding(size_t size)
Definition: tlvCommon.h:54
Tlv::UNLIMITED
constexpr size_t UNLIMITED
Definition: tlvCommon.h:12
Tlv::alignmentSize
constexpr size_t alignmentSize
Definition: tlvCommon.h:15
Tlv::type_t
uint32_t type_t
Definition: tlvCommon.h:9
Tlv::__attribute__
struct __attribute__((packed)) header_s
Definition: tlvCommon.h:19
length
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
header
const std::string header


outsight_alb_driver
Author(s): Outsight
autogenerated on Thu Oct 13 2022 02:21:45