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/libgps/gpsutils.c
Line
Count
Source
1
/* gpsutils.c -- code shared between low-level and high-level interfaces
2
 *
3
 * This file is Copyright by the GPSD project
4
 * SPDX-License-Identifier: BSD-2-clause
5
 */
6
7
/* The strptime prototype is not provided unless explicitly requested.
8
 * We also need to set the value high enough to signal inclusion of
9
 * newer features (like clock_gettime).  See the POSIX spec for more info:
10
 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_02_01_02 */
11
12
#include "../include/gpsd_config.h"    // must be before all includes
13
14
#include <ctype.h>
15
#include <errno.h>
16
#include <math.h>
17
#include <stdbool.h>
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <sys/select.h>  // for to have a pselect(2) prototype a la POSIX
22
#include <sys/time.h>    // for to have a pselect(2) prototype a la SuS
23
#include <time.h>
24
25
#include "../include/gps.h"
26
#include "../include/libgps.h"
27
#include "../include/os_compat.h"
28
#include "../include/timespec.h"
29
30
#ifdef USE_QT
31
#include <QDateTime>
32
#include <QStringList>
33
#endif
34
35
// decodes for gps_fix_t
36
37
// ant_stat
38
const struct vlist_t vant_status[] = {
39
    {ANT_UNK, "UNK"},     // 0
40
    {ANT_OK, "OK"},     // 1
41
    {ANT_OPEN, "OPEN"},    // 2
42
    {ANT_SHORT, "SHORT"},   // 3
43
    {0, NULL},
44
};
45
46
// gnssId names
47
const struct vlist_t vgnssId[] = {
48
    {0, "GPS"},
49
    {1, "SBAS"},
50
    {2, "GAL"},
51
    {3, "BDS"},
52
    {4, "IMES"},
53
    {5, "QZSS"},
54
    {6, "GLO"},
55
    {7, "NavIC"},
56
    {0, NULL},
57
};
58
59
// mode val to mode string
60
const struct vlist_t vmode_str[] = {
61
    {1, "No Fix"},
62
    {2, "2D Fix"},
63
    {3, "3D Fix"},
64
    {0, NULL},
65
};
66
67
// status val to status string
68
const struct vlist_t vstatus_str[] = {
69
    {0, "UNK"},
70
    {1, "GPS"},
71
    {2, "DGPS"},
72
    {3, "RTK_FIX"},
73
    {4, "RTK_FLT"},
74
    {5, "DR"},
75
    {6, "GNSSDR"},
76
    {7, "TIME"},      // Surveyd
77
    {8, "SIM "},
78
    {0, NULL},
79
};
80
81
/* char2str(ch, vlist) - given a char, return a matching string.
82
 *
83
 * Return: pointer to string
84
 */
85
const char *char2str(unsigned char ch, const struct clist_t *clist)
86
491
{
87
1.74k
    while (NULL != clist->str) {
88
1.46k
        if (clist->ch == ch) {
89
213
            return clist->str;
90
213
        }
91
1.25k
        clist++;
92
1.25k
    }
93
278
    return "Unk";
94
491
}
95
96
/* flags2str(val, vlist) - given flags, return a matching string.
97
 *
98
 * flags the flags to find in vlist
99
 * buffer -  the buffer to return string in
100
 ( buflen - length of buffer
101
 *
102
 * Return: pointer to passed in buffer
103
 *         A string matching the flags.
104
 */
105
const char *flags2str(unsigned long flags, const struct flist_t *flist,
106
                      char *buffer, size_t buflen)
107
0
{
108
0
    buffer[0] = '\0';
109
0
    while (NULL != flist->str) {
110
0
        if (flist->val == (flist->mask & flags)) {
111
0
            if ('\0' != buffer[0]) {
112
0
                strlcat(buffer, ",", buflen);
113
0
            }
114
0
            strlcat(buffer, flist->str, buflen);
115
0
        }
116
0
        flist++;
117
0
    }
118
0
    return buffer;
119
0
}
120
121
/* Translate table from gnmssid/sigid to NMEA sigid, signal name and
122
 * RINEX observation code
123
 */
124
1.13k
#define SIGID_NUM 16
125
struct sig_xlate_t {
126
    const char *name;          // plain name
127
    const char *obs;           // RINEX observation code
128
    uint8_t nmea_sigid;        // NMEA 4.10 signal id.  0 == None
129
} const sig_xlate[GNSSID_CNT][SIGID_NUM] = {
130
    {   // 0 - GPS
131
        {"L1 C/A",   "C1C", 1},
132
        {NULL,       NULL},
133
        {NULL,       NULL},
134
        {"L2 CL",    "C2L", 6},
135
        {"L2 CM",    "C2S", 5},
136
        {NULL,       NULL},        // 5
137
        {"L5 I",     "C5I", 7},
138
        {"L5 Q",     "C5Q", 8},
139
    },
140
    {   // 1- SBAS
141
        {"L1C",      "C1C", 1},
142
        // {1, "L5I", "C5I"},  // ??
143
    },
144
    {   // 2 - Galileo
145
        {"E1 C",     "C1C", 7},
146
        {"E1 B",     "C1B", 7},
147
        {NULL,       NULL},
148
        {"E5 aI",    "C5I", 1},  // 3
149
        {"E5 aQ",    "C5Q", 1},
150
        {"E5 bI",    "C7I", 2},
151
        {"E5 bQ",    "C7Q", 2},
152
        {NULL,       NULL},
153
        {"E6 B",     "C6B", 5},   // 8
154
        {"E6 C",     "C6C", 5},
155
        {"E6 A",     "C6A", 4},
156
    },
157
    {   // 3 - BeiDou
158
        {"B1I D1",   "C2I", 1},
159
        {"B1I D2",   "C2I", 1},
160
        {"B2I D1",   "C7I", 0xb},
161
        {"B2I D2",   "C7I", 0xb},
162
        {"B3I D1",   "C6I", 0xb},
163
        {"B1 Cp",    "C1P", 3},  // 5
164
        {"B1 Cd",    "C1D", 3},
165
        {"B2 ap",    "C5P", 5},
166
        {"B2 ad",    "C5P", 5},
167
        {NULL,       NULL},       // 9
168
        {"B3I D2",   "C6I", 0xb},
169
    },
170
    {   // 4 - IMES
171
        {"L5 A",     NULL, 0},
172
    },
173
    {   // 5 - QZSS
174
        {"L1 C/A",   "C1C", 1},
175
        {"L1 S",     "C1Z", 4},
176
        {NULL,       NULL},
177
        {NULL,       NULL},
178
        {"L2 CM",    "C2S", 5},
179
        {"L2 CL",    "C2L", 6},   // 5
180
        {NULL,       NULL},
181
        {NULL,       NULL},
182
        {"L5 I",     "C5I", 7},
183
        {"L5 Q",     "C5Q", 8},
184
        {NULL,       NULL},    //  10
185
        {NULL,       NULL},
186
        {"L1 C/B",   "C1E", 0},
187
    },
188
    {   // 6 - GLONASS
189
        {"L1 OF",    "C1C", 1},
190
        {NULL,       NULL},
191
        {"L2 OF",    "C2C", 3},
192
    },
193
    {   // 8 - IRNSS (NavIC)
194
        {"L5 A",     "C5A", 1},
195
    },
196
};
197
198
199
/* sigid2str()
200
 *
201
 * given a gpsd gnssid, and gpsd sigid, return a string for the
202
 * sigid.  These are (mostly) UBX compatible.  NOT NMEA compatible.
203
 *
204
 * See sigid in include/gps.h
205
 *
206
 * return: static const string
207
 */
208
const char *sigid2str(unsigned char gnssid, unsigned char sigid)
209
0
{
210
0
    const char *rets = "Unk";
211
212
0
    if (GNSSID_CNT <= gnssid) {
213
0
        rets = "GNSS-Unk";
214
0
    } else if (SIGID_NUM <= sigid) {
215
0
        rets = "SIG-Unk";
216
0
    } else {
217
0
        rets = sig_xlate[gnssid][sigid].name;
218
0
        if (NULL == rets) {
219
0
            rets = "Unk";
220
0
        }
221
0
    }
222
223
0
    return rets;
224
0
}
225
226
/* sigid2obs()
227
 *
228
 * given a gpsd gnssid, and gpsd sigid, return a string for the
229
 * RINEX observation code.  These are (mostly) UBX compatible.
230
 *  NOT NMEA compatible.
231
 *
232
 * return: static const string
233
 */
234
const char *sigid2obs(unsigned char gnssid, unsigned char sigid)
235
3.83k
{
236
3.83k
    const char *rets = "Unk";
237
238
3.83k
    if (GNSSID_CNT <= gnssid) {
239
2.70k
        rets = "GNSS-Unk";
240
2.70k
    } else if (SIGID_NUM <= sigid) {
241
282
        rets = "SIG-Unk";
242
848
    } else {
243
848
        rets = sig_xlate[gnssid][sigid].obs;
244
848
        if (NULL == rets) {
245
231
            rets = "Unk";
246
231
        }
247
848
    }
248
249
3.83k
    return rets;
250
3.83k
}
251
252
/* val2str(val, vlist) - given a value, return a matching string.
253
 *
254
 * val the value to find in vlist
255
 *
256
 * Return: pointer to static string
257
 *         The string matching val, or "Unk".
258
 */
259
const char *val2str(unsigned long val, const struct vlist_t *vlist)
260
0
{
261
0
    while (NULL != vlist->str) {
262
0
        if (vlist->val == val) {
263
0
            return vlist->str;
264
0
        }
265
0
        vlist++;
266
0
    }
267
0
    return "Unk";
268
0
}
269
270
/*
271
 * Berkeley implementation of strtod(), inlined to avoid locale problems
272
 * with the decimal point and stripped down to an atof()-equivalent.
273
 */
274
275
/* Takes a decimal ASCII floating-point number, optionally
276
 * preceded by white space.  Must have form "SI.FE-X",
277
 * S may be ither of the signs may be "+", "-", or omitted.
278
 * I is the integer part of the mantissa,
279
 * F is the fractional part of the mantissa,
280
 * X is the exponent.
281
 * Either I or F may be omitted, or both.
282
 * The decimal point isn't necessary unless F is
283
 * present.  The "E" may actually be an "e".  E and X
284
 * may both be omitted (but not just one).
285
 *
286
 * This code appears to e from early BSD.
287
 *
288
 * returns NaN if:
289
 *    *string is zero length,
290
 *    the first non-white space is not negative sign ('-'), positive sign ('_')
291
 *    or a digit
292
 */
293
double safe_atof(const char *string)
294
87.1k
{
295
87.1k
    static int maxExponent = 511;   /* Largest possible base 10 exponent.  Any
296
                                     * exponent larger than this will already
297
                                     * produce underflow or overflow, so there's
298
                                     * no need to worry about additional digits.
299
                                     */
300
    /* Table giving binary powers of 10.  Entry is 10^2^i.
301
     * Used to convert decimal exponents into floating-point numbers. */
302
87.1k
    static double powersOf10[] = {
303
87.1k
        10.,
304
87.1k
        100.,
305
87.1k
        1.0e4,
306
87.1k
        1.0e8,
307
87.1k
        1.0e16,
308
87.1k
        1.0e32,
309
87.1k
        1.0e64,
310
87.1k
        1.0e128,
311
87.1k
        1.0e256
312
87.1k
    };
313
314
87.1k
    bool sign = false, expSign = false;
315
87.1k
    double fraction, dblExp, *d;
316
87.1k
    const char *p;
317
87.1k
    unsigned char c;
318
87.1k
    int exp = 0;                // Exponent read from "EX" field.
319
87.1k
    int fracExp = 0;            /* Exponent that derives from the fractional
320
                                 * part.  Under normal circumstatnces, it is
321
                                 * the negative of the number of digits in F.
322
                                 * However, if I is very long, the last digits
323
                                 * of I get dropped (otherwise a long I with a
324
                                 * large negative exponent could cause an
325
                                 * unnecessary overflow on I alone).  In this
326
                                 * case, fracExp is incremented one for each
327
                                 * dropped digit. */
328
87.1k
    int mantSize;               // Number of digits in mantissa.
329
87.1k
    int decPt;                  /* Number of mantissa digits BEFORE decimal
330
                                 * point. */
331
87.1k
    const char *pExp;           /* Temporarily holds location of exponent
332
                                 * in string. */
333
334
    /*
335
     * Strip off leading blanks and check for a sign.
336
     */
337
338
    // FIXME: JSON is UTF-8, but isspace() islocale dependent...
339
340
87.1k
    p = string;
341
87.1k
    while (isspace((int)*p)) {
342
510
        p += 1;
343
510
    }
344
87.1k
    if (isdigit((unsigned char)*p)) {
345
        // ignore
346
55.1k
    } else if ('-' == *p) {
347
3.06k
        sign = true;
348
3.06k
        p += 1;
349
52.0k
    } else if ('+' == *p) {
350
902
        p += 1;
351
51.1k
    } else if ('.' == *p) {
352
        // ignore
353
38.8k
    } else {
354
38.8k
        return NAN;
355
38.8k
    }
356
357
    /*
358
     * Count the number of digits in the mantissa (including the decimal
359
     * point), and also locate the decimal point.
360
     */
361
362
48.3k
    decPt = -1;
363
197k
    for (mantSize = 0; ; mantSize += 1) {
364
197k
        c = *p;
365
197k
        if (!isdigit(c)) {
366
67.4k
            if ((c != '.') || (decPt >= 0)) {
367
48.3k
                break;
368
48.3k
            }
369
19.1k
            decPt = mantSize;
370
19.1k
        }
371
149k
        p += 1;
372
149k
    }
373
374
    /*
375
     * Now suck up the digits in the mantissa.  Use two integers to
376
     * collect 9 digits each (this is faster than using floating-point).
377
     * If the mantissa has more than 18 digits, ignore the extras, since
378
     * they can't affect the value anyway.
379
     */
380
381
48.3k
    pExp  = p;
382
48.3k
    p -= mantSize;
383
48.3k
    if (decPt < 0) {
384
29.2k
        decPt = mantSize;
385
29.2k
    } else {
386
19.1k
        mantSize -= 1;                  // One of the digits was the point.
387
19.1k
    }
388
48.3k
    if (mantSize > 18) {
389
802
        fracExp = decPt - 18;
390
802
        mantSize = 18;
391
47.5k
    } else {
392
47.5k
        fracExp = decPt - mantSize;
393
47.5k
    }
394
48.3k
    if (mantSize == 0) {
395
6.67k
        fraction = 0.0;
396
        // p = string;
397
6.67k
        goto done;
398
41.6k
    } else {
399
41.6k
        int frac1, frac2;
400
401
41.6k
        frac1 = 0;
402
54.4k
        for ( ; mantSize > 9; mantSize -= 1) {
403
12.7k
            c = *p;
404
12.7k
            p += 1;
405
12.7k
            if ('.' == c) {
406
765
                c = *p;
407
765
                p += 1;
408
765
            }
409
12.7k
            frac1 = 10*frac1 + (c - '0');
410
12.7k
        }
411
41.6k
        frac2 = 0;
412
147k
        for (; mantSize > 0; mantSize -= 1) {
413
105k
            c = *p;
414
105k
            p += 1;
415
105k
            if ('.' == c) {
416
12.5k
                c = *p;
417
12.5k
                p += 1;
418
12.5k
            }
419
105k
            frac2 = 10*frac2 + (c - '0');
420
105k
        }
421
41.6k
        fraction = (1.0e9 * frac1) + frac2;
422
41.6k
    }
423
424
    /*
425
     * Skim off the exponent.
426
     */
427
428
41.6k
    p = pExp;
429
41.6k
    if (('E' == *p) ||
430
39.2k
        ('e' == *p)) {
431
6.17k
        p += 1;
432
6.17k
        if ('-' == *p) {
433
1.45k
            expSign = true;
434
1.45k
            p += 1;
435
4.71k
        } else {
436
4.71k
            if ('+' == *p) {
437
461
                p += 1;
438
461
            }
439
4.71k
            expSign = false;
440
4.71k
        }
441
12.4k
        while (isdigit((unsigned char) *p)) {
442
12.4k
            exp = exp * 10 + (*p - '0');
443
12.4k
            if (1024 < exp) {
444
1.93k
                if (true == expSign) {
445
                    // exponent underflow!
446
942
                    if (sign) {
447
467
                        return -0.0;
448
467
                    }
449
475
                    return 0.0;
450
942
                } // else  exponent overflow!
451
993
                if (sign) {
452
465
                    return -INFINITY;
453
465
                }
454
528
                return INFINITY;
455
993
            }
456
10.4k
            p += 1;
457
10.4k
        }
458
6.17k
    }
459
39.7k
    if (expSign) {
460
515
        exp = fracExp - exp;
461
39.2k
    } else {
462
39.2k
        exp = fracExp + exp;
463
39.2k
    }
464
465
    /*
466
     * Generate a floating-point number that represents the exponent.
467
     * Do this by processing the exponent one bit at a time to combine
468
     * many powers of 2 of 10. Then combine the exponent with the
469
     * fraction.
470
     */
471
472
39.7k
    if (0 > exp) {
473
13.2k
        expSign = true;
474
13.2k
        exp = -exp;
475
26.4k
    } else {
476
26.4k
        expSign = false;
477
26.4k
    }
478
39.7k
    if (exp > maxExponent) {
479
664
        exp = maxExponent;
480
664
        errno = ERANGE;
481
664
    }
482
39.7k
    dblExp = 1.0;
483
74.2k
    for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
484
34.5k
        if (exp & 01) {
485
24.6k
            dblExp *= *d;
486
24.6k
        }
487
34.5k
    }
488
39.7k
    if (expSign) {
489
13.2k
        fraction /= dblExp;
490
26.4k
    } else {
491
26.4k
        fraction *= dblExp;
492
26.4k
    }
493
494
46.4k
done:
495
46.4k
    if (sign) {
496
2.13k
        return -fraction;
497
2.13k
    }
498
44.2k
    return fraction;
499
46.4k
}
500
501
16.3k
#define MONTHSPERYEAR   12      // months per calendar year
502
503
// clear a baseline_t
504
static void gps_clear_base(struct baseline_t *base)
505
5.26M
{
506
5.26M
    base->status = STATUS_UNK;
507
5.26M
    base->east = NAN;
508
5.26M
    base->north = NAN;
509
5.26M
    base->up = NAN;
510
5.26M
    base->length = NAN;
511
5.26M
    base->course = NAN;
512
5.26M
    base->ratio = NAN;
513
5.26M
}
514
515
// stuff a fix structure with recognizable out-of-band values
516
void gps_clear_fix(struct gps_fix_t *fixp)
517
5.18M
{
518
5.18M
    memset(fixp, 0, sizeof(struct gps_fix_t));
519
5.18M
    fixp->altitude = NAN;        // DEPRECATED, undefined
520
5.18M
    fixp->altHAE = NAN;
521
5.18M
    fixp->altMSL = NAN;
522
5.18M
    fixp->climb = NAN;
523
5.18M
    fixp->depth = NAN;
524
5.18M
    fixp->epc = NAN;
525
5.18M
    fixp->epd = NAN;
526
5.18M
    fixp->eph = NAN;
527
5.18M
    fixp->eps = NAN;
528
5.18M
    fixp->ept = NAN;
529
5.18M
    fixp->epv = NAN;
530
5.18M
    fixp->epx = NAN;
531
5.18M
    fixp->epy = NAN;
532
5.18M
    fixp->latitude = NAN;
533
5.18M
    fixp->longitude = NAN;
534
5.18M
    fixp->magnetic_track = NAN;
535
5.18M
    fixp->magnetic_var = NAN;
536
5.18M
    fixp->mode = MODE_NOT_SEEN;
537
5.18M
    fixp->sep = NAN;
538
5.18M
    fixp->speed = NAN;
539
5.18M
    fixp->track = NAN;
540
    // clear ECEF too
541
5.18M
    fixp->ecef.x = NAN;
542
5.18M
    fixp->ecef.y = NAN;
543
5.18M
    fixp->ecef.z = NAN;
544
5.18M
    fixp->ecef.vx = NAN;
545
5.18M
    fixp->ecef.vy = NAN;
546
5.18M
    fixp->ecef.vz = NAN;
547
5.18M
    fixp->ecef.pAcc = NAN;
548
5.18M
    fixp->ecef.vAcc = NAN;
549
5.18M
    fixp->errEllipseOrient = NAN;
550
5.18M
    fixp->errEllipseMajor = NAN;
551
5.18M
    fixp->errEllipseMinor = NAN;
552
5.18M
    fixp->NED.relPosN = NAN;
553
5.18M
    fixp->NED.relPosE = NAN;
554
5.18M
    fixp->NED.relPosD = NAN;
555
5.18M
    fixp->NED.velN = NAN;
556
5.18M
    fixp->NED.velE = NAN;
557
5.18M
    fixp->NED.velD = NAN;
558
5.18M
    fixp->geoid_sep = NAN;
559
5.18M
    fixp->dgps_age = NAN;
560
5.18M
    fixp->dgps_station = -1;
561
5.18M
    fixp->temp = NAN;
562
5.18M
    fixp->wanglem = NAN;
563
5.18M
    fixp->wangler = NAN;
564
5.18M
    fixp->wanglet = NAN;
565
5.18M
    fixp->wspeedr = NAN;
566
5.18M
    fixp->wspeedt = NAN;
567
5.18M
    fixp->wtemp = NAN;
568
5.18M
    gps_clear_base(&fixp->base);
569
5.18M
}
570
571
// stuff an attitude structure with recognizable out-of-band values
572
void gps_clear_att(struct attitude_t *attp)
573
78.0k
{
574
78.0k
    memset(attp, 0, sizeof(struct attitude_t));
575
78.0k
    attp->acc_len = NAN;
576
78.0k
    attp->acc_x = NAN;
577
78.0k
    attp->acc_y = NAN;
578
78.0k
    attp->acc_z = NAN;
579
78.0k
    attp->depth = NAN;
580
78.0k
    attp->dip = NAN;
581
78.0k
    attp->gyro_temp = NAN;
582
78.0k
    attp->gyro_x = NAN;
583
78.0k
    attp->gyro_y = NAN;
584
78.0k
    attp->gyro_z = NAN;
585
78.0k
    attp->heading = NAN;
586
78.0k
    attp->mheading = NAN;
587
78.0k
    attp->mag_len = NAN;
588
78.0k
    attp->mag_x = NAN;
589
78.0k
    attp->mag_y = NAN;
590
78.0k
    attp->mag_z = NAN;
591
78.0k
    attp->pitch = NAN;
592
78.0k
    attp->roll = NAN;
593
78.0k
    attp->rot = NAN;
594
78.0k
    attp->temp = NAN;
595
78.0k
    attp->yaw = NAN;
596
78.0k
    gps_clear_base(&attp->base);
597
78.0k
}
598
599
// Clear a dop_t structure
600
void gps_clear_dop( struct dop_t *dop)
601
70.4k
{
602
70.4k
    dop->xdop = dop->ydop = dop->vdop = dop->tdop = dop->hdop = dop->pdop =
603
70.4k
        dop->gdop = NAN;
604
70.4k
}
605
606
// Clear a gst structure
607
void gps_clear_gst( struct gst_t *gst)
608
51.1k
{
609
51.1k
    memset(&gst->utctime, 0, sizeof(gst->utctime));
610
51.1k
    gst-> rms_deviation =  NAN;
611
51.1k
    gst-> smajor_deviation =  NAN;
612
51.1k
    gst-> sminor_deviation =  NAN;
613
51.1k
    gst-> smajor_orientation =  NAN;
614
51.1k
    gst-> lat_err_deviation =  NAN;
615
51.1k
    gst-> lon_err_deviation =  NAN;
616
51.1k
    gst-> alt_err_deviation =  NAN;
617
51.1k
    gst-> ve_err_deviation =  NAN;
618
51.1k
    gst-> vn_err_deviation =  NAN;
619
51.1k
    gst-> vu_err_deviation =  NAN;
620
51.1k
}
621
622
// stuff a log structure with recognizable out-of-band values
623
void gps_clear_log(struct gps_log_t *logp)
624
294
{
625
294
    memset(logp, 0, sizeof(struct gps_log_t));
626
294
    logp->lon = NAN;
627
294
    logp->lat = NAN;
628
294
    logp->altHAE = NAN;
629
294
    logp->altMSL = NAN;
630
294
    logp->gSpeed = NAN;
631
294
    logp->heading = NAN;
632
294
    logp->tAcc = NAN;
633
294
    logp->hAcc = NAN;
634
294
    logp->vAcc = NAN;
635
294
    logp->sAcc = NAN;
636
294
    logp->headAcc = NAN;
637
294
    logp->velN = NAN;
638
294
    logp->velE = NAN;
639
294
    logp->velD = NAN;
640
294
    logp->pDOP = NAN;
641
294
    logp->distance = NAN;
642
294
    logp->totalDistance = NAN;
643
294
    logp->distanceStd = NAN;
644
294
    logp->fixType = -1;
645
294
}
646
647
/* merge new data (from) into current fix (to)
648
 * Being careful not to lose information */
649
void gps_merge_fix(struct gps_fix_t *to,
650
                   gps_mask_t transfer,
651
                   struct gps_fix_t *from)
652
384k
{
653
384k
    if ((NULL == to) ||
654
384k
        (NULL == from)) {
655
0
        return;
656
0
    }
657
384k
    if (0 != (transfer & TIME_SET)) {
658
28.3k
        to->time = from->time;
659
28.3k
    }
660
384k
    if (0 != (transfer & LATLON_SET)) {
661
15.2k
        to->latitude = from->latitude;
662
15.2k
        to->longitude = from->longitude;
663
15.2k
    }
664
384k
    if (0 != (transfer & MODE_SET)) {
665
        // FIXME?  Maybe only upgrade mode, not downgrade it
666
37.7k
        to->mode = from->mode;
667
37.7k
    }
668
    /* Some messages only report mode, some mode and status, some only status.
669
     * Only upgrade status, not downgrade it */
670
384k
    if (0 != (transfer & STATUS_SET)) {
671
35.5k
        if (to->status < from->status) {
672
9.11k
            to->status = from->status;
673
9.11k
        }
674
35.5k
    }
675
384k
    if ((transfer & ALTITUDE_SET) != 0) {
676
13.2k
        if (0 != isfinite(from->altHAE)) {
677
8.64k
            to->altHAE = from->altHAE;
678
8.64k
        }
679
13.2k
        if (0 != isfinite(from->altMSL)) {
680
3.44k
            to->altMSL = from->altMSL;
681
3.44k
        }
682
13.2k
        if (0 != isfinite(from->depth)) {
683
415
            to->depth = from->depth;
684
415
        }
685
13.2k
    }
686
384k
    if (0 != (transfer & TRACK_SET)) {
687
8.40k
        to->track = from->track;
688
8.40k
    }
689
384k
    if (0 != (transfer & MAGNETIC_TRACK_SET)) {
690
3.00k
        if (0 != isfinite(from->magnetic_track)) {
691
490
            to->magnetic_track = from->magnetic_track;
692
490
        }
693
3.00k
        if (0 != isfinite(from->magnetic_var)) {
694
448
            to->magnetic_var = from->magnetic_var;
695
448
        }
696
3.00k
    }
697
384k
    if (0 != (transfer & SPEED_SET)) {
698
8.47k
        to->speed = from->speed;
699
8.47k
    }
700
384k
    if (0 != (transfer & CLIMB_SET)) {
701
3.82k
        to->climb = from->climb;
702
3.82k
    }
703
384k
    if (0 != (transfer & TIMERR_SET)) {
704
1.64k
        to->ept = from->ept;
705
1.64k
    }
706
384k
    if (0 != isfinite(from->epx) &&
707
722
        0 != isfinite(from->epy)) {
708
569
        to->epx = from->epx;
709
569
        to->epy = from->epy;
710
569
    }
711
384k
    if (0 != isfinite(from->epd)) {
712
273
        to->epd = from->epd;
713
273
    }
714
384k
    if (0 != isfinite(from->eph)) {
715
2.40k
        to->eph = from->eph;
716
2.40k
    }
717
384k
    if (0 != isfinite(from->eps)) {
718
3.49k
        to->eps = from->eps;
719
3.49k
    }
720
    // spherical error probability, not geoid separation
721
384k
    if (0 != isfinite(from->sep)) {
722
866
        to->sep = from->sep;
723
866
    }
724
    // geoid separation, not spherical error probability
725
384k
    if (0 != isfinite(from->geoid_sep)) {
726
1.32k
        to->geoid_sep = from->geoid_sep;
727
1.32k
    }
728
384k
    if (0 != isfinite(from->epv)) {
729
2.76k
        to->epv = from->epv;
730
2.76k
    }
731
384k
    if (0 != (transfer & SPEEDERR_SET)) {
732
3.96k
        to->eps = from->eps;
733
3.96k
    }
734
384k
    if (0 != (transfer & ECEF_SET)) {
735
6.71k
        to->ecef.x = from->ecef.x;
736
6.71k
        to->ecef.y = from->ecef.y;
737
6.71k
        to->ecef.z = from->ecef.z;
738
6.71k
        to->ecef.pAcc = from->ecef.pAcc;
739
6.71k
    }
740
384k
    if (0 != (transfer & VECEF_SET)) {
741
4.68k
        to->ecef.vx = from->ecef.vx;
742
4.68k
        to->ecef.vy = from->ecef.vy;
743
4.68k
        to->ecef.vz = from->ecef.vz;
744
4.68k
        to->ecef.vAcc = from->ecef.vAcc;
745
4.68k
    }
746
384k
    if (0 != isfinite(from->errEllipseOrient) &&
747
477
        0 != isfinite(from->errEllipseMajor) &&
748
477
        0 != isfinite(from->errEllipseMinor)) {
749
477
        to->errEllipseOrient = from->errEllipseOrient;
750
477
        to->errEllipseMajor = from->errEllipseMajor;
751
477
        to->errEllipseMinor = from->errEllipseMinor;
752
477
    }
753
384k
    if (0 != (transfer & NED_SET)) {
754
1.55k
        to->NED.relPosN = from->NED.relPosN;
755
1.55k
        to->NED.relPosE = from->NED.relPosE;
756
1.55k
        to->NED.relPosD = from->NED.relPosD;
757
1.55k
        if ((0 != isfinite(from->NED.relPosH)) &&
758
1.55k
            (0 != isfinite(from->NED.relPosL))) {
759
1.55k
            to->NED.relPosH = from->NED.relPosH;
760
1.55k
            to->NED.relPosL = from->NED.relPosL;
761
1.55k
        }
762
1.55k
    }
763
384k
    if (0 != (transfer & VNED_SET)) {
764
6.76k
        to->NED.velN = from->NED.velN;
765
6.76k
        to->NED.velE = from->NED.velE;
766
6.76k
        to->NED.velD = from->NED.velD;
767
6.76k
    }
768
384k
    if ('\0' != from->datum[0]) {
769
939
        strlcpy(to->datum, from->datum, sizeof(to->datum));
770
939
    }
771
384k
    if (0 != isfinite(from->dgps_age) &&
772
1.09k
        0 <= from->dgps_station) {
773
        // both, or neither
774
576
        to->dgps_age = from->dgps_age;
775
576
        to->dgps_station = from->dgps_station;
776
576
    }
777
778
384k
    if (STATUS_UNK != from->base.status) {
779
410
        to->base.status = from->base.status;
780
410
    }
781
782
384k
    if (0 != isfinite(from->base.ratio)) {
783
206
        to->base.ratio = from->base.ratio;
784
206
    }
785
786
384k
    if (0 != isfinite(from->base.east) &&
787
274
        0 != isfinite(from->base.north) &&
788
198
        0 != isfinite(from->base.up)) {
789
131
        to->base.east = from->base.east;
790
131
        to->base.north = from->base.north;
791
131
        to->base.up = from->base.up;
792
131
    }
793
794
384k
    if (0 != isfinite(from->base.length) &&
795
227
        0 != isfinite(from->base.course)) {
796
126
        to->base.length = from->base.length;
797
126
        to->base.course = from->base.course;
798
126
    }
799
800
384k
    if (ANT_UNK != from->ant_stat) {
801
4.74k
        to->ant_stat = from->ant_stat;
802
4.74k
    }
803
384k
    if (0 < from->jam) {
804
226
        to->jam = from->jam;
805
226
    }
806
384k
    if (ANT_PWR_UNK != from->ant_power) {
807
88
        to->ant_power = from->ant_power;
808
88
    }
809
    // navdata stuff.  just wind angle and angle for now
810
384k
    if (0 != (transfer & NAVDATA_SET)) {
811
1.60k
        if (0 != isfinite(from->wanglem)) {
812
77
            to->wanglem = from->wanglem;
813
77
        }
814
1.60k
        if (0 != isfinite(from->wangler)) {
815
68
            to->wangler = from->wangler;
816
68
        }
817
1.60k
        if (0 != isfinite(from->wanglet)) {
818
107
            to->wanglet = from->wanglet;
819
107
        }
820
1.60k
        if (0 != isfinite(from->wspeedr)) {
821
67
            to->wspeedr = from->wspeedr;
822
67
        }
823
1.60k
        if (0 != isfinite(from->wspeedt)) {
824
80
            to->wspeedt = from->wspeedt;
825
80
        }
826
1.60k
    }
827
384k
    if (0 != isfinite(from->temp)) {
828
1.70k
        to->temp = from->temp;
829
1.70k
    }
830
384k
    if (0 != isfinite(from->wtemp)) {
831
71
        to->wtemp = from->wtemp;
832
71
    }
833
384k
}
834
835
/* mkgmtime(tm)
836
 * convert struct tm, as UTC, to seconds since Unix epoch
837
 * This differs from mktime() from libc.
838
 * mktime() takes struct tm as localtime.
839
 *
840
 * The inverse of gmtime(time_t)
841
 *
842
 * Return: -1 on error, set errno to EOVERFLOW
843
 */
844
time_t mkgmtime(struct tm * t)
845
17.2k
{
846
17.2k
    int year;
847
17.2k
    time_t result;
848
17.2k
    static const int cumdays[MONTHSPERYEAR] =
849
17.2k
        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
850
851
    // check ranges, ignore tm_isdst and max tm_year
852
17.2k
    if (0 > t->tm_sec ||
853
17.1k
        0 > t->tm_min ||
854
17.1k
        0 > t->tm_hour ||
855
17.1k
        1 > t->tm_mday ||
856
15.3k
        0 > t->tm_mon ||
857
15.1k
        0 > t->tm_year ||
858
12.9k
        0 > t->tm_wday ||
859
12.9k
        0 > t->tm_yday ||
860
12.9k
        61 < t->tm_sec ||
861
10.9k
        59 < t->tm_min ||
862
9.37k
        23 < t->tm_hour ||
863
7.67k
        31 < t->tm_mday ||
864
7.45k
        11 < t->tm_mon ||
865
7.30k
        6 < t->tm_wday ||
866
9.97k
        365 < t->tm_yday) {
867
9.97k
        errno = EOVERFLOW;
868
9.97k
        return -1;
869
9.97k
    }
870
17.2k
    errno = 0;
871
7.30k
    year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR;
872
7.30k
    result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR];
873
7.30k
    result += (year - 1968) / 4;
874
7.30k
    result -= (year - 1900) / 100;
875
7.30k
    result += (year - 1600) / 400;
876
7.30k
    if (0 == (year % 4) &&
877
2.66k
        (0 != (year % 100) ||
878
1.60k
         0 == (year % 400)) &&
879
1.78k
        (2 > (t->tm_mon % MONTHSPERYEAR))) {
880
875
        result--;
881
875
    }
882
7.30k
    result += t->tm_mday - 1;
883
7.30k
    result *= 24;
884
7.30k
    result += t->tm_hour;
885
7.30k
    result *= 60;
886
7.30k
    result += t->tm_min;
887
7.30k
    result *= 60;
888
7.30k
    result += t->tm_sec;
889
    /* this is UTC, no DST
890
     * if (t->tm_isdst == 1)
891
     * result -= 3600;
892
     */
893
7.30k
    return result;
894
17.2k
}
895
896
// ISO8601 UTC to Unix timespec, no leapsecond correction.
897
timespec_t iso8601_to_timespec(const char *isotime)
898
3.80k
{
899
3.80k
    timespec_t ret = {0};
900
901
#ifdef USE_QT
902
  #ifndef __clang_analyzer__
903
    double usec = 0;
904
905
    QString t(isotime);
906
    QDateTime d = QDateTime::fromString(isotime, Qt::ISODate);
907
    QStringList sl = t.split(".");
908
    if (1 < sl.size()) {
909
        usec = sl[1].toInt() / pow(10., (double)sl[1].size());
910
    }
911
// toSecsSinceEpoch wasn't introduced until 5.8
912
#if QT_VERSION < QT_VERSION_CHECK(5,8,0)
913
    ret.tv_sec = d.toTime_t();
914
#else
915
    ret.tv_sec = d.toSecsSinceEpoch();
916
#endif
917
    ret.tv_nsec = usec * 1e9;
918
  #endif  // __clang_analyzer__
919
#else  // USE_QT
920
3.80k
    double usec = 0;
921
3.80k
    struct tm tm = {0};
922
923
3.80k
    {
924
3.80k
        char *dp = NULL;
925
3.80k
        dp = strptime(isotime, "%Y-%m-%dT%H:%M:%S", &tm);
926
3.80k
        if (NULL != dp &&
927
1.23k
            '.' == *dp) {
928
388
            usec = strtod(dp, NULL);
929
388
        }
930
3.80k
    }
931
932
    /*
933
     * It would be nice if we could say mktime(&tm) - timezone + usec instead,
934
     * but timezone is not available at all on some BSDs. Besides, when working
935
     * with historical dates the value of timezone after an ordinary tzset(3)
936
     * can be wrong; you have to do a redirect through the IANA historical
937
     * timezone database to get it right.
938
     */
939
3.80k
    ret.tv_sec = mkgmtime(&tm);
940
3.80k
    ret.tv_nsec = usec * 1e9;
941
3.80k
#endif  // USE_QT
942
943
3.80k
#if 4 < SIZEOF_TIME_T
944
3.80k
    if (253402300799LL < ret.tv_sec) {
945
        // enforce max "9999-12-31T23:59:59.999Z"
946
392
        ret.tv_sec = 253402300799LL;
947
392
    }
948
3.80k
#endif
949
3.80k
    return ret;
950
3.80k
}
951
952
/* Convert POSIX timespec to ISO8601 UTC, put result in isotime.
953
 * no timezone adjustment
954
 * Return: pointer to isotime.
955
 * example: 2007-12-11T23:38:51.033Z */
956
char *timespec_to_iso8601(timespec_t fixtime, char isotime[], size_t len)
957
0
{
958
0
    struct tm when;
959
0
    char timestr[30];
960
0
    long fracsec;
961
962
0
    if (0 > fixtime.tv_sec) {
963
        // Allow 0 for testing of 1970-01-01T00:00:00.000Z
964
0
        strlcpy(isotime, "NaN", len);
965
0
        return isotime;
966
0
    }
967
0
    if (999499999 < fixtime.tv_nsec) {
968
        // round up
969
0
        fixtime.tv_sec++;
970
0
        fixtime.tv_nsec = 0;
971
0
    }
972
973
0
#if 4 < SIZEOF_TIME_T
974
0
    if (253402300799LL < fixtime.tv_sec) {
975
        // enforce max "9999-12-31T23:59:59.999Z"
976
0
        fixtime.tv_sec = 253402300799LL;
977
0
    }
978
0
#endif
979
980
0
    (void)gmtime_r(&fixtime.tv_sec, &when);
981
982
    /*
983
     * Do not mess casually with the number of decimal digits in the
984
     * format!  Most GPSes report over serial links at 0.01s or 0.001s
985
     * precision.  Round to 0.001s
986
     */
987
0
    fracsec = (fixtime.tv_nsec + 500000) / 1000000;
988
989
0
    (void)strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &when);
990
0
    (void)snprintf(isotime, len, "%s.%03ldZ",timestr, fracsec);
991
992
0
    return isotime;
993
0
}
994
995
/* return time now as ISO8601, no timezone adjustment
996
 * example: 2007-12-11T23:38:51.033Z */
997
char *now_to_iso8601(char *tbuf, size_t tbuf_sz)
998
0
{
999
0
    timespec_t ts_now;
1000
1001
0
    (void)clock_gettime(CLOCK_REALTIME, &ts_now);
1002
0
    return timespec_to_iso8601(ts_now, tbuf, tbuf_sz);
1003
0
}
1004
1005
24.0k
#define Deg2Rad(n)      ((n) * DEG_2_RAD)
1006
1007
/* Distance in meters between two points specified in degrees, optionally
1008
 * with initial and final bearings. */
1009
double earth_distance_and_bearings(double lat1, double lon1,
1010
                                   double lat2, double lon2,
1011
                                   double *ib, double *fb)
1012
8.00k
{
1013
    /*
1014
     * this is a translation of the javascript implementation of the
1015
     * Vincenty distance formula by Chris Veness. See
1016
     * http://www.movable-type.co.uk/scripts/latlong-vincenty.html
1017
     */
1018
8.00k
    double a, b, f;             // WGS-84 ellipsoid params
1019
8.00k
    double L, L_P, U1, U2, s_U1, c_U1, s_U2, c_U2;
1020
8.00k
    double uSq, A, B, d_S, lambda;
1021
    // cppcheck-suppress variableScope
1022
8.00k
    double s_L, c_L, s_A, C;
1023
8.00k
    double c_S, S, s_S, c_SqA, c_2SM;
1024
8.00k
    int i = 100;
1025
1026
8.00k
    a = WGS84A;
1027
8.00k
    b = WGS84B;
1028
8.00k
    f = 1 / WGS84F;
1029
8.00k
    L = Deg2Rad(lon2 - lon1);
1030
8.00k
    U1 = atan((1 - f) * tan(Deg2Rad(lat1)));
1031
8.00k
    U2 = atan((1 - f) * tan(Deg2Rad(lat2)));
1032
8.00k
    s_U1 = sin(U1);
1033
8.00k
    c_U1 = cos(U1);
1034
8.00k
    s_U2 = sin(U2);
1035
8.00k
    c_U2 = cos(U2);
1036
8.00k
    lambda = L;
1037
1038
61.6k
    do {
1039
61.6k
        s_L = sin(lambda);
1040
61.6k
        c_L = cos(lambda);
1041
61.6k
        s_S = sqrt((c_U2 * s_L) * (c_U2 * s_L) +
1042
61.6k
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L) *
1043
61.6k
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L));
1044
1045
61.6k
        if (0 == s_S) {
1046
1.11k
            return 0;
1047
1.11k
        }
1048
1049
60.5k
        c_S = s_U1 * s_U2 + c_U1 * c_U2 * c_L;
1050
60.5k
        S = atan2(s_S, c_S);
1051
60.5k
        s_A = c_U1 * c_U2 * s_L / s_S;
1052
60.5k
        c_SqA = 1 - s_A * s_A;
1053
60.5k
        c_2SM = c_S - 2 * s_U1 * s_U2 / c_SqA;
1054
1055
60.5k
        if (0 == isfinite(c_2SM)) {
1056
51.0k
            c_2SM = 0;
1057
51.0k
        }
1058
1059
60.5k
        C = f / 16 * c_SqA * (4 + f * (4 - 3 * c_SqA));
1060
60.5k
        L_P = lambda;
1061
60.5k
        lambda = L + (1 - C) * f * s_A *
1062
60.5k
            (S + C * s_S * (c_2SM + C * c_S * (2 * c_2SM * c_2SM - 1)));
1063
60.5k
    } while ((fabs(lambda - L_P) > 1.0e-12) &&
1064
54.0k
             (0 < --i));
1065
1066
6.88k
    if (0 == i) {
1067
478
        return NAN;             // formula failed to converge
1068
478
    }
1069
1070
6.40k
    uSq = c_SqA * ((a * a) - (b * b)) / (b * b);
1071
6.40k
    A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
1072
6.40k
    B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
1073
6.40k
    d_S = B * s_S * (c_2SM + B / 4 *
1074
6.40k
                     (c_S * (-1 + 2 * c_2SM * c_2SM) - B / 6 * c_2SM *
1075
6.40k
                      (-3 + 4 * s_S * s_S) * (-3 + 4 * c_2SM * c_2SM)));
1076
1077
6.40k
    if (NULL != ib) {
1078
0
        *ib = atan2(c_U2 * sin(lambda),
1079
0
                    c_U1 * s_U2 - s_U1 * c_U2 * cos(lambda));
1080
0
    }
1081
6.40k
    if (NULL != fb) {
1082
0
        *fb = atan2(c_U1 * sin(lambda),
1083
0
                    c_U1 * s_U2 * cos(lambda) - s_U1 * c_U2);
1084
0
    }
1085
1086
6.40k
    return (WGS84B * A * (S - d_S));
1087
6.88k
}
1088
1089
// Distance in meters between two points specified in degrees.
1090
double earth_distance(double lat1, double lon1, double lat2, double lon2)
1091
8.00k
{
1092
8.00k
    return earth_distance_and_bearings(lat1, lon1, lat2, lon2, NULL, NULL);
1093
8.00k
}
1094
1095
/* Wait for data until timeout, ignoring signals.
1096
 *
1097
 * pselect() may set errno on error
1098
 */
1099
bool nanowait(int fd, struct timespec *to)
1100
0
{
1101
0
    fd_set fdset;
1102
1103
0
    FD_ZERO(&fdset);
1104
0
    FD_SET(fd, &fdset);
1105
0
    TS_NORM(to);         // just in case
1106
0
    errno = 0;
1107
    // sigmask is NULL, so equivalent to select()
1108
0
    return pselect(fd + 1, &fdset, NULL, NULL, to, NULL) == 1;
1109
0
}
1110
1111
/* Accept a datum code, return matching string
1112
 *
1113
 * There are a ton of these, only a few are here
1114
 *
1115
 */
1116
void datum_code_string(int code, char *buffer, size_t len)
1117
136
{
1118
136
    const char *datum_str;
1119
1120
136
    switch (code) {
1121
7
    case 0:
1122
7
        datum_str = "WGS84";
1123
7
        break;
1124
1
    case 21:
1125
1
        datum_str = "WGS84";
1126
1
        break;
1127
1
    case 178:
1128
1
        datum_str = "Tokyo Mean";
1129
1
        break;
1130
1
    case 179:
1131
1
        datum_str = "Tokyo-Japan";
1132
1
        break;
1133
1
    case 180:
1134
1
        datum_str = "Tokyo-Korea";
1135
1
        break;
1136
2
    case 181:
1137
2
        datum_str = "Tokyo-Okinawa";
1138
2
        break;
1139
1
    case 182:
1140
1
        datum_str = "PZ90.11";
1141
1
        break;
1142
1
    case 999:
1143
1
        datum_str = "User Defined";
1144
1
        break;
1145
121
    default:
1146
121
        datum_str = NULL;
1147
121
        break;
1148
136
    }
1149
1150
136
    if (NULL == datum_str) {
1151
        // Fake it
1152
121
        snprintf(buffer, len, "%d", code);
1153
121
    } else {
1154
15
        strlcpy(buffer, datum_str, len);
1155
15
    }
1156
136
}
1157
1158
/* make up an NMEA 4.0 (extended) PRN based on gnssId:svId,
1159
 * This does NOT match NMEA 4.10 and 4.11 where all PRN are 1-99,
1160
 * except IMES, QZSS, and some SBAS.
1161
 *
1162
 * Ref Appendix A from u-blox ZED-F9P Interface Description
1163
 * and
1164
 * Section 1.5.3  M10-FW500_InterfaceDescription_UBX-20053845.pdf
1165
 *
1166
 * Using ST Teseo PRN fors for those not defined by UBX.
1167
 * um3407-teseo-vi-and-teseo-app2nmea-specifications-and-commands-stmicroelectronics.pdf
1168
 * Section 3.5
1169
 * But we do not use the per sigId PRNs from ST.
1170
 *
1171
 * Return PRN, less than one for error
1172
 *        -1 for GLONASS svid 255
1173
 */
1174
short ubx2_to_prn(int gnssId, int svId)
1175
19.0k
{
1176
19.0k
    short nmea_PRN = 0;
1177
1178
19.0k
    if (1 > svId) {
1179
        // skip 0 svId
1180
1.04k
        return 0;
1181
1.04k
    }
1182
1183
17.9k
    switch (gnssId) {
1184
1.97k
    case 0:
1185
        // GPS, 1-32 maps to 1-32
1186
1.97k
        if (32 >= svId) {
1187
1.64k
            nmea_PRN = svId;
1188
1.64k
        }
1189
1.97k
        break;
1190
2.26k
    case 1:
1191
        // SBAS, 120..151, 152..158 maps to 33..64, 152..158
1192
2.26k
        if (120 <= svId &&
1193
1.64k
            151 >= svId) {
1194
1.02k
            nmea_PRN = svId - 87;
1195
1.24k
        } else if (158 >= svId) {
1196
1.12k
            nmea_PRN = svId;
1197
1.12k
        }
1198
2.26k
        break;
1199
1.39k
    case 2:
1200
        // Galileo, ubx gnssid:svid 1..36 ->  301-336
1201
        // Galileo, ubx PRN         211..246 ->  301-336
1202
1.39k
        if (36 >= svId) {
1203
1.00k
            nmea_PRN = svId + 300;
1204
1.00k
        } else if (211 > svId) {
1205
            // skip bad svId
1206
164
            return 0;
1207
222
        } else if (246 >= svId) {
1208
127
            nmea_PRN = svId + 90;
1209
127
        }
1210
1.22k
        break;
1211
3.10k
    case 3:
1212
        /* BeiDou, ubx gnssid:svid    1..37 -> to 401-437
1213
         * have seen 1-63 on F10 ProtVer 40.0, March 2025
1214
         *   ubx gnssid:svid    1..63 -> to 401-463
1215
         * BeiDou, ubx "single svid"  159..163,33..64 -> to 401-437 ?? */
1216
3.10k
        if (63 >= svId) {
1217
2.76k
            nmea_PRN = svId + 400;
1218
2.76k
        } else if (159 > svId) {
1219
            // skip bad svId
1220
120
            return 0;
1221
225
        } else if (163 >= svId) {
1222
120
            nmea_PRN = svId + 242;
1223
120
        }
1224
2.98k
        break;
1225
2.98k
    case 4:
1226
        // IMES, ubx gnssid:svid 1-10 -> to 173-182
1227
        // IMES, ubx PRN         173-182 to 173-182
1228
1.15k
        if (10 >= svId) {
1229
946
            nmea_PRN = svId + 172;
1230
946
        } else if (173 > svId) {
1231
            // skip bad svId
1232
89
            return 0;
1233
124
        } else if (182 >= svId) {
1234
11
            nmea_PRN = svId;
1235
11
        }
1236
1.07k
        break;
1237
1.07k
    case 5:
1238
        // QZSS, ubx gnssid:svid 1-10 to 193-202
1239
        // QZSS, ubx PRN         193-202 to 193-202
1240
1.07k
        if (10 >= svId) {
1241
701
            nmea_PRN = svId + 192;
1242
701
        } else if (193 > svId) {
1243
            // skip bad svId
1244
129
            return 0;
1245
247
        } else if (202 >= svId) {
1246
95
            nmea_PRN = svId;
1247
95
        }
1248
948
        break;
1249
2.58k
    case 6:
1250
        // GLONASS, 1-32 maps to 65-96
1251
2.58k
        if (32 >= svId) {
1252
1.53k
            nmea_PRN = svId + 64;
1253
1.53k
        } else if (65 > svId) {
1254
            // skip bad svId
1255
110
            return 0;
1256
936
        } else if (96 >= svId) {
1257
562
            nmea_PRN = svId;
1258
562
        } else if (255 == svId) {
1259
            // skip bad svId, 255 == tracked, but unidentified, skip
1260
153
            nmea_PRN = -1;
1261
153
        }
1262
2.47k
        break;
1263
2.47k
    case 7:
1264
        // NavIC (IRNSS), 1 - 14 -> 801 - 814
1265
460
        if (14 >= svId) {
1266
329
            nmea_PRN = svId + 800;;
1267
329
        }
1268
460
        break;
1269
3.96k
    default:
1270
        // Huh?
1271
3.96k
        nmea_PRN = 0;
1272
17.9k
    }
1273
1274
17.3k
    return nmea_PRN;
1275
17.9k
}
1276
1277
// vim: set expandtab shiftwidth=4