Coverage Report

Created: 2025-07-11 06:47

/src/gpsd/gpsd-3.26.2~dev/libgps/gpsutils.c
Line
Count
Source (jump to first uncovered line)
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
/* val2str(val, vlist) - given a value, return a matching string.
122
 *
123
 * val the value to find in vlist
124
 *
125
 * Return: pointer to static string
126
 *         The string matching val, or "Unk".
127
 */
128
const char *val2str(unsigned long val, const struct vlist_t *vlist)
129
0
{
130
0
    while (NULL != vlist->str) {
131
0
        if (vlist->val == val) {
132
0
            return vlist->str;
133
0
        }
134
0
        vlist++;
135
0
    }
136
0
    return "Unk";
137
0
}
138
139
/*
140
 * Berkeley implementation of strtod(), inlined to avoid locale problems
141
 * with the decimal point and stripped down to an atof()-equivalent.
142
 */
143
144
/* Takes a decimal ASCII floating-point number, optionally
145
 * preceded by white space.  Must have form "SI.FE-X",
146
 * S may be ither of the signs may be "+", "-", or omitted.
147
 * I is the integer part of the mantissa,
148
 * F is the fractional part of the mantissa,
149
 * X is the exponent.
150
 * Either I or F may be omitted, or both.
151
 * The decimal point isn't necessary unless F is
152
 * present.  The "E" may actually be an "e".  E and X
153
 * may both be omitted (but not just one).
154
 *
155
 * returns NaN if:
156
 *    *string is zero length,
157
 *    the first non-white space is not negative sign ('-'), positive sign ('_')
158
 *    or a digit
159
 */
160
double safe_atof(const char *string)
161
5.01k
{
162
5.01k
    static int maxExponent = 511;   /* Largest possible base 10 exponent.  Any
163
                                     * exponent larger than this will already
164
                                     * produce underflow or overflow, so there's
165
                                     * no need to worry about additional digits.
166
                                     */
167
    /* Table giving binary powers of 10.  Entry is 10^2^i.
168
     * Used to convert decimal exponents into floating-point numbers. */
169
5.01k
    static double powersOf10[] = {
170
5.01k
        10.,
171
5.01k
        100.,
172
5.01k
        1.0e4,
173
5.01k
        1.0e8,
174
5.01k
        1.0e16,
175
5.01k
        1.0e32,
176
5.01k
        1.0e64,
177
5.01k
        1.0e128,
178
5.01k
        1.0e256
179
5.01k
    };
180
181
5.01k
    bool sign = false, expSign = false;
182
5.01k
    double fraction, dblExp, *d;
183
5.01k
    const char *p;
184
5.01k
    int c;
185
5.01k
    int exp = 0;                // Exponent read from "EX" field.
186
5.01k
    int fracExp = 0;            /* Exponent that derives from the fractional
187
                                 * part.  Under normal circumstatnces, it is
188
                                 * the negative of the number of digits in F.
189
                                 * However, if I is very long, the last digits
190
                                 * of I get dropped (otherwise a long I with a
191
                                 * large negative exponent could cause an
192
                                 * unnecessary overflow on I alone).  In this
193
                                 * case, fracExp is incremented one for each
194
                                 * dropped digit. */
195
5.01k
    int mantSize;               // Number of digits in mantissa.
196
5.01k
    int decPt;                  /* Number of mantissa digits BEFORE decimal
197
                                 * point. */
198
5.01k
    const char *pExp;           /* Temporarily holds location of exponent
199
                                 * in string. */
200
201
    /*
202
     * Strip off leading blanks and check for a sign.
203
     */
204
205
5.01k
    p = string;
206
5.01k
    while (isspace((int)*p)) {
207
0
        p += 1;
208
0
    }
209
5.01k
    if (isdigit((int)*p)) {
210
        // ignore
211
2.79k
    } else if ('-' == *p) {
212
435
        sign = true;
213
435
        p += 1;
214
1.78k
    } else if ('+' == *p) {
215
240
        p += 1;
216
1.54k
    } else if ('.' == *p) {
217
        // ignore
218
868
    } else {
219
674
        return NAN;
220
674
    }
221
222
    /*
223
     * Count the number of digits in the mantissa (including the decimal
224
     * point), and also locate the decimal point.
225
     */
226
227
4.34k
    decPt = -1;
228
20.9k
    for (mantSize = 0; ; mantSize += 1) {
229
20.9k
        c = *p;
230
20.9k
        if (!isdigit((int)c)) {
231
5.42k
            if ((c != '.') || (decPt >= 0)) {
232
4.34k
                break;
233
4.34k
            }
234
1.08k
            decPt = mantSize;
235
1.08k
        }
236
16.5k
        p += 1;
237
16.5k
    }
238
239
    /*
240
     * Now suck up the digits in the mantissa.  Use two integers to
241
     * collect 9 digits each (this is faster than using floating-point).
242
     * If the mantissa has more than 18 digits, ignore the extras, since
243
     * they can't affect the value anyway.
244
     */
245
246
4.34k
    pExp  = p;
247
4.34k
    p -= mantSize;
248
4.34k
    if (decPt < 0) {
249
3.25k
        decPt = mantSize;
250
3.25k
    } else {
251
1.08k
        mantSize -= 1;                  // One of the digits was the point.
252
1.08k
    }
253
4.34k
    if (mantSize > 18) {
254
240
        fracExp = decPt - 18;
255
240
        mantSize = 18;
256
4.10k
    } else {
257
4.10k
        fracExp = decPt - mantSize;
258
4.10k
    }
259
4.34k
    if (mantSize == 0) {
260
1.07k
        fraction = 0.0;
261
        // p = string;
262
1.07k
        goto done;
263
3.27k
    } else {
264
3.27k
        int frac1, frac2;
265
266
3.27k
        frac1 = 0;
267
6.34k
        for ( ; mantSize > 9; mantSize -= 1) {
268
3.07k
            c = *p;
269
3.07k
            p += 1;
270
3.07k
            if ('.' == c) {
271
245
                c = *p;
272
245
                p += 1;
273
245
            }
274
3.07k
            frac1 = 10*frac1 + (c - '0');
275
3.07k
        }
276
3.27k
        frac2 = 0;
277
12.9k
        for (; mantSize > 0; mantSize -= 1) {
278
9.70k
            c = *p;
279
9.70k
            p += 1;
280
9.70k
            if ('.' == c) {
281
227
                c = *p;
282
227
                p += 1;
283
227
            }
284
9.70k
            frac2 = 10*frac2 + (c - '0');
285
9.70k
        }
286
3.27k
        fraction = (1.0e9 * frac1) + frac2;
287
3.27k
    }
288
289
    /*
290
     * Skim off the exponent.
291
     */
292
293
3.27k
    p = pExp;
294
3.27k
    if (('E' == *p) ||
295
3.27k
        ('e' == *p)) {
296
1.73k
        p += 1;
297
1.73k
        if ('-' == *p) {
298
412
            expSign = true;
299
412
            p += 1;
300
1.32k
        } else {
301
1.32k
            if ('+' == *p) {
302
206
                p += 1;
303
206
            }
304
1.32k
            expSign = false;
305
1.32k
        }
306
2.98k
        while (isdigit((int) *p)) {
307
2.98k
            exp = exp * 10 + (*p - '0');
308
2.98k
            if (1024 < exp) {
309
415
                if (true == expSign) {
310
                    // exponent underflow!
311
194
                    return 0.0;
312
194
                } // else  exponent overflow!
313
221
                return INFINITY;
314
415
            }
315
2.57k
            p += 1;
316
2.57k
        }
317
1.73k
    }
318
2.85k
    if (expSign) {
319
218
        exp = fracExp - exp;
320
2.63k
    } else {
321
2.63k
        exp = fracExp + exp;
322
2.63k
    }
323
324
    /*
325
     * Generate a floating-point number that represents the exponent.
326
     * Do this by processing the exponent one bit at a time to combine
327
     * many powers of 2 of 10. Then combine the exponent with the
328
     * fraction.
329
     */
330
331
2.85k
    if (0 > exp) {
332
496
        expSign = true;
333
496
        exp = -exp;
334
2.35k
    } else {
335
2.35k
        expSign = false;
336
2.35k
    }
337
2.85k
    if (exp > maxExponent) {
338
228
        exp = maxExponent;
339
228
        errno = ERANGE;
340
228
    }
341
2.85k
    dblExp = 1.0;
342
7.67k
    for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
343
4.81k
        if (exp & 01) {
344
3.67k
            dblExp *= *d;
345
3.67k
        }
346
4.81k
    }
347
2.85k
    if (expSign) {
348
496
        fraction /= dblExp;
349
2.35k
    } else {
350
2.35k
        fraction *= dblExp;
351
2.35k
    }
352
353
3.92k
done:
354
3.92k
    if (sign) {
355
435
        return -fraction;
356
435
    }
357
3.49k
    return fraction;
358
3.92k
}
359
360
3.35k
#define MONTHSPERYEAR   12      // months per calendar year
361
362
// clear a baseline_t
363
static void gps_clear_base(struct baseline_t *base)
364
0
{
365
0
    base->status = STATUS_UNK;
366
0
    base->east = NAN;
367
0
    base->north = NAN;
368
0
    base->up = NAN;
369
0
    base->length = NAN;
370
0
    base->course = NAN;
371
0
    base->ratio = NAN;
372
0
}
373
374
// stuff a fix structure with recognizable out-of-band values
375
void gps_clear_fix(struct gps_fix_t *fixp)
376
0
{
377
0
    memset(fixp, 0, sizeof(struct gps_fix_t));
378
0
    fixp->altitude = NAN;        // DEPRECATED, undefined
379
0
    fixp->altHAE = NAN;
380
0
    fixp->altMSL = NAN;
381
0
    fixp->climb = NAN;
382
0
    fixp->depth = NAN;
383
0
    fixp->epc = NAN;
384
0
    fixp->epd = NAN;
385
0
    fixp->eph = NAN;
386
0
    fixp->eps = NAN;
387
0
    fixp->ept = NAN;
388
0
    fixp->epv = NAN;
389
0
    fixp->epx = NAN;
390
0
    fixp->epy = NAN;
391
0
    fixp->latitude = NAN;
392
0
    fixp->longitude = NAN;
393
0
    fixp->magnetic_track = NAN;
394
0
    fixp->magnetic_var = NAN;
395
0
    fixp->mode = MODE_NOT_SEEN;
396
0
    fixp->sep = NAN;
397
0
    fixp->speed = NAN;
398
0
    fixp->track = NAN;
399
    // clear ECEF too
400
0
    fixp->ecef.x = NAN;
401
0
    fixp->ecef.y = NAN;
402
0
    fixp->ecef.z = NAN;
403
0
    fixp->ecef.vx = NAN;
404
0
    fixp->ecef.vy = NAN;
405
0
    fixp->ecef.vz = NAN;
406
0
    fixp->ecef.pAcc = NAN;
407
0
    fixp->ecef.vAcc = NAN;
408
0
    fixp->NED.relPosN = NAN;
409
0
    fixp->NED.relPosE = NAN;
410
0
    fixp->NED.relPosD = NAN;
411
0
    fixp->NED.velN = NAN;
412
0
    fixp->NED.velE = NAN;
413
0
    fixp->NED.velD = NAN;
414
0
    fixp->geoid_sep = NAN;
415
0
    fixp->dgps_age = NAN;
416
0
    fixp->dgps_station = -1;
417
0
    fixp->temp = NAN;
418
0
    fixp->wanglem = NAN;
419
0
    fixp->wangler = NAN;
420
0
    fixp->wanglet = NAN;
421
0
    fixp->wspeedr = NAN;
422
0
    fixp->wspeedt = NAN;
423
0
    fixp->wtemp = NAN;
424
0
    gps_clear_base(&fixp->base);
425
0
}
426
427
// stuff an attitude structure with recognizable out-of-band values
428
void gps_clear_att(struct attitude_t *attp)
429
0
{
430
0
    memset(attp, 0, sizeof(struct attitude_t));
431
0
    attp->acc_len = NAN;
432
0
    attp->acc_x = NAN;
433
0
    attp->acc_y = NAN;
434
0
    attp->acc_z = NAN;
435
0
    attp->depth = NAN;
436
0
    attp->dip = NAN;
437
0
    attp->gyro_temp = NAN;
438
0
    attp->gyro_x = NAN;
439
0
    attp->gyro_y = NAN;
440
0
    attp->gyro_z = NAN;
441
0
    attp->heading = NAN;
442
0
    attp->mheading = NAN;
443
0
    attp->mag_len = NAN;
444
0
    attp->mag_x = NAN;
445
0
    attp->mag_y = NAN;
446
0
    attp->mag_z = NAN;
447
0
    attp->pitch = NAN;
448
0
    attp->roll = NAN;
449
0
    attp->rot = NAN;
450
0
    attp->temp = NAN;
451
0
    attp->yaw = NAN;
452
0
    gps_clear_base(&attp->base);
453
0
}
454
455
// Clear a dop_t structure
456
void gps_clear_dop( struct dop_t *dop)
457
0
{
458
0
    dop->xdop = dop->ydop = dop->vdop = dop->tdop = dop->hdop = dop->pdop =
459
0
        dop->gdop = NAN;
460
0
}
461
462
// Clear a gst structure
463
void gps_clear_gst( struct gst_t *gst)
464
0
{
465
0
    memset(&gst->utctime, 0, sizeof(gst->utctime));
466
0
    gst-> rms_deviation =  NAN;
467
0
    gst-> smajor_deviation =  NAN;
468
0
    gst-> sminor_deviation =  NAN;
469
0
    gst-> smajor_orientation =  NAN;
470
0
    gst-> lat_err_deviation =  NAN;
471
0
    gst-> lon_err_deviation =  NAN;
472
0
    gst-> alt_err_deviation =  NAN;
473
0
    gst-> ve_err_deviation =  NAN;
474
0
    gst-> vn_err_deviation =  NAN;
475
0
    gst-> vu_err_deviation =  NAN;
476
0
}
477
478
// stuff a log structure with recognizable out-of-band values
479
void gps_clear_log(struct gps_log_t *logp)
480
0
{
481
0
    memset(logp, 0, sizeof(struct gps_log_t));
482
0
    logp->lon = NAN;
483
0
    logp->lat = NAN;
484
0
    logp->altHAE = NAN;
485
0
    logp->altMSL = NAN;
486
0
    logp->gSpeed = NAN;
487
0
    logp->heading = NAN;
488
0
    logp->tAcc = NAN;
489
0
    logp->hAcc = NAN;
490
0
    logp->vAcc = NAN;
491
0
    logp->sAcc = NAN;
492
0
    logp->headAcc = NAN;
493
0
    logp->velN = NAN;
494
0
    logp->velE = NAN;
495
0
    logp->velD = NAN;
496
0
    logp->pDOP = NAN;
497
0
    logp->distance = NAN;
498
0
    logp->totalDistance = NAN;
499
0
    logp->distanceStd = NAN;
500
0
    logp->fixType = -1;
501
0
}
502
503
/* merge new data (from) into current fix (to)
504
 * Being careful not to lose information */
505
void gps_merge_fix(struct gps_fix_t *to,
506
                   gps_mask_t transfer,
507
                   struct gps_fix_t *from)
508
0
{
509
0
    if ((NULL == to) ||
510
0
        (NULL == from)) {
511
0
        return;
512
0
    }
513
0
    if (0 != (transfer & TIME_SET)) {
514
0
        to->time = from->time;
515
0
    }
516
0
    if (0 != (transfer & LATLON_SET)) {
517
0
        to->latitude = from->latitude;
518
0
        to->longitude = from->longitude;
519
0
    }
520
0
    if (0 != (transfer & MODE_SET)) {
521
        // FIXME?  Maybe only upgrade mode, not downgrade it
522
0
        to->mode = from->mode;
523
0
    }
524
    /* Some messages only report mode, some mode and status, some only status.
525
     * Only upgrade status, not downgrade it */
526
0
    if (0 != (transfer & STATUS_SET)) {
527
0
        if (to->status < from->status) {
528
0
            to->status = from->status;
529
0
        }
530
0
    }
531
0
    if ((transfer & ALTITUDE_SET) != 0) {
532
0
        if (0 != isfinite(from->altHAE)) {
533
0
            to->altHAE = from->altHAE;
534
0
        }
535
0
        if (0 != isfinite(from->altMSL)) {
536
0
            to->altMSL = from->altMSL;
537
0
        }
538
0
        if (0 != isfinite(from->depth)) {
539
0
            to->depth = from->depth;
540
0
        }
541
0
    }
542
0
    if (0 != (transfer & TRACK_SET)) {
543
0
        to->track = from->track;
544
0
    }
545
0
    if (0 != (transfer & MAGNETIC_TRACK_SET)) {
546
0
        if (0 != isfinite(from->magnetic_track)) {
547
0
            to->magnetic_track = from->magnetic_track;
548
0
        }
549
0
        if (0 != isfinite(from->magnetic_var)) {
550
0
            to->magnetic_var = from->magnetic_var;
551
0
        }
552
0
    }
553
0
    if (0 != (transfer & SPEED_SET)) {
554
0
        to->speed = from->speed;
555
0
    }
556
0
    if (0 != (transfer & CLIMB_SET)) {
557
0
        to->climb = from->climb;
558
0
    }
559
0
    if (0 != (transfer & TIMERR_SET)) {
560
0
        to->ept = from->ept;
561
0
    }
562
0
    if (0 != isfinite(from->epx) &&
563
0
        0 != isfinite(from->epy)) {
564
0
        to->epx = from->epx;
565
0
        to->epy = from->epy;
566
0
    }
567
0
    if (0 != isfinite(from->epd)) {
568
0
        to->epd = from->epd;
569
0
    }
570
0
    if (0 != isfinite(from->eph)) {
571
0
        to->eph = from->eph;
572
0
    }
573
0
    if (0 != isfinite(from->eps)) {
574
0
        to->eps = from->eps;
575
0
    }
576
    // spherical error probability, not geoid separation
577
0
    if (0 != isfinite(from->sep)) {
578
0
        to->sep = from->sep;
579
0
    }
580
    // geoid separation, not spherical error probability
581
0
    if (0 != isfinite(from->geoid_sep)) {
582
0
        to->geoid_sep = from->geoid_sep;
583
0
    }
584
0
    if (0 != isfinite(from->epv)) {
585
0
        to->epv = from->epv;
586
0
    }
587
0
    if (0 != (transfer & SPEEDERR_SET)) {
588
0
        to->eps = from->eps;
589
0
    }
590
0
    if (0 != (transfer & ECEF_SET)) {
591
0
        to->ecef.x = from->ecef.x;
592
0
        to->ecef.y = from->ecef.y;
593
0
        to->ecef.z = from->ecef.z;
594
0
        to->ecef.pAcc = from->ecef.pAcc;
595
0
    }
596
0
    if (0 != (transfer & VECEF_SET)) {
597
0
        to->ecef.vx = from->ecef.vx;
598
0
        to->ecef.vy = from->ecef.vy;
599
0
        to->ecef.vz = from->ecef.vz;
600
0
        to->ecef.vAcc = from->ecef.vAcc;
601
0
    }
602
0
    if (0 != (transfer & NED_SET)) {
603
0
        to->NED.relPosN = from->NED.relPosN;
604
0
        to->NED.relPosE = from->NED.relPosE;
605
0
        to->NED.relPosD = from->NED.relPosD;
606
0
        if ((0 != isfinite(from->NED.relPosH)) &&
607
0
            (0 != isfinite(from->NED.relPosL))) {
608
0
            to->NED.relPosH = from->NED.relPosH;
609
0
            to->NED.relPosL = from->NED.relPosL;
610
0
        }
611
0
    }
612
0
    if (0 != (transfer & VNED_SET)) {
613
0
        to->NED.velN = from->NED.velN;
614
0
        to->NED.velE = from->NED.velE;
615
0
        to->NED.velD = from->NED.velD;
616
0
    }
617
0
    if ('\0' != from->datum[0]) {
618
0
        strlcpy(to->datum, from->datum, sizeof(to->datum));
619
0
    }
620
0
    if (0 != isfinite(from->dgps_age) &&
621
0
        0 <= from->dgps_station) {
622
        // both, or neither
623
0
        to->dgps_age = from->dgps_age;
624
0
        to->dgps_station = from->dgps_station;
625
0
    }
626
627
0
    if (ANT_UNK != from->ant_stat) {
628
0
        to->ant_stat = from->ant_stat;
629
0
    }
630
0
    if (0 < from->jam) {
631
0
        to->jam = from->jam;
632
0
    }
633
    // navdata stuff.  just wind angle and angle for now
634
0
    if (0 != (transfer & NAVDATA_SET)) {
635
0
        if (0 != isfinite(from->wanglem)) {
636
0
            to->wanglem = from->wanglem;
637
0
        }
638
0
        if (0 != isfinite(from->wangler)) {
639
0
            to->wangler = from->wangler;
640
0
        }
641
0
        if (0 != isfinite(from->wanglet)) {
642
0
            to->wanglet = from->wanglet;
643
0
        }
644
0
        if (0 != isfinite(from->wspeedr)) {
645
0
            to->wspeedr = from->wspeedr;
646
0
        }
647
0
        if (0 != isfinite(from->wspeedt)) {
648
0
            to->wspeedt = from->wspeedt;
649
0
        }
650
0
    }
651
0
    if (0 != isfinite(from->temp)) {
652
0
        to->temp = from->temp;
653
0
    }
654
0
    if (0 != isfinite(from->wtemp)) {
655
0
        to->wtemp = from->wtemp;
656
0
    }
657
0
}
658
659
/* mkgmtime(tm)
660
 * convert struct tm, as UTC, to seconds since Unix epoch
661
 * This differs from mktime() from libc.
662
 * mktime() takes struct tm as localtime.
663
 *
664
 * The inverse of gmtime(time_t)
665
 *
666
 * Return: -1 on error, set errno to EOVERFLOW
667
 */
668
time_t mkgmtime(struct tm * t)
669
2.47k
{
670
2.47k
    int year;
671
2.47k
    time_t result;
672
2.47k
    static const int cumdays[MONTHSPERYEAR] =
673
2.47k
        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
674
675
    // check ranges, ignore tm_isdst and max tm_year
676
2.47k
    if (0 > t->tm_sec ||
677
2.47k
        0 > t->tm_min ||
678
2.47k
        0 > t->tm_hour ||
679
2.47k
        1 > t->tm_mday ||
680
2.47k
        0 > t->tm_mon ||
681
2.47k
        0 > t->tm_year ||
682
2.47k
        0 > t->tm_wday ||
683
2.47k
        0 > t->tm_yday ||
684
2.47k
        61 < t->tm_sec ||
685
2.47k
        59 < t->tm_min ||
686
2.47k
        23 < t->tm_hour ||
687
2.47k
        31 < t->tm_mday ||
688
2.47k
        11 < t->tm_mon ||
689
2.47k
        6 < t->tm_wday ||
690
2.47k
        365 < t->tm_yday) {
691
1.11k
        errno = EOVERFLOW;
692
1.11k
        return -1;
693
1.11k
    }
694
1.36k
    errno = 0;
695
1.36k
    year = 1900 + t->tm_year + t->tm_mon / MONTHSPERYEAR;
696
1.36k
    result = (year - 1970) * 365 + cumdays[t->tm_mon % MONTHSPERYEAR];
697
1.36k
    result += (year - 1968) / 4;
698
1.36k
    result -= (year - 1900) / 100;
699
1.36k
    result += (year - 1600) / 400;
700
1.36k
    if (0 == (year % 4) &&
701
1.36k
        (0 != (year % 100) ||
702
825
         0 == (year % 400)) &&
703
1.36k
        (2 > (t->tm_mon % MONTHSPERYEAR))) {
704
406
        result--;
705
406
    }
706
1.36k
    result += t->tm_mday - 1;
707
1.36k
    result *= 24;
708
1.36k
    result += t->tm_hour;
709
1.36k
    result *= 60;
710
1.36k
    result += t->tm_min;
711
1.36k
    result *= 60;
712
1.36k
    result += t->tm_sec;
713
    /* this is UTC, no DST
714
     * if (t->tm_isdst == 1)
715
     * result -= 3600;
716
     */
717
1.36k
    return result;
718
2.47k
}
719
720
// ISO8601 UTC to Unix timespec, no leapsecond correction.
721
timespec_t iso8601_to_timespec(const char *isotime)
722
2.47k
{
723
2.47k
    timespec_t ret;
724
725
2.47k
#ifndef __clang_analyzer__
726
#ifdef USE_QT
727
    double usec = 0;
728
729
    QString t(isotime);
730
    QDateTime d = QDateTime::fromString(isotime, Qt::ISODate);
731
    QStringList sl = t.split(".");
732
    if (1 < sl.size()) {
733
        usec = sl[1].toInt() / pow(10., (double)sl[1].size());
734
    }
735
    ret.tv_sec = d.toTime_t();
736
    ret.tv_nsec = usec * 1e9;
737
#else  // USE_QT
738
2.47k
    double usec = 0;
739
2.47k
    struct tm tm = {0};
740
741
2.47k
    {
742
2.47k
        char *dp = NULL;
743
2.47k
        dp = strptime(isotime, "%Y-%m-%dT%H:%M:%S", &tm);
744
2.47k
        if (NULL != dp &&
745
2.47k
            '.' == *dp) {
746
194
            usec = strtod(dp, NULL);
747
194
        }
748
2.47k
    }
749
750
    /*
751
     * It would be nice if we could say mktime(&tm) - timezone + usec instead,
752
     * but timezone is not available at all on some BSDs. Besides, when working
753
     * with historical dates the value of timezone after an ordinary tzset(3)
754
     * can be wrong; you have to do a redirect through the IANA historical
755
     * timezone database to get it right.
756
     */
757
2.47k
    ret.tv_sec = mkgmtime(&tm);
758
2.47k
    ret.tv_nsec = usec * 1e9;
759
2.47k
#endif  // USE_QT
760
2.47k
#endif  // __clang_analyzer__
761
762
2.47k
#if 4 < SIZEOF_TIME_T
763
2.47k
    if (253402300799LL < ret.tv_sec) {
764
        // enforce max "9999-12-31T23:59:59.999Z"
765
194
        ret.tv_sec = 253402300799LL;
766
194
    }
767
2.47k
#endif
768
2.47k
    return ret;
769
2.47k
}
770
771
/* Convert POSIX timespec to ISO8601 UTC, put result in isotime.
772
 * no timezone adjustment
773
 * Return: pointer to isotime.
774
 * example: 2007-12-11T23:38:51.033Z */
775
char *timespec_to_iso8601(timespec_t fixtime, char isotime[], size_t len)
776
0
{
777
0
    struct tm when;
778
0
    char timestr[30];
779
0
    long fracsec;
780
781
0
    if (0 > fixtime.tv_sec) {
782
        // Allow 0 for testing of 1970-01-01T00:00:00.000Z
783
0
        strlcpy(isotime, "NaN", len);
784
0
        return isotime;
785
0
    }
786
0
    if (999499999 < fixtime.tv_nsec) {
787
        // round up
788
0
        fixtime.tv_sec++;
789
0
        fixtime.tv_nsec = 0;
790
0
    }
791
792
0
#if 4 < SIZEOF_TIME_T
793
0
    if (253402300799LL < fixtime.tv_sec) {
794
        // enforce max "9999-12-31T23:59:59.999Z"
795
0
        fixtime.tv_sec = 253402300799LL;
796
0
    }
797
0
#endif
798
799
0
    (void)gmtime_r(&fixtime.tv_sec, &when);
800
801
    /*
802
     * Do not mess casually with the number of decimal digits in the
803
     * format!  Most GPSes report over serial links at 0.01s or 0.001s
804
     * precision.  Round to 0.001s
805
     */
806
0
    fracsec = (fixtime.tv_nsec + 500000) / 1000000;
807
808
0
    (void)strftime(timestr, sizeof(timestr), "%Y-%m-%dT%H:%M:%S", &when);
809
0
    (void)snprintf(isotime, len, "%s.%03ldZ",timestr, fracsec);
810
811
0
    return isotime;
812
0
}
813
814
/* return time now as ISO8601, no timezone adjustment
815
 * example: 2007-12-11T23:38:51.033Z */
816
char *now_to_iso8601(char *tbuf, size_t tbuf_sz)
817
0
{
818
0
    timespec_t ts_now;
819
820
0
    (void)clock_gettime(CLOCK_REALTIME, &ts_now);
821
0
    return timespec_to_iso8601(ts_now, tbuf, tbuf_sz);
822
0
}
823
824
0
#define Deg2Rad(n)      ((n) * DEG_2_RAD)
825
826
/* Distance in meters between two points specified in degrees, optionally
827
 * with initial and final bearings. */
828
double earth_distance_and_bearings(double lat1, double lon1,
829
                                   double lat2, double lon2,
830
                                   double *ib, double *fb)
831
0
{
832
    /*
833
     * this is a translation of the javascript implementation of the
834
     * Vincenty distance formula by Chris Veness. See
835
     * http://www.movable-type.co.uk/scripts/latlong-vincenty.html
836
     */
837
0
    double a, b, f;             // WGS-84 ellipsoid params
838
0
    double L, L_P, U1, U2, s_U1, c_U1, s_U2, c_U2;
839
0
    double uSq, A, B, d_S, lambda;
840
    // cppcheck-suppress variableScope
841
0
    double s_L, c_L, s_A, C;
842
0
    double c_S, S, s_S, c_SqA, c_2SM;
843
0
    int i = 100;
844
845
0
    a = WGS84A;
846
0
    b = WGS84B;
847
0
    f = 1 / WGS84F;
848
0
    L = Deg2Rad(lon2 - lon1);
849
0
    U1 = atan((1 - f) * tan(Deg2Rad(lat1)));
850
0
    U2 = atan((1 - f) * tan(Deg2Rad(lat2)));
851
0
    s_U1 = sin(U1);
852
0
    c_U1 = cos(U1);
853
0
    s_U2 = sin(U2);
854
0
    c_U2 = cos(U2);
855
0
    lambda = L;
856
857
0
    do {
858
0
        s_L = sin(lambda);
859
0
        c_L = cos(lambda);
860
0
        s_S = sqrt((c_U2 * s_L) * (c_U2 * s_L) +
861
0
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L) *
862
0
                   (c_U1 * s_U2 - s_U1 * c_U2 * c_L));
863
864
0
        if (0 == s_S) {
865
0
            return 0;
866
0
        }
867
868
0
        c_S = s_U1 * s_U2 + c_U1 * c_U2 * c_L;
869
0
        S = atan2(s_S, c_S);
870
0
        s_A = c_U1 * c_U2 * s_L / s_S;
871
0
        c_SqA = 1 - s_A * s_A;
872
0
        c_2SM = c_S - 2 * s_U1 * s_U2 / c_SqA;
873
874
0
        if (0 == isfinite(c_2SM)) {
875
0
            c_2SM = 0;
876
0
        }
877
878
0
        C = f / 16 * c_SqA * (4 + f * (4 - 3 * c_SqA));
879
0
        L_P = lambda;
880
0
        lambda = L + (1 - C) * f * s_A *
881
0
            (S + C * s_S * (c_2SM + C * c_S * (2 * c_2SM * c_2SM - 1)));
882
0
    } while ((fabs(lambda - L_P) > 1.0e-12) &&
883
0
             (0 < --i));
884
885
0
    if (0 == i) {
886
0
        return NAN;             // formula failed to converge
887
0
    }
888
889
0
    uSq = c_SqA * ((a * a) - (b * b)) / (b * b);
890
0
    A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
891
0
    B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
892
0
    d_S = B * s_S * (c_2SM + B / 4 *
893
0
                     (c_S * (-1 + 2 * c_2SM * c_2SM) - B / 6 * c_2SM *
894
0
                      (-3 + 4 * s_S * s_S) * (-3 + 4 * c_2SM * c_2SM)));
895
896
0
    if (NULL != ib) {
897
0
        *ib = atan2(c_U2 * sin(lambda),
898
0
                    c_U1 * s_U2 - s_U1 * c_U2 * cos(lambda));
899
0
    }
900
0
    if (NULL != fb) {
901
0
        *fb = atan2(c_U1 * sin(lambda),
902
0
                    c_U1 * s_U2 * cos(lambda) - s_U1 * c_U2);
903
0
    }
904
905
0
    return (WGS84B * A * (S - d_S));
906
0
}
907
908
// Distance in meters between two points specified in degrees.
909
double earth_distance(double lat1, double lon1, double lat2, double lon2)
910
0
{
911
0
    return earth_distance_and_bearings(lat1, lon1, lat2, lon2, NULL, NULL);
912
0
}
913
914
/* Wait for data until timeout, ignoring signals.
915
 *
916
 * pselect() may set errno on error
917
 */
918
bool nanowait(int fd, struct timespec *to)
919
0
{
920
0
    fd_set fdset;
921
922
0
    FD_ZERO(&fdset);
923
0
    FD_SET(fd, &fdset);
924
0
    TS_NORM(to);         // just in case
925
0
    errno = 0;
926
    // sigmask is NULL, so equivalent to select()
927
0
    return pselect(fd + 1, &fdset, NULL, NULL, to, NULL) == 1;
928
0
}
929
930
/* Accept a datum code, return matching string
931
 *
932
 * There are a ton of these, only a few are here
933
 *
934
 */
935
void datum_code_string(int code, char *buffer, size_t len)
936
0
{
937
0
    const char *datum_str;
938
939
0
    switch (code) {
940
0
    case 0:
941
0
        datum_str = "WGS84";
942
0
        break;
943
0
    case 21:
944
0
        datum_str = "WGS84";
945
0
        break;
946
0
    case 178:
947
0
        datum_str = "Tokyo Mean";
948
0
        break;
949
0
    case 179:
950
0
        datum_str = "Tokyo-Japan";
951
0
        break;
952
0
    case 180:
953
0
        datum_str = "Tokyo-Korea";
954
0
        break;
955
0
    case 181:
956
0
        datum_str = "Tokyo-Okinawa";
957
0
        break;
958
0
    case 182:
959
0
        datum_str = "PZ90.11";
960
0
        break;
961
0
    case 999:
962
0
        datum_str = "User Defined";
963
0
        break;
964
0
    default:
965
0
        datum_str = NULL;
966
0
        break;
967
0
    }
968
969
0
    if (NULL == datum_str) {
970
        // Fake it
971
0
        snprintf(buffer, len, "%d", code);
972
0
    } else {
973
0
        strlcpy(buffer, datum_str, len);
974
0
    }
975
0
}
976
977
/* make up an NMEA 4.0 (extended) PRN based on gnssId:svId,
978
 * This does NOT match NMEA 4.10 and 4.11 where all PRN are 1-99,
979
 * except IMES, QZSS, and some SBAS.
980
 *
981
 * Ref Appendix A from u-blox ZED-F9P Interface Description
982
 * and
983
 * Section 1.5.3  M10-FW500_InterfaceDescription_UBX-20053845.pdf
984
 *
985
 * Return PRN, less than one for error
986
 *        -1 for GLONASS svid 255
987
 */
988
short ubx2_to_prn(int gnssId, int svId)
989
0
{
990
0
    short nmea_PRN = 0;
991
992
0
    if (1 > svId) {
993
        // skip 0 svId
994
0
        return 0;
995
0
    }
996
997
0
    switch (gnssId) {
998
0
    case 0:
999
        // GPS, 1-32 maps to 1-32
1000
0
        if (32 < svId) {
1001
            // skip bad svId
1002
0
            return 0;
1003
0
        }
1004
0
        nmea_PRN = svId;
1005
0
        break;
1006
0
    case 1:
1007
        // SBAS, 120..151, 152..158 maps to 33..64, 152..158
1008
0
        if (120 > svId) {
1009
            // Huh?
1010
0
            return 0;
1011
0
        } else if (151 >= svId) {
1012
0
            nmea_PRN = svId - 87;
1013
0
        } else if (158 >= svId) {
1014
0
            nmea_PRN = svId;
1015
0
        } else {
1016
            // Huh?
1017
0
            return 0;
1018
0
        }
1019
0
        break;
1020
0
    case 2:
1021
        // Galileo, ubx gnssid:svid 1..36 ->  301-336
1022
        // Galileo, ubx PRN         211..246 ->  301-336
1023
0
        if (36 >= svId) {
1024
0
            nmea_PRN = svId + 300;
1025
0
        } else if (211 > svId) {
1026
            // skip bad svId
1027
0
            return 0;
1028
0
        } else if (246 >= svId) {
1029
0
            nmea_PRN = svId + 90;
1030
0
        } else {
1031
            // skip bad svId
1032
0
            return 0;
1033
0
        }
1034
0
        break;
1035
0
    case 3:
1036
        /* BeiDou, ubx gnssid:svid    1..37 -> to 401-437
1037
         * have seen 1-63 on F10 ProtVer 40.0, March 2025
1038
         *   ubx gnssid:svid    1..63 -> to 401-463
1039
         * BeiDou, ubx "single svid"  159..163,33..64 -> to 401-437 ?? */
1040
0
        if (63 >= svId) {
1041
0
            nmea_PRN = svId + 400;
1042
0
        } else if (159 > svId) {
1043
            // skip bad svId
1044
0
            return 0;
1045
0
        } else if (163 >= svId) {
1046
0
            nmea_PRN = svId + 242;
1047
0
        } else {
1048
            // skip bad svId
1049
0
            return 0;
1050
0
        }
1051
0
        break;
1052
0
    case 4:
1053
        // IMES, ubx gnssid:svid 1-10 -> to 173-182
1054
        // IMES, ubx PRN         173-182 to 173-182
1055
0
        if (10 >= svId) {
1056
0
            nmea_PRN = svId + 172;
1057
0
        } else if (173 > svId) {
1058
            // skip bad svId
1059
0
            return 0;
1060
0
        } else if (182 >= svId) {
1061
0
            nmea_PRN = svId;
1062
0
        } else {
1063
            // > 182, skip bad svId
1064
0
            return 0;
1065
0
        }
1066
0
        break;
1067
0
    case 5:
1068
        // QZSS, ubx gnssid:svid 1-10 to 193-202
1069
        // QZSS, ubx PRN         193-202 to 193-202
1070
0
        if (10 >= svId) {
1071
0
            nmea_PRN = svId + 192;
1072
0
        } else if (193 > svId) {
1073
            // skip bad svId
1074
0
            return 0;
1075
0
        } else if (202 >= svId) {
1076
0
            nmea_PRN = svId;
1077
0
        } else {
1078
            // skip bad svId
1079
0
            return 0;
1080
0
        }
1081
0
        break;
1082
0
    case 6:
1083
        // GLONASS, 1-32 maps to 65-96
1084
0
        if (32 >= svId) {
1085
0
            nmea_PRN = svId + 64;
1086
0
        } else if (65 > svId) {
1087
            // skip bad svId
1088
0
            return 0;
1089
0
        } else if (96 >= svId) {
1090
0
            nmea_PRN = svId;
1091
0
        } else if (255 == svId) {
1092
            // skip bad svId, 255 == tracked, but unidentified, skip
1093
0
            nmea_PRN = -1;
1094
0
        } else {
1095
0
            return 0;
1096
0
        }
1097
0
        break;
1098
0
    case 7:
1099
        // NavIC (IRNSS)
1100
0
        FALLTHROUGH
1101
0
    default:
1102
        // Huh?
1103
0
        return 0;
1104
0
    }
1105
1106
0
    return nmea_PRN;
1107
0
}
1108
1109
// vim: set expandtab shiftwidth=4