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