154359Sroberto/* 2182007Sroberto * /src/NTP/REPOSITORY/ntp4-dev/libparse/clk_rawdcf.c,v 4.18 2006/06/22 18:40:01 kardel RELEASE_20060622_A 3290001Sglebius * 4182007Sroberto * clk_rawdcf.c,v 4.18 2006/06/22 18:40:01 kardel RELEASE_20060622_A 554359Sroberto * 654359Sroberto * Raw DCF77 pulse clock support 754359Sroberto * 8290001Sglebius * Copyright (c) 1995-2015 by Frank Kardel <kardel <AT> ntp.org> 9290001Sglebius * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany 1054359Sroberto * 11182007Sroberto * Redistribution and use in source and binary forms, with or without 12182007Sroberto * modification, are permitted provided that the following conditions 13182007Sroberto * are met: 14182007Sroberto * 1. Redistributions of source code must retain the above copyright 15182007Sroberto * notice, this list of conditions and the following disclaimer. 16182007Sroberto * 2. Redistributions in binary form must reproduce the above copyright 17182007Sroberto * notice, this list of conditions and the following disclaimer in the 18182007Sroberto * documentation and/or other materials provided with the distribution. 19182007Sroberto * 3. Neither the name of the author nor the names of its contributors 20182007Sroberto * may be used to endorse or promote products derived from this software 21182007Sroberto * without specific prior written permission. 22182007Sroberto * 23182007Sroberto * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24182007Sroberto * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25182007Sroberto * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26182007Sroberto * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27182007Sroberto * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28182007Sroberto * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29182007Sroberto * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30182007Sroberto * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31182007Sroberto * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32182007Sroberto * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33182007Sroberto * SUCH DAMAGE. 34182007Sroberto * 3554359Sroberto */ 3654359Sroberto 3754359Sroberto#ifdef HAVE_CONFIG_H 3854359Sroberto# include <config.h> 3954359Sroberto#endif 4054359Sroberto 4154359Sroberto#if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_RAWDCF) 4254359Sroberto 4354359Sroberto#include "ntp_fp.h" 44290001Sglebius#include "timevalops.h" 4554359Sroberto#include "ntp_unixtime.h" 4654359Sroberto#include "ntp_calendar.h" 4754359Sroberto 4854359Sroberto#include "parse.h" 4954359Sroberto#ifdef PARSESTREAM 5054359Sroberto# include <sys/parsestreams.h> 5154359Sroberto#endif 5254359Sroberto 5354359Sroberto#ifndef PARSEKERNEL 5454359Sroberto# include "ntp_stdlib.h" 5554359Sroberto#endif 5654359Sroberto 5754359Sroberto/* 5854359Sroberto * DCF77 raw time code 5954359Sroberto * 6054359Sroberto * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig 6154359Sroberto * und Berlin, Maerz 1989 6254359Sroberto * 6354359Sroberto * Timecode transmission: 6454359Sroberto * AM: 6554359Sroberto * time marks are send every second except for the second before the 6654359Sroberto * next minute mark 6754359Sroberto * time marks consist of a reduction of transmitter power to 25% 6854359Sroberto * of the nominal level 6954359Sroberto * the falling edge is the time indication (on time) 7054359Sroberto * time marks of a 100ms duration constitute a logical 0 7154359Sroberto * time marks of a 200ms duration constitute a logical 1 7254359Sroberto * FM: 7354359Sroberto * see the spec. (basically a (non-)inverted psuedo random phase shift) 7454359Sroberto * 7554359Sroberto * Encoding: 7654359Sroberto * Second Contents 7754359Sroberto * 0 - 10 AM: free, FM: 0 7854359Sroberto * 11 - 14 free 79290001Sglebius * 15 R - "call bit" used to signalize irregularities in the control facilities 80290001Sglebius * (until 2003 indicated transmission via alternate antenna) 8154359Sroberto * 16 A1 - expect zone change (1 hour before) 8254359Sroberto * 17 - 18 Z1,Z2 - time zone 8354359Sroberto * 0 0 illegal 8454359Sroberto * 0 1 MEZ (MET) 8554359Sroberto * 1 0 MESZ (MED, MET DST) 8654359Sroberto * 1 1 illegal 8754359Sroberto * 19 A2 - expect leap insertion/deletion (1 hour before) 8854359Sroberto * 20 S - start of time code (1) 8954359Sroberto * 21 - 24 M1 - BCD (lsb first) Minutes 9054359Sroberto * 25 - 27 M10 - BCD (lsb first) 10 Minutes 9154359Sroberto * 28 P1 - Minute Parity (even) 9254359Sroberto * 29 - 32 H1 - BCD (lsb first) Hours 9354359Sroberto * 33 - 34 H10 - BCD (lsb first) 10 Hours 9454359Sroberto * 35 P2 - Hour Parity (even) 9554359Sroberto * 36 - 39 D1 - BCD (lsb first) Days 9654359Sroberto * 40 - 41 D10 - BCD (lsb first) 10 Days 9754359Sroberto * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday) 9854359Sroberto * 45 - 49 MO - BCD (lsb first) Month 9954359Sroberto * 50 MO0 - 10 Months 10054359Sroberto * 51 - 53 Y1 - BCD (lsb first) Years 10154359Sroberto * 54 - 57 Y10 - BCD (lsb first) 10 Years 10254359Sroberto * 58 P3 - Date Parity (even) 10354359Sroberto * 59 - usually missing (minute indication), except for leap insertion 10454359Sroberto */ 10554359Sroberto 106290001Sglebiusstatic parse_pps_fnc_t pps_rawdcf; 107290001Sglebiusstatic parse_cvt_fnc_t cvt_rawdcf; 108290001Sglebiusstatic parse_inp_fnc_t inp_rawdcf; 10954359Sroberto 11056746Srobertotypedef struct last_tcode { 111290001Sglebius time_t tcode; /* last converted time code */ 112290001Sglebius timestamp_t tminute; /* sample time for minute start */ 113290001Sglebius timestamp_t timeout; /* last timeout timestamp */ 11456746Sroberto} last_tcode_t; 11556746Sroberto 116182007Sroberto#define BUFFER_MAX 61 117182007Sroberto 11854359Srobertoclockformat_t clock_rawdcf = 11954359Sroberto{ 12054359Sroberto inp_rawdcf, /* DCF77 input handling */ 12154359Sroberto cvt_rawdcf, /* raw dcf input conversion */ 12254359Sroberto pps_rawdcf, /* examining PPS information */ 12354359Sroberto 0, /* no private configuration data */ 12454359Sroberto "RAW DCF77 Timecode", /* direct decoding / time synthesis */ 12554359Sroberto 126182007Sroberto BUFFER_MAX, /* bit buffer */ 12756746Sroberto sizeof(last_tcode_t) 12854359Sroberto}; 12954359Sroberto 13054359Srobertostatic struct dcfparam 13154359Sroberto{ 132290001Sglebius const unsigned char *onebits; 133290001Sglebius const unsigned char *zerobits; 134290001Sglebius} dcfparameter = 13554359Sroberto{ 136290001Sglebius (const unsigned char *)"###############RADMLS1248124P124812P1248121241248112481248P??", /* 'ONE' representation */ 137290001Sglebius (const unsigned char *)"--------------------s-------p------p----------------------p__" /* 'ZERO' representation */ 13854359Sroberto}; 13954359Sroberto 140290001Sglebiusstatic struct rawdcfcode 14154359Sroberto{ 14254359Sroberto char offset; /* start bit */ 14354359Sroberto} rawdcfcode[] = 14454359Sroberto{ 14554359Sroberto { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 }, 14654359Sroberto { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 } 14754359Sroberto}; 14854359Sroberto 14954359Sroberto#define DCF_M 0 15054359Sroberto#define DCF_R 1 15154359Sroberto#define DCF_A1 2 15254359Sroberto#define DCF_Z 3 15354359Sroberto#define DCF_A2 4 15454359Sroberto#define DCF_S 5 15554359Sroberto#define DCF_M1 6 15654359Sroberto#define DCF_M10 7 15754359Sroberto#define DCF_P1 8 15854359Sroberto#define DCF_H1 9 15954359Sroberto#define DCF_H10 10 16054359Sroberto#define DCF_P2 11 16154359Sroberto#define DCF_D1 12 16254359Sroberto#define DCF_D10 13 16354359Sroberto#define DCF_DW 14 16454359Sroberto#define DCF_MO 15 16554359Sroberto#define DCF_MO0 16 16654359Sroberto#define DCF_Y1 17 16754359Sroberto#define DCF_Y10 18 16854359Sroberto#define DCF_P3 19 16954359Sroberto 17054359Srobertostatic struct partab 17154359Sroberto{ 17254359Sroberto char offset; /* start bit of parity field */ 17354359Sroberto} partab[] = 17454359Sroberto{ 17554359Sroberto { 21 }, { 29 }, { 36 }, { 59 } 17654359Sroberto}; 17754359Sroberto 17854359Sroberto#define DCF_P_P1 0 17954359Sroberto#define DCF_P_P2 1 18054359Sroberto#define DCF_P_P3 2 18154359Sroberto 18254359Sroberto#define DCF_Z_MET 0x2 18354359Sroberto#define DCF_Z_MED 0x1 18454359Sroberto 18554359Srobertostatic u_long 18654359Srobertoext_bf( 187182007Sroberto unsigned char *buf, 188182007Sroberto int idx, 189290001Sglebius const unsigned char *zero 19054359Sroberto ) 19154359Sroberto{ 192182007Sroberto u_long sum = 0; 193182007Sroberto int i, first; 19454359Sroberto 19554359Sroberto first = rawdcfcode[idx].offset; 196290001Sglebius 19754359Sroberto for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--) 19854359Sroberto { 19954359Sroberto sum <<= 1; 20054359Sroberto sum |= (buf[i] != zero[i]); 20154359Sroberto } 20254359Sroberto return sum; 20354359Sroberto} 20454359Sroberto 20554359Srobertostatic unsigned 20654359Srobertopcheck( 20754359Sroberto unsigned char *buf, 20854359Sroberto int idx, 209290001Sglebius const unsigned char *zero 21054359Sroberto ) 21154359Sroberto{ 21254359Sroberto int i,last; 21354359Sroberto unsigned psum = 1; 21454359Sroberto 21554359Sroberto last = partab[idx+1].offset; 21654359Sroberto 21754359Sroberto for (i = partab[idx].offset; i < last; i++) 21854359Sroberto psum ^= (buf[i] != zero[i]); 21954359Sroberto 22054359Sroberto return psum; 22154359Sroberto} 22254359Sroberto 22354359Srobertostatic u_long 22454359Srobertoconvert_rawdcf( 22554359Sroberto unsigned char *buffer, 22654359Sroberto int size, 22754359Sroberto struct dcfparam *dcfprm, 22854359Sroberto clocktime_t *clock_time 22954359Sroberto ) 23054359Sroberto{ 231182007Sroberto unsigned char *s = buffer; 232290001Sglebius const unsigned char *b = dcfprm->onebits; 233290001Sglebius const unsigned char *c = dcfprm->zerobits; 234182007Sroberto int i; 23554359Sroberto 236290001Sglebius parseprintf(DD_RAWDCF,("parse: convert_rawdcf: \"%.*s\"\n", size, buffer)); 23754359Sroberto 23854359Sroberto if (size < 57) 23954359Sroberto { 24054359Sroberto#ifndef PARSEKERNEL 241290001Sglebius msyslog(LOG_ERR, "parse: convert_rawdcf: INCOMPLETE DATA - time code only has %d bits", size); 24254359Sroberto#endif 243290001Sglebius return CVT_FAIL|CVT_BADFMT; 24454359Sroberto } 245290001Sglebius 246182007Sroberto for (i = 0; i < size; i++) 24754359Sroberto { 24854359Sroberto if ((*s != *b) && (*s != *c)) 24954359Sroberto { 25054359Sroberto /* 25154359Sroberto * we only have two types of bytes (ones and zeros) 25254359Sroberto */ 25354359Sroberto#ifndef PARSEKERNEL 254182007Sroberto msyslog(LOG_ERR, "parse: convert_rawdcf: BAD DATA - no conversion"); 25554359Sroberto#endif 256290001Sglebius return CVT_FAIL|CVT_BADFMT; 25754359Sroberto } 258182007Sroberto if (*b) b++; 259182007Sroberto if (*c) c++; 26054359Sroberto s++; 26154359Sroberto } 262182007Sroberto 26354359Sroberto /* 26454359Sroberto * check Start and Parity bits 26554359Sroberto */ 26654359Sroberto if ((ext_bf(buffer, DCF_S, dcfprm->zerobits) == 1) && 26754359Sroberto pcheck(buffer, DCF_P_P1, dcfprm->zerobits) && 26854359Sroberto pcheck(buffer, DCF_P_P2, dcfprm->zerobits) && 26954359Sroberto pcheck(buffer, DCF_P_P3, dcfprm->zerobits)) 27054359Sroberto { 27154359Sroberto /* 27254359Sroberto * buffer OK 27354359Sroberto */ 27454359Sroberto parseprintf(DD_RAWDCF,("parse: convert_rawdcf: parity check passed\n")); 27554359Sroberto 276290001Sglebius clock_time->flags = PARSEB_S_CALLBIT|PARSEB_S_LEAP; 27754359Sroberto clock_time->utctime= 0; 27854359Sroberto clock_time->usecond= 0; 27954359Sroberto clock_time->second = 0; 28054359Sroberto clock_time->minute = ext_bf(buffer, DCF_M10, dcfprm->zerobits); 28154359Sroberto clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1, dcfprm->zerobits); 28254359Sroberto clock_time->hour = ext_bf(buffer, DCF_H10, dcfprm->zerobits); 28354359Sroberto clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1, dcfprm->zerobits); 28454359Sroberto clock_time->day = ext_bf(buffer, DCF_D10, dcfprm->zerobits); 28554359Sroberto clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1, dcfprm->zerobits); 28654359Sroberto clock_time->month = ext_bf(buffer, DCF_MO0, dcfprm->zerobits); 28754359Sroberto clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO, dcfprm->zerobits); 28854359Sroberto clock_time->year = ext_bf(buffer, DCF_Y10, dcfprm->zerobits); 28954359Sroberto clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1, dcfprm->zerobits); 29054359Sroberto 29154359Sroberto switch (ext_bf(buffer, DCF_Z, dcfprm->zerobits)) 29254359Sroberto { 29354359Sroberto case DCF_Z_MET: 29454359Sroberto clock_time->utcoffset = -1*60*60; 29554359Sroberto break; 29654359Sroberto 29754359Sroberto case DCF_Z_MED: 29854359Sroberto clock_time->flags |= PARSEB_DST; 29954359Sroberto clock_time->utcoffset = -2*60*60; 30054359Sroberto break; 30154359Sroberto 30254359Sroberto default: 30354359Sroberto parseprintf(DD_RAWDCF,("parse: convert_rawdcf: BAD TIME ZONE\n")); 30454359Sroberto return CVT_FAIL|CVT_BADFMT; 30554359Sroberto } 30654359Sroberto 30754359Sroberto if (ext_bf(buffer, DCF_A1, dcfprm->zerobits)) 30854359Sroberto clock_time->flags |= PARSEB_ANNOUNCE; 30954359Sroberto 31054359Sroberto if (ext_bf(buffer, DCF_A2, dcfprm->zerobits)) 31154359Sroberto clock_time->flags |= PARSEB_LEAPADD; /* default: DCF77 data format deficiency */ 31254359Sroberto 31354359Sroberto if (ext_bf(buffer, DCF_R, dcfprm->zerobits)) 314290001Sglebius clock_time->flags |= PARSEB_CALLBIT; 31554359Sroberto 316290001Sglebius parseprintf(DD_RAWDCF,("parse: convert_rawdcf: TIME CODE OK: %02d:%02d, %02d.%02d.%02d, flags 0x%lx\n", 31754359Sroberto (int)clock_time->hour, (int)clock_time->minute, (int)clock_time->day, (int)clock_time->month,(int) clock_time->year, 31854359Sroberto (u_long)clock_time->flags)); 31954359Sroberto return CVT_OK; 32054359Sroberto } 32154359Sroberto else 32254359Sroberto { 32354359Sroberto /* 32454359Sroberto * bad format - not for us 32554359Sroberto */ 32654359Sroberto#ifndef PARSEKERNEL 327290001Sglebius msyslog(LOG_ERR, "parse: convert_rawdcf: start bit / parity check FAILED for \"%.*s\"", size, buffer); 32854359Sroberto#endif 32954359Sroberto return CVT_FAIL|CVT_BADFMT; 33054359Sroberto } 33154359Sroberto} 33254359Sroberto 33354359Sroberto/* 334290001Sglebius * parse_cvt_fnc_t cvt_rawdcf 33554359Sroberto * raw dcf input routine - needs to fix up 50 baud 33654359Sroberto * characters for 1/0 decision 33754359Sroberto */ 33854359Srobertostatic u_long 33954359Srobertocvt_rawdcf( 34054359Sroberto unsigned char *buffer, 34154359Sroberto int size, 34254359Sroberto struct format *param, 34354359Sroberto clocktime_t *clock_time, 34454359Sroberto void *local 34554359Sroberto ) 34654359Sroberto{ 347182007Sroberto last_tcode_t *t = (last_tcode_t *)local; 348182007Sroberto unsigned char *s = (unsigned char *)buffer; 349182007Sroberto unsigned char *e = s + size; 350290001Sglebius const unsigned char *b = dcfparameter.onebits; 351290001Sglebius const unsigned char *c = dcfparameter.zerobits; 352182007Sroberto u_long rtc = CVT_NONE; 353182007Sroberto unsigned int i, lowmax, highmax, cutoff, span; 35454359Sroberto#define BITS 9 35554359Sroberto unsigned char histbuf[BITS]; 35654359Sroberto /* 35754359Sroberto * the input buffer contains characters with runs of consecutive 35854359Sroberto * bits set. These set bits are an indication of the DCF77 pulse 35954359Sroberto * length. We assume that we receive the pulse at 50 Baud. Thus 36054359Sroberto * a 100ms pulse would generate a 4 bit train (20ms per bit and 36154359Sroberto * start bit) 36254359Sroberto * a 200ms pulse would create all zeroes (and probably a frame error) 36354359Sroberto */ 36454359Sroberto 36554359Sroberto for (i = 0; i < BITS; i++) 36654359Sroberto { 36754359Sroberto histbuf[i] = 0; 36854359Sroberto } 36954359Sroberto 37054359Sroberto cutoff = 0; 37154359Sroberto lowmax = 0; 37254359Sroberto 37354359Sroberto while (s < e) 37454359Sroberto { 375182007Sroberto unsigned int ch = *s ^ 0xFF; 37654359Sroberto /* 37754359Sroberto * these lines are left as an excercise to the reader 8-) 37854359Sroberto */ 37954359Sroberto if (!((ch+1) & ch) || !*s) 38054359Sroberto { 38154359Sroberto 38254359Sroberto for (i = 0; ch; i++) 38354359Sroberto { 38454359Sroberto ch >>= 1; 38554359Sroberto } 38654359Sroberto 387290001Sglebius *s = (unsigned char) i; 38854359Sroberto histbuf[i]++; 38954359Sroberto cutoff += i; 39054359Sroberto lowmax++; 39154359Sroberto } 39254359Sroberto else 39354359Sroberto { 39454359Sroberto parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: character check for 0x%x@%d FAILED\n", *s, (int)(s - (unsigned char *)buffer))); 39554359Sroberto *s = (unsigned char)~0; 39654359Sroberto rtc = CVT_FAIL|CVT_BADFMT; 39754359Sroberto } 39854359Sroberto s++; 39954359Sroberto } 40054359Sroberto 40154359Sroberto if (lowmax) 40254359Sroberto { 40354359Sroberto cutoff /= lowmax; 40454359Sroberto } 40554359Sroberto else 40654359Sroberto { 40754359Sroberto cutoff = 4; /* doesn't really matter - it'll fail anyway, but gives error output */ 40854359Sroberto } 40954359Sroberto 41054359Sroberto parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: average bit count: %d\n", cutoff)); 41154359Sroberto 41254359Sroberto lowmax = 0; 41354359Sroberto highmax = 0; 41454359Sroberto 41554359Sroberto parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: histogram:")); 41654359Sroberto for (i = 0; i <= cutoff; i++) 41754359Sroberto { 41854359Sroberto lowmax+=histbuf[i] * i; 41954359Sroberto highmax += histbuf[i]; 42054359Sroberto parseprintf(DD_RAWDCF,(" %d", histbuf[i])); 42154359Sroberto } 42254359Sroberto parseprintf(DD_RAWDCF, (" <M>")); 42354359Sroberto 42454359Sroberto lowmax += highmax / 2; 42554359Sroberto 42654359Sroberto if (highmax) 42754359Sroberto { 42854359Sroberto lowmax /= highmax; 42954359Sroberto } 43054359Sroberto else 43154359Sroberto { 43254359Sroberto lowmax = 0; 43354359Sroberto } 43454359Sroberto 43554359Sroberto highmax = 0; 43654359Sroberto cutoff = 0; 43754359Sroberto 43854359Sroberto for (; i < BITS; i++) 43954359Sroberto { 44054359Sroberto highmax+=histbuf[i] * i; 44154359Sroberto cutoff +=histbuf[i]; 44254359Sroberto parseprintf(DD_RAWDCF,(" %d", histbuf[i])); 44354359Sroberto } 44454359Sroberto parseprintf(DD_RAWDCF,("\n")); 44554359Sroberto 44654359Sroberto if (cutoff) 44754359Sroberto { 44854359Sroberto highmax /= cutoff; 44954359Sroberto } 45054359Sroberto else 45154359Sroberto { 45254359Sroberto highmax = BITS-1; 45354359Sroberto } 45454359Sroberto 45554359Sroberto span = cutoff = lowmax; 45654359Sroberto for (i = lowmax; i <= highmax; i++) 45754359Sroberto { 45854359Sroberto if (histbuf[cutoff] > histbuf[i]) 45954359Sroberto { 46054359Sroberto cutoff = i; 46154359Sroberto span = i; 46254359Sroberto } 46354359Sroberto else 46454359Sroberto if (histbuf[cutoff] == histbuf[i]) 46554359Sroberto { 46654359Sroberto span = i; 46754359Sroberto } 46854359Sroberto } 46954359Sroberto 47054359Sroberto cutoff = (cutoff + span) / 2; 47154359Sroberto 47254359Sroberto parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff)); 47354359Sroberto 47454359Sroberto s = (unsigned char *)buffer; 475182007Sroberto while (s < e) 47654359Sroberto { 47754359Sroberto if (*s == (unsigned char)~0) 47854359Sroberto { 47954359Sroberto *s = '?'; 48054359Sroberto } 48154359Sroberto else 48254359Sroberto { 48354359Sroberto *s = (*s >= cutoff) ? *b : *c; 48454359Sroberto } 48554359Sroberto s++; 486182007Sroberto if (*b) b++; 487182007Sroberto if (*c) c++; 48854359Sroberto } 48954359Sroberto 490290001Sglebius *s = '\0'; 491290001Sglebius 49256746Sroberto if (rtc == CVT_NONE) 49356746Sroberto { 49456746Sroberto rtc = convert_rawdcf(buffer, size, &dcfparameter, clock_time); 49556746Sroberto if (rtc == CVT_OK) 49656746Sroberto { 49756746Sroberto time_t newtime; 49856746Sroberto 49956746Sroberto newtime = parse_to_unixtime(clock_time, &rtc); 50056746Sroberto if ((rtc == CVT_OK) && t) 50156746Sroberto { 502290001Sglebius if ((newtime - t->tcode) <= 600) /* require a successful telegram within last 10 minutes */ 50356746Sroberto { 504290001Sglebius parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: recent timestamp check OK\n")); 50556746Sroberto clock_time->utctime = newtime; 50656746Sroberto } 50756746Sroberto else 50856746Sroberto { 509290001Sglebius parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: recent timestamp check FAIL - ignore timestamp\n")); 510290001Sglebius rtc = CVT_SKIP; 51156746Sroberto } 51256746Sroberto t->tcode = newtime; 51356746Sroberto } 51456746Sroberto } 51556746Sroberto } 516290001Sglebius 51756746Sroberto return rtc; 51854359Sroberto} 51954359Sroberto 52054359Sroberto/* 521290001Sglebius * parse_pps_fnc_t pps_rawdcf 52254359Sroberto * 52354359Sroberto * currently a very stupid version - should be extended to decode 52454359Sroberto * also ones and zeros (which is easy) 52554359Sroberto */ 52654359Sroberto/*ARGSUSED*/ 52754359Srobertostatic u_long 52854359Srobertopps_rawdcf( 529182007Sroberto parse_t *parseio, 530182007Sroberto int status, 531182007Sroberto timestamp_t *ptime 53254359Sroberto ) 53354359Sroberto{ 53454359Sroberto if (!status) /* negative edge for simpler wiring (Rx->DCD) */ 53554359Sroberto { 53654359Sroberto parseio->parse_dtime.parse_ptime = *ptime; 53754359Sroberto parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS; 53854359Sroberto } 53954359Sroberto 54054359Sroberto return CVT_NONE; 54154359Sroberto} 54254359Sroberto 543290001Sglebiusstatic long 544290001Sglebiuscalc_usecdiff( 545290001Sglebius timestamp_t *ref, 546290001Sglebius timestamp_t *base, 547290001Sglebius long offset 548290001Sglebius ) 549290001Sglebius{ 550290001Sglebius struct timeval delta; 551290001Sglebius long delta_usec = 0; 552290001Sglebius 553290001Sglebius#ifdef PARSEKERNEL 554290001Sglebius delta.tv_sec = ref->tv.tv_sec - offset - base->tv.tv_sec; 555290001Sglebius delta.tv_usec = ref->tv.tv_usec - base->tv.tv_usec; 556290001Sglebius if (delta.tv_usec < 0) 557290001Sglebius { 558290001Sglebius delta.tv_sec -= 1; 559290001Sglebius delta.tv_usec += 1000000; 560290001Sglebius } 561290001Sglebius#else 562290001Sglebius l_fp delt; 563290001Sglebius 564290001Sglebius delt = ref->fp; 565290001Sglebius delt.l_i -= offset; 566290001Sglebius L_SUB(&delt, &base->fp); 567290001Sglebius TSTOTV(&delt, &delta); 568290001Sglebius#endif 569290001Sglebius 570290001Sglebius delta_usec = 1000000 * (int32_t)delta.tv_sec + delta.tv_usec; 571290001Sglebius return delta_usec; 572290001Sglebius} 573290001Sglebius 57454359Srobertostatic u_long 57554359Srobertosnt_rawdcf( 576182007Sroberto parse_t *parseio, 577182007Sroberto timestamp_t *ptime 57854359Sroberto ) 57954359Sroberto{ 580290001Sglebius /* 581290001Sglebius * only synthesize if all of following conditions are met: 582290001Sglebius * - CVT_OK parse_status (we have a time stamp base) 583290001Sglebius * - ABS(ptime - tminute - (parse_index - 1) sec) < 500ms (spaced by 1 sec +- 500ms) 584290001Sglebius * - minute marker is available (confirms minute raster as base) 585290001Sglebius */ 586290001Sglebius last_tcode_t *t = (last_tcode_t *)parseio->parse_pdata; 587290001Sglebius long delta_usec = -1; 588290001Sglebius 589290001Sglebius if (t != NULL && t->tminute.tv.tv_sec != 0) { 590290001Sglebius delta_usec = calc_usecdiff(ptime, &t->tminute, parseio->parse_index - 1); 591290001Sglebius if (delta_usec < 0) 592290001Sglebius delta_usec = -delta_usec; 593290001Sglebius } 594290001Sglebius 595290001Sglebius parseprintf(DD_RAWDCF,("parse: snt_rawdcf: synth for offset %d seconds - absolute usec error %ld\n", 596290001Sglebius parseio->parse_index - 1, delta_usec)); 597290001Sglebius 598290001Sglebius if (((parseio->parse_dtime.parse_status & CVT_MASK) == CVT_OK) && 599290001Sglebius (delta_usec < 500000 && delta_usec >= 0)) /* only if minute marker is available */ 60054359Sroberto { 60154359Sroberto parseio->parse_dtime.parse_stime = *ptime; 60254359Sroberto 60354359Sroberto#ifdef PARSEKERNEL 60454359Sroberto parseio->parse_dtime.parse_time.tv.tv_sec++; 60554359Sroberto#else 60654359Sroberto parseio->parse_dtime.parse_time.fp.l_ui++; 60754359Sroberto#endif 608290001Sglebius 60954359Sroberto parseprintf(DD_RAWDCF,("parse: snt_rawdcf: time stamp synthesized offset %d seconds\n", parseio->parse_index - 1)); 610290001Sglebius 61154359Sroberto return updatetimeinfo(parseio, parseio->parse_lstate); 61254359Sroberto } 61354359Sroberto return CVT_NONE; 61454359Sroberto} 61554359Sroberto 61654359Sroberto/* 617290001Sglebius * parse_inp_fnc_t inp_rawdcf 61854359Sroberto * 619182007Sroberto * grab DCF77 data from input stream 62054359Sroberto */ 62154359Srobertostatic u_long 62254359Srobertoinp_rawdcf( 62354359Sroberto parse_t *parseio, 624290001Sglebius char ch, 62554359Sroberto timestamp_t *tstamp 62654359Sroberto ) 62754359Sroberto{ 62854359Sroberto static struct timeval timeout = { 1, 500000 }; /* 1.5 secongs denote second #60 */ 629290001Sglebius 630293896Sglebius parseprintf(DD_PARSE, ("inp_rawdcf(0x%p, 0x%x, ...)\n", (void*)parseio, ch)); 631290001Sglebius 63254359Sroberto parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */ 63354359Sroberto 63454359Sroberto if (parse_timedout(parseio, tstamp, &timeout)) 63554359Sroberto { 636290001Sglebius last_tcode_t *t = (last_tcode_t *)parseio->parse_pdata; 637290001Sglebius long delta_usec; 638290001Sglebius 639290001Sglebius parseprintf(DD_RAWDCF, ("inp_rawdcf: time out seen\n")); 640290001Sglebius /* finish collection */ 641290001Sglebius (void) parse_end(parseio); 64254359Sroberto 643290001Sglebius if (t != NULL) 644290001Sglebius { 645290001Sglebius /* remember minute start sample time if timeouts occur in minute raster */ 646290001Sglebius if (t->timeout.tv.tv_sec != 0) 647290001Sglebius { 648290001Sglebius delta_usec = calc_usecdiff(tstamp, &t->timeout, 60); 649290001Sglebius if (delta_usec < 0) 650290001Sglebius delta_usec = -delta_usec; 651290001Sglebius } 652290001Sglebius else 653290001Sglebius { 654290001Sglebius delta_usec = -1; 655290001Sglebius } 656290001Sglebius 657290001Sglebius if (delta_usec < 500000 && delta_usec >= 0) 658290001Sglebius { 659290001Sglebius parseprintf(DD_RAWDCF, ("inp_rawdcf: timeout time difference %ld usec - minute marker set\n", delta_usec)); 660290001Sglebius /* collect minute markers only if spaced by 60 seconds */ 661290001Sglebius t->tminute = *tstamp; 662290001Sglebius } 663290001Sglebius else 664290001Sglebius { 665290001Sglebius parseprintf(DD_RAWDCF, ("inp_rawdcf: timeout time difference %ld usec - minute marker cleared\n", delta_usec)); 666290001Sglebius memset((char *)&t->tminute, 0, sizeof(t->tminute)); 667290001Sglebius } 668290001Sglebius t->timeout = *tstamp; 669290001Sglebius } 67054359Sroberto (void) parse_addchar(parseio, ch); 671290001Sglebius 672290001Sglebius /* pass up to higher layers */ 67354359Sroberto return PARSE_INP_TIME; 67454359Sroberto } 67554359Sroberto else 67654359Sroberto { 67754359Sroberto unsigned int rtc; 678290001Sglebius 67954359Sroberto rtc = parse_addchar(parseio, ch); 68054359Sroberto if (rtc == PARSE_INP_SKIP) 68154359Sroberto { 68254359Sroberto if (snt_rawdcf(parseio, tstamp) == CVT_OK) 68354359Sroberto return PARSE_INP_SYNTH; 68454359Sroberto } 68554359Sroberto return rtc; 68654359Sroberto } 68754359Sroberto} 68854359Sroberto 68954359Sroberto#else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */ 69054359Srobertoint clk_rawdcf_bs; 69154359Sroberto#endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */ 69254359Sroberto 69354359Sroberto/* 69454359Sroberto * History: 69554359Sroberto * 69654359Sroberto * clk_rawdcf.c,v 697182007Sroberto * Revision 4.18 2006/06/22 18:40:01 kardel 698182007Sroberto * clean up signedness (gcc 4) 699182007Sroberto * 700182007Sroberto * Revision 4.17 2006/01/22 16:01:55 kardel 701182007Sroberto * update version information 702182007Sroberto * 703182007Sroberto * Revision 4.16 2006/01/22 15:51:22 kardel 704182007Sroberto * generate reasonable timecode output on invalid input 705182007Sroberto * 706182007Sroberto * Revision 4.15 2005/08/06 19:17:06 kardel 707182007Sroberto * clean log output 708182007Sroberto * 709182007Sroberto * Revision 4.14 2005/08/06 17:39:40 kardel 710182007Sroberto * cleanup size handling wrt/ to buffer boundaries 711182007Sroberto * 712182007Sroberto * Revision 4.13 2005/04/16 17:32:10 kardel 713182007Sroberto * update copyright 714182007Sroberto * 715182007Sroberto * Revision 4.12 2004/11/14 15:29:41 kardel 716182007Sroberto * support PPSAPI, upgrade Copyright to Berkeley style 717182007Sroberto * 71856746Sroberto * Revision 4.9 1999/12/06 13:42:23 kardel 71956746Sroberto * transfer correctly converted time codes always into tcode 72056746Sroberto * 72156746Sroberto * Revision 4.8 1999/11/28 09:13:50 kardel 72256746Sroberto * RECON_4_0_98F 72356746Sroberto * 72456746Sroberto * Revision 4.7 1999/04/01 20:07:20 kardel 72556746Sroberto * added checking for minutie increment of timestamps in clk_rawdcf.c 72656746Sroberto * 72754359Sroberto * Revision 4.6 1998/06/14 21:09:37 kardel 72854359Sroberto * Sun acc cleanup 72954359Sroberto * 73054359Sroberto * Revision 4.5 1998/06/13 12:04:16 kardel 73154359Sroberto * fix SYSV clock name clash 73254359Sroberto * 73354359Sroberto * Revision 4.4 1998/06/12 15:22:28 kardel 73454359Sroberto * fix prototypes 73554359Sroberto * 73654359Sroberto * Revision 4.3 1998/06/06 18:33:36 kardel 73754359Sroberto * simplified condidional compile expression 73854359Sroberto * 73954359Sroberto * Revision 4.2 1998/05/24 11:04:18 kardel 74054359Sroberto * triggering PPS on negative edge for simpler wiring (Rx->DCD) 74154359Sroberto * 74254359Sroberto * Revision 4.1 1998/05/24 09:39:53 kardel 74354359Sroberto * implementation of the new IO handling model 74454359Sroberto * 74554359Sroberto * Revision 4.0 1998/04/10 19:45:30 kardel 74654359Sroberto * Start 4.0 release version numbering 74754359Sroberto * 74854359Sroberto * from V3 3.24 log info deleted 1998/04/11 kardel 74954359Sroberto * 75054359Sroberto */ 751