-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver_ubx.c
1780 lines (1636 loc) · 55.6 KB
/
driver_ubx.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
/*
* UBX driver. All capabilities are common to Antaris4 and u-blox 6.
* Reference manuals are at
* http://www.u-blox.com/en/download/documents-a-resources/u-blox-6-gps-modules-resources.html
*
* updated for u-blox 8
* http://www.ublox.com/images/downloads/Product_Docs/u-bloxM8_ReceiverDescriptionProtocolSpec_%28UBX-13003221%29_Public.pdf
*
* Week counters are not limited to 10 bits. It's unknown what
* the firmware is doing to disambiguate them, if anything; it might just
* be adding a fixed offset based on a hidden epoch value, in which case
* unhappy things will occur on the next rollover.
*
* For the Antaris 4, the default leap-second offset (before getting one from
* the sats, one presumes) is 0sec; for the u-blox 6 it's 15sec.
*
* This file is Copyright (c) 2010-2018 by the GPSD project
* SPDX-License-Identifier: BSD-2-clause
*
*/
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "gpsd.h"
#if defined(UBLOX_ENABLE) && defined(BINARY_ENABLE)
#include "driver_ubx.h"
#include "bits.h"
/*
* A ubx packet looks like this:
* leader: 0xb5 0x62
* message class: 1 byte
* message type: 1 byte
* length of payload: 2 bytes
* payload: variable length
* checksum: 2 bytes
*
* see also the FV25 and UBX documents on reference.html
*/
#define UBX_PREFIX_LEN 6
#define UBX_CLASS_OFFSET 2
#define UBX_TYPE_OFFSET 3
/* because we hates magic numbers forever */
#define USART1_ID 1
#define USART2_ID 2
#define USB_ID 3
#define UBX_PROTOCOL_MASK 0x01
#define NMEA_PROTOCOL_MASK 0x02
#define RTCM_PROTOCOL_MASK 0x04
#define UBX_CFG_LEN 20
#define outProtoMask 14
static gps_mask_t ubx_parse(struct gps_device_t *session, unsigned char *buf,
size_t len);
static gps_mask_t ubx_msg_nav_eoe(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static gps_mask_t ubx_msg_nav_dop(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static void ubx_msg_inf(struct gps_device_t *session, unsigned char *buf, size_t data_len);
static gps_mask_t ubx_msg_nav_posecef(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static gps_mask_t ubx_msg_nav_pvt(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static void ubx_msg_sbas(struct gps_device_t *session, unsigned char *buf);
static gps_mask_t ubx_msg_nav_sat(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static gps_mask_t ubx_msg_nav_sol(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static gps_mask_t ubx_msg_nav_svinfo(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static gps_mask_t ubx_msg_nav_timegps(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static gps_mask_t ubx_msg_nav_velecef(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
static void ubx_msg_mon_ver(struct gps_device_t *session,
unsigned char *buf, size_t data_len);
#ifdef RECONFIGURE_ENABLE
static void ubx_mode(struct gps_device_t *session, int mode);
#endif /* RECONFIGURE_ENABLE */
/**
* Receiver/Software Version
* UBX-MON-VER
*
* sadly more info than fits in session->swtype for now.
* so squish the data hard, max is maybe 100?
*/
static void
ubx_msg_mon_ver(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
size_t n = 0; /* extended info counter */
char obuf[128]; /* temp version string buffer */
if ( 44 > data_len ) {
/* incomplete message */
return;
}
/* save SW and HW Version as subtype */
(void)snprintf(obuf, sizeof(obuf),
"SW %.30s,HW %.10s",
(char *)&buf[UBX_MESSAGE_DATA_OFFSET + 0],
(char *)&buf[UBX_MESSAGE_DATA_OFFSET + 30]);
/* get n number of Extended info strings. what is max n? */
for ( n = 0; ; n++ ) {
size_t start_of_str = UBX_MESSAGE_DATA_OFFSET + 40 + (30 * n);
if ( (start_of_str + 2 ) > data_len ) {
/* last one can be shorter than 30 */
/* no more data */
break;
}
(void)strlcat(obuf, ",", sizeof(obuf));
(void)strlcat(obuf, (char *)&buf[start_of_str], sizeof(obuf));
}
/* save what we can */
(void)strlcpy(session->subtype, obuf, sizeof(session->subtype));
/* output SW and HW Version at LOG_INFO */
gpsd_log(&session->context->errout, LOG_INF,
"UBX_MON_VER: %.*s\n",
(int)sizeof(obuf), obuf);
}
/*
* Navigation Position ECEF message
*/
static gps_mask_t
ubx_msg_nav_posecef(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
gps_mask_t mask = ECEF_SET;
double fTOW;
if (data_len < 20) {
gpsd_log(&session->context->errout, LOG_WARN,
"Invalid NAV POSECEF message, payload len %zd", data_len);
return 0;
}
fTOW = getleu32(buf, 0) / 1000.0;
session->newdata.ecef.x = getles32(buf, 4) / 100.0;
session->newdata.ecef.y = getles32(buf, 8) / 100.0;
session->newdata.ecef.z = getles32(buf, 12) / 100.0;
session->newdata.ecef.pAcc = getleu32(buf, 16) / 100.0;
gpsd_log(&session->context->errout, LOG_DATA,
"UBX_NAV_POSECEF: fTOW=%.3f ECEF x=%.2f y=%.2f z=%.2f pAcc=%.2f\n",
fTOW,
session->newdata.ecef.x,
session->newdata.ecef.y,
session->newdata.ecef.z,
session->newdata.ecef.pAcc);
return mask;
}
/**
* Navigation Position Velocity Time solution message
*/
static gps_mask_t
ubx_msg_nav_pvt(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
uint8_t valid;
uint8_t flags;
uint8_t navmode;
struct tm unpacked_date;
double hacc, vacc, sacc;
int *status = &session->gpsdata.status;
int *mode = &session->newdata.mode;
gps_mask_t mask = 0;
if (data_len != 92)
return 0;
valid = (unsigned int)getub(buf, 11);
navmode = (unsigned char)getub(buf, 20);
flags = (unsigned int)getub(buf, 21);
switch (navmode)
{
case UBX_MODE_TMONLY:
{
if (*mode != MODE_NO_FIX) {
*mode = MODE_NO_FIX;
mask |= MODE_SET;
}
if (*status != STATUS_NO_FIX) {
*status = STATUS_NO_FIX;
mask |= STATUS_SET;
}
break;
}
case UBX_MODE_3D:
case UBX_MODE_GPSDR:
{
if (*mode != MODE_3D) {
*mode = MODE_3D;
mask |= MODE_SET;
}
if ((flags & UBX_NAV_PVT_FLAG_DGPS) == UBX_NAV_PVT_FLAG_DGPS) {
if (*status != STATUS_DGPS_FIX) {
*status = STATUS_DGPS_FIX;
mask |= STATUS_SET;
}
} else {
if (*status != STATUS_FIX) {
*status = STATUS_FIX;
mask |= STATUS_SET;
}
}
mask |= LATLON_SET | ALTITUDE_SET | SPEED_SET | TRACK_SET;
break;
}
case UBX_MODE_2D:
case UBX_MODE_DR: /* consider this too as 2D */
{
if (*mode != MODE_2D) {
*mode = MODE_2D;
mask |= MODE_SET;
};
if (*status != STATUS_FIX) {
*status = STATUS_FIX;
mask |= STATUS_SET;
}
mask |= LATLON_SET | SPEED_SET;
break;
}
default:
{
if (*mode != MODE_NO_FIX) {
*mode = MODE_NO_FIX;
mask |= MODE_SET;
};
if (*status != STATUS_NO_FIX) {
*status = STATUS_NO_FIX;
mask |= STATUS_SET;
}
break;
}
}
if ((valid & UBX_NAV_PVT_VALID_DATE_TIME) == UBX_NAV_PVT_VALID_DATE_TIME) {
double subseconds;
unpacked_date.tm_year = (uint16_t)getleu16(buf, 4) - 1900;
unpacked_date.tm_mon = (uint8_t)getub(buf, 6) - 1;
unpacked_date.tm_mday = (uint8_t)getub(buf, 7);
unpacked_date.tm_hour = (uint8_t)getub(buf, 8);
unpacked_date.tm_min = (uint8_t)getub(buf, 9);
unpacked_date.tm_sec = (uint8_t)getub(buf, 10);
unpacked_date.tm_isdst = 0;
unpacked_date.tm_wday = 0;
unpacked_date.tm_yday = 0;
subseconds = 1e-9 * (int32_t)getles32(buf, 16);
session->newdata.time = \
(timestamp_t)mkgmtime(&unpacked_date) + subseconds;
mask |= TIME_SET | NTPTIME_IS;
}
session->newdata.longitude = 1e-7 * (int32_t)getles32(buf, 24);
session->newdata.latitude = 1e-7 * (int32_t)getles32(buf, 28);
session->newdata.altitude = 1e-3 * (int32_t)getles32(buf, 32);
session->newdata.speed = 1e-3 * (int32_t)getles32(buf, 60);
session->newdata.track = 1e-5 * (int32_t)getles32(buf, 64);
hacc = (double)(getles32(buf, 40) / 1000.0);
vacc = (double)(getles32(buf, 44) / 1000.0);
sacc = (double)(getles32(buf, 48) / 1000.0);
// Assuming hacc == epx == epy is the best we can do
session->newdata.epx = session->newdata.epy = hacc;
session->newdata.epv = vacc;
session->newdata.eps = sacc;
mask |= HERR_SET | VERR_SET | SPEEDERR_SET;
gpsd_log(&session->context->errout, LOG_DATA,
"NAV_PVT: flags=%02x time=%.2f lat=%.2f lon=%.2f alt=%.2f track=%.2f speed=%.2f climb=%.2f mode=%d status=%d used=%d\n",
flags,
session->newdata.time,
session->newdata.latitude,
session->newdata.longitude,
session->newdata.altitude,
session->newdata.track,
session->newdata.speed,
session->newdata.climb,
session->newdata.mode,
session->gpsdata.status,
session->gpsdata.satellites_used);
return mask;
}
/**
* Navigation solution message: UBX-NAV-SOL
*/
static gps_mask_t
ubx_msg_nav_sol(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
unsigned int flags;
unsigned char navmode;
gps_mask_t mask;
if (data_len != 52)
return 0;
flags = (unsigned int)getub(buf, 11);
mask = 0;
#define DATE_VALID (UBX_SOL_VALID_WEEK | UBX_SOL_VALID_TIME)
if ((flags & DATE_VALID) == DATE_VALID) {
unsigned short gw;
unsigned int tow;
tow = (unsigned int)getleu32(buf, 0);
gw = (unsigned short)getles16(buf, 8);
session->newdata.time = gpsd_gpstime_resolve(session, gw, tow / 1000.0);
mask |= TIME_SET | NTPTIME_IS;
}
#undef DATE_VALID
session->newdata.ecef.x = getles32(buf, 12) / 100.0;
session->newdata.ecef.y = getles32(buf, 16) / 100.0;
session->newdata.ecef.z = getles32(buf, 20) / 100.0;
session->newdata.ecef.pAcc = getleu32(buf, 24) / 100.0;
session->newdata.ecef.vx = getles32(buf, 28) / 100.0;
session->newdata.ecef.vy = getles32(buf, 32) / 100.0;
session->newdata.ecef.vz = getles32(buf, 36) / 100.0;
session->newdata.ecef.vAcc = getleu32(buf, 40) / 100.0;
ecef_to_wgs84fix(&session->newdata, &session->gpsdata.separation,
session->newdata.ecef.x, session->newdata.ecef.y,
session->newdata.ecef.z, session->newdata.ecef.vx,
session->newdata.ecef.vy, session->newdata.ecef.vz);
mask |= LATLON_SET | ALTITUDE_SET | SPEED_SET | TRACK_SET | CLIMB_SET \
| ECEF_SET | VECEF_SET;
if (session->driver.ubx.last_herr > 0.0) {
session->newdata.epx = session->newdata.epy \
= session->driver.ubx.last_herr;
mask |= HERR_SET;
session->driver.ubx.last_herr = 0.0;
}
if (session->driver.ubx.last_verr > 0.0) {
session->newdata.epv = session->driver.ubx.last_verr;
mask |= VERR_SET;
session->driver.ubx.last_verr = 0.0;
}
session->newdata.eps = (double)(getles32(buf, 40) / 100.0);
mask |= SPEEDERR_SET;
/* Better to have a single point of truth about DOPs */
//session->gpsdata.dop.pdop = (double)(getleu16(buf, 44)/100.0);
session->gpsdata.satellites_used = (int)getub(buf, 47);
navmode = (unsigned char)getub(buf, 10);
switch (navmode) {
case UBX_MODE_TMONLY:
/* Surveyed-in, better not have moved */
session->newdata.mode = MODE_3D;
mask |= GOODTIME_IS;
break;
case UBX_MODE_3D:
session->newdata.mode = MODE_3D;
break;
case UBX_MODE_2D:
case UBX_MODE_DR: /* consider this too as 2D */
case UBX_MODE_GPSDR: /* FIX-ME: DR-aided GPS may be valid 3D */
session->newdata.mode = MODE_2D;
break;
default:
session->newdata.mode = MODE_NO_FIX;
}
if ((flags & UBX_SOL_FLAG_DGPS) != 0)
session->gpsdata.status = STATUS_DGPS_FIX;
else if (session->newdata.mode != MODE_NO_FIX)
session->gpsdata.status = STATUS_FIX;
mask |= MODE_SET | STATUS_SET;
gpsd_log(&session->context->errout, LOG_DATA,
"UBX_NAV_SOL: time=%.2f lat=%.2f lon=%.2f alt=%.2f track=%.2f speed=%.2f climb=%.2f mode=%d status=%d used=%d\n",
session->newdata.time,
session->newdata.latitude,
session->newdata.longitude,
session->newdata.altitude,
session->newdata.track,
session->newdata.speed,
session->newdata.climb,
session->newdata.mode,
session->gpsdata.status,
session->gpsdata.satellites_used);
return mask;
}
/**
* Geodetic position solution message
*/
static gps_mask_t
ubx_msg_nav_posllh(struct gps_device_t *session, unsigned char *buf,
size_t data_len UNUSED)
{
session->driver.ubx.last_herr = (double)(getleu32(buf, 20) / 1000.0);
session->driver.ubx.last_verr = (double)(getleu32(buf, 24) / 1000.0);
return 0;
}
/**
* Dilution of precision message
*/
static gps_mask_t
ubx_msg_nav_dop(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
if (data_len != 18)
return 0;
/*
* We make a deliberate choice not to clear DOPs from the
* last skyview here, but rather to treat this as a supplement
* to our calculations from the visibility matrix, trusting
* the firmware algorithms over ours.
*/
session->gpsdata.dop.gdop = (double)(getleu16(buf, 4) / 100.0);
session->gpsdata.dop.pdop = (double)(getleu16(buf, 6) / 100.0);
session->gpsdata.dop.tdop = (double)(getleu16(buf, 8) / 100.0);
session->gpsdata.dop.vdop = (double)(getleu16(buf, 10) / 100.0);
session->gpsdata.dop.hdop = (double)(getleu16(buf, 12) / 100.0);
gpsd_log(&session->context->errout, LOG_DATA,
"NAVDOP: gdop=%.2f pdop=%.2f "
"hdop=%.2f vdop=%.2f tdop=%.2f mask={DOP}\n",
session->gpsdata.dop.gdop,
session->gpsdata.dop.hdop,
session->gpsdata.dop.vdop,
session->gpsdata.dop.pdop, session->gpsdata.dop.tdop);
return DOP_SET;
}
/**
* End of Epoch
*/
static gps_mask_t
ubx_msg_nav_eoe(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
long int iTOW;
if ( 4 > data_len)
return 0;
iTOW = getles32(buf, 0);
gpsd_log(&session->context->errout, LOG_DATA, "EOE: iTOW=%ld\n", iTOW);
/* nothing really to new, but report data collected so far
* and clear for next data set */
// return CLEAR_IS | REPORT_IS;
// return REPORT_IS | STATUS_SET;
/* strangely any mask return here makes things worse... */
return 0;
}
/**
* GPS Leap Seconds - UBX-NAV-TIMEGPS
*/
static gps_mask_t
ubx_msg_nav_timegps(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
uint8_t valid; /* Validity Flags */
gps_mask_t mask = 0;
if (data_len != 16)
return 0;
valid = getub(buf, 11);
// Valid leap seconds ?
if ((valid & UBX_TIMEGPS_VALID_LEAP_SECOND) ==
UBX_TIMEGPS_VALID_LEAP_SECOND) {
session->context->leap_seconds = (int)getub(buf, 10);
session->context->valid |= LEAP_SECOND_VALID;
}
// Valid GPS time of week and week number
#define VALID_TIME (UBX_TIMEGPS_VALID_TIME | UBX_TIMEGPS_VALID_WEEK)
if ((valid & VALID_TIME) == VALID_TIME) {
#undef VALID_TIME
uint16_t week;
double iTOW; /* integer part of TOW in ms */
double fTOW; /* fractional part of TOW in ns */
double TOW; /* complete TOW in seconds */
double tAcc; /* Time Accuracy Estimate in ns */
iTOW = (double)getleu32(buf, 0); /* GPS TOW in ms */
fTOW = (double)getles32(buf, 4); /* Fractional part of TOW */
week = getles16(buf, 8);
tAcc = (double)getleu32(buf, 12); /* tAcc in ms */
TOW = (iTOW * 1e-3) + (fTOW * 1e-9);
session->newdata.time = gpsd_gpstime_resolve(session, week, TOW);
session->newdata.ept = tAcc * 1e-9;
mask |= (TIME_SET | NTPTIME_IS);
}
gpsd_log(&session->context->errout, LOG_DATA,
"TIMEGPS: time=%.2f mask={TIME}\n",
session->newdata.time);
return mask;
}
/**
* GPS Satellite Info -- new style UBX-NAV-SAT
*/
static gps_mask_t
ubx_msg_nav_sat(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
unsigned int i, nchan, nsv, st, ver;
if (data_len < 8) {
gpsd_log(&session->context->errout, LOG_PROG,
"runt NAV-SAT (datalen=%zd)\n", data_len);
return 0;
}
ver = (unsigned int)getub(buf, 4);
if (1 != ver) {
gpsd_log(&session->context->errout, LOG_WARN,
"NAV-SAT message unknown version %d", ver);
return 0;
}
nchan = (unsigned int)getub(buf, 5);
if (nchan > MAXCHANNELS) {
gpsd_log(&session->context->errout, LOG_WARN,
"Invalid NAV-SAT message, >%d reported visible",
MAXCHANNELS);
return 0;
}
/* two "unused" bytes at buf[6:7] */
gpsd_zero_satellites(&session->gpsdata);
nsv = 0;
for (i = st = 0; i < nchan; i++) {
unsigned int off = 8 + 12 * i;
short PRN = 0;
unsigned char gnssId = getub(buf, off + 0);
short svId = (short)getub(buf, off + 1);
unsigned char cno = getub(buf, off + 2);
uint32_t flags = getleu32(buf, off + 8);
bool used = (bool)(flags & 0x08);
if (0 == svId) {
/* skip 0 svId */
continue;
}
/* make up a PRN based on gnssId:svId, using Appendix A from
* u-blox 8 / u-blox M8 Receiver Description - Manual */
switch (gnssId) {
case 0:
/* GPS, 1-32 maps to 1-32 */
if (32 < svId) {
/* skip bad svId */
continue;
}
PRN = svId;
break;
case 1:
/* SBAS, 120-158 mapes to 120-158 */
if (120 > svId || 158 < svId) {
/* skip bad svId */
continue;
}
PRN = svId;
break;
case 2:
/* Galileo, 1-36 maps to 211-246 */
if (36 < svId) {
/* skip bad svId */
continue;
}
PRN = svId + 210;
break;
case 3:
/* BeiDou, 1-37 maps to 159-163,33-64 */
if (6 > svId) {
/* 1-5 maps to 159-163 */
PRN = svId + 158;
} else if (37 < svId) {
/* skip bad svId */
continue;
} else {
/* 6-37 maps to 33-64 */
PRN = svId + 27;
}
break;
case 4:
/* IMES, 1-10 maps to 173-182 */
if (10 < svId) {
/* skip bad svId */
continue;
}
PRN = svId + 172;
break;
case 5:
/* QZSS, 1-5 maps to 193-197 */
if (5 < svId) {
/* skip bad svId */
continue;
}
PRN = svId + 192;
break;
case 6:
/* GLONASS, 1-32 maps to 65-96 */
if (32 < svId) {
/* skip bad svId */
/* 255 == tracked, but unidentified, skip */
continue;
}
PRN = svId + 64;
break;
default:
/* Huh? */
continue;
}
session->gpsdata.skyview[st].gnssid = gnssId;
session->gpsdata.skyview[st].svid = svId;
session->gpsdata.skyview[st].PRN = PRN;
session->gpsdata.skyview[st].ss = (float)cno;
session->gpsdata.skyview[st].elevation = (short)getsb(buf, off + 3);
session->gpsdata.skyview[st].azimuth = (short)getles16(buf, off + 4);
session->gpsdata.skyview[st].used = used;
if (used || PRN == (short)session->driver.ubx.sbas_in_use) {
nsv++;
session->gpsdata.skyview[st].used = true;
}
st++;
}
/* UBX does not give us these, so recompute */
session->gpsdata.dop.xdop = NAN;
session->gpsdata.dop.ydop = NAN;
session->gpsdata.skyview_time = NAN;
session->gpsdata.satellites_visible = (int)st;
session->gpsdata.satellites_used = (int)nsv;
gpsd_log(&session->context->errout, LOG_DATA,
"SAT: visible=%d used=%d mask={SATELLITE|USED}\n",
session->gpsdata.satellites_visible,
session->gpsdata.satellites_used);
return SATELLITE_SET | USED_IS;
}
/**
* GPS Satellite Info -- deprecated - UBX-NAV-SVINFO
*/
static gps_mask_t
ubx_msg_nav_svinfo(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
unsigned int i, nchan, nsv, st;
if (data_len < 8) {
gpsd_log(&session->context->errout, LOG_PROG,
"runt svinfo (datalen=%zd)\n", data_len);
return 0;
}
nchan = (unsigned int)getub(buf, 4);
if (nchan > MAXCHANNELS) {
gpsd_log(&session->context->errout, LOG_WARN,
"Invalid NAV SVINFO message, >%d reported visible",
MAXCHANNELS);
return 0;
}
gpsd_zero_satellites(&session->gpsdata);
nsv = 0;
for (i = st = 0; i < nchan; i++) {
unsigned int off = 8 + 12 * i;
short PRN = (short)getub(buf, off + 1);
unsigned char snr = getub(buf, off + 4);
bool used = (bool)(getub(buf, off + 2) & 0x01);
/* fit into gnssid:svid */
if (0 == PRN) {
/* skip 0 PRN */
continue;
} else if ((1 <= PRN) && (32 >= PRN)) {
/* GPS */
session->gpsdata.skyview[st].gnssid = 0;
session->gpsdata.skyview[st].svid = PRN;
} else if ((33 <= PRN) && (64 >= PRN)) {
/* BeiDou */
session->gpsdata.skyview[st].gnssid = 3;
session->gpsdata.skyview[st].svid = PRN - 158;
} else if ((65 <= PRN) && (96 >= PRN)) {
/* GLONASS */
session->gpsdata.skyview[st].gnssid = 6;
session->gpsdata.skyview[st].svid = PRN - 64;
} else if ((120 <= PRN) && (158 >= PRN)) {
/* SBAS */
session->gpsdata.skyview[st].gnssid = 1;
session->gpsdata.skyview[st].svid = PRN;
} else if ((159 <= PRN) && (163 >= PRN)) {
/* BeiDou, again */
session->gpsdata.skyview[st].gnssid = 3;
session->gpsdata.skyview[st].svid = PRN - 126;
} else if ((173 <= PRN) && (182 >= PRN)) {
/* IMES */
session->gpsdata.skyview[st].gnssid = 4;
session->gpsdata.skyview[st].svid = PRN - 172;
} else if ((193 <= PRN) && (197 >= PRN)) {
/* QZSS */
session->gpsdata.skyview[st].gnssid = 5;
session->gpsdata.skyview[st].svid = PRN - 192;
} else if ((211 <= PRN) && (246 >= PRN)) {
/* Galileo */
session->gpsdata.skyview[st].gnssid = 2;
session->gpsdata.skyview[st].svid = PRN - 210;
} else if (255 == PRN) {
/* GLONASS, again */
session->gpsdata.skyview[st].gnssid = 6;
session->gpsdata.skyview[st].svid = 255;
}
session->gpsdata.skyview[st].PRN = PRN;
session->gpsdata.skyview[st].ss = (float)snr;
session->gpsdata.skyview[st].elevation = (short)getsb(buf, off + 5);
session->gpsdata.skyview[st].azimuth = (short)getles16(buf, off + 6);
session->gpsdata.skyview[st].used = used;
if (used || PRN == (short)session->driver.ubx.sbas_in_use) {
nsv++;
session->gpsdata.skyview[st].used = true;
}
st++;
}
/* UBX does not give us these, so recompute */
session->gpsdata.dop.xdop = NAN;
session->gpsdata.dop.ydop = NAN;
session->gpsdata.skyview_time = NAN;
session->gpsdata.satellites_visible = (int)st;
session->gpsdata.satellites_used = (int)nsv;
gpsd_log(&session->context->errout, LOG_DATA,
"SVINFO: visible=%d used=%d mask={SATELLITE|USED}\n",
session->gpsdata.satellites_visible,
session->gpsdata.satellites_used);
return SATELLITE_SET | USED_IS;
}
/*
* Velocity Position ECEF message
*/
static gps_mask_t
ubx_msg_nav_velecef(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
gps_mask_t mask = VECEF_SET;
double fTOW;
if (data_len < 20) {
gpsd_log(&session->context->errout, LOG_WARN,
"Invalid NAV VELECEF message, payload len %zd", data_len);
return 0;
}
fTOW = getleu32(buf, 0) / 1000.0;
session->newdata.ecef.vx = getles32(buf, 4) / 100.0;
session->newdata.ecef.vy = getles32(buf, 8) / 100.0;
session->newdata.ecef.vz = getles32(buf, 12) / 100.0;
session->newdata.ecef.vAcc = getleu32(buf, 16) / 100.0;
gpsd_log(&session->context->errout, LOG_DATA,
"UBX_NAV_VELECEF: fTOW=%.3f ECEF vx=%.2f vy=%.2f vz=%.2f vAcc=%.2f\n",
fTOW,
session->newdata.ecef.vx,
session->newdata.ecef.vy,
session->newdata.ecef.vz,
session->newdata.ecef.vAcc);
return mask;
}
/*
* SBAS Info
*/
static void ubx_msg_sbas(struct gps_device_t *session, unsigned char *buf)
{
#ifdef __UNUSED_DEBUG__
unsigned int i, nsv;
gpsd_log(&session->context->errout, LOG_WARN,
"SBAS: %d %d %d %d %d\n",
(int)getub(buf, 4), (int)getub(buf, 5), (int)getub(buf, 6),
(int)getub(buf, 7), (int)getub(buf, 8));
nsv = (int)getub(buf, 8);
for (i = 0; i < nsv; i++) {
int off = 12 + 12 * i;
gpsd_log(&session->context->errout, LOG_WARN,
"SBAS info on SV: %d\n", (int)getub(buf, off));
}
#endif /* __UNUSED_DEBUG__ */
/* really 'in_use' depends on the sats info, EGNOS is still in test */
/* In WAAS areas one might also check for the type of corrections indicated */
session->driver.ubx.sbas_in_use = (unsigned char)getub(buf, 4);
}
/*
* Multi-GNSS Raw measurement Data -- UBX-RXM-RAWX
*/
static gps_mask_t ubx_rxm_rawx(struct gps_device_t *session,
const unsigned char *buf,
size_t data_len)
{
double rcvTow, t_intp;
uint16_t week;
int8_t leapS;
uint8_t numMeas;
uint8_t recStat;
int i;
const char * obs_code;
if (16 > data_len) {
gpsd_log(&session->context->errout, LOG_WARN,
"Invalid RXM-RAWX message, payload len %zd", data_len);
return 0;
}
rcvTow = getled64((const char *)buf, 0); /* time of week in seconds */
week = getleu16(buf, 8);
leapS = getsb(buf, 10);
numMeas = getub(buf, 11);
recStat = getub(buf, 12);
gpsd_log(&session->context->errout, LOG_PROG,
"UBX_RXM_RAWX: rcvTow %f week %u leapS %d numMeas %u recStat %d\n",
rcvTow, week, leapS, numMeas, recStat);
if (recStat & 1) {
/* Valid leap seconds */
session->context->leap_seconds = leapS;
session->context->valid |= LEAP_SECOND_VALID;
}
/* convert GPS weeks and TOW to UTC */
session->newdata.time = gpsd_gpstime_resolve(session, week, rcvTow);
/* get mtime in GPS time, not UTC */
session->gpsdata.raw.mtime.tv_nsec =
modf(session->newdata.time, &t_intp) * 10e8;
/* u-blox says to add in leapS, valid or not */
session->gpsdata.raw.mtime.tv_sec = (time_t)t_intp + leapS;
/* zero the measurement data */
/* so we can tell which meas never got set */
memset(session->gpsdata.raw.meas, 0, sizeof(session->gpsdata.raw.meas));
for (i = 0; i < numMeas; i++) {
int off = 32 * i;
/* psuedorange in meters */
double prMes = getled64((const char *)buf, off + 16);
/* carrier phase in cycles */
double cpMes = getled64((const char *)buf, off + 24);
/* doppler in Hz, positive towards sat */
double doMes = getlef32((const char *)buf, off + 32);
uint8_t gnssId = getub(buf, off + 36);
uint8_t svId = getub(buf, off + 37);
/* GLONASS frequency slot */
uint8_t freqId = getub(buf, off + 39);
/* carrier phase locktime in ms, max 64500ms */
uint16_t locktime = getleu16(buf, off + 40);
/* carrier-to-noise density ratio dB-Hz */
uint8_t cno = getub(buf, off + 42);
uint8_t prStdev = getub(buf, off + 43) & 0x0f;
uint8_t cpStdev = getub(buf, off + 44) & 0x0f;
uint8_t doStdev = getub(buf, off + 45) & 0x0f;
/* tracking stat
* bit 0 - prMes valid
* bit 1 - cpMes valid
* bit 2 - halfCycle valid
* bit 3 - halfCycle subtracted from phase
*/
uint8_t trkStat = getub(buf, off + 46);
gpsd_log(&session->context->errout, LOG_DATA,
"%u:%u:%u prMes %f cpMes %f doMes %f locktime %u\n"
"cno %u prStdev %u cpStdev %u doStdev %u rtkStat %u\n",
gnssId, svId, freqId, prMes, cpMes, doMes, locktime,
cno, prStdev, cpStdev, doStdev, trkStat);
session->gpsdata.raw.meas[i].gnssid = gnssId;
switch (gnssId) {
case 0: /* GPS */
case 5: /* QZSS */
obs_code = "L1C"; /* u-blox calls this L1C/A */
break;
case 1: /* SBAS */
svId -= 100; /* adjust for RINEX 3 svid */
obs_code = "L1C"; /* u-blox calls this L1C/A */
break;
case 2: /* GALILEO */
obs_code = "L1B"; /* u-blox calls this E1OS */
break;
case 3: /* BeiDou */
obs_code = "L2I"; /* u-blox calls this B1I */
break;
default: /* huh? */
case 4: /* IMES. really? */
obs_code = ""; /* u-blox calls this L1 */
break;
case 6: /* GLONASS */
obs_code = "L1C"; /* u-blox calls this L1OF */
break;
}
(void)strlcpy(session->gpsdata.raw.meas[i].obs_code, obs_code,
sizeof(session->gpsdata.raw.meas[i].obs_code));
session->gpsdata.raw.meas[i].svid = svId;
session->gpsdata.raw.meas[i].freqid = freqId;
session->gpsdata.raw.meas[i].snr = cno;
session->gpsdata.raw.meas[i].satstat = trkStat;
if (trkStat & 1) {
/* prMes valid */
session->gpsdata.raw.meas[i].pseudorange = prMes;
} else {
session->gpsdata.raw.meas[i].pseudorange = NAN;
}
if ((trkStat & 2) && (5 >= cpStdev)) {
/* cpMes valid, RTKLIB uses 5 < cpStdev */
session->gpsdata.raw.meas[i].carrierphase = cpMes;
} else {
session->gpsdata.raw.meas[i].carrierphase = NAN;
}
session->gpsdata.raw.meas[i].doppler = doMes;
session->gpsdata.raw.meas[i].codephase = NAN;
session->gpsdata.raw.meas[i].deltarange = NAN;
session->gpsdata.raw.meas[i].locktime = locktime;
if (0 == locktime) {
/* possible slip */
session->gpsdata.raw.meas[i].lli = 2;
}
}
return RAW_IS;
}
/*
* Raw Subframes - UBX-RXM-SFRB
*/
static gps_mask_t ubx_rxm_sfrb(struct gps_device_t *session, unsigned char *buf)
{
unsigned int i, chan, svid;
uint32_t words[10];
chan = (unsigned int)getub(buf, 0);
svid = (unsigned int)getub(buf, 1);
gpsd_log(&session->context->errout, LOG_PROG,
"UBX_RXM_SFRB: %u %u\n", chan, svid);
/* UBX does all the parity checking, but still bad data gets through */
for (i = 0; i < 10; i++) {
words[i] = (uint32_t)getleu32(buf, 4 * i + 2) & 0xffffff;
}
return gpsd_interpret_subframe(session, svid, words);
}
static void ubx_msg_inf(struct gps_device_t *session, unsigned char *buf,
size_t data_len)
{
unsigned short msgid;
static char txtbuf[MAX_PACKET_LENGTH];
msgid = (unsigned short)((buf[2] << 8) | buf[3]);
if (data_len > MAX_PACKET_LENGTH - 1)
data_len = MAX_PACKET_LENGTH - 1;
(void)strlcpy(txtbuf, (char *)buf + UBX_PREFIX_LEN, sizeof(txtbuf));
txtbuf[data_len] = '\0';
switch (msgid) {
case UBX_INF_DEBUG:
gpsd_log(&session->context->errout, LOG_PROG, "UBX_INF_DEBUG: %s\n", txtbuf);
break;
case UBX_INF_TEST:
gpsd_log(&session->context->errout, LOG_PROG, "UBX_INF_TEST: %s\n", txtbuf);
break;
case UBX_INF_NOTICE:
gpsd_log(&session->context->errout, LOG_INF, "UBX_INF_NOTICE: %s\n", txtbuf);
break;
case UBX_INF_WARNING:
gpsd_log(&session->context->errout, LOG_WARN, "UBX_INF_WARNING: %s\n", txtbuf);
break;
case UBX_INF_ERROR:
gpsd_log(&session->context->errout, LOG_WARN, "UBX_INF_ERROR: %s\n", txtbuf);
break;
default:
break;
}
return;
}
gps_mask_t ubx_parse(struct gps_device_t * session, unsigned char *buf,
size_t len)
{
size_t data_len;
unsigned short msgid;