-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArduinoUbxLib.cpp
390 lines (334 loc) · 11.7 KB
/
ArduinoUbxLib.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "ArduinoUbxLib.h"
#include "Arduino.h"
/* ****************************************************************************
Constructor:
****************************************************************************/
UbxGps::UbxGps () {
};
/* ****************************************************************************
handleUbxPacket: analyzes the information the parser collected and creates
objects of known messages to be handed over to the
onReceive() method. If the message is unknown, it is handed
over to the handleUnsupportedBinMsg () method for further
processing.
****************************************************************************/
byte UbxGps::handleUbxPacket () {
if (logOutput) {
UBX_LOGF (false, "handleUbxPacket: UBX binary [");
for (unsigned short i = 0; i < parseInfo.payloadLen + 8; i++) {
if (inBuf [i] < 0x10) {
UBX_LOGF (false, "0");
}
UBX_LOG (false, inBuf [i], HEX);
UBX_LOGF (false, " ");
if (i == 5) {
UBX_LOGF (false, " | ");
}
}
UBX_LOGF (true, "]");
}
UbxPacket* packet = NULL;
byte res = UBX_NONE;
switch (parseInfo.msgClsID) {
case UBX_ACK_ACK:
packet = new UbxAckAck (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_ACK_NAK:
packet = new UbxAckNak (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_STATUS:
packet = new UbxNavStatus (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_SOL:
packet = new UbxNavSol (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_POSLLH:
packet = new UbxNavPosLLH (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_TIMEUTC:
packet = new UbxNavTimeUTC (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_TIMEGPS:
packet = new UbxNavTimeGPS (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_CFG_NMEA:
packet = new UbxCfgNMEA (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_CFG_RATE:
packet = new UbxCfgRate (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_CFG_RXM:
packet = new UbxCfgRXM (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_MON_VER:
packet = new UbxMonVer (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_RXM_SVSI:
packet = new UbxRxmSVSI (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_CLOCK:
packet = new UbxNavClock (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_DGPS:
packet = new UbxNavDGPS (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_NAV_DOP:
packet = new UbxNavDOP (inBuf, parseInfo.payloadLen + 8);
break;
case UBX_CFG_PM:
packet = new UbxCfgPM (inBuf, parseInfo.payloadLen + 8);
break;
default:
UBX_LOGF (false, "UBX msg with unknown type detected, cls/msgid: ");
UBX_LOG (true, parseInfo.msgClsID, HEX);
res = handleUnsupportedBinMsg (inBuf, parseInfo.payloadLen + 8);
}
if (NULL != packet && packet->valid) {
onReceive (packet);
delete packet;
res = UBX_BINMSG;
} else {
if (NULL == packet) {
UBX_LOGF (true, "packet is NULL! ");
} else if (!packet->valid) {
UBX_LOGF (true, "packet invalid! ");
}
}
return res;
};
/* ****************************************************************************
handleNMEA_GPMsg: calls the onReceive() method for NMEA strings.
****************************************************************************/
void UbxGps::handleNMEA_GPMsg () {
onReceive ((char*) inBuf, parseInfo.pbCount);
if (false) {
UBX_LOGF (false, "handleNMEA_GPMsg: NMEA GP [");
for (unsigned short i = 0; i < parseInfo.pbCount; i++) {
if (inBuf [i] == 0x0D) {
UBX_LOGF (false, "<CR>");
} else if (inBuf [i] == 0x0D) {
UBX_LOGF (true, "<LF>]");
}else {
UBX_LOG (false,(char) inBuf [i]);
}
}
UBX_LOGF (true, "");
}
};
/* ****************************************************************************
handleNMEA_PUBXMsg: calls the onReceive() method for PUBX strings.
****************************************************************************/
void UbxGps::handleNMEA_PUBXMsg () {
onReceive ((char*) inBuf, parseInfo.pbCount);
if (false) {
UBX_LOGF (false, "handleNMEA_PUBXMsg: NMEA PUBX [");
for (unsigned short i = 0; i < parseInfo.pbCount; i++) {
if (inBuf [i] == 0x0D) {
UBX_LOGF (false, "<CR>");
} else if (inBuf [i] == 0x0D) {
UBX_LOGF (true, "<LF>]");
} else {
UBX_LOGF (false, (char) inBuf [i]);
}
}
UBX_LOGF (true, "");
}
};
/* ****************************************************************************
resetParser: resets the inner parser info structure.
****************************************************************************/
void UbxGps::resetParser () {
parseInfo.state = PSTATE_INIT;
parseInfo.msgClsID = UBX_UNDEFINED_MSGTYPE;
parseInfo.pbCount = 0;
parseInfo.payloadLen = 0;
parseInfo.msgType = UBX_NONE;
memset (inBuf, 0, __inBufLen);
}
/* ****************************************************************************
parse: analyzes the input byte and updates the inner parser state. If a
complete ubx message, a NMEA or PUBX string has been identified, the
corresponding handle...() methods are called for further processing.
****************************************************************************/
byte UbxGps::parse (byte data) {
const UbxParserStates lastState = parseInfo.state;
byte res = UBX_NONE;
switch (parseInfo.state) {
case PSTATE_INIT:
if (data == UBX_SYNCCHAR1) {
inBuf [0] = data;
parseInfo.state = PSTATE_UBX_SYNC1;
} else if (data == '$') {
inBuf [parseInfo.pbCount++] = data;
parseInfo.state = PSTATE_NMEA_SYNC;
}
break;
case PSTATE_UBX_SYNC1:
if (data == UBX_SYNCCHAR2) {
inBuf [1] = data;
parseInfo.state = PSTATE_UBX_SYNC2;
} else {
UBX_LOGF (true, "parse: PSTATE_UBX_SYNC1: unexpected character, reset.");
resetParser ();
}
break;
case PSTATE_UBX_SYNC2:
if (data == CLS_NAV ||
data == CLS_RXM ||
data == CLS_INF ||
data == CLS_ACK ||
data == CLS_CFG ||
data == CLS_MON ||
data == CLS_AID ||
data == CLS_TIM ||
data == CLS_ESF) {
parseInfo.state = PSTATE_UBX_MSGCLS;
parseInfo.msgClsID = data << 8;
inBuf [2] = data;
} else {
UBX_LOGF (true, "parse: PSTATE_UBX_SYNC2: unexpected character, reset.");
resetParser ();
}
break;
case PSTATE_UBX_MSGCLS:
inBuf [3] = data;
parseInfo.msgClsID |= data;
parseInfo.state = PSTATE_UBX_MSGID;
break;
case PSTATE_UBX_MSGID: // first length byte
inBuf [4] = data;
parseInfo.payloadLen = data;
parseInfo.state = PSTATE_UBX_LEN1;
break;
case PSTATE_UBX_LEN1: // second length byte
inBuf [5] = data;
parseInfo.payloadLen |= data << 8;
// UBX_LOGF (false, "PSTATE_LEN2: payloadLen: ");
// UBX_LOG (true, parseInfo.payloadLen);
parseInfo.state = PSTATE_UBX_LEN2;
break;
case PSTATE_UBX_LEN2: // payload ...
// UBX_LOGF (false, "PSTATE_LEN2: payload, pbCount: ");
// UBX_LOG (true, parseInfo.pbCount);
if (6 + parseInfo.pbCount < __inBufLen) {
inBuf [6 + parseInfo.pbCount] = data;
if (parseInfo.pbCount == parseInfo.payloadLen - 1) {
parseInfo.state = PSTATE_UBX_PAYLOAD;
// UBX_LOGF (true, "PSTATE_LEN2: payload done.");
}
parseInfo.pbCount++;
} else {
UBX_LOGF (true, "parse: PSTATE_UBX_LEN2: msg length exceeds buffer length, reset.");
resetParser ();
}
break;
case PSTATE_UBX_PAYLOAD:
// UBX_LOGF (true, "PSTATE_PAYLOAD: CK_A found.");
if (6 + parseInfo.payloadLen < __inBufLen) {
inBuf [6 + parseInfo.payloadLen] = data;
parseInfo.state = PSTATE_UBX_CHK_A;
} else {
UBX_LOGF (true, "parse: PSTATE_UBX_PAYLOAD: msg length exceeds buffer length, reset.");
resetParser ();
}
break;
case PSTATE_UBX_CHK_A:
// UBX_LOGF (true, "PSTATE_PAYLOAD: CK_B found.");
if (7 + parseInfo.payloadLen < __inBufLen) {
inBuf [7 + parseInfo.payloadLen] = data;
res = handleUbxPacket ();
}
// UBX_LOGF (true, "parse: PSTATE_UBX_CHK_A: msg complete, reset");
resetParser ();
break;
case PSTATE_NMEA_SYNC:
inBuf[parseInfo.pbCount++] = data;
if (data == 'G') {
parseInfo.state = PSTATE_NMEA_IDGP1;
} else if (data == 'P') {
parseInfo.state = PSTATE_NMEA_IDP;
} else {
UBX_LOGF (true, "parse: PSTATE_NMEA_SYNC: unexpected character, reset.");
resetParser ();
}
break;
case PSTATE_NMEA_IDGP1:
inBuf[parseInfo.pbCount++] = data;
if (data == 'P') {
parseInfo.state = PSTATE_NMEA_PAYLOAD;
parseInfo.msgType = UBX_GPMSG;
} else {
UBX_LOGF (true, "parse: PSTATE_NMEA_IDGP1: unexpected character, reset.");
resetParser ();
}
break;
case PSTATE_NMEA_IDP:
inBuf[parseInfo.pbCount++] = data;
if (data == 'U') {
parseInfo.state = PSTATE_NMEA_IDU;
} else {
UBX_LOGF (true, "parse: PSTATE_NMEA_IDP: unexpected character, reset.");
resetParser ();
}
break;
case PSTATE_NMEA_IDU:
inBuf[parseInfo.pbCount++] = data;
if (data == 'B') {
parseInfo.state = PSTATE_NMEA_IDB;
} else {
UBX_LOGF (true, "parse: PSTATE_NMEA_IDU: unexpected character, reset.");
resetParser ();
}
break;
case PSTATE_NMEA_IDB:
inBuf[parseInfo.pbCount++] = data;
if (data == 'X') {
parseInfo.state = PSTATE_NMEA_PAYLOAD;
parseInfo.msgType = UBX_PUBXMSG;
} else {
UBX_LOGF (true, "parse: PSTATE_NMEA_IDB: unexpected character, reset.");
resetParser ();
}
break;
case PSTATE_NMEA_PAYLOAD:
if (parseInfo.pbCount < __inBufLen) {
inBuf[parseInfo.pbCount++] = data;
if (data == 0x0D) {
parseInfo.state = PSTATE_NMEA_CR;
}
} else {
UBX_LOGF (true, "parse: PSTATE_NMEA_PAYLOAD: msg length exceeds buffer length, reset.");
resetParser ();
}
break;
case PSTATE_NMEA_CR:
if (parseInfo.pbCount < __inBufLen) {
inBuf[parseInfo.pbCount++] = data;
if (data == 0x0A) { // NMEA message complete
// reset parseInfo
if (parseInfo.msgType == UBX_PUBXMSG) {
handleNMEA_PUBXMsg();
} else if (parseInfo.msgType == UBX_GPMSG) {
handleNMEA_GPMsg();
}
res = parseInfo.msgType;
// UBX_LOGF (true, "parse: PSTATE_NMEA_CR: msg complete, reset");
resetParser ();
}
} else {
UBX_LOGF (true, "parse: PSTATE_NMEA_PAYLOAD: msg length exceeds buffer length, reset.");
resetParser ();
}
break;
default:
UBX_LOGF (true, "parse: unknown parser state, reset.");
resetParser ();
}
if (false) {
UBX_LOGF (false, " PSTATE: ");
UBX_LOG (false, lastState);
UBX_LOGF (false, " --> ");
UBX_LOG (true, parseInfo.state);
}
return res;
};