QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
RTCMParser.cc
Go to the documentation of this file.
1#include "RTCMParser.h"
2
7
9{
10 _state = WaitingForPreamble;
11 _messageLength = 0;
12 _bytesRead = 0;
13 _lengthBytesRead = 0;
14 _crcBytesRead = 0;
15}
16
17bool RTCMParser::addByte(uint8_t byte)
18{
19 switch (_state) {
20 case WaitingForPreamble:
21 if (byte == RTCM3_PREAMBLE) {
22 _buffer[0] = byte;
23 _bytesRead = 1;
24 _state = ReadingLength;
25 _lengthBytesRead = 0;
26 }
27 break;
28
29 case ReadingLength:
30 _lengthBytes[_lengthBytesRead++] = byte;
31 _buffer[_bytesRead++] = byte;
32 if (_lengthBytesRead == 2) {
33 _messageLength = ((_lengthBytes[0] & 0x03) << 8) | _lengthBytes[1];
34 if (_messageLength > 0 && _messageLength <= kMaxPayloadLength) {
35 _state = ReadingMessage;
36 } else {
37 reset();
38 }
39 }
40 break;
41
42 case ReadingMessage:
43 if (_bytesRead < kHeaderSize + kMaxPayloadLength) {
44 _buffer[_bytesRead++] = byte;
45 }
46 if (_bytesRead >= _messageLength + kHeaderSize) {
47 _state = ReadingCRC;
48 _crcBytesRead = 0;
49 }
50 break;
51
52 case ReadingCRC:
53 _crcBytes[_crcBytesRead++] = byte;
54 if (_crcBytesRead == kCrcSize) {
55 return true;
56 }
57 break;
58 }
59 return false;
60}
61
62uint16_t RTCMParser::messageId() const
63{
64 if (_messageLength >= 2) {
65 return ((_buffer[3] << 4) | (_buffer[4] >> 4)) & 0xFFF;
66 }
67 return 0;
68}
69
70uint32_t RTCMParser::crc24q(const uint8_t* data, size_t len)
71{
72 static constexpr uint32_t kPoly = 0x1864CFB;
73 uint32_t crc = 0;
74 for (size_t i = 0; i < len; i++) {
75 crc ^= static_cast<uint32_t>(data[i]) << 16;
76 for (int j = 0; j < 8; j++) {
77 crc <<= 1;
78 if (crc & 0x1000000) {
79 crc ^= kPoly;
80 }
81 }
82 }
83 return crc & 0xFFFFFF;
84}
85
87{
88 if (_messageLength == 0 || _bytesRead < kHeaderSize + _messageLength) {
89 return false;
90 }
91
92 const uint32_t computed = crc24q(_buffer, kHeaderSize + _messageLength);
93 const uint32_t received = (static_cast<uint32_t>(_crcBytes[0]) << 16) |
94 (static_cast<uint32_t>(_crcBytes[1]) << 8) |
95 static_cast<uint32_t>(_crcBytes[2]);
96 return computed == received;
97}
static constexpr uint8_t RTCM3_PREAMBLE
Definition RTCMParser.h:6
void reset()
Definition RTCMParser.cc:8
bool validateCrc() const
Definition RTCMParser.cc:86
static uint32_t crc24q(const uint8_t *data, size_t len)
Definition RTCMParser.cc:70
static constexpr int kCrcSize
Definition RTCMParser.h:18
uint16_t messageId() const
Definition RTCMParser.cc:62
bool addByte(uint8_t byte)
Definition RTCMParser.cc:17