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