2 * refclock_zyfer - clock driver for the Zyfer GPSTarplus Clock
4 * Harlan Stenn, Jan 2002
11 #if defined(REFCLOCK) && defined(CLOCK_ZYFER)
15 #include "ntp_refclock.h"
16 #include "ntp_stdlib.h"
17 #include "ntp_unixtime.h"
18 #include "ntp_calgps.h"
23 #if defined(HAVE_TERMIOS_H)
25 #elif defined(HAVE_SYS_TERMIOS_H)
26 # include <sys/termios.h>
28 #ifdef HAVE_SYS_PPSCLOCK_H
29 # include <sys/ppsclock.h>
33 * This driver provides support for the TOD serial port of a Zyfer GPStarplus.
34 * This clock also provides PPS as well as IRIG outputs.
35 * Precision is limited by the serial driver, etc.
37 * If I was really brave I'd hack/generalize the serial driver to deal
38 * with arbitrary on-time characters. This clock *begins* the stream with
39 * `!`, the on-time character, and the string is *not* EOL-terminated.
41 * Configure the beast for 9600, 8N1. While I see leap-second stuff
42 * in the documentation, the published specs on the TOD format only show
43 * the seconds going to '59'. I see no leap warning in the TOD format.
45 * The clock sends the following message once per second:
47 * !TIME,2002,017,07,59,32,2,4,1
48 * YYYY DDD HH MM SS m T O
52 * DDD 001-366 Day of Year
55 * SS 00-59 Second (probably 00-60)
59 * 3 = LGPS time (Local GPS)
60 * 4 = LUTC time (Local UTC)
62 * T 4-9 Time Figure Of Merit:
69 * O 0-4 Operation Mode:
79 * Interface definitions
81 #define DEVICE "/dev/zyfer%d" /* device name and unit */
82 #define SPEED232 B9600 /* uart speed (9600 baud) */
83 #define PRECISION (-20) /* precision assumed (about 1 us) */
84 #define REFID "GPS\0" /* reference ID */
85 #define DESCRIPTION "Zyfer GPStarplus" /* WRU */
87 #define LENZYFER 29 /* timecode length */
90 * Unit control structure
93 u_char Rcvbuf[LENZYFER + 1];
94 u_char polled; /* poll message flag */
96 l_fp tstamp; /* timestamp of last poll */
101 * Function prototypes
103 static int zyfer_start (int, struct peer *);
104 static void zyfer_shutdown (int, struct peer *);
105 static void zyfer_receive (struct recvbuf *);
106 static void zyfer_poll (int, struct peer *);
111 struct refclock refclock_zyfer = {
112 zyfer_start, /* start up driver */
113 zyfer_shutdown, /* shut down driver */
114 zyfer_poll, /* transmit poll message */
115 noentry, /* not used (old zyfer_control) */
116 noentry, /* initialize driver (not used) */
117 noentry, /* not used (old zyfer_buginfo) */
118 NOFLAGS /* not used */
123 * zyfer_start - open the devices and initialize data for processing
131 register struct zyferunit *up;
132 struct refclockproc *pp;
138 * Something like LDISC_ACTS that looked for ! would be nice...
140 snprintf(device, sizeof(device), DEVICE, unit);
141 fd = refclock_open(device, SPEED232, LDISC_RAW);
145 msyslog(LOG_NOTICE, "zyfer(%d) fd: %d dev <%s>", unit, fd, device);
148 * Allocate and initialize unit structure
150 up = emalloc(sizeof(struct zyferunit));
151 memset(up, 0, sizeof(struct zyferunit));
153 pp->io.clock_recv = zyfer_receive;
154 pp->io.srcclock = peer;
157 if (!io_addclock(&pp->io)) {
166 * Initialize miscellaneous variables
168 peer->precision = PRECISION;
169 pp->clockdesc = DESCRIPTION;
170 memcpy((char *)&pp->refid, REFID, 4);
172 up->polled = 0; /* May not be needed... */
179 * zyfer_shutdown - shut down the clock
187 register struct zyferunit *up;
188 struct refclockproc *pp;
193 io_closeclock(&pp->io);
200 * zyfer_receive - receive data from the serial interface
204 struct recvbuf *rbufp
207 register struct zyferunit *up;
208 struct refclockproc *pp;
210 int tmode; /* Time mode */
211 int tfom; /* Time Figure Of Merit */
212 int omode; /* Operation mode */
219 peer = rbufp->recv_peer;
222 p = (u_char *) &rbufp->recv_space;
225 * - if *rbufp->recv_space is !
226 * - - call refclock_gtlin to get things going
228 * else stuff it on the end of lastcode
229 * If we don't have LENZYFER bytes
230 * - wait for more data
231 * Crack the beast, and if it's OK, process it.
233 * We use refclock_gtlin() because we might use LDISC_CLK.
235 * Under FreeBSD, we get the ! followed by two 14-byte packets.
238 if (pp->lencode >= LENZYFER)
243 pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode,
248 memcpy(pp->a_lastcode + pp->lencode, p, rbufp->recv_length);
249 pp->lencode += rbufp->recv_length;
250 pp->a_lastcode[pp->lencode] = '\0';
253 if (pp->lencode < LENZYFER)
256 record_clock_stats(&peer->srcadr, pp->a_lastcode);
259 * We get down to business, check the timecode format and decode
260 * its contents. If the timecode has invalid length or is not in
261 * proper format, we declare bad format and exit.
264 if (pp->lencode != LENZYFER) {
265 refclock_report(peer, CEVNT_BADTIME);
270 * Timecode sample: "!TIME,2002,017,07,59,32,2,4,1"
272 if (sscanf(pp->a_lastcode, "!TIME,%4d,%3d,%2d,%2d,%2d,%d,%d,%d",
273 &pp->year, &pp->day, &pp->hour, &pp->minute, &pp->second,
274 &tmode, &tfom, &omode) != 8) {
275 refclock_report(peer, CEVNT_BADREPLY);
280 refclock_report(peer, CEVNT_BADTIME);
284 /* Should we make sure tfom is 4? */
287 pp->leap = LEAP_NOTINSYNC;
291 /* treat GPS input as subject to era warps */
295 tsdoy.year = pp->year;
296 tsdoy.yearday = pp->day;
297 tsdoy.hour = pp->hour;
298 tsdoy.minute = pp->minute;
299 tsdoy.second = pp->second;
301 /* note: We kept 'month' and 'monthday' zero above. That forces
302 * day-of-year based calculation now:
304 tsntp = gpsntp_from_calendar(&tsdoy, tfrac);
305 tfrac = ntpfp_from_ntpdatum(&tsntp);
306 refclock_process_offset(pp, tfrac, pp->lastrec, pp->fudgetime1);
309 * Good place for record_clock_stats()
315 refclock_receive(peer);
321 * zyfer_poll - called by the transmit procedure
329 register struct zyferunit *up;
330 struct refclockproc *pp;
333 * We don't really do anything here, except arm the receiving
334 * side to capture a sample and check for timeouts.
339 refclock_report(peer, CEVNT_TIMEOUT);
347 int refclock_zyfer_bs;
348 #endif /* REFCLOCK */