Coverage Report

Created: 2026-10-17 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gpsd/gpsd-3.27.6~dev/drivers/driver_greis.c
Line
Count
Source
1
/*
2
 * A Javad GNSS Receiver External Interface Specification (GREIS) driver.
3
 *
4
 * Author(s):
5
 * - Gregory Fong <gregory.fong@virginorbit.com>
6
 *
7
 * Documentation for GREIS can be found at:
8
http://www.javad.com/downloads/javadgnss/manuals/GREIS/GREIS_Reference_Guide.pdf
9
 *
10
 * The version used for reference is that which
11
 * "Reflects Firmware Version 3.6.7, Last revised: August 25, 2016".
12
 *
13
 * This assumes little endian byte order in messages, which is the default, but
14
 * that is configurable. A future improvement could change to read the
15
 * information in [MF] Message Format.
16
 *
17
 * This file is Copyright Virgin Orbit
18
 * This file is Copyright the GPSD project
19
 * SPDX-License-Identifier: BSD-2-clause
20
 */
21
22
#include "../include/gpsd_config.h"   // must be before all includes
23
24
#include <assert.h>
25
#include <math.h>
26
#include <stdbool.h>
27
#include <stdio.h>
28
#include <stdlib.h>       // for abs()
29
#include <string.h>
30
#include <sys/select.h>
31
32
#include "../include/bits.h"
33
#include "../include/driver_greis.h"
34
#include "../include/gpsd.h"
35
#include "../include/timespec.h"
36
37
#if defined(GREIS_ENABLE)
38
39
31.5k
#define HEADER_LENGTH 5
40
41
static ssize_t greis_write(struct gps_device_t *session,
42
                           const char *msg, size_t msglen);
43
static const char disable_messages[] = "\%dm\%dm";
44
static const char get_vendor[] = "\%vendor\%print,/par/rcv/vendor";
45
static const char get_ver[] = "\%ver\%print,rcv/ver";
46
static const char set_update_rate_4hz[] = "\%msint\%set,/par/raw/msint,250";
47
48
// Where applicable, the order here is how these will be received per cycle.
49
// TODO: stop hardcoding the cycle time, make it selectable
50
static const char enable_messages_4hz[] =
51
    "\%em\%em,,jps/{RT,UO,GT,PV,SG,DP,SI,EL,AZ,EC,SS,ET}:0.25";
52
53
/*
54
 * GREIS message handlers. The checksum has been already confirmed valid in the
55
 * packet acceptance logic, so we don't need to retest it here.
56
 */
57
58
/**
59
 * Handle the message [RE] Reply
60
 */
61
static gps_mask_t greis_msg_RE(struct gps_device_t *session,
62
                               unsigned char *buf, size_t len)
63
5.19k
{
64
5.19k
    if (0 == memcmp(buf, "%ver%", 5)) {
65
97
        strlcpy(session->subtype, (const char*)&buf[5],
66
97
                sizeof(session->subtype));
67
97
        GPSD_LOG(LOG_DATA, &session->context->errout,
68
97
                 "GREIS: RE, ->subtype: %s\n", session->subtype);
69
97
        return DEVICEID_SET;
70
97
    }
71
72
5.09k
    GPSD_LOG(LOG_INFO, &session->context->errout,
73
5.09k
             "GREIS: RE %3zd, reply: %.*s\n", len, (int)len, buf);
74
5.09k
    return 0;
75
5.19k
}
76
77
/**
78
 * Handle the message [ER] Reply
79
 */
80
static gps_mask_t greis_msg_ER(struct gps_device_t *session,
81
                               unsigned char *buf, size_t len)
82
83
{
83
83
    GPSD_LOG(LOG_WARN, &session->context->errout,
84
83
             "GREIS: ER %3zd, reply: %.*s\n", len, (int)len, buf);
85
83
    return 0;
86
83
}
87
88
/**
89
 * Handle the message [~~](RT) Receiver Time.
90
 */
91
static gps_mask_t greis_msg_RT(struct gps_device_t *session,
92
                               unsigned char *buf, size_t len)
93
241
{
94
241
    if (len < 5) {
95
76
        GPSD_LOG(LOG_WARN, &session->context->errout,
96
76
                 "GREIS: RT bad len %zu\n", len);
97
76
        return 0;
98
76
    }
99
100
165
    session->driver.greis.rt_tod = getleu32(buf, 0);
101
165
    memset(&session->gpsdata.raw, 0, sizeof(session->gpsdata.raw));
102
103
165
    session->driver.greis.seen_rt = true;
104
165
    session->driver.greis.seen_az = false;
105
165
    session->driver.greis.seen_ec = false;
106
165
    session->driver.greis.seen_el = false;
107
165
    session->driver.greis.seen_si = false;
108
165
    GPSD_LOG(LOG_DATA, &session->context->errout,
109
165
             "GREIS: RT, tod: %lu\n",
110
165
             (unsigned long)session->driver.greis.rt_tod);
111
112
165
    return CLEAR_IS;
113
241
}
114
115
/**
116
 * Handle the message [UO] GPS UTC Time Parameters.
117
 */
118
static gps_mask_t greis_msg_UO(struct gps_device_t *session,
119
                               unsigned char *buf, size_t len)
120
0
{
121
    /*
122
     * For additional details on these parameters and the computation done using
123
     * them, refer to the Javad GREIS spec mentioned at the top of this file and
124
     * also to ICD-GPS-200C, Revision IRN-200C-004 April 12, 2000. At the time
125
     * of writing, that could be found at
126
     * https://www.navcen.uscg.gov/pubs/gps/icd200/ICD200Cw1234.pdf .
127
     */
128
0
    uint32_t tot;           // Reference time of week [s]
129
0
    uint16_t wnt;           // Reference week number [dimensionless]
130
0
    int8_t dtls;            // Delta time due to leap seconds [s]
131
0
    uint8_t dn;             // 'Future' reference day number [1..7]
132
0
    uint16_t wnlsf;         // 'Future' reference week number [dimensionless]
133
0
    int8_t dtlsf;           // 'Future' delta time due to leap seconds [s]
134
135
0
    if (len < 24) {
136
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
137
0
                 "GREIS: UO bad len %zu\n", len);
138
0
        return 0;
139
0
    }
140
141
0
    tot = getleu32(buf, 12);
142
0
    wnt = getleu16(buf, 16);
143
0
    dtls = getsb(buf, 18);
144
0
    dn = getub(buf, 19);
145
0
    wnlsf = getleu16(buf, 20);
146
0
    dtlsf = getsb(buf, 22);
147
0
    session->driver.greis.seen_uo = true;
148
149
    /*
150
     * See ICD-GPS-200C 20.3.3.5.2.4 "Universal Coordinated Time (UTC)".
151
     * I totally ripped this off of driver_navcom.c.  Might want to dedupe at
152
     * some point.
153
     */
154
0
    if ((wnt % 256U) * 604800U + tot < wnlsf * 604800U + dn * 86400U) {
155
        // Current time is before effectivity time of the leap second event
156
0
        session->context->leap_seconds = dtls;
157
0
    } else {
158
0
        session->context->leap_seconds = dtlsf;
159
0
    }
160
161
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
162
0
             "GREIS: UO, leap_seconds: %d\n", session->context->leap_seconds);
163
164
0
    return 0;
165
0
}
166
167
/**
168
 * Handle the message [GT] GPS Time.
169
 */
170
static gps_mask_t greis_msg_GT(struct gps_device_t *session,
171
                               unsigned char *buf, size_t len)
172
135
{
173
135
    timespec_t ts_tow;
174
135
    uint32_t tow;            // Time of week [ms]
175
135
    uint16_t wn;             // GPS week number (modulo 1024) [dimensionless]
176
135
    char ts_buf[TIMESPEC_LEN];
177
178
135
    if (len < 7) {
179
69
        GPSD_LOG(LOG_WARN, &session->context->errout,
180
69
                 "GREIS: GT bad len %zu\n", len);
181
69
        return 0;
182
69
    }
183
184
66
    if (!session->driver.greis.seen_uo) {
185
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
186
66
                 "GREIS: can't use GT until after UO has supplied "
187
66
                 "leap second data\n");
188
66
        return 0;
189
66
    }
190
191
0
    tow = getleu32(buf, 0);
192
0
    wn = getleu16(buf, 4);
193
194
0
    MSTOTS(&ts_tow, tow);
195
0
    session->newdata.time = gpsd_gpstime_resolv(session, wn, ts_tow);
196
197
0
    GPSD_LOG(LOG_DATA, &session->context->errout,
198
0
             "GREIS: GT, tow: %" PRIu32 ", wn: %" PRIu16 ", time: %s Leap:%u\n",
199
0
             tow, wn,
200
0
             timespec_str(&session->newdata.time, ts_buf, sizeof(ts_buf)),
201
0
             session->context->leap_seconds);
202
203
204
    // save raw.mtime, just in case
205
0
    session->gpsdata.raw.mtime = session->newdata.time;
206
207
0
    return TIME_SET | NTPTIME_IS | ONLINE_SET;
208
66
}
209
210
/**
211
 * Handle the message [PV] Cartesian Position and Velocity.
212
 */
213
static gps_mask_t greis_msg_PV(struct gps_device_t *session,
214
                               unsigned char *buf, size_t len)
215
332
{
216
332
    double x, y, z;         // Cartesian coordinates [m]
217
332
    float p_sigma;          // Position spherical error probability (SEP) [m]
218
332
    float vx, vy, vz;       // Cartesian velocities [m/s]
219
332
    float v_sigma;          // Velocity SEP [m/s]
220
332
    uint8_t solution_type;
221
332
    gps_mask_t mask = 0;
222
223
332
    if (len < 46) {
224
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
225
66
                 "GREIS: PV bad len %zu\n", len);
226
66
        return 0;
227
66
    }
228
229
266
    x = getled64((char *)buf, 0);
230
266
    y = getled64((char *)buf, 8);
231
266
    z = getled64((char *)buf, 16);
232
266
    p_sigma = getlef32((char *)buf, 24);
233
266
    vx = getlef32((char *)buf, 28);
234
266
    vy = getlef32((char *)buf, 32);
235
266
    vz = getlef32((char *)buf, 36);
236
266
    v_sigma = getlef32((char *)buf, 40);
237
266
    solution_type = getub(buf, 44);
238
239
266
    session->newdata.ecef.x = x;
240
266
    session->newdata.ecef.y = y;
241
266
    session->newdata.ecef.z = z;
242
266
    session->newdata.ecef.pAcc = p_sigma;
243
266
    session->newdata.ecef.vx = vx;
244
266
    session->newdata.ecef.vy = vy;
245
266
    session->newdata.ecef.vz = vz;
246
266
    session->newdata.ecef.vAcc = v_sigma;
247
248
    // GREIS Reference Guide 3.4.2 "General Notes" part "Solution Types"
249
266
    if (0 < solution_type &&
250
200
        5 > solution_type) {
251
132
        session->newdata.mode = MODE_3D;
252
132
        if (1 < solution_type) {
253
66
            session->newdata.status = STATUS_DGPS;
254
66
        } else {
255
66
            session->newdata.status = STATUS_GPS;
256
66
        }
257
132
    }
258
259
266
    GPSD_LOG(LOG_DATA, &session->context->errout,
260
266
             "GREIS: PV, ECEF x=%.2f y=%.2f z=%.2f pAcc=%.2f\n",
261
266
             session->newdata.ecef.x,
262
266
             session->newdata.ecef.y,
263
266
             session->newdata.ecef.z,
264
266
             session->newdata.ecef.pAcc);
265
266
266
    GPSD_LOG(LOG_DATA, &session->context->errout,
267
266
             "GREIS: PV, ECEF vx=%.2f vy=%.2f vz=%.2f vAcc=%.2f "
268
266
             "solution_type: %d\n",
269
266
             session->newdata.ecef.vx,
270
266
             session->newdata.ecef.vy,
271
266
             session->newdata.ecef.vz,
272
266
             session->newdata.ecef.vAcc,
273
266
             solution_type);
274
275
266
   mask |= MODE_SET | STATUS_SET | ECEF_SET | VECEF_SET;
276
266
   return mask;
277
332
}
278
279
/**
280
 * Handle the message [SG] Position and Velocity RMS Errors.
281
 */
282
static gps_mask_t greis_msg_SG(struct gps_device_t *session,
283
                               unsigned char *buf, size_t len)
284
199
{
285
199
    float hpos;                 // Horizontal position RMS error [m]
286
199
    float vpos;                 // Vertical position RMS error [m]
287
199
    float hvel;                 // Horizontal velocity RMS error [m/s]
288
199
    float vvel;                 // Vertical velocity RMS error [m/s]
289
290
199
    if (len < 18) {
291
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
292
66
                 "GREIS: SG bad len %zu\n", len);
293
66
        return 0;
294
66
    }
295
296
133
    hpos = getlef32((char *)buf, 0);
297
133
    vpos = getlef32((char *)buf, 4);
298
133
    hvel = getlef32((char *)buf, 8);
299
133
    vvel = getlef32((char *)buf, 12);
300
301
    /*
302
     * All errors are RMS which can be approximated as 1 sigma, so we can just
303
     * use them directly.
304
     *
305
     * Compute missing items in gpsd_error_model(), not here.
306
     */
307
133
    session->newdata.eph = hpos;
308
133
    session->newdata.epv = vpos;
309
133
    session->newdata.eps = hvel;
310
133
    session->newdata.epc = vvel;
311
312
133
    GPSD_LOG(LOG_DATA, &session->context->errout,
313
133
             "GREIS: SG, eph: %.2f, eps: %.2f, epc: %.2f\n",
314
133
             session->newdata.eph,
315
133
             session->newdata.eps, session->newdata.epc);
316
317
133
    return HERR_SET | SPEEDERR_SET | CLIMBERR_SET;
318
199
}
319
320
/**
321
 * Handle the message [DP] Dilution of Precision.
322
 * Note that fill_dop() will handle the unset dops later.
323
 */
324
static gps_mask_t greis_msg_DP(struct gps_device_t *session,
325
                               unsigned char *buf, size_t len)
326
132
{
327
132
    if (len < 18) {
328
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
329
66
                 "GREIS: DP bad len %zu\n", len);
330
66
        return 0;
331
66
    }
332
333
    // clear so that computed DOPs get recomputed.
334
66
    gps_clear_dop(&session->gpsdata.dop);
335
336
66
    session->gpsdata.dop.hdop = getlef32((char *)buf, 0);
337
66
    session->gpsdata.dop.vdop = getlef32((char *)buf, 4);
338
66
    session->gpsdata.dop.tdop = getlef32((char *)buf, 8);
339
340
66
    session->gpsdata.dop.pdop = sqrt(pow(session->gpsdata.dop.hdop, 2) +
341
66
                                     pow(session->gpsdata.dop.vdop, 2));
342
343
66
    GPSD_LOG(LOG_DATA, &session->context->errout,
344
66
             "GREIS: DP, hdop: %.2f, vdop: %.2f, tdop: %.2f, pdop: %.2f\n",
345
66
             session->gpsdata.dop.hdop, session->gpsdata.dop.vdop,
346
66
             session->gpsdata.dop.tdop, session->gpsdata.dop.pdop);
347
348
66
    return DOP_SET;
349
132
}
350
351
/**
352
 * Handle the message [SI] Satellite Indices.
353
 *
354
 * This message tells us how many satellites are seen and contains their
355
 * Universal Satellite Identifier (USI).
356
 */
357
static gps_mask_t greis_msg_SI(struct gps_device_t *session,
358
                               unsigned char *buf, size_t len)
359
660
{
360
660
    unsigned i;
361
362
660
    if (1 > len) {
363
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
364
0
                 "GREIS: SI bad len %zu\n", len);
365
0
        return 0;
366
0
    }
367
368
660
    gpsd_zero_satellites(&session->gpsdata);
369
660
    session->gpsdata.satellites_visible = (int)(len - 1);
370
660
    if (MAXCHANNELS < session->gpsdata.satellites_visible) {
371
72
        GPSD_LOG(LOG_WARN, &session->context->errout,
372
72
                "GREIS: SI too many satellites %d\n",
373
72
                 session->gpsdata.satellites_visible);
374
72
        session->gpsdata.satellites_visible = MAXCHANNELS;
375
72
    }
376
25.6k
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
377
        // This isn't really PRN, this is USI.  Convert it.
378
24.9k
        unsigned short PRN = getub(buf, i);
379
24.9k
        session->gpsdata.skyview[i].PRN = PRN;
380
381
        // fit into gnssid:svid
382
24.9k
        if (0 == PRN) {
383
            // skip 0 PRN
384
3.47k
            continue;
385
21.4k
        } else if (37 >= PRN) {
386
            // GPS, 1 .. 37
387
3.48k
            session->gpsdata.skyview[i].gnssid = 0;
388
3.48k
            session->gpsdata.skyview[i].svid = PRN;
389
17.9k
        } else if (69 >= PRN) {
390
            // GLONASS, 38 .. 69
391
6.76k
            session->gpsdata.skyview[i].gnssid = 6;
392
6.76k
            session->gpsdata.skyview[i].svid = PRN - 37;
393
11.2k
        } else if (70 == PRN) {
394
            // GLONASS, again, 70
395
283
            session->gpsdata.skyview[i].gnssid = 6;
396
283
            session->gpsdata.skyview[i].svid = 255;
397
10.9k
        } else if (119 >= PRN) {
398
            // Galileo, 71 .. 119
399
5.89k
            session->gpsdata.skyview[i].gnssid = 2;
400
5.89k
            session->gpsdata.skyview[i].svid = PRN - 70;
401
5.89k
        } else if (142 >= PRN) {
402
            // SBAS, 120 .. 142
403
393
            session->gpsdata.skyview[i].gnssid = 1;
404
393
            session->gpsdata.skyview[i].svid = PRN - 119;
405
4.65k
        } else if ((193 <= PRN) &&
406
2.72k
                   (197 >= PRN)) {
407
            // QZSS
408
213
            session->gpsdata.skyview[i].gnssid = 5;
409
213
            session->gpsdata.skyview[i].svid = PRN - 192;
410
4.44k
        } else if ((211 <= PRN) &&
411
2.18k
                   (247 >= PRN)) {
412
            // BeiDou
413
863
            session->gpsdata.skyview[i].gnssid = 3;
414
863
            session->gpsdata.skyview[i].svid = PRN - 210;
415
863
        }
416
21.4k
        session->gpsdata.raw.meas[i].obs_code[0] = '\0';
417
21.4k
        session->gpsdata.raw.meas[i].gnssid =
418
21.4k
            session->gpsdata.skyview[i].gnssid;
419
21.4k
        session->gpsdata.raw.meas[i].svid =
420
21.4k
            session->gpsdata.skyview[i].svid;
421
        // GREIS does not report locktime, so assume max
422
21.4k
        session->gpsdata.raw.meas[i].locktime = LOCKMAX;
423
        // Make sure the unused raw fields are set consistently
424
21.4k
        session->gpsdata.raw.meas[i].sigid = 0;
425
21.4k
        session->gpsdata.raw.meas[i].snr = 0;
426
21.4k
        session->gpsdata.raw.meas[i].freqid = 0;
427
21.4k
        session->gpsdata.raw.meas[i].lli = 0;
428
21.4k
        session->gpsdata.raw.meas[i].codephase = NAN;
429
21.4k
        session->gpsdata.raw.meas[i].deltarange = NAN;
430
21.4k
    }
431
432
660
    session->driver.greis.seen_si = true;
433
660
    GPSD_LOG(LOG_DATA, &session->context->errout,
434
660
             "GREIS: SI, satellites_visible: %d\n",
435
660
             session->gpsdata.satellites_visible);
436
437
660
    return 0;
438
660
}
439
440
/**
441
 * Handle the message [EL] Satellite Elevations.
442
 */
443
static gps_mask_t greis_msg_EL(struct gps_device_t *session,
444
                               unsigned char *buf, size_t len)
445
461
{
446
461
    unsigned i;
447
448
461
    if (!session->driver.greis.seen_si) {
449
69
        GPSD_LOG(LOG_WARN, &session->context->errout,
450
69
                 "GREIS: can't use EL until after SI provides indices\n");
451
69
        return 0;
452
69
    }
453
454
    // check against number of satellites + checksum
455
392
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
456
392
        len != session->gpsdata.satellites_visible + 1) {
457
92
        GPSD_LOG(LOG_WARN, &session->context->errout,
458
92
                 "GREIS: EL bad len %zu, visible %d\n", len,
459
92
                 session->gpsdata.satellites_visible);
460
92
        return 0;
461
92
    }
462
463
2.45k
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
464
2.15k
        short elevation;
465
466
        // GREIS elevation is -90 to 90 degrees
467
        // GREIS uses 127 for n/a
468
        // gpsd uses NAN for n/a, so adjust accordingly
469
2.15k
        elevation = getub(buf, i);
470
2.15k
        if (90 < abs(elevation)) {
471
660
            session->gpsdata.skyview[i].elevation = (double)elevation;
472
660
        }  // else leave as NAN
473
2.15k
    }
474
475
300
    session->driver.greis.seen_el = true;
476
300
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: EL\n");
477
478
300
    return 0;
479
392
}
480
481
/**
482
 * Handle the message [AZ] Satellite Azimuths.
483
 */
484
static gps_mask_t greis_msg_AZ(struct gps_device_t *session,
485
                               unsigned char *buf, size_t len)
486
317
{
487
317
    unsigned i;
488
489
317
    if (!session->driver.greis.seen_si) {
490
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
491
66
                 "GREIS: can't use AZ until after SI provides indices\n");
492
66
        return 0;
493
66
    }
494
495
    // check against number of satellites + checksum
496
251
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
497
251
        len != session->gpsdata.satellites_visible + 1) {
498
67
        GPSD_LOG(LOG_WARN, &session->context->errout,
499
67
                 "GREIS: AZ bad len %zu, visible %d\n", len,
500
67
                 session->gpsdata.satellites_visible);
501
67
        return 0;
502
67
    }
503
504
2.98k
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
505
2.80k
        short azimuth;
506
507
        /* GREIS azimuth is 0 to 180, multiply by 2 for 0 to 360
508
         * GREIS uses 255 for n/a
509
         * gpsd azimuth is 0 to 359, so adjust accordingly */
510
2.80k
        azimuth = getub(buf, i) * 2;
511
2.80k
        if (360 == azimuth) {
512
79
            session->gpsdata.skyview[i].azimuth = 0;
513
2.72k
        } else if (0 <= azimuth &&
514
2.72k
                   360 > azimuth) {
515
2.04k
            session->gpsdata.skyview[i].azimuth = (double)azimuth;
516
2.04k
        }  // else leave as NAN
517
2.80k
    }
518
519
184
    session->driver.greis.seen_az = true;
520
184
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: AZ\n");
521
522
184
    return 0;
523
251
}
524
525
/**
526
 * Handle the message [DC] Doppler (CA/L1)
527
 */
528
static gps_mask_t greis_msg_DC(struct gps_device_t *session,
529
                               unsigned char *buf, size_t len)
530
280
{
531
280
    unsigned i;
532
280
    size_t len_needed = (session->gpsdata.satellites_visible * 4) + 1;
533
534
280
    if (!session->driver.greis.seen_si) {
535
67
        GPSD_LOG(LOG_WARN, &session->context->errout,
536
67
                 "GREIS: can't use DC until after SI provides indices\n");
537
67
        return 0;
538
67
    }
539
540
    // check against number of satellites + checksum
541
213
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
542
213
        len != len_needed) {
543
67
        GPSD_LOG(LOG_WARN, &session->context->errout,
544
67
                 "GREIS: DC bad len %zu, visible %d\n", len,
545
67
                 session->gpsdata.satellites_visible);
546
67
        return 0;
547
67
    }
548
549
978
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
550
832
        long int_doppler = getles32((char *)buf, i * 4);
551
832
        if (0x7fffffff == int_doppler) {
552
            // out of range
553
69
            session->gpsdata.raw.meas[i].doppler = NAN;
554
763
        } else {
555
763
            session->gpsdata.raw.meas[i].doppler = int_doppler * 1e-4;
556
763
        }
557
832
    }
558
559
146
    session->driver.greis.seen_raw = true;
560
146
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: DC\n");
561
562
146
    return 0;
563
213
}
564
565
/**
566
 * Handle the message [EC] SNR (CA/L1).
567
 * EC really outputs CNR, but what gpsd refers to as SNR _is_ CNR.
568
 */
569
static gps_mask_t greis_msg_EC(struct gps_device_t *session,
570
                               unsigned char *buf, size_t len)
571
354
{
572
354
    unsigned i;
573
574
354
    if (!session->driver.greis.seen_si) {
575
68
        GPSD_LOG(LOG_WARN, &session->context->errout,
576
68
                 "GREIS: can't use EC until after SI provides indices\n");
577
68
        return 0;
578
68
    }
579
580
    // check against number of satellites + checksum
581
286
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
582
286
        len != session->gpsdata.satellites_visible + 1) {
583
72
        GPSD_LOG(LOG_WARN, &session->context->errout,
584
72
                 "GREIS: EC bad len %zu, visible %d\n", len,
585
72
                 session->gpsdata.satellites_visible);
586
72
        return 0;
587
72
    }
588
589
2.77k
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
590
2.56k
        session->gpsdata.skyview[i].ss = getub(buf, i);
591
2.56k
    }
592
593
214
    session->driver.greis.seen_ec = true;
594
214
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: EC\n");
595
596
214
    return 0;
597
286
}
598
599
600
/**
601
 * Handle the message [P3] CA/L2 Carrier Phases, RINEX L2C
602
 */
603
static gps_mask_t greis_msg_P3(struct gps_device_t *session,
604
                               unsigned char *buf, size_t len)
605
140
{
606
140
    unsigned i;
607
140
    size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
608
609
140
    if (!session->driver.greis.seen_si) {
610
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
611
66
                 "GREIS: can't use P3 until after SI provides indices\n");
612
66
        return 0;
613
66
    }
614
615
    // check against number of satellites + checksum
616
74
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
617
74
        len != len_needed) {
618
74
        GPSD_LOG(LOG_WARN, &session->context->errout,
619
74
                 "GREIS: P3 bad len %zu, visible %d\n", len,
620
74
                 session->gpsdata.satellites_visible);
621
74
        return 0;
622
74
    }
623
624
0
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
625
0
        session->gpsdata.raw.meas[i].l2c = getled64((char *)buf, i * 8);
626
0
    }
627
628
0
    session->driver.greis.seen_raw = true;
629
0
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: P3\n");
630
631
0
    return 0;
632
74
}
633
634
/**
635
 * Handle the message [PC] CA/L1 Carrier Phases, RINEX L1C
636
 */
637
static gps_mask_t greis_msg_PC(struct gps_device_t *session,
638
                               unsigned char *buf, size_t len)
639
184
{
640
184
    unsigned i;
641
184
    size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
642
643
184
    if (!session->driver.greis.seen_si) {
644
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
645
66
                 "GREIS: can't use PC until after SI provides indices\n");
646
66
        return 0;
647
66
    }
648
649
    // check against number of satellites + checksum
650
118
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
651
118
        len != len_needed) {
652
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
653
66
                 "GREIS: PC bad len %zu, visible %d\n", len,
654
66
                 session->gpsdata.satellites_visible);
655
66
        return 0;
656
66
    }
657
658
228
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
659
176
        session->gpsdata.raw.meas[i].carrierphase = getled64((char *)buf,
660
176
                                                            i * 8);
661
176
    }
662
663
52
    session->driver.greis.seen_raw = true;
664
52
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: PC\n");
665
666
52
    return 0;
667
118
}
668
669
/**
670
 * Handle the message [R3] CA/L2 Pseudo-range, RINEX C2C
671
 */
672
static gps_mask_t greis_msg_R3(struct gps_device_t *session,
673
                               unsigned char *buf, size_t len)
674
138
{
675
138
    unsigned i;
676
138
    size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
677
678
138
    if (!session->driver.greis.seen_si) {
679
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
680
66
                 "GREIS: can't use R3 until after SI provides indices\n");
681
66
        return 0;
682
66
    }
683
684
    // check against number of satellites + checksum
685
72
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
686
72
        len != len_needed) {
687
72
        GPSD_LOG(LOG_WARN, &session->context->errout,
688
72
                 "GREIS: R3 bad len %zu, visible %d\n", len,
689
72
                 session->gpsdata.satellites_visible);
690
72
        return 0;
691
72
    }
692
693
0
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
694
        // get, and convert to meters
695
0
        session->gpsdata.raw.meas[i].c2c = \
696
0
            getled64((char *)buf, i * 8) * CLIGHT;
697
0
    }
698
699
0
    session->driver.greis.seen_raw = true;
700
0
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: R3\n");
701
702
0
    return 0;
703
72
}
704
705
/**
706
 * Handle the message [RC] Pseudo-range CA/L1, RINEX C1C
707
 */
708
static gps_mask_t greis_msg_RC(struct gps_device_t *session,
709
                               unsigned char *buf, size_t len)
710
244
{
711
244
    unsigned i;
712
244
    size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
713
714
244
    if (!session->driver.greis.seen_si) {
715
67
        GPSD_LOG(LOG_WARN, &session->context->errout,
716
67
                 "GREIS: can't use RC until after SI provides indices\n");
717
67
        return 0;
718
67
    }
719
720
    // check against number of satellites + checksum
721
177
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
722
177
        len != len_needed) {
723
93
        GPSD_LOG(LOG_WARN, &session->context->errout,
724
93
                 "GREIS: RC bad len %zu, visible %d\n", len,
725
93
                 session->gpsdata.satellites_visible);
726
93
        return 0;
727
93
    }
728
729
417
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
730
        // get, and convert to meters
731
333
        session->gpsdata.raw.meas[i].pseudorange = \
732
333
            getled64((char *)buf, i * 8) * CLIGHT;
733
333
    }
734
735
84
    session->driver.greis.seen_raw = true;
736
84
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: RC\n");
737
738
84
    return 0;
739
177
}
740
741
/**
742
 * Handle the message [SS] Satellite Navigation Status.
743
 */
744
static gps_mask_t greis_msg_SS(struct gps_device_t *session,
745
                               unsigned char *buf, size_t len)
746
325
{
747
325
    unsigned i;
748
325
    int used_count = 0;
749
750
325
    if (!session->driver.greis.seen_si) {
751
69
        GPSD_LOG(LOG_WARN, &session->context->errout,
752
69
                 "GREIS: can't use SS until after SI provides indices\n");
753
69
        return 0;
754
69
    }
755
756
    // check against number of satellites + solution type + checksum
757
256
    if (MAXCHANNELS < session->gpsdata.satellites_visible ||
758
256
        len != session->gpsdata.satellites_visible + 2) {
759
66
        GPSD_LOG(LOG_WARN, &session->context->errout,
760
66
                 "GREIS: SS bad len %zu, visible %d\n", len,
761
66
                 session->gpsdata.satellites_visible);
762
66
        return 0;
763
66
    }
764
765
3.03k
    for (i = 0; i < session->gpsdata.satellites_visible; i++) {
766
        /*
767
         * From the GREIS Reference Guide: "Codes [0...3], [40...62], and
768
         * [64...255] indicate that given satellite is used in position
769
         * computation and show which measurements are used. The rest of codes
770
         * indicate that satellite is not used in position computation and
771
         * indicate why this satellite is excluded from position computation."
772
         * Refer to Table 3-4 "Satellite Navigation Status" for the specific
773
         * code meanings.
774
         */
775
2.84k
        uint8_t nav_status = getub(buf, i);
776
2.84k
        session->gpsdata.skyview[i].used =
777
2.84k
            (nav_status <= 3) ||
778
2.62k
            (nav_status >= 40 && nav_status <= 62) ||
779
1.99k
            (nav_status >= 64);
780
781
2.84k
        if (session->gpsdata.skyview[i].used) {
782
2.14k
            used_count++;
783
2.14k
        }
784
2.84k
    }
785
190
    session->gpsdata.satellites_used = used_count;
786
787
190
    GPSD_LOG(LOG_DATA, &session->context->errout,
788
190
             "GREIS: SS, satellites_used: %d\n",
789
190
             session->gpsdata.satellites_used);
790
791
190
    return used_count ? USED_IS : 0;
792
256
}
793
794
795
/**
796
 * Handle the message [::](ET) Epoch Time.
797
 * This should be kept as the last message in each epoch.
798
 */
799
static gps_mask_t greis_msg_ET(struct gps_device_t *session,
800
                               unsigned char *buf, size_t len)
801
760
{
802
760
    uint32_t tod;
803
760
    gps_mask_t mask = 0;
804
805
760
    if (len < 5) {
806
67
        GPSD_LOG(LOG_WARN, &session->context->errout,
807
67
                 "GREIS: ET bad len %zu\n", len);
808
67
        return 0;
809
67
    }
810
811
693
    if (!session->driver.greis.seen_rt) {
812
83
        GPSD_LOG(LOG_WARN, &session->context->errout,
813
83
                 "GREIS: got ET, but no preceding RT for epoch\n");
814
83
        return 0;
815
83
    }
816
817
610
    tod = getleu32(buf, 0);
818
610
    if (tod != session->driver.greis.rt_tod) {
819
97
        GPSD_LOG(LOG_WARN, &session->context->errout,
820
97
                 "GREIS: broken epoch, RT had %lu, but ET has %lu\n",
821
97
                 (unsigned long)session->driver.greis.rt_tod,
822
97
                 (unsigned long)tod);
823
97
        return 0;
824
97
    }
825
826
    // Skyview time does not differ from time in GT message
827
513
    session->gpsdata.skyview_time.tv_sec = 0;
828
513
    session->gpsdata.skyview_time.tv_nsec = 0;
829
830
513
    GPSD_LOG(LOG_DATA, &session->context->errout,
831
513
             "GREIS: ET, seen: az %d, ec %d, el %d, rt %d, si %d, uo %d\n",
832
513
             (int)session->driver.greis.seen_az,
833
513
             (int)session->driver.greis.seen_ec,
834
513
             (int)session->driver.greis.seen_el,
835
513
             (int)session->driver.greis.seen_rt,
836
513
             (int)session->driver.greis.seen_si,
837
513
             (int)session->driver.greis.seen_uo);
838
839
    // Make sure we got the satellite data, then report it.
840
513
    if ((session->driver.greis.seen_az && session->driver.greis.seen_ec &&
841
199
         session->driver.greis.seen_el && session->driver.greis.seen_si)) {
842
        // Skyview seen, update it.  Go even if no seen_ss or none visible
843
133
        mask |= SATELLITE_SET;
844
845
133
        if (session->driver.greis.seen_raw) {
846
67
            mask |= RAW_IS;
847
67
        } else {
848
66
            session->gpsdata.raw.mtime.tv_sec = 0;
849
66
            session->gpsdata.raw.mtime.tv_nsec = 0;
850
66
        }
851
852
380
    } else {
853
380
        session->gpsdata.raw.mtime.tv_sec = 0;
854
380
        session->gpsdata.raw.mtime.tv_nsec = 0;
855
380
        GPSD_LOG(LOG_WARN, &session->context->errout,
856
380
                 "GREIS: ET: missing satellite details in this epoch\n");
857
380
    }
858
859
513
    GPSD_LOG(LOG_DATA, &session->context->errout, "GREIS: ET, tod: %lu\n",
860
513
             (unsigned long)tod);
861
862
    /* This is a good place to poll firmware version if we need it.
863
     * Waited until now to avoid the startup rush and out of
864
     * critical time path
865
     */
866
513
    if (0 == strnlen(session->subtype, sizeof(session->subtype))) {
867
        // get version
868
254
        (void)greis_write(session, get_ver, sizeof(get_ver) - 1);
869
254
    }
870
    /* The driver waits for ET to send any reports
871
     * Just REPORT_IS is not enough to trigger sending of reports to clients.
872
     * STATUS_SET seems best, if no status by now the status is no fix */
873
513
    return mask | REPORT_IS | STATUS_SET;
874
610
}
875
876
struct dispatch_table_entry {
877
    char id0;
878
    char id1;
879
    gps_mask_t (*handler)(struct gps_device_t *, unsigned char *, size_t);
880
};
881
882
static struct dispatch_table_entry dispatch_table[] = {
883
    {':', ':', greis_msg_ET},
884
    {'A', 'Z', greis_msg_AZ},
885
    {'D', 'C', greis_msg_DC},
886
    {'D', 'P', greis_msg_DP},
887
    {'E', 'C', greis_msg_EC},
888
    {'E', 'R', greis_msg_ER},
889
    {'E', 'L', greis_msg_EL},
890
    {'G', 'T', greis_msg_GT},
891
    {'R', '3', greis_msg_R3},
892
    {'R', 'C', greis_msg_RC},
893
    {'P', '3', greis_msg_P3},
894
    {'P', 'C', greis_msg_PC},
895
    {'P', 'V', greis_msg_PV},
896
    {'R', 'E', greis_msg_RE},
897
    {'S', 'G', greis_msg_SG},
898
    {'S', 'I', greis_msg_SI},
899
    {'S', 'S', greis_msg_SS},
900
    {'U', 'O', greis_msg_UO},
901
    {'~', '~', greis_msg_RT},
902
};
903
904
123k
#define dispatch_table_size (sizeof(dispatch_table) / sizeof(dispatch_table[0]))
905
906
/**
907
 * Parse the data from the device
908
 */
909
static gps_mask_t greis_dispatch(struct gps_device_t *session,
910
                                 unsigned char *buf, size_t len)
911
10.5k
{
912
10.5k
    size_t i;
913
10.5k
    char id0, id1;
914
915
10.5k
    if (len == 0)
916
0
        return 0;
917
918
    /*
919
     * This is set because the device reliably signals end of cycle.
920
     * The core library zeroes it just before it calls each driver's
921
     * packet analyzer.
922
     */
923
10.5k
    session->cycle_end_reliable = true;
924
925
    // Length should have already been checked in packet.c, but just in case
926
10.5k
    if (len < HEADER_LENGTH) {
927
0
        GPSD_LOG(LOG_WARN, &session->context->errout,
928
0
                 "GREIS: Packet length %zu shorter than min length\n", len);
929
0
        return 0;
930
0
    }
931
932
    // we may need to dump the raw packet
933
10.5k
    GPSD_LOG(LOG_RAW, &session->context->errout,
934
10.5k
             "GREIS: raw packet id '%c%c'\n", buf[0], buf[1]);
935
936
10.5k
    id0 = buf[0];
937
10.5k
    id1 = buf[1];
938
10.5k
    len -= HEADER_LENGTH;
939
10.5k
    buf += HEADER_LENGTH;
940
941
123k
    for (i = 0; i < dispatch_table_size; i++) {
942
123k
        struct dispatch_table_entry *entry = &dispatch_table[i];
943
944
123k
        if (id0 == entry->id0 && id1 == entry->id1) {
945
10.1k
            return entry->handler(session, buf, len);
946
10.1k
        }
947
123k
    }
948
949
330
    GPSD_LOG(LOG_WARN, &session->context->errout,
950
330
             "GREIS: unknown packet id '%c%c' length %zu\n", id0, id1, len);
951
330
    return 0;
952
10.5k
}
953
954
/**********************************************************
955
 *
956
 * Externally called routines below here
957
 *
958
 **********************************************************/
959
960
/**
961
 * Write data to the device with checksum.
962
 * Returns number of bytes written on successful write, -1 otherwise.
963
 */
964
static ssize_t greis_write(struct gps_device_t *session,
965
                           const char *msg, size_t msglen)
966
521
{
967
521
    char checksum_str[3] = {0};
968
521
    ssize_t count;
969
970
521
    if (session->context->readonly) {
971
        // readonly mode, do not write anything
972
130
        return -1;
973
130
    }
974
975
391
    if (NULL == msg) {
976
        /* We do sometimes write zero length to wake up GPS,
977
         * so just test for NULL msg, not zero length message */
978
0
        GPSD_LOG(LOG_ERROR, &session->context->errout,
979
0
                 "GREIS: nothing to write\n");
980
0
        return -1;
981
0
    }
982
983
    // Account for length + checksum marker + checksum + \r + \n + \0
984
391
    if (msglen + 6 > sizeof(session->msgbuf)) {
985
0
        GPSD_LOG(LOG_ERROR, &session->context->errout,
986
0
                 "GREIS: msgbuf is smaller than write length %zu\n", msglen);
987
0
        return -1;
988
0
    }
989
990
391
    memcpy(&session->msgbuf[0], msg, msglen);
991
992
391
    if (msglen == 0) {
993
        // This is a dummy write, don't give a checksum.
994
0
        session->msgbuf[0] = '\n';
995
0
        session->msgbuflen = 1;
996
0
        GPSD_LOG(LOG_PROG, &session->context->errout,
997
0
                 "GREIS: Dummy write\n");
998
391
    } else {
999
391
        unsigned char checksum;
1000
1001
391
        session->msgbuflen = msglen;
1002
391
        session->msgbuf[session->msgbuflen++] = '@';  // checksum marker
1003
1004
        // calculate checksum with @, place at end, and set length to write
1005
391
        checksum = greis_checksum((unsigned char *)session->msgbuf,
1006
391
                                  session->msgbuflen);
1007
391
        (void)snprintf(checksum_str, sizeof(checksum_str), "%02X", checksum);
1008
391
        session->msgbuf[session->msgbuflen++] = checksum_str[0];
1009
391
        session->msgbuf[session->msgbuflen++] = checksum_str[1];
1010
391
        session->msgbuf[session->msgbuflen++] = '\r';
1011
391
        session->msgbuf[session->msgbuflen++] = '\n';
1012
1013
391
        GPSD_LOG(LOG_PROG, &session->context->errout,
1014
391
                 "GREIS: Writing command '%.*s', checksum: %s\n",
1015
391
                 (int)msglen, msg, checksum_str);
1016
391
    }
1017
391
    session->msgbuf[session->msgbuflen] = '\0';
1018
391
    count = gpsd_write(session, session->msgbuf, session->msgbuflen);
1019
1020
391
    if (count != (ssize_t)session->msgbuflen) {
1021
391
        return -1;
1022
391
    }  // else
1023
1024
0
    return count;
1025
391
}
1026
1027
/**
1028
 * Write data to the device, doing any required padding or checksumming
1029
 */
1030
static ssize_t greis_control_send(struct gps_device_t *session,
1031
                                  char *msg, size_t msglen)
1032
0
{
1033
0
    return greis_write(session, msg, msglen);
1034
0
}
1035
1036
static void greis_event_hook(struct gps_device_t *session, event_t event)
1037
14.1k
{
1038
14.1k
    if (session->context->readonly ||
1039
10.2k
        session->context->passive) {
1040
10.2k
        return;
1041
10.2k
    }
1042
1043
3.92k
    if (event == EVENT_WAKEUP) {
1044
        /*
1045
         * Code to make the device ready to communicate.  Only needed if the
1046
         * device is in some kind of sleeping state, and only shipped to
1047
         * RS232C, so that gpsd won't send strings to unidentified USB devices
1048
         * that might not be GPSes at all.
1049
         */
1050
1051
        /*
1052
         * Disable any existing messages, then request vendor for
1053
         * identification.
1054
         */
1055
0
        (void)greis_write(session, disable_messages,
1056
0
                          sizeof(disable_messages) - 1);
1057
0
        (void)greis_write(session, get_vendor, sizeof(get_vendor) - 1);
1058
3.92k
    } else if (event == EVENT_IDENTIFIED ||
1059
3.83k
               event == EVENT_REACTIVATE) {
1060
        /*
1061
         * Fires when the first full packet is recognized from a previously
1062
         * unidentified device OR the device is reactivated after close. The
1063
         * session.lexer counter is zeroed.
1064
         *
1065
         * TODO: If possible, get the software version and store it in
1066
         * session->subtype.
1067
         */
1068
89
        (void)greis_write(session, disable_messages,
1069
89
                          sizeof(disable_messages) - 1);
1070
89
        (void)greis_write(session, set_update_rate_4hz,
1071
89
                          sizeof(set_update_rate_4hz) - 1);
1072
89
        (void)greis_write(session, enable_messages_4hz,
1073
89
                          sizeof(enable_messages_4hz) - 1);
1074
1075
        // Store (expected) cycle time (seconds)
1076
89
        session->gpsdata.dev.cycle.tv_sec = 0;
1077
89
        session->gpsdata.dev.cycle.tv_nsec = 250000000L;
1078
3.83k
    } else if (event == EVENT_DRIVER_SWITCH) {
1079
        /*
1080
         * Fires when the driver on a device is changed *after* it
1081
         * has been identified.
1082
         */
1083
3.30k
    } else if (event == EVENT_DEACTIVATE) {
1084
        /*
1085
         * Fires when the device is deactivated.  Use this to revert
1086
         * whatever was done at EVENT_IDENTIFIED and EVENT_CONFIGURE
1087
         * time.
1088
         */
1089
0
        (void)greis_write(session, disable_messages,
1090
0
                          sizeof(disable_messages) - 1);
1091
0
    }
1092
3.92k
}
1093
1094
/**
1095
 * This is the entry point to the driver. When the packet sniffer recognizes
1096
 * a packet for this driver, it calls this method which passes the packet to
1097
 * the binary processor or the nmea processor, depending on the session type.
1098
 */
1099
static gps_mask_t greis_parse_input(struct gps_device_t *session)
1100
10.5k
{
1101
10.5k
    if (GREIS_PACKET == session->lexer.type) {
1102
10.5k
        return greis_dispatch(session, session->lexer.outbuffer,
1103
10.5k
                              session->lexer.outbuflen);
1104
10.5k
    }
1105
0
    if (NMEA_PACKET == session->lexer.type) {
1106
0
        return nmea_parse((char *)session->lexer.outbuffer, session);
1107
0
    }
1108
0
    return 0;
1109
0
}
1110
1111
/**
1112
 * Set port operating mode, speed, parity, stopbits etc. here.
1113
 * Note: parity is passed as 'N'/'E'/'O', but you should program
1114
 * defensively and allow 0/1/2 as well.
1115
 */
1116
static bool greis_set_speed(struct gps_device_t *session,
1117
                            speed_t speed, char parity, int stopbits)
1118
0
{
1119
    // change on current port
1120
0
    static const char set_rate[] = "set,/par/cur/term/rate,";
1121
0
    static const char set_parity[] = "set,/par/cur/term/parity,";
1122
0
    static const char set_stops[] = "set,/par/cur/term/stops,";
1123
0
    static const char parity_none[] = "N";
1124
0
    static const char parity_even[] = "even";
1125
0
    static const char parity_odd[] = "odd";
1126
1127
0
    char command[BUFSIZ] = {0};
1128
0
    const char *selected_parity = NULL;
1129
1130
0
    switch (parity) {
1131
0
    case 'N':
1132
0
    case 0:
1133
0
        selected_parity = parity_none;
1134
0
        break;
1135
0
    case 'E':
1136
0
    case 1:
1137
0
        selected_parity = parity_even;
1138
0
        break;
1139
0
    case 'O':
1140
0
    case 2:
1141
0
        selected_parity = parity_odd;
1142
0
        break;
1143
0
    default:
1144
0
        return false;
1145
0
    }
1146
1147
0
    (void)snprintf(command, sizeof(command) - 1, "%s%lu && %s%s && %s%d",
1148
0
             set_rate, (unsigned long)speed, set_parity, selected_parity,
1149
0
             set_stops, stopbits);
1150
0
    return (bool)greis_write(session, command,
1151
0
                             strnlen(command, sizeof(command)));
1152
0
}
1153
1154
#if 0
1155
/**
1156
 * TODO: Switch between NMEA and binary mode
1157
 */
1158
static void greis_set_mode(struct gps_device_t *session, int mode)
1159
{
1160
    if (mode == MODE_NMEA) {
1161
        // send a mode switch control string
1162
    } else {
1163
        // send a mode switch control string
1164
    }
1165
}
1166
#endif
1167
1168
#if 0  // TODO
1169
static double greis_time_offset(struct gps_device_t *session)
1170
{
1171
    /*
1172
     * If NTP notification is enabled, the GPS will occasionally NTP
1173
     * its notion of the time. This will lag behind actual time by
1174
     * some amount which has to be determined by observation vs. (say
1175
     * WWVB radio broadcasts) and, furthermore, may differ by baud
1176
     * rate. This method is for computing the NTP fudge factor.  If
1177
     * it's absent, an offset of 0.0 will be assumed, effectively
1178
     * falling back on what's in ntp.conf. When it returns NAN,
1179
     * nothing will be sent to NTP.
1180
     */
1181
    return MAGIC_CONSTANT;
1182
}
1183
#endif
1184
1185
// This is everything we export
1186
// *INDENT-OFF*
1187
const struct gps_type_t driver_greis = {
1188
    // Full name of type
1189
    .type_name        = "GREIS",
1190
    // Associated lexer packet type
1191
    .packet_type      = GREIS_PACKET,
1192
    // Driver type flags
1193
    .flags            = DRIVER_STICKY,
1194
    // Response string that identifies device (not active)
1195
    .trigger          = NULL,
1196
    // Number of satellite channels supported by the device
1197
    .channels         = 128,
1198
    // Startup-time device detector
1199
    .probe_detect     = NULL,
1200
    // Packet getter (using default routine)
1201
    .get_packet       = packet_get1,
1202
    // Parse message packets
1203
    .parse_packet     = greis_parse_input,
1204
    // non-perturbing initial query (e.g. for version)
1205
    .init_query        = NULL,
1206
    // fire on various lifetime events
1207
    .event_hook       = greis_event_hook,
1208
    // Speed (baudrate) switch
1209
    .speed_switcher   = greis_set_speed,
1210
#if 0  // TODO
1211
    // Switch to NMEA mode
1212
    .mode_switcher    = greis_set_mode,
1213
#endif
1214
    // Message delivery rate switcher (not active)
1215
    .rate_switcher    = NULL,
1216
    /* Minimum cycle time of the device.
1217
     * Default is 1/100, but this is tunable using /par/raw/msint . */
1218
    .min_cycle.tv_sec  = 0,
1219
    .min_cycle.tv_nsec = 10000000,
1220
    // Control string sender - should provide checksum and headers/trailer
1221
    .control_send   = greis_control_send,
1222
    .time_offset     = NULL,
1223
// *INDENT-ON*
1224
};
1225
#endif  // defined(GREIS_ENABLE)
1226
// vim: set expandtab shiftwidth=4