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