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_navcom.c
Line
Count
Source
1
/*
2
 * Driver for Navcom receivers using proprietary NCT messages,
3
 * a binary protocol.
4
 *
5
 * Vendor website: http://www.navcomtech.com/
6
 * Technical references: Technical Reference Manual P/N 96-3120001-3001
7
 *
8
 * Tested with two SF-2040G models
9
 *
10
 * At this stage, this driver implements the following commands:
11
 *
12
 * 0x20: Data Request (tell the unit which responses you want)
13
 * 0x3f: LED Configuration (controls the front panel LEDs -- for testing)
14
 * 0x1c: Test Support Block (again, blinks the front panel lights)
15
 *
16
 * and it understands the following responses:
17
 *
18
 * 0x06: Acknowledgement (without error)
19
 * 0x15: Negative Acknowledge
20
 * 0x86: Channel Status
21
 * 0xae: Identification Block
22
 * 0xb0: Raw Meas. Data Block
23
 * 0xb1: PVT Block
24
 * 0xb5: Pseudorange Noise Statistics
25
 * 0xd3: LBM DSP Status Block
26
 * 0xef: Clock Drift and Offset
27
 *
28
 * By Diego Berge. Contact via web form at http://www.navlost.eu/contact
29
 *
30
 * Week counters are not limited to 10 bits. It's unknown what
31
 * the firmware is doing to disambiguate them, if anything; it might just
32
 * be adding a fixed offset based on a hidden epoch value, in which case
33
 * unhappy things will occur on the next rollover.
34
 *
35
 * This file is Copyright by the GPSD project
36
 * SPDX-License-Identifier: BSD-2-clause
37
 */
38
39
#include "../include/gpsd_config.h"  // must be before all includes
40
41
#include <math.h>
42
#include <stdbool.h>
43
#include <stdio.h>
44
#include <string.h>
45
46
#include "../include/gpsd.h"
47
48
#if defined(NAVCOM_ENABLE)
49
#include "../include/bits.h"
50
#include "../include/timespec.h"
51
52
// Have data which is 24 bits long
53
0
#define getles3224(buf,off)  (int32_t)(((uint32_t)getub((buf), (off) + 2) << 24 | (uint32_t)getub((buf), (off) + 1) << 16 | (uint32_t)getub((buf), (off)) << 8) >> 8)
54
#define getleu3224(buf,off) (uint32_t)(((uint32_t)getub((buf), (off) + 2) << 24 | (uint32_t)getub((buf), (off) + 1) << 16 | (uint32_t)getub((buf), (off)) << 8) >> 8)
55
56
/* And just to be difficult, Navcom is little endian but the GPS data stream
57
 *  is big endian.  Some messages contain raw GPS data */
58
0
#define getles16_be(buf, off)   (int16_t)((((uint16_t)getub(buf, (off)) << 8) \
59
0
                                           | (uint16_t)getub(buf, (off) + 1)))
60
0
#define getleu16_be(buf, off)   (uint16_t)((((uint16_t)getub(buf, (off)) << 8) \
61
0
                                            | (uint16_t)getub(buf, (off) + 1)))
62
0
#define getles32_be(buf, off)   (int32_t)((((uint16_t)getleu16_be(buf, (off)) << 16) \
63
0
                                           | getleu16_be(buf, (off) + 2)))
64
0
#define getleu32_be(buf, off)   (uint32_t)((((uint16_t)getleu16_be(buf, (off)) << 16) \
65
0
                                            | getleu16_be(buf, (off) + 2)))
66
0
#define getles3224_be(buf,off)     (int32_t)(((uint32_t)getub((buf), (off)) << 24 \
67
0
                                              | (uint32_t)getub((buf), (off) + 1) << 16 \
68
0
                                              | (uint32_t)getub((buf), (off) + 2) << 8) >> 8)
69
70
#define NAVCOM_CHANNELS 12
71
72
    static uint8_t checksum(unsigned char *buf, size_t len)
73
0
    {
74
0
        size_t n;
75
0
        uint8_t csum = (uint8_t)0x00;
76
0
        for (n = 0; n < len; n++) {
77
0
            csum ^= buf[n];
78
0
        }
79
0
        return csum;
80
0
    }
81
82
    static bool navcom_send_cmd(struct gps_device_t *session,
83
                                unsigned char *cmd, size_t len)
84
0
    {
85
0
        return (gpsd_write(session, (const char *)cmd, len) == (ssize_t) len);
86
0
    }
87
88
// Data Request
89
    static void navcom_cmd_0x20(struct gps_device_t *session, uint8_t block_id,
90
                                uint16_t rate)
91
0
    {
92
0
        unsigned char msg[18];
93
0
        putbyte(msg, 0, 0x02);
94
0
        putbyte(msg, 1, 0x99);
95
0
        putbyte(msg, 2, 0x66);
96
0
        putbyte(msg, 3, 0x20);     // Cmd ID
97
0
        putle16(msg, 4, 0x000e);   // Length
98
0
        putbyte(msg, 6, 0x00);     // Action
99
0
        putbyte(msg, 7, 0x01);     // Count of blocks
100
0
        putbyte(msg, 8, block_id); // Data Block ID
101
0
        putbyte(msg, 9, 0x02);     // Logical Ports
102
0
        putle16(msg, 10, rate);    // Data rate
103
0
        putbyte(msg, 12, 0x71);
104
0
        putbyte(msg, 13, 0x00);
105
0
        putle16(msg, 14, 0x0000);
106
0
        putbyte(msg, 16, checksum(msg + 3, 13));
107
0
        putbyte(msg, 17, 0x03);
108
0
        (void)navcom_send_cmd(session, msg, 18);
109
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
110
0
                 "Navcom: sent command 0x20 (Data Request) "
111
0
                 "- data block id = %02x at rate %02x\n", block_id, rate);
112
0
    }
113
114
// cppcheck-suppress unusedFunction
115
    static void UNUSED navcom_cmd_0x3f(struct gps_device_t *session)
116
// Changes the LED settings in the receiver
117
0
    {
118
0
        unsigned char msg[12];
119
0
        putbyte(msg, 0, 0x02);
120
0
        putbyte(msg, 1, 0x99);
121
0
        putbyte(msg, 2, 0x66);
122
0
        putbyte(msg, 3, 0x3f);  // Cmd ID
123
0
        putle16(msg, 4, 0x0008);
124
0
        putbyte(msg, 6, 0x01);  // Action
125
0
        putbyte(msg, 7, 0x00);  // Reserved
126
0
        putbyte(msg, 8, 0x02);  // Link LED setting
127
0
        putbyte(msg, 9, 0x0a);  // Battery LED setting
128
0
        putbyte(msg, 10, checksum(msg + 3, 7));
129
0
        putbyte(msg, 11, 0x03);
130
0
        (void)navcom_send_cmd(session, msg, 12);
131
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
132
0
                 "Navcom: sent command 0x3f (LED Configuration Block)\n");
133
0
    }
134
135
// Test Support Block - Blinks the LEDs
136
    static void navcom_cmd_0x1c(struct gps_device_t *session, uint8_t mode,
137
                                uint8_t length)
138
0
    {
139
0
        unsigned char msg[12];
140
0
        putbyte(msg, 0, 0x02);
141
0
        putbyte(msg, 1, 0x99);
142
0
        putbyte(msg, 2, 0x66);
143
0
        putbyte(msg, 3, 0x1c);   // Cmd ID
144
0
        putle16(msg, 4, 0x0008);
145
0
        putbyte(msg, 6, 0x04);   // Use ACK/NAK
146
0
        putbyte(msg, 7, mode);   // 0x01 or 0x02
147
0
        putbyte(msg, 8, length); // Only if mode == 0x01
148
0
        putbyte(msg, 9, 0x00);
149
0
        putbyte(msg, 10, checksum(msg + 3, 7));
150
0
        putbyte(msg, 11, 0x03);
151
0
        (void)navcom_send_cmd(session, msg, 12);
152
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
153
0
                 "Navcom: sent command 0x1c (Test Support Block)\n");
154
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
155
0
                 "Navcom: command 0x1c mode = %02x, length = %u\n",
156
0
                 mode, length);
157
0
    }
158
159
// Serial Port Configuration
160
    static void navcom_cmd_0x11(struct gps_device_t *session,
161
                                uint8_t port_selection)
162
0
    {
163
        /* NOTE - We only allow changing one port at a time,
164
         * although the message supports doing both at once. */
165
0
        unsigned char msg[12];
166
0
        putbyte(msg, 0, 0x02);
167
0
        putbyte(msg, 1, 0x99);
168
0
        putbyte(msg, 2, 0x66);
169
0
        putbyte(msg, 3, 0x11);   // Cmd ID
170
0
        putle16(msg, 4, 0x0008); // Length
171
0
        putbyte(msg, 6, 0x04);   // Action - Use ACK/NAK)
172
0
        putbyte(msg, 7, port_selection);
173
0
        putbyte(msg, 8, 0x00);   // Reserved
174
0
        putbyte(msg, 9, 0x00);   // Reserved
175
0
        putbyte(msg, 10, checksum(msg + 3, 7));
176
0
        putbyte(msg, 11, 0x03);
177
0
        (void)navcom_send_cmd(session, msg, 12);
178
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
179
0
                 "Navcom: sent command 0x11 (Serial Port Configuration)\n");
180
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
181
0
                 "Navcom: serial port selection: 0x%02x\n", port_selection);
182
0
    }
183
184
    static void navcom_event_hook(struct gps_device_t *session, event_t event)
185
0
    {
186
0
        if (session->context->readonly) {
187
0
            return;
188
0
        }
189
190
        // Request the following messages:
191
0
        if (event == EVENT_IDENTIFIED) {
192
            /* NOTE - Channel Status allows us to know into which of the
193
             * unit's various serial ports we are connected.
194
             * Its value gets updated every time we receive a 0x06 (Ack)
195
             * message.  Note that if commands are being fed into the
196
             * unit from more than one port (which is entirely possible
197
             * although not necessarily a bright idea), there is a good
198
             * chance that we might misidentify our port */
199
0
            navcom_cmd_0x1c(session, 0x02, 0);  // Test Support Block
200
0
            navcom_cmd_0x20(session, 0xae, 0x0000); // Identification Block
201
0
            navcom_cmd_0x20(session, 0x86, 0x000a); // Channel Status
202
0
            navcom_cmd_0x1c(session, 0x01, 5);  // Blink LEDs on receiver
203
            // Identification Block - send every 10 min
204
0
            navcom_cmd_0x20(session, 0xae, 0x1770);
205
0
            navcom_cmd_0x20(session, 0xb1, 0x4000); // PVT Block
206
            // Pseudorange Noise Statistics - send every 20s
207
0
            navcom_cmd_0x20(session, 0xb5, 0x00c8);
208
0
            navcom_cmd_0x20(session, 0xb0, 0x4000); // Raw Meas Data Block
209
            // Packed Ephemeris Data - send once
210
0
            navcom_cmd_0x20(session, 0x81, 0x0000);
211
0
            navcom_cmd_0x20(session, 0x81, 0x4000); // Packed Ephemeris Data
212
0
            navcom_cmd_0x20(session, 0x86, 0x4000); // Channel Status
213
0
            navcom_cmd_0x20(session, 0x83, 0x4000); // Ionosphere and UTC Data
214
            // Clock Drift - send every 5 min
215
0
            navcom_cmd_0x20(session, 0xef, 0x0bb8);
216
0
        }
217
0
    }
218
219
// Ionosphere and UTC Data
220
    static gps_mask_t handle_0x83(struct gps_device_t *session)
221
0
    {
222
        /* NOTE - At the present moment this is only being used
223
         * for determining the GPS-UTC time difference,
224
         * for which the iono data is not needed as far
225
         * as we are concerned.  However, I am still
226
         * reporting it (if debuglevel >= LOG_DATA) as a
227
         * matter of interest */
228
// 2^-30
229
0
#define SF_A0 (0.000000000931322574615478515625)
230
// 2^-50
231
0
#define SF_A1 (0.000000000000000888178419700125)
232
// 2^12
233
0
#define SF_TOT (4096)
234
// 2^-30
235
0
#define SF_ALPHA0 (0.000000000931322574615478515625)
236
// 2^-27
237
0
#define SF_ALPHA1 (0.000000007450580596923828125)
238
// 2^-24
239
0
#define SF_ALPHA2 (0.000000059604644775390625)
240
// 2^-24
241
0
#define SF_ALPHA3 (0.000000059604644775390625)
242
// 2^11
243
0
#define SF_BETA0 (2048)
244
// 2^14
245
0
#define SF_BETA1 (16384)
246
// 2^16
247
0
#define SF_BETA2 (65536)
248
// 2^16
249
0
#define SF_BETA3 (65536)
250
0
        unsigned char *buf = session->lexer.outbuffer + 3;
251
0
        uint16_t week = getleu16(buf, 3);
252
0
        uint32_t tow = getleu32(buf, 5);
253
0
        int8_t alpha0 = getsb(buf, 9);
254
0
        int8_t alpha1 = getsb(buf, 10);
255
0
        int8_t alpha2 = getsb(buf, 11);
256
0
        int8_t alpha3 = getsb(buf, 12);
257
0
        int8_t beta0 = getsb(buf, 13);
258
0
        int8_t beta1 = getsb(buf, 14);
259
0
        int8_t beta2 = getsb(buf, 15);
260
0
        int8_t beta3 = getsb(buf, 16);
261
0
        int32_t a1 = getles32(buf, 17);
262
0
        int32_t a0 = getles32(buf, 21);
263
0
        uint8_t tot = getub(buf, 25);
264
0
        uint8_t wnt = getub(buf, 26);
265
0
        int8_t dtls = getsb(buf, 27);
266
0
        uint8_t wnlsf = getub(buf, 28);
267
0
        uint8_t dn = getub(buf, 29);
268
0
        int8_t dtlsf = getsb(buf, 30);
269
270
        // Ref.: ICD-GPS-200C 20.3.3.5.2.4
271
0
        if (((week % 256) * 604800 + tow / 1000.0) <
272
0
            ( wnlsf * 604800 + dn * 86400)) {
273
            // Effectivity time is in the future, use dtls
274
0
            session->context->leap_seconds = (int)dtls;
275
0
        } else {
276
            // Effectivity time is not in the future, use dtlsf
277
0
            session->context->leap_seconds = (int)dtlsf;
278
0
        }
279
280
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
281
0
                 "Navcom: received packet type 0x83 "
282
0
                 "(Ionosphere and UTC Data)\n");
283
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
284
0
                 "Navcom: Scaled parameters follow:\n");
285
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
286
0
                 "Navcom: GPS Week: %u, GPS Time of Week: %u (GPS Time: %f)\n",
287
0
                 week, tow, week * 604800 + tow / 1000.0);
288
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
289
0
                 "Navcom: a0: %12.4E, a1: %12.4E, a2: %12.4E, a3: %12.4E, "
290
0
                 "b0: %12.4E, b1: %12.4E, b2: %12.4E, b3: %12.4E\n",
291
0
                 (double)alpha0 * SF_ALPHA0, (double)alpha1 * SF_ALPHA1,
292
0
                 (double)alpha2 * SF_ALPHA2, (double)alpha3 * SF_ALPHA3,
293
0
                 (double)beta0 * SF_BETA0, (double)beta1 * SF_BETA1,
294
0
                 (double)beta2 * SF_BETA2, (double)beta3 * SF_BETA3);
295
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
296
0
                 "Navcom: A0: %19.12E, A1: %19.12E\n", (double)a0 * SF_A0,
297
0
                 (double)a1 * SF_A1);
298
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
299
0
                 "Navcom: UTC Ref. Time: %lu, UTC Ref. Week: %u, dTls: %d\n",
300
0
                 (unsigned long)tot * SF_TOT, wnt, dtls);
301
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
302
0
                 "Navcom: Week of leap seconds: %u, Day number of leap "
303
0
                 "seconds: %u, dTlsf: %d\n",
304
0
                 wnlsf, dn, dtlsf);
305
306
0
        return 0; // No flag for update of leap seconds (Not part of a fix)
307
308
0
#undef SF_A0
309
0
#undef SF_A1
310
0
#undef SF_TOT
311
0
#undef SF_ALPHA0
312
0
#undef SF_ALPHA1
313
0
#undef SF_ALPHA2
314
0
#undef SF_ALPHA3
315
0
#undef SF_BETA0
316
0
#undef SF_BETA1
317
0
#undef SF_BETA2
318
0
#undef SF_BETA3
319
0
    }
320
321
// Acknowledgement (without error)
322
    static gps_mask_t handle_0x06(struct gps_device_t *session)
323
0
    {
324
0
        unsigned char *buf = session->lexer.outbuffer + 3;
325
0
        uint8_t cmd_id = getub(buf, 3);
326
0
        uint8_t port = getub(buf, 4);
327
328
        // This tells us which serial port was used last
329
0
        session->driver.navcom.physical_port = port;
330
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
331
0
                 "Navcom: received packet type 0x06 (Acknowledgement (without "
332
0
                 "error))\n");
333
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
334
0
                 "Navcom: acknowledged command id 0x%02x on port %c\n",
335
0
                 cmd_id, (port == 0 ? 'A' : (port == 1 ? 'B' : '?')));
336
0
        return 0;               // Nothing updated
337
0
    }
338
339
// Negative Acknowledge*/
340
    static gps_mask_t handle_0x15(struct gps_device_t *session)
341
0
    {
342
0
        unsigned n;
343
0
        unsigned char *buf = session->lexer.outbuffer + 3;
344
0
        unsigned msg_len = getleu16(buf, 1);
345
0
        uint8_t port, cmd_id = getub(buf, 3);
346
347
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
348
0
                 "Navcom: received packet type 0x15 (Negative Acknowledge)\n");
349
350
0
        if (18 < msg_len) {
351
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
352
0
                     "Navcom: 0x15 too long %u\n", msg_len);
353
0
            return 0;
354
0
        }
355
0
        for (n = 4; n < (msg_len - 2); n += 2) {
356
0
            uint8_t err_id = getub(buf, n);
357
0
            uint8_t err_desc = getub(buf, n + 1);
358
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
359
0
                     "Navcom: error id = 0x%02x, error description = 0x%02x\n",
360
0
                     err_id, err_desc);
361
0
        }
362
0
        port = getub(buf, n);
363
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
364
0
                 "Navcom: negative acknowledge was for command id 0x%02x "
365
0
                 "on port %c\n",
366
0
                 cmd_id, (port == 0 ? 'A' : (port == 1 ? 'B' : '?')));
367
0
        return 0;               // Nothing updated*/
368
0
    }
369
370
// PVT Block*/
371
    static gps_mask_t handle_0xb1(struct gps_device_t *session)
372
0
    {
373
0
        gps_mask_t mask = 0;
374
0
        unsigned char *buf = session->lexer.outbuffer + 3;
375
0
        uint16_t week;
376
0
        uint32_t tow;
377
0
        timespec_t ts_tow = {0, 0};  // pacify codacy
378
0
        int32_t lat, lon;
379
0
        char ts_buf[TIMESPEC_LEN];
380
        // Resolution of lat/lon values (2^-11)*/
381
0
#define LL_RES (0.00048828125)
382
0
        uint8_t lat_fraction, lon_fraction;
383
        // Resolution of lat/lon fractions (2^-15)*/
384
0
#define LL_FRAC_RES (0.000030517578125)
385
0
        uint8_t nav_mode;
386
0
        int32_t ellips_height, altitude;
387
        // Resolution of height and altitude values (2.0^-10)*/
388
0
#define EL_RES (0.0009765625)
389
0
        double vel_north, vel_east, vel_up;
390
0
        uint8_t gdop, pdop, hdop, vdop, tdop;
391
        // This value means "undefined"*/
392
0
#define DOP_UNDEFINED (255)
393
394
0
        int16_t ant_height_adj;
395
0
        int32_t set_delta_up;
396
        /* Resolution of delta north, east, and up,
397
         * and ant. height adjustment values (1mm) */
398
0
#define D_RES (0.001)
399
400
#ifdef __UNUSED__
401
            /* Other values provided by the PVT block which we
402
             * may want to provide in the future.
403
             */
404
            uint8_t dgps_conf;
405
            uint16_t max_dgps_age;
406
            uint8_t ext_nav_mode;
407
            int32_t set_delta_north, set_delta_east;
408
            uint8_t nav_failure_code;
409
#endif  // __UNUSED__*/
410
411
        // Timestamp
412
0
        week = (uint16_t) getleu16(buf, 3);
413
0
        tow = (uint32_t) getleu32(buf, 5);    // tow in ms
414
0
        MSTOTS(&ts_tow, tow);
415
0
        session->newdata.time = gpsd_gpstime_resolv(session, week, ts_tow);
416
417
        // Get latitude, longitude
418
0
        lat = getles32(buf, 13);
419
0
        lon = getles32(buf, 17);
420
0
        lat_fraction = (uint8_t) (getub(buf, 21) >> 4);
421
0
        lon_fraction = (uint8_t) (getub(buf, 21) & 0x0f);
422
423
0
        session->newdata.latitude =
424
0
            (double)(lat * LL_RES + lat_fraction * LL_FRAC_RES) / 3600;
425
0
        session->newdata.longitude =
426
0
            (double)(lon * LL_RES + lon_fraction * LL_FRAC_RES) / 3600;
427
428
        // Nav mode
429
0
        nav_mode = (uint8_t) getub(buf, 22);
430
0
        if (0 == (nav_mode & 0x80)) {
431
0
            session->newdata.status = STATUS_UNK;
432
0
            session->newdata.mode = MODE_NO_FIX;
433
0
        } else {
434
0
            session->newdata.mode = ((nav_mode & 0x40) != 0 ?
435
0
                                     MODE_3D : MODE_2D);
436
0
            session->newdata.status =
437
0
                ((nav_mode & 0x03) != 0 ? STATUS_DGPS : STATUS_GPS);
438
0
        }
439
440
        // altHAE
441
0
        ellips_height = getles32(buf, 23);
442
        // altMSL
443
0
        altitude = getles32(buf, 27);
444
445
0
        ant_height_adj = getles16(buf, 51);
446
0
        set_delta_up = getles32(buf, 79);
447
448
0
        session->newdata.altMSL = (double)(altitude * EL_RES) +
449
0
                                  (ant_height_adj * D_RES) +
450
0
                                  (set_delta_up * D_RES);
451
0
        session->newdata.altHAE = (double)(ellips_height) * EL_RES +
452
0
                                  (ant_height_adj * D_RES) +
453
0
                                  (set_delta_up * D_RES);
454
        // Let gpsd_error_model() deal with geoid_sep
455
456
        // Speed Data
457
0
        vel_north = (double)getles3224(buf, 31);
458
0
        vel_east = (double)getles3224(buf, 34);
459
0
        vel_up = (double)getles3224(buf, 37);
460
461
0
        session->newdata.NED.velN = vel_north * 0.1;
462
0
        session->newdata.NED.velE = vel_east * 0.1;
463
0
        session->newdata.NED.velD = -vel_up * 0.1;
464
465
        // Quality indicators
466
        // UNUSED fom = getub(buf, 40);     * FOM is DRMS
467
0
        gdop = getub(buf, 41);
468
0
        pdop = getub(buf, 42);
469
0
        hdop = getub(buf, 43);
470
0
        vdop = getub(buf, 44);
471
0
        tdop = getub(buf, 45);
472
        // UNUSED tfom = getub(buf, 46);    * tfom == 10 * TDOP
473
474
        // let gpsd_error_model() do the error estimates
475
476
0
        if (gdop != DOP_UNDEFINED) {
477
0
            session->gpsdata.dop.gdop = gdop / 10.0;
478
0
            mask |= DOP_SET;
479
0
        }
480
0
        if (pdop != DOP_UNDEFINED) {
481
0
            session->gpsdata.dop.pdop = pdop / 10.0;
482
0
            mask |= DOP_SET;
483
0
        }
484
0
        if (hdop != DOP_UNDEFINED) {
485
0
            session->gpsdata.dop.hdop = hdop / 10.0;
486
0
            mask |= DOP_SET;
487
0
        }
488
0
        if (vdop != DOP_UNDEFINED) {
489
0
            session->gpsdata.dop.vdop = vdop / 10.0;
490
0
            mask |= DOP_SET;
491
0
        }
492
0
        if (tdop != DOP_UNDEFINED) {
493
0
            session->gpsdata.dop.tdop = tdop / 10.0;
494
0
            mask |= DOP_SET;
495
0
        }
496
497
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
498
0
                 "Navcom: received packet type 0xb1 (PVT Report)\n");
499
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
500
0
                 "Navcom: navigation mode %s (0x%02x) - %s - %s\n",
501
0
                 (0 == (nav_mode & 0x80) ? "invalid" : "valid"), nav_mode,
502
0
                 ((nav_mode & 0x40) != '\0' ? "3D" : "2D"),
503
0
                 ((nav_mode & 0x03) != '\0' ? "DGPS" : "GPS"));
504
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
505
0
                 "Navcom: velocities: north = %f east = %f up = %f\n",
506
0
                 session->newdata.NED.velN,
507
0
                 session->newdata.NED.velE,
508
0
                 -session->newdata.NED.velD);
509
0
#undef D_RES
510
0
#undef LL_RES
511
0
#undef LL_FRAC_RES
512
0
#undef EL_RES
513
0
#undef VEL_RES
514
0
#undef DOP_UNDEFINED
515
516
0
        mask |= LATLON_SET | ALTITUDE_SET | STATUS_SET | MODE_SET | USED_IS |
517
0
                HERR_SET | TIMERR_SET | VNED_SET | TIME_SET | NTPTIME_IS;
518
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
519
0
                 "PVT 0xb1: time=%s, lat=%.2f lon=%.2f altHAE=%.2f "
520
0
                 "altMSL %.2f mode=%d status=%d gdop=%.2f pdop=%.2f hdop=%.2f "
521
0
                 "vdop=%.2f tdop=%.2f mask={%s}\n",
522
0
                 timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
523
0
                 session->newdata.latitude,
524
0
                 session->newdata.longitude,
525
0
                 session->newdata.altHAE,
526
0
                 session->newdata.altMSL,
527
0
                 session->newdata.mode,
528
0
                 session->newdata.status,
529
0
                 session->gpsdata.dop.gdop,
530
0
                 session->gpsdata.dop.pdop,
531
0
                 session->gpsdata.dop.hdop,
532
0
                 session->gpsdata.dop.vdop, session->gpsdata.dop.tdop,
533
0
                 gps_maskdump(mask));
534
0
        return mask | CLEAR_IS | REPORT_IS;
535
0
    }
536
537
// Packed Ephemeris Data
538
    static gps_mask_t handle_0x81(struct gps_device_t *session)
539
0
    {
540
        // Scale factors for everything
541
        // 2^-31
542
0
#define SF_TGD       (.000000000465661287307739257812)
543
        // 2^4
544
0
#define SF_TOC     (16)
545
        // 2^-55
546
0
#define SF_AF2       (.000000000000000027755575615628)
547
        // 2^-43
548
0
#define SF_AF1       (.000000000000113686837721616029)
549
        // 2^-31
550
0
#define SF_AF0       (.000000000465661287307739257812)
551
        // 2^-5
552
0
#define SF_CRS       (.031250000000000000000000000000)
553
        // 2^-43
554
0
#define SF_DELTA_N   (.000000000000113686837721616029)
555
        // 2^-31
556
0
#define SF_M0   (.000000000465661287307739257812)
557
        // 2^-29
558
0
#define SF_CUC       (.000000001862645149230957031250)
559
        // 2^-33
560
0
#define SF_E     (.000000000116415321826934814453)
561
        // 2^-29
562
0
#define SF_CUS       (.000000001862645149230957031250)
563
        // 2^-19
564
0
#define SF_SQRT_A    (.000001907348632812500000000000)
565
        // 2^4
566
0
#define SF_TOE     (16)
567
        // 2^-29
568
0
#define SF_CIC       (.000000001862645149230957031250)
569
        // 2^-31
570
0
#define SF_OMEGA0    (.000000000465661287307739257812)
571
        // 2^-29
572
0
#define SF_CIS       (.000000001862645149230957031250)
573
        // 2^-31
574
0
#define SF_I0   (.000000000465661287307739257812)
575
        // 2^-5
576
0
#define SF_CRC       (.031250000000000000000000000000)
577
        // 2^-31
578
0
#define SF_OMEGA     (.000000000465661287307739257812)
579
        // 2^-43
580
0
#define SF_OMEGADOT  (.000000000000113686837721616029)
581
        // 2^-43
582
0
#define SF_IDOT      (.000000000000113686837721616029)
583
584
0
        char ts_buf[TIMESPEC_LEN];
585
0
        unsigned char *buf = session->lexer.outbuffer + 3;
586
0
        uint8_t prn = getub(buf, 3);
587
0
        uint16_t week = getleu16(buf, 4);
588
0
        uint32_t tow = getleu32(buf, 6);
589
0
        uint16_t iodc = getleu16(buf, 10);
590
        /* And now the fun starts... everything that follows is
591
         * raw GPS data minus parity */
592
        // Subframe 1, words 3 to 10 minus parity
593
0
        uint16_t wn = (getleu16_be(buf, 12) & 0xffc0) >> 6;
594
0
        uint8_t cl2 = (getub(buf, 13) & 0x30) >> 4;
595
0
        uint8_t ura = getub(buf, 13) & 0x0f;
596
0
        uint8_t svh = (getub(buf, 14) & 0xfc) >> 2;
597
        /* We already have IODC from earlier in the message, so
598
         * we do not decode again */
599
// uint16_t iodc = (getub(buf, 14)&0x03)<<8;*/
600
0
        uint8_t l2pd = (getub(buf, 15) & 0x80) >> 7;
601
0
        int8_t tgd = getsb(buf, 26);
602
// iodc |= getub(buf, 27);*/
603
0
        uint16_t toc = getleu16_be(buf, 28);
604
0
        int8_t af2 = getsb(buf, 30);
605
0
        int16_t af1 = getles16_be(buf, 31);
606
0
        int32_t af0 = getles3224_be(buf, 33) >> 2;
607
        // Subframe 2, words 3 to 10 minus parity
608
0
        uint8_t iode = getub(buf, 36);
609
0
        int16_t crs = getles16_be(buf, 37);
610
0
        int16_t delta_n = getles16_be(buf, 39);
611
0
        int32_t m0 = getles32_be(buf, 41);
612
0
        int16_t cuc = getles16_be(buf, 45);
613
0
        uint32_t e = getleu32_be(buf, 47);
614
0
        int16_t cus = getles16_be(buf, 51);
615
0
        uint32_t sqrt_a = getleu32_be(buf, 53);
616
0
        uint16_t toe = getleu16_be(buf, 57);
617
        // NOTE - Fit interval & AODO not collected
618
        // Subframe 3, words 3 to 10 minus parity
619
0
        int16_t cic = getles16_be(buf, 60);
620
0
        int32_t Omega0 = getles32_be(buf, 62);
621
0
        int16_t cis = getles16_be(buf, 66);
622
0
        int32_t i0 = getles32_be(buf, 68);
623
0
        int16_t crc = getles16_be(buf, 72);
624
0
        int32_t omega = getles32_be(buf, 74);
625
0
        int32_t Omegadot = getles3224_be(buf, 78);
626
        /* Question: What is the proper way of shifting a signed int 2 bits to
627
         * the right, preserving sign? Answer: integer division by 4. */
628
0
        int16_t idot = (int16_t) (((getles16_be(buf, 82) & 0xfffc) / 4) |
629
0
                                  ((getub(buf, 82) & 80) ? 0xc000 : 0x0000));
630
0
        session->context->gps_week = (unsigned short)wn;
631
0
        DTOTS(&session->context->gps_tow, (double)(toc * SF_TOC));
632
        // leap second?
633
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
634
0
                 "Navcom: received packet type 0x81 (Packed Ephemeris Data)\n");
635
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
636
0
                 "Navcom: PRN: %u, Week: %u, TOW: %s "
637
0
                 "SV clock bias/drift/drift rate: "
638
0
                 " %#19.12E/%#19.12E/%#19.12E\n",
639
0
                 prn,
640
0
                 session->context->gps_week,
641
0
                 timespec_str(&session->context->gps_tow, ts_buf,
642
0
                              sizeof(ts_buf)),
643
0
                 ((double)af0) * SF_AF0,
644
0
                 ((double)af1) * SF_AF1, ((double)af2) * SF_AF2);
645
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
646
0
                 "Navcom: IODE (!AODE): %u Crs: %19.12e, Delta n: %19.12e, "
647
0
                 "M0: %19.12e\n",
648
0
                 iode, (double)crs * SF_CRS,
649
0
                 (double)delta_n * SF_DELTA_N * GPS_PI,
650
0
                 (double)m0 * SF_M0 * GPS_PI);
651
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
652
0
                 "Navcom: Cuc: %19.12e, Eccentricity: %19.12e, Cus: %19.12e, "
653
0
                 "A^1/2: %19.12e\n",
654
0
                 (double)cuc * SF_CUC, (double)e * SF_E, (double)cus * SF_CUS,
655
0
                 (double)sqrt_a * SF_SQRT_A);
656
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
657
0
                 "Navcom: TOE: %u, Cic: %19.12e, Omega %19.12e, Cis: %19.12e\n",
658
0
                 toe * SF_TOE, (double)cic * SF_CIC,
659
0
                 (double)Omega0 * SF_OMEGA0 * GPS_PI, (double)cis * SF_CIS);
660
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
661
0
                 "Navcom: i0: %19.12e, Crc: %19.12e, omega: %19.12e, "
662
0
                 "Omega dot: %19.12e\n",
663
0
                 (double)i0 * SF_I0 * GPS_PI, (double)crc * SF_CRC,
664
0
                 (double)omega * SF_OMEGA * GPS_PI,
665
0
                 (double)Omegadot * SF_OMEGADOT * GPS_PI);
666
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
667
0
                 "Navcom: IDOT: %19.12e, Codes on L2: 0x%x, GPS Week: %u, "
668
0
                 "L2 P data flag: %x\n",
669
0
                 (double)idot * SF_IDOT * GPS_PI, cl2,
670
0
                 week - (week % 1024) + wn, l2pd);
671
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
672
0
                 "Navcom: SV accuracy: 0x%x, SV health: 0x%x, TGD: %f, "
673
0
                 " IODC (!AODC): %u\n",
674
0
                 ura, svh, (double)tgd * SF_TGD, iodc);
675
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
676
0
                 "Navcom: Transmission time: %u\n", tow);
677
678
0
#undef SF_TGD
679
0
#undef SF_TOC
680
0
#undef SF_AF2
681
0
#undef SF_AF1
682
0
#undef SF_AF0
683
0
#undef SF_CRS
684
0
#undef SF_DELTA_N
685
0
#undef SF_M0
686
0
#undef SF_CUC
687
0
#undef SF_E
688
0
#undef SF_CUS
689
0
#undef SF_SQRT_A
690
0
#undef SF_TOE
691
0
#undef SF_CIC
692
0
#undef SF_OMEGA0
693
0
#undef SF_CIS
694
0
#undef SF_I0
695
0
#undef SF_CRC
696
0
#undef SF_OMEGA
697
0
#undef SF_OMEGADOT
698
0
#undef SF_IDOT
699
700
0
        return 0;
701
0
    }
702
703
// Channel Status
704
    static gps_mask_t handle_0x86(struct gps_device_t *session)
705
0
    {
706
0
        size_t n, i, nsu;
707
0
        unsigned char *buf = session->lexer.outbuffer + 3;
708
0
        unsigned msg_len = getleu16(buf, 1);
709
0
        unsigned short week = getleu16(buf, 3);
710
0
        uint32_t tow = getleu32(buf, 5);
711
0
        uint8_t eng_status = getub(buf, 9);
712
0
        uint16_t sol_status = getleu16(buf, 10);
713
0
        uint8_t sats_visible = getub(buf, 12);
714
        // uint8_t sats_tracked = getub(buf, 13);
715
        // uint8_t used_sats = getub(buf, 14);
716
        // uint8_t pdop = getub(buf, 15);
717
0
        timespec_t ts_tow;
718
719
0
        MSTOTS(&ts_tow, tow);
720
721
        // Timestamp
722
0
        session->gpsdata.skyview_time = gpsd_gpstime_resolv(session, week,
723
0
                                                            ts_tow);
724
725
        // Give this driver a single point of truth about DOPs
726
        // session->gpsdata.dop.pdop = (int)pdop / 10.0;
727
728
        // Satellite count
729
0
        session->gpsdata.satellites_visible = sats_visible;
730
0
        if (MAXCHANNELS < session->gpsdata.satellites_visible) {
731
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
732
0
                     "NAVCOM: too many satellites %d\n",
733
0
                     session->gpsdata.satellites_visible);
734
0
            session->gpsdata.satellites_visible = MAXCHANNELS;
735
0
        }
736
737
        // Fix mode
738
0
        switch (sol_status & 0x05) {
739
0
        case 0x05:
740
0
            session->newdata.status = STATUS_DGPS;
741
0
            break;
742
0
        case 0x01:
743
0
            session->newdata.status = STATUS_GPS;
744
0
            break;
745
0
        default:
746
0
            session->newdata.status = STATUS_UNK;
747
0
        }
748
749
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
750
0
                 "Navcom: engine status 0x%x almanac %s time 0x%x pos 0x%x\n",
751
0
                 eng_status & 0x07, ((eng_status & 0x08) ? "valid" : "invalid"),
752
0
                 eng_status & 0x30 >> 4, eng_status & 0xc0 >> 6);
753
754
        // Satellite details
755
0
        i = nsu = 0;
756
0
        if (298 < msg_len) {
757
            /* pacify coverity
758
             * msg_len = 18 + (14 * nsat)
759
             * assume 20 sats max */
760
0
            msg_len = 298;
761
0
        }
762
763
0
        if (msg_len > session->lexer.outbuflen) {
764
            // not enough data
765
0
            return 0;
766
0
        }
767
0
        if (0 != ((msg_len - 18) % 14)) {
768
            // not 18 byte header and 14 bytes per sat.
769
0
            return 0;
770
0
        }
771
772
        // 14-byte sat record must end before the checksum byte.
773
0
        for (n = 17; n + 14 < msg_len; n += 14) {
774
0
            uint8_t prn, ele, ca_snr, p2_snr, log_channel, hw_channel, s, stat;
775
0
            uint16_t azm, dgps_age;
776
0
            if (i >= MAXCHANNELS) {
777
0
                GPSD_LOG(LOG_ERROR, &session->context->errout,
778
0
                         "Navcom: packet type 0x86: too many satellites!\n");
779
0
                gpsd_zero_satellites(&session->gpsdata);
780
0
                return 0;
781
0
            }
782
0
            prn = getub(buf, n);
783
            /*
784
             * This field is described in the Technical Reference as follows:
785
             *
786
             * Channel Tracking Status:
787
             * B0-B1: C/A tracking status
788
             * B2-B3: P1 tracking status
789
             * B4-B5: P2 tracking status
790
             *    00 Acquisition or reacquisition
791
             *    01 Code loop locked
792
             *    02 Costas loop locked
793
             *    11 Full tracking with aiding and active
794
             *       multipath reduction - all data is valid
795
             * B6=1: C/A Bit sync
796
             * B7=1: C/A Frame sync
797
             *
798
             * By observation, the satellite is in use if this status is 0xff.
799
             * But errors here are not very serious, all they can affect is
800
             * the coverance-matrix calculation for error modeling.
801
             */
802
0
            stat = getub(buf, n + 1);
803
0
            log_channel = getub(buf, n + 2);
804
0
            ele = getub(buf, n + 5);
805
0
            azm = getleu16(buf, n + 6);
806
0
            ca_snr = getub(buf, n + 8);
807
0
            p2_snr = getub(buf, n + 10);
808
0
            dgps_age = getleu16(buf, n + 11);
809
0
            hw_channel = getub(buf, n + 13);
810
0
            s = (unsigned char)0;
811
            /* NOTE - In theory, I think one would check for hw channel number
812
             * to see if one is dealing with a GPS or other satellite, but the
813
             * channel numbers reported bear no resemblance to what the spec
814
             * says should be.  So I check for the fact that if all three
815
             * values below are zero, one is not interested in this satellite
816
             */
817
0
            if (!(ele == 0 && azm == 0 && dgps_age == 0)) {
818
0
                session->gpsdata.skyview[i].PRN = (short)prn;
819
0
                session->gpsdata.skyview[i].elevation = (double)ele;
820
0
                session->gpsdata.skyview[i].azimuth = (double)azm;
821
0
                s = session->gpsdata.skyview[i].ss =
822
0
                    (p2_snr ? p2_snr : ca_snr) / 4.0;
823
0
                session->gpsdata.skyview[i++].used = (stat == 0xff);
824
0
                if (0xff == stat) {
825
0
                    nsu++;
826
0
                }
827
0
            }
828
0
            session->gpsdata.satellites_used = nsu;
829
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
830
0
                     "Navcom: prn = %3u, ele = %02u, azm = %03u, snr = %d (%s), "
831
0
                     "dgps age = %.1fs, log ch = %d, hw ch = 0x%02x\n",
832
0
                     prn, ele, azm, s, (p2_snr ? "P2" : "C/A"),
833
0
                     (double)dgps_age * 0.1, log_channel & 0x3f, hw_channel);
834
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
835
0
                     "Navcom:           sol. valid = %c, clock = %s, "
836
0
                     "pos. = %s, height = %s, err. code = 0x%x\n",
837
0
                     ((sol_status & 0x01) ? 'Y' : 'N'),
838
0
                     ((sol_status & 0x02) ? "stable" : "unstable"),
839
0
                     ((sol_status & 0x04) ? "dgps" : "unaided"),
840
0
                     ((sol_status & 0x08) ? "solved" : "constrained"),
841
0
                     ((sol_status & 0x01) ? 0x00 : sol_status & 0x0f00 >> 8));
842
0
        }
843
844
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
845
0
                 "CS 0x86: visible=%d, used=%d, mask={SATELLITE|STATUS}\n",
846
0
                 session->gpsdata.satellites_visible,
847
0
                 session->gpsdata.satellites_used);
848
0
        return SATELLITE_SET | STATUS_SET;
849
0
    }
850
851
/* Raw Meas. Data Block
852
 * Size 4 + 8 + (16 * numSat) = 524
853
 */
854
    static gps_mask_t handle_0xb0(struct gps_device_t *session)
855
0
    {
856
0
        char ts_buf[TIMESPEC_LEN];
857
        /* L1 wavelength (299792458m/s / 1575420000Hz)
858
         * from their Technical reference Manual */
859
0
#define LAMBDA_L1 (299792458.0 / 1575420000.0)
860
0
        unsigned n;
861
0
        unsigned char *buf = session->lexer.outbuffer + 3;
862
0
        unsigned msg_len = getleu16(buf, 1);
863
0
        uint16_t week = getleu16(buf, 3);
864
0
        uint32_t tow = getleu32(buf, 5);
865
0
        uint8_t tm_slew_acc = getub(buf, 9);
866
0
        uint8_t status = getub(buf, 10);
867
868
0
        session->context->gps_week = (unsigned short)week;
869
0
        MSTOTS(&session->context->gps_tow, tow);
870
871
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
872
0
                 "Navcom: received packet type 0xb0 (Raw Meas. Data Block)\n");
873
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
874
0
                 "Navcom: week = %u, tow = %s "
875
0
                 "time slew accumulator = %u (1/1023mS), status = 0x%02x "
876
0
                 "(%sclock %s - %u blocks follow) msg_len %u\n",
877
0
                 session->context->gps_week,
878
0
                 timespec_str(&session->context->gps_tow, ts_buf,
879
0
                              sizeof(ts_buf)),
880
0
                 tm_slew_acc, status,
881
0
                 ((status & 0x80) ? "channel time set - " : ""),
882
0
                 ((status & 0x40) ? "stable" : "not stable"), status & 0x0f,
883
0
                 msg_len);
884
885
0
        if (530 < msg_len) {
886
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
887
0
                     "Navcom: received packet type 0xb0, length %u too long\n",
888
0
                     msg_len);
889
0
            return 0;
890
0
        }
891
0
        if (0 != ((msg_len - 12) % 16)) {
892
            // not 12 byte header and 16 bytes per sat.
893
0
            return 0;
894
0
        }
895
896
0
        for (n = 11; n < (msg_len - 1); n += 16) {
897
0
            uint8_t sv_status = getub(buf, n);
898
0
            uint8_t ch_status = getub(buf, n + 1);
899
0
            uint32_t ca_pseudorange = getleu32(buf, n + 2);
900
            // integer division by 16 is sign-preserving right shift of 4 bits
901
0
            int32_t l1_phase = getles3224(buf, n + 6) / 16;
902
0
            uint8_t l1_slips = (uint8_t) (getles3224(buf, n + 6) & 0x0f);
903
0
            int16_t p1_ca_pseudorange = getles16(buf, n + 9);
904
0
            int16_t p2_ca_pseudorange = getles16(buf, n + 11);
905
0
            int32_t l2_phase = getles3224(buf, n + 13) / 16;
906
0
            uint8_t l2_slips = (uint8_t) (getles3224(buf, n + 13) & 0x0f);
907
0
            double c1 =
908
0
                ((sv_status & 0x80) ? (double)ca_pseudorange / 16.0 *
909
0
                 LAMBDA_L1 : NAN);
910
0
            double l1 =
911
0
                ((sv_status & 0x80) ? (double)ca_pseudorange / 16.0 +
912
0
                 (double)l1_phase / 256.0 : NAN);
913
0
            double l2 =
914
0
                ((sv_status & 0x20)
915
0
             ? ((double)ca_pseudorange / 16.0 +
916
0
                (double)p2_ca_pseudorange / 16.0) * (120.0 / 154.0)
917
0
                 + (double)l2_phase / 256.0 : NAN);
918
0
            double p1 =
919
0
                ((sv_status & 0x40) ? c1 +
920
0
                 (double)p1_ca_pseudorange / 16.0 * LAMBDA_L1 : NAN);
921
0
            double p2 =
922
0
                ((sv_status & 0x20) ? c1 +
923
0
                 (double)p2_ca_pseudorange / 16.0 * LAMBDA_L1 : NAN);
924
0
            GPSD_LOG(LOG_SPIN, &session->context->errout,
925
0
                     "Navcom: >> sv status = 0x%02x (PRN %u - C/A & L1 %s "
926
0
                     "- P1 %s - P2 & L2 %s)\n",
927
0
                     sv_status, (sv_status & 0x1f),
928
0
                     ((sv_status & 0x80) ? "valid" : "invalid"),
929
0
                     ((sv_status & 0x40) ? "valid" : "invalid"),
930
0
                     ((sv_status & 0x20) ? "valid" : "invalid"));
931
0
            GPSD_LOG(LOG_SPIN, &session->context->errout,
932
0
                     "Navcom: >>> ch status = 0x%02x "
933
0
                     "(Logical channel: %u - CA C/No: %u dBHz) "
934
0
                     "sL1: %u, sL2: %u\n", ch_status, ch_status & 0x0f,
935
0
                     ((ch_status & 0xf0) >> 4) + 35, l1_slips, l2_slips);
936
0
            GPSD_LOG(LOG_SPIN, &session->context->errout,
937
0
                     "Navcom: >>> C1: %14.3f L1: %14.3f L2: %14.3f "
938
0
                     "P1: %14.3f P2: %14.3f\n",
939
0
                     c1, l1, l2, p1, p2);
940
0
        }
941
0
#undef LAMBDA_L1
942
0
        return 0;           // FIXME: use raw measurements
943
0
    }
944
945
// Pseudorange Noise Statistics
946
    static gps_mask_t handle_0xb5(struct gps_device_t *session)
947
0
    {
948
0
        if (sizeof(double) == 8) {
949
0
            gps_mask_t mask = TIME_SET;
950
0
            char *buf = (char *)session->lexer.outbuffer + 3;
951
0
            uint16_t week = getleu16(buf, 3);
952
0
            uint32_t tow = getleu32(buf, 5);
953
0
            timespec_t ts_tow;
954
#ifdef __UNUSED__
955
                double rms = getled64(buf, 9);
956
                /* Reason why it's unused is these figures do not agree
957
                 * with those obtained from the PVT report (handle_0xb1).
958
                 * The figures from 0xb1 do agree with the values reported
959
                 * by Navcom's PC utility */
960
                // let gpsd_error_model() handle this
961
                // double ellips_maj = getled64(buf, 17);
962
                // double ellips_min = getled64(buf, 25);
963
                // double ellips_azm = getled64(buf, 33);
964
                double lat_sd = getled64(buf, 41);
965
                double lon_sd = getled64(buf, 49);
966
                double alt_sd = getled64(buf, 57);
967
                double hrms = sqrt(pow(lat_sd, 2) + pow(lon_sd, 2));
968
                // Navcom doc unclear, this is likely sep?
969
                session->newdata.sep = rms * 1.96;
970
                session->newdata.eph = hrms * 1.96;
971
                session->newdata.epv = alt_sd * 1.96;
972
                mask |= HERR_SET;
973
#endif  // __UNUSED__
974
0
            MSTOTS(&ts_tow, tow);
975
0
            session->newdata.time = gpsd_gpstime_resolv(session,
976
0
                                                        (unsigned short)week,
977
0
                                                        ts_tow);
978
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
979
0
                     "Navcom: received packet type 0xb5 "
980
0
                     "(Pseudorange Noise Statistics)\n");
981
0
            GPSD_LOG(LOG_DATA, &session->context->errout,
982
0
                     "Navcom: sep = %f\n", session->newdata.sep);
983
0
            return mask;
984
0
        } else {
985
            // Ignore this message block
986
0
            if (!session->driver.navcom.warned) {
987
0
                GPSD_LOG(LOG_WARN, &session->context->errout,
988
0
                         "Navcom: received packet type 0xb5 (Pseudorange "
989
0
                         "Noise Statistics) ignored "
990
0
                         " - sizeof(double) == 64 bits required\n");
991
0
                session->driver.navcom.warned = true;
992
0
            }
993
0
            return 0;           // Block ignored - wrong sizeof(double)
994
0
        }
995
0
    }
996
997
// LBM DSP Status Block
998
    static gps_mask_t handle_0xd3(struct gps_device_t *session UNUSED)
999
0
    {
1000
        /* This block contains status information about the
1001
         * unit's L-band (Inmarsat) module.  There is nothing
1002
         * interesting in it for our purposes so we do not deal
1003
         * with it.  This callback is purely to a) stop
1004
         * "unrecognised packet" messages appearing in the log
1005
         * and b) explain what it is for the curious */
1006
0
        return 0;               // Nothing updated
1007
0
    }
1008
1009
// Identification Block
1010
    static gps_mask_t handle_0xae(struct gps_device_t *session)
1011
0
    {
1012
0
        char *engconfstr, *asicstr;
1013
0
        unsigned char *buf = session->lexer.outbuffer + 3;
1014
0
        size_t msg_len = (size_t) getleu16(buf, 1);
1015
0
        uint8_t engconf = getub(buf, 3);
1016
0
        uint8_t asic = getub(buf, 4);
1017
0
        uint8_t swvermaj = getub(buf, 5);
1018
0
        uint8_t swvermin = getub(buf, 6);
1019
0
        uint16_t dcser = getleu16(buf, 7);
1020
0
        uint8_t dcclass = getub(buf, 9);
1021
0
        uint16_t rfcser = getleu16(buf, 10);
1022
0
        uint8_t rfcclass = getub(buf, 12);
1023
0
        uint8_t softtm[17] = {0};
1024
0
        uint8_t bootstr[17] = {0};
1025
0
        uint8_t ioptm[17] = {0};
1026
0
        uint8_t iopvermaj = (uint8_t) 0x00;
1027
0
        uint8_t iopvermin = (uint8_t) 0x00;
1028
0
        uint8_t picver = (uint8_t) 0x00;
1029
0
        uint8_t slsbn = (uint8_t) 0x00;
1030
0
        uint8_t iopsbn = (uint8_t) 0x00;
1031
1032
0
        memcpy(softtm, &buf[13], 16);
1033
0
        memcpy(bootstr, &buf[29], 16);
1034
0
        if (0x0037 == msg_len) { // No IOP
1035
0
            slsbn = getub(buf, 53);
1036
0
        } else {                 // IOP Present
1037
0
            iopvermaj = getub(buf, 53);
1038
0
            iopvermin = getub(buf, 54);
1039
0
            memcpy(ioptm, &buf[55], 16);
1040
0
            picver = getub(buf, 71);
1041
0
            slsbn = getub(buf, 72);
1042
0
            iopsbn = getub(buf, 73);
1043
0
        }
1044
1045
0
        switch (engconf) {
1046
0
        case 0x00:
1047
0
            engconfstr = "Unknown/Undefined";
1048
0
            break;
1049
0
        case 0x01:
1050
0
            engconfstr = "NCT 2000 S";
1051
0
            break;
1052
0
        case 0x02:
1053
0
            engconfstr = "NCT 2000 D";
1054
0
            break;
1055
0
        case 0x03:
1056
0
            engconfstr = "Startfire Single";
1057
0
            break;
1058
0
        case 0x04:
1059
0
            engconfstr = "Starfire Dual";
1060
0
            break;
1061
0
        case 0x05:
1062
0
            engconfstr = "Pole Mount RTK (Internal Radio)";
1063
0
            break;
1064
0
        case 0x06:
1065
0
            engconfstr = "Pole Mount GIS (LBM)";
1066
0
            break;
1067
0
        case 0x07:
1068
0
            engconfstr = "Black Box RTK (Internal Radio)";
1069
0
            break;
1070
0
        case 0x08:
1071
0
            engconfstr = "Black Box GIS (LBM)";
1072
0
            break;
1073
0
        case 0x80:
1074
0
            engconfstr = "R100";
1075
0
            break;
1076
0
        case 0x81:
1077
0
            engconfstr = "R200";
1078
0
            break;
1079
0
        case 0x82:
1080
0
            engconfstr = "R210";
1081
0
            break;
1082
0
        case 0x83:
1083
0
            engconfstr = "R300";
1084
0
            break;
1085
0
        case 0x84:
1086
0
            engconfstr = "R310";
1087
0
            break;
1088
0
        default:
1089
0
            engconfstr = "?";
1090
0
        }
1091
1092
0
        switch (asic) {
1093
0
        case 0x01:
1094
0
            asicstr = "A-ASIC";
1095
0
            break;
1096
0
        case 0x02:
1097
0
            asicstr = "B-ASIC";
1098
0
            break;
1099
0
        case 0x03:
1100
0
            asicstr = "C-ASIC";
1101
0
            break;
1102
0
        case 0x04:
1103
0
            asicstr = "M-ASIC";
1104
0
            break;
1105
0
        default:
1106
0
            asicstr = "?";
1107
0
        }
1108
1109
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
1110
0
                 "Navcom: received packet type 0xae (Identification Block)\n");
1111
0
        if (0x0037 == msg_len) {
1112
0
            GPSD_LOG(LOG_INF, &session->context->errout, "Navcom: ID Data: "
1113
0
                     "%s %s Ver. %u.%u.%u, DC S/N: %u.%u, RF S/N: %u.%u, "
1114
0
                     "Build ID: %s, Boot software: %s\n",
1115
0
                     engconfstr, asicstr, swvermaj, swvermin, slsbn, dcser,
1116
0
                     dcclass, rfcser, rfcclass, softtm, bootstr);
1117
0
        } else {
1118
0
            GPSD_LOG(LOG_INF, &session->context->errout, "Navcom: ID Data: "
1119
0
                     "%s %s Ver. %u.%u.%u, DC S/N: %u.%u, RF S/N: %u.%u, "
1120
0
                     "Build ID: %s, Boot software: %s, "
1121
0
                     "IOP Ver.: %u.%u.%u, PIC: %u, IOP Build ID: %s\n",
1122
0
                     engconfstr, asicstr, swvermaj, swvermin, slsbn, dcser,
1123
0
                     dcclass, rfcser, rfcclass, softtm, bootstr, iopvermaj,
1124
0
                     iopvermin, iopsbn, picver, ioptm);
1125
0
        }
1126
1127
0
        (void)snprintf(session->subtype, sizeof(session->subtype),
1128
0
                       "%s %s Ver. %u.%u.%u S/N %u.%u %u.%u",
1129
0
                       engconfstr, asicstr, swvermaj, swvermin, slsbn, dcser,
1130
0
                       dcclass, rfcser, rfcclass);
1131
0
        return DEVICEID_SET;
1132
0
    }
1133
1134
// Clock Drift and Offset
1135
    static gps_mask_t handle_0xef(struct gps_device_t *session)
1136
0
    {
1137
0
        unsigned char *buf = session->lexer.outbuffer + 3;
1138
        // uint16_t week = getleu16(buf, 3);
1139
        // uint32_t tow = getleu32(buf, 5);
1140
0
        int8_t osc_temp = getsb(buf, 9);
1141
0
        uint8_t nav_status = getub(buf, 10);
1142
0
        double nav_clock_offset;
1143
0
        float nav_clock_drift;
1144
0
        float osc_filter_drift_est;
1145
0
        char ts_buf[TIMESPEC_LEN];
1146
0
        int32_t time_slew = (int32_t) getles32(buf, 27);
1147
0
        if (sizeof(double) == 8) {
1148
0
            nav_clock_offset = getled64((char *)buf, 11);
1149
0
        } else {
1150
0
            nav_clock_offset = NAN;
1151
0
        }
1152
0
        if (sizeof(float) == 4) {
1153
0
            nav_clock_drift = getlef32((char *)buf, 19);
1154
0
            osc_filter_drift_est = getlef32((char *)buf, 23);
1155
0
        } else {
1156
0
            nav_clock_drift = NAN;
1157
0
            osc_filter_drift_est = NAN;
1158
0
        }
1159
1160
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
1161
0
                 "Navcom: oscillator temp. = %d, nav. status = 0x%02x, "
1162
0
                 "nav. clock offset = %f, nav. clock drift = %f, "
1163
0
                 "osc. filter drift est. = %f, acc.time slew value = %d\n",
1164
0
                 osc_temp, nav_status, nav_clock_offset, nav_clock_drift,
1165
0
                 osc_filter_drift_est, time_slew);
1166
0
        GPSD_LOG(LOG_DATA, &session->context->errout,
1167
0
                 "CDO 0xef: time=%s mask={TIME}\n",
1168
0
                 timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)));
1169
0
        return 0;
1170
0
    }
1171
1172
1173
    gps_mask_t navcom_parse(struct gps_device_t *session, unsigned char *buf,
1174
                            size_t len)
1175
0
    {
1176
0
        unsigned char cmd_id;
1177
0
        unsigned msg_len;
1178
0
        unsigned bad_len = 0;
1179
0
        gps_mask_t mask = 0;
1180
1181
0
        if (7 > len) {
1182
            // min msg: STX, 0x99, 0x66, cmd_id, msg_len(2), ..., checksum
1183
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1184
0
                     "Navcom: too short, len %zu\n", len);
1185
0
            return 0;
1186
0
        }
1187
        // Navcom checksum already checked in gpsd/parser.c
1188
1189
0
        cmd_id = (unsigned char)getub(buf, 3);
1190
        // payload = &buf[6];
1191
0
        msg_len = getleu16(buf, 4);
1192
1193
0
        if (4 > msg_len ||
1194
0
            (msg_len + 4) > len) {
1195
            /* min payload: cmd_id, msg_len(2), cksum
1196
             * Fuzzers love to do this. */
1197
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1198
0
                     "Navcom: bad lengths, msg_len %u len %zu\n", msg_len, len);
1199
0
            return 0;
1200
0
        }
1201
1202
0
        GPSD_LOG(LOG_RAW, &session->context->errout,
1203
0
                 "Navcom: packet type x%02x msg_len %u len %zu\n",
1204
0
                 cmd_id, msg_len, len);
1205
1206
0
        session->cycle_end_reliable = true;
1207
1208
0
        switch (cmd_id) {
1209
0
        case 0x06:
1210
0
            if (6 > msg_len) {
1211
0
                bad_len = 6;
1212
0
                break;
1213
0
            }
1214
0
            mask = handle_0x06(session);
1215
0
            break;
1216
0
        case 0x15:
1217
0
            if (6 > msg_len) {
1218
                // 6 + n * 2
1219
0
                bad_len = 6;
1220
0
                break;
1221
0
            }
1222
0
            mask = handle_0x15(session);
1223
0
            break;
1224
0
        case 0x81: // Packed Ephemeris Data
1225
0
            if (86 > msg_len) {
1226
0
                bad_len = 86;
1227
0
                break;
1228
0
            }
1229
0
            mask = handle_0x81(session);
1230
0
            break;
1231
0
        case 0x83:
1232
0
            if (32 > msg_len) {
1233
0
                bad_len = 32;
1234
0
                break;
1235
0
            }
1236
0
            mask = handle_0x83(session);
1237
0
            break;
1238
0
        case 0x86:
1239
0
            if (18 > msg_len) {
1240
                // 18 + (14 * N)
1241
0
                bad_len = 18;
1242
0
                break;
1243
0
            }
1244
0
            mask = handle_0x86(session);
1245
0
            break;
1246
0
        case 0xae:
1247
0
            if (55 > msg_len ||
1248
0
                75 > msg_len) {
1249
0
                bad_len = 55;
1250
0
                break;
1251
0
            }
1252
0
            mask = handle_0xae(session);
1253
0
            break;
1254
0
        case 0xb0:
1255
0
            if (12 > msg_len) {
1256
                // 4 + 8 + (16 * N)
1257
0
                bad_len = 12;
1258
0
                break;
1259
0
            }
1260
0
            mask = handle_0xb0(session);
1261
0
            break;
1262
0
        case 0xb1:
1263
0
            if (86 > msg_len) {
1264
0
                bad_len = 86;
1265
0
                break;
1266
0
            }
1267
0
            mask = handle_0xb1(session);
1268
0
            break;
1269
0
        case 0xb5:
1270
0
            if (66 > msg_len) {
1271
0
                bad_len = 66;
1272
0
                break;
1273
0
            }
1274
0
            mask = handle_0xb5(session);
1275
0
            break;
1276
0
        case 0xd3:
1277
0
            if (70 > msg_len) {
1278
0
                bad_len = 70;
1279
0
                break;
1280
0
            }
1281
0
            mask = handle_0xd3(session);
1282
0
            break;
1283
0
        case 0xef:
1284
0
            if (32 > msg_len) {
1285
0
                bad_len = 32;
1286
0
                break;
1287
0
            }
1288
0
            mask = handle_0xef(session);
1289
0
            break;
1290
0
        case 0x44: // Packed Almanac Loading
1291
0
            FALLTHROUGH
1292
0
        default:
1293
0
            GPSD_LOG(LOG_PROG, &session->context->errout,
1294
0
                     "Navcom: received packet type 0x%02x, msg_len %d - "
1295
0
                     "unknown or unimplemented\n",
1296
0
                     cmd_id, msg_len);
1297
0
            break;
1298
0
        }
1299
0
        if (bad_len) {
1300
0
            GPSD_LOG(LOG_WARN, &session->context->errout,
1301
0
                     "Navcom: %u: runt payload len %u s/b %zd",
1302
0
                     cmd_id, bad_len, session->lexer.outbuflen);
1303
0
        }
1304
0
        return mask;
1305
0
    }
1306
1307
1308
    static gps_mask_t navcom_parse_input(struct gps_device_t *session)
1309
0
    {
1310
0
        if (NAVCOM_PACKET == session->lexer.type) {
1311
0
            return navcom_parse(session, session->lexer.outbuffer,
1312
0
                                session->lexer.outbuflen);
1313
0
        }
1314
0
        if (NMEA_PACKET == session->lexer.type) {
1315
0
            return nmea_parse((char *)session->lexer.outbuffer, session);
1316
0
        }
1317
0
        return 0;
1318
0
    }
1319
1320
    static ssize_t navcom_control_send(struct gps_device_t *session,
1321
                                       char *buf, size_t len)
1322
0
    {
1323
0
        putbyte(session->msgbuf, 0, 0x02);
1324
0
        putbyte(session->msgbuf, 1, 0x99);
1325
0
        putbyte(session->msgbuf, 2, 0x66);
1326
0
        putbyte(session->msgbuf, 3, buf[0]);    // Cmd ID
1327
0
        putle16(session->msgbuf, 4, len + 4);   // Length
1328
0
        memcpy(session->msgbuf, buf + 6, len - 1);
1329
0
        putbyte(session->msgbuf, 6 + len,
1330
0
                checksum((unsigned char *)session->msgbuf + 3, len + 5));
1331
0
        putbyte(session->msgbuf, 7 + len, 0x03);
1332
0
        session->msgbuflen = len + 9;
1333
0
        return gpsd_write(session, session->msgbuf, session->msgbuflen);
1334
0
    }
1335
1336
    static bool navcom_speed(struct gps_device_t *session,
1337
                             speed_t speed, char parity, int stopbits)
1338
0
    {
1339
        // parity and stopbit switching aren't implemented
1340
0
        if (parity != session->gpsdata.dev.parity
1341
0
            || stopbits != (int)session->gpsdata.dev.stopbits) {
1342
0
            return false;
1343
0
        } else {
1344
0
            uint8_t port, port_selection;
1345
0
            uint8_t baud;
1346
0
            if (session->driver.navcom.physical_port == (uint8_t) 0xFF) {
1347
                // We still don't know which port we're connected to
1348
0
                return false;
1349
0
            }
1350
0
            switch (speed) {
1351
            /* NOTE - The spec says that certain baud combinations
1352
             * on ports A and B are not allowed, those are
1353
             * 1200/115200, 2400/57600, and 2400/115200.
1354
             * To try and minimise the possibility of those
1355
             * occurring, we do not allow baud rates below
1356
             * 4800.  We could also disallow 57600 and 115200
1357
             * to totally prevent this, but I do not consider
1358
             * that reasonable.  Finding which baud speed the
1359
             * other port is set at would also be too much
1360
             * trouble, so we do not do it. */
1361
0
            case 4800:
1362
0
                baud = 0x04;
1363
0
                break;
1364
0
            case 9600:
1365
0
                baud = 0x06;
1366
0
                break;
1367
0
            case 19200:
1368
0
                baud = 0x08;
1369
0
                break;
1370
0
            case 38400:
1371
0
                baud = 0x0a;
1372
0
                break;
1373
0
            case 57600:
1374
0
                baud = 0x0c;
1375
0
                break;
1376
0
            case 115200:
1377
0
                baud = 0x0e;
1378
0
                break;
1379
0
            default:
1380
                // Unsupported speed
1381
0
                return false;
1382
0
            }
1383
1384
            // Proceed to construct our message
1385
0
            port = session->driver.navcom.physical_port;
1386
0
            port_selection = (port ? port : (uint8_t) 0xff) | baud;
1387
1388
            // Send it off
1389
0
            navcom_cmd_0x11(session, port_selection);
1390
1391
            /* And cheekily return true, even though we have
1392
             * no way to know if the speed change succeeded
1393
             * until and if we receive an ACK (message 0x06),
1394
             * which will be at the new baud speed if the
1395
             * command was successful.  Bottom line, the client
1396
             * should requery gpsd to see if the new speed is
1397
             * different than the old one */
1398
            return true;
1399
0
        }
1400
0
    }
1401
1402
// this is everything we export
1403
// *INDENT-OFF*
1404
const struct gps_type_t driver_navcom =
1405
{
1406
    .type_name      = "Navcom NCT",             // full name of type
1407
    .packet_type    = NAVCOM_PACKET,            // lexer packet type
1408
    .flags          = DRIVER_STICKY,            // remember this
1409
    .trigger        = NULL,                     // none
1410
    .channels       = NAVCOM_CHANNELS,     // 12 L1 + 12 L2 + 2 Inmarsat L-Band
1411
    .probe_detect   = NULL,                     // no probe
1412
    .get_packet     = packet_get1,              // use generic one
1413
    .parse_packet   = navcom_parse_input,       // parse message packets
1414
    .rtcm_writer    = gpsd_write,               // send RTCM data straight
1415
    .init_query     = NULL,                     // non-perturbing query
1416
    .event_hook     = navcom_event_hook,        // lifetime event handler
1417
    .speed_switcher = navcom_speed,             // we do change baud rates
1418
    .mode_switcher  = NULL,                     // there is not a mode switcher
1419
    .rate_switcher  = NULL,                     // no sample-rate switcher
1420
    .min_cycle.tv_sec  = 1,             // not relevant, no rate switch
1421
    .min_cycle.tv_nsec = 0,             // not relevant, no rate switch
1422
    .control_send   = navcom_control_send,      // how to send a control string
1423
    .time_offset     = NULL,            // no method for NTP fudge factor
1424
};
1425
// *INDENT-ON*
1426
1427
#endif  // defined(NAVCOM_ENABLE)
1428
// vim: set expandtab shiftwidth=4