-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.c
2448 lines (2391 loc) · 70.7 KB
/
packet.c
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
NAME:
packet.c -- a packet-sniffing engine for reading from GPS devices
DESCRIPTION:
Initial conditions of the problem:
1. We have a file descriptor open for (possibly non-blocking) read. The device
on the other end is sending packets at us.
2. It may require more than one read to gather a packet. Reads may span packet
boundaries.
3. There may be leading garbage before the first packet. After the first
start-of-packet, the input should be well-formed.
The problem: how do we recognize which kind of packet we're getting?
No need to handle Garmin USB binary, we know that type by the fact we're
connected to the Garmin kernel driver. But we need to be able to tell the
others apart and distinguish them from baud barf.
PERMISSIONS
This file is Copyright (c) 2010 by the GPSD project
SPDX-License-Identifier: BSD-2-clause
***************************************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* for htons() */
#include <unistd.h>
#include "bits.h"
#include "gpsd.h"
#include "crc24q.h"
#include "strfuncs.h"
/*
* The packet-recognition state machine. This takes an incoming byte stream
* and tries to segment it into packets. There are four types of packets:
*
* 1) Comments. These begin with # and end with \r\n.
*
* 2) NMEA lines. These begin with $, and with \r\n, and have a checksum.
*
* 3) Checksummed binary packets. These begin with some fixed leader
* character(s), have a length embedded in them, and end with a
* checksum (and possibly some fixed trailing bytes).
*
* 4) ISGPS packets. The input may be a bitstream containing IS-GPS-200
* packets. Each includes a fixed leader byte, a length, and check bits.
* In this case, it is not guaranted that packet starts begin on byte
* bounaries; the recognizer has to run a separate state machine against
* each byte just to achieve synchronization lock with the bitstream.
*
* 5) Un-checksummed binary packets. Like case 3, but without
* a checksum it's much easier to get a false match from garbage.
* The packet recognizer gives checksummed types higher priority.
*
* Adding support for a new GPS protocol typically reqires adding state
* transitions to support whatever binary packet structure it has. The
* goal is for the lexer to be able to cope with arbitrarily mixed packet
* types on the input stream. This is a requirement because (1) sometimes
* gpsd wants to switch a device that supports both NMEA and a binary
* packet protocol to the latter for more detailed reporting, and (b) in
* the presence of device hotplugging, the type of GPS report coming
* in is subject to change at any time.
*
* Caller should consume a packet when it sees one of the *_RECOGNIZED
* states. It's good practice to follow the _RECOGNIZED transition
* with one that recognizes a leader of the same packet type rather
* than dropping back to ground state -- this for example will prevent
* the state machine from hopping between recognizing TSIP and
* EverMore packets that both start with a DLE.
*
* Error handling is brutally simple; any time we see an unexpected
* character, go to GROUND_STATE and reset the machine (except that a
* $ in an NMEA payload only resets back to NMEA_DOLLAR state). Because
* another good packet will usually be along in less than a second
* repeating the same data, Boyer-Moore-like attempts to do parallel
* recognition beyond the headers would make no sense in this
* application, they'd just add complexity.
*
* The NMEA portion of the state machine allows the following talker IDs:
* $GP -- Global Positioning System.
* $GL -- GLONASS, according to IEIC 61162-1
* $GN -- Mixed GPS and GLONASS data, according to IEIC 61162-1
* $BD -- Beidou
* $GB -- Beidou
* $QZ -- QZSS GPS augmentation system
* $II -- Integrated Instrumentation (Raytheon's SeaTalk system).
* $IN -- Integrated Navigation (Garmin uses this).
* $WI -- Weather instrument (Airmar PB200, Radio Ocean ROWIND, Vaisala WXT520).
* $HC -- Heading/compass (Airmar PB200).
* $TI -- Turn indicator (Airmar PB200).
* $EC -- Electronic Chart Display & Information System (ECDIS)
* $SD -- Depth Sounder
* $ST -- $STI, Skytraq Debug Output
* $YX -- Transducer (used by some Airmar equipment including PB100)
* $P -- Vendor-specific sentence
*
* !AB -- NMEA 4.0 Base AIS station
* !AD -- MMEA 4.0 Dependent AIS Base Station
* !AI -- Mobile AIS station
* !AN -- NMEA 4.0 Aid to Navigation AIS station
* !AR -- NMEA 4.0 AIS Receiving Station
* !AX -- NMEA 4.0 Repeater AIS station
* !AS -- NMEA 4.0 Limited Base Station
* !AT -- NMEA 4.0 AIS Transmitting Station
* !BS -- Base AIS station (deprecated in NMEA 4.0)
* !SA -- NMEA 4.0 Physical Shore AIS Station
*/
enum
{
#include "packet_states.h"
};
static char *state_table[] = {
#include "packet_names.h"
};
#define SOH (unsigned char)0x01
#define DLE (unsigned char)0x10
#define STX (unsigned char)0x02
#define ETX (unsigned char)0x03
#ifdef ONCORE_ENABLE
static size_t oncore_payload_cksum_length(unsigned char id1, unsigned char id2)
{
size_t l;
/* For the packet sniffer to not terminate the message due to
* payload data looking like a trailer, the known payload lengths
* including the checksum are given. Return -1 for unknown IDs.
*/
#define ONCTYPE(id2,id3) ((((unsigned int)id2)<<8)|(id3))
/* *INDENT-OFF* */
switch (ONCTYPE(id1,id2)) {
case ONCTYPE('A','b'): l = 10; break; /* GMT offset */
case ONCTYPE('A','w'): l = 8; break; /* time mode */
case ONCTYPE('A','c'): l = 11; break; /* date */
case ONCTYPE('A','a'): l = 10; break; /* time of day */
case ONCTYPE('A','d'): l = 11; break; /* latitude */
case ONCTYPE('A','e'): l = 11; break; /* longitude */
case ONCTYPE('A','f'): l = 15; break; /* height */
case ONCTYPE('E','a'): l = 76; break; /* position/status/data */
case ONCTYPE('A','g'): l = 8; break; /* satellite mask angle */
case ONCTYPE('B','b'): l = 92; break; /* visible satellites status */
case ONCTYPE('B','j'): l = 8; break; /* leap seconds pending */
case ONCTYPE('A','q'): l = 8; break; /* atmospheric correction mode */
case ONCTYPE('A','p'): l = 25; break; /* set user datum / select datum */
/* Command "Ao" gives "Ap" response (select datum) */
case ONCTYPE('C','h'): l = 9; break; /* almanac input ("Cb" response) */
case ONCTYPE('C','b'): l = 33; break; /* almanac output ("Be" response) */
case ONCTYPE('S','z'): l = 8; break; /* system power-on failure */
case ONCTYPE('C','j'): l = 294; break; /* receiver ID */
case ONCTYPE('F','a'): l = 9; break; /* self-test */
case ONCTYPE('C','f'): l = 7; break; /* set-to-defaults */
case ONCTYPE('E','q'): l = 96; break; /* ASCII position */
case ONCTYPE('A','u'): l = 12; break; /* altitide hold height */
case ONCTYPE('A','v'): l = 8; break; /* altitude hold mode */
case ONCTYPE('A','N'): l = 8; break; /* velocity filter */
case ONCTYPE('A','O'): l = 8; break; /* RTCM report mode */
case ONCTYPE('C','c'): l = 80; break; /* ephemeris data input ("Bf") */
case ONCTYPE('C','k'): l = 7; break; /* pseudorng correction inp. ("Ce")*/
/* Command "Ci" (switch to NMEA, GT versions only) has no response */
case ONCTYPE('B','o'): l = 8; break; /* UTC offset status */
case ONCTYPE('A','z'): l = 11; break; /* 1PPS cable delay */
case ONCTYPE('A','y'): l = 11; break; /* 1PPS offset */
case ONCTYPE('A','P'): l = 8; break; /* pulse mode */
case ONCTYPE('A','s'): l = 20; break; /* position-hold position */
case ONCTYPE('A','t'): l = 8; break; /* position-hold mode */
case ONCTYPE('E','n'): l = 69; break; /* time RAIM setup and status */
default:
return 0;
}
/* *INDENT-ON* */
return l - 6; /* Subtract header and trailer. */
}
#endif /* ONCORE_ENABLE */
static bool character_pushback(struct gps_lexer_t *lexer, unsigned int newstate)
/* push back the last character grabbed, setting a specified state */
{
--lexer->inbufptr;
--lexer->char_counter;
lexer->state = newstate;
if (lexer->errout.debug >= LOG_RAW + 2)
{
unsigned char c = *lexer->inbufptr;
gpsd_log(&lexer->errout, LOG_RAW + 2,
"%08ld: character '%c' [%02x] pushed back, state set to %s\n",
lexer->char_counter,
(isprint((int)c) ? c : '.'), c,
state_table[lexer->state]);
}
return false;
}
static bool nextstate(struct gps_lexer_t *lexer, unsigned char c)
{
static int n = 0;
#ifdef RTCM104V2_ENABLE
enum isgpsstat_t isgpsstat;
#endif /* RTCM104V2_ENABLE */
#ifdef SUPERSTAR2_ENABLE
static unsigned char ctmp;
#endif /* SUPERSTAR2_ENABLE */
n++;
switch (lexer->state) {
case GROUND_STATE:
n = 0;
#ifdef STASH_ENABLE
lexer->stashbuflen = 0;
#endif
if (c == '#') {
lexer->state = COMMENT_BODY;
break;
}
#ifdef NMEA0183_ENABLE
if (c == '$') {
lexer->state = NMEA_DOLLAR;
break;
}
if (c == '!') {
lexer->state = NMEA_BANG;
break;
}
#endif /* NMEA0183_ENABLE */
#if defined(TNT_ENABLE) || defined(GARMINTXT_ENABLE) || defined(ONCORE_ENABLE)
if (c == '@') {
#ifdef RTCM104V2_ENABLE
if (rtcm2_decode(lexer, c) == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = AT1_LEADER;
break;
}
#endif
#if defined(SIRF_ENABLE) || defined(SKYTRAQ_ENABLE)
if (c == 0xa0) {
lexer->state = SIRF_LEADER_1;
break;
}
#endif /* SIRF_ENABLE || SKYTRAQ_ENABLE */
#ifdef SUPERSTAR2_ENABLE
if (c == SOH) {
lexer->state = SUPERSTAR2_LEADER;
break;
}
#endif /* SUPERSTAR2_ENABLE */
#if defined(TSIP_ENABLE) || defined(EVERMORE_ENABLE) || defined(GARMIN_ENABLE)
if (c == DLE) {
lexer->state = DLE_LEADER;
break;
}
#endif /* TSIP_ENABLE || EVERMORE_ENABLE || GARMIN_ENABLE */
#ifdef TRIPMATE_ENABLE
if (c == 'A') {
#ifdef RTCM104V2_ENABLE
if (rtcm2_decode(lexer, c) == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = ASTRAL_1;
break;
}
#endif /* TRIPMATE_ENABLE */
#ifdef EARTHMATE_ENABLE
if (c == 'E') {
#ifdef RTCM104V2_ENABLE
if (rtcm2_decode(lexer, c) == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = EARTHA_1;
break;
}
#endif /* EARTHMATE_ENABLE */
#ifdef ZODIAC_ENABLE
if (c == 0xff) {
lexer->state = ZODIAC_LEADER_1;
break;
}
#endif /* ZODIAC_ENABLE */
#ifdef UBLOX_ENABLE
if (c == 0xb5) {
lexer->state = UBX_LEADER_1;
break;
}
#endif /* UBLOX_ENABLE */
#ifdef ITRAX_ENABLE
if (c == '<') {
lexer->state = ITALK_LEADER_1;
break;
}
#endif /* ITRAX_ENABLE */
#ifdef NAVCOM_ENABLE
if (c == 0x02) {
lexer->state = NAVCOM_LEADER_1;
break;
}
#endif /* NAVCOM_ENABLE */
#ifdef GEOSTAR_ENABLE
if (c == 'P') {
lexer->state = GEOSTAR_LEADER_1;
break;
}
#endif /* GEOSTAR_ENABLE */
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
#ifdef RTCM104V3_ENABLE
if (c == 0xD3) {
lexer->state = RTCM3_LEADER_1;
break;
}
#endif /* RTCM104V3_ENABLE */
#ifdef PASSTHROUGH_ENABLE
if (c == '{')
return character_pushback(lexer, JSON_LEADER);
#endif /* PASSTHROUGH_ENABLE */
break;
case COMMENT_BODY:
if (c == '\n')
lexer->state = COMMENT_RECOGNIZED;
else if (!isprint(c))
return character_pushback(lexer, GROUND_STATE);
break;
#ifdef NMEA0183_ENABLE
case NMEA_DOLLAR:
if (c == 'G')
lexer->state = NMEA_PUB_LEAD;
else if (c == 'P') /* vendor sentence */
lexer->state = NMEA_VENDOR_LEAD;
else if (c == 'I') /* Seatalk */
lexer->state = SEATALK_LEAD_1;
else if (c == 'W') /* Weather instrument */
lexer->state = WEATHER_LEAD_1;
else if (c == 'H') /* Heading/compass */
lexer->state = HEADCOMP_LEAD_1;
else if (c == 'T') /* Turn indicator */
lexer->state = TURN_LEAD_1;
else if (c == 'A') /* SiRF Ack */
lexer->state = SIRF_ACK_LEAD_1;
else if (c == 'E') /* ECDIS */
lexer->state = ECDIS_LEAD_1;
else if (c == 'S')
lexer->state = SOUNDER_LEAD_1;
else if (c == 'Y')
lexer->state = TRANSDUCER_LEAD_1;
else if (c == 'B')
lexer->state = BEIDOU_LEAD_1;
else if (c == 'Q')
lexer->state = QZSS_LEAD_1;
#ifdef OCEANSERVER_ENABLE
else if (c == 'C')
lexer->state = NMEA_LEADER_END;
#endif /* OCEANSERVER_ENABLE */
else
(void) character_pushback(lexer, GROUND_STATE);
break;
case NMEA_PUB_LEAD:
/*
* $GP == GPS, $GL = GLONASS only, $GN = mixed GPS and GLONASS,
* according to NMEA (IEIC 61162-1) DRAFT 02/06/2009.
* We have a log from China with a Beidou device using $GB
* rather than $BD.
*/
if (c == 'B' || c == 'P' || c == 'N' || c == 'L')
lexer->state = NMEA_LEADER_END;
else
(void) character_pushback(lexer, GROUND_STATE);
break;
case NMEA_VENDOR_LEAD:
if (c == 'A')
lexer->state = NMEA_PASHR_A;
else if (isalpha(c))
lexer->state = NMEA_LEADER_END;
else
(void) character_pushback(lexer, GROUND_STATE);
break;
/*
* Without the following six states, DLE in a $PASHR can fool the
* sniffer into thinking it sees a TSIP packet. Hilarity ensues.
*/
case NMEA_PASHR_A:
if (c == 'S')
lexer->state = NMEA_PASHR_S;
else if (isalpha(c))
lexer->state = NMEA_LEADER_END;
else
(void) character_pushback(lexer, GROUND_STATE);
break;
case NMEA_PASHR_S:
if (c == 'H')
lexer->state = NMEA_PASHR_H;
else if (isalpha(c))
lexer->state = NMEA_LEADER_END;
else
(void) character_pushback(lexer, GROUND_STATE);
break;
case NMEA_PASHR_H:
if (c == 'R')
lexer->state = NMEA_BINARY_BODY;
else if (isalpha(c))
lexer->state = NMEA_LEADER_END;
else
(void) character_pushback(lexer, GROUND_STATE);
break;
case NMEA_BINARY_BODY:
if (c == '\r')
lexer->state = NMEA_BINARY_CR;
break;
case NMEA_BINARY_CR:
if (c == '\n')
lexer->state = NMEA_BINARY_NL;
else
lexer->state = NMEA_BINARY_BODY;
break;
case NMEA_BINARY_NL:
if (c == '$')
(void) character_pushback(lexer, NMEA_RECOGNIZED);
else
lexer->state = NMEA_BINARY_BODY;
break;
case NMEA_BANG:
if (c == 'A')
lexer->state = AIS_LEAD_1;
else if (c == 'B')
lexer->state = AIS_LEAD_ALT1;
else if (c == 'S')
lexer->state = AIS_LEAD_ALT3;
else
return character_pushback(lexer, GROUND_STATE);
break;
case AIS_LEAD_1:
if (strchr("BDINRSTX", c) != NULL)
lexer->state = AIS_LEAD_2;
else
return character_pushback(lexer, GROUND_STATE);
break;
case AIS_LEAD_2:
if (isalpha(c))
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case AIS_LEAD_ALT1:
if (c == 'S')
lexer->state = AIS_LEAD_ALT2;
else
return character_pushback(lexer, GROUND_STATE);
break;
case AIS_LEAD_ALT2:
if (isalpha(c))
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case AIS_LEAD_ALT3:
if (c == 'A')
lexer->state = AIS_LEAD_ALT4;
else
return character_pushback(lexer, GROUND_STATE);
break;
case AIS_LEAD_ALT4:
if (isalpha(c))
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
#if defined(TNT_ENABLE) || defined(GARMINTXT_ENABLE) || defined(ONCORE_ENABLE)
case AT1_LEADER:
switch (c) {
#ifdef ONCORE_ENABLE
case '@':
lexer->state = ONCORE_AT2;
break;
#endif /* ONCORE_ENABLE */
#ifdef TNT_ENABLE
case '*':
/*
* TNT has similar structure to NMEA packet, '*' before
* optional checksum ends the packet. Since '*' cannot be
* received from GARMIN working in TEXT mode, use this
* difference to tell that this is not GARMIN TEXT packet,
* could be TNT.
*/
lexer->state = NMEA_LEADER_END;
break;
#endif /* TNT_ENABLE */
#if defined(GARMINTXT_ENABLE)
case '\r':
/* stay in this state, next character should be '\n' */
/* in the theory we can stop search here and don't wait for '\n' */
lexer->state = AT1_LEADER;
break;
case '\n':
/* end of packet found */
lexer->state = GTXT_RECOGNIZED;
break;
#endif /* GARMINTXT_ENABLE */
default:
if (!isprint(c))
return character_pushback(lexer, GROUND_STATE);
}
break;
#endif /* defined(TNT_ENABLE) || defined(GARMINTXT_ENABLE) || defined(ONCORE_ENABLE) */
case NMEA_LEADER_END:
if (c == '\r')
lexer->state = NMEA_CR;
else if (c == '\n')
/* not strictly correct, but helps for interpreting logfiles */
lexer->state = NMEA_RECOGNIZED;
else if (c == '$') {
#ifdef STASH_ENABLE
(void) character_pushback(lexer, STASH_RECOGNIZED);
#else
(void) character_pushback(lexer, GROUND_STATE);
#endif
} else if (!isprint(c))
(void) character_pushback(lexer, GROUND_STATE);
break;
case NMEA_CR:
if (c == '\n')
lexer->state = NMEA_RECOGNIZED;
/*
* There's a GPS called a Jackson Labs Firefly-1a that emits \r\r\n
* at the end of each sentence. Don't be confused by this.
*/
else if (c == '\r')
lexer->state = NMEA_CR;
else
(void) character_pushback(lexer, GROUND_STATE);
break;
case NMEA_RECOGNIZED:
if (c == '#')
lexer->state = COMMENT_BODY;
else if (c == '$')
lexer->state = NMEA_DOLLAR;
else if (c == '!')
lexer->state = NMEA_BANG;
#ifdef UBLOX_ENABLE
else if (c == 0xb5) /* LEA-5H can and will output NMEA and UBX back to back */
lexer->state = UBX_LEADER_1;
#endif
#ifdef PASSTHROUGH_ENABLE
else if (c == '{')
return character_pushback(lexer, JSON_LEADER);
#endif /* PASSTHROUGH_ENABLE */
else
return character_pushback(lexer, GROUND_STATE);
break;
case SEATALK_LEAD_1:
if (c == 'I' || c == 'N') /* II or IN are accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case WEATHER_LEAD_1:
if (c == 'I') /* Weather instrument leader accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case HEADCOMP_LEAD_1:
if (c == 'C') /* Heading/compass leader accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case TURN_LEAD_1:
if (c == 'I') /* Turn indicator leader accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case ECDIS_LEAD_1:
if (c == 'C') /* ECDIS leader accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case SOUNDER_LEAD_1:
if (c == 'D') /* Depth-sounder leader accepted */
lexer->state = NMEA_LEADER_END;
#ifdef SKYTRAQ_ENABLE
else if (c == 'T') /* $ST leader accepted, to $STI */
lexer->state = NMEA_LEADER_END;
#endif /* SKYTRAQ_ENABLE */
else
return character_pushback(lexer, GROUND_STATE);
break;
case TRANSDUCER_LEAD_1:
if (c == 'X') /* Transducer leader accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case BEIDOU_LEAD_1:
if (c == 'D') /* Beidou leader accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
case QZSS_LEAD_1:
if (c == 'Z') /* QZSS leader accepted */
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
#ifdef TRIPMATE_ENABLE
case ASTRAL_1:
if (c == 'S') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = ASTRAL_2;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case ASTRAL_2:
if (c == 'T') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = ASTRAL_3;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case ASTRAL_3:
if (c == 'R') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = ASTRAL_5;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case ASTRAL_4:
if (c == 'A') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = ASTRAL_2;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case ASTRAL_5:
if (c == 'L') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = NMEA_RECOGNIZED;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
#endif /* TRIPMATE_ENABLE */
#ifdef EARTHMATE_ENABLE
case EARTHA_1:
if (c == 'A') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = EARTHA_2;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case EARTHA_2:
if (c == 'R') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = EARTHA_3;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case EARTHA_3:
if (c == 'T') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = EARTHA_4;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case EARTHA_4:
if (c == 'H') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = EARTHA_5;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
case EARTHA_5:
if (c == 'A') {
#ifdef RTCM104V2_ENABLE
if ((isgpsstat = rtcm2_decode(lexer, c)) == ISGPS_SYNC) {
lexer->state = RTCM2_SYNC_STATE;
break;
} else if (isgpsstat == ISGPS_MESSAGE) {
lexer->state = RTCM2_RECOGNIZED;
break;
}
#endif /* RTCM104V2_ENABLE */
lexer->state = NMEA_RECOGNIZED;
} else
(void) character_pushback(lexer, GROUND_STATE);
break;
#endif /* EARTHMATE_ENABLE */
case SIRF_ACK_LEAD_1:
if (c == 'c')
lexer->state = SIRF_ACK_LEAD_2;
else if (c == 'I')
lexer->state = AIS_LEAD_2;
else
return character_pushback(lexer, GROUND_STATE);
break;
case SIRF_ACK_LEAD_2:
if (c == 'k')
lexer->state = NMEA_LEADER_END;
else
return character_pushback(lexer, GROUND_STATE);
break;
#endif /* NMEA0183_ENABLE */
#if defined(SIRF_ENABLE) || defined(SKYTRAQ_ENABLE)
case SIRF_LEADER_1:
# ifdef SIRF_ENABLE
/* SIRF leads with 0xA0,0xA2 */
if (c == 0xa2)
lexer->state = SIRF_LEADER_2;
else
# endif /* SIRF_ENABLE */
# ifdef SKYTRAQ_ENABLE
/* Skytraq leads with 0xA0,0xA1 */
if (c == 0xa1)
lexer->state = SKY_LEADER_2;
else
# endif /* SKYTRAQ_ENABLE */
return character_pushback(lexer, GROUND_STATE);
break;
#endif /* SIRF_ENABLE || SKYTRAQ_ENABLE */
#ifdef SIRF_ENABLE
case SIRF_LEADER_2:
lexer->length = (size_t) (c << 8);
lexer->state = SIRF_LENGTH_1;
break;
case SIRF_LENGTH_1:
lexer->length += c + 2;
if (lexer->length <= MAX_PACKET_LENGTH)
lexer->state = SIRF_PAYLOAD;
else
return character_pushback(lexer, GROUND_STATE);
break;
case SIRF_PAYLOAD:
if (--lexer->length == 0)
lexer->state = SIRF_DELIVERED;
break;
case SIRF_DELIVERED:
if (c == 0xb0)
lexer->state = SIRF_TRAILER_1;
else
return character_pushback(lexer, GROUND_STATE);
break;
case SIRF_TRAILER_1:
if (c == 0xb3)
lexer->state = SIRF_RECOGNIZED;
else
return character_pushback(lexer, GROUND_STATE);
break;
case SIRF_RECOGNIZED:
if (c == 0xa0)
lexer->state = SIRF_LEADER_1;
else
return character_pushback(lexer, GROUND_STATE);
break;
#endif /* SIRF_ENABLE */
#ifdef SKYTRAQ_ENABLE
case SKY_LEADER_2:
/* MSB of length is first */
lexer->length = (size_t) (c << 8);
lexer->state = SKY_LENGTH_1;
break;
case SKY_LENGTH_1:
/* Skytraq length can be any 16 bit number, except 0 */
lexer->length += c;
if ( 0 == lexer->length )
return character_pushback(lexer, GROUND_STATE);
if (lexer->length > MAX_PACKET_LENGTH)
return character_pushback(lexer, GROUND_STATE);
lexer->state = SKY_PAYLOAD;
break;
case SKY_PAYLOAD:
if ( 00 == --lexer->length)
lexer->state = SKY_DELIVERED;
break;
case SKY_DELIVERED:
if ( lexer->errout.debug >= LOG_RAW+1) {
char scratchbuf[MAX_PACKET_LENGTH*4+1];
gpsd_log(&lexer->errout, LOG_RAW+1,
"Skytraq = %s\n",
gpsd_packetdump(scratchbuf, sizeof(scratchbuf),
(char *)lexer->inbuffer,
lexer->inbufptr - (unsigned char *)lexer->inbuffer));
}
{
unsigned char csum = 0;
for (n = 4;
(unsigned char *)(lexer->inbuffer + n) < lexer->inbufptr - 1;
n++)
csum ^= lexer->inbuffer[n];
if (csum != c) {
gpsd_log(&lexer->errout, LOG_IO,
"Skytraq bad checksum 0x%hhx, expecting 0x%x\n",
csum, c);
lexer->state = GROUND_STATE;
break;
}
}
lexer->state = SKY_CSUM;
break;
case SKY_CSUM:
if ( 0x0d != c)
return character_pushback(lexer, GROUND_STATE);
lexer->state = SKY_TRAILER_1;
break;
case SKY_TRAILER_1:
if ( 0x0a != c)
return character_pushback(lexer, GROUND_STATE);
lexer->state = SKY_RECOGNIZED;
break;
case SKY_RECOGNIZED:
if ( 0xa0 != c)
return character_pushback(lexer, GROUND_STATE);
lexer->state = SIRF_LEADER_1;
break;
#endif /* SKYTRAQ */
#ifdef SUPERSTAR2_ENABLE
case SUPERSTAR2_LEADER:
ctmp = c;
lexer->state = SUPERSTAR2_ID1;
break;
case SUPERSTAR2_ID1:
if ((ctmp ^ 0xff) == c)
lexer->state = SUPERSTAR2_ID2;
else
return character_pushback(lexer, GROUND_STATE);
break;
case SUPERSTAR2_ID2:
lexer->length = (size_t) c; /* how many data bytes follow this byte */
if (lexer->length)
lexer->state = SUPERSTAR2_PAYLOAD;
else
lexer->state = SUPERSTAR2_CKSUM1; /* no data, jump to checksum */
break;
case SUPERSTAR2_PAYLOAD:
if (--lexer->length == 0)
lexer->state = SUPERSTAR2_CKSUM1;
break;
case SUPERSTAR2_CKSUM1:
lexer->state = SUPERSTAR2_CKSUM2;
break;
case SUPERSTAR2_CKSUM2:
lexer->state = SUPERSTAR2_RECOGNIZED;
break;
case SUPERSTAR2_RECOGNIZED:
if (c == SOH)
lexer->state = SUPERSTAR2_LEADER;
else
return character_pushback(lexer, GROUND_STATE);
break;
#endif /* SUPERSTAR2_ENABLE */
#ifdef ONCORE_ENABLE
case ONCORE_AT2:
if (isupper(c)) {
lexer->length = (size_t) c;
lexer->state = ONCORE_ID1;
} else
return character_pushback(lexer, GROUND_STATE);
break;
case ONCORE_ID1:
if (isalpha(c)) {
lexer->length =
oncore_payload_cksum_length((unsigned char)lexer->length, c);
if (lexer->length != 0) {
lexer->state = ONCORE_PAYLOAD;
break;
}
} else
return character_pushback(lexer, GROUND_STATE);
break;
case ONCORE_PAYLOAD:
if (--lexer->length == 0)
lexer->state = ONCORE_CHECKSUM;
break;
case ONCORE_CHECKSUM:
if (c != '\r')
return character_pushback(lexer, GROUND_STATE);
else
lexer->state = ONCORE_CR;
break;
case ONCORE_CR:
if (c == '\n')
lexer->state = ONCORE_RECOGNIZED;
else
lexer->state = ONCORE_PAYLOAD;
break;
case ONCORE_RECOGNIZED:
if (c == '@')
lexer->state = AT1_LEADER;
else
return character_pushback(lexer, GROUND_STATE);
break;
#endif /* ONCORE_ENABLE */
#if defined(TSIP_ENABLE) || defined(EVERMORE_ENABLE) || defined(GARMIN_ENABLE)
case DLE_LEADER:
#ifdef EVERMORE_ENABLE
if (c == STX) {
lexer->state = EVERMORE_LEADER_2;
break;
}
#endif /* EVERMORE_ENABLE */