NetBSD/dist/ntp/ntpd/ntp_refclock.c

1232 lines
30 KiB
C
Raw Normal View History

2006-06-11 23:34:07 +04:00
/* $NetBSD: ntp_refclock.c,v 1.4 2006/06/11 19:34:11 kardel Exp $ */
2000-03-29 16:38:44 +04:00
/*
* ntp_refclock - processing support for reference clocks
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_unixtime.h"
2003-12-04 19:05:14 +03:00
#include "ntp_tty.h"
2000-03-29 16:38:44 +04:00
#include "ntp_refclock.h"
#include "ntp_stdlib.h"
2003-12-04 19:05:14 +03:00
#include <stdio.h>
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif /* HAVE_SYS_IOCTL_H */
2000-03-29 16:38:44 +04:00
#ifdef REFCLOCK
#ifdef TTYCLK
# ifdef HAVE_SYS_CLKDEFS_H
# include <sys/clkdefs.h>
2003-12-04 19:05:14 +03:00
# include <stropts.h>
2000-03-29 16:38:44 +04:00
# endif
# ifdef HAVE_SYS_SIO_H
# include <sys/sio.h>
# endif
#endif /* TTYCLK */
2003-12-04 19:05:14 +03:00
#ifdef KERNEL_PLL
#include "ntp_syscall.h"
#endif /* KERNEL_PLL */
2000-03-29 16:38:44 +04:00
/*
* Reference clock support is provided here by maintaining the fiction
* that the clock is actually a peer. As no packets are exchanged with a
* reference clock, however, we replace the transmit, receive and packet
* procedures with separate code to simulate them. Routines
* refclock_transmit() and refclock_receive() maintain the peer
* variables in a state analogous to an actual peer and pass reference
* clock data on through the filters. Routines refclock_peer() and
* refclock_unpeer() are called to initialize and terminate reference
* clock associations. A set of utility routines is included to open
* serial devices, process sample data, edit input lines to extract
* embedded timestamps and to peform various debugging functions.
*
* The main interface used by these routines is the refclockproc
* structure, which contains for most drivers the decimal equivalants of
* the year, day, month, hour, second and millisecond/microsecond
* decoded from the ASCII timecode. Additional information includes the
* receive timestamp, exception report, statistics tallies, etc. In
* addition, there may be a driver-specific unit structure used for
* local control of the device.
*
* The support routines are passed a pointer to the peer structure,
* which is used for all peer-specific processing and contains a pointer
* to the refclockproc structure, which in turn containes a pointer to
* the unit structure, if used. The peer structure is identified by an
2006-06-11 23:34:07 +04:00
* interface address in the dotted quad form 127.127.t.u (for now only
* IPv4 addresses are used, so we need to be sure the address is it),
* where t is the clock type and u the unit. Some legacy drivers derive
* the refclockproc structure pointer from the table
* typeunit[type][unit]. This interface is strongly discouraged and may
* be abandoned in future.
2000-03-29 16:38:44 +04:00
*/
#define MAXUNIT 8 /* max units */
2003-12-04 19:05:14 +03:00
#define FUDGEFAC .1 /* fudge correction factor */
2006-06-11 23:34:07 +04:00
#define LF 0x0a /* ASCII LF */
2000-03-29 16:38:44 +04:00
2006-06-11 23:34:07 +04:00
#ifdef PPS
int fdpps; /* ppsclock legacy */
#endif /* PPS */
int cal_enable; /* enable refclock calibrate */
2000-03-29 16:38:44 +04:00
/*
* Type/unit peer index. Used to find the peer structure for control and
* debugging. When all clock drivers have been converted to new style,
* this dissapears.
*/
static struct peer *typeunit[REFCLK_MAX + 1][MAXUNIT];
/*
* Forward declarations
*/
#ifdef QSORT_USES_VOID_P
static int refclock_cmpl_fp P((const void *, const void *));
#else
static int refclock_cmpl_fp P((const double *, const double *));
#endif /* QSORT_USES_VOID_P */
static int refclock_sample P((struct refclockproc *));
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* refclock_report - note the occurance of an event
*
* This routine presently just remembers the report and logs it, but
* does nothing heroic for the trap handler. It tries to be a good
* citizen and bothers the system log only if things change.
*/
void
refclock_report(
struct peer *peer,
int code
)
{
struct refclockproc *pp;
2003-12-04 19:05:14 +03:00
pp = peer->procptr;
if (pp == NULL)
2000-03-29 16:38:44 +04:00
return;
2006-06-11 23:34:07 +04:00
switch (code) {
case CEVNT_NOMINAL:
break;
case CEVNT_TIMEOUT:
pp->noreply++;
break;
case CEVNT_BADREPLY:
pp->badformat++;
break;
case CEVNT_FAULT:
break;
case CEVNT_PROP:
break;
case CEVNT_BADDATE:
case CEVNT_BADTIME:
pp->baddata++;
break;
default:
/* shouldn't happen */
break;
}
2000-03-29 16:38:44 +04:00
if (pp->currentstatus != code) {
2003-12-04 19:05:14 +03:00
pp->currentstatus = (u_char)code;
2006-06-11 23:34:07 +04:00
/* RFC1305: copy only iff not CEVNT_NOMINAL */
if (code != CEVNT_NOMINAL)
pp->lastevent = (u_char)code;
2000-03-29 16:38:44 +04:00
if (code == CEVNT_FAULT)
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"clock %s event '%s' (0x%02x)",
refnumtoa(&peer->srcadr),
ceventstr(code), code);
2000-03-29 16:38:44 +04:00
else {
NLOG(NLOG_CLOCKEVENT)
2006-06-11 23:34:07 +04:00
msyslog(LOG_INFO,
"clock %s event '%s' (0x%02x)",
refnumtoa(&peer->srcadr),
ceventstr(code), code);
2000-03-29 16:38:44 +04:00
}
2006-06-11 23:34:07 +04:00
/* RFC1305: post peer clock event */
report_event(EVNT_PEERCLOCK, peer);
2000-03-29 16:38:44 +04:00
}
}
/*
* init_refclock - initialize the reference clock drivers
*
* This routine calls each of the drivers in turn to initialize internal
* variables, if necessary. Most drivers have nothing to say at this
* point.
*/
void
init_refclock(void)
{
int i, j;
for (i = 0; i < (int)num_refclock_conf; i++) {
if (refclock_conf[i]->clock_init != noentry)
(refclock_conf[i]->clock_init)();
for (j = 0; j < MAXUNIT; j++)
typeunit[i][j] = 0;
}
}
/*
* refclock_newpeer - initialize and start a reference clock
*
* This routine allocates and initializes the interface structure which
* supports a reference clock in the form of an ordinary NTP peer. A
* driver-specific support routine completes the initialization, if
* used. Default peer variables which identify the clock and establish
* its reference ID and stratum are set here. It returns one if success
* and zero if the clock address is invalid or already running,
* insufficient resources are available or the driver declares a bum
* rap.
*/
int
refclock_newpeer(
struct peer *peer /* peer structure pointer */
)
{
struct refclockproc *pp;
u_char clktype;
int unit;
/*
* Check for valid clock address. If already running, shut it
* down first.
*/
2003-12-04 19:05:14 +03:00
if (peer->srcadr.ss_family != AF_INET) {
msyslog(LOG_ERR,
"refclock_newpeer: clock address %s invalid, address family not implemented for refclock",
stoa(&peer->srcadr));
return (0);
}
2000-03-29 16:38:44 +04:00
if (!ISREFCLOCKADR(&peer->srcadr)) {
msyslog(LOG_ERR,
"refclock_newpeer: clock address %s invalid",
2003-12-04 19:05:14 +03:00
stoa(&peer->srcadr));
2000-03-29 16:38:44 +04:00
return (0);
}
clktype = (u_char)REFCLOCKTYPE(&peer->srcadr);
unit = REFCLOCKUNIT(&peer->srcadr);
if (clktype >= num_refclock_conf || unit >= MAXUNIT ||
refclock_conf[clktype]->clock_start == noentry) {
msyslog(LOG_ERR,
"refclock_newpeer: clock type %d invalid\n",
clktype);
return (0);
}
/*
* Allocate and initialize interface structure
*/
2003-12-04 19:05:14 +03:00
pp = (struct refclockproc *)emalloc(sizeof(struct refclockproc));
if (pp == NULL)
2000-03-29 16:38:44 +04:00
return (0);
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
memset((char *)pp, 0, sizeof(struct refclockproc));
typeunit[clktype][unit] = peer;
peer->procptr = pp;
/*
* Initialize structures
*/
peer->refclktype = clktype;
2003-12-04 19:05:14 +03:00
peer->refclkunit = (u_char)unit;
2006-06-11 23:34:07 +04:00
peer->flags |= FLAG_REFCLOCK | FLAG_FIXPOLL;
peer->leap = LEAP_NOTINSYNC;
2003-12-04 19:05:14 +03:00
peer->stratum = STRATUM_REFCLOCK;
2006-06-11 23:34:07 +04:00
peer->ppoll = peer->maxpoll;
2000-03-29 16:38:44 +04:00
pp->type = clktype;
pp->timestarted = current_time;
/*
* Set peer.pmode based on the hmode. For appearances only.
*/
switch (peer->hmode) {
2003-12-04 19:05:14 +03:00
case MODE_ACTIVE:
2000-03-29 16:38:44 +04:00
peer->pmode = MODE_PASSIVE;
break;
2003-12-04 19:05:14 +03:00
default:
2000-03-29 16:38:44 +04:00
peer->pmode = MODE_SERVER;
break;
}
/*
* Do driver dependent initialization. The above defaults
* can be wiggled, then finish up for consistency.
*/
if (!((refclock_conf[clktype]->clock_start)(unit, peer))) {
2003-12-04 19:05:14 +03:00
refclock_unpeer(peer);
2000-03-29 16:38:44 +04:00
return (0);
}
2003-12-04 19:05:14 +03:00
peer->refid = pp->refid;
2000-03-29 16:38:44 +04:00
return (1);
}
/*
* refclock_unpeer - shut down a clock
*/
void
refclock_unpeer(
struct peer *peer /* peer structure pointer */
)
{
u_char clktype;
int unit;
/*
* Wiggle the driver to release its resources, then give back
* the interface structure.
*/
if (!peer->procptr)
return;
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
clktype = peer->refclktype;
unit = peer->refclkunit;
if (refclock_conf[clktype]->clock_shutdown != noentry)
(refclock_conf[clktype]->clock_shutdown)(unit, peer);
free(peer->procptr);
peer->procptr = 0;
}
2006-06-11 23:34:07 +04:00
/*
* refclock_timer - called once per second for housekeeping.
*/
void
refclock_timer(
struct peer *peer /* peer structure pointer */
)
{
u_char clktype;
int unit;
clktype = peer->refclktype;
unit = peer->refclkunit;
if (refclock_conf[clktype]->clock_timer != noentry)
(refclock_conf[clktype]->clock_timer)(unit, peer);
}
2000-03-29 16:38:44 +04:00
/*
* refclock_transmit - simulate the transmit procedure
*
* This routine implements the NTP transmit procedure for a reference
* clock. This provides a mechanism to call the driver at the NTP poll
* interval, as well as provides a reachability mechanism to detect a
* broken radio or other madness.
*/
void
refclock_transmit(
struct peer *peer /* peer structure pointer */
)
{
u_char clktype;
int unit;
clktype = peer->refclktype;
unit = peer->refclkunit;
peer->sent++;
2006-06-11 23:34:07 +04:00
get_systime(&peer->xmt);
2000-03-29 16:38:44 +04:00
/*
* This is a ripoff of the peer transmit routine, but
* specialized for reference clocks. We do a little less
* protocol here and call the driver-specific transmit routine.
*/
if (peer->burst == 0) {
u_char oreach;
#ifdef DEBUG
if (debug)
printf("refclock_transmit: at %ld %s\n",
2003-12-04 19:05:14 +03:00
current_time, stoa(&(peer->srcadr)));
2000-03-29 16:38:44 +04:00
#endif
/*
* Update reachability and poll variables like the
* network code.
*/
oreach = peer->reach;
2003-12-04 19:05:14 +03:00
peer->reach <<= 1;
2006-06-11 23:34:07 +04:00
peer->outdate = current_time;
2003-12-04 19:05:14 +03:00
if (!peer->reach) {
if (oreach) {
2000-03-29 16:38:44 +04:00
report_event(EVNT_UNREACH, peer);
peer->timereachable = current_time;
}
} else {
2006-06-11 23:34:07 +04:00
if (!(oreach & 0x07)) {
2000-03-29 16:38:44 +04:00
clock_filter(peer, 0., 0., MAXDISPERSE);
clock_select();
}
if (peer->flags & FLAG_BURST)
peer->burst = NSTAGE;
}
2006-06-11 23:34:07 +04:00
} else {
peer->burst--;
2000-03-29 16:38:44 +04:00
}
if (refclock_conf[clktype]->clock_poll != noentry)
(refclock_conf[clktype]->clock_poll)(unit, peer);
2006-06-11 23:34:07 +04:00
poll_update(peer, peer->hpoll);
2000-03-29 16:38:44 +04:00
}
/*
* Compare two doubles - used with qsort()
*/
#ifdef QSORT_USES_VOID_P
static int
refclock_cmpl_fp(
const void *p1,
const void *p2
)
{
const double *dp1 = (const double *)p1;
const double *dp2 = (const double *)p2;
if (*dp1 < *dp2)
return (-1);
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
if (*dp1 > *dp2)
return (1);
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
return (0);
}
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
#else
static int
refclock_cmpl_fp(
const double *dp1,
const double *dp2
)
{
if (*dp1 < *dp2)
return (-1);
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
if (*dp1 > *dp2)
return (1);
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
return (0);
}
#endif /* QSORT_USES_VOID_P */
/*
* refclock_process_offset - update median filter
*
2003-12-04 19:05:14 +03:00
* This routine uses the given offset and timestamps to construct a new
* entry in the median filter circular buffer. Samples that overflow the
* filter are quietly discarded.
2000-03-29 16:38:44 +04:00
*/
void
refclock_process_offset(
2003-12-04 19:05:14 +03:00
struct refclockproc *pp, /* refclock structure pointer */
l_fp lasttim, /* last timecode timestamp */
l_fp lastrec, /* last receive timestamp */
2000-03-29 16:38:44 +04:00
double fudge
)
{
2003-12-04 19:05:14 +03:00
l_fp lftemp;
2000-03-29 16:38:44 +04:00
double doffset;
pp->lastrec = lastrec;
2003-12-04 19:05:14 +03:00
lftemp = lasttim;
L_SUB(&lftemp, &lastrec);
LFPTOD(&lftemp, doffset);
2000-03-29 16:38:44 +04:00
SAMPLE(doffset + fudge);
}
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* refclock_process - process a sample from the clock
*
* This routine converts the timecode in the form days, hours, minutes,
* seconds and milliseconds/microseconds to internal timestamp format,
* then constructs a new entry in the median filter circular buffer.
* Return success (1) if the data are correct and consistent with the
* converntional calendar.
2006-06-11 23:34:07 +04:00
*
* Important for PPS users: Normally, the pp->lastrec is set to the
* system time when the on-time character is received and the pp->year,
* ..., pp->second decoded and the seconds fraction pp->nsec in
* nanoseconds). When a PPS offset is available, pp->nsec is forced to
* zero and the fraction for pp->lastrec is set to the PPS offset.
*/
2000-03-29 16:38:44 +04:00
int
refclock_process(
2003-12-04 19:05:14 +03:00
struct refclockproc *pp /* refclock structure pointer */
2000-03-29 16:38:44 +04:00
)
{
2003-12-04 19:05:14 +03:00
l_fp offset, ltemp;
2000-03-29 16:38:44 +04:00
/*
* Compute the timecode timestamp from the days, hours, minutes,
* seconds and milliseconds/microseconds of the timecode. Use
* clocktime() for the aggregate seconds and the msec/usec for
* the fraction, when present. Note that this code relies on the
* filesystem time for the years and does not use the years of
* the timecode.
*/
if (!clocktime(pp->day, pp->hour, pp->minute, pp->second, GMT,
pp->lastrec.l_ui, &pp->yearstart, &offset.l_ui))
return (0);
2006-06-11 23:34:07 +04:00
2003-12-04 19:05:14 +03:00
offset.l_uf = 0;
DTOLFP(pp->nsec / 1e9, &ltemp);
L_ADD(&offset, &ltemp);
2000-03-29 16:38:44 +04:00
refclock_process_offset(pp, offset, pp->lastrec,
pp->fudgetime1);
return (1);
}
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* refclock_sample - process a pile of samples from the clock
*
* This routine implements a recursive median filter to suppress spikes
* in the data, as well as determine a performance statistic. It
2006-06-11 23:34:07 +04:00
* calculates the mean offset and RMS jitter. A time adjustment
2003-12-04 19:05:14 +03:00
* fudgetime1 can be added to the final offset to compensate for various
* systematic errors. The routine returns the number of samples
* processed, which could be zero.
2000-03-29 16:38:44 +04:00
*/
static int
refclock_sample(
2003-12-04 19:05:14 +03:00
struct refclockproc *pp /* refclock structure pointer */
2000-03-29 16:38:44 +04:00
)
{
2006-06-11 23:34:07 +04:00
int i, j, k, m, n;
double off[MAXSTAGE];
double offset;
2000-03-29 16:38:44 +04:00
/*
* Copy the raw offsets and sort into ascending order. Don't do
* anything if the buffer is empty.
*/
n = 0;
2003-12-04 19:05:14 +03:00
while (pp->codeproc != pp->coderecv) {
pp->codeproc = (pp->codeproc + 1) % MAXSTAGE;
off[n] = pp->filter[pp->codeproc];
n++;
}
if (n == 0)
return (0);
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
if (n > 1)
2006-06-11 23:34:07 +04:00
qsort(
#ifdef QSORT_USES_VOID_P
(void *)
#else
(char *)
#endif
off, (size_t)n, sizeof(double), refclock_cmpl_fp);
2000-03-29 16:38:44 +04:00
/*
* Reject the furthest from the median of the samples until
* approximately 60 percent of the samples remain.
*/
i = 0; j = n;
2006-06-11 23:34:07 +04:00
m = n - (n * 4) / 10;
2003-12-04 19:05:14 +03:00
while ((j - i) > m) {
2000-03-29 16:38:44 +04:00
offset = off[(j + i) / 2];
if (off[j - 1] - offset < offset - off[i])
i++; /* reject low end */
else
j--; /* reject high end */
}
/*
2003-12-04 19:05:14 +03:00
* Determine the offset and jitter.
2000-03-29 16:38:44 +04:00
*/
2006-06-11 23:34:07 +04:00
pp->offset = 0;
pp->jitter = 0;
for (k = i; k < j; k++) {
pp->offset += off[k];
if (k > i)
pp->jitter += SQUARE(off[k] - off[k - 1]);
}
pp->offset /= m;
pp->jitter = max(SQRT(pp->jitter / m), LOGTOD(sys_precision));
2000-03-29 16:38:44 +04:00
#ifdef DEBUG
if (debug)
printf(
2003-12-04 19:05:14 +03:00
"refclock_sample: n %d offset %.6f disp %.6f jitter %.6f\n",
2006-06-11 23:34:07 +04:00
n, pp->offset, pp->disp, pp->jitter);
2000-03-29 16:38:44 +04:00
#endif
return (n);
}
/*
* refclock_receive - simulate the receive and packet procedures
*
* This routine simulates the NTP receive and packet procedures for a
* reference clock. This provides a mechanism in which the ordinary NTP
* filter, selection and combining algorithms can be used to suppress
* misbehaving radios and to mitigate between them when more than one is
* available for backup.
*/
void
refclock_receive(
struct peer *peer /* peer structure pointer */
)
{
struct refclockproc *pp;
#ifdef DEBUG
if (debug)
printf("refclock_receive: at %lu %s\n",
2003-12-04 19:05:14 +03:00
current_time, stoa(&peer->srcadr));
2000-03-29 16:38:44 +04:00
#endif
/*
* Do a little sanity dance and update the peer structure. Groom
* the median filter samples and give the data to the clock
* filter.
*/
pp = peer->procptr;
peer->leap = pp->leap;
2006-06-11 23:34:07 +04:00
if (peer->leap == LEAP_NOTINSYNC)
2000-03-29 16:38:44 +04:00
return;
2006-06-11 23:34:07 +04:00
peer->received++;
peer->timereceived = current_time;
if (!peer->reach) {
2000-03-29 16:38:44 +04:00
report_event(EVNT_REACH, peer);
2006-06-11 23:34:07 +04:00
peer->timereachable = current_time;
}
2000-03-29 16:38:44 +04:00
peer->reach |= 1;
2003-12-04 19:05:14 +03:00
peer->reftime = pp->lastref;
peer->org = pp->lastrec;
peer->rootdispersion = pp->disp;
2000-03-29 16:38:44 +04:00
get_systime(&peer->rec);
if (!refclock_sample(pp))
return;
2006-06-11 23:34:07 +04:00
2003-12-04 19:05:14 +03:00
clock_filter(peer, pp->offset, 0., pp->jitter);
2000-03-29 16:38:44 +04:00
record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
2003-12-04 19:05:14 +03:00
peer->offset, peer->delay, clock_phi * (current_time -
2006-06-11 23:34:07 +04:00
peer->epoch), peer->jitter);
2003-12-04 19:05:14 +03:00
if (cal_enable && last_offset < MINDISPERSE) {
#ifdef KERNEL_PLL
if (peer != sys_peer || pll_status & STA_PPSTIME)
#else
if (peer != sys_peer)
#endif /* KERNEL_PLL */
pp->fudgetime1 -= pp->offset * FUDGEFAC;
else
pp->fudgetime1 -= pp->fudgetime1 * FUDGEFAC;
}
2000-03-29 16:38:44 +04:00
}
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* refclock_gtlin - groom next input line and extract timestamp
*
* This routine processes the timecode received from the clock and
2006-06-11 23:34:07 +04:00
* strips the parity bit and control characters. It returns the number
* of characters in the line followed by a NULL character ('\0'), which
* is not included in the count. In case of an empty line, the previous
* line is preserved.
2000-03-29 16:38:44 +04:00
*/
int
refclock_gtlin(
struct recvbuf *rbufp, /* receive buffer pointer */
2006-06-11 23:34:07 +04:00
char *lineptr, /* current line pointer */
int bmax, /* remaining characters in line */
l_fp *tsptr /* pointer to timestamp returned */
2000-03-29 16:38:44 +04:00
)
{
2006-06-11 23:34:07 +04:00
char s[BMAX];
char *dpt, *dpend, *dp;
dpt = s;
dpend = s + refclock_gtraw(rbufp, s, BMAX - 1, tsptr);
if (dpend - dpt > bmax - 1)
dpend = dpt + bmax - 1;
for (dp = lineptr; dpt < dpend; dpt++) {
char c;
c = *dpt & 0x7f;
if (c >= 0x20 && c < 0x7f)
*dp++ = c;
}
if (dp == lineptr)
return (0);
*dp = '\0';
return (dp - lineptr);
}
/*
* refclock_gtraw - get next line/chunk of data
*
* This routine returns the raw data received from the clock in both
* canonical or raw modes. The terminal interface routines map CR to LF.
* In canonical mode this results in two lines, one containing data
* followed by LF and another containing only LF. In raw mode the
* interface routines can deliver arbitraty chunks of data from one
* character to a maximum specified by the calling routine. In either
* mode the routine returns the number of characters in the line
* followed by a NULL character ('\0'), which is not included in the
* count.
*
* If a timestamp is present in the timecode, as produced by the tty_clk
* STREAMS module, it returns that as the timestamp; otherwise, it
* returns the buffer timestamp.
*/
int
refclock_gtraw(
struct recvbuf *rbufp, /* receive buffer pointer */
char *lineptr, /* current line pointer */
int bmax, /* remaining characters in line */
l_fp *tsptr /* pointer to timestamp returned */
)
{
char *dpt, *dpend, *dp;
l_fp trtmp, tstmp;
int i;
2000-03-29 16:38:44 +04:00
/*
* Check for the presence of a timestamp left by the tty_clock
* module and, if present, use that instead of the buffer
* timestamp captured by the I/O routines. We recognize a
* timestamp by noting its value is earlier than the buffer
* timestamp, but not more than one second earlier.
*/
2003-12-04 19:05:14 +03:00
dpt = (char *)rbufp->recv_buffer;
2000-03-29 16:38:44 +04:00
dpend = dpt + rbufp->recv_length;
trtmp = rbufp->recv_time;
if (dpend >= dpt + 8) {
if (buftvtots(dpend - 8, &tstmp)) {
L_SUB(&trtmp, &tstmp);
if (trtmp.l_ui == 0) {
#ifdef DEBUG
if (debug > 1) {
printf(
"refclock_gtlin: fd %d ldisc %s",
2006-06-11 23:34:07 +04:00
rbufp->fd, lfptoa(&trtmp,
6));
2000-03-29 16:38:44 +04:00
get_systime(&trtmp);
L_SUB(&trtmp, &tstmp);
2006-06-11 23:34:07 +04:00
printf(" sigio %s\n",
lfptoa(&trtmp, 6));
2000-03-29 16:38:44 +04:00
}
#endif
dpend -= 8;
trtmp = tstmp;
} else
trtmp = rbufp->recv_time;
}
}
/*
2006-06-11 23:34:07 +04:00
* Copy the raw buffer to the user string. The string is padded
* with a NULL, which is not included in the character count.
2000-03-29 16:38:44 +04:00
*/
if (dpend - dpt > bmax - 1)
dpend = dpt + bmax - 1;
2006-06-11 23:34:07 +04:00
for (dp = lineptr; dpt < dpend; dpt++)
*dp++ = *dpt;
*dp = '\0';
2000-03-29 16:38:44 +04:00
i = dp - lineptr;
#ifdef DEBUG
2006-06-11 23:34:07 +04:00
if (debug > 1)
printf("refclock_gtraw: fd %d time %s timecode %d %s\n",
rbufp->fd, ulfptoa(&trtmp, 6), i, lineptr);
2000-03-29 16:38:44 +04:00
#endif
*tsptr = trtmp;
return (i);
}
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* The following code does not apply to WINNT & VMS ...
*/
#if !defined SYS_VXWORKS && !defined SYS_WINNT
#if defined(HAVE_TERMIOS) || defined(HAVE_SYSV_TTYS) || defined(HAVE_BSD_TTYS)
/*
* refclock_open - open serial port for reference clock
*
* This routine opens a serial port for I/O and sets default options. It
* returns the file descriptor if success and zero if failure.
*/
int
refclock_open(
2006-06-11 23:34:07 +04:00
char *dev, /* device name pointer */
u_int speed, /* serial port speed (code) */
u_int lflags /* line discipline flags */
2000-03-29 16:38:44 +04:00
)
{
2006-06-11 23:34:07 +04:00
int fd;
int omode;
2000-03-29 16:38:44 +04:00
/*
* Open serial port and set default options
*/
2003-12-04 19:05:14 +03:00
omode = O_RDWR;
2000-03-29 16:38:44 +04:00
#ifdef O_NONBLOCK
2003-12-04 19:05:14 +03:00
omode |= O_NONBLOCK;
#endif
#ifdef O_NOCTTY
omode |= O_NOCTTY;
#endif
fd = open(dev, omode, 0777);
if (fd < 0) {
2006-06-11 23:34:07 +04:00
msyslog(LOG_ERR, "refclock_open %s: %m", dev);
return (0);
}
if (!refclock_setup(fd, speed, lflags)) {
close(fd);
2000-03-29 16:38:44 +04:00
return (0);
}
2006-06-11 23:34:07 +04:00
if (!refclock_ioctl(fd, lflags)) {
close(fd);
return (0);
}
return (fd);
}
2000-03-29 16:38:44 +04:00
2006-06-11 23:34:07 +04:00
/*
* refclock_setup - initialize terminal interface structure
*/
int
refclock_setup(
int fd, /* file descriptor */
u_int speed, /* serial port speed (code) */
u_int lflags /* line discipline flags */
)
{
int i;
TTY ttyb, *ttyp;
#ifdef PPS
fdpps = fd; /* ppsclock legacy */
#endif /* PPS */
2003-12-04 19:05:14 +03:00
2000-03-29 16:38:44 +04:00
/*
2006-06-11 23:34:07 +04:00
* By default, the serial line port is initialized in canonical
* (line-oriented) mode at specified line speed, 8 bits and no
* parity. LF ends the line and CR is mapped to LF. The break,
* erase and kill functions are disabled. There is a different
* section for each terminal interface, as selected at compile
* time. The flag bits can be used to set raw mode and echo.
2000-03-29 16:38:44 +04:00
*/
ttyp = &ttyb;
#ifdef HAVE_TERMIOS
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* POSIX serial line parameters (termios interface)
*/
if (tcgetattr(fd, ttyp) < 0) {
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"refclock_setup fd %d tcgetattr: %m", fd);
2000-03-29 16:38:44 +04:00
return (0);
}
/*
* Set canonical mode and local connection; set specified speed,
* 8 bits and no parity; map CR to NL; ignore break.
*/
2006-06-11 23:34:07 +04:00
if (speed) {
u_int ltemp = 0;
ttyp->c_iflag = IGNBRK | IGNPAR | ICRNL;
ttyp->c_oflag = 0;
ttyp->c_cflag = CS8 | CLOCAL | CREAD;
if (lflags & LDISC_7O1) {
/* HP Z3801A needs 7-bit, odd parity */
ttyp->c_cflag = CS7 | PARENB | PARODD | CLOCAL | CREAD;
}
cfsetispeed(&ttyb, speed);
cfsetospeed(&ttyb, speed);
for (i = 0; i < NCCS; ++i)
ttyp->c_cc[i] = '\0';
#if defined(TIOCMGET) && !defined(SCO5_CLOCK)
/*
* If we have modem control, check to see if modem leads
* are active; if so, set remote connection. This is
* necessary for the kernel pps mods to work.
*/
if (ioctl(fd, TIOCMGET, (char *)&ltemp) < 0)
msyslog(LOG_ERR,
"refclock_setup fd %d TIOCMGET: %m", fd);
#ifdef DEBUG
if (debug)
printf("refclock_setup fd %d modem status: 0x%x\n",
fd, ltemp);
#endif
if (ltemp & TIOCM_DSR && lflags & LDISC_REMOTE)
ttyp->c_cflag &= ~CLOCAL;
#endif /* TIOCMGET */
2000-03-29 16:38:44 +04:00
}
/*
2006-06-11 23:34:07 +04:00
* Set raw and echo modes. These can be changed on-fly.
2000-03-29 16:38:44 +04:00
*/
2006-06-11 23:34:07 +04:00
ttyp->c_lflag = ICANON;
if (lflags & LDISC_RAW) {
2000-03-29 16:38:44 +04:00
ttyp->c_lflag = 0;
2006-06-11 23:34:07 +04:00
ttyp->c_iflag = 0;
2000-03-29 16:38:44 +04:00
ttyp->c_cc[VMIN] = 1;
}
2006-06-11 23:34:07 +04:00
if (lflags & LDISC_ECHO)
ttyp->c_lflag |= ECHO;
2000-03-29 16:38:44 +04:00
if (tcsetattr(fd, TCSANOW, ttyp) < 0) {
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"refclock_setup fd %d TCSANOW: %m", fd);
2000-03-29 16:38:44 +04:00
return (0);
}
#endif /* HAVE_TERMIOS */
#ifdef HAVE_SYSV_TTYS
/*
* System V serial line parameters (termio interface)
*
*/
if (ioctl(fd, TCGETA, ttyp) < 0) {
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"refclock_setup fd %d TCGETA: %m", fd);
2000-03-29 16:38:44 +04:00
return (0);
}
/*
* Set canonical mode and local connection; set specified speed,
* 8 bits and no parity; map CR to NL; ignore break.
*/
2006-06-11 23:34:07 +04:00
if (speed) {
u_int ltemp = 0;
ttyp->c_iflag = IGNBRK | IGNPAR | ICRNL;
ttyp->c_oflag = 0;
ttyp->c_cflag = speed | CS8 | CLOCAL | CREAD;
for (i = 0; i < NCCS; ++i)
ttyp->c_cc[i] = '\0';
#if defined(TIOCMGET) && !defined(SCO5_CLOCK)
/*
* If we have modem control, check to see if modem leads
* are active; if so, set remote connection. This is
* necessary for the kernel pps mods to work.
*/
if (ioctl(fd, TIOCMGET, (char *)&ltemp) < 0)
msyslog(LOG_ERR,
"refclock_setup fd %d TIOCMGET: %m", fd);
#ifdef DEBUG
if (debug)
printf("refclock_setup fd %d modem status: %x\n",
fd, ltemp);
#endif
if (ltemp & TIOCM_DSR)
ttyp->c_cflag &= ~CLOCAL;
#endif /* TIOCMGET */
}
2000-03-29 16:38:44 +04:00
/*
2006-06-11 23:34:07 +04:00
* Set raw and echo modes. These can be changed on-fly.
2000-03-29 16:38:44 +04:00
*/
2006-06-11 23:34:07 +04:00
ttyp->c_lflag = ICANON;
if (lflags & LDISC_RAW) {
2000-03-29 16:38:44 +04:00
ttyp->c_lflag = 0;
2006-06-11 23:34:07 +04:00
ttyp->c_iflag = 0;
ttyp->c_cc[VMIN] = 1;
2000-03-29 16:38:44 +04:00
}
if (ioctl(fd, TCSETA, ttyp) < 0) {
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"refclock_setup fd %d TCSETA: %m", fd);
2000-03-29 16:38:44 +04:00
return (0);
}
#endif /* HAVE_SYSV_TTYS */
#ifdef HAVE_BSD_TTYS
/*
* 4.3bsd serial line parameters (sgttyb interface)
*/
if (ioctl(fd, TIOCGETP, (char *)ttyp) < 0) {
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"refclock_setup fd %d TIOCGETP: %m", fd);
2000-03-29 16:38:44 +04:00
return (0);
}
2006-06-11 23:34:07 +04:00
if (speed)
ttyp->sg_ispeed = ttyp->sg_ospeed = speed;
2000-03-29 16:38:44 +04:00
ttyp->sg_flags = EVENP | ODDP | CRMOD;
if (ioctl(fd, TIOCSETP, (char *)ttyp) < 0) {
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"refclock_setup TIOCSETP: %m");
2000-03-29 16:38:44 +04:00
return (0);
}
#endif /* HAVE_BSD_TTYS */
2006-06-11 23:34:07 +04:00
return(1);
2000-03-29 16:38:44 +04:00
}
#endif /* HAVE_TERMIOS || HAVE_SYSV_TTYS || HAVE_BSD_TTYS */
#endif /* SYS_VXWORKS SYS_WINNT */
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* refclock_ioctl - set serial port control functions
*
* This routine attempts to hide the internal, system-specific details
* of serial ports. It can handle POSIX (termios), SYSV (termio) and BSD
* (sgtty) interfaces with varying degrees of success. The routine sets
2003-12-04 19:05:14 +03:00
* up optional features such as tty_clk. The routine returns 1 if
* success and 0 if failure.
2000-03-29 16:38:44 +04:00
*/
int
refclock_ioctl(
2006-06-11 23:34:07 +04:00
int fd, /* file descriptor */
u_int lflags /* line discipline flags */
2000-03-29 16:38:44 +04:00
)
{
2006-06-11 23:34:07 +04:00
/*
* simply return 1 if no UNIX line discipline is supported
*/
2000-03-29 16:38:44 +04:00
#if !defined SYS_VXWORKS && !defined SYS_WINNT
#if defined(HAVE_TERMIOS) || defined(HAVE_SYSV_TTYS) || defined(HAVE_BSD_TTYS)
#ifdef DEBUG
if (debug)
2006-06-11 23:34:07 +04:00
printf("refclock_ioctl: fd %d flags 0x%x\n", fd,
lflags);
2000-03-29 16:38:44 +04:00
#endif
#ifdef TTYCLK
/*
* The TTYCLK option provides timestamping at the driver level.
* It requires the tty_clk streams module and System V STREAMS
* support. If not available, don't complain.
*/
2006-06-11 23:34:07 +04:00
if (lflags & (LDISC_CLK | LDISC_CLKPPS | LDISC_ACTS)) {
2000-03-29 16:38:44 +04:00
int rval = 0;
if (ioctl(fd, I_PUSH, "clk") < 0) {
msyslog(LOG_NOTICE,
2006-06-11 23:34:07 +04:00
"refclock_ioctl fd %d I_PUSH: %m", fd);
return (0);
#ifdef CLK_SETSTR
2000-03-29 16:38:44 +04:00
} else {
char *str;
2006-06-11 23:34:07 +04:00
if (lflags & LDISC_CLKPPS)
2000-03-29 16:38:44 +04:00
str = "\377";
2006-06-11 23:34:07 +04:00
else if (lflags & LDISC_ACTS)
2000-03-29 16:38:44 +04:00
str = "*";
else
str = "\n";
2006-06-11 23:34:07 +04:00
if (ioctl(fd, CLK_SETSTR, str) < 0) {
2000-03-29 16:38:44 +04:00
msyslog(LOG_ERR,
2006-06-11 23:34:07 +04:00
"refclock_ioctl fd %d CLK_SETSTR: %m", fd);
return (0);
}
#endif /*CLK_SETSTR */
2000-03-29 16:38:44 +04:00
}
}
#endif /* TTYCLK */
#endif /* HAVE_TERMIOS || HAVE_SYSV_TTYS || HAVE_BSD_TTYS */
#endif /* SYS_VXWORKS SYS_WINNT */
return (1);
}
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
/*
* refclock_control - set and/or return clock values
*
* This routine is used mainly for debugging. It returns designated
* values from the interface structure that can be displayed using
* ntpdc and the clockstat command. It can also be used to initialize
* configuration variables, such as fudgetimes, fudgevalues, reference
* ID and stratum.
*/
void
refclock_control(
2003-12-04 19:05:14 +03:00
struct sockaddr_storage *srcadr,
2000-03-29 16:38:44 +04:00
struct refclockstat *in,
struct refclockstat *out
)
{
struct peer *peer;
struct refclockproc *pp;
u_char clktype;
int unit;
/*
* Check for valid address and running peer
*/
2003-12-04 19:05:14 +03:00
if (srcadr->ss_family != AF_INET)
return;
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
if (!ISREFCLOCKADR(srcadr))
return;
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
clktype = (u_char)REFCLOCKTYPE(srcadr);
unit = REFCLOCKUNIT(srcadr);
if (clktype >= num_refclock_conf || unit >= MAXUNIT)
return;
2006-06-11 23:34:07 +04:00
2003-12-04 19:05:14 +03:00
peer = typeunit[clktype][unit];
if (peer == NULL)
return;
2006-06-11 23:34:07 +04:00
2003-12-04 19:05:14 +03:00
if (peer->procptr == NULL)
2000-03-29 16:38:44 +04:00
return;
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
pp = peer->procptr;
/*
* Initialize requested data
*/
if (in != 0) {
if (in->haveflags & CLK_HAVETIME1)
pp->fudgetime1 = in->fudgetime1;
if (in->haveflags & CLK_HAVETIME2)
pp->fudgetime2 = in->fudgetime2;
if (in->haveflags & CLK_HAVEVAL1)
2006-06-11 23:34:07 +04:00
peer->stratum = pp->stratum = (u_char)in->fudgeval1;
2000-03-29 16:38:44 +04:00
if (in->haveflags & CLK_HAVEVAL2)
2006-06-11 23:34:07 +04:00
peer->refid = pp->refid = in->fudgeval2;
2000-03-29 16:38:44 +04:00
if (in->haveflags & CLK_HAVEFLAG1) {
pp->sloppyclockflag &= ~CLK_FLAG1;
pp->sloppyclockflag |= in->flags & CLK_FLAG1;
}
if (in->haveflags & CLK_HAVEFLAG2) {
pp->sloppyclockflag &= ~CLK_FLAG2;
pp->sloppyclockflag |= in->flags & CLK_FLAG2;
}
if (in->haveflags & CLK_HAVEFLAG3) {
pp->sloppyclockflag &= ~CLK_FLAG3;
pp->sloppyclockflag |= in->flags & CLK_FLAG3;
}
if (in->haveflags & CLK_HAVEFLAG4) {
pp->sloppyclockflag &= ~CLK_FLAG4;
pp->sloppyclockflag |= in->flags & CLK_FLAG4;
}
}
/*
* Readback requested data
*/
if (out != 0) {
out->haveflags = CLK_HAVETIME1 | CLK_HAVEVAL1 |
CLK_HAVEVAL2 | CLK_HAVEFLAG4;
out->fudgetime1 = pp->fudgetime1;
out->fudgetime2 = pp->fudgetime2;
2003-12-04 19:05:14 +03:00
out->fudgeval1 = pp->stratum;
2000-03-29 16:38:44 +04:00
out->fudgeval2 = pp->refid;
out->flags = (u_char) pp->sloppyclockflag;
out->timereset = current_time - pp->timestarted;
out->polls = pp->polls;
out->noresponse = pp->noreply;
out->badformat = pp->badformat;
out->baddata = pp->baddata;
out->lastevent = pp->lastevent;
out->currentstatus = pp->currentstatus;
out->type = pp->type;
out->clockdesc = pp->clockdesc;
out->lencode = pp->lencode;
out->p_lastcode = pp->a_lastcode;
}
/*
* Give the stuff to the clock
*/
if (refclock_conf[clktype]->clock_control != noentry)
(refclock_conf[clktype]->clock_control)(unit, in, out, peer);
}
/*
* refclock_buginfo - return debugging info
*
* This routine is used mainly for debugging. It returns designated
* values from the interface structure that can be displayed using
* ntpdc and the clkbug command.
*/
void
refclock_buginfo(
2003-12-04 19:05:14 +03:00
struct sockaddr_storage *srcadr, /* clock address */
2000-03-29 16:38:44 +04:00
struct refclockbug *bug /* output structure */
)
{
struct peer *peer;
struct refclockproc *pp;
u_char clktype;
int unit;
int i;
/*
* Check for valid address and peer structure
*/
2003-12-04 19:05:14 +03:00
if (srcadr->ss_family != AF_INET)
return;
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
if (!ISREFCLOCKADR(srcadr))
return;
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
clktype = (u_char) REFCLOCKTYPE(srcadr);
unit = REFCLOCKUNIT(srcadr);
if (clktype >= num_refclock_conf || unit >= MAXUNIT)
return;
2006-06-11 23:34:07 +04:00
2003-12-04 19:05:14 +03:00
peer = typeunit[clktype][unit];
if (peer == NULL)
2000-03-29 16:38:44 +04:00
return;
2006-06-11 23:34:07 +04:00
2000-03-29 16:38:44 +04:00
pp = peer->procptr;
/*
* Copy structure values
*/
bug->nvalues = 8;
bug->svalues = 0x0000003f;
bug->values[0] = pp->year;
bug->values[1] = pp->day;
bug->values[2] = pp->hour;
bug->values[3] = pp->minute;
bug->values[4] = pp->second;
2003-12-04 19:05:14 +03:00
bug->values[5] = pp->nsec;
2000-03-29 16:38:44 +04:00
bug->values[6] = pp->yearstart;
bug->values[7] = pp->coderecv;
bug->stimes = 0xfffffffc;
bug->times[0] = pp->lastref;
bug->times[1] = pp->lastrec;
for (i = 2; i < (int)bug->ntimes; i++)
DTOLFP(pp->filter[i - 2], &bug->times[i]);
/*
* Give the stuff to the clock
*/
if (refclock_conf[clktype]->clock_buginfo != noentry)
(refclock_conf[clktype]->clock_buginfo)(unit, bug, peer);
}
#endif /* REFCLOCK */