2 * /src/NTP/ntp-4/include/parse.h,v 4.5 1998/08/09 22:23:32 kardel RELEASE_19990228_A
4 * parse.h,v 4.5 1998/08/09 22:23:32 kardel RELEASE_19990228_A
6 * Copyright (C) 1989-1998 by Frank Kardel
7 * Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
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.
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";
21 #include "ntp_types.h"
23 #include "parse_conf.h"
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.
31 #if defined(KERNEL) || defined(_KERNEL)
36 #if defined(PARSESTREAM) && defined(HAVE_SYS_STREAM_H)
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_)
50 #define MALLOC(_X_) (char *)kmem_alloc(_X_, KM_SLEEP)
51 #define FREE(_X_, _Y_) kmem_free((caddr_t)_X_, _Y_)
54 #define MALLOC(_X_) malloc(_X_)
55 #define FREE(_X_, _Y_) free(_X_)
58 #if defined(PARSESTREAM) && defined(HAVE_SYS_STREAM_H)
59 #include <sys/stream.h>
60 #include <sys/stropts.h>
63 #include "ntp_syslog.h"
67 #define parseprintf(LEVEL, ARGS) if (debug > LEVEL) printf ARGS
69 #define parseprintf(LEVEL, ARGS)
71 #endif /* PARSESTREAM */
73 #if defined(timercmp) && defined(__GNUC__)
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))
84 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
90 #define PARSEB_POWERUP 0x00000001 /* no synchronisation */
91 #define PARSEB_NOSYNC 0x00000002 /* timecode currently not confirmed */
94 * time zone information
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 */
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 */
108 * optional status information
110 #define PARSEB_ALTERNATE 0x00001000 /* alternate antenna used */
111 #define PARSEB_POSITION 0x00002000 /* position available */
112 #define PARSEB_MESSAGE 0x00004000 /* addtitional message data */
114 * feature information
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) */
122 * time stamp availability
124 #define PARSEB_TIMECODE 0x10000000 /* valid time code sample */
125 #define PARSEB_PPS 0x20000000 /* valid PPS sample */
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)
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))
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)
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)
153 * operation flags - lower nibble contains fudge flags
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 */
163 #define PARSE_TCMAX 400 /* maximum addition data size */
165 typedef union timestamp
167 struct timeval tv; /* timeval - kernel view */
168 l_fp fp; /* fixed point - ntp view */
172 * standard time stamp structure
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 */
187 typedef struct parsetime parsetime_t;
189 /*---------- STREAMS interface ----------*/
191 #ifdef HAVE_SYS_STREAM_H
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')
204 /*------ IO handling flags (sorry) ------*/
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
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 */
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 */
235 u_long parse_cs; /* character size (needed for stripping) */
239 typedef union parsectl parsectl_t;
241 /*------ for conversion routines --------*/
243 struct parse /* parse module local data */
245 int parse_flags; /* operation and current status flags */
247 int parse_ioflags; /* io handling flags (5-8 Bit control currently) */
250 * private data - fixed format only
252 unsigned short parse_plen; /* length of private data */
253 void *parse_pdata; /* private data pointer */
256 * time code input buffer (from RS232 or PPS)
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 */
267 timestamp_t parse_lastchar; /* last time a character was received */
268 parsetime_t parse_dtime; /* external data prototype */
271 typedef struct parse parse_t;
273 struct clocktime /* clock time broken up from time code */
282 long utcoffset; /* in seconds */
283 time_t utctime; /* the actual time - alternative to date/time */
284 u_long flags; /* current clock status */
287 typedef struct clocktime clocktime_t;
290 * parser related return/error codes
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 */
303 * return codes used by special input parsers
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 */
314 #define SYNC_ZERO 0x00
315 #define SYNC_ONE 0x01
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 */
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 */
334 typedef struct clockformat clockformat_t;
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 *));
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 *));
353 extern int Strok P((const unsigned char *, const unsigned char *));
354 extern int Stoi P((const unsigned char *, long *, int));
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 *));
370 * Revision 4.5 1998/08/09 22:23:32 kardel
371 * 4.0.73e2 adjustments
373 * Revision 4.4 1998/06/14 21:09:27 kardel
376 * Revision 4.3 1998/06/13 11:49:25 kardel
377 * STREAM macro gone in favor of HAVE_SYS_STREAM_H
379 * Revision 4.2 1998/06/12 15:14:25 kardel
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
390 * form V3 3.31 - log info deleted 1998/04/11 kardel