xref: /freebsd/contrib/ntp/include/parse.h (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*
2  * /src/NTP/ntp-4/include/parse.h,v 4.5 1998/08/09 22:23:32 kardel RELEASE_19990228_A
3  *
4  * parse.h,v 4.5 1998/08/09 22:23:32 kardel RELEASE_19990228_A
5  *
6  * Copyright (C) 1989-1998 by Frank Kardel
7  * Friedrich-Alexander Universit�t Erlangen-N�rnberg, Germany
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  */
14 
15 #ifndef __PARSE_H__
16 #define __PARSE_H__
17 #if	!(defined(lint) || defined(__GNUC__))
18   static char parsehrcsid[]="parse.h,v 4.5 1998/08/09 22:23:32 kardel RELEASE_19990228_A";
19 #endif
20 
21 #include "ntp_types.h"
22 
23 #include "parse_conf.h"
24 
25 /*
26  * we use the following datastructures in two modes
27  * either in the NTP itself where we use NTP time stamps at some places
28  * or in the kernel, where only struct timeval will be used.
29  */
30 #undef PARSEKERNEL
31 #if defined(KERNEL) || defined(_KERNEL)
32 #ifndef PARSESTREAM
33 #define PARSESTREAM
34 #endif
35 #endif
36 #if defined(PARSESTREAM) && defined(HAVE_SYS_STREAM_H)
37 #define PARSEKERNEL
38 #endif
39 #ifdef PARSEKERNEL
40 #ifndef _KERNEL
41 extern caddr_t kmem_alloc P((unsigned int));
42 extern caddr_t kmem_free P((caddr_t, unsigned int));
43 extern unsigned int splx P((unsigned int));
44 extern unsigned int splhigh P((void));
45 extern unsigned int splclock P((void));
46 #define MALLOC(_X_) (char *)kmem_alloc(_X_)
47 #define FREE(_X_, _Y_) kmem_free((caddr_t)_X_, _Y_)
48 #else
49 #include <sys/kmem.h>
50 #define MALLOC(_X_) (char *)kmem_alloc(_X_, KM_SLEEP)
51 #define FREE(_X_, _Y_) kmem_free((caddr_t)_X_, _Y_)
52 #endif
53 #else
54 #define MALLOC(_X_) malloc(_X_)
55 #define FREE(_X_, _Y_) free(_X_)
56 #endif
57 
58 #if defined(PARSESTREAM) && defined(HAVE_SYS_STREAM_H)
59 #include <sys/stream.h>
60 #include <sys/stropts.h>
61 #else	/* STREAM */
62 #include <stdio.h>
63 #include "ntp_syslog.h"
64 #ifdef	DEBUG
65 #define DD_PARSE 5
66 #define DD_RAWDCF 4
67 #define parseprintf(LEVEL, ARGS) if (debug > LEVEL) printf ARGS
68 #else	/* DEBUG */
69 #define parseprintf(LEVEL, ARGS)
70 #endif	/* DEBUG */
71 #endif	/* PARSESTREAM */
72 
73 #if defined(timercmp) && defined(__GNUC__)
74 #undef timercmp
75 #endif
76 
77 #if !defined(timercmp)
78 #define	timercmp(tvp, uvp, cmp)	\
79 	((tvp)->tv_sec cmp (uvp)->tv_sec || \
80 	 ((tvp)->tv_sec == (uvp)->tv_sec && (tvp)->tv_usec cmp (uvp)->tv_usec))
81 #endif
82 
83 #ifndef TIMES10
84 #define TIMES10(_X_)	(((_X_) << 3) + ((_X_) << 1))
85 #endif
86 
87 /*
88  * state flags
89  */
90 #define PARSEB_POWERUP            0x00000001 /* no synchronisation */
91 #define PARSEB_NOSYNC             0x00000002 /* timecode currently not confirmed */
92 
93 /*
94  * time zone information
95  */
96 #define PARSEB_ANNOUNCE           0x00000010 /* switch time zone warning (DST switch) */
97 #define PARSEB_DST                0x00000020 /* DST in effect */
98 #define PARSEB_UTC		  0x00000040 /* UTC time */
99 
100 /*
101  * leap information
102  */
103 #define PARSEB_LEAPDEL		  0x00000100 /* LEAP deletion warning */
104 #define PARSEB_LEAPADD		  0x00000200 /* LEAP addition warning */
105 #define PARSEB_LEAPS		  0x00000300 /* LEAP warnings */
106 #define PARSEB_LEAPSECOND	  0x00000400 /* actual leap second */
107 /*
108  * optional status information
109  */
110 #define PARSEB_ALTERNATE	  0x00001000 /* alternate antenna used */
111 #define PARSEB_POSITION		  0x00002000 /* position available */
112 #define PARSEB_MESSAGE            0x00004000 /* addtitional message data */
113 /*
114  * feature information
115  */
116 #define PARSEB_S_LEAP		  0x00010000 /* supports LEAP */
117 #define PARSEB_S_ANTENNA	  0x00020000 /* supports antenna information */
118 #define PARSEB_S_PPS     	  0x00040000 /* supports PPS time stamping */
119 #define PARSEB_S_POSITION	  0x00080000 /* supports position information (GPS) */
120 
121 /*
122  * time stamp availability
123  */
124 #define PARSEB_TIMECODE		  0x10000000 /* valid time code sample */
125 #define PARSEB_PPS		  0x20000000 /* valid PPS sample */
126 
127 #define PARSE_TCINFO		(PARSEB_ANNOUNCE|PARSEB_POWERUP|PARSEB_NOSYNC|PARSEB_DST|\
128 				 PARSEB_UTC|PARSEB_LEAPS|PARSEB_ALTERNATE|PARSEB_S_LEAP|\
129 				 PARSEB_S_LOCATION|PARSEB_TIMECODE|PARSEB_MESSAGE)
130 
131 #define PARSE_POWERUP(x)        ((x) & PARSEB_POWERUP)
132 #define PARSE_NOSYNC(x)         (((x) & (PARSEB_POWERUP|PARSEB_NOSYNC)) == PARSEB_NOSYNC)
133 #define PARSE_SYNC(x)           (((x) & (PARSEB_POWERUP|PARSEB_NOSYNC)) == 0)
134 #define PARSE_ANNOUNCE(x)       ((x) & PARSEB_ANNOUNCE)
135 #define PARSE_DST(x)            ((x) & PARSEB_DST)
136 #define PARSE_UTC(x)		((x) & PARSEB_UTC)
137 #define PARSE_LEAPADD(x)	(PARSE_SYNC(x) && (((x) & PARSEB_LEAPS) == PARSEB_LEAPADD))
138 #define PARSE_LEAPDEL(x)	(PARSE_SYNC(x) && (((x) & PARSEB_LEAPS) == PARSEB_LEAPDEL))
139 #define PARSE_ALTERNATE(x)	((x) & PARSEB_ALTERNATE)
140 #define PARSE_LEAPSECOND(x)	(PARSE_SYNC(x) && ((x) & PARSEB_LEAP_SECOND))
141 
142 #define PARSE_S_LEAP(x)		((x) & PARSEB_S_LEAP)
143 #define PARSE_S_ANTENNA(x)	((x) & PARSEB_S_ANTENNA)
144 #define PARSE_S_PPS(x)		((x) & PARSEB_S_PPS)
145 #define PARSE_S_POSITION(x)	((x) & PARSEB_S_POSITION)
146 
147 #define PARSE_TIMECODE(x)	((x) & PARSEB_TIMECODE)
148 #define PARSE_PPS(x)		((x) & PARSEB_PPS)
149 #define PARSE_POSITION(x)	((x) & PARSEB_POSITION)
150 #define PARSE_MESSAGE(x)	((x) & PARSEB_MESSAGE)
151 
152 /*
153  * operation flags - lower nibble contains fudge flags
154  */
155 #define PARSE_STATISTICS    0x08  /* enable statistics */
156 #define PARSE_LEAP_DELETE   0x04  /* delete leap */
157 #define PARSE_FIXED_FMT     0x10  /* fixed format */
158 #define PARSE_PPSCLOCK      0x20  /* try to get PPS time stamp via ppsclock ioctl */
159 
160 /*
161  * size of buffers
162  */
163 #define PARSE_TCMAX	    400	  /* maximum addition data size */
164 
165 typedef union timestamp
166 {
167   struct timeval tv;		/* timeval - kernel view */
168   l_fp           fp;		/* fixed point - ntp view */
169 } timestamp_t;
170 
171 /*
172  * standard time stamp structure
173  */
174 struct parsetime
175 {
176   u_long  parse_status;	/* data status - CVT_OK, CVT_NONE, CVT_FAIL ... */
177   timestamp_t	 parse_time;	/* PARSE timestamp */
178   timestamp_t	 parse_stime;	/* telegram sample timestamp */
179   timestamp_t	 parse_ptime;	/* PPS time stamp */
180   long           parse_usecerror;	/* sampled usec error */
181   u_long	 parse_state;	/* current receiver state */
182   unsigned short parse_format;	/* format code */
183   unsigned short parse_msglen;	/* length of message */
184   unsigned char  parse_msg[PARSE_TCMAX]; /* original messages */
185 };
186 
187 typedef struct parsetime parsetime_t;
188 
189 /*---------- STREAMS interface ----------*/
190 
191 #ifdef HAVE_SYS_STREAM_H
192 /*
193  * ioctls
194  */
195 #define PARSEIOC_ENABLE		(('D'<<8) + 'E')
196 #define PARSEIOC_DISABLE	(('D'<<8) + 'D')
197 #define PARSEIOC_SETFMT         (('D'<<8) + 'f')
198 #define PARSEIOC_GETFMT	        (('D'<<8) + 'F')
199 #define PARSEIOC_SETCS	        (('D'<<8) + 'C')
200 #define PARSEIOC_TIMECODE	(('D'<<8) + 'T')
201 
202 #endif
203 
204 /*------ IO handling flags (sorry) ------*/
205 
206 #define PARSE_IO_CSIZE	0x00000003
207 #define PARSE_IO_CS5	0x00000000
208 #define PARSE_IO_CS6	0x00000001
209 #define PARSE_IO_CS7	0x00000002
210 #define PARSE_IO_CS8	0x00000003
211 
212 /*
213  * ioctl structure
214  */
215 union parsectl
216 {
217   struct parsegettc
218     {
219       u_long         parse_state;	/* last state */
220       u_long         parse_badformat; /* number of bad packets since last query */
221       unsigned short parse_format;/* last decoded format */
222       unsigned short parse_count;	/* count of valid time code bytes */
223       char           parse_buffer[PARSE_TCMAX+1]; /* timecode buffer */
224     } parsegettc;
225 
226   struct parseformat
227     {
228       unsigned short parse_format;/* number of examined format */
229       unsigned short parse_count;	/* count of valid string bytes */
230       char           parse_buffer[PARSE_TCMAX+1]; /* format code string */
231     } parseformat;
232 
233   struct parsesetcs
234     {
235       u_long         parse_cs;	/* character size (needed for stripping) */
236     } parsesetcs;
237 };
238 
239 typedef union parsectl parsectl_t;
240 
241 /*------ for conversion routines --------*/
242 
243 struct parse			/* parse module local data */
244 {
245   int            parse_flags;	/* operation and current status flags */
246 
247   int		 parse_ioflags;	   /* io handling flags (5-8 Bit control currently) */
248 
249   /*
250    * private data - fixed format only
251    */
252   unsigned short parse_plen;	/* length of private data */
253   void          *parse_pdata;	/* private data pointer */
254 
255   /*
256    * time code input buffer (from RS232 or PPS)
257    */
258   unsigned short parse_index;	/* current buffer index */
259   char          *parse_data;    /* data buffer */
260   unsigned short parse_dsize;	/* size of data buffer */
261   unsigned short parse_lformat;	/* last format used */
262   u_long         parse_lstate;	/* last state code */
263   char          *parse_ldata;	/* last data buffer */
264   unsigned short parse_ldsize;	/* last data buffer length */
265   u_long         parse_badformat;	/* number of unparsable pakets */
266 
267   timestamp_t    parse_lastchar; /* last time a character was received */
268   parsetime_t    parse_dtime;	/* external data prototype */
269 };
270 
271 typedef struct parse parse_t;
272 
273 struct clocktime		/* clock time broken up from time code */
274 {
275   long day;
276   long month;
277   long year;
278   long hour;
279   long minute;
280   long second;
281   long usecond;
282   long utcoffset;	/* in seconds */
283   time_t utctime;	/* the actual time - alternative to date/time */
284   u_long flags;		/* current clock status */
285 };
286 
287 typedef struct clocktime clocktime_t;
288 
289 /*
290  * parser related return/error codes
291  */
292 #define CVT_MASK	 (unsigned)0x0000000F /* conversion exit code */
293 #define   CVT_NONE	 (unsigned)0x00000001 /* format not applicable */
294 #define   CVT_FAIL	 (unsigned)0x00000002 /* conversion failed - error code returned */
295 #define   CVT_OK	 (unsigned)0x00000004 /* conversion succeeded */
296 #define   CVT_SKIP	 (unsigned)0x00000008 /* conversion succeeded */
297 #define CVT_ADDITIONAL   (unsigned)0x00000010 /* additional data is available */
298 #define CVT_BADFMT	 (unsigned)0x00000100 /* general format error - (unparsable) */
299 #define CVT_BADDATE      (unsigned)0x00000200 /* date field incorrect */
300 #define CVT_BADTIME	 (unsigned)0x00000400 /* time field incorrect */
301 
302 /*
303  * return codes used by special input parsers
304  */
305 #define PARSE_INP_SKIP  0x00	/* discard data - may have been consumed */
306 #define PARSE_INP_TIME  0x01	/* time code assembled */
307 #define PARSE_INP_PARSE 0x02	/* parse data using normal algorithm */
308 #define PARSE_INP_DATA  0x04	/* additional data to pass up */
309 #define PARSE_INP_SYNTH 0x08	/* just pass up synthesized time */
310 
311 /*
312  * PPS edge info
313  */
314 #define SYNC_ZERO	0x00
315 #define SYNC_ONE	0x01
316 
317 struct clockformat
318 {
319   /* special input protocol - implies fixed format */
320   u_long	(*input)   P((parse_t *, unsigned int, timestamp_t *));
321   /* conversion routine */
322   u_long        (*convert) P((unsigned char *, int, struct format *, clocktime_t *, void *));
323   /* routine for handling RS232 sync events (time stamps) */
324   /* PPS input routine */
325   u_long        (*syncpps) P((parse_t *, int, timestamp_t *));
326   /* time code synthesizer */
327 
328   void           *data;		/* local parameters */
329   const char     *name;		/* clock format name */
330   unsigned short  length;	/* maximum length of data packet */
331   unsigned short  plen;		/* length of private data - implies fixed format */
332 };
333 
334 typedef struct clockformat clockformat_t;
335 
336 /*
337  * parse interface
338  */
339 extern int  parse_ioinit P((parse_t *));
340 extern void parse_ioend P((parse_t *));
341 extern int  parse_ioread P((parse_t *, unsigned int, timestamp_t *));
342 extern int  parse_iopps P((parse_t *, int, timestamp_t *));
343 extern void parse_iodone P((parse_t *));
344 extern int  parse_timecode P((parsectl_t *, parse_t *));
345 extern int  parse_getfmt P((parsectl_t *, parse_t *));
346 extern int  parse_setfmt P((parsectl_t *, parse_t *));
347 extern int  parse_setcs P((parsectl_t *, parse_t *));
348 
349 extern unsigned int parse_restart P((parse_t *, unsigned int));
350 extern unsigned int parse_addchar P((parse_t *, unsigned int));
351 extern unsigned int parse_end P((parse_t *));
352 
353 extern int Strok P((const unsigned char *, const unsigned char *));
354 extern int Stoi P((const unsigned char *, long *, int));
355 
356 extern time_t parse_to_unixtime P((clocktime_t *, u_long *));
357 extern u_long updatetimeinfo P((parse_t *, u_long));
358 extern void syn_simple P((parse_t *, timestamp_t *, struct format *, u_long));
359 extern u_long pps_simple P((parse_t *, int, timestamp_t *));
360 extern u_long pps_one P((parse_t *, int, timestamp_t *));
361 extern u_long pps_zero P((parse_t *, int, timestamp_t *));
362 extern int parse_timedout P((parse_t *, timestamp_t *, struct timeval *));
363 
364 #endif
365 
366 /*
367  * History:
368  *
369  * parse.h,v
370  * Revision 4.5  1998/08/09 22:23:32  kardel
371  * 4.0.73e2 adjustments
372  *
373  * Revision 4.4  1998/06/14 21:09:27  kardel
374  * Sun acc cleanup
375  *
376  * Revision 4.3  1998/06/13 11:49:25  kardel
377  * STREAM macro gone in favor of HAVE_SYS_STREAM_H
378  *
379  * Revision 4.2  1998/06/12 15:14:25  kardel
380  * fixed prototypes
381  *
382  * Revision 4.1  1998/05/24 10:07:59  kardel
383  * removed old data structure cruft (new input model)
384  * new PARSE_INP* macros for input handling
385  * removed old SYNC_* macros from old input model
386  * (struct clockformat): removed old parse functions in favor of the
387  * new input model
388  * updated prototypes
389  *
390  * form V3 3.31 - log info deleted 1998/04/11 kardel
391  */
392