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