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