/*
* This driver supports the Chronolog K-series WWVB receiver.
*
* Input format:
*
* Y YY/MM/DD<cr><lf>
* Z hh:mm:ss<cr><lf>
*
* YY/MM/DD -- what you'd expect. This arrives a few seconds before the
* timestamp.
* hh:mm:ss -- what you'd expect. We take time on the <cr>.
*
* Our Chronolog writes time out at 2400 bps 8/N/1, but it can be configured
* otherwise. The clock seems to appear every 60 seconds, which doesn't make
* for good statistics collection.
*
* The original source of this module was the WWVB module.
*/
/*
* Transfer vector
*/
struct refclock refclock_chronolog = {
chronolog_start, /* start up driver */
chronolog_shutdown, /* shut down driver */
chronolog_poll, /* poll the driver -- a nice fabrication */
noentry, /* not used */
noentry, /* not used */
noentry, /* not used */
NOFLAGS /* not used */
};
/*
* chronolog_start - open the devices and initialize data for processing
*/
static int
chronolog_start(
int unit,
struct peer *peer
)
{
register struct chronolog_unit *up;
struct refclockproc *pp;
int fd;
char device[20];
/*
* Open serial port. Don't bother with CLK line discipline, since
* it's not available.
*/
snprintf(device, sizeof(device), DEVICE, unit);
#ifdef DEBUG
if (debug)
printf ("starting Chronolog with device %s\n",device);
#endif
fd = refclock_open(&peer->srcadr, device, SPEED232, 0);
if (fd <= 0)
return (0);
/*
* chronolog_shutdown - shut down the clock
*/
static void
chronolog_shutdown(
int unit,
struct peer *peer
)
{
register struct chronolog_unit *up;
struct refclockproc *pp;
pp = peer->procptr;
up = pp->unitptr;
if (-1 != pp->io.fd)
io_closeclock(&pp->io);
if (NULL != up)
free(up);
}
/*
* chronolog_receive - receive data from the serial interface
*/
static void
chronolog_receive(
struct recvbuf *rbufp
)
{
struct chronolog_unit *up;
struct refclockproc *pp;
struct peer *peer;
l_fp trtmp; /* arrival timestamp */
int hours; /* hour-of-day */
int minutes; /* minutes-past-the-hour */
int seconds; /* seconds */
int temp; /* int temp */
int got_good; /* got a good time flag */
/*
* Initialize pointers and read the timecode and timestamp
*/
peer = rbufp->recv_peer;
pp = peer->procptr;
up = pp->unitptr;
temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
/*
* We get down to business. Check the timecode format and decode
* its contents. This code uses the first character to see whether
* we're looking at a date or a time. We store data data across
* calls since it is transmitted a few seconds ahead of the
* timestamp.
*/
got_good=0;
if (sscanf(pp->a_lastcode, "Y %d/%d/%d", &up->year,&up->month,&up->day))
{
/*
* Y2K convert the 2-digit year
*/
up->year = up->year >= 69 ? up->year : up->year + 100;
return;
}
if (sscanf(pp->a_lastcode,"Z %02d:%02d:%02d",
&hours,&minutes,&seconds) == 3)
{
#ifdef GET_LOCALTIME
struct tm local;
struct tm *gmtp;
time_t unixtime;
int adjyear;
int adjmon;
/*
* Convert to GMT for sites that distribute localtime. This
* means we have to do Y2K conversion on the 2-digit year;
* otherwise, we get the time wrong.
*/
#else
/*
* For more rational sites distributing UTC
*/
pp->day = ymd2yd(year+1900,month,day);
pp->hour = hours;
pp->minute = minutes;
pp->second = seconds;
#endif
got_good=1;
}
if (!got_good)
return;
/*
* Process the new sample in the median filter and determine the
* timecode timestamp.
*/
if (!refclock_process(pp)) {
refclock_report(peer, CEVNT_BADTIME);
return;
}
pp->lastref = pp->lastrec;
refclock_receive(peer);
record_clock_stats(&peer->srcadr, pp->a_lastcode);
up->lasthour = (u_char)pp->hour;
}
/*
* chronolog_poll - called by the transmit procedure
*/
static void
chronolog_poll(
int unit,
struct peer *peer
)
{
/*
* Time to poll the clock. The Chrono-log clock is supposed to
* respond to a 'T' by returning a timecode in the format(s)
* specified above. Ours does (can?) not, but this seems to be
* an installation-specific problem. This code is dyked out,
* but may be re-enabled if anyone ever finds a Chrono-log that
* actually listens to this command.
*/
#if 0
register struct chronolog_unit *up;
struct refclockproc *pp;
char pollchar;
pp = peer->procptr;
up = pp->unitptr;
if (peer->burst == 0 && peer->reach == 0)
refclock_report(peer, CEVNT_TIMEOUT);
if (up->linect > 0)
pollchar = 'R';
else
pollchar = 'T';
if (write(pp->io.fd, &pollchar, 1) != 1)
refclock_report(peer, CEVNT_FAULT);
else
pp->polls++;
#endif
}