ntp_refclock.h revision 132451
154359Sroberto/*
254359Sroberto * ntp_refclock.h - definitions for reference clock support
354359Sroberto */
454359Sroberto
554359Sroberto#ifndef NTP_REFCLOCK_H
654359Sroberto#define NTP_REFCLOCK_H
754359Sroberto
854359Sroberto#include "ntp_types.h"
954359Sroberto
1054359Sroberto#if defined(HAVE_BSD_TTYS)
1154359Sroberto#include <sgtty.h>
1254359Sroberto#endif /* HAVE_BSD_TTYS */
1354359Sroberto
1454359Sroberto#if defined(HAVE_SYSV_TTYS)
1554359Sroberto#include <termio.h>
1654359Sroberto#endif /* HAVE_SYSV_TTYS */
1754359Sroberto
1854359Sroberto#if defined(HAVE_TERMIOS)
1954359Sroberto# ifdef TERMIOS_NEEDS__SVID3
2054359Sroberto#  define _SVID3
2154359Sroberto# endif
2254359Sroberto# include <termios.h>
2354359Sroberto# ifdef TERMIOS_NEEDS__SVID3
2454359Sroberto#  undef _SVID3
2554359Sroberto# endif
2654359Sroberto#endif
2754359Sroberto
2854359Sroberto#if defined(HAVE_SYS_MODEM_H)
2954359Sroberto#include <sys/modem.h>
3054359Sroberto#endif
3154359Sroberto
3282498Sroberto#if 0 /* If you need that, include ntp_io.h instead */
3354359Sroberto#if defined(STREAM)
3454359Sroberto#include <stropts.h>
3582498Sroberto#if defined(CLK) /* This is never defined, except perhaps by a system header file */
3654359Sroberto#include <sys/clkdefs.h>
3754359Sroberto#endif /* CLK */
3854359Sroberto#endif /* STREAM */
3982498Sroberto#endif
4054359Sroberto
4154359Sroberto#include "recvbuff.h"
4254359Sroberto
4354359Sroberto#if !defined(SYSV_TTYS) && !defined(STREAM) & !defined(BSD_TTYS)
4454359Sroberto#define BSD_TTYS
4554359Sroberto#endif /* SYSV_TTYS STREAM BSD_TTYS */
4654359Sroberto
47132451Sroberto#define SAMPLE(x)	pp->coderecv = (pp->coderecv + 1) % MAXSTAGE; \
48132451Sroberto			pp->filter[pp->coderecv] = (x); \
49132451Sroberto			if (pp->coderecv == pp->codeproc) \
50132451Sroberto				pp->codeproc = (pp->codeproc + 1) % MAXSTAGE;
5154359Sroberto
5254359Sroberto/*
5354359Sroberto * Macros to determine the clock type and unit numbers from a
5454359Sroberto * 127.127.t.u address
5554359Sroberto */
5654359Sroberto#define	REFCLOCKTYPE(srcadr)	((SRCADR(srcadr) >> 8) & 0xff)
5754359Sroberto#define REFCLOCKUNIT(srcadr)	(SRCADR(srcadr) & 0xff)
5854359Sroberto
5954359Sroberto/*
6054359Sroberto * List of reference clock names and descriptions. These must agree with
6154359Sroberto * lib/clocktypes.c and ntpd/refclock_conf.c.
6254359Sroberto */
6354359Srobertostruct clktype {
6454359Sroberto	int code;		/* driver "major" number */
6554359Sroberto	const char *clocktype;	/* long description */
6654359Sroberto	const char *abbrev;	/* short description */
6754359Sroberto};
6854359Sroberto
6954359Sroberto/*
7054359Sroberto * Configuration flag values
7154359Sroberto */
7254359Sroberto#define	CLK_HAVETIME1	0x1
7354359Sroberto#define	CLK_HAVETIME2	0x2
7454359Sroberto#define	CLK_HAVEVAL1	0x4
7554359Sroberto#define	CLK_HAVEVAL2	0x8
7654359Sroberto
7754359Sroberto#define	CLK_FLAG1	0x1
7854359Sroberto#define	CLK_FLAG2	0x2
7954359Sroberto#define	CLK_FLAG3	0x4
8054359Sroberto#define	CLK_FLAG4	0x8
8154359Sroberto
8254359Sroberto#define	CLK_HAVEFLAG1	0x10
8354359Sroberto#define	CLK_HAVEFLAG2	0x20
8454359Sroberto#define	CLK_HAVEFLAG3	0x40
8554359Sroberto#define	CLK_HAVEFLAG4	0x80
8654359Sroberto
8754359Sroberto/*
8854359Sroberto * Constant for disabling event reporting in
8954359Sroberto * refclock_receive. ORed in leap
9054359Sroberto * parameter
9154359Sroberto */
9254359Sroberto#define REFCLOCK_OWN_STATES	0x80
9354359Sroberto
9454359Sroberto/*
9554359Sroberto * Structure for returning clock status
9654359Sroberto */
9754359Srobertostruct refclockstat {
9854359Sroberto	u_char	type;		/* clock type */
9954359Sroberto	u_char	flags;		/* clock flags */
10054359Sroberto	u_char	haveflags;	/* bit array of valid flags */
10154359Sroberto	u_short	lencode;	/* length of last timecode */
10254359Sroberto	const char *p_lastcode;	/* last timecode received */
10354359Sroberto	u_int32	polls;		/* transmit polls */
10454359Sroberto	u_int32	noresponse;	/* no response to poll */
10554359Sroberto	u_int32	badformat;	/* bad format timecode received */
10654359Sroberto	u_int32	baddata;	/* invalid data timecode received */
10754359Sroberto	u_int32	timereset;	/* driver resets */
10854359Sroberto	const char *clockdesc;	/* ASCII description */
10954359Sroberto	double	fudgetime1;	/* configure fudge time1 */
11054359Sroberto	double	fudgetime2;	/* configure fudge time2 */
11154359Sroberto	int32	fudgeval1;	/* configure fudge value1 */
11254359Sroberto	int32	fudgeval2;	/* configure fudge value2 */
11354359Sroberto	u_char	currentstatus;	/* clock status */
11454359Sroberto	u_char	lastevent;	/* last exception event */
11554359Sroberto	u_char	leap;		/* leap bits */
11654359Sroberto	struct	ctl_var *kv_list; /* additional variables */
11754359Sroberto};
11854359Sroberto
11954359Sroberto/*
12054359Sroberto * Reference clock I/O structure.  Used to provide an interface between
12154359Sroberto * the reference clock drivers and the I/O module.
12254359Sroberto */
12354359Srobertostruct refclockio {
12454359Sroberto	struct	refclockio *next; /* link to next structure */
12554359Sroberto	void	(*clock_recv) P((struct recvbuf *)); /* completion routine */
12654359Sroberto	int 	(*io_input)   P((struct recvbuf *)); /* input routine -
12754359Sroberto				to avoid excessive buffer use
12854359Sroberto				due to small bursts
12954359Sroberto				of refclock input data */
13054359Sroberto	caddr_t	srcclock;	/* pointer to clock structure */
13154359Sroberto	int	datalen;	/* lenth of data */
132132451Sroberto	SOCKET	fd;		/* file descriptor */
13354359Sroberto	u_long	recvcount;	/* count of receive completions */
13454359Sroberto};
13554359Sroberto
13654359Sroberto/*
13754359Sroberto * Structure for returning debugging info
13854359Sroberto */
13954359Sroberto#define	NCLKBUGVALUES	16
14054359Sroberto#define	NCLKBUGTIMES	32
14154359Sroberto
14254359Srobertostruct refclockbug {
14354359Sroberto	u_char	nvalues;	/* values following */
14454359Sroberto	u_char	ntimes;		/* times following */
14554359Sroberto	u_short	svalues;	/* values format sign array */
14654359Sroberto	u_int32	stimes;		/* times format sign array */
14754359Sroberto	u_int32	values[NCLKBUGVALUES]; /* real values */
14854359Sroberto	l_fp	times[NCLKBUGTIMES]; /* real times */
14954359Sroberto};
15054359Sroberto
15154359Sroberto/*
15254359Sroberto * Structure interface between the reference clock support
15354359Sroberto * ntp_refclock.c and the driver utility routines
15454359Sroberto */
15554359Sroberto#define MAXSTAGE	60	/* max median filter stages  */
15654359Sroberto#define NSTAGE		5	/* default median filter stages */
15754359Sroberto#define BMAX		128	/* max timecode length */
15854359Sroberto#define GMT		0	/* I hope nobody sees this */
15954359Sroberto#define MAXDIAL		60	/* max length of modem dial strings */
16054359Sroberto
16154359Sroberto/*
16254359Sroberto * Line discipline flags. These require line discipline or streams
16354359Sroberto * modules to be installed/loaded in the kernel. If specified, but not
16454359Sroberto * installed, the code runs as if unspecified.
16554359Sroberto */
16654359Sroberto#define LDISC_STD	0x0	/* standard */
16754359Sroberto#define LDISC_CLK	0x1	/* tty_clk \n intercept */
16854359Sroberto#define LDISC_CLKPPS	0x2	/* tty_clk \377 intercept */
16954359Sroberto#define LDISC_ACTS	0x4	/* tty_clk #* intercept */
17056746Sroberto#define LDISC_CHU	0x8	/* depredated */
17156746Sroberto#define LDISC_PPS	0x10	/* ppsclock, ppsapi */
17254359Sroberto#define LDISC_RAW	0x20	/* raw binary */
17354359Sroberto
17454359Srobertostruct refclockproc {
17554359Sroberto	struct	refclockio io;	/* I/O handler structure */
17654359Sroberto	caddr_t	unitptr;	/* pointer to unit structure */
17754359Sroberto	u_char	leap;		/* leap/synchronization code */
17854359Sroberto	u_char	currentstatus;	/* clock status */
17954359Sroberto	u_char	lastevent;	/* last exception event */
18054359Sroberto	u_char	type;		/* clock type */
18154359Sroberto	const char *clockdesc;	/* clock description */
18254359Sroberto
18354359Sroberto	char	a_lastcode[BMAX]; /* last timecode received */
18454359Sroberto	u_short	lencode;	/* length of last timecode */
18554359Sroberto
18654359Sroberto	int	year;		/* year of eternity */
18754359Sroberto	int	day;		/* day of year */
18854359Sroberto	int	hour;		/* hour of day */
18954359Sroberto	int	minute;		/* minute of hour */
19054359Sroberto	int	second;		/* second of minute */
191132451Sroberto	long	nsec;		/* nanosecond of second */
19254359Sroberto	u_long	yearstart;	/* beginning of year */
19354359Sroberto	int	coderecv;	/* put pointer */
19454359Sroberto	int	codeproc;	/* get pointer */
195132451Sroberto	l_fp	lastref;	/* reference timestamp */
196132451Sroberto	l_fp	lastrec;	/* receive timestamp */
19754359Sroberto	double	offset;		/* mean offset */
19854359Sroberto	double	disp;		/* sample dispersion */
19982498Sroberto	double	jitter;		/* jitter (mean squares) */
20054359Sroberto	double	filter[MAXSTAGE]; /* median filter */
20154359Sroberto
20254359Sroberto	/*
20354359Sroberto	 * Configuration data
20454359Sroberto	 */
20554359Sroberto	double	fudgetime1;	/* fudge time1 */
20654359Sroberto	double	fudgetime2;	/* fudge time2 */
207132451Sroberto	u_char	stratum;	/* server stratum */
20854359Sroberto	u_int32	refid;		/* reference identifier */
20954359Sroberto	u_char	sloppyclockflag; /* fudge flags */
21054359Sroberto
21154359Sroberto	/*
21254359Sroberto	 * Status tallies
21354359Sroberto 	 */
21454359Sroberto	u_long	timestarted;	/* time we started this */
21554359Sroberto	u_long	polls;		/* polls sent */
21654359Sroberto	u_long	noreply;	/* no replies to polls */
21754359Sroberto	u_long	badformat;	/* bad format reply */
21854359Sroberto	u_long	baddata;	/* bad data reply */
21954359Sroberto};
22054359Sroberto
22154359Sroberto/*
22254359Sroberto * Structure interface between the reference clock support
22354359Sroberto * ntp_refclock.c and particular clock drivers. This must agree with the
22454359Sroberto * structure defined in the driver.
22554359Sroberto */
22654359Sroberto#define	noentry	0		/* flag for null routine */
22754359Sroberto#define	NOFLAGS	0		/* flag for null flags */
22854359Sroberto
22954359Srobertostruct refclock {
23054359Sroberto	int (*clock_start)	P((int, struct peer *));
23154359Sroberto	void (*clock_shutdown)	P((int, struct peer *));
23254359Sroberto	void (*clock_poll)	P((int, struct peer *));
23354359Sroberto	void (*clock_control)	P((int, struct refclockstat *,
23454359Sroberto				    struct refclockstat *, struct peer *));
23554359Sroberto	void (*clock_init)	P((void));
23654359Sroberto	void (*clock_buginfo)	P((int, struct refclockbug *, struct peer *));
23754359Sroberto	u_long clock_flags;
23854359Sroberto};
23954359Sroberto
24054359Sroberto/*
24154359Sroberto * Function prototypes
24254359Sroberto */
24354359Sroberto/*
24454359Sroberto * auxiliary PPS interface (implemented by refclock_atom())
24554359Sroberto */
24654359Srobertoextern	int	pps_sample P((l_fp *));
24754359Srobertoextern	int	io_addclock_simple P((struct refclockio *));
24854359Srobertoextern	int	io_addclock	P((struct refclockio *));
24954359Srobertoextern	void	io_closeclock	P((struct refclockio *));
25054359Sroberto
25154359Sroberto#ifdef REFCLOCK
252132451Srobertoextern	void	refclock_buginfo P((struct sockaddr_storage *,
25354359Sroberto				    struct refclockbug *));
254132451Srobertoextern	void	refclock_control P((struct sockaddr_storage *,
25554359Sroberto				    struct refclockstat *,
25654359Sroberto				    struct refclockstat *));
25754359Srobertoextern	int	refclock_open	P((char *, int, int));
25854359Srobertoextern	void	refclock_transmit P((struct peer *));
25954359Srobertoextern	int	refclock_ioctl	P((int, int));
26054359Srobertoextern 	int	refclock_process P((struct refclockproc *));
26154359Srobertoextern 	void	refclock_process_offset P((struct refclockproc *, l_fp, l_fp, double));
26254359Srobertoextern	void	refclock_report	P((struct peer *, int));
26354359Srobertoextern	int	refclock_gtlin	P((struct recvbuf *, char *, int,
26454359Sroberto				    l_fp *));
26554359Sroberto#endif /* REFCLOCK */
26654359Sroberto
26754359Sroberto#endif /* NTP_REFCLOCK_H */
268