Coverage Report

Created: 2025-10-13 07:02

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