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