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