Coverage Report

Created: 2026-07-16 06:23

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