xref: /freebsd/contrib/ntp/libparse/clk_rawdcf.c (revision cddbc3b40812213ff00041f79174cac0be360a2a)
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 u_long
224 convert_rawdcf(
225 	       unsigned char   *buffer,
226 	       int              size,
227 	       struct dcfparam *dcfprm,
228 	       clocktime_t     *clock_time
229 	       )
230 {
231 	unsigned char *s = buffer;
232 	const unsigned char *b = dcfprm->onebits;
233 	const unsigned char *c = dcfprm->zerobits;
234 	int i;
235 
236 	parseprintf(DD_RAWDCF,("parse: convert_rawdcf: \"%.*s\"\n", size, buffer));
237 
238 	if (size < 57)
239 	{
240 #ifndef PARSEKERNEL
241 		msyslog(LOG_ERR, "parse: convert_rawdcf: INCOMPLETE DATA - time code only has %d bits", size);
242 #endif
243 		return CVT_FAIL|CVT_BADFMT;
244 	}
245 
246 	for (i = 0; i < size; i++)
247 	{
248 		if ((*s != *b) && (*s != *c))
249 		{
250 			/*
251 			 * we only have two types of bytes (ones and zeros)
252 			 */
253 #ifndef PARSEKERNEL
254 			msyslog(LOG_ERR, "parse: convert_rawdcf: BAD DATA - no conversion");
255 #endif
256 			return CVT_FAIL|CVT_BADFMT;
257 		}
258 		if (*b) b++;
259 		if (*c) c++;
260 		s++;
261 	}
262 
263 	/*
264 	 * check Start and Parity bits
265 	 */
266 	if ((ext_bf(buffer, DCF_S, dcfprm->zerobits) == 1) &&
267 	    pcheck(buffer, DCF_P_P1, dcfprm->zerobits) &&
268 	    pcheck(buffer, DCF_P_P2, dcfprm->zerobits) &&
269 	    pcheck(buffer, DCF_P_P3, dcfprm->zerobits))
270 	{
271 		/*
272 		 * buffer OK
273 		 */
274 		parseprintf(DD_RAWDCF,("parse: convert_rawdcf: parity check passed\n"));
275 
276 		clock_time->flags  = PARSEB_S_CALLBIT|PARSEB_S_LEAP;
277 		clock_time->utctime= 0;
278 		clock_time->usecond= 0;
279 		clock_time->second = 0;
280 		clock_time->minute = ext_bf(buffer, DCF_M10, dcfprm->zerobits);
281 		clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1, dcfprm->zerobits);
282 		clock_time->hour   = ext_bf(buffer, DCF_H10, dcfprm->zerobits);
283 		clock_time->hour   = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1, dcfprm->zerobits);
284 		clock_time->day    = ext_bf(buffer, DCF_D10, dcfprm->zerobits);
285 		clock_time->day    = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1, dcfprm->zerobits);
286 		clock_time->month  = ext_bf(buffer, DCF_MO0, dcfprm->zerobits);
287 		clock_time->month  = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO, dcfprm->zerobits);
288 		clock_time->year   = ext_bf(buffer, DCF_Y10, dcfprm->zerobits);
289 		clock_time->year   = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1, dcfprm->zerobits);
290 
291 		switch (ext_bf(buffer, DCF_Z, dcfprm->zerobits))
292 		{
293 		    case DCF_Z_MET:
294 			clock_time->utcoffset = -1*60*60;
295 			break;
296 
297 		    case DCF_Z_MED:
298 			clock_time->flags     |= PARSEB_DST;
299 			clock_time->utcoffset  = -2*60*60;
300 			break;
301 
302 		    default:
303 			parseprintf(DD_RAWDCF,("parse: convert_rawdcf: BAD TIME ZONE\n"));
304 			return CVT_FAIL|CVT_BADFMT;
305 		}
306 
307 		if (ext_bf(buffer, DCF_A1, dcfprm->zerobits))
308 		    clock_time->flags |= PARSEB_ANNOUNCE;
309 
310 		if (ext_bf(buffer, DCF_A2, dcfprm->zerobits))
311 		    clock_time->flags |= PARSEB_LEAPADD; /* default: DCF77 data format deficiency */
312 
313 		if (ext_bf(buffer, DCF_R, dcfprm->zerobits))
314 		    clock_time->flags |= PARSEB_CALLBIT;
315 
316 		parseprintf(DD_RAWDCF,("parse: convert_rawdcf: TIME CODE OK: %02d:%02d, %02d.%02d.%02d, flags 0x%lx\n",
317 				       (int)clock_time->hour, (int)clock_time->minute, (int)clock_time->day, (int)clock_time->month,(int) clock_time->year,
318 				       (u_long)clock_time->flags));
319 		return CVT_OK;
320 	}
321 	else
322 	{
323 		/*
324 		 * bad format - not for us
325 		 */
326 #ifndef PARSEKERNEL
327 		msyslog(LOG_ERR, "parse: convert_rawdcf: start bit / parity check FAILED for \"%.*s\"", size, buffer);
328 #endif
329 		return CVT_FAIL|CVT_BADFMT;
330 	}
331 }
332 
333 /*
334  * parse_cvt_fnc_t cvt_rawdcf
335  * raw dcf input routine - needs to fix up 50 baud
336  * characters for 1/0 decision
337  */
338 static u_long
339 cvt_rawdcf(
340 	   unsigned char   *buffer,
341 	   int              size,
342 	   struct format   *param,
343 	   clocktime_t     *clock_time,
344 	   void            *local
345 	   )
346 {
347 	last_tcode_t  *t = (last_tcode_t *)local;
348 	unsigned char *s = (unsigned char *)buffer;
349 	unsigned char *e = s + size;
350 	const unsigned char *b = dcfparameter.onebits;
351 	const unsigned char *c = dcfparameter.zerobits;
352 	u_long       rtc = CVT_NONE;
353 	unsigned int i, lowmax, highmax, cutoff, span;
354 #define BITS 9
355 	unsigned char     histbuf[BITS];
356 	/*
357 	 * the input buffer contains characters with runs of consecutive
358 	 * bits set. These set bits are an indication of the DCF77 pulse
359 	 * length. We assume that we receive the pulse at 50 Baud. Thus
360 	 * a 100ms pulse would generate a 4 bit train (20ms per bit and
361 	 * start bit)
362 	 * a 200ms pulse would create all zeroes (and probably a frame error)
363 	 */
364 
365 	for (i = 0; i < BITS; i++)
366 	{
367 		histbuf[i] = 0;
368 	}
369 
370 	cutoff = 0;
371 	lowmax = 0;
372 
373 	while (s < e)
374 	{
375 		unsigned int ch = *s ^ 0xFF;
376 		/*
377 		 * these lines are left as an excercise to the reader 8-)
378 		 */
379 		if (!((ch+1) & ch) || !*s)
380 		{
381 
382 			for (i = 0; ch; i++)
383 			{
384 				ch >>= 1;
385 			}
386 
387 			*s = (unsigned char) i;
388 			histbuf[i]++;
389 			cutoff += i;
390 			lowmax++;
391 		}
392 		else
393 		{
394 			parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: character check for 0x%x@%d FAILED\n", *s, (int)(s - (unsigned char *)buffer)));
395 			*s = (unsigned char)~0;
396 			rtc = CVT_FAIL|CVT_BADFMT;
397 		}
398 		s++;
399 	}
400 
401 	if (lowmax)
402 	{
403 		cutoff /= lowmax;
404 	}
405 	else
406 	{
407 		cutoff = 4;	/* doesn't really matter - it'll fail anyway, but gives error output */
408 	}
409 
410 	parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: average bit count: %d\n", cutoff));
411 
412 	lowmax = 0;
413 	highmax = 0;
414 
415 	parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: histogram:"));
416 	for (i = 0; i <= cutoff; i++)
417 	{
418 		lowmax+=histbuf[i] * i;
419 		highmax += histbuf[i];
420 		parseprintf(DD_RAWDCF,(" %d", histbuf[i]));
421 	}
422 	parseprintf(DD_RAWDCF, (" <M>"));
423 
424 	lowmax += highmax / 2;
425 
426 	if (highmax)
427 	{
428 		lowmax /= highmax;
429 	}
430 	else
431 	{
432 		lowmax = 0;
433 	}
434 
435 	highmax = 0;
436 	cutoff = 0;
437 
438 	for (; i < BITS; i++)
439 	{
440 		highmax+=histbuf[i] * i;
441 		cutoff +=histbuf[i];
442 		parseprintf(DD_RAWDCF,(" %d", histbuf[i]));
443 	}
444 	parseprintf(DD_RAWDCF,("\n"));
445 
446 	if (cutoff)
447 	{
448 		highmax /= cutoff;
449 	}
450 	else
451 	{
452 		highmax = BITS-1;
453 	}
454 
455 	span = cutoff = lowmax;
456 	for (i = lowmax; i <= highmax; i++)
457 	{
458 		if (histbuf[cutoff] > histbuf[i])
459 		{
460 			cutoff = i;
461 			span = i;
462 		}
463 		else
464 		    if (histbuf[cutoff] == histbuf[i])
465 		    {
466 			    span = i;
467 		    }
468 	}
469 
470 	cutoff = (cutoff + span) / 2;
471 
472 	parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff));
473 
474 	s = (unsigned char *)buffer;
475 	while (s < e)
476 	{
477 		if (*s == (unsigned char)~0)
478 		{
479 			*s = '?';
480 		}
481 		else
482 		{
483 			*s = (*s >= cutoff) ? *b : *c;
484 		}
485 		s++;
486 		if (*b) b++;
487 		if (*c) c++;
488 	}
489 
490 	*s = '\0';
491 
492         if (rtc == CVT_NONE)
493         {
494 	       rtc = convert_rawdcf(buffer, size, &dcfparameter, clock_time);
495 	       if (rtc == CVT_OK)
496 	       {
497 			time_t newtime;
498 
499 			newtime = parse_to_unixtime(clock_time, &rtc);
500 			if ((rtc == CVT_OK) && t)
501 			{
502 				if ((newtime - t->tcode) <= 600) /* require a successful telegram within last 10 minutes */
503 				{
504 				        parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: recent timestamp check OK\n"));
505 					clock_time->utctime = newtime;
506 				}
507 				else
508 				{
509 					parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: recent timestamp check FAIL - ignore timestamp\n"));
510 					rtc = CVT_SKIP;
511 				}
512 				t->tcode            = newtime;
513 			}
514 	       }
515         }
516 
517     	return rtc;
518 }
519 
520 /*
521  * parse_pps_fnc_t pps_rawdcf
522  *
523  * currently a very stupid version - should be extended to decode
524  * also ones and zeros (which is easy)
525  */
526 /*ARGSUSED*/
527 static u_long
528 pps_rawdcf(
529 	parse_t *parseio,
530 	int status,
531 	timestamp_t *ptime
532 	)
533 {
534 	if (!status)		/* negative edge for simpler wiring (Rx->DCD) */
535 	{
536 		parseio->parse_dtime.parse_ptime  = *ptime;
537 		parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS;
538 	}
539 
540 	return CVT_NONE;
541 }
542 
543 static long
544 calc_usecdiff(
545 	timestamp_t *ref,
546 	timestamp_t *base,
547 	long         offset
548 	)
549 {
550 	struct timeval delta;
551 	long delta_usec = 0;
552 
553 #ifdef PARSEKERNEL
554 	delta.tv_sec = ref->tv.tv_sec - offset - base->tv.tv_sec;
555 	delta.tv_usec = ref->tv.tv_usec - base->tv.tv_usec;
556 	if (delta.tv_usec < 0)
557 	{
558 		delta.tv_sec  -= 1;
559 		delta.tv_usec += 1000000;
560 	}
561 #else
562 	l_fp delt;
563 
564 	delt = ref->fp;
565 	delt.l_i -= offset;
566 	L_SUB(&delt, &base->fp);
567 	TSTOTV(&delt, &delta);
568 #endif
569 
570 	delta_usec = 1000000 * (int32_t)delta.tv_sec + delta.tv_usec;
571 	return delta_usec;
572 }
573 
574 static u_long
575 snt_rawdcf(
576 	parse_t *parseio,
577 	timestamp_t *ptime
578 	)
579 {
580 	/*
581 	 * only synthesize if all of following conditions are met:
582 	 * - CVT_OK parse_status (we have a time stamp base)
583 	 * - ABS(ptime - tminute - (parse_index - 1) sec) < 500ms (spaced by 1 sec +- 500ms)
584 	 * - minute marker is available (confirms minute raster as base)
585 	 */
586 	last_tcode_t  *t = (last_tcode_t *)parseio->parse_pdata;
587 	long delta_usec = -1;
588 
589 	if (t != NULL && t->tminute.tv.tv_sec != 0) {
590 		delta_usec = calc_usecdiff(ptime, &t->tminute, parseio->parse_index - 1);
591 		if (delta_usec < 0)
592 			delta_usec = -delta_usec;
593 	}
594 
595 	parseprintf(DD_RAWDCF,("parse: snt_rawdcf: synth for offset %d seconds - absolute usec error %ld\n",
596 			       parseio->parse_index - 1, delta_usec));
597 
598 	if (((parseio->parse_dtime.parse_status & CVT_MASK) == CVT_OK) &&
599 	    (delta_usec < 500000 && delta_usec >= 0)) /* only if minute marker is available */
600 	{
601 		parseio->parse_dtime.parse_stime = *ptime;
602 
603 #ifdef PARSEKERNEL
604 		parseio->parse_dtime.parse_time.tv.tv_sec++;
605 #else
606 		parseio->parse_dtime.parse_time.fp.l_ui++;
607 #endif
608 
609 		parseprintf(DD_RAWDCF,("parse: snt_rawdcf: time stamp synthesized offset %d seconds\n", parseio->parse_index - 1));
610 
611 		return updatetimeinfo(parseio, parseio->parse_lstate);
612 	}
613 	return CVT_NONE;
614 }
615 
616 /*
617  * parse_inp_fnc_t inp_rawdcf
618  *
619  * grab DCF77 data from input stream
620  */
621 static u_long
622 inp_rawdcf(
623 	  parse_t      *parseio,
624 	  char         ch,
625 	  timestamp_t  *tstamp
626 	  )
627 {
628 	static struct timeval timeout = { 1, 500000 }; /* 1.5 secongs denote second #60 */
629 
630 	parseprintf(DD_PARSE, ("inp_rawdcf(0x%p, 0x%x, ...)\n", (void*)parseio, ch));
631 
632 	parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */
633 
634 	if (parse_timedout(parseio, tstamp, &timeout))
635 	{
636 		last_tcode_t *t = (last_tcode_t *)parseio->parse_pdata;
637 		long delta_usec;
638 
639 		parseprintf(DD_RAWDCF, ("inp_rawdcf: time out seen\n"));
640 		/* finish collection */
641 		(void) parse_end(parseio);
642 
643 		if (t != NULL)
644 		{
645 			/* remember minute start sample time if timeouts occur in minute raster */
646 			if (t->timeout.tv.tv_sec != 0)
647 			{
648 				delta_usec = calc_usecdiff(tstamp, &t->timeout, 60);
649 				if (delta_usec < 0)
650 					delta_usec = -delta_usec;
651 			}
652 			else
653 			{
654 				delta_usec = -1;
655 			}
656 
657 			if (delta_usec < 500000 && delta_usec >= 0)
658 			{
659 				parseprintf(DD_RAWDCF, ("inp_rawdcf: timeout time difference %ld usec - minute marker set\n", delta_usec));
660 				/* collect minute markers only if spaced by 60 seconds */
661 				t->tminute = *tstamp;
662 			}
663 			else
664 			{
665 				parseprintf(DD_RAWDCF, ("inp_rawdcf: timeout time difference %ld usec - minute marker cleared\n", delta_usec));
666 				memset((char *)&t->tminute, 0, sizeof(t->tminute));
667 			}
668 			t->timeout = *tstamp;
669 		}
670 		(void) parse_addchar(parseio, ch);
671 
672 		/* pass up to higher layers */
673 		return PARSE_INP_TIME;
674 	}
675 	else
676 	{
677 		unsigned int rtc;
678 
679 		rtc = parse_addchar(parseio, ch);
680 		if (rtc == PARSE_INP_SKIP)
681 		{
682 			if (snt_rawdcf(parseio, tstamp) == CVT_OK)
683 				return PARSE_INP_SYNTH;
684 		}
685 		return rtc;
686 	}
687 }
688 
689 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */
690 int clk_rawdcf_bs;
691 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */
692 
693 /*
694  * History:
695  *
696  * clk_rawdcf.c,v
697  * Revision 4.18  2006/06/22 18:40:01  kardel
698  * clean up signedness (gcc 4)
699  *
700  * Revision 4.17  2006/01/22 16:01:55  kardel
701  * update version information
702  *
703  * Revision 4.16  2006/01/22 15:51:22  kardel
704  * generate reasonable timecode output on invalid input
705  *
706  * Revision 4.15  2005/08/06 19:17:06  kardel
707  * clean log output
708  *
709  * Revision 4.14  2005/08/06 17:39:40  kardel
710  * cleanup size handling wrt/ to buffer boundaries
711  *
712  * Revision 4.13  2005/04/16 17:32:10  kardel
713  * update copyright
714  *
715  * Revision 4.12  2004/11/14 15:29:41  kardel
716  * support PPSAPI, upgrade Copyright to Berkeley style
717  *
718  * Revision 4.9  1999/12/06 13:42:23  kardel
719  * transfer correctly converted time codes always into tcode
720  *
721  * Revision 4.8  1999/11/28 09:13:50  kardel
722  * RECON_4_0_98F
723  *
724  * Revision 4.7  1999/04/01 20:07:20  kardel
725  * added checking for minutie increment of timestamps in clk_rawdcf.c
726  *
727  * Revision 4.6  1998/06/14 21:09:37  kardel
728  * Sun acc cleanup
729  *
730  * Revision 4.5  1998/06/13 12:04:16  kardel
731  * fix SYSV clock name clash
732  *
733  * Revision 4.4  1998/06/12 15:22:28  kardel
734  * fix prototypes
735  *
736  * Revision 4.3  1998/06/06 18:33:36  kardel
737  * simplified condidional compile expression
738  *
739  * Revision 4.2  1998/05/24 11:04:18  kardel
740  * triggering PPS on negative edge for simpler wiring (Rx->DCD)
741  *
742  * Revision 4.1  1998/05/24 09:39:53  kardel
743  * implementation of the new IO handling model
744  *
745  * Revision 4.0  1998/04/10 19:45:30  kardel
746  * Start 4.0 release version numbering
747  *
748  * from V3 3.24 log info deleted 1998/04/11 kardel
749  *
750  */
751