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 Calculate the CRC for the message (payload) contained in the buffer.
35  *
36  * @param buffer A byte buffer containing a payload.
37  * @param length The length of the buffer.
38  * @param initial_value The seed value of the CRC calculation.
39  *
40  * @return The calculated CRC value.
41  */
42 P1_EXPORT uint32_t CalculateCRC(const void* buffer, size_t length,
43  uint32_t initial_value = 0);
44 
45 /**
46  * @brief Check if the message contained in the buffer has a valid CRC.
47  *
48  * @param buffer A byte buffer containing a @ref MessageHeader and payload.
49  *
50  * @return `true` if the CRC value in the header matches the CRC computed from
51  * the current contents.
52  */
53 inline 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 }
63 
64 /** @} */
65 
66 } // namespace messages
67 } // namespace fusion_engine
68 } // namespace point_one
uint32_t CalculateCRC(const void *buffer, size_t length, uint32_t initial_value)
Calculate the CRC for the message (payload) contained in the buffer.
Definition: crc.cc:45
Library portability helper definitions.
The header present at the beginning of every message.
Definition: defs.h:569
static const size_t MAX_MESSAGE_SIZE_BYTES
The maximum expected message size (in bytes), used for sanity checking.
Definition: defs.h:578
#define P1_EXPORT
Definition: portability.h:33
GNSS signal and frequency type definitions.
Definition: logging.h:38
bool IsValid(const void *buffer)
Check if the message contained in the buffer has a valid CRC.
Definition: crc.h:53
uint32_t payload_size_bytes
The size of the serialized message (bytes).
Definition: defs.h:610
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:593