Coverage Report

Created: 2026-06-10 06:25

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
338
{
87
1.37k
    while (NULL != clist->str) {
88
1.15k
        if (clist->ch == ch) {
89
114
            return clist->str;
90
114
        }
91
1.04k
        clist++;
92
1.04k
    }
93
224
    return "Unk";
94
338
}
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
16.2k
#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
18.0k
{
236
18.0k
    const char *rets = "Unk";
237
238
18.0k
    if (GNSSID_CNT <= gnssid) {
239
1.81k
        rets = "GNSS-Unk";
240
16.2k
    } else if (SIGID_NUM <= sigid) {
241
401
        rets = "SIG-Unk";
242
15.8k
    } else {
243
15.8k
        rets = sig_xlate[gnssid][sigid].obs;
244
15.8k
        if (NULL == rets) {
245
272
            rets = "Unk";
246
272
        }
247
15.8k
    }
248
249
18.0k
    return rets;
250
18.0k
}
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
63.9k
{
295
63.9k
    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
63.9k
    static double powersOf10[] = {
303
63.9k
        10.,
304
63.9k
        100.,
305
63.9k
        1.0e4,
306
63.9k
        1.0e8,
307
63.9k
        1.0e16,
308
63.9k
        1.0e32,
309
63.9k
        1.0e64,
310
63.9k
        1.0e128,
311
63.9k
        1.0e256
312
63.9k
    };
313
314
63.9k
    bool sign = false, expSign = false;
315
63.9k
    double fraction, dblExp, *d;
316
63.9k
    const char *p;
317
63.9k
    unsigned char c;
318
63.9k
    int exp = 0;                // Exponent read from "EX" field.
319
63.9k
    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
63.9k
    int mantSize;               // Number of digits in mantissa.
329
63.9k
    int decPt;                  /* Number of mantissa digits BEFORE decimal
330
                                 * point. */
331
63.9k
    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
63.9k
    p = string;
341
63.9k
    while (isspace((int)*p)) {
342
350
        p += 1;
343
350
    }
344
63.9k
    if (isdigit((unsigned char)*p)) {
345
        // ignore
346
38.8k
    } else if ('-' == *p) {
347
890
        sign = true;
348
890
        p += 1;
349
37.9k
    } else if ('+' == *p) {
350
336
        p += 1;
351
37.6k
    } else if ('.' == *p) {
352
        // ignore
353
29.6k
    } else {
354
29.6k
        return NAN;
355
29.6k
    }
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
34.2k
    decPt = -1;
363
144k
    for (mantSize = 0; ; mantSize += 1) {
364
144k
        c = *p;
365
144k
        if (!isdigit(c)) {
366
50.1k
            if ((c != '.') || (decPt >= 0)) {
367
34.2k
                break;
368
34.2k
            }
369
15.8k
            decPt = mantSize;
370
15.8k
        }
371
110k
        p += 1;
372
110k
    }
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
34.2k
    pExp  = p;
382
34.2k
    p -= mantSize;
383
34.2k
    if (decPt < 0) {
384
18.4k
        decPt = mantSize;
385
18.4k
    } else {
386
15.8k
        mantSize -= 1;                  // One of the digits was the point.
387
15.8k
    }
388
34.2k
    if (mantSize > 18) {
389
384
        fracExp = decPt - 18;
390
384
        mantSize = 18;
391
33.8k
    } else {
392
33.8k
        fracExp = decPt - mantSize;
393
33.8k
    }
394
34.2k
    if (mantSize == 0) {
395
2.09k
        fraction = 0.0;
396
        // p = string;
397
2.09k
        goto done;
398
32.1k
    } else {
399
32.1k
        int frac1, frac2;
400
401
32.1k
        frac1 = 0;
402
39.1k
        for ( ; mantSize > 9; mantSize -= 1) {
403
6.94k
            c = *p;
404
6.94k
            p += 1;
405
6.94k
            if ('.' == c) {
406
325
                c = *p;
407
325
                p += 1;
408
325
            }
409
6.94k
            frac1 = 10*frac1 + (c - '0');
410
6.94k
        }
411
32.1k
        frac2 = 0;
412
118k
        for (; mantSize > 0; mantSize -= 1) {
413
85.9k
            c = *p;
414
85.9k
            p += 1;
415
85.9k
            if ('.' == c) {
416
13.3k
                c = *p;
417
13.3k
                p += 1;
418
13.3k
            }
419
85.9k
            frac2 = 10*frac2 + (c - '0');
420
85.9k
        }
421
32.1k
        fraction = (1.0e9 * frac1) + frac2;
422
32.1k
    }
423
424
    /*
425
     * Skim off the exponent.
426
     */
427
428
32.1k
    p = pExp;
429
32.1k
    if (('E' == *p) ||
430
31.6k
        ('e' == *p)) {
431
1.21k
        p += 1;
432
1.21k
        if ('-' == *p) {
433
176
            expSign = true;
434
176
            p += 1;
435
1.04k
        } else {
436
1.04k
            if ('+' == *p) {
437
71
                p += 1;
438
71
            }
439
1.04k
            expSign = false;
440
1.04k
        }
441
2.39k
        while (isdigit((unsigned char) *p)) {
442
2.39k
            exp = exp * 10 + (*p - '0');
443
2.39k
            if (1024 < exp) {
444
253
                if (true == expSign) {
445
                    // exponent underflow!
446
108
                    if (sign) {
447
32
                        return -0.0;
448
32
                    }
449
76
                    return 0.0;
450
108
                } // else  exponent overflow!
451
145
                if (sign) {
452
70
                    return -INFINITY;
453
70
                }
454
75
                return INFINITY;
455
145
            }
456
2.13k
            p += 1;
457
2.13k
        }
458
1.21k
    }
459
31.9k
    if (expSign) {
460
68
        exp = fracExp - exp;
461
31.8k
    } else {
462
31.8k
        exp = fracExp + exp;
463
31.8k
    }
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
31.9k
    if (0 > exp) {
473
13.5k
        expSign = true;
474
13.5k
        exp = -exp;
475
18.3k
    } else {
476
18.3k
        expSign = false;
477
18.3k
    }
478
31.9k
    if (exp > maxExponent) {
479
196
        exp = maxExponent;
480
196
        errno = ERANGE;
481
196
    }
482
31.9k
    dblExp = 1.0;
483
57.8k
    for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
484
25.9k
        if (exp & 01) {
485
18.4k
            dblExp *= *d;
486
18.4k
        }
487
25.9k
    }
488
31.9k
    if (expSign) {
489
13.5k
        fraction /= dblExp;
490
18.3k
    } else {
491
18.3k
        fraction *= dblExp;
492
18.3k
    }
493
494
34.0k
done:
495
34.0k
    if (sign) {
496
788
        return -fraction;
497
788
    }
498
33.2k
    return fraction;
499
34.0k
}
500
501
7.97k
#define MONTHSPERYEAR   12      // months per calendar year
502
503
// clear a baseline_t
504
static void gps_clear_base(struct baseline_t *base)
505
2.19M
{
506
2.19M
    base->status = STATUS_UNK;
507
2.19M
    base->east = NAN;
508
2.19M
    base->north = NAN;
509
2.19M
    base->up = NAN;
510
2.19M
    base->length = NAN;
511
2.19M
    base->course = NAN;
512
2.19M
    base->ratio = NAN;
513
2.19M
}
514
515
// stuff a fix structure with recognizable out-of-band values
516
void gps_clear_fix(struct gps_fix_t *fixp)
517
2.15M
{
518
2.15M
    memset(fixp, 0, sizeof(struct gps_fix_t));
519
2.15M
    fixp->altitude = NAN;        // DEPRECATED, undefined
520
2.15M
    fixp->altHAE = NAN;
521
2.15M
    fixp->altMSL = NAN;
522
2.15M
    fixp->climb = NAN;
523
2.15M
    fixp->depth = NAN;
524
2.15M
    fixp->epc = NAN;
525
2.15M
    fixp->epd = NAN;
526
2.15M
    fixp->eph = NAN;
527
2.15M
    fixp->eps = NAN;
528
2.15M
    fixp->ept = NAN;
529
2.15M
    fixp->epv = NAN;
530
2.15M
    fixp->epx = NAN;
531
2.15M
    fixp->epy = NAN;
532
2.15M
    fixp->latitude = NAN;
533
2.15M
    fixp->longitude = NAN;
534
2.15M
    fixp->magnetic_track = NAN;
535
2.15M
    fixp->magnetic_var = NAN;
536
2.15M
    fixp->mode = MODE_NOT_SEEN;
537
2.15M
    fixp->sep = NAN;
538
2.15M
    fixp->speed = NAN;
539
2.15M
    fixp->track = NAN;
540
    // clear ECEF too
541
2.15M
    fixp->ecef.x = NAN;
542
2.15M
    fixp->ecef.y = NAN;
543
2.15M
    fixp->ecef.z = NAN;
544
2.15M
    fixp->ecef.vx = NAN;
545
2.15M
    fixp->ecef.vy = NAN;
546
2.15M
    fixp->ecef.vz = NAN;
547
2.15M
    fixp->ecef.pAcc = NAN;
548
2.15M
    fixp->ecef.vAcc = NAN;
549
2.15M
    fixp->errEllipseOrient = NAN;
550
2.15M
    fixp->errEllipseMajor = NAN;
551
2.15M
    fixp->errEllipseMinor = NAN;
552
2.15M
    fixp->NED.relPosN = NAN;
553
2.15M
    fixp->NED.relPosE = NAN;
554
2.15M
    fixp->NED.relPosD = NAN;
555
2.15M
    fixp->NED.velN = NAN;
556
2.15M
    fixp->NED.velE = NAN;
557
2.15M
    fixp->NED.velD = NAN;
558
2.15M
    fixp->geoid_sep = NAN;
559
2.15M
    fixp->dgps_age = NAN;
560
2.15M
    fixp->dgps_station = -1;
561
2.15M
    fixp->temp = NAN;
562
2.15M
    fixp->wanglem = NAN;
563
2.15M
    fixp->wangler = NAN;
564
2.15M
    fixp->wanglet = NAN;
565
2.15M
    fixp->wspeedr = NAN;
566
2.15M
    fixp->wspeedt = NAN;
567
2.15M
    fixp->wtemp = NAN;
568
2.15M
    gps_clear_base(&fixp->base);
569
2.15M
}
570
571
// stuff an attitude structure with recognizable out-of-band values
572
void gps_clear_att(struct attitude_t *attp)
573
37.0k
{
574
37.0k
    memset(attp, 0, sizeof(struct attitude_t));
575
37.0k
    attp->acc_len = NAN;
576
37.0k
    attp->acc_x = NAN;
577
37.0k
    attp->acc_y = NAN;
578
37.0k
    attp->acc_z = NAN;
579
37.0k
    attp->depth = NAN;
580
37.0k
    attp->dip = NAN;
581
37.0k
    attp->gyro_temp = NAN;
582
37.0k
    attp->gyro_x = NAN;
583
37.0k
    attp->gyro_y = NAN;
584
37.0k
    attp->gyro_z = NAN;
585
37.0k
    attp->heading = NAN;
586
37.0k
    attp->mheading = NAN;
587
37.0k
    attp->mag_len = NAN;
588
37.0k
    attp->mag_x = NAN;
589
37.0k
    attp->mag_y = NAN;
590
37.0k
    attp->mag_z = NAN;
591
37.0k
    attp->pitch = NAN;
592
37.0k
    attp->roll = NAN;
593
37.0k
    attp->rot = NAN;
594
37.0k
    attp->temp = NAN;
595
37.0k
    attp->yaw = NAN;
596
37.0k
    gps_clear_base(&attp->base);
597
37.0k
}
598
599
// Clear a dop_t structure
600
void gps_clear_dop( struct dop_t *dop)
601
33.2k
{
602
33.2k
    dop->xdop = dop->ydop = dop->vdop = dop->tdop = dop->hdop = dop->pdop =
603
33.2k
        dop->gdop = NAN;
604
33.2k
}
605
606
// Clear a gst structure
607
void gps_clear_gst( struct gst_t *gst)
608
21.2k
{
609
21.2k
    memset(&gst->utctime, 0, sizeof(gst->utctime));
610
21.2k
    gst-> rms_deviation =  NAN;
611
21.2k
    gst-> smajor_deviation =  NAN;
612
21.2k
    gst-> sminor_deviation =  NAN;
613
21.2k
    gst-> smajor_orientation =  NAN;
614
21.2k
    gst-> lat_err_deviation =  NAN;
615
21.2k
    gst-> lon_err_deviation =  NAN;
616
21.2k
    gst-> alt_err_deviation =  NAN;
617
21.2k
    gst-> ve_err_deviation =  NAN;
618
21.2k
    gst-> vn_err_deviation =  NAN;
619
21.2k
    gst-> vu_err_deviation =  NAN;
620
21.2k
}
621
622
// stuff a log structure with recognizable out-of-band values
623
void gps_clear_log(struct gps_log_t *logp)
624
2
{
625
2
    memset(logp, 0, sizeof(struct gps_log_t));
626
2
    logp->lon = NAN;
627
2
    logp->lat = NAN;
628
2
    logp->altHAE = NAN;
629
2
    logp->altMSL = NAN;
630
2
    logp->gSpeed = NAN;
631
2
    logp->heading = NAN;
632
2
    logp->tAcc = NAN;
633
2
    logp->hAcc = NAN;
634
2
    logp->vAcc = NAN;
635
2
    logp->sAcc = NAN;
636
2
    logp->headAcc = NAN;
637
2
    logp->velN = NAN;
638
2
    logp->velE = NAN;
639
2
    logp->velD = NAN;
640
2
    logp->pDOP = NAN;
641
2
    logp->distance = NAN;
642
2
    logp->totalDistance = NAN;
643
2
    logp->distanceStd = NAN;
644
2
    logp->fixType = -1;
645
2
}
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
257k
{
653
257k
    if ((NULL == to) ||
654
257k
        (NULL == from)) {
655
0
        return;
656
0
    }
657
257k
    if (0 != (transfer & TIME_SET)) {
658
19.1k
        to->time = from->time;
659
19.1k
    }
660
257k
    if (0 != (transfer & LATLON_SET)) {
661
11.5k
        to->latitude = from->latitude;
662
11.5k
        to->longitude = from->longitude;
663
11.5k
    }
664
257k
    if (0 != (transfer & MODE_SET)) {
665
        // FIXME?  Maybe only upgrade mode, not downgrade it
666
22.7k
        to->mode = from->mode;
667
22.7k
    }
668
    /* Some messages only report mode, some mode and status, some only status.
669
     * Only upgrade status, not downgrade it */
670
257k
    if (0 != (transfer & STATUS_SET)) {
671
21.2k
        if (to->status < from->status) {
672
5.38k
            to->status = from->status;
673
5.38k
        }
674
21.2k
    }
675
257k
    if ((transfer & ALTITUDE_SET) != 0) {
676
10.0k
        if (0 != isfinite(from->altHAE)) {
677
6.65k
            to->altHAE = from->altHAE;
678
6.65k
        }
679
10.0k
        if (0 != isfinite(from->altMSL)) {
680
3.36k
            to->altMSL = from->altMSL;
681
3.36k
        }
682
10.0k
        if (0 != isfinite(from->depth)) {
683
129
            to->depth = from->depth;
684
129
        }
685
10.0k
    }
686
257k
    if (0 != (transfer & TRACK_SET)) {
687
4.01k
        to->track = from->track;
688
4.01k
    }
689
257k
    if (0 != (transfer & MAGNETIC_TRACK_SET)) {
690
1.53k
        if (0 != isfinite(from->magnetic_track)) {
691
339
            to->magnetic_track = from->magnetic_track;
692
339
        }
693
1.53k
        if (0 != isfinite(from->magnetic_var)) {
694
358
            to->magnetic_var = from->magnetic_var;
695
358
        }
696
1.53k
    }
697
257k
    if (0 != (transfer & SPEED_SET)) {
698
4.07k
        to->speed = from->speed;
699
4.07k
    }
700
257k
    if (0 != (transfer & CLIMB_SET)) {
701
2.03k
        to->climb = from->climb;
702
2.03k
    }
703
257k
    if (0 != (transfer & TIMERR_SET)) {
704
1.34k
        to->ept = from->ept;
705
1.34k
    }
706
257k
    if (0 != isfinite(from->epx) &&
707
974
        0 != isfinite(from->epy)) {
708
818
        to->epx = from->epx;
709
818
        to->epy = from->epy;
710
818
    }
711
257k
    if (0 != isfinite(from->epd)) {
712
0
        to->epd = from->epd;
713
0
    }
714
257k
    if (0 != isfinite(from->eph)) {
715
1.02k
        to->eph = from->eph;
716
1.02k
    }
717
257k
    if (0 != isfinite(from->eps)) {
718
910
        to->eps = from->eps;
719
910
    }
720
    // spherical error probability, not geoid separation
721
257k
    if (0 != isfinite(from->sep)) {
722
978
        to->sep = from->sep;
723
978
    }
724
    // geoid separation, not spherical error probability
725
257k
    if (0 != isfinite(from->geoid_sep)) {
726
1.08k
        to->geoid_sep = from->geoid_sep;
727
1.08k
    }
728
257k
    if (0 != isfinite(from->epv)) {
729
1.38k
        to->epv = from->epv;
730
1.38k
    }
731
257k
    if (0 != (transfer & SPEEDERR_SET)) {
732
1.50k
        to->eps = from->eps;
733
1.50k
    }
734
257k
    if (0 != (transfer & ECEF_SET)) {
735
3.55k
        to->ecef.x = from->ecef.x;
736
3.55k
        to->ecef.y = from->ecef.y;
737
3.55k
        to->ecef.z = from->ecef.z;
738
3.55k
        to->ecef.pAcc = from->ecef.pAcc;
739
3.55k
    }
740
257k
    if (0 != (transfer & VECEF_SET)) {
741
2.31k
        to->ecef.vx = from->ecef.vx;
742
2.31k
        to->ecef.vy = from->ecef.vy;
743
2.31k
        to->ecef.vz = from->ecef.vz;
744
2.31k
        to->ecef.vAcc = from->ecef.vAcc;
745
2.31k
    }
746
257k
    if (0 != isfinite(from->errEllipseOrient) &&
747
0
        0 != isfinite(from->errEllipseMajor) &&
748
0
        0 != isfinite(from->errEllipseMinor)) {
749
0
        to->errEllipseOrient = from->errEllipseOrient;
750
0
        to->errEllipseMajor = from->errEllipseMajor;
751
0
        to->errEllipseMinor = from->errEllipseMinor;
752
0
    }
753
257k
    if (0 != (transfer & NED_SET)) {
754
838
        to->NED.relPosN = from->NED.relPosN;
755
838
        to->NED.relPosE = from->NED.relPosE;
756
838
        to->NED.relPosD = from->NED.relPosD;
757
838
        if ((0 != isfinite(from->NED.relPosH)) &&
758
838
            (0 != isfinite(from->NED.relPosL))) {
759
838
            to->NED.relPosH = from->NED.relPosH;
760
838
            to->NED.relPosL = from->NED.relPosL;
761
838
        }
762
838
    }
763
257k
    if (0 != (transfer & VNED_SET)) {
764
5.56k
        to->NED.velN = from->NED.velN;
765
5.56k
        to->NED.velE = from->NED.velE;
766
5.56k
        to->NED.velD = from->NED.velD;
767
5.56k
    }
768
257k
    if ('\0' != from->datum[0]) {
769
420
        strlcpy(to->datum, from->datum, sizeof(to->datum));
770
420
    }
771
257k
    if (0 != isfinite(from->dgps_age) &&
772
632
        0 <= from->dgps_station) {
773
        // both, or neither
774
291
        to->dgps_age = from->dgps_age;
775
291
        to->dgps_station = from->dgps_station;
776
291
    }
777
778
257k
    if (STATUS_UNK != from->base.status) {
779
450
        to->base.status = from->base.status;
780
450
    }
781
782
257k
    if (0 != isfinite(from->base.ratio)) {
783
193
        to->base.ratio = from->base.ratio;
784
193
    }
785
786
257k
    if (0 != isfinite(from->base.east) &&
787
229
        0 != isfinite(from->base.north) &&
788
135
        0 != isfinite(from->base.up)) {
789
67
        to->base.east = from->base.east;
790
67
        to->base.north = from->base.north;
791
67
        to->base.up = from->base.up;
792
67
    }
793
794
257k
    if (0 != isfinite(from->base.length) &&
795
142
        0 != isfinite(from->base.course)) {
796
37
        to->base.length = from->base.length;
797
37
        to->base.course = from->base.course;
798
37
    }
799
800
257k
    if (ANT_UNK != from->ant_stat) {
801
3.19k
        to->ant_stat = from->ant_stat;
802
3.19k
    }
803
257k
    if (0 < from->jam) {
804
178
        to->jam = from->jam;
805
178
    }
806
257k
    if (ANT_PWR_UNK != from->ant_power) {
807
0
        to->ant_power = from->ant_power;
808
0
    }
809
    // navdata stuff.  just wind angle and angle for now
810
257k
    if (0 != (transfer & NAVDATA_SET)) {
811
894
        if (0 != isfinite(from->wanglem)) {
812
67
            to->wanglem = from->wanglem;
813
67
        }
814
894
        if (0 != isfinite(from->wangler)) {
815
0
            to->wangler = from->wangler;
816
0
        }
817
894
        if (0 != isfinite(from->wanglet)) {
818
135
            to->wanglet = from->wanglet;
819
135
        }
820
894
        if (0 != isfinite(from->wspeedr)) {
821
0
            to->wspeedr = from->wspeedr;
822
0
        }
823
894
        if (0 != isfinite(from->wspeedt)) {
824
127
            to->wspeedt = from->wspeedt;
825
127
        }
826
894
    }
827
257k
    if (0 != isfinite(from->temp)) {
828
768
        to->temp = from->temp;
829
768
    }
830
257k
    if (0 != isfinite(from->wtemp)) {
831
86
        to->wtemp = from->wtemp;
832
86
    }
833
257k
}
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
9.25k
{
846
9.25k
    int year;
847
9.25k
    time_t result;
848
9.25k
    static const int cumdays[MONTHSPERYEAR] =
849
9.25k
        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
850
851
    // check ranges, ignore tm_isdst and max tm_year
852
9.25k
    if (0 > t->tm_sec ||
853
9.17k
        0 > t->tm_min ||
854
9.17k
        0 > t->tm_hour ||
855
9.17k
        1 > t->tm_mday ||
856
8.43k
        0 > t->tm_mon ||
857
8.35k
        0 > t->tm_year ||
858
7.61k
        0 > t->tm_wday ||
859
7.61k
        0 > t->tm_yday ||
860
7.61k
        61 < t->tm_sec ||
861
6.29k
        59 < t->tm_min ||
862
4.94k
        23 < t->tm_hour ||
863
3.93k
        31 < t->tm_mday ||
864
3.81k
        11 < t->tm_mon ||
865
3.71k
        6 < t->tm_wday ||
866
5.54k
        365 < t->tm_yday) {
867
5.54k
        errno = EOVERFLOW;
868
5.54k
        return -1;
869
5.54k
    }
870
9.25k
    errno = 0;
871
3.71k
    year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR;
872
3.71k
    result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR];
873
3.71k
    result += (year - 1968) / 4;
874
3.71k
    result -= (year - 1900) / 100;
875
3.71k
    result += (year - 1600) / 400;
876
3.71k
    if (0 == (year % 4) &&
877
689
        (0 != (year % 100) ||
878
456
         0 == (year % 400)) &&
879
540
        (2 > (t->tm_mon % MONTHSPERYEAR))) {
880
241
        result--;
881
241
    }
882
3.71k
    result += t->tm_mday - 1;
883
3.71k
    result *= 24;
884
3.71k
    result += t->tm_hour;
885
3.71k
    result *= 60;
886
3.71k
    result += t->tm_min;
887
3.71k
    result *= 60;
888
3.71k
    result += t->tm_sec;
889
    /* this is UTC, no DST
890
     * if (t->tm_isdst == 1)
891
     * result -= 3600;
892
     */
893
3.71k
    return result;
894
9.25k
}
895
896
// ISO8601 UTC to Unix timespec, no leapsecond correction.
897
timespec_t iso8601_to_timespec(const char *isotime)
898
0
{
899
0
    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
0
    double usec = 0;
921
0
    struct tm tm = {0};
922
923
0
    {
924
0
        char *dp = NULL;
925
0
        dp = strptime(isotime, "%Y-%m-%dT%H:%M:%S", &tm);
926
0
        if (NULL != dp &&
927
0
            '.' == *dp) {
928
0
            usec = strtod(dp, NULL);
929
0
        }
930
0
    }
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
0
    ret.tv_sec = mkgmtime(&tm);
940
0
    ret.tv_nsec = usec * 1e9;
941
0
#endif  // USE_QT
942
943
0
#if 4 < SIZEOF_TIME_T
944
0
    if (253402300799LL < ret.tv_sec) {
945
        // enforce max "9999-12-31T23:59:59.999Z"
946
0
        ret.tv_sec = 253402300799LL;
947
0
    }
948
0
#endif
949
0
    return ret;
950
0
}
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
17.5k
#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
5.84k
{
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
5.84k
    double a, b, f;             // WGS-84 ellipsoid params
1019
5.84k
    double L, L_P, U1, U2, s_U1, c_U1, s_U2, c_U2;
1020
5.84k
    double uSq, A, B, d_S, lambda;
1021
    // cppcheck-suppress variableScope
1022
5.84k
    double s_L, c_L, s_A, C;
1023
5.84k
    double c_S, S, s_S, c_SqA, c_2SM;
1024
5.84k
    int i = 100;
1025
1026
5.84k
    a = WGS84A;
1027
5.84k
    b = WGS84B;
1028
5.84k
    f = 1 / WGS84F;
1029
5.84k
    L = Deg2Rad(lon2 - lon1);
1030
5.84k
    U1 = atan((1 - f) * tan(Deg2Rad(lat1)));
1031
5.84k
    U2 = atan((1 - f) * tan(Deg2Rad(lat2)));
1032
5.84k
    s_U1 = sin(U1);
1033
5.84k
    c_U1 = cos(U1);
1034
5.84k
    s_U2 = sin(U2);
1035
5.84k
    c_U2 = cos(U2);
1036
5.84k
    lambda = L;
1037
1038
37.4k
    do {
1039
37.4k
        s_L = sin(lambda);
1040
37.4k
        c_L = cos(lambda);
1041
37.4k
        s_S = sqrt((c_U2 * s_L) * (c_U2 * s_L) +
1042
37.4k
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L) *
1043
37.4k
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L));
1044
1045
37.4k
        if (0 == s_S) {
1046
774
            return 0;
1047
774
        }
1048
1049
36.7k
        c_S = s_U1 * s_U2 + c_U1 * c_U2 * c_L;
1050
36.7k
        S = atan2(s_S, c_S);
1051
36.7k
        s_A = c_U1 * c_U2 * s_L / s_S;
1052
36.7k
        c_SqA = 1 - s_A * s_A;
1053
36.7k
        c_2SM = c_S - 2 * s_U1 * s_U2 / c_SqA;
1054
1055
36.7k
        if (0 == isfinite(c_2SM)) {
1056
28.7k
            c_2SM = 0;
1057
28.7k
        }
1058
1059
36.7k
        C = f / 16 * c_SqA * (4 + f * (4 - 3 * c_SqA));
1060
36.7k
        L_P = lambda;
1061
36.7k
        lambda = L + (1 - C) * f * s_A *
1062
36.7k
            (S + C * s_S * (c_2SM + C * c_S * (2 * c_2SM * c_2SM - 1)));
1063
36.7k
    } while ((fabs(lambda - L_P) > 1.0e-12) &&
1064
31.9k
             (0 < --i));
1065
1066
5.06k
    if (0 == i) {
1067
264
        return NAN;             // formula failed to converge
1068
264
    }
1069
1070
4.80k
    uSq = c_SqA * ((a * a) - (b * b)) / (b * b);
1071
4.80k
    A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
1072
4.80k
    B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
1073
4.80k
    d_S = B * s_S * (c_2SM + B / 4 *
1074
4.80k
                     (c_S * (-1 + 2 * c_2SM * c_2SM) - B / 6 * c_2SM *
1075
4.80k
                      (-3 + 4 * s_S * s_S) * (-3 + 4 * c_2SM * c_2SM)));
1076
1077
4.80k
    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
4.80k
    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
4.80k
    return (WGS84B * A * (S - d_S));
1087
5.06k
}
1088
1089
// Distance in meters between two points specified in degrees.
1090
double earth_distance(double lat1, double lon1, double lat2, double lon2)
1091
5.84k
{
1092
5.84k
    return earth_distance_and_bearings(lat1, lon1, lat2, lon2, NULL, NULL);
1093
5.84k
}
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
0
{
1118
0
    const char *datum_str;
1119
1120
0
    switch (code) {
1121
0
    case 0:
1122
0
        datum_str = "WGS84";
1123
0
        break;
1124
0
    case 21:
1125
0
        datum_str = "WGS84";
1126
0
        break;
1127
0
    case 178:
1128
0
        datum_str = "Tokyo Mean";
1129
0
        break;
1130
0
    case 179:
1131
0
        datum_str = "Tokyo-Japan";
1132
0
        break;
1133
0
    case 180:
1134
0
        datum_str = "Tokyo-Korea";
1135
0
        break;
1136
0
    case 181:
1137
0
        datum_str = "Tokyo-Okinawa";
1138
0
        break;
1139
0
    case 182:
1140
0
        datum_str = "PZ90.11";
1141
0
        break;
1142
0
    case 999:
1143
0
        datum_str = "User Defined";
1144
0
        break;
1145
0
    default:
1146
0
        datum_str = NULL;
1147
0
        break;
1148
0
    }
1149
1150
0
    if (NULL == datum_str) {
1151
        // Fake it
1152
0
        snprintf(buffer, len, "%d", code);
1153
0
    } else {
1154
0
        strlcpy(buffer, datum_str, len);
1155
0
    }
1156
0
}
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
49.9k
{
1176
49.9k
    short nmea_PRN = 0;
1177
1178
49.9k
    if (1 > svId) {
1179
        // skip 0 svId
1180
14.7k
        return 0;
1181
14.7k
    }
1182
1183
35.2k
    switch (gnssId) {
1184
6.61k
    case 0:
1185
        // GPS, 1-32 maps to 1-32
1186
6.61k
        if (32 >= svId) {
1187
2.70k
            nmea_PRN = svId;
1188
2.70k
        }
1189
6.61k
        break;
1190
2.67k
    case 1:
1191
        // SBAS, 120..151, 152..158 maps to 33..64, 152..158
1192
2.67k
        if (120 <= svId &&
1193
1.96k
            151 >= svId) {
1194
806
            nmea_PRN = svId - 87;
1195
1.87k
        } else if (158 >= svId) {
1196
1.63k
            nmea_PRN = svId;
1197
1.63k
        }
1198
2.67k
        break;
1199
2.24k
    case 2:
1200
        // Galileo, ubx gnssid:svid 1..36 ->  301-336
1201
        // Galileo, ubx PRN         211..246 ->  301-336
1202
2.24k
        if (36 >= svId) {
1203
1.60k
            nmea_PRN = svId + 300;
1204
1.60k
        } else if (211 > svId) {
1205
            // skip bad svId
1206
222
            return 0;
1207
420
        } else if (246 >= svId) {
1208
202
            nmea_PRN = svId + 90;
1209
202
        }
1210
2.02k
        break;
1211
5.63k
    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
5.63k
        if (63 >= svId) {
1217
5.06k
            nmea_PRN = svId + 400;
1218
5.06k
        } else if (159 > svId) {
1219
            // skip bad svId
1220
220
            return 0;
1221
353
        } else if (163 >= svId) {
1222
50
            nmea_PRN = svId + 242;
1223
50
        }
1224
5.41k
        break;
1225
5.41k
    case 4:
1226
        // IMES, ubx gnssid:svid 1-10 -> to 173-182
1227
        // IMES, ubx PRN         173-182 to 173-182
1228
1.40k
        if (10 >= svId) {
1229
549
            nmea_PRN = svId + 172;
1230
852
        } else if (173 > svId) {
1231
            // skip bad svId
1232
386
            return 0;
1233
466
        } else if (182 >= svId) {
1234
229
            nmea_PRN = svId;
1235
229
        }
1236
1.01k
        break;
1237
1.56k
    case 5:
1238
        // QZSS, ubx gnssid:svid 1-10 to 193-202
1239
        // QZSS, ubx PRN         193-202 to 193-202
1240
1.56k
        if (10 >= svId) {
1241
957
            nmea_PRN = svId + 192;
1242
957
        } else if (193 > svId) {
1243
            // skip bad svId
1244
276
            return 0;
1245
336
        } else if (202 >= svId) {
1246
133
            nmea_PRN = svId;
1247
133
        }
1248
1.29k
        break;
1249
2.15k
    case 6:
1250
        // GLONASS, 1-32 maps to 65-96
1251
2.15k
        if (32 >= svId) {
1252
1.21k
            nmea_PRN = svId + 64;
1253
1.21k
        } else if (65 > svId) {
1254
            // skip bad svId
1255
206
            return 0;
1256
728
        } else if (96 >= svId) {
1257
103
            nmea_PRN = svId;
1258
625
        } else if (255 == svId) {
1259
            // skip bad svId, 255 == tracked, but unidentified, skip
1260
287
            nmea_PRN = -1;
1261
287
        }
1262
1.94k
        break;
1263
1.94k
    case 7:
1264
        // NavIC (IRNSS), 1 - 14 -> 801 - 814
1265
1.78k
        if (14 >= svId) {
1266
1.08k
            nmea_PRN = svId + 800;;
1267
1.08k
        }
1268
1.78k
        break;
1269
11.1k
    default:
1270
        // Huh?
1271
11.1k
        nmea_PRN = 0;
1272
35.2k
    }
1273
1274
33.9k
    return nmea_PRN;
1275
35.2k
}
1276
1277
// vim: set expandtab shiftwidth=4