Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gpsd/gpsd-3.28.1~dev/drivers/drivers.c
Line
Count
Source
1
/*
2
 * This file is Copyright by the GPSD project
3
 * SPDX-License-Identifier: BSD-2-clause
4
 */
5
6
#include "../include/gpsd_config.h"  // must be before all includes
7
8
#include <assert.h>
9
#include <stdarg.h>
10
#include <stdbool.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <unistd.h>
15
16
#include "../include/bits.h"     // for getbeu16(), to extract big-endian words
17
#include "../include/compiler.h" // for FALLTHROUGH
18
#include "../include/gpsd.h"
19
#include "../include/strfuncs.h"
20
21
// This handles only bad, comment, and maybe NMEA packets.
22
gps_mask_t generic_parse_input(struct gps_device_t *session)
23
0
{
24
0
    if (BAD_PACKET == session->lexer.type ||
25
0
        COMMENT_PACKET == session->lexer.type) {
26
        // ignore bad packets and comment packets
27
0
        return 0;
28
0
    }
29
30
0
    if (NMEA_PACKET == session->lexer.type) {
31
0
        const struct gps_type_t **dp;
32
0
        gps_mask_t st = 0;
33
0
        char *sentence = (char *)session->lexer.outbuffer;
34
0
        size_t slen = strnlen(sentence, sizeof(session->lexer.outbuffer));
35
0
        char buf[1024];
36
37
        // don't log any binary!
38
0
        GPSD_LOG(LOG_IO, &session->context->errout, "<= GPS: \"%s\"\n",
39
0
                 gps_visibilize(buf, sizeof(buf), sentence, slen));
40
41
0
        if (0 == (st = nmea_parse(sentence, session))) {
42
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
43
0
                     "bad sentence: \"%s\"\n", buf);
44
0
            return 0;
45
0
        }
46
0
        for (dp = gpsd_drivers; *dp; dp++) {
47
0
            char *trigger = (*dp)->trigger;
48
49
0
            if (NULL != trigger &&
50
0
                str_starts_with(sentence, trigger)) {
51
0
                GPSD_LOG(LOG_PROG, &session->context->errout,
52
0
                         "found trigger string %s.\n", trigger);
53
0
                if (*dp != session->device_type) {
54
0
                    (void)gpsd_switch_driver(session, (*dp)->type_name);
55
0
                    if (NULL != session->device_type &&
56
0
                        NULL != session->device_type->event_hook) {
57
0
                        session->device_type->event_hook(session,
58
0
                                                         EVENT_TRIGGERMATCH);
59
0
                        st |= DEVICEID_SET;
60
0
                    }
61
0
                }
62
0
            }
63
0
        }
64
0
        return st;
65
0
    }
66
67
0
    GPSD_LOG(LOG_SHOUT, &session->context->errout,
68
0
             "packet type %d fell through (should never happen): %s.\n",
69
0
             session->lexer.type, gpsd_prettydump(session));
70
0
    return 0;
71
0
}
72
73
/**************************************************************************
74
*
75
* Generic driver -- make no assumptions about the device type
76
*
77
**************************************************************************/
78
79
// *INDENT-OFF*
80
const struct gps_type_t driver_unknown = {
81
    .type_name      = "Unknown",        // full name of type
82
    .packet_type    = COMMENT_PACKET,   // associated lexer packet type
83
    .flags          = DRIVER_NOFLAGS,   // no flags set
84
    .trigger        = NULL,             // it's the default
85
    .channels       = 12,               // consumer-grade GPS
86
    .probe_detect   = NULL,             // no probe
87
    .get_packet     = packet_get1,      // use generic packet getter
88
    .parse_packet   = generic_parse_input,      // how to interpret a packet
89
    .rtcm_writer    = NULL,             // write RTCM data straight
90
    .init_query     = NULL,             // non-perturbing initial query
91
    .event_hook     = NULL,             // lifetime event handler
92
    .speed_switcher = NULL,             // no speed switcher
93
    .mode_switcher  = NULL,             // no mode switcher
94
    .rate_switcher  = NULL,             // no sample-rate switcher
95
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
96
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
97
    .control_send   = NULL,             // how to send control strings
98
    .time_offset     = NULL,            // no method for NTP fudge factor
99
};
100
// *INDENT-ON*
101
102
/**************************************************************************
103
*
104
* NMEA 0183
105
*
106
* This is separate from the 'unknown' driver because we don't want to
107
* ship NMEA subtype probe strings to a device until we've seen at
108
* least one NMEA packet.  This avoids spamming devices that might
109
* actually be USB modems or other things in USB device class FF that
110
* just happen to have one of 'our' adaptor chips in front of them.
111
*
112
**************************************************************************/
113
114
static void nmea_event_hook(struct gps_device_t *session, event_t event)
115
0
{
116
0
    if (session->context->readonly) {
117
0
        return;
118
0
    }
119
    /*
120
     * This is where we try to tickle NMEA devices into revealing their
121
     * inner natures.
122
     */
123
0
    if (event == EVENT_CONFIGURE) {
124
        /*
125
         * The reason for splitting these probes up by packet sequence
126
         * number, interleaving them with the first few packet receives,
127
         * is because many generic-NMEA devices get confused if you send
128
         * too much at them in one go.
129
         *
130
         * A fast response to an early probe will change drivers so the
131
         * later ones won't be sent at all.  Thus, for best overall
132
         * performance, order these to probe for the most popular types
133
         * soonest.
134
         *
135
         * Note: don't make the trigger strings identical to the probe,
136
         * because some NMEA devices (notably SiRFs) will just echo
137
         * unknown strings right back at you. A useful dodge is to append
138
         * a comma to the trigger, because that won't be in the response
139
         * unless there is actual following data.
140
         */
141
0
        switch (session->lexer.counter) {
142
0
        case 0:
143
            // probe for Garmin serial GPS -- expect $PGRMC followed by data
144
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
145
0
                     "=> Probing for Garmin NMEA\n");
146
0
            (void)nmea_send(session, "$PGRMCE");
147
0
            break;
148
0
#ifdef SIRF_ENABLE
149
0
                case 1:
150
                    /*
151
                     * We used to try to probe for SiRF by issuing
152
                     * "$PSRF105,1" and expecting "$Ack Input105.".  But it
153
                     * turns out this only works for SiRF-IIs; SiRF-I and
154
                     * SiRF-III don't respond.  Sadly, the MID132 binary
155
                     * request for firmware version is ignored in NMEA mode.
156
                     * Thus the only reliable probe is to try to flip the SiRF
157
                     * into binary mode, cluing in the library to revert it on
158
                     * close.
159
                     *
160
                     * SiRFs dominate the consumer-grade GPS-mouse market, so
161
                     * we used to put this test first. Unfortunately this
162
                     * causes problems for gpsctl, as it cannot select the
163
                     * NMEA driver without switching the device back to binary
164
                     * mode!  Fix this if we ever find a nondisruptive probe
165
                     * string.
166
                     */
167
0
                    GPSD_LOG(LOG_PROG, &session->context->errout,
168
0
                             "=> Probing for SiRF\n");
169
0
                    (void)nmea_send(session,
170
0
                                    "$PSRF100,0,%d,%d,%d,0",
171
0
                                    session->gpsdata.dev.baudrate,
172
0
                                    9 - session->gpsdata.dev.stopbits,
173
0
                                    session->gpsdata.dev.stopbits);
174
0
                    break;
175
0
#endif  // SIRF_ENABLE
176
0
        case 2:
177
            // probe for the FV-18 -- expect $PFEC,GPint followed by data
178
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
179
0
                     "=> Probing for FV-18\n");
180
0
            (void)nmea_send(session, "$PFEC,GPint");
181
0
            break;
182
0
        case 3:
183
            // probe for the Trimble Copernicus
184
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
185
0
                     "=> Probing for Trimble Copernicus\n");
186
0
            (void)nmea_send(session, "$PTNLSNM,0139,01");
187
0
            break;
188
0
#ifdef EVERMORE_ENABLE
189
0
                case 4:
190
0
                    GPSD_LOG(LOG_PROG, &session->context->errout,
191
0
                             "=> Probing for Evermore\n");
192
                    // FIXME: not passive compatible
193
                    /* Enable checksum and GGA(1s), GLL(0s), GSA(1s), GSV(1s),
194
                     * RMC(1s), VTG(0s), PEMT101(0s)
195
                     * EverMore will reply with:
196
                     * \x10\x02\x04\x38\x8E\xC6\x10\x03 */
197
0
                    (void)gpsd_write(session,
198
0
                                     "\x10\x02\x12\x8E\x7F\x01\x01\x00\x01\x01\x01"
199
0
                                     "\x00\x00\x00\x00\x00\x00\x00\x00\x13\x10\x03",
200
0
                                     22);
201
0
                    break;
202
0
#endif  // EVERMORE_ENABLE
203
0
#ifdef GPSCLOCK_ENABLE
204
0
                case 5:
205
                    /* probe for Furuno Electric GH-79L4-N (GPSClock);
206
                     * expect $PFEC,GPssd */
207
0
                    GPSD_LOG(LOG_PROG, &session->context->errout,
208
0
                             "=> Probing for GPSClock\n");
209
0
                    (void)nmea_send(session, "$PFEC,GPsrq");
210
0
                    break;
211
0
#endif  // GPSCLOCK_ENABLE
212
0
        case 6:
213
            // probe for Ashtech -- expect $PASHR,RID
214
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
215
0
                     "=> Probing for Ashtech\n");
216
0
            (void)nmea_send(session, "$PASHQ,RID");
217
0
            break;
218
0
        case 7:
219
            // probe for UBX
220
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
221
0
                     "=> Probing for UBX\n");
222
            // query port configuration UBX-MON-VER
223
0
            (void)ubx_write(session, 0x0a, 0x04, NULL, 0);
224
            // query port configuration UBX-CFG-PRT
225
0
            (void)ubx_write(session, 0x06, 0x00, NULL, 0);
226
0
            break;
227
0
        case 8:
228
            // probe for MTK-3301 -- expect $PMTK705
229
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
230
0
                     "=> Probing for MediaTek\n");
231
0
            (void)nmea_send(session, "$PMTK605");
232
0
            break;
233
0
#ifdef GREIS_ENABLE
234
0
                case 9:
235
                    // probe for Javad GREIS -- expect reply with JAVAD
236
0
                    GPSD_LOG(LOG_PROG, &session->context->errout,
237
0
                             "=> Probing for Javad GREIS\n");
238
                    // TODO: confirm this actually gets JAVAD response
239
0
                    (void)nmea_send(session, "print,/par/rcv/vendor");
240
0
                    break;
241
0
#endif  // GREIS_ENABLE
242
0
        case 10:
243
            // probe for ALLYSTAR
244
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
245
0
                     "=> Probing for ALLYSTAR\n");
246
            // query version MON-VER
247
0
            (void)ally_write(session, 0x0a, 0x04, NULL, 0);
248
0
            break;
249
0
        case 11:
250
            // probe for CASIC
251
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
252
0
                     "=> Probing for CASIC\n");
253
            // query version MON-VER
254
0
            (void)casic_write(session, 0x0a, 0x04, NULL, 0);
255
0
            break;
256
0
        default:
257
0
            break;
258
0
        }
259
0
    }
260
0
}
261
262
// *INDENT-OFF*
263
const struct gps_type_t driver_nmea0183 = {
264
    .type_name      = "NMEA0183",       // full name of type
265
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
266
    .flags          = DRIVER_NOFLAGS,   // remember this
267
    .trigger        = NULL,             // it's the default
268
    .channels       = 12,               // consumer-grade GPS
269
    .probe_detect   = NULL,             // no probe
270
    .get_packet     = packet_get1,      // use generic packet getter
271
    .parse_packet   = generic_parse_input,      // how to interpret a packet
272
    .rtcm_writer    = gpsd_write,       // write RTCM data straight
273
    .init_query     = NULL,             // non-perturbing initial query
274
    .event_hook     = nmea_event_hook,  // lifetime event handler
275
    .speed_switcher = NULL,             // no speed switcher
276
    .mode_switcher  = NULL,             // maybe switchable if it was a SiRF
277
    .rate_switcher  = NULL,             // no sample-rate switcher
278
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
279
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
280
    .control_send   = nmea_write,       // how to send control strings
281
    .time_offset     = NULL,            // no method for NTP fudge factor
282
};
283
// *INDENT-ON*
284
285
#if defined(GARMIN_ENABLE)
286
/**************************************************************************
287
*
288
* Garmin NMEA
289
*
290
**************************************************************************/
291
292
// only does anything in one direction, going to Garmin binary driver
293
    static void garmin_mode_switch(struct gps_device_t *session, int mode)
294
0
    {
295
0
        struct timespec delay;
296
297
0
        if (mode == MODE_BINARY) {
298
0
            (void)nmea_send(session, "$PGRMC1,1,2,1,,,,2,W,N");
299
0
            (void)nmea_send(session, "$PGRMI,,,,,,,R");
300
            // wait 333 uSec, standard Garmin settling time
301
0
            delay.tv_sec = 0;
302
0
            delay.tv_nsec = 333000L;
303
0
            nanosleep(&delay, NULL);
304
0
        }
305
0
    }
306
307
    static void garmin_nmea_event_hook(struct gps_device_t *session,
308
                                       event_t event)
309
0
    {
310
0
        if (session->context->readonly) {
311
0
            return;
312
0
        }
313
314
0
        if (event == EVENT_DRIVER_SWITCH) {
315
            // forces a reconfigure as the following packets come in
316
0
            session->lexer.counter = 0;
317
0
        }
318
0
        if (session->context->passive) {
319
0
            return;
320
0
        }
321
0
        if (event == EVENT_CONFIGURE) {
322
            /*
323
             * And here's that reconfigure.  It's split up like this because
324
             * receivers like the Garmin GPS-10 don't handle having having a lot
325
             * of
326
             * probes shoved at them very well.
327
             */
328
0
            switch (session->lexer.counter) {
329
0
            case 0:
330
                /* reset some config, AutoFix, WGS84, PPS
331
                 * Set the PPS pulse length to 40ms which leaves the Garmin
332
                 * 18-5hz
333
                 * with a 160ms low state.
334
                 * NOTE: new PPS only takes effect after next power cycle
335
                 */
336
0
                (void)nmea_send(session, "$PGRMC,A,,100,,,,,,A,,1,2,1,30");
337
0
                break;
338
0
            case 1:
339
                // once a sec, no averaging, NMEA 2.3, WAAS
340
0
                (void)nmea_send(session, "$PGRMC1,1,1,1,,,,2,W,N");
341
0
                break;
342
0
            case 2:
343
                // get some more config info
344
0
                (void)nmea_send(session, "$PGRMC1E");
345
0
                break;
346
0
            case 3:
347
                // turn off all output except GGA
348
0
                (void)nmea_send(session, "$PGRMO,,2");
349
0
                (void)nmea_send(session, "$PGRMO,GPGGA,1");
350
0
                break;
351
0
            case 4:
352
                // enable GPGGA, GPGSA, GPGSV, GPRMC on Garmin serial GPS
353
0
                (void)nmea_send(session, "$PGRMO,GPGSA,1");
354
0
                break;
355
0
            case 5:
356
0
                (void)nmea_send(session, "$PGRMO,GPGSV,1");
357
0
                break;
358
0
            case 6:
359
0
                (void)nmea_send(session, "$PGRMO,GPRMC,1");
360
0
                break;
361
0
            case 7:
362
0
                (void)nmea_send(session, "$PGRMO,PGRME,1");
363
0
                break;
364
0
            }
365
0
        }
366
0
    }
367
368
// *INDENT-OFF*
369
const struct gps_type_t driver_garmin = {
370
    .type_name      = "Garmin NMEA",    // full name of type
371
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
372
    .flags          = DRIVER_STICKY,    // remember this
373
    .trigger        = "$PGRMC,",        // Garmin private
374
    .channels       = 12,               // not used by this driver
375
    .probe_detect   = NULL,             // no probe
376
    .get_packet     = packet_get1,      // use generic packet getter
377
    .parse_packet   = generic_parse_input,      // how to interpret a packet
378
    .rtcm_writer    = NULL,             // some do, some don't, skip for now
379
    .init_query     = NULL,             // non-perturbing initial query
380
    .event_hook     = garmin_nmea_event_hook,   // lifetime event handler
381
    .speed_switcher = NULL,                     // no speed switcher
382
    .mode_switcher  = garmin_mode_switch,       // mode switcher
383
    .rate_switcher  = NULL,             // no sample-rate switcher
384
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
385
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
386
    .control_send   = nmea_write,       // how to send control strings
387
    .time_offset     = NULL,            // no method for NTP fudge factor
388
};
389
// *INDENT-ON*
390
#endif  // GARMIN_ENABLE
391
392
/**************************************************************************
393
*
394
* Inertial Sense
395
* https://inertialsense.com/
396
* Note that many of the IS "NMEA" messages are too long, non-standard,
397
* or just broken.
398
*
399
**************************************************************************/
400
static void is_event_hook(struct gps_device_t *session, event_t event)
401
0
{
402
0
    if (session->context->readonly) {
403
0
        return;
404
0
    }
405
406
0
    if (event == EVENT_WAKEUP) {
407
        /* probe for device, which is quiet by default
408
         * $INFO is not a valid NMEA message type, public or proprietary */
409
0
        (void)nmea_send(session, "$INFO");
410
0
    }
411
412
0
    if (session->context->passive) {
413
0
        return;
414
0
    }
415
0
    if (event == EVENT_IDENTIFIED) {
416
        // enable some "NMEA", at 5Hz
417
0
        (void)nmea_send(session, "$ASCE,0,6,1,7,1,8,1,10,1,11,1,15,1");
418
0
    }
419
0
}
420
421
// *INDENT-OFF*
422
const struct gps_type_t driver_is = {
423
    .type_name      = "InertialSense",  // full name of type
424
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
425
    .flags          = DRIVER_STICKY,    // remember this
426
    .trigger        = NULL,
427
    .channels       = 24,               // not used, unknown channels
428
    .probe_detect   = NULL,             // no probe
429
    .get_packet     = packet_get1,      // how to get a packet
430
    .parse_packet   = generic_parse_input,      // how to interpret a packet
431
    .rtcm_writer    = gpsd_write,       // write RTCM data straight
432
    .init_query     = NULL,             // non-perturbing initial query
433
    .event_hook     = is_event_hook,    // lifetime event handler
434
    .speed_switcher = NULL,             // no speed switcher
435
    .mode_switcher  = NULL,             // no mode switcher
436
    .rate_switcher  = NULL,             // no sample-rate switcher
437
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
438
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
439
    .control_send   = nmea_write,       // how to send control strings
440
    .time_offset     = NULL,            // no method for NTP fudge factor
441
};
442
/**************************************************************************
443
 *
444
 * Ashtech (then Thales, now Magellan Professional) Receivers
445
 *
446
 **************************************************************************/
447
448
static void ashtech_event_hook(struct gps_device_t *session, event_t event)
449
0
{
450
0
    if (session->context->readonly) {
451
0
        return;
452
0
    }
453
454
0
    if (event == EVENT_WAKEUP) {
455
0
        (void)nmea_send(session, "$PASHQ,RID");
456
0
    }
457
458
0
    if (session->context->passive) {
459
0
        return;
460
0
    }
461
0
    if (event == EVENT_IDENTIFIED) {
462
        // turn WAAS on. can't hurt...
463
0
        (void)nmea_send(session, "$PASHS,WAS,ON");
464
        // reset to known output state
465
0
        (void)nmea_send(session, "$PASHS,NME,ALL,A,OFF");
466
        // then turn on some useful sentences
467
#ifdef __future__
468
        // we could parse these, but they're oversize so they get dropped
469
        (void)nmea_send(session, "$PASHS,NME,POS,A,ON");
470
        (void)nmea_send(session, "$PASHS,NME,SAT,A,ON");
471
#else
472
0
        (void)nmea_send(session, "$PASHS,NME,GGA,A,ON");
473
0
        (void)nmea_send(session, "$PASHS,NME,GSA,A,ON");
474
0
        (void)nmea_send(session, "$PASHS,NME,GSV,A,ON");
475
0
        (void)nmea_send(session, "$PASHS,NME,RMC,A,ON");
476
0
#endif
477
0
        (void)nmea_send(session, "$PASHS,NME,ZDA,A,ON");
478
0
    }
479
0
}
480
481
const struct gps_type_t driver_ashtech = {
482
    .type_name      = "Ashtech",        // full name of type
483
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
484
    .flags          = DRIVER_STICKY,    // remember this
485
    .trigger        = "$PASHR,RID,",    // Ashtech receivers respond thus
486
    .channels       = 24,               // not used, GG24 has 24 channels
487
    .probe_detect   = NULL,             // no probe
488
    .get_packet     = packet_get1,      // how to get a packet
489
    .parse_packet   = generic_parse_input,      // how to interpret a packet
490
    .rtcm_writer    = gpsd_write,       // write RTCM data straight
491
    .init_query     = NULL,             // non-perturbing initial query
492
    .event_hook     = ashtech_event_hook, // lifetime event handler
493
    .speed_switcher = NULL,             // no speed switcher
494
    .mode_switcher  = NULL,             // no mode switcher
495
    .rate_switcher  = NULL,             // no sample-rate switcher
496
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
497
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
498
    .control_send   = nmea_write,       // how to send control strings
499
    .time_offset     = NULL,            // no method for NTP fudge factor
500
};
501
// *INDENT-ON*
502
503
#ifdef FV18_ENABLE
504
/**************************************************************************
505
*
506
* FV18 -- uses 2 stop bits, needs to be told to send GSAs
507
*
508
**************************************************************************/
509
510
    static void fv18_event_hook(struct gps_device_t *session, event_t event)
511
0
    {
512
0
        if (session->context->readonly) {
513
0
            return;
514
0
        }
515
516
        /*
517
         * Tell an FV18 to send GSAs so we'll know if 3D is accurate.
518
         * Suppress GLL and VTG.  Enable ZDA so dates will be accurate for
519
         * replay.
520
         * It's possible we might not need to redo this on event_reactivate,
521
         * but doing so is safe and cheap.
522
         */
523
0
        if (event == EVENT_IDENTIFIED ||
524
0
            event == EVENT_REACTIVATE) {
525
0
            (void)nmea_send(session,
526
0
                            "$PFEC,GPint,GSA01,DTM00,ZDA01,RMC01,GLL00,VTG00,GSV05");
527
0
        }
528
0
    }
529
530
// *INDENT-OFF*
531
const struct gps_type_t driver_fv18 = {
532
    .type_name      = "San Jose Navigation FV18",       // full name of type
533
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
534
    .flags          = DRIVER_STICKY,    // remember this
535
    .trigger        = "$PFEC,GPint,",   // FV18s should echo the probe
536
    .channels       = 12,               // not used by this driver
537
    .probe_detect   = NULL,             // no probe
538
    .get_packet     = packet_get1,      // how to get a packet
539
    .parse_packet   = generic_parse_input,      // how to interpret a packet
540
    .rtcm_writer    = gpsd_write,       // write RTCM data straight
541
    .init_query     = NULL,             // non-perturbing initial query
542
    .event_hook     = fv18_event_hook,  // lifetime event handler
543
    .speed_switcher = NULL,             // no speed switcher
544
    .mode_switcher  = NULL,             // no mode switcher
545
    .rate_switcher  = NULL,             // no sample-rate switcher
546
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
547
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
548
    .control_send   = nmea_write,       // how to send control strings
549
    .time_offset     = NULL,            // no method for NTP fudge factor
550
};
551
// *INDENT-ON*
552
#endif  // FV18_ENABLE
553
554
#ifdef GPSCLOCK_ENABLE
555
/**************************************************************************
556
*
557
* Furuno Electric GPSClock (GH-79L4)
558
*
559
**************************************************************************/
560
561
/*
562
 * Based on http://www.tecsys.de/fileadmin/user_upload/pdf/gh79_1an_intant.pdf
563
 */
564
565
// *INDENT-OFF*
566
const struct gps_type_t driver_gpsclock = {
567
    .type_name      = "Furuno Electric GH-79L4",        // full name of type
568
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
569
    .flags          = DRIVER_STICKY,    // remember this
570
    .trigger        = "$PFEC,GPssd",    // GPSClock should return this
571
    .channels       = 12,               // not used by this driver
572
    .probe_detect   = NULL,             // no probe
573
    .get_packet     = packet_get1,      // how to get a packet
574
    .parse_packet   = generic_parse_input,      // how to interpret a packet
575
    .rtcm_writer    = gpsd_write,       // write RTCM data straight
576
    .init_query     = NULL,             // non-perturbing initial query
577
    .event_hook     = NULL,             // no lifetime event handler
578
    .speed_switcher = NULL,             // no speed switcher
579
    .mode_switcher  = NULL,             // no mode switcher
580
    .rate_switcher  = NULL,             // sample rate is fixed
581
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
582
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
583
    .control_send   = nmea_write,       // how to send control strings
584
    .time_offset     = NULL,            // no method for NTP fudge factor
585
};
586
// *INDENT-ON*
587
#endif  // GPSCLOCK_ENABLE
588
589
#ifdef TRIPMATE_ENABLE
590
/**************************************************************************
591
*
592
* TripMate -- extended NMEA, gets faster fix when primed with lat/long/time
593
*
594
**************************************************************************/
595
596
/*
597
 * Some technical FAQs on the TripMate:
598
 * http://vancouver-webpages.com/pub/peter/tripmate.faq
599
 * http://www.asahi-net.or.jp/~KN6Y-GTU/tripmate/trmfaqe.html
600
 * The TripMate was discontinued sometime before November 1998
601
 * and was replaced by the Zodiac EarthMate.
602
 */
603
604
    static void tripmate_event_hook(struct gps_device_t *session, event_t event)
605
0
    {
606
0
        if (session->context->readonly) {
607
0
            return;
608
0
        }
609
610
        // TripMate requires this response to the ASTRAL it sends at boot time
611
0
        if (event == EVENT_IDENTIFIED) {
612
0
            (void)nmea_send(session, "$IIGPQ,ASTRAL");
613
0
        }
614
        // stop it sending PRWIZCH
615
0
        if (event == EVENT_IDENTIFIED ||
616
0
            event == EVENT_REACTIVATE) {
617
0
            (void)nmea_send(session, "$PRWIILOG,ZCH,V,,");
618
0
        }
619
0
    }
620
621
// *INDENT-OFF*
622
static const struct gps_type_t driver_tripmate = {
623
    .type_name     = "Delorme TripMate",        // full name of type
624
    .packet_type   = NMEA_PACKET,               // lexer packet type
625
    .flags         = DRIVER_STICKY,             // no rollover or other flags
626
    .trigger       ="ASTRAL",                   // tells us to switch
627
    .channels      = 12,                        // consumer-grade GPS
628
    .probe_detect  = NULL,                      // no probe
629
    .get_packet    = packet_get1,               // how to get a packet
630
    .parse_packet  = generic_parse_input,       // how to interpret a packet
631
    .rtcm_writer   = gpsd_write,                // send RTCM data straight
632
    .init_query    = NULL,                      // non-perturbing query
633
    .event_hook    = tripmate_event_hook,       // lifetime event handler
634
    .speed_switcher= NULL,                      // no speed switcher
635
    .mode_switcher = NULL,                      // no mode switcher
636
    .rate_switcher = NULL,                      // no sample-rate switcher
637
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
638
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
639
    .control_send  = nmea_write,        // how to send control strings
640
    .time_offset     = NULL,            // no method for NTP fudge factor
641
};
642
// *INDENT-ON*
643
#endif  // TRIPMATE_ENABLE
644
645
#ifdef EARTHMATE_ENABLE
646
/**************************************************************************
647
*
648
* Zodiac EarthMate textual mode
649
*
650
* Note: This is the pre-2003 version using Zodiac binary protocol.
651
* There is a good HOWTO at <http://www.hamhud.net/ka9mva/earthmate.htm>.
652
* It has been replaced with a design that uses a SiRF chipset.
653
*
654
**************************************************************************/
655
656
    static void earthmate_event_hook(struct gps_device_t *session, event_t event)
657
0
    {
658
0
        struct timespec delay;
659
660
0
        if (session->context->readonly) {
661
0
            return;
662
0
        }
663
0
        if (event == EVENT_TRIGGERMATCH) {
664
0
            (void)gpsd_write(session, "EARTHA\r\n", 8);
665
            // wait 10,000 uSec
666
0
            delay.tv_sec = 0;
667
0
            delay.tv_nsec = 10000000L;
668
0
            nanosleep(&delay, NULL);
669
670
0
            (void)gpsd_switch_driver(session, "Zodiac");
671
0
        }
672
0
    }
673
674
// *INDENT-OFF*
675
static const struct gps_type_t driver_earthmate = {
676
    .type_name     = "Pre-2003 Delorme EarthMate",
677
    .packet_type   = NMEA_PACKET,       // associated lexer packet type
678
    .flags         = DRIVER_STICKY,             // no rollover or other flags
679
    .trigger       = "EARTHA",                  // Earthmate trigger string
680
    .channels      = 12,                        // not used by NMEA parser
681
    .probe_detect  = NULL,                      // no probe
682
    .get_packet    = packet_get1,               // how to get a packet
683
    .parse_packet  = generic_parse_input,       // how to interpret a packet
684
    .rtcm_writer   = NULL,                      // don't send RTCM data
685
    .init_query     = NULL,                     // non-perturbing query
686
    .event_hook    = earthmate_event_hook,      // lifetime event handler
687
    .speed_switcher= NULL,                      // no speed switcher
688
    .mode_switcher = NULL,                      // no mode switcher
689
    .rate_switcher = NULL,                      // no sample-rate switcher
690
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
691
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
692
    .control_send  = nmea_write,        // never actually used.
693
    .time_offset     = NULL,            // no method for NTP fudge factor
694
};
695
// *INDENT-ON*
696
#endif  // EARTHMATE_ENABLE
697
698
#ifdef TNT_ENABLE
699
/**************************************************************************
700
* True North Technologies - Revolution 2X Digital compass
701
*
702
* More info: http://www.tntc.com/
703
*
704
* This is a digital compass which uses magnetometers to measure the
705
* strength of the earth's magnetic field. Based on these measurements
706
* it provides a compass heading using NMEA formatted output strings.
707
* This is useful to supplement the heading provided by another GPS
708
* unit. A GPS heading is unreliable at slow speed or no speed.
709
*
710
**************************************************************************/
711
712
// send a control string in TNT native formal
713
    static ssize_t tnt_control_send(struct gps_device_t *session,
714
                                    char *msg, size_t len UNUSED)
715
0
    {
716
0
        ssize_t status;
717
0
        unsigned char sum = '\0';
718
0
        char c, *p = msg;
719
720
0
        if ('@' == *p) {
721
0
            p++;
722
0
        }
723
#ifdef __UNUSED__
724
            else {
725
                GPSD_LOG(LOG_ERROR, &session->context->errout,
726
                         "Bad TNT sentence: '%s'\n", msg);
727
            }
728
#endif  // __UNUSED__
729
0
        while ('\0' != (c = *p)) {
730
0
            sum ^= c;
731
0
            p++;
732
0
        }
733
0
        (void)snprintf(p, 6, "*%02X\r\n", (unsigned int)sum);
734
735
0
        status = gpsd_write(session, msg, strlen(msg));
736
0
        return status;
737
0
    }
738
739
// printf(3)-like TNT command generator
740
    static bool tnt_send(struct gps_device_t *session, const char *fmt, ...)
741
0
    {
742
0
        char buf[BUFSIZ];
743
0
        va_list ap;
744
0
        ssize_t sent;
745
746
0
        va_start(ap, fmt);
747
0
        (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap);
748
0
        va_end(ap);
749
0
        sent = tnt_control_send(session, buf, strnlen(buf, sizeof(buf)));
750
0
        if ((ssize_t)strnlen(buf, sizeof(buf)) == sent) {
751
0
            GPSD_LOG(LOG_IO, &session->context->errout,
752
0
                     "=> GPS: %s\n", buf);
753
0
            return true;
754
0
        } // else
755
756
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
757
0
                 "=> GPS: %s FAILED\n", buf);
758
0
        return false;
759
0
    }
760
761
    static bool tnt_speed(struct gps_device_t *session,
762
                          speed_t speed, char parity UNUSED, int stopbits UNUSED)
763
0
    {
764
        /*
765
         * Baud rate change followed by device reset.
766
         * See page 40 of Technical Guide 1555-B.  We need:
767
         * 2400 -> 1, 4800 -> 2, 9600 -> 3, 19200 -> 4, 38400 -> 5
768
         */
769
0
        unsigned int val = speed / 2400u; // 2400->1, 4800->2, 9600->4,
770
                                          // 19200->8...
771
0
        unsigned int i = 0;
772
773
        // fast way to compute log2(val)
774
0
        while (1 < (val >> i)) {
775
0
            ++i;
776
0
        }
777
0
        return tnt_send(session, "@B6=%d", i + 1) &&
778
0
               tnt_send(session, "@F28.6=1");
779
0
    }
780
781
// TNT lifetime event hook
782
    static void tnt_event_hook(struct gps_device_t *session, event_t event)
783
0
    {
784
0
        if (session->context->readonly) {
785
0
            return;
786
0
        }
787
0
        if (event == EVENT_WAKEUP) {
788
0
            (void)tnt_send(session, "@F0.3=1"); // set run mode
789
0
            (void)tnt_send(session, "@F2.2=1"); // report in degrees
790
0
        }
791
0
    }
792
793
// *INDENT-OFF*
794
const struct gps_type_t driver_trueNorth = {
795
    .type_name      = "True North",     // full name of type
796
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
797
    .flags          = DRIVER_STICKY,    // remember this
798
    .trigger        = "$PTNTHTM",       // their proprietary sentence
799
    .channels       = 0,                // not an actual GPS at all
800
    .probe_detect   = NULL,             // no probe in run mode
801
    .get_packet     = packet_get1,      // how to get a packet
802
    .parse_packet   = generic_parse_input,      // how to interpret a packet
803
    .rtcm_writer    = NULL,             // Don't send
804
    .init_query     = NULL,             // non-perturbing initial query
805
    .event_hook     = tnt_event_hook,   // lifetime event handler
806
    .speed_switcher = tnt_speed,        // no speed switcher
807
    .mode_switcher  = NULL,             // no mode switcher
808
    .rate_switcher  = NULL,             // no wrapup
809
    .min_cycle.tv_sec  = 0,             // not relevant, no rate switch
810
    .min_cycle.tv_nsec = 500000000,     // not relevant, no rate switch
811
    .control_send   = tnt_control_send, // how to send control strings
812
    .time_offset     = NULL,
813
};
814
// *INDENT-ON*
815
#endif
816
817
#ifdef FURY_ENABLE
818
/**************************************************************************
819
*
820
* Jackson Labs Fury, a high-precision laboratory clock
821
*
822
* Will also support other Jackon Labs boards, including the Firefly.
823
*
824
* Note: you must either build with fixed_port_speed=115200 or tweak the
825
* speed on the port to 115200 before running.  The device's default mode
826
* does not stream output, so our hunt loop will simply time out otherwise.
827
*
828
**************************************************************************/
829
830
    static bool fury_rate_switcher(struct gps_device_t *session, double rate)
831
0
    {
832
0
        char buf[78];
833
0
        double inverted;
834
835
        // rate is a frequency, but the command takes interval in # of seconds
836
0
        if (rate == 0.0) {
837
0
            inverted = 0.0;
838
0
        } else {
839
0
            inverted = 1.0 / rate;
840
0
        }
841
0
        if (inverted > 256) {
842
0
            return false;
843
0
        }
844
0
        (void)snprintf(buf, sizeof(buf), "GPS:GPGGA %d\r\n", (int)inverted);
845
0
        (void)gpsd_write(session, buf, strnlen(buf, sizeof(buf)));
846
0
        return true;
847
0
    }
848
849
    static void fury_event_hook(struct gps_device_t *session, event_t event)
850
0
    {
851
0
        if (event == EVENT_WAKEUP &&
852
0
            gpsd_get_speed(session) == 115200) {
853
0
            (void)fury_rate_switcher(session, 1.0);
854
0
        } else if (event == EVENT_DEACTIVATE) {
855
0
            (void)fury_rate_switcher(session, 0.0);
856
0
        }
857
0
    }
858
859
860
// *INDENT-OFF*
861
static const struct gps_type_t driver_fury = {
862
    .type_name      = "Jackson Labs Fury", // full name of type
863
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
864
    .flags          = DRIVER_STICKY,    // no rollover or other flags
865
    .trigger        = NULL,             // detect their main sentence
866
    .channels       = 0,                // not an actual GPS at all
867
    .probe_detect   = NULL,
868
    .get_packet     = packet_get1,      // how to get a packet
869
    .parse_packet   = generic_parse_input,      // how to interpret a packet
870
    .rtcm_writer    = NULL,             // Don't send
871
    .init_query     = NULL,             // non-perturbing initial query
872
    .event_hook     = fury_event_hook,
873
    .speed_switcher = NULL,             // no speed switcher
874
    .mode_switcher  = NULL,             // no mode switcher
875
    .rate_switcher  = fury_rate_switcher,
876
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
877
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
878
    .control_send   = nmea_write,       // how to send control strings
879
    .time_offset     = NULL,
880
};
881
// *INDENT-ON*
882
883
#endif  // FURY_ENABLE
884
885
/**************************************************************************
886
*
887
* RTCM-104 (v2), used for broadcasting DGPS corrections and by DGPS radios
888
*
889
**************************************************************************/
890
891
static gps_mask_t rtcm104v2_analyze(struct gps_device_t *session)
892
0
{
893
0
    rtcm2_unpack(session, &session->gpsdata.rtcm2,
894
0
                 (char *)session->lexer.isgps.buf);
895
0
    session->cycle_end_reliable = true;
896
0
    return RTCM2_SET;
897
0
}
898
899
// *INDENT-OFF*
900
static const struct gps_type_t driver_rtcm104v2 = {
901
    .type_name     = "RTCM104V2",       // full name of type
902
    .packet_type   = RTCM2_PACKET,      // associated lexer packet type
903
    .flags         = DRIVER_NOFLAGS,    // no rollover or other flags
904
    .trigger       = NULL,              // no recognition string
905
    .channels      = 0,                 // not used
906
    .probe_detect  = NULL,              // no probe
907
    .get_packet    = packet_get1,       // how to get a packet
908
    .parse_packet  = rtcm104v2_analyze, //
909
    .rtcm_writer   = NULL,              // don't send RTCM data
910
    .init_query     = NULL,             // non-perturbing initial query
911
    .event_hook    = NULL,              // no event_hook
912
    .speed_switcher= NULL,              // no speed switcher
913
    .mode_switcher = NULL,              // no mode switcher
914
    .rate_switcher = NULL,              // no sample-rate switcher
915
    // cycle not relevant, no rate switch, but can generate log noise
916
    .min_cycle.tv_sec  = 10,            // Some NTRIP servers are bursty
917
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
918
    .control_send   = NULL,             // how to send control strings
919
    .time_offset     = NULL,
920
};
921
// *INDENT-ON*
922
923
/**************************************************************************
924
*
925
* RTCM-104 (v3), used for broadcasting DGPS corrections and by DGPS radios
926
*
927
**************************************************************************/
928
929
static gps_mask_t rtcm104v3_analyze(struct gps_device_t *session)
930
0
{
931
0
    uint16_t type = getbeu16(session->lexer.outbuffer, 3) >> 4;
932
933
0
    GPSD_LOG(LOG_RAW, &session->context->errout, "RTCM 3.x packet %d\n", type);
934
0
    rtcm3_unpack(session->context,
935
0
                 &session->gpsdata.rtcm3,
936
0
                 session->lexer.outbuffer);
937
0
    session->cycle_end_reliable = true;
938
0
    return RTCM3_SET;
939
0
}
940
941
// *INDENT-OFF*
942
static const struct gps_type_t driver_rtcm104v3 = {
943
    .type_name     = "RTCM104V3",       // full name of type
944
    .packet_type   = RTCM3_PACKET,      // associated lexer packet type
945
    .flags         = DRIVER_NOFLAGS,    // no rollover or other flags
946
    .trigger       = NULL,              // no recognition string
947
    .channels      = 0,                 // not used
948
    .probe_detect  = NULL,              // no probe
949
    .get_packet    = packet_get1,       // how to get a packet
950
    .parse_packet  = rtcm104v3_analyze, //
951
    .rtcm_writer   = NULL,              // don't send RTCM data
952
    .init_query    = NULL,              // non-perturbing initial query
953
    .event_hook    = NULL,              // no event hook
954
    .speed_switcher= NULL,              // no speed switcher
955
    .mode_switcher = NULL,              // no mode switcher
956
    .rate_switcher = NULL,              // no sample-rate switcher
957
    // cycle not relevant, no rate switch, but can generate log noise
958
    .min_cycle.tv_sec  = 10,            // Some NTRIP servers are bursty
959
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
960
    .control_send   = NULL,             // how to send control strings
961
    .time_offset     = NULL,
962
};
963
// *INDENT-ON*
964
965
#ifdef GARMINTXT_ENABLE
966
/**************************************************************************
967
*
968
* Garmin Simple Text protocol
969
*
970
**************************************************************************/
971
972
// *INDENT-OFF*
973
static const struct gps_type_t driver_garmintxt = {
974
    .type_name     = "Garmin Simple Text",              // full name of type
975
    .packet_type   = GARMINTXT_PACKET,  // associated lexer packet type
976
    .flags         = DRIVER_NOFLAGS,    // no rollover or other flags
977
    .trigger       = NULL,              // no recognition string
978
    .channels      = 0,                 // not used
979
    .probe_detect  = NULL,              // no probe
980
    .get_packet    = packet_get1,       // how to get a packet
981
    .parse_packet  = garmintxt_parse,   // how to parse one
982
    .rtcm_writer   = NULL,              // don't send RTCM data
983
    .init_query     = NULL,             // non-perturbing initial query
984
    .event_hook    = NULL,              // no event hook
985
    .speed_switcher= NULL,              // no speed switcher
986
    .mode_switcher = NULL,              // no mode switcher
987
    .rate_switcher = NULL,              // no sample-rate switcher
988
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
989
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
990
    .control_send   = NULL,             // how to send control strings
991
    .time_offset     = NULL,
992
};
993
// *INDENT-ON*
994
#endif  // GARMINTXT_ENABLE
995
996
/**************************************************************************
997
*
998
* MediaTek MTK-3301, 3329, 3333, 3339
999
*
1000
* OEMs for several GPS vendors, notably including Garmin, FasTrax, Trimble,
1001
* and AdaFruit. Website at <http://www.mediatek.com/>.
1002
*
1003
* The Trimble Condor appears to be an MTK3329.  It behaves as an MTK3301
1004
* and positively acknowledges all 3301 sentences as valid. It ignores $PMTK
1005
* sentence fields that are not implemented in the Trimble Condor. It does
1006
* not have power-save mode and ignores $PMTK320.  For $PMTK314 it silently
1007
* ignores periodic enabling of messages that aren't supported.
1008
*
1009
* From its datasheet the MTK3339 seems to add QZSS support not present in
1010
* earlier versions. The data sheet says it has 66 channels, which mkes
1011
* sense given the multi-constellation capability. The channel count
1012
* in the driver is never used by the NMEA driver so leaving the lower MTK3301
1013
* value in there is OK.
1014
*
1015
* MTK3333 support 10Hz.
1016
*
1017
* The Adafruit GPS HAT for the Raspberry Pi is an MTK3339. It works with this
1018
* driver; in fact AdaFruit's overview page for the product features GPSD.
1019
*
1020
* SIMCom DIM68M appears to be an MTK3333.
1021
*
1022
* AXN_1.36 reports the fix as differential whenever SBAS is enabled and
1023
* an SBAS satellite is tracked, including outside that SBAS service area.
1024
* GGA quality is 2 and the RMC FAA mode is 'D', so gpsd reports
1025
* STATUS_DGPS.  The DGPS age and station ID stay 0000, and the SBAS
1026
* satellite is not listed as used in GSA.  Seen 2026-10-18 in Singapore
1027
* while tracking GAGAN GSAT-15 (PRN 132); Singapore is outside the GAGAN
1028
* service volume.  This driver requests that mode with $PMTK301,2 and
1029
* $PMTK313,1.
1030
*
1031
**************************************************************************/
1032
1033
static void mtk3301_event_hook(struct gps_device_t *session, event_t event)
1034
0
{
1035
/*
1036
 *  0  NMEA_SEN_GLL,  GPGLL   interval - Geographic Position - Latitude
1037
 * longitude
1038
 *  1  NMEA_SEN_RMC,  GPRMC   interval - Recommended Minimum Specific GNSS
1039
 * Sentence
1040
 *  2  NMEA_SEN_VTG,  GPVTG   interval - Course Over Ground and Ground Speed
1041
 *  3  NMEA_SEN_GGA,  GPGGA   interval - GPS Fix Data
1042
 *  4  NMEA_SEN_GSA,  GPGSA   interval - GNSS DOPS and Active Satellites
1043
 *  5  NMEA_SEN_GSV,  GPGSV   interval - GNSS Satellites in View
1044
 *  6  NMEA_SEN_GRS,  GPGRS   interval - GNSS Range Residuals
1045
 *  7  NMEA_SEN_GST,  GPGST   interval - GNSS Pseudorange Errors Statistics
1046
 *  13 NMEA_SEN_MALM, PMTKALM interval - GPS almanac information
1047
 *  14 NMEA_SEN_MEPH, PMTKEPH interval - GPS ephemeris information
1048
 *  15 NMEA_SEN_MDGP, PMTKDGP interval - GPS differential correction information
1049
 *  16 NMEA_SEN_MDBG, PMTKDBG interval – MTK debug information
1050
 *  17 NMEA_SEN_ZDA,  GPZDA   interval - Time & Date
1051
 *  18 NMEA_SEN_MCHN, PMTKCHN interval – GPS channel status
1052
 *  // MTK 3333
1053
 *  19 NMEA_SEN_DTM,  GPDTM   interval - Datum reference
1054
 *
1055
 *  "$PMTK314,1,1,1,1,1,5,1,1,0,0,0,0,0,0,0,0,0,1,0"
1056
 *
1057
 */
1058
0
    if (session->context->readonly) {
1059
0
        return;
1060
0
    }
1061
0
    if (event == EVENT_TRIGGERMATCH) {
1062
0
        (void)nmea_send(session, "$PMTK320,0");  // power save off
1063
        // Fix interval, 1000 milliseconds
1064
0
        (void)nmea_send(session, "$PMTK300,1000,0,0,0.0,0.0");
1065
        // Set NMEA sentences.
1066
0
        (void)nmea_send(session,
1067
0
                        "$PMTK314,0,1,0,1,1,5,1,1,0,0,0,0,0,0,0,0,0,1,0");
1068
        // DGPS is WAAS
1069
0
        (void)nmea_send(session, "$PMTK301,2");
1070
        // SBAS enable
1071
0
        (void)nmea_send(session, "$PMTK313,1");
1072
1073
        /* PMTK_API_Q_OUTPUT_CTL - Query PPS pulse width - Trimble only?
1074
         * http://trl.trimble.com/docushare/dsweb/Get/Document-482603/CONDOR_UG_2C_75263-00.pdf
1075
         * *
1076
         * badly documented */
1077
0
        (void)nmea_send(session, "$PMTK424");
1078
0
    }
1079
0
}
1080
1081
// mtk3301_speed_switcher()
1082
// Always returns True
1083
static bool mtk3301_speed_switcher(struct gps_device_t *session,
1084
                                   speed_t speed, char parity UNUSED,
1085
                                   int stopbits UNUSED)
1086
0
{
1087
0
    int i;
1088
0
    char buf[BUFSIZ] = {0};
1089
1090
0
    (void)snprintf(buf, sizeof(buf), "$PQBAUD,W,%d", (int) speed);
1091
1092
    // Calling PQBAUD fails to set the receiver's baud rate
1093
    // some of the time.  Sending it twice seems to work every time.
1094
    // So just for good measure, send it three times.
1095
0
    for (i = 0; i < 3; i++) {
1096
0
        (void)nmea_send(session, buf);
1097
0
    }
1098
1099
0
    return true;
1100
0
}
1101
1102
static bool mtk3301_rate_switcher(struct gps_device_t *session, double rate)
1103
0
{
1104
0
    char buf[78];
1105
1106
0
    unsigned int milliseconds = (unsigned int)(1000 * rate);
1107
0
    if (rate > 1) {
1108
        // force no slower than 1Hz
1109
0
        milliseconds = 1000;
1110
0
    } else if (rate < 0.1) {
1111
        // force no faster than 10Hz, MTK3333 can do 10Hz
1112
0
        milliseconds = 100;
1113
0
    }
1114
1115
0
    (void)snprintf(buf, sizeof(buf), "$PMTK300,%u,0,0,0,0", milliseconds);
1116
0
    (void)nmea_send(session, buf);      // Fix interval
1117
0
    return true;
1118
0
}
1119
1120
// *INDENT-OFF*
1121
const struct gps_type_t driver_mtk3301 = {
1122
    .type_name      = "MTK-3301",               // full name of type
1123
    .packet_type    = NMEA_PACKET,              // associated lexer packet type
1124
    .flags          = DRIVER_STICKY,            // remember this
1125
    .trigger        = "$PMTK705,",         // firmware release name and version
1126
    .channels       = 12,                       // not used by this driver
1127
    .probe_detect   = NULL,                     // no probe
1128
    .get_packet     = packet_get1,              // how to get a packet
1129
    .parse_packet   = generic_parse_input,      // how to interpret a packet
1130
    .rtcm_writer    = gpsd_write,               // write RTCM data straight
1131
    .init_query     = NULL,                     // non-perturbing initial query
1132
    .event_hook     = mtk3301_event_hook,       // lifetime event handler
1133
    .speed_switcher = mtk3301_speed_switcher,   // sample speed switcher
1134
    .mode_switcher  = NULL,                     // no mode switcher
1135
    .rate_switcher  = mtk3301_rate_switcher,    // sample rate switcher
1136
    .min_cycle.tv_sec  = 0,
1137
    .min_cycle.tv_nsec = 100000000,             // max 10Hz
1138
    .control_send   = nmea_write,               // how to send control strings
1139
    .time_offset     = NULL,
1140
};
1141
// *INDENT-ON*
1142
1143
#ifdef ISYNC_ENABLE
1144
/**************************************************************************
1145
*
1146
* Spectratime LNRCLOK / GRCLOK iSync GPS-disciplined rubidium oscillators
1147
*
1148
* These devices comprise a u-blox 6 attached to a separate iSync
1149
* microcontroller which drives the rubidium oscillator.  The iSync
1150
* microcontroller can be configured to pass through the underlying
1151
* GPS communication channel, while still using the GPS PPSREF signal
1152
* to discipline the rubidium oscillator.
1153
*
1154
* The iSync can also generate its own periodic status packets in NMEA
1155
* format.  These describe the state of the oscillator (including
1156
* whether or not the oscillator PPSOUT is synced to the GPS PPSREF).
1157
* These packets are transmitted in the middle of the underlying GPS
1158
* packets, forcing us to handle interrupted NMEA packets.
1159
*
1160
* The default state of the device is to generate no periodic output.
1161
* We send a probe string to initiate beating of the iSync-proprietary
1162
* $PTNTS,B message, which is then detected as a NMEA trigger.
1163
*
1164
**************************************************************************/
1165
1166
    static ssize_t isync_write(struct gps_device_t *session, const char *data)
1167
0
    {
1168
        // 80 nseems a reasonable max, and strnlen() shuts up Codacy.
1169
0
        return gpsd_write(session, data, strnlen(data, 80));
1170
0
    }
1171
1172
    static bool isync_detect(struct gps_device_t *session)
1173
0
    {
1174
0
        speed_t old_baudrate;
1175
0
        char old_parity;
1176
0
        unsigned int old_stopbits;
1177
1178
        // Set 9600 8N1
1179
0
        old_baudrate = session->gpsdata.dev.baudrate;
1180
0
        old_parity = session->gpsdata.dev.parity;
1181
0
        old_stopbits = session->gpsdata.dev.stopbits;
1182
0
        gpsd_set_speed(session, 9600, 'N', 1);
1183
1184
        /* Cancel pass-through mode and initiate beating of $PTNTS,B
1185
         * message, which can subsequently be detected as a trigger.
1186
         */
1187
0
        (void)isync_write(session, "@@@@\r\nMAW0C0B\r\n");
1188
1189
        // return serial port to original settings
1190
0
        gpsd_set_speed(session, old_baudrate, old_parity, old_stopbits);
1191
1192
0
        return false;
1193
0
    }
1194
1195
    static void isync_event_hook(struct gps_device_t *session, event_t event)
1196
0
    {
1197
0
        if (session->context->readonly) {
1198
0
            return;
1199
0
        }
1200
1201
0
        if (event == EVENT_DRIVER_SWITCH) {
1202
0
            session->lexer.counter = 0;
1203
0
        }
1204
1205
0
        if (event == EVENT_CONFIGURE) {
1206
0
            switch (session->lexer.counter) {
1207
0
            case 1:
1208
                /* Configure timing and frequency flags:
1209
                 *  - Thermal compensation active
1210
                 *  - PPSREF active
1211
                 *  - PPSOUT active
1212
                 */
1213
0
                (void)isync_write(session, "MAW040B\r\n");
1214
                /* Configure tracking flags:
1215
                 *  - Save frequency every 24 hours
1216
                 *  - Align PPSOUT to PPSINT
1217
                 *  - Track PPSREF
1218
                 */
1219
0
                (void)isync_write(session, "MAW0513\r\n");
1220
                /* Configure tracking start flags:
1221
                 *  - Tracking restart allowed
1222
                 *  - Align to PPSREF
1223
                 */
1224
0
                (void)isync_write(session, "MAW0606\r\n");
1225
                /* Configure tracking window:
1226
                 *  - 4us
1227
                 */
1228
0
                (void)isync_write(session, "MAW1304\r\n");
1229
                /* Configure alarm window:
1230
                 *  - 4us
1231
                 */
1232
0
                (void)isync_write(session, "MAW1404\r\n");
1233
0
                break;
1234
0
            case 2:
1235
                /* Configure pulse every d second:
1236
                 *  - pulse every second
1237
                 */
1238
0
                (void)isync_write(session, "MAW1701\r\n");
1239
                /* Configure pulse origin:
1240
                 *  - zero offset
1241
                 */
1242
0
                (void)isync_write(session, "MAW1800\r\n");
1243
                /* Configure pulse width:
1244
                 *  - 600ms
1245
                 */
1246
0
                (void)isync_write(session, "MAW1223C34600\r\n");
1247
0
                break;
1248
0
            case 3:
1249
                /* Configure GPS resource utilization:
1250
                 *  - do not consider GPS messages
1251
                 */
1252
0
                (void)isync_write(session, "MAW2200\r\n");
1253
                // Restart sync
1254
0
                (void)isync_write(session, "SY1\r\n");
1255
                // Restart tracking
1256
0
                (void)isync_write(session, "TR1\r\n");
1257
0
                break;
1258
0
            case 4:
1259
                // Cancel BTx messages (if any)
1260
0
                (void)isync_write(session, "BT0\r\n");
1261
                /* Configure messages coming out every second:
1262
                 *  - Oscillator status ($PTNTA) at 750ms
1263
                 */
1264
0
                (void)isync_write(session, "MAW0B00\r\n");
1265
0
                (void)isync_write(session, "MAW0C0A\r\n");
1266
0
                break;
1267
0
            case 5:
1268
                /* Enable GPS passthrough and force u-blox driver to
1269
                 * select NMEA mode.
1270
                 */
1271
0
                session->mode = 0;
1272
0
                session->drivers_identified = 0;
1273
0
                (void)isync_write(session, "@@@@GPS\r\n");
1274
0
                break;
1275
0
            case 6:
1276
                /* Trigger detection of underlying u-blox (if necessary)
1277
                 * UBX-CFG-PRT */
1278
0
                (void)ubx_write(session, 0x06, 0x00, NULL, 0);
1279
0
                break;
1280
0
            }
1281
0
        }
1282
0
    }
1283
1284
// *INDENT-OFF*
1285
const struct gps_type_t driver_isync = {
1286
    .type_name      = "iSync",          // full name of type
1287
    .packet_type    = NMEA_PACKET,      // associated lexer packet type
1288
    .flags          = DRIVER_STICKY,    // remember this
1289
    .trigger        = "$PTNTS,B,",      // iSync status message
1290
    .channels       = 50,               // copied from driver_ubx
1291
    .probe_detect   = isync_detect,     // how to detect at startup time
1292
    .get_packet     = packet_get1,      // how to get a packet
1293
    .parse_packet   = generic_parse_input,      // how to interpret a packet
1294
    .init_query     = NULL,             // non-perturbing initial query
1295
    .event_hook     = isync_event_hook, // lifetime event handler
1296
    .speed_switcher = NULL,             // no speed switcher
1297
    .mode_switcher  = NULL,             // no mode switcher
1298
    .rate_switcher  = NULL,             // no sample-rate switcher
1299
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
1300
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
1301
    .control_send   = nmea_write,       // how to send control strings
1302
    .time_offset     = NULL,            // no method for NTP fudge factor
1303
};
1304
// *INDENT-ON*
1305
#endif  // ISYNC_ENABLE
1306
1307
#ifdef AIVDM_ENABLE
1308
/**************************************************************************
1309
*
1310
* AIVDM - ASCII armoring of binary AIS packets
1311
* FIXME:  Move into drivers/drivar_ais.c
1312
*
1313
**************************************************************************/
1314
1315
    static bool aivdm_decode(unsigned char *buf, size_t buflen,
1316
                             struct gps_device_t *session, struct ais_t *ais,
1317
                             int debug)
1318
0
    {
1319
#ifdef __UNUSED_DEBUG__
1320
            static char *sixbits[64] = {
1321
                "000000", "000001", "000010", "000011", "000100",
1322
                "000101", "000110", "000111", "001000", "001001",
1323
                "001010", "001011", "001100", "001101", "001110",
1324
                "001111", "010000", "010001", "010010", "010011",
1325
                "010100", "010101", "010110", "010111", "011000",
1326
                "011001", "011010", "011011", "011100", "011101",
1327
                "011110", "011111", "100000", "100001", "100010",
1328
                "100011", "100100", "100101", "100110", "100111",
1329
                "101000", "101001", "101010", "101011", "101100",
1330
                "101101", "101110", "101111", "110000", "110001",
1331
                "110010", "110011", "110100", "110101", "110110",
1332
                "110111", "111000", "111001", "111010", "111011",
1333
                "111100", "111101", "111110", "111111",
1334
            };
1335
#endif  // __UNUSED_DEBUG__
1336
0
        int nfrags, ifrag, nfields = 0;
1337
        /* Maximum ength of IEC 61162 message is 79 chars.
1338
         * But we have seen 136.
1339
         * We will copy fields here, NUL separated */
1340
0
        unsigned char fieldcopy[200];
1341
        /* pointer to fields in fieldcopy, so only 80 fields possible
1342
         * if buf is all commas, then there as many fields as chars. */
1343
0
        unsigned char *field[200];
1344
0
        unsigned char *data, *cp;
1345
0
        char buf2[1024];             // debug buffer
1346
0
        int pad;
1347
0
        struct aivdm_context_t *ais_context;
1348
0
        int i;
1349
1350
0
        if (12 >= buflen ||
1351
0
            136 < buflen) {
1352
0
            session->lexer.type = BAD_PACKET;
1353
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1354
0
                     "AIVDM: bad sized packet of %zd+ chars rejected.\n",
1355
0
                     buflen);
1356
0
            return false;
1357
0
        }
1358
1359
0
        if ('!' != buf[0] ||
1360
0
            !ISUPPER(buf[1]) ||
1361
0
            !ISUPPER(buf[2])) {
1362
0
            session->lexer.type = BAD_PACKET;
1363
            // Should not happen, but fuzzers deep link past packet.c checks
1364
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1365
0
                     "AIVDM: bad talker x%02x x%02x  x%02x\n",
1366
0
                     buf[0], buf[1], buf[2]);
1367
0
            return false;
1368
0
        }
1369
1370
        // we may need to dump the raw packet
1371
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
1372
0
                 "AIVDM packet length %zd: %s\n", buflen,
1373
0
                 gps_visibilize(buf2, sizeof(buf2), (char *)buf, buflen));
1374
1375
        // first clear the result, making sure we don't return garbage
1376
0
        memset(ais, 0, sizeof(*ais));
1377
1378
        // discard overlong sentences
1379
0
        if (buflen > (sizeof(fieldcopy) - 1)) {
1380
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
1381
0
                     "overlong AIVDM packet.\n");
1382
0
            return false;
1383
0
        }
1384
1385
        // avoid reoccuring CVE here
1386
0
        memset(fieldcopy, 0, sizeof(fieldcopy));
1387
0
        memset(field, 0, sizeof(field));
1388
1389
        // extract packet fields
1390
0
        field[nfields++] = fieldcopy;
1391
0
        for (i = 0; i < (int)buflen; i++) {
1392
0
            unsigned char c = buf[i];
1393
1394
0
            if (!ISASCII(c)) {
1395
                /* discard sentences with high-half characters in them,
1396
                 * they're corrupted. */
1397
0
                GPSD_LOG(LOG_ERROR, &session->context->errout,
1398
0
                         "corrupted AIVDM packet.\n");
1399
0
                return false;
1400
0
            }
1401
0
            if (((unsigned char)',' == c) ||
1402
0
                ((unsigned char)'*' == c)) {
1403
0
                c = '\0';
1404
0
                field[nfields++] = &fieldcopy[i] + 1;
1405
0
            }
1406
0
            fieldcopy[i] = c;
1407
0
        }
1408
#ifdef __UNDEF_DEBUG_
1409
            for (int nf = 0; nf < nfields; nf++) {
1410
                GPSD_LOG(LOG_DATA, &session->context->errout,
1411
                         "field [%d] [%s]\n", nf, field[nf]);
1412
            }
1413
#endif
1414
1415
        // discard sentences with exiguous commas; catches run-ons
1416
0
        if (7 > nfields) {
1417
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
1418
0
                     "malformed AIVDM packet.\n");
1419
0
            return false;
1420
0
        }
1421
1422
0
        switch (field[4][0]) {
1423
0
        case '\0':
1424
            /*
1425
             * Apparently an empty channel is normal for AIVDO sentences,
1426
             * which makes sense as they don't come in over radio.  This
1427
             * is going to break if there's ever an AIVDO type 24, though.
1428
             */
1429
0
            if (!str_starts_with((const char *)field[0], "!AIVDO")) {
1430
0
                GPSD_LOG(LOG_INF, &session->context->errout,
1431
0
                         "invalid empty AIS channel. Assuming 'A'\n");
1432
0
            }
1433
0
            ais_context = &session->driver.aivdm.context[0];
1434
0
            session->driver.aivdm.ais_channel = 'A';
1435
0
            break;
1436
0
        case '1':
1437
0
            if (strcmp((char *)field[4], (char *)"12") == 0) {
1438
0
                GPSD_LOG(LOG_INF, &session->context->errout,
1439
0
                         "ignoring bogus AIS channel '12'.\n");
1440
0
                return false;
1441
0
            }
1442
0
            FALLTHROUGH
1443
0
        case 'A':
1444
0
            ais_context = &session->driver.aivdm.context[0];
1445
0
            session->driver.aivdm.ais_channel = 'A';
1446
0
            break;
1447
0
        case '2':
1448
0
            FALLTHROUGH
1449
0
        case 'B':
1450
0
            ais_context = &session->driver.aivdm.context[1];
1451
0
            session->driver.aivdm.ais_channel = 'B';
1452
0
            break;
1453
0
        case 'C':
1454
0
            GPSD_LOG(LOG_INF, &session->context->errout,
1455
0
                     "ignoring AIS channel C (secure AIS).\n");
1456
0
            return false;
1457
0
        default:
1458
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
1459
0
                     "invalid AIS channel 0x%0X .\n", field[4][0]);
1460
0
            return false;
1461
0
        }
1462
1463
0
        nfrags = atoi((char *)field[1]); // number of fragments to expect
1464
0
        ifrag = atoi((char *)field[2]);  // fragment id
1465
0
        data = field[5];
1466
1467
0
        pad = 0;
1468
0
        if (ISDIGIT(field[6][0])) {
1469
0
            pad = field[6][0] - '0';     // number padding bits ASCII encoded
1470
0
        }
1471
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
1472
0
                 "nfrags %d ifrag %d decoded_frags %d data %s pad %d "
1473
0
                 "bitlen %zu datalen %zu\n",
1474
0
                 nfrags, ifrag, ais_context->decoded_frags, data, pad,
1475
0
                 ais_context->bitlen, strnlen((char *)data, 256));
1476
1477
        // assemble the binary data
1478
1479
        // check fragment ordering
1480
0
        if (ifrag != ais_context->decoded_frags + 1) {
1481
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
1482
0
                     "invalid fragment #%d received, expected #%d.\n",
1483
0
                     ifrag, ais_context->decoded_frags + 1);
1484
0
            if (1 != ifrag) {
1485
0
                return false;
1486
0
            }
1487
            /* else, ifrag==1: Just discard all that was previously decoded and
1488
             * simply handle that packet */
1489
0
            ais_context->decoded_frags = 0;
1490
0
        }
1491
0
        if (1 == ifrag) {
1492
0
            (void)memset(ais_context->bits, '\0', sizeof(ais_context->bits));
1493
0
            ais_context->bitlen = 0;
1494
0
        }
1495
1496
        /* wacky 6-bit encoding, shades of FIELDATA
1497
         * See IEC 61162-1, Table 7.
1498
         * All 64 values that can fit in 6 bits are valid.
1499
         * Max 256 data chars is a guess, to pacify Codacy
1500
         * IEC61993.pdf implies 84.
1501
         * Max bitlen is likely 1008, max see is 776 */
1502
0
        for (cp = data; cp < data + strnlen((char *)data, 256); cp++) {
1503
0
            unsigned char ch;
1504
0
            ch = *cp;
1505
0
            ch -= 48;
1506
0
            if (ch >= 40) {
1507
0
                ch -= 8;
1508
0
            }
1509
#ifdef __UNUSED_DEBUG__
1510
                GPSD_LOG(LOG_RAW, &session->context->errout,
1511
                         "%c: %s\n", *cp, sixbits[ch]);
1512
#endif  // __UNUSED_DEBUG__
1513
0
            if (sizeof(ais_context->bits) <=
1514
0
                (ais_context->bitlen + 6)) {
1515
                // FIXME?  Maybe move above the previous for() ??
1516
0
                GPSD_LOG(LOG_INF, &session->context->errout,
1517
0
                         "overlong AIVDM payload ignored.\n");
1518
0
                (void)memset(ais_context->bits, '\0',
1519
0
                             sizeof(ais_context->bits));
1520
0
                ais_context->bitlen = 0;
1521
0
                return false;
1522
0
            }
1523
0
            for (i = 5; i >= 0; i--) {
1524
0
                if ((ch >> i) & 0x01) {
1525
0
                    ais_context->bits[ais_context->bitlen / 8] |=
1526
0
                        (1 << (7 - ais_context->bitlen % 8));
1527
0
                }
1528
0
                ais_context->bitlen++;
1529
0
            }
1530
0
        }
1531
0
        ais_context->bitlen -= pad;
1532
0
        if (ais_context->bitlen >= sizeof(ais_context->bits)) {
1533
0
            GPSD_LOG(LOG_INF, &session->context->errout,
1534
0
                     "invalid bitlen AIVDM payload ignored.\n");
1535
0
            (void)memset(ais_context->bits, '\0',
1536
0
                         sizeof(ais_context->bits));
1537
0
            ais_context->bitlen = 0;
1538
0
            return false;
1539
0
        }
1540
1541
        // time to pass buffered-up data to where it's actually processed?
1542
0
        if (ifrag == nfrags) {
1543
0
            if (debug >= LOG_INF) {
1544
0
                size_t clen = BITS_TO_BYTES(ais_context->bitlen);
1545
0
                GPSD_LOG(LOG_INF, &session->context->errout,
1546
0
                         "AIVDM payload is %zd bits, %zd chars: %s\n",
1547
0
                         ais_context->bitlen, clen,
1548
0
                         gps_hexdump(session->msgbuf, sizeof(session->msgbuf),
1549
0
                                     ais_context->bits, clen));
1550
0
            }
1551
1552
            // clear waiting fragments count
1553
0
            ais_context->decoded_frags = 0;
1554
1555
            // decode the assembled binary packet
1556
0
            return ais_binary_decode(&session->context->errout,
1557
0
                                     ais,
1558
0
                                     ais_context->bits,
1559
0
                                     ais_context->bitlen,
1560
0
                                     &ais_context->type24_queue);
1561
0
        }
1562
1563
        // we're still waiting on another sentence
1564
0
        ais_context->decoded_frags++;
1565
0
        return false;
1566
0
    }
1567
1568
    static gps_mask_t aivdm_analyze(struct gps_device_t *session)
1569
0
    {
1570
0
        if (session->lexer.type == AIVDM_PACKET) {
1571
0
            if (aivdm_decode(session->lexer.outbuffer, session->lexer.outbuflen,
1572
0
                             session, &session->gpsdata.ais,
1573
0
                             session->context->errout.debug)) {
1574
0
                return ONLINE_SET | AIS_SET;
1575
0
            }
1576
            // else
1577
0
            return ONLINE_SET;
1578
0
        }
1579
0
        if (session->lexer.type == NMEA_PACKET) {
1580
0
            return nmea_parse((char *)session->lexer.outbuffer, session);
1581
0
        }
1582
        // else
1583
0
        return 0;
1584
0
    }
1585
1586
// *INDENT-OFF*
1587
const struct gps_type_t driver_aivdm = {
1588
    // Full name of type
1589
    .type_name        = "AIVDM",        // associated lexer packet type
1590
    .packet_type      = AIVDM_PACKET,   // numeric packet type
1591
    .flags            = DRIVER_NOFLAGS, // no rollover or other flags
1592
    .trigger          = NULL,           // identifying response
1593
    .channels         = 0,              // not used by this driver
1594
    .probe_detect     = NULL,           // no probe
1595
    .get_packet       = packet_get1,    // how to get a packet
1596
    .parse_packet     = aivdm_analyze,  // how to analyze a packet
1597
    .rtcm_writer      = NULL,           // don't send RTCM data,
1598
    .init_query       = NULL,           // non-perturbing initial query
1599
    .event_hook       = NULL,           // lifetime event handler
1600
    .speed_switcher   = NULL,           // no speed switcher
1601
    .mode_switcher    = NULL,           // no mode switcher
1602
    .rate_switcher    = NULL,           // no rate switcher
1603
    .min_cycle.tv_sec  = 1,             // max 1Hz
1604
    .min_cycle.tv_nsec = 0,
1605
    .control_send     = NULL,           // no control sender
1606
    .time_offset     = NULL,            // no NTP communication
1607
};
1608
// *INDENT-ON*
1609
#endif  // AIVDM_ENABLE
1610
1611
/**************************************************************************
1612
*
1613
* JSON passthrough driver
1614
*
1615
**************************************************************************/
1616
1617
// prepend the session path to the value of a specified attribute
1618
static void path_rewrite(struct gps_device_t *session, char *prefix)
1619
0
{
1620
    /*
1621
     * Hack the packet to reflect its origin.  This code is supposed
1622
     * to insert the path naming the remote gpsd instance into the
1623
     * beginning of the path attribute, followed by a # to separate it
1624
     * from the device.
1625
     */
1626
0
    char *outbuf = (char *)session->lexer.outbuffer;
1627
0
    char *prefloc;
1628
    // only the path before any '#', to avoid accumulating device names
1629
0
    size_t pathlen = strcspn(session->gpsdata.dev.path, "#");
1630
1631
0
    assert(prefix != NULL && session->lexer.outbuffer != NULL);
1632
1633
    // possibly the rewrite has been done already, this comw up in gpsmon
1634
0
    if (strstr(outbuf, session->gpsdata.dev.path) != NULL) {
1635
0
        return;
1636
0
    }
1637
1638
0
    for (prefloc = outbuf;
1639
0
         prefloc < outbuf + session->lexer.outbuflen;
1640
0
         prefloc++) {
1641
0
        if (str_starts_with(prefloc, prefix)) {
1642
0
            size_t sfxlen;
1643
1644
0
            prefloc += strlen(prefix);
1645
0
            sfxlen = strlen(prefloc);
1646
            // the path, the '#', the rest and the NUL must all fit
1647
0
            if (sizeof(session->lexer.outbuffer) <=
1648
0
                (size_t)(prefloc - outbuf) + pathlen + 1 + sfxlen) {
1649
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
1650
0
                         "JSON: no room for %zu more bytes, "
1651
0
                         "rest passed as is\n", pathlen + 1);
1652
0
                break;
1653
0
            }
1654
0
            memmove(prefloc + pathlen + 1, prefloc, sfxlen + 1);
1655
0
            memcpy(prefloc, session->gpsdata.dev.path, pathlen);
1656
0
            prefloc += pathlen;
1657
0
            *prefloc = '#';
1658
            // keep scanning to the end of the grown packet
1659
0
            session->lexer.outbuflen = (size_t)(prefloc - outbuf) +
1660
0
                                       1 + sfxlen;
1661
0
        }
1662
0
    }
1663
0
    session->lexer.outbuflen = strnlen(outbuf,
1664
0
                                       sizeof(session->lexer.outbuffer));
1665
0
}
1666
1667
static gps_mask_t json_pass_packet(struct gps_device_t *session)
1668
0
{
1669
0
    GPSD_LOG(LOG_IO, &session->context->errout,
1670
0
             "<= GPS: %s\n", (char *)session->lexer.outbuffer);
1671
1672
0
    if (strstr(session->gpsdata.dev.path, ":/") != NULL &&
1673
0
        strstr(session->gpsdata.dev.path, "localhost") == NULL) {
1674
        // devices and paths need to be edited
1675
0
        if (strstr((char *)session->lexer.outbuffer, "DEVICE") != NULL) {
1676
0
            path_rewrite(session, "\"path\":\"");
1677
0
        }
1678
0
        path_rewrite(session, "\"device\":\"");
1679
1680
        // mark certain responses without a path or device attribute
1681
0
        if (session->gpsdata.dev.path[0] != '\0') {
1682
0
            if (strstr((char *)session->lexer.outbuffer, "VERSION") != NULL ||
1683
0
                strstr((char *)session->lexer.outbuffer, "WATCH") != NULL ||
1684
0
                strstr((char *)session->lexer.outbuffer, "DEVICES") != NULL) {
1685
0
                session->lexer.outbuffer[session->lexer.outbuflen - 1] = '\0';
1686
0
                (void)strlcat((char *)session->lexer.outbuffer, ",\"remote\":\"",
1687
0
                              sizeof(session->lexer.outbuffer));
1688
0
                (void)strlcat((char *)session->lexer.outbuffer,
1689
0
                              session->gpsdata.dev.path,
1690
0
                              sizeof(session->lexer.outbuffer));
1691
0
                (void)strlcat((char *)session->lexer.outbuffer, "\"}",
1692
0
                              sizeof(session->lexer.outbuffer));
1693
0
            }
1694
0
            session->lexer.outbuflen = strnlen(
1695
0
                (char *)session->lexer.outbuffer,
1696
0
                sizeof(session->lexer.outbuffer));
1697
0
        }
1698
0
    }
1699
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
1700
0
             "JSON, passing through %s\n",
1701
0
             (char *)session->lexer.outbuffer);
1702
0
    return PASSTHROUGH_IS;
1703
0
}
1704
1705
// *INDENT-OFF*
1706
const struct gps_type_t driver_json_passthrough = {
1707
    .type_name      = "JSON slave driver",      // full name of type
1708
    .packet_type    = JSON_PACKET,      // associated lexer packet type
1709
    .flags          = DRIVER_NOFLAGS,   // don't remember this
1710
    .trigger        = NULL,             // it's the default
1711
    .channels       = 0,                // not used
1712
    .probe_detect   = NULL,             // no probe
1713
    .get_packet     = packet_get1,      // use generic packet getter
1714
    .parse_packet   = json_pass_packet, // how to interpret a packet
1715
    .rtcm_writer    = NULL,             // write RTCM data straight
1716
    .init_query     = NULL,             // non-perturbing initial query
1717
    .event_hook     = NULL,             // lifetime event handler
1718
    .speed_switcher = NULL,             // no speed switcher
1719
    .mode_switcher  = NULL,             // no mode switcher
1720
    .rate_switcher  = NULL,             // no sample-rate switcher
1721
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
1722
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
1723
    .control_send   = NULL,             // how to send control strings
1724
    .time_offset     = NULL,            // no method for NTP fudge factor
1725
};
1726
// *INDENT-ON*
1727
1728
1729
// *INDENT-OFF*
1730
const struct gps_type_t driver_pps = {
1731
    .type_name      = "PPS",            // full name of type
1732
    .packet_type    = BAD_PACKET,       // associated lexer packet type
1733
    .flags          = DRIVER_NOFLAGS,   // don't remember this
1734
    .trigger        = NULL,             // it's the default
1735
    .channels       = 0,                // not used
1736
    .probe_detect   = NULL,             // no probe
1737
    .get_packet     = NULL,             // use generic packet getter
1738
    .parse_packet   = NULL,             // how to interpret a packet
1739
    .rtcm_writer    = NULL,             // write RTCM data straight
1740
    .init_query     = NULL,             // non-perturbing initial query
1741
    .event_hook     = NULL,             // lifetime event handler
1742
    .speed_switcher = NULL,             // no speed switcher
1743
    .mode_switcher  = NULL,             // no mode switcher
1744
    .rate_switcher  = NULL,             // no sample-rate switcher
1745
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
1746
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
1747
    .control_send   = NULL,             // how to send control strings
1748
    .time_offset     = NULL,            // no method for NTP fudge factor
1749
};
1750
1751
static const struct gps_type_t driver_spartn = {
1752
    .type_name     = "SPARTN",          // full name of type
1753
    .packet_type   = SPARTN_PACKET,     // associated lexer packet type
1754
    .flags         = DRIVER_NOFLAGS,    // no rollover or other flags
1755
    .trigger       = NULL,              // no recognition string
1756
    .channels      = 0,                 // not used
1757
    .probe_detect  = NULL,              // no probe
1758
    .get_packet    = NULL,              // use generic packet getter
1759
    .parse_packet  = spartn_parse,      //
1760
    .rtcm_writer   = NULL,              // Do not write back to NTRIP!
1761
    .init_query    = NULL,              // non-perturbing initial query
1762
    .event_hook    = NULL,              // no event hook
1763
    .speed_switcher= NULL,              // no speed switcher
1764
    .mode_switcher = NULL,              // no mode switcher
1765
    .rate_switcher = NULL,              // no sample-rate switcher
1766
    // cycle not relevant, no rate switch, but can generate log noise
1767
    .min_cycle.tv_sec  = 10,            // Some NTRIP servers are bursty
1768
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
1769
    .control_send   = NULL,             // how to send control strings
1770
    .time_offset     = NULL,
1771
};
1772
// *INDENT-ON*
1773
1774
extern const struct gps_type_t driver_allystar;
1775
extern const struct gps_type_t driver_casic;
1776
extern const struct gps_type_t driver_evermore;
1777
extern const struct gps_type_t driver_garmin_ser_binary;
1778
extern const struct gps_type_t driver_garmin_usb_binary;
1779
extern const struct gps_type_t driver_geostar;
1780
extern const struct gps_type_t driver_greis;
1781
extern const struct gps_type_t driver_italk;
1782
extern const struct gps_type_t driver_navcom;
1783
extern const struct gps_type_t driver_nmea2000;
1784
extern const struct gps_type_t driver_oncore;
1785
extern const struct gps_type_t driver_sirf;
1786
extern const struct gps_type_t driver_skytraq;
1787
extern const struct gps_type_t driver_superstar2;
1788
extern const struct gps_type_t driver_tsip;
1789
extern const struct gps_type_t driver_ubx;
1790
extern const struct gps_type_t driver_zodiac;
1791
1792
// the point of this rigamarole is to not have to export a table size
1793
static const struct gps_type_t *gpsd_driver_array[] = {
1794
    &driver_unknown,
1795
    &driver_nmea0183,
1796
    &driver_allystar,
1797
    &driver_is,
1798
    &driver_ashtech,
1799
    &driver_casic,
1800
#ifdef TRIPMATE_ENABLE
1801
        &driver_tripmate,
1802
#endif  // TRIPMATE_ENABLE
1803
#ifdef EARTHMATE_ENABLE
1804
        &driver_earthmate,
1805
#endif  // EARTHMATE_ENABLE
1806
#ifdef GPSCLOCK_ENABLE
1807
        &driver_gpsclock,
1808
#endif  // GPSCLOCK_ENABLE
1809
#ifdef GARMIN_ENABLE
1810
        &driver_garmin,
1811
#endif  // GARMIN_ENABLE
1812
    &driver_mtk3301,
1813
#ifdef FV18_ENABLE
1814
        &driver_fv18,
1815
#endif  // FV18_ENABLE
1816
#ifdef TNT_ENABLE
1817
        &driver_trueNorth,
1818
#endif  // TNT_ENABLE
1819
#ifdef FURY_ENABLE
1820
        &driver_fury,
1821
#endif  // FURY_ENABLE
1822
#ifdef AIVDM_ENABLE
1823
        &driver_aivdm,
1824
#endif  // AIVDM_ENABLE
1825
1826
#ifdef EVERMORE_ENABLE
1827
        &driver_evermore,
1828
#endif  // EVERMORE_ENABLE
1829
#ifdef GARMIN_ENABLE
1830
        // be sure to try Garmin Serial Binary before Garmin USB Binary
1831
        &driver_garmin_ser_binary,
1832
        &driver_garmin_usb_binary,
1833
#endif  // GARMIN_ENABLE
1834
#ifdef GEOSTAR_ENABLE
1835
        &driver_geostar,
1836
#endif  // GEOSTAR_ENABLE
1837
#ifdef GREIS_ENABLE
1838
        &driver_greis,
1839
#endif  // GREIS_ENABLE
1840
#ifdef ITRAX_ENABLE
1841
        &driver_italk,
1842
#endif  // ITRAX_ENABLE
1843
#ifdef ONCORE_ENABLE
1844
        &driver_oncore,
1845
#endif  // ONCORE_ENABLE
1846
#ifdef NAVCOM_ENABLE
1847
        &driver_navcom,
1848
#endif  // NAVCOM_ENABLE
1849
#ifdef SIRF_ENABLE
1850
        &driver_sirf,
1851
#endif  // SIRF_ENABLE
1852
#ifdef SKYTRAQ_ENABLE
1853
        &driver_skytraq,
1854
#endif  // SKYTRAQ_ENABLE
1855
    &driver_spartn,
1856
#ifdef SUPERSTAR2_ENABLE
1857
        &driver_superstar2,
1858
#endif  // SUPERSTAR2_ENABLE
1859
#ifdef TSIP_ENABLE
1860
        &driver_tsip,
1861
#endif  // TSIP_ENABLE
1862
#ifdef ISYNC_ENABLE
1863
        &driver_isync,
1864
#endif  // ISYNC_ENABLE
1865
    &driver_ubx,
1866
#ifdef ZODIAC_ENABLE
1867
        &driver_zodiac,
1868
#endif  // ZODIAC_ENABLE
1869
1870
#ifdef NMEA2000_ENABLE
1871
        &driver_nmea2000,
1872
#endif  // NMEA2000_ENABLE
1873
1874
    &driver_rtcm104v2,
1875
    &driver_rtcm104v3,
1876
#ifdef GARMINTXT_ENABLE
1877
        &driver_garmintxt,
1878
#endif  // GARMINTXT_ENABLE
1879
1880
    &driver_json_passthrough,
1881
    &driver_pps,
1882
    NULL,
1883
};
1884
1885
const struct gps_type_t **gpsd_drivers = &gpsd_driver_array[0];
1886
// vim: set expandtab shiftwidth=4