Coverage Report

Created: 2026-02-26 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gpsd/gpsd-3.27.6~dev/drivers/driver_nmea0183.c
Line
Count
Source
1
/*
2
 * Driver for NMEA 0183 protocol, aka IEC 61162-1
3
 * There are many versions of NMEA 0183.
4
 *
5
 * IEC 61162-1:1995
6
 * IEC 61162-1:2000
7
 * IEC 61162-1:2007
8
 * NMEA 4.00 aligns with IEC 61162-1:2010
9
 * NMEA 4.10 aligns with IEC 61162-1:2016
10
 *
11
 * Sadly, the protocol is proprietary and not documented publicly.
12
 * So every firmware seems to have a different opinion on how
13
 * to implement the messages.
14
 *
15
 * This file is Copyright by the GPSD project
16
 * SPDX-License-Identifier: BSD-2-clause
17
 */
18
19
#include "../include/gpsd_config.h"  // must be before all includes
20
21
#include <ctype.h>       // for isdigit()
22
#include <float.h>       // for FLT_EVAL_METHOD
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <stdbool.h>
26
#include <math.h>
27
#include <string.h>
28
#include <stdarg.h>
29
#include <time.h>
30
31
#include "../include/gpsd.h"
32
#include "../include/strfuncs.h"
33
34
#include "../include/timespec.h"
35
36
/* hex2uchar() -- convert a signgle hex char to an insigned char
37
 *
38
 * Return: 0 on error
39
 *         The converted char
40
 */
41
0
static unsigned char hex2uchar(unsigned char hex) {
42
43
0
    if ('0' <= hex &&
44
0
        '9' >= hex) {
45
0
        return hex - '0';
46
0
    }
47
0
    if ('A' <= hex &&
48
0
        'F' >= hex) {
49
0
        return hex - 'A' + 10;
50
0
    }
51
0
    if ('a' <= hex &&
52
0
        'f' >= hex) {
53
0
        return hex - 'a' + 10;
54
0
    }
55
    // fail
56
0
    return 0;
57
0
}
58
59
// $SNRSTAAT insstatus
60
static const struct vlist_t vsnrstat_insstatus[] = {
61
    {-1, "Failure"},
62
    {0, "Disabled"},
63
    {1, "Init started"},
64
    {2, "Known inst angle"},
65
    {3, "Init OK"},
66
    {0, NULL},
67
};
68
69
// $SNRSTAAT odostatus
70
static const struct vlist_t vsnrstat_odostatus[] = {
71
    {-1, "Failure"},
72
    {0, "Disabled"},
73
    {1, "Init started"},
74
    {2, "Known scale"},
75
    {3, "Init OK"},
76
    {0, NULL},
77
};
78
79
// $SNRSTAAT InstallState
80
static const struct vlist_t vsnrstat_InstallState[] = {
81
    {-1, "Failure"},
82
    {0, "In progress"},
83
    {1, "Weak Sats"},
84
    {2, "Need Acc"},
85
    {3, "Low Speed"},
86
    {0, NULL},
87
};
88
89
// $SNRSTAAT mapstat
90
static const struct vlist_t vsnrstat_mapstat[] = {
91
    {-2, "Abnormal"},
92
    {-1, "Unconfigured"},
93
    {0, "No info"},
94
    {1, "Unapplied"},
95
    {1, "OK"},
96
    {0, NULL},
97
};
98
99
/**************************************************************************
100
 *
101
 * Parser helpers begin here
102
 *
103
 **************************************************************************/
104
105
/* Allow avoiding long double intermediate values.
106
 *
107
 * On platforms with 0 != FLT_EVAL_METHOD intermediate values may be kept
108
 * as long doubles.  Some 32-bit OpenBSD and 32-bit Debian have
109
 * FLT_EVAL_METHOD == 2.  FreeBSD 13,0 has FLT_EVAL_METHOD == -1.  Various
110
 * cc options (-mfpmath=387, -mno-sse, etc.) can also change FLT_EVAL_METHOD
111
 * from 0.
112
 *
113
 * Although (long double) may in principle more accurate then (double), it
114
 * can cause slight differences that lead to regression failures.  In
115
 * other cases (long double) and (double) are the same, thus no effect.
116
 * Storing values in volatile variables forces the exact size requested.
117
 * Where the volatile declaration is unnecessary (and absent), such extra
118
 * intermediate variables are normally optimized out.
119
 */
120
121
#if !defined(FLT_EVAL_METHOD) || 0 != FLT_EVAL_METHOD
122
#define FLT_VOLATILE volatile
123
#else
124
#define FLT_VOLATILE
125
#endif   // FLT_EVAL_METHOD
126
127
/* Common lat/lon decoding for do_lat_lon
128
 *
129
 * This version avoids the use of modf(), which can be slow and also suffers
130
 * from exactness problems.  The integer minutes are first extracted and
131
 * corrected for the improper degree scaling, using integer arithmetic.
132
 * Then the fractional minutes are added as a double, and the result is scaled
133
 * to degrees, using multiply which is faster than divide.
134
 *
135
 * Forcing the intermediate minutes value to a double is sufficient to
136
 * avoid regression problems with FLT_EVAL_METHOD>=2.
137
 */
138
static inline double decode_lat_or_lon(const char *field)
139
0
{
140
0
    long degrees, minutes;
141
0
    FLT_VOLATILE double full_minutes;
142
0
    char *cp;
143
144
    // Get integer "minutes"
145
0
    minutes = strtol(field, &cp, 10);
146
    // Must have decimal point
147
0
    if ('.' != *cp) {
148
0
        return NAN;
149
0
    }
150
    // Extract degrees (scaled by 100)
151
0
    degrees = minutes / 100;
152
    // Rescale degrees to normal factor of 60
153
0
    minutes -= degrees * (100 - 60);
154
    // Add fractional minutes
155
0
    full_minutes = minutes + safe_atof(cp);
156
    // Scale to degrees & return
157
0
    return full_minutes * (1.0 / 60.0);
158
0
}
159
160
/* process a pair of latitude/longitude fields starting at field index BEGIN
161
 * The input fields look like this:
162
 *     field[0]: 4404.1237962
163
 *     field[1]: N
164
 *     field[2]: 12118.8472460
165
 *     field[3]: W
166
 * input format of lat/lon is NMEA style  DDDMM.mmmmmmm
167
 * yes, 7 digits of precision past the decimal point from survey grade GPS
168
 *
169
 * Ignoring the complications ellipsoids add:
170
 *   1 minute latitude = 1853 m
171
 *   0.001 minute latitude = 1.853 m
172
 *   0.000001 minute latitude = 0.001853 m = 1.853 mm
173
 *   0.0000001 minute latitude = 0.0001853 m = 0.1853 mm
174
 *
175
 * return: 0 == OK, non zero is failure.
176
 */
177
static int do_lat_lon(char *field[], struct gps_fix_t *out)
178
0
{
179
0
    double lon;
180
0
    double lat;
181
182
0
    if ('\0' == field[0][0] ||
183
0
        '\0' == field[1][0] ||
184
0
        '\0' == field[2][0] ||
185
0
        '\0' == field[3][0]) {
186
0
        return 1;
187
0
    }
188
189
0
    lat = decode_lat_or_lon(field[0]);
190
0
    if ('S' == field[1][0])
191
0
        lat = -lat;
192
193
0
    lon = decode_lat_or_lon(field[2]);
194
0
    if ('W' == field[3][0])
195
0
        lon = -lon;
196
197
0
    if (0 == isfinite(lat) ||
198
0
        0 == isfinite(lon)) {
199
0
        return 2;
200
0
    }
201
202
0
    out->latitude = lat;
203
0
    out->longitude = lon;
204
0
    return 0;
205
0
}
206
207
// decode for FAA Mode indicator.  NMEA 4+
208
static const struct clist_t c_faa_mode[] = {
209
    {'A', "Autonomous"},
210
    {'C', "Caution"},        // Quectel Querk
211
    {'D', "Differential"},
212
    {'E', "Estimated"},      // dead reckoning)
213
    {'F', "Float RTK"},
214
    {'M', "Manual Input."},  // surveyed)
215
    {'N', "Data Not Valid"},
216
    {'0', "Unk"},            // Skytraq??
217
    {'P', "Precise"},        // (NMEA 4+)
218
    {'R', "Integer RTK"},
219
    {'S', "Simulated"},
220
    {'U', "Unsafe"},         // Quectel querk
221
    {'V', "Invalid"},        // ??
222
    {'\0', NULL}
223
};
224
225
/* process an FAA mode character
226
 * As used in $GPRMC (field 13) and similar.
227
 * return status as in session->newdata.status
228
 */
229
static int faa_mode(char mode)
230
0
{
231
0
    int newstatus = STATUS_GPS;
232
233
0
    switch (mode) {
234
0
    case '\0':  // missing
235
0
        FALLTHROUGH
236
0
    case 'O':  // Skytraq ??
237
0
        FALLTHROUGH
238
0
    case 'V':   // Invalid
239
0
        newstatus = STATUS_UNK;
240
0
        break;
241
0
    case 'A':   // Autonomous
242
0
        FALLTHROUGH
243
0
    default:
244
0
        newstatus = STATUS_GPS;
245
0
        break;
246
0
    case 'D':   // Differential
247
0
        newstatus = STATUS_DGPS;
248
0
        break;
249
0
    case 'E':   // Estimated dead reckoning
250
0
        newstatus = STATUS_DR;
251
0
        break;
252
0
    case 'F':   // Float RTK
253
0
        newstatus = STATUS_RTK_FLT;
254
0
        break;
255
0
    case 'M':   // manual input.  Interpret as surveyed to better match GGA
256
0
        newstatus = STATUS_TIME;
257
0
        break;
258
0
    case 'N':   // Data Not Valid
259
        // already handled, for paranoia sake also here
260
0
        newstatus = STATUS_UNK;
261
0
        break;
262
0
    case 'P':   // Precise (NMEA 4+)
263
0
        newstatus = STATUS_DGPS;    // sort of DGPS
264
0
        break;
265
0
    case 'R':   // fixed RTK
266
0
        newstatus = STATUS_RTK_FIX;
267
0
        break;
268
0
    case 'S':   // simulator
269
0
        newstatus = STATUS_SIM;
270
0
        break;
271
0
    }
272
0
    return newstatus;
273
0
}
274
275
/**************************************************************************
276
 *
277
 * Scary timestamp fudging begins here
278
 *
279
 * Four sentences, GGA and GLL and RMC and ZDA, contain timestamps.
280
 * GGA/GLL/RMC timestamps look like hhmmss.ss, with the trailing .ss,
281
 * or .sss, part optional.
282
 * RMC has a date field, in the format ddmmyy.  ZDA has separate fields
283
 * for day/month/year, with a 4-digit year.  This means that for RMC we
284
 * must supply a century and for GGA and GLL we must supply a century,
285
 * year, and day.  We get the missing data from a previous RMC or ZDA;
286
 * century in RMC is supplied from the daemon's context (initialized at
287
 * startup time) if there has been no previous ZDA.
288
 *
289
 **************************************************************************/
290
291
0
#define DD(s)   ((int)((s)[0]-'0')*10+(int)((s)[1]-'0'))
292
293
/* decode supplied ddmmyy, but no century part, into *date
294
 *
295
 * return: 0 == OK,  greater than zero on failure
296
 */
297
static int decode_ddmmyy(struct tm *date, const char *ddmmyy,
298
                         struct gps_device_t *session)
299
0
{
300
0
    int mon;
301
0
    int mday;
302
0
    int year;
303
0
    unsigned i;    // NetBSD complains about signed array index
304
305
0
    if (NULL == ddmmyy ||
306
0
        '\0' == ddmmyy[0]) {
307
0
        return 1;
308
0
    }
309
0
    for (i = 0; i < 6; i++) {
310
        // NetBSD 6 wants the cast
311
0
        if (0 == isdigit((int)ddmmyy[i])) {
312
            // catches NUL and non-digits
313
            // Telit HE910 can set year to "-1" (1999 - 2000)
314
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
315
0
                     "NMEA0183: merge_ddmmyy(%s), malformed date\n",  ddmmyy);
316
0
            return 2;
317
0
        }
318
0
    }
319
    // check for termination
320
0
    if ('\0' != ddmmyy[6]) {
321
        // missing NUL
322
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
323
0
                 "NMEA0183: merge_ddmmyy(%s), malformed date\n",  ddmmyy);
324
0
        return 3;
325
0
    }
326
327
    // should be no defects left to segfault DD()
328
0
    mday = DD(ddmmyy);
329
0
    mon = DD(ddmmyy + 2);
330
0
    year = DD(ddmmyy + 4);
331
332
    // check for century wrap, so 1968 < year < 2069
333
0
    if (69 > year) {
334
0
        year += 100;
335
0
    }
336
337
0
    if (!IN(1, mon, 12)) {
338
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
339
0
                 "NMEA0183: merge_ddmmyy(%s), malformed month\n",  ddmmyy);
340
0
        return 4;
341
0
    }  // else
342
0
    if (!IN(1, mday, 31)) {
343
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
344
0
                 "NMEA0183: merge_ddmmyy(%s), malformed day\n",  ddmmyy);
345
0
        return 5;
346
0
    }  // else
347
348
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
349
0
             "NMEA0183: merge_ddmmyy(%s) sets year %d\n",
350
0
             ddmmyy, year);
351
0
    date->tm_year = year;
352
0
    date->tm_mon = mon - 1;
353
0
    date->tm_mday = mday;
354
    // FIXME: check fractional time!
355
356
0
    GPSD_LOG(LOG_RAW, &session->context->errout,
357
0
             "NMEA0183: merge_ddmmyy(%s) %d %d %d\n",
358
0
             ddmmyy, date->tm_mon, date->tm_mday, date->tm_year);
359
0
    return 0;
360
0
}
361
362
/* sentence supplied ddmmyy, but no century part
363
 * iff valid, merge into session_>nmea.date
364
 *
365
 * return: 0 == OK,  greater than zero on failure
366
 */
367
static int merge_ddmmyy(const char *ddmmyy, struct gps_device_t *session)
368
0
{
369
0
    struct tm date = {0};
370
0
    int retcode;
371
372
0
    retcode = decode_ddmmyy(&date, ddmmyy, session);
373
0
    if (0 != retcode) {
374
        // leave session->nmea untouched.
375
0
        return retcode;
376
0
    }
377
    // check for century wrap ??
378
    // Good time, merge it.
379
0
    session->nmea.date.tm_mday = date.tm_mday;
380
0
    session->nmea.date.tm_mon = date.tm_mon;
381
0
    session->nmea.date.tm_year = date.tm_year;
382
0
    return 0;
383
0
}
384
385
/* decode an hhmmss.ss string into struct tm data and nsecs
386
 *
387
 * return: 0 == OK,  otherwise failure
388
 */
389
static int decode_hhmmss(struct tm *date, long *nsec, const char *hhmmss,
390
                         struct gps_device_t *session)
391
0
{
392
0
    int old_hour = date->tm_hour;
393
0
    int i;
394
395
0
    if (NULL == hhmmss ||
396
0
        '\0' == hhmmss[0]) {
397
0
        return 1;
398
0
    }
399
0
    for (i = 0; i < 6; i++) {
400
        // NetBSD 6 wants the cast
401
0
        if (0 == isdigit((int)hhmmss[i])) {
402
            // catches NUL and non-digits
403
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
404
0
                     "NMEA0183: decode_hhmmss(%s), malformed time\n",  hhmmss);
405
0
            return 2;
406
0
        }
407
0
    }
408
    // don't check for termination, might have fractional seconds
409
410
0
    date->tm_hour = DD(hhmmss);
411
0
    if (date->tm_hour < old_hour) {  // midnight wrap
412
        // really??
413
0
        date->tm_mday++;
414
0
    }
415
0
    date->tm_min = DD(hhmmss + 2);
416
0
    date->tm_sec = DD(hhmmss + 4);
417
418
0
    if ('.' == hhmmss[6] &&
419
        // NetBSD 6 wants the cast
420
0
        0 != isdigit((int)hhmmss[7])) {
421
        // codacy hates strlen()
422
0
        int sublen = strnlen(hhmmss + 7, 20);
423
0
        i = atoi(hhmmss + 7);
424
0
        *nsec = (long)i * (long)pow(10.0, 9 - sublen);
425
0
    } else {
426
0
        *nsec = 0;
427
0
    }
428
0
    GPSD_LOG(LOG_RAW, &session->context->errout,
429
0
             "NMEA0183: decode_hhmmss(%s) %d %d %d %09ld\n",
430
0
             hhmmss,
431
0
             date->tm_hour, date->tm_min, date->tm_sec, *nsec);
432
433
0
    return 0;
434
0
}
435
436
/* decode an hhmmss UTC time
437
 * if valid, merge into:
438
 *      session->nmea.date
439
 *      session->nmea.subseconds
440
 *
441
 * return: 0 == OK,  greater than zero on failure
442
 */
443
static int merge_hhmmss(const char *hhmmss, struct gps_device_t *session)
444
0
{
445
0
    struct tm date = {0};
446
0
    timespec_t ts = {0};
447
0
    int retcode;
448
449
0
    retcode = decode_hhmmss(&date, &ts.tv_nsec, hhmmss, session);
450
0
    if (0 != retcode) {
451
        // leave session->nmea untouched.
452
0
        return retcode;
453
0
    }
454
    // Good time, merge it.
455
0
    session->nmea.date.tm_hour = date.tm_hour;
456
0
    session->nmea.date.tm_min = date.tm_min;
457
0
    session->nmea.date.tm_sec = date.tm_sec;
458
0
    session->nmea.subseconds.tv_sec = 0;
459
0
    session->nmea.subseconds.tv_nsec = ts.tv_nsec;
460
461
0
    return 0;
462
0
}
463
464
/* register_fractional_time()
465
 * "fractional time" is a struct timespec of seconds since midnight
466
 * used to try to detect epoch changes as NMEA comes in.
467
 * tag is field[0]
468
 * *fld is "hhmmss.ss"
469
 */
470
static void register_fractional_time(const char *tag, const char *fld,
471
                                     struct gps_device_t *session)
472
0
{
473
0
    struct tm date = {0};
474
0
    struct timespec ts = {0};
475
0
    char ts_buf[TIMESPEC_LEN];
476
477
0
    if (0 != decode_hhmmss(&date, &ts.tv_nsec, fld, session)) {
478
        // invalid time
479
0
        return;
480
0
    }
481
482
0
    ts.tv_sec = date.tm_hour * 3600 + date.tm_min * 60 + date.tm_sec;
483
484
0
    session->nmea.last_frac_time = session->nmea.this_frac_time;
485
0
    session->nmea.this_frac_time = ts;
486
0
    session->nmea.latch_frac_time = true;
487
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
488
0
             "NMEA0183: %s: registers fractional time %s\n",
489
0
             tag,
490
0
             timespec_str(&session->nmea.this_frac_time, ts_buf,
491
0
                          sizeof(ts_buf)));
492
0
}
493
494
/* Table to convert nmea sigid to ubx sigid (row index for nmea gnssid and
495
 * column index for nmea sigid).
496
 * 99 means unknown conversion.
497
 * Note: not all dcumented, some deduced by comparing UBX and NMEA.
498
 */
499
0
#define NMEA_GNSSIDS 7
500
0
#define NMEA_SIGIDS 12
501
static const unsigned char nmea_to_ubx_table[NMEA_GNSSIDS][NMEA_SIGIDS] = {
502
        {0, 0, 99, 99, 99, 4, 3, 6, 7, 99, 99, 99},       // Unknown assume GPS
503
        {0, 4, 99, 99, 99, 4, 3, 6, 7, 99, 99, 99},       // GPS
504
        {0, 0, 99, 2, 99, 99, 99, 99, 99, 99, 99, 99},    // GLONASS
505
        // Quectel uses sigid 6 for L1-A ?
506
        {0, 3, 5, 99, 10, 8, 0, 4, 99, 99, 99, 99},       // Galileo
507
        // BeiDou B could be UBX 2 or 3
508
        {0, 0, 2, 5, 0, 7, 99, 99, 4, 99, 99, 2},         // BeiDou
509
        {0, 0, 99, 99, 1, 4, 5, 8, 9, 99, 99, 99},        // QZSS
510
        {0, 0, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99}};  // IRNSS (NavIC)
511
512
// convert NMEA sigid to ublox sigid
513
static unsigned char nmea_sigid_to_ubx(struct gps_device_t *session,
514
                                       unsigned char nmea_gnssid,
515
                                       unsigned char nmea_sigid)
516
0
{
517
0
    unsigned char ubx_sigid = 0;
518
519
0
    if ((NMEA_GNSSIDS > nmea_gnssid) &&
520
0
        (NMEA_SIGIDS > nmea_sigid)) {
521
0
        ubx_sigid = nmea_to_ubx_table[nmea_gnssid][nmea_sigid];
522
0
        if (99 == ubx_sigid) {
523
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
524
0
                     "NMEA0183: Unknown map nmea_gnssid:sigid %u:%d\n",
525
0
                     nmea_gnssid, nmea_sigid);
526
0
            ubx_sigid = 0;
527
0
        }
528
0
    } else {
529
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
530
0
                 "NMEA0183: Unknown nmea_sigid %u with nmea_gnssid %u\n",
531
0
                 nmea_sigid, nmea_gnssid);
532
0
    }
533
534
0
    return ubx_sigid;
535
0
}
536
537
/* Deal with range-mapping attempts to use IDs 1-32 by Beidou, etc.
538
 *
539
 * See struct satellite_t in gps.h for ubx and nmea gnssid and svid mappings
540
 *
541
 * char *talker              -- NMEA talker string
542
 * int nmea_satnum           -- NMEA (All ver) satellite number (kinda the PRN)
543
 * int nmea_gnssid           -- NMEA 4.10 gnssid, if known, otherwise zero
544
 * unsigned char *ubx_gnssid -- returned u-blox gnssid
545
 * unsigned char *ubx_svid   -- returned u-blox gnssid
546
 *
547
 * Return the NMEA 2.x to 4.0 extended PRN
548
 */
549
static int nmeaid_to_prn(char *talker, int nmea_satnum,
550
                         int nmea_gnssid,
551
                         unsigned char *ubx_gnssid,
552
                         unsigned char *ubx_svid)
553
0
{
554
    /*
555
     * According to https://github.com/mvglasow/satstat/wiki/NMEA-IDs
556
     * and u-blox documentation.
557
     * NMEA IDs can be roughly divided into the following ranges:
558
     *
559
     *   1..32:  GPS
560
     *   33..64: Various SBAS systems (EGNOS, WAAS, SDCM, GAGAN, MSAS)
561
     *   65..96: GLONASS
562
     *   101..136: Quectel Querk, (not NMEA), seems to be Galileo
563
     *   152..158: Various SBAS systems (EGNOS, WAAS, SDCM, GAGAN, MSAS)
564
     *   173..182: IMES
565
     *   193..202: QZSS   (u-blox extended 4.10)
566
     *   201..264: BeiDou (not NMEA, not u-blox?) Quectel Querk.
567
     *   301..336: Galileo
568
     *   401..437: BeiDou
569
     *   null: GLONASS unused
570
     *   500-509: NavIC (IRNSS)  NOT STANDARD!
571
     *   901..918: NavIC (IRNSS), ALLYSTAR
572
     *
573
     * The issue is what to do when GPSes from these different systems
574
     * fight for IDs in the  1-32 range, as in this pair of Beidou sentences
575
     *
576
     * $BDGSV,2,1,07,01,00,000,45,02,13,089,35,03,00,000,37,04,00,000,42*6E
577
     * $BDGSV,2,2,07,05,27,090,,13,19,016,,11,07,147,*5E
578
     *
579
     * Because the PRNs are only used for generating a satellite
580
     * chart, mistakes here aren't dangerous.  The code will record
581
     * and use multiple sats with the same ID in one skyview; in
582
     * effect, they're recorded by the order in which they occur
583
     * rather than by PRN.
584
     */
585
0
    int nmea2_prn = nmea_satnum;
586
587
0
    *ubx_gnssid = 0;   // default to ubx_gnssid is GPS
588
0
    *ubx_svid = 0;     // default to unknown ubx_svid
589
590
0
    if (1 > nmea_satnum) {
591
        // uh, oh...
592
0
        nmea2_prn = 0;
593
0
    } else if (0 < nmea_gnssid) {
594
        // this switch handles case where nmea_gnssid is known
595
0
        switch (nmea_gnssid) {
596
0
        case 1:
597
0
            if (33 > nmea_satnum) {
598
                // 1 = GPS       1-32
599
0
                *ubx_gnssid = 0;
600
0
                *ubx_svid = nmea_satnum;
601
0
            } else if (65 > nmea_satnum) {
602
                // 1 = SBAS      33-64
603
0
                *ubx_gnssid = 1;
604
0
                *ubx_svid = nmea_satnum + 87;
605
0
            } else if (137 > nmea_satnum) {
606
                // 3 = Galileo, 101-136, NOT NMEA.  Quectel Querk
607
0
                *ubx_gnssid = 3;
608
0
                *ubx_svid = nmea_satnum - 100;
609
0
            } else if (152 > nmea_satnum) {
610
                // Huh?
611
0
                *ubx_gnssid = 0;
612
0
                *ubx_svid = 0;
613
0
                nmea2_prn = 0;
614
0
            } else if (158 > nmea_satnum) {
615
                // 1 = SBAS      152-158
616
0
                *ubx_gnssid = 1;
617
0
                *ubx_svid = nmea_satnum;
618
0
            } else if (193 > nmea_satnum) {
619
                // Huh?
620
0
                *ubx_gnssid = 0;
621
0
                *ubx_svid = 0;
622
0
                nmea2_prn = 0;
623
0
            } else if (200 > nmea_satnum) {
624
                // 1 = QZSS      193-197
625
                // undocumented u-blox goes to 199
626
0
                *ubx_gnssid = 3;
627
0
                *ubx_svid = nmea_satnum - 192;
628
0
            } else if (265 > nmea_satnum) {
629
                // 3 = BeiDor, 201-264, NOT NMEA.  Quectel Querk
630
0
                *ubx_gnssid = 3;
631
0
                *ubx_svid = nmea_satnum - 200;
632
0
            } else {
633
                // Huh?
634
0
                *ubx_gnssid = 0;
635
0
                *ubx_svid = 0;
636
0
                nmea2_prn = 0;
637
0
            }
638
0
            break;
639
0
        case 2:
640
            //  2 = GLONASS   65-96, nul
641
0
            *ubx_gnssid = 6;
642
0
            if (64 > nmea_satnum) {
643
                // NMEA svid 1 - 64
644
0
                *ubx_svid = nmea_satnum;
645
0
            } else {
646
                /* Jackson Labs Micro JLT, Quectel Querk, SiRF, Skytrak,
647
                 * u-blox quirk: GLONASS are  65 to 96 */
648
0
                *ubx_svid = nmea_satnum - 64;
649
0
            }
650
0
            nmea2_prn = 64 + *ubx_svid;
651
0
            break;
652
0
        case 3:
653
            //  3 = Galileo   1-36
654
0
            *ubx_gnssid = 2;
655
0
            if (100 > nmea_satnum) {
656
                // NMEA
657
0
                *ubx_svid = nmea_satnum;
658
0
            } else if (100 < nmea_satnum &&
659
0
                       200 > nmea_satnum) {
660
                // Quectel Querk, NOT NMEA, 101 - 199
661
0
                *ubx_svid = nmea_satnum - 100;
662
0
            } else if (300 < nmea_satnum &&
663
0
                       400 > nmea_satnum) {
664
                // Jackson Labs quirk, NOT NMEA, 301 - 399
665
0
                *ubx_svid = nmea_satnum - 300;
666
0
            }
667
0
            nmea2_prn = 300 + *ubx_svid;    // 301 - 399
668
0
            break;
669
0
        case 4:
670
            //  4 - BeiDou    1-37
671
0
            *ubx_gnssid = 3;
672
0
            if (100 > nmea_satnum) {
673
                // NMEA 1 - 99
674
0
                *ubx_svid = nmea_satnum;
675
0
            } else if (200 < nmea_satnum &&
676
0
                       300 > nmea_satnum) {
677
                // Quectel Querk, NOT NMEA, 201 - 299
678
0
                *ubx_svid = nmea_satnum - 200;
679
0
            } else if (400 < nmea_satnum &&
680
0
                       500 > nmea_satnum) {
681
                // Jackson Labs quirk, NOT NMEA, 401 - 499
682
0
                *ubx_svid = nmea_satnum - 400;
683
0
            }
684
            // put it at 400+ where NMEA 4.11 wants it
685
0
            nmea2_prn = 400 + *ubx_svid;
686
0
            break;
687
0
        case 5:
688
            //  5 - QZSS, 1 - 10, NMEA 4.11
689
0
            *ubx_gnssid = 5;
690
0
            if (100 > nmea_satnum) {
691
                // NMEA 1 - 99
692
0
                *ubx_svid = nmea_satnum;
693
0
            } else {
694
                // Telit quirk, not NMEA 193 - 199
695
0
                *ubx_svid = nmea_satnum - 192;
696
0
            }
697
698
            // put it at 193 to 199 where NMEA 4.11 wants it
699
            // huh?  space for only 7?
700
0
            nmea2_prn = 192 + *ubx_svid;
701
0
            break;
702
0
        case 6:
703
            //  6 - NavIC (IRNSS)    1-15
704
0
            *ubx_gnssid = 7;
705
0
            *ubx_svid = nmea_satnum;
706
0
            nmea2_prn = nmea_satnum + 500;  // This is wrong...
707
0
            break;
708
0
        default:
709
            // unknown
710
            // x = IMES                Not defined by NMEA 4.10
711
0
            nmea2_prn = 0;
712
0
            break;
713
0
        }
714
715
    /* left with NMEA 2.x to NMEA 4.0 satnums
716
     * use talker ID to disambiguate */
717
0
    } else if (32 >= nmea_satnum) {
718
0
        *ubx_svid = nmea_satnum;
719
0
        switch (talker[0]) {
720
0
        case 'G':
721
0
            switch (talker[1]) {
722
0
            case 'A':
723
                // Galileo
724
0
                nmea2_prn = 300 + nmea_satnum;
725
0
                *ubx_gnssid = 2;
726
0
                break;
727
0
            case 'B':
728
                // map Beidou IDs 1..37 to 401..437
729
0
                *ubx_gnssid = 3;
730
0
                nmea2_prn = 400 + nmea_satnum;
731
0
                break;
732
0
            case 'I':
733
                // map NavIC (IRNSS) IDs 1..10 to 500 - 509, not NMEA
734
0
                *ubx_gnssid = 7;
735
0
                nmea2_prn = 500 + nmea_satnum;
736
0
                break;
737
0
            case 'L':
738
                // GLONASS GL doesn't seem to do this, better safe than sorry
739
0
                nmea2_prn = 64 + nmea_satnum;
740
0
                *ubx_gnssid = 6;
741
0
                break;
742
0
            case 'Q':
743
                // GQ, QZSS, 1 - 10
744
0
                nmea2_prn = 192 + nmea_satnum;
745
0
                *ubx_gnssid = 5;
746
0
                break;
747
0
            case 'N':
748
                // all of them, but only GPS is 0 < PRN < 33
749
0
                FALLTHROUGH
750
0
            case 'P':
751
                // GPS,SBAS,QZSS, but only GPS is 0 < PRN < 33
752
0
                FALLTHROUGH
753
0
            default:
754
                // WTF?
755
0
                break;
756
0
            }  // else ??
757
0
            break;
758
0
        case 'B':
759
0
            if ('D' == talker[1]) {
760
                // map Beidou IDs
761
0
                nmea2_prn = 400 + nmea_satnum;
762
0
                *ubx_gnssid = 3;
763
0
            }  // else ??
764
0
            break;
765
0
        case 'P':
766
            // Quectel EC25 & EC21 use PQxxx for BeiDou
767
0
            if ('Q' == talker[1]) {
768
                // map Beidou IDs
769
0
                nmea2_prn = 400 + nmea_satnum;
770
0
                *ubx_gnssid = 3;
771
0
            }  // else ??
772
0
            break;
773
0
        case 'Q':
774
0
            if ('Z' == talker[1]) {
775
                // QZSS
776
0
                nmea2_prn = 192 + nmea_satnum;
777
0
                *ubx_gnssid = 5;
778
0
            }  // else ?
779
0
            break;
780
0
        default:
781
            // huh?
782
0
            break;
783
0
        }
784
0
    } else if (64 >= nmea_satnum) {
785
        // NMEA-ID (33..64) to SBAS PRN 120-151.
786
        // SBAS
787
0
        *ubx_gnssid = 1;
788
0
        *ubx_svid = 87 + nmea_satnum;
789
0
    } else if (96 >= nmea_satnum) {
790
        // GLONASS 65..96
791
0
        *ubx_gnssid = 6;
792
0
        *ubx_svid = nmea_satnum - 64;
793
0
    } else if (120 > nmea_satnum) {
794
        // Huh?
795
0
        *ubx_gnssid = 0;
796
0
        *ubx_svid = 0;
797
0
        nmea2_prn = 0;
798
0
    } else if (158 >= nmea_satnum) {
799
        // SBAS 120..158
800
0
        *ubx_gnssid = 1;
801
0
        *ubx_svid = nmea_satnum;
802
0
    } else if (173 > nmea_satnum) {
803
        // Huh?
804
0
        *ubx_gnssid = 0;
805
0
        *ubx_svid = 0;
806
0
        nmea2_prn = 0;
807
0
    } else if (182 >= nmea_satnum) {
808
        // IMES 173..182
809
0
        *ubx_gnssid = 4;
810
0
        *ubx_svid = nmea_satnum - 172;
811
0
    } else if (193 > nmea_satnum) {
812
        // Huh?
813
0
        *ubx_gnssid = 0;
814
0
        *ubx_svid = 0;
815
0
        nmea2_prn = 0;
816
0
    } else if (197 >= nmea_satnum) {
817
        // QZSS 193..197
818
        // undocumented u-blox goes to 199
819
0
        *ubx_gnssid = 5;
820
0
        *ubx_svid = nmea_satnum - 192;
821
0
    } else if (201 > nmea_satnum) {
822
        // Huh?
823
0
        *ubx_gnssid = 0;
824
0
        *ubx_svid = 0;
825
0
        nmea2_prn = 0;
826
0
    } else if (237 >= nmea_satnum) {
827
        // BeiDou, non-standard, some SiRF put BeiDou 201-237
828
        // $GBGSV,2,2,05,209,07,033,*62
829
0
        *ubx_gnssid = 3;
830
0
        *ubx_svid = nmea_satnum - 200;
831
0
        nmea2_prn += 200;           // move up to 400 where NMEA 2.x wants it.
832
0
    } else if (301 > nmea_satnum) {
833
        // Huh?
834
0
        *ubx_gnssid = 0;
835
0
        *ubx_svid = 0;
836
0
        nmea2_prn = 0;
837
0
    } else if (356 >= nmea_satnum) {
838
        // Galileo 301..356
839
0
        *ubx_gnssid = 2;
840
0
        *ubx_svid = nmea_satnum - 300;
841
0
    } else if (401 > nmea_satnum) {
842
        // Huh?
843
0
        *ubx_gnssid = 0;
844
0
        *ubx_svid = 0;
845
0
        nmea2_prn = 0;
846
0
    } else if (437 >= nmea_satnum) {
847
        // BeiDou
848
0
        *ubx_gnssid = 3;
849
0
        *ubx_svid = nmea_satnum - 400;
850
0
    } else if (499 >= nmea_satnum) {
851
        // 438 to 500??
852
0
        *ubx_gnssid = 0;
853
0
        *ubx_svid = 0;
854
0
        nmea2_prn = 0;
855
0
    } else if (518 >= nmea_satnum) {
856
        // NavIC (IRNSS) IDs 1..18 to 510 - 509, not NMEA
857
0
        *ubx_gnssid = 7;
858
0
        *ubx_svid = nmea_satnum - 500;
859
0
    } else if (900 >= nmea_satnum) {
860
        // 438 to 900??
861
0
        *ubx_gnssid = 0;
862
0
        *ubx_svid = 0;
863
0
        nmea2_prn = 0;
864
0
    } else if (918 >= nmea_satnum) {
865
        // 900 to 918 NavIC (IRNSS), per ALLYSTAR (NMEA?)
866
0
        *ubx_gnssid = 7;
867
0
        *ubx_svid = nmea_satnum - 900;
868
0
    } else {
869
        // greater than 437 Huh?
870
0
        *ubx_gnssid = 0;
871
0
        *ubx_svid = 0;
872
0
        nmea2_prn = 0;
873
0
    }
874
875
0
    return nmea2_prn;
876
0
}
877
878
/**************************************************************************
879
 *
880
 * NMEA sentence handling begins here
881
 *
882
 **************************************************************************/
883
884
static gps_mask_t processACCURACY(int c UNUSED, char *field[],
885
                                  struct gps_device_t *session)
886
0
{
887
    /*
888
     * $GPACCURACY,961.2*04
889
     *
890
     * ACCURACY,x.x*hh<cr><lf>
891
     *
892
     * The only data field is "accuracy".
893
     * The MT3333 manual just says "The smaller the number is, the be better"
894
     */
895
0
    gps_mask_t mask = ONLINE_SET;
896
897
0
    if ('\0' == field[1][0]) {
898
        // no data
899
0
        return mask;
900
0
    }
901
902
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
903
0
             "NMEA0183: $GPACCURACY: %10s.\n", field[1]);
904
0
    return mask;
905
0
}
906
907
// BWC - Bearing and Distance to Waypoint - Great Circle
908
static gps_mask_t processBWC(int count, char *field[],
909
                             struct gps_device_t *session)
910
0
{
911
    /*
912
     * GPBWC,220516,5130.02,N,00046.34,W,213.8,T,218.0,M,0004.6,N,EGLM*11
913
     *
914
     * 1. UTC Time, hh is hours, mm is minutes, ss.ss is seconds
915
     * 2. Waypoint Latitude
916
     * 3. N = North, S = South
917
     * 4. Waypoint Longitude
918
     * 5. E = East, W = West
919
     * 6. Bearing, degrees True
920
     * 7. T = True
921
     * 8. Bearing, degrees Magnetic
922
     * 9. M = Magnetic
923
     * 10. Distance, Nautical Miles
924
     * 11. N = Nautical Miles
925
     * 12. Waypoint ID
926
     * 13. FAA mode indicator (NMEA 2.3 and later, optional)
927
     * 14. Checksum
928
     *
929
     * Parse this just to get the time, to help the cycle ender
930
     */
931
0
    gps_mask_t mask = ONLINE_SET;
932
933
0
    if ('\0' != field[1][0]) {
934
0
        if (0 == merge_hhmmss(field[1], session)) {
935
0
            if (0 == session->nmea.date.tm_year) {
936
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
937
0
                         "NMEA0183: can't use BWC time until after ZDA or RMC"
938
0
                         " has supplied a year.\n");
939
0
            } else {
940
0
                mask = TIME_SET;
941
0
            }
942
0
        }
943
0
    }
944
0
    if (14 <= count) {
945
        // NMEA 2.3 and later
946
0
        session->newdata.status = faa_mode(field[13][0]);
947
0
    }
948
949
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
950
0
             "NMEA0183: BWC: hhmmss=%s status %d faa mode %s(%s)\n",
951
0
             field[1], session->newdata.status,
952
0
             field[13], char2str(field[13][0], c_faa_mode));
953
0
    return mask;
954
0
}
955
956
static gps_mask_t processDBT(int c UNUSED, char *field[],
957
                             struct gps_device_t *session)
958
0
{
959
    /*
960
     * $SDDBT,7.7,f,2.3,M,1.3,F*05
961
     * 1) Depth below sounder in feet
962
     * 2) Fixed value 'f' indicating feet
963
     * 3) Depth below sounder in meters
964
     * 4) Fixed value 'M' indicating meters
965
     * 5) Depth below sounder in fathoms
966
     * 6) Fixed value 'F' indicating fathoms
967
     * 7) Checksum.
968
     *
969
     * In real-world sensors, sometimes not all three conversions are reported.
970
     */
971
0
    gps_mask_t mask = ONLINE_SET;
972
973
0
    if ('\0' != field[3][0]) {
974
0
        session->newdata.depth = safe_atof(field[3]);
975
0
        mask |= (ALTITUDE_SET);
976
0
    } else if ('\0' != field[1][0]) {
977
0
        session->newdata.depth = safe_atof(field[1]) * FEET_TO_METERS;
978
0
        mask |= (ALTITUDE_SET);
979
0
    } else if ('\0' != field[5][0]) {
980
0
        session->newdata.depth = safe_atof(field[5]) * FATHOMS_TO_METERS;
981
0
        mask |= (ALTITUDE_SET);
982
0
    }
983
984
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
985
0
             "NMEA0183: %s mode %d, depth %lf.\n",
986
0
             field[0],
987
0
             session->newdata.mode,
988
0
             session->newdata.depth);
989
0
    return mask;
990
0
}
991
992
static gps_mask_t processDPT(int c UNUSED, char *field[],
993
                             struct gps_device_t *session)
994
0
{
995
    /*
996
     * $--DPT,x.x,x.x,x.x*hh<CR><LF>
997
     * 1) Depth below sounder in meters
998
     * 2) (+) Offset between sounder and waterline in meters
999
     *    (-) Offset between sounder and keel in meters
1000
     * 3) Maximum range scale
1001
     * 4) Checksum.
1002
     *
1003
     * $SDDBT and $SDDPT should agree, but often don't.
1004
     *
1005
     */
1006
0
    double offset;
1007
0
    gps_mask_t mask = ONLINE_SET;
1008
1009
0
    if ('\0' == field[1][0]) {
1010
        // no depth
1011
0
        return mask;
1012
0
    }
1013
0
    session->newdata.depth = safe_atof(field[1]);
1014
0
    offset = safe_atof(field[2]);
1015
0
    if (0.0 > offset) {
1016
        // adjust to get depth from keel
1017
0
        session->newdata.depth -= offset;
1018
0
    }
1019
0
    mask |= ALTITUDE_SET;
1020
1021
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
1022
0
             "NMEA0183: %s depth %.1f offset %s max %s\n",
1023
0
             field[0],
1024
0
             session->newdata.depth, field[2], field[3]);
1025
0
    return mask;
1026
0
}
1027
1028
/* NMEA Map Datum
1029
 *
1030
 * FIXME: seems to happen after cycle ender, so nothing happens...
1031
 */
1032
static gps_mask_t processDTM(int c UNUSED, char *field[],
1033
                               struct gps_device_t *session)
1034
0
{
1035
    /*
1036
     * $GPDTM,W84,C*52
1037
     * $GPDTM,xxx,x,xx.xxxx,x,xx.xxxx,x,,xxx*hh<CR><LF>
1038
     * 1    = Local datum code (xxx):
1039
     *          W84 – WGS84
1040
     *          W72 – WGS72
1041
     *          S85 – SGS85
1042
     *          P90 – PE90
1043
     *          999 – User defined
1044
     *          IHO datum code
1045
     * 2     = Local datum sub code (x)
1046
     * 3     = Latitude offset in minutes (xx.xxxx)
1047
     * 4     = Latitude offset mark (N: +, S: -) (x)
1048
     * 5     = Longitude offset in minutes (xx.xxxx)
1049
     * 6     = Longitude offset mark (E: +, W: -) (x)
1050
     * 7     = Altitude offset in meters. Always null
1051
     * 8     = Datum (xxx):
1052
     *          W84 – WGS84
1053
     *          W72 – WGS72
1054
     *          S85 – SGS85
1055
     *          P90 – PE90
1056
     *          999 – User defined
1057
     *          IHO datum code
1058
     * 9    = checksum
1059
     */
1060
0
    int i;
1061
0
    static struct
1062
0
    {
1063
0
        char *code;
1064
0
        char *name;
1065
0
    } codes[] = {
1066
0
        {"W84", "WGS84"},
1067
0
        {"W72", "WGS72"},
1068
0
        {"S85", "SGS85"},
1069
0
        {"P90", "PE90"},
1070
0
        {"999", "User Defined"},
1071
0
        {"", ""},
1072
0
    };
1073
1074
0
    gps_mask_t mask = ONLINE_SET;
1075
1076
0
    if ('\0' == field[1][0]) {
1077
0
        return mask;
1078
0
    }
1079
1080
0
    for (i = 0; ; i++) {
1081
0
        if ('\0' == codes[i].code[0]) {
1082
            // not found
1083
0
            strlcpy(session->newdata.datum, field[1],
1084
0
                    sizeof(session->newdata.datum));
1085
0
            break;
1086
0
        }
1087
0
        if (0 ==strcmp(codes[i].code, field[1])) {
1088
0
            strlcpy(session->newdata.datum, codes[i].name,
1089
0
                    sizeof(session->newdata.datum));
1090
0
            break;
1091
0
        }
1092
0
    }
1093
1094
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
1095
0
             "NMEA0183: xxDTM: datum=%.40s\n",
1096
0
             session->newdata.datum);
1097
0
    return mask;
1098
0
}
1099
1100
// NMEA 3.0 Estimated Position Error
1101
static gps_mask_t processGBS(int c UNUSED, char *field[],
1102
                               struct gps_device_t *session)
1103
0
{
1104
    /*
1105
     * $GPGBS,082941.00,2.4,1.5,3.9,25,,-43.7,27.5*65
1106
     *  1) UTC time of the fix associated with this sentence (hhmmss.ss)
1107
     *  2) Expected error in latitude (meters)
1108
     *  3) Expected error in longitude (meters)
1109
     *  4) Expected error in altitude (meters)
1110
     *  5) PRN of most likely failed satellite
1111
     *  6) Probability of missed detection for most likely failed satellite
1112
     *  7) Estimate of bias in meters on most likely failed satellite
1113
     *  8) Standard deviation of bias estimate
1114
     *  9) NMEA 4.1 GNSS ID
1115
     * 10) NMEA 4.1 Signal ID
1116
     *     Checksum
1117
     *
1118
     * Fields 2, 3 and 4 are one standard deviation.
1119
     */
1120
0
    gps_mask_t mask = ONLINE_SET;
1121
1122
    // register fractional time for end-of-cycle detection
1123
0
    register_fractional_time(field[0], field[1], session);
1124
1125
    // check that we're associated with the current fix
1126
0
    if (session->nmea.date.tm_hour == DD(field[1]) &&
1127
0
        session->nmea.date.tm_min == DD(field[1] + 2) &&
1128
0
        session->nmea.date.tm_sec == DD(field[1] + 4)) {
1129
        // FIXME: check fractional time!
1130
0
        session->newdata.epy = safe_atof(field[2]);
1131
0
        session->newdata.epx = safe_atof(field[3]);
1132
0
        session->newdata.epv = safe_atof(field[4]);
1133
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
1134
0
                 "NMEA0183: GBS: epx=%.2f epy=%.2f epv=%.2f\n",
1135
0
                 session->newdata.epx,
1136
0
                 session->newdata.epy,
1137
0
                 session->newdata.epv);
1138
0
        mask = HERR_SET | VERR_SET;
1139
0
    } else {
1140
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
1141
0
                 "NMEA0183: second in $GPGBS error estimates doesn't match.\n");
1142
0
    }
1143
0
    return mask;
1144
0
}
1145
1146
// Global Positioning System Fix Data
1147
static gps_mask_t processGGA(int c UNUSED, char *field[],
1148
                               struct gps_device_t *session)
1149
0
{
1150
    /*
1151
     * GGA,123519,4807.038,N,01131.324,E,1,08,0.9,545.4,M,46.9,M, , *42
1152
     * 1     123519       Fix taken at 12:35:19 UTC
1153
     * 2,3   4807.038,N   Latitude 48 deg 07.038' N
1154
     * 4,5   01131.324,E  Longitude 11 deg 31.324' E
1155
     * 6     1            Fix quality:
1156
     *                     0 = invalid,
1157
     *                     1 = GPS,
1158
     *                         u-blox may use 1 for Estimated
1159
     *                     2 = DGPS,
1160
     *                     3 = PPS (Precise Position Service),
1161
     *                     4 = RTK (Real Time Kinematic) with fixed integers,
1162
     *                     5 = Float RTK,
1163
     *                     6 = Estimated,
1164
     *                     7 = Manual,
1165
     *                     8 = Simulator
1166
     * 7     08           Number of satellites in use
1167
     * 8     0.9          Horizontal dilution of position
1168
     * 9,10  545.4,M      Altitude, Meters MSL
1169
     * 11,12 46.9,M       Height of geoid (mean sea level) above WGS84
1170
     *                    ellipsoid, in Meters
1171
     * 13    33           time in seconds since last DGPS update
1172
     *                    usually empty
1173
     * 14    1023         DGPS station ID number (0000-1023)
1174
     *                    usually empty
1175
     *
1176
     * Some GPS, like the SiRFstarV in NMEA mode, send both GPGSA and
1177
     * GLGPSA with identical data.
1178
     */
1179
0
    gps_mask_t mask = ONLINE_SET;
1180
0
    int newstatus;
1181
0
    char last_last_gga_talker = session->nmea.last_gga_talker;
1182
0
    int fix;              // a.k.a Quality flag
1183
0
    session->nmea.last_gga_talker = field[0][1];
1184
1185
0
    if ('\0' == field[6][0]) {
1186
        /* no data is no data, assume no fix
1187
         * the test/daemon/myguide-3100.log shows lat/lon/alt but
1188
         * no status, and related RMC shows no fix. */
1189
0
        fix = -1;
1190
0
    } else {
1191
0
        fix = atoi(field[6]);
1192
0
    }
1193
    // Jackson Labs Micro JLT uses nonstadard fix flag, not handled
1194
0
    switch (fix) {
1195
0
    case 0:     // no fix
1196
0
        newstatus = STATUS_UNK;
1197
0
        if ('\0' == field[1][0]) {
1198
            /* No time available. That breaks cycle end detector
1199
             * Force report to bypass cycle detector and get report out.
1200
             * To handle Querks (Quectel) like this:
1201
             *  $GPGGA,,,,,,0,,,,,,,,*66
1202
             */
1203
0
            memset(&session->nmea.date, 0, sizeof(session->nmea.date));
1204
0
            session->cycle_end_reliable = false;
1205
0
            mask |= REPORT_IS | TIME_SET;
1206
0
        }
1207
0
        break;
1208
0
    case 1:
1209
        // could be 2D, 3D, GNSSDR
1210
0
        newstatus = STATUS_GPS;
1211
0
        break;
1212
0
    case 2:     // differential
1213
0
        newstatus = STATUS_DGPS;
1214
0
        break;
1215
0
    case 3:
1216
        // GPS PPS, fix valid, could be 2D, 3D, GNSSDR
1217
0
        newstatus = STATUS_PPS_FIX;
1218
0
        break;
1219
0
    case 4:     // RTK integer
1220
0
        newstatus = STATUS_RTK_FIX;
1221
0
        break;
1222
0
    case 5:     // RTK float
1223
0
        newstatus = STATUS_RTK_FLT;
1224
0
        break;
1225
0
    case 6:
1226
        // dead reckoning, could be valid or invalid
1227
0
        newstatus = STATUS_DR;
1228
0
        break;
1229
0
    case 7:
1230
        // manual input, surveyed
1231
0
        newstatus = STATUS_TIME;
1232
0
        break;
1233
0
    case 8:
1234
        /* simulated mode
1235
         * Garmin GPSMAP and Gecko sends an 8, but undocumented why */
1236
0
        newstatus = STATUS_SIM;
1237
0
        break;
1238
0
    case -1:
1239
0
        FALLTHROUGH
1240
0
    default:
1241
0
        newstatus = -1;
1242
0
        break;
1243
0
    }
1244
0
    if (0 <= newstatus) {
1245
0
        session->newdata.status = newstatus;
1246
0
        mask = STATUS_SET;
1247
0
    }
1248
    /*
1249
     * There are some receivers (the Trimble Placer 450 is an example) that
1250
     * don't ship a GSA with mode 1 when they lose satellite lock. Instead
1251
     * they just keep reporting GGA and GSA on subsequent cycles with the
1252
     * timestamp not advancing and a bogus mode.
1253
     *
1254
     * On the assumption that GGA is only issued once per cycle we can
1255
     * detect this here (it would be nicer to do it on GSA but GSA has
1256
     * no timestamp).
1257
     *
1258
     * SiRFstarV breaks this assumption, sending GGA with different
1259
     * talker IDs.
1260
     */
1261
0
    if ('\0' != last_last_gga_talker &&
1262
0
        last_last_gga_talker != session->nmea.last_gga_talker) {
1263
        // skip the time check
1264
0
        session->nmea.latch_mode = 0;
1265
0
    } else {
1266
0
        session->nmea.latch_mode = strncmp(field[1],
1267
0
                          session->nmea.last_gga_timestamp,
1268
0
                          sizeof(session->nmea.last_gga_timestamp))==0;
1269
0
    }
1270
1271
0
    if (session->nmea.latch_mode) {
1272
0
        session->newdata.status = STATUS_UNK;
1273
0
        session->newdata.mode = MODE_NO_FIX;
1274
0
        mask |= MODE_SET | STATUS_SET;
1275
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
1276
0
                 "NMEA0183: xxGGA: latch mode\n");
1277
0
    } else {
1278
0
        (void)strlcpy(session->nmea.last_gga_timestamp, field[1],
1279
0
                      sizeof(session->nmea.last_gga_timestamp));
1280
0
    }
1281
1282
    /* satellites_visible is used as an accumulator in xxGSV
1283
     * so if we set it here we break xxGSV
1284
     * Some GPS, like SiRFstarV NMEA, report per GNSS used
1285
     * counts in GPGGA and GLGGA.
1286
     */
1287
0
    session->nmea.gga_sats_used = atoi(field[7]);
1288
1289
0
    if ('\0' == field[1][0]) {
1290
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
1291
0
                 "NMEA0183: GGA time missing.\n");
1292
0
    } else if (0 == merge_hhmmss(field[1], session)) {
1293
0
        register_fractional_time(field[0], field[1], session);
1294
0
        if (0 == session->nmea.date.tm_year) {
1295
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1296
0
                     "NMEA0183: can't use GGA time until after ZDA or RMC"
1297
0
                     " has supplied a year.\n");
1298
0
        } else {
1299
0
            mask |= TIME_SET;
1300
0
        }
1301
0
    }
1302
1303
0
    if (0 == do_lat_lon(&field[2], &session->newdata)) {
1304
0
        session->newdata.mode = MODE_2D;
1305
0
        mask |= LATLON_SET;
1306
0
        if ('\0' != field[11][0]) {
1307
0
            session->newdata.geoid_sep = safe_atof(field[11]);
1308
0
        } else {
1309
0
            session->newdata.geoid_sep = wgs84_separation(
1310
0
                session->newdata.latitude, session->newdata.longitude);
1311
0
        }
1312
        /*
1313
         * SiRF chipsets up to version 2.2 report a null altitude field.
1314
         * See <http://www.sirf.com/Downloads/Technical/apnt0033.pdf>.
1315
         * If we see this, force mode to 2D at most.
1316
         */
1317
0
        if ('\0' != field[9][0]) {
1318
            // altitude is MSL
1319
0
            session->newdata.altMSL = safe_atof(field[9]);
1320
            // Let gpsd_error_model() deal with altHAE
1321
0
            mask |= ALTITUDE_SET;
1322
            /*
1323
             * This is a bit dodgy.  Technically we shouldn't set the mode
1324
             * bit until we see GSA.  But it may be later in the cycle,
1325
             * some devices like the FV-18 don't send it by default, and
1326
             * elsewhere in the code we want to be able to test for the
1327
             * presence of a valid fix with mode > MODE_NO_FIX.
1328
             *
1329
             * Use gga_sats_used; as double check on MODE_3D
1330
             */
1331
0
            if (4 <= session->nmea.gga_sats_used) {
1332
0
                session->newdata.mode = MODE_3D;
1333
0
            }
1334
0
        }
1335
        /* the next test works when we only see GPGGA or GNGGA, but
1336
         * not GPGGA, BDGGA, etc.  As some constellations may see no
1337
         * sats, and others do.
1338
         * For some receivers, like the bn-9015, this is the only
1339
         * way to know fix mode.  It reports lat/lon/alt and no
1340
         * sats used. */
1341
0
        if (3 > session->nmea.gga_sats_used &&
1342
0
            'G' == field[0][0] &&
1343
0
            ('P' == field[0][1] ||
1344
0
             'N' == field[0][1])) {
1345
            // G[NP]GGA and not enough sats used for a fix.
1346
0
            session->newdata.mode = MODE_NO_FIX;
1347
0
        }
1348
0
    } else {
1349
0
        session->newdata.mode = MODE_NO_FIX;
1350
0
    }
1351
0
    mask |= MODE_SET;
1352
1353
    // BT-451 sends 99.99 for invalid DOPs
1354
    // Jackson Labs send 99.00 for invalid DOPs
1355
    // Skytraq send 0.00 for invalid DOPs
1356
0
    if ('\0' != field[8][0]) {
1357
0
        double hdop;
1358
0
        hdop = safe_atof(field[8]);
1359
0
        if (IN(0.01, hdop, 89.99)) {
1360
            // why not to newdata?
1361
0
            session->gpsdata.dop.hdop = hdop;
1362
0
            mask |= DOP_SET;
1363
0
        }
1364
0
    }
1365
1366
    // get DGPS stuff
1367
0
    if ('\0' != field[13][0] &&
1368
0
        '\0' != field[14][0]) {
1369
        // both, or neither
1370
0
        double age;
1371
0
        int station;
1372
1373
0
        age = safe_atof(field[13]);
1374
0
        station = atoi(field[14]);
1375
0
        if (0.09 < age ||
1376
0
            0 < station) {
1377
            // ignore both zeros
1378
0
            session->newdata.dgps_age = age;
1379
0
            session->newdata.dgps_station = station;
1380
0
        }
1381
0
    }
1382
1383
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
1384
0
             "NMEA0183: GGA: hhmmss=%s lat=%.2f lon=%.2f altMSL=%.2f "
1385
0
             "mode=%d status=%d\n",
1386
0
             field[1],
1387
0
             session->newdata.latitude,
1388
0
             session->newdata.longitude,
1389
0
             session->newdata.altMSL,
1390
0
             session->newdata.mode,
1391
0
             session->newdata.status);
1392
0
    return mask;
1393
0
}
1394
1395
// Geographic position - Latitude, Longitude
1396
static gps_mask_t processGLL(int count, char *field[],
1397
                             struct gps_device_t *session)
1398
0
{
1399
    /* Introduced in NMEA 3.0.
1400
     *
1401
     * $GPGLL,4916.45,N,12311.12,W,225444,A,A*5C
1402
     *
1403
     * 1,2: 4916.46,N    Latitude 49 deg. 16.45 min. North
1404
     * 3,4: 12311.12,W   Longitude 123 deg. 11.12 min. West
1405
     * 5:   225444       Fix taken at 22:54:44 UTC
1406
     * 6:   A            Data valid
1407
     * 7:   A            Autonomous mode
1408
     * 8:   *5C          Mandatory NMEA checksum
1409
     *
1410
     * 1,2 Latitude, N (North) or S (South)
1411
     * 3,4 Longitude, E (East) or W (West)
1412
     * 5 UTC of position
1413
     * 6 A = Active, V = Invalid data
1414
     * 7 Mode Indicator
1415
     *    See faa_mode() for possible mode values.
1416
     *
1417
     * I found a note at <http://www.secoh.ru/windows/gps/nmfqexep.txt>
1418
     * indicating that the Garmin 65 does not return time and status.
1419
     * SiRF chipsets don't return the Mode Indicator.
1420
     * This code copes gracefully with both quirks.
1421
     *
1422
     * Unless you care about the FAA indicator, this sentence supplies nothing
1423
     * that GPRMC doesn't already.  But at least two (Garmin GPS 48 and
1424
     * Magellan Triton 400) actually ship updates in GLL that aren't redundant.
1425
     *
1426
     */
1427
0
    char *status = field[7];
1428
0
    gps_mask_t mask = ONLINE_SET;
1429
1430
0
    if (field[5][0] != '\0') {
1431
0
        if (0 == merge_hhmmss(field[5], session)) {
1432
0
            register_fractional_time(field[0], field[5], session);
1433
0
            if (0 == session->nmea.date.tm_year) {
1434
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
1435
0
                         "NMEA0183: can't use GLL time until after ZDA or RMC"
1436
0
                         " has supplied a year.\n");
1437
0
            } else {
1438
0
                mask = TIME_SET;
1439
0
            }
1440
0
        }
1441
0
    }
1442
0
    if ('\0' == field[6][0] ||
1443
0
        'V' == field[6][0]) {
1444
        // Invalid
1445
0
        session->newdata.status = STATUS_UNK;
1446
0
        session->newdata.mode = MODE_NO_FIX;
1447
0
    } else if ('A' == field[6][0] &&
1448
0
        (count < 8 || *status != 'N') &&
1449
0
        0 == do_lat_lon(&field[1], &session->newdata)) {
1450
0
        int newstatus;
1451
1452
0
        mask |= LATLON_SET;
1453
1454
0
        newstatus = STATUS_GPS;
1455
0
        if (8 <= count) {
1456
0
            newstatus = faa_mode(*status);
1457
0
        }
1458
        /*
1459
         * This is a bit dodgy.  Technically we shouldn't set the mode
1460
         * bit until we see GSA, or similar.  But it may be later in the
1461
         * cycle, some devices like the FV-18 don't send it by default,
1462
         * and elsewhere in the code we want to be able to test for the
1463
         * presence of a valid fix with mode > MODE_NO_FIX.
1464
         */
1465
0
        if (0 != isfinite(session->gpsdata.fix.altHAE) ||
1466
0
            0 != isfinite(session->gpsdata.fix.altMSL)) {
1467
0
            session->newdata.mode = MODE_3D;
1468
0
        } else if (3 < session->gpsdata.satellites_used) {
1469
            // 4 sats used means 3D
1470
0
            session->newdata.mode = MODE_3D;
1471
0
        } else if (MODE_2D > session->gpsdata.fix.mode ||
1472
0
                   (0 == isfinite(session->oldfix.altHAE) &&
1473
0
                    0 == isfinite(session->oldfix.altMSL))) {
1474
0
            session->newdata.mode = MODE_2D;
1475
0
        }
1476
0
        session->newdata.status = newstatus;
1477
0
    } else {
1478
0
        session->newdata.status = STATUS_UNK;
1479
0
        session->newdata.mode = MODE_NO_FIX;
1480
0
    }
1481
0
    mask |= STATUS_SET | MODE_SET;
1482
1483
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
1484
0
             "NMEA0183: GLL: hhmmss=%s lat=%.2f lon=%.2f mode=%d status=%d "
1485
0
             "faa mode %s(%s)\n",
1486
0
             field[5],
1487
0
             session->newdata.latitude,
1488
0
             session->newdata.longitude,
1489
0
             session->newdata.mode,
1490
0
             session->newdata.status,
1491
0
             field[7], char2str(field[7][0], c_faa_mode));
1492
0
    return mask;
1493
0
}
1494
1495
// Geographic position - Latitude, Longitude, and more
1496
static gps_mask_t processGNS(int count UNUSED, char *field[],
1497
                             struct gps_device_t *session)
1498
0
{
1499
    /* Introduced in NMEA 4.0?
1500
     *
1501
     * This mostly duplicates RMC, except for the multi GNSS mode
1502
     * indicator.
1503
     *
1504
     * Example.  Ignore the line break.
1505
     * $GPGNS,224749.00,3333.4268304,N,11153.3538273,W,D,19,0.6,406.110,
1506
     *        -26.294,6.0,0138,S,*6A
1507
     *
1508
     * 1:  224749.00     UTC HHMMSS.SS.  22:47:49.00
1509
     * 2:  3333.4268304  Latitude DDMM.MMMMM. 33 deg. 33.4268304 min
1510
     * 3:  N             Latitude North
1511
     * 4:  12311.12      Longitude 111 deg. 53.3538273 min
1512
     * 5:  W             Longitude West
1513
     * 6:  D             FAA mode indicator
1514
     *                     see faa_mode() for possible mode values
1515
     *                     May be one to six characters.
1516
     *                       Char 1 = GPS
1517
     *                       Char 2 = GLONASS
1518
     *                       Char 3 = Galileo
1519
     *                       Char 4 = BDS
1520
     *                       Char 5 = QZSS
1521
     *                       Char 6 = NavIC (IRNSS)
1522
     * 7:  19           Number of Satellites used in solution
1523
     * 8:  0.6          HDOP
1524
     * 9:  406110       MSL Altitude in meters
1525
     * 10: -26.294      Geoid separation in meters
1526
     * 11: 6.0          Age of differential corrections, in seconds
1527
     * 12: 0138         Differential reference station ID
1528
     * 13: S            NMEA 4.1+ Navigation status
1529
     *                   S = Safe
1530
     *                   C = Caution
1531
     *                   U = Unsafe
1532
     *                   V = Not valid for navigation
1533
     * 8:   *6A          Mandatory NMEA checksum
1534
     *
1535
     */
1536
0
    int newstatus;
1537
0
    gps_mask_t mask = ONLINE_SET;
1538
1539
0
    if ('\0' != field[1][0]) {
1540
0
        if (0 == merge_hhmmss(field[1], session)) {
1541
0
            register_fractional_time(field[0], field[1], session);
1542
0
            if (0 == session->nmea.date.tm_year) {
1543
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
1544
0
                         "NMEA0183: can't use GNS time until after ZDA or RMC"
1545
0
                         " has supplied a year.\n");
1546
0
            } else {
1547
0
                mask = TIME_SET;
1548
0
            }
1549
0
        }
1550
0
    }
1551
1552
    /* FAA mode: not valid, ignore
1553
     * Yes, in 2019 a GLONASS only fix may be valid, but not worth
1554
     * the confusion */
1555
0
    if ('\0' == field[6][0] ||      // FAA mode: missing
1556
0
        'N' == field[6][0]) {       // FAA mode: not valid
1557
0
        session->newdata.mode = MODE_NO_FIX;
1558
0
        mask |= MODE_SET;
1559
0
        return mask;
1560
0
    }
1561
    /* navigation status, assume S=safe and C=caution are OK
1562
     * can be missing on valid fix */
1563
0
    if ('U' == field[13][0] ||      // Unsafe
1564
0
        'V' == field[13][0]) {      // not valid
1565
0
        return mask;
1566
0
    }
1567
1568
0
    session->nmea.gga_sats_used = atoi(field[7]);
1569
1570
0
    if (0 == do_lat_lon(&field[2], &session->newdata)) {
1571
0
        mask |= LATLON_SET;
1572
0
        session->newdata.mode = MODE_2D;
1573
1574
0
        if ('\0' != field[9][0]) {
1575
            // altitude is MSL
1576
0
            session->newdata.altMSL = safe_atof(field[9]);
1577
0
            if (0 != isfinite(session->newdata.altMSL)) {
1578
0
                mask |= ALTITUDE_SET;
1579
0
                if (3 < session->nmea.gga_sats_used) {
1580
                    // more than 3 sats used means 3D
1581
0
                    session->newdata.mode = MODE_3D;
1582
0
                }
1583
0
            }
1584
            // only need geoid_sep if in 3D mode
1585
0
            if ('\0' != field[10][0]) {
1586
0
                session->newdata.geoid_sep = safe_atof(field[10]);
1587
0
            }
1588
            // Let gpsd_error_model() deal with geoid_sep and altHAE
1589
0
        }
1590
0
    } else {
1591
0
        session->newdata.mode = MODE_NO_FIX;
1592
0
        mask |= MODE_SET;
1593
0
    }
1594
1595
0
    if ('\0' != field[8][0]) {
1596
0
        session->gpsdata.dop.hdop = safe_atof(field[8]);
1597
0
        mask |= DOP_SET;
1598
0
    }
1599
1600
    // we ignore all but the leading mode indicator.
1601
0
    newstatus = faa_mode(field[6][0]);
1602
1603
0
    session->newdata.status = newstatus;
1604
0
    mask |= MODE_SET | STATUS_SET;
1605
1606
    // get DGPS stuff
1607
0
    if ('\0' != field[11][0] &&
1608
0
        '\0' != field[12][0]) {
1609
        // both, or neither
1610
0
        session->newdata.dgps_age = safe_atof(field[11]);
1611
0
        session->newdata.dgps_station = atoi(field[12]);
1612
0
    }
1613
1614
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
1615
0
             "NMEA0183: GNS: hhmmss=%s lat=%.2f lon=%.2f mode=%d status=%d "
1616
0
             "faa mode %s(%s)\n",
1617
0
             field[1],
1618
0
             session->newdata.latitude,
1619
0
             session->newdata.longitude,
1620
0
             session->newdata.mode,
1621
0
             session->newdata.status,
1622
0
             field[6], char2str(field[6][0], c_faa_mode));
1623
0
    return mask;
1624
0
}
1625
1626
// GNSS Range residuals
1627
static gps_mask_t processGRS(int count UNUSED, char *field[],
1628
                             struct gps_device_t *session)
1629
0
{
1630
    /* In NMEA 3.01
1631
     *
1632
     * Example:
1633
     * $GPGRS,150119.000,1,-0.33,-2.59,3.03,-0.09,-2.98,7.12,-15.6,17.0,,,,*5A
1634
     *
1635
     * 1:  150119.000    UTC HHMMSS.SS
1636
     * 2:  1             Mode: 0 == original, 1 == recomputed
1637
     * 3:  -0.33         range residual in meters sat 1
1638
     * 4:  -2.59         range residual sat 2
1639
     * [...]
1640
     * n:   *5A          Mandatory NMEA checksum
1641
     *
1642
     */
1643
0
    int mode;
1644
0
    gps_mask_t mask = ONLINE_SET;
1645
1646
0
    if ('\0' == field[1][0] ||
1647
0
        0 != merge_hhmmss(field[1], session)) {
1648
        // bad time
1649
0
        return mask;
1650
0
    }
1651
1652
0
    mode = atoi(field[2]);
1653
0
    if (1 != mode &&
1654
0
        2 != mode) {
1655
        // bad mode
1656
0
        return mask;
1657
0
    }
1658
1659
    // FIXME: partial decode.  How to match sat numbers up with GSA?
1660
1661
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
1662
0
             "NMEA0183: %s: mode %d count %d\n",
1663
0
             field[0], mode, count);
1664
0
    return mask;
1665
0
}
1666
1667
// GPS DOP and Active Satellites
1668
static gps_mask_t processGSA(int count, char *field[],
1669
                             struct gps_device_t *session)
1670
0
{
1671
0
#define GSA_TALKER      field[0][1]
1672
    /*
1673
     * eg1. $GPGSA,A,3,,,,,,16,18,,22,24,,,3.6,2.1,2.2*3C
1674
     * eg2. $GPGSA,A,3,19,28,14,18,27,22,31,39,,,,,1.7,1.0,1.3*35
1675
     * NMEA 4.10: $GNGSA,A,3,13,12,22,19,08,21,,,,,,,1.05,0.64,0.83,4*0B
1676
     * 1    = Mode:
1677
     *         M=Manual, forced to operate in 2D or 3D
1678
     *         A=Automatic, 3D/2D
1679
     * 2    = Mode:
1680
     *         1=Fix not available,
1681
     *         2=2D,
1682
     *         3=3D
1683
     *         E=Dead Reckonig (Antaris)
1684
     * 3-14 (or 24!) = satellite PRNs used in position fix (null unused)
1685
     * 15   = PDOP
1686
     * 16   = HDOP
1687
     * 17   = VDOP
1688
     *  -- -- --
1689
     * 18   - NMEA 4.10+ GNSS System ID, u-blox extended, Quectel $PQ
1690
     *             0 = QZSS (Trimble only)
1691
     *             1 = GPS
1692
     *             2 = GLONASS
1693
     *             3 = Galileo
1694
     *             4 = BeiDou
1695
     *             5 = QZSS
1696
     *             6 - NavIC (IRNSS)
1697
     *  -- OR --
1698
     * 18     SiRF TriG puts a floating point number here.
1699
     *  -- -- --
1700
     *
1701
     * Not all documentation specifies the number of PRN fields, it
1702
     * may be variable.  Most doc that specifies says 12 PRNs.
1703
     *
1704
     * The Navior-24 CH-4701 outputs 30 fields, 24 PRNs!
1705
     * GPGSA,A,3,27,23,13,07,25,,,,,,,,,,,,,,,,,,,,07.9,06.0,05.2
1706
     *
1707
     * The Skytraq S2525F8-BD-RTK output both GPGSA and BDGSA in the
1708
     * same cycle:
1709
     * $GPGSA,A,3,23,31,22,16,03,07,,,,,,,1.8,1.1,1.4*3E
1710
     * $BDGSA,A,3,214,,,,,,,,,,,,1.8,1.1,1.4*18
1711
     * These need to be combined like GPGSV and BDGSV
1712
     *
1713
     * The SiRF-TriG, found in their Atlas VI SoC,  uses field 18 for
1714
     * something other than the NMEA gnss ID:
1715
     * $GPGSA,A,3,25,32,12,14,,,,,,,,,2.1,1.1,1.8,1.2*39
1716
     * $BDGSA,A,3,02,03,04,,,,,,,,,,2.1,1.1,1.8,1.2*2D
1717
     *
1718
     * Some GPS emit GNGSA.  So far we have not seen a GPS emit GNGSA
1719
     * and then another flavor of xxGSA
1720
     *
1721
     * Some Skytraq will emit all GPS in one GNGSA, Then follow with
1722
     * another GNGSA with the BeiDou birds.
1723
     *
1724
     * SEANEXX, SiRFstarIV, and others also do it twice in one cycle:
1725
     * $GNGSA,A,3,31,26,21,,,,,,,,,,3.77,2.55,2.77*1A
1726
     * $GNGSA,A,3,75,86,87,,,,,,,,,,3.77,2.55,2.77*1C
1727
     * seems like the first is GNSS and the second GLONASS
1728
     *
1729
     * u-blox 9 outputs one per GNSS on each cycle.  Note the
1730
     * extra last parameter which is NMEA gnssid:
1731
     * $GNGSA,A,3,13,16,21,15,10,29,27,20,,,,,1.05,0.64,0.83,1*03
1732
     * $GNGSA,A,3,82,66,81,,,,,,,,,,1.05,0.64,0.83,2*0C
1733
     * $GNGSA,A,3,07,12,33,,,,,,,,,,1.05,0.64,0.83,3*0A
1734
     * $GNGSA,A,3,13,12,22,19,08,21,,,,,,,1.05,0.64,0.83,4*0B
1735
     * Also note the NMEA 4.0 GLONASS PRN (82) in an NMEA 4.1
1736
     * sentence.
1737
     *
1738
     * Another Quectel Querk.  Note the extra field on the end.
1739
     *   System ID, 4 = BeiDou, 5 = QZSS
1740
     *
1741
     * $PQGSA,A,3,12,,,,,,,,,,,,1.2,0.9,0.9,4*3C
1742
     * $PQGSA,A,3,,,,,,,,,,,,,1.2,0.9,0.9,5*3E
1743
     * NMEA 4.11 says they should use $BDGSA and $GQGSA
1744
     */
1745
0
    gps_mask_t mask = ONLINE_SET;
1746
0
    char last_last_gsa_talker = session->nmea.last_gsa_talker;
1747
0
    int nmea_gnssid = 0;
1748
1749
    /*
1750
     * One chipset called the i.Trek M3 issues GPGSA lines that look like
1751
     * this: "$GPGSA,A,1,,,,*32" when it has no fix.  This is broken
1752
     * in at least two ways: it's got the wrong number of fields, and
1753
     * it claims to be a valid sentence (A flag) when it isn't.
1754
     * Alarmingly, it's possible this error may be generic to SiRFstarIII.
1755
     */
1756
0
    if (session->nmea.latch_mode) {
1757
        // last GGA had a non-advancing timestamp; don't trust this GSA
1758
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
1759
0
                 "NMEA0183: %s: non-advancing timestamp\n", field[0]);
1760
        // FIXME: return here?
1761
0
    } else {
1762
0
        int i;
1763
1764
0
        i = atoi(field[2]);
1765
        /*
1766
         * The first arm of this conditional ignores dead-reckoning
1767
         * fixes from an Antaris chipset. which returns E in field 2
1768
         * for a dead-reckoning estimate.  Fix by Andreas Stricker.
1769
         */
1770
0
        if (1 <= i &&
1771
0
            3 >= i) {
1772
0
            session->newdata.mode = i;
1773
0
            mask = MODE_SET;
1774
1775
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
1776
0
                     "NMEA0183: %s sets mode %d\n",
1777
0
                     field[0], session->newdata.mode);
1778
0
        }
1779
1780
#if 0   // debug
1781
        GPSD_LOG(LOG_SHOUT, &session->context->errout,
1782
                 "NMEA0183: %s: count %d \n", field[0], count);
1783
#endif  // debug
1784
0
        if (19 < count) {
1785
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1786
0
                     "NMEA0183: %s: count %d too long!\n", field[0], count);
1787
0
        } else {
1788
0
            double dop;
1789
1790
            // Just ignore the last fields of the Navior CH-4701
1791
1792
            // BT-451 sends 99.99 for invalid DOPs
1793
            // Jackson Labs send 99.00 for invalid DOPs
1794
            // Skytraq send 0.00 for invalid DOPs
1795
0
            if ('\0' != field[15][0]) {
1796
0
                dop = safe_atof(field[15]);
1797
0
                if (IN(0.01, dop, 89.99)) {
1798
0
                    session->gpsdata.dop.pdop = dop;
1799
0
                    mask |= DOP_SET;
1800
0
                }
1801
0
            }
1802
0
            if ('\0' != field[16][0]) {
1803
0
                dop = safe_atof(field[16]);
1804
0
                if (IN(0.01, dop, 89.99)) {
1805
0
                    session->gpsdata.dop.hdop = dop;
1806
0
                    mask |= DOP_SET;
1807
0
                }
1808
0
            }
1809
0
            if ('\0' != field[17][0]) {
1810
0
                dop = safe_atof(field[17]);
1811
0
                if (IN(0.01, dop, 89.99)) {
1812
0
                    session->gpsdata.dop.vdop = dop;
1813
0
                    mask |= DOP_SET;
1814
0
                }
1815
0
            }
1816
0
            if (19 == count &&
1817
0
                '\0' != field[18][0]) {
1818
0
                if (NULL != strchr(field[18], '.')) {
1819
                    // SiRF TriG puts a floating point in field 18
1820
0
                    GPSD_LOG(LOG_WARN, &session->context->errout,
1821
0
                             "NMEA0183: %s: illegal field 18 (%s)!\n",
1822
0
                             field[0], field[18]);
1823
0
                } else {
1824
                    // get the NMEA 4.10, or $PQGSA, system ID
1825
0
                    nmea_gnssid = atoi(field[18]);
1826
0
                }
1827
0
            }
1828
0
        }
1829
        /*
1830
         * might have gone from GPGSA to GLGSA/BDGSA
1831
         * or GNGSA to GNGSA
1832
         * or GNGSA to PQGSA
1833
         * in which case accumulate
1834
         */
1835
        // FIXME: maybe on clear on first GPGSA?
1836
0
        if ('\0' == session->nmea.last_gsa_talker ||
1837
0
            (GSA_TALKER == session->nmea.last_gsa_talker &&
1838
0
             'N' != GSA_TALKER &&
1839
0
             'Q' != GSA_TALKER) ) {
1840
0
            session->gpsdata.satellites_used = 0;
1841
0
            memset(session->nmea.sats_used, 0, sizeof(session->nmea.sats_used));
1842
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
1843
0
                     "NMEA0183: %s: clear sats_used\n", field[0]);
1844
0
        }
1845
0
        session->nmea.last_gsa_talker = GSA_TALKER;
1846
1847
        /* figure out which constellation(s) this GSA is for by looking
1848
         * at the talker ID. */
1849
0
        switch (session->nmea.last_gsa_talker) {
1850
0
        case 'A':
1851
            // GA Galileo
1852
0
            nmea_gnssid = 3;
1853
0
            session->nmea.seen_gagsa = true;
1854
0
            break;
1855
0
        case 'B':
1856
            // GB BeiDou
1857
0
            FALLTHROUGH
1858
0
        case 'D':
1859
            // BD BeiDou
1860
0
            nmea_gnssid = 4;
1861
0
            session->nmea.seen_bdgsa = true;
1862
0
            break;
1863
0
        case 'I':
1864
            // GI IRNSS
1865
0
            nmea_gnssid = 6;
1866
0
            session->nmea.seen_gigsa = true;
1867
0
            break;
1868
0
        case 'L':
1869
            // GL GLONASS
1870
0
            nmea_gnssid = 2;
1871
0
            session->nmea.seen_glgsa = true;
1872
0
            break;
1873
0
        case 'N':
1874
            // GN GNSS
1875
0
            session->nmea.seen_gngsa = true;
1876
            // field 18 is the NMEA gnssid in 4.10 and up.
1877
            // nmea_gnssid set above
1878
0
            break;
1879
0
        case 'P':
1880
            // GP GPS
1881
0
            session->nmea.seen_gpgsa = true;
1882
0
            nmea_gnssid = 1;
1883
0
            break;
1884
0
        case 'Q':
1885
            // Quectel EC25 & EC21 use PQGSA for QZSS and GLONASS
1886
0
            if ('P' == field[0][0] &&
1887
0
                0 != nmea_gnssid) {
1888
                /* Quectel EC25 & EC21 use PQGSV for BeiDou or QZSS
1889
                 * nmea_gnssid set above.  What about seen?
1890
                 */
1891
0
                break;
1892
0
            }
1893
0
            FALLTHROUGH
1894
0
        case 'Z':        // QZ QZSS
1895
            // NMEA 4.11 GQGSA for QZSS
1896
0
            nmea_gnssid = 5;
1897
0
            session->nmea.seen_qzgsa = true;
1898
0
            break;
1899
0
        }
1900
1901
        /* The magic 6 here is the tag, two mode fields, and three DOP fields.
1902
         * Maybe 7, NMEA 4.10+, also has gnssid field. */
1903
0
        for (i = 0; i < count - 6; i++) {
1904
0
            int prn;
1905
0
            int n;
1906
0
            int nmea_satnum;            // almost svid...
1907
0
            unsigned char ubx_gnssid;   // UNUSED
1908
0
            unsigned char ubx_svid;     // UNUSED
1909
1910
            // skip empty fields, otherwise empty becomes prn=200
1911
0
            if ('\0' == field[i + 3][0]) {
1912
0
                continue;
1913
0
            }
1914
0
            if (NULL != strchr(field[i + 3], '.')) {
1915
                // found a float, must be PDOP, done.
1916
0
                break;
1917
0
            }
1918
0
            nmea_satnum = atoi(field[i + 3]);
1919
0
            if (1 > nmea_satnum ||
1920
0
                600 < nmea_satnum) {
1921
0
                continue;
1922
0
            }
1923
0
            prn = nmeaid_to_prn(field[0], nmea_satnum, nmea_gnssid,
1924
0
                                &ubx_gnssid, &ubx_svid);
1925
1926
#if 0       // debug
1927
            GPSD_LOG(LOG_SHOUT, &session->context->errout,
1928
                     "NMEA0183: %s PRN %d nmea_gnssid %d "
1929
                     "nmea_satnum %d ubx_gnssid %d ubx_svid %d count %d \n",
1930
                     field[0], prn, nmea_gnssid, nmea_satnum, ubx_gnssid,
1931
                     ubx_svid, count);
1932
#endif      //  debug
1933
1934
0
            if (0 >= prn) {
1935
                // huh?
1936
0
                continue;
1937
0
            }
1938
            // check first BEFORE over-writing memory
1939
0
            if (MAXCHANNELS <= session->gpsdata.satellites_used) {
1940
                /* This should never happen as xxGSA is limited to 12,
1941
                 * except for the Navior-24 CH-4701.
1942
                 * But it could happen with multiple GSA per cycle */
1943
0
                GPSD_LOG(LOG_ERROR, &session->context->errout,
1944
0
                         "NMEA0183: %s used >= MAXCHANNELS!\n", field[0]);
1945
0
                break;
1946
0
            }
1947
            /* check for duplicate.
1948
             * Often GPS in both $GPGSA and $GNGSA, for example Quectel. */
1949
0
            for (n = 0; n < MAXCHANNELS; n++) {
1950
0
                if ( 0 == session->nmea.sats_used[n]) {
1951
                    // unused slot, use it.
1952
0
                    session->nmea.sats_used[n] = (unsigned short)prn;
1953
0
                    session->gpsdata.satellites_used = n + 1;
1954
0
                    break;
1955
0
                }
1956
0
                if (session->nmea.sats_used[n] == (unsigned short)prn) {
1957
                    // Duplicate!
1958
0
                    break;
1959
0
                }
1960
0
            }
1961
0
        }
1962
0
        mask |= USED_IS;
1963
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
1964
0
                 "NMEA0183: %s: mode=%d used=%d pdop=%.2f hdop=%.2f "
1965
0
                 "vdop=%.2f nmea_gnssid %d\n",
1966
0
                 field[0], session->newdata.mode,
1967
0
                 session->gpsdata.satellites_used,
1968
0
                 session->gpsdata.dop.pdop,
1969
0
                 session->gpsdata.dop.hdop,
1970
0
                 session->gpsdata.dop.vdop, nmea_gnssid);
1971
0
    }
1972
    // assumes GLGSA or BDGSA, if present, is emitted directly after the GPGSA
1973
0
    if ((session->nmea.seen_bdgsa ||
1974
0
         session->nmea.seen_gagsa ||
1975
0
         session->nmea.seen_gigsa ||
1976
0
         session->nmea.seen_glgsa ||
1977
0
         session->nmea.seen_gngsa ||
1978
0
         session->nmea.seen_qzgsa) &&
1979
0
         GSA_TALKER == 'P') {
1980
0
        mask = ONLINE_SET;
1981
0
    } else if ('N' != last_last_gsa_talker &&
1982
0
               'N' == GSA_TALKER) {
1983
        /* first of two GNGSA
1984
         * if mode == 1 some GPS only output 1 GNGSA, so ship mode always */
1985
0
        mask =  ONLINE_SET | MODE_SET;
1986
0
    }
1987
1988
    // cast for 32/64 compatibility
1989
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
1990
0
             "NMEA0183: %s: count %d visible %d used %d mask %#llx\n",
1991
0
             field[0], count, session->gpsdata.satellites_used,
1992
0
             session->gpsdata.satellites_used,
1993
0
             (long long unsigned)mask);
1994
0
    return mask;
1995
0
#undef GSA_TALKER
1996
0
}
1997
1998
// GST - GPS Pseudorange Noise Statistics
1999
static gps_mask_t processGST(int count, char *field[],
2000
                             struct gps_device_t *session)
2001
0
{
2002
    /*
2003
     * GST,hhmmss.ss,x,x,x,x,x,x,x,*hh
2004
     * 1 UTC time of associated GGA fix
2005
     * 2 Total RMS standard deviation of ranges inputs to the nav solution
2006
     * 3 Standard deviation (meters) of semi-major axis of error ellipse
2007
     * 4 Standard deviation (meters) of semi-minor axis of error ellipse
2008
     * 5 Orientation of semi-major axis of error ellipse (true north degrees)
2009
     * 6 Standard deviation (meters) of latitude error
2010
     * 7 Standard deviation (meters) of longitude error
2011
     * 8 Standard deviation (meters) of altitude error
2012
     * 9 Checksum
2013
     */
2014
0
    struct tm date = {0};
2015
0
    timespec_t ts;
2016
0
    int ret;
2017
0
    char ts_buf[TIMESPEC_LEN];
2018
0
    gps_mask_t mask = ONLINE_SET;
2019
2020
0
    if (0 > count) {
2021
0
      return mask;
2022
0
    }
2023
2024
    // since it is NOT current time, do not register_fractional_time()
2025
    // compute start of today
2026
0
    if (0 < session->nmea.date.tm_year) {
2027
        // Do not bother if no current year
2028
0
        memset(&date, 0, sizeof(date));
2029
0
        date.tm_year = session->nmea.date.tm_year;
2030
0
        date.tm_mon = session->nmea.date.tm_mon;
2031
0
        date.tm_mday = session->nmea.date.tm_mday;
2032
2033
        /* note this is not full UTC, just HHMMSS.ss
2034
         * this is not the current time,
2035
         * it references another GPA of the same stamp. So do not set
2036
         * any time stamps with it */
2037
0
        ret = decode_hhmmss(&date, &ts.tv_nsec, field[1], session);
2038
0
    } else {
2039
0
        ret = 1;
2040
0
    }
2041
0
    if (0 == ret) {
2042
        // convert to timespec_t , tv_nsec already set
2043
0
        session->gpsdata.gst.utctime.tv_sec = mkgmtime(&date);
2044
0
        session->gpsdata.gst.utctime.tv_nsec = ts.tv_nsec;
2045
0
    } else {
2046
        // no idea of UTC time now
2047
0
        session->gpsdata.gst.utctime.tv_sec = 0;
2048
0
        session->gpsdata.gst.utctime.tv_nsec = 0;
2049
0
    }
2050
0
    session->gpsdata.gst.rms_deviation       = safe_atof(field[2]);
2051
0
    session->gpsdata.gst.smajor_deviation    = safe_atof(field[3]);
2052
0
    session->gpsdata.gst.sminor_deviation    = safe_atof(field[4]);
2053
0
    session->gpsdata.gst.smajor_orientation  = safe_atof(field[5]);
2054
0
    session->gpsdata.gst.lat_err_deviation   = safe_atof(field[6]);
2055
0
    session->gpsdata.gst.lon_err_deviation   = safe_atof(field[7]);
2056
0
    session->gpsdata.gst.alt_err_deviation   = safe_atof(field[8]);
2057
2058
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2059
0
             "NMEA0183: GST: utc = %s, rms = %.2f, maj = %.2f, min = %.2f,"
2060
0
             " ori = %.2f, lat = %.2f, lon = %.2f, alt = %.2f\n",
2061
0
             timespec_str(&session->gpsdata.gst.utctime, ts_buf,
2062
0
                          sizeof(ts_buf)),
2063
0
             session->gpsdata.gst.rms_deviation,
2064
0
             session->gpsdata.gst.smajor_deviation,
2065
0
             session->gpsdata.gst.sminor_deviation,
2066
0
             session->gpsdata.gst.smajor_orientation,
2067
0
             session->gpsdata.gst.lat_err_deviation,
2068
0
             session->gpsdata.gst.lon_err_deviation,
2069
0
             session->gpsdata.gst.alt_err_deviation);
2070
2071
0
    mask = GST_SET | ONLINE_SET;
2072
0
    return mask;
2073
0
}
2074
2075
// xxGSV -  GPS Satellites in View
2076
static gps_mask_t processGSV(int count, char *field[],
2077
                             struct gps_device_t *session)
2078
0
{
2079
0
#define GSV_TALKER      field[0][1]
2080
    /*
2081
     * GSV,2,1,08,01,40,083,46,02,17,308,41,12,07,344,39,14,22,228,45*75
2082
     *  1) 2           Number of sentences for full data
2083
     *  2) 1           Sentence 1 of 2
2084
     *  3) 08          Total number of satellites in view
2085
     *  4) 01          Satellite PRN number
2086
     *  5) 40          Elevation, degrees
2087
     *  6) 083         Azimuth, degrees
2088
     *  7) 46          Signal-to-noise ratio in decibels
2089
     * <repeat for up to 4 satellites per sentence>
2090
     *   m - 1)        NMEA 4.10 Signal Id (optional), hexadecimal
2091
     *                 Quecktel Querk: 0 for "All Signa;s".
2092
     *   m=n-1)        Quectel Querk: System ID (optional)
2093
     *                     4 = BeiDou, 5 = QZSS
2094
     *   n)            checksum
2095
     *
2096
     * NMEA 4.1+:
2097
     * $GAGSV,3,1,09,02,00,179,,04,09,321,,07,11,134,11,11,10,227,,7*7F
2098
     * after the satellite block, before the checksum, new field:
2099
     *             NMEA Signal ID, depends on constellation.
2100
     &             see include/gps.h
2101
     *
2102
     * Quectel Querk:
2103
     * $PQGSV,4,2,15,09,16,120,,10,26,049,,16,07,123,,19,34,212,,0,4*62
2104
     * after the Signal ID, before the checksum, new field:
2105
     *             System ID
2106
     *             4 = BeiDou
2107
     *             5 = QZSS
2108
     *
2109
     * Can occur with talker IDs:
2110
     *   BD (Beidou),
2111
     *   GA (Galileo),
2112
     *   GB (Beidou),
2113
     *   GI (IRNSS),
2114
     *   GL (GLONASS),
2115
     *   GN (GLONASS, any combination GNSS),
2116
     *   GP (GPS, SBAS, QZSS),
2117
     *   GQ (QZSS).
2118
     *   PQ (QZSS). Quectel Querk. BeiDou or QZSS
2119
     *   QZ (QZSS).
2120
     *
2121
     * As of April 2019:
2122
     *    no gpsd regressions have GNGSV
2123
     *    every xxGSV cycle starts with GPGSV
2124
     *    xxGSV cycles may be spread over several xxRMC cycles
2125
     *
2126
     * GL may be (incorrectly) used when GSVs are mixed containing
2127
     * GLONASS, GN may be (incorrectly) used when GSVs contain GLONASS
2128
     * only.  Usage is inconsistent.
2129
     *
2130
     * In the GLONASS version sat IDs run from 65-96 (NMEA0183
2131
     * standardizes this). At least two GPSes, the BU-353 GLONASS and
2132
     * the u-blox NEO-M8N, emit a GPGSV set followed by a GLGSV set.
2133
     * We have also seen two GPSes, the Skytraq S2525F8-BD-RTK and a
2134
     * SiRF-IV variant, that emit GPGSV followed by BDGSV. We need to
2135
     * combine these.
2136
     *
2137
     * The following shows how the Skytraq S2525F8-BD-RTK output both
2138
     * GPGSV and BDGSV in the same cycle:
2139
     * $GPGSV,4,1,13,23,66,310,29,03,65,186,33,26,43,081,27,16,41,124,38*78
2140
     * $GPGSV,4,2,13,51,37,160,38,04,37,066,25,09,34,291,07,22,26,156,37*77
2141
     * $GPGSV,4,3,13,06,19,301,,31,17,052,20,193,11,307,,07,11,232,27*4F
2142
     * $GPGSV,4,4,13,01,03,202,30*4A
2143
     * $BDGSV,1,1,02,214,55,153,40,208,01,299,*67
2144
     *
2145
     * The driver automatically adapts to either case, but it takes until the
2146
     * second cycle (usually 10 seconds from device connect) for it to
2147
     * learn to expect BDGSV or GLGSV.
2148
     *
2149
     * Some GPS (Garmin 17N) spread the xxGSV over several cycles.  So
2150
     * cycles, or cycle time, can not be used to determine start of
2151
     * xxGSV cycle.
2152
     *
2153
     * NMEA 4.1 adds a signal-ID field just before the checksum. First
2154
     * seen in May 2015 on a u-blox M8.  It can output 2 sets of GPGSV
2155
     * in one cycle, one for L1C and the other for L2C.
2156
     *
2157
     * Once again, Quectel is Querky.  They added the $PQGSV sentence
2158
     * to handle what NMEA 4.11 says should be in the $BDGSV and $GQGSV
2159
     * sentences.  $PQGSV adds a new field just before the checksum for the
2160
     * System ID. This field is set to 4 for BeiDou, or 5 for QZSS.  The EG25
2161
     * output can look like this:
2162
     *
2163
     * $GLGSV,2,1,08,78,37,039,22,79,53,317,21,69,56,275,20,88,23,077,18,1*7A
2164
     * $GLGSV,2,2,08,87,11,030,17,68,37,195,21,70,11,331,,81,13,129,,1*79
2165
     * $PQGSV,4,1,15,02,20,116,,03,,,,05,34,137,,07,05,046,,0,4*58
2166
     * $PQGSV,4,2,15,09,16,120,,10,26,049,,16,07,123,,19,34,212,,0,4*62
2167
     * $PQGSV,4,3,15,20,03,174,,21,05,324,,22,37,281,,27,28,085,,0,4*65
2168
     * $PQGSV,4,4,15,28,07,039,,29,,,,30,23,143,,0,4*67
2169
     * $GAGSV,1,1,02,04,53,296,,09,05,322,,7*71
2170
     * $GPGSV,3,1,10,13,80,247,17,14,47,043,19,15,48,295,20,17,48,108,17,1*62
2171
     * $GPGSV,3,2,10,19,36,151,18,30,32,087,18,05,14,230,,07,03,098,,1*65
2172
     * $GPGSV,3,3,10,12,00,234,,24,13,295,,1*69
2173
     *
2174
     * Skytraq PX1172RH_DS can output GPGSV, GLGSV, GAGSV and GBGSV all in
2175
     * same epoch.  And each of those repeated for different signals
2176
     * (L1C/L2C/etc.)
2177
     */
2178
2179
0
    int n, fldnum;
2180
0
    unsigned char  nmea_sigid = 0;
2181
0
    int nmea_gnssid = 0;
2182
0
    unsigned char  ubx_sigid = 0;
2183
0
    int sane = 0;
2184
2185
0
    if (3 >= count) {
2186
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2187
0
                 "NMEA0183: %s, malformed - fieldcount %d <= 3\n",
2188
0
                 field[0], count);
2189
0
        gpsd_zero_satellites(&session->gpsdata);
2190
0
        return ONLINE_SET;
2191
0
    }
2192
0
    session->nmea.await = atoi(field[1]);
2193
0
    if (1 > session->nmea.await ||
2194
0
        10 < session->nmea.await) {
2195
        // numbeof sentences is either 0, or too many
2196
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2197
0
                 "NMEA0183: %s: bad number of sentences %d\n",
2198
0
                 field[0], session->nmea.await);
2199
0
        gpsd_zero_satellites(&session->gpsdata);
2200
0
        return ONLINE_SET;
2201
0
    }
2202
0
    session->nmea.part = atoi(field[2]);
2203
0
    if (1 > session->nmea.part ||
2204
0
        session->nmea.await < session->nmea.part) {
2205
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2206
0
                 "NMEA0183: %s: bad part %d of %d\n",
2207
0
                 field[0], session->nmea.part, session->nmea.await);
2208
0
        gpsd_zero_satellites(&session->gpsdata);
2209
0
        return ONLINE_SET;
2210
0
    }
2211
2212
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2213
0
             "NMEA0183: %s: part %s of %s, last_gsv_talker '%#x' "
2214
0
             " last_gsv_sigid %u\n",
2215
0
             field[0], field[2], field[1],
2216
0
             session->nmea.last_gsv_talker,
2217
0
             session->nmea.last_gsv_sigid);
2218
2219
    /*
2220
     * This check used to be !=0, but we have loosen it a little to let by
2221
     * NMEA 4.1 GSVs with an extra signal-ID field at the end.  Then loosen
2222
     * some more for Quectel  Querky $PQGSV.
2223
     */
2224
0
    switch (count % 4) {
2225
0
    case 0:
2226
        // normal, pre-NMEA 4.10
2227
0
        break;
2228
0
    case 1:
2229
        // NMEA 4.10, and later, get the signal ID
2230
0
        nmea_sigid = hex2uchar(field[count - 1][0]);
2231
0
        break;
2232
0
    case 2:
2233
        // Quectel Querk. $PQGSV, get the signal ID, and system ID
2234
0
        nmea_sigid = hex2uchar(field[count - 2][0]);
2235
0
        nmea_gnssid = atoi(field[count - 1]);
2236
0
        if (4 > nmea_gnssid ||
2237
0
            5 < nmea_gnssid) {
2238
            // Quectel says only 4 or 5
2239
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
2240
0
                     "NMEA0183: %sm invalid nmea_gnssid %d\n",
2241
0
                     field[0], nmea_gnssid);
2242
0
            return ONLINE_SET;
2243
0
        }
2244
0
        break;
2245
0
    default:
2246
        // bad count
2247
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2248
0
                 "NMEA0183: malformed %s - fieldcount(%d)\n",
2249
0
                 field[0], count);
2250
0
        gpsd_zero_satellites(&session->gpsdata);
2251
0
        return ONLINE_SET;
2252
0
    }
2253
2254
0
    if (1 == session->nmea.part) {
2255
        /*
2256
         * might have gone from GPGSV to GLGSV/BDGSV/QZGSV,
2257
         * in which case accumulate
2258
         *
2259
         * NMEA 4.1 might have gone from GPGVS,sigid=x to GPGSV,sigid=y
2260
         *
2261
         * Quectel EG25 can go GLGSV, PQGSV, GAGSV, GPGSV, in one cycle.
2262
         *
2263
         * session->nmea.last_gsv_talker is zero at cycle start
2264
         */
2265
0
        if ('\0' == session->nmea.last_gsv_talker) {
2266
            // Assume all xxGSV in same epoch.  Clear at 1st in eopoch.
2267
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
2268
0
                     "NMEA0183: %s: new part %d, last_gsv_talker '%#x', "
2269
0
                     "zeroing\n",
2270
0
                     field[0],
2271
0
                     session->nmea.part,
2272
0
                     session->nmea.last_gsv_talker);
2273
0
            gpsd_zero_satellites(&session->gpsdata);
2274
0
        }
2275
0
    }
2276
2277
0
    session->nmea.last_gsv_talker = GSV_TALKER;
2278
0
    switch (GSV_TALKER) {
2279
0
    case 'A':        // GA Galileo
2280
0
        nmea_gnssid = 3;
2281
        // Quectel LC79D can have sigid 6 (L1-A) and 1 (E5a)
2282
0
        session->nmea.seen_gagsv = true;
2283
0
        break;
2284
0
    case 'B':        // GB BeiDou
2285
0
        FALLTHROUGH
2286
0
    case 'D':        // BD BeiDou
2287
0
        nmea_gnssid = 4;
2288
0
        session->nmea.seen_bdgsv = true;
2289
0
        break;
2290
0
    case 'I':        // GI IRNSS
2291
0
        nmea_gnssid = 6;
2292
0
        session->nmea.seen_gigsv = true;
2293
0
        break;
2294
0
    case 'L':        // GL GLONASS
2295
0
        nmea_gnssid = 2;
2296
0
        session->nmea.seen_glgsv = true;
2297
0
        break;
2298
0
    case 'N':        // GN GNSS
2299
0
        session->nmea.seen_gngsv = true;
2300
0
        break;
2301
0
    case 'P':        // GP GPS
2302
0
        session->nmea.seen_gpgsv = true;
2303
0
        break;
2304
0
    case 'Q':        // $GQ, and $PQ (Quectel Querk) QZSS
2305
0
        if ('P' == field[0][0] &&
2306
0
            0 != nmea_gnssid) {
2307
            /* Quectel EC25 & EC21 use PQGSV for BeiDou or QZSS
2308
             * 4 = BeiDou, 5 = QZSS
2309
             * nmea_gnssid set above, what about seen?
2310
             */
2311
0
            if (4 == nmea_gnssid) {
2312
0
                session->nmea.seen_bdgsv = true;
2313
0
            } else if (5 == nmea_gnssid) {
2314
0
                session->nmea.seen_qzgsv = true;
2315
0
            } else {
2316
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
2317
0
                         "NMEA0183: %s: invalid nmea_gnssid %d\n",
2318
0
                         field[0], nmea_gnssid);
2319
0
                return ONLINE_SET;
2320
0
            }
2321
0
            break;
2322
0
        }
2323
        // else $GQ
2324
0
        FALLTHROUGH
2325
0
    case 'Z':        // QZ QZSS
2326
0
        nmea_gnssid = 5;
2327
0
        session->nmea.seen_qzgsv = true;
2328
0
        break;
2329
0
    default:
2330
        // uh, what?
2331
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2332
0
                 "NMEA0183: %s: unknown nmea_gnssid %d\n",
2333
0
                 field[0], nmea_gnssid);
2334
0
        break;
2335
0
    }
2336
2337
    // If NMEA 4.10, or later,then, or Quectel
2338
0
    if (0 != nmea_sigid) {
2339
        // get ubx sig_id from nmea_gnssid, nmea_sigid, get from talker ID
2340
0
        ubx_sigid = nmea_sigid_to_ubx(session, nmea_gnssid, nmea_sigid);
2341
0
    }
2342
0
    session->nmea.last_gsv_sigid = ubx_sigid;  // UNUSED
2343
2344
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2345
0
             "NMEA0183: %s: part %d of %d nmea_gnssid %d nmea_sigid %d "
2346
0
             "ubx_sigid %d\n",
2347
0
             field[0], session->nmea.part, session->nmea.await,
2348
0
             nmea_gnssid, nmea_sigid, ubx_sigid);
2349
2350
0
    for (fldnum = 4; fldnum < count / 4 * 4;) {
2351
0
        struct satellite_t *sp;
2352
0
        int nmea_svid;
2353
2354
0
        if (MAXCHANNELS <= session->gpsdata.satellites_visible) {
2355
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
2356
0
                     "NMEA0183: %s: internal error - too many "
2357
0
                     "satellites [%d]!\n",
2358
0
                     field[0], session->gpsdata.satellites_visible);
2359
0
            gpsd_zero_satellites(&session->gpsdata);
2360
0
            break;
2361
0
        }
2362
0
        sp = &session->gpsdata.skyview[session->gpsdata.satellites_visible];
2363
0
        nmea_svid = atoi(field[fldnum++]);
2364
0
        if (0 == nmea_svid) {
2365
            // skip bogus fields
2366
0
            continue;
2367
0
        }
2368
0
        sp->PRN = (short)nmeaid_to_prn(field[0], nmea_svid, nmea_gnssid,
2369
0
                                       &sp->gnssid, &sp->svid);
2370
2371
0
        sp->elevation = (double)atoi(field[fldnum++]);
2372
0
        sp->azimuth = (double)atoi(field[fldnum++]);
2373
0
        sp->ss = (double)atoi(field[fldnum++]);
2374
0
        sp->used = false;
2375
0
        sp->sigid = ubx_sigid;
2376
2377
        /* sadly NMEA 4.1 does not tell us which sigid (L1, L2) is
2378
         * used.  So if the ss is zero, do not mark used */
2379
0
        if (0 < sp->PRN &&
2380
0
            0 < sp->ss) {
2381
0
            for (n = 0; n < MAXCHANNELS; n++) {
2382
0
                if (session->nmea.sats_used[n] == (unsigned short)sp->PRN) {
2383
0
                    sp->used = true;
2384
0
                    break;
2385
0
                }
2386
0
            }
2387
0
        }
2388
#if 0   // debug
2389
        GPSD_LOG(LOG_SHOUT, &session->context->errout,
2390
                 "NMEA0183: %s nmea_gnssid %d nmea_satnum %d ubx_gnssid %d "
2391
                 "ubx_svid %d nmea2_prn %d az %.1f el %.1f used %d\n",
2392
                 field[0], nmea_gnssid, nmea_svid, sp->gnssid, sp->svid,
2393
                 sp->PRN, sp->elevation, sp->azimuth, sp->used);
2394
#endif  // debug
2395
2396
        /*
2397
         * Incrementing this unconditionally falls afoul of chipsets like
2398
         * the Motorola Oncore GT+ that emit empty fields at the end of the
2399
         * last sentence in a GPGSV set if the number of satellites is not
2400
         * a multiple of 4.
2401
         */
2402
0
        session->gpsdata.satellites_visible++;
2403
0
    }
2404
2405
#if 0    // debug code
2406
    GPSD_LOG(LOG_SHOUT, &session->context->errout,
2407
        "NMEA0183: %s: vis %d bdgsv %d gagsv %d gigsv %d glgsv %d "
2408
        "gngsv %d qpgsv %d qzgsv %d\n",
2409
        field[0],
2410
        session->gpsdata.satellites_visible,
2411
        session->nmea.seen_bdgsv,
2412
        session->nmea.seen_gagsv,
2413
        session->nmea.seen_gigsv,
2414
        session->nmea.seen_glgsv,
2415
        session->nmea.seen_gngsv,
2416
        session->nmea.seen_gpgsv,
2417
        session->nmea.seen_qzgsv);
2418
#endif  // debug
2419
2420
#if 0
2421
    /*
2422
     * Alas, we can't sanity check field counts when there are multiple sat
2423
     * pictures, because the visible member counts *all* satellites - you
2424
     * get a bad result on the second and later SV spans.  Note, this code
2425
     * assumes that if any of the special sat pics occur they come right
2426
     * after a stock GPGSV one.
2427
     *
2428
     * FIXME: Add per-talker totals so we can do this check properly.
2429
     */
2430
    if (!(session->nmea.seen_bdgsv ||
2431
          session->nmea.seen_gagsv ||
2432
          session->nmea.seen_gigsv ||
2433
          session->nmea.seen_glgsv ||
2434
          session->nmea.seen_gngsv ||
2435
          session->nmea.seen_qzgsv)) {
2436
        if (session->nmea.part == session->nmea.await
2437
                && atoi(field[3]) != session->gpsdata.satellites_visible) {
2438
            GPSD_LOG(LOG_WARN, &session->context->errout,
2439
                     "NMEA0183: %s field 3 value of %d != actual count %d\n",
2440
                     field[0], atoi(field[3]),
2441
                     session->gpsdata.satellites_visible);
2442
        }
2443
    }
2444
#endif    // FIXME
2445
2446
    // not valid data until we've seen a complete set of parts
2447
0
    if (session->nmea.part < session->nmea.await) {
2448
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
2449
0
                 "NMEA0183: %s: Partial satellite data (%d of %d).\n",
2450
0
                 field[0], session->nmea.part, session->nmea.await);
2451
0
        session->nmea.gsx_more = true;
2452
0
        return ONLINE_SET;
2453
0
    }
2454
0
    session->nmea.gsx_more = false;
2455
0
    if (MAXCHANNELS < session->gpsdata.satellites_visible) {
2456
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2457
0
                "NMEA0183: %s too many satellites %d\n", field[0],
2458
0
                 session->gpsdata.satellites_visible);
2459
0
        session->gpsdata.satellites_visible = MAXCHANNELS;
2460
0
    }
2461
    /*
2462
     * This sanity check catches an odd behavior of SiRFstarII receivers.
2463
     * When they can't see any satellites at all (like, inside a
2464
     * building) they sometimes cough up a hairball in the form of a
2465
     * GSV packet with all the azimuth entries 0 (but nonzero
2466
     * elevations).  This behavior was observed under SiRF firmware
2467
     * revision 231.000.000_A2.
2468
     */
2469
0
    sane = 0;
2470
0
    for (n = 0; n < session->gpsdata.satellites_visible; n++) {
2471
0
        if (0 != session->gpsdata.skyview[n].azimuth) {
2472
0
            sane = 1;
2473
0
            break;
2474
0
        }
2475
0
    }
2476
2477
0
    if (0 == sane) {
2478
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2479
0
                 "NMEA0183: %s: Satellite data no good (%d of %d).\n",
2480
0
                 field[0], session->nmea.part, session->nmea.await);
2481
0
        gpsd_zero_satellites(&session->gpsdata);
2482
0
        return ONLINE_SET;
2483
0
    }
2484
2485
0
    session->gpsdata.skyview_time.tv_sec = 0;
2486
0
    session->gpsdata.skyview_time.tv_nsec = 0;
2487
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2488
0
             "NMEA0183: %s: Satellite data OK (%d of %d).\n",
2489
0
             field[0], session->nmea.part, session->nmea.await);
2490
2491
    /* assumes GLGSV or BDGSV group, if present, is emitted after the GPGSV
2492
     * An assumption that Quectel breaks;  The EG25 can send in one epoch:
2493
     * $GLGSV, $PQGSV, $GAGSV, then $GPGSV! */
2494
0
    if ((session->nmea.seen_bdgsv ||
2495
0
         session->nmea.seen_gagsv ||
2496
0
         session->nmea.seen_gigsv ||
2497
0
         session->nmea.seen_glgsv ||
2498
0
         session->nmea.seen_gngsv ||
2499
0
         session->nmea.seen_qzgsv) &&
2500
0
        ('P' == GSV_TALKER &&
2501
0
         'P' != session->nmea.end_gsv_talker)) {
2502
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
2503
0
                 "NMEA0183: %s: not end talker %d\n",
2504
0
                 field[0], session->nmea.end_gsv_talker);
2505
0
        return ONLINE_SET;
2506
0
    }
2507
2508
#if 0   // debug code
2509
    {
2510
        char ts_buf[TIMESPEC_LEN];
2511
        char ts_buf1[TIMESPEC_LEN];
2512
        GPSD_LOG(LOG_SHOUT, &session->context->errout,
2513
            "NMEA0183: %s: set skyview_time %s frac_time %s\n",
2514
            field[0],
2515
            timespec_str(&session->gpsdata.skyview_time, ts_buf,
2516
                         sizeof(ts_buf)),
2517
            timespec_str(&session->nmea.this_frac_time, ts_buf1,
2518
                         sizeof(ts_buf1)));
2519
    }
2520
#endif  // debug
2521
2522
0
    return SATELLITE_SET;
2523
0
#undef GSV_TALKER
2524
0
}
2525
2526
/*
2527
 * Unicore $GYOACC  MEMS Sensor DAta
2528
 * Note: Invalid sender: $GY
2529
 */
2530
static gps_mask_t processGYOACC(int c UNUSED, char *field[],
2531
                             struct gps_device_t *session)
2532
0
{
2533
    /*
2534
     * $GYOACC,050624,002133.10,0.004634,0.000273,0.004348,100,-4.666065,
2535
     *    -3.466573,7.960348,100,31,0,100,0*02
2536
     */
2537
0
    gps_mask_t mask = ONLINE_SET;
2538
0
    double gyroX = safe_atof(field[3]);       // deg/s
2539
0
    double gyroY = safe_atof(field[4]);       // deg/s
2540
0
    double gyroZ = safe_atof(field[5]);       // deg/s
2541
0
    unsigned gyroPeriod = atoi(field[6]);     // period in ms
2542
0
    double accX = safe_atof(field[7]);        // m/s^2
2543
0
    double accY = safe_atof(field[8]);        // m/s^2
2544
0
    double accZ = safe_atof(field[9]);        // m/s^2
2545
0
    unsigned accPeriod = atoi(field[10]);     // period in ms
2546
0
    int temp = atoi(field[11]);               // temperature C
2547
0
    unsigned speed = atoi(field[12]);         // pulses
2548
0
    unsigned pulsePeriod = atoi(field[13]);   // period in ms
2549
0
    unsigned fwd = atoi(field[14]);           // 0 == forward, 1 == reverse
2550
0
    struct tm date = {0};
2551
0
    timespec_t ts = {0};
2552
2553
    // Not at the same rate at the GNSS epoch. So do not use session->nmea
2554
0
    if (0 == decode_hhmmss(&date, &ts.tv_nsec, field[2], session) &&
2555
0
        0 == decode_ddmmyy(&date, field[1], session)) {
2556
2557
0
        session->gpsdata.attitude.mtime.tv_sec = mkgmtime(&date);
2558
0
        session->gpsdata.attitude.mtime.tv_nsec = ts.tv_nsec;
2559
0
    } else {
2560
0
        session->gpsdata.attitude.mtime.tv_sec = 0;
2561
0
        session->gpsdata.attitude.mtime.tv_nsec = 0;
2562
0
    }
2563
2564
0
    session->gpsdata.attitude.gyro_x = gyroX;
2565
0
    session->gpsdata.attitude.gyro_y = gyroY;
2566
0
    session->gpsdata.attitude.gyro_z = gyroZ;
2567
0
    session->gpsdata.attitude.acc_x = accX;
2568
0
    session->gpsdata.attitude.acc_y = accY;
2569
0
    session->gpsdata.attitude.acc_z = accZ;
2570
0
    mask |= ATTITUDE_SET;
2571
2572
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2573
0
             "NMEA0183: $GYOACC time %lld.%09lld "
2574
0
             "gyro X %.6f Y %.6f Z %.6f per %u "
2575
0
             "acc X %.6f Y %.6f Z %.6f per %u "
2576
0
             "temp %d speed %u per %u fwd %u\n",
2577
0
             (long long)session->gpsdata.attitude.mtime.tv_sec,
2578
0
             (long long)session->gpsdata.attitude.mtime.tv_nsec,
2579
0
             gyroX, gyroY, gyroZ, gyroPeriod,
2580
0
             accX, accY, accZ, accPeriod,
2581
0
             temp, speed, pulsePeriod, fwd);
2582
0
    return mask;
2583
0
}
2584
2585
static gps_mask_t processHDG(int c UNUSED, char *field[],
2586
                             struct gps_device_t *session)
2587
0
{
2588
    /*
2589
     *  $SDHDG,234.6,,,1.3,E*34
2590
     *
2591
     *  $--HDG,h.h,d.d,a,v.v,a*hh<CR><LF>
2592
     *  Magnetic sensor heading, degrees
2593
     *  Magnetic deviation, degrees E/W
2594
     *  Magnetic variation, degrees, E/W
2595
     *
2596
     *  1. To obtain Magnetic Heading:
2597
     *  Add Easterly deviation (E) to Magnetic Sensor Reading
2598
     *  Subtract Westerly deviation (W) from Magnetic Sensor Reading
2599
     *  2. To obtain True Heading:
2600
     *  Add Easterly variation (E) to Magnetic Heading
2601
     *  Subtract Westerly variation (W) from Magnetic Heading
2602
     *  3. Variation and deviation fields shall be null fields if unknown.
2603
     */
2604
2605
0
    gps_mask_t mask = ONLINE_SET;
2606
0
    double sensor_heading;
2607
0
    double magnetic_deviation;
2608
2609
0
    if ('\0' == field[1][0]) {
2610
        // no data
2611
0
        return mask;
2612
0
    }
2613
0
    sensor_heading = safe_atof(field[1]);
2614
0
    if ((0.0 > sensor_heading) ||
2615
0
        (360.0 < sensor_heading)) {
2616
        // bad data */
2617
0
        return mask;
2618
0
    }
2619
0
    magnetic_deviation = safe_atof(field[2]);
2620
0
    if ((0.0 > magnetic_deviation) ||
2621
0
        (360.0 < magnetic_deviation)) {
2622
        // bad data
2623
0
        return mask;
2624
0
    }
2625
0
    switch (field[2][0]) {
2626
0
    case 'E':
2627
0
        sensor_heading += magnetic_deviation;
2628
0
        break;
2629
0
    case 'W':
2630
0
        sensor_heading += magnetic_deviation;
2631
0
        break;
2632
0
    default:
2633
        // ignore
2634
0
        break;
2635
0
    }
2636
2637
    // good data
2638
0
    session->newdata.magnetic_track = sensor_heading;
2639
0
    mask |= MAGNETIC_TRACK_SET;
2640
2641
    // get magnetic variation
2642
0
    if ('\0' != field[3][0] &&
2643
0
        '\0' != field[4][0]) {
2644
0
        session->newdata.magnetic_var = safe_atof(field[3]);
2645
2646
0
        switch (field[4][0]) {
2647
0
        case 'E':
2648
            // no change
2649
0
            mask |= MAGNETIC_TRACK_SET;
2650
0
            break;
2651
0
        case 'W':
2652
0
            session->newdata.magnetic_var = -session->newdata.magnetic_var;
2653
0
            mask |= MAGNETIC_TRACK_SET;
2654
0
            break;
2655
0
        default:
2656
            // huh?
2657
0
            session->newdata.magnetic_var = NAN;
2658
0
            break;
2659
0
        }
2660
0
    }
2661
2662
2663
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2664
0
             "NMEA0183: $SDHDG heading %lf var %.1f\n",
2665
0
             session->newdata.magnetic_track,
2666
0
             session->newdata.magnetic_var);
2667
0
    return mask;
2668
0
}
2669
2670
/* precessHDM() - process magnetic headingxxHDM messages
2671
 *
2672
 * Deprecated by NMEA in 2008
2673
 */
2674
static gps_mask_t processHDM(int c UNUSED, char *field[],
2675
                             struct gps_device_t *session)
2676
0
{
2677
    /*
2678
     * $APHDM,218.634,M*39
2679
     *
2680
     * 1) Magnetic heading
2681
     * 2) M == Magnetic
2682
     * )  checksum
2683
     *
2684
     */
2685
0
    gps_mask_t mask = ONLINE_SET;
2686
2687
0
    if ('\0' == field[1][0]) {
2688
        // no data
2689
0
        return mask;
2690
0
    }
2691
2692
    // assume good data
2693
0
    session->gpsdata.attitude.mheading = safe_atof(field[1]);
2694
0
    mask |= ATTITUDE_SET;
2695
2696
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2697
0
             "NMEA0183: $xxHDM: Magnetic heading %f\n",
2698
0
             session->gpsdata.attitude.mheading);
2699
0
    return mask;
2700
0
}
2701
2702
static gps_mask_t processHDT(int c UNUSED, char *field[],
2703
                             struct gps_device_t *session)
2704
0
{
2705
    /*
2706
     * $HEHDT,341.8,T*21
2707
     *
2708
     * $xxHDT,x.x*hh<cr><lf>
2709
     *
2710
     * The only data field is true heading in degrees.
2711
     * The following field is required to be 'T' indicating a true heading.
2712
     * It is followed by a mandatory nmea_checksum.
2713
     */
2714
0
    gps_mask_t mask = ONLINE_SET;
2715
0
    double heading;
2716
2717
0
    if ('\0' == field[1][0]) {
2718
        // no data
2719
0
        return mask;
2720
0
    }
2721
0
    heading = safe_atof(field[1]);
2722
0
    if (0.0 > heading ||
2723
0
        360.0 < heading) {
2724
        // bad data
2725
0
        return mask;
2726
0
    }
2727
    // True heading
2728
0
    session->gpsdata.attitude.heading = heading;
2729
2730
0
    mask |= ATTITUDE_SET;
2731
2732
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2733
0
             "NMEA0183: $xxHDT heading %lf.\n",
2734
0
             session->gpsdata.attitude.heading);
2735
0
    return mask;
2736
0
}
2737
2738
/* $INFO, Inertial Sense product info
2739
 * Not a legal NMEA message name
2740
 * https://docs.inertialsense.com/user-manual/com-protocol/nmea/#info
2741
 */
2742
static gps_mask_t processINFO(int c UNUSED, char *field[],
2743
                              struct gps_device_t *session)
2744
0
{
2745
    /*
2746
     * $INFO,928404541,1.0.2.0,2.2.2.0,-377462659,2.0.0.0,-53643429,
2747
     *    Inertial Sense Inc,2025-01-10,16:06:13.50,GPX -1,4,0, *7D
2748
     *
2749
     * 1  Serial number    Manufacturer serial number
2750
     * 2  Hardware version Hardware version
2751
     * 3  Firmware version Firmware version
2752
     * 4  Build number     Firmware build number
2753
     * 5  Protocol version Communications protocol version
2754
     * 6  Repo revision    Repository revision number
2755
     * 7  Manufacturer     Manufacturer name
2756
     * 8  Build date       Build date
2757
     * 9  Build time       Build time
2758
     * 10 Add Info         Additional information
2759
     * 11 Hardware         Hardware: 1=uINS, 2=EVB, 3=IMX, 4=GPX
2760
     * 12 Reserved         Reserved for internal purpose.
2761
     * 13 Build type       Build type:
2762
     *  'a'=ALPHA, 'b'=BETA, 'c'=RELEASE CANDIDATE, 'r'=PRODUCTION RELEASE,
2763
     *  'd'=debug, ' '= ????
2764
     */
2765
2766
    // hardwaare
2767
0
    static struct clist_t hardware[] = {
2768
0
        {'1', "uISN"},
2769
0
        {'2', "EVB"},
2770
0
        {'3', "INX"},
2771
0
        {'4', "GPX"},
2772
0
        {'\0', NULL},
2773
0
    };
2774
2775
0
    if ('\0' == session->subtype[0] &&
2776
0
        !session->context->passive) {
2777
        // first time seen, send init
2778
2779
0
        (void)nmea_send(session, "$STPC");   // stop all messages
2780
2781
        /* Enable all possible NMEA messages, at 1Hz
2782
         * 1 PIMU, 2 PPIMU, 3 PRIMU, 4 PINS1, 5 PINS2
2783
         * 6 PGPSP, 7 GGA, 8 GLL, 9 GSA, 10 RMC, 11 ZDA, 12 PASHR
2784
         * 13 PSTRB, 14 INFO, 15 GSV, 16 VTG
2785
         * there are many more...
2786
         */
2787
0
        (void)nmea_send(session,
2788
0
                        "$ASCE,0,"    // Set current port
2789
0
                        "1,0,"        // PIMU
2790
0
                        "2,0,"        // PPIMU
2791
0
                        "3,0,"        // PRIMU
2792
0
                        "4,0,"        // PINS1
2793
0
                        "5,0,"        // PINS2
2794
0
                        "6,5,"        // PGPSP
2795
0
                        "7,5,"        // GGA
2796
0
                        "8,5,"        // GLL
2797
0
                        "9,5,"        // GSA
2798
0
                        "10,5,"       // RMC
2799
0
                        "11,5,"       // ZDA
2800
0
                        "12,5,"       // PASHR
2801
0
                        "13,5,"       // PSTRB
2802
0
                        "14,0,"       // INFO
2803
0
                        "15,5,"       // GSV
2804
0
                        "16,5,"       // VTG
2805
0
                        "17,5,"       // ?
2806
0
                        "18,5");      // ?
2807
0
    }
2808
2809
    // save serial number
2810
0
    strlcpy(session->gpsdata.dev.sernum, field[1],
2811
0
            sizeof(session->gpsdata.dev.sernum));
2812
    // save HW as subtype
2813
0
    (void)snprintf(session->subtype, sizeof(session->subtype),
2814
0
                   "%s-%.11s",
2815
0
                   char2str(field[11][0], hardware), field[2]);
2816
    // save FW Version as subtype1
2817
0
    (void)snprintf(session->subtype1, sizeof(session->subtype1),
2818
0
                   "FW %.11s",
2819
0
                   field[3]);
2820
2821
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
2822
0
             "NMEA0183: INFO: serial %s subtype %s subtype1 %s\n",
2823
0
             session->gpsdata.dev.sernum, session->subtype, session->subtype1);
2824
2825
0
    return ONLINE_SET;
2826
0
}
2827
2828
static gps_mask_t processMTW(int c UNUSED, char *field[],
2829
                              struct gps_device_t *session)
2830
0
{
2831
    /* Water temp in degrees C
2832
     * $--MTW,x.x,C*hh<CR><LF>
2833
     *
2834
     * Fields in order:
2835
     * 1. water temp degrees C
2836
     * 2. C
2837
     * *hh          mandatory nmea_checksum
2838
     */
2839
0
    gps_mask_t mask = ONLINE_SET;
2840
2841
0
    if ('\0' == field[1][0] ||
2842
0
        'C' != field[2][0]) {
2843
        // no temp
2844
0
        return mask;
2845
0
    }
2846
0
    session->newdata.wtemp = safe_atof(field[1]);
2847
2848
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2849
0
        "NMEA0183: %s temp %.1f C\n",
2850
0
        field[0], session->newdata.wtemp);
2851
0
    return mask;
2852
0
}
2853
2854
static gps_mask_t processMWD(int c UNUSED, char *field[],
2855
                              struct gps_device_t *session)
2856
0
{
2857
    /*
2858
     * xxMWD - Wind direction and speed
2859
     * $xxMWD,x.x,T,x.x,M,x.x,N,x.x,M*hh<cr><lf>
2860
     * Fields in order:
2861
     * 1. wind direction, 0 to 359, True
2862
     * 2. T
2863
     * 3. wind direction, 0 to 359, Magnetic
2864
     * 4. M
2865
     * 5. wind speed, knots
2866
     * 6. N
2867
     * 7. wind speed, meters/sec
2868
     * 8. M
2869
     * *hh          mandatory nmea_checksum
2870
     */
2871
0
    gps_mask_t mask = ONLINE_SET;
2872
2873
0
    session->newdata.wanglet = safe_atof(field[1]);
2874
0
    session->newdata.wanglem = safe_atof(field[3]);
2875
0
    session->newdata.wspeedt = safe_atof(field[7]);
2876
0
    mask |= NAVDATA_SET;
2877
2878
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2879
0
        "NMEA0183: xxMWD wanglet %.2f wanglem %.2f wspeedt %.2f\n",
2880
0
        session->newdata.wanglet,
2881
0
        session->newdata.wanglem,
2882
0
        session->newdata.wspeedt);
2883
0
    return mask;
2884
0
}
2885
2886
static gps_mask_t processMWV(int c UNUSED, char *field[],
2887
                              struct gps_device_t *session)
2888
0
{
2889
    /*
2890
     * xxMWV - Wind speed and angle
2891
     * $xxMWV,x.x,a,x.x,a,A*hh<cr><lf>
2892
     * Fields in order:
2893
     * 1. wind angle, 0 to 359, True
2894
     * 2. R = Relative (apparent), T = Theoretical (calculated)
2895
     *    Is T magnetic or true??
2896
     * 3. wind speed
2897
     * 4. wind speed units K/M/N/S
2898
     * 6. A = Valid, V = invalid
2899
     * *hh          mandatory nmea_checksum
2900
     */
2901
0
    gps_mask_t mask = ONLINE_SET;
2902
2903
0
    if (('R' == field[2][0]) &&
2904
0
        ('N' == field[4][0]) &&
2905
0
        ('A' == field[5][0])) {
2906
        // relative, knots, and valid
2907
0
        session->newdata.wangler = safe_atof(field[1]);
2908
0
        session->newdata.wspeedr = safe_atof(field[3]) * KNOTS_TO_MPS;
2909
0
        mask |= NAVDATA_SET;
2910
0
    }
2911
2912
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2913
0
        "NMEA0183: xxMWV wangler %.2f wspeedr %.2f\n",
2914
0
        session->newdata.wangler,
2915
0
        session->newdata.wspeedr);
2916
0
    return mask;
2917
0
}
2918
2919
// PAIRxxx is Airoha, spunoff from Mediatek
2920
2921
// PAIR001 -- ACK/NAK
2922
static gps_mask_t processPAIR001(int c UNUSED, char *field[],
2923
                                 struct gps_device_t *session)
2924
0
{
2925
0
    int reason;
2926
0
    const char *reasons[] = {
2927
0
        "Success",
2928
0
        "In process, wait",
2929
0
        "Failed",
2930
0
        "Not supported",
2931
0
        "Busy, try again.",
2932
0
        "Unknown",             // gpsd only
2933
0
    };
2934
2935
    // ACK / NACK
2936
0
    reason = atoi(field[2]);
2937
0
    if (4 == reason) {
2938
        // ACK
2939
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
2940
0
                 "NMEA0183: PAIR001, ACK: %s\n", field[1]);
2941
0
        return ONLINE_SET;
2942
0
    }
2943
2944
    // else, NACK
2945
0
    if (0 > reason ||
2946
0
        4 < reason) {
2947
        // WTF?
2948
0
        reason = 5;
2949
0
    }
2950
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
2951
0
             "NMEA0183: PAIR NACK: %s, reason: %s\n",
2952
0
             field[1], reasons[reason]);
2953
2954
0
    return ONLINE_SET;
2955
0
}
2956
2957
// PAIR010 -- Request Aiding
2958
static gps_mask_t processPAIR010(int c UNUSED, char *field[],
2959
                                 struct gps_device_t *session)
2960
0
{
2961
0
    int type;
2962
0
    const char *types[] = {
2963
0
        "EPO data",
2964
0
        "Time",
2965
0
        "Location",
2966
0
        "Unknown",             // gpsd only
2967
0
    };
2968
2969
0
    int system;
2970
0
    const char *systems[] = {
2971
0
        "GPS",
2972
0
        "GLONASS",
2973
0
        "Galileo",
2974
0
        "BDS",
2975
0
        "QZSS",
2976
0
        "Unknown",             // gpsd only
2977
0
    };
2978
0
    int wn;         // week number
2979
0
    int tow;        // time of week
2980
2981
0
    type = atoi(field[1]);
2982
0
    if (0 > type ||
2983
0
        2 < type) {
2984
        // WTF?
2985
0
        type = 3;
2986
0
    }
2987
0
    system = atoi(field[2]);
2988
0
    if (0 > system ||
2989
0
        4 < system) {
2990
        // WTF?
2991
0
        system = 5;
2992
0
    }
2993
0
    wn = atoi(field[3]);
2994
0
    tow = atoi(field[4]);
2995
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
2996
0
             "NMEA0183: PAIR010: Need %s for %s.  WN %d TOW %d\n",
2997
0
             types[type], systems[system], wn, tow);
2998
2999
0
    return ONLINE_SET;
3000
0
}
3001
3002
// PDTINFO Unicore Product Information
3003
static gps_mask_t processPDTINFO(int c UNUSED, char *field[],
3004
                                 struct gps_device_t *session)
3005
0
{
3006
0
    (void)snprintf(session->subtype, sizeof(session->subtype),
3007
0
                   "%s, %s, %s",
3008
0
                   field[1], field[2], field[5]);
3009
    // save SW and HW Version as subtype1
3010
0
    (void)snprintf(session->subtype1, sizeof(session->subtype1),
3011
0
                   "SW %s,HW %s",
3012
0
                   field[4], field[3]);
3013
3014
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
3015
0
             "NMEA0183: PDTINFO: subtype %s subtype1 %s\n",
3016
0
             session->subtype, session->subtype1);
3017
3018
0
    return ONLINE_SET;
3019
0
}
3020
3021
/* Ashtech sentences take this format:
3022
 * $PASHDR,type[,val[,val]]*CS
3023
 * type is an alphabetic subsentence type
3024
 *
3025
 * Oxford Technical Solutions (OxTS) also uses the $PASHR sentence,
3026
 * but with a very different sentence contents:
3027
 * $PASHR,HHMMSS.SSS,HHH.HH,T,RRR.RR,PPP.PP,aaa.aa,r.rrr,p.ppp,h.hhh,Q1,Q2*CS
3028
 *
3029
 * so field 1 in ASHTECH is always alphabetic and numeric in OXTS
3030
 *
3031
 */
3032
static gps_mask_t processPASHR(int c UNUSED, char *field[],
3033
                               struct gps_device_t *session)
3034
0
{
3035
0
    gps_mask_t mask = ONLINE_SET;
3036
0
    char ts_buf[TIMESPEC_LEN];
3037
3038
0
    if (0 == strcmp("ACK", field[1])) {
3039
        // ACK
3040
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,ACK\n");
3041
0
        return ONLINE_SET;
3042
0
    } else if (0 == strcmp("MCA", field[1])) {
3043
        // MCA, raw data
3044
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,MCA\n");
3045
0
        return ONLINE_SET;
3046
0
    } else if (0 == strcmp("NAK", field[1])) {
3047
        // NAK
3048
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,NAK\n");
3049
0
        return ONLINE_SET;
3050
0
    } else if (0 == strcmp("PBN", field[1])) {
3051
        // PBN, position data
3052
        // FIXME: decode this for ECEF
3053
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,PBN\n");
3054
0
        return ONLINE_SET;
3055
0
    } else if (0 == strcmp("POS", field[1])) {  // 3D Position
3056
        /* $PASHR,POS,
3057
         *
3058
         * 2: position type:
3059
         *      0 = autonomous
3060
         *      1 = position differentially corrected with RTCM code
3061
         *      2 = position differentially corrected with CPD float solution
3062
         *      3 = position is CPD fixed solution
3063
         */
3064
0
        mask |= MODE_SET | STATUS_SET | CLEAR_IS;
3065
0
        if ('\0' == field[2][0]) {
3066
            // empty first field means no 3D fix is available
3067
0
            session->newdata.status = STATUS_UNK;
3068
0
            session->newdata.mode = MODE_NO_FIX;
3069
0
        } else {
3070
3071
            // if we make it this far, we at least have a 3D fix
3072
0
            session->newdata.mode = MODE_3D;
3073
0
            if (1 <= atoi(field[2]))
3074
0
                session->newdata.status = STATUS_DGPS;
3075
0
            else
3076
0
                session->newdata.status = STATUS_GPS;
3077
3078
0
            session->nmea.gga_sats_used = atoi(field[3]);
3079
0
            if (0 == merge_hhmmss(field[4], session)) {
3080
0
                register_fractional_time(field[0], field[4], session);
3081
0
                mask |= TIME_SET;
3082
0
            }
3083
0
            if (0 == do_lat_lon(&field[5], &session->newdata)) {
3084
0
                mask |= LATLON_SET;
3085
0
                if ('\0' != field[9][0]) {
3086
                    // altitude is already WGS 84
3087
0
                    session->newdata.altHAE = safe_atof(field[9]);
3088
0
                    mask |= ALTITUDE_SET;
3089
0
                }
3090
0
            }
3091
0
            session->newdata.track = safe_atof(field[11]);
3092
0
            session->newdata.speed = safe_atof(field[12]) / MPS_TO_KPH;
3093
0
            session->newdata.climb = safe_atof(field[13]);
3094
0
            if ('\0' != field[14][0]) {
3095
0
                session->gpsdata.dop.pdop = safe_atof(field[14]);
3096
0
                mask |= DOP_SET;
3097
0
            }
3098
0
            if ('\0' != field[15][0]) {
3099
0
                session->gpsdata.dop.hdop = safe_atof(field[15]);
3100
0
                mask |= DOP_SET;
3101
0
            }
3102
0
            if ('\0' != field[16][0]) {
3103
0
                session->gpsdata.dop.vdop = safe_atof(field[16]);
3104
0
                mask |= DOP_SET;
3105
0
            }
3106
0
            if ('\0' != field[17][0]) {
3107
0
                session->gpsdata.dop.tdop = safe_atof(field[17]);
3108
0
                mask |= DOP_SET;
3109
0
            }
3110
0
            mask |= (SPEED_SET | TRACK_SET | CLIMB_SET);
3111
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3112
0
                     "NMEA0183: PASHR,POS: hhmmss=%s lat=%.2f lon=%.2f"
3113
0
                     " altHAE=%.f"
3114
0
                     " speed=%.2f track=%.2f climb=%.2f mode=%d status=%d"
3115
0
                     " pdop=%.2f hdop=%.2f vdop=%.2f tdop=%.2f used=%d\n",
3116
0
                     field[4], session->newdata.latitude,
3117
0
                     session->newdata.longitude, session->newdata.altHAE,
3118
0
                     session->newdata.speed, session->newdata.track,
3119
0
                     session->newdata.climb, session->newdata.mode,
3120
0
                     session->newdata.status, session->gpsdata.dop.pdop,
3121
0
                     session->gpsdata.dop.hdop, session->gpsdata.dop.vdop,
3122
0
                     session->gpsdata.dop.tdop, session->nmea.gga_sats_used);
3123
0
        }
3124
0
    } else if (0 == strcmp("RID", field[1])) {  // Receiver ID
3125
0
        (void)snprintf(session->subtype, sizeof(session->subtype) - 1,
3126
0
                       "%s ver %s", field[2], field[3]);
3127
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3128
0
                 "NMEA0183: PASHR,RID: subtype=%s mask={}\n",
3129
0
                 session->subtype);
3130
0
        return mask;
3131
0
    } else if (0 == strcmp("SAT", field[1])) {  // Satellite Status
3132
0
        struct satellite_t *sp;
3133
0
        int i;
3134
0
        session->gpsdata.satellites_visible = atoi(field[2]);
3135
3136
0
        if (((NMEA_MAX_FLD - 15) / 5) < session->gpsdata.satellites_visible) {
3137
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
3138
0
                    "NMEA0183: PASHR,SAT: too many satellites %d\n",
3139
0
                     session->gpsdata.satellites_visible);
3140
0
            session->gpsdata.satellites_visible = (NMEA_MAX_FLD - 15) / 5;
3141
0
        }
3142
0
        session->gpsdata.satellites_used = 0;
3143
0
        sp = session->gpsdata.skyview;
3144
0
        for (i = 0; i < session->gpsdata.satellites_visible; i++) {
3145
0
            sp[i].PRN = (short)atoi(field[3 + i * 5 + 0]);
3146
0
            sp[i].azimuth = (double)atoi(field[3 + i * 5 + 1]);
3147
0
            sp[i].elevation = (double)atoi(field[3 + i * 5 + 2]);
3148
0
            sp[i].ss = safe_atof(field[3 + i * 5 + 3]);
3149
0
            sp[i].used = false;
3150
0
            if ('U' == field[3 + i * 5 + 4][0]) {
3151
0
                sp[i].used = true;
3152
0
                session->gpsdata.satellites_used++;
3153
0
            }
3154
0
        }
3155
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3156
0
                 "NMEA0183: PASHR,SAT: used=%d\n",
3157
0
                 session->gpsdata.satellites_used);
3158
0
        session->gpsdata.skyview_time.tv_sec = 0;
3159
0
        session->gpsdata.skyview_time.tv_nsec = 0;
3160
0
        mask |= SATELLITE_SET | USED_IS;
3161
3162
0
    } else if (0 == strcmp("T", field[3])) {   // Assume OxTS PASHR
3163
        // FIXME: decode OxTS $PASHDR, time is wrong, breaks cycle order
3164
0
        if (0 == merge_hhmmss(field[1], session)) {
3165
            // register_fractional_time(field[0], field[1], session);
3166
            // mask |= TIME_SET; confuses cycle order
3167
0
        }
3168
        // Assume true heading
3169
0
        session->gpsdata.attitude.heading = safe_atof(field[2]);
3170
0
        session->gpsdata.attitude.roll = safe_atof(field[4]);
3171
0
        session->gpsdata.attitude.pitch = safe_atof(field[5]);
3172
        // mask |= ATTITUDE_SET;  * confuses cycle order ??
3173
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3174
0
                 "NMEA0183: PASHR (OxTS) time %s, heading %lf.\n",
3175
0
                  timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
3176
0
                  session->gpsdata.attitude.heading);
3177
0
    }
3178
0
    return mask;
3179
0
}
3180
3181
/* Ericsson $PERC,FWsts - Firmware status
3182
 * Firmware version and status information for timing modules
3183
 *
3184
 * $PERC,FWsts,<grp1>,<grp2>,<grp3>,<state>,<substatus>,<type>*XX
3185
 *
3186
 * Field 1: grp1 - Status group 1 (6 digits, format XXYYZZ)
3187
 * Field 2: grp2 - Status group 2 (6 digits, format XXYYZZ)
3188
 * Field 3: grp3 - Status group 3 (6 digits, format XXYYZZ)
3189
 * Field 4: state - Firmware state (0-3)
3190
 * Field 5: substatus - Sub-status code (0=normal, 1=0xD, 2=0xE)
3191
 * Field 6: type - Type indicator
3192
 *
3193
 * Periodic sentence (~19s interval) providing firmware status details.
3194
 * Status groups contain device-specific diagnostic information.
3195
 */
3196
static gps_mask_t processPERCFWsts(int c UNUSED, char *field[],
3197
                                   struct gps_device_t *session)
3198
0
{
3199
0
    gps_mask_t mask = ONLINE_SET;
3200
0
    int grp1, grp2, grp3, state, substatus, type;
3201
3202
0
    static const struct vlist_t vfwsts_type[] = {
3203
0
        {0, "Normal"},
3204
0
        {1, "Type 1"},
3205
0
        {2, "Type 2"},
3206
0
        {3, "Type 3"},
3207
0
        {0, NULL},
3208
0
    };
3209
3210
    // field[0]="PERC", field[1]="FWsts", data starts at field[2]
3211
0
    grp1 = atoi(field[2]);
3212
0
    grp2 = atoi(field[3]);
3213
0
    grp3 = atoi(field[4]);
3214
0
    state = atoi(field[5]);
3215
0
    substatus = atoi(field[6]);
3216
0
    type = atoi(field[7]);
3217
3218
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3219
0
             "NMEA0183: PERC,FWsts: groups=%06d,%06d,%06d state=%d "
3220
0
             "substatus=%d type=%s(%d)\n",
3221
0
             grp1, grp2, grp3, state, substatus,
3222
0
             val2str(type, vfwsts_type), type);
3223
3224
0
    return mask;
3225
0
}
3226
3227
/* Ericsson $PERC,GPavp - Averaged position
3228
 * Position-hold reference coordinates for timing modules
3229
 *
3230
 * $PERC,GPavp,<lat>,<lat_ns>,<lon>,<lon_ew>,<alt>,<alt_unit>*XX
3231
 *
3232
 * Field 1: lat - Latitude in ddmm.mmmm format
3233
 * Field 2: lat_ns - Latitude hemisphere (N/S)
3234
 * Field 3: lon - Longitude in dddmm.mmmm format
3235
 * Field 4: lon_ew - Longitude hemisphere (E/W)
3236
 * Field 5: alt - Altitude in meters
3237
 * Field 6: alt_unit - Altitude units (M=meters)
3238
 *
3239
 * This sentence appears only in mode 2 (position-hold) and provides the
3240
 * surveyed average position that the timing module uses as its reference.
3241
 * Format matches standard NMEA position encoding.
3242
 */
3243
static gps_mask_t processPERCGPavp(int c UNUSED, char *field[],
3244
                                   struct gps_device_t *session)
3245
0
{
3246
0
    gps_mask_t mask = ONLINE_SET;
3247
0
    double lat, lon, alt;
3248
3249
    // field[0]="PERC", field[1]="GPavp", data starts at field[2]
3250
    // Format: lat, lat_ns, lon, lon_ew, alt, alt_unit
3251
0
    lat = decode_lat_or_lon(field[2]);
3252
0
    if ('S' == field[3][0])
3253
0
        lat = -lat;
3254
3255
0
    lon = decode_lat_or_lon(field[4]);
3256
0
    if ('W' == field[5][0])
3257
0
        lon = -lon;
3258
3259
0
    alt = safe_atof(field[6]);
3260
3261
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3262
0
             "NMEA0183: PERC,GPavp: lat=%.6f lon=%.6f alt=%.1fm\n",
3263
0
             lat, lon, alt);
3264
3265
    // Save surveyed/averaged position (position-hold reference)
3266
0
    session->newdata.latitude = lat;
3267
0
    session->newdata.longitude = lon;
3268
0
    session->newdata.altHAE = alt;
3269
0
    mask |= LATLON_SET | ALTITUDE_SET;
3270
3271
0
    return mask;
3272
0
}
3273
3274
/* Ericsson $PERC,GPctr - Control/heartbeat
3275
 * Periodic heartbeat and configuration status message for timing modules
3276
 *
3277
 * $PERC,GPctr,<direction>,<data>,<f3>,<f4>,<config_byte>,<f6>,<f7>*XX
3278
 *
3279
 * Field 1: direction - 'R'=Read response, 'V'=Value notification
3280
 * Field 2: data - Configuration data (21 bytes per sentence)
3281
 * Field 3-5: Additional config fields
3282
 * Field 6: config_byte - Configuration byte
3283
 * Field 7: Reserved
3284
 *
3285
 * Periodic sentence (~15s interval) serving as keepalive/heartbeat.
3286
 * Provides configuration and status information.
3287
 */
3288
static gps_mask_t processPERCGPctr(int c UNUSED, char *field[],
3289
                                   struct gps_device_t *session)
3290
0
{
3291
0
    gps_mask_t mask = ONLINE_SET;
3292
0
    char direction;
3293
0
    int config_byte = 0;
3294
3295
    // field[0]="PERC", field[1]="GPctr", data starts at field[2]
3296
0
    direction = field[2][0];
3297
0
    if (field[6][0] != '\0') {
3298
0
        config_byte = atoi(field[6]);
3299
0
    }
3300
3301
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3302
0
             "NMEA0183: PERC,GPctr: direction=%c data=%s config=%d\n",
3303
0
             direction, field[3], config_byte);
3304
3305
0
    return mask;
3306
0
}
3307
3308
/* Ericsson $PERC,GPdbg - Debug output
3309
 * Satellite tracking debug and status counter for Ericsson timing modules
3310
 *
3311
 * Two variants based on type field:
3312
 *
3313
 * Type 1: Satellite tracking debug (4 messages per second when enabled)
3314
 * $PERC,GPdbg,1,<page_count>,<page_num>,<sv_data>...*XX
3315
 *
3316
 * Pages 1-3: Up to 7 satellites per page in PPPSSXX format:
3317
 *   PPP = PRN (3 digits)
3318
 *   SS = SNR (2 digits)
3319
 *   XX = Status (2 hex digits: 00=solid, 01=weak, 05=acquiring)
3320
 *
3321
 * Page 10: Timing/phase measurements with bitmask
3322
 *   field[5] = Satellite bitmask (hex, e.g., 07FF = 11 sats)
3323
 *   fields[6-19] = Timing/phase/frequency measurements
3324
 *
3325
 * Type 2: Status counter debug
3326
 * $PERC,GPdbg,2,<counter>*XX
3327
 *
3328
 * Enabled via command: $PERC,GPdbg,1*43
3329
 * Provides real-time satellite tracking status beyond standard GPGSV.
3330
 */
3331
static gps_mask_t processPERCGPdbg(int c UNUSED, char *field[],
3332
                                   struct gps_device_t *session)
3333
0
{
3334
0
    gps_mask_t mask = ONLINE_SET;
3335
0
    int type, page_count, page_num, i;
3336
3337
    // field[0]="PERC", field[1]="GPdbg", data starts at field[2]
3338
0
    type = atoi(field[2]);
3339
3340
0
    if (type == 1) {
3341
        // Satellite tracking debug
3342
0
        page_count = atoi(field[3]);
3343
0
        page_num = atoi(field[4]);
3344
3345
0
        if (page_num == 10) {
3346
            // Page 10: Special format with timing/phase data
3347
            // field[5]=bitmask (hex), field[6+]=timing fields
3348
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3349
0
                     "NMEA0183: PERC,GPdbg: type=1 page=%d/%d bitmask=%s "
3350
0
                     "timing=[%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s]\n",
3351
0
                     page_num, page_count, field[5],
3352
0
                     field[6], field[7], field[8], field[9], field[10],
3353
0
                     field[11], field[12], field[13], field[14], field[15],
3354
0
                     field[16], field[17], field[18], field[19]);
3355
0
        } else {
3356
            // Pages 1-3: Satellite tracking data
3357
            // Format: PRN(3)+SNR(2)+Status(2) = 7 digits per satellite
3358
            // Count non-empty satellite fields
3359
0
            int sv_count = 0;
3360
0
            for (i = 5; i < NMEA_MAX_FLD && field[i][0] != '\0'; i++) {
3361
0
                sv_count++;
3362
0
            }
3363
3364
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3365
0
                     "NMEA0183: PERC,GPdbg: type=1 page=%d/%d sv_count=%d\n",
3366
0
                     page_num, page_count, sv_count);
3367
3368
            // Log individual satellites (up to 7 per page)
3369
            // FIXME: save them in skyview[]
3370
0
            for (i = 5; i < 5 + 7 && field[i][0] != '\0'; i++) {
3371
0
                int prn = 0, snr = 0, status = 0;
3372
0
                if (7 <= strnlen(field[i], 8)) {
3373
                    // Parse PPPSSXX format
3374
0
                    char prn_str[4], snr_str[3], status_str[3];
3375
3376
0
                    strlcpy(prn_str, field[i], sizeof(prn_str));
3377
0
                    strlcpy(snr_str, field[i] + 3, sizeof(snr_str));
3378
0
                    strlcpy(status_str, field[i] + 5, sizeof(status_str));
3379
3380
0
                    prn = atoi(prn_str);
3381
0
                    snr = atoi(snr_str);
3382
0
                    status = (int)strtol(status_str, NULL, 16);
3383
3384
0
                    GPSD_LOG(LOG_DATA, &session->context->errout,
3385
0
                             "NMEA0183: PERC,GPdbg:   sv%d: PRN=%d SNR=%d "
3386
0
                             "status=0x%02x\n",
3387
0
                             i - 4, prn, snr, status);
3388
0
                }
3389
0
            }
3390
0
        }
3391
0
    } else if (type == 2) {
3392
        // Status counter debug
3393
0
        int counter = atoi(field[3]);
3394
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3395
0
                 "NMEA0183: PERC,GPdbg: type=2 counter=%d\n",
3396
0
                 counter);
3397
0
    } else {
3398
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
3399
0
                 "NMEA0183: PERC,GPdbg: unknown type=%d\n",
3400
0
                 type);
3401
0
    }
3402
3403
    // Debug data is logged but not stored in gps_data_t API structures.
3404
    // Per-satellite tracking status and page 10 timing measurements
3405
3406
    // FIXME: store into satellite_t and skyview
3407
3408
0
    return mask;
3409
0
}
3410
3411
/* Ericsson $PERC,GPppf - Position phase/frequency error
3412
 * Oscillator discipline quality for timing modules
3413
 *
3414
 * $PERC,GPppf,<phase_error>,<freq_error>,<status>,<leap_sec>,<quality>*XX
3415
 *
3416
 * Field 1: phase_error - Phase error in nanoseconds (signed)
3417
 * Field 2: freq_error - Frequency error in ppb (parts per billion, signed)
3418
 * Field 3: status - Status indicator (1=holdover/acquiring, 0=GPS-disciplined)
3419
 * Field 4: leap_sec - UTC-GPS leap second offset (e.g., 18 in 2026)
3420
 * Field 5: quality - PPS quality indicator (0=locked, 1=good, 2=degraded)
3421
 *
3422
 * This sentence provides real-time oscillator discipline quality metrics.
3423
 * Phase error indicates PPS timing offset, frequency error shows oscillator
3424
 * stability relative to GPS reference. Leap seconds typically appear ~2.5
3425
 * minutes after first fix.
3426
 */
3427
static gps_mask_t processPERCGPppf(int c UNUSED, char *field[],
3428
                                   struct gps_device_t *session)
3429
0
{
3430
0
    gps_mask_t mask = ONLINE_SET;
3431
0
    double phase_error, freq_error;
3432
0
    int status, leap_sec, quality;
3433
3434
    // field[0]="PERC", field[1]="GPppf", data starts at field[2]
3435
0
    phase_error = safe_atof(field[2]);
3436
0
    freq_error = safe_atof(field[3]);
3437
0
    status = atoi(field[4]);
3438
0
    leap_sec = atoi(field[5]);
3439
0
    quality = atoi(field[6]);
3440
3441
    // Note: Always populate oscillator data - distros configure builds
3442
    // inconsistently. The oscillator field is part of the standard API.
3443
0
    session->gpsdata.osc.running = true;
3444
0
    session->gpsdata.osc.reference = true;
3445
0
    session->gpsdata.osc.disciplined = (status == 0);
3446
0
    session->gpsdata.osc.delta = (int)phase_error;
3447
0
    mask |= OSCILLATOR_SET;
3448
3449
    // Store leap seconds (can be negative in the future!)
3450
0
    session->gpsdata.leap_seconds = leap_sec;
3451
3452
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3453
0
             "NMEA0183: PERC,GPppf: phase=%.1fns freq=%.1fppb "
3454
0
             "disciplined=%d leap=%d quality=%d\n",
3455
0
             phase_error, freq_error, (status == 0), leap_sec, quality);
3456
3457
0
    return mask;
3458
0
}
3459
3460
/* Ericsson $PERC,GPppr - Position pulse reference
3461
 * GPS time reference and PPS quality for timing modules
3462
 *
3463
 * $PERC,GPppr,<tow_sec>,<gps_week>,<param>,<sv_count>,<pps_flag>,<reserved>*XX
3464
 *
3465
 * Field 1: tow_sec - GPS Time of Week in seconds (0-604799)
3466
 * Field 2: gps_week - GPS week number
3467
 * Field 3: param - Constant parameter (always 00050, likely cable delay in ns)
3468
 * Field 4: sv_count - Number of satellites used in solution
3469
 * Field 5: pps_flag - PPS quality indicator (0=OK/locked, 3=not locked)
3470
 * Field 6: reserved - Reserved field (always 0)
3471
 *
3472
 * Critical timing sentence providing GPS time reference and PPS lock status.
3473
 * The pps_flag field indicates whether PPS output is reliable.
3474
 */
3475
static gps_mask_t processPERCGPppr(int c UNUSED, char *field[],
3476
                                   struct gps_device_t *session)
3477
0
{
3478
0
    gps_mask_t mask = ONLINE_SET;
3479
0
    unsigned int tow_sec, gps_week, param, sv_count, pps_flag, reserved;
3480
0
    timespec_t ts_tow;
3481
3482
0
    static const struct vlist_t vpercgpppr_pps[] = {
3483
0
        {0, "locked"},
3484
0
        {3, "unlocked"},
3485
0
        {0, NULL},
3486
0
    };
3487
3488
    // field[0]="PERC", field[1]="GPppr", data starts at field[2]
3489
0
    tow_sec = atoi(field[2]);
3490
0
    gps_week = atoi(field[3]);
3491
0
    param = atoi(field[4]);
3492
0
    sv_count = atoi(field[5]);
3493
0
    pps_flag = atoi(field[6]);
3494
    // field[7] is reserved (always 0)
3495
0
    reserved = atoi(field[7]);
3496
3497
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3498
0
             "NMEA0183: PERC,GPppr: week=%u tow=%u param=%u sats=%u "
3499
0
             "pps_flag=%s reserved=%d(%u)\n",
3500
0
             gps_week, tow_sec, param, sv_count,
3501
0
             val2str(pps_flag, vpercgpppr_pps), pps_flag, reserved);
3502
3503
    // Convert GPS week + TOW to Unix timestamp
3504
0
    ts_tow.tv_sec = tow_sec;
3505
0
    ts_tow.tv_nsec = 0;
3506
0
    session->newdata.time = gpsd_gpstime_resolv(session, gps_week, ts_tow);
3507
0
    mask |= TIME_SET;
3508
3509
    // Save satellite count
3510
0
    session->gpsdata.satellites_used = (int)sv_count;
3511
0
    mask |= SATELLITE_SET;
3512
3513
    /* Cable delay parameter (constant 50ns on GRU 04??)
3514
     * Logged for reference - no suitable API field for cable delay */
3515
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3516
0
             "NMEA0183: Cable delay compensation: %u\n", param);
3517
3518
    // Save PPS lock status to oscillator structure
3519
    // pps_flag: 0 = locked/OK, 3 = not locked
3520
0
    if (0 == pps_flag) {
3521
0
        session->gpsdata.osc.reference = true;
3522
0
    } else {
3523
0
        session->gpsdata.osc.reference = false;
3524
0
    }
3525
0
    mask |= OSCILLATOR_SET;
3526
3527
0
    return mask;
3528
0
}
3529
3530
/* Ericsson $PERC,GPreh - Receiver health
3531
 * Health status for timing modules
3532
 *
3533
 * $PERC,GPreh,<timestamp>,<health_code>*XX
3534
 *
3535
 * Field 1: timestamp - Time/date string
3536
                        (format varies, often null "00:00:00 00/00/0000")
3537
 * Field 2: health_code - Health status code (numeric)
3538
 *
3539
 * Periodic sentence (~19s interval) providing receiver health status.
3540
 * Health code interpretation is device-specific.
3541
 */
3542
static gps_mask_t processPERCGPreh(int c UNUSED, char *field[],
3543
                                   struct gps_device_t *session)
3544
0
{
3545
0
    gps_mask_t mask = ONLINE_SET;
3546
0
    int health_code;
3547
3548
    // field[0]="PERC", field[1]="GPreh", data starts at field[2]
3549
0
    health_code = atoi(field[3]);
3550
3551
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3552
0
             "NMEA0183: PERC,GPreh: timestamp=%s health=%d\n",
3553
0
             field[2], health_code);
3554
3555
0
    return mask;
3556
0
}
3557
3558
/* Ericsson $PERC,GPsts - Receiver status
3559
 * Timing module operating mode and constellation information
3560
 *
3561
 * $PERC,GPsts,<mode>,<survey_flag>,<constellation>,<capabilities>*XX
3562
 *
3563
 * Field 1: mode - Operating mode (0=acquiring, 1=survey, 2=position-hold)
3564
 * Field 2: survey_flag - Survey status (0=position valid, 1=no position)
3565
 * Field 3: constellation - Constellation configuration (2=GPS+GLONASS)
3566
 * Field 4: capabilities - 18-digit capability bitmask "010011111111011111"
3567
 *
3568
 * Primary status sentence providing operating mode and system capabilities.
3569
 */
3570
static gps_mask_t processPERCGPsts(int c UNUSED, char *field[],
3571
                                   struct gps_device_t *session)
3572
0
{
3573
0
    int mode, survey_flag, constellation;
3574
0
    gps_mask_t mask = ONLINE_SET;
3575
3576
0
    static const struct vlist_t vpercgpsts_mode[] = {
3577
0
        {0, "Acquiring"},
3578
0
        {1, "Survey"},
3579
0
        {2, "Position-hold"},
3580
0
        {3, "Overdetermined"},
3581
0
        {4, "Manual"},
3582
0
        {5, "3D-hold"},
3583
0
        {0, NULL},
3584
0
    };
3585
3586
    // field[0]="PERC", field[1]="GPsts", data starts at field[2]
3587
0
    mode = atoi(field[2]);
3588
0
    survey_flag = atoi(field[3]);
3589
0
    constellation = atoi(field[4]);
3590
3591
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3592
0
             "NMEA0183: PERC,GPsts: mode=%s(%d) survey_flag=%d "
3593
0
             "constellation=%d capabilities=%s\n",
3594
0
             val2str(mode, vpercgpsts_mode), mode, survey_flag,
3595
0
             constellation, field[5]);
3596
3597
    /* Map receiver operating mode to fix status
3598
     * Survey and Position-hold modes are timing receiver states */
3599
0
    if (1 == mode ||
3600
0
        2 == mode ||
3601
0
        4 == mode) {
3602
        // Survey, Position-hold, or Manual → timing mode
3603
0
        session->newdata.status = STATUS_TIME;
3604
0
        mask |= STATUS_SET;
3605
0
    } else if (0 == mode) {
3606
        // Acquiring → GPS fix mode
3607
0
        session->newdata.status = STATUS_GPS;
3608
0
        mask |= STATUS_SET;
3609
0
    }
3610
    // Modes 3 (Overdetermined) and 5 (3D-hold) not mapped
3611
3612
0
    return mask;
3613
0
}
3614
3615
/* Ericsson $PERC,GPver - Receiver identification
3616
 * Returns hardware model and serial number
3617
 *
3618
 * $PERC,GPver,<model>,<part_num>,<hw_rev>,<serial>*XX
3619
 *
3620
 * Field 1: model - "GRU 04 01" or "GRU 04 02"
3621
 * Field 2: part_num - "NCD 901 65/1" or "NCD 901 78/1"
3622
 * Field 3: hw_rev - Hardware revision (e.g., "R1E")
3623
 * Field 4: serial - Serial number
3624
 */
3625
static gps_mask_t processPERCGPver(int c UNUSED, char *field[],
3626
                                   struct gps_device_t *session)
3627
0
{
3628
0
    gps_mask_t mask = ONLINE_SET;
3629
0
    char new_serial[32];
3630
0
    char old_subtype[64];
3631
3632
    // field[0]="PERC", field[1]="GPver", data starts at field[2]
3633
    // Save old subtype for comparison
3634
0
    strlcpy(old_subtype, session->subtype, sizeof(old_subtype));
3635
0
    strlcpy(new_serial, field[5], sizeof(new_serial));
3636
3637
    // Build subtype directly into session->subtype
3638
0
    (void)snprintf(session->subtype, sizeof(session->subtype),
3639
0
                   "%s %s %s",
3640
0
                   field[2], field[3], field[4]);
3641
3642
    // Only log at high level if changed
3643
0
    if (0 != strcmp(old_subtype, session->subtype) ||
3644
0
        0 != strcmp(session->gpsdata.dev.sernum, new_serial)) {
3645
0
        GPSD_LOG(LOG_INF, &session->context->errout,
3646
0
                 "NMEA0183: PERC,GPver: new device %s serial %s\n",
3647
0
                 session->subtype, new_serial);
3648
0
        mask |= DEVICEID_SET;
3649
0
    } else {
3650
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3651
0
                 "NMEA0183: PERC,GPver: (unchanged)\n");
3652
0
    }
3653
3654
    // Update serial number
3655
0
    strlcpy(session->gpsdata.dev.sernum, new_serial,
3656
0
            sizeof(session->gpsdata.dev.sernum));
3657
3658
0
    return mask;
3659
0
}
3660
3661
/* Trimble $PTNLRNM - Receiver Navigation Mode
3662
 * Navigation mode status for Trimble/Ericsson receivers
3663
 *
3664
 * $PTNLRNM,<mode>*XX
3665
 *
3666
 * Field 1: mode - Navigation mode status character
3667
 *          A = Autonomous/Active
3668
 *          D = Differential
3669
 *          E = Estimated/Dead Reckoning
3670
 *          N = Data not valid
3671
 *
3672
 * Periodic sentence providing receiver navigation mode status.
3673
 * Typically outputs 'A' for autonomous operation.
3674
 */
3675
static gps_mask_t processPTNLRNM(int c UNUSED, char *field[],
3676
                                 struct gps_device_t *session)
3677
0
{
3678
0
    gps_mask_t mask = ONLINE_SET;
3679
0
    char mode;
3680
3681
0
    static const struct vlist_t vptnlrnm_mode[] = {
3682
0
        {'A', "Autonomous"},
3683
0
        {'D', "Differential"},
3684
0
        {'E', "Estimated"},
3685
0
        {'N', "Invalid"},
3686
0
        {0, NULL},
3687
0
    };
3688
3689
    // field[0]="PTNLRNM", data starts at field[1]
3690
0
    mode = field[1][0];
3691
3692
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3693
0
             "NMEA0183: PTNLRNM: navigation mode=%c (%s)\n",
3694
0
             mode, val2str(mode, vptnlrnm_mode));
3695
3696
    // Map mode to GPS status
3697
0
    switch (mode) {
3698
0
    case 'A':
3699
0
        session->newdata.status = STATUS_GPS;
3700
0
        mask |= STATUS_SET;
3701
0
        break;
3702
0
    case 'D':
3703
0
        session->newdata.status = STATUS_DGPS;
3704
0
        mask |= STATUS_SET;
3705
0
        break;
3706
0
    case 'E':
3707
0
        session->newdata.status = STATUS_DR;
3708
0
        mask |= STATUS_SET;
3709
0
        break;
3710
0
    default:
3711
        // N or unknown - no status set
3712
0
        break;
3713
0
    }
3714
3715
0
    return mask;
3716
0
}
3717
3718
/* Trimble $PTNLRBA - Antenna status
3719
 * Antenna connection health monitoring for Trimble/Ericsson receivers
3720
 *
3721
 * $PTNLRBA,<status>,<flag>*XX
3722
 *
3723
 * Field 1: status - Antenna status (1=OK, 0=fault)
3724
 * Field 2: flag - Additional status flag
3725
 *
3726
 * Monitors antenna connection health. Important for timing applications
3727
 * as antenna problems directly affect signal quality and position accuracy.
3728
 */
3729
static gps_mask_t processPTNLRBA(int c UNUSED, char *field[],
3730
                                 struct gps_device_t *session)
3731
0
{
3732
0
    gps_mask_t mask = ONLINE_SET;
3733
0
    int status, flag;
3734
3735
0
    static const struct vlist_t vptnlrba_status[] = {
3736
0
        {0, "Fault"},
3737
0
        {1, "OK"},
3738
0
        {0, NULL},
3739
0
    };
3740
3741
    // field[0]="PTNLRBA", data starts at field[1]
3742
0
    status = atoi(field[1]);
3743
0
    flag = atoi(field[2]);
3744
3745
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3746
0
             "NMEA0183: PTNLRBA: antenna_status=%s(%d) flag=%d\n",
3747
0
             val2str(status, vptnlrba_status), status, flag);
3748
3749
    // Save antenna status to fix structure
3750
0
    if (1 == status) {
3751
0
        session->newdata.ant_stat = ANT_OK;
3752
0
    } else {
3753
        // Status 0 = Fault (type unknown, map to generic fault)
3754
0
        session->newdata.ant_stat = ANT_SHORT;
3755
0
        mask |= ERROR_SET;
3756
0
    }
3757
3758
0
    return mask;
3759
0
}
3760
3761
/* Trimble $PTNLRTP - Receiver temperature
3762
 * Internal temperature monitoring for Trimble/Ericsson receivers
3763
 *
3764
 * $PTNLRTP,T,<temp>,<precision>*XX
3765
 *
3766
 * Field 1: type - Always 'T' for temperature
3767
 * Field 2: temp - Temperature in degrees Celsius
3768
 * Field 3: precision - Temperature measurement precision/accuracy
3769
 *
3770
 * Monitors receiver internal temperature. Useful for thermal stability
3771
 * analysis in timing applications. Observed range: 30-40°C typical.
3772
 */
3773
static gps_mask_t processPTNLRTP(int c UNUSED, char *field[],
3774
                                 struct gps_device_t *session)
3775
0
{
3776
0
    gps_mask_t mask = ONLINE_SET;
3777
0
    double temp, precision;
3778
0
    char type;
3779
3780
    // field[0]="PTNLRTP", data starts at field[1]
3781
0
    type = field[1][0];
3782
0
    temp = safe_atof(field[2]);
3783
0
    precision = safe_atof(field[3]);
3784
3785
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3786
0
             "NMEA0183: PTNLRTP: type=%c temp=%.2f°C precision=%.1f\n",
3787
0
             type, temp, precision);
3788
3789
    // Store temperature in gps_fix_t.temp field.  What temperature is it?
3790
0
    session->newdata.temp = temp;
3791
    // No specific mask for temperature - aux data included in fix
3792
3793
0
    return mask;
3794
0
}
3795
3796
/* Trimble $PTNLRXO - Crystal oscillator status
3797
 * Oscillator lock and frequency offset for Trimble/Ericsson receivers
3798
 *
3799
 * $PTNLRXO,<status>,<offset>*XX
3800
 *
3801
 * Field 1: status - Oscillator lock status (1=locked, 0=unlocked)
3802
 * Field 2: offset - Frequency offset in ppb (parts per billion)
3803
 *
3804
 * Reports crystal oscillator disciplining status and frequency error.
3805
 * Complementary to $PERC,GPppf which provides phase error in nanoseconds.
3806
 * Typical offset: -450 to -500 ppb for this hardware.
3807
 */
3808
static gps_mask_t processPTNLRXO(int c UNUSED, char *field[],
3809
                                 struct gps_device_t *session)
3810
0
{
3811
0
    gps_mask_t mask = ONLINE_SET;
3812
0
    int status;
3813
0
    double offset;
3814
3815
0
    static const struct vlist_t vptnlrxo_status[] = {
3816
0
        {0, "Unlocked"},
3817
0
        {1, "Locked"},
3818
0
        {0, NULL},
3819
0
    };
3820
3821
    // field[0]="PTNLRXO", data starts at field[1]
3822
0
    status = atoi(field[1]);
3823
0
    offset = safe_atof(field[2]);
3824
3825
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3826
0
             "NMEA0183: PTNLRXO: osc_status=%s(%d) offset=%.3f ppb\n",
3827
0
             val2str(status, vptnlrxo_status), status, offset);
3828
3829
    // Frequency offset could be stored alongside GPppf data if an
3830
    // extended oscillator_t structure is added to gps_data_t.
3831
    // Note: API extension needed for frequency offset storage.
3832
3833
0
    return mask;
3834
0
}
3835
3836
/* Android GNSS super message
3837
 * A stub.
3838
 */
3839
static gps_mask_t processPGLOR(int c UNUSED, char *field[],
3840
                               struct gps_device_t *session)
3841
0
{
3842
    /*
3843
     * $PGLOR,0,FIX,....
3844
     * 1    = sentence version (may not be present)
3845
     * 2    = message subtype
3846
     * ....
3847
     *
3848
     * subtypes:
3849
     *  $PGLOR,[],AGC - ??
3850
     *  $PGLOR,[],CPU - CPU Loading
3851
     *  $PGLOR,[],FIN - Request completion status
3852
     *  $PGLOR,0,FIX,seconds - Time To Fix
3853
     *  $PGLOR,[],FTS - Factory Test Status
3854
     *  $PGLOR,[],GFC - GeoFence Fix
3855
     *  $PGLOR,[],GLO - ??
3856
     *  $PGLOR,[],HLA - Value of HULA sensors
3857
     *  $PGLOR,[],IMS - IMES messages
3858
     *  $PGLOR,1,LSQ,hhmmss.ss  - Least squares GNSS fix
3859
     *  $PGLOR,NET    - Report network information
3860
     *  $PGLOR,[],NEW - Indicate new GPS request
3861
     *  $PGLOR,[],PFM - Platform Status
3862
     *  $PGLOR,[],PPS - Indicate PPS time corrections
3863
     *  $PGLOR,5,PWR i - Power consumption report
3864
     *                  Only have doc for 5, Quectel uses 4
3865
     *  $PGLOR,[],RID - Version Information
3866
     *  $PGLOR,2,SAT - GPS Satellite information
3867
     *  $PGLOR,[],SIO - Serial I/O status report
3868
     *  $PGLOR,[],SPA - Spectrum analyzer results
3869
     *  $PGLOR,0,SPD  - Speed, Steps, etc.
3870
     *  $PGLOR,SPL    - ??
3871
     *  $PGLOR,[],SPS - ??
3872
     *  $PGLOR,10,STA - GLL status
3873
     *  $PGLOR,[],SVC - ??
3874
     *  $PGLOR,[],SVD - SV Dopplers detected in the false alarm test.
3875
     *  $PGLOR,[],SMx - Report GPS Summary Information
3876
     *  $PGLOR,[],UNC - ??
3877
     *
3878
     * Are NET and SPL really so different?
3879
     *
3880
     */
3881
0
    gps_mask_t mask = ONLINE_SET;
3882
0
    int got_one = 0;
3883
3884
0
    switch (field[1][0]) {
3885
0
    case '0':
3886
0
        if (0 == strncmp("FIX", field[2], 3)) {
3887
0
            got_one = 1;
3888
            // field 3, time to first fix in seconds
3889
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3890
0
                     "NMEA0183: PGLOR: FIX, TTFF %s\n",
3891
0
                     field[3]);
3892
0
        } else if (0 == strncmp("SPD", field[2], 3)) {
3893
0
            got_one = 1;
3894
            // field 4, ddmmy.ss UTC
3895
            // field 5, hhmmss.ss UTC
3896
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3897
0
                     "NMEA0183: PGLOR: SPD, %s %s UTC\n",
3898
0
                     field[4], field[5]);
3899
0
        }
3900
0
        break;
3901
0
    case '1':
3902
0
        if (0 == strncmp("LSQ", field[2], 3)) {
3903
0
            got_one = 1;
3904
            // field 3, hhmmss.ss UTC, only field Quectel supplies
3905
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3906
0
                     "NMEA0183: PGLOR: LSQ %s UTC\n",
3907
0
                     field[3]);
3908
0
        } else if ('0' == field[1][1] &&
3909
0
                   0 == strncmp("STA", field[2], 3)) {
3910
            // version 10
3911
0
            got_one = 1;
3912
            // field 3, hhmmss.ss UTC
3913
            // field 7, Position uncertainty meters
3914
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3915
0
                     "NMEA0183: PGLOR: STA, UTC %s PosUncer  %s\n",
3916
0
                     field[3], field[7]);
3917
0
        }
3918
0
        break;
3919
0
    }
3920
0
    if (0 != got_one) {
3921
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3922
0
                 "NMEA0183: PGLOR: seq %s type %s\n",
3923
0
                 field[1], field[2]);
3924
0
    }
3925
0
    return mask;
3926
0
}
3927
3928
// Inertial Sense GPS nav data, not a legal message name
3929
static gps_mask_t processPGPSP(int count UNUSED, char *field[],
3930
                               struct gps_device_t *session)
3931
0
{
3932
    /*
3933
     * $PGPSP,523970800,2351,778,44.06887670,-121.31410390,1114.07,1134.17,
3934
     * 2.55,4.32,11.26,0.13,0.52,0.25,0.10,25.7,0.0000,18*51
3935
     *
3936
     */
3937
0
    gps_mask_t mask = ONLINE_SET;
3938
0
    unsigned long i_tow = strtoul(field[1], NULL, 10);   // ms
3939
0
    int weeks = atoi(field[2]);
3940
0
    unsigned long status = strtoul(field[3], NULL, 10);
3941
0
    int used = status & 0x0ff;
3942
0
    int gpsStatus = (status >> 8) & 0x0ff;
3943
0
    int fixType = (status >> 16) & 0x0ff;
3944
0
    double lat = safe_atof(field[4]);
3945
0
    double lon = safe_atof(field[5]);
3946
0
    double altHAE = safe_atof(field[6]);
3947
0
    double altMSL = safe_atof(field[7]);
3948
0
    double pDOP = safe_atof(field[8]);
3949
0
    double hAcc = safe_atof(field[9]);
3950
0
    double vAcc = safe_atof(field[10]);
3951
0
    double vecefX = safe_atof(field[11]);
3952
0
    double vecefY = safe_atof(field[12]);
3953
0
    double vecefZ = safe_atof(field[13]);
3954
0
    double sAcc = safe_atof(field[14]);
3955
0
    double cnoMean = safe_atof(field[15]);
3956
0
    double towOffset = safe_atof(field[16]);
3957
0
    int leapS = atoi(field[17]);
3958
0
    char ts_buf[TIMESPEC_LEN];
3959
0
    char scr[128];
3960
3961
0
    switch (gpsStatus) {
3962
0
    case 0:
3963
        // no fix
3964
0
        session->newdata.status = STATUS_UNK;
3965
0
        session->newdata.mode = MODE_NO_FIX;
3966
0
        break;
3967
0
    case 1:
3968
        // DR
3969
0
        session->newdata.status = STATUS_DR;
3970
0
        session->newdata.mode = MODE_3D;
3971
0
        break;
3972
0
    case 2:
3973
        // 2D
3974
0
        session->newdata.status = STATUS_GPS;
3975
0
        session->newdata.mode = MODE_2D;
3976
0
        break;
3977
0
    case 3:
3978
        // 3D
3979
0
        session->newdata.status = STATUS_GPS;
3980
0
        session->newdata.mode = MODE_3D;
3981
0
        break;
3982
0
    case 4:
3983
        // GPSDR
3984
0
        session->newdata.status = STATUS_GNSSDR;
3985
0
        session->newdata.mode = MODE_3D;
3986
0
        break;
3987
0
    case 5:
3988
        // surveyed
3989
0
        session->newdata.status = STATUS_TIME;
3990
0
        session->newdata.mode = MODE_3D;
3991
0
        break;
3992
0
    case 8:
3993
        // DGPS
3994
0
        session->newdata.status = STATUS_DGPS;
3995
0
        session->newdata.mode = MODE_3D;
3996
0
        break;
3997
0
    case 9:
3998
        // SBAS ??
3999
0
        session->newdata.status = STATUS_GPS;
4000
0
        session->newdata.mode = MODE_3D;
4001
0
        break;
4002
0
    case 10:
4003
        // FTK SINGLE ??
4004
0
        session->newdata.status = STATUS_RTK_FLT;  // ??
4005
0
        session->newdata.mode = MODE_3D;
4006
0
        break;
4007
0
    case 11:
4008
        // FTK FLOAT
4009
0
        session->newdata.status = STATUS_RTK_FLT;
4010
0
        session->newdata.mode = MODE_3D;
4011
0
        break;
4012
0
    case 12:
4013
        // FTK FIX
4014
0
        session->newdata.status = STATUS_RTK_FIX;
4015
0
        session->newdata.mode = MODE_3D;
4016
0
        break;
4017
0
    default:
4018
        // Huh?
4019
0
        session->newdata.status = STATUS_UNK;
4020
0
        session->newdata.mode = MODE_NOT_SEEN;
4021
0
        break;
4022
0
    }
4023
0
    mask |= MODE_SET | STATUS_SET;
4024
4025
0
    if (MODE_2D == session->newdata.mode ||
4026
0
        MODE_3D == session->newdata.mode) {
4027
0
            timespec_t ts_tow;
4028
4029
0
            session->newdata.latitude = lat;
4030
0
            session->newdata.longitude = lon;
4031
0
            mask |= LATLON_SET;
4032
0
            if (MODE_3D == session->newdata.mode) {
4033
0
                session->newdata.altHAE = altHAE;
4034
0
                session->newdata.altMSL = altMSL;
4035
0
                mask |= ALTITUDE_SET;
4036
0
            }
4037
            // assume leapS is valid if we are 2D ???
4038
0
            session->context->leap_seconds = leapS;
4039
0
            session->context->valid |= LEAP_SECOND_VALID;
4040
4041
            // assume time is valid if we are 2D ???
4042
0
            MSTOTS(&ts_tow, i_tow);
4043
0
            session->newdata.time = gpsd_gpstime_resolv(session, weeks,
4044
0
                                                        ts_tow);
4045
4046
0
            mask |= (TIME_SET | NTPTIME_IS);
4047
0
    }
4048
4049
0
    GPSD_LOG(LOG_IO, &session->context->errout,
4050
0
             "NMEA0183: PGPSP: %s i_tow=%lu weeks=%d "
4051
0
             "status=x%lx used=%d gpsStatus=%d type=%d "
4052
0
             "lat=%.2f lon=%.2f "
4053
0
             "altHAE=%.2f altMSL=%.2f "
4054
0
             "pdop=%.2f hacc=%.2f vacc=%.2f sacc=%.2f "
4055
0
             "vecef: X=%.2f Y=%.2f Z=%.2f cnoMean=.%1f "
4056
0
             "towOffset=%.4f leapS=%d\n",
4057
0
             timespec_to_iso8601(session->newdata.time, scr, sizeof(scr)),
4058
0
             i_tow, weeks, status, used, gpsStatus, fixType, lat, lon,
4059
0
             altHAE, altMSL,
4060
0
             pDOP, hAcc, vAcc, sAcc,
4061
0
             vecefX, vecefY, vecefZ, cnoMean,
4062
0
             towOffset, leapS);
4063
4064
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4065
0
             "NMEA0183: PGPSP: time=%s lat=%.2f lon=%.2f "
4066
0
             "mode=%d status=%d\n",
4067
0
             timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
4068
0
             session->newdata.latitude,
4069
0
             session->newdata.longitude,
4070
0
             session->newdata.mode,
4071
0
             session->newdata.status);
4072
0
    return mask;
4073
0
}
4074
4075
// Garmin Estimated Position Error
4076
static gps_mask_t processPGRME(int c UNUSED, char *field[],
4077
                               struct gps_device_t *session)
4078
0
{
4079
    /*
4080
     * $PGRME,15.0,M,45.0,M,25.0,M*22
4081
     * 1    = horizontal error estimate
4082
     * 2    = units
4083
     * 3    = vertical error estimate
4084
     * 4    = units
4085
     * 5    = spherical error estimate
4086
     * 6    = units
4087
     * *
4088
     * * Garmin won't say, but the general belief is that these are 50% CEP.
4089
     * * We follow the advice at <http://gpsinformation.net/main/errors.htm>.
4090
     * * If this assumption changes here, it should also change in garmin.c
4091
     * * where we scale error estimates from Garmin binary packets, and
4092
     * * in libgpsd_core.c where we generate $PGRME.
4093
     */
4094
0
    gps_mask_t mask = ONLINE_SET;
4095
4096
0
    if ('M' == field[2][0] &&
4097
0
        'M' == field[4][0] &&
4098
0
        'M' == field[6][0]) {
4099
0
        session->newdata.epx = session->newdata.epy =
4100
0
            safe_atof(field[1]) * (1 / sqrt(2))
4101
0
                      * (GPSD_CONFIDENCE / CEP50_SIGMA);
4102
0
        session->newdata.epv =
4103
0
            safe_atof(field[3]) * (GPSD_CONFIDENCE / CEP50_SIGMA);
4104
0
        session->newdata.sep =
4105
0
            safe_atof(field[5]) * (GPSD_CONFIDENCE / CEP50_SIGMA);
4106
0
        mask = HERR_SET | VERR_SET | PERR_IS;
4107
0
    }
4108
4109
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4110
0
             "NMEA0183: PGRME: epx=%.2f epy=%.2f sep=%.2f\n",
4111
0
             session->newdata.epx,
4112
0
             session->newdata.epy,
4113
0
             session->newdata.sep);
4114
0
    return mask;
4115
0
}
4116
4117
/* Garmin GPS Fix Data Sentence
4118
 *
4119
 * FIXME: seems to happen after cycle ender, so little happens...
4120
 */
4121
static gps_mask_t processPGRMF(int c UNUSED, char *field[],
4122
                               struct gps_device_t *session)
4123
0
{
4124
 /*
4125
  * $PGRMF,290,293895,160305,093802,13,5213.1439,N,02100.6511,E,A,2,0,226,2,1*11
4126
  *
4127
  * 1 = GPS week
4128
  * 2 = GPS seconds
4129
  * 3 = UTC Date ddmmyy
4130
  * 4 = UTC time hhmmss
4131
  * 5 = GPS leap seconds
4132
  * 6 = Latitude ddmm.mmmm
4133
  * 7 = N or S
4134
  * 8 = Longitude dddmm.mmmm
4135
  * 9 = E or W
4136
  * 10 = Mode, M = Manual, A = Automatic
4137
  * 11 = Fix type, 0 = No fix, 2 = 2D fix, 2 = 3D fix
4138
  * 12 = Ground Speed, 0 to 1151 km/hr
4139
  * 13 = Course over ground, 0 to 359 degrees true
4140
  * 14 = pdop, 0 to 9
4141
  * 15 = dop, 0 to 9
4142
  */
4143
0
    gps_mask_t mask = ONLINE_SET;
4144
0
    timespec_t ts_tow = {0, 0};
4145
4146
    /* Some garmin fail due to GPS Week Roll Over
4147
     * Ignore their UTC date/time, use their GPS week, GPS tow and
4148
     * leap seconds to decide the correct time */
4149
0
    if (isdigit((int)field[5][0])) {
4150
0
        session->context->leap_seconds = atoi(field[5]);
4151
0
        session->context->valid = LEAP_SECOND_VALID;
4152
0
    }
4153
0
    if (isdigit((int)field[1][0]) &&
4154
0
        isdigit((int)field[2][0]) &&
4155
0
        0 < session->context->leap_seconds) {
4156
        // have GPS week, tow and leap second
4157
0
        unsigned short week = atol(field[1]);
4158
0
        ts_tow.tv_sec = atol(field[2]);
4159
0
        ts_tow.tv_nsec = 0;
4160
0
        session->newdata.time = gpsd_gpstime_resolv(session, week, ts_tow);
4161
0
        mask |= TIME_SET;
4162
        // (long long) cast for 32/64 bit compat
4163
0
        GPSD_LOG(LOG_SPIN, &session->context->errout,
4164
0
                 "NMEA0183: PGRMF gps time %lld\n",
4165
0
                 (long long)session->newdata.time.tv_sec);
4166
0
    } else if (0 == merge_hhmmss(field[4], session) &&
4167
0
               0 == merge_ddmmyy(field[3], session)) {
4168
        // fall back to UTC if we need and can
4169
        // (long long) cast for 32/64 bit compat
4170
0
        GPSD_LOG(LOG_SPIN, &session->context->errout,
4171
0
                 "NMEA0183: PGRMF gps time %lld\n",
4172
0
                 (long long)session->newdata.time.tv_sec);
4173
0
        mask |= TIME_SET;
4174
0
    }
4175
0
    if ('A' != field[10][0]) {
4176
        // Huh?
4177
0
        return mask;
4178
0
    }
4179
0
    if (0 == do_lat_lon(&field[6], &session->newdata)) {
4180
0
        mask |= LATLON_SET;
4181
0
    }
4182
0
    switch (field[11][0]) {
4183
0
    default:
4184
        // Huh?
4185
0
        break;
4186
0
    case '0':
4187
0
        session->newdata.mode = MODE_NO_FIX;
4188
0
        mask |= MODE_SET;
4189
0
        break;
4190
0
    case '1':
4191
0
        session->newdata.mode = MODE_2D;
4192
0
        mask |= MODE_SET;
4193
0
        break;
4194
0
    case '2':
4195
0
        session->newdata.mode = MODE_3D;
4196
0
        mask |= MODE_SET;
4197
0
        break;
4198
0
    }
4199
0
    session->newdata.speed = safe_atof(field[12]) / MPS_TO_KPH;
4200
0
    session->newdata.track = safe_atof(field[13]);
4201
0
    mask |= SPEED_SET | TRACK_SET;
4202
0
    if ('\0' != field[14][0]) {
4203
0
        session->gpsdata.dop.pdop = safe_atof(field[14]);
4204
0
        mask |= DOP_SET;
4205
0
    }
4206
0
    if ('\0' != field[15][0]) {
4207
0
        session->gpsdata.dop.tdop = safe_atof(field[15]);
4208
0
        mask |= DOP_SET;
4209
0
    }
4210
4211
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4212
0
             "NMEA0183: PGRMF: pdop %.1f tdop %.1f \n",
4213
0
             session->gpsdata.dop.pdop,
4214
0
             session->gpsdata.dop.tdop);
4215
0
    return mask;
4216
0
}
4217
4218
/* Garmin Map Datum
4219
 *
4220
 * FIXME: seems to happen after cycle ender, so nothing happens...
4221
 */
4222
static gps_mask_t processPGRMM(int c UNUSED, char *field[],
4223
                               struct gps_device_t *session)
4224
0
{
4225
    /*
4226
     * $PGRMM,NAD83*29
4227
     * 1    = Map Datum
4228
     */
4229
0
    gps_mask_t mask = ONLINE_SET;
4230
4231
0
    if ('\0' != field[1][0]) {
4232
0
        strlcpy(session->newdata.datum, field[1],
4233
0
                sizeof(session->newdata.datum));
4234
0
    }
4235
4236
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4237
0
             "NMEA0183: PGRMM: datum=%.40s\n",
4238
0
             session->newdata.datum);
4239
0
    return mask;
4240
0
}
4241
4242
// Garmin Sensor Status Info
4243
static gps_mask_t processPGRMT(int c UNUSED, char *field[],
4244
                               struct gps_device_t *session)
4245
0
{
4246
    /*
4247
     * $PGRMT,GPS 15x-W software ver. 4.20,,,,,,,,*6A
4248
     * 1    = Product, model and software version
4249
     * 2    = ROM Checksum test P=pass, F=fail
4250
     * 3    = Receiver failure discrete, P=pass, F=fail
4251
     * 4    = Stored data lost, R=retained, L=lost
4252
     * 5    = Real time clock lost, R=retained, L=lost
4253
     * 6    = Oscillator drift discrete, P=pass, F=excessive drift detected
4254
     * 7    = Data collection discrete, C=collecting, null if not collecting
4255
     * 8    = GPS sensor temperature in degrees C
4256
     * 9    = GPS sensor configuration data, R=retained, L=lost
4257
     *
4258
     * Output once per minuite by default.
4259
     * 50 char max.
4260
     *
4261
     * As of October 2022, only ever seen field 1 populated
4262
     */
4263
0
    gps_mask_t mask = ONLINE_SET;
4264
4265
0
    strlcpy(session->subtype, field[1], sizeof(session->subtype));
4266
4267
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4268
0
             "NMEA0183: PGRMT: subtype %s\n",
4269
0
             session->subtype);
4270
0
    return mask;
4271
0
}
4272
4273
// Garmin 3D Velocity Information
4274
static gps_mask_t processPGRMV(int c UNUSED, char *field[],
4275
                               struct gps_device_t *session)
4276
0
{
4277
    /*
4278
     * $PGRMV,-2.4,-1.1,0.3*59
4279
     * 1    = true east velocity,  m/s
4280
     * 2    = true north velocity,  m/s
4281
     * 3    = true up velocity,  m/s
4282
     */
4283
0
    gps_mask_t mask = ONLINE_SET;
4284
4285
0
    if ('\0' == field[1][0] ||
4286
0
        '\0' == field[2][0] ||
4287
0
        '\0' == field[3][0]) {
4288
        // nothing to report
4289
0
        return mask;
4290
0
    }
4291
4292
0
    session->newdata.NED.velE = safe_atof(field[1]);
4293
0
    session->newdata.NED.velN = safe_atof(field[2]);
4294
0
    session->newdata.NED.velD = -safe_atof(field[3]);
4295
4296
0
    mask |= VNED_SET;
4297
4298
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4299
0
             "NMEA0183: PGRMV: velE %.2f velN %.2f velD %.2f\n",
4300
0
            session->newdata.NED.velE,
4301
0
            session->newdata.NED.velN,
4302
0
            session->newdata.NED.velD);
4303
0
    return mask;
4304
0
}
4305
4306
// Garmin Altitude Information
4307
static gps_mask_t processPGRMZ(int c UNUSED, char *field[],
4308
                               struct gps_device_t *session)
4309
0
{
4310
    /*
4311
     * $PGRMZ,246,f,3*1B
4312
     * 1    = Altitude (probably MSL) in feet
4313
     * 2    = f (feet)
4314
     * 3    = Mode
4315
     *         1 = No Fix
4316
     *         2 = 2D Fix
4317
     *         3 = 3D Fix
4318
     *
4319
     * From: Garmin Proprietary NMEA 0183 Sentences
4320
     *       technical Specifications
4321
     *       190-00684-00, Revision C December 2008
4322
     */
4323
0
    gps_mask_t mask = ONLINE_SET;
4324
4325
    // codacy does not like strlen()
4326
0
    if ('f' == field[2][0] &&
4327
0
        0 < strnlen(field[1], 20)) {
4328
        // have a GPS altitude, must be 3D
4329
        // seems to be altMSL.  regressions show this matches GPGGA MSL
4330
0
        session->newdata.altMSL = atoi(field[1]) * FEET_TO_METERS;
4331
0
        mask |= (ALTITUDE_SET);
4332
0
    }
4333
0
    switch (field[3][0]) {
4334
0
    default:
4335
        // Huh?
4336
0
        break;
4337
0
    case '1':
4338
0
        session->newdata.mode = MODE_NO_FIX;
4339
0
        mask |= MODE_SET;
4340
0
        break;
4341
0
    case '2':
4342
0
        session->newdata.mode = MODE_2D;
4343
0
        mask |= MODE_SET;
4344
0
        break;
4345
0
    case '3':
4346
0
        session->newdata.mode = MODE_3D;
4347
0
        mask |= MODE_SET;
4348
0
        break;
4349
0
    }
4350
4351
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4352
0
             "NMEA0183: PGRMZ: altMSL %.2f mode %d\n",
4353
0
             session->newdata.altMSL,
4354
0
             session->newdata.mode);
4355
0
    return mask;
4356
0
}
4357
4358
// Magellan Status
4359
static gps_mask_t processPMGNST(int c UNUSED, char *field[],
4360
                                struct gps_device_t *session)
4361
0
{
4362
    /*
4363
     * $PMGNST,01.75,3,T,816,11.1,-00496,00*43
4364
     * 1 = Firmware version number
4365
     * 2 = Mode (1 = no fix, 2 = 2D fix, 3 = 3D fix)
4366
     * 3 = T if we have a fix
4367
     * 4 = battery percentage left in tenths of a percent
4368
     * 5 = time left on the GPS battery in hours
4369
     * 6 = numbers change (freq. compensation?)
4370
     * 7 = PRN number receiving current focus
4371
     */
4372
0
    gps_mask_t mask = ONLINE_SET;
4373
0
    int newmode = atoi(field[3]);
4374
4375
0
    if ('T' == field[4][0]) {
4376
0
        switch(newmode) {
4377
0
        default:
4378
0
            session->newdata.mode = MODE_NO_FIX;
4379
0
            break;
4380
0
        case 2:
4381
0
            session->newdata.mode = MODE_2D;
4382
0
            break;
4383
0
        case 3:
4384
0
            session->newdata.mode = MODE_3D;
4385
0
            break;
4386
0
        }
4387
0
    } else {
4388
        // Can report 3D fix, but 'F' for no fix
4389
0
        session->newdata.mode = MODE_NO_FIX;
4390
0
    }
4391
0
    mask |= MODE_SET;
4392
4393
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4394
0
             "NMEA0183: PMGNST: mode: %d\n",
4395
0
             session->newdata.mode);
4396
0
    return mask;
4397
0
}
4398
4399
static gps_mask_t processPMTK001(int c UNUSED, char *field[],
4400
                                 struct gps_device_t *session)
4401
0
{
4402
0
    int reason;
4403
0
    const char *mtk_reasons[] = {
4404
0
        "Invalid",
4405
0
        "Unsupported",
4406
0
        "Valid but Failed",
4407
0
        "Valid success",       // unused, see above
4408
0
        "Unknown",             // gpsd only
4409
0
    };
4410
4411
    // ACK / NACK
4412
0
    reason = atoi(field[2]);
4413
0
    if (4 == reason) {
4414
        // ACK
4415
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
4416
0
                 "NMEA0183: MTK ACK: %s\n", field[1]);
4417
0
        return ONLINE_SET;
4418
0
    }
4419
4420
    // else, NACK
4421
0
    if (0 > reason ||
4422
0
        3 < reason) {
4423
        // WTF?
4424
0
        reason = 4;
4425
0
    }
4426
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
4427
0
             "NMEA0183: MTK NACK: %s, reason: %s\n",
4428
0
             field[1], mtk_reasons[reason]);
4429
0
    return ONLINE_SET;
4430
0
}
4431
4432
static gps_mask_t processPMTK424(int c UNUSED, char *field[],
4433
                                 struct gps_device_t *session)
4434
0
{
4435
    // PPS pulse width response
4436
    /*
4437
     * Response will look something like: $PMTK424,0,0,1,0,69*12
4438
     * The pulse width is in field 5 (69 in this example).  This
4439
     * sentence is poorly documented at:
4440
     * http://www.trimble.com/embeddedsystems/condor-gps-module.aspx?dtID=documentation
4441
     *
4442
     * Packet Type: 324 PMTK_API_SET_OUTPUT_CTL
4443
     * Packet meaning
4444
     * Write the TSIP/antenna/PPS configuration data to the Flash memory.
4445
     * DataField [Data0]:TSIP Packet[on/off]
4446
     * 0 - Disable TSIP output (Default).
4447
     * 1 - Enable TSIP output.
4448
     * [Data1]:Antenna Detect[on/off]
4449
     * 0 - Disable antenna detect function (Default).
4450
     * 1 - Enable antenna detect function.
4451
     * [Data2]:PPS on/off
4452
     * 0 - Disable PPS function.
4453
     * 1 - Enable PPS function (Default).
4454
     * [Data3]:PPS output timing
4455
     * 0 - Always output PPS (Default).
4456
     * 1 - Only output PPS when GPS position is fixed.
4457
     * [Data4]:PPS pulse width
4458
     * 1~16367999: 61 ns~(61x 16367999) ns (Default = 69)
4459
     *
4460
     * The documentation does not give the units of the data field.
4461
     * Andy Walls <andy@silverblocksystems.net> says:
4462
     *
4463
     * "The best I can figure using an oscilloscope, is that it is
4464
     * in units of 16.368000 MHz clock cycles.  It may be
4465
     * different for any other unit other than the Trimble
4466
     * Condor. 69 cycles / 16368000 cycles/sec = 4.216 microseconds
4467
     * [which is the pulse width I have observed]"
4468
     *
4469
     * Support for this theory comes from the fact that crystal
4470
     * TXCOs with a 16.368MHZ period are commonly available from
4471
     * multiple vendors. Furthermore, 61*69 = 4209, which is
4472
     * close to the observed cycle time and suggests that the
4473
     * documentation is trying to indicate 61ns units.
4474
     *
4475
     * He continues:
4476
     *
4477
     * "I chose [127875] because to divides 16368000 nicely and the
4478
     * pulse width is close to 1/100th of a second.  Any number
4479
     * the user wants to use would be fine.  127875 cycles /
4480
     * 16368000 cycles/second = 1/128 seconds = 7.8125
4481
     * milliseconds"
4482
     */
4483
4484
    // too short?  Make it longer
4485
0
    if (127875 > atoi(field[5])) {
4486
0
        (void)nmea_send(session, "$PMTK324,0,0,1,0,127875");
4487
0
    }
4488
0
    return ONLINE_SET;
4489
0
}
4490
4491
static gps_mask_t processPMTK705(int count, char *field[],
4492
                                 struct gps_device_t *session)
4493
0
{
4494
    /* Trimble version:
4495
     * $PMTK705,AXN_1.30,0000,20090609,*20<CR><LF>
4496
     *
4497
     * 0 PMTK705
4498
     * 1 ReleaseStr - Firmware release name and version
4499
     * 2 Build_ID   - Build ID
4500
     * 3 Date code  - YYYYMMDD
4501
     * 4 Checksum
4502
     *
4503
     * Quectel Querk.  L26.
4504
     * $PMTK705,AXN_3.20_3333_13071501,0003,QUECTEL-L26,*1E<CR><LF>
4505
     *
4506
     * 0 PMTK705
4507
     * 1 ReleaseStr - Firmware release name and version
4508
     * 2 Build_ID   - Build ID
4509
     * 3 Date code  - Product Model
4510
     * 4 SDK Version (optional)
4511
     * * Checksum
4512
    */
4513
4514
    // set device subtype
4515
0
    if (4 == count) {
4516
0
        (void)snprintf(session->subtype, sizeof(session->subtype),
4517
0
                       "%s,%s,%s",
4518
0
                       field[1], field[2], field[3]);
4519
0
    } else {
4520
        // Once again Quectel goes their own way...
4521
0
        (void)snprintf(session->subtype, sizeof(session->subtype),
4522
0
                       "%s,%s,%s,%s",
4523
0
                       field[1], field[2], field[3], field[4]);
4524
0
    }
4525
4526
0
    if ('\0' == session->subtype1[0]) {
4527
        /* Query for the Quectel firmware version.
4528
         * Quectel GPS receivers containing an MTK chipset use
4529
         * this command to return their FW version.
4530
         * From
4531
         * https://forums.quectel.com/t/determine-nmea-version-of-l76-l/3882/5
4532
         * "$PQVERNO is an internal command and used to query Quectel FW
4533
         * version. We haven’t added this internal command in GNSS
4534
         * protocol spec."
4535
         */
4536
0
        (void)nmea_send(session, "$PQVERNO,R");
4537
0
    }
4538
4539
0
    return ONLINE_SET;
4540
0
}
4541
4542
static gps_mask_t processPQxERR(int c UNUSED, char* field[],
4543
                                struct gps_device_t* session)
4544
0
{
4545
    /* Quectel generic PQxxxERRROR message handler
4546
     * The messages are content free, not very useful.
4547
     *
4548
     * $PQTMCFGEINSMSGERROR*4A
4549
     * $PQTMCFGORIENTATIONERROR*54
4550
     * $PQTMCFGWHEELTICKERROR*44
4551
     * $PQTMQMPTERROR*58
4552
     */
4553
4554
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
4555
0
             "NMEA0183: %s Error\n", field[0]);
4556
0
    return ONLINE_SET;
4557
0
}
4558
4559
static gps_mask_t processPQxOK(int c UNUSED, char* field[],
4560
                               struct gps_device_t* session)
4561
0
{
4562
    /* Quectel generic PQTMxxxOK message handler
4563
     * The messages are content free, not very useful.
4564
     *
4565
     * $PQTMCFGEINSMSGOK*16
4566
     * $PQTMCFGORIENTATIONOK*08
4567
     * $PQTMCFGWHEELTICKOK*18
4568
     */
4569
4570
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4571
0
             "NMEA0183: %s OK\n", field[0]);
4572
0
    return ONLINE_SET;
4573
0
}
4574
4575
// Quectel $PQTMGPS - GNSS position stuff
4576
static gps_mask_t processPQTMGPS(int count UNUSED, char *field[],
4577
                                 struct gps_device_t *session)
4578
0
{
4579
    /*
4580
     * $PQTMGPS,671335,463792.000,31.822084600,117.115221100,59.4260,63.0420,
4581
     *  0.0270,-171.7101,5.9890,1.3300,2.1100,3,18,*75
4582
     *
4583
     * 1   Milliseconds since turn on. 32-bit unsigned integer.
4584
     * 2   Time of week. Seconds
4585
     * 3   Latitude. Degrees
4586
     * 4   Longitude. Degrees
4587
     * 5   Height above ellipsoid, Meters
4588
     * 6   Altitude above mean-sea-level. Meters
4589
     * 7   Ground speed (2D). Meters / sec
4590
     * 8   Heading (2D). Degrees.
4591
     * 9   Horizontal accuracy estimate. Meters.
4592
     * 10  HDOP
4593
     * 11  PDOP
4594
     * 12  Fix type.  0 = No fix.  2 = 2D fix.  3 = 3D fix.
4595
     * 13  Number of navigation satellites (seen? used?)
4596
     *
4597
     * Note: incomplete time stamp.
4598
     */
4599
0
    gps_mask_t mask = ONLINE_SET;
4600
0
    unsigned ts = atoi(field[1]);
4601
0
    unsigned tow = atoi(field[2]);
4602
0
    double lat = safe_atof(field[3]);
4603
0
    double lon = safe_atof(field[4]);
4604
0
    double hae = safe_atof(field[5]);
4605
0
    double msl = safe_atof(field[6]);
4606
0
    double speed = safe_atof(field[7]);
4607
0
    double heading = safe_atof(field[8]);
4608
0
    double hAcc = safe_atof(field[9]);
4609
0
    double hdop = safe_atof(field[10]);
4610
0
    double pdop = safe_atof(field[11]);
4611
0
    unsigned fix = atoi(field[12]);
4612
0
    unsigned numsat = atoi(field[13]);
4613
4614
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4615
0
             "NMEA0183: PQTMGPS ts %u tow %u lat %.9f lon %.9f HAE %.4f "
4616
0
             "MSL %.4f speed %.4f head %.4f hacc %.4f hdop %.4f pdop %.4f "
4617
0
             "mode %u nsat %u\n",
4618
0
             ts, tow, lat, lon, hae, msl, speed, heading, hAcc, hdop,
4619
0
             pdop, fix, numsat);
4620
0
    return mask;
4621
0
}
4622
4623
// Quectel $PQTMIMU - IMU Raw Data
4624
static gps_mask_t processPQTMIMU(int count UNUSED, char *field[],
4625
                                 struct gps_device_t *session)
4626
0
{
4627
    /*
4628
     * $PQTMIMU,42634,-0.006832,-0.022814,1.014552,0.315000,-0.402500,
4629
       -0.332500,0,0*55
4630
     *
4631
     * 1   Milliseconds since turn on. 32-bit unsigned integer.
4632
     * 2   Acceleration in X-axis direction. g
4633
     * 3   Acceleration in Y-axis direction. g
4634
     * 4   Acceleration in A-axis direction. g
4635
     * 5   Angular rate in X-axis direction. Degrees / second
4636
     * 6   Angular rate in y-axis direction. Degrees / second
4637
     * 7   Angular rate in Z-axis direction. Degrees / second
4638
     * 8   Cumulative ticks
4639
     * 9   Timestamp of last tick
4640
     */
4641
0
    gps_mask_t mask = ONLINE_SET;
4642
0
    unsigned ts = atoi(field[1]);
4643
0
    double accX = safe_atof(field[2]);
4644
0
    double accY = safe_atof(field[3]);
4645
0
    double accZ = safe_atof(field[4]);
4646
0
    double rateX = safe_atof(field[5]);
4647
0
    double rateY = safe_atof(field[6]);
4648
0
    double rateZ = safe_atof(field[7]);
4649
0
    unsigned ticks = atoi(field[8]);
4650
0
    unsigned tick_ts = atoi(field[9]);
4651
4652
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4653
0
             "NMEA0183: PQTMIMU ts %u accX %.6f accY %.6f accZ %.6f "
4654
0
             "rateX %.6f rateY %.6f rateZ %.6f ticks %u tick_ts %u\n",
4655
0
             ts, accX, accY, accZ, rateX, rateY, rateZ, ticks, tick_ts);
4656
0
    return mask;
4657
0
}
4658
4659
// Quectel $PQTMINS - DR Nav results
4660
static gps_mask_t processPQTMINS(int count UNUSED, char *field[],
4661
                                 struct gps_device_t *session)
4662
0
{
4663
    /*
4664
     * $PQTMINS,42529,1,31.822038000,117.115182800,67.681000,,,,-0.392663,
4665
        1.300793,0.030088*4D
4666
     *
4667
     * 1   Milliseconds since turn on. 32-bit unsigned integer.
4668
     * 2   Solution type, 0 = Pitch and Roll, 1 = GNSS, pitch, roll, heading
4669
     *                    2 = GNSS + DR, 3 = DR Only
4670
     * 3   Latitude. Degrees
4671
     * 4   Longitude. Degrees
4672
     * 5   Height (HAE?, MSL?) , Meters
4673
     * 6   Northward velocity
4674
     * 7   Eastward velocity
4675
     * 8   Downward velocity
4676
     * 9   Roll
4677
     * 10  Pitch
4678
     * 11  Heading
4679
     *
4680
     */
4681
0
    gps_mask_t mask = ONLINE_SET;
4682
0
    unsigned ts = atoi(field[1]);
4683
0
    unsigned sol = atoi(field[2]);
4684
0
    double lat = safe_atof(field[3]);
4685
0
    double lon = safe_atof(field[4]);
4686
0
    double alt = safe_atof(field[5]);
4687
0
    double velN = safe_atof(field[6]);
4688
0
    double velE = safe_atof(field[7]);
4689
0
    double velD = safe_atof(field[8]);
4690
0
    double roll = safe_atof(field[9]);
4691
0
    double pitch = safe_atof(field[10]);
4692
0
    double head = safe_atof(field[11]);
4693
4694
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4695
0
             "NMEA0183: PQTMINS ts %u sol %u lat %.9f lon %.9f alt %.6f "
4696
0
             "velN %.6f velE %.6f velD %.6f roll %.6f pitch %.6f head %.6f\n",
4697
0
             ts, sol, lat, lon, alt, velN, velE, velD, roll, pitch, head);
4698
0
    return mask;
4699
0
}
4700
4701
// Quectel $PQTMVER - Firmware info
4702
static gps_mask_t processPQTMVER(int count UNUSED, char *field[],
4703
                                 struct gps_device_t *session)
4704
0
{
4705
    /*
4706
     * $PQTMVER,MODULE_L89HANR01A06S,2022/07/28,18:27:04*7A
4707
     *
4708
     * 1   Version
4709
     * 2   build date yyyy/mm/dd
4710
     * 3   build time hh:mm:ss
4711
     *
4712
     */
4713
0
    char obuf[128];                      // temp version string buffer
4714
0
    gps_mask_t mask = ONLINE_SET;
4715
4716
    // save as subtype
4717
0
    (void)snprintf(obuf, sizeof(obuf),
4718
0
             "%s %.12s %.10s",
4719
0
             field[1], field[2], field[3]);
4720
4721
    // save what we can
4722
0
    (void)strlcpy(session->subtype, obuf, sizeof(session->subtype) - 1);
4723
4724
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4725
0
             "NMEA0183: PQTMVER %s\n",
4726
0
             session->subtype);
4727
4728
0
    return mask;
4729
0
}
4730
4731
static gps_mask_t processPQVERNO(int c UNUSED, char* field[],
4732
                                 struct gps_device_t* session)
4733
0
{
4734
    /* Example request & response are provided courtesy of Quectel below.
4735
     * This command is not publicly documented, but Quectel support
4736
     * provided this description via email. This has been tested on
4737
     * Quectel version L70-M39, but all recent (2022) versions of Quectel
4738
     * support this command is well.
4739
     *
4740
     * Request:
4741
     * $PQVERNO,R*3F
4742
     *
4743
     * Response:
4744
     * $PQVERNO,R,L96NR01A03S,2018/07/30,04:17*6B
4745
     *
4746
     * Description of the 6 fields are below.
4747
     *
4748
     * 1. $PQVERNO,              Query command
4749
     * 2. R,                     Read
4750
     * 3. L96NR01A03S,           Quectel firmware version number
4751
     * 4. 2018/07/30,            Firmware build date
4752
     * 5. 04:17*                 Firmware build time
4753
     * 6. 6B                     Checksum
4754
     */
4755
4756
0
    if (0 == strncmp(session->nmea.field[0], "PQVERNO", sizeof("PQVERNO")) &&
4757
0
        '\0' != field[2][0]) {
4758
0
        (void)snprintf(session->subtype1, sizeof(session->subtype1),
4759
0
                       "%s,%s,%s",
4760
0
                       field[2], field[3], field[4]);
4761
0
    }
4762
4763
0
    return ONLINE_SET;
4764
0
}
4765
4766
/* smart watch sensors
4767
 * A stub.
4768
 */
4769
static gps_mask_t processPRHS(int c UNUSED, char *field[],
4770
                               struct gps_device_t *session)
4771
0
{
4772
    /*
4773
     * $PRHS ,type,....
4774
     *   type = message type
4775
     *
4776
     * Yes: $PRHS[space],
4777
     *
4778
     * types:
4779
     * $PRHS ,ACC,9.952756,0.37819514,1.3165021,20150305072428436*44
4780
     * $PRHS ,COM,238.09642,16.275442,82.198425,20150305072428824*43
4781
     * $PRHS ,GYR,0.0,0.0,0.0,20150305072428247*4D
4782
     * $PRHS ,LAC,0.23899937,0.009213656,0.02143073,20150305072428437*46
4783
     * $PRHS ,MAG,47.183502,-51.789,-2.7145,20150305072428614*41
4784
     * $PRHS ,ORI,187.86511,-2.1546898,-82.405205,20150305072428614*53
4785
     * $PRHS ,RMC,20150305072427985*55
4786
     *
4787
     */
4788
0
    gps_mask_t mask = ONLINE_SET;
4789
4790
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4791
0
             "NMEA0183: PRHS: type %s\n",
4792
0
             field[1]);
4793
0
    return mask;
4794
0
}
4795
4796
static gps_mask_t processPSRFEPE(int c UNUSED, char *field[],
4797
                               struct gps_device_t *session)
4798
0
{
4799
    /*
4800
     * $PSRFEPE,100542.000,A,0.7,6.82,10.69,0.0,180.0*24
4801
     * 1    = UTC Time hhmmss.sss
4802
     * 2    = Status.  A = Valid, V = Data not valid
4803
     * 3    = HDOP
4804
     * 4    = EHPE meters (Estimated Horizontal Position Error)
4805
     * 5    = EVPE meters (Estimated Vertical Position Error)
4806
     * 6    = EHVE meters (Estimated Speed Over Ground/Velocity Error)
4807
     * 7    = EHE degrees (Estimated Heading Error)
4808
     *
4809
     * SiRF won't say if these are 1-sigma or what...
4810
     */
4811
0
    gps_mask_t mask = STATUS_SET;
4812
4813
    // get time/ valid or not
4814
0
    if ('\0' != field[1][0]) {
4815
0
        if (0 == merge_hhmmss(field[1], session)) {
4816
0
            register_fractional_time(field[0], field[1], session);
4817
0
            if (0 == session->nmea.date.tm_year) {
4818
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
4819
0
                         "NMEA0183: can't use PSRFEPE time until after ZDA "
4820
0
                         "or RMC has supplied a year.\n");
4821
0
            } else {
4822
0
                mask |= TIME_SET;
4823
0
            }
4824
0
        }
4825
0
    }
4826
0
    if ('A' != field[2][0]) {
4827
        // Huh?
4828
0
        return mask;
4829
0
    }
4830
4831
0
    if ('\0' != field[3][0]) {
4832
        /* This adds nothing, it just agrees with the gpsd calculation
4833
         * from the skyview.  Which is a nice confirmation. */
4834
0
        session->gpsdata.dop.hdop = safe_atof(field[3]);
4835
0
        mask |= DOP_SET;
4836
0
    }
4837
0
    if ('\0' != field[4][0]) {
4838
        // EHPE (Estimated Horizontal Position Error)
4839
0
        session->newdata.eph = safe_atof(field[4]);
4840
0
        mask |= HERR_SET;
4841
0
    }
4842
4843
0
    if ('\0' != field[5][0]) {
4844
        // Estimated Vertical Position Error (meters, 0.01 resolution)
4845
0
        session->newdata.epv = safe_atof(field[5]);
4846
0
        mask |= VERR_SET;
4847
0
    }
4848
4849
0
    if ('\0' != field[6][0]) {
4850
        // Estimated Horizontal Speed Error meters/sec
4851
0
        session->newdata.eps = safe_atof(field[6]);
4852
0
    }
4853
4854
0
    if ('\0' != field[7][0]) {
4855
        // Estimated Heading Error degrees
4856
0
        session->newdata.epd = safe_atof(field[7]);
4857
0
    }
4858
4859
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4860
0
             "NMEA0183: PSRFEPE: hdop=%.1f eph=%.1f epv=%.1f eps=%.1f "
4861
0
             "epd=%.1f\n",
4862
0
             session->gpsdata.dop.hdop,
4863
0
             session->newdata.eph,
4864
0
             session->newdata.epv,
4865
0
             session->newdata.eps,
4866
0
             session->newdata.epd);
4867
0
    return mask;
4868
0
}
4869
4870
/*  Recommended Minimum 3D GNSS Data
4871
 *  Skytaq
4872
 */
4873
static gps_mask_t processPSTI030(int count UNUSED, char *field[],
4874
                                 struct gps_device_t *session)
4875
0
{
4876
    /*
4877
     * $PSTI,030,hhmmss.sss,A,dddmm.mmmmmmm,a,dddmm.mmmmmmm,a,x.x,
4878
            x.x,x.x,x.x,ddmmyy,a.x.x,x.x*hh<CR><LF>
4879
     * 1     030          Sentence ID
4880
     * 2     225446.334   Time of fix 22:54:46 UTC
4881
     * 3     A            Status of Fix: A = Autonomous, valid;
4882
     *                                 V = invalid
4883
     * 4,5   4916.45,N    Latitude 49 deg. 16.45 min North
4884
     * 6,7   12311.12,W   Longitude 123 deg. 11.12 min West
4885
     * 8     103.323      Mean Sea Level meters
4886
     * 9     0.00         East Velocity meters/sec
4887
     * 10    0.00         North Velocity meters/sec
4888
     * 11    0.00         Up Velocity meters/sec
4889
     * 12    181194       Date of fix  18 November 1994
4890
     * 13    A            FAA mode indicator
4891
     *                        See faa_mode() for possible mode values.
4892
     * 14    1.2          RTK Age
4893
     * 15    4.2          RTK Ratio
4894
     * 16    *68          mandatory nmea_checksum
4895
     *
4896
     * In private email, SkyTraq says F mode is 10x more accurate
4897
     * than R mode.
4898
     */
4899
0
    gps_mask_t mask = ONLINE_SET;
4900
4901
0
    if (0 != strncmp(session->device_type->type_name, "Skytraq", 7)) {
4902
        // this is skytraq, but not marked yet, so probe for Skytraq
4903
        // Send MID 0x02, to get back MID 0x80
4904
0
        (void)gpsd_write(session, "\xA0\xA1\x00\x02\x02\x01\x03\x0d\x0a",9);
4905
0
    }
4906
4907
0
    if ('V' == field[3][0] ||
4908
0
        'N' == field[13][0]) {
4909
        // nav warning, or FAA not valid, ignore the rest of the data
4910
0
        session->newdata.status = STATUS_UNK;
4911
0
        session->newdata.mode = MODE_NO_FIX;
4912
0
        mask |= MODE_SET | STATUS_SET;
4913
0
    } else if ('A' == field[3][0]) {
4914
0
        double east, north, climb, age, ratio;
4915
4916
        // data valid
4917
0
        if ('\0' != field[2][0] &&
4918
0
            '\0' != field[12][0]) {
4919
            // good date and time
4920
0
            if (0 == merge_hhmmss(field[2], session) &&
4921
0
                0 == merge_ddmmyy(field[12], session)) {
4922
0
                mask |= TIME_SET;
4923
0
                register_fractional_time( "PSTI030", field[2], session);
4924
0
            }
4925
0
        }
4926
0
        if (0 == do_lat_lon(&field[4], &session->newdata)) {
4927
0
            session->newdata.mode = MODE_2D;
4928
0
            mask |= LATLON_SET;
4929
0
            if ('\0' != field[8][0]) {
4930
                // altitude is MSL
4931
0
                session->newdata.altMSL = safe_atof(field[8]);
4932
0
                mask |= ALTITUDE_SET;
4933
0
                session->newdata.mode = MODE_3D;
4934
                // Let gpsd_error_model() deal with geoid_sep and altHAE
4935
0
            }
4936
0
            mask |= MODE_SET;
4937
0
        }
4938
        /* convert ENU to track
4939
         * this has more precision than GPVTG, GPVTG comes earlier
4940
         * in the cycle */
4941
0
        east = safe_atof(field[9]);     // east velocity m/s
4942
0
        north = safe_atof(field[10]);   // north velocity m/s
4943
0
        climb = safe_atof(field[11]);   // up velocity m/s
4944
0
        age = safe_atof(field[14]);
4945
0
        ratio = safe_atof(field[15]);
4946
4947
0
        session->newdata.NED.velN = north;
4948
0
        session->newdata.NED.velE = east;
4949
0
        session->newdata.NED.velD = -climb;
4950
0
        if (0.05 < (age + ratio)) {
4951
            // don't report age == ratio == 0.0
4952
0
            session->newdata.dgps_age = age;
4953
0
            session->gpsdata.fix.base.ratio = ratio;
4954
0
        }
4955
4956
0
        mask |= VNED_SET | STATUS_SET;
4957
4958
0
        session->newdata.status = faa_mode(field[13][0]);
4959
0
        if (STATUS_RTK_FIX == session->newdata.status ||
4960
0
            STATUS_RTK_FLT == session->newdata.status) {
4961
            // RTK_FIX or RTK_FLT
4962
0
            session->gpsdata.fix.base.status = session->newdata.status;
4963
0
        }
4964
0
    }
4965
4966
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4967
0
             "NMEA0183: PSTI,030: ddmmyy=%s hhmmss=%s lat=%.2f lon=%.2f "
4968
0
             "status=%d, RTK(Age=%.1f Ratio=%.1f) faa mode %s(%s)\n",
4969
0
             field[12], field[2],
4970
0
             session->newdata.latitude,
4971
0
             session->newdata.longitude,
4972
0
             session->newdata.status,
4973
0
             session->newdata.dgps_age,
4974
0
             session->newdata.base.ratio,
4975
0
             field[13], char2str(field[13][0], c_faa_mode));
4976
0
    return mask;
4977
0
}
4978
4979
/* Skytraq RTK Baseline, fixed base to rover or moving base
4980
 * Same as $PSTI.035, except that is moving base to rover
4981
 * PX1172RH
4982
 */
4983
static gps_mask_t processPSTI032(int count UNUSED, char *field[],
4984
                                 struct gps_device_t *session)
4985
0
{
4986
    /*
4987
     * $PSTI,032,041457.000,170316,A,R,0.603,‐0.837,‐0.089,1.036,144.22,,,,,*1B
4988
     *
4989
     * 2  UTC time,  hhmmss.sss
4990
     * 3  UTC Date, ddmmyy
4991
     * 4  Status, ‘V’ = Void ‘A’ = Active
4992
     * 5  Mode indicator, 'O' = Float RTK, ‘F’ = Float RTK. ‘R’ = Fixed RTK
4993
     * 6  East‐projection of baseline, meters
4994
     * 7  North‐projection of baseline, meters
4995
     * 8  Up‐projection of baseline, meters
4996
     * 9  Baseline length, meters
4997
     * 10 Baseline course 144.22, true degrees
4998
     * 11 Reserved
4999
     * 12 Reserved
5000
     * 13 Reserved
5001
     * 14 Reserved
5002
     * 15 Reserved
5003
     * 16 Checksum
5004
     */
5005
0
    gps_mask_t mask = ONLINE_SET;
5006
0
    struct baseline_t *base = &session->gpsdata.fix.base;
5007
5008
0
    if ('A' != field[4][0]) {
5009
        //  status not valid
5010
0
        return mask;
5011
0
    }
5012
5013
    // Status Valid
5014
0
    if ('\0' != field[2][0] &&
5015
0
        '\0' != field[3][0]) {
5016
        // have date and time
5017
0
        if (0 == merge_hhmmss(field[2], session) &&
5018
0
            0 == merge_ddmmyy(field[3], session)) {
5019
            // good date and time
5020
0
            mask |= TIME_SET;
5021
0
            register_fractional_time("PSTI032", field[2], session);
5022
0
        }
5023
0
    }
5024
5025
0
    if ('F' == field[5][0] ||
5026
0
        'O' == field[5][0]) {
5027
        // Floating point RTK
5028
        // 'O' is undocuemented, private email says it is just a crappy 'F'.
5029
0
        base->status = STATUS_RTK_FLT;
5030
0
    } else if ('R' == field[5][0]) {
5031
        // Fixed point RTK
5032
0
        base->status = STATUS_RTK_FIX;
5033
0
    } else {
5034
        // WTF?
5035
0
        return mask;
5036
0
    }
5037
5038
0
    base->east = safe_atof(field[6]);
5039
0
    base->north = safe_atof(field[7]);
5040
0
    base->up = safe_atof(field[8]);
5041
0
    base->length = safe_atof(field[9]);
5042
0
    base->course = safe_atof(field[10]);
5043
5044
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5045
0
             "NMEA0183: PSTI,032: RTK Baseline mode %d E %.3f  N %.3f  U %.3f "
5046
0
             "length %.3f course %.3f\n",
5047
0
             base->status, base->east, base->north, base->up,
5048
0
             base->length, base->course);
5049
0
    return mask;
5050
0
}
5051
5052
/* Skytraq  RTK RAW Measurement Monitoring Data
5053
 */
5054
static gps_mask_t processPSTI033(int count UNUSED, char *field[],
5055
                                 struct gps_device_t *session)
5056
0
{
5057
    /*
5058
     * $PSTI,033,hhmmss.sss,ddmmyy,x,R,x,G,x,x,,,C,x,x,,,E,x,x,,,R,x,x,,*hh
5059
     * $PSTI,033,110431.000,150517,2,R,1,G,1,0,,,C,0,0,,,E,0,0,,,R,0,0,,*72
5060
     *
5061
     * 2  UTC time,  hhmmss.sss
5062
     * 3  UTC Date, ddmmyy
5063
     * 4  "2", version
5064
     * 5  Receiver, R = Rover, B = Base
5065
     * 6  total cycle‐slipped raw measurements
5066
     * 7  "G", GPS
5067
     * 8  cycle slipped L1
5068
     * 9  cycle slipped L2
5069
     * 10 reserved
5070
     * 11 reserved
5071
     * 12 "C", BDS
5072
     * 12 cycle slipped B1
5073
     * 14 cycle slipped B2
5074
     * 15 reserved
5075
     * 16 reserved
5076
     * 17 "E", Galileo
5077
     * 18 cycle slipped E1
5078
     * 19 cycle slipped E5b
5079
     * 20 reserved
5080
     * 21 reserved
5081
     * 22 "R", GLONASS
5082
     * 23 cycle slipped G1
5083
     * 24 cycle slipped G2
5084
     * 25 reserved
5085
     * 26 reserved
5086
     * 27 Checksum
5087
     */
5088
0
    gps_mask_t mask = ONLINE_SET;
5089
0
    char receiver;
5090
0
    unsigned total, L1, L2, B1, B2, E1, E5b, G1, G2;
5091
5092
0
    if ('2' != field[4][0]) {
5093
        //  we only understand version 2
5094
0
        return mask;
5095
0
    }
5096
0
    if ('B' != field[5][0] &&
5097
0
        'R' != field[5][0]) {
5098
        //  Huh?  Rover or Base
5099
0
        return mask;
5100
0
    }
5101
0
    receiver = field[5][0];
5102
5103
0
    if ('\0' != field[2][0] &&
5104
0
        '\0' != field[3][0]) {
5105
        // have date and time
5106
0
        if (0 == merge_hhmmss(field[2], session) &&
5107
0
            0 == merge_ddmmyy(field[3], session)) {
5108
            // good date and time
5109
0
            mask |= TIME_SET;
5110
0
            register_fractional_time("PSTI033", field[2], session);
5111
0
        }
5112
0
    }
5113
0
    total = atoi(field[6]);
5114
0
    L1 = atoi(field[7]);
5115
0
    L2 = atoi(field[8]);
5116
0
    B1 = atoi(field[13]);
5117
0
    B2 = atoi(field[14]);
5118
0
    E1 = atoi(field[18]);
5119
0
    E5b = atoi(field[19]);
5120
0
    G1 = atoi(field[23]);
5121
0
    G2 = atoi(field[24]);
5122
5123
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5124
0
             "NMEA0183: PSTI,033: RTK RAW receiver %c Slips: total %u L1 %u "
5125
0
             "L2 %u B1 %u B2 %u E1 %u E5b %u G1 %u G2 %u\n",
5126
0
             receiver, total, L1, L2, B1, B2, E1, E5b, G1, G2);
5127
0
    return mask;
5128
0
}
5129
5130
// Skytraq RTK Baseline, moving base to moving rover
5131
// PX1172RH
5132
static gps_mask_t processPSTI035(int count UNUSED, char *field[],
5133
                                 struct gps_device_t *session)
5134
0
{
5135
    /*
5136
     * $PSTI,035,041457.000,170316,A,R,0.603,‐0.837,‐0.089,1.036,144.22,,,,,*1B
5137
     *
5138
     * 2  UTC time,  hhmmss.sss
5139
     * 3  UTC Date, ddmmyy
5140
     * 4  Status, ‘V’ = Void ‘A’ = Active
5141
     * 5  Mode indicator, ‘F’ = Float RTK. ‘R’ = FIxed RTK
5142
     * 6  East‐projection of baseline, meters
5143
     * 7  North‐projection of baseline, meters
5144
     * 8  Up‐projection of baseline, meters
5145
     * 9  Baseline length, meters
5146
     * 10 Baseline course 144.22, true degrees
5147
     * 11 Reserved
5148
     * 12 Reserved
5149
     * 13 Reserved
5150
     * 14 Reserved
5151
     * 15 Reserved
5152
     * 16 Checksum
5153
     */
5154
5155
0
    gps_mask_t mask = ONLINE_SET;
5156
0
    struct baseline_t *base = &session->gpsdata.attitude.base;
5157
5158
    // RTK Baseline Data of Rover Moving Base Receiver
5159
0
    if ('\0' != field[2][0] &&
5160
0
        '\0' != field[3][0]) {
5161
        // good date and time
5162
0
        if (0 == merge_hhmmss(field[2], session) &&
5163
0
            0 == merge_ddmmyy(field[3], session)) {
5164
0
            mask |= TIME_SET;
5165
0
            register_fractional_time( "PSTI035", field[2], session);
5166
0
        }
5167
0
    }
5168
0
    if ('A' != field[4][0]) {
5169
        // No valid data, except time, sort of
5170
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
5171
0
                 "NMEA0183: PSTI,035: not valid\n");
5172
0
        base->status = STATUS_UNK;
5173
0
        return mask;
5174
0
    }
5175
0
    if ('F' == field[5][0]) {
5176
        // Float RTX
5177
0
        base->status = STATUS_RTK_FLT;
5178
0
    } else if ('R' == field[5][0]) {
5179
        // Fix RTX
5180
0
        base->status = STATUS_RTK_FIX;
5181
0
    } // else ??
5182
5183
0
    base->east = safe_atof(field[6]);
5184
0
    base->north = safe_atof(field[7]);
5185
0
    base->up = safe_atof(field[8]);
5186
0
    base->length = safe_atof(field[9]);
5187
0
    base->course = safe_atof(field[10]);
5188
0
    mask |= ATTITUDE_SET;
5189
5190
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5191
0
             "NMEA0183: PSTI,035: RTK Baseline mode %d E %.3f  N %.3f  U %.3f "
5192
0
             "length %.3f course %.3f\n",
5193
0
             base->status, base->east, base->north, base->up,
5194
0
             base->length, base->course);
5195
0
    return mask;
5196
0
}
5197
5198
// Skytraq PSTI,036 – Heading, Pitch and Roll
5199
// PX1172RH
5200
static gps_mask_t processPSTI036(int count UNUSED, char *field[],
5201
                                 struct gps_device_t *session)
5202
0
{
5203
    /*
5204
     * $PSTI,036,054314.000,030521,191.69,‐16.35,0.00,R*4D
5205
     *
5206
     * 2  UTC time,  hhmmss.sss
5207
     * 3  UTC Date, ddmmyy
5208
     * 4  Heading, 0 - 359.9, when mode == R, degrees
5209
     * 5  Pitch, -90 - 90, when mode == R, degrees
5210
     * 6  Roll, -90 - 90, when mode == R, degrees
5211
     * 7  Mode
5212
     *     'N’ = Data not valid
5213
     *     'A’ = Autonomous mode
5214
     *     'D’ = Differential mode
5215
     *     'E’ = Estimated (dead reckoning) mode
5216
     *     'M’ = Manual input mode
5217
     *     'S’ = Simulator mode
5218
     *     'F’ = Float RTK
5219
     *     'R’ = Fix RTK
5220
     * 8  Checksum
5221
     */
5222
5223
0
    gps_mask_t mask = ONLINE_SET;
5224
0
    int mode;
5225
5226
0
    if ('\0' != field[2][0] &&
5227
0
        '\0' != field[3][0]) {
5228
        // good date and time
5229
0
        if (0 == merge_hhmmss(field[2], session) &&
5230
0
            0 == merge_ddmmyy(field[3], session)) {
5231
0
            mask |= TIME_SET;
5232
0
            register_fractional_time("PSTI036", field[2], session);
5233
0
        }
5234
0
    }
5235
0
    if ('\0' == field[7][0] ||
5236
0
        'N' == field[7][0]) {
5237
        // No valid data, except time, sort of
5238
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
5239
0
                 "NMEA0183: PSTI,036: not valid\n");
5240
0
        return mask;
5241
0
    }
5242
    // good attitude data to use
5243
0
    session->gpsdata.attitude.mtime = gpsd_utc_resolve(session);
5244
0
    session->gpsdata.attitude.heading = safe_atof(field[4]);
5245
0
    session->gpsdata.attitude.pitch = safe_atof(field[5]);
5246
0
    session->gpsdata.attitude.roll = safe_atof(field[6]);
5247
0
    mode = faa_mode(field[7][0]);
5248
5249
0
    mask |= ATTITUDE_SET;
5250
5251
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5252
0
             "NMEA0183: PSTI,036: mode %d heading %.2f  pitch %.2f roll %.2f "
5253
0
             "faa mode %s(%s)\n",
5254
0
             mode,
5255
0
             session->gpsdata.attitude.heading,
5256
0
             session->gpsdata.attitude.pitch,
5257
0
             session->gpsdata.attitude.roll,
5258
0
             field[7], char2str(field[7][0], c_faa_mode));
5259
0
    return mask;
5260
0
}
5261
5262
/* decoce $PSTMANTENNASTATUS antenna status
5263
 * Private STM
5264
 * Also used bysome Quectel.
5265
 * Present in ST Teseo liv4f
5266
 *
5267
 */
5268
static gps_mask_t processPSTMANTENNASTATUS(int c UNUSED, char *field[],
5269
                                          struct gps_device_t *session)
5270
0
{
5271
    /*
5272
     * $PSTMANTENNASTATUS,<ant_status>,<op_mode>,<rf_path>,<pwr_switch>*<chk>
5273
     * $PSTMANTENNASTATUS,0,0,0,0*51
5274
     *
5275
     *  ant_status Decimal Current
5276
     *      0 = Normal condition
5277
     *      1 = Open condition
5278
     *      2 = Short condition
5279
     *
5280
     *  op_mode Decimal
5281
     *  Current antenna detection operating mode
5282
     *      0 = Automatic mode
5283
     *      1 = Manual mode
5284
     *
5285
     * rf_path Decimal
5286
     * Current RF path
5287
     *      0 = External antenna
5288
     *      1 = Internal antenna
5289
     *
5290
     * pwr_switch Decimal
5291
     * Current antenna power status
5292
     *      0 = Antenna power is on
5293
     *      1 = Antenna power is off
5294
     */
5295
5296
0
    static const struct vlist_t vop_mode[] = {
5297
0
        {0, "Auto"},
5298
0
        {1, "Manual"},
5299
0
        {0, NULL},
5300
0
    };
5301
5302
0
    static const struct vlist_t vpwr_switch[] = {
5303
0
        {0, "On"},
5304
0
        {1, "Off"},
5305
0
        {0, NULL},
5306
0
    };
5307
5308
0
    static const struct vlist_t vrf_path[] = {
5309
0
        {0, "External"},
5310
0
        {1, "Internal"},
5311
0
        {0, NULL},
5312
0
    };
5313
5314
0
    gps_mask_t mask = ONLINE_SET;
5315
0
    int ant_status = atoi(field[1]);
5316
0
    int op_mode = atoi(field[2]);
5317
0
    int rf_path = atoi(field[3]);
5318
0
    int pwr_switch = atoi(field[4]);
5319
5320
0
    switch(ant_status) {
5321
0
    case 0:
5322
0
        session->newdata.ant_stat = ANT_OK;
5323
0
        break;
5324
0
    case 1:
5325
0
        session->newdata.ant_stat = ANT_OPEN;
5326
0
        break;
5327
0
    case 2:
5328
0
        session->newdata.ant_stat = ANT_SHORT;
5329
0
        break;
5330
0
    default:
5331
0
        session->newdata.ant_stat = ANT_UNK;
5332
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
5333
0
                "NMEA0183: ant_stat: UNKNOWN(%d)\n", ant_status);
5334
0
        break;
5335
0
    }
5336
5337
0
    if (ANT_UNK != session->newdata.ant_stat) {
5338
0
        mask |= STATUS_SET;
5339
0
    }
5340
5341
0
    if (0 > op_mode ||
5342
0
        1 < op_mode) {
5343
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
5344
0
                "NMEA0183: malformed PSTMANTENNASTATUS op_mode: %s\n",
5345
0
                field[2]);
5346
0
    }
5347
5348
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5349
0
            "NMEA0183: PSTMANTENNASTATUS ant_status:%d op_mode:%d "
5350
0
            "rf_path:%d pwr_switch:%d\n",
5351
0
            ant_status, op_mode, rf_path, pwr_switch);
5352
0
    GPSD_LOG(LOG_IO, &session->context->errout,
5353
0
             "NMEA0183: PSTMANTENNASTATUS ant_status:%s(%d) op_mode:%s(%d) "
5354
0
             "rf_path:%s(%d) pwer_switch:%s(%d)\n",
5355
0
             val2str(session->newdata.ant_stat, vant_status),
5356
0
             session->newdata.ant_stat,
5357
0
             val2str(op_mode, vop_mode), op_mode,
5358
0
             val2str(rf_path, vrf_path), rf_path,
5359
0
             val2str(pwr_switch, vpwr_switch), pwr_switch);
5360
5361
0
    return mask;
5362
0
}
5363
5364
/* decoce $PSTMVER
5365
 * Private STM
5366
 * Present in ST Teseo liv4f
5367
 *
5368
 * Response to $PSTMGETVER,255
5369
 *
5370
 */
5371
static gps_mask_t processPSTMVER(int c UNUSED, char *field[],
5372
                                 struct gps_device_t *session)
5373
0
{
5374
    /*
5375
    * $PSTMVER,<SW name and version>*<checksum>
5376
    *
5377
    * $PSTMVER,FreeRTOS_V10.4.3_ARM*57
5378
    * $PSTMVER,BINIMG_STA8041_4.6.6.5.6_ARM*0C
5379
    * $PSTMVER,SWCFG_86065331*62
5380
    * $PSTMVER,GNSSLIB_8.4.8.13_ARM*7F
5381
    * $PSTMVER,OS20LIB_4.3.0_ARM*47
5382
    * $PSTMVER,GPSAPP_2.2.1_ARM*1D
5383
    * $PSTMVER,SWCFG_8102510d*35
5384
    * $PSTMVER,WAASLIB_2.18.0_ARM*61
5385
    * $PSTMVER,STAGPSLIB_5.0.0_ARM*59
5386
    * $PSTMVER,STA8090_622bc043*6F
5387
    */
5388
5389
0
    gps_mask_t mask = ONLINE_SET;
5390
0
    size_t m_len =  strnlen(field[1], 40) + 2;
5391
0
    size_t st_left =  (sizeof(session->subtype) -
5392
0
                       strnlen(session->subtype, sizeof(session->subtype)));
5393
0
    size_t st1_left =  (sizeof(session->subtype1) -
5394
0
                        strnlen(session->subtype1, sizeof(session->subtype1)));
5395
5396
0
    if (NULL != strstr(session->subtype, field[1]) ||
5397
0
        NULL != strstr(session->subtype1, field[1])) {
5398
        // already haev it, ignore.
5399
0
    } else if (m_len < st_left) {
5400
        // room in subtype
5401
0
        if ('\0' == session->subtype[0]) {
5402
0
            (void)strncat(session->subtype, "STM,",
5403
0
                          sizeof(session->subtype) - 1);
5404
0
        } else {
5405
0
            (void)strncat(session->subtype, ",",
5406
0
                          sizeof(session->subtype) - 1);
5407
0
        }
5408
0
        (void)strncat(session->subtype, field[1],
5409
0
                      sizeof(session->subtype) - 1);
5410
0
    } else if (m_len < st1_left) {
5411
        // room in subtype1
5412
0
        if ('\0' != session->subtype1[0]) {
5413
0
            (void)strncat(session->subtype1, ",",
5414
0
                          sizeof(session->subtype1) - 1);
5415
0
        }
5416
0
        (void)strncat(session->subtype1, field[1],
5417
0
                      sizeof(session->subtype1) - 1);
5418
0
    } else {
5419
        // else no room.  log it
5420
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
5421
0
                "NMEA0183: $PSTMVER: no room for: %s\n", field[1]);
5422
0
    }
5423
5424
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5425
0
            "NMEA0183: $PSTMVER: %s, %s\n",
5426
0
            session->subtype, session->subtype1);
5427
5428
0
    return mask;
5429
0
}
5430
5431
// Recommend Minimum Course Specific GPS/TRANSIT Data
5432
static gps_mask_t processRMC(int count, char *field[],
5433
                             struct gps_device_t *session)
5434
0
{
5435
    /*
5436
     * RMC,225446.33,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E,A*68
5437
     * 1     225446.33    Time of fix 22:54:46 UTC
5438
     * 2     A            Status of Fix:
5439
     *                     A = Autonomous, valid;
5440
     *                     V = invalid
5441
     * 3,4   4916.45,N    Latitude 49 deg. 16.45 min North
5442
     * 5,6   12311.12,W   Longitude 123 deg. 11.12 min West
5443
     * 7     000.5        Speed over ground, Knots
5444
     * 8     054.7        Course Made Good, True north
5445
     * 9     181194       Date of fix ddmmyy.  18 November 1994
5446
     * 10,11 020.3,E      Magnetic variation 20.3 deg East
5447
     * 12    A            FAA mode indicator (NMEA 2.3 and later)
5448
     *                     see faa_mode() for possible mode values
5449
     * 13    V            Nav Status (NMEA 4.1 and later)
5450
     *                     A = autonomous,
5451
     *                     D = differential,
5452
     *                     E = Estimated (DR),
5453
     *                     F = RTK Float
5454
     *                     M = Manual input mode
5455
     *                     N = No fix.  Not valid,
5456
     *                     P = High Precision Mode
5457
     *                     R = RTK Integer
5458
     *                     S = Simulator,
5459
     *                     V = Invalid
5460
     * *68        mandatory nmea_checksum
5461
     *
5462
     * SiRF chipsets don't return either Mode Indicator or magnetic variation.
5463
     */
5464
0
    gps_mask_t mask = ONLINE_SET;
5465
0
    char status = field[2][0];
5466
0
    int newstatus;
5467
5468
    /* As of Dec 2023, the regressions only have A, or V in field 2.
5469
     * NMEA says only A, and V are valid
5470
     */
5471
0
    switch (status) {
5472
0
    default:
5473
        // missing, never seen this case.
5474
0
        FALLTHROUGH
5475
0
    case 'V':
5476
        // Invalid
5477
0
        session->newdata.mode = MODE_NO_FIX;
5478
0
        if ('\0' == field[1][0] ||
5479
0
            '\0' ==  field[9][0]) {
5480
            /* No time available. That breaks cycle end detector
5481
             * Force report to bypass cycle detector and get report out.
5482
             * To handle Querks (Quectel) like this:
5483
             *  $GPRMC,,V,,,,,,,,,,N*53
5484
             */
5485
0
            memset(&session->nmea.date, 0, sizeof(session->nmea.date));
5486
0
            session->cycle_end_reliable = false;
5487
0
            mask |= REPORT_IS | TIME_SET;
5488
0
        }
5489
0
        mask |= STATUS_SET | MODE_SET;
5490
0
        break;
5491
0
    case 'A':
5492
        // Valid Fix
5493
        /*
5494
         * The MTK3301, Royaltek RGM-3800, and possibly other
5495
         * devices deliver bogus time values when the navigation
5496
         * warning bit is set.
5497
         */
5498
        /* The Meinberg GPS164 only outputs GPRMC.  Do set status
5499
         * so it can increment fixcnt.
5500
         */
5501
0
        if ('\0' != field[1][0] &&
5502
0
            9 < count &&
5503
0
            '\0' !=  field[9][0]) {
5504
0
            if (0 == merge_hhmmss(field[1], session) &&
5505
0
                0 == merge_ddmmyy(field[9], session)) {
5506
                // got a good data/time
5507
0
                mask |= TIME_SET;
5508
0
                register_fractional_time(field[0], field[1], session);
5509
0
            }
5510
0
        }
5511
        // else, no point to the time only case, no regressions with that
5512
5513
0
        if (0 == do_lat_lon(&field[3], &session->newdata)) {
5514
0
            newstatus = STATUS_GPS;
5515
0
            mask |= LATLON_SET;
5516
0
            if (MODE_2D >= session->lastfix.mode) {
5517
                /* we have at least a 2D fix
5518
                 * might cause blinking */
5519
0
                session->newdata.mode = MODE_2D;
5520
0
            } else if (MODE_3D == session->lastfix.mode) {
5521
                // keep the 3D, this may be cycle starter
5522
                // might cause blinking
5523
0
                session->newdata.mode = MODE_3D;
5524
0
            }
5525
0
        } else {
5526
0
            newstatus = STATUS_UNK;
5527
0
            session->newdata.mode = MODE_NO_FIX;
5528
0
        }
5529
0
        mask |= MODE_SET;
5530
0
        if ('\0' != field[7][0]) {
5531
0
            session->newdata.speed = safe_atof(field[7]) * KNOTS_TO_MPS;
5532
0
            mask |= SPEED_SET;
5533
0
        }
5534
0
        if ('\0' != field[8][0]) {
5535
0
            session->newdata.track = safe_atof(field[8]);
5536
0
            mask |= TRACK_SET;
5537
0
        }
5538
5539
        // get magnetic variation
5540
0
        if ('\0' != field[10][0] &&
5541
0
            '\0' != field[11][0]) {
5542
0
            session->newdata.magnetic_var = safe_atof(field[10]);
5543
5544
0
            switch (field[11][0]) {
5545
0
            case 'E':
5546
                // no change
5547
0
                break;
5548
0
            case 'W':
5549
0
                session->newdata.magnetic_var = -session->newdata.magnetic_var;
5550
0
                break;
5551
0
            default:
5552
                // huh?
5553
0
                session->newdata.magnetic_var = NAN;
5554
0
                break;
5555
0
            }
5556
0
            if (0 == isfinite(session->newdata.magnetic_var) ||
5557
0
                0.09 >= fabs(session->newdata.magnetic_var)) {
5558
                // some GPS set 0.0,E, or 0,w instead of blank
5559
0
                session->newdata.magnetic_var = NAN;
5560
0
            } else {
5561
0
                mask |= MAGNETIC_TRACK_SET;
5562
0
            }
5563
0
        }
5564
5565
0
        if (12 < count) {
5566
0
            if ('\0' != field[12][0]) {
5567
                // Have FAA mode indicator (NMEA 2.3 and later)
5568
0
                newstatus = faa_mode(field[12][0]);
5569
0
            }
5570
            /*
5571
             * Navigation Status
5572
             * If present, can not be NUL:
5573
             * S = Safe
5574
             * C = Caution
5575
             * U = Unsafe
5576
             * V = invalid.
5577
             *
5578
             * In the regressions, as of Dec 2023, field 13 is
5579
             * always 'V', and field 2 is always 'A'.  That seems
5580
             * like an invalid combination.... */
5581
0
            if (13 < count) {
5582
0
                if ('\0' != field[13][0]) {
5583
0
                    ;  // skip for now
5584
0
                }
5585
0
            }
5586
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
5587
0
                     "NMEA0183: RMC: status %s(%d) faa mode %s(%s) "
5588
0
                     "faa status %s\n",
5589
0
                     field[2], newstatus, field[12],
5590
0
                     char2str(field[12][0], c_faa_mode), field[13]);
5591
0
        }
5592
5593
        /*
5594
         * This copes with GPSes like the Magellan EC-10X that *only* emit
5595
         * GPRMC. In this case we set mode and status here so the client
5596
         * code that relies on them won't mistakenly believe it has never
5597
         * received a fix.
5598
         */
5599
0
        if (3 < session->gpsdata.satellites_used) {
5600
            // 4 sats used means 3D
5601
0
            session->newdata.mode = MODE_3D;
5602
0
        } else if (0 != isfinite(session->gpsdata.fix.altHAE) ||
5603
0
                   0 != isfinite(session->gpsdata.fix.altMSL)) {
5604
            /* we probably have at least a 3D fix
5605
             * this handles old GPS that do not report 3D */
5606
0
            session->newdata.mode = MODE_3D;
5607
0
        }
5608
0
        session->newdata.status = newstatus;
5609
0
        mask |= STATUS_SET | MODE_SET;
5610
0
    }
5611
5612
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5613
0
             "NMEA0183: RMC: ddmmyy=%s hhmmss=%s lat=%.2f lon=%.2f "
5614
0
             "speed=%.2f track=%.2f mode=%d var=%.1f status=%d\n",
5615
0
             field[9], field[1],
5616
0
             session->newdata.latitude,
5617
0
             session->newdata.longitude,
5618
0
             session->newdata.speed,
5619
0
             session->newdata.track,
5620
0
             session->newdata.mode,
5621
0
             session->newdata.magnetic_var,
5622
0
             session->newdata.status);
5623
0
    return mask;
5624
0
}
5625
5626
/* precessROT() - process Rate Of Turn
5627
 *
5628
 * Deprecated by NMEA in 2008
5629
 */
5630
static gps_mask_t processROT(int c UNUSED, char *field[],
5631
                             struct gps_device_t *session)
5632
0
{
5633
    /*
5634
     * $APROT,0.013,A*35
5635
     *
5636
     * 1) Rate of Turn deg/min
5637
     * 2) A = valid, V = Void
5638
     * )  checksum
5639
     *
5640
     */
5641
0
    gps_mask_t mask = ONLINE_SET;
5642
5643
0
    if ('\0' == field[1][0] ||
5644
0
        'A' != field[2][0]) {
5645
        // no data
5646
0
        return mask;
5647
0
    }
5648
5649
    // assume good data
5650
0
    session->gpsdata.attitude.rot = safe_atof(field[1]);
5651
0
    mask |= ATTITUDE_SET;
5652
5653
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5654
0
             "NMEA0183: $xxROT:Rate of Turn %f\n",
5655
0
             session->gpsdata.attitude.rot);
5656
0
    return mask;
5657
0
}
5658
5659
/*
5660
 * Unicore $SNRSTAT  Sensor status
5661
 * Note: Invalid sender: $SN
5662
 */
5663
static gps_mask_t processSNRSTAT(int count UNUSED, char *field[],
5664
                                struct gps_device_t *session)
5665
0
{
5666
    /*
5667
     * $SNRSTAT,1,1,0,0*5D
5668
     */
5669
5670
0
    gps_mask_t mask = ONLINE_SET;
5671
0
    static char probe[] = "$PDTINFO\r\n";
5672
0
    static char type[] = "Unicore";
5673
0
    int insstatus = atoi(field[1]);     // IMU status
5674
0
    int odostatus = atoi(field[2]);     // Odometer Status
5675
0
    int InstallState = atoi(field[3]);  // Install State
5676
0
    int mapstat = atoi(field[4]);       // PAP status
5677
5678
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5679
0
             "NMEA0183: SNRSTAT insstatus %d obsstatus %d InstallState %d "
5680
0
             "mapstat %d\n",
5681
0
             insstatus, odostatus, InstallState, mapstat);
5682
5683
0
    GPSD_LOG(LOG_IO, &session->context->errout,
5684
0
             "NMEA0183: SNRSTAT insstatus %s obsstatus %s InstallState %s "
5685
0
             "mapstat %s\n",
5686
0
             val2str(insstatus, vsnrstat_insstatus),
5687
0
             val2str(odostatus, vsnrstat_odostatus),
5688
0
             val2str(InstallState, vsnrstat_InstallState),
5689
0
             val2str(mapstat, vsnrstat_mapstat));
5690
5691
0
    if ('\0' == session->subtype[0]) {
5692
        // this is Unicore
5693
        // Send $PDTINFO to get back $PDTINFO,....
5694
0
        (void)gpsd_write(session, probe, sizeof(probe));
5695
        // mark so we don't ask twice
5696
0
        (void)strlcpy(session->subtype, type, sizeof(session->subtype) - 1);
5697
0
    }
5698
0
    return mask;
5699
0
}
5700
5701
5702
/*
5703
 * Skytraq undocumented debug sentences take this format:
5704
 * $STI,type,val*CS
5705
 * type is a 2 char subsentence type
5706
 * Note: NO checksum
5707
 */
5708
static gps_mask_t processSTI(int count, char *field[],
5709
                             struct gps_device_t *session)
5710
0
{
5711
0
    gps_mask_t mask = ONLINE_SET;
5712
5713
0
    if (0 != strncmp(session->device_type->type_name, "Skytraq", 7)) {
5714
        // this is skytraq, but marked yet, so probe for Skytraq
5715
        // Send MID 0x02, to get back MID 0x80
5716
0
        (void)gpsd_write(session, "\xA0\xA1\x00\x02\x02\x01\x03\x0d\x0a",9);
5717
0
    }
5718
5719
0
    if ( 0 == strcmp( field[1], "IC") ) {
5720
        // $STI,IC,error=XX, this is always very bad, but undocumented
5721
0
        GPSD_LOG(LOG_ERROR, &session->context->errout,
5722
0
                 "NMEA0183: Skytraq: $STI,%s,%s\n", field[1], field[2]);
5723
0
        return mask;
5724
0
    }
5725
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5726
0
             "NMEA0183: STI,%s: Unknown type, Count: %d\n", field[1], count);
5727
5728
0
    return mask;
5729
0
}
5730
5731
// SiRF Estimated Position Errors
5732
// $xxTHS -- True Heading and Status
5733
static gps_mask_t processTHS(int c UNUSED, char *field[],
5734
                             struct gps_device_t *session)
5735
0
{
5736
    /*
5737
     * $GNTHS,121.15.A*1F<CR><LF>
5738
     * 1  - Heading, degrees True
5739
     * 2  - Mode indicator
5740
     *      'A’ = Autonomous
5741
     *      'E’ = Estimated (dead reckoning)
5742
     *      'M’ = Manual input
5743
     *      'S’ = Simulator
5744
     *      'V’ = Data not valid
5745
     * 3  - Checksum
5746
     */
5747
0
    gps_mask_t mask = ONLINE_SET;
5748
0
    double heading;
5749
5750
0
    if ('\0' == field[1][0] ||
5751
0
        '\0' == field[2][0]) {
5752
        // no data
5753
0
        return mask;
5754
0
    }
5755
0
    if ('V' == field[2][0]) {
5756
        // invalid data
5757
        // ignore A, E, M and S for now
5758
0
        return mask;
5759
0
    }
5760
0
    heading = safe_atof(field[1]);
5761
0
    if ((0.0 > heading) ||
5762
0
        (360.0 < heading)) {
5763
        // bad data
5764
0
        return mask;
5765
0
    }
5766
5767
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5768
0
             "NMEA0183: $xxTHS heading %lf mode %s\n",
5769
0
             heading, field[2]);
5770
5771
0
    return mask;
5772
0
}
5773
5774
static gps_mask_t processTNTA(int c UNUSED, char *field[],
5775
                              struct gps_device_t *session)
5776
0
{
5777
    /*
5778
     * Proprietary sentence for iSync GRClok/LNRClok.
5779
5780
     $PTNTA,20000102173852,1,T4,,,6,1,0*32
5781
5782
     1. Date/time in format year, month, day, hour, minute, second
5783
     2. Oscillator quality 0:warming up, 1:freerun, 2:disciplined.
5784
     3. Always T4. Format indicator.
5785
     4. Interval ppsref-ppsout in [ns]. Blank if no ppsref.
5786
     5. Fine phase comparator in approx. [ns]. Always close to -500 or
5787
        +500 if not disciplined. Blank if no ppsref.
5788
     6. iSync Status.  0:warming up or no light, 1:tracking set-up,
5789
        2:track to PPSREF, 3:synch to PPSREF, 4:Free Run. Track OFF,
5790
        5:FR. PPSREF unstable, 6:FR. No PPSREF, 7:FREEZE, 8:factory
5791
        used, 9:searching Rb line
5792
     7. GPS messages indicator. 0:do not take account, 1:take account,
5793
        but no message, 2:take account, partially ok, 3:take account,
5794
        totally ok.
5795
     8. Transfer quality of date/time. 0:no, 1:manual, 2:GPS, older
5796
        than x hours, 3:GPS, fresh.
5797
5798
     */
5799
0
    gps_mask_t mask = ONLINE_SET;
5800
5801
0
    if (0 == strcmp(field[3], "T4")) {
5802
0
        struct oscillator_t *osc = &session->gpsdata.osc;
5803
0
        unsigned int quality = atoi(field[2]);
5804
0
        unsigned int delta = atoi(field[4]);
5805
0
        unsigned int fine = atoi(field[5]);
5806
0
        unsigned int status = atoi(field[6]);
5807
0
        char deltachar = field[4][0];
5808
5809
0
        osc->running = (0 < quality);
5810
0
        osc->reference = (deltachar && (deltachar != '?'));
5811
0
        if (osc->reference) {
5812
0
            if (500 > delta) {
5813
0
                osc->delta = fine;
5814
0
            } else {
5815
0
                osc->delta = ((delta < 500000000) ? delta : 1000000000 - delta);
5816
0
            }
5817
0
        } else {
5818
0
            osc->delta = 0;
5819
0
        }
5820
0
        osc->disciplined = ((quality == 2) && (status == 3));
5821
0
        mask |= OSCILLATOR_SET;
5822
5823
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
5824
0
                 "NMEA0183: PTNTA,T4: quality=%s, delta=%s, fine=%s,"
5825
0
                 "status=%s\n",
5826
0
                 field[2], field[4], field[5], field[6]);
5827
0
    }
5828
0
    return mask;
5829
0
}
5830
5831
static gps_mask_t processTNTHTM(int c UNUSED, char *field[],
5832
                                struct gps_device_t *session)
5833
0
{
5834
    /*
5835
     * Proprietary sentence for True North Technologies Magnetic Compass.
5836
     * This may also apply to some Honeywell units since they may have been
5837
     * designed by True North.
5838
5839
     $PTNTHTM,14223,N,169,N,-43,N,13641,2454*15
5840
5841
     HTM,x.x,a,x.x,a,x.x,a,x.x,x.x*hh<cr><lf>
5842
     Fields in order:
5843
     1. True heading (compass measurement + deviation + variation)
5844
     2. magnetometer status character:
5845
     C = magnetometer calibration alarm
5846
     L = low alarm
5847
     M = low warning
5848
     N = normal
5849
     O = high warning
5850
     P = high alarm
5851
     V = magnetometer voltage level alarm
5852
     3. pitch angle
5853
     4. pitch status character - see field 2 above
5854
     5. roll angle
5855
     6. roll status character - see field 2 above
5856
     7. dip angle
5857
     8. relative magnitude horizontal component of earth's magnetic field
5858
     *hh          mandatory nmea_checksum
5859
5860
     By default, angles are reported as 26-bit integers: weirdly, the
5861
     technical manual says either 0 to 65535 or -32768 to 32767 can
5862
     occur as a range.
5863
     */
5864
0
    gps_mask_t mask = ONLINE_SET;
5865
5866
    // True heading
5867
0
    session->gpsdata.attitude.heading = safe_atof(field[1]);
5868
0
    session->gpsdata.attitude.mag_st = *field[2];
5869
0
    session->gpsdata.attitude.pitch = safe_atof(field[3]);
5870
0
    session->gpsdata.attitude.pitch_st = *field[4];
5871
0
    session->gpsdata.attitude.roll = safe_atof(field[5]);
5872
0
    session->gpsdata.attitude.roll_st = *field[6];
5873
0
    session->gpsdata.attitude.dip = safe_atof(field[7]);
5874
0
    session->gpsdata.attitude.mag_x = safe_atof(field[8]);
5875
0
    mask |= (ATTITUDE_SET);
5876
5877
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
5878
0
             "NMEA0183: $PTNTHTM heading %lf (%c).\n",
5879
0
             session->gpsdata.attitude.heading,
5880
0
             session->gpsdata.attitude.mag_st);
5881
0
    return mask;
5882
0
}
5883
5884
// GPS Text message
5885
static gps_mask_t processTXT(int count, char *field[],
5886
                             struct gps_device_t *session)
5887
0
{
5888
    /*
5889
     * $GNTXT,01,01,01,PGRM inv format*2A
5890
     * 1                   Number of sentences for full data
5891
     * 1                   Sentence 1 of 1
5892
     * 01                  Message type
5893
     *       00 - error
5894
     *       01 - warning
5895
     *       02 - notice
5896
     *       07 - user
5897
     * PGRM inv format     ASCII text
5898
     *
5899
     * Can occur with talker IDs:
5900
     *   BD (Beidou),
5901
     *   GA (Galileo),
5902
     *   GB (Beidou),
5903
     *   GI (IRNSS
5904
     *   GL (GLONASS),
5905
     *   GN (GLONASS, any combination GNSS),
5906
     *   GP (GPS, SBAS, QZSS),
5907
     *   GQ (QZSS).
5908
     *   PQ (QZSS). Quectel Quirk
5909
     *   QZ (QZSS).
5910
     *
5911
     * Unicore undcumented:
5912
     *
5913
     * $GNTXT,01,01,01,0,500482,0000,80A0,80A0,-45.277,0*6B
5914
     * $GNTXT,01,01,02,0,00,10000,00,01,17,01,0001,0000,0.000*57
5915
     */
5916
0
    gps_mask_t mask = ONLINE_SET;
5917
0
    int msgType = 0;
5918
0
    char *msgType_txt = "Unknown";
5919
5920
0
    if (5 != count) {
5921
0
      return mask;
5922
0
    }
5923
5924
0
    msgType = atoi(field[3]);
5925
5926
0
    switch ( msgType ) {
5927
0
    case 0:
5928
0
        msgType_txt = "Error";
5929
0
        break;
5930
0
    case 1:
5931
0
        msgType_txt = "Warning";
5932
0
        break;
5933
0
    case 2:
5934
0
        msgType_txt = "Notice";
5935
0
        break;
5936
0
    case 7:
5937
0
        msgType_txt = "User";
5938
0
        break;
5939
0
    }
5940
5941
    // maximum text length unknown, guess 80
5942
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
5943
0
             "NMEA0183: TXT: %.10s: %.80s\n",
5944
0
             msgType_txt, field[4]);
5945
0
    return mask;
5946
0
}
5947
5948
/* process xxVTG
5949
 *     $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K
5950
 *     $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K,A
5951
 *
5952
 * where:
5953
 *         1,2     054.7,T      True track made good (degrees)
5954
 *         3,4     034.4,M      Magnetic track made good
5955
 *         5,6     005.5,N      Ground speed, knots
5956
 *         7,8     010.2,K      Ground speed, Kilometers per hour
5957
 *         9       A            Mode Indicator (optional)
5958
 *                                see faa_mode() for possible mode values
5959
 *
5960
 * see also:
5961
 * https://gpsd.gitlab.io/gpsd/NMEA.html#_vtg_track_made_good_and_ground_speed
5962
 */
5963
static gps_mask_t processVTG(int count,
5964
                             char *field[],
5965
                             struct gps_device_t *session)
5966
0
{
5967
0
    gps_mask_t mask = ONLINE_SET;
5968
5969
0
    if( (field[1][0] == '\0') || (field[5][0] == '\0')){
5970
0
        return mask;
5971
0
    }
5972
5973
    // ignore empty/missing field, fix mode of last resort
5974
0
    if ((9 < count) &&
5975
0
        ('\0' != field[9][0])) {
5976
5977
0
        switch (field[9][0]) {
5978
0
        case 'A':
5979
            // Autonomous, 2D or 3D fix
5980
0
            FALLTHROUGH
5981
0
        case 'D':
5982
            // Differential, 2D or 3D fix
5983
            // MODE_SET here causes issues
5984
            // mask |= MODE_SET;
5985
0
            break;
5986
0
        case 'E':
5987
            // Estimated, DR only
5988
0
            FALLTHROUGH
5989
0
        case 'N':
5990
            // Not Valid
5991
            // MODE_SET here causes issues
5992
            // mask |= MODE_SET;
5993
            // nothing to use here, leave
5994
0
            return mask;
5995
0
        default:
5996
            // Huh?
5997
0
            break;
5998
0
        }
5999
0
    }
6000
6001
    // set true track
6002
0
    session->newdata.track = safe_atof(field[1]);
6003
0
    mask |= TRACK_SET;
6004
6005
    // set magnetic variation
6006
0
    if ('\0' != field[3][0]) {  // ignore empty fields
6007
0
        session->newdata.magnetic_track = safe_atof(field[3]);
6008
0
        mask |= MAGNETIC_TRACK_SET;
6009
0
    }
6010
6011
0
    session->newdata.speed = safe_atof(field[5]) * KNOTS_TO_MPS;
6012
0
    mask |= SPEED_SET;
6013
6014
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
6015
0
             "NMEA0183: VTG: course(T)=%.2f, course(M)=%.2f, speed=%.2f",
6016
0
             session->newdata.track, session->newdata.magnetic_track,
6017
0
             session->newdata.speed);
6018
0
    return mask;
6019
0
}
6020
6021
/* precessXDR() - process transducer messages
6022
 */
6023
static gps_mask_t processXDR(int count, char *field[],
6024
                             struct gps_device_t *session)
6025
0
{
6026
    /*
6027
     * $APXDR,A,0.135,D,PTCH*7C
6028
     * $APXDR,A,3.861,D,ROLL*65
6029
     *
6030
     * 1) Transducer type
6031
     *     A = Angular Displacement
6032
     * 2) Measurement data
6033
     * 3) Units of measure
6034
     *     D = degrees
6035
     * 4) Transducer ID
6036
     *     can be repeated...
6037
     * )  checksum
6038
     *
6039
     * TODO: stacked measurements, like the TNT Revolution:
6040
  $HCXDR,A,177,D,PITCH,A,-40,D,ROLL,G,358,,MAGX,G,2432,,MAGY,G,-8974,,MAGZ*47
6041
     *  the bund_zeus:
6042
  $IIXDR,C,,C,AIRTEMP,A,-3.0,D,HEEL,A,3.7,D,TRIM,P,,B,BARO,A,-4.2,D,RUDDER*28
6043
     *
6044
     */
6045
0
    gps_mask_t mask = ONLINE_SET;
6046
0
    unsigned int i;
6047
0
    unsigned int num_meas = count / 4;
6048
6049
0
    for (i = 0; i < num_meas; i++) {
6050
0
        double data = 0.0;
6051
0
        unsigned int j = i * 4;
6052
6053
0
        if ('\0' == field[j + 2][0]) {
6054
            // no data, skip it
6055
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6056
0
                     "NMEA0183: $xxXDR: Type %.10s Data %.10s Units %.10s "
6057
0
                     "ID %.10s\n",
6058
0
                     field[j + 1], field[j + 2], field[j + 3], field[j + 4]);
6059
0
            continue;
6060
0
        }
6061
6062
0
        data = safe_atof(field[j + 2]);
6063
6064
0
        switch (field[j + 1][0]) {
6065
0
        case 'A':
6066
            // angles
6067
0
            if ('D' != field[j + 3][0]) {
6068
                // not degrees
6069
0
                continue;
6070
0
            }
6071
0
            if (0 == strncmp( "HEEL", field[j + 4], 10)) {
6072
                // session->gpsdata.attitude.roll = data;
6073
                // mask |= ATTITUDE_SET;
6074
0
            } else if (0 == strncmp( "PTCH", field[j + 4], 10) ||
6075
0
                0 == strncmp( "PITCH", field[j + 4], 10)) {
6076
0
                session->gpsdata.attitude.pitch = data;
6077
0
                mask |= ATTITUDE_SET;
6078
0
            } else if (0 == strncmp( "ROLL", field[j + 4], 10)) {
6079
0
                session->gpsdata.attitude.roll = data;
6080
0
                mask |= ATTITUDE_SET;
6081
0
            } else if (0 == strncmp( "RUDDER", field[j + 4], 10)) {
6082
                // session->gpsdata.attitude.roll = data;
6083
                // mask |= ATTITUDE_SET;
6084
0
            } else if (0 == strncmp( "TRIM", field[j + 4], 10)) {
6085
                // session->gpsdata.attitude.roll = data;
6086
                // mask |= ATTITUDE_SET;
6087
0
            }
6088
            // else, unknown
6089
0
            break;
6090
0
        case 'G':
6091
            // G: TODO: G,358,,MAGX,G,2432,,MAGY,G,-8974,,MAGZ*47
6092
            // oddly field[j + 3][0] is NUL...
6093
6094
0
            if (0 == strncmp( "MAGX", field[j + 4], 10)) {
6095
                // unknown scale
6096
0
                session->gpsdata.attitude.mag_x = data;
6097
0
                mask |= ATTITUDE_SET;
6098
0
            } else if (0 == strncmp( "MAGY", field[j + 4], 10)) {
6099
                // unknown scale
6100
0
                session->gpsdata.attitude.mag_y = data;
6101
0
                mask |= ATTITUDE_SET;
6102
0
            } else if (0 == strncmp( "MAGZ", field[j + 4], 10)) {
6103
                // unknown scale
6104
0
                session->gpsdata.attitude.mag_z = data;
6105
0
                mask |= ATTITUDE_SET;
6106
0
            }
6107
0
            break;
6108
0
        case 'C':
6109
            // C,,C,AIRTEMP,
6110
0
            FALLTHROUGH
6111
0
        case 'P':
6112
            // Pressure: TODO: P,,B,BARO
6113
0
            FALLTHROUGH
6114
0
        default:
6115
0
            break;
6116
0
        }
6117
6118
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
6119
0
                 "NMEA0183: $xxXDR: Type %.10s Data %f Units %.10s ID %.10s\n",
6120
0
                 field[j + 1], data, field[j + 3], field[j + 4]);
6121
0
    }
6122
0
    return mask;
6123
0
}
6124
6125
// Time & Date
6126
static gps_mask_t processZDA(int c UNUSED, char *field[],
6127
                               struct gps_device_t *session)
6128
0
{
6129
    /*
6130
     * $GPZDA,160012.71,11,03,2004,-1,00*7D
6131
     * 1) UTC time (hours, minutes, seconds, may have fractional subsecond)
6132
     * 2) Day, 01 to 31
6133
     * 3) Month, 01 to 12
6134
     * 4) Year (4 digits)
6135
     * 5) Local zone description, 00 to +- 13 hours
6136
     * 6) Local zone minutes description, apply same sign as local hours
6137
     * 7) Checksum
6138
     *
6139
     * Note: some devices, like the u-blox ANTARIS 4h, are known to ship ZDAs
6140
     * with some fields blank under poorly-understood circumstances (probably
6141
     * when they don't have satellite lock yet).
6142
     */
6143
0
    gps_mask_t mask = ONLINE_SET;
6144
0
    int year, mon, mday, century;
6145
0
    char ts_buf[TIMESPEC_LEN];
6146
6147
0
    if ('\0' == field[1][0] ||
6148
0
        '\0' == field[2][0] ||
6149
0
        '\0' == field[3][0] ||
6150
0
        '\0' == field[4][0]) {
6151
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6152
0
                 "NMEA0183: ZDA fields are empty\n");
6153
0
        return mask;
6154
0
    }
6155
6156
0
    if (0 != merge_hhmmss(field[1], session)) {
6157
        // bad time
6158
0
        return mask;
6159
0
    }
6160
6161
    /*
6162
     * We didn't register fractional time here because we wanted to leave
6163
     * ZDA out of end-of-cycle detection. Some devices sensibly emit it only
6164
     * when they have a fix, so watching for it can make them look
6165
     * like they have a variable fix reporting cycle.  But later thought
6166
     * was to not throw out good data because it is inconvenient.
6167
     */
6168
0
    mday = atoi(field[2]);
6169
0
    mon = atoi(field[3]);
6170
0
    year = atoi(field[4]);
6171
0
    century = year - year % 100;
6172
0
    if (1900 > year  ||
6173
0
        2200 < year) {
6174
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6175
0
                 "NMEA0183: malformed ZDA year: %s\n",  field[4]);
6176
0
    } else if (1 > mon ||
6177
0
               12 < mon) {
6178
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6179
0
                 "NMEA0183: malformed ZDA month: %s\n",  field[3]);
6180
0
    } else if (1 > mday ||
6181
0
               31 < mday) {
6182
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6183
0
                 "NMEA0183: malformed ZDA day: %s\n",  field[2]);
6184
0
    } else {
6185
0
        gpsd_century_update(session, century);
6186
0
        session->nmea.date.tm_year = year - 1900;
6187
0
        session->nmea.date.tm_mon = mon - 1;
6188
0
        session->nmea.date.tm_mday = mday;
6189
0
        session->newdata.time = gpsd_utc_resolve(session);
6190
0
        register_fractional_time(field[0], field[1], session);
6191
0
        mask = TIME_SET;
6192
0
    }
6193
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
6194
0
         "NMEA0183: ZDA time %s\n",
6195
0
          timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)));
6196
0
    return mask;
6197
0
}
6198
6199
6200
6201
/**************************************************************************
6202
 *
6203
 * Entry points begin here
6204
 *
6205
 **************************************************************************/
6206
6207
// parse an NMEA sentence, unpack it into a session structure
6208
gps_mask_t nmea_parse(char *sentence, struct gps_device_t * session)
6209
0
{
6210
0
    typedef gps_mask_t(*nmea_decoder) (int count, char *f[],
6211
0
                                       struct gps_device_t * session);
6212
0
    static struct
6213
0
    {
6214
0
        char *name;
6215
0
        char *name1;            // 2nd field to match, as is $PSTI,030
6216
0
        int nf;                 // minimum number of fields required to parse
6217
0
        bool cycle_continue;    // cycle continuer?
6218
0
        nmea_decoder decoder;
6219
0
    } nmea_phrase[NMEA_NUM] = {
6220
0
        {"PGLOR", NULL, 2,  false, processPGLOR},  // Android something...
6221
        // Ericsson firmware status
6222
0
        {"PERC", "FWsts", 6, false, processPERCFWsts},
6223
        // Ericsson averaged position
6224
0
        {"PERC", "GPavp", 6, false, processPERCGPavp},
6225
        // Ericsson control/heartbeat
6226
0
        {"PERC", "GPctr", 6, false, processPERCGPctr},
6227
        // Ericsson debug output
6228
0
        {"PERC", "GPdbg", 19, false, processPERCGPdbg},
6229
        // Ericsson oscillator phase/freq
6230
0
        {"PERC", "GPppf", 5, false, processPERCGPppf},
6231
        // Ericsson GPS time reference
6232
0
        {"PERC", "GPppr", 6, false, processPERCGPppr},
6233
        // Ericsson receiver health
6234
0
        {"PERC", "GPreh", 2, false, processPERCGPreh},
6235
        // Ericsson receiver status
6236
0
        {"PERC", "GPsts", 4, false, processPERCGPsts},
6237
        // Ericsson version info
6238
0
        {"PERC", "GPver", 4, false, processPERCGPver},
6239
0
        {"PGRMB", NULL, 0,  false, NULL},     // ignore Garmin DGPS Beacon Info
6240
0
        {"PGRMC", NULL, 0,  false, NULL},        // ignore Garmin Sensor Config
6241
0
        {"PGRME", NULL, 7,  false, processPGRME},
6242
0
        {"PGRMF", NULL, 15, false, processPGRMF},  // Garmin GPS Fix Data
6243
0
        {"PGRMH", NULL, 0,  false, NULL},     // ignore Garmin Aviation Height
6244
0
        {"PGRMI", NULL, 0,  false, NULL},          // ignore Garmin Sensor Init
6245
0
        {"PGRMM", NULL, 2,  false, processPGRMM},  // Garmin Map Datum
6246
0
        {"PGRMO", NULL, 0,  false, NULL},     // ignore Garmin Sentence Enable
6247
0
        {"PGRMT", NULL, 10, false, processPGRMT},  // Garmin Sensor Info
6248
0
        {"PGRMV", NULL, 4,  false, processPGRMV},  // Garmin 3D Velocity Info
6249
0
        {"PGRMZ", NULL, 4,  false, processPGRMZ},
6250
            /*
6251
             * Basic sentences must come after the PG* ones, otherwise
6252
             * Garmins can get stuck in a loop that looks like this:
6253
             *
6254
             * 1. A Garmin GPS in NMEA mode is detected.
6255
             *
6256
             * 2. PGRMC is sent to reconfigure to Garmin binary mode.
6257
             *    If successful, the GPS echoes the phrase.
6258
             *
6259
             * 3. nmea_parse() sees the echo as RMC because the talker
6260
             *    ID is ignored, and fails to recognize the echo as
6261
             *    PGRMC and ignore it.
6262
             *
6263
             * 4. The mode is changed back to NMEA, resulting in an
6264
             *    infinite loop.
6265
             */
6266
0
        {"AAM", NULL, 0,  false, NULL},    // ignore Waypoint Arrival Alarm
6267
0
        {"ACCURACY", NULL, 1,  true, processACCURACY},
6268
0
        {"ACN", NULL, 0,  false, NULL},    // Alert Command, 4.10+
6269
0
        {"ALC", NULL, 0,  false, NULL},    // Cyclic Alert List, 4.10+
6270
0
        {"ALF", NULL, 0,  false, NULL},    // Alert Sentence, 4.10+
6271
0
        {"ALM", NULL, 0,  false, NULL},    // GPS Almanac Data
6272
0
        {"APB", NULL, 0,  false, NULL},    // Autopilot Sentence B
6273
0
        {"ACF", NULL, 0,  false, NULL},    // Alert Command Refused, 4.10+
6274
0
        {"AVR", NULL, 0,  false, NULL},    // Same as $PTNL,AVR
6275
0
        {"BOD", NULL, 0,  false, NULL},    // Bearing Origin to Destination
6276
        // Bearing & Distance to Waypoint, Great Circle
6277
0
        {"BWC", NULL, 12, false, processBWC},
6278
0
        {"DBT", NULL, 7,  false, processDBT},  // depth
6279
0
        {"DPT", NULL, 4,  false, processDPT},  // depth
6280
0
        {"DTM", NULL, 2,  false, processDTM},  // datum
6281
0
        {"EPV", NULL, 0,  false, NULL},     // Command/report Prop Value, 4.10+
6282
0
        {"GBS", NULL, 7,  false, processGBS},  // GNSS Sat Fault Detection
6283
0
        {"GGA", NULL, 13, false, processGGA},  // GPS fix data
6284
0
        {"GGK", NULL, 0,  false, NULL},        // Same as $PTNL,GGK
6285
0
        {"GGQ", NULL, 0,  false, NULL},        // Leica Position
6286
0
        {"GLC", NULL, 0,  false, NULL},        // Geographic Position, LoranC
6287
0
        {"GLL", NULL, 7,  true, processGLL},   // Position, Lat/Lon
6288
0
        {"GMP", NULL, 0,  false, NULL},        // Map Projection
6289
0
        {"GNS", NULL, 13, false, processGNS},  // GNSS fix data
6290
0
        {"GRS", NULL, 4,  false, processGRS},  // GNSS Range Residuals
6291
0
        {"GSA", NULL, 18, false, processGSA},  // DOP and Active sats
6292
0
        {"GST", NULL, 8,  false, processGST},  // Pseudorange error stats
6293
0
        {"GSV", NULL, 4,  false, processGSV},  // Sats in view
6294
        // UNICORE MEMES sensor data
6295
0
        {"GYOACC", NULL, 14,  false, processGYOACC},
6296
        // Inertial Sense info, over long
6297
        // INFO,928404541,1.0.2.0,2.2.2.0,-377462659,2.0.0.0,-53643429,
6298
        // Inertial Sense Inc,2025-01-10,16:06:13.50,GPX -1,4,0, *7D
6299
0
        {"INFO", NULL, 14,  false, processINFO},
6300
0
        {"HCR", NULL, 0,  false, NULL},        // Heading Correction, 4.10+
6301
        // Heading, Deviation and Variation
6302
0
        {"HDG", NULL, 0,  false, processHDG},
6303
0
        {"HDM", NULL, 3,  false, processHDM},   // $APHDM, Magnetic Heading
6304
0
        {"HDT", NULL, 1,  false, processHDT},   // Heading true
6305
        // Hell Andle, Roll Period, Roll Amplitude.  NMEA 4.10+
6306
0
        {"HRM", NULL, 0,  false, NULL},
6307
0
        {"HRP", NULL, 0, false, NULL},       // Serpentrio Headinf, Roll, Pitch
6308
0
        {"HWBIAS", NULL, 0, false, NULL},       // Unknown HuaWei sentence
6309
0
        {"LLK", NULL, 0, false, NULL},          // Leica local pos and GDOP
6310
0
        {"LLQ", NULL, 0, false, NULL},          // Leica local pos and quality
6311
0
        {"MLA", NULL, 0,  false, NULL},         // GLONASS Almana Data
6312
0
        {"MOB", NULL, 0,  false, NULL},         // Man Overboard, NMEA 4.10+
6313
0
        {"MSS", NULL, 0,  false, NULL},         // beacon receiver status
6314
0
        {"MTW", NULL, 3,  false, processMTW},   // Water Temperature
6315
0
        {"MWD", NULL, 0,  false, processMWD},   // Wind Direction and Speed
6316
0
        {"MWV", NULL, 0,  false, processMWV},   // Wind Speed and Angle
6317
0
        {"OHPR", NULL, 18, false, NULL},        // Oceanserver, not supported
6318
0
        {"OSD", NULL, 0,  false, NULL},             // ignore Own Ship Data
6319
        // general handler for Ashtech
6320
0
        {"PASHR", NULL, 3, false, processPASHR},
6321
        // Airoha proprietary
6322
0
        {"PAIR001", NULL, 3, false, processPAIR001},  // ACK/NAK
6323
0
        {"PAIR010", NULL, 5, false, processPAIR010},  // Request Aiding
6324
6325
        // Unicore proprietary
6326
0
        {"PDTINFO", NULL, 6, false, processPDTINFO},  // Product ID
6327
6328
0
        {"PEMT", NULL, 5, false, NULL},               // Evermore proprietary
6329
        // Furuno proprietary
6330
0
        {"PERDACK", NULL, 4, false, NULL},            // ACK
6331
        // {"PERDAPI", NULL, 3, false, NULL},         // Config Send
6332
0
        {"PERDCRD", NULL, 15, false, NULL},           // NLOSMASK?
6333
0
        {"PERDCRG", "DCR", 6, false, NULL},           // QZSS DC report
6334
0
        {"PERDCRJ", "FREQ", 9, false, NULL},          // Jamming Status
6335
0
        {"PERDCRP", NULL, 9, false, NULL},            // Position
6336
0
        {"PERDCRQ", NULL, 11, false, NULL},           // Galileo SAR
6337
0
        {"PERDCRW", "TPS1", 8, false, NULL},          // Time
6338
0
        {"PERDCRX", "TPS2", 12, false, NULL},         // PPS
6339
0
        {"PERDCRY", "TPS3", 11, false, NULL},         // Position Mode
6340
0
        {"PERDCRZ", "TPS4", 13, false, NULL},         // GCLK
6341
0
        {"PERDMSG", NULL, 3, false, NULL},            // Message
6342
0
        {"PERDSYS", "ANTSEL", 5, false, NULL},        // Antenna
6343
0
        {"PERDSYS", "FIXSESSION", 5, false, NULL},    // Fix Session
6344
0
        {"PERDSYS", "GPIO", 3, false, NULL},          // GPIO
6345
0
        {"PERDSYS", "VERSION", 6, false, NULL},       // Version
6346
6347
        // Inertial Sense
6348
0
        {"PGPSP", NULL, 18,  false, processPGPSP},     // GPS nav data
6349
6350
        // Jackson Labs proprietary
6351
0
        {"PJLTS", NULL, 11,  false, NULL},            // GPSDO status
6352
0
        {"PJLTV", NULL, 4,  false, NULL},             // Time and 3D velocity
6353
        // GPS-320FW -- $PLCS
6354
0
        {"PMGNST", NULL, 8, false, processPMGNST},    // Magellan Status
6355
        // MediaTek proprietary, EOL.  Replaced by Airoha
6356
0
        {"PMTK001", NULL, 3, false, processPMTK001},  // ACK/NAK
6357
0
        {"PMTK010", NULL, 2, false, NULL},            // System Message
6358
0
        {"PMTK011", NULL, 2, false, NULL},            // Text Message
6359
0
        {"PMTK424", NULL, 3, false, processPMTK424},
6360
0
        {"PMTK705", NULL, 4, false, processPMTK705},
6361
        // MediaTek/Trimble Satellite Channel Status
6362
0
        {"PMTKCHN", NULL, 0, false, NULL},
6363
6364
        // MTK-3301 -- $POLYN
6365
6366
        // Quectel proprietary
6367
0
        {"PQTMCFGEINSMSGERROR", NULL, 1, false, processPQxERR},      // Error
6368
0
        {"PQTMCFGEINSMSGOK", NULL, 1, false, processPQxOK},          // OK
6369
0
        {"PQTMCFGORIENTATIONERROR", NULL, 1, false, processPQxERR},  // Error
6370
0
        {"PQTMCFGORIENTATION", NULL, 3, false, NULL},       // Orientation
6371
0
        {"PQTMCFGORIENTATIONOK", NULL, 1, false, processPQxOK},      // OK
6372
0
        {"PQTMCFGWHEELTICKERROR", NULL, 1, false, processPQxERR},    // Error
6373
0
        {"PQTMCFGWHEELTICKOK", NULL, 1, false, processPQxOK},        // OK
6374
0
        {"PQTMGPS", NULL, 14, false, processPQTMGPS},  // GPS Status
6375
0
        {"PQTMIMU", NULL, 10, false, processPQTMIMU},  // IMU Raw Data
6376
0
        {"PQTMINS", NULL, 11, false, processPQTMINS},  // INS Results
6377
0
        {"PQTMQMPTERROR", NULL, 1, false, processPQxERR},       // Error
6378
0
        {"PQTMQMPT", NULL, 2, false, NULL},            // Meters / tick
6379
0
        {"PQTMVEHMSG", NULL, 2, false, NULL},          // Vehicle Info
6380
0
        {"PQTMVER", NULL, 4, false, processPQTMVER},   // Firmware info
6381
6382
0
        {"PQVERNO", NULL, 5, false, processPQVERNO},   // Version
6383
        // smart watch sensors, Yes: space!
6384
0
        {"PRHS ", NULL, 2,  false, processPRHS},
6385
0
        {"PRWIZCH", NULL, 0, false, NULL},          // Rockwell Channel Status
6386
0
        {"PSRF140", NULL, 0, false, NULL},          // SiRF ephemeris
6387
0
        {"PSRF150", NULL, 0, false, NULL},          // SiRF flow control
6388
0
        {"PSRF151", NULL, 0, false, NULL},          // SiRF Power
6389
0
        {"PSRF152", NULL, 0, false, NULL},          // SiRF ephemeris
6390
0
        {"PSRF155", NULL, 0, false, NULL},          // SiRF proprietary
6391
0
        {"PSRFEPE", NULL, 7, false, processPSRFEPE},  // SiRF Estimated Errors
6392
6393
        /* Serpentrio
6394
         * $PSSN,HRP  -- Heading Pitch, Roll
6395
         * $PSSN,RBD  -- Rover-Base Direction
6396
         * $PSSN,RBP  -- Rover-Base Position
6397
         * $PSSN,RBV  -- Rover-Base Velocity
6398
         * $PSSN,SNC  -- NTRIP Client Status
6399
         * $PSSN,TFM  -- RTCM coordinate transform
6400
         */
6401
0
        {"PSSN", NULL, 0, false, NULL},          // $PSSN
6402
6403
        /*
6404
         * Skytraq sentences take this format:
6405
         * $PSTI,type[,val[,val]]*CS
6406
         * type is a 2 or 3 digit subsentence type
6407
         *
6408
         * Note: these sentences can be at least 105 chars long.
6409
         * That violates the NMEA 3.01 max of 82.
6410
         */
6411
        // 1 PPS Timing report ID
6412
0
        {"PSTI", "000", 4, false, NULL},
6413
        // Active Antenna Status Report
6414
0
        {"PSTI", "001", 2, false, NULL},
6415
        // GPIO 10 event-triggered time & position stamp.
6416
0
        {"PSTI", "005", 2, false, NULL},
6417
        //  Recommended Minimum 3D GNSS Data
6418
0
        {"PSTI", "030", 16, false, processPSTI030},
6419
        // RTK Baseline
6420
0
        {"PSTI", "032", 16, false, processPSTI032},
6421
        // RTK RAW Measurement Monitoring Data
6422
0
        {"PSTI", "033", 27, false,  processPSTI033},
6423
        // RTK Baseline Data of Rover Moving Base Receiver
6424
0
        {"PSTI", "035", 8, false, processPSTI035},
6425
        // Heading, Pitch and Roll Messages of vehicle
6426
0
        {"PSTI", "036", 2, false, processPSTI036},
6427
        // $PSTM ST Micro STA8088xx/STA8089xx/STA8090xx
6428
0
        {"PSTM", NULL, 0, false, NULL},
6429
        // STM messages
6430
0
        {"PSTMANTENNASTATUS", NULL, 4, false, processPSTMANTENNASTATUS},
6431
0
        {"PSTMVER", NULL, 1, false, processPSTMVER},
6432
6433
        /* Kongsberg Seatex AS. Seapath 320
6434
         * $PSXN,20,horiz-qual,hgt-qual,head-qual,rp-qual*csum
6435
         * $PSXN,21,event*csum
6436
         * $PSXN,22,gyro-calib,gyro-offs*csum
6437
         * $PSXN,23,roll,pitch,head,heave*csum
6438
         * $PSXN,24,roll-rate,pitch-rate,yaw-rate,vertical-vel*csum
6439
         */
6440
0
        {"PSXN", NULL, 0, false, NULL},
6441
0
        {"PTFTTXT", NULL, 0, false, NULL},        // unknown uptime
6442
6443
        /* Trimble Proprietary
6444
         * $PTNL,AVR
6445
         * $PTNL,GGK
6446
         */
6447
0
        {"PTNI", NULL, 0, false, NULL},
6448
6449
0
        {"PTKM", NULL, 0, false, NULL},           // Robertson RGC12 Gyro
6450
0
        {"PTNLRBA", NULL, 2, false, processPTNLRBA},  // Trimble/Ericsson antenna status
6451
0
        {"PTNLRHVR", NULL, 0, false, NULL},       // Trimble Software Version
6452
0
        {"PTNLRNM", NULL, 1, false, processPTNLRNM},  // Trimble receiver navigation mode
6453
0
        {"PTNLRPT", NULL, 0, false, NULL},        // Trimble Serial Port COnfig
6454
0
        {"PTNLRSVR", NULL, 0, false, NULL},       // Trimble Firmware Version
6455
0
        {"PTNLRTP", NULL, 3, false, processPTNLRTP},  // Trimble/Ericsson temperature
6456
0
        {"PTNLRXO", NULL, 2, false, processPTNLRXO},  // Trimble/Ericsson oscillator status
6457
0
        {"PTNLRZD", NULL, 0, false, NULL},        // Extended Time and Date
6458
0
        {"PTNTA", NULL, 8, false, processTNTA},
6459
0
        {"PTNTHTM", NULL, 9, false, processTNTHTM},
6460
0
        {"PUBX", NULL, 0, false, NULL},         // u-blox and Antaris
6461
0
        {"QSM", NULL, 3, false, NULL},          // QZSS DC Report
6462
0
        {"RBD", NULL, 0, false, NULL},       // Serpentrio rover-base direction
6463
0
        {"RBP", NULL, 0, false, NULL},       // Serpentrio rover-base position
6464
0
        {"RBV", NULL, 0, false, NULL},       // Serpentrio rover-base velocity
6465
0
        {"RLM", NULL, 0, false, NULL},       // Return Link Message, NMEA 4.10+
6466
        // ignore Recommended Minimum Navigation Info, waypoint
6467
0
        {"RMB", NULL, 0,  false, NULL},         // Recommended Min Nav Info
6468
0
        {"RMC", NULL, 8,  false, processRMC},   // Recommended Minimum Data
6469
0
        {"ROT", NULL, 3,  false, processROT},   // Rate of Turn
6470
0
        {"RPM", NULL, 0,  false, NULL},         // ignore Revolutions
6471
0
        {"RRT", NULL, 0, false, NULL},     // Report Route Transfer, NMEA 4.10+
6472
0
        {"RSA", NULL, 0,  false, NULL},         // Rudder Sensor Angle
6473
0
        {"RTE", NULL, 0,  false, NULL},         // ignore Routes
6474
        // UNICORE, Sensor Status invalid sender (SN)
6475
0
        {"SNRSTAT", NULL, 5,  false, processSNRSTAT},
6476
0
        {"SM1", NULL, 0, false, NULL},     // SafteyNET, All Ships, NMEA 4.10+
6477
0
        {"SM2", NULL, 0, false, NULL},     // SafteyNET, Coastal, NMEA 4.10+
6478
0
        {"SM3", NULL, 0, false, NULL},     // SafteyNET, Circular, NMEA 4.10+
6479
0
        {"SM4", NULL, 0, false, NULL},     // SafteyNET, Rectangular, NMEA 4.10+
6480
0
        {"SMB", NULL, 0, false, NULL},     // SafteyNET, Msg Body, NMEA 4.10+
6481
0
        {"SPW", NULL, 0, false, NULL},     // Security Password, NMEA 4.10+
6482
0
        {"SNC", NULL, 0, false, NULL},       // Serpentrio NTRIP client status
6483
0
        {"STI", NULL, 2,  false, processSTI},   // $STI  Skytraq
6484
0
        {"TFM", NULL, 0, false, NULL},          // Serpentrio Coord Transform
6485
0
        {"THS", NULL, 0,  false, processTHS},   // True Heading and Status
6486
0
        {"TRL", NULL, 0, false, NULL},     // AIS Xmit offline, NMEA 4.10+
6487
0
        {"TXT", NULL, 5,  false, processTXT},
6488
0
        {"TXTbase", NULL, 0,  false, NULL},     // RTCM 1029 TXT
6489
0
        {"VBW", NULL, 0,  false, NULL},         // Dual Ground/Water Speed
6490
0
        {"VDO", NULL, 0,  false, NULL},         // Own Vessel's Information
6491
0
        {"VDR", NULL, 0,  false, NULL},         // Set and Drift
6492
0
        {"VHW", NULL, 0,  false, NULL},         // Water Speed and Heading
6493
0
        {"VLW", NULL, 0,  false, NULL},         // Dual ground/water distance
6494
0
        {"VTG", NULL, 5,  false, processVTG},   // Course/speed over ground
6495
        // $APXDR, $HCXDR, Transducer measurements
6496
0
        {"XDR", NULL, 5,  false, processXDR},
6497
0
        {"XTE", NULL, 0,  false, NULL},         // Cross-Track Error
6498
0
        {"ZDA", NULL ,4,  false, processZDA},   // Time and Date
6499
0
        {NULL, NULL,  0,  false, NULL},         // no more
6500
0
    };
6501
6502
0
    int count;
6503
0
    gps_mask_t mask = 0;
6504
0
    unsigned i, thistag = 0, lasttag;
6505
0
    char *p, *e;
6506
0
    volatile char *t;
6507
0
    char ts_buf1[TIMESPEC_LEN];
6508
0
    char ts_buf2[TIMESPEC_LEN];
6509
0
    bool skytraq_sti = false;
6510
0
    size_t mlen;
6511
6512
    /*
6513
     * We've had reports that on the Garmin GPS-10 the device sometimes
6514
     * (1:1000 or so) sends garbage packets that have a valid checksum
6515
     * but are like 2 successive NMEA packets merged together in one
6516
     * with some fields lost.  Usually these are much longer than the
6517
     * legal limit for NMEA, so we can cope by just tossing out overlong
6518
     * packets.  This may be a generic bug of all Garmin chipsets.
6519
     */
6520
    // codacy does not like strlen()
6521
0
    mlen = strnlen(sentence, NMEA_MAX + 1);
6522
0
    if (NMEA_MAX < mlen) {
6523
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6524
0
                 "NMEA0183: Overlong packet of %zd+ chars rejected.\n",
6525
0
                 mlen);
6526
0
        return ONLINE_SET;
6527
0
    }
6528
6529
    // make an editable copy of the sentence
6530
0
    (void)strlcpy((char *)session->nmea.fieldcopy, sentence,
6531
0
                  sizeof(session->nmea.fieldcopy) - 1);
6532
    // discard the checksum part
6533
0
    for (p = (char *)session->nmea.fieldcopy;
6534
0
         ('*' != *p) && (' ' <= *p);) {
6535
0
        ++p;
6536
0
    }
6537
0
    if ('*' == *p) {
6538
0
        *p++ = ',';             // otherwise we drop the last field
6539
0
    }
6540
#ifdef SKYTRAQ_ENABLE_UNUSED
6541
    // $STI is special, no trailing *, or chacksum
6542
    if (0 != strncmp( "STI,", sentence, 4)) {
6543
        skytraq_sti = true;
6544
        *p++ = ',';             // otherwise we drop the last field
6545
    }
6546
#endif
6547
0
    *p = '\0';
6548
0
    e = p;
6549
6550
    // split sentence copy on commas, filling the field array
6551
0
    count = 0;
6552
0
    t = p;                      // end of sentence
6553
0
    p = (char *)session->nmea.fieldcopy + 1;  // beginning of tag, 'G' not '$'
6554
    // while there is a search string and we haven't run off the buffer...
6555
0
    while ((NULL != p) &&
6556
0
           (p <= t)) {
6557
0
        session->nmea.field[count] = p;      // we have a field. record it
6558
0
        if (NULL != (p = strchr(p, ','))) {  // search for the next delimiter
6559
0
            *p = '\0';                       // replace it with a NUL
6560
0
            count++;                         // bump the counters and continue
6561
0
            p++;
6562
0
        }
6563
0
    }
6564
6565
    // point remaining fields at empty string, just in case
6566
0
    for (i = (unsigned int)count; i < NMEA_MAX_FLD; i++) {
6567
0
        session->nmea.field[i] = e;
6568
0
    }
6569
6570
    // sentences handlers will tell us when they have fractional time
6571
0
    session->nmea.latch_frac_time = false;
6572
    // GSA and GSV will set this if more in that series to come.
6573
0
    session->nmea.gsx_more = false;
6574
6575
#ifdef __UNUSED
6576
    // debug
6577
    GPSD_LOG(0, &session->context->errout,
6578
             "NMEA0183: got %s\n", session->nmea.field[0]);
6579
#endif // __UNUSED
6580
6581
    // dispatch on field zero, the sentence tag
6582
0
    for (i = 0; i < NMEA_NUM; ++i) {
6583
0
        char *s = session->nmea.field[0];
6584
6585
        // CODACY #350416, wants explicit numeric end check
6586
0
        if ((NMEA_NUM - 1) <= i ||
6587
0
            NULL == nmea_phrase[i].name) {
6588
0
            mask = ONLINE_SET;
6589
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
6590
0
                     "NMEA0183: Unknown sentence type %s\n",
6591
0
                     session->nmea.field[0]);
6592
0
            break;
6593
0
        }
6594
        // strnlen() to shut up codacy
6595
0
        if (3 == strnlen(nmea_phrase[i].name, 4) &&
6596
0
            !skytraq_sti) {
6597
            // $STI is special
6598
0
            s += 2;             // skip talker ID
6599
0
        }
6600
0
        if (0 != strcmp(nmea_phrase[i].name, s)) {
6601
            // no match
6602
0
            continue;
6603
0
        }
6604
0
        if (NULL != nmea_phrase[i].name1 &&
6605
0
            0 != strcmp(nmea_phrase[i].name1, session->nmea.field[1])) {
6606
            // no match on field 2.  As in $PSTI,030,
6607
0
            continue;
6608
0
        }
6609
        // got a match
6610
0
        if (NULL == nmea_phrase[i].decoder) {
6611
            // no decoder for this sentence
6612
0
            mask = ONLINE_SET;
6613
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
6614
0
                     "NMEA0183: No decoder for sentence type %s\n",
6615
0
                     session->nmea.field[0]);
6616
0
            break;
6617
0
        }
6618
0
        if (count < nmea_phrase[i].nf) {
6619
            // sentence to short
6620
0
            mask = ONLINE_SET;
6621
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
6622
0
                     "NMEA0183: Sentence %s too short\n",
6623
0
                     session->nmea.field[0]);
6624
0
            break;
6625
0
        }
6626
0
        mask = (nmea_phrase[i].decoder)(count, session->nmea.field,
6627
0
                                        session);
6628
0
        session->nmea.cycle_continue = nmea_phrase[i].cycle_continue;
6629
        /*
6630
         * Must force this to be nz, as we're going to rely on a zero
6631
         * value to mean "no previous tag" later.
6632
         */
6633
        // FIXME: this fails on Skytrak, $PSTI,xx, many different xx
6634
0
        thistag = i + 1;
6635
0
        break;
6636
0
    }
6637
6638
    // prevent overaccumulation of sat reports
6639
0
    if (!str_starts_with(session->nmea.field[0] + 2, "GSV")) {
6640
        // This assumes all $xxGSV are contiguous.
6641
0
        if (0 != session->nmea.last_gsv_talker) {
6642
0
            session->nmea.end_gsv_talker = session->nmea.last_gsv_talker;
6643
0
        }
6644
0
        session->nmea.last_gsv_talker = '\0';
6645
0
    }
6646
0
    if (!str_starts_with(session->nmea.field[0] + 2, "GSA")) {
6647
0
        session->nmea.last_gsa_talker = '\0';
6648
0
    }
6649
6650
    // timestamp recording for fixes happens here
6651
0
    if (0 != (mask & TIME_SET)) {
6652
0
        if (0 == session->nmea.date.tm_year &&
6653
0
            0 == session->nmea.date.tm_mday) {
6654
            // special case to time zero
6655
0
            session->newdata.time = (timespec_t){0, 0};
6656
0
        } else {
6657
0
            session->newdata.time = gpsd_utc_resolve(session);
6658
0
        }
6659
6660
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
6661
0
                 "NMEA0183: %s newtime is %s = "
6662
0
                 "%d-%02d-%02dT%02d:%02d:%02d.%03ldZ\n",
6663
0
                 session->nmea.field[0],
6664
0
                 timespec_str(&session->newdata.time, ts_buf1, sizeof(ts_buf1)),
6665
0
                 1900 + session->nmea.date.tm_year,
6666
0
                 session->nmea.date.tm_mon + 1,
6667
0
                 session->nmea.date.tm_mday,
6668
0
                 session->nmea.date.tm_hour,
6669
0
                 session->nmea.date.tm_min,
6670
0
                 session->nmea.date.tm_sec,
6671
0
                 session->nmea.subseconds.tv_nsec / 1000000L);
6672
        /*
6673
         * If we have time and PPS is available, assume we have good time.
6674
         * Because this is a generic driver we don't really have enough
6675
         * information for a sharper test, so we'll leave it up to the
6676
         * PPS code to do its own sanity filtering.
6677
         */
6678
0
        mask |= NTPTIME_IS;
6679
0
    }
6680
6681
    /*
6682
     * The end-of-cycle detector.  This code depends on just one
6683
     * assumption: if a sentence with a timestamp occurs just before
6684
     * start of cycle, then it is always good to trigger a report on
6685
     * that sentence in the future.  For devices with a fixed cycle
6686
     * this should work perfectly, locking in detection after one
6687
     * cycle.  Most split-cycle devices (Garmin 48, for example) will
6688
     * work fine.  Problems will only arise if a a sentence that
6689
     * occurs just before timestamp increments also occurs in
6690
     * mid-cycle, as in the Garmin eXplorist 210; those might jitter.
6691
     */
6692
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
6693
0
             "NMEA0183: %s time %s last %s latch %d cont %d\n",
6694
0
             session->nmea.field[0],
6695
0
             timespec_str(&session->nmea.this_frac_time, ts_buf1,
6696
0
                          sizeof(ts_buf1)),
6697
0
             timespec_str(&session->nmea.last_frac_time, ts_buf2,
6698
0
                          sizeof(ts_buf2)),
6699
0
             session->nmea.latch_frac_time,
6700
0
             session->nmea.cycle_continue);
6701
0
    lasttag = session->nmea.lasttag;
6702
0
    if (session->nmea.gsx_more) {
6703
        // more to come, so ignore for cycle ender
6704
        // appears that GSA and GSV never start a cycle.
6705
0
    } else if (session->nmea.latch_frac_time) {
6706
0
        timespec_t ts_delta;
6707
0
        TS_SUB(&ts_delta, &session->nmea.this_frac_time,
6708
0
                          &session->nmea.last_frac_time);
6709
0
        if (0.01 < fabs(TSTONS(&ts_delta))) {
6710
            // time changed
6711
0
            mask |= CLEAR_IS;
6712
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6713
0
                     "NMEA0183: %s starts a reporting cycle. lasttag %d\n",
6714
0
                     session->nmea.field[0], lasttag);
6715
            /*
6716
             * Have we seen a previously timestamped NMEA tag?
6717
             * If so, designate as end-of-cycle marker.
6718
             * But not if there are continuation sentences;
6719
             * those get sorted after the last timestamped sentence
6720
             *
6721
             */
6722
0
            if (0 < lasttag &&
6723
0
                false == (session->nmea.cycle_enders[lasttag]) &&
6724
0
                !session->nmea.cycle_continue) {
6725
0
                session->nmea.cycle_enders[lasttag] = true;
6726
                // we might have a (somewhat) reliable end-of-cycle
6727
0
                session->cycle_end_reliable = true;
6728
0
                GPSD_LOG(LOG_PROG, &session->context->errout,
6729
0
                         "NMEA0183: tagged %s as a cycle ender. %u\n",
6730
0
                         nmea_phrase[lasttag - 1].name,
6731
0
                         lasttag);
6732
0
            }
6733
0
        }
6734
0
    } else {
6735
        // ignore multiple sequential, like GSV, GSA
6736
        // extend the cycle to an un-timestamped sentence?
6737
0
        if (true == session->nmea.cycle_enders[lasttag]) {
6738
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6739
0
                     "NMEA0183: %s is just after a cycle ender. (%s)\n",
6740
0
                     session->nmea.field[0],
6741
0
                     gps_maskdump(mask));
6742
0
            if (0 != (mask & ~ONLINE_SET)) {
6743
                // new data... after cycle ender
6744
0
                mask |= REPORT_IS;
6745
0
            }
6746
0
        }
6747
0
        if (session->nmea.cycle_continue) {
6748
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6749
0
                     "NMEA0183: %s extends the reporting cycle.\n",
6750
0
                     session->nmea.field[0]);
6751
            // change ender
6752
0
            session->nmea.cycle_enders[lasttag] = false;
6753
0
            session->nmea.cycle_enders[thistag] = true;
6754
            // have a cycle ender
6755
0
            session->cycle_end_reliable = true;
6756
0
        }
6757
0
    }
6758
6759
    // here's where we check for end-of-cycle
6760
0
    if ((session->nmea.latch_frac_time ||
6761
0
         session->nmea.cycle_continue) &&
6762
0
        (true == session->nmea.cycle_enders[thistag]) &&
6763
0
        !session->nmea.gsx_more) {
6764
0
        if (NULL == nmea_phrase[i].name1) {
6765
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6766
0
                     "NMEA0183: %s ends a reporting cycle.\n",
6767
0
                     session->nmea.field[0]);
6768
0
        } else {
6769
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6770
0
                     "NMEA0183: %s,%s ends a reporting cycle.\n",
6771
0
                     session->nmea.field[0],
6772
0
                     session->nmea.field[1]);
6773
0
        }
6774
0
        mask |= REPORT_IS;
6775
0
    }
6776
0
    if (session->nmea.latch_frac_time) {
6777
0
        session->nmea.lasttag = thistag;
6778
0
    }
6779
6780
    /* don't downgrade mode if holding previous fix
6781
     * usually because of xxRMC which does not report 2D/3D */
6782
0
    if (MODE_SET == (mask & MODE_SET) &&
6783
0
        MODE_3D == session->gpsdata.fix.mode &&
6784
0
        MODE_NO_FIX != session->newdata.mode &&
6785
0
        (0 != isfinite(session->lastfix.altHAE) ||
6786
0
         0 != isfinite(session->oldfix.altHAE) ||
6787
0
         0 != isfinite(session->lastfix.altMSL) ||
6788
0
         0 != isfinite(session->oldfix.altMSL))) {
6789
0
        session->newdata.mode = session->gpsdata.fix.mode;
6790
0
    }
6791
0
    return mask;
6792
0
}
6793
6794
6795
/* add NMEA checksum to a possibly terminated sentence
6796
 * if \0 terminated adds exactly 5 chars: "*XX\n\n"
6797
 * if *\0 terminated adds exactly 4 chars: "XX\n\n"
6798
 */
6799
void nmea_add_checksum(char *sentence)
6800
0
{
6801
0
    unsigned char sum = '\0';
6802
0
    char c, *p = sentence;
6803
6804
0
    if ('$' == *p ||
6805
0
        '!' == *p) {
6806
0
        p++;
6807
0
    }
6808
0
    while (('*' != (c = *p)) &&
6809
0
           ('\0' != c)) {
6810
0
        sum ^= c;
6811
0
        p++;
6812
0
    }
6813
0
    (void)snprintf(p, 6, "*%02X\r\n", (unsigned)sum);
6814
0
}
6815
6816
// ship a command to the GPS, adding * and correct checksum
6817
ssize_t nmea_write(struct gps_device_t *session, char *buf, size_t len UNUSED)
6818
0
{
6819
0
    (void)strlcpy(session->msgbuf, buf, sizeof(session->msgbuf));
6820
0
    if ('$' == session->msgbuf[0]) {
6821
0
        (void)strlcat(session->msgbuf, "*", sizeof(session->msgbuf));
6822
0
        nmea_add_checksum(session->msgbuf);
6823
0
    } else {
6824
0
        (void)strlcat(session->msgbuf, "\r\n", sizeof(session->msgbuf));
6825
0
    }
6826
    // codacy hates strlen()
6827
0
    session->msgbuflen = strnlen(session->msgbuf, sizeof(session->msgbuf));
6828
0
    return gpsd_write(session, session->msgbuf, session->msgbuflen);
6829
0
}
6830
6831
ssize_t nmea_send(struct gps_device_t * session, const char *fmt, ...)
6832
0
{
6833
0
    char buf[BUFSIZ];
6834
0
    va_list ap;
6835
6836
0
    va_start(ap, fmt);
6837
0
    (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap);
6838
0
    va_end(ap);
6839
    // codacy hates strlen()
6840
0
    return nmea_write(session, buf, strnlen(buf, sizeof(buf)));
6841
0
}
6842
6843
// vim: set expandtab shiftwidth=4