Coverage Report

Created: 2026-02-26 06:35

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