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
|
/* libgpsd_core.c -- direct access to GPSes on serial or USB devices. */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include "gpsd.h"
#if defined(PPS_ENABLE) && defined(TIOCMIWAIT)
#ifndef S_SPLINT_S
#include <pthread.h> /* pacifies OpenBSD's compiler */
#endif
#endif
int gpsd_switch_driver(struct gps_device_t *session, char* typename)
{
struct gps_type_t **dp;
/* make it idempotent */
if (session->device_type != NULL &&
strcmp(session->device_type->typename, typename) == 0)
return 0;
/*@ -compmempass @*/
for (dp = gpsd_drivers; *dp; dp++)
if (strcmp((*dp)->typename, typename) == 0) {
gpsd_report(3, "Selecting %s driver...\n", (*dp)->typename);
if (session->saved_baud == -1)
session->saved_baud = (int)cfgetispeed(&session->ttyset);
if (session->device_type != NULL && session->device_type->wrapup != NULL)
session->device_type->wrapup(session);
/*@i@*/session->device_type = *dp;
if (session->device_type->initializer)
session->device_type->initializer(session);
return 1;
}
gpsd_report(1, "invalid GPS type \"%s\".\n", typename);
return 0;
/*@ +compmempass @*/
}
void gpsd_clear_data(struct gps_device_t *session)
/* clear all data associated with a GPS session */
{
gps_clear_fix(&session->gpsdata.fix);
session->gpsdata.set &=~ (FIX_SET | DOP_SET);
session->gpsdata.hdop = NAN;
session->gpsdata.vdop = NAN;
session->gpsdata.pdop = NAN;
session->gpsdata.tdop = NAN;
session->gpsdata.gdop = NAN;
session->gpsdata.epe = NAN;
}
void gpsd_init(struct gps_device_t *session, struct gps_context_t *context, char *device)
/* initialize GPS polling */
{
/*@ -mayaliasunique @*/
strncpy(session->gpsdata.gps_device, device, PATH_MAX);
/*@ -mustfreeonly @*/
session->device_type = NULL; /* start by hunting packets */
session->rtcmtime = 0;
/*@ -temptrans @*/
session->context = context;
/*@ +temptrans @*/
/*@ +mayaliasunique @*/
/*@ +mustfreeonly @*/
gpsd_clear_data(session);
/* mark GPS fd closed */
session->gpsdata.gps_fd = -1;
/* necessary in case we start reading in the middle of a GPGSV sequence */
gpsd_zero_satellites(&session->gpsdata);
/* initialize things for the packet parser */
packet_reset(session);
}
void gpsd_deactivate(struct gps_device_t *session)
/* temporarily release the GPS device */
{
gpsd_report(1, "closing GPS=%s (%d)\n",
session->gpsdata.gps_device, session->gpsdata.gps_fd);
#ifdef NTPSHM_ENABLE
(void)ntpshm_free(session->context, session->shmTime);
session->shmTime = -1;
# ifdef PPS_ENABLE
(void)ntpshm_free(session->context, session->shmTimeP);
session->shmTimeP = -1;
# endif /* PPS_ENABLE */
#endif /* NTPSHM_ENABLE */
if (session->device_type != NULL && session->device_type->wrapup != NULL)
session->device_type->wrapup(session);
(void)gpsd_close(session);
}
#if defined(PPS_ENABLE) && defined(TIOCMIWAIT)
static void *gpsd_ppsmonitor(void *arg)
{
struct gps_device_t *session = (struct gps_device_t *)arg;
int cycle,duration, state = 0, laststate = -1, unchanged = 0;
struct timeval tv;
struct timeval pulse[2] = {{0,0},{0,0}};
/* wait for status change on the device's carrier-detect line */
while (ioctl(session->gpsdata.gps_fd, TIOCMIWAIT, TIOCM_CAR) == 0) {
(void)gettimeofday(&tv,NULL);
/*@ +ignoresigns */
if (ioctl(session->gpsdata.gps_fd, TIOCMGET, &state) != 0)
break;
/*@ -ignoresigns */
state = (int)((state & TIOCM_CAR) != 0);
if (state == laststate) {
if (++unchanged == 10) {
gpsd_report(1, "TIOCMIWAIT returns unchanged state, ppsmonitor terminates\n");
break;
}
} else {
gpsd_report(5, "carrier-detect on %s changed to %d\n",
session->gpsdata.gps_device, state);
laststate = state;
unchanged = 0;
}
/*@ +boolint @*/
if (session->gpsdata.newdata.mode > MODE_NO_FIX) {
/*
* The PPS pulse is normally a short pulse with a frequency of
* 1 Hz, and the UTC second is defined by the front edge. But we
* don't know the polarity of the pulse (different receivers
* emit different polarities). The duration variable is used to
* determine which way the pulse is going. The code assumes
* that the UTC second is changing when the signal has not
* been changing for at least 800ms, i.e. it assumes the duty
* cycle is at most 20%.
*/
#define timediff(x, y) (int)((x.tv_sec-y.tv_sec)*1000000+x.tv_usec-y.tv_usec)
cycle = timediff(tv, pulse[state]);
duration = timediff(tv, pulse[state == 0]);
#undef timediff
if (cycle > 999000 && cycle < 1001000 && duration > 800000)
(void)ntpshm_pps(session, &tv);
}
/*@ -boolint @*/
pulse[state] = tv;
}
return NULL;
}
#endif /* PPS_ENABLE */
int gpsd_activate(struct gps_device_t *session)
/* acquire a connection to the GPS device */
{
#if defined(PPS_ENABLE) && defined(TIOCMIWAIT)
pthread_t pt;
#endif /* PPS_ENABLE */
if (gpsd_open(session) < 0)
return -1;
else {
session->gpsdata.online = timestamp();
#ifdef SIRF_ENABLE
session->driver.sirf.satcounter = 0;
#endif /* SIRF_ENABLE */
session->char_counter = 0;
session->retry_counter = 0;
gpsd_report(1, "gpsd_activate: opened GPS (%d)\n", session->gpsdata.gps_fd);
// session->gpsdata.online = 0;
session->gpsdata.newdata.mode = MODE_NOT_SEEN;
session->gpsdata.status = STATUS_NO_FIX;
session->gpsdata.newdata.track = NAN;
session->gpsdata.separation = NAN;
#ifdef BINARY_ENABLE
session->mag_var = NAN;
#endif /* BINARY_ENABLE */
#ifdef NTPSHM_ENABLE
session->shmTime = ntpshm_alloc(session->context);
#if defined(PPS_ENABLE) && defined(TIOCMIWAIT)
if (session->shmTime >= 0 && session->context->shmTimePPS) {
if ((session->shmTimeP = ntpshm_alloc(session->context)) >= 0)
/*@i1@*/(void)pthread_create(&pt,NULL,gpsd_ppsmonitor,(void *)session);
}
#endif /* defined(PPS_ENABLE) && defined(TIOCMIWAIT) */
#endif /* NTPSHM_ENABLE */
return session->gpsdata.gps_fd;
}
}
#if defined(BINARY_ENABLE) || defined(RTCM_ENABLE) || defined(NTRIP_ENABLE)
/*
* Support for generic binary drivers. These functions dump NMEA for passing
* to the client in raw mode. They assume that (a) the public gps.h structure
* members are in a valid state, (b) that the private members hours, minutes,
* and seconds have also been filled in, (c) that if the private member
* mag_var is not NAN it is a magnetic variation in degrees that should be
* passed on, and (d) if the private member separation does not have the
* value NAN, it is a valid WGS84 geoidal separation in
* meters for the fix.
*/
static double degtodm(double a)
{
double m, t;
m = modf(a, &t);
t = floor(a) * 100 + m * 60;
return t;
}
/*@ -mustdefine @*/
void gpsd_position_fix_dump(struct gps_device_t *session,
/*@out@*/char bufp[], size_t len)
{
struct tm tm;
time_t intfixtime;
intfixtime = (time_t)session->gpsdata.newdata.time;
(void)gmtime_r(&intfixtime, &tm);
if (session->gpsdata.newdata.mode > 1) {
(void)snprintf(bufp, len,
"$GPGGA,%02d%02d%02d,%09.4f,%c,%010.4f,%c,%d,%02d,",
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
degtodm(fabs(session->gpsdata.newdata.latitude)),
((session->gpsdata.newdata.latitude > 0) ? 'N' : 'S'),
degtodm(fabs(session->gpsdata.newdata.longitude)),
((session->gpsdata.newdata.longitude > 0) ? 'E' : 'W'),
session->gpsdata.newdata.mode,
session->gpsdata.satellites_used);
if (isnan(session->gpsdata.hdop))
(void)strlcat(bufp, ",", len);
else
(void)snprintf(bufp+strlen(bufp), len-strlen(bufp),
"%.2f,",session->gpsdata.hdop);
if (isnan(session->gpsdata.newdata.altitude))
(void)strlcat(bufp, ",", len);
else
(void)snprintf(bufp+strlen(bufp), len-strlen(bufp),
"%.1f,M,", session->gpsdata.newdata.altitude);
if (isnan(session->gpsdata.separation))
(void)strlcat(bufp, ",", len);
else
(void)snprintf(bufp+strlen(bufp), len-strlen(bufp),
"%.3f,M,", session->gpsdata.separation);
if (isnan(session->mag_var))
(void)strlcat(bufp, ",", len);
else {
(void)snprintf(bufp+strlen(bufp),
len-strlen(bufp),
"%3.2f,", fabs(session->mag_var));
(void)strlcat(bufp, (session->mag_var > 0) ? "E": "W", len);
}
nmea_add_checksum(bufp);
}
}
/*@ +mustdefine @*/
static void gpsd_transit_fix_dump(struct gps_device_t *session,
char bufp[], size_t len)
{
struct tm tm;
time_t intfixtime;
intfixtime = (time_t)session->gpsdata.newdata.time;
(void)gmtime_r(&intfixtime, &tm);
/*@ -usedef @*/
(void)snprintf(bufp, len,
"$GPRMC,%02d%02d%02d,%c,%09.4f,%c,%010.4f,%c,%.4f,%.3f,%02d%02d%02d,,",
tm.tm_hour,
tm.tm_min,
tm.tm_sec,
session->gpsdata.status ? 'A' : 'V',
degtodm(fabs(session->gpsdata.newdata.latitude)),
((session->gpsdata.newdata.latitude > 0) ? 'N' : 'S'),
degtodm(fabs(session->gpsdata.newdata.longitude)),
((session->gpsdata.newdata.longitude > 0) ? 'E' : 'W'),
session->gpsdata.newdata.speed * MPS_TO_KNOTS,
session->gpsdata.newdata.track,
tm.tm_mday,
tm.tm_mon + 1,
tm.tm_year % 100);
/*@ +usedef @*/
nmea_add_checksum(bufp);
}
static void gpsd_binary_fix_dump(struct gps_device_t *session,
char bufp[], size_t len)
{
gpsd_position_fix_dump(session, bufp, len);
gpsd_transit_fix_dump(session, bufp + strlen(bufp), len - strlen(bufp));
}
static void gpsd_binary_satellite_dump(struct gps_device_t *session,
char bufp[], size_t len)
{
int i;
char *bufp2 = bufp;
bufp[0] = '\0';
for( i = 0 ; i < session->gpsdata.satellites; i++ ) {
if (i % 4 == 0) {
bufp += strlen(bufp);
bufp2 = bufp;
len -= snprintf(bufp, len,
"$GPGSV,%d,%d,%02d",
((session->gpsdata.satellites-1) / 4) + 1,
(i / 4) + 1,
session->gpsdata.satellites);
}
bufp += strlen(bufp);
if (i < session->gpsdata.satellites)
len -= snprintf(bufp, len,
",%02d,%02d,%03d,%02d",
session->gpsdata.PRN[i],
session->gpsdata.elevation[i],
session->gpsdata.azimuth[i],
session->gpsdata.ss[i]);
if (i % 4 == 3 || i == session->gpsdata.satellites-1) {
nmea_add_checksum(bufp2);
len -= 5;
}
}
#ifdef ZODIAC_ENABLE
if (session->packet_type == ZODIAC_PACKET && session->driver.zodiac.Zs[0] != 0) {
bufp += strlen(bufp);
bufp2 = bufp;
(void)strlcpy(bufp, "$PRWIZCH", len);
for (i = 0; i < ZODIAC_CHANNELS; i++) {
len -= snprintf(bufp+strlen(bufp), len,
",%02u,%X",
session->driver.zodiac.Zs[i],
session->driver.zodiac.Zv[i] & 0x0f);
}
nmea_add_checksum(bufp2);
}
#endif /* ZODIAC_ENABLE */
}
static void gpsd_binary_quality_dump(struct gps_device_t *session,
char bufp[], size_t len)
{
int i, j;
char *bufp2 = bufp;
(void)snprintf(bufp, len-strlen(bufp),
"$GPGSA,%c,%d,", 'A', session->gpsdata.newdata.mode);
j = 0;
for (i = 0; i < session->device_type->channels; i++) {
if (session->gpsdata.used[i]) {
bufp += strlen(bufp);
(void)snprintf(bufp, len-strlen(bufp),
"%02d,", session->gpsdata.PRN[i]);
j++;
}
}
for (i = j; i < session->device_type->channels; i++) {
bufp += strlen(bufp);
(void)strlcpy(bufp, ",", len);
}
bufp += strlen(bufp);
#define ZEROIZE(x) (isnan(x)!=0 ? 0.0 : x)
if (session->gpsdata.newdata.mode == MODE_NO_FIX)
(void)strlcat(bufp, ",,,", len);
else
(void)snprintf(bufp, len-strlen(bufp),
"%.1f,%.1f,%.1f*",
ZEROIZE(session->gpsdata.pdop),
ZEROIZE(session->gpsdata.hdop),
ZEROIZE(session->gpsdata.vdop));
nmea_add_checksum(bufp2);
bufp += strlen(bufp);
if (finite(session->gpsdata.newdata.eph)
|| finite(session->gpsdata.newdata.epv)
|| finite(session->gpsdata.epe)) {
/* output PGRME only if realistic */
(void)snprintf(bufp, len-strlen(bufp),
"$PGRME,%.2f,%.2f,%.2f",
ZEROIZE(session->gpsdata.newdata.eph),
ZEROIZE(session->gpsdata.newdata.epv),
ZEROIZE(session->gpsdata.epe));
nmea_add_checksum(bufp);
}
#undef ZEROIZE
}
#endif /* BINARY_ENABLE */
void gpsd_error_model(struct gps_device_t *session,
struct gps_fix_t *fix, struct gps_fix_t *oldfix)
/* compute errors and derived quantities */
{
/*
* Now we compute derived quantities. This is where the tricky error-
* modeling stuff goes. Presently we don't know how to derive
* time error.
*
* Some drivers set the position-error fields. Only the Zodiacs
* report speed error. Nobody reports track error or climb error.
*/
#define UERE_NO_DGPS 8.0 /* meters, 95% confidence */
#define UERE_WITH_DGPS 2.0 /* meters, 95% confidence */
double uere = (session->gpsdata.status == STATUS_DGPS_FIX ? UERE_WITH_DGPS : UERE_NO_DGPS);
/*
* Field reports match the theoretical prediction that
* expected time error should be half the resolution of
* the GPS clock, so we put the bound of the error
* in as a constant pending getting it from each driver.
*/
if (isnan(fix->ept)!=0)
fix->ept = 0.005;
/* Other error computations depend on having a valid fix */
if (fix->mode >= MODE_2D) {
if (isnan(fix->eph)!=0 && finite(session->gpsdata.hdop)!=0)
fix->eph = session->gpsdata.hdop * uere;
if ((fix->mode >= MODE_3D)
&& isnan(fix->epv)!=0 && finite(session->gpsdata.vdop)!=0)
fix->epv = session->gpsdata.vdop * uere;
if (isnan(session->gpsdata.epe)!=0 && finite(session->gpsdata.vdop)!=0)
session->gpsdata.epe = session->gpsdata.pdop * uere;
/*
* If we have a current fix and an old fix, and the packet handler
* didn't set the speed error and climb error members itself,
* try to compute them now.
*/
if (isnan(fix->eps)!=0 && fix->time > oldfix->time) {
if (oldfix->mode > MODE_NO_FIX && fix->mode > MODE_NO_FIX) {
double t = fix->time-oldfix->time;
double e = oldfix->eph + fix->eph;
fix->eps = e/t;
}
}
if ((fix->mode >= MODE_3D)
&& isnan(fix->epc)!=0 && fix->time > oldfix->time) {
if (oldfix->mode > MODE_3D && fix->mode > MODE_3D) {
double t = fix->time-oldfix->time;
double e = oldfix->epv + fix->epv;
/* if vertical uncertainties are zero this will be too */
fix->epc = e/t;
}
/*
* We compute track error solely from the position of this
* fix and the last one. The maximum track error, as seen from the
* position of last fix, is the angle subtended by the two
* most extreme possible error positions of the current fix.
* Let the position of the old fix be A and of the new fix B.
* We model the view from A as two right triangles ABC and ABD
* with BC and BD both having the length of the new fix's
* estimated error. adj = len(AB), opp = len(BC) = len(BD),
* hyp = len(AC) = len(AD). Yes, this normally leads to
* uncertainties near 180 when we're moving slowly.
*/
fix->epd = NAN;
if (oldfix->mode >= MODE_2D) {
double adj = earth_distance(
oldfix->latitude, oldfix->longitude,
fix->latitude, fix->longitude);
if (adj != 0) {
double opp = fix->eph;
double hyp = sqrt(adj*adj + opp*opp);
fix->epd = RAD_2_DEG * 2 * asin(opp / hyp);
}
}
}
}
/* save old fix for later error computations */
/*@ -mayaliasunique @*/
if (fix->mode >= MODE_2D)
(void)memcpy(oldfix, fix, sizeof(struct gps_fix_t));
/*@ +mayaliasunique @*/
}
gps_mask_t gpsd_poll(struct gps_device_t *session)
/* update the stuff in the scoreboard structure */
{
ssize_t newdata;
gps_clear_fix(&session->gpsdata.newdata);
if (session->inbuflen==0)
session->gpsdata.d_xmit_time = timestamp();
/* can we get a full packet from the device? */
if (session->device_type) {
newdata = session->device_type->get_packet(session);
session->gpsdata.d_xmit_time = timestamp();
} else {
newdata = packet_get(session);
session->gpsdata.d_xmit_time = timestamp();
gpsd_report(3,
"packet sniff finds type %d\n",
session->packet_type);
if (session->packet_type != BAD_PACKET) {
switch (session->packet_type) {
#ifdef SIRF_ENABLE
case SIRF_PACKET:
(void)gpsd_switch_driver(session, "SiRF binary");
break;
#endif /* SIRF_ENABLE */
#ifdef TSIP_ENABLE
case TSIP_PACKET:
(void)gpsd_switch_driver(session, "Trimble TSIP");
break;
#endif /* TSIP_ENABLE */
#ifdef NMEA_ENABLE
case NMEA_PACKET:
(void)gpsd_switch_driver(session, "Generic NMEA");
break;
#endif /* NMEA_ENABLE */
#ifdef ZODIAC_ENABLE
case ZODIAC_PACKET:
(void)gpsd_switch_driver(session, "Zodiac binary");
break;
#endif /* ZODIAC_ENABLE */
#ifdef EVERMORE_ENABLE
case EVERMORE_PACKET:
(void)gpsd_switch_driver(session, "EverMore binary");
break;
#endif /* EVERMORE_ENABLE */
#ifdef ITALK_ENABLE
case ITALK_PACKET:
(void)gpsd_switch_driver(session, "iTalk binary");
break;
#endif /* ITALK_ENABLE */
#ifdef RTCM104_ENABLE
case RTCM_PACKET:
(void)gpsd_switch_driver(session, "RTCM104");
break;
#endif /* RTCM104_ENABLE */
}
} else if (!gpsd_next_hunt_setting(session))
return ERROR_SET;
}
/* update the scoreboard structure from the GPS */
gpsd_report(7, "GPS sent %d new characters\n", newdata);
if (newdata == -1) { /* read error */
session->gpsdata.online = 0;
return 0;
} else if (newdata == 0) { /* no new data */
if (session->device_type != NULL && timestamp()>session->gpsdata.online+session->device_type->cycle+1){
gpsd_report(3, "GPS is offline (%lf sec since data)\n",
timestamp() - session->gpsdata.online);
session->gpsdata.online = 0;
return 0;
} else
return ONLINE_SET;
} else if (session->outbuflen == 0) { /* got new data, but no packet */
gpsd_report(8, "New data, not yet a packet\n");
return ONLINE_SET;
} else {
gps_mask_t received, dopmask = 0;
session->gpsdata.online = timestamp();
/*@ -nullstate @*/
if (session->gpsdata.raw_hook)
session->gpsdata.raw_hook(&session->gpsdata,
(char *)session->outbuffer,
(size_t)session->outbuflen, 2);
/*@ -nullstate @*/
session->gpsdata.sentence_length = session->outbuflen;
session->gpsdata.d_recv_time = timestamp();
/* Get data from current packet into the newdata structure */
if (session->device_type != NULL && session->device_type->parse_packet!=NULL)
received = session->device_type->parse_packet(session);
else
received = 0; /* it was all done in the packet getter */
/* Clear fix data at start of cycle */
if ((received & CYCLE_START_SET)!=0)
gpsd_clear_data(session);
/*
* Compute fix-quality data from the satellite positions.
* This may be overridden by DOPs reported from the packet we just got.
*/
if (session->gpsdata.newdata.mode > MODE_NO_FIX
&& (session->gpsdata.set & SATELLITE_SET) != 0
&& session->gpsdata.satellites > 0)
dopmask = dop(&session->gpsdata);
/* Merge in the data from the current packet. */
gps_merge_fix(&session->gpsdata.fix, received, &session->gpsdata.newdata);
session->gpsdata.set = ONLINE_SET | dopmask | received;
/* count good fixes */
if (session->gpsdata.status > STATUS_NO_FIX)
session->context->fixcnt++;
session->gpsdata.d_decode_time = timestamp();
/* also copy the sentence up to clients in raw mode */
if (session->packet_type == NMEA_PACKET)
session->gpsdata.raw_hook(&session->gpsdata,
(char *)session->outbuffer,
strlen((char *)session->outbuffer), 1);
else {
char buf2[MAX_PACKET_LENGTH*3+2];
buf2[0] = '\0';
#ifdef RTCM104_ENABLE
if ((session->gpsdata.set & RTCM_SET) != 0)
rtcm_dump(session,
buf2+strlen(buf2),
(sizeof(buf2)-strlen(buf2)));
else
#endif /* RTCM104_ENABLE */
#ifdef BINARY_ENABLE
if ((session->gpsdata.set & LATLON_SET) != 0)
gpsd_binary_fix_dump(session,
buf2+strlen(buf2),
(sizeof(buf2)-strlen(buf2)));
if ((session->gpsdata.set & HDOP_SET) != 0)
gpsd_binary_quality_dump(session,
buf2 + strlen(buf2),
(sizeof(buf2)-strlen(buf2)));
if ((session->gpsdata.set & SATELLITE_SET) != 0)
gpsd_binary_satellite_dump(session,
buf2 + strlen(buf2),
(sizeof(buf2)-strlen(buf2)));
#endif /* BINARY_ENABLE */
if (buf2[0] != '\0') {
gpsd_report(3, "<= GPS: %s", buf2);
if (session->gpsdata.raw_hook)
session->gpsdata.raw_hook(&session->gpsdata,
buf2, strlen(buf2), 1);
}
}
dgnss_report(session);
return session->gpsdata.set;
}
}
void gpsd_wrap(struct gps_device_t *session)
/* end-of-session wrapup */
{
gpsd_deactivate(session);
}
void gpsd_zero_satellites(/*@out@*/struct gps_data_t *out)
{
(void)memset(out->PRN, 0, sizeof(out->PRN));
(void)memset(out->elevation, 0, sizeof(out->elevation));
(void)memset(out->azimuth, 0, sizeof(out->azimuth));
(void)memset(out->ss, 0, sizeof(out->ss));
out->satellites = 0;
}
char /*@ observer @*/ *gpsd_hexdump(const void *binbuf, size_t binbuflen)
{
static char hexbuf[MAX_PACKET_LENGTH*2+1];
size_t i;
size_t len = (size_t)((binbuflen > MAX_PACKET_LENGTH) ? MAX_PACKET_LENGTH : binbuflen);
const char *ibuf = (const char *)binbuf;
memset(hexbuf, 0, sizeof(hexbuf));
for (i = 0; i < len; i++) {
(void)snprintf(hexbuf + (2 * i), 3, "%02x", (unsigned int)(ibuf[i]&0xff));
}
return hexbuf;
}
#ifdef BINARY_ENABLE
/*@ -usedef @*/
void gpsd_interpret_subframe(struct gps_device_t *session,unsigned int words[])
/* extract leap-second from RTCM-104 subframe data */
{
/*
* Heavy black magic begins here!
*
* A description of how to decode these bits is at
* <http://home-2.worldonline.nl/~samsvl/nav2eu.htm>
*
* We're after subframe 4 page 18 word 9, the leap year correction.
* We assume that the chip is presenting clean data that has been
* parity-checked.
*
* To date this code has been tested only on SiRFs. It's in the
* core because other chipsets reporting only GPS time but with
* the capability to read subframe data may want it.
*/
int i;
unsigned int pageid, subframe, leap;
gpsd_report(4,
"50B (raw): %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
words[0], words[1], words[2], words[3], words[4],
words[5], words[6], words[7], words[8], words[9]);
/*
* Mask off the high 2 bits and shift out the 6 parity bits.
* Once we've filtered, we can ignore the TEL and HOW words.
* We don't need to check parity here, the SiRF chipset does
* that and throws a subframe error if the parity is wrong.
*/
for (i = 0; i < 10; i++)
words[i] = (words[i] & 0x3fffffff) >> 6;
/*
* "First, throw away everything that doesn't start with 8b or
* 74. more correctly the first byte should be 10001011. If
* it's 01110100, then you have a subframe with inverted
* polarity and each byte needs to be xored against 0xff to
* remove the inversion."
*/
words[0] &= 0xff0000;
if (words[0] != 0x8b0000 && words[0] != 0x740000)
return;
if (words[0] == 0x740000)
for (i = 1; i < 10; i++)
words[i] ^= 0xffffff;
/*
* The subframe ID is in the Hand Over Word (page 80)
*/
subframe = ((words[1] >> 2) & 0x07);
/* we're not interested in anything but subframe 4 */
if (subframe != 4)
return;
/*
* Pages 66-76a,80 of ICD-GPS-200 are the subframe structures.
* Subframe 4 page 18 is on page 74.
* See page 105 for the mapping between magic SVIDs and pages.
*/
pageid = (words[2] & 0x3F0000) >> 16;
gpsd_report(2, "Subframe 4 SVID is %d\n", pageid);
if (pageid == 56) { /* magic SVID for page 18 */
/* once we've filtered, we can ignore the TEL and HOW words */
gpsd_report(2, "50B: SF=%d %06x %06x %06x %06x %06x %06x %06x %06x\n",
subframe,
words[2], words[3], words[4], words[5],
words[6], words[7], words[8], words[9]);
leap = (words[8] & 0xff0000) >> 16;
/*
* On SiRFs, there appears to be some bizarre bug that
* randomly causes this field to come out two's-complemented.
* This could very well be a general problem; work around it.
* At the current expected rate of issuing leap-seconds this
* kluge won't bite until about 2070, by which time the
* vendors had better have fixed their damn firmware...
*
* Carl: ...I am unsure, and suggest you
* experiment. The D30 bit is in bit 30 of the 32-bit
* word (next to MSB), and should signal an inverted
* value when it is one coming over the air. But if
* the bit is set and the word decodes right without
* inversion, then we properly caught it. Cases where
* you see subframe 6 rather than 1 means we should
* have done the inversion but we did not. Some other
* things you can watch for: in any subframe, the
* second word (HOW word) should have last 2 parity
* bits 00 -- there are bits within the rest of the
* word that are set as required to ensure that. The
* same goes for word 10. That means that both words
* 1 and 3 (the words that immediately follow words 10
* and 2, respectively) should always be uninverted.
* In these cases, the D29 and D30 from the previous
* words, found in the two MSBs of the word, should
* show 00 -- if they don't then you may find an
* unintended inversion due to noise on the data link.
*/
if (leap > 128)
leap ^= 0xff;
gpsd_report(2, "leap-seconds is %d\n", leap);
session->context->leap_seconds = (int)leap;
session->context->valid |= LEAP_SECOND_VALID;
}
}
/*@ +usedef @*/
#endif /* BINARY_ENABLE */
|