xref: /freebsd/contrib/ntp/include/ntp_refclock.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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  */
28 struct clktype {
29 	int code;		/* driver "major" number */
30 	const char *clocktype;	/* long description */
31 	const char *abbrev;	/* short description */
32 };
33 extern 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  */
64 struct 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  */
91 struct 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 
116 struct 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
126 extern	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 
139 struct 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 
199 struct 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  */
213 extern	int	io_addclock	(struct refclockio *);
214 extern	void	io_closeclock	(struct refclockio *);
215 
216 #define FDWRITE_ERROR	((size_t)-1)
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 sockaddr_u *srcadr, 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	int	refclock_samples_avail(struct refclockproc const *);
233 extern	int	refclock_samples_expire(struct refclockproc *, int);
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	size_t	refclock_write  (const struct peer *, const void *, size_t,
238 				 const char * what);
239 extern	size_t	refclock_fdwrite(const struct peer *, int, const void *, size_t,
240 				 const char * what);
241 extern	int	indicate_refclock_packet(struct refclockio *,
242 					 struct recvbuf *);
243 extern	void	process_refclock_packet(struct recvbuf *);
244 
245 /* save string as la_code, size==(size_t)-1 ==> ASCIIZ string */
246 extern	void	refclock_save_lcode(
247 			struct refclockproc *, char const *, size_t);
248 /* format data into la_code */
249 extern	void	refclock_format_lcode(
250 			struct refclockproc *, char const *, ...);
251 extern	void	refclock_vformat_lcode(
252 			struct refclockproc *, char const *, va_list);
253 
254 struct refclock_atom;
255 extern int	refclock_ppsaugment(
256     const struct refclock_atom*, l_fp *rcvtime ,
257     double rcvfudge, double ppsfudge);
258 
259 extern int ppsdev_reopen(const sockaddr_u *srcadr,
260 			 int ttyfd, int ppsfd, const char *ppspath,
261 			 int mode, int flags);
262 extern void ppsdev_close(int ttyfd, int ppsfd);
263 
264 #endif /* REFCLOCK */
265 
266 #endif /* NTP_REFCLOCK_H */
267