Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Crc16CCITT Class Reference

Helper class for computing CRC-16 according to the CCITT specification (polynomial = 0x1021, x^16 + x^12 + x^5 + 1, initial value = 0xFFFF). More...

#include <Crc.hxx>

Public Member Functions

void init ()
 Re-sets the state machine for checksumming a new message.
 
uint16_t get ()
 
bool check_ok ()
 Checks that the message has a correct CRC.
 
bool check_ok (uint16_t checksum)
 Checks that the message has a correct CRC.
 
void update16 (uint8_t message_byte)
 Processes one byte of the incoming message.
 
void update256 (uint8_t message_byte)
 Processes one byte of the incoming message.
 
void update (uint8_t message_byte)
 Processes one byte of the incoming message.
 
void crc (const void *data, size_t length_bytes)
 Computes the 16-bit CRC value over data.
 

Private Attributes

uint16_t state_
 Current value of the state register for the CRC computation.
 

Static Private Attributes

static const uint16_t table256 [256]
 256-entry lookup table for the update256 function.
 
static const uint16_t tableHi16 [16]
 16-entry lookup table for the update16 function.
 
static const uint16_t tableLo16 [16]
 16-entry lookup table for the update16 function.
 

Detailed Description

Helper class for computing CRC-16 according to the CCITT specification (polynomial = 0x1021, x^16 + x^12 + x^5 + 1, initial value = 0xFFFF).

This class can incrementally compute CRC byte by byte. There are two implementations available, with different code space requirements.

Definition at line 215 of file Crc.hxx.

Constructor & Destructor Documentation

◆ Crc16CCITT()

Crc16CCITT::Crc16CCITT ( )
inline

Definition at line 218 of file Crc.hxx.

Member Function Documentation

◆ check_ok() [1/2]

bool Crc16CCITT::check_ok ( )
inline

Checks that the message has a correct CRC.

This function assumes that the CRC byte has already been consumed.

Returns
true if the message checksum is correct.

Definition at line 238 of file Crc.hxx.

◆ check_ok() [2/2]

bool Crc16CCITT::check_ok ( uint16_t  checksum)
inline

Checks that the message has a correct CRC.

This function assumes that the CRC byte was not part of the message.

Parameters
checksumthe CRC16 of the received message.
Returns
true if the message checksum is correct.

Definition at line 247 of file Crc.hxx.

◆ crc()

void Crc16CCITT::crc ( const void *  data,
size_t  length_bytes 
)
inline

Computes the 16-bit CRC value over data.

Parameters
datawhat to compute the checksum over
length_byteshow long data is

Definition at line 286 of file Crc.hxx.

◆ get()

uint16_t Crc16CCITT::get ( )
inline
Returns
the checksum of the currently consumed message.

Definition at line 230 of file Crc.hxx.

◆ init()

void Crc16CCITT::init ( )
inline

Re-sets the state machine for checksumming a new message.

Definition at line 224 of file Crc.hxx.

◆ update()

void Crc16CCITT::update ( uint8_t  message_byte)
inline

Processes one byte of the incoming message.

Parameters
message_bytenext byte in the message.

Definition at line 272 of file Crc.hxx.

◆ update16()

void Crc16CCITT::update16 ( uint8_t  message_byte)
inline

Processes one byte of the incoming message.

A small lookup table will be used.

Parameters
message_bytenext byte in the message.

Definition at line 255 of file Crc.hxx.

◆ update256()

void Crc16CCITT::update256 ( uint8_t  message_byte)
inline

Processes one byte of the incoming message.

A 256-byte lookup table will be used.

Parameters
message_bytenext byte in the message.

Definition at line 265 of file Crc.hxx.

Member Data Documentation

◆ state_

uint16_t Crc16CCITT::state_
private

Current value of the state register for the CRC computation.

Definition at line 310 of file Crc.hxx.

◆ table256

const uint16_t Crc16CCITT::table256
staticprivate

256-entry lookup table for the update256 function.

Definition at line 275 of file Crc.hxx.

◆ tableHi16

const uint16_t Crc16CCITT::tableHi16
staticprivate
Initial value:
=
{
0x0000, 0x1231, 0x2462, 0x3653, 0x48C4, 0x5AF5, 0x6CA6, 0x7E97,
0x9188, 0x83B9, 0xB5EA, 0xA7DB, 0xD94C, 0xCB7D, 0xFD2E, 0xEF1F
}

16-entry lookup table for the update16 function.

Definition at line 317 of file Crc.hxx.

◆ tableLo16

const uint16_t Crc16CCITT::tableLo16
staticprivate
Initial value:
=
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF
}

16-entry lookup table for the update16 function.

Definition at line 311 of file Crc.hxx.


The documentation for this class was generated from the following files: