2 * ntp_refclock - processing support for reference clocks
10 #include "ntp_unixtime.h"
12 #include "ntp_refclock.h"
13 #include "ntp_stdlib.h"
17 #ifdef HAVE_SYS_IOCTL_H
18 # include <sys/ioctl.h>
19 #endif /* HAVE_SYS_IOCTL_H */
24 # ifdef HAVE_SYS_CLKDEFS_H
25 # include <sys/clkdefs.h>
28 # ifdef HAVE_SYS_SIO_H
34 #include "ntp_syscall.h"
35 #endif /* KERNEL_PLL */
38 * Reference clock support is provided here by maintaining the fiction
39 * that the clock is actually a peer. As no packets are exchanged with a
40 * reference clock, however, we replace the transmit, receive and packet
41 * procedures with separate code to simulate them. Routines
42 * refclock_transmit() and refclock_receive() maintain the peer
43 * variables in a state analogous to an actual peer and pass reference
44 * clock data on through the filters. Routines refclock_peer() and
45 * refclock_unpeer() are called to initialize and terminate reference
46 * clock associations. A set of utility routines is included to open
47 * serial devices, process sample data, edit input lines to extract
48 * embedded timestamps and to peform various debugging functions.
50 * The main interface used by these routines is the refclockproc
51 * structure, which contains for most drivers the decimal equivalants of
52 * the year, day, month, hour, second and millisecond/microsecond
53 * decoded from the ASCII timecode. Additional information includes the
54 * receive timestamp, exception report, statistics tallies, etc. In
55 * addition, there may be a driver-specific unit structure used for
56 * local control of the device.
58 * The support routines are passed a pointer to the peer structure,
59 * which is used for all peer-specific processing and contains a pointer
60 * to the refclockproc structure, which in turn containes a pointer to
61 * the unit structure, if used. The peer structure is identified by an
62 * interface address in the dotted quad form 127.127.t.u (for now only
63 * IPv4 addresses are used, so we need to be sure the address is it),
64 * where t is the clock type and u the unit. Some legacy drivers derive
65 * the refclockproc structure pointer from the table
66 * typeunit[type][unit]. This interface is strongly discouraged and may
67 * be abandoned in future.
69 #define MAXUNIT 4 /* max units */
70 #define FUDGEFAC .1 /* fudge correction factor */
71 #define LF 0x0a /* ASCII LF */
74 int fdpps; /* ppsclock legacy */
76 int cal_enable; /* enable refclock calibrate */
79 * Type/unit peer index. Used to find the peer structure for control and
80 * debugging. When all clock drivers have been converted to new style,
83 static struct peer *typeunit[REFCLK_MAX + 1][MAXUNIT];
86 * Forward declarations
88 #ifdef QSORT_USES_VOID_P
89 static int refclock_cmpl_fp P((const void *, const void *));
91 static int refclock_cmpl_fp P((const double *, const double *));
92 #endif /* QSORT_USES_VOID_P */
93 static int refclock_sample P((struct refclockproc *));
97 * refclock_report - note the occurance of an event
99 * This routine presently just remembers the report and logs it, but
100 * does nothing heroic for the trap handler. It tries to be a good
101 * citizen and bothers the system log only if things change.
109 struct refclockproc *pp;
139 /* shouldn't happen */
143 if (pp->currentstatus != code) {
144 pp->currentstatus = (u_char)code;
146 /* RFC1305: copy only iff not CEVNT_NOMINAL */
147 if (code != CEVNT_NOMINAL)
148 pp->lastevent = (u_char)code;
150 if (code == CEVNT_FAULT)
152 "clock %s event '%s' (0x%02x)",
153 refnumtoa(&peer->srcadr),
154 ceventstr(code), code);
156 NLOG(NLOG_CLOCKEVENT)
158 "clock %s event '%s' (0x%02x)",
159 refnumtoa(&peer->srcadr),
160 ceventstr(code), code);
163 /* RFC1305: post peer clock event */
164 report_event(EVNT_PEERCLOCK, peer);
169 * init_refclock - initialize the reference clock drivers
171 * This routine calls each of the drivers in turn to initialize internal
172 * variables, if necessary. Most drivers have nothing to say at this
180 for (i = 0; i < (int)num_refclock_conf; i++) {
181 if (refclock_conf[i]->clock_init != noentry)
182 (refclock_conf[i]->clock_init)();
183 for (j = 0; j < MAXUNIT; j++)
190 * refclock_newpeer - initialize and start a reference clock
192 * This routine allocates and initializes the interface structure which
193 * supports a reference clock in the form of an ordinary NTP peer. A
194 * driver-specific support routine completes the initialization, if
195 * used. Default peer variables which identify the clock and establish
196 * its reference ID and stratum are set here. It returns one if success
197 * and zero if the clock address is invalid or already running,
198 * insufficient resources are available or the driver declares a bum
203 struct peer *peer /* peer structure pointer */
206 struct refclockproc *pp;
211 * Check for valid clock address. If already running, shut it
214 if (peer->srcadr.ss_family != AF_INET) {
216 "refclock_newpeer: clock address %s invalid, address family not implemented for refclock",
217 stoa(&peer->srcadr));
220 if (!ISREFCLOCKADR(&peer->srcadr)) {
222 "refclock_newpeer: clock address %s invalid",
223 stoa(&peer->srcadr));
226 clktype = (u_char)REFCLOCKTYPE(&peer->srcadr);
227 unit = REFCLOCKUNIT(&peer->srcadr);
228 if (clktype >= num_refclock_conf || unit >= MAXUNIT ||
229 refclock_conf[clktype]->clock_start == noentry) {
231 "refclock_newpeer: clock type %d invalid\n",
237 * Allocate and initialize interface structure
239 pp = (struct refclockproc *)emalloc(sizeof(struct refclockproc));
243 memset((char *)pp, 0, sizeof(struct refclockproc));
244 typeunit[clktype][unit] = peer;
248 * Initialize structures
250 peer->refclktype = clktype;
251 peer->refclkunit = (u_char)unit;
252 peer->flags |= FLAG_REFCLOCK | FLAG_FIXPOLL;
253 peer->leap = LEAP_NOTINSYNC;
254 peer->stratum = STRATUM_REFCLOCK;
255 peer->ppoll = peer->maxpoll;
257 pp->timestarted = current_time;
260 * Set peer.pmode based on the hmode. For appearances only.
262 switch (peer->hmode) {
264 peer->pmode = MODE_PASSIVE;
268 peer->pmode = MODE_SERVER;
273 * Do driver dependent initialization. The above defaults
274 * can be wiggled, then finish up for consistency.
276 if (!((refclock_conf[clktype]->clock_start)(unit, peer))) {
277 refclock_unpeer(peer);
280 peer->refid = pp->refid;
286 * refclock_unpeer - shut down a clock
290 struct peer *peer /* peer structure pointer */
297 * Wiggle the driver to release its resources, then give back
298 * the interface structure.
303 clktype = peer->refclktype;
304 unit = peer->refclkunit;
305 if (refclock_conf[clktype]->clock_shutdown != noentry)
306 (refclock_conf[clktype]->clock_shutdown)(unit, peer);
313 * refclock_timer - called once per second for housekeeping.
317 struct peer *peer /* peer structure pointer */
323 clktype = peer->refclktype;
324 unit = peer->refclkunit;
325 if (refclock_conf[clktype]->clock_timer != noentry)
326 (refclock_conf[clktype]->clock_timer)(unit, peer);
331 * refclock_transmit - simulate the transmit procedure
333 * This routine implements the NTP transmit procedure for a reference
334 * clock. This provides a mechanism to call the driver at the NTP poll
335 * interval, as well as provides a reachability mechanism to detect a
336 * broken radio or other madness.
340 struct peer *peer /* peer structure pointer */
346 clktype = peer->refclktype;
347 unit = peer->refclkunit;
349 get_systime(&peer->xmt);
352 * This is a ripoff of the peer transmit routine, but
353 * specialized for reference clocks. We do a little less
354 * protocol here and call the driver-specific transmit routine.
356 if (peer->burst == 0) {
360 printf("refclock_transmit: at %ld %s\n",
361 current_time, stoa(&(peer->srcadr)));
365 * Update reachability and poll variables like the
368 oreach = peer->reach;
370 peer->outdate = current_time;
373 report_event(EVNT_UNREACH, peer);
374 peer->timereachable = current_time;
377 if (!(oreach & 0x07)) {
378 clock_filter(peer, 0., 0., MAXDISPERSE);
381 if (peer->flags & FLAG_BURST)
382 peer->burst = NSTAGE;
387 if (refclock_conf[clktype]->clock_poll != noentry)
388 (refclock_conf[clktype]->clock_poll)(unit, peer);
389 poll_update(peer, peer->hpoll);
394 * Compare two doubles - used with qsort()
396 #ifdef QSORT_USES_VOID_P
403 const double *dp1 = (const double *)p1;
404 const double *dp2 = (const double *)p2;
430 #endif /* QSORT_USES_VOID_P */
434 * refclock_process_offset - update median filter
436 * This routine uses the given offset and timestamps to construct a new
437 * entry in the median filter circular buffer. Samples that overflow the
438 * filter are quietly discarded.
441 refclock_process_offset(
442 struct refclockproc *pp, /* refclock structure pointer */
443 l_fp lasttim, /* last timecode timestamp */
444 l_fp lastrec, /* last receive timestamp */
451 pp->lastrec = lastrec;
453 L_SUB(&lftemp, &lastrec);
454 LFPTOD(&lftemp, doffset);
455 SAMPLE(doffset + fudge);
460 * refclock_process - process a sample from the clock
462 * This routine converts the timecode in the form days, hours, minutes,
463 * seconds and milliseconds/microseconds to internal timestamp format,
464 * then constructs a new entry in the median filter circular buffer.
465 * Return success (1) if the data are correct and consistent with the
466 * converntional calendar.
468 * Important for PPS users: Normally, the pp->lastrec is set to the
469 * system time when the on-time character is received and the pp->year,
470 * ..., pp->second decoded and the seconds fraction pp->nsec in
471 * nanoseconds). When a PPS offset is available, pp->nsec is forced to
472 * zero and the fraction for pp->lastrec is set to the PPS offset.
476 struct refclockproc *pp /* refclock structure pointer */
482 * Compute the timecode timestamp from the days, hours, minutes,
483 * seconds and milliseconds/microseconds of the timecode. Use
484 * clocktime() for the aggregate seconds and the msec/usec for
485 * the fraction, when present. Note that this code relies on the
486 * filesystem time for the years and does not use the years of
489 if (!clocktime(pp->day, pp->hour, pp->minute, pp->second, GMT,
490 pp->lastrec.l_ui, &pp->yearstart, &offset.l_ui))
494 DTOLFP(pp->nsec / 1e9, <emp);
495 L_ADD(&offset, <emp);
496 refclock_process_offset(pp, offset, pp->lastrec,
503 * refclock_sample - process a pile of samples from the clock
505 * This routine implements a recursive median filter to suppress spikes
506 * in the data, as well as determine a performance statistic. It
507 * calculates the mean offset and RMS jitter. A time adjustment
508 * fudgetime1 can be added to the final offset to compensate for various
509 * systematic errors. The routine returns the number of samples
510 * processed, which could be zero.
514 struct refclockproc *pp /* refclock structure pointer */
518 double off[MAXSTAGE];
522 * Copy the raw offsets and sort into ascending order. Don't do
523 * anything if the buffer is empty.
526 while (pp->codeproc != pp->coderecv) {
527 pp->codeproc = (pp->codeproc + 1) % MAXSTAGE;
528 off[n] = pp->filter[pp->codeproc];
536 #ifdef QSORT_USES_VOID_P
541 off, (size_t)n, sizeof(double), refclock_cmpl_fp);
544 * Reject the furthest from the median of the samples until
545 * approximately 60 percent of the samples remain.
548 m = n - (n * 4) / 10;
549 while ((j - i) > m) {
550 offset = off[(j + i) / 2];
551 if (off[j - 1] - offset < offset - off[i])
552 i++; /* reject low end */
554 j--; /* reject high end */
558 * Determine the offset and jitter.
562 for (k = i; k < j; k++) {
563 pp->offset += off[k];
565 pp->jitter += SQUARE(off[k] - off[k - 1]);
568 pp->jitter = max(SQRT(pp->jitter / m), LOGTOD(sys_precision));
572 "refclock_sample: n %d offset %.6f disp %.6f jitter %.6f\n",
573 n, pp->offset, pp->disp, pp->jitter);
580 * refclock_receive - simulate the receive and packet procedures
582 * This routine simulates the NTP receive and packet procedures for a
583 * reference clock. This provides a mechanism in which the ordinary NTP
584 * filter, selection and combining algorithms can be used to suppress
585 * misbehaving radios and to mitigate between them when more than one is
586 * available for backup.
590 struct peer *peer /* peer structure pointer */
593 struct refclockproc *pp;
597 printf("refclock_receive: at %lu %s\n",
598 current_time, stoa(&peer->srcadr));
602 * Do a little sanity dance and update the peer structure. Groom
603 * the median filter samples and give the data to the clock
607 peer->leap = pp->leap;
608 if (peer->leap == LEAP_NOTINSYNC)
612 peer->timereceived = current_time;
614 report_event(EVNT_REACH, peer);
615 peer->timereachable = current_time;
618 peer->reftime = pp->lastref;
619 peer->org = pp->lastrec;
620 peer->rootdispersion = pp->disp;
621 get_systime(&peer->rec);
622 if (!refclock_sample(pp))
625 clock_filter(peer, pp->offset, 0., pp->jitter);
626 record_peer_stats(&peer->srcadr, ctlpeerstatus(peer),
627 peer->offset, peer->delay, clock_phi * (current_time -
628 peer->epoch), peer->jitter);
629 if (cal_enable && last_offset < MINDISPERSE) {
631 if (peer != sys_peer || pll_status & STA_PPSTIME)
633 if (peer != sys_peer)
634 #endif /* KERNEL_PLL */
635 pp->fudgetime1 -= pp->offset * FUDGEFAC;
637 pp->fudgetime1 -= pp->fudgetime1 * FUDGEFAC;
643 * refclock_gtlin - groom next input line and extract timestamp
645 * This routine processes the timecode received from the clock and
646 * strips the parity bit and control characters. It returns the number
647 * of characters in the line followed by a NULL character ('\0'), which
648 * is not included in the count. In case of an empty line, the previous
653 struct recvbuf *rbufp, /* receive buffer pointer */
654 char *lineptr, /* current line pointer */
655 int bmax, /* remaining characters in line */
656 l_fp *tsptr /* pointer to timestamp returned */
660 char *dpt, *dpend, *dp;
663 dpend = s + refclock_gtraw(rbufp, s, BMAX - 1, tsptr);
664 if (dpend - dpt > bmax - 1)
665 dpend = dpt + bmax - 1;
666 for (dp = lineptr; dpt < dpend; dpt++) {
670 if (c >= 0x20 && c < 0x7f)
677 return (dp - lineptr);
682 * refclock_gtraw - get next line/chunk of data
684 * This routine returns the raw data received from the clock in both
685 * canonical or raw modes. The terminal interface routines map CR to LF.
686 * In canonical mode this results in two lines, one containing data
687 * followed by LF and another containing only LF. In raw mode the
688 * interface routines can deliver arbitraty chunks of data from one
689 * character to a maximum specified by the calling routine. In either
690 * mode the routine returns the number of characters in the line
691 * followed by a NULL character ('\0'), which is not included in the
694 * If a timestamp is present in the timecode, as produced by the tty_clk
695 * STREAMS module, it returns that as the timestamp; otherwise, it
696 * returns the buffer timestamp.
700 struct recvbuf *rbufp, /* receive buffer pointer */
701 char *lineptr, /* current line pointer */
702 int bmax, /* remaining characters in line */
703 l_fp *tsptr /* pointer to timestamp returned */
706 char *dpt, *dpend, *dp;
711 * Check for the presence of a timestamp left by the tty_clock
712 * module and, if present, use that instead of the buffer
713 * timestamp captured by the I/O routines. We recognize a
714 * timestamp by noting its value is earlier than the buffer
715 * timestamp, but not more than one second earlier.
717 dpt = (char *)rbufp->recv_buffer;
718 dpend = dpt + rbufp->recv_length;
719 trtmp = rbufp->recv_time;
720 if (dpend >= dpt + 8) {
721 if (buftvtots(dpend - 8, &tstmp)) {
722 L_SUB(&trtmp, &tstmp);
723 if (trtmp.l_ui == 0) {
727 "refclock_gtlin: fd %d ldisc %s",
728 rbufp->fd, lfptoa(&trtmp,
731 L_SUB(&trtmp, &tstmp);
732 printf(" sigio %s\n",
739 trtmp = rbufp->recv_time;
744 * Copy the raw buffer to the user string. The string is padded
745 * with a NULL, which is not included in the character count.
747 if (dpend - dpt > bmax - 1)
748 dpend = dpt + bmax - 1;
749 for (dp = lineptr; dpt < dpend; dpt++)
755 printf("refclock_gtraw: fd %d time %s timecode %d %s\n",
756 rbufp->fd, ulfptoa(&trtmp, 6), i, lineptr);
764 * The following code does not apply to WINNT & VMS ...
766 #if !defined SYS_VXWORKS && !defined SYS_WINNT
767 #if defined(HAVE_TERMIOS) || defined(HAVE_SYSV_TTYS) || defined(HAVE_BSD_TTYS)
770 * refclock_open - open serial port for reference clock
772 * This routine opens a serial port for I/O and sets default options. It
773 * returns the file descriptor if success and zero if failure.
777 char *dev, /* device name pointer */
778 u_int speed, /* serial port speed (code) */
779 u_int lflags /* line discipline flags */
786 * Open serial port and set default options
796 fd = open(dev, omode, 0777);
798 msyslog(LOG_ERR, "refclock_open %s: %m", dev);
801 if (!refclock_setup(fd, speed, lflags)) {
805 if (!refclock_ioctl(fd, lflags)) {
813 * refclock_setup - initialize terminal interface structure
817 int fd, /* file descriptor */
818 u_int speed, /* serial port speed (code) */
819 u_int lflags /* line discipline flags */
825 fdpps = fd; /* ppsclock legacy */
829 * By default, the serial line port is initialized in canonical
830 * (line-oriented) mode at specified line speed, 8 bits and no
831 * parity. LF ends the line and CR is mapped to LF. The break,
832 * erase and kill functions are disabled. There is a different
833 * section for each terminal interface, as selected at compile
834 * time. The flag bits can be used to set raw mode and echo.
840 * POSIX serial line parameters (termios interface)
842 if (tcgetattr(fd, ttyp) < 0) {
844 "refclock_setup fd %d tcgetattr: %m", fd);
849 * Set canonical mode and local connection; set specified speed,
850 * 8 bits and no parity; map CR to NL; ignore break.
855 ttyp->c_iflag = IGNBRK | IGNPAR | ICRNL;
857 ttyp->c_cflag = CS8 | CLOCAL | CREAD;
858 if (lflags & LDISC_7O1) {
859 /* HP Z3801A needs 7-bit, odd parity */
860 ttyp->c_cflag = CS7 | PARENB | PARODD | CLOCAL | CREAD;
862 cfsetispeed(&ttyb, speed);
863 cfsetospeed(&ttyb, speed);
864 for (i = 0; i < NCCS; ++i)
865 ttyp->c_cc[i] = '\0';
867 #if defined(TIOCMGET) && !defined(SCO5_CLOCK)
870 * If we have modem control, check to see if modem leads
871 * are active; if so, set remote connection. This is
872 * necessary for the kernel pps mods to work.
874 if (ioctl(fd, TIOCMGET, (char *)<emp) < 0)
876 "refclock_setup fd %d TIOCMGET: %m", fd);
879 printf("refclock_setup fd %d modem status: 0x%x\n",
882 if (ltemp & TIOCM_DSR && lflags & LDISC_REMOTE)
883 ttyp->c_cflag &= ~CLOCAL;
884 #endif /* TIOCMGET */
888 * Set raw and echo modes. These can be changed on-fly.
890 ttyp->c_lflag = ICANON;
891 if (lflags & LDISC_RAW) {
894 ttyp->c_cc[VMIN] = 1;
896 if (lflags & LDISC_ECHO)
897 ttyp->c_lflag |= ECHO;
898 if (tcsetattr(fd, TCSANOW, ttyp) < 0) {
900 "refclock_setup fd %d TCSANOW: %m", fd);
903 #endif /* HAVE_TERMIOS */
905 #ifdef HAVE_SYSV_TTYS
908 * System V serial line parameters (termio interface)
911 if (ioctl(fd, TCGETA, ttyp) < 0) {
913 "refclock_setup fd %d TCGETA: %m", fd);
918 * Set canonical mode and local connection; set specified speed,
919 * 8 bits and no parity; map CR to NL; ignore break.
924 ttyp->c_iflag = IGNBRK | IGNPAR | ICRNL;
926 ttyp->c_cflag = speed | CS8 | CLOCAL | CREAD;
927 for (i = 0; i < NCCS; ++i)
928 ttyp->c_cc[i] = '\0';
930 #if defined(TIOCMGET) && !defined(SCO5_CLOCK)
933 * If we have modem control, check to see if modem leads
934 * are active; if so, set remote connection. This is
935 * necessary for the kernel pps mods to work.
937 if (ioctl(fd, TIOCMGET, (char *)<emp) < 0)
939 "refclock_setup fd %d TIOCMGET: %m", fd);
942 printf("refclock_setup fd %d modem status: %x\n",
945 if (ltemp & TIOCM_DSR)
946 ttyp->c_cflag &= ~CLOCAL;
947 #endif /* TIOCMGET */
951 * Set raw and echo modes. These can be changed on-fly.
953 ttyp->c_lflag = ICANON;
954 if (lflags & LDISC_RAW) {
957 ttyp->c_cc[VMIN] = 1;
959 if (ioctl(fd, TCSETA, ttyp) < 0) {
961 "refclock_setup fd %d TCSETA: %m", fd);
964 #endif /* HAVE_SYSV_TTYS */
969 * 4.3bsd serial line parameters (sgttyb interface)
971 if (ioctl(fd, TIOCGETP, (char *)ttyp) < 0) {
973 "refclock_setup fd %d TIOCGETP: %m", fd);
977 ttyp->sg_ispeed = ttyp->sg_ospeed = speed;
978 ttyp->sg_flags = EVENP | ODDP | CRMOD;
979 if (ioctl(fd, TIOCSETP, (char *)ttyp) < 0) {
981 "refclock_setup TIOCSETP: %m");
984 #endif /* HAVE_BSD_TTYS */
987 #endif /* HAVE_TERMIOS || HAVE_SYSV_TTYS || HAVE_BSD_TTYS */
988 #endif /* SYS_VXWORKS SYS_WINNT */
992 * refclock_ioctl - set serial port control functions
994 * This routine attempts to hide the internal, system-specific details
995 * of serial ports. It can handle POSIX (termios), SYSV (termio) and BSD
996 * (sgtty) interfaces with varying degrees of success. The routine sets
997 * up optional features such as tty_clk. The routine returns 1 if
998 * success and 0 if failure.
1002 int fd, /* file descriptor */
1003 u_int lflags /* line discipline flags */
1007 * simply return 1 if no UNIX line discipline is supported
1009 #if !defined SYS_VXWORKS && !defined SYS_WINNT
1010 #if defined(HAVE_TERMIOS) || defined(HAVE_SYSV_TTYS) || defined(HAVE_BSD_TTYS)
1014 printf("refclock_ioctl: fd %d flags 0x%x\n", fd,
1020 * The TTYCLK option provides timestamping at the driver level.
1021 * It requires the tty_clk streams module and System V STREAMS
1022 * support. If not available, don't complain.
1024 if (lflags & (LDISC_CLK | LDISC_CLKPPS | LDISC_ACTS)) {
1027 if (ioctl(fd, I_PUSH, "clk") < 0) {
1029 "refclock_ioctl fd %d I_PUSH: %m", fd);
1035 if (lflags & LDISC_CLKPPS)
1037 else if (lflags & LDISC_ACTS)
1041 if (ioctl(fd, CLK_SETSTR, str) < 0) {
1043 "refclock_ioctl fd %d CLK_SETSTR: %m", fd);
1046 #endif /*CLK_SETSTR */
1050 #endif /* HAVE_TERMIOS || HAVE_SYSV_TTYS || HAVE_BSD_TTYS */
1051 #endif /* SYS_VXWORKS SYS_WINNT */
1057 * refclock_control - set and/or return clock values
1059 * This routine is used mainly for debugging. It returns designated
1060 * values from the interface structure that can be displayed using
1061 * ntpdc and the clockstat command. It can also be used to initialize
1062 * configuration variables, such as fudgetimes, fudgevalues, reference
1067 struct sockaddr_storage *srcadr,
1068 struct refclockstat *in,
1069 struct refclockstat *out
1073 struct refclockproc *pp;
1078 * Check for valid address and running peer
1080 if (srcadr->ss_family != AF_INET)
1083 if (!ISREFCLOCKADR(srcadr))
1086 clktype = (u_char)REFCLOCKTYPE(srcadr);
1087 unit = REFCLOCKUNIT(srcadr);
1088 if (clktype >= num_refclock_conf || unit >= MAXUNIT)
1091 peer = typeunit[clktype][unit];
1095 if (peer->procptr == NULL)
1101 * Initialize requested data
1104 if (in->haveflags & CLK_HAVETIME1)
1105 pp->fudgetime1 = in->fudgetime1;
1106 if (in->haveflags & CLK_HAVETIME2)
1107 pp->fudgetime2 = in->fudgetime2;
1108 if (in->haveflags & CLK_HAVEVAL1)
1109 peer->stratum = pp->stratum = (u_char)in->fudgeval1;
1110 if (in->haveflags & CLK_HAVEVAL2)
1111 peer->refid = pp->refid = in->fudgeval2;
1112 if (in->haveflags & CLK_HAVEFLAG1) {
1113 pp->sloppyclockflag &= ~CLK_FLAG1;
1114 pp->sloppyclockflag |= in->flags & CLK_FLAG1;
1116 if (in->haveflags & CLK_HAVEFLAG2) {
1117 pp->sloppyclockflag &= ~CLK_FLAG2;
1118 pp->sloppyclockflag |= in->flags & CLK_FLAG2;
1120 if (in->haveflags & CLK_HAVEFLAG3) {
1121 pp->sloppyclockflag &= ~CLK_FLAG3;
1122 pp->sloppyclockflag |= in->flags & CLK_FLAG3;
1124 if (in->haveflags & CLK_HAVEFLAG4) {
1125 pp->sloppyclockflag &= ~CLK_FLAG4;
1126 pp->sloppyclockflag |= in->flags & CLK_FLAG4;
1131 * Readback requested data
1134 out->haveflags = CLK_HAVETIME1 | CLK_HAVEVAL1 |
1135 CLK_HAVEVAL2 | CLK_HAVEFLAG4;
1136 out->fudgetime1 = pp->fudgetime1;
1137 out->fudgetime2 = pp->fudgetime2;
1138 out->fudgeval1 = pp->stratum;
1139 out->fudgeval2 = pp->refid;
1140 out->flags = (u_char) pp->sloppyclockflag;
1142 out->timereset = current_time - pp->timestarted;
1143 out->polls = pp->polls;
1144 out->noresponse = pp->noreply;
1145 out->badformat = pp->badformat;
1146 out->baddata = pp->baddata;
1148 out->lastevent = pp->lastevent;
1149 out->currentstatus = pp->currentstatus;
1150 out->type = pp->type;
1151 out->clockdesc = pp->clockdesc;
1152 out->lencode = pp->lencode;
1153 out->p_lastcode = pp->a_lastcode;
1157 * Give the stuff to the clock
1159 if (refclock_conf[clktype]->clock_control != noentry)
1160 (refclock_conf[clktype]->clock_control)(unit, in, out, peer);
1165 * refclock_buginfo - return debugging info
1167 * This routine is used mainly for debugging. It returns designated
1168 * values from the interface structure that can be displayed using
1169 * ntpdc and the clkbug command.
1173 struct sockaddr_storage *srcadr, /* clock address */
1174 struct refclockbug *bug /* output structure */
1178 struct refclockproc *pp;
1184 * Check for valid address and peer structure
1186 if (srcadr->ss_family != AF_INET)
1189 if (!ISREFCLOCKADR(srcadr))
1192 clktype = (u_char) REFCLOCKTYPE(srcadr);
1193 unit = REFCLOCKUNIT(srcadr);
1194 if (clktype >= num_refclock_conf || unit >= MAXUNIT)
1197 peer = typeunit[clktype][unit];
1204 * Copy structure values
1207 bug->svalues = 0x0000003f;
1208 bug->values[0] = pp->year;
1209 bug->values[1] = pp->day;
1210 bug->values[2] = pp->hour;
1211 bug->values[3] = pp->minute;
1212 bug->values[4] = pp->second;
1213 bug->values[5] = pp->nsec;
1214 bug->values[6] = pp->yearstart;
1215 bug->values[7] = pp->coderecv;
1216 bug->stimes = 0xfffffffc;
1217 bug->times[0] = pp->lastref;
1218 bug->times[1] = pp->lastrec;
1219 for (i = 2; i < (int)bug->ntimes; i++)
1220 DTOLFP(pp->filter[i - 2], &bug->times[i]);
1223 * Give the stuff to the clock
1225 if (refclock_conf[clktype]->clock_buginfo != noentry)
1226 (refclock_conf[clktype]->clock_buginfo)(unit, bug, peer);
1229 #endif /* REFCLOCK */