1/*
2 * ntp_refclock.h - definitions for reference clock support
3 */
4
5#ifndef NTP_REFCLOCK_H
6#define NTP_REFCLOCK_H
7
8#if defined(HAVE_SYS_MODEM_H)
9#include <sys/modem.h>
10#endif
11
12#include "ntp_types.h"
13#include "ntp_tty.h"
14#include "recvbuff.h"
15
16
17/*
18 * Macros to determine the clock type and unit numbers from a
19 * 127.127.t.u address
20 */
21#define	REFCLOCKTYPE(srcadr)	((SRCADR(srcadr) >> 8) & 0xff)
22#define REFCLOCKUNIT(srcadr)	(SRCADR(srcadr) & 0xff)
23
24/*
25 * List of reference clock names and descriptions. These must agree with
26 * lib/clocktypes.c and ntpd/refclock_conf.c.
27 */
28struct clktype {
29	int code;		/* driver "major" number */
30	const char *clocktype;	/* long description */
31	const char *abbrev;	/* short description */
32};
33extern struct clktype clktypes[];
34
35/*
36 * Configuration flag values
37 */
38#define	CLK_HAVETIME1	0x1
39#define	CLK_HAVETIME2	0x2
40#define	CLK_HAVEVAL1	0x4
41#define	CLK_HAVEVAL2	0x8
42
43#define	CLK_FLAG1	0x1
44#define	CLK_FLAG2	0x2
45#define	CLK_FLAG3	0x4
46#define	CLK_FLAG4	0x8
47
48#define	CLK_HAVEFLAG1	0x10
49#define	CLK_HAVEFLAG2	0x20
50#define	CLK_HAVEFLAG3	0x40
51#define	CLK_HAVEFLAG4	0x80
52#define	CLK_HAVEMINJIT	0x100
53
54/*
55 * Constant for disabling event reporting in
56 * refclock_receive. ORed in leap
57 * parameter
58 */
59#define REFCLOCK_OWN_STATES	0x80
60
61/*
62 * Structure for returning clock status
63 */
64struct refclockstat {
65	u_char	type;		/* clock type */
66	u_char	flags;		/* clock flags */
67	u_short	haveflags;	/* bit array of valid flags */
68	u_short	lencode;	/* length of last timecode */
69	const char *p_lastcode;	/* last timecode received */
70	u_int32	polls;		/* transmit polls */
71	u_int32	noresponse;	/* no response to poll */
72	u_int32	badformat;	/* bad format timecode received */
73	u_int32	baddata;	/* invalid data timecode received */
74	u_int32	timereset;	/* driver resets */
75	const char *clockdesc;	/* ASCII description */
76	double	fudgeminjitter;	/* configure fudge minjitter */
77	double	fudgetime1;	/* configure fudge time1 */
78	double	fudgetime2;	/* configure fudge time2 */
79	int32	fudgeval1;	/* configure fudge value1 */
80	u_int32	fudgeval2;	/* configure fudge value2 */
81	u_char	currentstatus;	/* clock status */
82	u_char	lastevent;	/* last exception event */
83	u_char	leap;		/* leap bits */
84	struct	ctl_var *kv_list; /* additional variables */
85};
86
87/*
88 * Reference clock I/O structure.  Used to provide an interface between
89 * the reference clock drivers and the I/O module.
90 */
91struct refclockio {
92	struct	refclockio *next; /* link to next structure */
93	void	(*clock_recv) (struct recvbuf *); /* completion routine */
94	int 	(*io_input)   (struct recvbuf *); /* input routine -
95				to avoid excessive buffer use
96				due to small bursts
97				of refclock input data */
98	struct peer *srcclock;	/* refclock peer */
99	int	datalen;	/* length of data */
100	int	fd;		/* file descriptor */
101	u_long	recvcount;	/* count of receive completions */
102	int	active;		/* nonzero when in use */
103
104#ifdef HAVE_IO_COMPLETION_PORT
105	void *	ioreg_ctx;	/* IO registration context */
106	void *	device_ctx;	/* device-related data for i/o subsystem */
107#endif
108};
109
110/*
111 * Structure for returning debugging info
112 */
113#define	NCLKBUGVALUES	16
114#define	NCLKBUGTIMES	32
115
116struct refclockbug {
117	u_char	nvalues;	/* values following */
118	u_char	ntimes;		/* times following */
119	u_short	svalues;	/* values format sign array */
120	u_int32	stimes;		/* times format sign array */
121	u_int32	values[NCLKBUGVALUES]; /* real values */
122	l_fp	times[NCLKBUGTIMES]; /* real times */
123};
124
125#ifdef HAVE_IO_COMPLETION_PORT
126extern	HANDLE	WaitableIoEventHandle;
127#endif
128
129/*
130 * Structure interface between the reference clock support
131 * ntp_refclock.c and the driver utility routines
132 */
133#define MAXSTAGE	64	/* max median filter stages  */
134#define NSTAGE		5	/* default median filter stages */
135#define BMAX		128	/* max timecode length */
136#define GMT		0	/* I hope nobody sees this */
137#define MAXDIAL		60	/* max length of modem dial strings */
138
139struct refclockproc {
140	void *	unitptr;	/* pointer to unit structure */
141	struct refclock * conf;	/* refclock_conf[type] */
142	struct refclockio io;	/* I/O handler structure */
143	u_char	leap;		/* leap/synchronization code */
144	u_char	currentstatus;	/* clock status */
145	u_char	lastevent;	/* last exception event */
146	u_char	type;		/* clock type */
147	u_char	inpoll;		/* waiting for 'refclock_receive()' */
148	const char *clockdesc;	/* clock description */
149	u_long	nextaction;	/* local activity timeout */
150	void	(*action)(struct peer *); /* timeout callback */
151
152	char	a_lastcode[BMAX]; /* last timecode received */
153	int	lencode;	/* length of last timecode */
154
155	int	year;		/* year of eternity */
156	int	day;		/* day of year */
157	int	hour;		/* hour of day */
158	int	minute;		/* minute of hour */
159	int	second;		/* second of minute */
160	long	nsec;		/* nanosecond of second */
161	u_long	yearstart;	/* beginning of year */
162	u_int	coderecv;	/* put pointer */
163	u_int	codeproc;	/* get pointer */
164	l_fp	lastref;	/* reference timestamp */
165	l_fp	lastrec;	/* receive timestamp */
166	double	offset;		/* mean offset */
167	double	disp;		/* sample dispersion */
168	double	jitter;		/* jitter (mean squares) */
169	double	filter[MAXSTAGE]; /* median filter */
170
171	/*
172	 * Configuration data
173	 */
174	double	fudgetime1;	/* fudge time1 */
175	double	fudgetime2;	/* fudge time2 */
176	double	fudgeminjitter;	/* manually set lower bound for jitter */
177	u_char	stratum;	/* server stratum */
178	u_int32	refid;		/* reference identifier */
179	u_char	sloppyclockflag; /* fudge flags */
180
181	/*
182	 * Status tallies
183 	 */
184	u_long	timestarted;	/* time we started this */
185	u_long	polls;		/* polls sent */
186	u_long	noreply;	/* no replies to polls */
187	u_long	badformat;	/* bad format reply */
188	u_long	baddata;	/* bad data reply */
189};
190
191/*
192 * Structure interface between the reference clock support
193 * ntp_refclock.c and particular clock drivers. This must agree with the
194 * structure defined in the driver.
195 */
196#define	noentry	0		/* flag for null routine */
197#define	NOFLAGS	0		/* flag for null flags */
198
199struct refclock {
200	int (*clock_start)	(int, struct peer *);
201	void (*clock_shutdown)	(int, struct peer *);
202	void (*clock_poll)	(int, struct peer *);
203	void (*clock_control)	(int, const struct refclockstat *,
204				 struct refclockstat *, struct peer *);
205	void (*clock_init)	(void);
206	void (*clock_buginfo)	(int, struct refclockbug *, struct peer *);
207	void (*clock_timer)	(int, struct peer *);
208};
209
210/*
211 * Function prototypes
212 */
213extern	int	io_addclock	(struct refclockio *);
214extern	void	io_closeclock	(struct refclockio *);
215
216#ifdef REFCLOCK
217extern	void	refclock_buginfo(sockaddr_u *,
218				 struct refclockbug *);
219extern	void	refclock_control(sockaddr_u *,
220				 const struct refclockstat *,
221				 struct refclockstat *);
222extern	int	refclock_open	(const char *, u_int, u_int);
223extern	int	refclock_setup	(int, u_int, u_int);
224extern	void	refclock_timer	(struct peer *);
225extern	void	refclock_transmit(struct peer *);
226extern 	int	refclock_process(struct refclockproc *);
227extern 	int	refclock_process_f(struct refclockproc *, double);
228extern 	void	refclock_process_offset(struct refclockproc *, l_fp,
229					l_fp, double);
230extern	int	refclock_samples_avail(struct refclockproc const *);
231extern	int	refclock_samples_expire(struct refclockproc *, int);
232extern	void	refclock_report	(struct peer *, int);
233extern	int	refclock_gtlin	(struct recvbuf *, char *, int, l_fp *);
234extern	int	refclock_gtraw	(struct recvbuf *, char *, int, l_fp *);
235extern	int	indicate_refclock_packet(struct refclockio *,
236					 struct recvbuf *);
237extern	void	process_refclock_packet(struct recvbuf *);
238
239/* save string as la_code, size==(size_t)-1 ==> ASCIIZ string */
240extern	void	refclock_save_lcode(
241			struct refclockproc *, char const *, size_t);
242/* format data into la_code */
243extern	void	refclock_format_lcode(
244			struct refclockproc *, char const *, ...);
245extern	void	refclock_vformat_lcode(
246			struct refclockproc *, char const *, va_list);
247
248struct refclock_atom;
249extern int	refclock_ppsaugment(
250    const struct refclock_atom*, l_fp *rcvtime ,
251    double rcvfudge, double ppsfudge);
252
253#endif /* REFCLOCK */
254
255#endif /* NTP_REFCLOCK_H */
256