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 
34 namespace crc {
42  uint16_t compute16CCITT(const uint8_t* buf,
43  size_t buf_length) // The CRC we choose is 2 bytes,
44  // remember, hence uint16_t..
45  {
46  uint16_t crc = 0; // Seed is 0, as suggested by the firmware, will compute
47  // CRC in the forward direction..
48 
49  for (size_t i = 0; i < buf_length; i++)
50  {
51  crc = (crc << 8) ^ CRC_LOOK_UP[uint8_t((crc >> 8) ^ buf[i])];
52  // The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does
53  // XOR on every bit of two numbers. The result of XOR is 1 if the two
54  // bits are different. The << (left shift) in C or C++ takes two numbers,
55  // left shifts the bits of the first operand, the second operand decides
56  // the number of places to shift. The >> (right shift) in C or C++ takes
57  // two numbers, right shifts the bits of the first operand, the second
58  // operand decides the number of places to shift; you can just loose the
59  // smallest values if big-endian. The left shift and right shift
60  // operators should not be used for negative numbers. The left-shift and
61  // right-shift operators are equivalent to multiplication and division by
62  // 2 respectively, hence only rightshift is non-exact (remainder is not
63  // retained). CRC_LOOK_UP is constructed from truncated polynomial
64  // (divisor). The above implements a kind of CRC 32 algorithm: efficient,
65  // fast.
66  }
67 
68  return crc;
69  }
70 
71  bool isValid(const std::vector<uint8_t>& message)
72  {
73  // We need all of the message except for the first 4 bytes (Sync and CRC),
74  // i.e. we start at the address of ID.
75  uint16_t length = parsing_utilities::getLength(message);
76  if (length > 4)
77  {
78  uint16_t crc = compute16CCITT(message.data() + 4, length - 4);
79  return (crc == parsing_utilities::getCrc(message));
80  } else
81  {
82  return false;
83  }
84  }
85 } // namespace crc
crc::isValid
bool isValid(const std::vector< uint8_t > &message)
Validates whether the calculated CRC of the SBF block at hand matches the CRC field of the streamed S...
Definition: crc.cpp:71
crc::compute16CCITT
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:42
crc.hpp
Declares the functions to compute and validate the CRC of a buffer.
CRC_LOOK_UP
static const std::array< uint16_t, 256 > CRC_LOOK_UP
CRC look-up table for fast computation of the 16-bit CRC for SBF blocks.
Definition: sbf_blocks.hpp:240
parsing_utilities::getLength
uint16_t getLength(const std::vector< uint8_t > &message)
Get the length of the SBF message.
Definition: parsing_utilities.cpp:429
parsing_utilities::getCrc
uint16_t getCrc(const std::vector< uint8_t > &message)
Get the CRC of the SBF message.
Definition: parsing_utilities.cpp:414
parsing_utilities.hpp
Declares utility functions used when parsing messages.
crc
Definition: crc.hpp:42
length
TF2SIMD_FORCE_INLINE tf2Scalar length(const Quaternion &q)


septentrio_gnss_driver
Author(s): Tibor Dome
autogenerated on Wed Nov 22 2023 04:04:27