crc.h
Go to the documentation of this file.
1 /**************************************************************************/ /**
2  * @brief Message CRC support.
3  * @file
4  ******************************************************************************/
5 
6 #pragma once
7 
8 #include <cstdint>
9 #include <string>
10 
13 
14 namespace point_one {
15 namespace fusion_engine {
16 namespace messages {
17 
18 /**
19  * @defgroup crc_support CRC Calculation/Message Validation Support
20  * @{
21  */
22 
23 /**
24  * @brief Calculate the CRC for the message (header + payload) contained in the
25  * buffer.
26  *
27  * @param buffer A byte buffer containing a @ref MessageHeader and payload.
28  *
29  * @return The calculated CRC value.
30  */
31 P1_EXPORT uint32_t CalculateCRC(const void* buffer);
32 
33 /**
34  * @brief Check if the message contained in the buffer has a valid CRC.
35  *
36  * @param buffer A byte buffer containing a @ref MessageHeader and payload.
37  *
38  * @return `true` if the CRC value in the header matches the CRC computed from
39  * the current contents.
40  */
41 inline bool IsValid(const void* buffer) {
42  // Sanity check the message payload length before calculating the CRC.
43  const MessageHeader& header = *static_cast<const MessageHeader*>(buffer);
44  if (sizeof(MessageHeader) + header.payload_size_bytes >
46  return false;
47  } else {
48  return header.crc == CalculateCRC(buffer);
49  }
50 }
51 
52 /** @} */
53 
54 } // namespace messages
55 } // namespace fusion_engine
56 } // namespace point_one
Library portability helper definitions.
The header present at the beginning of every message.
Definition: defs.h:446
static const size_t MAX_MESSAGE_SIZE_BYTES
The maximum expected message size (in bytes), used for sanity checking.
Definition: defs.h:455
uint32_t CalculateCRC(const void *buffer)
Calculate the CRC for the message (header + payload) contained in the buffer.
Definition: crc.cc:57
#define P1_EXPORT
Definition: portability.h:33
Definition: logging.h:36
bool IsValid(const void *buffer)
Check if the message contained in the buffer has a valid CRC.
Definition: crc.h:41
uint32_t payload_size_bytes
The size of the serialized message (bytes).
Definition: defs.h:487
Point One FusionEngine output message common definitions.
uint32_t crc
The 32-bit CRC of all bytes from and including the protocol_version field to the last byte in the mes...
Definition: defs.h:470