CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_GPS/RTCM3_Parser.cpp
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
/*
16
RTCMv3 parser, used to support moving baseline RTK mode between two
17
GPS modules
18
*/
19
20
#include <string.h>
21
#include <AP_Math/AP_Math.h>
22
#include "RTCM3_Parser.h"
23
24
// reset state
25
void RTCM3_Parser::reset(void)
26
{
27
pkt_len = 0;
28
pkt_bytes = 0;
29
found_len = 0;
30
}
31
32
// clear previous packet
33
void RTCM3_Parser::clear_packet(void)
34
{
35
if (found_len == 0) {
36
return;
37
}
38
// clear previous packet
39
if (pkt_bytes > found_len) {
40
memmove(&pkt[0], &pkt[found_len], pkt_bytes-found_len);
41
pkt_bytes -= found_len;
42
} else {
43
pkt_bytes = 0;
44
}
45
found_len = 0;
46
pkt_len = 0;
47
}
48
49
// return length of found packet
50
uint16_t RTCM3_Parser::get_len(const uint8_t *&bytes) const
51
{
52
if (found_len > 0) {
53
bytes = &pkt[0];
54
}
55
return found_len;
56
}
57
58
// return ID of found packet
59
uint16_t RTCM3_Parser::get_id(void) const
60
{
61
if (found_len == 0) {
62
return 0;
63
}
64
return (pkt[3]<<8 | pkt[4]) >> 4;
65
}
66
67
// look for preamble to try to resync
68
void RTCM3_Parser::resync(void)
69
{
70
const uint8_t *p = (const uint8_t *)memchr(&pkt[1], RTCMv3_PREAMBLE, pkt_bytes-1);
71
if (p != nullptr) {
72
const uint16_t idx = p - &pkt[0];
73
memmove(&pkt[0], p, pkt_bytes - idx);
74
pkt_bytes -= idx;
75
if (pkt_bytes >= 3) {
76
pkt_len = (pkt[1]<<8 | pkt[2]) & 0x3ff;
77
} else {
78
pkt_len = 0;
79
}
80
} else {
81
reset();
82
}
83
}
84
85
// parse packet
86
bool RTCM3_Parser::parse(void)
87
{
88
const uint8_t *parity = &pkt[pkt_len+3];
89
uint32_t crc1 = (parity[0] << 16) | (parity[1] << 8) | parity[2];
90
uint32_t crc2 = crc_crc24(pkt, pkt_len+3);
91
if (crc1 != crc2) {
92
resync();
93
return false;
94
}
95
96
// we got a good packet
97
found_len = pkt_len+6;
98
return true;
99
}
100
101
// read in one byte, return true if a full packet is available
102
bool RTCM3_Parser::read(uint8_t byte)
103
{
104
clear_packet();
105
106
if (pkt_bytes > 0 && pkt[0] != RTCMv3_PREAMBLE) {
107
resync();
108
}
109
110
if (pkt_bytes == 0 && byte != RTCMv3_PREAMBLE) {
111
// discard
112
return false;
113
}
114
115
if (pkt_bytes >= sizeof(pkt)) {
116
// too long, resync
117
resync();
118
}
119
120
pkt[pkt_bytes++] = byte;
121
122
if (pkt_len == 0 && pkt_bytes >= 3) {
123
pkt_len = (pkt[1]<<8 | pkt[2]) & 0x3ff;
124
if (pkt_len == 0) {
125
resync();
126
}
127
}
128
129
if (pkt_len != 0 && pkt_bytes >= pkt_len + 6) {
130
// got header, packet body and parity
131
return parse();
132
}
133
134
// need more bytes
135
return false;
136
}
137
138
#ifdef RTCM_MAIN_TEST
139
/*
140
parsing test, taking a raw file captured from UART to u-blox F9
141
*/
142
#include <sys/types.h>
143
#include <sys/stat.h>
144
#include <fcntl.h>
145
#include <stdlib.h>
146
#include <unistd.h>
147
#include <stdio.h>
148
149
int main(int argc, const char *argv[])
150
{
151
const char *fname = argv[1];
152
int fd = ::open(fname, O_RDONLY);
153
if (fd == -1) {
154
perror(fname);
155
::exit(1);
156
}
157
RTCM3_Parser parser {};
158
uint8_t b;
159
while (::read(fd, &b, 1) == 1) {
160
if (parser.read(b)) {
161
const uint8_t *bytes;
162
printf("packet len %u ID %u\n", parser.get_len(bytes), parser.get_id());
163
}
164
}
165
return 0;
166
}
167
#endif
168
169