Skip to main content

CRC Calculation/Message Validation Support

Functions Index

uint32_tCalculateCRC (const void *buffer)

Calculate the CRC for the message (header + payload) contained in the buffer. More...

uint32_tCalculateCRC (const void *buffer, size_t length, uint32_t initial_value=0)

Calculate the CRC for the message (payload) contained in the buffer. More...

boolIsValid (const void *buffer)

Check if the message contained in the buffer has a valid CRC. More...

Functions

CalculateCRC()

P1_EXPORT uint32_t point_one::fusion_engine::messages::CalculateCRC (const void * buffer)

Calculate the CRC for the message (header + payload) contained in the buffer.

Parameters
buffer

A byte buffer containing a MessageHeader and payload.

Returns

The calculated CRC value.

Definition at line 57 of file crc.cc.

58 static constexpr size_t offset = offsetof(MessageHeader, protocol_version);
59 const MessageHeader& header = *static_cast<const MessageHeader*>(buffer);
60 size_t size_bytes =
61 (sizeof(MessageHeader) - offset) + header.payload_size_bytes;
62 return CalculateCRC(reinterpret_cast<const uint8_t*>(&header) + offset,
64}

CalculateCRC()

P1_EXPORT uint32_t point_one::fusion_engine::messages::CalculateCRC (const void * buffer, size_t length, uint32_t initial_value=0)

Calculate the CRC for the message (payload) contained in the buffer.

Parameters
buffer

A byte buffer containing a payload.

length

The length of the buffer.

initial_value

The seed value of the CRC calculation.

Returns

The calculated CRC value.

Definition at line 45 of file crc.cc.

45uint32_t CalculateCRC(const void* buffer, size_t length,
47 static const uint32_t* crc_table = ::GetCRCTable();
48 uint32_t c = initial_value ^ 0xFFFFFFFF;
49 const uint8_t* u = static_cast<const uint8_t*>(buffer);
50 for (size_t i = 0; i < length; ++i) {
51 c = crc_table[(c ^ u[i]) & 0xFF] ^ (c >> 8);
52 }
53 return c ^ 0xFFFFFFFF;
54}

IsValid()

bool point_one::fusion_engine::messages::IsValid (const void * buffer)
inline

Check if the message contained in the buffer has a valid CRC.

Parameters
buffer

A byte buffer containing a MessageHeader and payload.

Returns

true if the CRC value in the header matches the CRC computed from the current contents.

Definition at line 53 of file crc.h.

53inline bool IsValid(const void* buffer) {
54 // Sanity check the message payload length before calculating the CRC.
55 const MessageHeader& header = *static_cast<const MessageHeader*>(buffer);
56 if (sizeof(MessageHeader) + header.payload_size_bytes >
58 return false;
59 } else {
60 return header.crc == CalculateCRC(buffer);
61 }
62}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.