F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CRC16.hpp
Go to the documentation of this file.
1 #ifndef SVC_CCSDS_UTILS_CRC16_HPP
2 #define SVC_CCSDS_UTILS_CRC16_HPP
3 
4 #include "Fw/Types/Assert.hpp"
6 // Include the libcrc library because we need update_crc_ccitt that is not provided through the main interface.
7 extern "C" {
9 }
10 
11 namespace Svc {
12 namespace Ccsds {
13 namespace Utils {
14 
19 class CRC16 {
20  public:
21  // Initial value is 0xFFFF
22  CRC16() : m_crc(std::numeric_limits<U16>::max()) {}
23 
29  void update(U8 new_byte) { this->m_crc = static_cast<U16>(update_crc_ccitt(m_crc, static_cast<char>(new_byte))); };
30 
32  U16 finalize() {
33  // Specified XOR value is 0x0000
34  return this->m_crc ^ static_cast<U16>(0);
35  };
36 
44  static U16 compute(const U8* buffer, U32 length) {
45  FW_ASSERT(buffer != nullptr);
46  U16 crc = std::numeric_limits<U16>::max(); // Initial value
47  for (U32 i = 0; i < length; ++i) {
48  crc = static_cast<U16>(update_crc_ccitt(crc, static_cast<char>(buffer[i])));
49  }
50  return crc ^ static_cast<U16>(0); // Finalize with XOR value
51  }
52 
53  U16 m_crc;
54 };
55 
56 } // namespace Utils
57 } // namespace Ccsds
58 } // namespace Svc
59 
60 #endif // SVC_CCSDS_UTILS_CRC16_HPP
static U16 compute(const U8 *buffer, U32 length)
compute CRC16 for a buffer
Definition: CRC16.hpp:44
CRC16 CCITT implementation.
Definition: CRC16.hpp:19
U16 finalize()
finalize and return CRC value
Definition: CRC16.hpp:32
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
unsigned short update_crc_ccitt(unsigned short crc, char c)
Definition: lib_crc.c:141
C++ header for working with basic fprime types.
void update(U8 new_byte)
update CRC with one new byte
Definition: CRC16.hpp:29
RateGroupDivider component implementation.
#define FW_ASSERT(...)
Definition: Assert.hpp:14