summaryrefslogtreecommitdiff
path: root/ubx.c
blob: 8e87a0f963956d46c4133040aafcbc8a600ec35b (plain)
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
/* $Id$
 *
 * UBX driver
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>

#include "gpsd_config.h"
#include "gpsd.h"
#if defined(UBX_ENABLE) && defined(BINARY_ENABLE)
#include "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
 */

static bool have_port_configuration = false;
static unsigned char original_port_settings[20];
static unsigned char sbas_in_use;

	bool 		ubx_write(int fd, unsigned int msg_class, unsigned int msg_id, unsigned char *msg, unsigned short data_len);
	gps_mask_t 	ubx_parse(struct gps_device_t *session, unsigned char *buf, size_t len);
	void 		ubx_catch_model(struct gps_device_t *session, unsigned char *buf, size_t 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_dop(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_svinfo(struct gps_device_t *session, unsigned char *buf, size_t data_len);
static	void		ubx_msg_sbas(unsigned char *buf);
static	void       	ubx_msg_inf(unsigned char *buf, size_t data_len);

/**
 * Navigation solution message
 */
static gps_mask_t
ubx_msg_nav_sol(struct gps_device_t *session, unsigned char *buf, size_t data_len)
{
    unsigned short gw;
    unsigned int tow, flags;
    double epx, epy, epz, evx, evy, evz;
    unsigned char navmode;
    gps_mask_t mask;
    double t;

    if (data_len != 52)
	return 0;

    flags = (unsigned int)getub(buf, 11);
    mask =  ONLINE_SET;
    if ((flags & (UBX_SOL_VALID_WEEK |UBX_SOL_VALID_TIME)) != 0){
	tow = getleul(buf, 0);
	gw = (unsigned short)getlesw(buf, 8);
	session->driver.ubx.gps_week = gw;

	t = gpstime_to_unix((int)session->driver.ubx.gps_week, tow/1000.0) - session->context->leap_seconds;
	session->gpsdata.sentence_time = t;
	session->gpsdata.fix.time = t;
	mask |= TIME_SET;
#ifdef NTPSHM_ENABLE
	/* TODO overhead */
	if (session->context->enable_ntpshm)
	    (void)ntpshm_put(session, session->gpsdata.sentence_time);
#endif
    }

    epx = (double)(getlesl(buf, 12)/100.0);
    epy = (double)(getlesl(buf, 16)/100.0);
    epz = (double)(getlesl(buf, 20)/100.0);
    evx = (double)(getlesl(buf, 28)/100.0);
    evy = (double)(getlesl(buf, 32)/100.0);
    evz = (double)(getlesl(buf, 36)/100.0);
    ecef_to_wgs84fix(&session->gpsdata, epx, epy, epz, evx, evy, evz);
    mask |= LATLON_SET | ALTITUDE_SET | SPEED_SET | TRACK_SET | CLIMB_SET;
    session->gpsdata.fix.eph = (double)(getlesl(buf, 24)/100.0);
    session->gpsdata.fix.eps = (double)(getlesl(buf, 40)/100.0);
    session->gpsdata.pdop = (double)(getleuw(buf, 44)/100.0);
    session->gpsdata.satellites_used = (int)getub(buf, 47);
    mask |= PDOP_SET ;

    navmode = getub(buf, 10);
    switch (navmode){
    case UBX_MODE_TMONLY:
    case UBX_MODE_3D:
	session->gpsdata.fix.mode = MODE_3D;
	break;
    case UBX_MODE_2D:
    case UBX_MODE_DR:	    /* consider this too as 2D */
    case UBX_MODE_GPSDR:    /* XXX DR-aided GPS may be valid 3D */
	session->gpsdata.fix.mode = MODE_2D;
	break;
    default:
	session->gpsdata.fix.mode = MODE_NO_FIX;
    }

    if ((flags & UBX_SOL_FLAG_DGPS) != 0)
	session->gpsdata.status = STATUS_DGPS_FIX;
    else if (session->gpsdata.fix.mode != MODE_NO_FIX)
	session->gpsdata.status = STATUS_FIX;

    mask |= MODE_SET | STATUS_SET | CYCLE_START_SET | USED_SET ;

    return mask;
}

/**
 * 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;

    session->gpsdata.gdop = (double)(getleuw(buf, 4)/100.0);
    session->gpsdata.pdop = (double)(getleuw(buf, 6)/100.0);
    session->gpsdata.tdop = (double)(getleuw(buf, 8)/100.0);
    session->gpsdata.vdop = (double)(getleuw(buf, 10)/100.0);
    session->gpsdata.hdop = (double)(getleuw(buf, 12)/100.0);

    return DOP_SET;
}

/**
 * GPS Leap Seconds
 */
static gps_mask_t
ubx_msg_nav_timegps(struct gps_device_t *session, unsigned char *buf, size_t data_len)
{
    unsigned int gw, tow, flags;
    double t;

    if (data_len != 16)
	return 0;

    tow = getleul(buf, 0);
    gw = (uint)getlesw(buf, 8);
    if (gw > session->driver.ubx.gps_week)
	session->driver.ubx.gps_week = gw;

    flags = (unsigned int)getub(buf, 11);
    if ((flags & 0x7) != 0)
	session->context->leap_seconds = (int)getub(buf, 10);

    t = gpstime_to_unix((int)session->driver.ubx.gps_week, tow/1000.0) - session->context->leap_seconds;
    session->gpsdata.sentence_time = session->gpsdata.fix.time = t;

    return TIME_SET | ONLINE_SET;
}

/**
 * GPS Satellite Info
 */
static gps_mask_t
ubx_msg_nav_svinfo(struct gps_device_t *session, unsigned char *buf, size_t data_len)
{
    unsigned int i, j, tow, nchan, nsv, st;

    if (data_len < 152 ) {
	gpsd_report(LOG_PROG, "runt svinfo (datalen=%zd)\n", data_len);
	return 0;
    }
    tow = getleul(buf, 0);
//    session->gpsdata.sentence_time = gpstime_to_unix(gps_week, tow) 
//				- session->context->leap_seconds;
    /*@ +charint @*/
    nchan = getub(buf, 4);
    if (nchan > MAXCHANNELS){
	gpsd_report(LOG_WARN, "Invalid NAV SVINFO message, >%d reported",MAXCHANNELS);
	return 0;
    }
    /*@ -charint @*/
    gpsd_zero_satellites(&session->gpsdata);
    nsv = 0;
    for (i = j = st = 0; i < nchan; i++) {
	unsigned int off = 8 + 12 * i;
	if((int)getub(buf, off+4) == 0) continue; /* LEA-5H seems to have a bug reporting sats it does not see or hear*/
	session->gpsdata.PRN[j]		= (int)getub(buf, off+1);
	session->gpsdata.ss[j]		= (int)getub(buf, off+4);
	session->gpsdata.elevation[j]	= (int)getsb(buf, off+5);
	session->gpsdata.azimuth[j]	= (int)getlesw(buf, off+6);
	if(session->gpsdata.PRN[j])
		st++;
	/*@ -predboolothers */
	if (getub(buf, off+2) & 0x01)
	    session->gpsdata.used[nsv++] = session->gpsdata.PRN[j];
	if (session->gpsdata.PRN[j] == (int)sbas_in_use)
	    session->gpsdata.used[nsv++] = session->gpsdata.PRN[j];
	/*@ +predboolothers */
	j++;
    }
    session->gpsdata.satellites = (int)st;
    session->gpsdata.satellites_used = (int)nsv;
    return SATELLITE_SET | USED_SET;
}

/*
 * SBAS Info
 */
static void
ubx_msg_sbas(unsigned char *buf)
{
#ifdef UBX_SBAS_DEBUG
    unsigned int i, nsv;

    gpsd_report(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_report(LOG_WARN, "SBAS info on SV: %d\n", (int)getub(buf, off));
    }
#endif
/* 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 */
    sbas_in_use = getub(buf, 4);
}

static void
ubx_msg_inf(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+6, MAX_PACKET_LENGTH); txtbuf[data_len] = '\0';
    switch (msgid) {
	case UBX_INF_DEBUG:
	    gpsd_report(LOG_PROG, "UBX_INF_DEBUG: %s\n", txtbuf);
	    break;
	case UBX_INF_TEST:
	    gpsd_report(LOG_PROG, "UBX_INF_TEST: %s\n", txtbuf);
	    break;
	case UBX_INF_NOTICE:
	    gpsd_report(LOG_INF, "UBX_INF_NOTICE: %s\n", txtbuf);
	    break;
	case UBX_INF_WARNING:
	    gpsd_report(LOG_WARN, "UBX_INF_WARNING: %s\n", txtbuf);
	    break;
	case UBX_INF_ERROR:
	    gpsd_report(LOG_WARN, "UBX_INF_ERROR: %s\n", txtbuf);
	    break;
	default:
	    break;
    }
    return ;
}

/*@ +charint @*/
gps_mask_t ubx_parse(struct gps_device_t *session, unsigned char *buf, size_t len)
{
    size_t data_len;
    unsigned short msgid;
    gps_mask_t mask = 0;
    int i;

    if (len < 6)    /* the packet at least contains a head of six bytes */
	return 0;

    /* extract message id and length */
    msgid = (buf[2] << 8) | buf[3];
    data_len = (size_t)getlesw(buf, 4);
    switch (msgid)
    {
	case UBX_NAV_POSECEF:
	    gpsd_report(LOG_IO, "UBX_NAV_POSECEF\n");
	    break;
	case UBX_NAV_POSLLH:
	    gpsd_report(LOG_IO, "UBX_NAV_POSLLH\n");
	    break;
	case UBX_NAV_STATUS:
	    gpsd_report(LOG_IO, "UBX_NAV_STATUS\n");
	    break;
	case UBX_NAV_DOP:
	    gpsd_report(LOG_PROG, "UBX_NAV_DOP\n");
	    mask = ubx_msg_nav_dop(session, &buf[6], data_len);
	    break;
	case UBX_NAV_SOL:
	    gpsd_report(LOG_PROG, "UBX_NAV_SOL\n");
	    mask = ubx_msg_nav_sol(session, &buf[6], data_len);
	    break;
	case UBX_NAV_POSUTM:
	    gpsd_report(LOG_IO, "UBX_NAV_POSUTM\n");
	    break;
	case UBX_NAV_VELECEF:
	    gpsd_report(LOG_IO, "UBX_NAV_VELECEF\n");
	    break;
	case UBX_NAV_VELNED:
	    gpsd_report(LOG_IO, "UBX_NAV_VELNED\n");
	    break;
	case UBX_NAV_TIMEGPS:
	    gpsd_report(LOG_PROG, "UBX_NAV_TIMEGPS\n");
	    mask = ubx_msg_nav_timegps(session, &buf[6], data_len);
	    break;
	case UBX_NAV_TIMEUTC:
	    gpsd_report(LOG_IO, "UBX_NAV_TIMEUTC\n");
	    break;
	case UBX_NAV_CLOCK:
	    gpsd_report(LOG_IO, "UBX_NAV_CLOCK\n");
	    break;
	case UBX_NAV_SVINFO:
	    gpsd_report(LOG_PROG, "UBX_NAV_SVINFO\n");
	    mask = ubx_msg_nav_svinfo(session, &buf[6], data_len);
	    break;
	case UBX_NAV_DGPS:
	    gpsd_report(LOG_IO, "UBX_NAV_DGPS\n");
	    break;
	case UBX_NAV_SBAS:
	    gpsd_report(LOG_IO, "UBX_NAV_SBAS\n");
	    ubx_msg_sbas(&buf[6]);
	    break;
	case UBX_NAV_EKFSTATUS:
	    gpsd_report(LOG_IO, "UBX_NAV_EKFSTATUS\n");
	    break;

	case UBX_RXM_RAW:
	    gpsd_report(LOG_IO, "UBX_RXM_RAW\n");
	    break;
	case UBX_RXM_SFRB:
	    gpsd_report(LOG_IO, "UBX_RXM_SFRB\n");
	    break;
	case UBX_RXM_SVSI:
	    gpsd_report(LOG_PROG, "UBX_RXM_SVSI\n");
	    break;
	case UBX_RXM_ALM:
	    gpsd_report(LOG_IO, "UBX_RXM_ALM\n");
	    break;
	case UBX_RXM_EPH:
	    gpsd_report(LOG_IO, "UBX_RXM_EPH\n");
	    break;
	case UBX_RXM_POSREQ:
	    gpsd_report(LOG_IO, "UBX_RXM_POSREQ\n");
	    break;

	case UBX_MON_SCHED:
	    gpsd_report(LOG_IO, "UBX_MON_SCHED\n");
	    break;
	case UBX_MON_IO:
	    gpsd_report(LOG_IO, "UBX_MON_IO\n");
	    break;
	case UBX_MON_IPC:
	    gpsd_report(LOG_IO, "UBX_MON_IPC\n");
	    break;
	case UBX_MON_VER:
	    gpsd_report(LOG_IO, "UBX_MON_VER\n");
	    break;
	case UBX_MON_EXCEPT:
	    gpsd_report(LOG_IO, "UBX_MON_EXCEPT\n");
	    break;
	case UBX_MON_MSGPP:
	    gpsd_report(LOG_IO, "UBX_MON_MSGPP\n");
	    break;
	case UBX_MON_RXBUF:
	    gpsd_report(LOG_IO, "UBX_MON_RXBUF\n");
	    break;
	case UBX_MON_TXBUF:
	    gpsd_report(LOG_IO, "UBX_MON_TXBUF\n");
	    break;
	case UBX_MON_HW:
	    gpsd_report(LOG_IO, "UBX_MON_HW\n");
	    break;
	case UBX_MON_USB:
	    gpsd_report(LOG_IO, "UBX_MON_USB\n");
	    break;

	case UBX_INF_DEBUG:
	    /* FALLTHROUGH */
	case UBX_INF_TEST:
	    /* FALLTHROUGH */
	case UBX_INF_NOTICE:
	    /* FALLTHROUGH */
	case UBX_INF_WARNING:
	    /* FALLTHROUGH */
	case UBX_INF_ERROR:
	    ubx_msg_inf(buf, data_len);
	    break;

	case UBX_TIM_TP:
	    gpsd_report(LOG_IO, "UBX_TIM_TP\n");
	    break;
	case UBX_TIM_TM:
	    gpsd_report(LOG_IO, "UBX_TIM_TM\n");
	    break;
	case UBX_TIM_TM2:
	    gpsd_report(LOG_IO, "UBX_TIM_TM2\n");
	    break;
	case UBX_TIM_SVIN:
	    gpsd_report(LOG_IO, "UBX_TIM_SVIN\n");
	    break;

	case UBX_CFG_PRT:
	    gpsd_report(LOG_IO, "UBX_CFG_PRT\n");
	    for(i=6;i<26;i++)
		original_port_settings[i-6] = buf[i];				/* copy the original port settings */
	    buf[14+6] &= ~0x02;							/* turn off NMEA output on this port */
	    (void)ubx_write(session->gpsdata.gps_fd, 0x06, 0x00, &buf[6], 20);	/* send back with all other settings intact */
	    have_port_configuration = true;
	    break;

	case UBX_ACK_NAK:
	    gpsd_report(LOG_IO, "UBX_ACK_NAK, class: %02x, id: %02x\n", buf[6], buf[7]);
	    break;
	case UBX_ACK_ACK:
	    gpsd_report(LOG_IO, "UBX_ACK_ACK, class: %02x, id: %02x\n", buf[6], buf[7]);
	    break;

    default:
	gpsd_report(LOG_WARN,
	    "UBX: unknown packet id 0x%04hx (length %zd) %s\n",
	    msgid, len, gpsd_hexdump_wrapper(buf, len, LOG_WARN));
    }

    if (mask)
	(void)snprintf(session->gpsdata.tag, sizeof(session->gpsdata.tag),
	   "0x%04hx", msgid);

    return mask | ONLINE_SET;
}
/*@ -charint @*/

static gps_mask_t parse_input(struct gps_device_t *session)
{
    gps_mask_t st;

    if (session->packet.type == UBX_PACKET){
	st = ubx_parse(session, session->packet.outbuffer, session->packet.outbuflen);
	session->gpsdata.driver_mode = 1;
	return st;
#ifdef NMEA_ENABLE
    } else if (session->packet.type == NMEA_PACKET) {
	st = nmea_parse((char *)session->packet.outbuffer, session);
	session->gpsdata.driver_mode = 0;
	return st;
#endif /* NMEA_ENABLE */
    } else
	return 0;
}

void ubx_catch_model(struct gps_device_t *session, unsigned char *buf, size_t len)
{
    /*@ +charint */
    unsigned char *ip = &buf[19];
    unsigned char *op = (unsigned char *)session->subtype;
    size_t end = ((len - 19) < 63)?(len - 19):63;
    size_t i;

    for(i=0;i<end;i++) {
	if((*ip == 0x00) || (*ip == '*')) {
	    *op = 0x00;
	    break;
	}
	*(op++) = *(ip++);
    }
    /*@ -charint */
}

bool ubx_write(int fd, unsigned int msg_class, unsigned int msg_id, unsigned char *msg, unsigned short data_len) {
   unsigned char CK_A, CK_B;
   unsigned char head_tail[8];
   ssize_t i, count;
   bool      ok;

   /*@ -type @*/
   head_tail[0] = 0xb5;
   head_tail[1] = 0x62;

   /* calculate CRC */
   CK_A = CK_B = 0;
   head_tail[2] = msg_class;
   head_tail[3] = msg_id;
   head_tail[4] = data_len & 0xff;
   head_tail[5] = (data_len >> 8) & 0xff;

   for (i = 2; i < 6; i++) {
	CK_A += head_tail[i];
	CK_B += CK_A;
   }

   /*@ -nullderef @*/
   for (i = 0; i < data_len; i++) {
	CK_A += msg[i];
	CK_B += CK_A;
   }

   head_tail[6] = CK_A;
   head_tail[7] = CK_B;
   /*@ +type @*/

   gpsd_report(LOG_IO,
       "=> GPS: UBX class: %02x, id: %02x, len: %d, data:%s, crc: %02x%02x\n",
       msg_class, msg_id, data_len,
	       gpsd_hexdump_wrapper(msg, (size_t)data_len, LOG_IO),
       CK_A, CK_B);

   assert(msg != NULL || data_len == 0);
   count = write(fd, head_tail, 6);
   (void)tcdrain(fd);
   if(data_len)
       /*@ -nullpass @*/
       count += write(fd, msg, (size_t)data_len);
       /*@ +nullpass @*/
   (void)tcdrain(fd);
   count += write(fd, &head_tail[6], 2);

   ok = (count == ((ssize_t)data_len + 8));
   (void)tcdrain(fd);
   /*@ +nullderef @*/
   return(0);
}

#ifdef ALLOW_RECONFIGURE
static void ubx_configure(struct gps_device_t *session, unsigned int seq)
{
    unsigned char msg[32];

    gpsd_report(LOG_IO, "UBX configure: %d\n",seq);

    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x00, NULL, 0);	/* get this port's settings */

    /*@ -type @*/
    msg[0] = 0x03; /* SBAS mode enabled, accept testbed mode */
    msg[1] = 0x07; /* SBAS usage: range, differential corrections and integrity */
    msg[2] = 0x03; /* use the maximun search range: 3 channels */
    msg[3] = 0x00; /* PRN numbers to search for all set to 0 => auto scan */
    msg[4] = 0x00;
    msg[5] = 0x00;
    msg[6] = 0x00;
    msg[7] = 0x00;
    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x16, msg, 8);

    msg[0] = 0x01; /* class */
    msg[1] = 0x04; /* msg id  = UBX_NAV_DOP */
    msg[2] = 0x01; /* rate */
    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x01, msg, 3);
    msg[0] = 0x01; /* class */
    msg[1] = 0x06; /* msg id  = NAV-SOL */
    msg[2] = 0x01; /* rate */
    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x01, msg, 3);
    msg[0] = 0x01; /* class */
    msg[1] = 0x20; /* msg id  = UBX_NAV_TIMEGPS */
    msg[2] = 0x01; /* rate */
    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x01, msg, 3);
    msg[0] = 0x01; /* class */
    msg[1] = 0x30; /* msg id  = NAV-SVINFO */
    msg[2] = 0x0a; /* rate */
    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x01, msg, 3);
    msg[0] = 0x01; /* class */
    msg[1] = 0x32; /* msg id  = NAV-SBAS */
    msg[2] = 0x0a; /* rate */
    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x01, msg, 3);
    /*@ +type @*/

}

static void ubx_revert(struct gps_device_t *session)
{
    /*@ -type @*/
    unsigned char msg[4] = {
	0x00, 0x00,	/* hotstart */
	0x01,		/* controlled software reset */
	0x00};		/* reserved */
    /*@ +type @*/

    gpsd_report(LOG_IO, "UBX revert\n");

    /* Reverting all in one fast and reliable reset */
    (void)ubx_write(session->gpsdata.gps_fd, 0x06, 0x04, msg, 4); /* CFG-RST */
}
#endif /* ALLOW_RECONFIGURE */

static void ubx_nmea_mode(struct gps_device_t *session, int mode)
{
    int i;
    unsigned char buf[20];

    if(!have_port_configuration)
	return;

    /*@ +charint -usedef @*/
    for(i=0;i<22;i++)
	buf[i] = original_port_settings[i];	/* copy the original port settings */
    if(buf[0] == 0x01)				/* set baudrate on serial port only */
	putlelong(buf, 8, session->gpsdata.baudrate);

    if (mode == 0) {
	buf[14] &= ~0x01;			/* turn off UBX output on this port */
	buf[14] |=  0x02;			/* turn on NMEA output on this port */
    } else {
	buf[14] &= ~0x02;			/* turn off NMEA output on this port */
	buf[14] |=  0x01;			/* turn on UBX output on this port */
    }
    /*@ -charint +usedef @*/
    (void)ubx_write(session->gpsdata.gps_fd, 0x06u, 0x00, &buf[6], 20);	/* send back with all other settings intact */
}

static bool ubx_speed(struct gps_device_t *session, speed_t speed)
{
    int i;
    unsigned char buf[20];

    /*@ +charint -usedef -compdef */
    if((!have_port_configuration) || (buf[0] != 0x01))	/* set baudrate on serial port only */
	return false;

    for(i=0;i<22;i++)
	buf[i] = original_port_settings[i];	/* copy the original port settings */
    putlelong(buf, 8, speed);
    (void)ubx_write(session->gpsdata.gps_fd, 0x06, 0x00, &buf[6], 20);	/* send back with all other settings intact */
    /*@ -charint +usedef +compdef */
    return true;
}

/* This is everything we export */
struct gps_type_t ubx_binary = {
    .type_name        = "uBlox UBX",    /* Full name of type */
    .trigger          = NULL,           /* Response string that identifies device (not active) */
    .channels         = 50,             /* Number of satellite channels supported by the device */
    .control_send     = NULL,		/* no control sender yet */
    .probe_detect     = NULL,           /* Startup-time device detector */
    .probe_wakeup     = NULL,           /* Wakeup to be done before each baud hunt */
    .probe_subtype    = NULL,           /* Initialize the device and get subtype */
#ifdef ALLOW_RECONFIGURE
    .configurator     = ubx_configure,  /* Enable what reports we need */
#endif /* ALLOW_RECONFIGURE */
    .get_packet       = generic_get,    /* Packet getter (using default routine) */
    .parse_packet     = parse_input,    /* Parse message packets */
    .rtcm_writer      = NULL,           /* RTCM handler (using default routine) */
    .speed_switcher   = ubx_speed,      /* Speed (baudrate) switch */
    .mode_switcher    = ubx_nmea_mode,  /* Switch to NMEA mode */
    .rate_switcher    = NULL,           /* Message delivery rate switcher */
    .cycle_chars      = -1,             /* Number of chars per report cycle */
#ifdef ALLOW_RECONFIGURE
    .revert           = ubx_revert,     /* Undo the actions of .configurator */
#endif /* ALLOW_RECONFIGURE */
    .wrapup           = NULL,           /* Puts device back to original settings */
    .cycle            = 1               /* Number of updates per second */
};
#endif /* defined(UBX_ENABLE) && defined(BINARY_ENABLE) */