2 * /src/NTP/ntp-4/libparse/parse.c,v 4.14 1999/11/28 09:13:52 kardel RELEASE_19991128_A
4 * parse.c,v 4.14 1999/11/28 09:13:52 kardel RELEASE_19991128_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) 1992-1998 by Frank Kardel
15 * Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 #if defined(REFCLOCK) && defined(CLOCK_PARSE)
29 #if !(defined(lint) || defined(__GNUC__))
30 static char rcsid[] = "parse.c,v 4.14 1999/11/28 09:13:52 kardel RELEASE_19991128_A";
34 #include "ntp_unixtime.h"
35 #include "ntp_calendar.h"
36 #include "ntp_stdlib.h"
37 #include "ntp_machine.h"
38 #include "ntp.h" /* (get Y2KFixes definitions) Y2KFixes */
45 # include "sys/parsestreams.h"
48 extern clockformat_t *clockformats[];
49 extern unsigned short nformats;
51 static u_long timepacket P((parse_t *));
54 * strings support usually not in kernel - duplicated, but what the heck
58 register const char *s
76 register const char *s,
77 register const char *t
82 if (!s || !t || (s == t))
87 while (!(c = *s++ - *t++) && *s && *t)
100 struct timeval delta;
103 delta.tv_sec = tstamp->tv.tv_sec - parseio->parse_lastchar.tv.tv_sec;
104 delta.tv_usec = tstamp->tv.tv_usec - parseio->parse_lastchar.tv.tv_usec;
105 if (delta.tv_usec < 0)
108 delta.tv_usec += 1000000;
111 extern long tstouslo[];
112 extern long tstousmid[];
113 extern long tstoushi[];
118 L_SUB(&delt, &parseio->parse_lastchar.fp);
119 TSTOTV(&delt, &delta);
122 if (timercmp(&delta, del, >))
124 parseprintf(DD_PARSE, ("parse: timedout: TRUE\n"));
129 parseprintf(DD_PARSE, ("parse: timedout: FALSE\n"));
137 register parse_t *parseio
140 parseprintf(DD_PARSE, ("parse_iostart\n"));
142 parseio->parse_plen = 0;
143 parseio->parse_pdata = (void *)0;
145 parseio->parse_data = 0;
146 parseio->parse_ldata = 0;
147 parseio->parse_dsize = 0;
149 parseio->parse_badformat = 0;
150 parseio->parse_ioflags = PARSE_IO_CS7; /* usual unix default */
151 parseio->parse_index = 0;
152 parseio->parse_ldsize = 0;
160 register parse_t *parseio
163 parseprintf(DD_PARSE, ("parse_ioend\n"));
165 if (parseio->parse_pdata)
166 FREE(parseio->parse_pdata, parseio->parse_plen);
168 if (parseio->parse_data)
169 FREE(parseio->parse_data, (unsigned)(parseio->parse_dsize * 2 + 2));
178 unsigned int updated = PARSE_INP_SKIP;
181 * re-start packet - timeout - overflow - start symbol
184 if (parseio->parse_index)
187 * filled buffer - thus not end character found
190 parseio->parse_data[parseio->parse_index] = '\0';
191 memcpy(parseio->parse_ldata, parseio->parse_data, (unsigned)(parseio->parse_index+1));
192 parseio->parse_ldsize = parseio->parse_index+1;
193 updated = PARSE_INP_TIME;
196 parseio->parse_index = 1;
197 parseio->parse_data[0] = ch;
198 parseprintf(DD_PARSE, ("parse: parse_restart: buffer start (updated = %x)\n", updated));
211 if (parseio->parse_index < parseio->parse_dsize)
214 * collect into buffer
216 parseprintf(DD_PARSE, ("parse: parse_addchar: buffer[%d] = 0x%x\n", parseio->parse_index, ch));
217 parseio->parse_data[parseio->parse_index++] = ch;
218 return PARSE_INP_SKIP;
222 * buffer overflow - attempt to make the best of it
224 return parse_restart(parseio, ch);
233 * message complete processing
235 parseio->parse_data[parseio->parse_index] = '\0';
236 memcpy(parseio->parse_ldata, parseio->parse_data, (unsigned)(parseio->parse_index+1));
237 parseio->parse_ldsize = parseio->parse_index+1;
238 parseio->parse_index = 0;
239 parseprintf(DD_PARSE, ("parse: parse_end: buffer end\n"));
240 return PARSE_INP_TIME;
246 register parse_t *parseio,
247 register unsigned int ch,
248 register timestamp_t *tstamp
251 register unsigned updated = CVT_NONE;
253 * within STREAMS CSx (x < 8) chars still have the upper bits set
254 * so we normalize the characters by masking unecessary bits off.
256 switch (parseio->parse_ioflags & PARSE_IO_CSIZE)
275 parseprintf(DD_PARSE, ("parse_ioread(0x%lx, char=0x%x, ..., ...)\n", (unsigned long)parseio, ch & 0xFF));
277 if (!clockformats[parseio->parse_lformat]->convert)
279 parseprintf(DD_PARSE, ("parse_ioread: input dropped.\n"));
283 if (clockformats[parseio->parse_lformat]->input)
285 unsigned long input_status;
287 input_status = clockformats[parseio->parse_lformat]->input(parseio, ch, tstamp);
289 if (input_status & PARSE_INP_SYNTH)
294 if (input_status & PARSE_INP_TIME) /* time sample is available */
296 updated = timepacket(parseio);
299 if (input_status & PARSE_INP_DATA) /* got additional data */
301 updated |= CVT_ADDITIONAL;
307 * remember last character time
309 parseio->parse_lastchar = *tstamp;
312 if ((updated & CVT_MASK) != CVT_NONE)
314 parseprintf(DD_PARSE, ("parse_ioread: time sample accumulated (status=0x%x)\n", updated));
318 parseio->parse_dtime.parse_status = updated;
320 return (((updated & CVT_MASK) != CVT_NONE) ||
321 ((updated & CVT_ADDITIONAL) != 0));
327 * take status line indication and derive synchronisation information
329 * It can also be used to decode a serial serial data format (such as the
330 * ONE, ZERO, MINUTE sync data stream from DCF77)
335 register parse_t *parseio,
337 register timestamp_t *ptime
340 register unsigned updated = CVT_NONE;
343 * PPS pulse information will only be delivered to ONE clock format
344 * this is either the last successful conversion module with a ppssync
345 * routine, or a fixed format with a ppssync routine
347 parseprintf(DD_PARSE, ("parse_iopps: STATUS %s\n", (status == SYNC_ONE) ? "ONE" : "ZERO"));
349 if (clockformats[parseio->parse_lformat]->syncpps)
351 updated = clockformats[parseio->parse_lformat]->syncpps(parseio, status == SYNC_ONE, ptime);
352 parseprintf(DD_PARSE, ("parse_iopps: updated = 0x%x\n", updated));
355 return (updated & CVT_MASK) != CVT_NONE;
361 * clean up internal status for new round
366 register parse_t *parseio
370 * we need to clean up certain flags for the next round
372 parseprintf(DD_PARSE, ("parse_iodone: DONE\n"));
373 parseio->parse_dtime.parse_state = 0; /* no problems with ISRs */
376 /*---------- conversion implementation --------------------*/
379 * convert a struct clock to UTC since Jan, 1st 1970 0:00 (the UNIX EPOCH)
381 #define days_per_year(x) ((x) % 4 ? 365 : ((x % 400) ? ((x % 100) ? 366 : 365) : 366))
385 register clocktime_t *clock_time,
386 register u_long *cvtrtc
389 #define SETRTC(_X_) { if (cvtrtc) *cvtrtc = (_X_); }
390 static int days_of_month[] =
392 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
397 if (clock_time->utctime)
398 return clock_time->utctime; /* if the conversion routine gets it right away - why not */
400 if ( clock_time->year < YEAR_PIVOT ) /* Y2KFixes [ */
401 clock_time->year += 100; /* convert 20xx%100 to 20xx-1900 */
402 if ( clock_time->year < YEAR_BREAK ) /* expand to full four-digits */
403 clock_time->year += 1900;
405 if (clock_time->year < 1970 ) /* Y2KFixes ] */
407 SETRTC(CVT_FAIL|CVT_BADDATE);
412 * sorry, slow section here - but it's not time critical anyway
414 t = julian0(clock_time->year) - julian0(1970); /* Y2kFixes */
416 if (clock_time->month <= 0 || clock_time->month > 12)
418 SETRTC(CVT_FAIL|CVT_BADDATE);
419 return -1; /* bad month */
423 /* adjust leap year */
424 if (clock_time->month < 3 && days_per_year(clock_time->year) == 366)
426 #else /* Y2KFixes [ */
427 if ( clock_time->month >= 3 && isleap_4(clock_time->year) )
428 t++; /* add one more if within leap year */
429 #endif /* Y2KFixes ] */
431 for (i = 1; i < clock_time->month; i++)
433 t += days_of_month[i];
436 if (clock_time->day < 1 || ((clock_time->month == 2 && days_per_year(clock_time->year) == 366) ?
437 clock_time->day > 29 : clock_time->day > days_of_month[clock_time->month]))
439 SETRTC(CVT_FAIL|CVT_BADDATE);
440 return -1; /* bad day */
443 t += clock_time->day - 1;
445 if (clock_time->hour < 0 || clock_time->hour >= 24)
447 SETRTC(CVT_FAIL|CVT_BADTIME);
448 return -1; /* bad hour */
451 t = TIMES24(t) + clock_time->hour;
454 if (clock_time->minute < 0 || clock_time->minute > 59)
456 SETRTC(CVT_FAIL|CVT_BADTIME);
457 return -1; /* bad min */
460 t = TIMES60(t) + clock_time->minute;
463 if (clock_time->second < 0 || clock_time->second > 60) /* allow for LEAPs */
465 SETRTC(CVT_FAIL|CVT_BADTIME);
466 return -1; /* bad sec */
469 t = TIMES60(t) + clock_time->second;
471 t += clock_time->utcoffset; /* warp to UTC */
475 clock_time->utctime = t; /* documentray only */
480 /*--------------- format conversion -----------------------------------*/
484 const unsigned char *s,
489 char unsigned const *b = s;
510 if (c == '\0' || c < '0' || c > '9' || (cnt && ((s-b) > cnt)))
521 z = (z << 3) + (z << 1) + ( c - '0' );
528 const unsigned char *s,
529 const unsigned char *m
537 if ((*m == ' ') ? 1 : (*s == *m))
552 register parse_t *parseio,
553 register u_long flags
561 parseio->parse_lstate = parseio->parse_dtime.parse_state | flags | PARSEB_TIMECODE;
563 parseio->parse_dtime.parse_state = parseio->parse_lstate;
566 (void)splx((unsigned int)s);
572 parseprintf(DD_PARSE, ("updatetimeinfo status=0x%x, time=%x\n", parseio->parse_dtime.parse_state,
573 parseio->parse_dtime.parse_time.tv.tv_sec));
575 parseprintf(DD_PARSE, ("updatetimeinfo status=0x%lx, time=%x\n", (long)parseio->parse_dtime.parse_state,
576 parseio->parse_dtime.parse_time.fp.l_ui));
579 return CVT_OK; /* everything fine and dandy... */
586 * handle a sync time stamp
591 register parse_t *parseio,
592 register timestamp_t *ts,
593 register struct format *format,
597 parseio->parse_dtime.parse_stime = *ts;
603 * handle a pps time stamp
608 register parse_t *parseio,
610 register timestamp_t *ptime
613 parseio->parse_dtime.parse_ptime = *ptime;
614 parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
622 * handle a pps time stamp in ONE edge
627 register parse_t *parseio,
629 register timestamp_t *ptime
633 return pps_simple(parseio, status, ptime);
641 * handle a pps time stamp in ZERO edge
646 register parse_t *parseio,
648 register timestamp_t *ptime
652 return pps_simple(parseio, status, ptime);
660 * process a data packet
664 register parse_t *parseio
667 register unsigned short format;
669 u_long cvtrtc; /* current conversion result */
670 clocktime_t clock_time;
672 memset((char *)&clock_time, 0, sizeof clock_time);
673 format = parseio->parse_lformat;
675 if (format == (unsigned short)~0)
678 switch ((cvtrtc = clockformats[format]->convert ?
679 clockformats[format]->convert((unsigned char *)parseio->parse_ldata, parseio->parse_ldsize, (struct format *)(clockformats[format]->data), &clock_time, parseio->parse_pdata) :
680 CVT_NONE) & CVT_MASK)
683 parseio->parse_badformat++;
688 * too bad - pretend bad format
690 parseio->parse_badformat++;
700 /* shouldn't happen */
702 msyslog(LOG_WARNING, "parse: INTERNAL error: bad return code of convert routine \"%s\"\n", clockformats[format]->name);
704 return CVT_FAIL|cvtrtc;
707 if ((t = parse_to_unixtime(&clock_time, &cvtrtc)) == -1)
709 return CVT_FAIL|cvtrtc;
716 parseio->parse_dtime.parse_time.tv.tv_sec = t;
717 parseio->parse_dtime.parse_time.tv.tv_usec = clock_time.usecond;
719 parseio->parse_dtime.parse_time.fp.l_ui = t + JAN_1970;
720 TVUTOTSF(clock_time.usecond, parseio->parse_dtime.parse_time.fp.l_uf);
723 parseio->parse_dtime.parse_format = format;
725 return updatetimeinfo(parseio, clock_time.flags);
735 dct->parsegettc.parse_state = parse->parse_lstate;
736 dct->parsegettc.parse_format = parse->parse_lformat;
738 * move out current bad packet count
739 * user program is expected to sum these up
740 * this is not a problem, as "parse" module are
741 * exclusive open only
743 dct->parsegettc.parse_badformat = parse->parse_badformat;
744 parse->parse_badformat = 0;
746 if (parse->parse_ldsize <= PARSE_TCMAX)
748 dct->parsegettc.parse_count = parse->parse_ldsize;
749 memcpy(dct->parsegettc.parse_buffer, parse->parse_ldata, dct->parsegettc.parse_count);
766 if (dct->parseformat.parse_count <= PARSE_TCMAX)
768 if (dct->parseformat.parse_count)
770 register unsigned short i;
772 for (i = 0; i < nformats; i++)
774 if (!Strcmp(dct->parseformat.parse_buffer, clockformats[i]->name))
776 if (parse->parse_pdata)
777 FREE(parse->parse_pdata, parse->parse_plen);
778 parse->parse_pdata = 0;
780 parse->parse_plen = clockformats[i]->plen;
782 if (parse->parse_plen)
784 parse->parse_pdata = MALLOC(parse->parse_plen);
785 if (!parse->parse_pdata)
787 parseprintf(DD_PARSE, ("set format failed: malloc for private data area failed\n"));
790 memset((char *)parse->parse_pdata, 0, parse->parse_plen);
793 if (parse->parse_data)
794 FREE(parse->parse_data, (unsigned)(parse->parse_dsize * 2 + 2));
795 parse->parse_ldata = parse->parse_data = 0;
797 parse->parse_dsize = clockformats[i]->length;
799 if (parse->parse_dsize)
801 parse->parse_data = (char*)MALLOC((unsigned)(parse->parse_dsize * 2 + 2));
802 if (!parse->parse_data)
804 if (parse->parse_pdata)
805 FREE(parse->parse_pdata, parse->parse_plen);
806 parse->parse_pdata = 0;
808 parseprintf(DD_PARSE, ("init failed: malloc for data area failed\n"));
815 * leave room for '\0'
817 parse->parse_ldata = parse->parse_data + parse->parse_dsize + 1;
819 parse->parse_lformat = i;
836 if (dct->parseformat.parse_format < nformats &&
837 Strlen(clockformats[dct->parseformat.parse_format]->name) <= PARSE_TCMAX)
839 dct->parseformat.parse_count = Strlen(clockformats[dct->parseformat.parse_format]->name)+1;
840 memcpy(dct->parseformat.parse_buffer, clockformats[dct->parseformat.parse_format]->name, dct->parseformat.parse_count);
856 parse->parse_ioflags &= ~PARSE_IO_CSIZE;
857 parse->parse_ioflags |= dct->parsesetcs.parse_cs & PARSE_IO_CSIZE;
861 #else /* not (REFCLOCK && CLOCK_PARSE) */
863 #endif /* not (REFCLOCK && CLOCK_PARSE) */
869 * Revision 4.14 1999/11/28 09:13:52 kardel
872 * Revision 4.13 1999/02/28 11:50:20 kardel
873 * (timepacket): removed unecessary code
875 * Revision 4.12 1999/02/21 12:17:44 kardel
876 * 4.91f reconcilation
878 * Revision 4.11 1999/02/21 11:09:47 kardel
879 * unified debug output
881 * Revision 4.10 1998/12/20 23:45:30 kardel
882 * fix types and warnings
884 * Revision 4.9 1998/08/09 22:26:06 kardel
885 * Trimble TSIP support
887 * Revision 4.8 1998/06/14 21:09:39 kardel
890 * Revision 4.7 1998/06/13 15:19:13 kardel
891 * fix mem*() to b*() function macro emulation
893 * Revision 4.6 1998/06/13 13:24:13 kardel
896 * Revision 4.5 1998/06/13 13:01:10 kardel
899 * Revision 4.4 1998/06/13 12:12:10 kardel
900 * bcopy/memcpy cleanup
901 * fix SVSV name clash
903 * Revision 4.3 1998/06/12 15:22:30 kardel
906 * Revision 4.2 1998/06/12 09:13:27 kardel
907 * conditional compile macros fixed
910 * Revision 4.1 1998/05/24 09:39:55 kardel
911 * implementation of the new IO handling model
913 * Revision 4.0 1998/04/10 19:45:36 kardel
914 * Start 4.0 release version numbering
916 * from V3 3.46 log info deleted 1998/04/11 kardel