/* * refclock_irig_ascii - clock driver for irig ascii string * as produced by trak model 8400 * copy this over ntpd/refclock_arbiter.c * */ #ifdef HAVE_CONFIG_H #include #endif #if defined(REFCLOCK) && defined(CLOCK_ARBITER) #include "ntpd.h" #include "ntp_io.h" #include "ntp_refclock.h" #include "ntp_stdlib.h" #include #include /* * once per second: * dddhhmmss000?? * ten times per second: * dddhhmmsst00?? * * on-time = d * ddd = day of year * hhmmss = hours, minutes, seconds * 000 = fraction of second (not used) * * The alarm condition is indicated by a '?', which indicates the * receiver is not synchronized. */ /* * Interface definitions */ #define DEVICE "/dev/irig%d" /* device name and unit */ #define SPEED232 B9600 /* uart speed (19200 baud) */ #define PRECISION (-14) /* precision assumed */ #define REFID "IRIG" /* reference ID */ #define DESCRIPTION "TRAK Model 8400" /* WRU */ #define LENRIG 14 /* timecode length */ #define MAXSTA 40 /* max length of status string */ #define MAXPOS 80 /* max length of position string */ /* * irig unit control structure */ struct irigunit { l_fp laststamp; /* last receive timestamp */ int tcswitch; /* timecode switch/counter */ char qualchar; /* IEEE P1344 quality (TQ command) */ char status[MAXSTA]; /* receiver status (SR command) */ char latlon[MAXPOS]; /* receiver position (lat/lon/alt) */ }; /* * Function prototypes */ static int irig_start (int, struct peer *); static void irig_shutdown (int, struct peer *); static void irig_receive (struct recvbuf *); static void irig_poll (int, struct peer *); /* * Transfer vector */ struct refclock refclock_arbiter = { irig_start, /* start up driver */ irig_shutdown, /* shut down driver */ irig_poll, /* transmit poll message */ noentry, noentry, /* not used (old arb_control) */ noentry, /* initialize driver (not used) */ noentry, /* not used (old arb_buginfo) */ NOFLAGS /* not used */ }; /* * arb_start - open the devices and initialize data for processing */ static int irig_start( int unit, struct peer *peer ) { register struct irigunit *up; struct refclockproc *pp; int fd; char device[20]; /* * Open serial port. Use CLK line discipline, if available. */ (void)sprintf(device, DEVICE, unit); if (!(fd = refclock_open(device, SPEED232, LDISC_CLK))) return (0); /* * Allocate and initialize unit structure */ if (!(up = (struct irigunit *)emalloc(sizeof(struct irigunit)))) { (void) close(fd); return (0); } memset((char *)up, 0, sizeof(struct irigunit)); pp = peer->procptr; pp->io.clock_recv = irig_receive; pp->io.srcclock = (caddr_t)peer; pp->io.datalen = 0; pp->io.fd = fd; if (!io_addclock(&pp->io)) { (void) close(fd); free(up); return (0); } pp->unitptr = (caddr_t)up; /* * Initialize miscellaneous variables */ peer->precision = PRECISION; pp->clockdesc = DESCRIPTION; memcpy((char *)&pp->refid, REFID, 4); write(pp->io.fd, "B0", 2); return (1); } /* * arb_shutdown - shut down the clock */ static void irig_shutdown( int unit, struct peer *peer ) { register struct irigunit *up; struct refclockproc *pp; pp = peer->procptr; up = (struct irigunit *)pp->unitptr; io_closeclock(&pp->io); free(up); } /* * arb_receive - receive data from the serial interface */ static void irig_receive( struct recvbuf *rbufp ) { register struct irigunit *up; struct refclockproc *pp; struct peer *peer; l_fp trtmp; int temp; u_char syncchar; /* synch indicator */ char tbuf[BMAX]; /* temp buffer */ /* * Initialize pointers and read the timecode and timestamp */ peer = (struct peer *)rbufp->recv_srcclock; pp = peer->procptr; up = (struct irigunit *)pp->unitptr; temp = refclock_gtlin(rbufp, tbuf, BMAX, &trtmp); if (temp == 0) return; pp->lastrec = trtmp; if (temp < 3) return; if (up->tcswitch == 0) { } /* * We get down to business, check the timecode format and decode * its contents. If the timecode has valid length, but not in * proper format, we declare bad format and exit. If the * timecode has invalid length, which sometimes occurs when the * B0 amputates the broadcast, we just quietly steal away. Note * that the time quality character and receiver status string is * tacked on the end for clockstats display. */ up->tcswitch++; if (up->tcswitch <= 1 || temp < LENRIG) return; up->qualchar = '0'; syncchar = ' '; strncpy(pp->a_lastcode, tbuf, BMAX); strcat(pp->a_lastcode, up->status); pp->lencode = strlen(pp->a_lastcode); if (sscanf(pp->a_lastcode, "%3d%2d%2d%2d000??", &pp->day, &pp->hour, &pp->minute, &pp->second) != 4) { refclock_report(peer, CEVNT_BADREPLY); return; } pp->year = 0; /* * We decode the clock dispersion from the time quality * character. */ switch (up->qualchar) { default: /* locked, max accuracy */ pp->disp = 1e-4; pp->lastref = pp->lastrec; break; } if (syncchar != ' ') pp->leap = LEAP_NOTINSYNC; else pp->leap = LEAP_NOWARNING; /* * Process the new sample in the median filter and determine the * timecode timestamp. */ if (!refclock_process(pp)) refclock_report(peer, CEVNT_BADTIME); else if (peer->disp > MAXDISTANCE) refclock_receive(peer); if (up->tcswitch >= MAXSTAGE) { } } /* * arb_poll - called by the transmit procedure */ static void irig_poll( int unit, struct peer *peer ) { register struct irigunit *up; struct refclockproc *pp; /* * Time to poll the clock. The Arbiter clock responds to a "B5" * by returning a timecode in the format specified above. * Transmission occurs once per second, unless turned off by a * "B0". Note there is no checking on state, since this may not * be the only customer reading the clock. Only one customer * need poll the clock; all others just listen in. */ pp = peer->procptr; up = (struct arbunit *)pp->unitptr; pp->polls++; up->tcswitch = 0; if (write(pp->io.fd, "TQ", 2) != 2) refclock_report(peer, CEVNT_FAULT); /* * Process median filter samples. If none received, declare a * timeout and keep going. */ if (pp->coderecv == pp->codeproc) { refclock_report(peer, CEVNT_TIMEOUT); return; } refclock_receive(peer); record_clock_stats(&peer->srcadr, pp->a_lastcode); #ifdef DEBUG if (debug) printf("irig: timecode %d %s\n", pp->lencode, pp->a_lastcode); #endif } #else int refclock_arbiter_bs; #endif /* REFCLOCK */