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