2 * refclock_nmea.c - clock driver for an NMEA GPS CLOCK
3 * Michael Petry Jun 20, 1994
4 * based on refclock_heathn.c
10 #if defined(SYS_WINNT)
12 #define close closesocket
15 #if defined(REFCLOCK) && defined(CLOCK_NMEA)
19 #include "ntp_unixtime.h"
20 #include "ntp_refclock.h"
21 #include "ntp_stdlib.h"
27 # ifdef HAVE_TIMEPPS_H
30 # ifdef HAVE_SYS_TIMEPPS_H
31 # include <sys/timepps.h>
34 #endif /* HAVE_PPSAPI */
37 * This driver supports the NMEA GPS Receiver with
39 * Protype was refclock_trak.c, Thanks a lot.
41 * The receiver used spits out the NMEA sentences for boat navigation.
42 * And you thought it was an information superhighway. Try a raging river
43 * filled with rapids and whirlpools that rip away your data and warp time.
45 * If HAVE_PPSAPI is defined code to use the PPSAPI will be compiled in.
46 * On startup if initialization of the PPSAPI fails, it will fall back
47 * to the "normal" timestamps.
49 * The PPSAPI part of the driver understands fudge flag2 and flag3. If
50 * flag2 is set, it will use the clear edge of the pulse. If flag3 is
51 * set, kernel hardpps is enabled.
53 * GPS sentences other than RMC (the default) may be enabled by setting
54 * the relevent bits of 'mode' in the server configuration line
55 * server 127.127.20.x mode X
57 * bit 0 - enables RMC (1)
58 * bit 1 - enables GGA (2)
59 * bit 2 - enables GLL (4)
60 * multiple sentences may be selected
67 # define DEVICE "COM%d:" /* COM 1 - 3 supported */
69 # define DEVICE "/dev/gps%d" /* name of radio device */
71 #define SPEED232 B4800 /* uart speed (4800 bps) */
72 #define PRECISION (-9) /* precision assumed (about 2 ms) */
73 #define PPS_PRECISION (-20) /* precision assumed (about 1 us) */
74 #define REFID "GPS\0" /* reference id */
75 #define DESCRIPTION "NMEA GPS Clock" /* who we are */
76 #define NANOSECOND 1000000000 /* one second (ns) */
77 #define RANGEGATE 500000 /* range gate (ns) */
79 #define LENNMEA 75 /* min timecode length */
82 * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
85 static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
86 static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
89 * Unit control structure
92 int pollcnt; /* poll message counter */
93 int polled; /* Hand in a sample? */
94 l_fp tstamp; /* timestamp of last poll */
96 struct timespec ts; /* last timestamp */
97 pps_params_t pps_params; /* pps parameters */
98 pps_info_t pps_info; /* last pps data */
99 pps_handle_t handle; /* pps handlebars */
100 #endif /* HAVE_PPSAPI */
104 * Function prototypes
106 static int nmea_start P((int, struct peer *));
107 static void nmea_shutdown P((int, struct peer *));
109 static void nmea_control P((int, struct refclockstat *, struct
110 refclockstat *, struct peer *));
111 static int nmea_ppsapi P((struct peer *, int, int));
112 static int nmea_pps P((struct nmeaunit *, l_fp *));
113 #endif /* HAVE_PPSAPI */
114 static void nmea_receive P((struct recvbuf *));
115 static void nmea_poll P((int, struct peer *));
116 static void gps_send P((int, const char *, struct peer *));
117 static char *field_parse P((char *, int));
122 struct refclock refclock_nmea = {
123 nmea_start, /* start up driver */
124 nmea_shutdown, /* shut down driver */
125 nmea_poll, /* transmit poll message */
127 nmea_control, /* fudge control */
129 noentry, /* fudge control */
130 #endif /* HAVE_PPSAPI */
131 noentry, /* initialize driver */
132 noentry, /* buginfo */
133 NOFLAGS /* not used */
137 * nmea_start - open the GPS devices and initialize data for processing
145 register struct nmeaunit *up;
146 struct refclockproc *pp;
151 * Open serial port. Use CLK line discipline, if available.
153 (void)sprintf(device, DEVICE, unit);
155 fd = refclock_open(device, SPEED232, LDISC_CLK);
160 * Allocate and initialize unit structure
162 up = (struct nmeaunit *)emalloc(sizeof(struct nmeaunit));
167 memset((char *)up, 0, sizeof(struct nmeaunit));
169 pp->io.clock_recv = nmea_receive;
170 pp->io.srcclock = (caddr_t)peer;
173 if (!io_addclock(&pp->io)) {
178 pp->unitptr = (caddr_t)up;
181 * Initialize miscellaneous variables
183 peer->precision = PRECISION;
184 pp->clockdesc = DESCRIPTION;
185 memcpy((char *)&pp->refid, REFID, 4);
187 gps_send(pp->io.fd,"$PMOTG,RMC,0000*1D\r\n", peer);
191 * Start the PPSAPI interface if it is there. Default to use
192 * the assert edge and do not enable the kernel hardpps.
194 if (time_pps_create(fd, &up->handle) < 0) {
197 "refclock_nmea: time_pps_create failed: %m");
200 return(nmea_ppsapi(peer, 0, 0));
203 #endif /* HAVE_PPSAPI */
207 * nmea_shutdown - shut down a GPS clock
215 register struct nmeaunit *up;
216 struct refclockproc *pp;
219 up = (struct nmeaunit *)pp->unitptr;
222 time_pps_destroy(up->handle);
223 #endif /* HAVE_PPSAPI */
224 io_closeclock(&pp->io);
230 * nmea_control - fudge control
234 int unit, /* unit (not used */
235 struct refclockstat *in, /* input parameters (not uded) */
236 struct refclockstat *out, /* output parameters (not used) */
237 struct peer *peer /* peer structure pointer */
240 struct refclockproc *pp;
243 nmea_ppsapi(peer, pp->sloppyclockflag & CLK_FLAG2,
244 pp->sloppyclockflag & CLK_FLAG3);
253 struct peer *peer, /* peer structure pointer */
254 int enb_clear, /* clear enable */
255 int enb_hardpps /* hardpps enable */
258 struct refclockproc *pp;
263 up = (struct nmeaunit *)pp->unitptr;
264 if (time_pps_getcap(up->handle, &capability) < 0) {
266 "refclock_nmea: time_pps_getcap failed: %m");
269 memset(&up->pps_params, 0, sizeof(pps_params_t));
271 up->pps_params.mode = capability & PPS_CAPTURECLEAR;
273 up->pps_params.mode = capability & PPS_CAPTUREASSERT;
274 if (!up->pps_params.mode) {
276 "refclock_nmea: invalid capture edge %d",
280 up->pps_params.mode |= PPS_TSFMT_TSPEC;
281 if (time_pps_setparams(up->handle, &up->pps_params) < 0) {
283 "refclock_nmea: time_pps_setparams failed: %m");
287 if (time_pps_kcbind(up->handle, PPS_KC_HARDPPS,
288 up->pps_params.mode & ~PPS_TSFMT_TSPEC,
289 PPS_TSFMT_TSPEC) < 0) {
291 "refclock_nmea: time_pps_kcbind failed: %m");
296 peer->precision = PPS_PRECISION;
300 time_pps_getparams(up->handle, &up->pps_params);
302 "refclock_ppsapi: capability 0x%x version %d mode 0x%x kern %d\n",
303 capability, up->pps_params.api_version,
304 up->pps_params.mode, enb_hardpps);
312 * Get PPSAPI timestamps.
314 * Return 0 on failure and 1 on success.
323 struct timespec timeout, ts;
328 * Convert the timespec nanoseconds field to ntp l_fp units.
334 memcpy(&pps_info, &up->pps_info, sizeof(pps_info_t));
335 if (time_pps_fetch(up->handle, PPS_TSFMT_TSPEC, &up->pps_info,
338 if (up->pps_params.mode & PPS_CAPTUREASSERT) {
339 if (pps_info.assert_sequence ==
340 up->pps_info.assert_sequence)
342 ts = up->pps_info.assert_timestamp;
343 } else if (up->pps_params.mode & PPS_CAPTURECLEAR) {
344 if (pps_info.clear_sequence ==
345 up->pps_info.clear_sequence)
347 ts = up->pps_info.clear_timestamp;
351 if ((up->ts.tv_sec == ts.tv_sec) && (up->ts.tv_nsec == ts.tv_nsec))
355 tstmp.l_ui = ts.tv_sec + JAN_1970;
356 dtemp = ts.tv_nsec * FRAC / 1e9;
357 tstmp.l_uf = (u_int32)dtemp;
361 #endif /* HAVE_PPSAPI */
364 * nmea_receive - receive data from the serial interface
368 struct recvbuf *rbufp
371 register struct nmeaunit *up;
372 struct refclockproc *pp;
378 /* Use these variables to hold data until we decide its worth keeping */
379 char rd_lastcode[BMAX];
384 * Initialize pointers and read the timecode and timestamp
386 peer = (struct peer *)rbufp->recv_srcclock;
388 up = (struct nmeaunit *)pp->unitptr;
389 rd_lencode = (u_short)refclock_gtlin(rbufp, rd_lastcode, BMAX, &rd_tmp);
392 * There is a case that a <CR><LF> gives back a "blank" line
399 printf("nmea: gpsread %d %s\n", rd_lencode,
404 * We check the timecode format and decode its contents. The
405 * we only care about a few of them. The most important being
407 * $GPRMC,hhmmss,a,fddmm.xx,n,dddmmm.xx,w,zz.z,yyy.,ddmmyy,dd,v*CC
408 * For Magellan (ColorTrak) GLL probably datum (order of sentences)
409 * also mode (0,1,2,3) select sentence ANY/ALL, RMC, GGA, GLL
410 * $GPGLL,3513.8385,S,14900.7851,E,232420.594,A*21
411 * $GPGGA,232420.59,3513.8385,S,14900.7851,E,1,05,3.4,00519,M,,,,*3F
413 * $GPRMC,232418.19,A,3513.8386,S,14900.7853,E,00.0,000.0,121199,12.,E*77
425 if(strncmp(cp,"$GPRMC",6)==0) {
428 else if(strncmp(cp,"$GPGGA",6)==0) {
431 else if(strncmp(cp,"$GPGLL",6)==0) {
434 else if(strncmp(cp,"$GPXXX",6)==0) {
441 /* See if I want to process this message type */
442 if ( ((peer->ttl == 0) && (cmdtype != GPRMC))
443 || ((peer->ttl != 0) && !(cmdtype & peer->ttl)) )
446 pp->lencode = rd_lencode;
447 strcpy(pp->a_lastcode,rd_lastcode);
450 pp->lastrec = up->tstamp = rd_tmp;
455 printf("nmea: timecode %d %s\n", pp->lencode,
460 /* Grab field depending on clock string type */
464 * Test for synchronization. Check for quality byte.
466 dp = field_parse(cp,2);
468 pp->leap = LEAP_NOTINSYNC;
470 pp->leap = LEAP_NOWARNING;
472 /* Now point at the time field */
473 dp = field_parse(cp,1);
479 * Test for synchronization. Check for quality byte.
481 dp = field_parse(cp,6);
483 pp->leap = LEAP_NOTINSYNC;
485 pp->leap = LEAP_NOWARNING;
487 /* Now point at the time field */
488 dp = field_parse(cp,1);
494 * Test for synchronization. Check for quality byte.
496 dp = field_parse(cp,6);
498 pp->leap = LEAP_NOTINSYNC;
500 pp->leap = LEAP_NOWARNING;
502 /* Now point at the time field */
503 dp = field_parse(cp,5);
515 * Check time code format of NMEA
518 if( !isdigit((int)dp[0]) ||
519 !isdigit((int)dp[1]) ||
520 !isdigit((int)dp[2]) ||
521 !isdigit((int)dp[3]) ||
522 !isdigit((int)dp[4]) ||
525 refclock_report(peer, CEVNT_BADREPLY);
531 * Convert time and check values.
533 pp->hour = ((dp[0] - '0') * 10) + dp[1] - '0';
534 pp->minute = ((dp[2] - '0') * 10) + dp[3] - '0';
535 pp->second = ((dp[4] - '0') * 10) + dp[5] - '0';
536 /* Default to 0 milliseconds, if decimal convert milliseconds in
537 one, two or three digits
541 if (isdigit((int)dp[7])) {
542 pp->nsec = (dp[7] - '0') * 100000000;
543 if (isdigit((int)dp[8])) {
544 pp->nsec += (dp[8] - '0') * 10000000;
545 if (isdigit((int)dp[9])) {
546 pp->nsec += (dp[9] - '0') * 1000000;
552 if (pp->hour > 23 || pp->minute > 59 || pp->second > 59
553 || pp->nsec > 1000000000) {
554 refclock_report(peer, CEVNT_BADTIME);
560 * Convert date and check values.
562 if (cmdtype==GPRMC) {
563 dp = field_parse(cp,9);
565 day = (day * 10) + dp[1] - '0';
567 month = (month * 10) + dp[3] - '0';
568 pp->year = dp[4] - '0';
569 pp->year = (pp->year * 10) + dp[5] - '0';
573 time_t tt = time(NULL);
574 struct tm * t = gmtime(&tt);
576 month = t->tm_mon + 1;
577 pp->year= t->tm_year;
580 if (month < 1 || month > 12 || day < 1) {
581 refclock_report(peer, CEVNT_BADTIME);
585 /* Hmmmm this will be a nono for 2100,2200,2300 but I don't think I'll be here */
586 /* good thing that 2000 is a leap year */
587 /* pp->year will be 00-99 if read from GPS, 00-> (years since 1900) from tm_year */
589 if (day > day1tab[month - 1]) {
590 refclock_report(peer, CEVNT_BADTIME);
593 for (i = 0; i < month - 1; i++)
596 if (day > day2tab[month - 1]) {
597 refclock_report(peer, CEVNT_BADTIME);
600 for (i = 0; i < month - 1; i++)
608 * If the PPSAPI is working, rather use its timestamps.
609 * assume that the PPS occurs on the second so blow any msec
611 if (nmea_pps(up, &rd_tmp) == 1) {
612 pp->lastrec = up->tstamp = rd_tmp;
615 #endif /* HAVE_PPSAPI */
618 * Process the new sample in the median filter and determine the
619 * reference clock offset and dispersion. We use lastrec as both
620 * the reference time and receive time, in order to avoid being
621 * cute, like setting the reference time later than the receive
622 * time, which may cause a paranoid protocol module to chuck out
626 if (!refclock_process(pp)) {
627 refclock_report(peer, CEVNT_BADTIME);
634 * Only go on if we had been polled.
639 pp->lastref = pp->lastrec;
640 refclock_receive(peer);
642 /* If we get here - what we got from the clock is OK, so say so */
643 refclock_report(peer, CEVNT_NOMINAL);
645 record_clock_stats(&peer->srcadr, pp->a_lastcode);
650 * nmea_poll - called by the transmit procedure
652 * We go to great pains to avoid changing state here, since there may be
653 * more than one eavesdropper receiving the same timecode.
661 register struct nmeaunit *up;
662 struct refclockproc *pp;
665 up = (struct nmeaunit *)pp->unitptr;
666 if (up->pollcnt == 0)
667 refclock_report(peer, CEVNT_TIMEOUT);
674 * usually nmea_receive can get a timestamp every second
677 gps_send(pp->io.fd,"$PMOTG,RMC,0000*1D\r\n", peer);
682 * gps_send(fd,cmd, peer) Sends a command to the GPS receiver.
683 * as gps_send(fd,"rqts,u\r", peer);
685 * We don't currently send any data, but would like to send
686 * RTCM SC104 messages for differential positioning. It should
687 * also give us better time. Without a PPS output, we're
688 * Just fooling ourselves because of the serial code paths
699 if (write(fd, cmd, strlen(cmd)) == -1) {
700 refclock_report(peer, CEVNT_FAULT);
713 for (tp = cp; *tp != '\0'; tp++) {
722 int refclock_nmea_bs;
723 #endif /* REFCLOCK */