-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathubloxdatastreamprocessor.cpp
284 lines (241 loc) · 8.72 KB
/
ubloxdatastreamprocessor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
ubloxdatastreamprocessor.cpp (part of GNSS-Stylus)
Copyright (C) 2019-2021 Pasi Nuutinmaki (gnssstylist<at>sci<dot>fi)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* @file ubloxdatastreamprocessor.cpp
* @brief Definitions for a class separating different types of messages (NMEA, UBX, RTCM) sent by u-blox devices from a stream of bytes.
*/
#include "ubloxdatastreamprocessor.h"
UBloxDataStreamProcessor::UBloxDataStreamProcessor(const unsigned int maxUBXMessageLength,
const unsigned int maxNMEASentenceLenght,
const unsigned int maxUnidentifiedDataSize)
{
this->maxUBXMessageLength = maxUBXMessageLength;
this->maxNMEASentenceLenght = maxNMEASentenceLenght;
this->maxUnidentifiedDataSize = maxUnidentifiedDataSize;
// inputBuffer.reserve(1024);
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
}
void UBloxDataStreamProcessor::process(const char inbyte, qint64 byteTime)
{
switch (state)
{
case WAITING_FOR_START_BYTE:
if ((inbyte == '$') || (static_cast<unsigned char>(inbyte) == 0xB5) || (static_cast<unsigned char>(inbyte) == 0xD3))
{
firstMessageByteTime = byteTime;
if (inputBuffer.length() != 0)
{
// inputBuffer should be empty at this point if all bytes can be interpreted as valid ones.
emit unidentifiedDataReceived(inputBuffer);
}
inputBuffer.clear();
inputBuffer.append(inbyte);
}
else if (static_cast<unsigned int>(inputBuffer.length()) >= maxUnidentifiedDataSize)
{
emit unidentifiedDataReceived(inputBuffer);
inputBuffer.clear();
}
if (inbyte == '$')
{
// '$' = NMEA's start-character
state = NMEA_WAITING_FOR_CR;
}
else if (static_cast<unsigned char>(inbyte) == 0xB5)
{
// 0xB5 = UBX's start-character
state = UBX_WAITING_FOR_SYNC_CHAR_2;
}
else if (static_cast<unsigned char>(inbyte) == 0xD3)
{
// 0xD3 = RTCM's start-character
state = RTCM_WAITING_FOR_MESSAGE_LENGTH_1;
}
else
{
// Byte not recognized as a start character of any supported message types
// -> Append it into inputBuffer (will be emitted as unidentified data when recognized data is received)
inputBuffer.append(inbyte);
}
break;
case UBX_WAITING_FOR_SYNC_CHAR_2:
if (static_cast<unsigned char>(inbyte) == 0x62)
{
inputBuffer.append(inbyte);
state = UBX_WAITING_FOR_MESSAGE_CLASS;
}
else
{
emit ubxParseError("No UBX sync char 2 after sync char 1.");
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
}
break;
case UBX_WAITING_FOR_MESSAGE_CLASS:
inputBuffer.append(inbyte);
state = UBX_WAITING_FOR_MESSAGE_ID;
break;
case UBX_WAITING_FOR_MESSAGE_ID:
inputBuffer.append(inbyte);
state = UBX_WAITING_FOR_LENGTH_BYTE_1;
break;
case UBX_WAITING_FOR_LENGTH_BYTE_1:
inputBuffer.append(inbyte);
state = UBX_WAITING_FOR_LENGTH_BYTE_2;
break;
case UBX_WAITING_FOR_LENGTH_BYTE_2:
inputBuffer.append(inbyte);
ubxPayloadLength = static_cast<unsigned char>(inputBuffer[4]) + 256 * static_cast<unsigned char>(inputBuffer[5]);
if (ubxPayloadLength > maxUBXMessageLength - 8)
{
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
emit ubxParseError("UBX message length exceeded maximum value.");
}
if (ubxPayloadLength != 0)
{
state = UBX_RECEIVING_PAYLOAD;
}
else
{
state = UBX_WAITING_FOR_CK_A;
}
break;
case UBX_RECEIVING_PAYLOAD:
inputBuffer.append(inbyte);
if (inputBuffer.length() >= ubxPayloadLength + 6)
{
state = UBX_WAITING_FOR_CK_A;
}
break;
case UBX_WAITING_FOR_CK_A:
inputBuffer.append(inbyte);
state = UBX_WAITING_FOR_CK_B;
break;
case UBX_WAITING_FOR_CK_B:
inputBuffer.append(inbyte);
{
// Calculate checksum
unsigned char ck_a = 0;
unsigned char ck_b = 0;
for (int i = 2; i < inputBuffer.length() - 2; i++)
{
ck_a += static_cast<unsigned char>(inputBuffer[i]);
ck_b += ck_a;
}
if (((static_cast<unsigned char>(inputBuffer[inputBuffer.length() - 2])) != ck_a) ||
((static_cast<unsigned char>(inputBuffer[inputBuffer.length() - 1])) != ck_b))
{
emit ubxParseError("UBX message checksum error.");
}
else
{
// Frame is formally valid
UBXMessage newUbxMessage(inputBuffer, firstMessageByteTime, byteTime);
emit ubxMessageReceived(newUbxMessage);
}
}
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
break;
case NMEA_WAITING_FOR_CR:
// TODO: Add checksum check for NMEA and emit signal if fail
inputBuffer.append(inbyte);
if (inputBuffer.length() >= static_cast<int>(maxNMEASentenceLenght - 1))
{
emit nmeaParseError("NMEA sentence exceeded maximum length.");
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
}
else if (inbyte == 13)
{
state = NMEA_WAITING_FOR_LF;
}
break;
case NMEA_WAITING_FOR_LF:
inputBuffer.append(inbyte);
if (inbyte == 10)
{
NMEAMessage newNMEAMessage(inputBuffer, firstMessageByteTime, byteTime);
emit nmeaSentenceReceived(newNMEAMessage);
}
else
{
emit nmeaParseError("No LF after CR in the end of NMEA sentence.");
}
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
break;
case RTCM_WAITING_FOR_MESSAGE_LENGTH_1:
inputBuffer.append(inbyte);
state = RTCM_WAITING_FOR_MESSAGE_LENGTH_2;
break;
case RTCM_WAITING_FOR_MESSAGE_LENGTH_2:
inputBuffer.append(inbyte);
// 10 lowest bits used, big-endian
rtcmDataLength = (static_cast<unsigned char>(inputBuffer[2]) + 256 * static_cast<unsigned char>(inputBuffer[1])) & 0x3FF;
if (rtcmDataLength != 0)
{
state = RTCM_RECEIVING_PAYLOAD;
}
else
{
state = RTCM_WAITING_FOR_CRC_1;
}
break;
case RTCM_RECEIVING_PAYLOAD:
inputBuffer.append(inbyte);
if (inputBuffer.length() >= rtcmDataLength + 3)
{
state = RTCM_WAITING_FOR_CRC_1;
}
break;
case RTCM_WAITING_FOR_CRC_1:
inputBuffer.append(inbyte);
state = RTCM_WAITING_FOR_CRC_2;
break;
case RTCM_WAITING_FOR_CRC_2:
inputBuffer.append(inbyte);
state = RTCM_WAITING_FOR_CRC_3;
break;
case RTCM_WAITING_FOR_CRC_3:
inputBuffer.append(inbyte);
// TODO: Calculate and check CRC
// Now this just naively emits data without any checking
RTCMMessage newRTCMMessage(inputBuffer, firstMessageByteTime, byteTime);
emit rtcmMessageReceived(newRTCMMessage);
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
break;
}
}
void UBloxDataStreamProcessor::process(const QByteArray& data, const qint64 firstByteTime, const qint64 lastByteTime)
{
for (int i = 0; i < data.length(); i++)
{
qint64 byteTime = data.length() > 1 ? firstByteTime + (lastByteTime - firstByteTime) * i / (data.length() - 1) : lastByteTime;
process(data[i], byteTime);
}
}
void UBloxDataStreamProcessor::flushInputBuffer()
{
inputBuffer.clear();
state = WAITING_FOR_START_BYTE;
}
unsigned int UBloxDataStreamProcessor::getNumOfUnprocessedBytes(void)
{
return static_cast<unsigned int>(inputBuffer.length());
}