xref: /freebsd/contrib/ntp/include/ntp_refclock.h (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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 *	device_context;	/* device-related data for i/o subsystem */
109 #endif
110 };
111 
112 /*
113  * Structure for returning debugging info
114  */
115 #define	NCLKBUGVALUES	16
116 #define	NCLKBUGTIMES	32
117 
118 struct refclockbug {
119 	u_char	nvalues;	/* values following */
120 	u_char	ntimes;		/* times following */
121 	u_short	svalues;	/* values format sign array */
122 	u_int32	stimes;		/* times format sign array */
123 	u_int32	values[NCLKBUGVALUES]; /* real values */
124 	l_fp	times[NCLKBUGTIMES]; /* real times */
125 };
126 
127 #ifdef HAVE_IO_COMPLETION_PORT
128 extern	HANDLE	WaitableIoEventHandle;
129 #endif
130 
131 /*
132  * Structure interface between the reference clock support
133  * ntp_refclock.c and the driver utility routines
134  */
135 #define MAXSTAGE	60	/* max median filter stages  */
136 #define NSTAGE		5	/* default median filter stages */
137 #define BMAX		128	/* max timecode length */
138 #define GMT		0	/* I hope nobody sees this */
139 #define MAXDIAL		60	/* max length of modem dial strings */
140 
141 
142 struct refclockproc {
143 	void *	unitptr;	/* pointer to unit structure */
144 	struct refclock * conf;	/* refclock_conf[type] */
145 	struct refclockio io;	/* I/O handler structure */
146 	u_char	leap;		/* leap/synchronization code */
147 	u_char	currentstatus;	/* clock status */
148 	u_char	lastevent;	/* last exception event */
149 	u_char	type;		/* clock type */
150 	const char *clockdesc;	/* clock description */
151 	u_long	nextaction;	/* local activity timeout */
152 	void	(*action)(struct peer *); /* timeout callback */
153 
154 	char	a_lastcode[BMAX]; /* last timecode received */
155 	int	lencode;	/* length of last timecode */
156 
157 	int	year;		/* year of eternity */
158 	int	day;		/* day of year */
159 	int	hour;		/* hour of day */
160 	int	minute;		/* minute of hour */
161 	int	second;		/* second of minute */
162 	long	nsec;		/* nanosecond of second */
163 	u_long	yearstart;	/* beginning of year */
164 	int	coderecv;	/* put pointer */
165 	int	codeproc;	/* get pointer */
166 	l_fp	lastref;	/* reference timestamp */
167 	l_fp	lastrec;	/* receive timestamp */
168 	double	offset;		/* mean offset */
169 	double	disp;		/* sample dispersion */
170 	double	jitter;		/* jitter (mean squares) */
171 	double	filter[MAXSTAGE]; /* median filter */
172 
173 	/*
174 	 * Configuration data
175 	 */
176 	double	fudgetime1;	/* fudge time1 */
177 	double	fudgetime2;	/* fudge time2 */
178 	u_char	stratum;	/* server stratum */
179 	u_int32	refid;		/* reference identifier */
180 	u_char	sloppyclockflag; /* fudge flags */
181 
182 	/*
183 	 * Status tallies
184  	 */
185 	u_long	timestarted;	/* time we started this */
186 	u_long	polls;		/* polls sent */
187 	u_long	noreply;	/* no replies to polls */
188 	u_long	badformat;	/* bad format reply */
189 	u_long	baddata;	/* bad data reply */
190 };
191 
192 /*
193  * Structure interface between the reference clock support
194  * ntp_refclock.c and particular clock drivers. This must agree with the
195  * structure defined in the driver.
196  */
197 #define	noentry	0		/* flag for null routine */
198 #define	NOFLAGS	0		/* flag for null flags */
199 
200 struct refclock {
201 	int (*clock_start)	(int, struct peer *);
202 	void (*clock_shutdown)	(int, struct peer *);
203 	void (*clock_poll)	(int, struct peer *);
204 	void (*clock_control)	(int, const struct refclockstat *,
205 				 struct refclockstat *, struct peer *);
206 	void (*clock_init)	(void);
207 	void (*clock_buginfo)	(int, struct refclockbug *, struct peer *);
208 	void (*clock_timer)	(int, struct peer *);
209 };
210 
211 /*
212  * Function prototypes
213  */
214 extern	int	io_addclock	(struct refclockio *);
215 extern	void	io_closeclock	(struct refclockio *);
216 
217 #ifdef REFCLOCK
218 extern	void	refclock_buginfo(sockaddr_u *,
219 				 struct refclockbug *);
220 extern	void	refclock_control(sockaddr_u *,
221 				 const struct refclockstat *,
222 				 struct refclockstat *);
223 extern	int	refclock_open	(const char *, u_int, u_int);
224 extern	int	refclock_setup	(int, u_int, u_int);
225 extern	void	refclock_timer	(struct peer *);
226 extern	void	refclock_transmit(struct peer *);
227 extern 	int	refclock_process(struct refclockproc *);
228 extern 	int	refclock_process_f(struct refclockproc *, double);
229 extern 	void	refclock_process_offset(struct refclockproc *, l_fp,
230 					l_fp, double);
231 extern	void	refclock_report	(struct peer *, int);
232 extern	int	refclock_gtlin	(struct recvbuf *, char *, int, l_fp *);
233 extern	int	refclock_gtraw	(struct recvbuf *, char *, int, l_fp *);
234 extern	int	indicate_refclock_packet(struct refclockio *,
235 					 struct recvbuf *);
236 extern	void	process_refclock_packet(struct recvbuf *);
237 #endif /* REFCLOCK */
238 
239 #endif /* NTP_REFCLOCK_H */
240