ROSaic
Functions
crc.c File Reference

Defines the CRC table and the functions to compute and validate the CRC of an SBF block. More...

#include <septentrio_gnss_driver/crc/crc.h>
Include dependency graph for crc.c:

Go to the source code of this file.

Functions

uint16_t compute16CCITT (const void *buf, size_t buf_length)
 This function computes the CRC-8-CCITT (Cyclic Redundancy Check) of a buffer "buf" of "buf_length" bytes. More...
 
bool isValid (const void *block)
 Validates whether the calculated CRC of the SBF block at hand matches the CRC field of the streamed SBF block. More...
 

Detailed Description

Defines the CRC table and the functions to compute and validate the CRC of an SBF block.

Date
17/08/20

Definition in file crc.c.

Function Documentation

◆ compute16CCITT()

uint16_t compute16CCITT ( const void *  buf,
size_t  buf_length 
)

This function computes the CRC-8-CCITT (Cyclic Redundancy Check) of a buffer "buf" of "buf_length" bytes.

Note that a void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type.

Definition at line 43 of file crc.c.

Referenced by isValid().

44 {
45  uint32_t i;
46  uint16_t crc = 0; // Seed is 0, as suggested by the firmware, will compute CRC in the forward direction..
47 
48  const uint8_t *buf8 = (const uint8_t *) buf;
49  for (i=0; i<buf_length; i++)
50  {
51  crc = (crc << 8) ^ CRC_LOOK_UP[ (crc >> 8) ^ buf8[i] ];
52  // The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers.
53  // The result of XOR is 1 if the two bits are different.
54  // The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand,
55  // the second operand decides the number of places to shift.
56  // The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand,
57  // the second operand decides the number of places to shift; you can just loose the smallest values if big-endian.
58  // The left shift and right shift operators should not be used for negative numbers.
59  // The left-shift and right-shift operators are equivalent to multiplication and division by 2 respectively,
60  // hence only rightshift is non-exact (remainder is not retained).
61  // CRC_LOOK_UP is constructed from truncated polynomial (divisor).
62  // The above implements a kind of CRC 32 algorithm: efficient, fast.
63  }
64 
65  return crc;
66 }
Here is the caller graph for this function:

◆ isValid()

bool isValid ( const void *  block)

Validates whether the calculated CRC of the SBF block at hand matches the CRC field of the streamed SBF block.

Parameters
blockThe SBF block that we are interested in
Returns
True if the CRC check of the SBFBlock has passed, false otherwise

Definition at line 68 of file crc.c.

References compute16CCITT(), BlockHeader_t::crc, BlockHeader_t::id, and BlockHeader_t::length.

Referenced by io_comm_rx::RxMessage::read().

69 {
70  // Convert to access the generic header fields. Otherwise block2->id would not work.
71  const BlockHeader_t * block2 = (const BlockHeader_t *) block;
72  uint16_t crc;
73  // We need all of the message except for the first 4 bytes (Sync and CRC), i.e. we start at the address of ID.
74  crc = compute16CCITT ( &(block2->id), block2->length-2*sizeof(uint16_t) );
75  return (crc == block2->crc) ? true:false;
76 }
uint16_t length
Length of the entire message including the header. A multiple of 4 between 8 and 4096.
Struct for the SBF block&#39;s header message.
uint16_t crc
The check sum.
uint16_t id
This is the block ID.
uint16_t compute16CCITT(const void *buf, size_t buf_length)
This function computes the CRC-8-CCITT (Cyclic Redundancy Check) of a buffer "buf" of "buf_length" by...
Definition: crc.c:43
Here is the call graph for this function:
Here is the caller graph for this function: