ROSaic
crc.c
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 
31 #include <rosaic/crc/crc.h>
32 
43 uint16_t compute16CCITT (const void *buf, size_t buf_length) // The CRC we choose is 2 bytes, remember, hence uint16_t..
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 }
67 
68 bool isValid(const void *block)
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.
Declares the functions to compute and validate the CRC of a buffer.
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
bool isValid(const void *block)
Validates whether the calculated CRC of the SBF block at hand matches the CRC field of the streamed S...
Definition: crc.c:68