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