2 * refclock_leitch - clock driver for the Leitch CSD-5300 Master Clock
11 #if defined(REFCLOCK) && defined(CLOCK_LEITCH)
18 #include "ntp_refclock.h"
19 #include "timevalops.h"
20 #include "ntp_stdlib.h"
24 * Driver for Leitch CSD-5300 Master Clock System
34 * TIME: <CR>/HHMMSS <CR>/HHMMSS <CR>/HHMMSS <CR>/
35 * second bondaried on the stop bit of the <CR>
36 * second boundaries at '/' above.
37 * STATUS: G (good), D (diag fail), T (time not provided) or
38 * P (last phone update failed)
40 #define PRECISION (-20) /* 1x10-8 */
41 #define MAXUNITS 1 /* max number of LEITCH units */
42 #define LEITCHREFID "ATOM" /* reference id */
43 #define LEITCH_DESCRIPTION "Leitch: CSD 5300 Master Clock System Driver"
44 #define LEITCH232 "/dev/leitch%d" /* name of radio device */
45 #define SPEED232 B300 /* uart speed (300 baud) */
47 #define leitch_send(A,M) \
48 if (debug) fprintf(stderr,"write leitch %s\n",M); \
49 if ((write(A->leitchio.fd,M,sizeof(M)) < 0)) {\
51 fprintf(stderr, "leitch_send: unit %d send failed\n", A->unit); \
53 msyslog(LOG_ERR, "leitch_send: unit %d send failed %m",A->unit);}
55 #define leitch_send(A,M) \
56 if ((write(A->leitchio.fd,M,sizeof(M)) < 0)) {\
57 msyslog(LOG_ERR, "leitch_send: unit %d send failed %m",A->unit);}
67 * LEITCH unit control structure
71 struct refclockio leitchio;
94 static void leitch_init (void);
95 static int leitch_start (int, struct peer *);
96 static void leitch_shutdown (int, struct peer *);
97 static void leitch_poll (int, struct peer *);
98 static void leitch_control (int, const struct refclockstat *, struct refclockstat *, struct peer *);
99 #define leitch_buginfo noentry
100 static void leitch_receive (struct recvbuf *);
101 static void leitch_process (struct leitchunit *);
103 static void leitch_timeout (struct peer *);
105 static int leitch_get_date (struct recvbuf *, struct leitchunit *);
106 static int leitch_get_time (struct recvbuf *, struct leitchunit *, int);
107 static int days_per_year (int);
109 static struct leitchunit leitchunits[MAXUNITS];
110 static u_char unitinuse[MAXUNITS];
111 static u_char stratumtouse[MAXUNITS];
112 static u_int32 refid[MAXUNITS];
114 static char days_in_month [] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
119 struct refclock refclock_leitch = {
120 leitch_start, leitch_shutdown, leitch_poll,
121 leitch_control, leitch_init, leitch_buginfo, NOFLAGS
125 * leitch_init - initialize internal leitch driver data
132 memset((char*)leitchunits, 0, sizeof(leitchunits));
133 memset((char*)unitinuse, 0, sizeof(unitinuse));
134 for (i = 0; i < MAXUNITS; i++)
135 memcpy((char *)&refid[i], LEITCHREFID, 4);
139 * leitch_shutdown - shut down a LEITCH clock
147 struct leitchunit *leitch;
149 if (unit >= MAXUNITS) {
152 leitch = &leitchunits[unit];
153 if (-1 != leitch->leitchio.fd)
154 io_closeclock(&leitch->leitchio);
157 fprintf(stderr, "leitch_shutdown()\n");
162 * leitch_poll - called by the transmit procedure
170 struct leitchunit *leitch;
172 /* start the state machine rolling */
176 fprintf(stderr, "leitch_poll()\n");
178 if (unit >= MAXUNITS) {
183 leitch = &leitchunits[unit];
185 if (leitch->state != STATE_IDLE) {
186 /* reset and wait for next poll */
188 leitch->state = STATE_IDLE;
190 leitch_send(leitch,"D\r");
191 leitch->state = STATE_DATE;
198 const struct refclockstat *in,
199 struct refclockstat *out,
200 struct peer *passed_peer
203 if (unit >= MAXUNITS) {
205 "leitch_control: unit %d invalid", unit);
210 if (in->haveflags & CLK_HAVEVAL1)
211 stratumtouse[unit] = (u_char)(in->fudgeval1);
212 if (in->haveflags & CLK_HAVEVAL2)
213 refid[unit] = in->fudgeval2;
214 if (unitinuse[unit]) {
217 peer = (&leitchunits[unit])->peer;
218 peer->stratum = stratumtouse[unit];
219 peer->refid = refid[unit];
224 memset((char *)out, 0, sizeof (struct refclockstat));
225 out->type = REFCLK_ATOM_LEITCH;
226 out->haveflags = CLK_HAVEVAL1 | CLK_HAVEVAL2;
227 out->fudgeval1 = (int32)stratumtouse[unit];
228 out->fudgeval2 = refid[unit];
229 out->p_lastcode = "";
230 out->clockdesc = LEITCH_DESCRIPTION;
235 * leitch_start - open the LEITCH devices and initialize data for processing
243 struct leitchunit *leitch;
248 * Check configuration info.
250 if (unit >= MAXUNITS) {
251 msyslog(LOG_ERR, "leitch_start: unit %d invalid", unit);
255 if (unitinuse[unit]) {
256 msyslog(LOG_ERR, "leitch_start: unit %d in use", unit);
263 snprintf(leitchdev, sizeof(leitchdev), LEITCH232, unit);
264 fd232 = open(leitchdev, O_RDWR, 0777);
267 "leitch_start: open of %s: %m", leitchdev);
271 leitch = &leitchunits[unit];
272 memset(leitch, 0, sizeof(*leitch));
274 #if defined(HAVE_SYSV_TTYS)
276 * System V serial line parameters (termio interface)
279 { struct termio ttyb;
280 if (ioctl(fd232, TCGETA, &ttyb) < 0) {
282 "leitch_start: ioctl(%s, TCGETA): %m", leitchdev);
285 ttyb.c_iflag = IGNBRK|IGNPAR|ICRNL;
287 ttyb.c_cflag = SPEED232|CS8|CLOCAL|CREAD;
288 ttyb.c_lflag = ICANON;
289 ttyb.c_cc[VERASE] = ttyb.c_cc[VKILL] = '\0';
290 if (ioctl(fd232, TCSETA, &ttyb) < 0) {
292 "leitch_start: ioctl(%s, TCSETA): %m", leitchdev);
296 #endif /* HAVE_SYSV_TTYS */
297 #if defined(HAVE_TERMIOS)
299 * POSIX serial line parameters (termios interface)
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 */
326 #if defined(HAVE_BSD_TTYS)
328 * 4.3bsd serial line parameters (sgttyb interface)
333 if (ioctl(fd232, TIOCGETP, &ttyb) < 0) {
335 "leitch_start: ioctl(%s, TIOCGETP): %m", leitchdev);
338 ttyb.sg_ispeed = ttyb.sg_ospeed = SPEED232;
339 ttyb.sg_erase = ttyb.sg_kill = '\0';
340 ttyb.sg_flags = EVENP|ODDP|CRMOD;
341 if (ioctl(fd232, TIOCSETP, &ttyb) < 0) {
343 "leitch_start: ioctl(%s, TIOCSETP): %m", leitchdev);
347 #endif /* HAVE_BSD_TTYS */
350 * Set up the structures
354 leitch->state = STATE_IDLE;
355 leitch->fudge1 = 15; /* 15ms */
357 leitch->leitchio.clock_recv = leitch_receive;
358 leitch->leitchio.srcclock = peer;
359 leitch->leitchio.datalen = 0;
360 leitch->leitchio.fd = fd232;
361 if (!io_addclock(&leitch->leitchio)) {
362 leitch->leitchio.fd = -1;
367 * All done. Initialize a few random peer variables, then
370 peer->precision = PRECISION;
371 peer->stratum = stratumtouse[unit];
372 peer->refid = refid[unit];
377 * Something broke; abandon ship.
385 * leitch_receive - receive data from the serial interface on a leitch
390 struct recvbuf *rbufp
393 struct leitchunit *leitch = rbufp->recv_peer->procptr->unitptr;
397 fprintf(stderr, "leitch_recieve(%*.*s)\n",
398 rbufp->recv_length, rbufp->recv_length,
401 if (rbufp->recv_length != 7)
402 return; /* The date is return with a trailing newline,
405 switch (leitch->state) {
406 case STATE_IDLE: /* unexpected, discard and resync */
409 if (!leitch_get_date(rbufp,leitch)) {
410 leitch->state = STATE_IDLE;
413 leitch_send(leitch,"T\r");
416 fprintf(stderr, "%u\n",leitch->yearday);
418 leitch->state = STATE_TIME1;
421 if (!leitch_get_time(rbufp,leitch,1)) {
423 if (!clocktime(leitch->yearday,leitch->hour,leitch->minute,
424 leitch->second, 1, rbufp->recv_time.l_ui,
425 &leitch->yearstart, &leitch->reftime1.l_ui)) {
426 leitch->state = STATE_IDLE;
429 leitch->reftime1.l_uf = 0;
432 fprintf(stderr, "%lu\n", (u_long)leitch->reftime1.l_ui);
434 MSUTOTSF(leitch->fudge1, leitch->reftime1.l_uf);
435 leitch->codetime1 = rbufp->recv_time;
436 leitch->state = STATE_TIME2;
439 if (!leitch_get_time(rbufp,leitch,2)) {
441 if (!clocktime(leitch->yearday,leitch->hour,leitch->minute,
442 leitch->second, 1, rbufp->recv_time.l_ui,
443 &leitch->yearstart, &leitch->reftime2.l_ui)) {
444 leitch->state = STATE_IDLE;
449 fprintf(stderr, "%lu\n", (u_long)leitch->reftime2.l_ui);
451 MSUTOTSF(leitch->fudge1, leitch->reftime2.l_uf);
452 leitch->codetime2 = rbufp->recv_time;
453 leitch->state = STATE_TIME3;
456 if (!leitch_get_time(rbufp,leitch,3)) {
458 if (!clocktime(leitch->yearday,leitch->hour,leitch->minute,
459 leitch->second, GMT, rbufp->recv_time.l_ui,
460 &leitch->yearstart, &leitch->reftime3.l_ui)) {
461 leitch->state = STATE_IDLE;
466 fprintf(stderr, "%lu\n", (u_long)leitch->reftime3.l_ui);
468 MSUTOTSF(leitch->fudge1, leitch->reftime3.l_uf);
469 leitch->codetime3 = rbufp->recv_time;
470 leitch_process(leitch);
471 leitch->state = STATE_IDLE;
475 "leitech_receive: invalid state %d unit %d",
476 leitch->state, leitch->unit);
481 * leitch_process - process a pile of samples from the clock
483 * This routine uses a three-stage median filter to calculate offset and
484 * dispersion. reduce jitter. The dispersion is calculated as the span
485 * of the filter (max - min), unless the quality character (format 2) is
486 * non-blank, in which case the dispersion is calculated on the basis of
487 * the inherent tolerance of the internal radio oscillator, which is
488 * +-2e-5 according to the radio specifications.
492 struct leitchunit *leitch
499 off = leitch->reftime1;
500 L_SUB(&off,&leitch->codetime1);
501 tmp_fp = leitch->reftime2;
502 L_SUB(&tmp_fp,&leitch->codetime2);
503 if (L_ISGEQ(&off,&tmp_fp))
505 tmp_fp = leitch->reftime3;
506 L_SUB(&tmp_fp,&leitch->codetime3);
508 if (L_ISGEQ(&off,&tmp_fp))
510 /*LFPTOD(&off, doffset);*/
511 refclock_receive(leitch->peer);
522 if (year%4) { /* not a potential leap year */
525 if (year % 100) { /* is a leap year */
539 struct recvbuf *rbufp,
540 struct leitchunit *leitch
545 if (rbufp->recv_length < 6)
547 #undef BAD /* confict: defined as (-1) in AIX sys/param.h */
548 #define BAD(A) (rbufp->recv_buffer[A] < '0') || (rbufp->recv_buffer[A] > '9')
549 if (BAD(0)||BAD(1)||BAD(2)||BAD(3)||BAD(4)||BAD(5))
551 #define ATOB(A) ((rbufp->recv_buffer[A])-'0')
552 leitch->year = ATOB(0)*10 + ATOB(1);
553 leitch->month = ATOB(2)*10 + ATOB(3);
554 leitch->day = ATOB(4)*10 + ATOB(5);
557 if (leitch->month > 12)
559 if (leitch->day > days_in_month[leitch->month-1])
562 /* calculate yearday */
564 leitch->yearday = leitch->day;
566 while ( i < (leitch->month-1) )
567 leitch->yearday += days_in_month[i++];
569 if ((days_per_year((leitch->year>90?1900:2000)+leitch->year)==365) &&
581 struct recvbuf *rbufp,
582 struct leitchunit *leitch,
586 if (BAD(0)||BAD(1)||BAD(2)||BAD(3)||BAD(4)||BAD(5))
588 leitch->hour = ATOB(0)*10 +ATOB(1);
589 leitch->minute = ATOB(2)*10 +ATOB(3);
590 leitch->second = ATOB(4)*10 +ATOB(5);
592 if ((leitch->hour > 23) || (leitch->minute > 60) ||
593 (leitch->second > 60))
599 NONEMPTY_TRANSLATION_UNIT
600 #endif /* REFCLOCK */