2 * /src/NTP/ntp4-dev/libparse/parse.c,v 4.20 2005/08/06 17:39:40 kardel RELEASE_20050806_A
4 * parse.c,v 4.20 2005/08/06 17:39:40 kardel RELEASE_20050806_A
6 * Parser module for reference clock
8 * PARSEKERNEL define switches between two personalities of the module
9 * if PARSEKERNEL is defined this module can be used
10 * as kernel module. In this case the time stamps will be
12 * when PARSEKERNEL is not defined NTP time stamps will be used.
14 * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
15 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the author nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 #if defined(REFCLOCK) && defined(CLOCK_PARSE)
49 #if !(defined(lint) || defined(__GNUC__))
50 static char rcsid[] = "parse.c,v 4.20 2005/08/06 17:39:40 kardel RELEASE_20050806_A";
54 #include "ntp_unixtime.h"
55 #include "ntp_calendar.h"
56 #include "ntp_stdlib.h"
57 #include "ntp_machine.h"
58 #include "ntp.h" /* (get Y2KFixes definitions) Y2KFixes */
65 # include "sys/parsestreams.h"
68 extern clockformat_t *clockformats[];
69 extern unsigned short nformats;
71 static u_long timepacket P((parse_t *));
74 * strings support usually not in kernel - duplicated, but what the heck
78 register const char *s
96 register const char *s,
97 register const char *t
102 if (!s || !t || (s == t))
107 while (!(c = *s++ - *t++) && *s && *t)
120 struct timeval delta;
123 delta.tv_sec = tstamp->tv.tv_sec - parseio->parse_lastchar.tv.tv_sec;
124 delta.tv_usec = tstamp->tv.tv_usec - parseio->parse_lastchar.tv.tv_usec;
125 if (delta.tv_usec < 0)
128 delta.tv_usec += 1000000;
131 extern long tstouslo[];
132 extern long tstousmid[];
133 extern long tstoushi[];
138 L_SUB(&delt, &parseio->parse_lastchar.fp);
139 TSTOTV(&delt, &delta);
142 if (timercmp(&delta, del, >))
144 parseprintf(DD_PARSE, ("parse: timedout: TRUE\n"));
149 parseprintf(DD_PARSE, ("parse: timedout: FALSE\n"));
157 register parse_t *parseio
160 parseprintf(DD_PARSE, ("parse_iostart\n"));
162 parseio->parse_plen = 0;
163 parseio->parse_pdata = (void *)0;
165 parseio->parse_data = 0;
166 parseio->parse_ldata = 0;
167 parseio->parse_dsize = 0;
169 parseio->parse_badformat = 0;
170 parseio->parse_ioflags = PARSE_IO_CS7; /* usual unix default */
171 parseio->parse_index = 0;
172 parseio->parse_ldsize = 0;
180 register parse_t *parseio
183 parseprintf(DD_PARSE, ("parse_ioend\n"));
185 if (parseio->parse_pdata)
186 FREE(parseio->parse_pdata, parseio->parse_plen);
188 if (parseio->parse_data)
189 FREE(parseio->parse_data, (unsigned)(parseio->parse_dsize * 2 + 2));
198 unsigned int updated = PARSE_INP_SKIP;
201 * re-start packet - timeout - overflow - start symbol
204 if (parseio->parse_index)
207 * filled buffer - thus not end character found
210 parseio->parse_data[parseio->parse_index] = '\0';
211 memcpy(parseio->parse_ldata, parseio->parse_data, (unsigned)(parseio->parse_index+1));
212 parseio->parse_ldsize = parseio->parse_index;
213 updated = PARSE_INP_TIME;
216 parseio->parse_index = 1;
217 parseio->parse_data[0] = ch;
218 parseprintf(DD_PARSE, ("parse: parse_restart: buffer start (updated = %x)\n", updated));
231 if (parseio->parse_index < parseio->parse_dsize)
234 * collect into buffer
236 parseprintf(DD_PARSE, ("parse: parse_addchar: buffer[%d] = 0x%x\n", parseio->parse_index, ch));
237 parseio->parse_data[parseio->parse_index++] = ch;
238 return PARSE_INP_SKIP;
242 * buffer overflow - attempt to make the best of it
244 return parse_restart(parseio, ch);
253 * message complete processing
255 parseio->parse_data[parseio->parse_index] = '\0';
256 memcpy(parseio->parse_ldata, parseio->parse_data, (unsigned)(parseio->parse_index+1));
257 parseio->parse_ldsize = parseio->parse_index;
258 parseio->parse_index = 0;
259 parseprintf(DD_PARSE, ("parse: parse_end: buffer end\n"));
260 return PARSE_INP_TIME;
266 register parse_t *parseio,
267 register unsigned int ch,
268 register timestamp_t *tstamp
271 register unsigned updated = CVT_NONE;
273 * within STREAMS CSx (x < 8) chars still have the upper bits set
274 * so we normalize the characters by masking unecessary bits off.
276 switch (parseio->parse_ioflags & PARSE_IO_CSIZE)
295 parseprintf(DD_PARSE, ("parse_ioread(0x%lx, char=0x%x, ..., ...)\n", (unsigned long)parseio, ch & 0xFF));
297 if (!clockformats[parseio->parse_lformat]->convert)
299 parseprintf(DD_PARSE, ("parse_ioread: input dropped.\n"));
303 if (clockformats[parseio->parse_lformat]->input)
305 unsigned long input_status;
307 input_status = clockformats[parseio->parse_lformat]->input(parseio, ch, tstamp);
309 if (input_status & PARSE_INP_SYNTH)
314 if (input_status & PARSE_INP_TIME) /* time sample is available */
316 updated = timepacket(parseio);
319 if (input_status & PARSE_INP_DATA) /* got additional data */
321 updated |= CVT_ADDITIONAL;
327 * remember last character time
329 parseio->parse_lastchar = *tstamp;
332 if ((updated & CVT_MASK) != CVT_NONE)
334 parseprintf(DD_PARSE, ("parse_ioread: time sample accumulated (status=0x%x)\n", updated));
338 parseio->parse_dtime.parse_status = updated;
340 return (((updated & CVT_MASK) != CVT_NONE) ||
341 ((updated & CVT_ADDITIONAL) != 0));
347 * take status line indication and derive synchronisation information
349 * It can also be used to decode a serial serial data format (such as the
350 * ONE, ZERO, MINUTE sync data stream from DCF77)
355 register parse_t *parseio,
357 register timestamp_t *ptime
360 register unsigned updated = CVT_NONE;
363 * PPS pulse information will only be delivered to ONE clock format
364 * this is either the last successful conversion module with a ppssync
365 * routine, or a fixed format with a ppssync routine
367 parseprintf(DD_PARSE, ("parse_iopps: STATUS %s\n", (status == SYNC_ONE) ? "ONE" : "ZERO"));
369 if (clockformats[parseio->parse_lformat]->syncpps)
371 updated = clockformats[parseio->parse_lformat]->syncpps(parseio, status == SYNC_ONE, ptime);
372 parseprintf(DD_PARSE, ("parse_iopps: updated = 0x%x\n", updated));
375 return (updated & CVT_MASK) != CVT_NONE;
381 * clean up internal status for new round
386 register parse_t *parseio
390 * we need to clean up certain flags for the next round
392 parseprintf(DD_PARSE, ("parse_iodone: DONE\n"));
393 parseio->parse_dtime.parse_state = 0; /* no problems with ISRs */
396 /*---------- conversion implementation --------------------*/
399 * convert a struct clock to UTC since Jan, 1st 1970 0:00 (the UNIX EPOCH)
401 #define days_per_year(x) ((x) % 4 ? 365 : ((x % 400) ? ((x % 100) ? 366 : 365) : 366))
405 register clocktime_t *clock_time,
406 register u_long *cvtrtc
409 #define SETRTC(_X_) { if (cvtrtc) *cvtrtc = (_X_); }
410 static int days_of_month[] =
412 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
417 if (clock_time->utctime)
418 return clock_time->utctime; /* if the conversion routine gets it right away - why not */
420 if ( clock_time->year < YEAR_PIVOT ) /* Y2KFixes [ */
421 clock_time->year += 100; /* convert 20xx%100 to 20xx-1900 */
422 if ( clock_time->year < YEAR_BREAK ) /* expand to full four-digits */
423 clock_time->year += 1900;
425 if (clock_time->year < 1970 ) /* Y2KFixes ] */
427 SETRTC(CVT_FAIL|CVT_BADDATE);
432 * sorry, slow section here - but it's not time critical anyway
434 t = julian0(clock_time->year) - julian0(1970); /* Y2kFixes */
436 if (clock_time->month <= 0 || clock_time->month > 12)
438 SETRTC(CVT_FAIL|CVT_BADDATE);
439 return -1; /* bad month */
443 /* adjust leap year */
444 if (clock_time->month < 3 && days_per_year(clock_time->year) == 366)
446 #else /* Y2KFixes [ */
447 if ( clock_time->month >= 3 && isleap_4(clock_time->year) )
448 t++; /* add one more if within leap year */
449 #endif /* Y2KFixes ] */
451 for (i = 1; i < clock_time->month; i++)
453 t += days_of_month[i];
456 if (clock_time->day < 1 || ((clock_time->month == 2 && days_per_year(clock_time->year) == 366) ?
457 clock_time->day > 29 : clock_time->day > days_of_month[clock_time->month]))
459 SETRTC(CVT_FAIL|CVT_BADDATE);
460 return -1; /* bad day */
463 t += clock_time->day - 1;
465 if (clock_time->hour < 0 || clock_time->hour >= 24)
467 SETRTC(CVT_FAIL|CVT_BADTIME);
468 return -1; /* bad hour */
471 t = TIMES24(t) + clock_time->hour;
474 if (clock_time->minute < 0 || clock_time->minute > 59)
476 SETRTC(CVT_FAIL|CVT_BADTIME);
477 return -1; /* bad min */
480 t = TIMES60(t) + clock_time->minute;
483 if (clock_time->second < 0 || clock_time->second > 60) /* allow for LEAPs */
485 SETRTC(CVT_FAIL|CVT_BADTIME);
486 return -1; /* bad sec */
489 t = TIMES60(t) + clock_time->second;
491 t += clock_time->utcoffset; /* warp to UTC */
495 clock_time->utctime = t; /* documentray only */
500 /*--------------- format conversion -----------------------------------*/
504 const unsigned char *s,
509 char unsigned const *b = s;
530 if (c == '\0' || c < '0' || c > '9' || (cnt && ((s-b) > cnt)))
541 z = (z << 3) + (z << 1) + ( c - '0' );
548 const unsigned char *s,
549 const unsigned char *m
557 if ((*m == ' ') ? 1 : (*s == *m))
572 register parse_t *parseio,
573 register u_long flags
581 parseio->parse_lstate = parseio->parse_dtime.parse_state | flags | PARSEB_TIMECODE;
583 parseio->parse_dtime.parse_state = parseio->parse_lstate;
586 (void)splx((unsigned int)s);
592 parseprintf(DD_PARSE, ("updatetimeinfo status=0x%x, time=%x\n", parseio->parse_dtime.parse_state,
593 parseio->parse_dtime.parse_time.tv.tv_sec));
595 parseprintf(DD_PARSE, ("updatetimeinfo status=0x%lx, time=%x\n", (long)parseio->parse_dtime.parse_state,
596 parseio->parse_dtime.parse_time.fp.l_ui));
599 return CVT_OK; /* everything fine and dandy... */
606 * handle a sync time stamp
611 register parse_t *parseio,
612 register timestamp_t *ts,
613 register struct format *format,
617 parseio->parse_dtime.parse_stime = *ts;
623 * handle a pps time stamp
628 register parse_t *parseio,
630 register timestamp_t *ptime
633 parseio->parse_dtime.parse_ptime = *ptime;
634 parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
642 * handle a pps time stamp in ONE edge
647 register parse_t *parseio,
649 register timestamp_t *ptime
653 return pps_simple(parseio, status, ptime);
661 * handle a pps time stamp in ZERO edge
666 register parse_t *parseio,
668 register timestamp_t *ptime
672 return pps_simple(parseio, status, ptime);
680 * process a data packet
684 register parse_t *parseio
687 register unsigned short format;
689 u_long cvtrtc; /* current conversion result */
690 clocktime_t clock_time;
692 memset((char *)&clock_time, 0, sizeof clock_time);
693 format = parseio->parse_lformat;
695 if (format == (unsigned short)~0)
698 switch ((cvtrtc = clockformats[format]->convert ?
699 clockformats[format]->convert((unsigned char *)parseio->parse_ldata, parseio->parse_ldsize, (struct format *)(clockformats[format]->data), &clock_time, parseio->parse_pdata) :
700 CVT_NONE) & CVT_MASK)
703 parseio->parse_badformat++;
708 * too bad - pretend bad format
710 parseio->parse_badformat++;
720 /* shouldn't happen */
722 msyslog(LOG_WARNING, "parse: INTERNAL error: bad return code of convert routine \"%s\"\n", clockformats[format]->name);
724 return CVT_FAIL|cvtrtc;
727 if ((t = parse_to_unixtime(&clock_time, &cvtrtc)) == -1)
729 return CVT_FAIL|cvtrtc;
736 parseio->parse_dtime.parse_time.tv.tv_sec = t;
737 parseio->parse_dtime.parse_time.tv.tv_usec = clock_time.usecond;
739 parseio->parse_dtime.parse_time.fp.l_ui = t + JAN_1970;
740 TVUTOTSF(clock_time.usecond, parseio->parse_dtime.parse_time.fp.l_uf);
743 parseio->parse_dtime.parse_format = format;
745 return updatetimeinfo(parseio, clock_time.flags);
755 dct->parsegettc.parse_state = parse->parse_lstate;
756 dct->parsegettc.parse_format = parse->parse_lformat;
758 * move out current bad packet count
759 * user program is expected to sum these up
760 * this is not a problem, as "parse" module are
761 * exclusive open only
763 dct->parsegettc.parse_badformat = parse->parse_badformat;
764 parse->parse_badformat = 0;
766 if (parse->parse_ldsize <= PARSE_TCMAX)
768 dct->parsegettc.parse_count = parse->parse_ldsize;
769 memcpy(dct->parsegettc.parse_buffer, parse->parse_ldata, dct->parsegettc.parse_count);
786 if (dct->parseformat.parse_count <= PARSE_TCMAX)
788 if (dct->parseformat.parse_count)
790 register unsigned short i;
792 for (i = 0; i < nformats; i++)
794 if (!Strcmp(dct->parseformat.parse_buffer, clockformats[i]->name))
796 if (parse->parse_pdata)
797 FREE(parse->parse_pdata, parse->parse_plen);
798 parse->parse_pdata = 0;
800 parse->parse_plen = clockformats[i]->plen;
802 if (parse->parse_plen)
804 parse->parse_pdata = MALLOC(parse->parse_plen);
805 if (!parse->parse_pdata)
807 parseprintf(DD_PARSE, ("set format failed: malloc for private data area failed\n"));
810 memset((char *)parse->parse_pdata, 0, parse->parse_plen);
813 if (parse->parse_data)
814 FREE(parse->parse_data, (unsigned)(parse->parse_dsize * 2 + 2));
815 parse->parse_ldata = parse->parse_data = 0;
817 parse->parse_dsize = clockformats[i]->length;
819 if (parse->parse_dsize)
821 parse->parse_data = (char*)MALLOC((unsigned)(parse->parse_dsize * 2 + 2));
822 if (!parse->parse_data)
824 if (parse->parse_pdata)
825 FREE(parse->parse_pdata, parse->parse_plen);
826 parse->parse_pdata = 0;
828 parseprintf(DD_PARSE, ("init failed: malloc for data area failed\n"));
835 * leave room for '\0'
837 parse->parse_ldata = parse->parse_data + parse->parse_dsize + 1;
839 parse->parse_lformat = i;
856 if (dct->parseformat.parse_format < nformats &&
857 Strlen(clockformats[dct->parseformat.parse_format]->name) <= PARSE_TCMAX)
859 dct->parseformat.parse_count = Strlen(clockformats[dct->parseformat.parse_format]->name)+1;
860 memcpy(dct->parseformat.parse_buffer, clockformats[dct->parseformat.parse_format]->name, dct->parseformat.parse_count);
876 parse->parse_ioflags &= ~PARSE_IO_CSIZE;
877 parse->parse_ioflags |= dct->parsesetcs.parse_cs & PARSE_IO_CSIZE;
881 #else /* not (REFCLOCK && CLOCK_PARSE) */
883 #endif /* not (REFCLOCK && CLOCK_PARSE) */
889 * Revision 4.20 2005/08/06 17:39:40 kardel
890 * cleanup size handling wrt/ to buffer boundaries
892 * Revision 4.19 2005/04/16 17:32:10 kardel
895 * Revision 4.18 2004/11/14 16:11:05 kardel
898 * Revision 4.17 2004/11/14 15:29:41 kardel
899 * support PPSAPI, upgrade Copyright to Berkeley style
901 * Revision 4.14 1999/11/28 09:13:52 kardel
904 * Revision 4.13 1999/02/28 11:50:20 kardel
905 * (timepacket): removed unecessary code
907 * Revision 4.12 1999/02/21 12:17:44 kardel
908 * 4.91f reconcilation
910 * Revision 4.11 1999/02/21 11:09:47 kardel
911 * unified debug output
913 * Revision 4.10 1998/12/20 23:45:30 kardel
914 * fix types and warnings
916 * Revision 4.9 1998/08/09 22:26:06 kardel
917 * Trimble TSIP support
919 * Revision 4.8 1998/06/14 21:09:39 kardel
922 * Revision 4.7 1998/06/13 15:19:13 kardel
923 * fix mem*() to b*() function macro emulation
925 * Revision 4.6 1998/06/13 13:24:13 kardel
928 * Revision 4.5 1998/06/13 13:01:10 kardel
931 * Revision 4.4 1998/06/13 12:12:10 kardel
932 * bcopy/memcpy cleanup
933 * fix SVSV name clash
935 * Revision 4.3 1998/06/12 15:22:30 kardel
938 * Revision 4.2 1998/06/12 09:13:27 kardel
939 * conditional compile macros fixed
942 * Revision 4.1 1998/05/24 09:39:55 kardel
943 * implementation of the new IO handling model
945 * Revision 4.0 1998/04/10 19:45:36 kardel
946 * Start 4.0 release version numbering
948 * from V3 3.46 log info deleted 1998/04/11 kardel