This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
driver_greis.c
1215 lines (1052 loc) · 36.6 KB
/
driver_greis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* A Javad GNSS Receiver External Interface Specification (GREIS) driver.
*
* Author(s):
* - Gregory Fong <gregory.fong@virginorbit.com>
*
* Documentation for GREIS can be found at:
* http://www.javad.com/downloads/javadgnss/manuals/GREIS/GREIS_Reference_Guide.pdf
*
* The version used for reference is that which
* "Reflects Firmware Version 3.6.7, Last revised: August 25, 2016".
*
* This assumes little endian byte order in messages, which is the default, but
* that is configurable. A future improvement could change to read the
* information in [MF] Message Format.
*
* This file is Copyright (c) 2017-2018 Virgin Orbit
* SPDX-License-Identifier: BSD-2-clause
*/
#include "gpsd_config.h" /* must be before all includes */
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include "bits.h"
#include "driver_greis.h"
#include "gpsd.h"
#if defined(GREIS_ENABLE) && defined(BINARY_ENABLE)
#define HEADER_LENGTH 5
static ssize_t greis_write(struct gps_device_t *session,
const char *msg, size_t msglen);
static const char disable_messages[] = "\%dm\%dm";
static const char get_vendor[] = "\%vendor\%print,/par/rcv/vendor";
static const char get_ver[] = "\%ver\%print,rcv/ver";
static const char set_update_rate_4hz[] = "\%msint\%set,/par/raw/msint,250";
/* Where applicable, the order here is how these will be received per cycle. */
/* TODO: stop hardcoding the cycle time, make it selectable */
static const char enable_messages_4hz[] =
"\%em\%em,,jps/{RT,UO,GT,PV,SG,DP,SI,EL,AZ,EC,SS,ET}:0.25";
/*
* GREIS message handlers. The checksum has been already confirmed valid in the
* packet acceptance logic, so we don't need to retest it here.
*/
/**
* Handle the message [RE] Reply
*/
static gps_mask_t greis_msg_RE(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
if (0 == memcmp(buf, "%ver%", 5)) {
strlcpy(session->subtype, (const char*)&buf[5],
sizeof(session->subtype));
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: RE, ->subtype: %s\n", session->subtype);
return DEVICEID_SET;
}
gpsd_log(&session->context->errout, LOG_INFO,
"GREIS: RE %3zd, reply: %.*s\n", len, (int)len, buf);
return 0;
}
/**
* Handle the message [ER] Reply
*/
static gps_mask_t greis_msg_ER(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: ER %3zd, reply: %.*s\n", len, (int)len, buf);
return 0;
}
/**
* Handle the message [~~](RT) Receiver Time.
*/
static gps_mask_t greis_msg_RT(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
if (len < 5) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: RT bad len %zu\n", len);
return 0;
}
session->driver.greis.rt_tod = getleu32(buf, 0);
memset(&session->gpsdata.raw, 0, sizeof(session->gpsdata.raw));
session->driver.greis.seen_rt = true;
session->driver.greis.seen_az = false;
session->driver.greis.seen_ec = false;
session->driver.greis.seen_el = false;
session->driver.greis.seen_si = false;
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: RT, tod: %lu\n",
(unsigned long)session->driver.greis.rt_tod);
return CLEAR_IS;
}
/**
* Handle the message [UO] GPS UTC Time Parameters.
*/
static gps_mask_t greis_msg_UO(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
/*
* For additional details on these parameters and the computation done using
* them, refer to the Javad GREIS spec mentioned at the top of this file and
* also to ICD-GPS-200C, Revision IRN-200C-004 April 12, 2000. At the time
* of writing, that could be found at
* https://www.navcen.uscg.gov/pubs/gps/icd200/ICD200Cw1234.pdf .
*/
uint32_t tot; /* Reference time of week [s] */
uint16_t wnt; /* Reference week number [dimensionless] */
int8_t dtls; /* Delta time due to leap seconds [s] */
uint8_t dn; /* 'Future' reference day number [1..7] */
uint16_t wnlsf; /* 'Future' reference week number [dimensionless] */
int8_t dtlsf; /* 'Future' delta time due to leap seconds [s] */
if (len < 24) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: UO bad len %zu\n", len);
return 0;
}
tot = getleu32(buf, 12);
wnt = getleu16(buf, 16);
dtls = getsb(buf, 18);
dn = getub(buf, 19);
wnlsf = getleu16(buf, 20);
dtlsf = getsb(buf, 22);
session->driver.greis.seen_uo = true;
/*
* See ICD-GPS-200C 20.3.3.5.2.4 "Universal Coordinated Time (UTC)".
* I totally ripped this off of driver_navcom.c. Might want to dedupe at
* some point.
*/
if ((wnt % 256U) * 604800U + tot < wnlsf * 604800U + dn * 86400U) {
/* Current time is before effectivity time of the leap second event */
session->context->leap_seconds = dtls;
} else {
session->context->leap_seconds = dtlsf;
}
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: UO, leap_seconds: %d\n", session->context->leap_seconds);
return 0;
}
/**
* Handle the message [GT] GPS Time.
*/
static gps_mask_t greis_msg_GT(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
double t_intp;
uint32_t tow; /* Time of week [ms] */
uint16_t wn; /* GPS week number (modulo 1024) [dimensionless] */
if (len < 7) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: GT bad len %zu\n", len);
return 0;
}
if (!session->driver.greis.seen_uo) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use GT until after UO has supplied leap second data\n");
return 0;
}
tow = getleu32(buf, 0);
wn = getleu16(buf, 4);
session->newdata.time = gpsd_gpstime_resolve(session, wn, tow / 1000.0);
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: GT, tow: %u, wn: %u, time: %.2f\n", tow, wn,
session->newdata.time);
/* save raw.mtime, just in case */
session->gpsdata.raw.mtime.tv_nsec =
modf(session->newdata.time, &t_intp) * 1e9;
session->gpsdata.raw.mtime.tv_sec = (time_t)t_intp + \
session->context->leap_seconds;
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: GT, RAW @ %ld.%09ld\n",
(long)session->gpsdata.raw.mtime.tv_sec,
session->gpsdata.raw.mtime.tv_nsec);
return TIME_SET | NTPTIME_IS | ONLINE_SET;
}
/**
* Handle the message [PV] Cartesian Position and Velocity.
*/
static gps_mask_t greis_msg_PV(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
double x, y, z; /* Cartesian coordinates [m] */
float p_sigma; /* Position spherical error probability (SEP) [m] */
float vx, vy, vz; /* Cartesian velocities [m/s] */
float v_sigma; /* Velocity SEP [m/s] */
uint8_t solution_type;
if (len < 46) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: PV bad len %zu\n", len);
return 0;
}
x = getled64((char *)buf, 0);
y = getled64((char *)buf, 8);
z = getled64((char *)buf, 16);
p_sigma = getlef32((char *)buf, 24);
vx = getlef32((char *)buf, 28);
vy = getlef32((char *)buf, 32);
vz = getlef32((char *)buf, 36);
v_sigma = getlef32((char *)buf, 40);
solution_type = getub(buf, 44);
session->newdata.ecef.x = x;
session->newdata.ecef.y = y;
session->newdata.ecef.z = z;
session->newdata.ecef.pAcc = p_sigma;
session->newdata.ecef.vx = vx;
session->newdata.ecef.vy = vy;
session->newdata.ecef.vz = vz;
session->newdata.ecef.vAcc = v_sigma;
ecef_to_wgs84fix(&session->newdata, &session->gpsdata.separation,
x, y, z, vx, vy, vz);
/* GREIS Reference Guide 3.4.2 "General Notes" part "Solution Types" */
if (solution_type > 0 && solution_type < 5) {
session->newdata.mode = MODE_3D;
if (solution_type > 1)
session->gpsdata.status = STATUS_DGPS_FIX;
else
session->gpsdata.status = STATUS_FIX;
}
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: PV, ECEF x=%.2f y=%.2f z=%.2f pAcc=%.2f\n",
session->newdata.ecef.x,
session->newdata.ecef.y,
session->newdata.ecef.z,
session->newdata.ecef.pAcc);
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: PV, ECEF vx=%.2f vy=%.2f vz=%.2f vAcc=%.2f\n",
session->newdata.ecef.vx,
session->newdata.ecef.vy,
session->newdata.ecef.vz,
session->newdata.ecef.vAcc);
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: PV, lat: %.2f, lon: %.2f, alt: %.2f, solution_type: %d\n",
session->newdata.latitude,
session->newdata.longitude, session->newdata.altitude,
solution_type);
return LATLON_SET | ALTITUDE_SET | CLIMB_SET | TRACK_SET | SPEED_SET |
MODE_SET | STATUS_SET | ECEF_SET | VECEF_SET;
}
/**
* Handle the message [SG] Position and Velocity RMS Errors.
*/
static gps_mask_t greis_msg_SG(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
float hpos; /* Horizontal position RMS error [m] */
float vpos; /* Vertical position RMS error [m] */
float hvel; /* Horizontal velocity RMS error [m/s] */
float vvel; /* Vertical velocity RMS error [m/s] */
if (len < 18) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: SG bad len %zu\n", len);
return 0;
}
hpos = getlef32((char *)buf, 0);
vpos = getlef32((char *)buf, 4);
hvel = getlef32((char *)buf, 8);
vvel = getlef32((char *)buf, 12);
/*
* All errors are RMS which can be approximated as 1 sigma, so we can just
* use them directly.
*
* Compute missing items in gpsd_error_model(), not here.
*/
session->newdata.eph = hpos;
session->newdata.epv = vpos;
session->newdata.eps = hvel;
session->newdata.epc = vvel;
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: SG, eph: %.2f, eps: %.2f, epc: %.2f\n",
session->newdata.eph,
session->newdata.eps, session->newdata.epc);
return HERR_SET | SPEEDERR_SET | CLIMBERR_SET;
}
/**
* Handle the message [DP] Dilution of Precision.
* Note that fill_dop() will handle the unset dops later.
*/
static gps_mask_t greis_msg_DP(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
if (len < 18) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: DP bad len %zu\n", len);
return 0;
}
/* clear so that computed DOPs get recomputed. */
gps_clear_dop(&session->gpsdata.dop);
session->gpsdata.dop.hdop = getlef32((char *)buf, 0);
session->gpsdata.dop.vdop = getlef32((char *)buf, 4);
session->gpsdata.dop.tdop = getlef32((char *)buf, 8);
session->gpsdata.dop.pdop = sqrt(pow(session->gpsdata.dop.hdop, 2) +
pow(session->gpsdata.dop.vdop, 2));
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: DP, hdop: %.2f, vdop: %.2f, tdop: %.2f, pdop: %.2f\n",
session->gpsdata.dop.hdop, session->gpsdata.dop.vdop,
session->gpsdata.dop.tdop, session->gpsdata.dop.pdop);
return DOP_SET;
}
/**
* Handle the message [SI] Satellite Indices.
*
* This message tells us how many satellites are seen and contains their
* Universal Satellite Identifier (USI).
*/
static gps_mask_t greis_msg_SI(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
if (len < 1) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: SI bad len %zu\n", len);
return 0;
}
gpsd_zero_satellites(&session->gpsdata);
/* FIXME: check against MAXCHANNELS? */
session->gpsdata.satellites_visible = len - 1;
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
/* This isn't really PRN, this is USI. Convert it. */
unsigned short PRN = getub(buf, i);
session->gpsdata.skyview[i].PRN = PRN;
/* fit into gnssid:svid */
if (0 == PRN) {
/* skip 0 PRN */
continue;
} else if ((1 <= PRN) && (37 >= PRN)) {
/* GPS */
session->gpsdata.skyview[i].gnssid = 0;
session->gpsdata.skyview[i].svid = PRN;
} else if ((38 <= PRN) && (69 >= PRN)) {
/* GLONASS */
session->gpsdata.skyview[i].gnssid = 6;
session->gpsdata.skyview[i].svid = PRN - 37;
} else if (70 == PRN) {
/* GLONASS, again */
session->gpsdata.skyview[i].gnssid = 6;
session->gpsdata.skyview[i].svid = 255;
} else if ((71 <= PRN) && (119 >= PRN)) {
/* Galileo */
session->gpsdata.skyview[i].gnssid = 2;
session->gpsdata.skyview[i].svid = PRN - 70;
} else if ((120 <= PRN) && (142 >= PRN)) {
/* SBAS */
session->gpsdata.skyview[i].gnssid = 1;
session->gpsdata.skyview[i].svid = PRN - 119;
} else if ((193 <= PRN) && (197 >= PRN)) {
/* QZSS */
session->gpsdata.skyview[i].gnssid = 5;
session->gpsdata.skyview[i].svid = PRN - 192;
} else if ((211 <= PRN) && (247 >= PRN)) {
/* BeiDou */
session->gpsdata.skyview[i].gnssid = 3;
session->gpsdata.skyview[i].svid = PRN - 210;
}
session->gpsdata.raw.meas[i].obs_code[0] = '\0';
session->gpsdata.raw.meas[i].gnssid =
session->gpsdata.skyview[i].gnssid;
session->gpsdata.raw.meas[i].svid =
session->gpsdata.skyview[i].svid;
/* GREIS does not report locktime, so assume max */
session->gpsdata.raw.meas[i].locktime = LOCKMAX;
/* Make sure the unused raw fields are set consistently */
session->gpsdata.raw.meas[i].sigid = 0;
session->gpsdata.raw.meas[i].snr = 0;
session->gpsdata.raw.meas[i].freqid = 0;
session->gpsdata.raw.meas[i].lli = 0;
session->gpsdata.raw.meas[i].codephase = NAN;
session->gpsdata.raw.meas[i].deltarange = NAN;
}
session->driver.greis.seen_si = true;
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: SI, satellites_visible: %d\n",
session->gpsdata.satellites_visible);
return 0;
}
/**
* Handle the message [EL] Satellite Elevations.
*/
static gps_mask_t greis_msg_EL(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use EL until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < session->gpsdata.satellites_visible + 1U) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: EL bad len %zu, needed at least %d\n", len,
session->gpsdata.satellites_visible + 1);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
short elevation;
/* GREIS elevation is -90 to 90 degrees */
/* GREIS uses 127 for n/a */
/* gpsd uses -91 for n/a, so adjust acordingly */
elevation = getub(buf, i);
if ((-90 > elevation) || (90 < elevation)) elevation = -91;
session->gpsdata.skyview[i].elevation = elevation;
}
session->driver.greis.seen_el = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: EL\n");
return 0;
}
/**
* Handle the message [AZ] Satellite Azimuths.
*/
static gps_mask_t greis_msg_AZ(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use AZ until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < session->gpsdata.satellites_visible + 1U) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: AZ bad len %zu, needed at least %d\n", len,
session->gpsdata.satellites_visible + 1);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
short azimuth;
/* GREIS azimuth is 0 to 180, multiply by 2 for 0 to 360 */
/* GREIS uses 255 for n/a */
/* gpsd azimuth is 0 to 359, so adjust acordingly */
azimuth = getub(buf, i) * 2;
if (360 == azimuth) azimuth = 0;
else if (360 < azimuth) azimuth = -1;
session->gpsdata.skyview[i].azimuth = azimuth;
}
session->driver.greis.seen_az = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: AZ\n");
return 0;
}
/**
* Handle the message [DC] Doppler (CA/L1)
*/
static gps_mask_t greis_msg_DC(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
long int_doppler;
size_t len_needed = (session->gpsdata.satellites_visible * 4) + 1;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use DC until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < len_needed) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: DC bad len %zu, needed at least %zu\n", len,
len_needed);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
int_doppler = getles32((char *)buf, i * 4);
if (0x7fffffff == int_doppler) {
/* out of range */
session->gpsdata.raw.meas[i].doppler = NAN;
} else {
session->gpsdata.raw.meas[i].doppler = int_doppler * 1e-4;
}
}
session->driver.greis.seen_raw = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: DC\n");
return 0;
}
/**
* Handle the message [EC] SNR (CA/L1).
* EC really outputs CNR, but what gpsd refers to as SNR _is_ CNR.
*/
static gps_mask_t greis_msg_EC(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use EC until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < session->gpsdata.satellites_visible + 1U) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: EC bad len %zu, needed at least %d\n", len,
session->gpsdata.satellites_visible + 1);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++)
session->gpsdata.skyview[i].ss = getub(buf, i);
session->driver.greis.seen_ec = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: EC\n");
return 0;
}
/**
* Handle the message [P3] CA/L2 Carrier Phases, RINEX L2C
*/
static gps_mask_t greis_msg_P3(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use P3 until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < len_needed) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: P3 bad len %zu, needed at least %zu\n", len,
len_needed);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
session->gpsdata.raw.meas[i].l2c = getled64((char *)buf, i * 8);
}
session->driver.greis.seen_raw = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: P3\n");
return 0;
}
/**
* Handle the message [PC] CA/L1 Carrier Phases, RINEX L1C
*/
static gps_mask_t greis_msg_PC(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use PC until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < len_needed) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: PC bad len %zu, needed at least %zu\n", len,
len_needed);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
session->gpsdata.raw.meas[i].carrierphase = getled64((char *)buf,
i * 8);
}
session->driver.greis.seen_raw = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: PC\n");
return 0;
}
/**
* Handle the message [R3] CA/L2 Pseudo-range, RINEX C2C
*/
static gps_mask_t greis_msg_R3(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use R3 until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < len_needed) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: R3 bad len %zu, needed at least %zu\n", len,
len_needed);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
/* get, and convert to meters */
session->gpsdata.raw.meas[i].c2c = \
getled64((char *)buf, i * 8) * CLIGHT;
}
session->driver.greis.seen_raw = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: R3\n");
return 0;
}
/**
* Handle the message [RC] Pseudo-range CA/L1, RINEX C1C
*/
static gps_mask_t greis_msg_RC(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
size_t len_needed = (session->gpsdata.satellites_visible * 8) + 1;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use RC until after SI provides indices\n");
return 0;
}
/* check against number of satellites + checksum */
if (len < len_needed) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: RC bad len %zu, needed at least %zu\n", len,
len_needed);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
/* get, and convert to meters */
session->gpsdata.raw.meas[i].pseudorange = \
getled64((char *)buf, i * 8) * CLIGHT;
}
session->driver.greis.seen_raw = true;
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: RC\n");
return 0;
}
/**
* Handle the message [SS] Satellite Navigation Status.
*/
static gps_mask_t greis_msg_SS(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
int i;
int used_count = 0;
if (!session->driver.greis.seen_si) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: can't use SS until after SI provides indices\n");
return 0;
}
/* check against number of satellites + solution type + checksum */
if (len < session->gpsdata.satellites_visible + 2U) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: SI bad len %zu, needed at least %d\n", len,
session->gpsdata.satellites_visible + 2);
return 0;
}
for (i = 0; i < session->gpsdata.satellites_visible; i++) {
/*
* From the GREIS Reference Guide: "Codes [0...3], [40...62], and
* [64...255] indicate that given satellite is used in position
* computation and show which measurements are used. The rest of codes
* indicate that satellite is not used in position computation and
* indicate why this satellite is excluded from position computation."
* Refer to Table 3-4 "Satellite Navigation Status" for the specific
* code meanings.
*/
uint8_t nav_status = getub(buf, i);
session->gpsdata.skyview[i].used =
(nav_status <= 3) ||
(nav_status >= 40 && nav_status <= 62) ||
(nav_status >= 64);
if (session->gpsdata.skyview[i].used)
used_count++;
}
session->gpsdata.satellites_used = used_count;
gpsd_log(&session->context->errout, LOG_DATA,
"GREIS: SS, satellites_used: %d\n",
session->gpsdata.satellites_used);
return used_count ? USED_IS : 0;
}
/**
* Handle the message [::](ET) Epoch Time.
* This should be kept as the last message in each epoch.
*/
static gps_mask_t greis_msg_ET(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
uint32_t tod;
gps_mask_t mask = 0;
if (len < 5) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: ET bad len %zu\n", len);
return 0;
}
if (!session->driver.greis.seen_rt) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: got ET, but no preceding RT for epoch\n");
return 0;
}
tod = getleu32(buf, 0);
if (tod != session->driver.greis.rt_tod) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: broken epoch, RT had %lu, but ET has %lu\n",
(unsigned long)session->driver.greis.rt_tod,
(unsigned long)tod);
return 0;
}
/* Skyview time does not differ from time in GT message */
session->gpsdata.skyview_time = NAN;
gpsd_log(&session->context->errout, LOG_DEBUG,
"GREIS: ET, seen: az %d, ec %d, el %d, rt %d, si %d, uo %d\n",
(int)session->driver.greis.seen_az,
(int)session->driver.greis.seen_ec,
(int)session->driver.greis.seen_el,
(int)session->driver.greis.seen_rt,
(int)session->driver.greis.seen_si,
(int)session->driver.greis.seen_uo);
/* Make sure we got the satellite data, then report it. */
if ((session->driver.greis.seen_az && session->driver.greis.seen_ec &&
session->driver.greis.seen_el && session->driver.greis.seen_si)) {
/* Skyview seen, update it. Go even if no seen_ss or none visible */
mask |= SATELLITE_SET;
if (session->driver.greis.seen_raw) {
mask |= RAW_IS;
} else {
session->gpsdata.raw.mtime.tv_sec = 0;
session->gpsdata.raw.mtime.tv_nsec = 0;
}
} else {
session->gpsdata.raw.mtime.tv_sec = 0;
session->gpsdata.raw.mtime.tv_nsec = 0;
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: ET: missing satellite details in this epoch\n");
}
gpsd_log(&session->context->errout, LOG_DATA, "GREIS: ET, tod: %lu\n",
(unsigned long)tod);
/* This is a good place to poll firmware version if we need it.
* Waited until now to avoid the startup rush and out of
* critical time path
*/
if (0 == strlen(session->subtype)) {
/* get version */
(void)greis_write(session, get_ver, sizeof(get_ver) - 1);
}
/* The driver waits for ET to send any reports
* Just REPORT_IS is not enough to trigger sending of reports to clients.
* STATUS_SET seems best, if no status by now the status is no fix */
return mask | REPORT_IS | STATUS_SET;
}
struct dispatch_table_entry {
char id0;
char id1;
gps_mask_t (*handler)(struct gps_device_t *, unsigned char *, size_t);
};
static struct dispatch_table_entry dispatch_table[] = {
{':', ':', greis_msg_ET},
{'A', 'Z', greis_msg_AZ},
{'D', 'C', greis_msg_DC},
{'D', 'P', greis_msg_DP},
{'E', 'C', greis_msg_EC},
{'E', 'R', greis_msg_ER},
{'E', 'L', greis_msg_EL},
{'G', 'T', greis_msg_GT},
{'R', '3', greis_msg_R3},
{'R', 'C', greis_msg_RC},
{'P', '3', greis_msg_P3},
{'P', 'C', greis_msg_PC},
{'P', 'V', greis_msg_PV},
{'R', 'E', greis_msg_RE},
{'S', 'G', greis_msg_SG},
{'S', 'I', greis_msg_SI},
{'S', 'S', greis_msg_SS},
{'U', 'O', greis_msg_UO},
{'~', '~', greis_msg_RT},
};
#define dispatch_table_size (sizeof(dispatch_table) / sizeof(dispatch_table[0]))
/**
* Parse the data from the device
*/
static gps_mask_t greis_dispatch(struct gps_device_t *session,
unsigned char *buf, size_t len)
{
size_t i;
char id0, id1;
if (len == 0)
return 0;
/*
* This is set because the device reliably signals end of cycle.
* The core library zeroes it just before it calls each driver's
* packet analyzer.
*/
session->cycle_end_reliable = true;
/* Length should have already been checked in packet.c, but just in case */
if (len < HEADER_LENGTH) {
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: Packet length %zu shorter than min length\n", len);
return 0;
}
/* we may need to dump the raw packet */
gpsd_log(&session->context->errout, LOG_RAW,
"GREIS: raw packet id '%c%c'\n", buf[0], buf[1]);
id0 = buf[0];
id1 = buf[1];
len -= HEADER_LENGTH;
buf += HEADER_LENGTH;
for (i = 0; i < dispatch_table_size; i++) {
struct dispatch_table_entry *entry = &dispatch_table[i];
if (id0 == entry->id0 && id1 == entry->id1) {
return entry->handler(session, buf, len);
}
}
gpsd_log(&session->context->errout, LOG_WARN,
"GREIS: unknown packet id '%c%c' length %zu\n", id0, id1, len);
return 0;
}
/**********************************************************
*
* Externally called routines below here
*
**********************************************************/
/**
* Write data to the device with checksum.
* Returns number of bytes written on successful write, -1 otherwise.
*/
static ssize_t greis_write(struct gps_device_t *session,
const char *msg, size_t msglen)
{
char checksum_str[3] = {0};
ssize_t count;
if (session->context->readonly) {
/* readonly mode, do not write anything */
return -1;
}
if (NULL == msg) {
/* We do sometimes write zero length to wake up GPS,
* so just test for NULL msg, not zero length message */
gpsd_log(&session->context->errout, LOG_ERROR,
"GREIS: nothing to write\n");
return -1;
}
/* Account for length + checksum marker + checksum + \r + \n + \0 */
if (msglen + 6 > sizeof(session->msgbuf)) {
gpsd_log(&session->context->errout, LOG_ERROR,
"GREIS: msgbuf is smaller than write length %zu\n", msglen);
return -1;
}
if (msg != NULL)
memcpy(&session->msgbuf[0], msg, msglen);
if (msglen == 0) {
/* This is a dummy write, don't give a checksum. */
session->msgbuf[0] = '\n';
session->msgbuflen = 1;
gpsd_log(&session->context->errout, LOG_PROG,
"GREIS: Dummy write\n");
} else {
unsigned char checksum;
session->msgbuflen = msglen;
session->msgbuf[session->msgbuflen++] = '@'; /* checksum marker */
/* calculate checksum with @, place at end, and set length to write */
checksum = greis_checksum((unsigned char *)session->msgbuf,
session->msgbuflen);
(void)snprintf(checksum_str, sizeof(checksum_str), "%02X", checksum);
session->msgbuf[session->msgbuflen++] = checksum_str[0];
session->msgbuf[session->msgbuflen++] = checksum_str[1];
session->msgbuf[session->msgbuflen++] = '\r';
session->msgbuf[session->msgbuflen++] = '\n';
gpsd_log(&session->context->errout, LOG_PROG,
"GREIS: Writing command '%.*s', checksum: %s\n",
(int)msglen, msg, checksum_str);
}
session->msgbuf[session->msgbuflen] = '\0';
count = gpsd_write(session, session->msgbuf, session->msgbuflen);
if (count != (ssize_t)session->msgbuflen)