2 * refclock_leitch - clock driver for the Leitch CSD-5300 Master Clock
9 #if defined(REFCLOCK) && defined(CLOCK_LEITCH)
13 #include "ntp_refclock.h"
14 #include "ntp_unixtime.h"
21 #if defined(LEITCHCLK)
22 #include <sys/clkdefs.h>
23 #endif /* LEITCHCLK */
26 #include "ntp_stdlib.h"
30 * Driver for Leitch CSD-5300 Master Clock System
40 * TIME: <CR>/HHMMSS <CR>/HHMMSS <CR>/HHMMSS <CR>/
41 * second bondaried on the stop bit of the <CR>
42 * second boundaries at '/' above.
43 * STATUS: G (good), D (diag fail), T (time not provided) or
44 * P (last phone update failed)
46 #define MAXUNITS 1 /* max number of LEITCH units */
47 #define LEITCHREFID "ATOM" /* reference id */
48 #define LEITCH_DESCRIPTION "Leitch: CSD 5300 Master Clock System Driver"
49 #define LEITCH232 "/dev/leitch%d" /* name of radio device */
50 #define SPEED232 B300 /* uart speed (300 baud) */
52 #define leitch_send(A,M) \
53 if (debug) fprintf(stderr,"write leitch %s\n",M); \
54 if ((write(A->leitchio.fd,M,sizeof(M)) < 0)) {\
56 fprintf(stderr, "leitch_send: unit %d send failed\n", A->unit); \
58 msyslog(LOG_ERR, "leitch_send: unit %d send failed %m",A->unit);}
60 #define leitch_send(A,M) \
61 if ((write(A->leitchio.fd,M,sizeof(M)) < 0)) {\
62 msyslog(LOG_ERR, "leitch_send: unit %d send failed %m",A->unit);}
72 * LEITCH unit control structure
76 struct refclockio leitchio;
99 static void leitch_init P((void));
100 static int leitch_start P((int, struct peer *));
101 static void leitch_shutdown P((int, struct peer *));
102 static void leitch_poll P((int, struct peer *));
103 static void leitch_control P((int, struct refclockstat *, struct refclockstat *, struct peer *));
104 #define leitch_buginfo noentry
105 static void leitch_receive P((struct recvbuf *));
106 static void leitch_process P((struct leitchunit *));
108 static void leitch_timeout P((struct peer *));
110 static int leitch_get_date P((struct recvbuf *, struct leitchunit *));
111 static int leitch_get_time P((struct recvbuf *, struct leitchunit *, int));
112 static int days_per_year P((int));
114 static struct leitchunit leitchunits[MAXUNITS];
115 static u_char unitinuse[MAXUNITS];
116 static u_char stratumtouse[MAXUNITS];
117 static u_int32 refid[MAXUNITS];
119 static char days_in_month [] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
124 struct refclock refclock_leitch = {
125 leitch_start, leitch_shutdown, leitch_poll,
126 leitch_control, leitch_init, leitch_buginfo, NOFLAGS
130 * leitch_init - initialize internal leitch driver data
137 memset((char*)leitchunits, 0, sizeof(leitchunits));
138 memset((char*)unitinuse, 0, sizeof(unitinuse));
139 for (i = 0; i < MAXUNITS; i++)
140 memcpy((char *)&refid[i], LEITCHREFID, 4);
144 * leitch_shutdown - shut down a LEITCH clock
154 fprintf(stderr, "leitch_shutdown()\n");
159 * leitch_poll - called by the transmit procedure
167 struct leitchunit *leitch;
169 /* start the state machine rolling */
173 fprintf(stderr, "leitch_poll()\n");
175 if (unit >= MAXUNITS) {
180 leitch = &leitchunits[unit];
182 if (leitch->state != STATE_IDLE) {
183 /* reset and wait for next poll */
185 leitch->state = STATE_IDLE;
187 leitch_send(leitch,"D\r");
188 leitch->state = STATE_DATE;
195 struct refclockstat *in,
196 struct refclockstat *out,
197 struct peer *passed_peer
200 if (unit >= MAXUNITS) {
202 "leitch_control: unit %d invalid", unit);
207 if (in->haveflags & CLK_HAVEVAL1)
208 stratumtouse[unit] = (u_char)(in->fudgeval1);
209 if (in->haveflags & CLK_HAVEVAL2)
210 refid[unit] = in->fudgeval2;
211 if (unitinuse[unit]) {
214 peer = (&leitchunits[unit])->peer;
215 peer->stratum = stratumtouse[unit];
216 peer->refid = refid[unit];
221 memset((char *)out, 0, sizeof (struct refclockstat));
222 out->type = REFCLK_ATOM_LEITCH;
223 out->haveflags = CLK_HAVEVAL1 | CLK_HAVEVAL2;
224 out->fudgeval1 = (int32)stratumtouse[unit];
225 out->fudgeval2 = refid[unit];
226 out->p_lastcode = "";
227 out->clockdesc = LEITCH_DESCRIPTION;
232 * leitch_start - open the LEITCH devices and initialize data for processing
240 struct leitchunit *leitch;
245 * Check configuration info.
247 if (unit >= MAXUNITS) {
248 msyslog(LOG_ERR, "leitch_start: unit %d invalid", unit);
252 if (unitinuse[unit]) {
253 msyslog(LOG_ERR, "leitch_start: unit %d in use", unit);
260 (void) sprintf(leitchdev, LEITCH232, unit);
261 fd232 = open(leitchdev, O_RDWR, 0777);
264 "leitch_start: open of %s: %m", leitchdev);
268 leitch = &leitchunits[unit];
269 memset((char*)leitch, 0, sizeof(*leitch));
271 #if defined(HAVE_SYSV_TTYS)
273 * System V serial line parameters (termio interface)
276 { struct termio ttyb;
277 if (ioctl(fd232, TCGETA, &ttyb) < 0) {
279 "leitch_start: ioctl(%s, TCGETA): %m", leitchdev);
282 ttyb.c_iflag = IGNBRK|IGNPAR|ICRNL;
284 ttyb.c_cflag = SPEED232|CS8|CLOCAL|CREAD;
285 ttyb.c_lflag = ICANON;
286 ttyb.c_cc[VERASE] = ttyb.c_cc[VKILL] = '\0';
287 if (ioctl(fd232, TCSETA, &ttyb) < 0) {
289 "leitch_start: ioctl(%s, TCSETA): %m", leitchdev);
293 #endif /* HAVE_SYSV_TTYS */
294 #if defined(HAVE_TERMIOS)
296 * POSIX serial line parameters (termios interface)
298 * The LEITCHCLK option provides timestamping at the driver level.
299 * It requires the tty_clk streams module.
301 { struct termios ttyb, *ttyp;
304 if (tcgetattr(fd232, ttyp) < 0) {
306 "leitch_start: tcgetattr(%s): %m", leitchdev);
309 ttyp->c_iflag = IGNBRK|IGNPAR|ICRNL;
311 ttyp->c_cflag = SPEED232|CS8|CLOCAL|CREAD;
312 ttyp->c_lflag = ICANON;
313 ttyp->c_cc[VERASE] = ttyp->c_cc[VKILL] = '\0';
314 if (tcsetattr(fd232, TCSANOW, ttyp) < 0) {
316 "leitch_start: tcsetattr(%s): %m", leitchdev);
319 if (tcflush(fd232, TCIOFLUSH) < 0) {
321 "leitch_start: tcflush(%s): %m", leitchdev);
325 #endif /* HAVE_TERMIOS */
327 #if defined(LEITCHCLK)
328 if (ioctl(fd232, I_PUSH, "clk") < 0)
330 "leitch_start: ioctl(%s, I_PUSH, clk): %m", leitchdev);
331 if (ioctl(fd232, CLK_SETSTR, "\n") < 0)
333 "leitch_start: ioctl(%s, CLK_SETSTR): %m", leitchdev);
334 #endif /* LEITCHCLK */
336 #if defined(HAVE_BSD_TTYS)
338 * 4.3bsd serial line parameters (sgttyb interface)
340 * The LEITCHCLK option provides timestamping at the driver level.
341 * It requires the tty_clk line discipline and 4.3bsd or later.
343 { struct sgttyb ttyb;
344 #if defined(LEITCHCLK)
345 int ldisc = CLKLDISC;
346 #endif /* LEITCHCLK */
348 if (ioctl(fd232, TIOCGETP, &ttyb) < 0) {
350 "leitch_start: ioctl(%s, TIOCGETP): %m", leitchdev);
353 ttyb.sg_ispeed = ttyb.sg_ospeed = SPEED232;
354 #if defined(LEITCHCLK)
355 ttyb.sg_erase = ttyb.sg_kill = '\r';
358 ttyb.sg_erase = ttyb.sg_kill = '\0';
359 ttyb.sg_flags = EVENP|ODDP|CRMOD;
360 #endif /* LEITCHCLK */
361 if (ioctl(fd232, TIOCSETP, &ttyb) < 0) {
363 "leitch_start: ioctl(%s, TIOCSETP): %m", leitchdev);
366 #if defined(LEITCHCLK)
367 if (ioctl(fd232, TIOCSETD, &ldisc) < 0) {
369 "leitch_start: ioctl(%s, TIOCSETD): %m",leitchdev);
372 #endif /* LEITCHCLK */
374 #endif /* HAVE_BSD_TTYS */
377 * Set up the structures
381 leitch->state = STATE_IDLE;
382 leitch->fudge1 = 15; /* 15ms */
384 leitch->leitchio.clock_recv = leitch_receive;
385 leitch->leitchio.srcclock = (caddr_t) leitch;
386 leitch->leitchio.datalen = 0;
387 leitch->leitchio.fd = fd232;
388 if (!io_addclock(&leitch->leitchio)) {
393 * All done. Initialize a few random peer variables, then
397 peer->stratum = stratumtouse[unit];
398 peer->refid = refid[unit];
403 * Something broke; abandon ship.
411 * leitch_receive - receive data from the serial interface on a leitch
416 struct recvbuf *rbufp
419 struct leitchunit *leitch = (struct leitchunit *)rbufp->recv_srcclock;
423 fprintf(stderr, "leitch_recieve(%*.*s)\n",
424 rbufp->recv_length, rbufp->recv_length,
427 if (rbufp->recv_length != 7)
428 return; /* The date is return with a trailing newline,
431 switch (leitch->state) {
432 case STATE_IDLE: /* unexpected, discard and resync */
435 if (!leitch_get_date(rbufp,leitch)) {
436 leitch->state = STATE_IDLE;
439 leitch_send(leitch,"T\r");
442 fprintf(stderr, "%u\n",leitch->yearday);
444 leitch->state = STATE_TIME1;
447 if (!leitch_get_time(rbufp,leitch,1)) {
449 if (!clocktime(leitch->yearday,leitch->hour,leitch->minute,
450 leitch->second, 1, rbufp->recv_time.l_ui,
451 &leitch->yearstart, &leitch->reftime1.l_ui)) {
452 leitch->state = STATE_IDLE;
455 leitch->reftime1.l_uf = 0;
458 fprintf(stderr, "%lu\n", (u_long)leitch->reftime1.l_ui);
460 MSUTOTSF(leitch->fudge1, leitch->reftime1.l_uf);
461 leitch->codetime1 = rbufp->recv_time;
462 leitch->state = STATE_TIME2;
465 if (!leitch_get_time(rbufp,leitch,2)) {
467 if (!clocktime(leitch->yearday,leitch->hour,leitch->minute,
468 leitch->second, 1, rbufp->recv_time.l_ui,
469 &leitch->yearstart, &leitch->reftime2.l_ui)) {
470 leitch->state = STATE_IDLE;
475 fprintf(stderr, "%lu\n", (u_long)leitch->reftime2.l_ui);
477 MSUTOTSF(leitch->fudge1, leitch->reftime2.l_uf);
478 leitch->codetime2 = rbufp->recv_time;
479 leitch->state = STATE_TIME3;
482 if (!leitch_get_time(rbufp,leitch,3)) {
484 if (!clocktime(leitch->yearday,leitch->hour,leitch->minute,
485 leitch->second, GMT, rbufp->recv_time.l_ui,
486 &leitch->yearstart, &leitch->reftime3.l_ui)) {
487 leitch->state = STATE_IDLE;
492 fprintf(stderr, "%lu\n", (u_long)leitch->reftime3.l_ui);
494 MSUTOTSF(leitch->fudge1, leitch->reftime3.l_uf);
495 leitch->codetime3 = rbufp->recv_time;
496 leitch_process(leitch);
497 leitch->state = STATE_IDLE;
501 "leitech_receive: invalid state %d unit %d",
502 leitch->state, leitch->unit);
507 * leitch_process - process a pile of samples from the clock
509 * This routine uses a three-stage median filter to calculate offset and
510 * dispersion. reduce jitter. The dispersion is calculated as the span
511 * of the filter (max - min), unless the quality character (format 2) is
512 * non-blank, in which case the dispersion is calculated on the basis of
513 * the inherent tolerance of the internal radio oscillator, which is
514 * +-2e-5 according to the radio specifications.
518 struct leitchunit *leitch
525 off = leitch->reftime1;
526 L_SUB(&off,&leitch->codetime1);
527 tmp_fp = leitch->reftime2;
528 L_SUB(&tmp_fp,&leitch->codetime2);
529 if (L_ISGEQ(&off,&tmp_fp))
531 tmp_fp = leitch->reftime3;
532 L_SUB(&tmp_fp,&leitch->codetime3);
534 if (L_ISGEQ(&off,&tmp_fp))
536 /*LFPTOD(&off, doffset);*/
537 refclock_receive(leitch->peer);
548 if (year%4) { /* not a potential leap year */
551 if (year % 100) { /* is a leap year */
565 struct recvbuf *rbufp,
566 struct leitchunit *leitch
571 if (rbufp->recv_length < 6)
573 #undef BAD /* confict: defined as (-1) in AIX sys/param.h */
574 #define BAD(A) (rbufp->recv_buffer[A] < '0') || (rbufp->recv_buffer[A] > '9')
575 if (BAD(0)||BAD(1)||BAD(2)||BAD(3)||BAD(4)||BAD(5))
577 #define ATOB(A) ((rbufp->recv_buffer[A])-'0')
578 leitch->year = ATOB(0)*10 + ATOB(1);
579 leitch->month = ATOB(2)*10 + ATOB(3);
580 leitch->day = ATOB(4)*10 + ATOB(5);
583 if (leitch->month > 12)
585 if (leitch->day > days_in_month[leitch->month-1])
588 /* calculate yearday */
590 leitch->yearday = leitch->day;
592 while ( i < (leitch->month-1) )
593 leitch->yearday += days_in_month[i++];
595 if ((days_per_year((leitch->year>90?1900:2000)+leitch->year)==365) &&
607 struct recvbuf *rbufp,
608 struct leitchunit *leitch,
612 if (BAD(0)||BAD(1)||BAD(2)||BAD(3)||BAD(4)||BAD(5))
614 leitch->hour = ATOB(0)*10 +ATOB(1);
615 leitch->minute = ATOB(2)*10 +ATOB(3);
616 leitch->second = ATOB(4)*10 +ATOB(5);
618 if ((leitch->hour > 23) || (leitch->minute > 60) ||
619 (leitch->second > 60))
625 int refclock_leitch_bs;
626 #endif /* REFCLOCK */