154359Sroberto/*
254359Sroberto * ntp_refclock.h - definitions for reference clock support
354359Sroberto */
454359Sroberto
554359Sroberto#ifndef NTP_REFCLOCK_H
654359Sroberto#define NTP_REFCLOCK_H
754359Sroberto
854359Sroberto#if defined(HAVE_SYS_MODEM_H)
954359Sroberto#include <sys/modem.h>
1054359Sroberto#endif
1154359Sroberto
12290001Sglebius#include "ntp_types.h"
13290001Sglebius#include "ntp_tty.h"
1454359Sroberto#include "recvbuff.h"
1554359Sroberto
1654359Sroberto
17132451Sroberto#define SAMPLE(x)	pp->coderecv = (pp->coderecv + 1) % MAXSTAGE; \
18132451Sroberto			pp->filter[pp->coderecv] = (x); \
19132451Sroberto			if (pp->coderecv == pp->codeproc) \
20132451Sroberto				pp->codeproc = (pp->codeproc + 1) % MAXSTAGE;
2154359Sroberto
2254359Sroberto/*
2354359Sroberto * Macros to determine the clock type and unit numbers from a
2454359Sroberto * 127.127.t.u address
2554359Sroberto */
2654359Sroberto#define	REFCLOCKTYPE(srcadr)	((SRCADR(srcadr) >> 8) & 0xff)
2754359Sroberto#define REFCLOCKUNIT(srcadr)	(SRCADR(srcadr) & 0xff)
2854359Sroberto
2954359Sroberto/*
3054359Sroberto * List of reference clock names and descriptions. These must agree with
3154359Sroberto * lib/clocktypes.c and ntpd/refclock_conf.c.
3254359Sroberto */
3354359Srobertostruct clktype {
3454359Sroberto	int code;		/* driver "major" number */
3554359Sroberto	const char *clocktype;	/* long description */
3654359Sroberto	const char *abbrev;	/* short description */
3754359Sroberto};
38182007Srobertoextern struct clktype clktypes[];
3954359Sroberto
4054359Sroberto/*
4154359Sroberto * Configuration flag values
4254359Sroberto */
4354359Sroberto#define	CLK_HAVETIME1	0x1
4454359Sroberto#define	CLK_HAVETIME2	0x2
4554359Sroberto#define	CLK_HAVEVAL1	0x4
4654359Sroberto#define	CLK_HAVEVAL2	0x8
4754359Sroberto
4854359Sroberto#define	CLK_FLAG1	0x1
4954359Sroberto#define	CLK_FLAG2	0x2
5054359Sroberto#define	CLK_FLAG3	0x4
5154359Sroberto#define	CLK_FLAG4	0x8
5254359Sroberto
5354359Sroberto#define	CLK_HAVEFLAG1	0x10
5454359Sroberto#define	CLK_HAVEFLAG2	0x20
5554359Sroberto#define	CLK_HAVEFLAG3	0x40
5654359Sroberto#define	CLK_HAVEFLAG4	0x80
5754359Sroberto
5854359Sroberto/*
5954359Sroberto * Constant for disabling event reporting in
6054359Sroberto * refclock_receive. ORed in leap
6154359Sroberto * parameter
6254359Sroberto */
6354359Sroberto#define REFCLOCK_OWN_STATES	0x80
6454359Sroberto
6554359Sroberto/*
6654359Sroberto * Structure for returning clock status
6754359Sroberto */
6854359Srobertostruct refclockstat {
6954359Sroberto	u_char	type;		/* clock type */
7054359Sroberto	u_char	flags;		/* clock flags */
7154359Sroberto	u_char	haveflags;	/* bit array of valid flags */
7254359Sroberto	u_short	lencode;	/* length of last timecode */
7354359Sroberto	const char *p_lastcode;	/* last timecode received */
7454359Sroberto	u_int32	polls;		/* transmit polls */
7554359Sroberto	u_int32	noresponse;	/* no response to poll */
7654359Sroberto	u_int32	badformat;	/* bad format timecode received */
7754359Sroberto	u_int32	baddata;	/* invalid data timecode received */
7854359Sroberto	u_int32	timereset;	/* driver resets */
7954359Sroberto	const char *clockdesc;	/* ASCII description */
8054359Sroberto	double	fudgetime1;	/* configure fudge time1 */
8154359Sroberto	double	fudgetime2;	/* configure fudge time2 */
8254359Sroberto	int32	fudgeval1;	/* configure fudge value1 */
83290001Sglebius	u_int32	fudgeval2;	/* configure fudge value2 */
8454359Sroberto	u_char	currentstatus;	/* clock status */
8554359Sroberto	u_char	lastevent;	/* last exception event */
8654359Sroberto	u_char	leap;		/* leap bits */
8754359Sroberto	struct	ctl_var *kv_list; /* additional variables */
8854359Sroberto};
8954359Sroberto
9054359Sroberto/*
9154359Sroberto * Reference clock I/O structure.  Used to provide an interface between
9254359Sroberto * the reference clock drivers and the I/O module.
9354359Sroberto */
9454359Srobertostruct refclockio {
9554359Sroberto	struct	refclockio *next; /* link to next structure */
96290001Sglebius	void	(*clock_recv) (struct recvbuf *); /* completion routine */
97290001Sglebius	int 	(*io_input)   (struct recvbuf *); /* input routine -
9854359Sroberto				to avoid excessive buffer use
9954359Sroberto				due to small bursts
10054359Sroberto				of refclock input data */
101290001Sglebius	struct peer *srcclock;	/* refclock peer */
102290001Sglebius	int	datalen;	/* length of data */
103290001Sglebius	int	fd;		/* file descriptor */
10454359Sroberto	u_long	recvcount;	/* count of receive completions */
105290001Sglebius	int	active;		/* nonzero when in use */
106290001Sglebius
107290001Sglebius#ifdef HAVE_IO_COMPLETION_PORT
108298770Sdelphij	void *	ioreg_ctx;	/* IO registration context */
109298770Sdelphij	void *	device_ctx;	/* device-related data for i/o subsystem */
110290001Sglebius#endif
11154359Sroberto};
11254359Sroberto
11354359Sroberto/*
11454359Sroberto * Structure for returning debugging info
11554359Sroberto */
11654359Sroberto#define	NCLKBUGVALUES	16
11754359Sroberto#define	NCLKBUGTIMES	32
11854359Sroberto
11954359Srobertostruct refclockbug {
12054359Sroberto	u_char	nvalues;	/* values following */
12154359Sroberto	u_char	ntimes;		/* times following */
12254359Sroberto	u_short	svalues;	/* values format sign array */
12354359Sroberto	u_int32	stimes;		/* times format sign array */
12454359Sroberto	u_int32	values[NCLKBUGVALUES]; /* real values */
12554359Sroberto	l_fp	times[NCLKBUGTIMES]; /* real times */
12654359Sroberto};
12754359Sroberto
128290001Sglebius#ifdef HAVE_IO_COMPLETION_PORT
129290001Sglebiusextern	HANDLE	WaitableIoEventHandle;
130290001Sglebius#endif
131290001Sglebius
13254359Sroberto/*
13354359Sroberto * Structure interface between the reference clock support
13454359Sroberto * ntp_refclock.c and the driver utility routines
13554359Sroberto */
13654359Sroberto#define MAXSTAGE	60	/* max median filter stages  */
13754359Sroberto#define NSTAGE		5	/* default median filter stages */
13854359Sroberto#define BMAX		128	/* max timecode length */
13954359Sroberto#define GMT		0	/* I hope nobody sees this */
14054359Sroberto#define MAXDIAL		60	/* max length of modem dial strings */
14154359Sroberto
14254359Sroberto
14354359Srobertostruct refclockproc {
144290001Sglebius	void *	unitptr;	/* pointer to unit structure */
145290001Sglebius	struct refclock * conf;	/* refclock_conf[type] */
146290001Sglebius	struct refclockio io;	/* I/O handler structure */
14754359Sroberto	u_char	leap;		/* leap/synchronization code */
14854359Sroberto	u_char	currentstatus;	/* clock status */
14954359Sroberto	u_char	lastevent;	/* last exception event */
15054359Sroberto	u_char	type;		/* clock type */
15154359Sroberto	const char *clockdesc;	/* clock description */
152290001Sglebius	u_long	nextaction;	/* local activity timeout */
153290001Sglebius	void	(*action)(struct peer *); /* timeout callback */
15454359Sroberto
15554359Sroberto	char	a_lastcode[BMAX]; /* last timecode received */
156290001Sglebius	int	lencode;	/* length of last timecode */
15754359Sroberto
15854359Sroberto	int	year;		/* year of eternity */
15954359Sroberto	int	day;		/* day of year */
16054359Sroberto	int	hour;		/* hour of day */
16154359Sroberto	int	minute;		/* minute of hour */
16254359Sroberto	int	second;		/* second of minute */
163132451Sroberto	long	nsec;		/* nanosecond of second */
16454359Sroberto	u_long	yearstart;	/* beginning of year */
16554359Sroberto	int	coderecv;	/* put pointer */
16654359Sroberto	int	codeproc;	/* get pointer */
167132451Sroberto	l_fp	lastref;	/* reference timestamp */
168132451Sroberto	l_fp	lastrec;	/* receive timestamp */
16954359Sroberto	double	offset;		/* mean offset */
17054359Sroberto	double	disp;		/* sample dispersion */
17182498Sroberto	double	jitter;		/* jitter (mean squares) */
17254359Sroberto	double	filter[MAXSTAGE]; /* median filter */
17354359Sroberto
17454359Sroberto	/*
17554359Sroberto	 * Configuration data
17654359Sroberto	 */
17754359Sroberto	double	fudgetime1;	/* fudge time1 */
17854359Sroberto	double	fudgetime2;	/* fudge time2 */
179132451Sroberto	u_char	stratum;	/* server stratum */
18054359Sroberto	u_int32	refid;		/* reference identifier */
18154359Sroberto	u_char	sloppyclockflag; /* fudge flags */
18254359Sroberto
18354359Sroberto	/*
18454359Sroberto	 * Status tallies
18554359Sroberto 	 */
18654359Sroberto	u_long	timestarted;	/* time we started this */
18754359Sroberto	u_long	polls;		/* polls sent */
18854359Sroberto	u_long	noreply;	/* no replies to polls */
18954359Sroberto	u_long	badformat;	/* bad format reply */
19054359Sroberto	u_long	baddata;	/* bad data reply */
19154359Sroberto};
19254359Sroberto
19354359Sroberto/*
19454359Sroberto * Structure interface between the reference clock support
19554359Sroberto * ntp_refclock.c and particular clock drivers. This must agree with the
19654359Sroberto * structure defined in the driver.
19754359Sroberto */
19854359Sroberto#define	noentry	0		/* flag for null routine */
19954359Sroberto#define	NOFLAGS	0		/* flag for null flags */
20054359Sroberto
20154359Srobertostruct refclock {
202290001Sglebius	int (*clock_start)	(int, struct peer *);
203290001Sglebius	void (*clock_shutdown)	(int, struct peer *);
204290001Sglebius	void (*clock_poll)	(int, struct peer *);
205290001Sglebius	void (*clock_control)	(int, const struct refclockstat *,
206290001Sglebius				 struct refclockstat *, struct peer *);
207290001Sglebius	void (*clock_init)	(void);
208290001Sglebius	void (*clock_buginfo)	(int, struct refclockbug *, struct peer *);
209290001Sglebius	void (*clock_timer)	(int, struct peer *);
21054359Sroberto};
21154359Sroberto
21254359Sroberto/*
21354359Sroberto * Function prototypes
21454359Sroberto */
215290001Sglebiusextern	int	io_addclock	(struct refclockio *);
216290001Sglebiusextern	void	io_closeclock	(struct refclockio *);
21754359Sroberto
21854359Sroberto#ifdef REFCLOCK
219290001Sglebiusextern	void	refclock_buginfo(sockaddr_u *,
220290001Sglebius				 struct refclockbug *);
221290001Sglebiusextern	void	refclock_control(sockaddr_u *,
222290001Sglebius				 const struct refclockstat *,
223290001Sglebius				 struct refclockstat *);
224293896Sglebiusextern	int	refclock_open	(const char *, u_int, u_int);
225290001Sglebiusextern	int	refclock_setup	(int, u_int, u_int);
226290001Sglebiusextern	void	refclock_timer	(struct peer *);
227290001Sglebiusextern	void	refclock_transmit(struct peer *);
228290001Sglebiusextern 	int	refclock_process(struct refclockproc *);
229290001Sglebiusextern 	int	refclock_process_f(struct refclockproc *, double);
230290001Sglebiusextern 	void	refclock_process_offset(struct refclockproc *, l_fp,
231290001Sglebius					l_fp, double);
232290001Sglebiusextern	void	refclock_report	(struct peer *, int);
233290001Sglebiusextern	int	refclock_gtlin	(struct recvbuf *, char *, int, l_fp *);
234290001Sglebiusextern	int	refclock_gtraw	(struct recvbuf *, char *, int, l_fp *);
235290001Sglebiusextern	int	indicate_refclock_packet(struct refclockio *,
236290001Sglebius					 struct recvbuf *);
237290001Sglebiusextern	void	process_refclock_packet(struct recvbuf *);
23854359Sroberto#endif /* REFCLOCK */
23954359Sroberto
24054359Sroberto#endif /* NTP_REFCLOCK_H */
241