Coverage Report

Created: 2026-10-12 06:10

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.37k
{
295
5.37k
    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.37k
    static double powersOf10[] = {
303
5.37k
        10.,
304
5.37k
        100.,
305
5.37k
        1.0e4,
306
5.37k
        1.0e8,
307
5.37k
        1.0e16,
308
5.37k
        1.0e32,
309
5.37k
        1.0e64,
310
5.37k
        1.0e128,
311
5.37k
        1.0e256
312
5.37k
    };
313
314
5.37k
    bool sign = false, expSign = false;
315
5.37k
    double fraction, dblExp, *d;
316
5.37k
    const char *p;
317
5.37k
    unsigned char c;
318
5.37k
    int exp = 0;                // Exponent read from "EX" field.
319
5.37k
    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.37k
    int mantSize;               // Number of digits in mantissa.
329
5.37k
    int decPt;                  /* Number of mantissa digits BEFORE decimal
330
                                 * point. */
331
5.37k
    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.37k
    p = string;
341
5.37k
    while (isspace((int)*p)) {
342
0
        p += 1;
343
0
    }
344
5.37k
    if (isdigit((unsigned char)*p)) {
345
        // ignore
346
2.80k
    } else if ('-' == *p) {
347
848
        sign = true;
348
848
        p += 1;
349
1.95k
    } else if ('+' == *p) {
350
370
        p += 1;
351
1.58k
    } else if ('.' == *p) {
352
        // ignore
353
1.18k
    } else {
354
402
        return NAN;
355
402
    }
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.97k
    decPt = -1;
363
20.4k
    for (mantSize = 0; ; mantSize += 1) {
364
20.4k
        c = *p;
365
20.4k
        if (!isdigit(c)) {
366
6.39k
            if ((c != '.') || (decPt >= 0)) {
367
4.97k
                break;
368
4.97k
            }
369
1.42k
            decPt = mantSize;
370
1.42k
        }
371
15.5k
        p += 1;
372
15.5k
    }
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.97k
    pExp  = p;
382
4.97k
    p -= mantSize;
383
4.97k
    if (decPt < 0) {
384
3.54k
        decPt = mantSize;
385
3.54k
    } else {
386
1.42k
        mantSize -= 1;                  // One of the digits was the point.
387
1.42k
    }
388
4.97k
    if (mantSize > 18) {
389
209
        fracExp = decPt - 18;
390
209
        mantSize = 18;
391
4.76k
    } else {
392
4.76k
        fracExp = decPt - mantSize;
393
4.76k
    }
394
4.97k
    if (mantSize == 0) {
395
1.61k
        fraction = 0.0;
396
        // p = string;
397
1.61k
        goto done;
398
3.35k
    } else {
399
3.35k
        int frac1, frac2;
400
401
3.35k
        frac1 = 0;
402
6.06k
        for ( ; mantSize > 9; mantSize -= 1) {
403
2.70k
            c = *p;
404
2.70k
            p += 1;
405
2.70k
            if ('.' == c) {
406
194
                c = *p;
407
194
                p += 1;
408
194
            }
409
2.70k
            frac1 = 10*frac1 + (c - '0');
410
2.70k
        }
411
3.35k
        frac2 = 0;
412
12.3k
        for (; mantSize > 0; mantSize -= 1) {
413
8.96k
            c = *p;
414
8.96k
            p += 1;
415
8.96k
            if ('.' == c) {
416
195
                c = *p;
417
195
                p += 1;
418
195
            }
419
8.96k
            frac2 = 10*frac2 + (c - '0');
420
8.96k
        }
421
3.35k
        fraction = (1.0e9 * frac1) + frac2;
422
3.35k
    }
423
424
    /*
425
     * Skim off the exponent.
426
     */
427
428
3.35k
    p = pExp;
429
3.35k
    if (('E' == *p) ||
430
3.09k
        ('e' == *p)) {
431
2.04k
        p += 1;
432
2.04k
        if ('-' == *p) {
433
597
            expSign = true;
434
597
            p += 1;
435
1.44k
        } else {
436
1.44k
            if ('+' == *p) {
437
194
                p += 1;
438
194
            }
439
1.44k
            expSign = false;
440
1.44k
        }
441
4.31k
        while (isdigit((unsigned char) *p)) {
442
4.31k
            exp = exp * 10 + (*p - '0');
443
4.31k
            if (1024 < exp) {
444
778
                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
390
                if (sign) {
452
194
                    return -INFINITY;
453
194
                }
454
196
                return INFINITY;
455
390
            }
456
3.53k
            p += 1;
457
3.53k
        }
458
2.04k
    }
459
2.58k
    if (expSign) {
460
209
        exp = fracExp - exp;
461
2.37k
    } else {
462
2.37k
        exp = fracExp + exp;
463
2.37k
    }
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.58k
    if (0 > exp) {
473
404
        expSign = true;
474
404
        exp = -exp;
475
2.17k
    } else {
476
2.17k
        expSign = false;
477
2.17k
    }
478
2.58k
    if (exp > maxExponent) {
479
224
        exp = maxExponent;
480
224
        errno = ERANGE;
481
224
    }
482
2.58k
    dblExp = 1.0;
483
6.96k
    for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
484
4.38k
        if (exp & 01) {
485
3.31k
            dblExp *= *d;
486
3.31k
        }
487
4.38k
    }
488
2.58k
    if (expSign) {
489
404
        fraction /= dblExp;
490
2.17k
    } else {
491
2.17k
        fraction *= dblExp;
492
2.17k
    }
493
494
4.19k
done:
495
4.19k
    if (sign) {
496
460
        return -fraction;
497
460
    }
498
3.73k
    return fraction;
499
4.19k
}
500
501
2.61k
#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_dev = NAN;
535
0
    fixp->magnetic_track = NAN;
536
0
    fixp->magnetic_var = NAN;
537
0
    fixp->mode = MODE_NOT_SEEN;
538
0
    fixp->sep = NAN;
539
0
    fixp->speed = NAN;
540
0
    fixp->track = NAN;
541
    // clear ECEF too
542
0
    fixp->ecef.x = NAN;
543
0
    fixp->ecef.y = NAN;
544
0
    fixp->ecef.z = NAN;
545
0
    fixp->ecef.vx = NAN;
546
0
    fixp->ecef.vy = NAN;
547
0
    fixp->ecef.vz = NAN;
548
0
    fixp->ecef.pAcc = NAN;
549
0
    fixp->ecef.vAcc = NAN;
550
0
    fixp->errEllipseOrient = NAN;
551
0
    fixp->errEllipseMajor = NAN;
552
0
    fixp->errEllipseMinor = NAN;
553
0
    fixp->NED.relPosN = NAN;
554
0
    fixp->NED.relPosE = NAN;
555
0
    fixp->NED.relPosD = NAN;
556
0
    fixp->NED.velN = NAN;
557
0
    fixp->NED.velE = NAN;
558
0
    fixp->NED.velD = NAN;
559
0
    fixp->geoid_sep = NAN;
560
0
    fixp->dgps_age = NAN;
561
0
    fixp->dgps_station = -1;
562
0
    fixp->temp = NAN;
563
0
    fixp->wanglem = NAN;
564
0
    fixp->wangler = NAN;
565
0
    fixp->wanglet = NAN;
566
0
    fixp->wspeedr = NAN;
567
0
    fixp->wspeedt = NAN;
568
0
    fixp->wtemp = NAN;
569
0
    gps_clear_base(&fixp->base);
570
0
}
571
572
// stuff an attitude structure with recognizable out-of-band values
573
void gps_clear_att(struct attitude_t *attp)
574
0
{
575
0
    memset(attp, 0, sizeof(struct attitude_t));
576
0
    attp->acc_len = NAN;
577
0
    attp->acc_x = NAN;
578
0
    attp->acc_y = NAN;
579
0
    attp->acc_z = NAN;
580
0
    attp->depth = NAN;
581
0
    attp->dip = NAN;
582
0
    attp->gyro_temp = NAN;
583
0
    attp->gyro_x = NAN;
584
0
    attp->gyro_y = NAN;
585
0
    attp->gyro_z = NAN;
586
0
    attp->heading = NAN;
587
0
    attp->mheading = NAN;
588
0
    attp->mag_len = NAN;
589
0
    attp->mag_x = NAN;
590
0
    attp->mag_y = NAN;
591
0
    attp->mag_z = NAN;
592
0
    attp->pitch = NAN;
593
0
    attp->roll = NAN;
594
0
    attp->rot = NAN;
595
0
    attp->temp = NAN;
596
0
    attp->yaw = NAN;
597
0
    gps_clear_base(&attp->base);
598
0
}
599
600
// Clear a dop_t structure
601
void gps_clear_dop( struct dop_t *dop)
602
0
{
603
0
    dop->xdop = dop->ydop = dop->vdop = dop->tdop = dop->hdop = dop->pdop =
604
0
        dop->gdop = NAN;
605
0
}
606
607
// Clear a gst structure
608
void gps_clear_gst( struct gst_t *gst)
609
0
{
610
0
    memset(&gst->utctime, 0, sizeof(gst->utctime));
611
0
    gst-> rms_deviation =  NAN;
612
0
    gst-> smajor_deviation =  NAN;
613
0
    gst-> sminor_deviation =  NAN;
614
0
    gst-> smajor_orientation =  NAN;
615
0
    gst-> lat_err_deviation =  NAN;
616
0
    gst-> lon_err_deviation =  NAN;
617
0
    gst-> alt_err_deviation =  NAN;
618
0
    gst-> ve_err_deviation =  NAN;
619
0
    gst-> vn_err_deviation =  NAN;
620
0
    gst-> vu_err_deviation =  NAN;
621
0
}
622
623
// stuff a log structure with recognizable out-of-band values
624
void gps_clear_log(struct gps_log_t *logp)
625
0
{
626
0
    memset(logp, 0, sizeof(struct gps_log_t));
627
0
    logp->lon = NAN;
628
0
    logp->lat = NAN;
629
0
    logp->altHAE = NAN;
630
0
    logp->altMSL = NAN;
631
0
    logp->gSpeed = NAN;
632
0
    logp->heading = NAN;
633
0
    logp->tAcc = NAN;
634
0
    logp->hAcc = NAN;
635
0
    logp->vAcc = NAN;
636
0
    logp->sAcc = NAN;
637
0
    logp->headAcc = NAN;
638
0
    logp->velN = NAN;
639
0
    logp->velE = NAN;
640
0
    logp->velD = NAN;
641
0
    logp->pDOP = NAN;
642
0
    logp->distance = NAN;
643
0
    logp->totalDistance = NAN;
644
0
    logp->distanceStd = NAN;
645
0
    logp->fixType = -1;
646
0
}
647
648
/* merge new data (from) into current fix (to)
649
 * Being careful not to lose information */
650
void gps_merge_fix(struct gps_fix_t *to,
651
                   gps_mask_t transfer,
652
                   struct gps_fix_t *from)
653
0
{
654
0
    if ((NULL == to) ||
655
0
        (NULL == from)) {
656
0
        return;
657
0
    }
658
0
    if (0 != (transfer & TIME_SET)) {
659
0
        to->time = from->time;
660
0
    }
661
0
    if (0 != (transfer & LATLON_SET)) {
662
0
        to->latitude = from->latitude;
663
0
        to->longitude = from->longitude;
664
0
    }
665
0
    if (0 != (transfer & MODE_SET)) {
666
        // FIXME?  Maybe only upgrade mode, not downgrade it
667
0
        to->mode = from->mode;
668
0
    }
669
    /* Some messages only report mode, some mode and status, some only status.
670
     * Only upgrade status, not downgrade it */
671
0
    if (0 != (transfer & STATUS_SET)) {
672
0
        if (to->status < from->status) {
673
0
            to->status = from->status;
674
0
        }
675
0
    }
676
0
    if ((transfer & ALTITUDE_SET) != 0) {
677
0
        if (0 != isfinite(from->altHAE)) {
678
0
            to->altHAE = from->altHAE;
679
0
        }
680
0
        if (0 != isfinite(from->altMSL)) {
681
0
            to->altMSL = from->altMSL;
682
0
        }
683
0
        if (0 != isfinite(from->depth)) {
684
0
            to->depth = from->depth;
685
0
        }
686
0
    }
687
0
    if (0 != (transfer & TRACK_SET)) {
688
0
        to->track = from->track;
689
0
    }
690
0
    if (0 != (transfer & MAGNETIC_TRACK_SET)) {
691
0
        if (0 != isfinite(from->magnetic_dev)) {
692
0
            to->magnetic_dev = from->magnetic_dev;
693
0
        }
694
0
        if (0 != isfinite(from->magnetic_track)) {
695
0
            to->magnetic_track = from->magnetic_track;
696
0
        }
697
0
        if (0 != isfinite(from->magnetic_var)) {
698
0
            to->magnetic_var = from->magnetic_var;
699
0
        }
700
0
    }
701
0
    if (0 != (transfer & SPEED_SET)) {
702
0
        to->speed = from->speed;
703
0
    }
704
0
    if (0 != (transfer & CLIMB_SET)) {
705
0
        to->climb = from->climb;
706
0
    }
707
0
    if (0 != (transfer & TIMERR_SET)) {
708
0
        to->ept = from->ept;
709
0
    }
710
0
    if (0 != isfinite(from->epx) &&
711
0
        0 != isfinite(from->epy)) {
712
0
        to->epx = from->epx;
713
0
        to->epy = from->epy;
714
0
    }
715
0
    if (0 != isfinite(from->epd)) {
716
0
        to->epd = from->epd;
717
0
    }
718
0
    if (0 != isfinite(from->eph)) {
719
0
        to->eph = from->eph;
720
0
    }
721
0
    if (0 != isfinite(from->eps)) {
722
0
        to->eps = from->eps;
723
0
    }
724
    // spherical error probability, not geoid separation
725
0
    if (0 != isfinite(from->sep)) {
726
0
        to->sep = from->sep;
727
0
    }
728
    // geoid separation, not spherical error probability
729
0
    if (0 != isfinite(from->geoid_sep)) {
730
0
        to->geoid_sep = from->geoid_sep;
731
0
    }
732
0
    if (0 != isfinite(from->epv)) {
733
0
        to->epv = from->epv;
734
0
    }
735
0
    if (0 != (transfer & SPEEDERR_SET)) {
736
0
        to->eps = from->eps;
737
0
    }
738
0
    if (0 != (transfer & ECEF_SET)) {
739
0
        to->ecef.x = from->ecef.x;
740
0
        to->ecef.y = from->ecef.y;
741
0
        to->ecef.z = from->ecef.z;
742
0
        to->ecef.pAcc = from->ecef.pAcc;
743
0
    }
744
0
    if (0 != (transfer & VECEF_SET)) {
745
0
        to->ecef.vx = from->ecef.vx;
746
0
        to->ecef.vy = from->ecef.vy;
747
0
        to->ecef.vz = from->ecef.vz;
748
0
        to->ecef.vAcc = from->ecef.vAcc;
749
0
    }
750
0
    if (0 != isfinite(from->errEllipseOrient) &&
751
0
        0 != isfinite(from->errEllipseMajor) &&
752
0
        0 != isfinite(from->errEllipseMinor)) {
753
0
        to->errEllipseOrient = from->errEllipseOrient;
754
0
        to->errEllipseMajor = from->errEllipseMajor;
755
0
        to->errEllipseMinor = from->errEllipseMinor;
756
0
    }
757
0
    if (0 != (transfer & NED_SET)) {
758
0
        to->NED.relPosN = from->NED.relPosN;
759
0
        to->NED.relPosE = from->NED.relPosE;
760
0
        to->NED.relPosD = from->NED.relPosD;
761
0
        if ((0 != isfinite(from->NED.relPosH)) &&
762
0
            (0 != isfinite(from->NED.relPosL))) {
763
0
            to->NED.relPosH = from->NED.relPosH;
764
0
            to->NED.relPosL = from->NED.relPosL;
765
0
        }
766
0
    }
767
0
    if (0 != (transfer & VNED_SET)) {
768
0
        to->NED.velN = from->NED.velN;
769
0
        to->NED.velE = from->NED.velE;
770
0
        to->NED.velD = from->NED.velD;
771
0
    }
772
0
    if ('\0' != from->datum[0]) {
773
0
        strlcpy(to->datum, from->datum, sizeof(to->datum));
774
0
    }
775
0
    if (0 != isfinite(from->dgps_age) &&
776
0
        0 <= from->dgps_station) {
777
        // both, or neither
778
0
        to->dgps_age = from->dgps_age;
779
0
        to->dgps_station = from->dgps_station;
780
0
    }
781
782
0
    if (STATUS_UNK != from->base.status) {
783
0
        to->base.status = from->base.status;
784
0
    }
785
786
0
    if (0 != isfinite(from->base.ratio)) {
787
0
        to->base.ratio = from->base.ratio;
788
0
    }
789
790
0
    if (0 != isfinite(from->base.east) &&
791
0
        0 != isfinite(from->base.north) &&
792
0
        0 != isfinite(from->base.up)) {
793
0
        to->base.east = from->base.east;
794
0
        to->base.north = from->base.north;
795
0
        to->base.up = from->base.up;
796
0
    }
797
798
0
    if (0 != isfinite(from->base.length) &&
799
0
        0 != isfinite(from->base.course)) {
800
0
        to->base.length = from->base.length;
801
0
        to->base.course = from->base.course;
802
0
    }
803
804
0
    if (ANT_UNK != from->ant_stat) {
805
0
        to->ant_stat = from->ant_stat;
806
0
    }
807
0
    if (0 < from->jam) {
808
0
        to->jam = from->jam;
809
0
    }
810
0
    if (ANT_PWR_UNK != from->ant_power) {
811
0
        to->ant_power = from->ant_power;
812
0
    }
813
    // navdata stuff.  just wind angle and angle for now
814
0
    if (0 != (transfer & NAVDATA_SET)) {
815
0
        if (0 != isfinite(from->wanglem)) {
816
0
            to->wanglem = from->wanglem;
817
0
        }
818
0
        if (0 != isfinite(from->wangler)) {
819
0
            to->wangler = from->wangler;
820
0
        }
821
0
        if (0 != isfinite(from->wanglet)) {
822
0
            to->wanglet = from->wanglet;
823
0
        }
824
0
        if (0 != isfinite(from->wspeedr)) {
825
0
            to->wspeedr = from->wspeedr;
826
0
        }
827
0
        if (0 != isfinite(from->wspeedt)) {
828
0
            to->wspeedt = from->wspeedt;
829
0
        }
830
0
    }
831
0
    if (0 != isfinite(from->temp)) {
832
0
        to->temp = from->temp;
833
0
    }
834
0
    if (0 != isfinite(from->wtemp)) {
835
0
        to->wtemp = from->wtemp;
836
0
    }
837
0
}
838
839
/* mkgmtime(tm)
840
 * convert struct tm, as UTC, to seconds since Unix epoch
841
 * This differs from mktime() from libc.
842
 * mktime() takes struct tm as localtime.
843
 *
844
 * The inverse of gmtime(time_t)
845
 *
846
 * Return: -1 on error, set errno to EOVERFLOW
847
 */
848
time_t mkgmtime(struct tm * t)
849
1.91k
{
850
1.91k
    int year;
851
1.91k
    time_t result;
852
1.91k
    static const int cumdays[MONTHSPERYEAR] =
853
1.91k
        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
854
855
    // check ranges, ignore tm_isdst and max tm_year
856
1.91k
    if (0 > t->tm_sec ||
857
1.91k
        0 > t->tm_min ||
858
1.91k
        0 > t->tm_hour ||
859
1.91k
        1 > t->tm_mday ||
860
1.72k
        0 > t->tm_mon ||
861
1.72k
        0 > t->tm_year ||
862
1.09k
        0 > t->tm_wday ||
863
1.09k
        0 > t->tm_yday ||
864
1.09k
        61 < t->tm_sec ||
865
1.09k
        59 < t->tm_min ||
866
1.09k
        23 < t->tm_hour ||
867
1.09k
        31 < t->tm_mday ||
868
1.09k
        11 < t->tm_mon ||
869
1.09k
        6 < t->tm_wday ||
870
1.09k
        365 < t->tm_yday) {
871
819
        errno = EOVERFLOW;
872
819
        return -1;
873
819
    }
874
1.91k
    errno = 0;
875
1.09k
    year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR;
876
1.09k
    result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR];
877
1.09k
    result += (year - 1968) / 4;
878
1.09k
    result -= (year - 1900) / 100;
879
1.09k
    result += (year - 1600) / 400;
880
1.09k
    if (0 == (year % 4) &&
881
617
        (0 != (year % 100) ||
882
400
         0 == (year % 400)) &&
883
418
        (2 > (t->tm_mon % MONTHSPERYEAR))) {
884
205
        result--;
885
205
    }
886
1.09k
    result += t->tm_mday - 1;
887
1.09k
    result *= 24;
888
1.09k
    result += t->tm_hour;
889
1.09k
    result *= 60;
890
1.09k
    result += t->tm_min;
891
1.09k
    result *= 60;
892
1.09k
    result += t->tm_sec;
893
    /* this is UTC, no DST
894
     * if (t->tm_isdst == 1)
895
     * result -= 3600;
896
     */
897
1.09k
    return result;
898
1.91k
}
899
900
// ISO8601 UTC to Unix timespec, no leapsecond correction.
901
timespec_t iso8601_to_timespec(const char *isotime)
902
1.91k
{
903
1.91k
    timespec_t ret = {0};
904
905
#ifdef USE_QT
906
  #ifndef __clang_analyzer__
907
    double usec = 0;
908
909
    QString t(isotime);
910
    QDateTime d = QDateTime::fromString(isotime, Qt::ISODate);
911
    QStringList sl = t.split(".");
912
    if (1 < sl.size()) {
913
        usec = sl[1].toInt() / pow(10., (double)sl[1].size());
914
    }
915
// toSecsSinceEpoch wasn't introduced until 5.8
916
#if QT_VERSION < QT_VERSION_CHECK(5,8,0)
917
    ret.tv_sec = d.toTime_t();
918
#else
919
    ret.tv_sec = d.toSecsSinceEpoch();
920
#endif
921
    ret.tv_nsec = usec * 1e9;
922
  #endif  // __clang_analyzer__
923
#else  // USE_QT
924
1.91k
    double usec = 0;
925
1.91k
    struct tm tm = {0};
926
927
1.91k
    {
928
1.91k
        char *dp = NULL;
929
1.91k
        dp = strptime(isotime, "%Y-%m-%dT%H:%M:%S", &tm);
930
1.91k
        if (NULL != dp &&
931
630
            '.' == *dp) {
932
194
            usec = strtod(dp, NULL);
933
194
        }
934
1.91k
    }
935
936
    /*
937
     * It would be nice if we could say mktime(&tm) - timezone + usec instead,
938
     * but timezone is not available at all on some BSDs. Besides, when working
939
     * with historical dates the value of timezone after an ordinary tzset(3)
940
     * can be wrong; you have to do a redirect through the IANA historical
941
     * timezone database to get it right.
942
     */
943
1.91k
    ret.tv_sec = mkgmtime(&tm);
944
1.91k
    ret.tv_nsec = usec * 1e9;
945
1.91k
#endif  // USE_QT
946
947
1.91k
#if 4 < SIZEOF_TIME_T
948
1.91k
    if (253402300799LL < ret.tv_sec) {
949
        // enforce max "9999-12-31T23:59:59.999Z"
950
194
        ret.tv_sec = 253402300799LL;
951
194
    }
952
1.91k
#endif
953
1.91k
    return ret;
954
1.91k
}
955
956
/* Convert POSIX timespec to ISO8601 UTC, put result in isotime.
957
 * no timezone adjustment
958
 * Return: pointer to isotime.
959
 * example: 2007-12-11T23:38:51.033Z */
960
char *timespec_to_iso8601(timespec_t fixtime, char isotime[], size_t len)
961
0
{
962
0
    struct tm when;
963
0
    char timestr[30];
964
0
    long fracsec;
965
966
0
    if (0 > fixtime.tv_sec) {
967
        // Allow 0 for testing of 1970-01-01T00:00:00.000Z
968
0
        strlcpy(isotime, "NaN", len);
969
0
        return isotime;
970
0
    }
971
0
    if (999499999 < fixtime.tv_nsec) {
972
        // round up
973
0
        fixtime.tv_sec++;
974
0
        fixtime.tv_nsec = 0;
975
0
    }
976
977
0
#if 4 < SIZEOF_TIME_T
978
0
    if (253402300799LL < fixtime.tv_sec) {
979
        // enforce max "9999-12-31T23:59:59.999Z"
980
0
        fixtime.tv_sec = 253402300799LL;
981
0
    }
982
0
#endif
983
984
0
    (void)gmtime_r(&fixtime.tv_sec, &when);
985
986
    /*
987
     * Do not mess casually with the number of decimal digits in the
988
     * format!  Most GPSes report over serial links at 0.01s or 0.001s
989
     * precision.  Round to 0.001s
990
     */
991
0
    fracsec = (fixtime.tv_nsec + 500000) / 1000000;
992
993
0
    (void)strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &when);
994
0
    (void)snprintf(isotime, len, "%s.%03ldZ",timestr, fracsec);
995
996
0
    return isotime;
997
0
}
998
999
/* return time now as ISO8601, no timezone adjustment
1000
 * example: 2007-12-11T23:38:51.033Z */
1001
char *now_to_iso8601(char *tbuf, size_t tbuf_sz)
1002
0
{
1003
0
    timespec_t ts_now;
1004
1005
0
    (void)clock_gettime(CLOCK_REALTIME, &ts_now);
1006
0
    return timespec_to_iso8601(ts_now, tbuf, tbuf_sz);
1007
0
}
1008
1009
0
#define Deg2Rad(n)      ((n) * DEG_2_RAD)
1010
1011
/* Distance in meters between two points specified in degrees, optionally
1012
 * with initial and final bearings. */
1013
double earth_distance_and_bearings(double lat1, double lon1,
1014
                                   double lat2, double lon2,
1015
                                   double *ib, double *fb)
1016
0
{
1017
    /*
1018
     * this is a translation of the javascript implementation of the
1019
     * Vincenty distance formula by Chris Veness. See
1020
     * http://www.movable-type.co.uk/scripts/latlong-vincenty.html
1021
     */
1022
0
    double a, b, f;             // WGS-84 ellipsoid params
1023
0
    double L, L_P, U1, U2, s_U1, c_U1, s_U2, c_U2;
1024
0
    double uSq, A, B, d_S, lambda;
1025
    // cppcheck-suppress variableScope
1026
0
    double s_L, c_L, s_A, C;
1027
0
    double c_S, S, s_S, c_SqA, c_2SM;
1028
0
    int i = 100;
1029
1030
0
    a = WGS84A;
1031
0
    b = WGS84B;
1032
0
    f = 1 / WGS84F;
1033
0
    L = Deg2Rad(lon2 - lon1);
1034
0
    U1 = atan((1 - f) * tan(Deg2Rad(lat1)));
1035
0
    U2 = atan((1 - f) * tan(Deg2Rad(lat2)));
1036
0
    s_U1 = sin(U1);
1037
0
    c_U1 = cos(U1);
1038
0
    s_U2 = sin(U2);
1039
0
    c_U2 = cos(U2);
1040
0
    lambda = L;
1041
1042
0
    do {
1043
0
        s_L = sin(lambda);
1044
0
        c_L = cos(lambda);
1045
0
        s_S = sqrt((c_U2 * s_L) * (c_U2 * s_L) +
1046
0
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L) *
1047
0
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L));
1048
1049
0
        if (0 == s_S) {
1050
0
            return 0;
1051
0
        }
1052
1053
0
        c_S = s_U1 * s_U2 + c_U1 * c_U2 * c_L;
1054
0
        S = atan2(s_S, c_S);
1055
0
        s_A = c_U1 * c_U2 * s_L / s_S;
1056
0
        c_SqA = 1 - s_A * s_A;
1057
0
        c_2SM = c_S - 2 * s_U1 * s_U2 / c_SqA;
1058
1059
0
        if (0 == isfinite(c_2SM)) {
1060
0
            c_2SM = 0;
1061
0
        }
1062
1063
0
        C = f / 16 * c_SqA * (4 + f * (4 - 3 * c_SqA));
1064
0
        L_P = lambda;
1065
0
        lambda = L + (1 - C) * f * s_A *
1066
0
            (S + C * s_S * (c_2SM + C * c_S * (2 * c_2SM * c_2SM - 1)));
1067
0
    } while ((fabs(lambda - L_P) > 1.0e-12) &&
1068
0
             (0 < --i));
1069
1070
0
    if (0 == i) {
1071
0
        return NAN;             // formula failed to converge
1072
0
    }
1073
1074
0
    uSq = c_SqA * ((a * a) - (b * b)) / (b * b);
1075
0
    A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
1076
0
    B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
1077
0
    d_S = B * s_S * (c_2SM + B / 4 *
1078
0
                     (c_S * (-1 + 2 * c_2SM * c_2SM) - B / 6 * c_2SM *
1079
0
                      (-3 + 4 * s_S * s_S) * (-3 + 4 * c_2SM * c_2SM)));
1080
1081
0
    if (NULL != ib) {
1082
0
        *ib = atan2(c_U2 * sin(lambda),
1083
0
                    c_U1 * s_U2 - s_U1 * c_U2 * cos(lambda));
1084
0
    }
1085
0
    if (NULL != fb) {
1086
0
        *fb = atan2(c_U1 * sin(lambda),
1087
0
                    c_U1 * s_U2 * cos(lambda) - s_U1 * c_U2);
1088
0
    }
1089
1090
0
    return (WGS84B * A * (S - d_S));
1091
0
}
1092
1093
// Distance in meters between two points specified in degrees.
1094
double earth_distance(double lat1, double lon1, double lat2, double lon2)
1095
0
{
1096
0
    return earth_distance_and_bearings(lat1, lon1, lat2, lon2, NULL, NULL);
1097
0
}
1098
1099
/* Wait for data until timeout, ignoring signals.
1100
 *
1101
 * pselect() may set errno on error
1102
 */
1103
bool nanowait(int fd, struct timespec *to)
1104
0
{
1105
0
    fd_set fdset;
1106
1107
0
    FD_ZERO(&fdset);
1108
0
    FD_SET(fd, &fdset);
1109
0
    TS_NORM(to);         // just in case
1110
0
    errno = 0;
1111
    // sigmask is NULL, so equivalent to select()
1112
0
    return pselect(fd + 1, &fdset, NULL, NULL, to, NULL) == 1;
1113
0
}
1114
1115
/* Accept a datum code, return matching string
1116
 *
1117
 * There are a ton of these, only a few are here
1118
 *
1119
 */
1120
void datum_code_string(int code, char *buffer, size_t len)
1121
0
{
1122
0
    const char *datum_str;
1123
1124
0
    switch (code) {
1125
0
    case 0:
1126
0
        datum_str = "WGS84";
1127
0
        break;
1128
0
    case 21:
1129
0
        datum_str = "WGS84";
1130
0
        break;
1131
0
    case 178:
1132
0
        datum_str = "Tokyo Mean";
1133
0
        break;
1134
0
    case 179:
1135
0
        datum_str = "Tokyo-Japan";
1136
0
        break;
1137
0
    case 180:
1138
0
        datum_str = "Tokyo-Korea";
1139
0
        break;
1140
0
    case 181:
1141
0
        datum_str = "Tokyo-Okinawa";
1142
0
        break;
1143
0
    case 182:
1144
0
        datum_str = "PZ90.11";
1145
0
        break;
1146
0
    case 999:
1147
0
        datum_str = "User Defined";
1148
0
        break;
1149
0
    default:
1150
0
        datum_str = NULL;
1151
0
        break;
1152
0
    }
1153
1154
0
    if (NULL == datum_str) {
1155
        // Fake it
1156
0
        snprintf(buffer, len, "%d", code);
1157
0
    } else {
1158
0
        strlcpy(buffer, datum_str, len);
1159
0
    }
1160
0
}
1161
1162
/* make up an NMEA 4.0 (extended) PRN based on gnssId:svId,
1163
 * This does NOT match NMEA 4.10 and 4.11 where all PRN are 1-99,
1164
 * except IMES, QZSS, and some SBAS.
1165
 *
1166
 * Ref Appendix A from u-blox ZED-F9P Interface Description
1167
 * and
1168
 * Section 1.5.3  M10-FW500_InterfaceDescription_UBX-20053845.pdf
1169
 *
1170
 * Using ST Teseo PRN fors for those not defined by UBX.
1171
 * um3407-teseo-vi-and-teseo-app2nmea-specifications-and-commands-stmicroelectronics.pdf
1172
 * Section 3.5
1173
 * But we do not use the per sigId PRNs from ST.
1174
 *
1175
 * Return PRN, less than one for error
1176
 *        -1 for GLONASS svid 255
1177
 */
1178
short ubx2_to_prn(int gnssId, int svId)
1179
0
{
1180
0
    short nmea_PRN = 0;
1181
1182
0
    if (1 > svId) {
1183
        // skip 0 svId
1184
0
        return 0;
1185
0
    }
1186
1187
0
    switch (gnssId) {
1188
0
    case 0:
1189
        // GPS, 1-32 maps to 1-32
1190
0
        if (32 >= svId) {
1191
0
            nmea_PRN = svId;
1192
0
        }
1193
0
        break;
1194
0
    case 1:
1195
        // SBAS, 120..151, 152..158 maps to 33..64, 152..158
1196
0
        if (120 <= svId &&
1197
0
            151 >= svId) {
1198
0
            nmea_PRN = svId - 87;
1199
0
        } else if (158 >= svId) {
1200
0
            nmea_PRN = svId;
1201
0
        }
1202
0
        break;
1203
0
    case 2:
1204
        // Galileo, ubx gnssid:svid 1..36 ->  301-336
1205
        // Galileo, ubx PRN         211..246 ->  301-336
1206
0
        if (36 >= svId) {
1207
0
            nmea_PRN = svId + 300;
1208
0
        } else if (211 > svId) {
1209
            // skip bad svId
1210
0
            return 0;
1211
0
        } else if (246 >= svId) {
1212
0
            nmea_PRN = svId + 90;
1213
0
        }
1214
0
        break;
1215
0
    case 3:
1216
        /* BeiDou, ubx gnssid:svid    1..37 -> to 401-437
1217
         * have seen 1-63 on F10 ProtVer 40.0, March 2025
1218
         *   ubx gnssid:svid    1..63 -> to 401-463
1219
         * BeiDou, ubx "single svid"  159..163,33..64 -> to 401-437 ?? */
1220
0
        if (63 >= svId) {
1221
0
            nmea_PRN = svId + 400;
1222
0
        } else if (159 > svId) {
1223
            // skip bad svId
1224
0
            return 0;
1225
0
        } else if (163 >= svId) {
1226
0
            nmea_PRN = svId + 242;
1227
0
        }
1228
0
        break;
1229
0
    case 4:
1230
        // IMES, ubx gnssid:svid 1-10 -> to 173-182
1231
        // IMES, ubx PRN         173-182 to 173-182
1232
0
        if (10 >= svId) {
1233
0
            nmea_PRN = svId + 172;
1234
0
        } else if (173 > svId) {
1235
            // skip bad svId
1236
0
            return 0;
1237
0
        } else if (182 >= svId) {
1238
0
            nmea_PRN = svId;
1239
0
        }
1240
0
        break;
1241
0
    case 5:
1242
        // QZSS, ubx gnssid:svid 1-10 to 193-202
1243
        // QZSS, ubx PRN         193-202 to 193-202
1244
0
        if (10 >= svId) {
1245
0
            nmea_PRN = svId + 192;
1246
0
        } else if (193 > svId) {
1247
            // skip bad svId
1248
0
            return 0;
1249
0
        } else if (202 >= svId) {
1250
0
            nmea_PRN = svId;
1251
0
        }
1252
0
        break;
1253
0
    case 6:
1254
        // GLONASS, 1-32 maps to 65-96
1255
0
        if (32 >= svId) {
1256
0
            nmea_PRN = svId + 64;
1257
0
        } else if (65 > svId) {
1258
            // skip bad svId
1259
0
            return 0;
1260
0
        } else if (96 >= svId) {
1261
0
            nmea_PRN = svId;
1262
0
        } else if (255 == svId) {
1263
            // skip bad svId, 255 == tracked, but unidentified, skip
1264
0
            nmea_PRN = -1;
1265
0
        }
1266
0
        break;
1267
0
    case 7:
1268
        // NavIC (IRNSS), 1 - 14 -> 801 - 814
1269
0
        if (14 >= svId) {
1270
0
            nmea_PRN = svId + 800;;
1271
0
        }
1272
0
        break;
1273
0
    default:
1274
        // Huh?
1275
0
        nmea_PRN = 0;
1276
0
    }
1277
1278
0
    return nmea_PRN;
1279
0
}
1280
1281
// vim: set expandtab shiftwidth=4