crc.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // © Copyright 2020, Septentrio NV/SA.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived
15 // from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // *****************************************************************************
30 
33 
40 uint16_t compute16CCITT (const uint8_t *buf, size_t buf_length) // The CRC we choose is 2 bytes, remember, hence uint16_t..
41 {
42  uint16_t crc = 0; // Seed is 0, as suggested by the firmware, will compute CRC in the forward direction..
43 
44  for (size_t i = 0; i < buf_length; i++)
45  {
46  crc = (crc << 8) ^ CRC_LOOK_UP[uint8_t( (crc >> 8) ^ buf[i])];
47  // The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers.
48  // The result of XOR is 1 if the two bits are different.
49  // The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand,
50  // the second operand decides the number of places to shift.
51  // The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand,
52  // the second operand decides the number of places to shift; you can just loose the smallest values if big-endian.
53  // The left shift and right shift operators should not be used for negative numbers.
54  // The left-shift and right-shift operators are equivalent to multiplication and division by 2 respectively,
55  // hence only rightshift is non-exact (remainder is not retained).
56  // CRC_LOOK_UP is constructed from truncated polynomial (divisor).
57  // The above implements a kind of CRC 32 algorithm: efficient, fast.
58  }
59 
60  return crc;
61 }
62 
63 bool isValid(const uint8_t *block)
64 {
65  // We need all of the message except for the first 4 bytes (Sync and CRC), i.e. we start at the address of ID.
66  uint16_t length = parsing_utilities::getLength(block);
67  if (length > 4)
68  {
69  uint16_t crc = compute16CCITT(block + 4, length - 4);
70  return (crc == parsing_utilities::getCrc(block));
71  }
72  else
73  {
74  return false;
75  }
76 }
uint16_t compute16CCITT(const uint8_t *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.cpp:40
uint16_t getCrc(const uint8_t *buffer)
Get the CRC of the SBF message.
Declares the functions to compute and validate the CRC of a buffer.
bool isValid(const uint8_t *block)
Validates whether the calculated CRC of the SBF block at hand matches the CRC field of the streamed S...
Definition: crc.cpp:63
uint16_t getLength(const uint8_t *buffer)
Get the length of the SBF message.
TF2SIMD_FORCE_INLINE tf2Scalar length(const Quaternion &q)
Declares utility functions used when parsing messages.


septentrio_gnss_driver
Author(s): Tibor Dome
autogenerated on Sat Mar 11 2023 03:12:55