2 * /src/NTP/REPOSITORY/ntp4-dev/include/parse.h,v 4.12 2007/01/14 08:36:03 kardel RELEASE_20070114_A
4 * parse.h,v 4.12 2007/01/14 08:36:03 kardel RELEASE_20070114_A
6 * Copyright (c) 1995-2015 by Frank Kardel <kardel <AT> ntp.org>
7 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if !(defined(lint) || defined(__GNUC__))
38 static char parsehrcsid[]="parse.h,v 4.12 2007/01/14 08:36:03 kardel RELEASE_20070114_A";
41 #include "ntp_types.h"
43 #include "parse_conf.h"
46 * we use the following datastructures in two modes
47 * either in the NTP itself where we use NTP time stamps at some places
48 * or in the kernel, where only struct timeval will be used.
51 #if defined(KERNEL) || defined(_KERNEL)
56 #if defined(PARSESTREAM) && defined(HAVE_SYS_STREAM_H)
61 extern caddr_t kmem_alloc (unsigned int);
62 extern caddr_t kmem_free (caddr_t, unsigned int);
63 extern unsigned int splx (unsigned int);
64 extern unsigned int splhigh (void);
65 extern unsigned int splclock (void);
66 #define MALLOC(_X_) (char *)kmem_alloc(_X_)
67 #define FREE(_X_, _Y_) kmem_free((caddr_t)_X_, _Y_)
70 #define MALLOC(_X_) (char *)kmem_alloc(_X_, KM_SLEEP)
71 #define FREE(_X_, _Y_) kmem_free((caddr_t)_X_, _Y_)
74 #define MALLOC(_X_) malloc(_X_)
75 #define FREE(_X_, _Y_) free(_X_)
78 #if defined(PARSESTREAM) && defined(HAVE_SYS_STREAM_H)
79 #include <sys/stream.h>
80 #include <sys/stropts.h>
83 #include "ntp_syslog.h"
87 #define parseprintf(LEVEL, ARGS) if (debug > LEVEL) printf ARGS
89 #define parseprintf(LEVEL, ARGS)
91 #endif /* PARSESTREAM */
93 #if defined(timercmp) && defined(__GNUC__)
97 #if !defined(timercmp)
98 #define timercmp(tvp, uvp, cmp) \
99 ((tvp)->tv_sec cmp (uvp)->tv_sec || \
100 ((tvp)->tv_sec == (uvp)->tv_sec && (tvp)->tv_usec cmp (uvp)->tv_usec))
104 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
108 * some constants useful for GPS time conversion
110 #define GPSORIGIN 2524953600UL /* NTP origin - GPS origin in seconds */
111 #define GPSWEEKS 1024 /* number of weeks until the GPS epch rolls over */
116 #define PARSEB_POWERUP 0x00000001 /* no synchronisation */
117 #define PARSEB_NOSYNC 0x00000002 /* timecode currently not confirmed */
120 * time zone information
122 #define PARSEB_ANNOUNCE 0x00000010 /* switch time zone warning (DST switch) */
123 #define PARSEB_DST 0x00000020 /* DST in effect */
124 #define PARSEB_UTC 0x00000040 /* UTC time */
129 #define PARSEB_LEAPDEL 0x00000100 /* LEAP deletion warning */
130 #define PARSEB_LEAPADD 0x00000200 /* LEAP addition warning */
131 #define PARSEB_LEAPS 0x00000300 /* LEAP warnings */
132 #define PARSEB_LEAPSECOND 0x00000400 /* actual leap second */
134 * optional status information
136 #define PARSEB_CALLBIT 0x00001000 /* "call bit" used to signalize irregularities in the control facilities */
137 #define PARSEB_POSITION 0x00002000 /* position available */
138 #define PARSEB_MESSAGE 0x00004000 /* addtitional message data */
140 * feature information
142 #define PARSEB_S_LEAP 0x00010000 /* supports LEAP */
143 #define PARSEB_S_CALLBIT 0x00020000 /* supports callbit information */
144 #define PARSEB_S_PPS 0x00040000 /* supports PPS time stamping */
145 #define PARSEB_S_POSITION 0x00080000 /* supports position information (GPS) */
148 * time stamp availability
150 #define PARSEB_TIMECODE 0x10000000 /* valid time code sample */
151 #define PARSEB_PPS 0x20000000 /* valid PPS sample */
153 #define PARSE_TCINFO (PARSEB_ANNOUNCE|PARSEB_POWERUP|PARSEB_NOSYNC|PARSEB_DST|\
154 PARSEB_UTC|PARSEB_LEAPS|PARSEB_CALLBIT|PARSEB_S_LEAP|\
155 PARSEB_S_LOCATION|PARSEB_TIMECODE|PARSEB_MESSAGE)
157 #define PARSE_POWERUP(x) ((x) & PARSEB_POWERUP)
158 #define PARSE_NOSYNC(x) (((x) & (PARSEB_POWERUP|PARSEB_NOSYNC)) == PARSEB_NOSYNC)
159 #define PARSE_SYNC(x) (((x) & (PARSEB_POWERUP|PARSEB_NOSYNC)) == 0)
160 #define PARSE_ANNOUNCE(x) ((x) & PARSEB_ANNOUNCE)
161 #define PARSE_DST(x) ((x) & PARSEB_DST)
162 #define PARSE_UTC(x) ((x) & PARSEB_UTC)
163 #define PARSE_LEAPADD(x) (PARSE_SYNC(x) && (((x) & PARSEB_LEAPS) == PARSEB_LEAPADD))
164 #define PARSE_LEAPDEL(x) (PARSE_SYNC(x) && (((x) & PARSEB_LEAPS) == PARSEB_LEAPDEL))
165 #define PARSE_CALLBIT(x) ((x) & PARSEB_CALLBIT)
166 #define PARSE_LEAPSECOND(x) (PARSE_SYNC(x) && ((x) & PARSEB_LEAP_SECOND))
168 #define PARSE_S_LEAP(x) ((x) & PARSEB_S_LEAP)
169 #define PARSE_S_CALLBIT(x) ((x) & PARSEB_S_CALLBIT)
170 #define PARSE_S_PPS(x) ((x) & PARSEB_S_PPS)
171 #define PARSE_S_POSITION(x) ((x) & PARSEB_S_POSITION)
173 #define PARSE_TIMECODE(x) ((x) & PARSEB_TIMECODE)
174 #define PARSE_PPS(x) ((x) & PARSEB_PPS)
175 #define PARSE_POSITION(x) ((x) & PARSEB_POSITION)
176 #define PARSE_MESSAGE(x) ((x) & PARSEB_MESSAGE)
179 * operation flags - lower nibble contains fudge flags
181 #define PARSE_TRUSTTIME CLK_FLAG1 /* use flag1 to indicate the time2 references mean the trust time */
182 #define PARSE_CLEAR CLK_FLAG2 /* use flag2 to control pps on assert */
183 #define PARSE_PPSKERNEL CLK_FLAG3 /* use flag3 to bind PPS to kernel */
184 #define PARSE_LEAP_DELETE CLK_FLAG4 /* use flag4 to force leap deletion - only necessary when earth slows down */
186 #define PARSE_FIXED_FMT 0x10 /* fixed format */
187 #define PARSE_PPSCLOCK 0x20 /* try to get PPS time stamp via ppsclock ioctl */
192 #define PARSE_TCMAX 400 /* maximum addition data size */
196 struct timeval tv; /* timeval - kernel view */
197 l_fp fp; /* fixed point - ntp view */
201 * standard time stamp structure
205 u_long parse_status; /* data status - CVT_OK, CVT_NONE, CVT_FAIL ... */
206 timestamp_t parse_time; /* PARSE timestamp */
207 timestamp_t parse_stime; /* telegram sample timestamp */
208 timestamp_t parse_ptime; /* PPS time stamp */
209 long parse_usecerror; /* sampled usec error */
210 u_long parse_state; /* current receiver state */
211 unsigned short parse_format; /* format code */
212 unsigned short parse_msglen; /* length of message */
213 unsigned char parse_msg[PARSE_TCMAX]; /* original messages */
216 typedef struct parsetime parsetime_t;
218 /*---------- STREAMS interface ----------*/
220 #ifdef HAVE_SYS_STREAM_H
224 #define PARSEIOC_ENABLE (('D'<<8) + 'E')
225 #define PARSEIOC_DISABLE (('D'<<8) + 'D')
226 #define PARSEIOC_SETFMT (('D'<<8) + 'f')
227 #define PARSEIOC_GETFMT (('D'<<8) + 'F')
228 #define PARSEIOC_SETCS (('D'<<8) + 'C')
229 #define PARSEIOC_TIMECODE (('D'<<8) + 'T')
233 /*------ IO handling flags (sorry) ------*/
235 #define PARSE_IO_CSIZE 0x00000003
236 #define PARSE_IO_CS5 0x00000000
237 #define PARSE_IO_CS6 0x00000001
238 #define PARSE_IO_CS7 0x00000002
239 #define PARSE_IO_CS8 0x00000003
248 u_long parse_state; /* last state */
249 u_long parse_badformat; /* number of bad packets since last query */
250 unsigned short parse_format;/* last decoded format */
251 unsigned short parse_count; /* count of valid time code bytes */
252 char parse_buffer[PARSE_TCMAX+1]; /* timecode buffer */
257 unsigned short parse_format;/* number of examined format */
258 unsigned short parse_count; /* count of valid string bytes */
259 char parse_buffer[PARSE_TCMAX+1]; /* format code string */
264 u_long parse_cs; /* character size (needed for stripping) */
268 typedef union parsectl parsectl_t;
270 /*------ for conversion routines --------*/
272 struct parse /* parse module local data */
274 int parse_flags; /* operation and current status flags */
276 int parse_ioflags; /* io handling flags (5-8 Bit control currently) */
279 * private data - fixed format only
281 unsigned short parse_plen; /* length of private data */
282 void *parse_pdata; /* private data pointer */
285 * time code input buffer (from RS232 or PPS)
287 unsigned short parse_index; /* current buffer index */
288 char *parse_data; /* data buffer */
289 unsigned short parse_dsize; /* size of data buffer */
290 unsigned short parse_lformat; /* last format used */
291 u_long parse_lstate; /* last state code */
292 char *parse_ldata; /* last data buffer */
293 unsigned short parse_ldsize; /* last data buffer length */
294 u_long parse_badformat; /* number of unparsable pakets */
296 timestamp_t parse_lastchar; /* last time a character was received */
297 parsetime_t parse_dtime; /* external data prototype */
300 typedef struct parse parse_t;
302 struct clocktime /* clock time broken up from time code */
311 long utcoffset; /* in seconds */
312 time_t utctime; /* the actual time - alternative to date/time */
313 u_long flags; /* current clock status */
316 typedef struct clocktime clocktime_t;
319 * parser related return/error codes
321 #define CVT_MASK (unsigned)0x0000000F /* conversion exit code */
322 #define CVT_NONE (unsigned)0x00000001 /* format not applicable */
323 #define CVT_FAIL (unsigned)0x00000002 /* conversion failed - error code returned */
324 #define CVT_OK (unsigned)0x00000004 /* conversion succeeded */
325 #define CVT_SKIP (unsigned)0x00000008 /* conversion succeeded */
326 #define CVT_ADDITIONAL (unsigned)0x00000010 /* additional data is available */
327 #define CVT_BADFMT (unsigned)0x00000100 /* general format error - (unparsable) */
328 #define CVT_BADDATE (unsigned)0x00000200 /* date field incorrect */
329 #define CVT_BADTIME (unsigned)0x00000400 /* time field incorrect */
332 * return codes used by special input parsers
334 #define PARSE_INP_SKIP 0x00 /* discard data - may have been consumed */
335 #define PARSE_INP_TIME 0x01 /* time code assembled */
336 #define PARSE_INP_PARSE 0x02 /* parse data using normal algorithm */
337 #define PARSE_INP_DATA 0x04 /* additional data to pass up */
338 #define PARSE_INP_SYNTH 0x08 /* just pass up synthesized time */
343 #define SYNC_ZERO 0x00
344 #define SYNC_ONE 0x01
346 typedef u_long parse_inp_fnc_t(parse_t *, char, timestamp_t *);
347 typedef u_long parse_cvt_fnc_t(unsigned char *, int, struct format *, clocktime_t *, void *);
348 typedef u_long parse_pps_fnc_t(parse_t *, int, timestamp_t *);
352 /* special input protocol - implies fixed format */
353 parse_inp_fnc_t *input;
354 /* conversion routine */
355 parse_cvt_fnc_t *convert;
356 /* routine for handling RS232 sync events (time stamps) */
357 /* PPS input routine */
358 parse_pps_fnc_t *syncpps;
359 /* time code synthesizer */
361 void *data; /* local parameters */
362 const char *name; /* clock format name */
363 unsigned short length; /* maximum length of data packet */
364 unsigned short plen; /* length of private data - implies fixed format */
367 typedef struct clockformat clockformat_t;
372 extern int parse_ioinit (parse_t *);
373 extern void parse_ioend (parse_t *);
374 extern int parse_ioread (parse_t *, char, timestamp_t *);
375 extern int parse_iopps (parse_t *, int, timestamp_t *);
376 extern void parse_iodone (parse_t *);
377 extern int parse_timecode (parsectl_t *, parse_t *);
378 extern int parse_getfmt (parsectl_t *, parse_t *);
379 extern int parse_setfmt (parsectl_t *, parse_t *);
380 extern int parse_setcs (parsectl_t *, parse_t *);
382 extern unsigned int parse_restart (parse_t *, char);
383 extern unsigned int parse_addchar (parse_t *, char);
384 extern unsigned int parse_end (parse_t *);
386 extern int Strok (const unsigned char *, const unsigned char *);
387 extern int Stoi (const unsigned char *, long *, int);
389 extern time_t parse_to_unixtime (clocktime_t *, u_long *);
390 extern u_long updatetimeinfo (parse_t *, u_long);
391 extern void syn_simple (parse_t *, timestamp_t *, struct format *, u_long);
392 extern parse_pps_fnc_t pps_simple;
393 extern parse_pps_fnc_t pps_one;
394 extern parse_pps_fnc_t pps_zero;
395 extern int parse_timedout (parse_t *, timestamp_t *, struct timeval *);
403 * Revision 4.12 2007/01/14 08:36:03 kardel
404 * make timestamp union anonymous to avoid conflicts with
405 * some OSes that choose to create a nameing conflic here.
407 * Revision 4.11 2005/06/25 10:58:45 kardel
408 * add missing log keywords
410 * Revision 4.5 1998/08/09 22:23:32 kardel
411 * 4.0.73e2 adjustments
413 * Revision 4.4 1998/06/14 21:09:27 kardel
416 * Revision 4.3 1998/06/13 11:49:25 kardel
417 * STREAM macro gone in favor of HAVE_SYS_STREAM_H
419 * Revision 4.2 1998/06/12 15:14:25 kardel
422 * Revision 4.1 1998/05/24 10:07:59 kardel
423 * removed old data structure cruft (new input model)
424 * new PARSE_INP* macros for input handling
425 * removed old SYNC_* macros from old input model
426 * (struct clockformat): removed old parse functions in favor of the
430 * form V3 3.31 - log info deleted 1998/04/11 kardel