Coverage Report

Created: 2026-08-13 07:04

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
    unsigned 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
                         gnssid_t *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 = GNSSID_GPS;   // 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 = GNSSID_GPS;
600
0
                *ubx_svid = nmea_satnum;
601
0
            } else if (65 > nmea_satnum) {
602
                // 1 = SBAS      33-64
603
0
                *ubx_gnssid = GNSSID_SBAS;
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 = GNSSID_GAL;
608
0
                *ubx_svid = nmea_satnum - 100;
609
0
            } else if (152 > nmea_satnum) {
610
                // Huh?
611
0
                *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_SBAS;
617
0
                *ubx_svid = nmea_satnum;
618
0
            } else if (193 > nmea_satnum) {
619
                // Huh?
620
0
                *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_QZSS;
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 = GNSSID_BD;
631
0
                *ubx_svid = nmea_satnum - 200;
632
0
            } else {
633
                // Huh?
634
0
                *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_GLO;
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 = GNSSID_GAL;
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 = GNSSID_BD;
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 = GNSSID_QZSS;
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 = GNSSID_IRNSS;
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 = GNSSID_GAL;
726
0
                break;
727
0
            case 'B':
728
                // map Beidou IDs 1..37 to 401..437
729
0
                *ubx_gnssid = GNSSID_BD;
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 = GNSSID_IRNSS;
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 = GNSSID_GLO;
741
0
                break;
742
0
            case 'Q':
743
                // GQ, QZSS, 1 - 10
744
0
                nmea2_prn = 192 + nmea_satnum;
745
0
                *ubx_gnssid = GNSSID_QZSS;
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 = GNSSID_BD;
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 = GNSSID_BD;
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 = GNSSID_QZSS;
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 = GNSSID_SBAS;
788
0
        *ubx_svid = 87 + nmea_satnum;
789
0
    } else if (96 >= nmea_satnum) {
790
        // GLONASS 65..96
791
0
        *ubx_gnssid = GNSSID_GLO;
792
0
        *ubx_svid = nmea_satnum - 64;
793
0
    } else if (120 > nmea_satnum) {
794
        // Huh?
795
0
        *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_SBAS;
801
0
        *ubx_svid = nmea_satnum;
802
0
    } else if (173 > nmea_satnum) {
803
        // Huh?
804
0
        *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_IMES;
810
0
        *ubx_svid = nmea_satnum - 172;
811
0
    } else if (193 > nmea_satnum) {
812
        // Huh?
813
0
        *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_QZSS;
820
0
        *ubx_svid = nmea_satnum - 192;
821
0
    } else if (201 > nmea_satnum) {
822
        // Huh?
823
0
        *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_BD;
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 = GNSSID_GPS;
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 = GNSSID_GAL;
840
0
        *ubx_svid = nmea_satnum - 300;
841
0
    } else if (401 > nmea_satnum) {
842
        // Huh?
843
0
        *ubx_gnssid = GNSSID_GPS;
844
0
        *ubx_svid = 0;
845
0
        nmea2_prn = 0;
846
0
    } else if (437 >= nmea_satnum) {
847
        // BeiDou
848
0
        *ubx_gnssid = GNSSID_BD;
849
0
        *ubx_svid = nmea_satnum - 400;
850
0
    } else if (499 >= nmea_satnum) {
851
        // 438 to 500??
852
0
        *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_IRNSS;
858
0
        *ubx_svid = nmea_satnum - 500;
859
0
    } else if (900 >= nmea_satnum) {
860
        // 438 to 900??
861
0
        *ubx_gnssid = GNSSID_GPS;
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 = GNSSID_IRNSS;
867
0
        *ubx_svid = nmea_satnum - 900;
868
0
    } else {
869
        // greater than 437 Huh?
870
0
        *ubx_gnssid = GNSSID_GPS;
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(unsigned count 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(unsigned 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(unsigned count 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(unsigned count 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(unsigned count 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
    unsigned 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(unsigned count 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(unsigned count 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(unsigned 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(unsigned 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(unsigned 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(unsigned 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
        unsigned 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
            gnssid_t 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_visible,
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(unsigned 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 (9 > 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(unsigned 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
    unsigned n;
2180
0
    unsigned recnum;
2181
0
    unsigned char  nmea_sigid = 0;
2182
0
    int nmea_gnssid = 0;
2183
0
    unsigned char  ubx_sigid = 0;
2184
0
    int sane = 0;
2185
2186
0
    if (3 >= count) {
2187
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2188
0
                 "NMEA0183: %s, malformed - fieldcount %d <= 3\n",
2189
0
                 field[0], count);
2190
0
        gpsd_zero_satellites(&session->gpsdata);
2191
0
        return ONLINE_SET;
2192
0
    }
2193
0
    session->nmea.await = atoi(field[1]);
2194
0
    if (1 > session->nmea.await ||
2195
0
        10 < session->nmea.await) {
2196
        // numbeof sentences is either 0, or too many
2197
0
        session->nmea.await = 0;
2198
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2199
0
                 "NMEA0183: %s: bad number of sentences %d\n",
2200
0
                 field[0], session->nmea.await);
2201
0
        gpsd_zero_satellites(&session->gpsdata);
2202
0
        return ONLINE_SET;
2203
0
    }
2204
0
    session->nmea.part = atoi(field[2]);
2205
0
    if (1 > session->nmea.part ||
2206
0
        session->nmea.await < session->nmea.part) {
2207
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2208
0
                 "NMEA0183: %s: bad part %d of %d\n",
2209
0
                 field[0], session->nmea.part, session->nmea.await);
2210
0
        gpsd_zero_satellites(&session->gpsdata);
2211
0
        return ONLINE_SET;
2212
0
    }
2213
2214
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2215
0
             "NMEA0183: %s: part %s of %s, last_gsv_talker '%#x' "
2216
0
             " last_gsv_sigid %u\n",
2217
0
             field[0], field[2], field[1],
2218
0
             session->nmea.last_gsv_talker,
2219
0
             session->nmea.last_gsv_sigid);
2220
2221
    /*
2222
     * This check used to be !=0, but we have loosen it a little to let by
2223
     * NMEA 4.1 GSVs with an extra signal-ID field at the end.  Then loosen
2224
     * some more for Quectel  Querky $PQGSV.
2225
     */
2226
0
    switch (count % 4) {
2227
0
    case 0:
2228
        // normal, pre-NMEA 4.10
2229
0
        break;
2230
0
    case 1:
2231
        // NMEA 4.10, and later, get the signal ID
2232
0
        nmea_sigid = hex2uchar(field[count - 1][0]);
2233
0
        break;
2234
0
    case 2:
2235
        // Quectel Querk. $PQGSV, get the signal ID, and system ID
2236
0
        nmea_sigid = hex2uchar(field[count - 2][0]);
2237
0
        nmea_gnssid = atoi(field[count - 1]);
2238
0
        if (4 > nmea_gnssid ||
2239
0
            5 < nmea_gnssid) {
2240
            // Quectel says only 4 or 5
2241
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
2242
0
                     "NMEA0183: %sm invalid nmea_gnssid %d\n",
2243
0
                     field[0], nmea_gnssid);
2244
0
            return ONLINE_SET;
2245
0
        }
2246
0
        break;
2247
0
    default:
2248
        // bad count
2249
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2250
0
                 "NMEA0183: malformed %s - fieldcount(%d)\n",
2251
0
                 field[0], count);
2252
0
        gpsd_zero_satellites(&session->gpsdata);
2253
0
        return ONLINE_SET;
2254
0
    }
2255
2256
0
    if (1 == session->nmea.part) {
2257
        /*
2258
         * might have gone from GPGSV to GLGSV/BDGSV/QZGSV,
2259
         * in which case accumulate
2260
         *
2261
         * NMEA 4.1 might have gone from GPGVS,sigid=x to GPGSV,sigid=y
2262
         *
2263
         * Quectel EG25 can go GLGSV, PQGSV, GAGSV, GPGSV, in one cycle.
2264
         *
2265
         * session->nmea.last_gsv_talker is zero at cycle start
2266
         */
2267
0
        if ('\0' == session->nmea.last_gsv_talker) {
2268
            // Assume all xxGSV in same epoch.  Clear at 1st in eopoch.
2269
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
2270
0
                     "NMEA0183: %s: new part %d, last_gsv_talker '%#x', "
2271
0
                     "zeroing\n",
2272
0
                     field[0],
2273
0
                     session->nmea.part,
2274
0
                     session->nmea.last_gsv_talker);
2275
0
            gpsd_zero_satellites(&session->gpsdata);
2276
0
        }
2277
0
    }
2278
2279
0
    session->nmea.last_gsv_talker = GSV_TALKER;
2280
0
    switch (GSV_TALKER) {
2281
0
    case 'A':        // GA Galileo
2282
0
        nmea_gnssid = 3;
2283
        // Quectel LC79D can have sigid 6 (L1-A) and 1 (E5a)
2284
0
        session->nmea.seen_gagsv = true;
2285
0
        break;
2286
0
    case 'B':        // GB BeiDou
2287
0
        FALLTHROUGH
2288
0
    case 'D':        // BD BeiDou
2289
0
        nmea_gnssid = 4;
2290
0
        session->nmea.seen_bdgsv = true;
2291
0
        break;
2292
0
    case 'I':        // GI IRNSS
2293
0
        nmea_gnssid = 6;
2294
0
        session->nmea.seen_gigsv = true;
2295
0
        break;
2296
0
    case 'L':        // GL GLONASS
2297
0
        nmea_gnssid = 2;
2298
0
        session->nmea.seen_glgsv = true;
2299
0
        break;
2300
0
    case 'N':        // GN GNSS
2301
0
        session->nmea.seen_gngsv = true;
2302
0
        break;
2303
0
    case 'P':        // GP GPS
2304
0
        session->nmea.seen_gpgsv = true;
2305
0
        break;
2306
0
    case 'Q':        // $GQ, and $PQ (Quectel Querk) QZSS
2307
0
        if ('P' == field[0][0] &&
2308
0
            0 != nmea_gnssid) {
2309
            /* Quectel EC25 & EC21 use PQGSV for BeiDou or QZSS
2310
             * 4 = BeiDou, 5 = QZSS
2311
             * nmea_gnssid set above, what about seen?
2312
             */
2313
0
            if (4 == nmea_gnssid) {
2314
0
                session->nmea.seen_bdgsv = true;
2315
0
            } else if (5 == nmea_gnssid) {
2316
0
                session->nmea.seen_qzgsv = true;
2317
0
            } else {
2318
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
2319
0
                         "NMEA0183: %s: invalid nmea_gnssid %d\n",
2320
0
                         field[0], nmea_gnssid);
2321
0
                return ONLINE_SET;
2322
0
            }
2323
0
            break;
2324
0
        }
2325
        // else $GQ
2326
0
        FALLTHROUGH
2327
0
    case 'Z':        // QZ QZSS
2328
0
        nmea_gnssid = 5;
2329
0
        session->nmea.seen_qzgsv = true;
2330
0
        break;
2331
0
    default:
2332
        // uh, what?
2333
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2334
0
                 "NMEA0183: %s: unknown nmea_gnssid %d\n",
2335
0
                 field[0], nmea_gnssid);
2336
0
        break;
2337
0
    }
2338
2339
    // If NMEA 4.10, or later,then, or Quectel
2340
0
    if (0 != nmea_sigid) {
2341
        // get ubx sig_id from nmea_gnssid, nmea_sigid, get from talker ID
2342
0
        ubx_sigid = nmea_sigid_to_ubx(session, nmea_gnssid, nmea_sigid);
2343
0
    }
2344
0
    session->nmea.last_gsv_sigid = ubx_sigid;  // UNUSED
2345
2346
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2347
0
             "NMEA0183: %s: part %d of %d nmea_gnssid %d nmea_sigid %d "
2348
0
             "ubx_sigid %d\n",
2349
0
             field[0], session->nmea.part, session->nmea.await,
2350
0
             nmea_gnssid, nmea_sigid, ubx_sigid);
2351
2352
0
    for (recnum = 1; recnum < count / 4; recnum++) {
2353
0
        struct satellite_t *sp;
2354
0
        int nmea_svid;
2355
0
        unsigned fldnum = recnum * 4;
2356
2357
        // Quectel querk: EL and AZ, but no PRN...
2358
0
        if ('\0' == field[fldnum][0]) {
2359
0
            continue;
2360
0
        }
2361
        // mtk-3301 has PRN and ss, but no az or el
2362
0
        nmea_svid = atoi(field[fldnum]);
2363
0
        if (0 == nmea_svid) {
2364
            // skip bogus fields
2365
0
            GPSD_LOG(LOG_SHOUT, &session->context->errout,
2366
0
                     "NMEA0183: %s bad svid %d\n",
2367
0
                     field[0], nmea_svid);
2368
0
            continue;
2369
0
        }
2370
2371
0
        if (MAXCHANNELS <= session->gpsdata.satellites_visible) {
2372
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
2373
0
                     "NMEA0183: %s: error - too many satellites [%d]!\n",
2374
0
                     field[0], session->gpsdata.satellites_visible);
2375
0
            gpsd_zero_satellites(&session->gpsdata);
2376
0
            break;
2377
0
        }
2378
0
        sp = &session->gpsdata.skyview[session->gpsdata.satellites_visible];
2379
0
        sp->PRN = (short)nmeaid_to_prn(field[0], nmea_svid, nmea_gnssid,
2380
0
                                       &sp->gnssid, &sp->svid);
2381
2382
        // both al/az, or neither. ericsson-gru04 can report only el!
2383
0
        if ('\0' != field[fldnum + 1][0] &&
2384
0
            '\0' != field[fldnum + 2][0]) {
2385
0
            int el = atoi(field[fldnum + 1]);
2386
0
            int az = atoi(field[fldnum + 2]);
2387
0
            if (90 >= abs(el)) {
2388
0
                sp->elevation = (double)el;
2389
0
            }
2390
0
            if (360 == az) {
2391
0
                az = 0;
2392
0
            }
2393
0
            if (360 > az ||
2394
0
                0 <= az) {
2395
0
                sp->azimuth = (double)az;
2396
0
            }
2397
0
        }
2398
0
        if ('\0' != field[fldnum + 3][0]) {
2399
0
            int ss = atoi(field[fldnum + 3]);
2400
0
            sp->ss = (double)ss;
2401
0
        }
2402
0
        sp->used = false;
2403
0
        sp->sigid = ubx_sigid;
2404
2405
        /* sadly NMEA 4.1 does not tell us which sigid (L1, L2) is
2406
         * used.  So if the ss is zero, do not mark used */
2407
0
        if (0 < sp->PRN &&
2408
0
            0 < sp->ss) {
2409
0
            for (n = 0; n < MAXCHANNELS; n++) {
2410
0
                if (session->nmea.sats_used[n] == (unsigned short)sp->PRN) {
2411
0
                    sp->used = true;
2412
0
                    break;
2413
0
                }
2414
0
            }
2415
0
        }
2416
#if 0   // debug
2417
        GPSD_LOG(LOG_SHOUT, &session->context->errout,
2418
                 "NMEA0183: %s nmea_gnssid %d nmea_satnum %d ubx_gnssid %d "
2419
                 "ubx_svid %d nmea2_prn %d az %.1f el %.1f used %d\n",
2420
                 field[0], nmea_gnssid, nmea_svid, sp->gnssid, sp->svid,
2421
                 sp->PRN, sp->elevation, sp->azimuth, sp->used);
2422
#endif  // debug
2423
2424
        /*
2425
         * Incrementing this unconditionally falls afoul of chipsets like
2426
         * the Motorola Oncore GT+ that emit empty fields at the end of the
2427
         * last sentence in a GPGSV set if the number of satellites is not
2428
         * a multiple of 4.
2429
         */
2430
0
        session->gpsdata.satellites_visible++;
2431
0
    }
2432
2433
#if 0    // debug code
2434
    GPSD_LOG(LOG_SHOUT, &session->context->errout,
2435
        "NMEA0183: %s: vis %d bdgsv %d gagsv %d gigsv %d glgsv %d "
2436
        "gngsv %d qpgsv %d qzgsv %d\n",
2437
        field[0],
2438
        session->gpsdata.satellites_visible,
2439
        session->nmea.seen_bdgsv,
2440
        session->nmea.seen_gagsv,
2441
        session->nmea.seen_gigsv,
2442
        session->nmea.seen_glgsv,
2443
        session->nmea.seen_gngsv,
2444
        session->nmea.seen_gpgsv,
2445
        session->nmea.seen_qzgsv);
2446
#endif  // debug
2447
2448
#if 0
2449
    /*
2450
     * Alas, we can't sanity check field counts when there are multiple sat
2451
     * pictures, because the visible member counts *all* satellites - you
2452
     * get a bad result on the second and later SV spans.  Note, this code
2453
     * assumes that if any of the special sat pics occur they come right
2454
     * after a stock GPGSV one.
2455
     *
2456
     * FIXME: Add per-talker totals so we can do this check properly.
2457
     */
2458
    if (!(session->nmea.seen_bdgsv ||
2459
          session->nmea.seen_gagsv ||
2460
          session->nmea.seen_gigsv ||
2461
          session->nmea.seen_glgsv ||
2462
          session->nmea.seen_gngsv ||
2463
          session->nmea.seen_qzgsv)) {
2464
        if (session->nmea.part == session->nmea.await
2465
                && atoi(field[3]) != session->gpsdata.satellites_visible) {
2466
            GPSD_LOG(LOG_WARN, &session->context->errout,
2467
                     "NMEA0183: %s field 3 value of %d != actual count %d\n",
2468
                     field[0], atoi(field[3]),
2469
                     session->gpsdata.satellites_visible);
2470
        }
2471
    }
2472
#endif    // FIXME
2473
2474
    // not valid data until we've seen a complete set of parts
2475
0
    if (session->nmea.part < session->nmea.await) {
2476
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
2477
0
                 "NMEA0183: %s: Partial satellite data (%d of %d).\n",
2478
0
                 field[0], session->nmea.part, session->nmea.await);
2479
0
        session->nmea.gsx_more = true;
2480
0
        return ONLINE_SET;
2481
0
    }
2482
0
    session->nmea.gsx_more = false;
2483
0
    if (MAXCHANNELS < session->gpsdata.satellites_visible) {
2484
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2485
0
                "NMEA0183: %s too many satellites %d\n", field[0],
2486
0
                 session->gpsdata.satellites_visible);
2487
0
        session->gpsdata.satellites_visible = MAXCHANNELS;
2488
0
    }
2489
    /*
2490
     * This sanity check catches an odd behavior of SiRFstarII receivers.
2491
     * When they can't see any satellites at all (like, inside a
2492
     * building) they sometimes cough up a hairball in the form of a
2493
     * GSV packet with all the azimuth entries 0 (but nonzero
2494
     * elevations).  This behavior was observed under SiRF firmware
2495
     * revision 231.000.000_A2.
2496
     */
2497
0
    sane = 0;
2498
0
    for (n = 0; n < (unsigned)session->gpsdata.satellites_visible; n++) {
2499
0
        if (0 != session->gpsdata.skyview[n].azimuth) {
2500
0
            sane = 1;
2501
0
            break;
2502
0
        }
2503
0
    }
2504
2505
0
    if (0 == sane) {
2506
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
2507
0
                 "NMEA0183: %s: Satellite data no good (%d of %d).\n",
2508
0
                 field[0], session->nmea.part, session->nmea.await);
2509
0
        gpsd_zero_satellites(&session->gpsdata);
2510
0
        return ONLINE_SET;
2511
0
    }
2512
2513
0
    session->gpsdata.skyview_time.tv_sec = 0;
2514
0
    session->gpsdata.skyview_time.tv_nsec = 0;
2515
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2516
0
             "NMEA0183: %s: Satellite data OK (%d of %d).\n",
2517
0
             field[0], session->nmea.part, session->nmea.await);
2518
2519
    /* assumes GLGSV or BDGSV group, if present, is emitted after the GPGSV
2520
     * An assumption that Quectel breaks;  The EG25 can send in one epoch:
2521
     * $GLGSV, $PQGSV, $GAGSV, then $GPGSV! */
2522
0
    if ((session->nmea.seen_bdgsv ||
2523
0
         session->nmea.seen_gagsv ||
2524
0
         session->nmea.seen_gigsv ||
2525
0
         session->nmea.seen_glgsv ||
2526
0
         session->nmea.seen_gngsv ||
2527
0
         session->nmea.seen_qzgsv) &&
2528
0
        ('P' == GSV_TALKER &&
2529
0
         'P' != session->nmea.end_gsv_talker)) {
2530
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
2531
0
                 "NMEA0183: %s: not end talker %d\n",
2532
0
                 field[0], session->nmea.end_gsv_talker);
2533
0
        return ONLINE_SET;
2534
0
    }
2535
2536
#if 0   // debug code
2537
    {
2538
        char ts_buf[TIMESPEC_LEN];
2539
        char ts_buf1[TIMESPEC_LEN];
2540
        GPSD_LOG(LOG_SHOUT, &session->context->errout,
2541
            "NMEA0183: %s: set skyview_time %s frac_time %s\n",
2542
            field[0],
2543
            timespec_str(&session->gpsdata.skyview_time, ts_buf,
2544
                         sizeof(ts_buf)),
2545
            timespec_str(&session->nmea.this_frac_time, ts_buf1,
2546
                         sizeof(ts_buf1)));
2547
    }
2548
#endif  // debug
2549
2550
0
    return SATELLITE_SET;
2551
0
#undef GSV_TALKER
2552
0
}
2553
2554
/*
2555
 * Unicore $GYOACC  MEMS Sensor DAta
2556
 * Note: Invalid sender: $GY
2557
 */
2558
static gps_mask_t processGYOACC(unsigned count UNUSED, char *field[],
2559
                                struct gps_device_t *session)
2560
0
{
2561
    /*
2562
     * $GYOACC,050624,002133.10,0.004634,0.000273,0.004348,100,-4.666065,
2563
     *    -3.466573,7.960348,100,31,0,100,0*02
2564
     */
2565
0
    gps_mask_t mask = ONLINE_SET;
2566
0
    double gyroX = safe_atof(field[3]);       // deg/s
2567
0
    double gyroY = safe_atof(field[4]);       // deg/s
2568
0
    double gyroZ = safe_atof(field[5]);       // deg/s
2569
0
    unsigned gyroPeriod = atoi(field[6]);     // period in ms
2570
0
    double accX = safe_atof(field[7]);        // m/s^2
2571
0
    double accY = safe_atof(field[8]);        // m/s^2
2572
0
    double accZ = safe_atof(field[9]);        // m/s^2
2573
0
    unsigned accPeriod = atoi(field[10]);     // period in ms
2574
0
    int temp = atoi(field[11]);               // temperature C
2575
0
    unsigned speed = atoi(field[12]);         // pulses
2576
0
    unsigned pulsePeriod = atoi(field[13]);   // period in ms
2577
0
    unsigned fwd = atoi(field[14]);           // 0 == forward, 1 == reverse
2578
0
    struct tm date = {0};
2579
0
    timespec_t ts = {0};
2580
2581
    // Not at the same rate at the GNSS epoch. So do not use session->nmea
2582
0
    if (0 == decode_hhmmss(&date, &ts.tv_nsec, field[2], session) &&
2583
0
        0 == decode_ddmmyy(&date, field[1], session)) {
2584
2585
0
        session->gpsdata.attitude.mtime.tv_sec = mkgmtime(&date);
2586
0
        session->gpsdata.attitude.mtime.tv_nsec = ts.tv_nsec;
2587
0
    } else {
2588
0
        session->gpsdata.attitude.mtime.tv_sec = 0;
2589
0
        session->gpsdata.attitude.mtime.tv_nsec = 0;
2590
0
    }
2591
2592
0
    session->gpsdata.attitude.gyro_x = gyroX;
2593
0
    session->gpsdata.attitude.gyro_y = gyroY;
2594
0
    session->gpsdata.attitude.gyro_z = gyroZ;
2595
0
    session->gpsdata.attitude.acc_x = accX;
2596
0
    session->gpsdata.attitude.acc_y = accY;
2597
0
    session->gpsdata.attitude.acc_z = accZ;
2598
0
    mask |= ATTITUDE_SET;
2599
2600
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2601
0
             "NMEA0183: $GYOACC time %lld.%09lld "
2602
0
             "gyro X %.6f Y %.6f Z %.6f per %u "
2603
0
             "acc X %.6f Y %.6f Z %.6f per %u "
2604
0
             "temp %d speed %u per %u fwd %u\n",
2605
0
             (long long)session->gpsdata.attitude.mtime.tv_sec,
2606
0
             (long long)session->gpsdata.attitude.mtime.tv_nsec,
2607
0
             gyroX, gyroY, gyroZ, gyroPeriod,
2608
0
             accX, accY, accZ, accPeriod,
2609
0
             temp, speed, pulsePeriod, fwd);
2610
0
    return mask;
2611
0
}
2612
2613
static gps_mask_t processHDG(unsigned count UNUSED, char *field[],
2614
                             struct gps_device_t *session)
2615
0
{
2616
    /*
2617
     *  $SDHDG,234.6,,,1.3,E*34
2618
     *
2619
     *  $--HDG,h.h,d.d,a,v.v,a*hh<CR><LF>
2620
     *  Magnetic sensor heading, degrees
2621
     *  Magnetic deviation, degrees E/W
2622
     *  Magnetic variation, degrees, E/W
2623
     *
2624
     *  1. To obtain Magnetic Heading:
2625
     *  Add Easterly deviation (E) to Magnetic Sensor Reading
2626
     *  Subtract Westerly deviation (W) from Magnetic Sensor Reading
2627
     *  2. To obtain True Heading:
2628
     *  Add Easterly variation (E) to Magnetic Heading
2629
     *  Subtract Westerly variation (W) from Magnetic Heading
2630
     *  3. Variation and deviation fields shall be null fields if unknown.
2631
     */
2632
2633
0
    gps_mask_t mask = ONLINE_SET;
2634
0
    double sensor_heading;
2635
0
    double magnetic_deviation;
2636
2637
0
    if ('\0' == field[1][0]) {
2638
        // no data
2639
0
        return mask;
2640
0
    }
2641
0
    sensor_heading = safe_atof(field[1]);
2642
0
    if ((0.0 > sensor_heading) ||
2643
0
        (360.0 < sensor_heading)) {
2644
        // bad data */
2645
0
        return mask;
2646
0
    }
2647
0
    magnetic_deviation = safe_atof(field[2]);
2648
0
    if ((0.0 > magnetic_deviation) ||
2649
0
        (360.0 < magnetic_deviation)) {
2650
        // bad data
2651
0
        return mask;
2652
0
    }
2653
0
    switch (field[2][0]) {
2654
0
    case 'E':
2655
0
        sensor_heading += magnetic_deviation;
2656
0
        break;
2657
0
    case 'W':
2658
0
        sensor_heading += magnetic_deviation;
2659
0
        break;
2660
0
    default:
2661
        // ignore
2662
0
        break;
2663
0
    }
2664
2665
    // good data
2666
0
    session->newdata.magnetic_track = sensor_heading;
2667
0
    mask |= MAGNETIC_TRACK_SET;
2668
2669
    // get magnetic variation
2670
0
    if ('\0' != field[3][0] &&
2671
0
        '\0' != field[4][0]) {
2672
0
        session->newdata.magnetic_var = safe_atof(field[3]);
2673
2674
0
        switch (field[4][0]) {
2675
0
        case 'E':
2676
            // no change
2677
0
            mask |= MAGNETIC_TRACK_SET;
2678
0
            break;
2679
0
        case 'W':
2680
0
            session->newdata.magnetic_var = -session->newdata.magnetic_var;
2681
0
            mask |= MAGNETIC_TRACK_SET;
2682
0
            break;
2683
0
        default:
2684
            // huh?
2685
0
            session->newdata.magnetic_var = NAN;
2686
0
            break;
2687
0
        }
2688
0
    }
2689
2690
2691
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2692
0
             "NMEA0183: $SDHDG heading %lf var %.1f\n",
2693
0
             session->newdata.magnetic_track,
2694
0
             session->newdata.magnetic_var);
2695
0
    return mask;
2696
0
}
2697
2698
/* precessHDM() - process magnetic headingxxHDM messages
2699
 *
2700
 * Deprecated by NMEA in 2008
2701
 */
2702
static gps_mask_t processHDM(unsigned count UNUSED, char *field[],
2703
                             struct gps_device_t *session)
2704
0
{
2705
    /*
2706
     * $APHDM,218.634,M*39
2707
     *
2708
     * 1) Magnetic heading
2709
     * 2) M == Magnetic
2710
     * )  checksum
2711
     *
2712
     */
2713
0
    gps_mask_t mask = ONLINE_SET;
2714
2715
0
    if ('\0' == field[1][0]) {
2716
        // no data
2717
0
        return mask;
2718
0
    }
2719
2720
    // assume good data
2721
0
    session->gpsdata.attitude.mheading = safe_atof(field[1]);
2722
0
    mask |= ATTITUDE_SET;
2723
2724
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2725
0
             "NMEA0183: $xxHDM: Magnetic heading %f\n",
2726
0
             session->gpsdata.attitude.mheading);
2727
0
    return mask;
2728
0
}
2729
2730
static gps_mask_t processHDT(unsigned count UNUSED, char *field[],
2731
                             struct gps_device_t *session)
2732
0
{
2733
    /*
2734
     * $HEHDT,341.8,T*21
2735
     *
2736
     * $xxHDT,x.x*hh<cr><lf>
2737
     *
2738
     * The only data field is true heading in degrees.
2739
     * The following field is required to be 'T' indicating a true heading.
2740
     * It is followed by a mandatory nmea_checksum.
2741
     */
2742
0
    gps_mask_t mask = ONLINE_SET;
2743
0
    double heading;
2744
2745
0
    if ('\0' == field[1][0]) {
2746
        // no data
2747
0
        return mask;
2748
0
    }
2749
0
    heading = safe_atof(field[1]);
2750
0
    if (0.0 > heading ||
2751
0
        360.0 < heading) {
2752
        // bad data
2753
0
        return mask;
2754
0
    }
2755
    // True heading
2756
0
    session->gpsdata.attitude.heading = heading;
2757
2758
0
    mask |= ATTITUDE_SET;
2759
2760
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2761
0
             "NMEA0183: $xxHDT heading %lf.\n",
2762
0
             session->gpsdata.attitude.heading);
2763
0
    return mask;
2764
0
}
2765
2766
/* $INFO, Inertial Sense product info
2767
 * Not a legal NMEA message name
2768
 * https://docs.inertialsense.com/user-manual/com-protocol/nmea/#info
2769
 */
2770
static gps_mask_t processINFO(unsigned count UNUSED, char *field[],
2771
                              struct gps_device_t *session)
2772
0
{
2773
    /*
2774
     * $INFO,928404541,1.0.2.0,2.2.2.0,-377462659,2.0.0.0,-53643429,
2775
     *    Inertial Sense Inc,2025-01-10,16:06:13.50,GPX -1,4,0, *7D
2776
     *
2777
     * 1  Serial number    Manufacturer serial number
2778
     * 2  Hardware version Hardware version
2779
     * 3  Firmware version Firmware version
2780
     * 4  Build number     Firmware build number
2781
     * 5  Protocol version Communications protocol version
2782
     * 6  Repo revision    Repository revision number
2783
     * 7  Manufacturer     Manufacturer name
2784
     * 8  Build date       Build date
2785
     * 9  Build time       Build time
2786
     * 10 Add Info         Additional information
2787
     * 11 Hardware         Hardware: 1=uINS, 2=EVB, 3=IMX, 4=GPX
2788
     * 12 Reserved         Reserved for internal purpose.
2789
     * 13 Build type       Build type:
2790
     *  'a'=ALPHA, 'b'=BETA, 'c'=RELEASE CANDIDATE, 'r'=PRODUCTION RELEASE,
2791
     *  'd'=debug, ' '= ????
2792
     */
2793
2794
    // hardwaare
2795
0
    static struct clist_t hardware[] = {
2796
0
        {'1', "uISN"},
2797
0
        {'2', "EVB"},
2798
0
        {'3', "INX"},
2799
0
        {'4', "GPX"},
2800
0
        {'\0', NULL},
2801
0
    };
2802
2803
0
    if ('\0' == session->subtype[0] &&
2804
0
        !session->context->passive) {
2805
        // first time seen, send init
2806
2807
0
        (void)nmea_send(session, "$STPC");   // stop all messages
2808
2809
        /* Enable all possible NMEA messages, at 1Hz
2810
         * 1 PIMU, 2 PPIMU, 3 PRIMU, 4 PINS1, 5 PINS2
2811
         * 6 PGPSP, 7 GGA, 8 GLL, 9 GSA, 10 RMC, 11 ZDA, 12 PASHR
2812
         * 13 PSTRB, 14 INFO, 15 GSV, 16 VTG
2813
         * there are many more...
2814
         */
2815
0
        (void)nmea_send(session,
2816
0
                        "$ASCE,0,"    // Set current port
2817
0
                        "1,0,"        // PIMU
2818
0
                        "2,0,"        // PPIMU
2819
0
                        "3,0,"        // PRIMU
2820
0
                        "4,0,"        // PINS1
2821
0
                        "5,0,"        // PINS2
2822
0
                        "6,5,"        // PGPSP
2823
0
                        "7,5,"        // GGA
2824
0
                        "8,5,"        // GLL
2825
0
                        "9,5,"        // GSA
2826
0
                        "10,5,"       // RMC
2827
0
                        "11,5,"       // ZDA
2828
0
                        "12,5,"       // PASHR
2829
0
                        "13,5,"       // PSTRB
2830
0
                        "14,0,"       // INFO
2831
0
                        "15,5,"       // GSV
2832
0
                        "16,5,"       // VTG
2833
0
                        "17,5,"       // ?
2834
0
                        "18,5");      // ?
2835
0
    }
2836
2837
    // save serial number
2838
0
    strlcpy(session->gpsdata.dev.sernum, field[1],
2839
0
            sizeof(session->gpsdata.dev.sernum));
2840
    // save HW as subtype
2841
0
    (void)snprintf(session->subtype, sizeof(session->subtype),
2842
0
                   "%s-%.11s",
2843
0
                   char2str(field[11][0], hardware), field[2]);
2844
    // save FW Version as subtype1
2845
0
    (void)snprintf(session->subtype1, sizeof(session->subtype1),
2846
0
                   "FW %.11s",
2847
0
                   field[3]);
2848
2849
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
2850
0
             "NMEA0183: INFO: serial %s subtype %s subtype1 %s\n",
2851
0
             session->gpsdata.dev.sernum, session->subtype, session->subtype1);
2852
2853
0
    return ONLINE_SET;
2854
0
}
2855
2856
static gps_mask_t processMTW(unsigned count UNUSED, char *field[],
2857
                             struct gps_device_t *session)
2858
0
{
2859
    /* Water temp in degrees C
2860
     * $--MTW,x.x,C*hh<CR><LF>
2861
     *
2862
     * Fields in order:
2863
     * 1. water temp degrees C
2864
     * 2. C
2865
     * *hh          mandatory nmea_checksum
2866
     */
2867
0
    gps_mask_t mask = ONLINE_SET;
2868
2869
0
    if ('\0' == field[1][0] ||
2870
0
        'C' != field[2][0]) {
2871
        // no temp
2872
0
        return mask;
2873
0
    }
2874
0
    session->newdata.wtemp = safe_atof(field[1]);
2875
2876
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
2877
0
        "NMEA0183: %s temp %.1f C\n",
2878
0
        field[0], session->newdata.wtemp);
2879
0
    return mask;
2880
0
}
2881
2882
static gps_mask_t processMWD(unsigned count UNUSED, char *field[],
2883
                             struct gps_device_t *session)
2884
0
{
2885
    /*
2886
     * xxMWD - Wind direction and speed
2887
     * $xxMWD,x.x,T,x.x,M,x.x,N,x.x,M*hh<cr><lf>
2888
     * Fields in order:
2889
     * 1. wind direction, 0 to 359, True
2890
     * 2. T
2891
     * 3. wind direction, 0 to 359, Magnetic
2892
     * 4. M
2893
     * 5. wind speed, knots
2894
     * 6. N
2895
     * 7. wind speed, meters/sec
2896
     * 8. M
2897
     * *hh          mandatory nmea_checksum
2898
     */
2899
0
    gps_mask_t mask = ONLINE_SET;
2900
2901
0
    session->newdata.wanglet = safe_atof(field[1]);
2902
0
    session->newdata.wanglem = safe_atof(field[3]);
2903
0
    session->newdata.wspeedt = safe_atof(field[7]);
2904
0
    mask |= NAVDATA_SET;
2905
2906
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2907
0
        "NMEA0183: xxMWD wanglet %.2f wanglem %.2f wspeedt %.2f\n",
2908
0
        session->newdata.wanglet,
2909
0
        session->newdata.wanglem,
2910
0
        session->newdata.wspeedt);
2911
0
    return mask;
2912
0
}
2913
2914
static gps_mask_t processMWV(unsigned count UNUSED, char *field[],
2915
                             struct gps_device_t *session)
2916
0
{
2917
    /*
2918
     * xxMWV - Wind speed and angle
2919
     * $xxMWV,x.x,a,x.x,a,A*hh<cr><lf>
2920
     * Fields in order:
2921
     * 1. wind angle, 0 to 359, True
2922
     * 2. R = Relative (apparent), T = Theoretical (calculated)
2923
     *    Is T magnetic or true??
2924
     * 3. wind speed
2925
     * 4. wind speed units K/M/N/S
2926
     * 6. A = Valid, V = invalid
2927
     * *hh          mandatory nmea_checksum
2928
     */
2929
0
    gps_mask_t mask = ONLINE_SET;
2930
2931
0
    if (('R' == field[2][0]) &&
2932
0
        ('N' == field[4][0]) &&
2933
0
        ('A' == field[5][0])) {
2934
        // relative, knots, and valid
2935
0
        session->newdata.wangler = safe_atof(field[1]);
2936
0
        session->newdata.wspeedr = safe_atof(field[3]) * KNOTS_TO_MPS;
2937
0
        mask |= NAVDATA_SET;
2938
0
    }
2939
2940
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
2941
0
        "NMEA0183: xxMWV wangler %.2f wspeedr %.2f\n",
2942
0
        session->newdata.wangler,
2943
0
        session->newdata.wspeedr);
2944
0
    return mask;
2945
0
}
2946
2947
// PAIRxxx is Airoha, spunoff from Mediatek
2948
2949
// PAIR001 -- ACK/NAK
2950
static gps_mask_t processPAIR001(unsigned count UNUSED, char *field[],
2951
                                 struct gps_device_t *session)
2952
0
{
2953
0
    int reason;
2954
0
    const char *reasons[] = {
2955
0
        "Success",
2956
0
        "In process, wait",
2957
0
        "Failed",
2958
0
        "Not supported",
2959
0
        "Busy, try again.",
2960
0
        "Unknown",             // gpsd only
2961
0
    };
2962
2963
    // ACK / NACK
2964
0
    reason = atoi(field[2]);
2965
0
    if (4 == reason) {
2966
        // ACK
2967
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
2968
0
                 "NMEA0183: PAIR001, ACK: %s\n", field[1]);
2969
0
        return ONLINE_SET;
2970
0
    }
2971
2972
    // else, NACK
2973
0
    if (0 > reason ||
2974
0
        4 < reason) {
2975
        // WTF?
2976
0
        reason = 5;
2977
0
    }
2978
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
2979
0
             "NMEA0183: PAIR NACK: %s, reason: %s\n",
2980
0
             field[1], reasons[reason]);
2981
2982
0
    return ONLINE_SET;
2983
0
}
2984
2985
// PAIR010 -- Request Aiding
2986
static gps_mask_t processPAIR010(unsigned count UNUSED, char *field[],
2987
                                 struct gps_device_t *session)
2988
0
{
2989
0
    int type;
2990
0
    const char *types[] = {
2991
0
        "EPO data",
2992
0
        "Time",
2993
0
        "Location",
2994
0
        "Unknown",             // gpsd only
2995
0
    };
2996
2997
0
    int system;
2998
0
    const char *systems[] = {
2999
0
        "GPS",
3000
0
        "GLONASS",
3001
0
        "Galileo",
3002
0
        "BDS",
3003
0
        "QZSS",
3004
0
        "Unknown",             // gpsd only
3005
0
    };
3006
0
    int wn;         // week number
3007
0
    int tow;        // time of week
3008
3009
0
    type = atoi(field[1]);
3010
0
    if (0 > type ||
3011
0
        2 < type) {
3012
        // WTF?
3013
0
        type = 3;
3014
0
    }
3015
0
    system = atoi(field[2]);
3016
0
    if (0 > system ||
3017
0
        4 < system) {
3018
        // WTF?
3019
0
        system = 5;
3020
0
    }
3021
0
    wn = atoi(field[3]);
3022
0
    tow = atoi(field[4]);
3023
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
3024
0
             "NMEA0183: PAIR010: Need %s for %s.  WN %d TOW %d\n",
3025
0
             types[type], systems[system], wn, tow);
3026
3027
0
    return ONLINE_SET;
3028
0
}
3029
3030
// PDTINFO Unicore Product Information
3031
static gps_mask_t processPDTINFO(unsigned count UNUSED, char *field[],
3032
                                 struct gps_device_t *session)
3033
0
{
3034
0
    (void)snprintf(session->subtype, sizeof(session->subtype),
3035
0
                   "%s, %s, %s",
3036
0
                   field[1], field[2], field[5]);
3037
    // save SW and HW Version as subtype1
3038
0
    (void)snprintf(session->subtype1, sizeof(session->subtype1),
3039
0
                   "SW %s,HW %s",
3040
0
                   field[4], field[3]);
3041
3042
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
3043
0
             "NMEA0183: PDTINFO: subtype %s subtype1 %s\n",
3044
0
             session->subtype, session->subtype1);
3045
3046
0
    return ONLINE_SET;
3047
0
}
3048
3049
/* Ashtech sentences take this format:
3050
 * $PASHDR,type[,val[,val]]*CS
3051
 * type is an alphabetic subsentence type
3052
 *
3053
 * Oxford Technical Solutions (OxTS) also uses the $PASHR sentence,
3054
 * but with a very different sentence contents:
3055
 * $PASHR,HHMMSS.SSS,HHH.HH,T,RRR.RR,PPP.PP,aaa.aa,r.rrr,p.ppp,h.hhh,Q1,Q2*CS
3056
 *
3057
 * so field 1 in ASHTECH is always alphabetic and numeric in OXTS
3058
 *
3059
 */
3060
static gps_mask_t processPASHR(unsigned count UNUSED, char *field[],
3061
                               struct gps_device_t *session)
3062
0
{
3063
0
    gps_mask_t mask = ONLINE_SET;
3064
0
    char ts_buf[TIMESPEC_LEN];
3065
3066
0
    if (0 == strcmp("ACK", field[1])) {
3067
        // ACK
3068
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,ACK\n");
3069
0
        return ONLINE_SET;
3070
0
    } else if (0 == strcmp("MCA", field[1])) {
3071
        // MCA, raw data
3072
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,MCA\n");
3073
0
        return ONLINE_SET;
3074
0
    } else if (0 == strcmp("NAK", field[1])) {
3075
        // NAK
3076
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,NAK\n");
3077
0
        return ONLINE_SET;
3078
0
    } else if (0 == strcmp("PBN", field[1])) {
3079
        // PBN, position data
3080
        // FIXME: decode this for ECEF
3081
0
        GPSD_LOG(LOG_DATA, &session->context->errout, "NMEA0183: PASHR,PBN\n");
3082
0
        return ONLINE_SET;
3083
0
    } else if (0 == strcmp("POS", field[1])) {  // 3D Position
3084
        /* $PASHR,POS,
3085
         *
3086
         * 2: position type:
3087
         *      0 = autonomous
3088
         *      1 = position differentially corrected with RTCM code
3089
         *      2 = position differentially corrected with CPD float solution
3090
         *      3 = position is CPD fixed solution
3091
         */
3092
0
        mask |= MODE_SET | STATUS_SET | CLEAR_IS;
3093
0
        if ('\0' == field[2][0]) {
3094
            // empty first field means no 3D fix is available
3095
0
            session->newdata.status = STATUS_UNK;
3096
0
            session->newdata.mode = MODE_NO_FIX;
3097
0
        } else {
3098
3099
            // if we make it this far, we at least have a 3D fix
3100
0
            session->newdata.mode = MODE_3D;
3101
0
            if (1 <= atoi(field[2]))
3102
0
                session->newdata.status = STATUS_DGPS;
3103
0
            else
3104
0
                session->newdata.status = STATUS_GPS;
3105
3106
0
            session->nmea.gga_sats_used = atoi(field[3]);
3107
0
            if (0 == merge_hhmmss(field[4], session)) {
3108
0
                register_fractional_time(field[0], field[4], session);
3109
0
                mask |= TIME_SET;
3110
0
            }
3111
0
            if (0 == do_lat_lon(&field[5], &session->newdata)) {
3112
0
                mask |= LATLON_SET;
3113
0
                if ('\0' != field[9][0]) {
3114
                    // altitude is already WGS 84
3115
0
                    session->newdata.altHAE = safe_atof(field[9]);
3116
0
                    mask |= ALTITUDE_SET;
3117
0
                }
3118
0
            }
3119
0
            session->newdata.track = safe_atof(field[11]);
3120
0
            session->newdata.speed = safe_atof(field[12]) / MPS_TO_KPH;
3121
0
            session->newdata.climb = safe_atof(field[13]);
3122
0
            if ('\0' != field[14][0]) {
3123
0
                session->gpsdata.dop.pdop = safe_atof(field[14]);
3124
0
                mask |= DOP_SET;
3125
0
            }
3126
0
            if ('\0' != field[15][0]) {
3127
0
                session->gpsdata.dop.hdop = safe_atof(field[15]);
3128
0
                mask |= DOP_SET;
3129
0
            }
3130
0
            if ('\0' != field[16][0]) {
3131
0
                session->gpsdata.dop.vdop = safe_atof(field[16]);
3132
0
                mask |= DOP_SET;
3133
0
            }
3134
0
            if ('\0' != field[17][0]) {
3135
0
                session->gpsdata.dop.tdop = safe_atof(field[17]);
3136
0
                mask |= DOP_SET;
3137
0
            }
3138
0
            mask |= (SPEED_SET | TRACK_SET | CLIMB_SET);
3139
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3140
0
                     "NMEA0183: PASHR,POS: hhmmss=%s lat=%.2f lon=%.2f"
3141
0
                     " altHAE=%.f"
3142
0
                     " speed=%.2f track=%.2f climb=%.2f mode=%d status=%d"
3143
0
                     " pdop=%.2f hdop=%.2f vdop=%.2f tdop=%.2f used=%d\n",
3144
0
                     field[4], session->newdata.latitude,
3145
0
                     session->newdata.longitude, session->newdata.altHAE,
3146
0
                     session->newdata.speed, session->newdata.track,
3147
0
                     session->newdata.climb, session->newdata.mode,
3148
0
                     session->newdata.status, session->gpsdata.dop.pdop,
3149
0
                     session->gpsdata.dop.hdop, session->gpsdata.dop.vdop,
3150
0
                     session->gpsdata.dop.tdop, session->nmea.gga_sats_used);
3151
0
        }
3152
0
    } else if (0 == strcmp("RID", field[1])) {  // Receiver ID
3153
0
        (void)snprintf(session->subtype, sizeof(session->subtype) - 1,
3154
0
                       "%s ver %s", field[2], field[3]);
3155
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3156
0
                 "NMEA0183: PASHR,RID: subtype=%s mask={}\n",
3157
0
                 session->subtype);
3158
0
        return mask;
3159
0
    } else if (0 == strcmp("SAT", field[1])) {  // Satellite Status
3160
0
        struct satellite_t *sp;
3161
0
        unsigned i;
3162
0
        session->gpsdata.satellites_visible = atoi(field[2]);
3163
3164
0
        if (((NMEA_MAX_FLD - 15) / 5) < session->gpsdata.satellites_visible) {
3165
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
3166
0
                    "NMEA0183: PASHR,SAT: too many satellites %d\n",
3167
0
                     session->gpsdata.satellites_visible);
3168
0
            session->gpsdata.satellites_visible = (NMEA_MAX_FLD - 15) / 5;
3169
0
        }
3170
0
        session->gpsdata.satellites_used = 0;
3171
0
        sp = session->gpsdata.skyview;
3172
0
        for (i = 0; i < session->gpsdata.satellites_visible; i++) {
3173
0
            sp[i].PRN = (short)atoi(field[3 + i * 5 + 0]);
3174
0
            sp[i].azimuth = (double)atoi(field[3 + i * 5 + 1]);
3175
0
            sp[i].elevation = (double)atoi(field[3 + i * 5 + 2]);
3176
0
            sp[i].ss = safe_atof(field[3 + i * 5 + 3]);
3177
0
            sp[i].used = false;
3178
0
            if ('U' == field[3 + i * 5 + 4][0]) {
3179
0
                sp[i].used = true;
3180
0
                session->gpsdata.satellites_used++;
3181
0
            }
3182
0
        }
3183
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3184
0
                 "NMEA0183: PASHR,SAT: used=%d\n",
3185
0
                 session->gpsdata.satellites_used);
3186
0
        session->gpsdata.skyview_time.tv_sec = 0;
3187
0
        session->gpsdata.skyview_time.tv_nsec = 0;
3188
0
        mask |= SATELLITE_SET | USED_IS;
3189
3190
0
    } else if (0 == strcmp("T", field[3])) {   // Assume OxTS PASHR
3191
        // FIXME: decode OxTS $PASHDR, time is wrong, breaks cycle order
3192
0
        if (0 == merge_hhmmss(field[1], session)) {
3193
            // register_fractional_time(field[0], field[1], session);
3194
            // mask |= TIME_SET; confuses cycle order
3195
0
        }
3196
        // Assume true heading
3197
0
        session->gpsdata.attitude.heading = safe_atof(field[2]);
3198
0
        session->gpsdata.attitude.roll = safe_atof(field[4]);
3199
0
        session->gpsdata.attitude.pitch = safe_atof(field[5]);
3200
        // mask |= ATTITUDE_SET;  * confuses cycle order ??
3201
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3202
0
                 "NMEA0183: PASHR (OxTS) time %s, heading %lf.\n",
3203
0
                  timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
3204
0
                  session->gpsdata.attitude.heading);
3205
0
    }
3206
0
    return mask;
3207
0
}
3208
3209
/* Ericsson $PERC,FWsts - Firmware status
3210
 * Firmware version and status information for timing modules
3211
 *
3212
 * $PERC,FWsts,<grp1>,<grp2>,<grp3>,<state>,<substatus>,<type>*XX
3213
 *
3214
 * Field 1: grp1 - Status group 1 (6 digits, format XXYYZZ)
3215
 * Field 2: grp2 - Status group 2 (6 digits, format XXYYZZ)
3216
 * Field 3: grp3 - Status group 3 (6 digits, format XXYYZZ)
3217
 * Field 4: state - Firmware state (0-3)
3218
 * Field 5: substatus - Sub-status code (0=normal, 1=0xD, 2=0xE)
3219
 * Field 6: type - Type indicator
3220
 *
3221
 * Periodic sentence (~19s interval) providing firmware status details.
3222
 * Status groups contain device-specific diagnostic information.
3223
 */
3224
static gps_mask_t processPERCFWsts(unsigned count UNUSED, char *field[],
3225
                                   struct gps_device_t *session)
3226
0
{
3227
0
    gps_mask_t mask = ONLINE_SET;
3228
0
    int grp1, grp2, grp3, state, substatus, type;
3229
3230
0
    static const struct vlist_t vfwsts_type[] = {
3231
0
        {0, "Normal"},
3232
0
        {1, "Type 1"},
3233
0
        {2, "Type 2"},
3234
0
        {3, "Type 3"},
3235
0
        {0, NULL},
3236
0
    };
3237
3238
    // field[0]="PERC", field[1]="FWsts", data starts at field[2]
3239
0
    grp1 = atoi(field[2]);
3240
0
    grp2 = atoi(field[3]);
3241
0
    grp3 = atoi(field[4]);
3242
0
    state = atoi(field[5]);
3243
0
    substatus = atoi(field[6]);
3244
0
    type = atoi(field[7]);
3245
3246
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3247
0
             "NMEA0183: PERC,FWsts: groups=%06d,%06d,%06d state=%d "
3248
0
             "substatus=%d type=%s(%d)\n",
3249
0
             grp1, grp2, grp3, state, substatus,
3250
0
             val2str(type, vfwsts_type), type);
3251
3252
0
    return mask;
3253
0
}
3254
3255
/* Ericsson $PERC,GPavp - Averaged position
3256
 * Position-hold reference coordinates for timing modules
3257
 *
3258
 * $PERC,GPavp,<lat>,<lat_ns>,<lon>,<lon_ew>,<alt>,<alt_unit>*XX
3259
 *
3260
 * Field 1: lat - Latitude in ddmm.mmmm format
3261
 * Field 2: lat_ns - Latitude hemisphere (N/S)
3262
 * Field 3: lon - Longitude in dddmm.mmmm format
3263
 * Field 4: lon_ew - Longitude hemisphere (E/W)
3264
 * Field 5: alt - Altitude in meters
3265
 * Field 6: alt_unit - Altitude units (M=meters)
3266
 *
3267
 * This sentence appears only in mode 2 (position-hold) and provides the
3268
 * surveyed average position that the timing module uses as its reference.
3269
 * Format matches standard NMEA position encoding.
3270
 */
3271
static gps_mask_t processPERCGPavp(unsigned count UNUSED, char *field[],
3272
                                   struct gps_device_t *session)
3273
0
{
3274
0
    gps_mask_t mask = ONLINE_SET;
3275
0
    double lat, lon, alt;
3276
3277
    // field[0]="PERC", field[1]="GPavp", data starts at field[2]
3278
    // Format: lat, lat_ns, lon, lon_ew, alt, alt_unit
3279
0
    lat = decode_lat_or_lon(field[2]);
3280
0
    if ('S' == field[3][0])
3281
0
        lat = -lat;
3282
3283
0
    lon = decode_lat_or_lon(field[4]);
3284
0
    if ('W' == field[5][0])
3285
0
        lon = -lon;
3286
3287
0
    alt = safe_atof(field[6]);
3288
3289
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3290
0
             "NMEA0183: PERC,GPavp: lat=%.6f lon=%.6f alt=%.1fm\n",
3291
0
             lat, lon, alt);
3292
3293
    // Save surveyed/averaged position (position-hold reference)
3294
0
    session->newdata.latitude = lat;
3295
0
    session->newdata.longitude = lon;
3296
0
    session->newdata.altHAE = alt;
3297
0
    mask |= LATLON_SET | ALTITUDE_SET;
3298
3299
0
    return mask;
3300
0
}
3301
3302
/* Ericsson $PERC,GPctr - Control/heartbeat
3303
 * Periodic heartbeat and configuration status message for timing modules
3304
 *
3305
 * $PERC,GPctr,<direction>,<data>,<f3>,<f4>,<config_byte>,<f6>,<f7>*XX
3306
 *
3307
 * Field 1: direction - 'R'=Read response, 'V'=Value notification
3308
 * Field 2: data - Configuration data (21 bytes per sentence)
3309
 * Field 3-5: Additional config fields
3310
 * Field 6: config_byte - Configuration byte
3311
 * Field 7: Reserved
3312
 *
3313
 * Periodic sentence (~15s interval) serving as keepalive/heartbeat.
3314
 * Provides configuration and status information.
3315
 */
3316
static gps_mask_t processPERCGPctr(unsigned count UNUSED, char *field[],
3317
                                   struct gps_device_t *session)
3318
0
{
3319
0
    gps_mask_t mask = ONLINE_SET;
3320
0
    char direction;
3321
0
    int config_byte = 0;
3322
3323
    // field[0]="PERC", field[1]="GPctr", data starts at field[2]
3324
0
    direction = field[2][0];
3325
0
    if (field[6][0] != '\0') {
3326
0
        config_byte = atoi(field[6]);
3327
0
    }
3328
3329
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3330
0
             "NMEA0183: PERC,GPctr: direction=%c data=%s config=%d\n",
3331
0
             direction, field[3], config_byte);
3332
3333
0
    return mask;
3334
0
}
3335
3336
/* Ericsson $PERC,GPdbg - Debug output
3337
 * Satellite tracking debug and status counter for Ericsson timing modules
3338
 *
3339
 * Two variants based on type field:
3340
 *
3341
 * Type 1: Satellite tracking debug (4 messages per second when enabled)
3342
 * $PERC,GPdbg,1,<page_count>,<page_num>,<sv_data>...*XX
3343
 *
3344
 * Pages 1-3: Up to 7 satellites per page in PPPSSXX format:
3345
 *   PPP = PRN (3 digits)
3346
 *   SS = SNR (2 digits)
3347
 *   XX = Status (2 hex digits: 00=solid, 01=weak, 05=acquiring)
3348
 *
3349
 * Page 10: Timing/phase measurements with bitmask
3350
 *   field[5] = Satellite bitmask (hex, e.g., 07FF = 11 sats)
3351
 *   fields[6-19] = Timing/phase/frequency measurements
3352
 *
3353
 * Type 2: Status counter debug
3354
 * $PERC,GPdbg,2,<counter>*XX
3355
 *
3356
 * Enabled via command: $PERC,GPdbg,1*43
3357
 * Provides real-time satellite tracking status beyond standard GPGSV.
3358
 */
3359
static gps_mask_t processPERCGPdbg(unsigned count UNUSED, char *field[],
3360
                                   struct gps_device_t *session)
3361
0
{
3362
0
    gps_mask_t mask = ONLINE_SET;
3363
0
    int type, page_count, page_num, i;
3364
3365
    // field[0]="PERC", field[1]="GPdbg", data starts at field[2]
3366
0
    type = atoi(field[2]);
3367
3368
0
    if (type == 1) {
3369
        // Satellite tracking debug
3370
0
        page_count = atoi(field[3]);
3371
0
        page_num = atoi(field[4]);
3372
3373
0
        if (page_num == 10) {
3374
            // Page 10: Special format with timing/phase data
3375
            // field[5]=bitmask (hex), field[6+]=timing fields
3376
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3377
0
                     "NMEA0183: PERC,GPdbg: type=1 page=%d/%d bitmask=%s "
3378
0
                     "timing=[%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s]\n",
3379
0
                     page_num, page_count, field[5],
3380
0
                     field[6], field[7], field[8], field[9], field[10],
3381
0
                     field[11], field[12], field[13], field[14], field[15],
3382
0
                     field[16], field[17], field[18], field[19]);
3383
0
        } else {
3384
            // Pages 1-3: Satellite tracking data
3385
            // Format: PRN(3)+SNR(2)+Status(2) = 7 digits per satellite
3386
            // Count non-empty satellite fields
3387
0
            int sv_count = 0;
3388
0
            for (i = 5; i < NMEA_MAX_FLD && field[i][0] != '\0'; i++) {
3389
0
                sv_count++;
3390
0
            }
3391
3392
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3393
0
                     "NMEA0183: PERC,GPdbg: type=1 page=%d/%d sv_count=%d\n",
3394
0
                     page_num, page_count, sv_count);
3395
3396
            // Log individual satellites (up to 7 per page)
3397
            // FIXME: save them in skyview[]
3398
0
            for (i = 5; i < 5 + 7 && field[i][0] != '\0'; i++) {
3399
0
                int prn = 0, snr = 0, status = 0;
3400
0
                if (7 <= strnlen(field[i], 8)) {
3401
                    // Parse PPPSSXX format
3402
0
                    char prn_str[4], snr_str[3], status_str[3];
3403
3404
0
                    strlcpy(prn_str, field[i], sizeof(prn_str));
3405
0
                    strlcpy(snr_str, field[i] + 3, sizeof(snr_str));
3406
0
                    strlcpy(status_str, field[i] + 5, sizeof(status_str));
3407
3408
0
                    prn = atoi(prn_str);
3409
0
                    snr = atoi(snr_str);
3410
0
                    status = (int)strtol(status_str, NULL, 16);
3411
3412
0
                    GPSD_LOG(LOG_DATA, &session->context->errout,
3413
0
                             "NMEA0183: PERC,GPdbg:   sv%d: PRN=%d SNR=%d "
3414
0
                             "status=0x%02x\n",
3415
0
                             i - 4, prn, snr, status);
3416
0
                }
3417
0
            }
3418
0
        }
3419
0
    } else if (type == 2) {
3420
        // Status counter debug
3421
0
        int counter = atoi(field[3]);
3422
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3423
0
                 "NMEA0183: PERC,GPdbg: type=2 counter=%d\n",
3424
0
                 counter);
3425
0
    } else {
3426
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
3427
0
                 "NMEA0183: PERC,GPdbg: unknown type=%d\n",
3428
0
                 type);
3429
0
    }
3430
3431
    // Debug data is logged but not stored in gps_data_t API structures.
3432
    // Per-satellite tracking status and page 10 timing measurements
3433
3434
    // FIXME: store into satellite_t and skyview
3435
3436
0
    return mask;
3437
0
}
3438
3439
/* Ericsson $PERC,GPppf - Position phase/frequency error
3440
 * Oscillator discipline quality for timing modules
3441
 *
3442
 * $PERC,GPppf,<phase_error>,<freq_error>,<status>,<leap_sec>,<quality>*XX
3443
 *
3444
 * Field 1: phase_error - Phase error in nanoseconds (signed)
3445
 * Field 2: freq_error - Frequency error in ppb (parts per billion, signed)
3446
 * Field 3: status - Status indicator (1=holdover/acquiring, 0=GPS-disciplined)
3447
 * Field 4: leap_sec - UTC-GPS leap second offset (e.g., 18 in 2026)
3448
 * Field 5: quality - PPS quality indicator (0=locked, 1=good, 2=degraded)
3449
 *
3450
 * This sentence provides real-time oscillator discipline quality metrics.
3451
 * Phase error indicates PPS timing offset, frequency error shows oscillator
3452
 * stability relative to GPS reference. Leap seconds typically appear ~2.5
3453
 * minutes after first fix.
3454
 */
3455
static gps_mask_t processPERCGPppf(unsigned count UNUSED, char *field[],
3456
                                   struct gps_device_t *session)
3457
0
{
3458
0
    gps_mask_t mask = ONLINE_SET;
3459
0
    double phase_error, freq_error;
3460
0
    int status, leap_sec, quality;
3461
3462
    // field[0]="PERC", field[1]="GPppf", data starts at field[2]
3463
0
    phase_error = safe_atof(field[2]);
3464
0
    freq_error = safe_atof(field[3]);
3465
0
    status = atoi(field[4]);
3466
0
    leap_sec = atoi(field[5]);
3467
0
    quality = atoi(field[6]);
3468
3469
    // Note: Always populate oscillator data - distros configure builds
3470
    // inconsistently. The oscillator field is part of the standard API.
3471
0
    session->gpsdata.osc.running = true;
3472
0
    session->gpsdata.osc.reference = true;
3473
0
    session->gpsdata.osc.disciplined = (status == 0);
3474
0
    session->gpsdata.osc.delta = (int)phase_error;
3475
0
    mask |= OSCILLATOR_SET;
3476
3477
    // Store leap seconds (can be negative in the future!)
3478
0
    session->gpsdata.leap_seconds = leap_sec;
3479
3480
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3481
0
             "NMEA0183: PERC,GPppf: phase=%.1fns freq=%.1fppb "
3482
0
             "disciplined=%d leap=%d quality=%d\n",
3483
0
             phase_error, freq_error, (status == 0), leap_sec, quality);
3484
3485
0
    return mask;
3486
0
}
3487
3488
/* Ericsson $PERC,GPppr - Position pulse reference
3489
 * GPS time reference and PPS quality for timing modules
3490
 *
3491
 * $PERC,GPppr,<tow_sec>,<gps_week>,<param>,<sv_count>,<pps_flag>,<reserved>*XX
3492
 *
3493
 * Field 1: tow_sec - GPS Time of Week in seconds (0-604799)
3494
 * Field 2: gps_week - GPS week number
3495
 * Field 3: param - Constant parameter (always 00050, likely cable delay in ns)
3496
 * Field 4: sv_count - Number of satellites used in solution
3497
 * Field 5: pps_flag - PPS quality indicator (0=OK/locked, 3=not locked)
3498
 * Field 6: reserved - Reserved field (always 0)
3499
 *
3500
 * Critical timing sentence providing GPS time reference and PPS lock status.
3501
 * The pps_flag field indicates whether PPS output is reliable.
3502
 */
3503
static gps_mask_t processPERCGPppr(unsigned count UNUSED, char *field[],
3504
                                   struct gps_device_t *session)
3505
0
{
3506
0
    gps_mask_t mask = ONLINE_SET;
3507
0
    unsigned int tow_sec, gps_week, param, sv_count, pps_flag, reserved;
3508
0
    timespec_t ts_tow;
3509
3510
0
    static const struct vlist_t vpercgpppr_pps[] = {
3511
0
        {0, "locked"},
3512
0
        {3, "unlocked"},
3513
0
        {0, NULL},
3514
0
    };
3515
3516
    // field[0]="PERC", field[1]="GPppr", data starts at field[2]
3517
0
    tow_sec = atoi(field[2]);
3518
0
    gps_week = atoi(field[3]);
3519
0
    param = atoi(field[4]);
3520
0
    sv_count = atoi(field[5]);
3521
0
    pps_flag = atoi(field[6]);
3522
    // field[7] is reserved (always 0)
3523
0
    reserved = atoi(field[7]);
3524
3525
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3526
0
             "NMEA0183: PERC,GPppr: week=%u tow=%u param=%u sats=%u "
3527
0
             "pps_flag=%s reserved=%d(%u)\n",
3528
0
             gps_week, tow_sec, param, sv_count,
3529
0
             val2str(pps_flag, vpercgpppr_pps), pps_flag, reserved);
3530
3531
    // Convert GPS week + TOW to Unix timestamp
3532
0
    ts_tow.tv_sec = tow_sec;
3533
0
    ts_tow.tv_nsec = 0;
3534
0
    session->newdata.time = gpsd_gpstime_resolv(session, gps_week, ts_tow);
3535
0
    mask |= TIME_SET;
3536
3537
    // Save satellite count
3538
0
    session->gpsdata.satellites_used = (int)sv_count;
3539
0
    mask |= SATELLITE_SET;
3540
3541
    /* Cable delay parameter (constant 50ns on GRU 04??)
3542
     * Logged for reference - no suitable API field for cable delay */
3543
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3544
0
             "NMEA0183: Cable delay compensation: %u\n", param);
3545
3546
    // Save PPS lock status to oscillator structure
3547
    // pps_flag: 0 = locked/OK, 3 = not locked
3548
0
    if (0 == pps_flag) {
3549
0
        session->gpsdata.osc.reference = true;
3550
0
    } else {
3551
0
        session->gpsdata.osc.reference = false;
3552
0
    }
3553
0
    mask |= OSCILLATOR_SET;
3554
3555
0
    return mask;
3556
0
}
3557
3558
/* Ericsson $PERC,GPreh - Receiver health
3559
 * Health status for timing modules
3560
 *
3561
 * $PERC,GPreh,<timestamp>,<health_code>*XX
3562
 *
3563
 * Field 1: timestamp - Time/date string
3564
                        (format varies, often null "00:00:00 00/00/0000")
3565
 * Field 2: health_code - Health status code (numeric)
3566
 *
3567
 * Periodic sentence (~19s interval) providing receiver health status.
3568
 * Health code interpretation is device-specific.
3569
 */
3570
static gps_mask_t processPERCGPreh(unsigned count UNUSED, char *field[],
3571
                                   struct gps_device_t *session)
3572
0
{
3573
0
    gps_mask_t mask = ONLINE_SET;
3574
0
    int health_code;
3575
3576
    // field[0]="PERC", field[1]="GPreh", data starts at field[2]
3577
0
    health_code = atoi(field[3]);
3578
3579
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3580
0
             "NMEA0183: PERC,GPreh: timestamp=%s health=%d\n",
3581
0
             field[2], health_code);
3582
3583
0
    return mask;
3584
0
}
3585
3586
/* Ericsson $PERC,GPsts - Receiver status
3587
 * Timing module operating mode and constellation information
3588
 *
3589
 * $PERC,GPsts,<mode>,<survey_flag>,<constellation>,<capabilities>*XX
3590
 *
3591
 * Field 1: mode - Operating mode (0=acquiring, 1=survey, 2=position-hold)
3592
 * Field 2: survey_flag - Survey status (0=position valid, 1=no position)
3593
 * Field 3: constellation - Constellation configuration (2=GPS+GLONASS)
3594
 * Field 4: capabilities - 18-digit capability bitmask "010011111111011111"
3595
 *
3596
 * Primary status sentence providing operating mode and system capabilities.
3597
 */
3598
static gps_mask_t processPERCGPsts(unsigned count UNUSED, char *field[],
3599
                                   struct gps_device_t *session)
3600
0
{
3601
0
    int mode, survey_flag, constellation;
3602
0
    gps_mask_t mask = ONLINE_SET;
3603
3604
0
    static const struct vlist_t vpercgpsts_mode[] = {
3605
0
        {0, "Acquiring"},
3606
0
        {1, "Survey"},
3607
0
        {2, "Position-hold"},
3608
0
        {3, "Overdetermined"},
3609
0
        {4, "Manual"},
3610
0
        {5, "3D-hold"},
3611
0
        {0, NULL},
3612
0
    };
3613
3614
    // field[0]="PERC", field[1]="GPsts", data starts at field[2]
3615
0
    mode = atoi(field[2]);
3616
0
    survey_flag = atoi(field[3]);
3617
0
    constellation = atoi(field[4]);
3618
3619
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3620
0
             "NMEA0183: PERC,GPsts: mode=%s(%d) survey_flag=%d "
3621
0
             "constellation=%d capabilities=%s\n",
3622
0
             val2str(mode, vpercgpsts_mode), mode, survey_flag,
3623
0
             constellation, field[5]);
3624
3625
    /* Map receiver operating mode to fix status
3626
     * Survey and Position-hold modes are timing receiver states */
3627
0
    if (1 == mode ||
3628
0
        2 == mode ||
3629
0
        4 == mode) {
3630
        // Survey, Position-hold, or Manual → timing mode
3631
0
        session->newdata.status = STATUS_TIME;
3632
0
        mask |= STATUS_SET;
3633
0
    } else if (0 == mode) {
3634
        // Acquiring → GPS fix mode
3635
0
        session->newdata.status = STATUS_GPS;
3636
0
        mask |= STATUS_SET;
3637
0
    }
3638
    // Modes 3 (Overdetermined) and 5 (3D-hold) not mapped
3639
3640
0
    return mask;
3641
0
}
3642
3643
/* Ericsson $PERC,GPver - Receiver identification
3644
 * Returns hardware model and serial number
3645
 *
3646
 * $PERC,GPver,<model>,<part_num>,<hw_rev>,<serial>*XX
3647
 *
3648
 * Field 1: model - "GRU 04 01" or "GRU 04 02"
3649
 * Field 2: part_num - "NCD 901 65/1" or "NCD 901 78/1"
3650
 * Field 3: hw_rev - Hardware revision (e.g., "R1E")
3651
 * Field 4: serial - Serial number
3652
 */
3653
static gps_mask_t processPERCGPver(unsigned count UNUSED, char *field[],
3654
                                   struct gps_device_t *session)
3655
0
{
3656
0
    gps_mask_t mask = ONLINE_SET;
3657
0
    char new_serial[32];
3658
0
    char old_subtype[64];
3659
3660
    // field[0]="PERC", field[1]="GPver", data starts at field[2]
3661
    // Save old subtype for comparison
3662
0
    strlcpy(old_subtype, session->subtype, sizeof(old_subtype));
3663
0
    strlcpy(new_serial, field[5], sizeof(new_serial));
3664
3665
    // Build subtype directly into session->subtype
3666
0
    (void)snprintf(session->subtype, sizeof(session->subtype),
3667
0
                   "%s %s %s",
3668
0
                   field[2], field[3], field[4]);
3669
3670
    // Only log at high level if changed
3671
0
    if (0 != strcmp(old_subtype, session->subtype) ||
3672
0
        0 != strcmp(session->gpsdata.dev.sernum, new_serial)) {
3673
0
        GPSD_LOG(LOG_INF, &session->context->errout,
3674
0
                 "NMEA0183: PERC,GPver: new device %s serial %s\n",
3675
0
                 session->subtype, new_serial);
3676
0
        mask |= DEVICEID_SET;
3677
0
    } else {
3678
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3679
0
                 "NMEA0183: PERC,GPver: (unchanged)\n");
3680
0
    }
3681
3682
    // Update serial number
3683
0
    strlcpy(session->gpsdata.dev.sernum, new_serial,
3684
0
            sizeof(session->gpsdata.dev.sernum));
3685
3686
0
    return mask;
3687
0
}
3688
3689
/* Trimble $PTNLRNM - Receiver Navigation Mode
3690
 * Navigation mode status for Trimble/Ericsson receivers
3691
 *
3692
 * $PTNLRNM,<mode>*XX
3693
 *
3694
 * Field 1: mode - Navigation mode status character
3695
 *          A = Autonomous/Active
3696
 *          D = Differential
3697
 *          E = Estimated/Dead Reckoning
3698
 *          N = Data not valid
3699
 *
3700
 * Periodic sentence providing receiver navigation mode status.
3701
 * Typically outputs 'A' for autonomous operation.
3702
 */
3703
static gps_mask_t processPTNLRNM(unsigned count UNUSED, char *field[],
3704
                                 struct gps_device_t *session)
3705
0
{
3706
0
    gps_mask_t mask = ONLINE_SET;
3707
0
    char mode;
3708
3709
0
    static const struct vlist_t vptnlrnm_mode[] = {
3710
0
        {'A', "Autonomous"},
3711
0
        {'D', "Differential"},
3712
0
        {'E', "Estimated"},
3713
0
        {'N', "Invalid"},
3714
0
        {0, NULL},
3715
0
    };
3716
3717
    // field[0]="PTNLRNM", data starts at field[1]
3718
0
    mode = field[1][0];
3719
3720
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3721
0
             "NMEA0183: PTNLRNM: navigation mode=%c (%s)\n",
3722
0
             mode, val2str(mode, vptnlrnm_mode));
3723
3724
    // Map mode to GPS status
3725
0
    switch (mode) {
3726
0
    case 'A':
3727
0
        session->newdata.status = STATUS_GPS;
3728
0
        mask |= STATUS_SET;
3729
0
        break;
3730
0
    case 'D':
3731
0
        session->newdata.status = STATUS_DGPS;
3732
0
        mask |= STATUS_SET;
3733
0
        break;
3734
0
    case 'E':
3735
0
        session->newdata.status = STATUS_DR;
3736
0
        mask |= STATUS_SET;
3737
0
        break;
3738
0
    default:
3739
        // N or unknown - no status set
3740
0
        break;
3741
0
    }
3742
3743
0
    return mask;
3744
0
}
3745
3746
/* Trimble $PTNLRBA - Antenna status
3747
 * Antenna connection health monitoring for Trimble/Ericsson receivers
3748
 *
3749
 * $PTNLRBA,<status>,<flag>*XX
3750
 *
3751
 * Field 1: status - Antenna status (1=OK, 0=fault)
3752
 * Field 2: flag - Additional status flag
3753
 *
3754
 * Monitors antenna connection health. Important for timing applications
3755
 * as antenna problems directly affect signal quality and position accuracy.
3756
 */
3757
static gps_mask_t processPTNLRBA(unsigned count UNUSED, char *field[],
3758
                                 struct gps_device_t *session)
3759
0
{
3760
0
    gps_mask_t mask = ONLINE_SET;
3761
0
    int status, flag;
3762
3763
0
    static const struct vlist_t vptnlrba_status[] = {
3764
0
        {0, "Fault"},
3765
0
        {1, "OK"},
3766
0
        {0, NULL},
3767
0
    };
3768
3769
    // field[0]="PTNLRBA", data starts at field[1]
3770
0
    status = atoi(field[1]);
3771
0
    flag = atoi(field[2]);
3772
3773
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3774
0
             "NMEA0183: PTNLRBA: antenna_status=%s(%d) flag=%d\n",
3775
0
             val2str(status, vptnlrba_status), status, flag);
3776
3777
    // Save antenna status to fix structure
3778
0
    if (1 == status) {
3779
0
        session->newdata.ant_stat = ANT_OK;
3780
0
    } else {
3781
        // Status 0 = Fault (type unknown, map to generic fault)
3782
0
        session->newdata.ant_stat = ANT_SHORT;
3783
0
        mask |= ERROR_SET;
3784
0
    }
3785
3786
0
    return mask;
3787
0
}
3788
3789
/* Trimble $PTNLRTP - Receiver temperature
3790
 * Internal temperature monitoring for Trimble/Ericsson receivers
3791
 *
3792
 * $PTNLRTP,T,<temp>,<precision>*XX
3793
 *
3794
 * Field 1: type - Always 'T' for temperature
3795
 * Field 2: temp - Temperature in degrees Celsius
3796
 * Field 3: precision - Temperature measurement precision/accuracy
3797
 *
3798
 * Monitors receiver internal temperature. Useful for thermal stability
3799
 * analysis in timing applications. Observed range: 30-40°C typical.
3800
 */
3801
static gps_mask_t processPTNLRTP(unsigned count UNUSED, char *field[],
3802
                                 struct gps_device_t *session)
3803
0
{
3804
0
    gps_mask_t mask = ONLINE_SET;
3805
0
    double temp, precision;
3806
0
    char type;
3807
3808
    // field[0]="PTNLRTP", data starts at field[1]
3809
0
    type = field[1][0];
3810
0
    temp = safe_atof(field[2]);
3811
0
    precision = safe_atof(field[3]);
3812
3813
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3814
0
             "NMEA0183: PTNLRTP: type=%c temp=%.2f°C precision=%.1f\n",
3815
0
             type, temp, precision);
3816
3817
    // Store temperature in gps_fix_t.temp field.  What temperature is it?
3818
0
    session->newdata.temp = temp;
3819
    // No specific mask for temperature - aux data included in fix
3820
3821
0
    return mask;
3822
0
}
3823
3824
/* Trimble $PTNLRXO - Crystal oscillator status
3825
 * Oscillator lock and frequency offset for Trimble/Ericsson receivers
3826
 *
3827
 * $PTNLRXO,<status>,<offset>*XX
3828
 *
3829
 * Field 1: status - Oscillator lock status (1=locked, 0=unlocked)
3830
 * Field 2: offset - Frequency offset in ppb (parts per billion)
3831
 *
3832
 * Reports crystal oscillator disciplining status and frequency error.
3833
 * Complementary to $PERC,GPppf which provides phase error in nanoseconds.
3834
 * Typical offset: -450 to -500 ppb for this hardware.
3835
 */
3836
static gps_mask_t processPTNLRXO(unsigned count UNUSED, char *field[],
3837
                                 struct gps_device_t *session)
3838
0
{
3839
0
    gps_mask_t mask = ONLINE_SET;
3840
0
    int status;
3841
0
    double offset;
3842
3843
0
    static const struct vlist_t vptnlrxo_status[] = {
3844
0
        {0, "Unlocked"},
3845
0
        {1, "Locked"},
3846
0
        {0, NULL},
3847
0
    };
3848
3849
    // field[0]="PTNLRXO", data starts at field[1]
3850
0
    status = atoi(field[1]);
3851
0
    offset = safe_atof(field[2]);
3852
3853
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
3854
0
             "NMEA0183: PTNLRXO: osc_status=%s(%d) offset=%.3f ppb\n",
3855
0
             val2str(status, vptnlrxo_status), status, offset);
3856
3857
    // Frequency offset could be stored alongside GPppf data if an
3858
    // extended oscillator_t structure is added to gps_data_t.
3859
    // Note: API extension needed for frequency offset storage.
3860
3861
0
    return mask;
3862
0
}
3863
3864
/* Android GNSS super message
3865
 * A stub.
3866
 */
3867
static gps_mask_t processPGLOR(unsigned count UNUSED, char *field[],
3868
                               struct gps_device_t *session)
3869
0
{
3870
    /*
3871
     * $PGLOR,0,FIX,....
3872
     * 1    = sentence version (may not be present)
3873
     * 2    = message subtype
3874
     * ....
3875
     *
3876
     * subtypes:
3877
     *  $PGLOR,[],AGC - ??
3878
     *  $PGLOR,[],CPU - CPU Loading
3879
     *  $PGLOR,[],FIN - Request completion status
3880
     *  $PGLOR,0,FIX,seconds - Time To Fix
3881
     *  $PGLOR,[],FTS - Factory Test Status
3882
     *  $PGLOR,[],GFC - GeoFence Fix
3883
     *  $PGLOR,[],GLO - ??
3884
     *  $PGLOR,[],HLA - Value of HULA sensors
3885
     *  $PGLOR,[],IMS - IMES messages
3886
     *  $PGLOR,1,LSQ,hhmmss.ss  - Least squares GNSS fix
3887
     *  $PGLOR,NET    - Report network information
3888
     *  $PGLOR,[],NEW - Indicate new GPS request
3889
     *  $PGLOR,[],PFM - Platform Status
3890
     *  $PGLOR,[],PPS - Indicate PPS time corrections
3891
     *  $PGLOR,5,PWR i - Power consumption report
3892
     *                  Only have doc for 5, Quectel uses 4
3893
     *  $PGLOR,[],RID - Version Information
3894
     *  $PGLOR,2,SAT - GPS Satellite information
3895
     *  $PGLOR,[],SIO - Serial I/O status report
3896
     *  $PGLOR,[],SPA - Spectrum analyzer results
3897
     *  $PGLOR,0,SPD  - Speed, Steps, etc.
3898
     *  $PGLOR,SPL    - ??
3899
     *  $PGLOR,[],SPS - ??
3900
     *  $PGLOR,10,STA - GLL status
3901
     *  $PGLOR,[],SVC - ??
3902
     *  $PGLOR,[],SVD - SV Dopplers detected in the false alarm test.
3903
     *  $PGLOR,[],SMx - Report GPS Summary Information
3904
     *  $PGLOR,[],UNC - ??
3905
     *
3906
     * Are NET and SPL really so different?
3907
     *
3908
     */
3909
0
    gps_mask_t mask = ONLINE_SET;
3910
0
    int got_one = 0;
3911
3912
0
    switch (field[1][0]) {
3913
0
    case '0':
3914
0
        if (0 == strncmp("FIX", field[2], 3)) {
3915
0
            got_one = 1;
3916
            // field 3, time to first fix in seconds
3917
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3918
0
                     "NMEA0183: PGLOR: FIX, TTFF %s\n",
3919
0
                     field[3]);
3920
0
        } else if (0 == strncmp("SPD", field[2], 3)) {
3921
0
            got_one = 1;
3922
            // field 4, ddmmy.ss UTC
3923
            // field 5, hhmmss.ss UTC
3924
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3925
0
                     "NMEA0183: PGLOR: SPD, %s %s UTC\n",
3926
0
                     field[4], field[5]);
3927
0
        }
3928
0
        break;
3929
0
    case '1':
3930
0
        if (0 == strncmp("LSQ", field[2], 3)) {
3931
0
            got_one = 1;
3932
            // field 3, hhmmss.ss UTC, only field Quectel supplies
3933
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3934
0
                     "NMEA0183: PGLOR: LSQ %s UTC\n",
3935
0
                     field[3]);
3936
0
        } else if ('0' == field[1][1] &&
3937
0
                   0 == strncmp("STA", field[2], 3)) {
3938
            // version 10
3939
0
            got_one = 1;
3940
            // field 3, hhmmss.ss UTC
3941
            // field 7, Position uncertainty meters
3942
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
3943
0
                     "NMEA0183: PGLOR: STA, UTC %s PosUncer  %s\n",
3944
0
                     field[3], field[7]);
3945
0
        }
3946
0
        break;
3947
0
    }
3948
0
    if (0 != got_one) {
3949
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
3950
0
                 "NMEA0183: PGLOR: seq %s type %s\n",
3951
0
                 field[1], field[2]);
3952
0
    }
3953
0
    return mask;
3954
0
}
3955
3956
// Inertial Sense GPS nav data, not a legal message name
3957
static gps_mask_t processPGPSP(unsigned count UNUSED, char *field[],
3958
                               struct gps_device_t *session)
3959
0
{
3960
    /*
3961
     * $PGPSP,523970800,2351,778,44.06887670,-121.31410390,1114.07,1134.17,
3962
     * 2.55,4.32,11.26,0.13,0.52,0.25,0.10,25.7,0.0000,18*51
3963
     *
3964
     */
3965
0
    gps_mask_t mask = ONLINE_SET;
3966
0
    unsigned long i_tow = strtoul(field[1], NULL, 10);   // ms
3967
0
    int weeks = atoi(field[2]);
3968
0
    unsigned long status = strtoul(field[3], NULL, 10);
3969
0
    int used = status & 0x0ff;
3970
0
    int gpsStatus = (status >> 8) & 0x0ff;
3971
0
    int fixType = (status >> 16) & 0x0ff;
3972
0
    double lat = safe_atof(field[4]);
3973
0
    double lon = safe_atof(field[5]);
3974
0
    double altHAE = safe_atof(field[6]);
3975
0
    double altMSL = safe_atof(field[7]);
3976
0
    double pDOP = safe_atof(field[8]);
3977
0
    double hAcc = safe_atof(field[9]);
3978
0
    double vAcc = safe_atof(field[10]);
3979
0
    double vecefX = safe_atof(field[11]);
3980
0
    double vecefY = safe_atof(field[12]);
3981
0
    double vecefZ = safe_atof(field[13]);
3982
0
    double sAcc = safe_atof(field[14]);
3983
0
    double cnoMean = safe_atof(field[15]);
3984
0
    double towOffset = safe_atof(field[16]);
3985
0
    int leapS = atoi(field[17]);
3986
0
    char ts_buf[TIMESPEC_LEN];
3987
0
    char scr[128];
3988
3989
0
    switch (gpsStatus) {
3990
0
    case 0:
3991
        // no fix
3992
0
        session->newdata.status = STATUS_UNK;
3993
0
        session->newdata.mode = MODE_NO_FIX;
3994
0
        break;
3995
0
    case 1:
3996
        // DR
3997
0
        session->newdata.status = STATUS_DR;
3998
0
        session->newdata.mode = MODE_3D;
3999
0
        break;
4000
0
    case 2:
4001
        // 2D
4002
0
        session->newdata.status = STATUS_GPS;
4003
0
        session->newdata.mode = MODE_2D;
4004
0
        break;
4005
0
    case 3:
4006
        // 3D
4007
0
        session->newdata.status = STATUS_GPS;
4008
0
        session->newdata.mode = MODE_3D;
4009
0
        break;
4010
0
    case 4:
4011
        // GPSDR
4012
0
        session->newdata.status = STATUS_GNSSDR;
4013
0
        session->newdata.mode = MODE_3D;
4014
0
        break;
4015
0
    case 5:
4016
        // surveyed
4017
0
        session->newdata.status = STATUS_TIME;
4018
0
        session->newdata.mode = MODE_3D;
4019
0
        break;
4020
0
    case 8:
4021
        // DGPS
4022
0
        session->newdata.status = STATUS_DGPS;
4023
0
        session->newdata.mode = MODE_3D;
4024
0
        break;
4025
0
    case 9:
4026
        // SBAS ??
4027
0
        session->newdata.status = STATUS_GPS;
4028
0
        session->newdata.mode = MODE_3D;
4029
0
        break;
4030
0
    case 10:
4031
        // FTK SINGLE ??
4032
0
        session->newdata.status = STATUS_RTK_FLT;  // ??
4033
0
        session->newdata.mode = MODE_3D;
4034
0
        break;
4035
0
    case 11:
4036
        // FTK FLOAT
4037
0
        session->newdata.status = STATUS_RTK_FLT;
4038
0
        session->newdata.mode = MODE_3D;
4039
0
        break;
4040
0
    case 12:
4041
        // FTK FIX
4042
0
        session->newdata.status = STATUS_RTK_FIX;
4043
0
        session->newdata.mode = MODE_3D;
4044
0
        break;
4045
0
    default:
4046
        // Huh?
4047
0
        session->newdata.status = STATUS_UNK;
4048
0
        session->newdata.mode = MODE_NOT_SEEN;
4049
0
        break;
4050
0
    }
4051
0
    mask |= MODE_SET | STATUS_SET;
4052
4053
0
    if (MODE_2D == session->newdata.mode ||
4054
0
        MODE_3D == session->newdata.mode) {
4055
0
            timespec_t ts_tow;
4056
4057
0
            session->newdata.latitude = lat;
4058
0
            session->newdata.longitude = lon;
4059
0
            mask |= LATLON_SET;
4060
0
            if (MODE_3D == session->newdata.mode) {
4061
0
                session->newdata.altHAE = altHAE;
4062
0
                session->newdata.altMSL = altMSL;
4063
0
                mask |= ALTITUDE_SET;
4064
0
            }
4065
            // assume leapS is valid if we are 2D ???
4066
0
            session->context->leap_seconds = leapS;
4067
0
            session->context->valid |= LEAP_SECOND_VALID;
4068
4069
            // assume time is valid if we are 2D ???
4070
0
            MSTOTS(&ts_tow, i_tow);
4071
0
            session->newdata.time = gpsd_gpstime_resolv(session, weeks,
4072
0
                                                        ts_tow);
4073
4074
0
            mask |= (TIME_SET | NTPTIME_IS);
4075
0
    }
4076
4077
0
    GPSD_LOG(LOG_IO, &session->context->errout,
4078
0
             "NMEA0183: PGPSP: %s i_tow=%lu weeks=%d "
4079
0
             "status=x%lx used=%d gpsStatus=%d type=%d "
4080
0
             "lat=%.2f lon=%.2f "
4081
0
             "altHAE=%.2f altMSL=%.2f "
4082
0
             "pdop=%.2f hacc=%.2f vacc=%.2f sacc=%.2f "
4083
0
             "vecef: X=%.2f Y=%.2f Z=%.2f cnoMean=.%1f "
4084
0
             "towOffset=%.4f leapS=%d\n",
4085
0
             timespec_to_iso8601(session->newdata.time, scr, sizeof(scr)),
4086
0
             i_tow, weeks, status, used, gpsStatus, fixType, lat, lon,
4087
0
             altHAE, altMSL,
4088
0
             pDOP, hAcc, vAcc, sAcc,
4089
0
             vecefX, vecefY, vecefZ, cnoMean,
4090
0
             towOffset, leapS);
4091
4092
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4093
0
             "NMEA0183: PGPSP: time=%s lat=%.2f lon=%.2f "
4094
0
             "mode=%d status=%d\n",
4095
0
             timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
4096
0
             session->newdata.latitude,
4097
0
             session->newdata.longitude,
4098
0
             session->newdata.mode,
4099
0
             session->newdata.status);
4100
0
    return mask;
4101
0
}
4102
4103
// Garmin Estimated Position Error
4104
static gps_mask_t processPGRME(unsigned count UNUSED, char *field[],
4105
                               struct gps_device_t *session)
4106
0
{
4107
    /*
4108
     * $PGRME,15.0,M,45.0,M,25.0,M*22
4109
     * 1    = horizontal error estimate
4110
     * 2    = units
4111
     * 3    = vertical error estimate
4112
     * 4    = units
4113
     * 5    = spherical error estimate
4114
     * 6    = units
4115
     * *
4116
     * * Garmin won't say, but the general belief is that these are 50% CEP.
4117
     * * We follow the advice at <http://gpsinformation.net/main/errors.htm>.
4118
     * * If this assumption changes here, it should also change in garmin.c
4119
     * * where we scale error estimates from Garmin binary packets, and
4120
     * * in libgpsd_core.c where we generate $PGRME.
4121
     */
4122
0
    gps_mask_t mask = ONLINE_SET;
4123
4124
0
    if ('M' == field[2][0] &&
4125
0
        'M' == field[4][0] &&
4126
0
        'M' == field[6][0]) {
4127
0
        session->newdata.epx = session->newdata.epy =
4128
0
            safe_atof(field[1]) * (1 / sqrt(2))
4129
0
                      * (GPSD_CONFIDENCE / CEP50_SIGMA);
4130
0
        session->newdata.epv =
4131
0
            safe_atof(field[3]) * (GPSD_CONFIDENCE / CEP50_SIGMA);
4132
0
        session->newdata.sep =
4133
0
            safe_atof(field[5]) * (GPSD_CONFIDENCE / CEP50_SIGMA);
4134
0
        mask = HERR_SET | VERR_SET | PERR_IS;
4135
0
    }
4136
4137
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4138
0
             "NMEA0183: PGRME: epx=%.2f epy=%.2f sep=%.2f\n",
4139
0
             session->newdata.epx,
4140
0
             session->newdata.epy,
4141
0
             session->newdata.sep);
4142
0
    return mask;
4143
0
}
4144
4145
/* Garmin GPS Fix Data Sentence
4146
 *
4147
 * FIXME: seems to happen after cycle ender, so little happens...
4148
 */
4149
static gps_mask_t processPGRMF(unsigned count UNUSED, char *field[],
4150
                               struct gps_device_t *session)
4151
0
{
4152
 /*
4153
  * $PGRMF,290,293895,160305,093802,13,5213.1439,N,02100.6511,E,A,2,0,226,2,1*11
4154
  *
4155
  * 1 = GPS week
4156
  * 2 = GPS seconds
4157
  * 3 = UTC Date ddmmyy
4158
  * 4 = UTC time hhmmss
4159
  * 5 = GPS leap seconds
4160
  * 6 = Latitude ddmm.mmmm
4161
  * 7 = N or S
4162
  * 8 = Longitude dddmm.mmmm
4163
  * 9 = E or W
4164
  * 10 = Mode, M = Manual, A = Automatic
4165
  * 11 = Fix type, 0 = No fix, 2 = 2D fix, 2 = 3D fix
4166
  * 12 = Ground Speed, 0 to 1151 km/hr
4167
  * 13 = Course over ground, 0 to 359 degrees true
4168
  * 14 = pdop, 0 to 9
4169
  * 15 = dop, 0 to 9
4170
  */
4171
0
    gps_mask_t mask = ONLINE_SET;
4172
0
    timespec_t ts_tow = {0, 0};
4173
4174
    /* Some garmin fail due to GPS Week Roll Over
4175
     * Ignore their UTC date/time, use their GPS week, GPS tow and
4176
     * leap seconds to decide the correct time */
4177
0
    if (isdigit((int)field[5][0])) {
4178
0
        session->context->leap_seconds = atoi(field[5]);
4179
0
        session->context->valid = LEAP_SECOND_VALID;
4180
0
    }
4181
0
    if (isdigit((int)field[1][0]) &&
4182
0
        isdigit((int)field[2][0]) &&
4183
0
        0 < session->context->leap_seconds) {
4184
        // have GPS week, tow and leap second
4185
0
        unsigned short week = atol(field[1]);
4186
0
        ts_tow.tv_sec = atol(field[2]);
4187
0
        ts_tow.tv_nsec = 0;
4188
0
        session->newdata.time = gpsd_gpstime_resolv(session, week, ts_tow);
4189
0
        mask |= TIME_SET;
4190
        // (long long) cast for 32/64 bit compat
4191
0
        GPSD_LOG(LOG_SPIN, &session->context->errout,
4192
0
                 "NMEA0183: PGRMF gps time %lld\n",
4193
0
                 (long long)session->newdata.time.tv_sec);
4194
0
    } else if (0 == merge_hhmmss(field[4], session) &&
4195
0
               0 == merge_ddmmyy(field[3], session)) {
4196
        // fall back to UTC if we need and can
4197
        // (long long) cast for 32/64 bit compat
4198
0
        GPSD_LOG(LOG_SPIN, &session->context->errout,
4199
0
                 "NMEA0183: PGRMF gps time %lld\n",
4200
0
                 (long long)session->newdata.time.tv_sec);
4201
0
        mask |= TIME_SET;
4202
0
    }
4203
0
    if ('A' != field[10][0]) {
4204
        // Huh?
4205
0
        return mask;
4206
0
    }
4207
0
    if (0 == do_lat_lon(&field[6], &session->newdata)) {
4208
0
        mask |= LATLON_SET;
4209
0
    }
4210
0
    switch (field[11][0]) {
4211
0
    default:
4212
        // Huh?
4213
0
        break;
4214
0
    case '0':
4215
0
        session->newdata.mode = MODE_NO_FIX;
4216
0
        mask |= MODE_SET;
4217
0
        break;
4218
0
    case '1':
4219
0
        session->newdata.mode = MODE_2D;
4220
0
        mask |= MODE_SET;
4221
0
        break;
4222
0
    case '2':
4223
0
        session->newdata.mode = MODE_3D;
4224
0
        mask |= MODE_SET;
4225
0
        break;
4226
0
    }
4227
0
    session->newdata.speed = safe_atof(field[12]) / MPS_TO_KPH;
4228
0
    session->newdata.track = safe_atof(field[13]);
4229
0
    mask |= SPEED_SET | TRACK_SET;
4230
0
    if ('\0' != field[14][0]) {
4231
0
        session->gpsdata.dop.pdop = safe_atof(field[14]);
4232
0
        mask |= DOP_SET;
4233
0
    }
4234
0
    if ('\0' != field[15][0]) {
4235
0
        session->gpsdata.dop.tdop = safe_atof(field[15]);
4236
0
        mask |= DOP_SET;
4237
0
    }
4238
4239
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4240
0
             "NMEA0183: PGRMF: pdop %.1f tdop %.1f \n",
4241
0
             session->gpsdata.dop.pdop,
4242
0
             session->gpsdata.dop.tdop);
4243
0
    return mask;
4244
0
}
4245
4246
/* Garmin Map Datum
4247
 *
4248
 * FIXME: seems to happen after cycle ender, so nothing happens...
4249
 */
4250
static gps_mask_t processPGRMM(unsigned count UNUSED, char *field[],
4251
                               struct gps_device_t *session)
4252
0
{
4253
    /*
4254
     * $PGRMM,NAD83*29
4255
     * 1    = Map Datum
4256
     */
4257
0
    gps_mask_t mask = ONLINE_SET;
4258
4259
0
    if ('\0' != field[1][0]) {
4260
0
        strlcpy(session->newdata.datum, field[1],
4261
0
                sizeof(session->newdata.datum));
4262
0
    }
4263
4264
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4265
0
             "NMEA0183: PGRMM: datum=%.40s\n",
4266
0
             session->newdata.datum);
4267
0
    return mask;
4268
0
}
4269
4270
// Garmin Sensor Status Info
4271
static gps_mask_t processPGRMT(unsigned count UNUSED, char *field[],
4272
                               struct gps_device_t *session)
4273
0
{
4274
    /*
4275
     * $PGRMT,GPS 15x-W software ver. 4.20,,,,,,,,*6A
4276
     * 1    = Product, model and software version
4277
     * 2    = ROM Checksum test P=pass, F=fail
4278
     * 3    = Receiver failure discrete, P=pass, F=fail
4279
     * 4    = Stored data lost, R=retained, L=lost
4280
     * 5    = Real time clock lost, R=retained, L=lost
4281
     * 6    = Oscillator drift discrete, P=pass, F=excessive drift detected
4282
     * 7    = Data collection discrete, C=collecting, null if not collecting
4283
     * 8    = GPS sensor temperature in degrees C
4284
     * 9    = GPS sensor configuration data, R=retained, L=lost
4285
     *
4286
     * Output once per minuite by default.
4287
     * 50 char max.
4288
     *
4289
     * As of October 2022, only ever seen field 1 populated
4290
     */
4291
0
    gps_mask_t mask = ONLINE_SET;
4292
4293
0
    strlcpy(session->subtype, field[1], sizeof(session->subtype));
4294
4295
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4296
0
             "NMEA0183: PGRMT: subtype %s\n",
4297
0
             session->subtype);
4298
0
    return mask;
4299
0
}
4300
4301
// Garmin 3D Velocity Information
4302
static gps_mask_t processPGRMV(unsigned count UNUSED, char *field[],
4303
                               struct gps_device_t *session)
4304
0
{
4305
    /*
4306
     * $PGRMV,-2.4,-1.1,0.3*59
4307
     * 1    = true east velocity,  m/s
4308
     * 2    = true north velocity,  m/s
4309
     * 3    = true up velocity,  m/s
4310
     */
4311
0
    gps_mask_t mask = ONLINE_SET;
4312
4313
0
    if ('\0' == field[1][0] ||
4314
0
        '\0' == field[2][0] ||
4315
0
        '\0' == field[3][0]) {
4316
        // nothing to report
4317
0
        return mask;
4318
0
    }
4319
4320
0
    session->newdata.NED.velE = safe_atof(field[1]);
4321
0
    session->newdata.NED.velN = safe_atof(field[2]);
4322
0
    session->newdata.NED.velD = -safe_atof(field[3]);
4323
4324
0
    mask |= VNED_SET;
4325
4326
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4327
0
             "NMEA0183: PGRMV: velE %.2f velN %.2f velD %.2f\n",
4328
0
            session->newdata.NED.velE,
4329
0
            session->newdata.NED.velN,
4330
0
            session->newdata.NED.velD);
4331
0
    return mask;
4332
0
}
4333
4334
// Garmin Altitude Information
4335
static gps_mask_t processPGRMZ(unsigned count UNUSED, char *field[],
4336
                               struct gps_device_t *session)
4337
0
{
4338
    /*
4339
     * $PGRMZ,246,f,3*1B
4340
     * 1    = Altitude (probably MSL) in feet
4341
     * 2    = f (feet)
4342
     * 3    = Mode
4343
     *         1 = No Fix
4344
     *         2 = 2D Fix
4345
     *         3 = 3D Fix
4346
     *
4347
     * From: Garmin Proprietary NMEA 0183 Sentences
4348
     *       technical Specifications
4349
     *       190-00684-00, Revision C December 2008
4350
     */
4351
0
    gps_mask_t mask = ONLINE_SET;
4352
4353
    // codacy does not like strlen()
4354
0
    if ('f' == field[2][0] &&
4355
0
        0 < strnlen(field[1], 20)) {
4356
        // have a GPS altitude, must be 3D
4357
        // seems to be altMSL.  regressions show this matches GPGGA MSL
4358
0
        session->newdata.altMSL = atoi(field[1]) * FEET_TO_METERS;
4359
0
        mask |= (ALTITUDE_SET);
4360
0
    }
4361
0
    switch (field[3][0]) {
4362
0
    default:
4363
        // Huh?
4364
0
        break;
4365
0
    case '1':
4366
0
        session->newdata.mode = MODE_NO_FIX;
4367
0
        mask |= MODE_SET;
4368
0
        break;
4369
0
    case '2':
4370
0
        session->newdata.mode = MODE_2D;
4371
0
        mask |= MODE_SET;
4372
0
        break;
4373
0
    case '3':
4374
0
        session->newdata.mode = MODE_3D;
4375
0
        mask |= MODE_SET;
4376
0
        break;
4377
0
    }
4378
4379
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4380
0
             "NMEA0183: PGRMZ: altMSL %.2f mode %d\n",
4381
0
             session->newdata.altMSL,
4382
0
             session->newdata.mode);
4383
0
    return mask;
4384
0
}
4385
4386
// Magellan Status
4387
static gps_mask_t processPMGNST(unsigned count UNUSED, char *field[],
4388
                                struct gps_device_t *session)
4389
0
{
4390
    /*
4391
     * $PMGNST,01.75,3,T,816,11.1,-00496,00*43
4392
     * 1 = Firmware version number
4393
     * 2 = Mode (1 = no fix, 2 = 2D fix, 3 = 3D fix)
4394
     * 3 = T if we have a fix
4395
     * 4 = battery percentage left in tenths of a percent
4396
     * 5 = time left on the GPS battery in hours
4397
     * 6 = numbers change (freq. compensation?)
4398
     * 7 = PRN number receiving current focus
4399
     */
4400
0
    gps_mask_t mask = ONLINE_SET;
4401
0
    int newmode = atoi(field[3]);
4402
4403
0
    if ('T' == field[4][0]) {
4404
0
        switch(newmode) {
4405
0
        default:
4406
0
            session->newdata.mode = MODE_NO_FIX;
4407
0
            break;
4408
0
        case 2:
4409
0
            session->newdata.mode = MODE_2D;
4410
0
            break;
4411
0
        case 3:
4412
0
            session->newdata.mode = MODE_3D;
4413
0
            break;
4414
0
        }
4415
0
    } else {
4416
        // Can report 3D fix, but 'F' for no fix
4417
0
        session->newdata.mode = MODE_NO_FIX;
4418
0
    }
4419
0
    mask |= MODE_SET;
4420
4421
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4422
0
             "NMEA0183: PMGNST: mode: %d\n",
4423
0
             session->newdata.mode);
4424
0
    return mask;
4425
0
}
4426
4427
static gps_mask_t processPMTK001(unsigned count UNUSED, char *field[],
4428
                                 struct gps_device_t *session)
4429
0
{
4430
0
    int reason;
4431
0
    const char *mtk_reasons[] = {
4432
0
        "Invalid",
4433
0
        "Unsupported",
4434
0
        "Valid but Failed",
4435
0
        "Valid success",       // unused, see above
4436
0
        "Unknown",             // gpsd only
4437
0
    };
4438
4439
    // ACK / NACK
4440
0
    reason = atoi(field[2]);
4441
0
    if (4 == reason) {
4442
        // ACK
4443
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
4444
0
                 "NMEA0183: MTK ACK: %s\n", field[1]);
4445
0
        return ONLINE_SET;
4446
0
    }
4447
4448
    // else, NACK
4449
0
    if (0 > reason ||
4450
0
        3 < reason) {
4451
        // WTF?
4452
0
        reason = 4;
4453
0
    }
4454
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
4455
0
             "NMEA0183: MTK NACK: %s, reason: %s\n",
4456
0
             field[1], mtk_reasons[reason]);
4457
0
    return ONLINE_SET;
4458
0
}
4459
4460
static gps_mask_t processPMTK424(unsigned count UNUSED, char *field[],
4461
                                 struct gps_device_t *session)
4462
0
{
4463
    // PPS pulse width response
4464
    /*
4465
     * Response will look something like: $PMTK424,0,0,1,0,69*12
4466
     * The pulse width is in field 5 (69 in this example).  This
4467
     * sentence is poorly documented at:
4468
     * http://www.trimble.com/embeddedsystems/condor-gps-module.aspx?dtID=documentation
4469
     *
4470
     * Packet Type: 324 PMTK_API_SET_OUTPUT_CTL
4471
     * Packet meaning
4472
     * Write the TSIP/antenna/PPS configuration data to the Flash memory.
4473
     * DataField [Data0]:TSIP Packet[on/off]
4474
     * 0 - Disable TSIP output (Default).
4475
     * 1 - Enable TSIP output.
4476
     * [Data1]:Antenna Detect[on/off]
4477
     * 0 - Disable antenna detect function (Default).
4478
     * 1 - Enable antenna detect function.
4479
     * [Data2]:PPS on/off
4480
     * 0 - Disable PPS function.
4481
     * 1 - Enable PPS function (Default).
4482
     * [Data3]:PPS output timing
4483
     * 0 - Always output PPS (Default).
4484
     * 1 - Only output PPS when GPS position is fixed.
4485
     * [Data4]:PPS pulse width
4486
     * 1~16367999: 61 ns~(61x 16367999) ns (Default = 69)
4487
     *
4488
     * The documentation does not give the units of the data field.
4489
     * Andy Walls <andy@silverblocksystems.net> says:
4490
     *
4491
     * "The best I can figure using an oscilloscope, is that it is
4492
     * in units of 16.368000 MHz clock cycles.  It may be
4493
     * different for any other unit other than the Trimble
4494
     * Condor. 69 cycles / 16368000 cycles/sec = 4.216 microseconds
4495
     * [which is the pulse width I have observed]"
4496
     *
4497
     * Support for this theory comes from the fact that crystal
4498
     * TXCOs with a 16.368MHZ period are commonly available from
4499
     * multiple vendors. Furthermore, 61*69 = 4209, which is
4500
     * close to the observed cycle time and suggests that the
4501
     * documentation is trying to indicate 61ns units.
4502
     *
4503
     * He continues:
4504
     *
4505
     * "I chose [127875] because to divides 16368000 nicely and the
4506
     * pulse width is close to 1/100th of a second.  Any number
4507
     * the user wants to use would be fine.  127875 cycles /
4508
     * 16368000 cycles/second = 1/128 seconds = 7.8125
4509
     * milliseconds"
4510
     */
4511
4512
    // too short?  Make it longer
4513
0
    if (127875 > atoi(field[5])) {
4514
0
        (void)nmea_send(session, "$PMTK324,0,0,1,0,127875");
4515
0
    }
4516
0
    return ONLINE_SET;
4517
0
}
4518
4519
static gps_mask_t processPMTK705(unsigned count, char *field[],
4520
                                 struct gps_device_t *session)
4521
0
{
4522
    /* Trimble version:
4523
     * $PMTK705,AXN_1.30,0000,20090609,*20<CR><LF>
4524
     *
4525
     * 0 PMTK705
4526
     * 1 ReleaseStr - Firmware release name and version
4527
     * 2 Build_ID   - Build ID
4528
     * 3 Date code  - YYYYMMDD
4529
     * 4 Checksum
4530
     *
4531
     * Quectel Querk.  L26.
4532
     * $PMTK705,AXN_3.20_3333_13071501,0003,QUECTEL-L26,*1E<CR><LF>
4533
     *
4534
     * 0 PMTK705
4535
     * 1 ReleaseStr - Firmware release name and version
4536
     * 2 Build_ID   - Build ID
4537
     * 3 Date code  - Product Model
4538
     * 4 SDK Version (optional)
4539
     * * Checksum
4540
    */
4541
4542
    // set device subtype
4543
0
    if (4 == count) {
4544
0
        (void)snprintf(session->subtype, sizeof(session->subtype),
4545
0
                       "%s,%s,%s",
4546
0
                       field[1], field[2], field[3]);
4547
0
    } else {
4548
        // Once again Quectel goes their own way...
4549
0
        (void)snprintf(session->subtype, sizeof(session->subtype),
4550
0
                       "%s,%s,%s,%s",
4551
0
                       field[1], field[2], field[3], field[4]);
4552
0
    }
4553
4554
0
    if ('\0' == session->subtype1[0]) {
4555
        /* Query for the Quectel firmware version.
4556
         * Quectel GPS receivers containing an MTK chipset use
4557
         * this command to return their FW version.
4558
         * From
4559
         * https://forums.quectel.com/t/determine-nmea-version-of-l76-l/3882/5
4560
         * "$PQVERNO is an internal command and used to query Quectel FW
4561
         * version. We haven’t added this internal command in GNSS
4562
         * protocol spec."
4563
         */
4564
0
        (void)nmea_send(session, "$PQVERNO,R");
4565
0
    }
4566
4567
0
    return ONLINE_SET;
4568
0
}
4569
4570
static gps_mask_t processPQxERR(unsigned count UNUSED, char* field[],
4571
                                struct gps_device_t* session)
4572
0
{
4573
    /* Quectel generic PQxxxERRROR message handler
4574
     * The messages are content free, not very useful.
4575
     *
4576
     * $PQTMCFGEINSMSGERROR*4A
4577
     * $PQTMCFGORIENTATIONERROR*54
4578
     * $PQTMCFGWHEELTICKERROR*44
4579
     * $PQTMQMPTERROR*58
4580
     */
4581
4582
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
4583
0
             "NMEA0183: %s Error\n", field[0]);
4584
0
    return ONLINE_SET;
4585
0
}
4586
4587
static gps_mask_t processPQxOK(unsigned count UNUSED, char* field[],
4588
                               struct gps_device_t* session)
4589
0
{
4590
    /* Quectel generic PQTMxxxOK message handler
4591
     * The messages are content free, not very useful.
4592
     *
4593
     * $PQTMCFGEINSMSGOK*16
4594
     * $PQTMCFGORIENTATIONOK*08
4595
     * $PQTMCFGWHEELTICKOK*18
4596
     */
4597
4598
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4599
0
             "NMEA0183: %s OK\n", field[0]);
4600
0
    return ONLINE_SET;
4601
0
}
4602
4603
// Quectel $PQTMGPS - GNSS position stuff
4604
static gps_mask_t processPQTMGPS(unsigned count UNUSED, char *field[],
4605
                                 struct gps_device_t *session)
4606
0
{
4607
    /*
4608
     * $PQTMGPS,671335,463792.000,31.822084600,117.115221100,59.4260,63.0420,
4609
     *  0.0270,-171.7101,5.9890,1.3300,2.1100,3,18,*75
4610
     *
4611
     * 1   Milliseconds since turn on. 32-bit unsigned integer.
4612
     * 2   Time of week. Seconds
4613
     * 3   Latitude. Degrees
4614
     * 4   Longitude. Degrees
4615
     * 5   Height above ellipsoid, Meters
4616
     * 6   Altitude above mean-sea-level. Meters
4617
     * 7   Ground speed (2D). Meters / sec
4618
     * 8   Heading (2D). Degrees.
4619
     * 9   Horizontal accuracy estimate. Meters.
4620
     * 10  HDOP
4621
     * 11  PDOP
4622
     * 12  Fix type.  0 = No fix.  2 = 2D fix.  3 = 3D fix.
4623
     * 13  Number of navigation satellites (seen? used?)
4624
     *
4625
     * Note: incomplete time stamp.
4626
     */
4627
0
    gps_mask_t mask = ONLINE_SET;
4628
0
    unsigned ts = atoi(field[1]);
4629
0
    unsigned tow = atoi(field[2]);
4630
0
    double lat = safe_atof(field[3]);
4631
0
    double lon = safe_atof(field[4]);
4632
0
    double hae = safe_atof(field[5]);
4633
0
    double msl = safe_atof(field[6]);
4634
0
    double speed = safe_atof(field[7]);
4635
0
    double heading = safe_atof(field[8]);
4636
0
    double hAcc = safe_atof(field[9]);
4637
0
    double hdop = safe_atof(field[10]);
4638
0
    double pdop = safe_atof(field[11]);
4639
0
    unsigned fix = atoi(field[12]);
4640
0
    unsigned numsat = atoi(field[13]);
4641
4642
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4643
0
             "NMEA0183: PQTMGPS ts %u tow %u lat %.9f lon %.9f HAE %.4f "
4644
0
             "MSL %.4f speed %.4f head %.4f hacc %.4f hdop %.4f pdop %.4f "
4645
0
             "mode %u nsat %u\n",
4646
0
             ts, tow, lat, lon, hae, msl, speed, heading, hAcc, hdop,
4647
0
             pdop, fix, numsat);
4648
0
    return mask;
4649
0
}
4650
4651
// Quectel $PQTMIMU - IMU Raw Data
4652
static gps_mask_t processPQTMIMU(unsigned count UNUSED, char *field[],
4653
                                 struct gps_device_t *session)
4654
0
{
4655
    /*
4656
     * $PQTMIMU,42634,-0.006832,-0.022814,1.014552,0.315000,-0.402500,
4657
       -0.332500,0,0*55
4658
     *
4659
     * 1   Milliseconds since turn on. 32-bit unsigned integer.
4660
     * 2   Acceleration in X-axis direction. g
4661
     * 3   Acceleration in Y-axis direction. g
4662
     * 4   Acceleration in A-axis direction. g
4663
     * 5   Angular rate in X-axis direction. Degrees / second
4664
     * 6   Angular rate in y-axis direction. Degrees / second
4665
     * 7   Angular rate in Z-axis direction. Degrees / second
4666
     * 8   Cumulative ticks
4667
     * 9   Timestamp of last tick
4668
     */
4669
0
    gps_mask_t mask = ONLINE_SET;
4670
0
    unsigned ts = atoi(field[1]);
4671
0
    double accX = safe_atof(field[2]);
4672
0
    double accY = safe_atof(field[3]);
4673
0
    double accZ = safe_atof(field[4]);
4674
0
    double rateX = safe_atof(field[5]);
4675
0
    double rateY = safe_atof(field[6]);
4676
0
    double rateZ = safe_atof(field[7]);
4677
0
    unsigned ticks = atoi(field[8]);
4678
0
    unsigned tick_ts = atoi(field[9]);
4679
4680
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4681
0
             "NMEA0183: PQTMIMU ts %u accX %.6f accY %.6f accZ %.6f "
4682
0
             "rateX %.6f rateY %.6f rateZ %.6f ticks %u tick_ts %u\n",
4683
0
             ts, accX, accY, accZ, rateX, rateY, rateZ, ticks, tick_ts);
4684
0
    return mask;
4685
0
}
4686
4687
// Quectel $PQTMINS - DR Nav results
4688
static gps_mask_t processPQTMINS(unsigned count UNUSED, char *field[],
4689
                                 struct gps_device_t *session)
4690
0
{
4691
    /*
4692
     * $PQTMINS,42529,1,31.822038000,117.115182800,67.681000,,,,-0.392663,
4693
        1.300793,0.030088*4D
4694
     *
4695
     * 1   Milliseconds since turn on. 32-bit unsigned integer.
4696
     * 2   Solution type, 0 = Pitch and Roll, 1 = GNSS, pitch, roll, heading
4697
     *                    2 = GNSS + DR, 3 = DR Only
4698
     * 3   Latitude. Degrees
4699
     * 4   Longitude. Degrees
4700
     * 5   Height (HAE?, MSL?) , Meters
4701
     * 6   Northward velocity
4702
     * 7   Eastward velocity
4703
     * 8   Downward velocity
4704
     * 9   Roll
4705
     * 10  Pitch
4706
     * 11  Heading
4707
     *
4708
     */
4709
0
    gps_mask_t mask = ONLINE_SET;
4710
0
    unsigned ts = atoi(field[1]);
4711
0
    unsigned sol = atoi(field[2]);
4712
0
    double lat = safe_atof(field[3]);
4713
0
    double lon = safe_atof(field[4]);
4714
0
    double alt = safe_atof(field[5]);
4715
0
    double velN = safe_atof(field[6]);
4716
0
    double velE = safe_atof(field[7]);
4717
0
    double velD = safe_atof(field[8]);
4718
0
    double roll = safe_atof(field[9]);
4719
0
    double pitch = safe_atof(field[10]);
4720
0
    double head = safe_atof(field[11]);
4721
4722
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4723
0
             "NMEA0183: PQTMINS ts %u sol %u lat %.9f lon %.9f alt %.6f "
4724
0
             "velN %.6f velE %.6f velD %.6f roll %.6f pitch %.6f head %.6f\n",
4725
0
             ts, sol, lat, lon, alt, velN, velE, velD, roll, pitch, head);
4726
0
    return mask;
4727
0
}
4728
4729
// Quectel $PQTMVER - Firmware info
4730
static gps_mask_t processPQTMVER(unsigned count UNUSED, char *field[],
4731
                                 struct gps_device_t *session)
4732
0
{
4733
    /*
4734
     * $PQTMVER,MODULE_L89HANR01A06S,2022/07/28,18:27:04*7A
4735
     *
4736
     * 1   Version
4737
     * 2   build date yyyy/mm/dd
4738
     * 3   build time hh:mm:ss
4739
     *
4740
     */
4741
0
    char obuf[128];                      // temp version string buffer
4742
0
    gps_mask_t mask = ONLINE_SET;
4743
4744
    // save as subtype
4745
0
    (void)snprintf(obuf, sizeof(obuf),
4746
0
             "%s %.12s %.10s",
4747
0
             field[1], field[2], field[3]);
4748
4749
    // save what we can
4750
0
    (void)strlcpy(session->subtype, obuf, sizeof(session->subtype) - 1);
4751
4752
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4753
0
             "NMEA0183: PQTMVER %s\n",
4754
0
             session->subtype);
4755
4756
0
    return mask;
4757
0
}
4758
4759
static gps_mask_t processPQVERNO(unsigned count UNUSED, char* field[],
4760
                                 struct gps_device_t* session)
4761
0
{
4762
    /* Example request & response are provided courtesy of Quectel below.
4763
     * This command is not publicly documented, but Quectel support
4764
     * provided this description via email. This has been tested on
4765
     * Quectel version L70-M39, but all recent (2022) versions of Quectel
4766
     * support this command is well.
4767
     *
4768
     * Request:
4769
     * $PQVERNO,R*3F
4770
     *
4771
     * Response:
4772
     * $PQVERNO,R,L96NR01A03S,2018/07/30,04:17*6B
4773
     *
4774
     * Description of the 6 fields are below.
4775
     *
4776
     * 1. $PQVERNO,              Query command
4777
     * 2. R,                     Read
4778
     * 3. L96NR01A03S,           Quectel firmware version number
4779
     * 4. 2018/07/30,            Firmware build date
4780
     * 5. 04:17*                 Firmware build time
4781
     * 6. 6B                     Checksum
4782
     */
4783
4784
0
    if (0 == strncmp(session->nmea.field[0], "PQVERNO", sizeof("PQVERNO")) &&
4785
0
        '\0' != field[2][0]) {
4786
0
        (void)snprintf(session->subtype1, sizeof(session->subtype1),
4787
0
                       "%s,%s,%s",
4788
0
                       field[2], field[3], field[4]);
4789
0
    }
4790
4791
0
    return ONLINE_SET;
4792
0
}
4793
4794
/* smart watch sensors
4795
 * A stub.
4796
 */
4797
static gps_mask_t processPRHS(unsigned count UNUSED, char *field[],
4798
                              struct gps_device_t *session)
4799
0
{
4800
    /*
4801
     * $PRHS ,type,....
4802
     *   type = message type
4803
     *
4804
     * Yes: $PRHS[space],
4805
     *
4806
     * types:
4807
     * $PRHS ,ACC,9.952756,0.37819514,1.3165021,20150305072428436*44
4808
     * $PRHS ,COM,238.09642,16.275442,82.198425,20150305072428824*43
4809
     * $PRHS ,GYR,0.0,0.0,0.0,20150305072428247*4D
4810
     * $PRHS ,LAC,0.23899937,0.009213656,0.02143073,20150305072428437*46
4811
     * $PRHS ,MAG,47.183502,-51.789,-2.7145,20150305072428614*41
4812
     * $PRHS ,ORI,187.86511,-2.1546898,-82.405205,20150305072428614*53
4813
     * $PRHS ,RMC,20150305072427985*55
4814
     *
4815
     */
4816
0
    gps_mask_t mask = ONLINE_SET;
4817
4818
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
4819
0
             "NMEA0183: PRHS: type %s\n",
4820
0
             field[1]);
4821
0
    return mask;
4822
0
}
4823
4824
static gps_mask_t processPSRFEPE(unsigned count UNUSED, char *field[],
4825
                                 struct gps_device_t *session)
4826
0
{
4827
    /*
4828
     * $PSRFEPE,100542.000,A,0.7,6.82,10.69,0.0,180.0*24
4829
     * 1    = UTC Time hhmmss.sss
4830
     * 2    = Status.  A = Valid, V = Data not valid
4831
     * 3    = HDOP
4832
     * 4    = EHPE meters (Estimated Horizontal Position Error)
4833
     * 5    = EVPE meters (Estimated Vertical Position Error)
4834
     * 6    = EHVE meters (Estimated Speed Over Ground/Velocity Error)
4835
     * 7    = EHE degrees (Estimated Heading Error)
4836
     *
4837
     * SiRF won't say if these are 1-sigma or what...
4838
     */
4839
0
    gps_mask_t mask = STATUS_SET;
4840
4841
    // get time/ valid or not
4842
0
    if ('\0' != field[1][0]) {
4843
0
        if (0 == merge_hhmmss(field[1], session)) {
4844
0
            register_fractional_time(field[0], field[1], session);
4845
0
            if (0 == session->nmea.date.tm_year) {
4846
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
4847
0
                         "NMEA0183: can't use PSRFEPE time until after ZDA "
4848
0
                         "or RMC has supplied a year.\n");
4849
0
            } else {
4850
0
                mask |= TIME_SET;
4851
0
            }
4852
0
        }
4853
0
    }
4854
0
    if ('A' != field[2][0]) {
4855
        // Huh?
4856
0
        return mask;
4857
0
    }
4858
4859
0
    if ('\0' != field[3][0]) {
4860
        /* This adds nothing, it just agrees with the gpsd calculation
4861
         * from the skyview.  Which is a nice confirmation. */
4862
0
        session->gpsdata.dop.hdop = safe_atof(field[3]);
4863
0
        mask |= DOP_SET;
4864
0
    }
4865
0
    if ('\0' != field[4][0]) {
4866
        // EHPE (Estimated Horizontal Position Error)
4867
0
        session->newdata.eph = safe_atof(field[4]);
4868
0
        mask |= HERR_SET;
4869
0
    }
4870
4871
0
    if ('\0' != field[5][0]) {
4872
        // Estimated Vertical Position Error (meters, 0.01 resolution)
4873
0
        session->newdata.epv = safe_atof(field[5]);
4874
0
        mask |= VERR_SET;
4875
0
    }
4876
4877
0
    if ('\0' != field[6][0]) {
4878
        // Estimated Horizontal Speed Error meters/sec
4879
0
        session->newdata.eps = safe_atof(field[6]);
4880
0
    }
4881
4882
0
    if ('\0' != field[7][0]) {
4883
        // Estimated Heading Error degrees
4884
0
        session->newdata.epd = safe_atof(field[7]);
4885
0
    }
4886
4887
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4888
0
             "NMEA0183: PSRFEPE: hdop=%.1f eph=%.1f epv=%.1f eps=%.1f "
4889
0
             "epd=%.1f\n",
4890
0
             session->gpsdata.dop.hdop,
4891
0
             session->newdata.eph,
4892
0
             session->newdata.epv,
4893
0
             session->newdata.eps,
4894
0
             session->newdata.epd);
4895
0
    return mask;
4896
0
}
4897
4898
/*  Recommended Minimum 3D GNSS Data
4899
 *  Skytaq
4900
 */
4901
static gps_mask_t processPSTI030(unsigned count UNUSED, char *field[],
4902
                                 struct gps_device_t *session)
4903
0
{
4904
    /*
4905
     * $PSTI,030,hhmmss.sss,A,dddmm.mmmmmmm,a,dddmm.mmmmmmm,a,x.x,
4906
            x.x,x.x,x.x,ddmmyy,a.x.x,x.x*hh<CR><LF>
4907
     * 1     030          Sentence ID
4908
     * 2     225446.334   Time of fix 22:54:46 UTC
4909
     * 3     A            Status of Fix: A = Autonomous, valid;
4910
     *                                 V = invalid
4911
     * 4,5   4916.45,N    Latitude 49 deg. 16.45 min North
4912
     * 6,7   12311.12,W   Longitude 123 deg. 11.12 min West
4913
     * 8     103.323      Mean Sea Level meters
4914
     * 9     0.00         East Velocity meters/sec
4915
     * 10    0.00         North Velocity meters/sec
4916
     * 11    0.00         Up Velocity meters/sec
4917
     * 12    181194       Date of fix  18 November 1994
4918
     * 13    A            FAA mode indicator
4919
     *                        See faa_mode() for possible mode values.
4920
     * 14    1.2          RTK Age
4921
     * 15    4.2          RTK Ratio
4922
     * 16    *68          mandatory nmea_checksum
4923
     *
4924
     * In private email, SkyTraq says F mode is 10x more accurate
4925
     * than R mode.
4926
     */
4927
0
    gps_mask_t mask = ONLINE_SET;
4928
4929
0
    if (0 != strncmp(session->device_type->type_name, "Skytraq", 7)) {
4930
        // this is skytraq, but not marked yet, so probe for Skytraq
4931
        // Send MID 0x02, to get back MID 0x80
4932
0
        (void)gpsd_write(session, "\xA0\xA1\x00\x02\x02\x01\x03\x0d\x0a",9);
4933
0
    }
4934
4935
0
    if ('V' == field[3][0] ||
4936
0
        'N' == field[13][0]) {
4937
        // nav warning, or FAA not valid, ignore the rest of the data
4938
0
        session->newdata.status = STATUS_UNK;
4939
0
        session->newdata.mode = MODE_NO_FIX;
4940
0
        mask |= MODE_SET | STATUS_SET;
4941
0
    } else if ('A' == field[3][0]) {
4942
0
        double east, north, climb, age, ratio;
4943
4944
        // data valid
4945
0
        if ('\0' != field[2][0] &&
4946
0
            '\0' != field[12][0]) {
4947
            // good date and time
4948
0
            if (0 == merge_hhmmss(field[2], session) &&
4949
0
                0 == merge_ddmmyy(field[12], session)) {
4950
0
                mask |= TIME_SET;
4951
0
                register_fractional_time( "PSTI030", field[2], session);
4952
0
            }
4953
0
        }
4954
0
        if (0 == do_lat_lon(&field[4], &session->newdata)) {
4955
0
            session->newdata.mode = MODE_2D;
4956
0
            mask |= LATLON_SET;
4957
0
            if ('\0' != field[8][0]) {
4958
                // altitude is MSL
4959
0
                session->newdata.altMSL = safe_atof(field[8]);
4960
0
                mask |= ALTITUDE_SET;
4961
0
                session->newdata.mode = MODE_3D;
4962
                // Let gpsd_error_model() deal with geoid_sep and altHAE
4963
0
            }
4964
0
            mask |= MODE_SET;
4965
0
        }
4966
        /* convert ENU to track
4967
         * this has more precision than GPVTG, GPVTG comes earlier
4968
         * in the cycle */
4969
0
        east = safe_atof(field[9]);     // east velocity m/s
4970
0
        north = safe_atof(field[10]);   // north velocity m/s
4971
0
        climb = safe_atof(field[11]);   // up velocity m/s
4972
0
        age = safe_atof(field[14]);
4973
0
        ratio = safe_atof(field[15]);
4974
4975
0
        session->newdata.NED.velN = north;
4976
0
        session->newdata.NED.velE = east;
4977
0
        session->newdata.NED.velD = -climb;
4978
0
        if (0.05 < (age + ratio)) {
4979
            // don't report age == ratio == 0.0
4980
0
            session->newdata.dgps_age = age;
4981
0
            session->newdata.base.ratio = ratio;
4982
0
        }
4983
4984
0
        mask |= VNED_SET | STATUS_SET;
4985
4986
0
        session->newdata.status = faa_mode(field[13][0]);
4987
0
        if (STATUS_RTK_FIX == session->newdata.status ||
4988
0
            STATUS_RTK_FLT == session->newdata.status) {
4989
            // RTK_FIX or RTK_FLT
4990
0
            session->gpsdata.fix.base.status = session->newdata.status;
4991
0
        }
4992
0
    }
4993
4994
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
4995
0
             "NMEA0183: PSTI,030: ddmmyy=%s hhmmss=%s lat=%.2f lon=%.2f "
4996
0
             "status=%d, RTK(Age=%.1f Ratio=%.1f) faa mode %s(%s)\n",
4997
0
             field[12], field[2],
4998
0
             session->newdata.latitude,
4999
0
             session->newdata.longitude,
5000
0
             session->newdata.status,
5001
0
             session->newdata.dgps_age,
5002
0
             session->newdata.base.ratio,
5003
0
             field[13], char2str(field[13][0], c_faa_mode));
5004
0
    return mask;
5005
0
}
5006
5007
/* Skytraq RTK Baseline, fixed base to rover or moving base
5008
 * Same as $PSTI.035, except that is moving base to rover
5009
 * PX1172RH
5010
 */
5011
static gps_mask_t processPSTI032(unsigned count UNUSED, char *field[],
5012
                                 struct gps_device_t *session)
5013
0
{
5014
    /*
5015
     * $PSTI,032,041457.000,170316,A,R,0.603,‐0.837,‐0.089,1.036,144.22,,,,,*1B
5016
     *
5017
     * 2  UTC time,  hhmmss.sss
5018
     * 3  UTC Date, ddmmyy
5019
     * 4  Status, ‘V’ = Void ‘A’ = Active
5020
     * 5  Mode indicator, 'O' = Float RTK, ‘F’ = Float RTK. ‘R’ = Fixed RTK
5021
     * 6  East‐projection of baseline, meters
5022
     * 7  North‐projection of baseline, meters
5023
     * 8  Up‐projection of baseline, meters
5024
     * 9  Baseline length, meters
5025
     * 10 Baseline course 144.22, true degrees
5026
     * 11 Reserved
5027
     * 12 Reserved
5028
     * 13 Reserved
5029
     * 14 Reserved
5030
     * 15 Reserved
5031
     * 16 Checksum
5032
     */
5033
0
    gps_mask_t mask = ONLINE_SET;
5034
0
    struct baseline_t *base = &session->newdata.base;
5035
5036
0
    if ('A' != field[4][0]) {
5037
        //  status not valid
5038
0
        return mask;
5039
0
    }
5040
5041
    // Status Valid
5042
0
    if ('\0' != field[2][0] &&
5043
0
        '\0' != field[3][0]) {
5044
        // have date and time
5045
0
        if (0 == merge_hhmmss(field[2], session) &&
5046
0
            0 == merge_ddmmyy(field[3], session)) {
5047
            // good date and time
5048
0
            mask |= TIME_SET;
5049
0
            register_fractional_time("PSTI032", field[2], session);
5050
0
        }
5051
0
    }
5052
5053
0
    if ('F' == field[5][0] ||
5054
0
        'O' == field[5][0]) {
5055
        // Floating point RTK
5056
        // 'O' is undocuemented, private email says it is just a crappy 'F'.
5057
0
        base->status = STATUS_RTK_FLT;
5058
0
    } else if ('R' == field[5][0]) {
5059
        // Fixed point RTK
5060
0
        base->status = STATUS_RTK_FIX;
5061
0
    } else {
5062
        // WTF?
5063
0
        return mask;
5064
0
    }
5065
5066
0
    base->east = safe_atof(field[6]);
5067
0
    base->north = safe_atof(field[7]);
5068
0
    base->up = safe_atof(field[8]);
5069
0
    base->length = safe_atof(field[9]);
5070
0
    base->course = safe_atof(field[10]);
5071
5072
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5073
0
             "NMEA0183: PSTI,032: RTK Baseline mode %d E %.3f  N %.3f  U %.3f "
5074
0
             "length %.3f course %.3f\n",
5075
0
             base->status, base->east, base->north, base->up,
5076
0
             base->length, base->course);
5077
0
    return mask;
5078
0
}
5079
5080
/* Skytraq  RTK RAW Measurement Monitoring Data
5081
 */
5082
static gps_mask_t processPSTI033(unsigned count UNUSED, char *field[],
5083
                                 struct gps_device_t *session)
5084
0
{
5085
    /*
5086
     * $PSTI,033,hhmmss.sss,ddmmyy,x,R,x,G,x,x,,,C,x,x,,,E,x,x,,,R,x,x,,*hh
5087
     * $PSTI,033,110431.000,150517,2,R,1,G,1,0,,,C,0,0,,,E,0,0,,,R,0,0,,*72
5088
     *
5089
     * 2  UTC time,  hhmmss.sss
5090
     * 3  UTC Date, ddmmyy
5091
     * 4  "2", version
5092
     * 5  Receiver, R = Rover, B = Base
5093
     * 6  total cycle‐slipped raw measurements
5094
     * 7  "G", GPS
5095
     * 8  cycle slipped L1
5096
     * 9  cycle slipped L2
5097
     * 10 reserved
5098
     * 11 reserved
5099
     * 12 "C", BDS
5100
     * 12 cycle slipped B1
5101
     * 14 cycle slipped B2
5102
     * 15 reserved
5103
     * 16 reserved
5104
     * 17 "E", Galileo
5105
     * 18 cycle slipped E1
5106
     * 19 cycle slipped E5b
5107
     * 20 reserved
5108
     * 21 reserved
5109
     * 22 "R", GLONASS
5110
     * 23 cycle slipped G1
5111
     * 24 cycle slipped G2
5112
     * 25 reserved
5113
     * 26 reserved
5114
     * 27 Checksum
5115
     */
5116
0
    gps_mask_t mask = ONLINE_SET;
5117
0
    char receiver;
5118
0
    unsigned total, L1, L2, B1, B2, E1, E5b, G1, G2;
5119
5120
0
    if ('2' != field[4][0]) {
5121
        //  we only understand version 2
5122
0
        return mask;
5123
0
    }
5124
0
    if ('B' != field[5][0] &&
5125
0
        'R' != field[5][0]) {
5126
        //  Huh?  Rover or Base
5127
0
        return mask;
5128
0
    }
5129
0
    receiver = field[5][0];
5130
5131
0
    if ('\0' != field[2][0] &&
5132
0
        '\0' != field[3][0]) {
5133
        // have date and time
5134
0
        if (0 == merge_hhmmss(field[2], session) &&
5135
0
            0 == merge_ddmmyy(field[3], session)) {
5136
            // good date and time
5137
0
            mask |= TIME_SET;
5138
0
            register_fractional_time("PSTI033", field[2], session);
5139
0
        }
5140
0
    }
5141
0
    total = atoi(field[6]);
5142
0
    L1 = atoi(field[7]);
5143
0
    L2 = atoi(field[8]);
5144
0
    B1 = atoi(field[13]);
5145
0
    B2 = atoi(field[14]);
5146
0
    E1 = atoi(field[18]);
5147
0
    E5b = atoi(field[19]);
5148
0
    G1 = atoi(field[23]);
5149
0
    G2 = atoi(field[24]);
5150
5151
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5152
0
             "NMEA0183: PSTI,033: RTK RAW receiver %c Slips: total %u L1 %u "
5153
0
             "L2 %u B1 %u B2 %u E1 %u E5b %u G1 %u G2 %u\n",
5154
0
             receiver, total, L1, L2, B1, B2, E1, E5b, G1, G2);
5155
0
    return mask;
5156
0
}
5157
5158
/* Skytraq RTK Baseline, moving base to rover
5159
 * Same as $PSTI.032, except that is moving base to rover
5160
 * PX1172RH
5161
 */
5162
static gps_mask_t processPSTI035(unsigned count UNUSED, char *field[],
5163
                                 struct gps_device_t *session)
5164
0
{
5165
    /*
5166
     * $PSTI,035,041457.000,170316,A,R,0.603,‐0.837,‐0.089,1.036,144.22,,,,,*1B
5167
     *
5168
     * 2  UTC time,  hhmmss.sss
5169
     * 3  UTC Date, ddmmyy
5170
     * 4  Status, ‘V’ = Void ‘A’ = Active
5171
     * 5  Mode indicator, ‘F’ = Float RTK. ‘R’ = FIxed RTK
5172
     * 6  East‐projection of baseline, meters
5173
     * 7  North‐projection of baseline, meters
5174
     * 8  Up‐projection of baseline, meters
5175
     * 9  Baseline length, meters
5176
     * 10 Baseline course 144.22, true degrees
5177
     * 11 Reserved
5178
     * 12 Reserved
5179
     * 13 Reserved
5180
     * 14 Reserved
5181
     * 15 Reserved
5182
     * 16 Checksum
5183
     */
5184
5185
0
    gps_mask_t mask = ONLINE_SET;
5186
    // should this be fix, not attitude??
5187
0
    struct baseline_t *base = &session->gpsdata.attitude.base;
5188
5189
    // RTK Baseline Data of Rover Moving Base Receiver
5190
0
    if ('\0' != field[2][0] &&
5191
0
        '\0' != field[3][0]) {
5192
        // good date and time
5193
0
        if (0 == merge_hhmmss(field[2], session) &&
5194
0
            0 == merge_ddmmyy(field[3], session)) {
5195
0
            mask |= TIME_SET;
5196
0
            register_fractional_time( "PSTI035", field[2], session);
5197
0
        }
5198
0
    }
5199
0
    if ('A' != field[4][0]) {
5200
        // No valid data, except time, sort of
5201
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
5202
0
                 "NMEA0183: PSTI,035: not valid\n");
5203
0
        base->status = STATUS_UNK;
5204
0
        return mask;
5205
0
    }
5206
0
    if ('F' == field[5][0]) {
5207
        // Float RTX
5208
0
        base->status = STATUS_RTK_FLT;
5209
0
    } else if ('R' == field[5][0]) {
5210
        // Fix RTX
5211
0
        base->status = STATUS_RTK_FIX;
5212
0
    } // else ??
5213
5214
0
    base->east = safe_atof(field[6]);
5215
0
    base->north = safe_atof(field[7]);
5216
0
    base->up = safe_atof(field[8]);
5217
0
    base->length = safe_atof(field[9]);
5218
0
    base->course = safe_atof(field[10]);
5219
0
    mask |= ATTITUDE_SET;
5220
5221
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5222
0
             "NMEA0183: PSTI,035: RTK Baseline mode %d E %.3f  N %.3f  U %.3f "
5223
0
             "length %.3f course %.3f\n",
5224
0
             base->status, base->east, base->north, base->up,
5225
0
             base->length, base->course);
5226
0
    return mask;
5227
0
}
5228
5229
// Skytraq PSTI,036 – Heading, Pitch and Roll
5230
// PX1172RH
5231
static gps_mask_t processPSTI036(unsigned count UNUSED, char *field[],
5232
                                 struct gps_device_t *session)
5233
0
{
5234
    /*
5235
     * $PSTI,036,054314.000,030521,191.69,‐16.35,0.00,R*4D
5236
     *
5237
     * 2  UTC time,  hhmmss.sss
5238
     * 3  UTC Date, ddmmyy
5239
     * 4  Heading, 0 - 359.9, when mode == R, degrees
5240
     * 5  Pitch, -90 - 90, when mode == R, degrees
5241
     * 6  Roll, -90 - 90, when mode == R, degrees
5242
     * 7  Mode
5243
     *     'N’ = Data not valid
5244
     *     'A’ = Autonomous mode
5245
     *     'D’ = Differential mode
5246
     *     'E’ = Estimated (dead reckoning) mode
5247
     *     'M’ = Manual input mode
5248
     *     'S’ = Simulator mode
5249
     *     'F’ = Float RTK
5250
     *     'R’ = Fix RTK
5251
     * 8  Checksum
5252
     */
5253
5254
0
    gps_mask_t mask = ONLINE_SET;
5255
0
    int mode;
5256
5257
0
    if ('\0' != field[2][0] &&
5258
0
        '\0' != field[3][0]) {
5259
        // good date and time
5260
0
        if (0 == merge_hhmmss(field[2], session) &&
5261
0
            0 == merge_ddmmyy(field[3], session)) {
5262
0
            mask |= TIME_SET;
5263
0
            register_fractional_time("PSTI036", field[2], session);
5264
0
        }
5265
0
    }
5266
0
    if ('\0' == field[7][0] ||
5267
0
        'N' == field[7][0]) {
5268
        // No valid data, except time, sort of
5269
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
5270
0
                 "NMEA0183: PSTI,036: not valid\n");
5271
0
        return mask;
5272
0
    }
5273
    // good attitude data to use
5274
0
    session->gpsdata.attitude.mtime = gpsd_utc_resolve(session);
5275
0
    session->gpsdata.attitude.heading = safe_atof(field[4]);
5276
0
    session->gpsdata.attitude.pitch = safe_atof(field[5]);
5277
0
    session->gpsdata.attitude.roll = safe_atof(field[6]);
5278
0
    mode = faa_mode(field[7][0]);
5279
5280
0
    mask |= ATTITUDE_SET;
5281
5282
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5283
0
             "NMEA0183: PSTI,036: mode %d heading %.2f  pitch %.2f roll %.2f "
5284
0
             "faa mode %s(%s)\n",
5285
0
             mode,
5286
0
             session->gpsdata.attitude.heading,
5287
0
             session->gpsdata.attitude.pitch,
5288
0
             session->gpsdata.attitude.roll,
5289
0
             field[7], char2str(field[7][0], c_faa_mode));
5290
0
    return mask;
5291
0
}
5292
5293
/* decoce $PSTMCPU CPU load
5294
 * Private STM
5295
 * Present in ST Teseo liv4f
5296
 *
5297
 */
5298
static gps_mask_t processPSTMCPU(unsigned count UNUSED, char *field[],
5299
                                 struct gps_device_t *session)
5300
0
{
5301
    /*
5302
     * $PSTMCPU,<CPU_Usage>,<PLL_ON_OFF>,<CPU_Speed>*<checksum><cr><lf>
5303
     *
5304
     *  CPU_Usage  %
5305
     *
5306
     *  PLL_ON_OFF
5307
     *      0 = PLL Disabled
5308
     *      1 = PLL Enabled
5309
     *      -1 = Not supported
5310
     *
5311
     *  CPU_Speed  decimal digits
5312
     */
5313
5314
0
    static const struct vlist_t pll[] = {
5315
0
        {0, "PLL Disabled"},
5316
0
        {1, "PLL Ensabled"},
5317
0
        {-1, "Not Supported"},
5318
0
        {0, NULL},
5319
0
    };
5320
5321
0
    gps_mask_t mask = ONLINE_SET;
5322
0
    double cpu_usage = atof(field[1]);
5323
0
    int pll_on_off = atoi(field[2]);
5324
0
    unsigned cpu_speed = atoi(field[3]);
5325
5326
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5327
0
            "NMEA0183: PSTMCPU cpu_usage %.2f pll %s(%d) cpu_speed %u\n",
5328
0
            cpu_usage, val2str(pll_on_off, pll), pll_on_off, cpu_speed);
5329
5330
0
    return mask;
5331
0
}
5332
5333
/* decoce $PSTMANTENNASTATUS antenna status
5334
 * Private STM
5335
 * Also used bysome Quectel.
5336
 * Present in ST Teseo liv4f
5337
 *
5338
 */
5339
static gps_mask_t processPSTMANTENNASTATUS(unsigned count UNUSED,
5340
                                           char *field[],
5341
                                           struct gps_device_t *session)
5342
0
{
5343
    /*
5344
     * $PSTMANTENNASTATUS,<ant_status>,<op_mode>,<rf_path>,<pwr_switch>*<chk>
5345
     * $PSTMANTENNASTATUS,0,0,0,0*51
5346
     *
5347
     *  ant_status Decimal Current
5348
     *      0 = Normal condition
5349
     *      1 = Open condition
5350
     *      2 = Short condition
5351
     *
5352
     *  op_mode Decimal
5353
     *  Current antenna detection operating mode
5354
     *      0 = Automatic mode
5355
     *      1 = Manual mode
5356
     *
5357
     * rf_path Decimal
5358
     * Current RF path
5359
     *      0 = External antenna
5360
     *      1 = Internal antenna
5361
     *
5362
     * pwr_switch Decimal
5363
     * Current antenna power status
5364
     *      0 = Antenna power is on
5365
     *      1 = Antenna power is off
5366
     */
5367
5368
0
    static const struct vlist_t vop_mode[] = {
5369
0
        {0, "Auto"},
5370
0
        {1, "Manual"},
5371
0
        {0, NULL},
5372
0
    };
5373
5374
0
    static const struct vlist_t vpwr_switch[] = {
5375
0
        {0, "On"},
5376
0
        {1, "Off"},
5377
0
        {0, NULL},
5378
0
    };
5379
5380
0
    static const struct vlist_t vrf_path[] = {
5381
0
        {0, "External"},
5382
0
        {1, "Internal"},
5383
0
        {0, NULL},
5384
0
    };
5385
5386
0
    gps_mask_t mask = ONLINE_SET;
5387
0
    int ant_status = atoi(field[1]);
5388
0
    int op_mode = atoi(field[2]);
5389
0
    int rf_path = atoi(field[3]);
5390
0
    int pwr_switch = atoi(field[4]);
5391
5392
0
    switch(ant_status) {
5393
0
    case 0:
5394
0
        session->newdata.ant_stat = ANT_OK;
5395
0
        break;
5396
0
    case 1:
5397
0
        session->newdata.ant_stat = ANT_OPEN;
5398
0
        break;
5399
0
    case 2:
5400
0
        session->newdata.ant_stat = ANT_SHORT;
5401
0
        break;
5402
0
    default:
5403
0
        session->newdata.ant_stat = ANT_UNK;
5404
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
5405
0
                "NMEA0183: ant_stat: UNKNOWN(%d)\n", ant_status);
5406
0
        break;
5407
0
    }
5408
5409
0
    if (ANT_UNK != session->newdata.ant_stat) {
5410
0
        mask |= STATUS_SET;
5411
0
    }
5412
5413
0
    if (0 > op_mode ||
5414
0
        1 < op_mode) {
5415
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
5416
0
                "NMEA0183: malformed PSTMANTENNASTATUS op_mode: %s\n",
5417
0
                field[2]);
5418
0
    }
5419
5420
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5421
0
            "NMEA0183: PSTMANTENNASTATUS ant_status:%d op_mode:%d "
5422
0
            "rf_path:%d pwr_switch:%d\n",
5423
0
            ant_status, op_mode, rf_path, pwr_switch);
5424
0
    GPSD_LOG(LOG_IO, &session->context->errout,
5425
0
             "NMEA0183: PSTMANTENNASTATUS ant_status:%s(%d) op_mode:%s(%d) "
5426
0
             "rf_path:%s(%d) pwer_switch:%s(%d)\n",
5427
0
             val2str(session->newdata.ant_stat, vant_status),
5428
0
             session->newdata.ant_stat,
5429
0
             val2str(op_mode, vop_mode), op_mode,
5430
0
             val2str(rf_path, vrf_path), rf_path,
5431
0
             val2str(pwr_switch, vpwr_switch), pwr_switch);
5432
5433
0
    return mask;
5434
0
}
5435
5436
/* decoce $PSTMVER
5437
 * Private STM
5438
 * Present in ST Teseo liv4f
5439
 *
5440
 * Response to $PSTMGETVER,255
5441
 *
5442
 */
5443
static gps_mask_t processPSTMVER(unsigned count UNUSED, char *field[],
5444
                                 struct gps_device_t *session)
5445
0
{
5446
    /*
5447
    * $PSTMVER,<SW name and version>*<checksum>
5448
    *
5449
    * $PSTMVER,FreeRTOS_V10.4.3_ARM*57
5450
    * $PSTMVER,BINIMG_STA8041_4.6.6.5.6_ARM*0C
5451
    * $PSTMVER,SWCFG_86065331*62
5452
    * $PSTMVER,GNSSLIB_8.4.8.13_ARM*7F
5453
    * $PSTMVER,OS20LIB_4.3.0_ARM*47
5454
    * $PSTMVER,GPSAPP_2.2.1_ARM*1D
5455
    * $PSTMVER,SWCFG_8102510d*35
5456
    * $PSTMVER,WAASLIB_2.18.0_ARM*61
5457
    * $PSTMVER,STAGPSLIB_5.0.0_ARM*59
5458
    * $PSTMVER,STA8090_622bc043*6F
5459
    */
5460
5461
0
    gps_mask_t mask = ONLINE_SET;
5462
0
    size_t m_len =  strnlen(field[1], 40) + 2;
5463
0
    size_t st_left =  (sizeof(session->subtype) -
5464
0
                       strnlen(session->subtype, sizeof(session->subtype)));
5465
0
    size_t st1_left =  (sizeof(session->subtype1) -
5466
0
                        strnlen(session->subtype1, sizeof(session->subtype1)));
5467
5468
0
    if (NULL != strstr(session->subtype, field[1]) ||
5469
0
        NULL != strstr(session->subtype1, field[1])) {
5470
        // already haev it, ignore.
5471
0
    } else if (m_len < st_left) {
5472
        // room in subtype
5473
0
        if ('\0' == session->subtype[0]) {
5474
0
            (void)strncat(session->subtype, "STM,",
5475
0
                          sizeof(session->subtype) - 1);
5476
0
        } else {
5477
0
            (void)strncat(session->subtype, ",",
5478
0
                          sizeof(session->subtype) - 1);
5479
0
        }
5480
0
        (void)strncat(session->subtype, field[1],
5481
0
                      sizeof(session->subtype) - 1);
5482
0
    } else if (m_len < st1_left) {
5483
        // room in subtype1
5484
0
        if ('\0' != session->subtype1[0]) {
5485
0
            (void)strncat(session->subtype1, ",",
5486
0
                          sizeof(session->subtype1) - 1);
5487
0
        }
5488
0
        (void)strncat(session->subtype1, field[1],
5489
0
                      sizeof(session->subtype1) - 1);
5490
0
    } else {
5491
        // else no room.  log it
5492
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
5493
0
                "NMEA0183: $PSTMVER: no room for: %s\n", field[1]);
5494
0
    }
5495
5496
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5497
0
            "NMEA0183: $PSTMVER: %s, %s\n",
5498
0
            session->subtype, session->subtype1);
5499
5500
0
    return mask;
5501
0
}
5502
5503
// Recommend Minimum Course Specific GPS/TRANSIT Data
5504
static gps_mask_t processRMC(unsigned count, char *field[],
5505
                             struct gps_device_t *session)
5506
0
{
5507
    /*
5508
     * RMC,225446.33,A,4916.45,N,12311.12,W,000.5,054.7,191194,020.3,E,A*68
5509
     * 1     225446.33    Time of fix 22:54:46 UTC
5510
     * 2     A            Status of Fix:
5511
     *                     A = Autonomous, valid;
5512
     *                     V = invalid
5513
     * 3,4   4916.45,N    Latitude 49 deg. 16.45 min North
5514
     * 5,6   12311.12,W   Longitude 123 deg. 11.12 min West
5515
     * 7     000.5        Speed over ground, Knots
5516
     * 8     054.7        Course Made Good, True north
5517
     * 9     181194       Date of fix ddmmyy.  18 November 1994
5518
     * 10,11 020.3,E      Magnetic variation 20.3 deg East
5519
     * 12    A            FAA mode indicator (NMEA 2.3 and later)
5520
     *                     see faa_mode() for possible mode values
5521
     * 13    V            Nav Status (NMEA 4.1 and later)
5522
     *                     A = autonomous,
5523
     *                     D = differential,
5524
     *                     E = Estimated (DR),
5525
     *                     F = RTK Float
5526
     *                     M = Manual input mode
5527
     *                     N = No fix.  Not valid,
5528
     *                     P = High Precision Mode
5529
     *                     R = RTK Integer
5530
     *                     S = Simulator,
5531
     *                     V = Invalid
5532
     * *68        mandatory nmea_checksum
5533
     *
5534
     * SiRF chipsets don't return either Mode Indicator or magnetic variation.
5535
     */
5536
0
    gps_mask_t mask = ONLINE_SET;
5537
0
    char status = field[2][0];
5538
0
    int newstatus;
5539
5540
    /* As of Dec 2023, the regressions only have A, or V in field 2.
5541
     * NMEA says only A, and V are valid
5542
     */
5543
0
    switch (status) {
5544
0
    default:
5545
        // missing, never seen this case.
5546
0
        FALLTHROUGH
5547
0
    case 'V':
5548
        // Invalid
5549
0
        session->newdata.mode = MODE_NO_FIX;
5550
0
        if ('\0' == field[1][0] ||
5551
0
            '\0' ==  field[9][0]) {
5552
            /* No time available. That breaks cycle end detector
5553
             * Force report to bypass cycle detector and get report out.
5554
             * To handle Querks (Quectel) like this:
5555
             *  $GPRMC,,V,,,,,,,,,,N*53
5556
             */
5557
0
            memset(&session->nmea.date, 0, sizeof(session->nmea.date));
5558
0
            session->cycle_end_reliable = false;
5559
0
            mask |= REPORT_IS | TIME_SET;
5560
0
        }
5561
0
        mask |= STATUS_SET | MODE_SET;
5562
0
        break;
5563
0
    case 'A':
5564
        // Valid Fix
5565
        /*
5566
         * The MTK3301, Royaltek RGM-3800, and possibly other
5567
         * devices deliver bogus time values when the navigation
5568
         * warning bit is set.
5569
         */
5570
        /* The Meinberg GPS164 only outputs GPRMC.  Do set status
5571
         * so it can increment fixcnt.
5572
         */
5573
0
        if ('\0' != field[1][0] &&
5574
0
            9 < count &&
5575
0
            '\0' !=  field[9][0]) {
5576
0
            if (0 == merge_hhmmss(field[1], session) &&
5577
0
                0 == merge_ddmmyy(field[9], session)) {
5578
                // got a good data/time
5579
0
                mask |= TIME_SET;
5580
0
                register_fractional_time(field[0], field[1], session);
5581
0
            }
5582
0
        }
5583
        // else, no point to the time only case, no regressions with that
5584
5585
0
        if (0 == do_lat_lon(&field[3], &session->newdata)) {
5586
0
            newstatus = STATUS_GPS;
5587
0
            mask |= LATLON_SET;
5588
0
            if (MODE_2D >= session->lastfix.mode) {
5589
                /* we have at least a 2D fix
5590
                 * might cause blinking */
5591
0
                session->newdata.mode = MODE_2D;
5592
0
            } else if (MODE_3D == session->lastfix.mode) {
5593
                // keep the 3D, this may be cycle starter
5594
                // might cause blinking
5595
0
                session->newdata.mode = MODE_3D;
5596
0
            }
5597
0
        } else {
5598
0
            newstatus = STATUS_UNK;
5599
0
            session->newdata.mode = MODE_NO_FIX;
5600
0
        }
5601
0
        mask |= MODE_SET;
5602
0
        if ('\0' != field[7][0]) {
5603
0
            session->newdata.speed = safe_atof(field[7]) * KNOTS_TO_MPS;
5604
0
            mask |= SPEED_SET;
5605
0
        }
5606
0
        if ('\0' != field[8][0]) {
5607
0
            session->newdata.track = safe_atof(field[8]);
5608
0
            mask |= TRACK_SET;
5609
0
        }
5610
5611
        // get magnetic variation
5612
0
        if ('\0' != field[10][0] &&
5613
0
            '\0' != field[11][0]) {
5614
0
            session->newdata.magnetic_var = safe_atof(field[10]);
5615
5616
0
            switch (field[11][0]) {
5617
0
            case 'E':
5618
                // no change
5619
0
                break;
5620
0
            case 'W':
5621
0
                session->newdata.magnetic_var = -session->newdata.magnetic_var;
5622
0
                break;
5623
0
            default:
5624
                // huh?
5625
0
                session->newdata.magnetic_var = NAN;
5626
0
                break;
5627
0
            }
5628
0
            if (0 == isfinite(session->newdata.magnetic_var) ||
5629
0
                0.09 >= fabs(session->newdata.magnetic_var)) {
5630
                // some GPS set 0.0,E, or 0,w instead of blank
5631
0
                session->newdata.magnetic_var = NAN;
5632
0
            } else {
5633
0
                mask |= MAGNETIC_TRACK_SET;
5634
0
            }
5635
0
        }
5636
5637
0
        if (12 < count) {
5638
0
            if ('\0' != field[12][0]) {
5639
                // Have FAA mode indicator (NMEA 2.3 and later)
5640
0
                newstatus = faa_mode(field[12][0]);
5641
0
            }
5642
            /*
5643
             * Navigation Status
5644
             * If present, can not be NUL:
5645
             * S = Safe
5646
             * C = Caution
5647
             * U = Unsafe
5648
             * V = invalid.
5649
             *
5650
             * In the regressions, as of Dec 2023, field 13 is
5651
             * always 'V', and field 2 is always 'A'.  That seems
5652
             * like an invalid combination.... */
5653
0
            if (13 < count) {
5654
0
                if ('\0' != field[13][0]) {
5655
0
                    ;  // skip for now
5656
0
                }
5657
0
            }
5658
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
5659
0
                     "NMEA0183: RMC: status %s(%d) faa mode %s(%s) "
5660
0
                     "faa status %s\n",
5661
0
                     field[2], newstatus, field[12],
5662
0
                     char2str(field[12][0], c_faa_mode), field[13]);
5663
0
        }
5664
5665
        /*
5666
         * This copes with GPSes like the Magellan EC-10X that *only* emit
5667
         * GPRMC. In this case we set mode and status here so the client
5668
         * code that relies on them won't mistakenly believe it has never
5669
         * received a fix.
5670
         */
5671
0
        if (3 < session->gpsdata.satellites_used) {
5672
            // 4 sats used means 3D
5673
0
            session->newdata.mode = MODE_3D;
5674
0
        } else if (0 != isfinite(session->gpsdata.fix.altHAE) ||
5675
0
                   0 != isfinite(session->gpsdata.fix.altMSL)) {
5676
            /* we probably have at least a 3D fix
5677
             * this handles old GPS that do not report 3D */
5678
0
            session->newdata.mode = MODE_3D;
5679
0
        }
5680
0
        session->newdata.status = newstatus;
5681
0
        mask |= STATUS_SET | MODE_SET;
5682
0
    }
5683
5684
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5685
0
             "NMEA0183: RMC: ddmmyy=%s hhmmss=%s lat=%.2f lon=%.2f "
5686
0
             "speed=%.2f track=%.2f mode=%d var=%.1f status=%d\n",
5687
0
             field[9], field[1],
5688
0
             session->newdata.latitude,
5689
0
             session->newdata.longitude,
5690
0
             session->newdata.speed,
5691
0
             session->newdata.track,
5692
0
             session->newdata.mode,
5693
0
             session->newdata.magnetic_var,
5694
0
             session->newdata.status);
5695
0
    return mask;
5696
0
}
5697
5698
/* precessROT() - process Rate Of Turn
5699
 *
5700
 * Deprecated by NMEA in 2008
5701
 */
5702
static gps_mask_t processROT(unsigned count UNUSED, char *field[],
5703
                             struct gps_device_t *session)
5704
0
{
5705
    /*
5706
     * $APROT,0.013,A*35
5707
     *
5708
     * 1) Rate of Turn deg/min
5709
     * 2) A = valid, V = Void
5710
     * )  checksum
5711
     *
5712
     */
5713
0
    gps_mask_t mask = ONLINE_SET;
5714
5715
0
    if ('\0' == field[1][0] ||
5716
0
        'A' != field[2][0]) {
5717
        // no data
5718
0
        return mask;
5719
0
    }
5720
5721
    // assume good data
5722
0
    session->gpsdata.attitude.rot = safe_atof(field[1]);
5723
0
    mask |= ATTITUDE_SET;
5724
5725
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5726
0
             "NMEA0183: $xxROT:Rate of Turn %f\n",
5727
0
             session->gpsdata.attitude.rot);
5728
0
    return mask;
5729
0
}
5730
5731
/*
5732
 * Unicore $SNRSTAT  Sensor status
5733
 * Note: Invalid sender: $SN
5734
 */
5735
static gps_mask_t processSNRSTAT(unsigned count UNUSED, char *field[],
5736
                                 struct gps_device_t *session)
5737
0
{
5738
    /*
5739
     * $SNRSTAT,1,1,0,0*5D
5740
     */
5741
5742
0
    gps_mask_t mask = ONLINE_SET;
5743
0
    static char probe[] = "$PDTINFO\r\n";
5744
0
    static char type[] = "Unicore";
5745
0
    int insstatus = atoi(field[1]);     // IMU status
5746
0
    int odostatus = atoi(field[2]);     // Odometer Status
5747
0
    int InstallState = atoi(field[3]);  // Install State
5748
0
    int mapstat = atoi(field[4]);       // PAP status
5749
5750
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5751
0
             "NMEA0183: SNRSTAT insstatus %d obsstatus %d InstallState %d "
5752
0
             "mapstat %d\n",
5753
0
             insstatus, odostatus, InstallState, mapstat);
5754
5755
0
    GPSD_LOG(LOG_IO, &session->context->errout,
5756
0
             "NMEA0183: SNRSTAT insstatus %s obsstatus %s InstallState %s "
5757
0
             "mapstat %s\n",
5758
0
             val2str(insstatus, vsnrstat_insstatus),
5759
0
             val2str(odostatus, vsnrstat_odostatus),
5760
0
             val2str(InstallState, vsnrstat_InstallState),
5761
0
             val2str(mapstat, vsnrstat_mapstat));
5762
5763
0
    if ('\0' == session->subtype[0]) {
5764
        // this is Unicore
5765
        // Send $PDTINFO to get back $PDTINFO,....
5766
0
        (void)gpsd_write(session, probe, sizeof(probe));
5767
        // mark so we don't ask twice
5768
0
        (void)strlcpy(session->subtype, type, sizeof(session->subtype) - 1);
5769
0
    }
5770
0
    return mask;
5771
0
}
5772
5773
5774
/*
5775
 * Skytraq undocumented debug sentences take this format:
5776
 * $STI,type,val*CS
5777
 * type is a 2 char subsentence type
5778
 * Note: NO checksum
5779
 */
5780
static gps_mask_t processSTI(unsigned count, char *field[],
5781
                             struct gps_device_t *session)
5782
0
{
5783
0
    gps_mask_t mask = ONLINE_SET;
5784
5785
0
    if (0 != strncmp(session->device_type->type_name, "Skytraq", 7)) {
5786
        // this is skytraq, but marked yet, so probe for Skytraq
5787
        // Send MID 0x02, to get back MID 0x80
5788
0
        (void)gpsd_write(session, "\xA0\xA1\x00\x02\x02\x01\x03\x0d\x0a",9);
5789
0
    }
5790
5791
0
    if ( 0 == strcmp( field[1], "IC") ) {
5792
        // $STI,IC,error=XX, this is always very bad, but undocumented
5793
0
        GPSD_LOG(LOG_ERROR, &session->context->errout,
5794
0
                 "NMEA0183: Skytraq: $STI,%s,%s\n", field[1], field[2]);
5795
0
        return mask;
5796
0
    }
5797
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5798
0
             "NMEA0183: STI,%s: Unknown type, Count: %d\n", field[1], count);
5799
5800
0
    return mask;
5801
0
}
5802
5803
// SiRF Estimated Position Errors
5804
// $xxTHS -- True Heading and Status
5805
static gps_mask_t processTHS(unsigned count UNUSED, char *field[],
5806
                             struct gps_device_t *session)
5807
0
{
5808
    /*
5809
     * $GNTHS,121.15.A*1F<CR><LF>
5810
     * 1  - Heading, degrees True
5811
     * 2  - Mode indicator
5812
     *      'A’ = Autonomous
5813
     *      'E’ = Estimated (dead reckoning)
5814
     *      'M’ = Manual input
5815
     *      'S’ = Simulator
5816
     *      'V’ = Data not valid
5817
     * 3  - Checksum
5818
     */
5819
0
    gps_mask_t mask = ONLINE_SET;
5820
0
    double heading;
5821
5822
0
    if ('\0' == field[1][0] ||
5823
0
        '\0' == field[2][0]) {
5824
        // no data
5825
0
        return mask;
5826
0
    }
5827
0
    if ('V' == field[2][0]) {
5828
        // invalid data
5829
        // ignore A, E, M and S for now
5830
0
        return mask;
5831
0
    }
5832
0
    heading = safe_atof(field[1]);
5833
0
    if ((0.0 > heading) ||
5834
0
        (360.0 < heading)) {
5835
        // bad data
5836
0
        return mask;
5837
0
    }
5838
5839
0
    GPSD_LOG(LOG_PROG, &session->context->errout,
5840
0
             "NMEA0183: $xxTHS heading %lf mode %s\n",
5841
0
             heading, field[2]);
5842
5843
0
    return mask;
5844
0
}
5845
5846
static gps_mask_t processTNTA(unsigned count UNUSED, char *field[],
5847
                              struct gps_device_t *session)
5848
0
{
5849
    /*
5850
     * Proprietary sentence for iSync GRClok/LNRClok.
5851
5852
     $PTNTA,20000102173852,1,T4,,,6,1,0*32
5853
5854
     1. Date/time in format year, month, day, hour, minute, second
5855
     2. Oscillator quality 0:warming up, 1:freerun, 2:disciplined.
5856
     3. Always T4. Format indicator.
5857
     4. Interval ppsref-ppsout in [ns]. Blank if no ppsref.
5858
     5. Fine phase comparator in approx. [ns]. Always close to -500 or
5859
        +500 if not disciplined. Blank if no ppsref.
5860
     6. iSync Status.  0:warming up or no light, 1:tracking set-up,
5861
        2:track to PPSREF, 3:synch to PPSREF, 4:Free Run. Track OFF,
5862
        5:FR. PPSREF unstable, 6:FR. No PPSREF, 7:FREEZE, 8:factory
5863
        used, 9:searching Rb line
5864
     7. GPS messages indicator. 0:do not take account, 1:take account,
5865
        but no message, 2:take account, partially ok, 3:take account,
5866
        totally ok.
5867
     8. Transfer quality of date/time. 0:no, 1:manual, 2:GPS, older
5868
        than x hours, 3:GPS, fresh.
5869
5870
     */
5871
0
    gps_mask_t mask = ONLINE_SET;
5872
5873
0
    if (0 == strcmp(field[3], "T4")) {
5874
0
        struct oscillator_t *osc = &session->gpsdata.osc;
5875
0
        unsigned int quality = atoi(field[2]);
5876
0
        unsigned int delta = atoi(field[4]);
5877
0
        unsigned int fine = atoi(field[5]);
5878
0
        unsigned int status = atoi(field[6]);
5879
0
        char deltachar = field[4][0];
5880
5881
0
        osc->running = (0 < quality);
5882
0
        osc->reference = (deltachar && (deltachar != '?'));
5883
0
        if (osc->reference) {
5884
0
            if (500 > delta) {
5885
0
                osc->delta = fine;
5886
0
            } else {
5887
0
                osc->delta = ((delta < 500000000) ? delta : 1000000000 - delta);
5888
0
            }
5889
0
        } else {
5890
0
            osc->delta = 0;
5891
0
        }
5892
0
        osc->disciplined = ((quality == 2) && (status == 3));
5893
0
        mask |= OSCILLATOR_SET;
5894
5895
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
5896
0
                 "NMEA0183: PTNTA,T4: quality=%s, delta=%s, fine=%s,"
5897
0
                 "status=%s\n",
5898
0
                 field[2], field[4], field[5], field[6]);
5899
0
    }
5900
0
    return mask;
5901
0
}
5902
5903
static gps_mask_t processTNTHTM(unsigned count UNUSED, char *field[],
5904
                                struct gps_device_t *session)
5905
0
{
5906
    /*
5907
     * Proprietary sentence for True North Technologies Magnetic Compass.
5908
     * This may also apply to some Honeywell units since they may have been
5909
     * designed by True North.
5910
5911
     $PTNTHTM,14223,N,169,N,-43,N,13641,2454*15
5912
5913
     HTM,x.x,a,x.x,a,x.x,a,x.x,x.x*hh<cr><lf>
5914
     Fields in order:
5915
     1. True heading (compass measurement + deviation + variation)
5916
     2. magnetometer status character:
5917
     C = magnetometer calibration alarm
5918
     L = low alarm
5919
     M = low warning
5920
     N = normal
5921
     O = high warning
5922
     P = high alarm
5923
     V = magnetometer voltage level alarm
5924
     3. pitch angle
5925
     4. pitch status character - see field 2 above
5926
     5. roll angle
5927
     6. roll status character - see field 2 above
5928
     7. dip angle
5929
     8. relative magnitude horizontal component of earth's magnetic field
5930
     *hh          mandatory nmea_checksum
5931
5932
     By default, angles are reported as 26-bit integers: weirdly, the
5933
     technical manual says either 0 to 65535 or -32768 to 32767 can
5934
     occur as a range.
5935
     */
5936
0
    gps_mask_t mask = ONLINE_SET;
5937
5938
    // True heading
5939
0
    session->gpsdata.attitude.heading = safe_atof(field[1]);
5940
0
    session->gpsdata.attitude.mag_st = *field[2];
5941
0
    session->gpsdata.attitude.pitch = safe_atof(field[3]);
5942
0
    session->gpsdata.attitude.pitch_st = *field[4];
5943
0
    session->gpsdata.attitude.roll = safe_atof(field[5]);
5944
0
    session->gpsdata.attitude.roll_st = *field[6];
5945
0
    session->gpsdata.attitude.dip = safe_atof(field[7]);
5946
0
    session->gpsdata.attitude.mag_x = safe_atof(field[8]);
5947
0
    mask |= (ATTITUDE_SET);
5948
5949
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
5950
0
             "NMEA0183: $PTNTHTM heading %lf (%c).\n",
5951
0
             session->gpsdata.attitude.heading,
5952
0
             session->gpsdata.attitude.mag_st);
5953
0
    return mask;
5954
0
}
5955
5956
// GPS Text message
5957
static gps_mask_t processTXT(unsigned count, char *field[],
5958
                             struct gps_device_t *session)
5959
0
{
5960
    /*
5961
     * $GNTXT,01,01,01,PGRM inv format*2A
5962
     * 1                   Number of sentences for full data
5963
     * 1                   Sentence 1 of 1
5964
     * 01                  Message type
5965
     *       00 - error
5966
     *       01 - warning
5967
     *       02 - notice
5968
     *       07 - user
5969
     * PGRM inv format     ASCII text
5970
     *
5971
     * Can occur with talker IDs:
5972
     *   BD (Beidou),
5973
     *   GA (Galileo),
5974
     *   GB (Beidou),
5975
     *   GI (IRNSS
5976
     *   GL (GLONASS),
5977
     *   GN (GLONASS, any combination GNSS),
5978
     *   GP (GPS, SBAS, QZSS),
5979
     *   GQ (QZSS).
5980
     *   PQ (QZSS). Quectel Quirk
5981
     *   QZ (QZSS).
5982
     *
5983
     * Unicore undcumented:
5984
     *
5985
     * $GNTXT,01,01,01,0,500482,0000,80A0,80A0,-45.277,0*6B
5986
     * $GNTXT,01,01,02,0,00,10000,00,01,17,01,0001,0000,0.000*57
5987
     */
5988
0
    gps_mask_t mask = ONLINE_SET;
5989
0
    int msgType = 0;
5990
0
    char *msgType_txt = "Unknown";
5991
5992
0
    if (5 != count) {
5993
0
      return mask;
5994
0
    }
5995
5996
0
    msgType = atoi(field[3]);
5997
5998
0
    switch ( msgType ) {
5999
0
    case 0:
6000
0
        msgType_txt = "Error";
6001
0
        break;
6002
0
    case 1:
6003
0
        msgType_txt = "Warning";
6004
0
        break;
6005
0
    case 2:
6006
0
        msgType_txt = "Notice";
6007
0
        break;
6008
0
    case 7:
6009
0
        msgType_txt = "User";
6010
0
        break;
6011
0
    }
6012
6013
    // maximum text length unknown, guess 80
6014
0
    GPSD_LOG(LOG_WARN, &session->context->errout,
6015
0
             "NMEA0183: TXT: %.10s: %.80s\n",
6016
0
             msgType_txt, field[4]);
6017
0
    return mask;
6018
0
}
6019
6020
/* process xxVTG
6021
 *     $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K
6022
 *     $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K,A
6023
 *
6024
 * where:
6025
 *         1,2     054.7,T      True track made good (degrees)
6026
 *         3,4     034.4,M      Magnetic track made good
6027
 *         5,6     005.5,N      Ground speed, knots
6028
 *         7,8     010.2,K      Ground speed, Kilometers per hour
6029
 *         9       A            Mode Indicator (optional)
6030
 *                                see faa_mode() for possible mode values
6031
 *
6032
 * see also:
6033
 * https://gpsd.gitlab.io/gpsd/NMEA.html#_vtg_track_made_good_and_ground_speed
6034
 */
6035
static gps_mask_t processVTG(unsigned count,
6036
                             char *field[],
6037
                             struct gps_device_t *session)
6038
0
{
6039
0
    gps_mask_t mask = ONLINE_SET;
6040
6041
0
    if( (field[1][0] == '\0') || (field[5][0] == '\0')){
6042
0
        return mask;
6043
0
    }
6044
6045
    // ignore empty/missing field, fix mode of last resort
6046
0
    if ((9 < count) &&
6047
0
        ('\0' != field[9][0])) {
6048
6049
0
        switch (field[9][0]) {
6050
0
        case 'A':
6051
            // Autonomous, 2D or 3D fix
6052
0
            FALLTHROUGH
6053
0
        case 'D':
6054
            // Differential, 2D or 3D fix
6055
            // MODE_SET here causes issues
6056
            // mask |= MODE_SET;
6057
0
            break;
6058
0
        case 'E':
6059
            // Estimated, DR only
6060
0
            FALLTHROUGH
6061
0
        case 'N':
6062
            // Not Valid
6063
            // MODE_SET here causes issues
6064
            // mask |= MODE_SET;
6065
            // nothing to use here, leave
6066
0
            return mask;
6067
0
        default:
6068
            // Huh?
6069
0
            break;
6070
0
        }
6071
0
    }
6072
6073
    // set true track
6074
0
    session->newdata.track = safe_atof(field[1]);
6075
0
    mask |= TRACK_SET;
6076
6077
    // set magnetic variation
6078
0
    if ('\0' != field[3][0]) {  // ignore empty fields
6079
0
        session->newdata.magnetic_track = safe_atof(field[3]);
6080
0
        mask |= MAGNETIC_TRACK_SET;
6081
0
    }
6082
6083
0
    session->newdata.speed = safe_atof(field[5]) * KNOTS_TO_MPS;
6084
0
    mask |= SPEED_SET;
6085
6086
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
6087
0
             "NMEA0183: VTG: course(T)=%.2f, course(M)=%.2f, speed=%.2f",
6088
0
             session->newdata.track, session->newdata.magnetic_track,
6089
0
             session->newdata.speed);
6090
0
    return mask;
6091
0
}
6092
6093
/* precessXDR() - process transducer messages
6094
 */
6095
static gps_mask_t processXDR(unsigned count, char *field[],
6096
                             struct gps_device_t *session)
6097
0
{
6098
    /*
6099
     * $APXDR,A,0.135,D,PTCH*7C
6100
     * $APXDR,A,3.861,D,ROLL*65
6101
     *
6102
     * 1) Transducer type
6103
     *     A = Angular Displacement
6104
     * 2) Measurement data
6105
     * 3) Units of measure
6106
     *     D = degrees
6107
     * 4) Transducer ID
6108
     *     can be repeated...
6109
     * The previsou 4 messages can be repeated at least 9 more times.
6110
     * )  checksum
6111
     *
6112
     * TODO: stacked measurements, like the TNT Revolution:
6113
  $HCXDR,A,177,D,PITCH,A,-40,D,ROLL,G,358,,MAGX,G,2432,,MAGY,G,-8974,,MAGZ*47
6114
     *  the bund_zeus:
6115
  $IIXDR,C,,C,AIRTEMP,A,-3.0,D,HEEL,A,3.7,D,TRIM,P,,B,BARO,A,-4.2,D,RUDDER*28
6116
     *
6117
     */
6118
0
    gps_mask_t mask = ONLINE_SET;
6119
0
    unsigned i;
6120
0
    unsigned num_meas = count / 4;
6121
6122
0
    if (10 < num_meas) {
6123
        // nodocumented limit of measurements, we pick 10
6124
0
        num_meas = 10;
6125
0
    }
6126
6127
0
    for (i = 0; i < num_meas; i++) {
6128
0
        double data = 0.0;
6129
0
        unsigned j = i * 4;
6130
6131
0
        if ('\0' == field[j + 2][0]) {
6132
            // no data, skip it
6133
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6134
0
                     "NMEA0183: $xxXDR: Type %.10s Data %.10s Units %.10s "
6135
0
                     "ID %.10s\n",
6136
0
                     field[j + 1], field[j + 2], field[j + 3], field[j + 4]);
6137
0
            continue;
6138
0
        }
6139
6140
0
        data = safe_atof(field[j + 2]);
6141
6142
0
        switch (field[j + 1][0]) {
6143
0
        case 'A':
6144
            // angles
6145
0
            if ('D' != field[j + 3][0]) {
6146
                // not degrees
6147
0
                continue;
6148
0
            }
6149
0
            if (0 == strncmp( "HEEL", field[j + 4], 10)) {
6150
                // session->gpsdata.attitude.roll = data;
6151
                // mask |= ATTITUDE_SET;
6152
0
            } else if (0 == strncmp( "PTCH", field[j + 4], 10) ||
6153
0
                0 == strncmp( "PITCH", field[j + 4], 10)) {
6154
0
                session->gpsdata.attitude.pitch = data;
6155
0
                mask |= ATTITUDE_SET;
6156
0
            } else if (0 == strncmp( "ROLL", field[j + 4], 10)) {
6157
0
                session->gpsdata.attitude.roll = data;
6158
0
                mask |= ATTITUDE_SET;
6159
0
            } else if (0 == strncmp( "RUDDER", field[j + 4], 10)) {
6160
                // session->gpsdata.attitude.roll = data;
6161
                // mask |= ATTITUDE_SET;
6162
0
            } else if (0 == strncmp( "TRIM", field[j + 4], 10)) {
6163
                // session->gpsdata.attitude.roll = data;
6164
                // mask |= ATTITUDE_SET;
6165
0
            }
6166
            // else, unknown
6167
0
            break;
6168
0
        case 'G':
6169
            // G: TODO: G,358,,MAGX,G,2432,,MAGY,G,-8974,,MAGZ*47
6170
            // oddly field[j + 3][0] is NUL...
6171
6172
0
            if (0 == strncmp( "MAGX", field[j + 4], 10)) {
6173
                // unknown scale
6174
0
                session->gpsdata.attitude.mag_x = data;
6175
0
                mask |= ATTITUDE_SET;
6176
0
            } else if (0 == strncmp( "MAGY", field[j + 4], 10)) {
6177
                // unknown scale
6178
0
                session->gpsdata.attitude.mag_y = data;
6179
0
                mask |= ATTITUDE_SET;
6180
0
            } else if (0 == strncmp( "MAGZ", field[j + 4], 10)) {
6181
                // unknown scale
6182
0
                session->gpsdata.attitude.mag_z = data;
6183
0
                mask |= ATTITUDE_SET;
6184
0
            }
6185
0
            break;
6186
0
        case 'C':
6187
            // C,,C,AIRTEMP,
6188
0
            FALLTHROUGH
6189
0
        case 'P':
6190
            // Pressure: TODO: P,,B,BARO
6191
0
            FALLTHROUGH
6192
0
        default:
6193
0
            break;
6194
0
        }
6195
6196
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
6197
0
                 "NMEA0183: $xxXDR: Type %.10s Data %f Units %.10s ID %.10s\n",
6198
0
                 field[j + 1], data, field[j + 3], field[j + 4]);
6199
0
    }
6200
0
    return mask;
6201
0
}
6202
6203
// Time & Date
6204
static gps_mask_t processZDA(unsigned count UNUSED, char *field[],
6205
                             struct gps_device_t *session)
6206
0
{
6207
    /*
6208
     * $GPZDA,160012.71,11,03,2004,-1,00*7D
6209
     * 1) UTC time (hours, minutes, seconds, may have fractional subsecond)
6210
     * 2) Day, 01 to 31
6211
     * 3) Month, 01 to 12
6212
     * 4) Year (4 digits)
6213
     * 5) Local zone description, 00 to +- 13 hours
6214
     * 6) Local zone minutes description, apply same sign as local hours
6215
     * 7) Checksum
6216
     *
6217
     * Note: some devices, like the u-blox ANTARIS 4h, are known to ship ZDAs
6218
     * with some fields blank under poorly-understood circumstances (probably
6219
     * when they don't have satellite lock yet).
6220
     */
6221
0
    gps_mask_t mask = ONLINE_SET;
6222
0
    int year, mon, mday, century;
6223
0
    char ts_buf[TIMESPEC_LEN];
6224
6225
0
    if ('\0' == field[1][0] ||
6226
0
        '\0' == field[2][0] ||
6227
0
        '\0' == field[3][0] ||
6228
0
        '\0' == field[4][0]) {
6229
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6230
0
                 "NMEA0183: ZDA fields are empty\n");
6231
0
        return mask;
6232
0
    }
6233
6234
0
    if (0 != merge_hhmmss(field[1], session)) {
6235
        // bad time
6236
0
        return mask;
6237
0
    }
6238
6239
    /*
6240
     * We didn't register fractional time here because we wanted to leave
6241
     * ZDA out of end-of-cycle detection. Some devices sensibly emit it only
6242
     * when they have a fix, so watching for it can make them look
6243
     * like they have a variable fix reporting cycle.  But later thought
6244
     * was to not throw out good data because it is inconvenient.
6245
     */
6246
0
    mday = atoi(field[2]);
6247
0
    mon = atoi(field[3]);
6248
0
    year = atoi(field[4]);
6249
0
    century = year - year % 100;
6250
0
    if (1900 > year  ||
6251
0
        2200 < year) {
6252
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6253
0
                 "NMEA0183: malformed ZDA year: %s\n",  field[4]);
6254
0
    } else if (1 > mon ||
6255
0
               12 < mon) {
6256
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6257
0
                 "NMEA0183: malformed ZDA month: %s\n",  field[3]);
6258
0
    } else if (1 > mday ||
6259
0
               31 < mday) {
6260
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6261
0
                 "NMEA0183: malformed ZDA day: %s\n",  field[2]);
6262
0
    } else {
6263
0
        gpsd_century_update(session, century);
6264
0
        session->nmea.date.tm_year = year - 1900;
6265
0
        session->nmea.date.tm_mon = mon - 1;
6266
0
        session->nmea.date.tm_mday = mday;
6267
0
        session->newdata.time = gpsd_utc_resolve(session);
6268
0
        register_fractional_time(field[0], field[1], session);
6269
0
        mask = TIME_SET;
6270
0
    }
6271
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
6272
0
         "NMEA0183: ZDA time %s\n",
6273
0
          timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)));
6274
0
    return mask;
6275
0
}
6276
6277
6278
6279
/**************************************************************************
6280
 *
6281
 * Entry points begin here
6282
 *
6283
 **************************************************************************/
6284
6285
// parse an NMEA sentence, unpack it into a session structure
6286
gps_mask_t nmea_parse(char *sentence, struct gps_device_t * session)
6287
0
{
6288
0
    typedef gps_mask_t(*nmea_decoder) (unsigned count, char *f[],
6289
0
                                       struct gps_device_t * session);
6290
0
    static struct
6291
0
    {
6292
0
        char *name;
6293
0
        char *name1;            // 2nd field to match, as is $PSTI,030
6294
0
        int nf;                 // minimum number of fields required to parse
6295
0
        bool cycle_continue;    // cycle continuer?
6296
0
        nmea_decoder decoder;
6297
0
    } nmea_phrase[NMEA_NUM] = {
6298
0
        {"PGLOR", NULL, 2,  false, processPGLOR},  // Android something...
6299
        // Ericsson firmware status
6300
0
        {"PERC", "FWsts", 6, false, processPERCFWsts},
6301
        // Ericsson averaged position
6302
0
        {"PERC", "GPavp", 6, false, processPERCGPavp},
6303
        // Ericsson control/heartbeat
6304
0
        {"PERC", "GPctr", 6, false, processPERCGPctr},
6305
        // Ericsson debug output
6306
0
        {"PERC", "GPdbg", 19, false, processPERCGPdbg},
6307
        // Ericsson oscillator phase/freq
6308
0
        {"PERC", "GPppf", 5, false, processPERCGPppf},
6309
        // Ericsson GPS time reference
6310
0
        {"PERC", "GPppr", 6, false, processPERCGPppr},
6311
        // Ericsson receiver health
6312
0
        {"PERC", "GPreh", 2, false, processPERCGPreh},
6313
        // Ericsson receiver status
6314
0
        {"PERC", "GPsts", 4, false, processPERCGPsts},
6315
        // Ericsson version info
6316
0
        {"PERC", "GPver", 4, false, processPERCGPver},
6317
0
        {"PGRMB", NULL, 0,  false, NULL},     // ignore Garmin DGPS Beacon Info
6318
0
        {"PGRMC", NULL, 0,  false, NULL},        // ignore Garmin Sensor Config
6319
0
        {"PGRME", NULL, 7,  false, processPGRME},
6320
0
        {"PGRMF", NULL, 15, false, processPGRMF},  // Garmin GPS Fix Data
6321
0
        {"PGRMH", NULL, 0,  false, NULL},     // ignore Garmin Aviation Height
6322
0
        {"PGRMI", NULL, 0,  false, NULL},          // ignore Garmin Sensor Init
6323
0
        {"PGRMM", NULL, 2,  false, processPGRMM},  // Garmin Map Datum
6324
0
        {"PGRMO", NULL, 0,  false, NULL},     // ignore Garmin Sentence Enable
6325
0
        {"PGRMT", NULL, 10, false, processPGRMT},  // Garmin Sensor Info
6326
0
        {"PGRMV", NULL, 4,  false, processPGRMV},  // Garmin 3D Velocity Info
6327
0
        {"PGRMZ", NULL, 4,  false, processPGRMZ},
6328
            /*
6329
             * Basic sentences must come after the PG* ones, otherwise
6330
             * Garmins can get stuck in a loop that looks like this:
6331
             *
6332
             * 1. A Garmin GPS in NMEA mode is detected.
6333
             *
6334
             * 2. PGRMC is sent to reconfigure to Garmin binary mode.
6335
             *    If successful, the GPS echoes the phrase.
6336
             *
6337
             * 3. nmea_parse() sees the echo as RMC because the talker
6338
             *    ID is ignored, and fails to recognize the echo as
6339
             *    PGRMC and ignore it.
6340
             *
6341
             * 4. The mode is changed back to NMEA, resulting in an
6342
             *    infinite loop.
6343
             */
6344
0
        {"AAM", NULL, 0,  false, NULL},    // ignore Waypoint Arrival Alarm
6345
0
        {"ACCURACY", NULL, 1,  true, processACCURACY},
6346
0
        {"ACN", NULL, 0,  false, NULL},    // Alert Command, 4.10+
6347
0
        {"ALC", NULL, 0,  false, NULL},    // Cyclic Alert List, 4.10+
6348
0
        {"ALF", NULL, 0,  false, NULL},    // Alert Sentence, 4.10+
6349
0
        {"ALM", NULL, 0,  false, NULL},    // GPS Almanac Data
6350
0
        {"APB", NULL, 0,  false, NULL},    // Autopilot Sentence B
6351
0
        {"ACF", NULL, 0,  false, NULL},    // Alert Command Refused, 4.10+
6352
0
        {"AVR", NULL, 0,  false, NULL},    // Same as $PTNL,AVR
6353
0
        {"BOD", NULL, 0,  false, NULL},    // Bearing Origin to Destination
6354
        // Bearing & Distance to Waypoint, Great Circle
6355
0
        {"BWC", NULL, 12, false, processBWC},
6356
0
        {"DBT", NULL, 7,  false, processDBT},  // depth
6357
0
        {"DPT", NULL, 4,  false, processDPT},  // depth
6358
0
        {"DTM", NULL, 2,  false, processDTM},  // datum
6359
0
        {"EPV", NULL, 0,  false, NULL},     // Command/report Prop Value, 4.10+
6360
0
        {"GBS", NULL, 7,  false, processGBS},  // GNSS Sat Fault Detection
6361
0
        {"GGA", NULL, 13, false, processGGA},  // GPS fix data
6362
0
        {"GGK", NULL, 0,  false, NULL},        // Same as $PTNL,GGK
6363
0
        {"GGQ", NULL, 0,  false, NULL},        // Leica Position
6364
0
        {"GLC", NULL, 0,  false, NULL},        // Geographic Position, LoranC
6365
0
        {"GLL", NULL, 7,  true, processGLL},   // Position, Lat/Lon
6366
0
        {"GMP", NULL, 0,  false, NULL},        // Map Projection
6367
0
        {"GNS", NULL, 13, false, processGNS},  // GNSS fix data
6368
0
        {"GRS", NULL, 4,  false, processGRS},  // GNSS Range Residuals
6369
0
        {"GSA", NULL, 18, false, processGSA},  // DOP and Active sats
6370
0
        {"GST", NULL, 8,  false, processGST},  // Pseudorange error stats
6371
0
        {"GSV", NULL, 4,  false, processGSV},  // Sats in view
6372
        // UNICORE MEMES sensor data
6373
0
        {"GYOACC", NULL, 14,  false, processGYOACC},
6374
        // Inertial Sense info, over long
6375
        // INFO,928404541,1.0.2.0,2.2.2.0,-377462659,2.0.0.0,-53643429,
6376
        // Inertial Sense Inc,2025-01-10,16:06:13.50,GPX -1,4,0, *7D
6377
0
        {"INFO", NULL, 14,  false, processINFO},
6378
0
        {"HCR", NULL, 0,  false, NULL},        // Heading Correction, 4.10+
6379
        // Heading, Deviation and Variation
6380
0
        {"HDG", NULL, 0,  false, processHDG},
6381
0
        {"HDM", NULL, 3,  false, processHDM},   // $APHDM, Magnetic Heading
6382
0
        {"HDT", NULL, 1,  false, processHDT},   // Heading true
6383
        // Hell Andle, Roll Period, Roll Amplitude.  NMEA 4.10+
6384
0
        {"HRM", NULL, 0,  false, NULL},
6385
0
        {"HRP", NULL, 0, false, NULL},       // Serpentrio Headinf, Roll, Pitch
6386
0
        {"HWBIAS", NULL, 0, false, NULL},       // Unknown HuaWei sentence
6387
0
        {"LLK", NULL, 0, false, NULL},          // Leica local pos and GDOP
6388
0
        {"LLQ", NULL, 0, false, NULL},          // Leica local pos and quality
6389
0
        {"MLA", NULL, 0,  false, NULL},         // GLONASS Almana Data
6390
0
        {"MOB", NULL, 0,  false, NULL},         // Man Overboard, NMEA 4.10+
6391
0
        {"MSS", NULL, 0,  false, NULL},         // beacon receiver status
6392
0
        {"MTW", NULL, 3,  false, processMTW},   // Water Temperature
6393
0
        {"MWD", NULL, 0,  false, processMWD},   // Wind Direction and Speed
6394
0
        {"MWV", NULL, 0,  false, processMWV},   // Wind Speed and Angle
6395
0
        {"OHPR", NULL, 18, false, NULL},        // Oceanserver, not supported
6396
0
        {"OSD", NULL, 0,  false, NULL},             // ignore Own Ship Data
6397
        // general handler for Ashtech
6398
0
        {"PASHR", NULL, 3, false, processPASHR},
6399
        // Airoha proprietary
6400
0
        {"PAIR001", NULL, 3, false, processPAIR001},  // ACK/NAK
6401
0
        {"PAIR010", NULL, 5, false, processPAIR010},  // Request Aiding
6402
6403
        // Unicore proprietary
6404
0
        {"PDTINFO", NULL, 6, false, processPDTINFO},  // Product ID
6405
6406
0
        {"PEMT", NULL, 5, false, NULL},               // Evermore proprietary
6407
        // Furuno proprietary
6408
0
        {"PERDACK", NULL, 4, false, NULL},            // ACK
6409
        // {"PERDAPI", NULL, 3, false, NULL},         // Config Send
6410
0
        {"PERDCRD", NULL, 15, false, NULL},           // NLOSMASK?
6411
0
        {"PERDCRG", "DCR", 6, false, NULL},           // QZSS DC report
6412
0
        {"PERDCRJ", "FREQ", 9, false, NULL},          // Jamming Status
6413
0
        {"PERDCRP", NULL, 9, false, NULL},            // Position
6414
0
        {"PERDCRQ", NULL, 11, false, NULL},           // Galileo SAR
6415
0
        {"PERDCRW", "TPS1", 8, false, NULL},          // Time
6416
0
        {"PERDCRX", "TPS2", 12, false, NULL},         // PPS
6417
0
        {"PERDCRY", "TPS3", 11, false, NULL},         // Position Mode
6418
0
        {"PERDCRZ", "TPS4", 13, false, NULL},         // GCLK
6419
0
        {"PERDMSG", NULL, 3, false, NULL},            // Message
6420
0
        {"PERDSYS", "ANTSEL", 5, false, NULL},        // Antenna
6421
0
        {"PERDSYS", "FIXSESSION", 5, false, NULL},    // Fix Session
6422
0
        {"PERDSYS", "GPIO", 3, false, NULL},          // GPIO
6423
0
        {"PERDSYS", "VERSION", 6, false, NULL},       // Version
6424
6425
        // Inertial Sense
6426
0
        {"PGPSP", NULL, 18,  false, processPGPSP},     // GPS nav data
6427
6428
        // Jackson Labs proprietary
6429
0
        {"PJLTS", NULL, 11,  false, NULL},            // GPSDO status
6430
0
        {"PJLTV", NULL, 4,  false, NULL},             // Time and 3D velocity
6431
        // GPS-320FW -- $PLCS
6432
0
        {"PMGNST", NULL, 8, false, processPMGNST},    // Magellan Status
6433
        // MediaTek proprietary, EOL.  Replaced by Airoha
6434
0
        {"PMTK001", NULL, 3, false, processPMTK001},  // ACK/NAK
6435
0
        {"PMTK010", NULL, 2, false, NULL},            // System Message
6436
0
        {"PMTK011", NULL, 2, false, NULL},            // Text Message
6437
0
        {"PMTK424", NULL, 3, false, processPMTK424},
6438
0
        {"PMTK705", NULL, 4, false, processPMTK705},
6439
        // MediaTek/Trimble Satellite Channel Status
6440
0
        {"PMTKCHN", NULL, 0, false, NULL},
6441
6442
        // MTK-3301 -- $POLYN
6443
6444
        // Quectel proprietary
6445
0
        {"PQTMCFGEINSMSGERROR", NULL, 1, false, processPQxERR},      // Error
6446
0
        {"PQTMCFGEINSMSGOK", NULL, 1, false, processPQxOK},          // OK
6447
0
        {"PQTMCFGORIENTATIONERROR", NULL, 1, false, processPQxERR},  // Error
6448
0
        {"PQTMCFGORIENTATION", NULL, 3, false, NULL},       // Orientation
6449
0
        {"PQTMCFGORIENTATIONOK", NULL, 1, false, processPQxOK},      // OK
6450
0
        {"PQTMCFGWHEELTICKERROR", NULL, 1, false, processPQxERR},    // Error
6451
0
        {"PQTMCFGWHEELTICKOK", NULL, 1, false, processPQxOK},        // OK
6452
0
        {"PQTMGPS", NULL, 14, false, processPQTMGPS},  // GPS Status
6453
0
        {"PQTMIMU", NULL, 10, false, processPQTMIMU},  // IMU Raw Data
6454
0
        {"PQTMINS", NULL, 11, false, processPQTMINS},  // INS Results
6455
0
        {"PQTMQMPTERROR", NULL, 1, false, processPQxERR},       // Error
6456
0
        {"PQTMQMPT", NULL, 2, false, NULL},            // Meters / tick
6457
0
        {"PQTMVEHMSG", NULL, 2, false, NULL},          // Vehicle Info
6458
0
        {"PQTMVER", NULL, 4, false, processPQTMVER},   // Firmware info
6459
6460
0
        {"PQVERNO", NULL, 5, false, processPQVERNO},   // Version
6461
        // smart watch sensors, Yes: space!
6462
0
        {"PRHS ", NULL, 2,  false, processPRHS},
6463
0
        {"PRWIZCH", NULL, 0, false, NULL},          // Rockwell Channel Status
6464
0
        {"PSRF140", NULL, 0, false, NULL},          // SiRF ephemeris
6465
0
        {"PSRF150", NULL, 0, false, NULL},          // SiRF flow control
6466
0
        {"PSRF151", NULL, 0, false, NULL},          // SiRF Power
6467
0
        {"PSRF152", NULL, 0, false, NULL},          // SiRF ephemeris
6468
0
        {"PSRF155", NULL, 0, false, NULL},          // SiRF proprietary
6469
0
        {"PSRFEPE", NULL, 7, false, processPSRFEPE},  // SiRF Estimated Errors
6470
6471
        /* Serpentrio
6472
         * $PSSN,HRP  -- Heading Pitch, Roll
6473
         * $PSSN,RBD  -- Rover-Base Direction
6474
         * $PSSN,RBP  -- Rover-Base Position
6475
         * $PSSN,RBV  -- Rover-Base Velocity
6476
         * $PSSN,SNC  -- NTRIP Client Status
6477
         * $PSSN,TFM  -- RTCM coordinate transform
6478
         */
6479
0
        {"PSSN", NULL, 0, false, NULL},          // $PSSN
6480
6481
        /*
6482
         * Skytraq sentences take this format:
6483
         * $PSTI,type[,val[,val]]*CS
6484
         * type is a 2 or 3 digit subsentence type
6485
         *
6486
         * Note: these sentences can be at least 105 chars long.
6487
         * That violates the NMEA 3.01 max of 82.
6488
         */
6489
        // 1 PPS Timing report ID
6490
0
        {"PSTI", "000", 4, false, NULL},
6491
        // Active Antenna Status Report
6492
0
        {"PSTI", "001", 2, false, NULL},
6493
        // GPIO 10 event-triggered time & position stamp.
6494
0
        {"PSTI", "005", 2, false, NULL},
6495
        //  Recommended Minimum 3D GNSS Data
6496
0
        {"PSTI", "030", 16, false, processPSTI030},
6497
        // RTK Baseline
6498
0
        {"PSTI", "032", 16, false, processPSTI032},
6499
        // RTK RAW Measurement Monitoring Data
6500
0
        {"PSTI", "033", 27, false,  processPSTI033},
6501
        // RTK Baseline Data of Rover Moving Base Receiver
6502
0
        {"PSTI", "035", 8, false, processPSTI035},
6503
        // Heading, Pitch and Roll Messages of vehicle
6504
0
        {"PSTI", "036", 2, false, processPSTI036},
6505
        // $PSTM ST Micro STA8088xx/STA8089xx/STA8090xx
6506
0
        {"PSTM", NULL, 0, false, NULL},
6507
        // STM messages
6508
0
        {"PSTMCPU", NULL, 4, false, processPSTMCPU},
6509
0
        {"PSTMANTENNASTATUS", NULL, 4, false, processPSTMANTENNASTATUS},
6510
0
        {"PSTMVER", NULL, 1, false, processPSTMVER},
6511
6512
        /* Kongsberg Seatex AS. Seapath 320
6513
         * $PSXN,20,horiz-qual,hgt-qual,head-qual,rp-qual*csum
6514
         * $PSXN,21,event*csum
6515
         * $PSXN,22,gyro-calib,gyro-offs*csum
6516
         * $PSXN,23,roll,pitch,head,heave*csum
6517
         * $PSXN,24,roll-rate,pitch-rate,yaw-rate,vertical-vel*csum
6518
         */
6519
0
        {"PSXN", NULL, 0, false, NULL},
6520
0
        {"PTFTTXT", NULL, 0, false, NULL},        // unknown uptime
6521
6522
        /* Trimble Proprietary
6523
         * $PTNL,AVR
6524
         * $PTNL,GGK
6525
         */
6526
0
        {"PTNI", NULL, 0, false, NULL},
6527
6528
0
        {"PTKM", NULL, 0, false, NULL},           // Robertson RGC12 Gyro
6529
0
        {"PTNLRBA", NULL, 2, false, processPTNLRBA},  // Trimble/Ericsson antenna status
6530
0
        {"PTNLRHVR", NULL, 0, false, NULL},       // Trimble Software Version
6531
0
        {"PTNLRNM", NULL, 1, false, processPTNLRNM},  // Trimble receiver navigation mode
6532
0
        {"PTNLRPT", NULL, 0, false, NULL},        // Trimble Serial Port COnfig
6533
0
        {"PTNLRSVR", NULL, 0, false, NULL},       // Trimble Firmware Version
6534
0
        {"PTNLRTP", NULL, 3, false, processPTNLRTP},  // Trimble/Ericsson temperature
6535
0
        {"PTNLRXO", NULL, 2, false, processPTNLRXO},  // Trimble/Ericsson oscillator status
6536
0
        {"PTNLRZD", NULL, 0, false, NULL},        // Extended Time and Date
6537
0
        {"PTNTA", NULL, 8, false, processTNTA},
6538
0
        {"PTNTHTM", NULL, 9, false, processTNTHTM},
6539
0
        {"PUBX", NULL, 0, false, NULL},         // u-blox and Antaris
6540
0
        {"QSM", NULL, 3, false, NULL},          // QZSS DC Report
6541
0
        {"RBD", NULL, 0, false, NULL},       // Serpentrio rover-base direction
6542
0
        {"RBP", NULL, 0, false, NULL},       // Serpentrio rover-base position
6543
0
        {"RBV", NULL, 0, false, NULL},       // Serpentrio rover-base velocity
6544
0
        {"RLM", NULL, 0, false, NULL},       // Return Link Message, NMEA 4.10+
6545
        // ignore Recommended Minimum Navigation Info, waypoint
6546
0
        {"RMB", NULL, 0,  false, NULL},         // Recommended Min Nav Info
6547
0
        {"RMC", NULL, 8,  false, processRMC},   // Recommended Minimum Data
6548
0
        {"ROT", NULL, 3,  false, processROT},   // Rate of Turn
6549
0
        {"RPM", NULL, 0,  false, NULL},         // ignore Revolutions
6550
0
        {"RRT", NULL, 0, false, NULL},     // Report Route Transfer, NMEA 4.10+
6551
0
        {"RSA", NULL, 0,  false, NULL},         // Rudder Sensor Angle
6552
0
        {"RTE", NULL, 0,  false, NULL},         // ignore Routes
6553
        // UNICORE, Sensor Status invalid sender (SN)
6554
0
        {"SNRSTAT", NULL, 5,  false, processSNRSTAT},
6555
0
        {"SM1", NULL, 0, false, NULL},     // SafteyNET, All Ships, NMEA 4.10+
6556
0
        {"SM2", NULL, 0, false, NULL},     // SafteyNET, Coastal, NMEA 4.10+
6557
0
        {"SM3", NULL, 0, false, NULL},     // SafteyNET, Circular, NMEA 4.10+
6558
0
        {"SM4", NULL, 0, false, NULL},     // SafteyNET, Rectangular, NMEA 4.10+
6559
0
        {"SMB", NULL, 0, false, NULL},     // SafteyNET, Msg Body, NMEA 4.10+
6560
0
        {"SPW", NULL, 0, false, NULL},     // Security Password, NMEA 4.10+
6561
0
        {"SNC", NULL, 0, false, NULL},       // Serpentrio NTRIP client status
6562
0
        {"STI", NULL, 2,  false, processSTI},   // $STI  Skytraq
6563
0
        {"TFM", NULL, 0, false, NULL},          // Serpentrio Coord Transform
6564
0
        {"THS", NULL, 0,  false, processTHS},   // True Heading and Status
6565
0
        {"TRL", NULL, 0, false, NULL},     // AIS Xmit offline, NMEA 4.10+
6566
0
        {"TXT", NULL, 5,  false, processTXT},
6567
0
        {"TXTbase", NULL, 0,  false, NULL},     // RTCM 1029 TXT
6568
0
        {"VBW", NULL, 0,  false, NULL},         // Dual Ground/Water Speed
6569
0
        {"VDO", NULL, 0,  false, NULL},         // Own Vessel's Information
6570
0
        {"VDR", NULL, 0,  false, NULL},         // Set and Drift
6571
0
        {"VHW", NULL, 0,  false, NULL},         // Water Speed and Heading
6572
0
        {"VLW", NULL, 0,  false, NULL},         // Dual ground/water distance
6573
0
        {"VTG", NULL, 5,  false, processVTG},   // Course/speed over ground
6574
        // $APXDR, $HCXDR, Transducer measurements
6575
0
        {"XDR", NULL, 5,  false, processXDR},
6576
0
        {"XTE", NULL, 0,  false, NULL},         // Cross-Track Error
6577
0
        {"ZDA", NULL ,4,  false, processZDA},   // Time and Date
6578
0
        {NULL, NULL,  0,  false, NULL},         // no more
6579
0
    };
6580
6581
0
    unsigned count;
6582
0
    gps_mask_t mask = 0;
6583
0
    unsigned i, thistag = 0, lasttag;
6584
0
    char *p, *e;
6585
0
    volatile char *t;
6586
0
    char ts_buf1[TIMESPEC_LEN];
6587
0
    char ts_buf2[TIMESPEC_LEN];
6588
0
    bool skytraq_sti = false;
6589
0
    size_t mlen;
6590
6591
    /*
6592
     * We've had reports that on the Garmin GPS-10 the device sometimes
6593
     * (1:1000 or so) sends garbage packets that have a valid checksum
6594
     * but are like 2 successive NMEA packets merged together in one
6595
     * with some fields lost.  Usually these are much longer than the
6596
     * legal limit for NMEA, so we can cope by just tossing out overlong
6597
     * packets.  This may be a generic bug of all Garmin chipsets.
6598
     */
6599
    // codacy does not like strlen()
6600
0
    mlen = strnlen(sentence, NMEA_MAX + 1);
6601
0
    if (NMEA_MAX < mlen) {
6602
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
6603
0
                 "NMEA0183: Overlong packet of %zd+ chars rejected.\n",
6604
0
                 mlen);
6605
0
        return ONLINE_SET;
6606
0
    }
6607
6608
    // make an editable copy of the sentence
6609
0
    (void)strlcpy((char *)session->nmea.fieldcopy, sentence,
6610
0
                  sizeof(session->nmea.fieldcopy) - 1);
6611
    // discard the checksum part
6612
0
    for (p = (char *)session->nmea.fieldcopy;
6613
0
         ('*' != *p) && (' ' <= *p);) {
6614
0
        ++p;
6615
0
    }
6616
0
    if ('*' == *p) {
6617
0
        *p++ = ',';             // otherwise we drop the last field
6618
0
    }
6619
#ifdef SKYTRAQ_ENABLE_UNUSED
6620
    // $STI is special, no trailing *, or chacksum
6621
    if (0 != strncmp( "STI,", sentence, 4)) {
6622
        skytraq_sti = true;
6623
        *p++ = ',';             // otherwise we drop the last field
6624
    }
6625
#endif
6626
0
    *p = '\0';
6627
0
    e = p;
6628
6629
    // split sentence copy on commas, filling the field array
6630
0
    count = 0;
6631
0
    t = p;                      // end of sentence
6632
0
    p = (char *)session->nmea.fieldcopy + 1;  // beginning of tag, 'G' not '$'
6633
    // while there is a search string and we haven't run off the buffer...
6634
0
    while ((NULL != p) &&
6635
0
           (p <= t)) {
6636
0
        session->nmea.field[count] = p;      // we have a field. record it
6637
0
        if (NULL != (p = strchr(p, ','))) {  // search for the next delimiter
6638
0
            *p = '\0';                       // replace it with a NUL
6639
0
            count++;                         // bump the counters and continue
6640
0
            p++;
6641
0
            if (NMEA_MAX_FLD <= count) {
6642
                // ensure no overflow
6643
0
                break;
6644
0
            }
6645
0
        }
6646
0
    }
6647
0
    if (NMEA_MAX_FLD < count) {
6648
        // ensure no overflow, OSS Fuzz 515100083
6649
0
        count = NMEA_MAX_FLD;
6650
0
    }
6651
6652
    // point remaining fields at empty string, just in case
6653
0
    for (i = count; i < NMEA_MAX_FLD; i++) {
6654
0
        session->nmea.field[i] = e;
6655
0
    }
6656
6657
    // sentences handlers will tell us when they have fractional time
6658
0
    session->nmea.latch_frac_time = false;
6659
    // GSA and GSV will set this if more in that series to come.
6660
0
    session->nmea.gsx_more = false;
6661
6662
#ifdef __UNUSED
6663
    // debug
6664
    GPSD_LOG(0, &session->context->errout,
6665
             "NMEA0183: got %s\n", session->nmea.field[0]);
6666
#endif // __UNUSED
6667
6668
    // dispatch on field zero, the sentence tag
6669
0
    for (i = 0; i < NMEA_NUM; ++i) {
6670
0
        char *s = session->nmea.field[0];
6671
6672
        // CODACY #350416, wants explicit numeric end check
6673
0
        if ((NMEA_NUM - 1) <= i ||
6674
0
            NULL == nmea_phrase[i].name) {
6675
0
            mask = ONLINE_SET;
6676
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
6677
0
                     "NMEA0183: Unknown sentence type %s\n",
6678
0
                     session->nmea.field[0]);
6679
0
            break;
6680
0
        }
6681
        // strnlen() to shut up codacy
6682
0
        if (3 == strnlen(nmea_phrase[i].name, 4) &&
6683
0
            !skytraq_sti) {
6684
            // $STI is special
6685
0
            s += 2;             // skip talker ID
6686
0
        }
6687
0
        if (0 != strcmp(nmea_phrase[i].name, s)) {
6688
            // no match
6689
0
            continue;
6690
0
        }
6691
0
        if (NULL != nmea_phrase[i].name1 &&
6692
0
            0 != strcmp(nmea_phrase[i].name1, session->nmea.field[1])) {
6693
            // no match on field 2.  As in $PSTI,030,
6694
0
            continue;
6695
0
        }
6696
        // got a match
6697
0
        if (NULL == nmea_phrase[i].decoder) {
6698
            // no decoder for this sentence
6699
0
            mask = ONLINE_SET;
6700
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
6701
0
                     "NMEA0183: No decoder for sentence type %s\n",
6702
0
                     session->nmea.field[0]);
6703
0
            break;
6704
0
        }
6705
0
        if (count < (unsigned)nmea_phrase[i].nf) {
6706
            // sentence too short
6707
0
            mask = ONLINE_SET;
6708
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
6709
0
                     "NMEA0183: Sentence %s too short\n",
6710
0
                     session->nmea.field[0]);
6711
0
            break;
6712
0
        }
6713
0
        mask = (nmea_phrase[i].decoder)(count, session->nmea.field,
6714
0
                                        session);
6715
0
        session->nmea.cycle_continue = nmea_phrase[i].cycle_continue;
6716
        /*
6717
         * Must force this to be nz, as we're going to rely on a zero
6718
         * value to mean "no previous tag" later.
6719
         */
6720
        // FIXME: this fails on Skytrak, $PSTI,xx, many different xx
6721
0
        thistag = i + 1;
6722
0
        break;
6723
0
    }
6724
6725
    // prevent overaccumulation of sat reports
6726
0
    if (!str_starts_with(session->nmea.field[0] + 2, "GSV")) {
6727
        // This assumes all $xxGSV are contiguous.
6728
0
        if (0 != session->nmea.last_gsv_talker) {
6729
0
            session->nmea.end_gsv_talker = session->nmea.last_gsv_talker;
6730
0
        }
6731
0
        session->nmea.last_gsv_talker = '\0';
6732
0
    }
6733
0
    if (!str_starts_with(session->nmea.field[0] + 2, "GSA")) {
6734
0
        session->nmea.last_gsa_talker = '\0';
6735
0
    }
6736
6737
    // timestamp recording for fixes happens here
6738
0
    if (0 != (mask & TIME_SET)) {
6739
0
        if (0 == session->nmea.date.tm_year &&
6740
0
            0 == session->nmea.date.tm_mday) {
6741
            // special case to time zero
6742
0
            session->newdata.time = (timespec_t){0, 0};
6743
0
        } else {
6744
0
            session->newdata.time = gpsd_utc_resolve(session);
6745
0
        }
6746
6747
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
6748
0
                 "NMEA0183: %s newtime is %s = "
6749
0
                 "%d-%02d-%02dT%02d:%02d:%02d.%03ldZ\n",
6750
0
                 session->nmea.field[0],
6751
0
                 timespec_str(&session->newdata.time, ts_buf1, sizeof(ts_buf1)),
6752
0
                 1900 + session->nmea.date.tm_year,
6753
0
                 session->nmea.date.tm_mon + 1,
6754
0
                 session->nmea.date.tm_mday,
6755
0
                 session->nmea.date.tm_hour,
6756
0
                 session->nmea.date.tm_min,
6757
0
                 session->nmea.date.tm_sec,
6758
0
                 session->nmea.subseconds.tv_nsec / 1000000L);
6759
        /*
6760
         * If we have time and PPS is available, assume we have good time.
6761
         * Because this is a generic driver we don't really have enough
6762
         * information for a sharper test, so we'll leave it up to the
6763
         * PPS code to do its own sanity filtering.
6764
         */
6765
0
        mask |= NTPTIME_IS;
6766
0
    }
6767
6768
    /*
6769
     * The end-of-cycle detector.  This code depends on just one
6770
     * assumption: if a sentence with a timestamp occurs just before
6771
     * start of cycle, then it is always good to trigger a report on
6772
     * that sentence in the future.  For devices with a fixed cycle
6773
     * this should work perfectly, locking in detection after one
6774
     * cycle.  Most split-cycle devices (Garmin 48, for example) will
6775
     * work fine.  Problems will only arise if a a sentence that
6776
     * occurs just before timestamp increments also occurs in
6777
     * mid-cycle, as in the Garmin eXplorist 210; those might jitter.
6778
     */
6779
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
6780
0
             "NMEA0183: %s time %s last %s latch %d cont %d\n",
6781
0
             session->nmea.field[0],
6782
0
             timespec_str(&session->nmea.this_frac_time, ts_buf1,
6783
0
                          sizeof(ts_buf1)),
6784
0
             timespec_str(&session->nmea.last_frac_time, ts_buf2,
6785
0
                          sizeof(ts_buf2)),
6786
0
             session->nmea.latch_frac_time,
6787
0
             session->nmea.cycle_continue);
6788
0
    lasttag = session->nmea.lasttag;
6789
0
    if (session->nmea.gsx_more) {
6790
        // more to come, so ignore for cycle ender
6791
        // appears that GSA and GSV never start a cycle.
6792
0
    } else if (session->nmea.latch_frac_time) {
6793
0
        timespec_t ts_delta;
6794
0
        TS_SUB(&ts_delta, &session->nmea.this_frac_time,
6795
0
                          &session->nmea.last_frac_time);
6796
0
        if (0.01 < fabs(TSTONS(&ts_delta))) {
6797
            // time changed
6798
0
            mask |= CLEAR_IS;
6799
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6800
0
                     "NMEA0183: %s starts a reporting cycle. lasttag %d\n",
6801
0
                     session->nmea.field[0], lasttag);
6802
            /*
6803
             * Have we seen a previously timestamped NMEA tag?
6804
             * If so, designate as end-of-cycle marker.
6805
             * But not if there are continuation sentences;
6806
             * those get sorted after the last timestamped sentence
6807
             *
6808
             */
6809
0
            if (0 < lasttag &&
6810
0
                false == (session->nmea.cycle_enders[lasttag]) &&
6811
0
                !session->nmea.cycle_continue) {
6812
0
                session->nmea.cycle_enders[lasttag] = true;
6813
                // we might have a (somewhat) reliable end-of-cycle
6814
0
                session->cycle_end_reliable = true;
6815
0
                GPSD_LOG(LOG_PROG, &session->context->errout,
6816
0
                         "NMEA0183: tagged %s as a cycle ender. %u\n",
6817
0
                         nmea_phrase[lasttag - 1].name,
6818
0
                         lasttag);
6819
0
            }
6820
0
        }
6821
0
    } else {
6822
        // ignore multiple sequential, like GSV, GSA
6823
        // extend the cycle to an un-timestamped sentence?
6824
0
        if (true == session->nmea.cycle_enders[lasttag]) {
6825
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6826
0
                     "NMEA0183: %s is just after a cycle ender. (%s)\n",
6827
0
                     session->nmea.field[0],
6828
0
                     gps_maskdump(mask));
6829
0
            if (0 != (mask & ~ONLINE_SET)) {
6830
                // new data... after cycle ender
6831
0
                mask |= REPORT_IS;
6832
0
            }
6833
0
        }
6834
0
        if (session->nmea.cycle_continue) {
6835
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6836
0
                     "NMEA0183: %s extends the reporting cycle.\n",
6837
0
                     session->nmea.field[0]);
6838
            // change ender
6839
0
            session->nmea.cycle_enders[lasttag] = false;
6840
0
            session->nmea.cycle_enders[thistag] = true;
6841
            // have a cycle ender
6842
0
            session->cycle_end_reliable = true;
6843
0
        }
6844
0
    }
6845
6846
    // here's where we check for end-of-cycle
6847
0
    if ((session->nmea.latch_frac_time ||
6848
0
         session->nmea.cycle_continue) &&
6849
0
        (true == session->nmea.cycle_enders[thistag]) &&
6850
0
        !session->nmea.gsx_more) {
6851
0
        if (NULL == nmea_phrase[i].name1) {
6852
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6853
0
                     "NMEA0183: %s ends a reporting cycle.\n",
6854
0
                     session->nmea.field[0]);
6855
0
        } else {
6856
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
6857
0
                     "NMEA0183: %s,%s ends a reporting cycle.\n",
6858
0
                     session->nmea.field[0],
6859
0
                     session->nmea.field[1]);
6860
0
        }
6861
0
        mask |= REPORT_IS;
6862
0
    }
6863
0
    if (session->nmea.latch_frac_time) {
6864
0
        session->nmea.lasttag = thistag;
6865
0
    }
6866
6867
    /* don't downgrade mode if holding previous fix
6868
     * usually because of xxRMC which does not report 2D/3D */
6869
0
    if (MODE_SET == (mask & MODE_SET) &&
6870
0
        MODE_3D == session->gpsdata.fix.mode &&
6871
0
        MODE_NO_FIX != session->newdata.mode &&
6872
0
        (0 != isfinite(session->lastfix.altHAE) ||
6873
0
         0 != isfinite(session->oldfix.altHAE) ||
6874
0
         0 != isfinite(session->lastfix.altMSL) ||
6875
0
         0 != isfinite(session->oldfix.altMSL))) {
6876
0
        session->newdata.mode = session->gpsdata.fix.mode;
6877
0
    }
6878
0
    return mask;
6879
0
}
6880
6881
6882
/* add NMEA checksum to a possibly terminated sentence
6883
 * if \0 terminated adds exactly 5 chars: "*XX\n\n"
6884
 * if *\0 terminated adds exactly 4 chars: "XX\n\n"
6885
 */
6886
void nmea_add_checksum(char *sentence)
6887
0
{
6888
0
    unsigned char sum = '\0';
6889
0
    char c, *p = sentence;
6890
6891
0
    if ('$' == *p ||
6892
0
        '!' == *p) {
6893
0
        p++;
6894
0
    }
6895
0
    while (('*' != (c = *p)) &&
6896
0
           ('\0' != c)) {
6897
0
        sum ^= c;
6898
0
        p++;
6899
0
    }
6900
0
    (void)snprintf(p, 6, "*%02X\r\n", (unsigned)sum);
6901
0
}
6902
6903
// ship a command to the GPS, adding * and correct checksum
6904
ssize_t nmea_write(struct gps_device_t *session, char *buf, size_t len UNUSED)
6905
0
{
6906
0
    (void)strlcpy(session->msgbuf, buf, sizeof(session->msgbuf));
6907
0
    if ('$' == session->msgbuf[0]) {
6908
0
        (void)strlcat(session->msgbuf, "*", sizeof(session->msgbuf));
6909
0
        nmea_add_checksum(session->msgbuf);
6910
0
    } else {
6911
0
        (void)strlcat(session->msgbuf, "\r\n", sizeof(session->msgbuf));
6912
0
    }
6913
    // codacy hates strlen()
6914
0
    session->msgbuflen = strnlen(session->msgbuf, sizeof(session->msgbuf));
6915
0
    return gpsd_write(session, session->msgbuf, session->msgbuflen);
6916
0
}
6917
6918
ssize_t nmea_send(struct gps_device_t * session, const char *fmt, ...)
6919
0
{
6920
0
    char buf[BUFSIZ];
6921
0
    va_list ap;
6922
6923
0
    va_start(ap, fmt);
6924
0
    (void)vsnprintf(buf, sizeof(buf) - 5, fmt, ap);
6925
0
    va_end(ap);
6926
    // codacy hates strlen()
6927
0
    return nmea_write(session, buf, strnlen(buf, sizeof(buf)));
6928
0
}
6929
6930
// vim: set expandtab shiftwidth=4