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