xref: /netbsd-src/external/bsd/ntp/dist/libparse/clk_rawdcf.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: clk_rawdcf.c,v 1.8 2020/05/25 20:47:25 christos Exp $	*/
2 
3 /*
4  * /src/NTP/REPOSITORY/ntp4-dev/libparse/clk_rawdcf.c,v 4.18 2006/06/22 18:40:01 kardel RELEASE_20060622_A
5  *
6  * clk_rawdcf.c,v 4.18 2006/06/22 18:40:01 kardel RELEASE_20060622_A
7  *
8  * Raw DCF77 pulse clock support
9  *
10  * Copyright (c) 1995-2015 by Frank Kardel <kardel <AT> ntp.org>
11  * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the author nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  */
38 
39 #ifdef HAVE_CONFIG_H
40 # include <config.h>
41 #endif
42 
43 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_RAWDCF)
44 
45 #include "ntp_fp.h"
46 #include "timevalops.h"
47 #include "ntp_unixtime.h"
48 #include "ntp_calendar.h"
49 
50 #include "parse.h"
51 #ifdef PARSESTREAM
52 # include <sys/parsestreams.h>
53 #endif
54 
55 #ifndef PARSEKERNEL
56 # include "ntp_stdlib.h"
57 #endif
58 
59 /*
60  * DCF77 raw time code
61  *
62  * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
63  * und Berlin, Maerz 1989
64  *
65  * Timecode transmission:
66  * AM:
67  *	time marks are send every second except for the second before the
68  *	next minute mark
69  *	time marks consist of a reduction of transmitter power to 25%
70  *	of the nominal level
71  *	the falling edge is the time indication (on time)
72  *	time marks of a 100ms duration constitute a logical 0
73  *	time marks of a 200ms duration constitute a logical 1
74  * FM:
75  *	see the spec. (basically a (non-)inverted psuedo random phase shift)
76  *
77  * Encoding:
78  * Second	Contents
79  * 0  - 10	AM: free, FM: 0
80  * 11 - 14	free
81  * 15		R     - "call bit" used to signalize irregularities in the control facilities
82  *		        (until 2003 indicated transmission via alternate antenna)
83  * 16		A1    - expect zone change (1 hour before)
84  * 17 - 18	Z1,Z2 - time zone
85  *		 0  0 illegal
86  *		 0  1 MEZ  (MET)
87  *		 1  0 MESZ (MED, MET DST)
88  *		 1  1 illegal
89  * 19		A2    - expect leap insertion/deletion (1 hour before)
90  * 20		S     - start of time code (1)
91  * 21 - 24	M1    - BCD (lsb first) Minutes
92  * 25 - 27	M10   - BCD (lsb first) 10 Minutes
93  * 28		P1    - Minute Parity (even)
94  * 29 - 32	H1    - BCD (lsb first) Hours
95  * 33 - 34      H10   - BCD (lsb first) 10 Hours
96  * 35		P2    - Hour Parity (even)
97  * 36 - 39	D1    - BCD (lsb first) Days
98  * 40 - 41	D10   - BCD (lsb first) 10 Days
99  * 42 - 44	DW    - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
100  * 45 - 49	MO    - BCD (lsb first) Month
101  * 50           MO0   - 10 Months
102  * 51 - 53	Y1    - BCD (lsb first) Years
103  * 54 - 57	Y10   - BCD (lsb first) 10 Years
104  * 58 		P3    - Date Parity (even)
105  * 59		      - usually missing (minute indication), except for leap insertion
106  */
107 
108 static parse_pps_fnc_t pps_rawdcf;
109 static parse_cvt_fnc_t cvt_rawdcf;
110 static parse_inp_fnc_t inp_rawdcf;
111 
112 typedef struct last_tcode {
113 	time_t      tcode;	/* last converted time code */
114         timestamp_t tminute;	/* sample time for minute start */
115         timestamp_t timeout;	/* last timeout timestamp */
116 } last_tcode_t;
117 
118 #define BUFFER_MAX	61
119 
120 clockformat_t clock_rawdcf =
121 {
122   inp_rawdcf,			/* DCF77 input handling */
123   cvt_rawdcf,			/* raw dcf input conversion */
124   pps_rawdcf,			/* examining PPS information */
125   0,				/* no private configuration data */
126   "RAW DCF77 Timecode",		/* direct decoding / time synthesis */
127 
128   BUFFER_MAX,			/* bit buffer */
129   sizeof(last_tcode_t)
130 };
131 
132 static struct dcfparam
133 {
134 	const unsigned char *onebits;
135 	const unsigned char *zerobits;
136 } dcfparameter =
137 {
138 	(const unsigned char *)"###############RADMLS1248124P124812P1248121241248112481248P??", /* 'ONE' representation */
139 	(const unsigned char *)"--------------------s-------p------p----------------------p__"  /* 'ZERO' representation */
140 };
141 
142 static struct rawdcfcode
143 {
144 	char offset;			/* start bit */
145 } rawdcfcode[] =
146 {
147 	{  0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
148 	{ 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
149 };
150 
151 #define DCF_M	0
152 #define DCF_R	1
153 #define DCF_A1	2
154 #define DCF_Z	3
155 #define DCF_A2	4
156 #define DCF_S	5
157 #define DCF_M1	6
158 #define DCF_M10	7
159 #define DCF_P1	8
160 #define DCF_H1	9
161 #define DCF_H10	10
162 #define DCF_P2	11
163 #define DCF_D1	12
164 #define DCF_D10	13
165 #define DCF_DW	14
166 #define DCF_MO	15
167 #define DCF_MO0	16
168 #define DCF_Y1	17
169 #define DCF_Y10	18
170 #define DCF_P3	19
171 
172 static struct partab
173 {
174 	char offset;			/* start bit of parity field */
175 } partab[] =
176 {
177 	{ 21 }, { 29 }, { 36 }, { 59 }
178 };
179 
180 #define DCF_P_P1	0
181 #define DCF_P_P2	1
182 #define DCF_P_P3	2
183 
184 #define DCF_Z_MET 0x2
185 #define DCF_Z_MED 0x1
186 
187 static u_long
188 ext_bf(
189 	unsigned char *buf,
190 	int   idx,
191 	const unsigned char *zero
192 	)
193 {
194 	u_long sum = 0;
195 	int i, first;
196 
197 	first = rawdcfcode[idx].offset;
198 
199 	for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
200 	{
201 		sum <<= 1;
202 		sum |= (buf[i] != zero[i]);
203 	}
204 	return sum;
205 }
206 
207 static unsigned
208 pcheck(
209        unsigned char *buf,
210        int   idx,
211        const unsigned char *zero
212        )
213 {
214 	int i,last;
215 	unsigned psum = 1;
216 
217 	last = partab[idx+1].offset;
218 
219 	for (i = partab[idx].offset; i < last; i++)
220 	    psum ^= (buf[i] != zero[i]);
221 
222 	return psum;
223 }
224 
225 static int/*BOOL*/
226 zeller_expand(
227 	clocktime_t     *clock_time,
228 	unsigned int	wd
229 	)
230 {
231         unsigned int  y = (unsigned int)clock_time->year;
232         unsigned int  m = (unsigned int)clock_time->month - 1u;
233         unsigned int  d = (unsigned int)clock_time->day - 1u;
234 	unsigned int  c;
235 
236 	/* Check basic constraints first. */
237         if ((y >= 100u) || (m >= 12u) || (d >= 31u) || (--wd >= 7u))
238 		return FALSE;
239 
240 	/* Get weekday of date in 1st century by a variation on Zeller's
241 	 * congruence. All operands are non-negative, and the month
242 	 * formula is adjusted to use a divider of 32, so we can do a
243 	 * shift instead of a 'true' division:
244 	 */
245 	if ((m += 10u) >= 12u)		/* shift base to 0000-03-01 */
246 		m -= 12u;
247 	else if (--y >= 100u)
248 		y += 100;
249 	d += y + (y >> 2) + 2u;		/* year-related share */
250 	d += (m * 83u + 16u) >> 5;	/* month-related share */
251 
252 	/* The next step combines the exact division by modular inverse
253 	 * with the (mod 7) step in such way that no true division and
254 	 * only one multiplication is needed. The multiplier is
255 	 *      M <- ceil((3*8)/7 * 2**29)
256 	 * and combines multiplication by invmod(5, 7) -> 3 and modulus
257 	 * by 7 transformation to (mod 8) in one step.
258 	 *   Note that 252 == 0 (mod 7) and that 'd' is less than 185,
259 	 * so the number to invert and reduce is strictly positive. In
260 	 * the end, 'c' is number of centuries since start of a great
261 	 * cycle and must be in [0..3] or we had bad input.
262 	 */
263 	c = (((252u + wd - d) * 0x6db6db6eU) >> 29) & 7u;
264 	if (c >= 4)
265 		return FALSE;
266 	/* undo calendar base shift now */
267 	if ((m > 9u) && (++y >= 100u)) {
268 		y -= 100u;
269 		c = (c + 1u) & 3u;
270 	}
271 	/* combine year with centuries & map to [1970..2369] */
272 	y += (c * 100u);
273 	clock_time->year = (int)y + ((y < 370u) ? 2000 : 1600);
274 	return TRUE;
275 }
276 
277 static u_long
278 convert_rawdcf(
279 	       unsigned char   *buffer,
280 	       int              size,
281 	       struct dcfparam *dcfprm,
282 	       clocktime_t     *clock_time
283 	       )
284 {
285 	unsigned char *s = buffer;
286 	const unsigned char *b = dcfprm->onebits;
287 	const unsigned char *c = dcfprm->zerobits;
288 	int i;
289 
290 	parseprintf(DD_RAWDCF,("parse: convert_rawdcf: \"%.*s\"\n", size, buffer));
291 
292 	if (size < 57)
293 	{
294 #ifndef PARSEKERNEL
295 		msyslog(LOG_ERR, "parse: convert_rawdcf: INCOMPLETE DATA - time code only has %d bits", size);
296 #endif
297 		return CVT_FAIL|CVT_BADFMT;
298 	}
299 
300 	for (i = 0; i < size; i++)
301 	{
302 		if ((*s != *b) && (*s != *c))
303 		{
304 			/*
305 			 * we only have two types of bytes (ones and zeros)
306 			 */
307 #ifndef PARSEKERNEL
308 			msyslog(LOG_ERR, "parse: convert_rawdcf: BAD DATA - no conversion");
309 #endif
310 			return CVT_FAIL|CVT_BADFMT;
311 		}
312 		if (*b) b++;
313 		if (*c) c++;
314 		s++;
315 	}
316 
317 	/*
318 	 * check Start and Parity bits
319 	 */
320 	if ((ext_bf(buffer, DCF_S, dcfprm->zerobits) == 1) &&
321 	    pcheck(buffer, DCF_P_P1, dcfprm->zerobits) &&
322 	    pcheck(buffer, DCF_P_P2, dcfprm->zerobits) &&
323 	    pcheck(buffer, DCF_P_P3, dcfprm->zerobits))
324 	{
325 		/*
326 		 * buffer OK
327 		 */
328 		parseprintf(DD_RAWDCF,("parse: convert_rawdcf: parity check passed\n"));
329 
330 		clock_time->flags  = PARSEB_S_CALLBIT|PARSEB_S_LEAP;
331 		clock_time->utctime= 0;
332 		clock_time->usecond= 0;
333 		clock_time->second = 0;
334 		clock_time->minute = ext_bf(buffer, DCF_M10, dcfprm->zerobits);
335 		clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1, dcfprm->zerobits);
336 		clock_time->hour   = ext_bf(buffer, DCF_H10, dcfprm->zerobits);
337 		clock_time->hour   = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1, dcfprm->zerobits);
338 		clock_time->day    = ext_bf(buffer, DCF_D10, dcfprm->zerobits);
339 		clock_time->day    = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1, dcfprm->zerobits);
340 		clock_time->month  = ext_bf(buffer, DCF_MO0, dcfprm->zerobits);
341 		clock_time->month  = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO, dcfprm->zerobits);
342 		clock_time->year   = ext_bf(buffer, DCF_Y10, dcfprm->zerobits);
343 		clock_time->year   = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1, dcfprm->zerobits);
344 
345 		if (!zeller_expand(clock_time, ext_bf(buffer, DCF_DW, dcfprm->zerobits)))
346 		    return CVT_FAIL|CVT_BADFMT;
347 
348 		switch (ext_bf(buffer, DCF_Z, dcfprm->zerobits))
349 		{
350 		    case DCF_Z_MET:
351 			clock_time->utcoffset = -1*60*60;
352 			break;
353 
354 		    case DCF_Z_MED:
355 			clock_time->flags     |= PARSEB_DST;
356 			clock_time->utcoffset  = -2*60*60;
357 			break;
358 
359 		    default:
360 			parseprintf(DD_RAWDCF,("parse: convert_rawdcf: BAD TIME ZONE\n"));
361 			return CVT_FAIL|CVT_BADFMT;
362 		}
363 
364 		if (ext_bf(buffer, DCF_A1, dcfprm->zerobits))
365 		    clock_time->flags |= PARSEB_ANNOUNCE;
366 
367 		if (ext_bf(buffer, DCF_A2, dcfprm->zerobits))
368 		    clock_time->flags |= PARSEB_LEAPADD; /* default: DCF77 data format deficiency */
369 
370 		if (ext_bf(buffer, DCF_R, dcfprm->zerobits))
371 		    clock_time->flags |= PARSEB_CALLBIT;
372 
373 		parseprintf(DD_RAWDCF,("parse: convert_rawdcf: TIME CODE OK: %02d:%02d, %02d.%02d.%02d, flags 0x%lx\n",
374 				       (int)clock_time->hour, (int)clock_time->minute, (int)clock_time->day, (int)clock_time->month,(int) clock_time->year,
375 				       (u_long)clock_time->flags));
376 		return CVT_OK;
377 	}
378 	else
379 	{
380 		/*
381 		 * bad format - not for us
382 		 */
383 #ifndef PARSEKERNEL
384 		msyslog(LOG_ERR, "parse: convert_rawdcf: start bit / parity check FAILED for \"%.*s\"", size, buffer);
385 #endif
386 		return CVT_FAIL|CVT_BADFMT;
387 	}
388 }
389 
390 /*
391  * parse_cvt_fnc_t cvt_rawdcf
392  * raw dcf input routine - needs to fix up 50 baud
393  * characters for 1/0 decision
394  */
395 static u_long
396 cvt_rawdcf(
397 	   unsigned char   *buffer,
398 	   int              size,
399 	   struct format   *param,
400 	   clocktime_t     *clock_time,
401 	   void            *local
402 	   )
403 {
404 	last_tcode_t  *t = (last_tcode_t *)local;
405 	unsigned char *s = (unsigned char *)buffer;
406 	unsigned char *e = s + size;
407 	const unsigned char *b = dcfparameter.onebits;
408 	const unsigned char *c = dcfparameter.zerobits;
409 	u_long       rtc = CVT_NONE;
410 	unsigned int i, lowmax, highmax, cutoff, span;
411 #define BITS 9
412 	unsigned char     histbuf[BITS];
413 	/*
414 	 * the input buffer contains characters with runs of consecutive
415 	 * bits set. These set bits are an indication of the DCF77 pulse
416 	 * length. We assume that we receive the pulse at 50 Baud. Thus
417 	 * a 100ms pulse would generate a 4 bit train (20ms per bit and
418 	 * start bit)
419 	 * a 200ms pulse would create all zeroes (and probably a frame error)
420 	 */
421 
422 	for (i = 0; i < BITS; i++)
423 	{
424 		histbuf[i] = 0;
425 	}
426 
427 	cutoff = 0;
428 	lowmax = 0;
429 
430 	while (s < e)
431 	{
432 		unsigned int ch = *s ^ 0xFF;
433 		/*
434 		 * these lines are left as an excercise to the reader 8-)
435 		 */
436 		if (!((ch+1) & ch) || !*s)
437 		{
438 
439 			for (i = 0; ch; i++)
440 			{
441 				ch >>= 1;
442 			}
443 
444 			*s = (unsigned char) i;
445 			histbuf[i]++;
446 			cutoff += i;
447 			lowmax++;
448 		}
449 		else
450 		{
451 			parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: character check for 0x%x@%d FAILED\n", *s, (int)(s - (unsigned char *)buffer)));
452 			*s = (unsigned char)~0;
453 			rtc = CVT_FAIL|CVT_BADFMT;
454 		}
455 		s++;
456 	}
457 
458 	if (lowmax)
459 	{
460 		cutoff /= lowmax;
461 	}
462 	else
463 	{
464 		cutoff = 4;	/* doesn't really matter - it'll fail anyway, but gives error output */
465 	}
466 
467 	parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: average bit count: %d\n", cutoff));
468 
469 	lowmax = 0;
470 	highmax = 0;
471 
472 	parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: histogram:"));
473 	for (i = 0; i <= cutoff; i++)
474 	{
475 		lowmax+=histbuf[i] * i;
476 		highmax += histbuf[i];
477 		parseprintf(DD_RAWDCF,(" %d", histbuf[i]));
478 	}
479 	parseprintf(DD_RAWDCF, (" <M>"));
480 
481 	lowmax += highmax / 2;
482 
483 	if (highmax)
484 	{
485 		lowmax /= highmax;
486 	}
487 	else
488 	{
489 		lowmax = 0;
490 	}
491 
492 	highmax = 0;
493 	cutoff = 0;
494 
495 	for (; i < BITS; i++)
496 	{
497 		highmax+=histbuf[i] * i;
498 		cutoff +=histbuf[i];
499 		parseprintf(DD_RAWDCF,(" %d", histbuf[i]));
500 	}
501 	parseprintf(DD_RAWDCF,("\n"));
502 
503 	if (cutoff)
504 	{
505 		highmax /= cutoff;
506 	}
507 	else
508 	{
509 		highmax = BITS-1;
510 	}
511 
512 	span = cutoff = lowmax;
513 	for (i = lowmax; i <= highmax; i++)
514 	{
515 		if (histbuf[cutoff] > histbuf[i])
516 		{
517 			cutoff = i;
518 			span = i;
519 		}
520 		else
521 		    if (histbuf[cutoff] == histbuf[i])
522 		    {
523 			    span = i;
524 		    }
525 	}
526 
527 	cutoff = (cutoff + span) / 2;
528 
529 	parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff));
530 
531 	s = (unsigned char *)buffer;
532 	while (s < e)
533 	{
534 		if (*s == (unsigned char)~0)
535 		{
536 			*s = '?';
537 		}
538 		else
539 		{
540 			*s = (*s >= cutoff) ? *b : *c;
541 		}
542 		s++;
543 		if (*b) b++;
544 		if (*c) c++;
545 	}
546 
547 	*s = '\0';
548 
549         if (rtc == CVT_NONE)
550         {
551 	       rtc = convert_rawdcf(buffer, size, &dcfparameter, clock_time);
552 	       if (rtc == CVT_OK)
553 	       {
554 			time_t newtime;
555 
556 			newtime = parse_to_unixtime(clock_time, &rtc);
557 			if ((rtc == CVT_OK) && t)
558 			{
559 				if ((newtime - t->tcode) <= 600) /* require a successful telegram within last 10 minutes */
560 				{
561 				        parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: recent timestamp check OK\n"));
562 					clock_time->utctime = newtime;
563 				}
564 				else
565 				{
566 					parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: recent timestamp check FAIL - ignore timestamp\n"));
567 					rtc = CVT_SKIP;
568 				}
569 				t->tcode            = newtime;
570 			}
571 	       }
572         }
573 
574     	return rtc;
575 }
576 
577 /*
578  * parse_pps_fnc_t pps_rawdcf
579  *
580  * currently a very stupid version - should be extended to decode
581  * also ones and zeros (which is easy)
582  */
583 /*ARGSUSED*/
584 static u_long
585 pps_rawdcf(
586 	parse_t *parseio,
587 	int status,
588 	timestamp_t *ptime
589 	)
590 {
591 	if (!status)		/* negative edge for simpler wiring (Rx->DCD) */
592 	{
593 		parseio->parse_dtime.parse_ptime  = *ptime;
594 		parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
595 	}
596 
597 	return CVT_NONE;
598 }
599 
600 static long
601 calc_usecdiff(
602 	timestamp_t *ref,
603 	timestamp_t *base,
604 	long         offset
605 	)
606 {
607 	struct timeval delta;
608 	long delta_usec = 0;
609 
610 #ifdef PARSEKERNEL
611 	delta.tv_sec = ref->tv.tv_sec - offset - base->tv.tv_sec;
612 	delta.tv_usec = ref->tv.tv_usec - base->tv.tv_usec;
613 	if (delta.tv_usec < 0)
614 	{
615 		delta.tv_sec  -= 1;
616 		delta.tv_usec += 1000000;
617 	}
618 #else
619 	l_fp delt;
620 
621 	delt = ref->fp;
622 	delt.l_i -= offset;
623 	L_SUB(&delt, &base->fp);
624 	TSTOTV(&delt, &delta);
625 #endif
626 
627 	delta_usec = 1000000 * (int32_t)delta.tv_sec + delta.tv_usec;
628 	return delta_usec;
629 }
630 
631 static u_long
632 snt_rawdcf(
633 	parse_t *parseio,
634 	timestamp_t *ptime
635 	)
636 {
637 	/*
638 	 * only synthesize if all of following conditions are met:
639 	 * - CVT_OK parse_status (we have a time stamp base)
640 	 * - ABS(ptime - tminute - (parse_index - 1) sec) < 500ms (spaced by 1 sec +- 500ms)
641 	 * - minute marker is available (confirms minute raster as base)
642 	 */
643 	last_tcode_t  *t = (last_tcode_t *)parseio->parse_pdata;
644 	long delta_usec = -1;
645 
646 	if (t != NULL && t->tminute.tv.tv_sec != 0) {
647 		delta_usec = calc_usecdiff(ptime, &t->tminute, parseio->parse_index - 1);
648 		if (delta_usec < 0)
649 			delta_usec = -delta_usec;
650 	}
651 
652 	parseprintf(DD_RAWDCF,("parse: snt_rawdcf: synth for offset %d seconds - absolute usec error %ld\n",
653 			       parseio->parse_index - 1, delta_usec));
654 
655 	if (((parseio->parse_dtime.parse_status & CVT_MASK) == CVT_OK) &&
656 	    (delta_usec < 500000 && delta_usec >= 0)) /* only if minute marker is available */
657 	{
658 		parseio->parse_dtime.parse_stime = *ptime;
659 
660 #ifdef PARSEKERNEL
661 		parseio->parse_dtime.parse_time.tv.tv_sec++;
662 #else
663 		parseio->parse_dtime.parse_time.fp.l_ui++;
664 #endif
665 
666 		parseprintf(DD_RAWDCF,("parse: snt_rawdcf: time stamp synthesized offset %d seconds\n", parseio->parse_index - 1));
667 
668 		return updatetimeinfo(parseio, parseio->parse_lstate);
669 	}
670 	return CVT_NONE;
671 }
672 
673 /*
674  * parse_inp_fnc_t inp_rawdcf
675  *
676  * grab DCF77 data from input stream
677  */
678 static u_long
679 inp_rawdcf(
680 	  parse_t      *parseio,
681 	  char         ch,
682 	  timestamp_t  *tstamp
683 	  )
684 {
685 	static struct timeval timeout = { 1, 500000 }; /* 1.5 secongs denote second #60 */
686 
687 	parseprintf(DD_PARSE, ("inp_rawdcf(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
688 
689 	parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
690 
691 	if (parse_timedout(parseio, tstamp, &timeout))
692 	{
693 		last_tcode_t *t = (last_tcode_t *)parseio->parse_pdata;
694 		long delta_usec;
695 
696 		parseprintf(DD_RAWDCF, ("inp_rawdcf: time out seen\n"));
697 		/* finish collection */
698 		(void) parse_end(parseio);
699 
700 		if (t != NULL)
701 		{
702 			/* remember minute start sample time if timeouts occur in minute raster */
703 			if (t->timeout.tv.tv_sec != 0)
704 			{
705 				delta_usec = calc_usecdiff(tstamp, &t->timeout, 60);
706 				if (delta_usec < 0)
707 					delta_usec = -delta_usec;
708 			}
709 			else
710 			{
711 				delta_usec = -1;
712 			}
713 
714 			if (delta_usec < 500000 && delta_usec >= 0)
715 			{
716 				parseprintf(DD_RAWDCF, ("inp_rawdcf: timeout time difference %ld usec - minute marker set\n", delta_usec));
717 				/* collect minute markers only if spaced by 60 seconds */
718 				t->tminute = *tstamp;
719 			}
720 			else
721 			{
722 				parseprintf(DD_RAWDCF, ("inp_rawdcf: timeout time difference %ld usec - minute marker cleared\n", delta_usec));
723 				memset((char *)&t->tminute, 0, sizeof(t->tminute));
724 			}
725 			t->timeout = *tstamp;
726 		}
727 		(void) parse_addchar(parseio, ch);
728 
729 		/* pass up to higher layers */
730 		return PARSE_INP_TIME;
731 	}
732 	else
733 	{
734 		unsigned int rtc;
735 
736 		rtc = parse_addchar(parseio, ch);
737 		if (rtc == PARSE_INP_SKIP)
738 		{
739 			if (snt_rawdcf(parseio, tstamp) == CVT_OK)
740 				return PARSE_INP_SYNTH;
741 		}
742 		return rtc;
743 	}
744 }
745 
746 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */
747 int clk_rawdcf_bs;
748 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */
749 
750 /*
751  * History:
752  *
753  * clk_rawdcf.c,v
754  * Revision 4.18  2006/06/22 18:40:01  kardel
755  * clean up signedness (gcc 4)
756  *
757  * Revision 4.17  2006/01/22 16:01:55  kardel
758  * update version information
759  *
760  * Revision 4.16  2006/01/22 15:51:22  kardel
761  * generate reasonable timecode output on invalid input
762  *
763  * Revision 4.15  2005/08/06 19:17:06  kardel
764  * clean log output
765  *
766  * Revision 4.14  2005/08/06 17:39:40  kardel
767  * cleanup size handling wrt/ to buffer boundaries
768  *
769  * Revision 4.13  2005/04/16 17:32:10  kardel
770  * update copyright
771  *
772  * Revision 4.12  2004/11/14 15:29:41  kardel
773  * support PPSAPI, upgrade Copyright to Berkeley style
774  *
775  * Revision 4.9  1999/12/06 13:42:23  kardel
776  * transfer correctly converted time codes always into tcode
777  *
778  * Revision 4.8  1999/11/28 09:13:50  kardel
779  * RECON_4_0_98F
780  *
781  * Revision 4.7  1999/04/01 20:07:20  kardel
782  * added checking for minutie increment of timestamps in clk_rawdcf.c
783  *
784  * Revision 4.6  1998/06/14 21:09:37  kardel
785  * Sun acc cleanup
786  *
787  * Revision 4.5  1998/06/13 12:04:16  kardel
788  * fix SYSV clock name clash
789  *
790  * Revision 4.4  1998/06/12 15:22:28  kardel
791  * fix prototypes
792  *
793  * Revision 4.3  1998/06/06 18:33:36  kardel
794  * simplified condidional compile expression
795  *
796  * Revision 4.2  1998/05/24 11:04:18  kardel
797  * triggering PPS on negative edge for simpler wiring (Rx->DCD)
798  *
799  * Revision 4.1  1998/05/24 09:39:53  kardel
800  * implementation of the new IO handling model
801  *
802  * Revision 4.0  1998/04/10 19:45:30  kardel
803  * Start 4.0 release version numbering
804  *
805  * from V3 3.24 log info deleted 1998/04/11 kardel
806  *
807  */
808