QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
Checksums.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <span>
5
6namespace QGC {
7
10{
11 uint8_t a = 0;
12 uint8_t b = 0;
13
14 constexpr bool operator==(const Fletcher8&) const = default;
15};
16
18[[nodiscard]] constexpr Fletcher8 fletcher8(std::span<const uint8_t> bytes, Fletcher8 state = {})
19{
20 for (const uint8_t byte : bytes) {
21 state.a = static_cast<uint8_t>(state.a + byte);
22 state.b = static_cast<uint8_t>(state.b + state.a);
23 }
24 return state;
25}
26
28[[nodiscard]] constexpr uint16_t crc16Xmodem(std::span<const uint8_t> bytes)
29{
30 uint16_t crc = 0;
31 for (const uint8_t byte : bytes) {
32 uint8_t x = static_cast<uint8_t>((crc >> 8) ^ byte);
33 x ^= x >> 4;
34 crc = static_cast<uint16_t>((crc << 8) ^ (x << 12) ^ (x << 5) ^ x);
35 }
36 return crc;
37}
38
40[[nodiscard]] constexpr uint32_t crc24q(std::span<const uint8_t> bytes)
41{
42 uint32_t crc = 0;
43 for (const uint8_t byte : bytes) {
44 crc ^= uint32_t(byte) << 16;
45 for (int bit = 0; bit < 8; ++bit) {
46 crc <<= 1;
47 if (crc & 0x1000000) {
48 crc ^= 0x1864cfb;
49 }
50 }
51 }
52 return crc & 0xffffff;
53}
54
56[[nodiscard]] constexpr uint8_t nmeaChecksum(std::span<const uint8_t> body)
57{
58 uint8_t result = 0;
59 for (const uint8_t byte : body) {
60 result ^= byte;
61 }
62 return result;
63}
64
65} // namespace QGC
constexpr uint32_t crc24q(std::span< const uint8_t > bytes)
CRC-24Q: polynomial 0x1864CFB, initial value 0, no reflection and no final XOR.
Definition Checksums.h:40
constexpr uint8_t nmeaChecksum(std::span< const uint8_t > body)
XOR of all bytes, the checksum NMEA 0183 sentences carry over their body.
Definition Checksums.h:56
constexpr uint16_t crc16Xmodem(std::span< const uint8_t > bytes)
CRC-16/XMODEM: polynomial 0x1021, initial value 0, no reflection and no final XOR.
Definition Checksums.h:28
constexpr Fletcher8 fletcher8(std::span< const uint8_t > bytes, Fletcher8 state={})
Continues state over bytes, so a frame checksummed in parts matches the contiguous result.
Definition Checksums.h:18
8-bit Fletcher checksum: two running byte sums modulo 256.
Definition Checksums.h:10
constexpr bool operator==(const Fletcher8 &) const =default