/contrib/ntp/include/parse.h

https://bitbucket.org/freebsd/freebsd-head/ · C Header · 421 lines · 239 code · 52 blank · 130 comment · 16 complexity · 65a30b177be5f2edda37447c94c38a51 MD5 · raw file

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