Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gpsd/gpsd-3.28.1~dev/drivers/driver_sirf.c
Line
Count
Source
1
/*
2
 * This is the gpsd driver for SiRF GPSes operating in binary mode.
3
 * It also handles early u-bloxes that were SiRF derivatives.
4
 *
5
 * The advantages: Reports climb/sink rate (raw-mode clients won't see this).
6
 * Also, we can flag DGPS satellites used in the skyview when SBAS is in use.
7
 * The disadvantages: Doesn't return PDOP or VDOP, just HDOP.
8
 *
9
 * Chris Kuethe, our SiRF expert, tells us:
10
 *
11
 * "I don't see any indication in any of my material that PDOP, GDOP
12
 * or VDOP are output. There are quantities called Estimated
13
 * {Horizontal Position, Vertical Position, Time, Horizontal Velocity}
14
 * Error, but those are apparently only valid when SiRFDRive is
15
 * active."
16
 *
17
 * "(SiRFdrive is their Dead Reckoning augmented firmware. It
18
 * allows you to feed odometer ticks, gyro and possibly
19
 * accelerometer inputs to the chip to allow it to continue
20
 * to navigate in the absence of satellite information, and
21
 * to improve fixes when you do have satellites.)"
22
 *
23
 * "[When we need RINEX data, we can get it from] SiRF Message #5.
24
 *  If it's no longer implemented on your receiver, messages
25
 * 7, 28, 29 and 30 will give you the same information."
26
 *
27
 * There is a known problem with the SiRF IV: it is prone to freeze
28
 * when being switched back to NMEA mode from SiRF binary. The
29
 * failure is randomly flaky, you may get away with several mode
30
 * flips before triggering it.  Powering off the device resets and
31
 * unfreezes it. We have tries waiting on command acknowledges as
32
 * the manual advises; this does not fix the problem.
33
 *
34
 * This file is Copyright 2010 by the GPSD project
35
 * SPDX-License-Identifier: BSD-2-clause
36
 */
37
38
#include "../include/gpsd_config.h"   // must be before all includes
39
40
#include <math.h>
41
#include <stdbool.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <strings.h>
46
#include <unistd.h>
47
48
#include "../include/compiler.h"   // for FALLTHROUGH
49
#include "../include/gpsd.h"
50
#include "../include/bits.h"
51
#include "../include/strfuncs.h"
52
#include "../include/timespec.h"
53
54
#if defined(SIRF_ENABLE)
55
56
1
#define HI(n)           ((n) >> 8)
57
1
#define LO(n)           ((n) & 0xff)
58
59
/*
60
 * According to the protocol reference, if you don't get ACK/NACK in response
61
 * to a control send within 6 seconds, you should just retry.
62
 */
63
#define SIRF_RETRY_TIME 6
64
65
// Poll Software Version MID 132
66
    static unsigned char versionprobe[] = {
67
        0xa0, 0xa2, 0x00, 0x02,
68
        0x84,           // MID 132
69
        0x00,           // unused
70
        0x00, 0x00, 0xb0, 0xb3
71
    };
72
73
// Poll Navigation Parameters MID 152, query for MID 19
74
    static unsigned char navparams[] = {
75
        0xa0, 0xa2, 0x00, 0x02,
76
        0x98,           // MID 152
77
        0x00,
78
        0x00, 0x00, 0xb0, 0xb3
79
    };
80
81
// DGPS Source MID 133
82
    static unsigned char dgpscontrol[] = {
83
        0xa0, 0xa2, 0x00, 0x07,
84
        0x85,           // MID 133
85
        0x01,           // use SBAS
86
        0x00, 0x00,
87
        0x00, 0x00, 0x00,
88
        0x00, 0x00, 0xb0, 0xb3
89
    };
90
91
// Set SBAS Parameters MID 170
92
    static unsigned char sbasparams[] = {
93
        0xa0, 0xa2, 0x00, 0x06,
94
        0xaa,           // MID 170
95
        0x00,           // SBAS PRN
96
        0x01,           // SBAS Mode
97
        0x00,           // Auto PRN
98
        0x00, 0x00,
99
        0x00, 0x00, 0xb0, 0xb3
100
    };
101
102
// Set Message Rate MID 166
103
    static unsigned char requestecef[] = {
104
        0xa0, 0xa2, 0x00, 0x08,
105
        0xa6,                   // MID 166
106
        0x00,                   // enable 1
107
        0x02,                   // MID 2
108
        0x01,                   // once per Sec
109
        0x00, 0x00,             // unused
110
        0x00, 0x00,             // unused
111
        0x00, 0x00, 0xb0, 0xb3
112
    };
113
114
// Set Message Rate MID 166
115
    static unsigned char requesttracker[] = {
116
        0xa0, 0xa2, 0x00, 0x08,
117
        0xa6,                   // MID 166
118
        0x00,                   // enable 1
119
        0x04,                   // MID 4
120
        0x03,                   // every 3 sec
121
        0x00, 0x00,             // unused
122
        0x00, 0x00,             // unused
123
        0x00, 0x00, 0xb0, 0xb3
124
    };
125
126
// disable MID XX
127
    static unsigned char unsetmidXX[] = {
128
        0xa0, 0xa2, 0x00, 0x08,
129
        0xa6,                   // MID 166
130
        0x00,                   // enable XX
131
        0x00,                   // MID 0xXX
132
        0x00,                   // rate: never
133
        0x00, 0x00,             // reserved
134
        0x00, 0x00,             // reserved
135
        0x00, 0x00, 0xb0, 0xb3
136
    };
137
138
/* message to enable:
139
 *   MID 7 Clock Status
140
 *   MID 8 50Bps subframe data
141
 *   MID 17 Differential  Corrections
142
 *   MID 28 Nav Lib Measurement Data
143
 *   MID 29 Nav Lib DGPS Data
144
 *   MID 30 Nav Lib SV State Data
145
 *   MID 31 Nav Lib Initialization data
146
 * at 1Hz rate */
147
    static unsigned char enablesubframe[] = {
148
        0xa0, 0xa2, 0x00, 0x19,
149
        0x80,                   // MID 128 initialize Data Source
150
        0x00, 0x00, 0x00, 0x00, // EXEF X
151
        0x00, 0x00, 0x00, 0x00, // ECEF Y
152
        0x00, 0x00, 0x00, 0x00, // ECEF Z
153
        0x00, 0x00, 0x00, 0x00, // clock drift
154
        0x00, 0x00, 0x00, 0x00, // time of week
155
        0x00, 0x00,             // week number
156
        0x0C,                   // Chans 1-12
157
        /* change the next 0x10 to 0x08 for factory reset
158
         * 0x10 turns on MIDs 7, 8, 17, 28, 29, 30 and 31 */
159
        0x10,
160
        0x00, 0x00, 0xb0, 0xb3
161
    };
162
163
// disable subframe data
164
    static unsigned char disablesubframe[] = {
165
        0xa0, 0xa2, 0x00, 0x19,
166
        0x80,                   // MID 128 initialize Data Source
167
        0x00, 0x00, 0x00, 0x00, // EXEF X
168
        0x00, 0x00, 0x00, 0x00, // ECEF Y
169
        0x00, 0x00, 0x00, 0x00, // ECEF Z
170
        0x00, 0x00, 0x00, 0x00, // clock drift
171
        0x00, 0x00, 0x00, 0x00, // time of week
172
        0x00, 0x00,             // week number
173
        0x0C,                   // Chans 1-12
174
175
        // 0x00 turns off MIDs 7, 8, 17, 28, 29, 30 and 31
176
        0x00,                   // reset bit map
177
178
        0x00, 0x00, 0xb0, 0xb3
179
    };
180
181
// mode control MID
182
    static unsigned char modecontrol[] = {
183
        0xa0, 0xa2, 0x00, 0x0e,
184
        0x88,                   // MID 136 Mode Control
185
        0x00, 0x00,             // pad bytes
186
        0x00,                   // degraded mode off
187
        0x00, 0x00,             // pad bytes
188
        0x00, 0x00,             // altitude
189
        0x00,                   // altitude hold auto
190
        0x00,                   // use last computed alt
191
        0x00,                   // reserved
192
        0x00,                   // disable degraded mode
193
        0x00,                   // disable dead reckoning
194
        0x01,                   // enable track smoothing
195
        0x00, 0x00, 0xb0, 0xb3
196
    };
197
198
// enable 1 PPS Time MID 52, using Set Message Rate MID 166
199
    static unsigned char enablemid52[] = {
200
        0xa0, 0xa2, 0x00, 0x08,
201
        0xa6,                   // MID 166
202
        0x00,                   // enable/disable one message
203
        0x34,                   // MID 52
204
        0x01,                   // sent once per second
205
        0x00, 0x00, 0x00, 0x00, // reserved, set to zero
206
        0x00, 0xdb, 0xb0, 0xb3
207
    };
208
209
210
    static gps_mask_t sirf_msg_debug(struct gps_device_t *,
211
                                     unsigned char *, size_t);
212
    static gps_mask_t sirf_msg_errors(struct gps_device_t *,
213
                                      unsigned char *, size_t);
214
    static gps_mask_t sirf_msg_navdata(struct gps_device_t *, unsigned char *,
215
                                       size_t);
216
    static gps_mask_t sirf_msg_navsol(struct gps_device_t *, unsigned char *,
217
                                      size_t);
218
    static gps_mask_t sirf_msg_nlmd(struct gps_device_t *, unsigned char *,
219
                                    size_t);
220
    static gps_mask_t sirf_msg_ppstime(struct gps_device_t *, unsigned char *,
221
                                       size_t);
222
    static gps_mask_t sirf_msg_nl(struct gps_device_t *, unsigned char *,
223
                                  size_t);
224
    static gps_mask_t sirf_msg_ee(struct gps_device_t *, unsigned char *,
225
                                  size_t);
226
    static gps_mask_t sirf_msg_svinfo(struct gps_device_t *, unsigned char *,
227
                                      size_t);
228
    static gps_mask_t sirf_msg_swversion(struct gps_device_t *, unsigned char *,
229
                                         size_t);
230
    static gps_mask_t sirf_msg_sysparam(struct gps_device_t *, unsigned char *,
231
                                        size_t);
232
    static gps_mask_t sirf_msg_dgpsstatus(struct gps_device_t *, unsigned char *,
233
                                          size_t);
234
    static gps_mask_t sirf_msg_ublox(struct gps_device_t *, unsigned char *,
235
                                     size_t);
236
237
238
    static bool sirf_write(struct gps_device_t *session, unsigned char *msg,
239
                           size_t msg_size)
240
2.26k
    {
241
2.26k
        unsigned int crc;
242
2.26k
        size_t i, len;
243
2.26k
        bool ok;
244
2.26k
        unsigned int type;
245
246
        // do not write if -b (readonly) option set
247
2.26k
        if (session->context->readonly) {
248
1
            return true;
249
1
        }
250
2.26k
        if (10 > msg_size) {
251
            // message is smaller than minimum size
252
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
253
0
                     "SiRF: sirf_write() msg too small!\n");
254
0
            return false;
255
0
        }
256
2.26k
        type = msg[4];
257
258
        /*
259
         * Control strings spaced too closely together confuse the SiRF
260
         * IV.  This wasn't an issue on older SiRFs, but they've gone to a
261
         * lower-powered processor that apparently has trouble keeping up.
262
         * Now you have to wait for the ACK, otherwise chaos ensues.
263
         * Add instrumentation to reveal when this may happen.
264
         */
265
        // can also be false because ACK was received after last send
266
2.26k
        if (0 < session->driver.sirf.need_ack) {
267
503
            GPSD_LOG(LOG_WARN, &session->context->errout,
268
503
                     "SiRF: warning, write of MID %#02x while "
269
503
                     "awaiting ACK for %#02x.\n",
270
503
                     type, session->driver.sirf.need_ack);
271
503
        }
272
273
2.26k
        len = (size_t)((msg[2] << 8) | msg[3]);
274
2.26k
        if ((len + 8) > msg_size) {
275
            // message is smaller than len + leader + trailer
276
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
277
0
                     "SiRF: sirf_write() msg less than %zu!\n", len + 8);
278
0
            return false;
279
0
        }
280
        // calculate CRC
281
2.26k
        crc = 0;
282
14.9k
        for (i = 0; i < len; i++) {
283
12.6k
            crc += (int)msg[4 + i];
284
12.6k
        }
285
2.26k
        crc &= 0x7fff;
286
287
        // enter CRC after payload
288
2.26k
        msg[len + 4] = (unsigned char)((crc & 0xff00) >> 8);
289
2.26k
        msg[len + 5] = (unsigned char)(crc & 0x00ff);
290
291
2.26k
        GPSD_LOG(LOG_PROG, &session->context->errout,
292
2.26k
                 "SiRF: Writing MID %#02x:\n", type);
293
2.26k
        ok = (gpsd_write(session, (const char *)msg, len + 8) ==
294
2.26k
              (ssize_t) (len + 8));
295
296
2.26k
        session->driver.sirf.need_ack = type;
297
2.26k
        return ok;
298
2.26k
    }
299
300
    static ssize_t sirf_control_send(struct gps_device_t *session, char *msg,
301
                                     size_t len)
302
0
    {
303
0
        session->msgbuf[0] = (char)0xa0;
304
0
        session->msgbuf[1] = (char)0xa2;
305
0
        session->msgbuf[2] = (len >> 8) & 0xff;
306
0
        session->msgbuf[3] = len & 0xff;
307
0
        memcpy(session->msgbuf + 4, msg, len);
308
0
        session->msgbuf[len + 6] = (char)0xb0;
309
0
        session->msgbuf[len + 7] = (char)0xb3;
310
0
        session->msgbuflen = len + 8;
311
312
        // *INDENT-OFF*
313
0
    return sirf_write(session,
314
0
              (unsigned char *)session->msgbuf,
315
0
              sizeof(session->msgbuf)) ? (int)session->msgbuflen : -1;
316
        // *INDENT-ON*
317
0
    }
318
319
// change speed in binary mode
320
    static bool sirfbin_speed(struct gps_device_t *session, speed_t speed,
321
                              char parity, int stopbits)
322
0
    {
323
0
        static unsigned char msg[] = {
324
0
            0xa0, 0xa2, 0x00, 0x09,
325
0
            0x86,               /* byte 4:
326
                                 * Set Binary Serial Port
327
                                 * MID 134 */
328
0
            0x00, 0x00, 0x12, 0xc0, // bytes 5-8: 4800 bps
329
0
            0x08,               // byte  9: 8 data bits
330
0
            0x01,               // byte 10: 1 stop bit
331
0
            0x00,               // byte 11: no parity
332
0
            0x00,               // byte 12: reserved pad
333
0
            0x00, 0x00, 0xb0, 0xb3
334
0
        };
335
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
336
0
                 "SiRF: sirf_speed(%u,%c,%d)\n",
337
0
                 (unsigned int)speed, parity, stopbits);
338
0
        if (9600 >= speed) {
339
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
340
0
                     "SiRF may lag at 9600bps or less.\n");
341
0
        }
342
343
0
        switch (parity) {
344
0
        case 'E':
345
0
        case 2:
346
0
            parity = (char)2;
347
0
            break;
348
0
        case 'O':
349
0
        case 1:
350
0
            parity = (char)1;
351
0
            break;
352
0
        case 'N':
353
0
        case 0:
354
0
        default:
355
0
            parity = (char)0;
356
0
            break;
357
0
        }
358
0
        msg[5] = (unsigned char)((speed >> 24) & 0xff);
359
0
        msg[6] = (unsigned char)((speed >> 16) & 0xff);
360
0
        msg[7] = (unsigned char)((speed >> 8) & 0xff);
361
0
        msg[8] = (unsigned char)(speed & 0xff);
362
0
        msg[10] = (unsigned char)stopbits;
363
0
        msg[11] = (unsigned char)parity;
364
0
        return sirf_write(session, msg, sizeof(msg));
365
0
    }
366
367
/* switch from binary to NMEA at specified baud
368
 * FIXME: does not seem to work... */
369
    static bool sirf_to_nmea(struct gps_device_t *session, speed_t speed)
370
1
    {
371
1
        static unsigned char msg[] = {
372
1
            0xa0, 0xa2, 0x00, 0x18,
373
1
            0x81, 0x02,
374
1
            0x01, 0x01,         // GGA
375
1
            0x00, 0x00,         // suppress GLL
376
1
            0x01, 0x01,         // GSA
377
1
            0x05, 0x01,         // GSV
378
1
            0x01, 0x01,         // RMC
379
1
            0x00, 0x00,         // suppress VTG
380
1
            0x00, 0x01,         // suppress MSS
381
1
            0x00, 0x01,         // suppress EPE
382
1
            0x00, 0x01,         // suppress EPE
383
1
            0x00, 0x01,         // suppress ZDA
384
1
            0x00, 0x00,         // unused
385
1
            0x12, 0xc0,         // 4800 bps
386
1
            0xb0, 0xb3
387
1
        };
388
389
1
        if (0xffff <= speed) {
390
0
            GPSD_LOG(LOG_ERROR, &session->context->errout,
391
0
                     "SiRF: can't switch from SiRF to NMEA because "
392
0
                     " current speed %u is big.",
393
0
                     (unsigned int)speed);
394
0
            return false;
395
0
        }
396
397
        // stop binary initialization
398
1
        session->cfg_stage = UINT_MAX;
399
400
1
        msg[26] = (unsigned char)HI(speed);
401
1
        msg[27] = (unsigned char)LO(speed);
402
1
        return sirf_write(session, msg, sizeof(msg));
403
1
    }
404
405
    static void sirfbin_mode(struct gps_device_t *session, int mode)
406
1
    {
407
1
        if (MODE_NMEA == mode) {
408
1
            (void)sirf_to_nmea(session, session->gpsdata.dev.baudrate);
409
1
        } else if (MODE_BINARY == mode) {
410
0
            char parity = '0';
411
0
            switch (session->gpsdata.dev.parity) {
412
0
            default:
413
0
            case 'N':
414
0
                parity = '0';
415
0
                break;
416
0
            case 'O':
417
0
                parity = '1';
418
0
                break;
419
0
            case 'E':
420
0
                parity = '2';
421
0
                break;
422
423
0
            }
424
            // gpsd only supports 8[NO]1 or 7[EO]2
425
            // thus the strange use of stopbits
426
0
            (void)nmea_send(session,
427
0
                            "$PSRF100,0,%d,%d,%d,%c",
428
0
                            session->gpsdata.dev.baudrate,
429
0
                            9 - session->gpsdata.dev.stopbits,
430
0
                            session->gpsdata.dev.stopbits, parity);
431
            // reset binary init steps
432
0
            session->cfg_stage = 0;
433
0
            session->cfg_step = 0;
434
0
        }
435
1
    }
436
437
// Debug messages MID 255 (0xff)
438
    static gps_mask_t sirf_msg_debug(struct gps_device_t *device,
439
                                     unsigned char *buf, size_t len)
440
180
    {
441
180
        char msgbuf[MAX_PACKET_LENGTH * 3 + 2];
442
180
        int i;
443
444
180
        memset(msgbuf, 0, (int)sizeof(msgbuf));
445
446
        // FIXME: always/only ID 255
447
180
        if (0xe1 == buf[0]) {   // Development statistics messages
448
0
            if (2 > len) {
449
                // too short
450
0
                GPSD_LOG(LOG_INF, &device->context->errout,
451
0
                         "SiRF: DBG (0xe1) runt len %zu\n", len);
452
0
                return 0;
453
0
            }
454
0
            for (i = 2; i < (int)len; i++) {
455
0
                str_appendf(msgbuf, sizeof(msgbuf), "%c", buf[i] ^ 0xff);
456
0
            }
457
0
            GPSD_LOG(LOG_PROG, &device->context->errout,
458
0
                     "SiRF: MID 0xe1 (225) SID %#0x %s\n", buf[1], msgbuf);
459
180
        } else if (0xff == buf[0]) { // Debug messages
460
1.10k
            for (i = 1; i < (int)len; i++) {
461
921
                if (ISPRINT(buf[i])) {
462
373
                    str_appendf(msgbuf, sizeof(msgbuf), "%c", buf[i]);
463
548
                } else {
464
548
                    str_appendf(msgbuf, sizeof(msgbuf),
465
548
                                "\\x%02x", (unsigned int)buf[i]);
466
548
                }
467
921
            }
468
180
            GPSD_LOG(LOG_PROG, &device->context->errout,
469
180
                     "SiRF: DBG 0xff: %s\n", msgbuf);
470
180
        }
471
180
        return 0;
472
180
    }
473
474
// decode Error ID Data MID 10 (0x0a)
475
    static gps_mask_t sirf_msg_errors(struct gps_device_t *device,
476
                                      unsigned char *buf,
477
                                      size_t len UNUSED)
478
130
    {
479
130
        unsigned count;
480
481
130
        if (5 > len) {
482
            // too short
483
68
            GPSD_LOG(LOG_INF, &device->context->errout,
484
68
                     "SiRF: EID (0x0a) runt len %zu\n", len);
485
68
            return 0;
486
68
        }
487
488
62
        count = getbeu16(buf, 3);
489
490
62
        switch (getbeu16(buf, 1)) {
491
1
        case 2:
492
            // ErrId_CS_SVParity
493
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
494
1
                     "SiRF: EID 0x0a type 2: Subframe %u error on PRN %u\n",
495
1
                     getbeu32(buf, 9), getbeu32(buf, 5));
496
1
            break;
497
498
1
        case 4:
499
            // ErrId_RMC_GettingPosition
500
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
501
1
                     "SiRF: EID 0x0a type 4: RMC_GettingPosition on PRN %u\n",
502
1
                     getbeu32(buf, 5));
503
1
            break;
504
505
1
        case 10:
506
            // ErrId_RXM_TimeExceeded
507
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
508
1
                     "SiRF: EID 0x0a type 10: RXM_TimeExceeded PR %u\n",
509
1
                     getbeu32(buf, 5));
510
1
            break;
511
512
1
        case 11:
513
            // ErrId_RXM_TDOPOverflow
514
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
515
1
                     "SiRF: EID 0x0a type 11: RXM_TDOPOverflow doppler %u\n",
516
1
                     getbeu32(buf, 5));
517
1
            break;
518
519
1
        case 12:
520
            // ErrId_RXM_ValidDurationExceeded
521
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
522
1
                     "SiRF: EID 0x0a type 12: RXM_ValidDurationExceeded PRN %u\n",
523
1
                     getbeu32(buf, 5));
524
1
            break;
525
526
1
        case 13:
527
            // ErrId_STRTP_BadPostion
528
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
529
1
                     "SiRF: EID 0x0a type 13: STRTP_BadPostion\n");
530
1
            break;
531
532
1
        case 4097:
533
            // ErrId_MI_VCOClockLost
534
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
535
1
                     "SiRF: EID 0x0a type 4097: MI_VCOClockLost %u\n",
536
1
                     getbeu32(buf, 5));
537
1
            break;
538
539
1
        case 4099:
540
            // ErrId_MI_FalseAcqReceiverReset
541
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
542
1
                     "SiRF: EID 0x0a type 4099: MI_FalseAcqReceiverReset %u\n",
543
1
                     getbeu32(buf, 5));
544
1
            break;
545
546
1
        case 4104:
547
            // ErrId_STRTP_SRAMCksum
548
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
549
1
                     "SiRF: EID 0x0a type 4104: Failed SRAM "
550
1
                     "checksum during startup.\n");
551
1
            break;
552
553
1
        case 4105:
554
            // ErrId_STRTP_RTCTimeInvalid
555
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
556
1
                     "SiRF: EID 0x0a type 4105: Failed RTC SRAM checksum.\n");
557
1
            break;
558
559
1
        case 4106:
560
            // ErrId_KFC_BackupFailed_Velocity
561
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
562
1
                     "SiRF: EID 0x0a type 4106: Failed saving position "
563
1
                     "to NVRAM.\n");
564
1
            break;
565
566
1
        case 4107:
567
            // ErrId_KFC_BackupFailed_NumSV
568
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
569
1
                     "SiRF: EID 0x0a type 4107: neither KF nor LSQ fix.\n");
570
1
            break;
571
572
1
        case 8193:
573
            // ErrId_MI_BufferAllocFailure
574
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
575
1
                     "SiRF: EID 0x0a type 8193: "
576
1
                     "Error ID Description:Buffer allocation error.\n");
577
1
            break;
578
579
1
        case 8194:
580
            // ErrId_MI_UpdateTimeFailure
581
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
582
1
                     "SiRF: EID 0x0a type 8194: "
583
1
                     "PROCESS_1SEC task unable to complete\n");
584
1
            break;
585
586
1
        case 8195:
587
            // ErrId_MI_MemoryTestFailed
588
1
            GPSD_LOG(LOG_PROG, &device->context->errout,
589
1
                     "SiRF: EID 0x0a type 8195: "
590
1
                     "Failure of hardware memory test.\n");
591
1
            break;
592
593
47
        default:
594
47
            GPSD_LOG(LOG_PROG, &device->context->errout,
595
47
                     "SiRF: EID 0x0a: Error MID %u, count %u\n",
596
47
                     getbeu16(buf, 1), count);
597
47
            break;
598
62
        }
599
62
        return 0;
600
62
    }
601
602
// Navigation Library Measurement Data MID 28 (0x1c)
603
    static gps_mask_t sirf_msg_nlmd(struct gps_device_t *session,
604
                                    unsigned char *buf UNUSED, size_t len)
605
67
    {
606
607
67
        double gps_tow = 0.0;
608
609
67
        if (56 != len) {
610
66
            GPSD_LOG(LOG_INF, &session->context->errout,
611
66
                     "SiRF: NLMD (0x0a) runt len %zu\n", len);
612
66
            return 0;
613
66
        }
614
615
        /* oh barf, SiRF claims to be IEEE754 but supports two
616
         * different double orders, neither IEEE754 */
617
        /* FIXME - decode the time, since this is the first MID with a
618
         * good time stamp this will be good for ntpshm time */
619
1
        GPSD_LOG(LOG_PROG, &session->context->errout,
620
1
                 "SiRF: NLMD 0x1c gps_tow: %f\n",
621
1
                 (double)gps_tow);
622
623
1
        return 0;
624
67
    }
625
626
// MID_SiRFNavNotification MID 51 (0x33)
627
    static gps_mask_t sirf_msg_navnot(struct gps_device_t *session,
628
                                      unsigned char *buf, size_t len)
629
124
    {
630
124
        const char *definition = "Unknown";
631
124
        gps_mask_t mask = 0;
632
633
124
        if (3 > len) {
634
110
            GPSD_LOG(LOG_INF, &session->context->errout,
635
110
                     "SiRF: MID (0x33) runt len %zu\n", len);
636
110
            return 0;
637
110
        }
638
639
14
        switch (buf[1]) {
640
0
        case 1:
641
            // last message sent every cycle
642
0
            definition = "SID_GPS_SIRFNAV_COMPLETE";
643
            // so push a report now
644
0
            mask = REPORT_IS;
645
0
            break;
646
1
        case 2:
647
1
            definition = "SID_GPS_SIRFNAV_TIMING";
648
1
            break;
649
0
        case 3:
650
0
            definition = "SID_GPS_DEMO_TIMING";
651
0
            break;
652
0
        case 4:
653
0
            definition = "SID_GPS_SIRFNAV_TIME_TAGS";
654
0
            break;
655
1
        case 5:
656
1
            definition = "SID_GPS_NAV_IS801_PSEUDORANGE_DATA";
657
1
            break;
658
2
        case 6:
659
2
            definition = "GPS_TRACKER_LOADER_STATE";
660
2
            break;
661
1
        case 7:
662
1
            definition = "SSB_SIRFNAV_START";
663
1
            break;
664
0
        case 8:
665
0
            definition = "SSB_SIRFNAV_STOP";
666
0
            break;
667
1
        case 9:
668
1
            definition = "SSB_RESULT";
669
1
            break;
670
1
        case 16:
671
1
            definition = "DEMO_TEST_STATUS";
672
1
            break;
673
0
        case 17:
674
0
            definition = "DEMO_TEST_STATE";
675
0
            break;
676
1
        case 18:
677
1
            definition = "DEMO_TEST_DATA";
678
1
            break;
679
0
        case 19:
680
0
            definition = "DEMO_TEST_STATS";
681
0
            break;
682
1
        case 20:
683
1
            definition = "DEMO_TEST_ERROR";
684
1
            break;
685
5
        default:
686
5
            break;
687
14
        }
688
689
14
        GPSD_LOG(LOG_PROG, &session->context->errout,
690
14
                 "SiRF IV: NavNotification 51 (0x33), SID: %d (%s), len %lu\n",
691
14
                 buf[1], definition, (long unsigned)len);
692
693
14
        return mask;
694
14
    }
695
696
/* Multiconstellation Navigation Data response MID 67,1 (0x43)
697
 * SIRF_MSG_SSB_GNSS_NAV_DATA
698
 * this replaces the deprecated MID 41 */
699
    static gps_mask_t sirf_msg_67_1(struct gps_device_t *session,
700
                                    unsigned char *buf, size_t len)
701
14
    {
702
14
        gps_mask_t mask = 0;
703
14
        uint32_t solution_validity;
704
14
        uint32_t solution_info;
705
14
        uint32_t gps_tow = 0;
706
14
        uint32_t msecs;                  // tow in ms
707
14
        uint32_t gps_tow_sub_ms = 0;
708
14
        uint16_t gps_week = 0;
709
14
        timespec_t gps_tow_ns = {0};
710
14
        timespec_t now;
711
14
        int16_t time_bias = 0;
712
14
        uint8_t time_accuracy = 0;
713
14
        uint8_t time_source = 0;
714
14
        struct tm unpacked_date = {0};
715
14
        unsigned char datum;
716
14
        int64_t clk_bias;
717
14
        uint32_t clk_bias_error;
718
14
        int32_t clk_offset;
719
14
        uint32_t clk_offset_error;
720
14
        int16_t heading_rate;           // rate of change cog deg/s * 100
721
14
        uint32_t distance_travel;       // distance traveled m * 100
722
14
        uint16_t distance_travel_error; // distance traveled error in m * 100
723
724
14
        uint32_t ehpe;                  // Est horizontal position error * 100
725
14
        unsigned char num_svs_in_sol;   // Num of satellites used in solution
726
14
        uint32_t sv_list_1;
727
14
        uint32_t sv_list_2;
728
14
        uint32_t sv_list_3;
729
14
        uint32_t sv_list_4;
730
14
        uint32_t sv_list_5;
731
14
        uint32_t additional_info;
732
14
        int debug_base = LOG_PROG;
733
734
14
        if (126 > len) {
735
5
            GPSD_LOG(LOG_INF, &session->context->errout,
736
5
                     "SiRF: MID 67,1 runt len %zu\n", len);
737
5
            return 0;
738
5
        }
739
740
9
        GPSD_LOG(LOG_PROG, &session->context->errout,
741
9
                 "SiRF V: MND 67,1 Multiconstellation Navigation Data Response \n");
742
743
9
        solution_validity = getbeu32(buf, 2);
744
9
        if (0 != solution_validity) {
745
            // invalid fix, just give up
746
1
            return 0;
747
1
        }
748
749
8
        solution_info = getbeu32(buf, 6);
750
8
        gps_week = getbeu16(buf, 10);
751
8
        msecs = getbeu32(buf, 12);
752
8
        gps_tow = msecs / 1000;
753
8
        gps_tow_ns.tv_sec = gps_tow;
754
        // get ms part
755
8
        gps_tow_sub_ms = msecs % 1000;
756
        // add in the ns
757
8
        gps_tow_ns.tv_nsec = (gps_tow_sub_ms * 1000000LL) + getbeu32(buf, 16);
758
8
        now = gpsd_gpstime_resolv(session, gps_week, gps_tow_ns);
759
        /* we'll not use this time, instead the unpacked date below,
760
         * to get the right epoch */
761
762
8
        time_bias = getbes16(buf, 20); // add in the ns
763
        // time_accuracy is an odd 8 bit float
764
8
        time_accuracy = getub(buf, 22);
765
8
        time_source = getub(buf, 23);  // unused
766
767
8
        memset(&unpacked_date, 0, sizeof(unpacked_date));
768
8
        unpacked_date.tm_year = (int)getbeu16(buf, 24) - 1900;
769
8
        unpacked_date.tm_mon = (int)getub(buf, 26) - 1;
770
8
        unpacked_date.tm_mday = (int)getub(buf, 27);
771
8
        unpacked_date.tm_hour = (int)getub(buf, 28);
772
8
        unpacked_date.tm_min = (int)getub(buf, 29);
773
8
        unpacked_date.tm_sec = (int)getbeu16(buf, 30) / 1000;
774
8
        session->context->leap_seconds = (int)getub(buf, 32);
775
8
        session->context->valid |= LEAP_SECOND_VALID;
776
8
        session->newdata.time.tv_sec = mkgmtime(&unpacked_date);
777
8
        session->newdata.time.tv_nsec = gps_tow_ns.tv_nsec;
778
        // got time now
779
8
        mask |= TIME_SET;
780
781
8
        datum = getub(buf, 33);
782
8
        datum_code_string(datum, session->newdata.datum,
783
8
                          sizeof(session->newdata.datum));
784
785
8
        clk_bias = getbes64(buf, 34) / 100.0;
786
8
        clk_bias_error = getbeu32(buf, 42) / 100.0;
787
8
        clk_offset = getbes32(buf, 46) / 100.0;
788
8
        clk_offset_error = getbeu32(buf, 50) / 100.0;
789
8
        session->newdata.latitude = getbes32(buf, 54) * 1e-7;
790
8
        session->newdata.longitude = getbes32(buf, 58) * 1e-7;
791
        // altitude WGS84
792
8
        session->newdata.altHAE = getbes32(buf, 62) * 1e-2;
793
        // altitude MSL
794
8
        session->newdata.altMSL = getbes32(buf, 66) * 1e-2;
795
        // Let gpsd_error_model() deal with geoid_sep
796
797
8
        mask |= LATLON_SET;
798
799
8
        switch (solution_info & 0x07) {
800
1
        case 0:  // no fix
801
1
            session->newdata.mode = MODE_NO_FIX;
802
1
            break;
803
1
        case 1:  // unused
804
1
            session->newdata.mode = MODE_NO_FIX;
805
1
            break;
806
1
        case 2:  // unused
807
1
            session->newdata.mode = MODE_NO_FIX;
808
1
            break;
809
1
        case 3:  // 3-SV KF Solution
810
1
            session->newdata.mode = MODE_2D;
811
1
            break;
812
1
        case 4:  // Four or more SV KF Solution
813
1
            session->newdata.mode = MODE_3D;
814
1
            break;
815
1
        case 5:  // 2-D Least-squares Solution
816
1
            session->newdata.mode = MODE_2D;
817
1
            break;
818
1
        case 6:  // 3-D Least-squaresSolution
819
1
            session->newdata.mode = MODE_3D;
820
1
            break;
821
1
        case 7:  // DR solution, assume 3D
822
1
            session->newdata.mode = MODE_3D;
823
1
            break;
824
0
        default: // can't really happen
825
0
            session->newdata.mode = MODE_NO_FIX;
826
0
            break;
827
8
        }
828
8
        mask |= MODE_SET;
829
830
8
        if (!(solution_info & 0x01000)) {
831
            // sog - speed over ground m/s * 100
832
4
            session->newdata.speed = getbeu16(buf, 70) / 100.0;
833
4
            mask |= SPEED_SET;
834
4
        }
835
        // cog - course over ground fm true north deg * 100
836
8
        session->newdata.track = getbeu16(buf, 72) / 100.0;
837
8
        mask |= TRACK_SET;
838
839
        // climb_rate - vertical velocity m/s * 100
840
8
        session->newdata.climb = getbes16(buf, 74) / 100.0;
841
842
8
        if (MODE_3D == session->newdata.mode) {
843
3
            mask |= ALTITUDE_SET | CLIMB_SET;
844
3
        }
845
846
8
        heading_rate = getbes16(buf, 76);    // rate of change cog deg/s * 100
847
8
        distance_travel = getbeu32(buf, 78); // distance traveled m * 100
848
        // heading_error error of cog deg * 100
849
8
        session->newdata.epd = getbeu16(buf, 82) / 100.0;
850
        // distance traveled error in m * 100
851
8
        distance_travel_error = getbeu16(buf, 84) / 100.0;
852
853
8
        ehpe = getbeu32(buf, 86);            // Estimated horizontal position
854
                                             // error * 100
855
        // Estimated vertical position error * 100
856
8
        session->newdata.epv = getbeu32(buf, 90) / 100.0;
857
        // Estimated horizontal velocity error * 100
858
8
        session->newdata.eps = getbeu16(buf, 94) / 100.0;
859
8
        mask |= SPEEDERR_SET;
860
861
8
        session->gpsdata.dop.gdop = (int)getub(buf, 96) / 5.0;
862
8
        session->gpsdata.dop.pdop = (int)getub(buf, 97) / 5.0;
863
8
        session->gpsdata.dop.hdop = (int)getub(buf, 98) / 5.0;
864
8
        session->gpsdata.dop.vdop = (int)getub(buf, 99) / 5.0;
865
8
        session->gpsdata.dop.tdop = (int)getub(buf, 100) / 5.0;
866
8
        mask |= DOP_SET;
867
868
8
        num_svs_in_sol = getub(buf, 101);
869
8
        sv_list_1 = getbeu32(buf, 102);
870
8
        sv_list_2 = getbeu32(buf, 106);
871
8
        sv_list_3 = getbeu32(buf, 110);
872
8
        sv_list_4 = getbeu32(buf, 114);
873
8
        sv_list_5 = getbeu32(buf, 118);
874
8
        additional_info = getbeu32(buf, 122);
875
876
8
        mask |= REPORT_IS; // send it
877
878
        // skip all the debug pushing and popping, unless needed
879
8
        if (session->context->errout.debug >= debug_base) {
880
0
            char ts_buf[TIMESPEC_LEN];
881
882
            /* coerce time_t to long to placate older OS, like 32-bit FreeBSD,
883
             * where time_t is int */
884
0
            GPSD_LOG(debug_base, &session->context->errout,
885
0
                     "SiRF V: MND 67,1 GPS Week %d, tow %d.%03d, time %s\n",
886
0
                     gps_week, gps_tow, gps_tow_sub_ms,
887
0
                     timespec_str(&now, ts_buf, sizeof(ts_buf)));
888
0
            GPSD_LOG(debug_base, &session->context->errout,
889
0
                     "SiRF V: MND 67,1 UTC time %s leaps %u, datum %s\n",
890
0
                     timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
891
0
                     session->context->leap_seconds,
892
0
                     session->newdata.datum);
893
0
            GPSD_LOG(debug_base, &session->context->errout,
894
0
                     "SiRF V: MND 67,1 packed: %02d%02d%02d %02d:%02d:%02d\n",
895
0
                     unpacked_date.tm_mday, unpacked_date.tm_mon + 1,
896
0
                     unpacked_date.tm_year % 100,
897
0
                     unpacked_date.tm_hour, unpacked_date.tm_min,
898
0
                     unpacked_date.tm_sec);
899
0
            GPSD_LOG(debug_base, &session->context->errout,
900
0
                     "SiRF V: MND 67,1 solution_info %08x\n", solution_info);
901
0
            GPSD_LOG(debug_base, &session->context->errout,
902
0
                     "SiRF V: MND 67,1 lat %.7f lon %.7f altHAE %.2f altMSL %.2f\n",
903
0
                     session->newdata.latitude, session->newdata.longitude,
904
0
                     session->newdata.altHAE, session->newdata.altMSL);
905
0
            GPSD_LOG(debug_base, &session->context->errout,
906
0
                     "SiRF V: MND 67,1 speed %.2f track %.2f climb %.2f "
907
0
                     "heading_rate %d\n",
908
0
                     session->newdata.speed, session->newdata.track,
909
0
                     session->newdata.climb, heading_rate);
910
0
            GPSD_LOG(debug_base, &session->context->errout,
911
0
                     "SiRF V: MND 67,1 time_bias %d time_accuracy %u "
912
0
                     "time_source %u\n",
913
0
                     time_bias, time_accuracy, time_source);
914
0
            GPSD_LOG(debug_base, &session->context->errout,
915
0
                     "SiRF V: MND 67,1 distance_travel %u "
916
0
                     "distance_travel_error %d\n",
917
0
                     distance_travel, distance_travel_error);
918
0
            GPSD_LOG(debug_base, &session->context->errout,
919
0
                     "SiRF V: MND 67,1 clk_bias %.2f clk_bias_error %u\n",
920
0
                     clk_bias / 100.0, clk_bias_error);
921
0
            GPSD_LOG(debug_base, &session->context->errout,
922
0
                     "SiRF V: MND 67,1 clk_offset %d clk_offset_error %u\n",
923
0
                     clk_offset, clk_offset_error);
924
0
            GPSD_LOG(debug_base, &session->context->errout,
925
0
                     "SiRF V: MND 67,1 ehpe %d epv %.2f eps %.2f epd %.2f "
926
0
                     "num_svs_in_sol %u\n",
927
0
                     ehpe, session->newdata.epv, session->newdata.eps,
928
0
                     session->newdata.epd, num_svs_in_sol);
929
0
            GPSD_LOG(debug_base, &session->context->errout,
930
0
                     "SiRF V: MND 67,1 sv_list_1 %08x sv_list_2 %08x s"
931
0
                     "v_list_3 %08x\n",
932
0
                     sv_list_1, sv_list_2, sv_list_3);
933
0
            GPSD_LOG(debug_base, &session->context->errout,
934
0
                     "SiRF V: MND 67,1 sv_list_4 %08x sv_list_5 %08x "
935
0
                     "add_info %08x\n",
936
0
                     sv_list_4, sv_list_5, additional_info);
937
0
        }
938
939
8
        return mask;
940
8
    }
941
942
/* Multiconstellation Navigation Data response MID 67,16 (0x43)
943
 * this replaces the deprecated MID 41 */
944
    static gps_mask_t sirf_msg_67_16(struct gps_device_t *session,
945
                                     unsigned char *buf, size_t len)
946
170
    {
947
170
        gps_mask_t mask = 0;
948
170
        uint32_t gps_tow = 0;
949
170
        uint32_t gps_tow_sub_ms = 0;
950
170
        uint16_t gps_week = 0;
951
170
        timespec_t gps_tow_ns = {0};
952
170
        timespec_t now = {0};
953
170
        int16_t time_bias = 0;
954
170
        uint8_t time_accuracy = 0;
955
170
        uint8_t time_source = 0;
956
170
        uint8_t msg_info = 0;
957
170
        uint8_t num_of_sats = 0;
958
170
        unsigned int sat_num;
959
170
        unsigned st, last_st;             // indexes into skyview
960
170
        char ts_buf[TIMESPEC_LEN];
961
962
170
        if (18 > len) {
963
            // zero sats is len == 18
964
1
            GPSD_LOG(LOG_WARN, &session->context->errout,
965
1
                     "SiRF V: MID 67,16 runt packet. Len %lu\n",
966
1
                     (unsigned long)len);
967
1
            return 0;
968
1
        }
969
970
169
        if (198 < len) {
971
            // max sats per message is 15
972
1
            GPSD_LOG(LOG_WARN, &session->context->errout,
973
1
                     "SiRF V: MID 67,16 packet too big. Len %lu\n",
974
1
                     (unsigned long)len);
975
1
            return 0;
976
1
        }
977
978
168
        GPSD_LOG(LOG_PROG, &session->context->errout,
979
168
                 "SiRF V: MID 67,16 Multiconstellation Satellite "
980
168
                 "Data Response\n");
981
982
168
        gps_week = getbeu16(buf, 2);
983
168
        gps_tow = getbeu32(buf, 4) / 1000;
984
        // get ms part, convert to ns
985
168
        gps_tow_sub_ms = 1000000 * (getbeu32(buf, 4) % 1000);
986
168
        gps_tow_sub_ms += getbeu32(buf, 8); // add in the ns
987
168
        gps_tow_ns.tv_sec = gps_tow;
988
168
        gps_tow_ns.tv_nsec = gps_tow_sub_ms;
989
168
        session->newdata.time = gpsd_gpstime_resolv(session, gps_week,
990
168
                                                    gps_tow_ns);
991
168
        session->gpsdata.skyview_time = session->newdata.time;
992
168
        time_bias = getbes16(buf, 12);
993
        // time_accuracy is an odd 8 bit float
994
168
        time_accuracy = getub(buf, 14);
995
168
        time_source = getub(buf, 15);
996
        /* msg_info[0:3] "order" of this sentence.  1-15
997
         * msg_info[4:7] number of sats in this sentence.
998
         */
999
168
        msg_info = getub(buf, 16);
1000
168
        if (0 == (msg_info & 0x0f)) {
1001
            // WTF?
1002
1
            return 0;
1003
1
        }
1004
167
        if (1 == (msg_info & 0x0f)) {
1005
            // first set, zero the sats
1006
19
            gpsd_zero_satellites(&session->gpsdata);
1007
19
        }
1008
        // st is index into skyview[] for each sat.  This is first one.
1009
167
        st = ((msg_info & 0x0f) - 1) * 15;
1010
        // use len to get number of sats in this message, compute last one.
1011
167
        last_st = st + ((len - 18) / 12);
1012
167
        num_of_sats = getub(buf, 17);
1013
        // got time now
1014
167
        mask |= TIME_SET;
1015
1016
167
        GPSD_LOG(LOG_IO, &session->context->errout,
1017
167
                 "SiRF V: MID 67,16 GPS Week %d, tow %d.%03d, time %s\n",
1018
167
                 gps_week, gps_tow, gps_tow_sub_ms,
1019
167
                 timespec_str(&now, ts_buf, sizeof(ts_buf)));
1020
167
        GPSD_LOG(LOG_IO, &session->context->errout,
1021
167
                 "SiRF V: MID 67,16 Time bias: %d ns, accuracy %#02x, "
1022
167
                 "source %u, msg_info %#02x, sats %u st %u last_st %u "
1023
167
                 "len %zu\n",
1024
167
                 time_bias, time_accuracy, time_source, msg_info,
1025
167
                 num_of_sats, st, last_st, len);
1026
1027
167
        if (MAXCHANNELS < num_of_sats) {
1028
            // shut up coverity
1029
25
            num_of_sats = MAXCHANNELS;
1030
25
        }
1031
167
        session->gpsdata.satellites_visible = num_of_sats;
1032
1033
        /* now decode the individual sat data
1034
         * 15 sats per message max.
1035
         * Seems to always be exactly 15, some empty */
1036
683
        for (sat_num = 0; sat_num < 15; sat_num++) {
1037
682
            unsigned offset;
1038
682
            uint16_t sat_info;
1039
682
            uint16_t other_info;
1040
682
            unsigned char gnssId_sirf;
1041
682
            unsigned char gnssId;
1042
682
            unsigned char svId;
1043
682
            short PRN;
1044
682
            double azimuth;
1045
682
            double elevation;
1046
682
            short avg_cno;
1047
682
            double ss;
1048
682
            uint32_t status;
1049
1050
682
            if (st >= MAXCHANNELS) {
1051
                // filled up skyview
1052
0
                break;
1053
0
            }
1054
            // 4 byte header, each seat 12 bytes
1055
682
            offset = 18 + (sat_num * 12);
1056
#if 0  // debug
1057
                GPSD_LOG(LOG_SHOUT, &session->context->errout,
1058
                         "SiRF V: MID 67,16 num_of_sats %u sat_num %u st %u "
1059
                         "offset %u\n",
1060
                         num_of_sats, sat_num, st, offset);
1061
#endif
1062
1063
            // len is always 198 bytes??
1064
682
            if (offset >= len) {
1065
                // Not enough data in message for this sat.
1066
166
                break;
1067
166
            }
1068
516
            sat_info = getbeu16(buf, offset);
1069
516
            if (0 == sat_info) {
1070
                // empty slot, ignore
1071
#if 0  // debug
1072
                    GPSD_LOG(LOG_SHOUT, &session->context->errout,
1073
                             "SiRF V: MID 67,16 slot %u empty\n", st);
1074
#endif
1075
31
                continue;
1076
31
            }
1077
1078
            /*  0 = GPS/QZSS
1079
             *  1 = SBAS
1080
             *  2 = GLONASS
1081
             *  3 = Galileo
1082
             *  4 = BDS
1083
             */
1084
485
            gnssId_sirf = sat_info >> 13;
1085
485
            svId = sat_info & 0x0ff;
1086
485
            other_info = (sat_info >> 8) & 0x1f;
1087
            /* If SBAS, other_info bits 8-12:
1088
             *  0 = WAAS
1089
             *  1 = EGNOS
1090
             *  2 = MSAS
1091
             *  3 = GAGAN
1092
             */
1093
            /* make up a PRN based on gnssId:svId, using table 4-55
1094
             * from (CS-303979-SP-9) SiRFstarV OSP Extensions
1095
             * Note: the Qualcomm doc is very vague
1096
             */
1097
485
            switch (gnssId_sirf) {
1098
164
            case 0:
1099
                /* GPS, 1-32 maps to 1-32
1100
                 * 173 to 182: QZSS IMES
1101
                 * 183 to 187: QZSS SAIF
1102
                 * 193 to 202: QZSS */
1103
164
                if ((173 <= svId) &&
1104
91
                    (182 >= svId)) {
1105
                    // IMES
1106
23
                    gnssId = 4;
1107
23
                    PRN = svId;
1108
23
                    svId -= 172;
1109
141
                } else if ((193 <= svId) &&
1110
47
                           (202 >= svId)) {
1111
                    // QZSS
1112
21
                    gnssId = 5;
1113
21
                    PRN = svId;
1114
21
                    svId -= 192;
1115
120
                } else {
1116
                    // GPS, or??
1117
120
                    gnssId = 0;
1118
120
                    PRN = svId;
1119
120
                }
1120
164
                break;
1121
70
            case 1:
1122
                // SBAS, 120-158 maps to 120-158
1123
70
                if (120 > svId ||
1124
49
                    158 < svId) {
1125
                    // skip bad svId
1126
49
                    continue;
1127
49
                }
1128
21
                gnssId = 1;
1129
21
                PRN = svId;
1130
21
                break;
1131
67
            case 2:
1132
                // GLONASS, 1-32 maps to 65-96
1133
67
                if (1 > svId) {
1134
                    // skip bad svId
1135
20
                    continue;
1136
20
                }
1137
47
                if (32 < svId) {
1138
                    // skip bad svId
1139
21
                    continue;
1140
21
                }
1141
26
                gnssId = 6;
1142
26
                PRN = svId + 64;
1143
26
                break;
1144
59
            case 3:
1145
                // Galileo, 1-36 maps to 211-246
1146
59
                if (1 > svId) {
1147
                    // skip bad svId
1148
18
                    continue;
1149
18
                }
1150
41
                if (37 < svId) {
1151
                    // skip bad svId
1152
20
                    continue;
1153
20
                }
1154
21
                gnssId = 2;
1155
21
                PRN = svId + 210;
1156
21
                break;
1157
89
            case 4:
1158
                // BeiDou, 1-37 maps to 159-163,33-64
1159
89
                if (1 > svId) {
1160
                    // skip bad svId
1161
21
                    continue;
1162
68
                } else if (6 > svId) {
1163
                    // 1-5 maps to 159-163
1164
22
                    PRN = svId + 158;
1165
46
                } else if (37 < svId) {
1166
                    // skip bad svId
1167
25
                    continue;
1168
25
                } else {
1169
                    // 6-37 maps to 33-64
1170
21
                    PRN = svId + 27;
1171
21
                }
1172
43
                gnssId = 3;
1173
43
                break;
1174
36
            default:
1175
                // Huh?  Skip bad gnssId
1176
36
                continue;
1177
485
            }
1178
1179
            // note tenths in az and el
1180
275
            azimuth = (double)getbeu16(buf, offset + 2) / 10.0;
1181
            // what, no negative elevation!
1182
275
            elevation = (double)getbeu16(buf, offset + 4) / 10.0;
1183
275
            avg_cno = getbeu16(buf, offset + 6);
1184
275
            ss = (double)avg_cno / 10.0;
1185
275
            status = getbeu32(buf, offset + 8);
1186
275
            if ((0 == avg_cno) &&
1187
122
                (0 >= elevation) &&
1188
43
                (0 >= azimuth)) {
1189
                // null data, skip it
1190
18
                continue;
1191
18
            }
1192
1193
257
            session->gpsdata.skyview[st].PRN = PRN;
1194
257
            session->gpsdata.skyview[st].svid = svId;
1195
257
            session->gpsdata.skyview[st].gnssid = gnssId;
1196
257
            session->gpsdata.skyview[st].azimuth = azimuth;
1197
257
            session->gpsdata.skyview[st].elevation = elevation;
1198
257
            session->gpsdata.skyview[st].ss = ss;
1199
257
            if (0x08000 == (status & 0x08000)) {
1200
79
                session->gpsdata.skyview[st].used = true;
1201
79
            }
1202
257
            GPSD_LOG(LOG_IO, &session->context->errout,
1203
257
                     "SiRF V: MID 67,16 sat_info %04x gnssId %u svId %3u "
1204
257
                     "o %2u PRN %3u az %4.1f "
1205
257
                     "el %5.1f ss %5.1f\n",
1206
257
                     sat_info, gnssId, svId, other_info, PRN, azimuth,
1207
257
                     elevation, ss);
1208
257
            st++;
1209
257
        }
1210
167
        if ((msg_info >> 4) == (msg_info & 0x0f)) {
1211
            // got all the sats
1212
34
            mask |= SATELLITE_SET;
1213
34
        }
1214
167
        return mask;
1215
167
    }
1216
1217
/* Multiconstellation Navigation Data response MID 67 (0x43)
1218
 * this replaces the deprecated MID 41 */
1219
    static gps_mask_t sirf_msg_67(struct gps_device_t *session,
1220
                                  unsigned char *buf, size_t len)
1221
316
    {
1222
316
        gps_mask_t mask = 0;
1223
1224
316
        if (2 > len) {
1225
66
            GPSD_LOG(LOG_INF, &session->context->errout,
1226
66
                     "SiRF: MID (0x43) runt len %zu\n", len);
1227
66
            return 0;
1228
66
        }
1229
1230
250
        switch (buf[1]) {
1231
14
        case 1:
1232
14
            return sirf_msg_67_1(session, buf, len);
1233
170
        case 16:
1234
170
            return sirf_msg_67_16(session, buf, len);
1235
66
        default:
1236
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
1237
250
                     "SiRF V: unused MID 67 (0x43), SID: %d, len %ld\n", buf[1],
1238
250
                     (long)len);
1239
250
        }
1240
66
        return mask;
1241
250
    }
1242
1243
// MID_QUERY_RESP MID 81 (0x51)
1244
    static gps_mask_t sirf_msg_qresp(struct gps_device_t *session,
1245
                                     unsigned char *buf, size_t len)
1246
67
    {
1247
1248
67
        if (3 > len) {
1249
66
            GPSD_LOG(LOG_INF, &session->context->errout,
1250
66
                     "SiRF: MID (0x51) runt len %zu\n", len);
1251
66
            return 0;
1252
66
        }
1253
1254
1
        GPSD_LOG(LOG_PROG, &session->context->errout,
1255
1
                 "SiRF IV: unused MID_QUERY_RESP 0x51 (81), Q MID: %d, "
1256
1
                 "SID: %d Elen: %d\n",
1257
1
                 buf[1], buf[2], buf[3]);
1258
1
        return 0;
1259
67
    }
1260
1261
// SBAS 50 (0x32)
1262
    static gps_mask_t sirf_msg_sbas(struct gps_device_t *session,
1263
                                    unsigned char *buf, size_t len)
1264
71
    {
1265
71
        unsigned char prn, mode, timeout, flags;
1266
1267
71
        if (13 != len) {
1268
70
            GPSD_LOG(LOG_INF, &session->context->errout,
1269
70
                     "SiRF: MID (0x32) runt len %zu\n", len);
1270
70
            return 0;
1271
70
        }
1272
1273
1
        prn = buf[1];
1274
1
        mode = buf[2];
1275
1
        timeout = buf[3];
1276
1
        flags = buf[4];
1277
1
        GPSD_LOG(LOG_PROG, &session->context->errout,
1278
1
                 "SiRF: SBAS 0x32 PRN %u mode %u timeout %u flags x%x\n",
1279
1
                 prn, mode, timeout, flags);
1280
1
        return 0;
1281
71
    }
1282
1283
// Statistics Channel MID 225 (0xe1)
1284
    static gps_mask_t sirf_msg_stats(struct gps_device_t *session,
1285
                                     unsigned char *buf, size_t len)
1286
143
    {
1287
143
        const char *definition = "Unknown";
1288
143
        char output[255] = "unused";
1289
143
        uint16_t ttff_reset;
1290
143
        uint16_t ttff_aid;
1291
143
        uint16_t ttff_nav;
1292
1293
143
        if (2 > len) {
1294
70
            GPSD_LOG(LOG_INF, &session->context->errout,
1295
70
                     "SiRF: MID (0xe1) runt len %zu\n", len);
1296
70
            return 0;
1297
70
        }
1298
1299
73
        switch (buf[1]) {
1300
1
        case 6:
1301
1
            definition = "SSB_SIRF_STATS 6";
1302
1
            ttff_reset = getbeu16(buf, 2);
1303
1
            ttff_aid = getbeu16(buf, 4);
1304
1
            ttff_nav = getbeu16(buf, 6);
1305
1
            (void)snprintf(output, sizeof(output),
1306
1
                           "ttff reset %.1f, aid %.1f nav %.1f",
1307
1
                           ttff_reset * 0.1, ttff_aid * 0.1, ttff_nav * 0.1);
1308
1
            break;
1309
1
        case 7:
1310
1
            definition = "SSB_SIRF_STATS 7";
1311
1
            ttff_reset = getbeu16(buf, 2);
1312
1
            ttff_aid = getbeu16(buf, 4);
1313
1
            ttff_nav = getbeu16(buf, 6);
1314
1
            (void)snprintf(output, sizeof(output),
1315
1
                           "ttff reset %.1f, aid %.1f nav %.1f",
1316
1
                           ttff_reset * 0.1, ttff_aid * 0.1, ttff_nav * 0.1);
1317
1
            break;
1318
1
        case 32:
1319
1
            definition = "SIRF_MSG_SSB_DL_COMPAT_REC_OUT ";
1320
1
            break;
1321
1
        case 33:
1322
1
            definition = "SIRF_MSG_SSB_DL_OUT_TERM";
1323
1
            break;
1324
1
        case 34:
1325
1
            definition = "SIRF_MSG_SSB_DL_STATUS_OUT";
1326
1
            break;
1327
1
        case 35:
1328
1
            definition = "SIRF_MSG_SSB_SIRF_INTERNAL_OUT";
1329
1
            break;
1330
1
        case 65:
1331
1
            definition = "SIRF_MSG_SSB_EE_SEA_PROVIDE_EPH_EXT";
1332
1
            break;
1333
66
        default:
1334
66
            break;
1335
73
        }
1336
1337
73
        GPSD_LOG(LOG_PROG, &session->context->errout,
1338
73
                 "SiRF IV: MID 225 (0xe1), SID: %d (%s)%s\n",
1339
73
                 buf[1], definition, output);
1340
1341
73
        return 0;
1342
73
    }
1343
1344
// MID_TCXO_LEARNING_OUT MID 93 (0x5d)
1345
    static gps_mask_t sirf_msg_tcxo(struct gps_device_t *session,
1346
                                    unsigned char *buf, size_t len)
1347
610
    {
1348
610
        const char *definition = "Unknown";
1349
610
        uint32_t gps_tow = 0;
1350
610
        uint16_t gps_week = 0;
1351
610
        timespec_t gps_tow_ns = {0};
1352
610
        char output[255] = "";
1353
610
        timespec_t now = {0};
1354
610
        gps_mask_t mask = 0;
1355
610
        unsigned int time_status = 0;
1356
610
        int clock_offset = 0;
1357
610
        unsigned int temp = 0;
1358
1359
610
        if (2 > len) {
1360
66
            GPSD_LOG(LOG_INF, &session->context->errout,
1361
66
                     "SiRF: MID (0x5d) runt len %zu\n", len);
1362
66
            return 0;
1363
66
        }
1364
1365
544
        switch (buf[1]) {
1366
66
        case 1:
1367
66
            definition = "CLOCK_MODEL_DATA_BASE_OUT";
1368
66
            break;
1369
66
        case 2:
1370
66
            definition = "TEMPERATURE_TABLE";
1371
66
            break;
1372
1
        case 4:
1373
1
            definition = "TEMP_RECORDER_MESSAGE";
1374
1
            break;
1375
1
        case 5:
1376
1
            definition = "EARC";
1377
1
            break;
1378
68
        case 6:
1379
68
            definition = "RTC_ALARM";
1380
68
            break;
1381
66
        case 7:
1382
66
            definition = "RTC_CAL";
1383
66
            break;
1384
66
        case 8:
1385
66
            definition = "MPM_ACQUIRED";
1386
66
            break;
1387
66
        case 9:
1388
66
            definition = "MPM_SEARCHES";
1389
66
            break;
1390
1
        case 10:
1391
1
            definition = "MPM_PREPOS";
1392
1
            break;
1393
1
        case 11:
1394
1
            definition = "MICRO_NAV_MEASUREMENT";
1395
1
            break;
1396
1
        case 12:
1397
1
            definition = "TCXO_UNCEARTAINTY";
1398
1
            break;
1399
1
        case 13:
1400
1
            definition = "SYSTEM_TIME_STAMP";
1401
1
            break;
1402
71
        case 18:
1403
71
            if (26 > len) {
1404
69
                GPSD_LOG(LOG_PROG, &session->context->errout,
1405
69
                         "SiRF IV: TCXO 0x5D (93), SID: %d BAD len %zd\n",
1406
69
                         buf[1], len);
1407
69
                return 0;
1408
69
            }
1409
1410
2
            definition = "SIRF_MSG_SSB_XO_TEMP_REC_VALUE";
1411
2
            gps_tow = getbeu32(buf, 2);
1412
2
            gps_week = getbeu16(buf, 6);
1413
2
            time_status = getub(buf, 8);
1414
2
            clock_offset = getsb(buf, 9); // looks like leapseconds?
1415
2
            temp = getub(buf, 22);
1416
2
            gps_tow_ns.tv_sec = gps_tow / 100;
1417
2
            gps_tow_ns.tv_nsec = (gps_tow % 100) * 10000000LL;
1418
2
            session->newdata.time = gpsd_gpstime_resolv(session, gps_week,
1419
2
                                                        gps_tow_ns);
1420
1421
            // skip all the debug pushing and popping, unless needed
1422
2
            if (LOG_PROG <= session->context->errout.debug) {
1423
                /* coerce time_t to long to placate older OS, like 32-bit
1424
                 * FreeBSD,
1425
                 * where time_t is int */
1426
0
                (void)snprintf(output, sizeof(output),
1427
0
                               ", GPS Week %u, tow %u, time %lld, time_status %u "
1428
0
                               "ClockOffset %d, Temp %.1f",
1429
0
                               gps_week, gps_tow, (long long)now.tv_sec,
1430
0
                               time_status,
1431
0
                               clock_offset, temp * 0.54902);
1432
0
            }
1433
1434
2
            if (7 == (time_status & 7)) {
1435
1
                mask |= TIME_SET;
1436
1
            }
1437
2
            break;
1438
69
        default:
1439
69
            break;
1440
544
        }
1441
1442
475
        GPSD_LOG(LOG_PROG, &session->context->errout,
1443
475
                 "SiRF IV: TCXO 0x5D (93), SID: %d (%s)%s\n",
1444
475
                 buf[1], definition, output);
1445
1446
475
        return mask;
1447
544
    }
1448
1449
/* Software Version String MID 6
1450
 * response to Poll Software Version MID 132 */
1451
    static gps_mask_t sirf_msg_swversion(struct gps_device_t *session,
1452
                                         unsigned char *buf, size_t len)
1453
363
    {
1454
363
        double fv;
1455
363
        unsigned char *cp;
1456
1457
363
        if (1 > len) {
1458
0
            GPSD_LOG(LOG_INF, &session->context->errout,
1459
0
                     "SiRF: MID (0x06) runt len %zu\n", len);
1460
0
            return 0;
1461
0
        }
1462
1463
363
        if ((3 < len) &&
1464
159
            (len == (unsigned int)(buf[1] + buf[2] + 3))) {
1465
            // new style message, Version 4+, max 162 bytes
1466
11
            (void)strlcpy(session->subtype, (char *)buf + 3,
1467
11
                          sizeof(session->subtype));
1468
11
            (void)strlcat(session->subtype, ";", sizeof(session->subtype));
1469
11
            (void)strlcat(session->subtype, (char *)buf + 3 + buf[1],
1470
11
                          sizeof(session->subtype));
1471
11
            session->driver.sirf.driverstate |= SIRF_GE_232;
1472
            // FIXME: this only finding major version, not minor version
1473
253
            for (cp = buf + 1; *cp != (unsigned char)'\0' &&
1474
244
                 0 == ISDIGIT(*cp); cp++) {
1475
242
                continue;
1476
242
            }
1477
11
            fv = safe_atof((const char *)cp);
1478
352
        } else {
1479
            // old style, version 3 and below
1480
1481
352
            (void)strlcpy(session->subtype, (char *)buf + 1,
1482
352
                          sizeof(session->subtype));
1483
1484
911
            for (cp = buf + 1; *cp != (unsigned char)'\0' &&
1485
648
                 0 == ISDIGIT(*cp); cp++) {
1486
559
                continue;
1487
559
            }
1488
352
            fv = safe_atof((const char *)cp);
1489
352
            if (231 > fv) {
1490
46
                session->driver.sirf.driverstate |= SIRF_LT_231;
1491
46
                if (200 < fv) {
1492
1
                    sirfbin_mode(session, 0);
1493
1
                }
1494
306
            } else if (232 > fv) {
1495
1
                session->driver.sirf.driverstate |= SIRF_EQ_231;
1496
305
            } else {
1497
305
                session->driver.sirf.driverstate |= SIRF_GE_232;
1498
305
            }
1499
352
            if (strstr((char *)(buf + 1), "ES")) {
1500
1
                GPSD_LOG(LOG_INF, &session->context->errout,
1501
1
                         "SiRF: Firmware has XTrac capability\n");
1502
1
            }
1503
352
        }
1504
363
        GPSD_LOG(LOG_PROG, &session->context->errout,
1505
363
                 "SiRF: FV 0x06: fv: %0.2f, driverstate %0x "
1506
363
                 "subtype '%s' len %lu buf1 %u buf2 %u\n",
1507
363
                 fv, session->driver.sirf.driverstate,
1508
363
                 session->subtype, (long)len, buf[1], buf[2]);
1509
363
        session->driver.sirf.time_seen = 0;
1510
363
        return DEVICEID_SET;
1511
363
    }
1512
1513
/* subframe data MID 8
1514
 * Use Message ID 14 and 15 instead?
1515
 * Use Message ID 56, Sub ID 5, instead? */
1516
    static gps_mask_t sirf_msg_navdata(struct gps_device_t *session,
1517
                                       unsigned char *buf, size_t len)
1518
73
    {
1519
73
        unsigned int i, chan, svid;
1520
73
        uint32_t words[10];
1521
73
        const unsigned int numwords = 10;
1522
1523
73
        if (43 != len) {
1524
70
            GPSD_LOG(LOG_INF, &session->context->errout,
1525
70
                     "SiRF: MID (0x08) runt len %zu\n", len);
1526
70
            return 0;
1527
70
        }
1528
1529
3
        chan = (unsigned int)getub(buf, 1);
1530
3
        svid = (unsigned int)getub(buf, 2);
1531
1532
33
        for (i = 0; i < numwords; i++) {
1533
            // packing undocumented...
1534
30
            words[i] = (uint32_t)getbeu32(buf, 4 * i + 3);
1535
30
        }
1536
1537
3
        GPSD_LOG(LOG_DATA, &session->context->errout,
1538
3
                 "SiRF: NavData chan %u svid %u\n", chan, svid);
1539
1540
        // SiRF recommends at least 57600 for SiRF IV nav data
1541
3
        if (57600 > session->gpsdata.dev.baudrate) {
1542
            // some USB are also too slow, no way to tell which ones
1543
3
            if (session->context->readonly ||
1544
2
                session->context->passive) {
1545
                // FIXME: duplicated in gpsd_internet_subframe_raw()
1546
2
                GPSD_LOG(LOG_WARN, &session->context->errout,
1547
2
                         "WARNING: SiRF: link too slow.\n");
1548
2
            } else {
1549
1
                GPSD_LOG(LOG_WARN, &session->context->errout,
1550
1
                         "WARNING: SiRF: link too slow, disabling subframes.\n");
1551
1
                (void)sirf_write(session, disablesubframe, sizeof(disablesubframe));
1552
1
            }
1553
3
        }
1554
1555
3
        return gpsd_interpret_subframe_raw(session, GNSSID_GPS, 0, svid, words,
1556
3
                                           numwords);
1557
73
    }
1558
1559
// max channels allowed in old MID 4 SiRF format
1560
325
#define SIRF_CHANNELS   12
1561
1562
/* decode Measured Tracker Data response ID 4 (0x04)
1563
* deprecated on Sirfstar V, use MID 67,16 instead */
1564
    static gps_mask_t sirf_msg_svinfo(struct gps_device_t *session,
1565
                                      unsigned char *buf, size_t len)
1566
94
    {
1567
94
        int st, i, j, nsv;
1568
94
        uint32_t hsec;    // TOW in hundredths of seconds
1569
94
        timespec_t ts_tow;
1570
94
        char ts_buf[TIMESPEC_LEN];
1571
94
        gps_mask_t mask = 0;
1572
1573
94
        if (188 != len) {
1574
69
            GPSD_LOG(LOG_INF, &session->context->errout,
1575
69
                     "SiRF: MID (0x04) runt len %zu\n", len);
1576
69
            return 0;
1577
69
        }
1578
1579
25
        hsec = getbeu32(buf, 3);
1580
25
        ts_tow.tv_sec = hsec / 100;
1581
25
        ts_tow.tv_nsec = (long)((hsec % 100) * 10000000LL);
1582
25
        session->gpsdata.skyview_time = gpsd_gpstime_resolv(session,
1583
25
                                                            (unsigned short)getbes16(buf, 1), ts_tow);
1584
1585
25
        gpsd_zero_satellites(&session->gpsdata);
1586
325
        for (i = st = nsv = 0; i < SIRF_CHANNELS; i++) {
1587
300
            int cn;
1588
300
            int off = 8 + 15 * i;
1589
300
            bool good;
1590
300
            short prn = (short)getub(buf, off);
1591
300
            unsigned short stat = (unsigned short)getbeu16(buf, off + 3);
1592
1593
300
            session->gpsdata.skyview[st].PRN = prn;
1594
300
            session->gpsdata.skyview[st].svid = prn;
1595
300
            if (120 <= prn &&
1596
133
                158 >= prn) {
1597
                // SBAS
1598
67
                session->gpsdata.skyview[st].gnssid = 1;
1599
233
            } else {
1600
                // GPS
1601
233
                session->gpsdata.skyview[st].gnssid = 0;
1602
233
            }
1603
300
            session->gpsdata.skyview[st].azimuth =
1604
300
                (double)(((unsigned)getub(buf, off + 1) * 3.0) / 2.0);
1605
300
            session->gpsdata.skyview[st].elevation =
1606
300
                (double)((unsigned)getub(buf, off + 2) / 2.0);
1607
300
            cn = 0;
1608
3.30k
            for (j = 0; j < 10; j++) {
1609
3.00k
                cn += (int)getub(buf, off + 5 + j);
1610
3.00k
            }
1611
1612
300
            session->gpsdata.skyview[st].ss = (float)(cn / 10.0);
1613
300
            session->gpsdata.skyview[st].used = (bool)(stat & 0x01);
1614
300
            good = session->gpsdata.skyview[st].PRN != 0 &&
1615
218
                   session->gpsdata.skyview[st].azimuth != 0 &&
1616
188
                   session->gpsdata.skyview[st].elevation != 0;
1617
#ifdef __UNUSED__
1618
                GPSD_LOG(LOG_PROG, &session->context->errout,
1619
                         "SiRF: MTD 4 PRN=%2d El=%3.2f Az=%3.2f ss=%3d stat=%04x %c\n",
1620
                         prn,
1621
                         getub(buf, off + 2) / 2.0,
1622
                         (getub(buf, off + 1) * 3) / 2.0,
1623
                         cn / 10, stat, good ? '*' : ' ');
1624
#endif // UNUSED
1625
300
            if (good != 0) {
1626
163
                st += 1;
1627
163
                if (stat & 0x01) {
1628
73
                    nsv++;
1629
73
                }
1630
163
            }
1631
300
        }
1632
25
        if (MAXCHANNELS < st) {
1633
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1634
0
                     "SiRF: MTD 4 too many satellites %d\n", st);
1635
0
            st = MAXCHANNELS;
1636
0
        }
1637
25
        session->gpsdata.satellites_visible = st;
1638
25
        session->gpsdata.satellites_used = nsv;
1639
        // mark SBAS sats in use if SBAS was in use as of the last MID 27
1640
188
        for (i = 0; i < st; i++) {
1641
163
            int prn = session->gpsdata.skyview[i].PRN;
1642
163
            if (120 <= prn &&
1643
108
                158 >= prn &&
1644
59
                STATUS_DGPS == session->lastfix.status &&
1645
0
                SIRF_DGPS_SOURCE_SBAS == session->driver.sirf.dgps_source) {
1646
                /* used does not seem right, DGPS means got the correction
1647
                 * data, not that the geometry was improved... */
1648
0
                session->gpsdata.skyview[i].used = true;
1649
0
            }
1650
163
        }
1651
25
        if (3 <= st) {
1652
            // SiRF says if 3 sats in view the time is good
1653
18
            mask |= NTPTIME_IS;
1654
18
            GPSD_LOG(LOG_PROG, &session->context->errout,
1655
18
                     "SiRF: MTD 0x04 NTPD valid time seen %#02x time %s, "
1656
18
                     "leap:%d\n",
1657
18
                     session->driver.sirf.time_seen,
1658
18
                     timespec_str(&session->gpsdata.skyview_time, ts_buf,
1659
18
                                  sizeof(ts_buf)),
1660
18
                     session->context->leap_seconds);
1661
18
        }
1662
25
        GPSD_LOG(LOG_DATA, &session->context->errout,
1663
25
                 "SiRF: MTD 0x04: visible=%d mask={SATELLITE}\n",
1664
25
                 session->gpsdata.satellites_visible);
1665
25
        return mask | SATELLITE_SET;
1666
94
    }
1667
1668
// return NTP time-offset fudge factor for this device
1669
// FIXME: this is pointless...
1670
    static double sirf_time_offset(struct gps_device_t *session)
1671
0
    {
1672
0
        double retval = 0;
1673
1674
        // we need to have seen UTC time with a valid leap-year offset
1675
0
        if (0 != (session->driver.sirf.time_seen & TIME_SEEN_UTC_2)) {
1676
0
            retval = NAN;
1677
0
        } else if ((unsigned char)52 == session->driver.sirf.lastid) {
1678
            // the PPS time message
1679
0
            retval = 0.3;
1680
0
        } else if ((unsigned char)98 == session->driver.sirf.lastid) {
1681
            // u-blox EMND message
1682
0
            retval = 0.570;
1683
0
        } else if ((unsigned char)41 == session->driver.sirf.lastid) {
1684
            // geodetic-data message
1685
0
            retval = 0.570;
1686
0
        } else if ((unsigned char)2 == session->driver.sirf.lastid) {
1687
            // the Navigation Solution message
1688
0
            if (SOURCE_USB == session->sourcetype ||
1689
0
                SOURCE_ACM == session->sourcetype) {
1690
0
                retval = 0.640;     // USB or ACM, expect +/- 50mS jitter
1691
0
            } else {
1692
0
                switch (session->gpsdata.dev.baudrate) {
1693
0
                default:
1694
0
                    retval = 0.704; // WAG
1695
0
                    break;
1696
0
                case 4800:
1697
0
                    retval = 0.704; // fudge valid at 4800bps
1698
0
                    break;
1699
0
                case 9600:
1700
0
                    retval = 0.688;
1701
0
                    break;
1702
0
                case 19200:
1703
0
                    retval = 0.484;
1704
0
                    break;
1705
0
                case 38400:
1706
0
                    retval = 0.845; // 0.388; ??
1707
0
                    break;
1708
0
                }
1709
0
            }
1710
0
        }
1711
1712
0
        return retval;
1713
0
    }
1714
1715
// Measured Navigation Data Out MND ID 2 (0x02)
1716
    static gps_mask_t sirf_msg_navsol(struct gps_device_t *session,
1717
                                      unsigned char *buf, size_t len)
1718
227
    {
1719
227
        unsigned short navtype;
1720
227
        unsigned short nav_mode2;
1721
227
        unsigned short gps_week;
1722
227
        uint32_t iTOW;
1723
227
        timespec_t tow;
1724
227
        gps_mask_t mask = 0;
1725
227
        char ts_buf[TIMESPEC_LEN];
1726
227
        double d;
1727
1728
        // later versions are 47 bytes long
1729
227
        if (41 > len) {
1730
68
            GPSD_LOG(LOG_INF, &session->context->errout,
1731
68
                     "SiRF: MID (0x02) runt len %zu\n", len);
1732
68
            return 0;
1733
68
        }
1734
1735
        /*
1736
         * A count of satellites used is an unsigned byte at offset 28
1737
         * and an array of unsigned bytes listing satellite PRNs used
1738
         * in this fix begins at offset 29, but we don't use either because
1739
         * in JSON the used bits are reported in the SKY sentence;
1740
         * we get that data from the svinfo packet.
1741
         */
1742
        // position/velocity is bytes 1-18
1743
        // ecef position  is in meters
1744
159
        session->newdata.ecef.x = (double)getbes32(buf, 1);
1745
159
        session->newdata.ecef.y = (double)getbes32(buf, 5);
1746
159
        session->newdata.ecef.z = (double)getbes32(buf, 9);
1747
        // ecef velocity is in meters/second * 8
1748
159
        session->newdata.ecef.vx = (double)getbes16(buf, 13) / 8.0;
1749
159
        session->newdata.ecef.vy = (double)getbes16(buf, 15) / 8.0;
1750
159
        session->newdata.ecef.vz = (double)getbes16(buf, 17) / 8.0;
1751
1752
159
        mask |= ECEF_SET | VECEF_SET;
1753
1754
        // fix status is byte 19
1755
159
        navtype = (unsigned short)getub(buf, 19);
1756
159
        session->newdata.mode = MODE_NO_FIX;
1757
159
        if (0 == (navtype & 0x07)) {
1758
            // no fix
1759
132
            session->newdata.status = STATUS_UNK;
1760
132
        } else if (7 == (navtype & 0x07)) {
1761
12
            session->newdata.status = STATUS_DR;
1762
15
        } else if (0x80 == (navtype & 0x80)) {
1763
3
            session->newdata.status = STATUS_DGPS;
1764
12
        } else {
1765
12
            session->newdata.status = STATUS_GPS;
1766
12
        }
1767
        // byte 20 is HDOP, or PDOP!
1768
159
        d = (double)getub(buf, 20) / 5.0;
1769
159
        if (4 == (navtype & 0x07) ||
1770
158
            6 == (navtype & 0x07)) {
1771
2
            session->newdata.mode = MODE_3D;
1772
2
            session->gpsdata.dop.pdop = d;
1773
2
            mask |= DOP_SET;
1774
157
        } else if (0 != session->newdata.status) {
1775
25
            session->newdata.mode = MODE_2D;
1776
25
            session->gpsdata.dop.hdop = d;
1777
25
            mask |= DOP_SET;
1778
25
        }
1779
        // byte 21 is nav_mode2, not clear how to interpret that
1780
159
        nav_mode2 = getub(buf, 21);
1781
1782
159
        gps_week = getbeu16(buf, 22); // modulo 1024
1783
159
        iTOW = getbeu32(buf, 24);
1784
        /* Gack.  The doc says early SiRF scales iTOW by 100, later ones
1785
         * by 1000.  But that does not seem to be true in sirfstar V. */
1786
159
        tow.tv_sec = iTOW / 100;
1787
159
        tow.tv_nsec = (iTOW % 100) * 10000000LL;
1788
159
        session->newdata.time = gpsd_gpstime_resolv(session, gps_week, tow);
1789
1790
159
        if (MODE_NO_FIX >= session->newdata.mode) {
1791
132
            GPSD_LOG(LOG_PROG, &session->context->errout,
1792
132
                     "SiRF: MND 0x02 NTPD no fix, mode: %d\n",
1793
132
                     session->newdata.mode);
1794
132
        } else {
1795
27
            GPSD_LOG(LOG_PROG, &session->context->errout,
1796
27
                     "SiRF: MND 0x02 NTPD valid time, seen %#02x time %s "
1797
27
                     "leap %d nav_mode2 %#x\n",
1798
27
                     session->driver.sirf.time_seen,
1799
27
                     timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
1800
27
                     session->context->leap_seconds,
1801
27
                     nav_mode2);
1802
27
        }
1803
159
        mask |= TIME_SET | STATUS_SET | MODE_SET | USED_IS;
1804
159
        if (3 <= session->gpsdata.satellites_visible) {
1805
70
            mask |= NTPTIME_IS;
1806
70
        }
1807
1808
159
        GPSD_LOG(LOG_PROG, &session->context->errout,
1809
159
                 "SiRF: MND 0x02: Navtype %#0x, Status %d mode %d "
1810
159
                 "gps_week %u iTOW %u\n",
1811
159
                 navtype, session->newdata.status, session->newdata.mode,
1812
159
                 gps_week, iTOW);
1813
159
        GPSD_LOG(LOG_DATA, &session->context->errout,
1814
159
                 "SiRF: MND 0x02: time %s ecef x: %.2f y: %.2f z: %.2f "
1815
159
                 "mode %d status %d hdop %.2f used %d\n",
1816
159
                 timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
1817
159
                 session->newdata.ecef.x,
1818
159
                 session->newdata.ecef.y, session->newdata.ecef.z,
1819
159
                 session->newdata.mode, session->newdata.status,
1820
159
                 session->gpsdata.dop.hdop, session->gpsdata.satellites_used);
1821
159
        return mask;
1822
227
    }
1823
1824
/***************************************************************************
1825
*
1826
*   Geodetic Navigation Data - MID 41 (0x29)
1827
*
1828
*   We've stopped interpreting GND (0x29) for the following reasons:
1829
*
1830
*   1) Versions of SiRF firmware still in wide circulation (and likely to be
1831
*   so for a while) don't report a valid time field, leading to annoying
1832
*   twice-per-second jitter in client displays.
1833
*
1834
*   2) What we wanted out of this that MND didn't give us was horizontal and
1835
*   vertical error estimates. But we have to do our own error estimation by
1836
*   computing DOPs from the skyview covariance matrix anyway, because we
1837
*   want separate epx and epy errors a la NMEA 3.0.
1838
*
1839
*   3) The fix-merge logic in gpsd.c is (unavoidably) NMEA-centric and
1840
*   thinks multiple sentences in one cycle should be treated as
1841
*   incremental updates.  This leads to various silly results when (as
1842
*   in GND) a subsequent sentence is (a) intended to be a complete fix
1843
*   in itself, and (b) frequently broken.
1844
*
1845
*   4) Ignoring this dodgy sentence allows us to go to a nice clean single
1846
*   fix update per cycle.
1847
*
1848
*   Code left in place in case we need to reverse this decision.
1849
*
1850
*   update: Jan 2022
1851
*
1852
*   Decode the sentence, we need the extended week.
1853
*
1854
*   update: November 2026
1855
*
1856
*   The extended week is a lie.
1857
*
1858
*   The cycle ender seems to be preventing the previously seen duplicate TPV.
1859
*   For now be cautious on the mask.
1860
*
1861
***************************************************************************/
1862
    static gps_mask_t sirf_msg_geodetic(struct gps_device_t *session,
1863
                                        unsigned char *buf, size_t len)
1864
93
    {
1865
93
        unsigned navtype;
1866
93
        gps_mask_t mask = 0;
1867
93
        char ts_buf[TIMESPEC_LEN];
1868
93
        unsigned gps_week;
1869
93
        timespec_t tow;
1870
93
        unsigned long iTOW;
1871
1872
93
        if (91 != len) {
1873
68
            GPSD_LOG(LOG_INF, &session->context->errout,
1874
68
                     "SiRF: MID (0x29) runt len %zu\n", len);
1875
68
            return 0;
1876
68
        }
1877
1878
25
        navtype = getbeu16(buf, 3);
1879
25
        if (0 == (navtype & 0x07)) {
1880
            // no fix
1881
1
            session->newdata.status = STATUS_UNK;
1882
24
        } else if (7 == (navtype & 0x07)) {
1883
6
            session->newdata.status = STATUS_DR;
1884
18
        } else if (0x80 == (navtype & 0x80)) {
1885
13
            session->newdata.status = STATUS_DGPS;
1886
13
        } else {
1887
5
            session->newdata.status = STATUS_GPS;
1888
5
        }
1889
1890
25
        session->newdata.mode = MODE_NO_FIX;
1891
25
        if (4 == (navtype & 0x07) ||
1892
24
            6 == (navtype & 0x07)) {
1893
6
            session->newdata.mode = MODE_3D;
1894
19
        } else if (session->newdata.status) {
1895
18
            session->newdata.mode = MODE_2D;
1896
18
        }
1897
1898
25
        gps_week = getbeu16(buf, 5); // extended gps week
1899
        // BU-353-W, extended week is a lie, truncate it
1900
        // gps_week &= 0x03ff;  // breaks test/daemon/sirf2.log
1901
25
        iTOW = getbeu32(buf, 7);
1902
25
        MSTOTS(&tow, iTOW);
1903
25
        session->newdata.time = gpsd_gpstime_resolv(session, gps_week, tow);
1904
25
        GPSD_LOG(LOG_PROG, &session->context->errout,
1905
25
                 "SiRF: GND 0x29: Navtype 0x%0x Status %d mode %d week %u "
1906
25
                 "iTOW %lu\n",
1907
25
                 navtype, session->newdata.status, session->newdata.mode,
1908
25
                 gps_week, iTOW);
1909
25
        mask |= STATUS_SET | MODE_SET | TIME_SET;
1910
1911
25
        session->newdata.latitude = getbes32(buf, 23) * 1e-7;
1912
25
        session->newdata.longitude = getbes32(buf, 27) * 1e-7;
1913
25
        if (MODE_NO_FIX < session->newdata.mode) {
1914
24
            unsigned u;
1915
1916
24
            if (3 <= session->gpsdata.satellites_visible) {
1917
0
                mask |= NTPTIME_IS;
1918
0
            }
1919
24
            if (0 != session->newdata.latitude &&
1920
20
                0 != session->newdata.longitude) {
1921
                // annoying if you happen be be at 9/0
1922
19
                mask |= LATLON_SET;
1923
19
            }
1924
1925
24
            u = getbeu32(buf, 50);
1926
24
            if (0 != u) {
1927
23
                session->newdata.eph = u * 1e-2;
1928
23
                mask |= HERR_SET;
1929
23
            }
1930
24
            u = getbeu32(buf, 54);
1931
24
            if (0 != u) {
1932
                // vertical error
1933
22
                session->newdata.epv = u * 1e-2;
1934
22
                mask |= VERR_SET;
1935
22
            }
1936
24
            u = getbeu16(buf, 62);
1937
24
            if (0 != u) {
1938
18
                session->newdata.eps = u * 1e-2;
1939
18
                mask |= SPEEDERR_SET;
1940
18
            }
1941
            // HDOP should be available at byte 89, but in 231 it's zero.
1942
24
            u = getub(buf, 89);
1943
24
            if (0 != u) {
1944
17
                session->gpsdata.dop.hdop = u * 0.2;
1945
17
                mask |= DOP_SET;
1946
17
            }
1947
24
        }
1948
1949
1950
25
        if ((MODE_NO_FIX < session->newdata.mode) &&
1951
24
            (session->driver.sirf.driverstate & SIRF_GE_232)) {
1952
0
            struct tm unpacked_date = {0};
1953
0
            unsigned subseconds;
1954
            /*
1955
             * Early versions of the SiRF protocol manual don't document
1956
             * this sentence at all.  Some that do incorrectly
1957
             * describe UTC Day, Hour, and Minute as 2-byte quantities,
1958
             * not 1-byte. Chris Kuethe, our SiRF expert, tells us:
1959
             *
1960
             * "The Geodetic Navigation packet (0x29) was not fully
1961
             * implemented in firmware prior to version 2.3.2. So for
1962
             * anyone running 231.000.000 or earlier (including ES,
1963
             * SiRFDRive, XTrac trains) you won't get UTC time. I don't
1964
             * know what's broken in firmwares before 2.3.1..."
1965
             *
1966
             * To work around the incomplete implementation of this
1967
             * packet in 231, we used to assume that only the altitude field
1968
             * from this packet is valid.  But even this doesn't necessarily
1969
             * seem to be the case.  Instead, we do our own computation
1970
             * of geoid separation now.
1971
             *
1972
             * UTC is left all zeros in 231 and older firmware versions,
1973
             * and misdocumented in version 1.4 of the Protocol Reference.
1974
             *            Documented:        Real:
1975
             * UTC year       2               2
1976
             * UTC month      1               1
1977
             * UTC day        2               1
1978
             * UTC hour       2               1
1979
             * UTC minute     2               1
1980
             * UTC second     2               2
1981
             *                11              8
1982
             *
1983
             * Documentation of this field was corrected in the 1.6 version
1984
             * of the protocol manual.
1985
             */
1986
0
            memset(&unpacked_date, 0, sizeof(unpacked_date));
1987
0
            unpacked_date.tm_year = (int)getbeu16(buf, 11) - 1900;
1988
0
            unpacked_date.tm_mon = (int)buf[13] - 1;
1989
0
            unpacked_date.tm_mday = (int)buf[14];
1990
0
            unpacked_date.tm_hour = (int)buf[15];
1991
0
            unpacked_date.tm_min = (int)buf[16];
1992
0
            subseconds = getbeu16(buf, 17);
1993
0
            unpacked_date.tm_sec = subseconds / 1000;
1994
            // we use iTOW time, not this UTC time, which is often missing
1995
1996
0
            if (MODE_2D <= session->newdata.mode &&
1997
0
                0 != unpacked_date.tm_year) {
1998
0
                GPSD_LOG(LOG_PROG, &session->context->errout,
1999
0
                         "SiRF: GND 0x29 NTPD valid time, seen=%#02x "
2000
0
                         "%4d/%02d/%02d %02d:%02d:%02.6f\n",
2001
0
                         session->driver.sirf.time_seen,
2002
0
                         unpacked_date.tm_year + 1900,
2003
0
                         unpacked_date.tm_mon + 1,
2004
0
                         unpacked_date.tm_mday,
2005
0
                         unpacked_date.tm_hour,
2006
0
                         unpacked_date.tm_min,
2007
0
                         subseconds * 0.001);
2008
0
            }
2009
2010
            // alititude WGS84
2011
0
            session->newdata.altHAE = getbes32(buf, 31) * 1e-2;
2012
0
            session->newdata.altMSL = getbes32(buf, 35) * 1e-2;
2013
            // Let gpsd_error_model() deal with geoid_sep and altHAE
2014
            // skip 1 byte of map datum
2015
0
            session->newdata.speed = getbeu16(buf, 40) * 1e-2;
2016
            // true track
2017
0
            session->newdata.track = getbeu16(buf, 42) * 1e-2;
2018
            // skip 2 bytes of magnetic variation, doc says not implemented
2019
0
            session->newdata.climb = getbes16(buf, 46) * 1e-2;
2020
0
            mask |= TIME_SET | SPEED_SET | TRACK_SET;
2021
0
            if (MODE_3D == session->newdata.mode) {
2022
0
                mask |= ALTITUDE_SET | CLIMB_SET;
2023
0
            }
2024
0
        }
2025
2026
25
        GPSD_LOG(LOG_PROG, &session->context->errout,
2027
25
                 "SiRF: GND 0x29: time=%s lat=%.2f lon=%.2f altHAE=%.2f "
2028
25
                 "track=%.2f speed=%.2f mode=%d status=%d\n",
2029
25
                 timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
2030
25
                 session->newdata.latitude,
2031
25
                 session->newdata.longitude,
2032
25
                 session->newdata.altHAE,
2033
25
                 session->newdata.track,
2034
25
                 session->newdata.speed,
2035
25
                 session->newdata.mode,
2036
25
                 session->newdata.status);
2037
25
        return mask;
2038
93
    }
2039
2040
// decode Navigation Parameters MID 19 (0x13) response to ID 152
2041
    static gps_mask_t sirf_msg_sysparam(struct gps_device_t *session,
2042
                                        unsigned char *buf, size_t len)
2043
67
    {
2044
2045
67
        if (65 > len) {
2046
66
            GPSD_LOG(LOG_INF, &session->context->errout,
2047
66
                     "SiRF: MID (0x13) runt len %zu\n", len);
2048
66
            return 0;
2049
66
        }
2050
2051
        // save these to restore them in the revert method
2052
1
        session->driver.sirf.nav_parameters_seen = true;
2053
1
        session->driver.sirf.altitude_hold_mode = (unsigned char)getub(buf, 5);
2054
1
        session->driver.sirf.altitude_hold_source = (unsigned char)getub(buf, 6);
2055
1
        session->driver.sirf.altitude_source_input = getbes16(buf, 7);
2056
1
        session->driver.sirf.degraded_mode = (unsigned char)getub(buf, 9);
2057
1
        session->driver.sirf.degraded_timeout = (unsigned char)getub(buf, 10);
2058
1
        session->driver.sirf.dr_timeout = (unsigned char)getub(buf, 11);
2059
1
        session->driver.sirf.track_smooth_mode = (unsigned char)getub(buf, 12);
2060
1
        return 0;
2061
67
    }
2062
2063
/* DGPS status MID 27 (0x1b)
2064
 * only documentented from prorocol version 1.7 (2005) onwards */
2065
    static gps_mask_t sirf_msg_dgpsstatus(struct gps_device_t *session,
2066
                                          unsigned char *buf, size_t len UNUSED)
2067
66
    {
2068
66
        session->driver.sirf.dgps_source = (unsigned int)getub(buf, 1);
2069
66
        return 0;
2070
66
    }
2071
2072
/* decode Extended Measured Navigation Data MID 98 (0x62)
2073
 * Used in u-blox TIM GPS receivers (SiRF2-ublox)
2074
 * "Firmware Release 2.1 UBX 1.0" */
2075
    static gps_mask_t sirf_msg_ublox(struct gps_device_t *session,
2076
                                     unsigned char *buf, size_t len UNUSED)
2077
71
    {
2078
71
        gps_mask_t mask;
2079
71
        unsigned short navtype;
2080
71
        char ts_buf[TIMESPEC_LEN];
2081
2082
71
        if (39 != len) {
2083
66
            GPSD_LOG(LOG_INF, &session->context->errout,
2084
66
                     "SiRF: MID (0x62) runt len %zu\n", len);
2085
66
            return 0;
2086
66
        }
2087
2088
        // this packet is only sent by u-blox firmware from version 1.32
2089
5
        mask = LATLON_SET | ALTITUDE_SET | SPEED_SET | TRACK_SET | CLIMB_SET |
2090
5
               STATUS_SET | MODE_SET;
2091
5
        session->newdata.latitude = (double)getbes32(buf, 1) * RAD_2_DEG * 1e-8;
2092
5
        session->newdata.longitude = (double)getbes32(buf, 5) * RAD_2_DEG * 1e-8;
2093
        // defaults to WGS84
2094
5
        session->newdata.altHAE = (double)getbes32(buf, 9) * 1e-3;
2095
5
        session->newdata.speed = (double)getbes32(buf, 13) * 1e-3;
2096
5
        session->newdata.climb = (double)getbes32(buf, 17) * 1e-3;
2097
5
        session->newdata.track = (double)getbes32(buf, 21) * RAD_2_DEG * 1e-8;
2098
2099
5
        navtype = (unsigned short)getub(buf, 25);
2100
5
        session->newdata.status = STATUS_UNK;
2101
5
        session->newdata.mode = MODE_NO_FIX;
2102
5
        if (0 == (navtype & 0x07)) {
2103
            // no fix
2104
1
            session->newdata.status = STATUS_UNK;
2105
4
        } else if (7 == (navtype & 0x07)) {
2106
1
            session->newdata.status = STATUS_DR;
2107
3
        } else if (0x80 == (navtype & 0x80)) {
2108
1
            session->newdata.status = STATUS_DGPS;
2109
2
        } else {
2110
2
            session->newdata.status = STATUS_GPS;
2111
2
        }
2112
5
        if (4 == (navtype & 0x07) ||
2113
4
            6 == (navtype & 0x07)) {
2114
2
            session->newdata.mode = MODE_3D;
2115
3
        } else if (session->newdata.status) {
2116
2
            session->newdata.mode = MODE_2D;
2117
2
        }
2118
5
        GPSD_LOG(LOG_PROG, &session->context->errout,
2119
5
                 "SiRF: EMND 0x62: Navtype = 0x%0x, Status = %d, mode = %d\n",
2120
5
                 navtype, session->newdata.status, session->newdata.mode);
2121
2122
5
        if (navtype & 0x40) {   // UTC corrected timestamp?
2123
4
            struct tm unpacked_date = {0};
2124
4
            uint32_t msec;
2125
2126
4
            mask |= TIME_SET;
2127
4
            if (3 <= session->gpsdata.satellites_visible) {
2128
0
                mask |= NTPTIME_IS;
2129
0
            }
2130
4
            memset(&unpacked_date, 0, sizeof(unpacked_date));
2131
4
            unpacked_date.tm_year = (int)getbeu16(buf, 26) - 1900;
2132
4
            unpacked_date.tm_mon = (int)getub(buf, 28) - 1;
2133
4
            unpacked_date.tm_mday = (int)getub(buf, 29);
2134
4
            unpacked_date.tm_hour = (int)getub(buf, 30);
2135
4
            unpacked_date.tm_min = (int)getub(buf, 31);
2136
4
            msec = getbeu16(buf, 32);
2137
4
            unpacked_date.tm_sec = msec / 1000;
2138
4
            session->newdata.time.tv_sec = mkgmtime(&unpacked_date);
2139
            // ms to ns
2140
4
            session->newdata.time.tv_nsec = (msec % 1000) * 1000000LL;
2141
4
            if (0 == (session->driver.sirf.time_seen & TIME_SEEN_UTC_2)) {
2142
4
                GPSD_LOG(LOG_RAW, &session->context->errout,
2143
4
                         "SiRF: EMND 0x62 NTPD just SEEN_UTC_2\n");
2144
4
            }
2145
4
            GPSD_LOG(LOG_PROG, &session->context->errout,
2146
4
                     "SiRF: EMND 0x62 NTPD valid time, seen=%#02x\n",
2147
4
                     session->driver.sirf.time_seen);
2148
4
            session->driver.sirf.time_seen |= TIME_SEEN_UTC_2;
2149
            /* The mode byte, bit 6 tells us if leap second is valid.
2150
             * But not what the leap second is.
2151
             * session->context->valid |= LEAP_SECOND_VALID;
2152
             */
2153
4
        }
2154
2155
5
        session->gpsdata.dop.gdop = (int)getub(buf, 34) / 5.0;
2156
5
        session->gpsdata.dop.pdop = (int)getub(buf, 35) / 5.0;
2157
5
        session->gpsdata.dop.hdop = (int)getub(buf, 36) / 5.0;
2158
5
        session->gpsdata.dop.vdop = (int)getub(buf, 37) / 5.0;
2159
5
        session->gpsdata.dop.tdop = (int)getub(buf, 38) / 5.0;
2160
5
        mask |= DOP_SET;
2161
5
        session->driver.sirf.driverstate |= UBLOX;
2162
5
        GPSD_LOG(LOG_DATA, &session->context->errout,
2163
5
                 "SiRF: EMND 0x62: time=%s lat=%.2f lon=%.2f altHAE=%.2f "
2164
5
                 "speed=%.2f track=%.2f climb=%.2f mode=%d status=%d gdop=%.2f "
2165
5
                 "pdop=%.2f hdop=%.2f vdop=%.2f tdop=%.2f\n",
2166
5
                 timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
2167
5
                 session->newdata.latitude,
2168
5
                 session->newdata.longitude, session->newdata.altHAE,
2169
5
                 session->newdata.speed, session->newdata.track,
2170
5
                 session->newdata.climb, session->newdata.mode,
2171
5
                 session->newdata.status, session->gpsdata.dop.gdop,
2172
5
                 session->gpsdata.dop.pdop, session->gpsdata.dop.hdop,
2173
5
                 session->gpsdata.dop.vdop, session->gpsdata.dop.tdop);
2174
5
        return mask;
2175
71
    }
2176
2177
// decode PPS Time MID 52 (0x34)
2178
    static gps_mask_t sirf_msg_ppstime(struct gps_device_t *session,
2179
                                       unsigned char *buf, size_t len)
2180
79
    {
2181
79
        gps_mask_t mask = 0;
2182
2183
79
        if (19 > len) {
2184
69
            GPSD_LOG(LOG_INF, &session->context->errout,
2185
69
                     "SiRF: MID (0x34) runt len %zu\n", len);
2186
69
            return 0;
2187
69
        }
2188
2189
10
        GPSD_LOG(LOG_PROG, &session->context->errout,
2190
10
                 "SiRF: PPS 0x34: Status = %#02x\n",
2191
10
                 getub(buf, 14));
2192
10
        if (0x07 == ((int)getub(buf, 14) & 0x07)) {
2193
            // valid UTC time
2194
8
            struct tm unpacked_date = {0};
2195
2196
8
            memset(&unpacked_date, 0, sizeof(unpacked_date));
2197
8
            unpacked_date.tm_hour = (int)getub(buf, 1);
2198
8
            unpacked_date.tm_min = (int)getub(buf, 2);
2199
8
            unpacked_date.tm_sec = (int)getub(buf, 3);
2200
8
            unpacked_date.tm_mday = (int)getub(buf, 4);
2201
8
            unpacked_date.tm_mon = (int)getub(buf, 5) - 1;
2202
8
            unpacked_date.tm_year = (int)getbeu16(buf, 6) - 1900;
2203
8
            session->newdata.time.tv_sec = mkgmtime(&unpacked_date);
2204
8
            session->newdata.time.tv_nsec = 0;
2205
8
            session->context->leap_seconds = (int)getbeu16(buf, 8);
2206
            // Ignore UTCOffsetFrac1
2207
8
            session->context->valid |= LEAP_SECOND_VALID;
2208
8
            if (0 == (session->driver.sirf.time_seen & TIME_SEEN_UTC_2)) {
2209
8
                GPSD_LOG(LOG_RAW, &session->context->errout,
2210
8
                         "SiRF: NTPD just SEEN_UTC_2\n");
2211
8
            }
2212
8
            GPSD_LOG(LOG_PROG, &session->context->errout,
2213
8
                     "SiRF: NTPD valid time MID 0x34, seen=%#02x, leap=%d\n",
2214
8
                     session->driver.sirf.time_seen,
2215
8
                     session->context->leap_seconds);
2216
8
            session->driver.sirf.time_seen |= TIME_SEEN_UTC_2;
2217
8
            mask |= TIME_SET;
2218
8
            if (3 <= session->gpsdata.satellites_visible) {
2219
0
                mask |= NTPTIME_IS;
2220
0
            }
2221
8
        }
2222
10
        return mask;
2223
79
    }
2224
2225
// decode Navigation Library Measurement Data MID 28 (0x38)
2226
    static gps_mask_t sirf_msg_nl(struct gps_device_t *session,
2227
                                  unsigned char *buf, size_t len)
2228
122
    {
2229
2230
122
        if (67 != len) {
2231
111
            GPSD_LOG(LOG_INF, &session->context->errout,
2232
111
                     "SiRF: MID (0x38) runt len %zu\n", len);
2233
111
            return 0;
2234
111
        }
2235
2236
11
        switch (buf[1]) {
2237
1
        case 1:
2238
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2239
1
                     "SiRF IV: unused NL 0x38, SubID: 1, GPS Data\n");
2240
1
            break;
2241
1
        case 2:
2242
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2243
1
                     "SiRF IV: unused NL 0x38, SubID: 2, EE Integrity\n");
2244
1
            break;
2245
1
        case 3:
2246
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2247
1
                     "SiRF IV: unused NL 0x38, SubID: 3, EE Integrity\n");
2248
1
            break;
2249
1
        case 4:
2250
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2251
1
                     "SiRF IV: unused NL 0x38, SubID: 4, EE Clock Bias\n");
2252
1
            break;
2253
1
        case 5:
2254
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2255
1
                     "SiRF IV: unused NL 0x38, SubID: 4, 50bps\n");
2256
1
            break;
2257
1
        case 32:
2258
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2259
1
                     "SiRF IV: unused NL 0x38, SubID: 4, ECLM ACK/NACK\n");
2260
1
            break;
2261
1
        case 33:
2262
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2263
1
                     "SiRF IV: unused NL 0x38, SubID: 4, ECLM EE Age\n");
2264
1
            break;
2265
1
        case 34:
2266
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2267
1
                     "SiRF IV: unused NL 0x38, SubID: 4, ECLM SGEE Age\n");
2268
1
            break;
2269
1
        case 35:
2270
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2271
1
                     "SiRF IV: unused NL 0x38, SubID: 4, ECLM Download Initiate\n");
2272
1
            break;
2273
1
        case 255:
2274
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2275
1
                     "SiRF IV: unused NL 0x38, SubID: 4, EE ACK\n");
2276
1
            break;
2277
1
        default:
2278
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2279
11
                     "SiRF IV: unused NL 0x38, unknown SubID: %d\n",
2280
11
                     buf[1]);
2281
11
        }
2282
2283
11
        return 0;
2284
11
    }
2285
2286
// decode  Extended Ephemeris Data MID 56 (0x38)
2287
    static gps_mask_t sirf_msg_ee(struct gps_device_t *session,
2288
                                  unsigned char *buf, size_t len)
2289
74
    {
2290
2291
74
        if (67 != len) {
2292
71
            GPSD_LOG(LOG_INF, &session->context->errout,
2293
71
                     "SiRF: MID (0x38) runt len %zu\n", len);
2294
71
            return 0;
2295
71
        }
2296
2297
3
        switch (buf[1]) {
2298
1
        case 1:
2299
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2300
1
                     "SiRF IV: unused EE 0x40, SubID: 1\n");
2301
1
            break;
2302
1
        case 2:
2303
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2304
1
                     "SiRF IV: unused EE 0x40, SubID: 2, PRN: %d\n",
2305
1
                     buf[2]);
2306
1
            break;
2307
1
        default:
2308
1
            GPSD_LOG(LOG_PROG, &session->context->errout,
2309
3
                     "SiRF IV: unused EE 0x40, unknown SubID: %d\n",
2310
3
                     buf[1]);
2311
3
        }
2312
2313
3
        return 0;
2314
3
    }
2315
2316
2317
    gps_mask_t sirf_parse(struct gps_device_t *session, unsigned char *buf,
2318
                          size_t len)
2319
6.18k
    {
2320
2321
6.18k
        if (0 == len) {
2322
0
            GPSD_LOG(LOG_INF, &session->context->errout,
2323
0
                     "SiRF: runt len %zu\n", len);
2324
0
            return 0;
2325
0
        }
2326
2327
6.18k
        buf += 4;
2328
6.18k
        len -= 8;
2329
        // cast for 32/64 bit compatiility
2330
6.18k
        GPSD_LOG(LOG_RAW, &session->context->errout,
2331
6.18k
                 "SiRF: Raw packet type %#04x len %ld\n", buf[0],
2332
6.18k
                 (long)len);
2333
6.18k
        session->driver.sirf.lastid = buf[0];
2334
2335
        // could change if the set of messages we enable does
2336
6.18k
        session->cycle_end_reliable = true;
2337
2338
6.18k
        switch (buf[0]) {
2339
227
        case 0x02:              // Measure Navigation Data Out MID 2
2340
227
            if (0 == (session->driver.sirf.driverstate & UBLOX)) {
2341
227
                return sirf_msg_navsol(session, buf,
2342
227
                                       len) | (CLEAR_IS | REPORT_IS);
2343
227
            }
2344
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
2345
0
                     "SiRF: MID 2 (0x02) MND skipped, u-blox flag is on.\n");
2346
0
            return 0;
2347
2348
94
        case 0x04:              // Measured tracker data out MID 4
2349
94
            return sirf_msg_svinfo(session, buf, len);
2350
2351
66
        case 0x05:              // Raw Tracker Data Out MID 5
2352
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
2353
66
                     "SiRF: unused MID 5 (0x05) Raw Tracker Data\n");
2354
66
            return 0;
2355
2356
363
        case 0x06:              // Software Version String MID 6
2357
363
            return sirf_msg_swversion(session, buf, len);
2358
2359
66
        case 0x07:              // Clock Status Data MID 7
2360
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
2361
66
                     "SiRF: unused MID 7 (0x07) CLK\n");
2362
66
            return 0;
2363
2364
73
        case 0x08:              // subframe data MID 8
2365
            // extract leap-second from this
2366
            /*
2367
             * Chris Kuethe says:
2368
             * "Message 8 is generated as the data is received. It is not
2369
             * buffered on the chip. So when you enable message 8, you'll
2370
             * get one subframe every 6 seconds.  Of the data received, the
2371
             * almanac and ephemeris are buffered and stored, so you can
2372
             * query them at will. Alas, the time parameters are not
2373
             * stored, which is really lame, as the UTC-GPS correction
2374
             * changes 1 second every few years. Maybe."
2375
             */
2376
73
            return sirf_msg_navdata(session, buf, len);
2377
2378
70
        case 0x09:              // CPU Throughput MID 9 (0x09)
2379
70
            GPSD_LOG(LOG_PROG, &session->context->errout,
2380
70
                     "SiRF: THR 0x09: SegStatMax=%.3f, SegStatLat=%3.f, "
2381
70
                     "AveTrkTime=%.3f, Last MS=%u\n",
2382
70
                     (float)getbeu16(buf, 1) / 186, (float)getbeu16(buf, 3) / 186,
2383
70
                     (float)getbeu16(buf, 5) / 186, getbeu16(buf, 7));
2384
70
            return 0;
2385
2386
130
        case 0x0a:              // Error ID Data MID 10
2387
130
            return sirf_msg_errors(session, buf, len);
2388
2389
204
        case 0x0b:              // Command Acknowledgement MID 11
2390
204
            if (2 > len) {
2391
66
                return 0;
2392
66
            }
2393
138
            if (2 == len) {
2394
66
                GPSD_LOG(LOG_PROG, &session->context->errout,
2395
66
                         "SiRF: ACK 0x0b: %#02x\n", getub(buf, 1));
2396
72
            } else {
2397
                // SiRF III+, has ACK ID
2398
72
                GPSD_LOG(LOG_PROG, &session->context->errout,
2399
72
                         "SiRF: ACK 0x0b: %#02x/%02x\n",
2400
72
                         getub(buf, 1), getub(buf, 2));
2401
72
            }
2402
138
            session->driver.sirf.need_ack = 0;
2403
138
            return 0;
2404
2405
1.80k
        case 0x0c:              // Command NAcknowledgement MID 12
2406
1.80k
            if (2 > len) {
2407
79
                return 0;
2408
79
            }
2409
1.72k
            if (2 == len) {
2410
1.63k
                GPSD_LOG(LOG_PROG, &session->context->errout,
2411
1.63k
                         "SiRF: NACK 0x0c: %#02x\n", getub(buf, 1));
2412
1.63k
            } else {
2413
                // SiRF III+, has NACK ID
2414
86
                GPSD_LOG(LOG_PROG, &session->context->errout,
2415
86
                         "SiRF: NACK 0x0c: %#02x/%02x\n",
2416
86
                         getub(buf, 1), getub(buf, 2));
2417
86
            }
2418
            // ugh -- there's no alternative but silent failure here
2419
1.72k
            session->driver.sirf.need_ack = 0;
2420
1.72k
            return 0;
2421
2422
66
        case 0x0d:     // Visible List MID 13
2423
            // no data here not already in MID 67,16
2424
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
2425
66
                     "SiRF: unused MID 13 (0x0d) Visible List, len %zd\n", len);
2426
66
            return 0;
2427
2428
66
        case 0x0e:     // Almanac Data MID 14
2429
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
2430
66
                     "SiRF: unused MID 14 (0x0e) ALM\n");
2431
66
            return 0;
2432
2433
67
        case 0x0f:     // Ephemeris Data MID 15
2434
67
            GPSD_LOG(LOG_PROG, &session->context->errout,
2435
67
                     "SiRF: unused MID 15 (0x0f) EPH\n");
2436
67
            return 0;
2437
2438
101
        case 0x11:     // Differential Corrections MID 17
2439
101
            GPSD_LOG(LOG_PROG, &session->context->errout,
2440
101
                     "SiRF: unused MID 17 (0x11) DIFF\n");
2441
101
            return 0;
2442
2443
66
        case 0x12:     // OK To Send MID 18 (0x12)
2444
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
2445
66
                     "SiRF: MID 18 (0x12) OkToSend: OK = %d\n",
2446
66
                     getub(buf, 1));
2447
66
            return 0;
2448
2449
67
        case 0x13:     // Navigation Parameters MID 19 (0x13)
2450
67
            return sirf_msg_sysparam(session, buf, len);
2451
2452
66
        case 0x1b:     // DGPS status MID 27
2453
66
            return sirf_msg_dgpsstatus(session, buf, len);
2454
2455
67
        case 0x1c:     // Navigation Library Measurement Data MID 28
2456
67
            return sirf_msg_nlmd(session, buf, len);
2457
2458
69
        case 0x1d:     // Navigation Library DGPS Data MID 29
2459
69
            GPSD_LOG(LOG_PROG, &session->context->errout,
2460
69
                     "SiRF: unused MID 29 (0x1d) NLDG\n");
2461
69
            return 0;
2462
2463
71
        case 0x1e:     // Navigation Library SV State Data MID 30
2464
71
            GPSD_LOG(LOG_PROG, &session->context->errout,
2465
71
                     "SiRF: unused MID 30 (0x1e) NLSV\n");
2466
71
            return 0;
2467
2468
68
        case 0x1f:     // Navigation Library Initialization Data MID 31
2469
68
            GPSD_LOG(LOG_PROG, &session->context->errout,
2470
68
                     "SiRF: unused MID 32 (0x1f) NLID\n");
2471
68
            return 0;
2472
2473
93
        case 0x29:     // Geodetic Navigation Data MID 41
2474
93
            return sirf_msg_geodetic(session, buf, len);
2475
2476
71
        case 0x32:     // SBAS corrections MID 50
2477
71
            return sirf_msg_sbas(session, buf, len);
2478
2479
124
        case 0x33:     // MID_SiRFNavNotification MID 51, 0x33
2480
124
            return sirf_msg_navnot(session, buf, len);
2481
2482
79
        case 0x34:     // PPS Time MID 52
2483
            /*
2484
             * Carl Carter from SiRF writes: "We do not output on the
2485
             * second (unless you are using MID 52).  We make
2486
             * measurements in the receiver in time with an internal
2487
             * counter that is not slaved to GPS time, so the measurements
2488
             * are made at a time that wanders around the second.  Then,
2489
             * after the measurements are made (all normalized to the same
2490
             * point in time) we dispatch the navigation software to make
2491
             * a solution, and that solution comes out some 200 to 300 ms
2492
             * after the measurement time.  So you may get a message at
2493
             * 700 ms after the second that uses measurements time tagged
2494
             * 450 ms after the second.  And if some other task jumps up
2495
             * and delays things, that message may not come out until 900
2496
             * ms after the second.  Things can get out of sync to the
2497
             * point that if you try to resolve the GPS time of our 1 PPS
2498
             * pulses using the navigation messages, you will find it
2499
             * impossible to be consistent.  That is why I added
2500
             * MID 52 to our system -- it is tied to the creation of the 1
2501
             * PPS and always comes out right around the top of the
2502
             * second."
2503
             */
2504
79
            return sirf_msg_ppstime(session, buf, len);
2505
2506
74
        case 0x38:     // EE Output MID 56
2507
74
            return sirf_msg_ee(session, buf, len);
2508
2509
122
        case 0x40:     // Nav Library MID 64
2510
122
            return sirf_msg_nl(session, buf, len);
2511
2512
316
        case 0x43:     // Multiconstellation Nav Data Response MID 67
2513
316
            return sirf_msg_67(session, buf, len);
2514
2515
66
        case 0x47:     // Hardware Config MID 71
2516
            // MID_HW_CONFIG_REQ
2517
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
2518
66
                     "SiRF IV: unused MID 71 (0x47) Hardware Config Request, "
2519
66
                     "len %zd\n", len);
2520
66
            return 0;
2521
2522
67
        case 0x51:     // MID_QUERY_RESP MID 81
2523
67
            return sirf_msg_qresp(session, buf, len);
2524
2525
66
        case 0x5c:     // Controller Interference Report MID 92
2526
66
            GPSD_LOG(LOG_PROG, &session->context->errout,
2527
66
                     "SiRF IV: unused MID 92 (0x5c) CW Interference Report\n");
2528
66
            return 0;
2529
2530
610
        case 0x5d:     // TCXO Output MID 93
2531
610
            return sirf_msg_tcxo(session, buf, len);
2532
2533
71
        case 0x62:     // u-blox Extended Measured Navigation Data MID 98
2534
71
            GPSD_LOG(LOG_PROG, &session->context->errout,
2535
71
                     "SiRF: MID 98 (0x62) u-blox EMND\n");
2536
71
            return sirf_msg_ublox(session, buf, len) | (CLEAR_IS | REPORT_IS);
2537
2538
67
        case 0x80:     // Initialize Data Source MID 128
2539
67
            GPSD_LOG(LOG_PROG, &session->context->errout,
2540
67
                     "SiRF: unused MID 128 (0x80) INIT\n");
2541
67
            return 0;
2542
2543
143
        case 0xe1:     // statistics messages MID 225
2544
143
            return sirf_msg_stats(session, buf, len);
2545
2546
180
        case 0xff:     // Debug messages MID 255
2547
180
            (void)sirf_msg_debug(session, buf, len);
2548
180
            return 0;
2549
2550
169
        default:
2551
169
            GPSD_LOG(LOG_PROG, &session->context->errout,
2552
169
                     "SiRF: Unknown packet id %d (%#x) length %zd\n",
2553
169
                     buf[0], buf[0], len);
2554
169
            return 0;
2555
6.18k
        }
2556
6.18k
    }
2557
2558
    static gps_mask_t sirfbin_parse_input(struct gps_device_t *session)
2559
6.51k
    {
2560
6.51k
        if (SIRF_PACKET == session->lexer.type) {
2561
6.18k
            return sirf_parse(session, session->lexer.outbuffer,
2562
6.18k
                              session->lexer.outbuflen);
2563
6.18k
        }
2564
325
        if (NMEA_PACKET == session->lexer.type) {
2565
325
            return nmea_parse((char *)session->lexer.outbuffer, session);
2566
325
        }
2567
0
        return 0;
2568
325
    }
2569
2570
    static void sirfbin_init_query(struct gps_device_t *session)
2571
350
    {
2572
350
        GPSD_LOG(LOG_PROG, &session->context->errout,
2573
350
                 "SiRF: Probing for firmware version.\n");
2574
2575
        // reset binary init steps
2576
350
        session->cfg_stage = 0;
2577
350
        session->cfg_step = 0;
2578
2579
        // MID 132
2580
350
        (void)sirf_write(session, versionprobe, sizeof(versionprobe));
2581
        // ask twice, SiRF IV on USB often misses the first request
2582
350
        (void)sirf_write(session, versionprobe, sizeof(versionprobe));
2583
350
    }
2584
2585
    static void sirfbin_event_hook(struct gps_device_t *session, event_t event)
2586
8.21k
    {
2587
8.21k
        static unsigned char moderevert[] = {
2588
8.21k
            0xa0, 0xa2, 0x00, 0x0e,
2589
8.21k
            0x88,
2590
8.21k
            0x00, 0x00, // pad bytes
2591
8.21k
            0x00,       // degraded mode
2592
8.21k
            0x00, 0x00, // pad bytes
2593
8.21k
            0x00, 0x00, // altitude source
2594
8.21k
            0x00,       // altitude hold mode
2595
8.21k
            0x00,       // use last computed alt
2596
8.21k
            0x00,       // reserved
2597
8.21k
            0x00,       // degraded mode timeout
2598
8.21k
            0x00,       // dead reckoning timeout
2599
8.21k
            0x00,       // track smoothing
2600
8.21k
            0x00, 0x00, 0xb0, 0xb3
2601
8.21k
        };
2602
2603
8.21k
        if (session->context->readonly ||
2604
6.42k
            session->context->passive) {
2605
2.74k
            return;
2606
2.74k
        }
2607
2608
5.46k
        switch (event) {
2609
315
        case EVENT_IDENTIFIED:
2610
315
            FALLTHROUGH
2611
315
        case EVENT_REACTIVATE:
2612
315
            if (NMEA_PACKET == session->lexer.type) {
2613
0
                GPSD_LOG(LOG_PROG, &session->context->errout,
2614
0
                         "SiRF: Switching chip mode to binary.\n");
2615
0
                (void)nmea_send(session,
2616
0
                                "$PSRF100,0,%d,8,1,0",
2617
0
                                session->gpsdata.dev.baudrate);
2618
0
            }
2619
315
            break;
2620
2621
4.76k
        case EVENT_CONFIGURE:
2622
            /* This wakes up on every received packet.
2623
             * Use this hook to step, slowly, through the init messages.
2624
             * We try, but not always succeed, to wait for the ACK/NACK.
2625
             * Send a message only every 15 times so we get an ACK/NACK
2626
             * before next one.
2627
             *
2628
             * This tries to avoid overrunning the input buffer, and makes
2629
             * it much easier to identify which messages get a NACK
2630
             */
2631
2632
2633
4.76k
            if (UINT_MAX == session->cfg_stage) {
2634
                // init done
2635
1.59k
                return;
2636
1.59k
            }
2637
3.16k
            session->cfg_step++;
2638
2639
3.16k
            if ((0 < session->driver.sirf.need_ack) &&
2640
1.60k
                (15 > session->cfg_step)) {
2641
                // we are waiting for ACK, just wait for 15 messages
2642
1.56k
                return;
2643
1.56k
            }
2644
1.60k
            session->cfg_step = 0;
2645
1.60k
            session->cfg_stage++;
2646
1.60k
            GPSD_LOG(LOG_PROG, &session->context->errout, "stage: %d\n",
2647
1.60k
                     session->cfg_stage);
2648
2649
2650
1.60k
            switch (session->cfg_stage) {
2651
0
            case 0:
2652
                // this slot used by EVENT_IDENTIFIED
2653
0
                return;
2654
2655
281
            case 1:
2656
281
                (void)sirf_write(session, versionprobe, sizeof(versionprobe));
2657
281
                break;
2658
214
            case 2:
2659
                // unset MID 0x40 = 64 first since there is a flood of them
2660
214
                GPSD_LOG(LOG_PROG, &session->context->errout,
2661
214
                         "SiRF: unset MID 0x40.\n");
2662
214
                unsetmidXX[5] = 1;    // enable/disable
2663
214
                unsetmidXX[6] = 0x40; // MID 0x40
2664
214
                (void)sirf_write(session, unsetmidXX, sizeof(unsetmidXX));
2665
214
                break;
2666
2667
192
            case 3:
2668
                /*
2669
                 * The response to this request will save the navigation
2670
                 * parameters so they can be reverted before close.
2671
                 */
2672
192
                GPSD_LOG(LOG_PROG, &session->context->errout,
2673
192
                         "SiRF: Requesting navigation parameters.\n");
2674
192
                (void)sirf_write(session, navparams, sizeof(navparams));
2675
192
                break;
2676
2677
153
            case 4:
2678
                // unset GND (0x29 = 41), it's not reliable on SiRF II
2679
153
                GPSD_LOG(LOG_PROG, &session->context->errout,
2680
153
                         "SiRF: unset MID 0x29.\n");
2681
153
                unsetmidXX[5] = 1;    // enable/disable
2682
153
                unsetmidXX[6] = 0x29; // MID 0x29
2683
153
                (void)sirf_write(session, unsetmidXX, sizeof(unsetmidXX));
2684
153
                break;
2685
2686
140
            case 5:
2687
140
                GPSD_LOG(LOG_PROG, &session->context->errout,
2688
140
                         "SiRF: Setting Navigation Parameters.\n");
2689
140
                (void)sirf_write(session, modecontrol, sizeof(modecontrol));
2690
140
                break;
2691
2692
125
            case 6:
2693
125
                GPSD_LOG(LOG_PROG, &session->context->errout,
2694
125
                         "SiRF: Requesting periodic ecef reports.\n");
2695
125
                (void)sirf_write(session, requestecef, sizeof(requestecef));
2696
125
                break;
2697
2698
108
            case 7:
2699
108
                GPSD_LOG(LOG_PROG, &session->context->errout,
2700
108
                         "SiRF: Requesting periodic tracker reports.\n");
2701
108
                (void)sirf_write(session, requesttracker, sizeof(requesttracker));
2702
108
                break;
2703
2704
95
            case 8:
2705
95
                GPSD_LOG(LOG_PROG, &session->context->errout,
2706
95
                         "SiRF: Setting DGPS control to use SBAS.\n");
2707
95
                (void)sirf_write(session, dgpscontrol, sizeof(dgpscontrol));
2708
95
                break;
2709
2710
86
            case 9:
2711
86
                GPSD_LOG(LOG_PROG, &session->context->errout,
2712
86
                         "SiRF: Setting SBAS to auto/integrity mode.\n");
2713
86
                (void)sirf_write(session, sbasparams, sizeof(sbasparams));
2714
86
                break;
2715
2716
74
            case 10:
2717
74
                GPSD_LOG(LOG_PROG, &session->context->errout,
2718
74
                         "SiRF: Enabling PPS message MID 52 (0x32).\n");
2719
                /* Not supported on some GPS.
2720
                 * It will be NACKed is not supported */
2721
74
                (void)sirf_write(session, enablemid52, sizeof(enablemid52));
2722
74
                break;
2723
2724
58
            case 11:
2725
                // SiRF recommends at least 57600 for SiRF IV subframe data
2726
58
                if (57600 <= session->gpsdata.dev.baudrate) {
2727
                    // fast enough, turn on subframe data
2728
0
                    GPSD_LOG(LOG_PROG, &session->context->errout,
2729
0
                             "SiRF: Enabling subframe transmission.\n");
2730
0
                    (void)sirf_write(session, enablesubframe,
2731
0
                                     sizeof(enablesubframe));
2732
58
                } else {
2733
                    // too slow, turn off subframe data
2734
58
                    GPSD_LOG(LOG_PROG, &session->context->errout,
2735
58
                             "SiRF: Disabling subframe transmission.\n");
2736
58
                    (void)sirf_write(session, disablesubframe,
2737
58
                                     sizeof(disablesubframe));
2738
58
                }
2739
58
                break;
2740
2741
37
            case 12:
2742
                /*
2743
                 * Disable navigation debug messages (the value 5 is magic)
2744
                 * must be done *after* subframe enable.
2745
                 */
2746
37
                GPSD_LOG(LOG_PROG, &session->context->errout,
2747
37
                         "SiRF: disable MID 7, 28, 29, 30, 31.\n");
2748
37
                unsetmidXX[5] = 5;
2749
37
                unsetmidXX[6] = 0;
2750
37
                (void)sirf_write(session, unsetmidXX, sizeof(unsetmidXX));
2751
37
                break;
2752
2753
43
            default:
2754
                // initialization is done
2755
43
                session->cfg_stage = UINT_MAX;
2756
43
                session->cfg_step = 0;
2757
43
                return;
2758
1.60k
            }
2759
1.56k
            break;
2760
2761
1.56k
        case EVENT_DEACTIVATE:
2762
2763
0
            putbyte(moderevert, 7, session->driver.sirf.degraded_mode);
2764
0
            putbe16(moderevert, 10, session->driver.sirf.altitude_source_input);
2765
0
            putbyte(moderevert, 12, session->driver.sirf.altitude_hold_mode);
2766
0
            putbyte(moderevert, 13, session->driver.sirf.altitude_hold_source);
2767
0
            putbyte(moderevert, 15, session->driver.sirf.degraded_timeout);
2768
0
            putbyte(moderevert, 16, session->driver.sirf.dr_timeout);
2769
0
            putbyte(moderevert, 17, session->driver.sirf.track_smooth_mode);
2770
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
2771
0
                     "SiRF: Reverting navigation parameters...\n");
2772
0
            (void)sirf_write(session, moderevert, sizeof(moderevert));
2773
0
            break;
2774
2775
394
        case EVENT_DRIVER_SWITCH:
2776
            // do what here?
2777
394
            break;
2778
0
        case EVENT_TRIGGERMATCH:
2779
            // do what here?
2780
0
            break;
2781
0
        case EVENT_WAKEUP:
2782
            // do what here?
2783
0
            break;
2784
5.46k
        }
2785
5.46k
    }
2786
2787
2788
2789
// this is everything we export
2790
// *INDENT-OFF*
2791
const struct gps_type_t driver_sirf =
2792
{
2793
    .type_name      = "SiRF",               // full name of type
2794
    .packet_type    = SIRF_PACKET,          // associated lexer packet type
2795
    .flags          = DRIVER_STICKY,        // remember this
2796
    .trigger        = NULL,                 // no trigger
2797
    .channels       = SIRF_CHANNELS,        // consumer-grade GPS
2798
    .probe_detect   = NULL,                 // no probe
2799
    .get_packet     = packet_get1,          // be prepared for SiRF or NMEA
2800
    .parse_packet   = sirfbin_parse_input,  // parse message packets
2801
    .rtcm_writer    = gpsd_write,           // send RTCM data straight
2802
    .init_query     = sirfbin_init_query,   // non-perturbing initial query
2803
    .event_hook     = sirfbin_event_hook,   // lifetime event handler
2804
    .speed_switcher = sirfbin_speed,        // we can change baud rate
2805
    .mode_switcher  = sirfbin_mode,         // there's a mode switcher
2806
    .rate_switcher  = NULL,                 // no sample-rate switcher
2807
    .min_cycle.tv_sec  = 1,                 // not relevant, no rate switch
2808
    .min_cycle.tv_nsec = 0,                 // not relevant, no rate switch
2809
    .control_send   = sirf_control_send,    // how to send a control string
2810
    .time_offset    = sirf_time_offset,
2811
};
2812
// *INDENT-ON*
2813
#endif // defined(SIRF_ENABLE)
2814
2815
// vim: set expandtab shiftwidth=4