#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if defined(REFCLOCK) && defined(CLOCK_TPRO)
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_refclock.h"
#include "ntp_unixtime.h"
#include "sys/tpro.h"
#include "ntp_stdlib.h"
#define DEVICE "/dev/tpro%d"
#define PRECISION (-20)
#define REFID "IRIG"
#define DESCRIPTION "KSI/Odetics TPRO/S IRIG Interface"
struct tprounit {
struct tproval tprodata;
};
static int tpro_start P((int, struct peer *));
static void tpro_shutdown P((int, struct peer *));
static void tpro_poll P((int unit, struct peer *));
struct refclock refclock_tpro = {
tpro_start,
tpro_shutdown,
tpro_poll,
noentry,
noentry,
noentry,
NOFLAGS
};
static int
tpro_start(
int unit,
struct peer *peer
)
{
register struct tprounit *up;
struct refclockproc *pp;
char device[20];
int fd;
(void)sprintf(device, DEVICE, unit);
fd = open(device, O_RDONLY | O_NDELAY, 0777);
if (fd == -1) {
msyslog(LOG_ERR, "tpro_start: open of %s: %m", device);
return (0);
}
if (!(up = (struct tprounit *) emalloc(sizeof(struct tprounit)))) {
(void) close(fd);
return (0);
}
memset((char *)up, 0, sizeof(struct tprounit));
pp = peer->procptr;
pp->io.clock_recv = noentry;
pp->io.srcclock = (caddr_t)peer;
pp->io.datalen = 0;
pp->io.fd = fd;
pp->unitptr = (caddr_t)up;
peer->precision = PRECISION;
peer->flags |= FLAG_BURST;
peer->burst = pp->nstages;
pp->clockdesc = DESCRIPTION;
memcpy((char *)&pp->refid, REFID, 4);
return (1);
}
static void
tpro_shutdown(
int unit,
struct peer *peer
)
{
register struct tprounit *up;
struct refclockproc *pp;
pp = peer->procptr;
up = (struct tprounit *)pp->unitptr;
io_closeclock(&pp->io);
free(up);
}
static void
tpro_poll(
int unit,
struct peer *peer
)
{
register struct tprounit *up;
struct refclockproc *pp;
struct tproval *tp;
pp = peer->procptr;
up = (struct tprounit *)pp->unitptr;
tp = &up->tprodata;
if (read(pp->io.fd, (char *)tp, sizeof(struct tproval)) < 0) {
refclock_report(peer, CEVNT_FAULT);
return;
}
get_systime(&pp->lastrec);
pp->polls++;
sprintf(pp->a_lastcode,
"%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x",
tp->day100, tp->day10, tp->day1, tp->hour10, tp->hour1,
tp->min10, tp->min1, tp->sec10, tp->sec1, tp->ms100,
tp->ms10, tp->ms1, tp->usec100, tp->usec10, tp->usec1,
tp->status);
pp->lencode = strlen(pp->a_lastcode);
#ifdef DEBUG
if (debug)
printf("tpro: time %s timecode %d %s\n",
ulfptoa(&pp->lastrec, 6), pp->lencode,
pp->a_lastcode);
#endif
if (sscanf(pp->a_lastcode, "%3d %2d:%2d:%2d.%6ld", &pp->day,
&pp->hour, &pp->minute, &pp->second, &pp->usec)
!= 5) {
refclock_report(peer, CEVNT_BADTIME);
return;
}
if (!refclock_process(pp)) {
refclock_report(peer, CEVNT_BADTIME);
peer->burst = 0;
return;
}
if (peer->burst > 0)
return;
record_clock_stats(&peer->srcadr, pp->a_lastcode);
if (!tp->status & 0x3)
pp->leap = LEAP_NOTINSYNC;
else
pp->leap = LEAP_NOWARNING;
refclock_receive(peer);
}
#else
int refclock_tpro_bs;
#endif