xref: /freebsd/contrib/ntp/include/ntp_refclock.h (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * ntp_refclock.h - definitions for reference clock support
3  */
4 
5 #ifndef NTP_REFCLOCK_H
6 #define NTP_REFCLOCK_H
7 
8 #include "ntp_types.h"
9 
10 #if defined(HAVE_BSD_TTYS)
11 #include <sgtty.h>
12 #endif /* HAVE_BSD_TTYS */
13 
14 #if defined(HAVE_SYSV_TTYS)
15 #include <termio.h>
16 #endif /* HAVE_SYSV_TTYS */
17 
18 #if defined(HAVE_TERMIOS)
19 # ifdef TERMIOS_NEEDS__SVID3
20 #  define _SVID3
21 # endif
22 # include <termios.h>
23 # ifdef TERMIOS_NEEDS__SVID3
24 #  undef _SVID3
25 # endif
26 #endif
27 
28 #if defined(HAVE_SYS_MODEM_H)
29 #include <sys/modem.h>
30 #endif
31 
32 #if defined(STREAM)
33 #include <stropts.h>
34 #if defined(CLK)
35 #include <sys/clkdefs.h>
36 #endif /* CLK */
37 #endif /* STREAM */
38 
39 #include "recvbuff.h"
40 
41 #if !defined(SYSV_TTYS) && !defined(STREAM) & !defined(BSD_TTYS)
42 #define BSD_TTYS
43 #endif /* SYSV_TTYS STREAM BSD_TTYS */
44 
45 #define SAMPLE(x)	if ((pp->coderecv + 1) % MAXSTAGE !=		\
46 			    pp->codeproc % MAXSTAGE)			\
47 				pp->filter[pp->coderecv++ % MAXSTAGE] =	\
48 				    (x);
49 
50 /*
51  * Macros to determine the clock type and unit numbers from a
52  * 127.127.t.u address
53  */
54 #define	REFCLOCKTYPE(srcadr)	((SRCADR(srcadr) >> 8) & 0xff)
55 #define REFCLOCKUNIT(srcadr)	(SRCADR(srcadr) & 0xff)
56 
57 /*
58  * List of reference clock names and descriptions. These must agree with
59  * lib/clocktypes.c and ntpd/refclock_conf.c.
60  */
61 struct clktype {
62 	int code;		/* driver "major" number */
63 	const char *clocktype;	/* long description */
64 	const char *abbrev;	/* short description */
65 };
66 
67 /*
68  * Configuration flag values
69  */
70 #define	CLK_HAVETIME1	0x1
71 #define	CLK_HAVETIME2	0x2
72 #define	CLK_HAVEVAL1	0x4
73 #define	CLK_HAVEVAL2	0x8
74 
75 #define	CLK_FLAG1	0x1
76 #define	CLK_FLAG2	0x2
77 #define	CLK_FLAG3	0x4
78 #define	CLK_FLAG4	0x8
79 
80 #define	CLK_HAVEFLAG1	0x10
81 #define	CLK_HAVEFLAG2	0x20
82 #define	CLK_HAVEFLAG3	0x40
83 #define	CLK_HAVEFLAG4	0x80
84 
85 /*
86  * Constant for disabling event reporting in
87  * refclock_receive. ORed in leap
88  * parameter
89  */
90 #define REFCLOCK_OWN_STATES	0x80
91 
92 /*
93  * Structure for returning clock status
94  */
95 struct refclockstat {
96 	u_char	type;		/* clock type */
97 	u_char	flags;		/* clock flags */
98 	u_char	haveflags;	/* bit array of valid flags */
99 	u_short	lencode;	/* length of last timecode */
100 	const char *p_lastcode;	/* last timecode received */
101 	u_int32	polls;		/* transmit polls */
102 	u_int32	noresponse;	/* no response to poll */
103 	u_int32	badformat;	/* bad format timecode received */
104 	u_int32	baddata;	/* invalid data timecode received */
105 	u_int32	timereset;	/* driver resets */
106 	const char *clockdesc;	/* ASCII description */
107 	double	fudgetime1;	/* configure fudge time1 */
108 	double	fudgetime2;	/* configure fudge time2 */
109 	int32	fudgeval1;	/* configure fudge value1 */
110 	int32	fudgeval2;	/* configure fudge value2 */
111 	u_char	currentstatus;	/* clock status */
112 	u_char	lastevent;	/* last exception event */
113 	u_char	leap;		/* leap bits */
114 	struct	ctl_var *kv_list; /* additional variables */
115 };
116 
117 /*
118  * Reference clock I/O structure.  Used to provide an interface between
119  * the reference clock drivers and the I/O module.
120  */
121 struct refclockio {
122 	struct	refclockio *next; /* link to next structure */
123 	void	(*clock_recv) P((struct recvbuf *)); /* completion routine */
124 	int 	(*io_input)   P((struct recvbuf *)); /* input routine -
125 				to avoid excessive buffer use
126 				due to small bursts
127 				of refclock input data */
128 	caddr_t	srcclock;	/* pointer to clock structure */
129 	int	datalen;	/* lenth of data */
130 	int	fd;		/* file descriptor */
131 	u_long	recvcount;	/* count of receive completions */
132 };
133 
134 /*
135  * Structure for returning debugging info
136  */
137 #define	NCLKBUGVALUES	16
138 #define	NCLKBUGTIMES	32
139 
140 struct refclockbug {
141 	u_char	nvalues;	/* values following */
142 	u_char	ntimes;		/* times following */
143 	u_short	svalues;	/* values format sign array */
144 	u_int32	stimes;		/* times format sign array */
145 	u_int32	values[NCLKBUGVALUES]; /* real values */
146 	l_fp	times[NCLKBUGTIMES]; /* real times */
147 };
148 
149 /*
150  * Structure interface between the reference clock support
151  * ntp_refclock.c and the driver utility routines
152  */
153 #define MAXSTAGE	60	/* max median filter stages  */
154 #define NSTAGE		5	/* default median filter stages */
155 #define BMAX		128	/* max timecode length */
156 #define GMT		0	/* I hope nobody sees this */
157 #define MAXDIAL		60	/* max length of modem dial strings */
158 
159 /*
160  * Line discipline flags. These require line discipline or streams
161  * modules to be installed/loaded in the kernel. If specified, but not
162  * installed, the code runs as if unspecified.
163  */
164 #define LDISC_STD	0x0	/* standard */
165 #define LDISC_CLK	0x1	/* tty_clk \n intercept */
166 #define LDISC_CLKPPS	0x2	/* tty_clk \377 intercept */
167 #define LDISC_ACTS	0x4	/* tty_clk #* intercept */
168 #define LDISC_CHU	0x8	/* depredated */
169 #define LDISC_PPS	0x10	/* ppsclock, ppsapi */
170 #define LDISC_RAW	0x20	/* raw binary */
171 
172 struct refclockproc {
173 	struct	refclockio io;	/* I/O handler structure */
174 	caddr_t	unitptr;	/* pointer to unit structure */
175 	u_char	leap;		/* leap/synchronization code */
176 	u_char	currentstatus;	/* clock status */
177 	u_char	lastevent;	/* last exception event */
178 	u_char	type;		/* clock type */
179 	const char *clockdesc;	/* clock description */
180 
181 	char	a_lastcode[BMAX]; /* last timecode received */
182 	u_short	lencode;	/* length of last timecode */
183 
184 	int	year;		/* year of eternity */
185 	int	day;		/* day of year */
186 	int	hour;		/* hour of day */
187 	int	minute;		/* minute of hour */
188 	int	second;		/* second of minute */
189 	int	msec;		/* millisecond of second */
190 	long	usec;		/* microsecond of second (alt) */
191 	u_long	yearstart;	/* beginning of year */
192 	int	coderecv;	/* put pointer */
193 	int	codeproc;	/* get pointer */
194 	l_fp	lastref;	/* timecode timestamp */
195 	l_fp	lastrec;	/* local timestamp */
196 	double	offset;		/* mean offset */
197 	double	disp;		/* sample dispersion */
198 	double	variance;	/* sample variance */
199 	double	filter[MAXSTAGE]; /* median filter */
200 
201 	/*
202 	 * Configuration data
203 	 */
204 	double	fudgetime1;	/* fudge time1 */
205 	double	fudgetime2;	/* fudge time2 */
206 	u_int32	refid;		/* reference identifier */
207 	u_char	sloppyclockflag; /* fudge flags */
208 
209 	/*
210 	 * Status tallies
211  	 */
212 	u_long	timestarted;	/* time we started this */
213 	u_long	polls;		/* polls sent */
214 	u_long	noreply;	/* no replies to polls */
215 	u_long	badformat;	/* bad format reply */
216 	u_long	baddata;	/* bad data reply */
217 };
218 
219 /*
220  * Structure interface between the reference clock support
221  * ntp_refclock.c and particular clock drivers. This must agree with the
222  * structure defined in the driver.
223  */
224 #define	noentry	0		/* flag for null routine */
225 #define	NOFLAGS	0		/* flag for null flags */
226 
227 struct refclock {
228 	int (*clock_start)	P((int, struct peer *));
229 	void (*clock_shutdown)	P((int, struct peer *));
230 	void (*clock_poll)	P((int, struct peer *));
231 	void (*clock_control)	P((int, struct refclockstat *,
232 				    struct refclockstat *, struct peer *));
233 	void (*clock_init)	P((void));
234 	void (*clock_buginfo)	P((int, struct refclockbug *, struct peer *));
235 	u_long clock_flags;
236 };
237 
238 /*
239  * Function prototypes
240  */
241 /*
242  * auxiliary PPS interface (implemented by refclock_atom())
243  */
244 extern	int	pps_sample P((l_fp *));
245 extern	int	io_addclock_simple P((struct refclockio *));
246 extern	int	io_addclock	P((struct refclockio *));
247 extern	void	io_closeclock	P((struct refclockio *));
248 
249 #ifdef REFCLOCK
250 extern	void	refclock_buginfo P((struct sockaddr_in *,
251 				    struct refclockbug *));
252 extern	void	refclock_control P((struct sockaddr_in *,
253 				    struct refclockstat *,
254 				    struct refclockstat *));
255 extern	int	refclock_open	P((char *, int, int));
256 extern	void	refclock_transmit P((struct peer *));
257 extern	int	refclock_ioctl	P((int, int));
258 extern 	int	refclock_process P((struct refclockproc *));
259 extern 	void	refclock_process_offset P((struct refclockproc *, l_fp, l_fp, double));
260 extern	void	refclock_report	P((struct peer *, int));
261 extern	int	refclock_gtlin	P((struct recvbuf *, char *, int,
262 				    l_fp *));
263 #endif /* REFCLOCK */
264 
265 #endif /* NTP_REFCLOCK_H */
266