refclock_wwvb.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1996 by Sun Microsystems, Inc.
* All Rights Reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* refclock_wwvb - clock driver for Spectracom WWVB receivers
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_refclock.h"
#include "ntp_stdlib.h"
#include "ntp_calendar.h"
/*
* This driver supports the Spectracom Model 8170 and Netclock/2 WWVB
* Synchronized Clock. This clock has proven a reliable source of time,
* except in some cases of high ambient conductive RF interference. The
* claimed accuracy of the clock is 100 usec relative to the broadcast
* signal; however, in most cases the actual accuracy is limited by the
* precision of the timecode and the latencies of the serial interface
* and operating system.
*
* The DIPswitches on this clock should be set to 24-hour display, AUTO
* DST off, time zone 0 (UTC), data format 0 or 2 (see below) and baud
* rate 9600. If this clock is to used as the source for the IRIG Audio
* Decoder (refclock_irig.c in this distribution), set the DIPswitches
* for AM IRIG output and IRIG format 1 (IRIG B with signature control).
*
* There are two timecode formats used by these clocks. Format 0, which
* is available with both the Netclock/2 and 8170, and format 2, which
* is available only with the Netclock/2 and specially modified 8170.
*
* Format 0 (22 ASCII printing characters):
*
* <cr><lf>i ddd hh:mm:ss TZ=zz<cr><lf>
*
* on-time = first <cr> * hh:mm:ss = hours, minutes, seconds
* i = synchronization flag (' ' = in synch, '?' = out of synch)
*
* The alarm condition is indicated by other than ' ' at A, which occurs
* during initial synchronization and when received signal is lost for
* about ten hours.
*
* Format 2 (24 ASCII printing characters):
*
* <cr><lf>iqyy ddd hh:mm:ss.fff ld
*
* on-time = <cr>
* i = synchronization flag (' ' = in synch, '?' = out of synch)
* q = quality indicator (' ' = locked, 'A'...'D' = unlocked)
* yy = year (as broadcast)
* ddd = day of year
* hh:mm:ss.fff = hours, minutes, seconds, milliseconds
*
* The alarm condition is indicated by other than ' ' at A, which occurs
* during initial synchronization and when received signal is lost for
* about ten hours. The unlock condition is indicated by other than ' '
* at Q.
*
* The Q is normally ' ' when the time error is less than 1 ms and a
* character in the set 'A'...'D' when the time error is less than 10,
* 100, 500 and greater than 500 ms respectively. The L is normally ' ',
* but is set to 'L' early in the month of an upcoming UTC leap second
* and reset to ' ' on the first day of the following month. The D is
* set to 'S' for standard time 'I' on the day preceding a switch to
* daylight time, 'D' for daylight time and 'O' on the day preceding a
* switch to standard time. The start bit of the first <cr> is
* synchronized to the indicated time as returned.
*
* This driver does not need to be told which format is in use - it
* figures out which one from the length of the message. A three-stage
* median filter is used to reduce jitter and provide a dispersion
* measure. The driver makes no attempt to correct for the intrinsic
* jitter of the radio itself, which is a known problem with the older
* radios.
*
* Fudge Factors
*
* This driver can retrieve a table of quality data maintained
* internally by the Netclock/2 receiver. If flag4 of the fudge
* configuration command is set to 1, the driver will retrieve this
* table and write it to the clockstats file on when the first timecode
* message of a new day is received.
*/
/*
* Interface definitions
*/
/*
* Imported from ntp_timer module
*/
/*
* Imported from ntpd module
*/
extern int debug; /* global debug flag */
/*
* WWVB unit control structure
*/
struct wwvbunit {
int pollcnt; /* poll message counter */
};
/*
* Function prototypes
*/
static int wwvb_start P((int, struct peer *));
static void wwvb_shutdown P((int, struct peer *));
static void wwvb_receive P((struct recvbuf *));
/*
* Transfer vector
*/
struct refclock refclock_wwvb = {
wwvb_start, /* start up driver */
wwvb_shutdown, /* shut down driver */
wwvb_poll, /* transmit poll message */
noentry, /* not used (old wwvb_control) */
noentry, /* initialize driver (not used) */
noentry, /* not used (old wwvb_buginfo) */
NOFLAGS /* not used */
};
/*
* wwvb_start - open the devices and initialize data for processing
*/
static int
int unit;
{
struct refclockproc *pp;
int fd;
char device[20];
#ifdef DEBUG
if (debug)
printf("inside wwvb_start\n");
#endif
/*
* Open serial port. Use CLK line discipline, if available.
*/
#ifdef TTYCLK
#else
#endif /* TTYCLK */
{
#ifdef DEBUG
if (debug)
printf ("refclock_open barfed\n");
#endif
return (0);
}
/*
* Allocate and initialize unit structure
*/
return (0);
}
return (0);
}
/*
* Initialize miscellaneous variables
*/
return (1);
}
/*
* wwvb_shutdown - shut down the clock
*/
static void
int unit;
{
struct refclockproc *pp;
}
/*
* wwvb_receive - receive data from the serial interface
*/
static void
{
struct refclockproc *pp;
int temp;
char syncchar; /* synchronization indicator */
char qualchar; /* quality indicator */
char leapchar; /* leap indicator */
int tz = 0;
#ifdef DEBUG
if (debug)
printf ("in wwvb_receive\n");
#endif
/*
* Initialize pointers and read the timecode and timestamp
*/
/*
* Note we get a buffer and timestamp for both a <cr> and <lf>,
* but only the <cr> timestamp is retained. Note: in format 0 on
* a Netclock/2 or upgraded 8170 the start bit is delayed 100
* +-50 us relative to the pps; however, on an unmodified 8170
* the start bit can be delayed up to 10 ms. In format 2 the
* reading precision is only to the millisecond. Thus, unless
* you have a pps gadget and don't have to have the year, format
* 0 provides the lowest jitter.
*/
if (temp == 0) {
} else
return;
}
#ifdef DEBUG
if (debug)
pp->a_lastcode);
#endif
/*
* We get down to business, check the timecode format and decode
* its contents. This code uses the timecode length to determine
* whether format 0 or format 2. If the timecode has invalid
* length or is not in proper format, we declare bad format and
* exit.
*/
case LENWWVB0:
/*
* Timecode format 0: "I ddd hh:mm:ss TZ=nn"
*/
{
int hr;
break;
}
case LENWWVB2:
/*
* Timecode format 2: "IQyy ddd hh:mm:ss.mmm LD"
*/
&leapchar) == 9)
break;
case LENWWVB3:
/*
* Timecode format 3: "0003I yyyymmdd hhmmss+0000SL#"
*/
{
int matched;
qualchar = ' ';
matched =
if (matched==8)
{
if (month > 2)
{
}
break;
}
#ifdef DEBUG
if (debug)
printf ("wwvb_receive: Surrender, Dorothy!\n");
#endif
}
default:
else
return;
}
/*
* Decode synchronization, quality and leap characters. If
* unsynchronized, set the leap bits accordingly and exit.
* Otherwise, set the leap bits according to the leap character.
* Once synchronized, the dispersion depends only on when the
* clock was last heard. The first time the clock is heard, the
* time last heard is faked based on the quality indicator. The
* magic numbers (in seconds) are from the clock specifications.
*/
switch (qualchar) {
case ' ':
ltemp = 0;
break;
case 'A':
ltemp = 800;
break;
case 'B':
ltemp = 5300;
break;
case 'C':
ltemp = 25300;
break;
case 'D':
ltemp = NTP_MAXAGE;
break;
default:
return;
}
if (syncchar != ' ')
else {
if (leapchar == 'L')
else
}
/*
* If the monitor flag is set (flag4), we dump the internal
* quality table at the first timecode beginning the day.
*/
/*
* Process the new sample in the median filter and determine the
* reference clock offset and dispersion. We use lastrec as both
* the reference time and receive time in order to avoid being
* cute, like setting the reference time later than the receive
* time, which may cause a paranoid protocol module to chuck out
* the data.
*/
return;
}
}
/*
* wwvb_poll - called by the transmit procedure
*/
static void
int unit;
{
struct refclockproc *pp;
char poll;
/*
* Time to poll the clock. The Spectracom clock responds to a
* 'T' by returning a timecode in the format(s) specified above.
* 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. If nothing is heard
* from the clock for two polls, declare a timeout and keep
* going.
*/
else
poll = 'R';
else
poll = 'T';
} else
}
#else /* not (REFCLOCK && WWVB) */
int refclock_wwvb_bs;
#endif /* not (REFCLOCK && WWVB) */