1*eabc0478Schristos /* $NetBSD: dcfd.c,v 1.7 2024/08/18 20:47:19 christos Exp $ */ 2abb0f93cSkardel 3abb0f93cSkardel /* 4abb0f93cSkardel * /src/NTP/REPOSITORY/ntp4-dev/parseutil/dcfd.c,v 4.18 2005/10/07 22:08:18 kardel RELEASE_20051008_A 5abb0f93cSkardel * 6abb0f93cSkardel * dcfd.c,v 4.18 2005/10/07 22:08:18 kardel RELEASE_20051008_A 7abb0f93cSkardel * 8abb0f93cSkardel * DCF77 100/200ms pulse synchronisation daemon program (via 50Baud serial line) 9abb0f93cSkardel * 10abb0f93cSkardel * Features: 11abb0f93cSkardel * DCF77 decoding 12abb0f93cSkardel * simple NTP loopfilter logic for local clock 13abb0f93cSkardel * interactive display for debugging 14abb0f93cSkardel * 15abb0f93cSkardel * Lacks: 16abb0f93cSkardel * Leap second handling (at that level you should switch to NTP Version 4 - really!) 17abb0f93cSkardel * 185d681e99Schristos * Copyright (c) 1995-2015 by Frank Kardel <kardel <AT> ntp.org> 197476e6e4Schristos * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany 20abb0f93cSkardel * 21abb0f93cSkardel * Redistribution and use in source and binary forms, with or without 22abb0f93cSkardel * modification, are permitted provided that the following conditions 23abb0f93cSkardel * are met: 24abb0f93cSkardel * 1. Redistributions of source code must retain the above copyright 25abb0f93cSkardel * notice, this list of conditions and the following disclaimer. 26abb0f93cSkardel * 2. Redistributions in binary form must reproduce the above copyright 27abb0f93cSkardel * notice, this list of conditions and the following disclaimer in the 28abb0f93cSkardel * documentation and/or other materials provided with the distribution. 29abb0f93cSkardel * 3. Neither the name of the author nor the names of its contributors 30abb0f93cSkardel * may be used to endorse or promote products derived from this software 31abb0f93cSkardel * without specific prior written permission. 32abb0f93cSkardel * 33abb0f93cSkardel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 34abb0f93cSkardel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 35abb0f93cSkardel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36abb0f93cSkardel * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 37abb0f93cSkardel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 38abb0f93cSkardel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 39abb0f93cSkardel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40abb0f93cSkardel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 41abb0f93cSkardel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 42abb0f93cSkardel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43abb0f93cSkardel * SUCH DAMAGE. 44abb0f93cSkardel * 45abb0f93cSkardel */ 46abb0f93cSkardel 47abb0f93cSkardel #ifdef HAVE_CONFIG_H 48abb0f93cSkardel # include <config.h> 49abb0f93cSkardel #endif 50abb0f93cSkardel 51abb0f93cSkardel #include <sys/ioctl.h> 52abb0f93cSkardel #include <unistd.h> 53abb0f93cSkardel #include <stdio.h> 54abb0f93cSkardel #include <fcntl.h> 55abb0f93cSkardel #include <sys/types.h> 56abb0f93cSkardel #include <sys/time.h> 57abb0f93cSkardel #include <signal.h> 58abb0f93cSkardel #include <syslog.h> 59abb0f93cSkardel #include <time.h> 60abb0f93cSkardel 61abb0f93cSkardel /* 62abb0f93cSkardel * NTP compilation environment 63abb0f93cSkardel */ 64abb0f93cSkardel #include "ntp_stdlib.h" 65abb0f93cSkardel #include "ntpd.h" /* indirectly include ntp.h to get YEAR_PIVOT Y2KFixes */ 66abb0f93cSkardel 67abb0f93cSkardel /* 68abb0f93cSkardel * select which terminal handling to use (currently only SysV variants) 69abb0f93cSkardel */ 70abb0f93cSkardel #if defined(HAVE_TERMIOS_H) || defined(STREAM) 71abb0f93cSkardel #include <termios.h> 72abb0f93cSkardel #define TTY_GETATTR(_FD_, _ARG_) tcgetattr((_FD_), (_ARG_)) 73abb0f93cSkardel #define TTY_SETATTR(_FD_, _ARG_) tcsetattr((_FD_), TCSANOW, (_ARG_)) 74abb0f93cSkardel #else /* not HAVE_TERMIOS_H || STREAM */ 75abb0f93cSkardel # if defined(HAVE_TERMIO_H) || defined(HAVE_SYSV_TTYS) 76abb0f93cSkardel # include <termio.h> 77abb0f93cSkardel # define TTY_GETATTR(_FD_, _ARG_) ioctl((_FD_), TCGETA, (_ARG_)) 78abb0f93cSkardel # define TTY_SETATTR(_FD_, _ARG_) ioctl((_FD_), TCSETAW, (_ARG_)) 79abb0f93cSkardel # endif/* HAVE_TERMIO_H || HAVE_SYSV_TTYS */ 80abb0f93cSkardel #endif /* not HAVE_TERMIOS_H || STREAM */ 81abb0f93cSkardel 82abb0f93cSkardel 83abb0f93cSkardel #ifndef TTY_GETATTR 84abb0f93cSkardel #include "Bletch: MUST DEFINE ONE OF 'HAVE_TERMIOS_H' or 'HAVE_TERMIO_H'" 85abb0f93cSkardel #endif 86abb0f93cSkardel 87abb0f93cSkardel #ifndef days_per_year 88abb0f93cSkardel #define days_per_year(_x_) (((_x_) % 4) ? 365 : (((_x_) % 400) ? 365 : 366)) 89abb0f93cSkardel #endif 90abb0f93cSkardel 91abb0f93cSkardel #define timernormalize(_a_) \ 92abb0f93cSkardel if ((_a_)->tv_usec >= 1000000) \ 93abb0f93cSkardel { \ 94abb0f93cSkardel (_a_)->tv_sec += (_a_)->tv_usec / 1000000; \ 95abb0f93cSkardel (_a_)->tv_usec = (_a_)->tv_usec % 1000000; \ 96abb0f93cSkardel } \ 97abb0f93cSkardel if ((_a_)->tv_usec < 0) \ 98abb0f93cSkardel { \ 99abb0f93cSkardel (_a_)->tv_sec -= 1 + (-(_a_)->tv_usec / 1000000); \ 100abb0f93cSkardel (_a_)->tv_usec = 999999 - (-(_a_)->tv_usec - 1); \ 101abb0f93cSkardel } 102abb0f93cSkardel 103abb0f93cSkardel #ifdef timeradd 104abb0f93cSkardel #undef timeradd 105abb0f93cSkardel #endif 106abb0f93cSkardel #define timeradd(_a_, _b_) \ 107abb0f93cSkardel (_a_)->tv_sec += (_b_)->tv_sec; \ 108abb0f93cSkardel (_a_)->tv_usec += (_b_)->tv_usec; \ 109abb0f93cSkardel timernormalize((_a_)) 110abb0f93cSkardel 111abb0f93cSkardel #ifdef timersub 112abb0f93cSkardel #undef timersub 113abb0f93cSkardel #endif 114abb0f93cSkardel #define timersub(_a_, _b_) \ 115abb0f93cSkardel (_a_)->tv_sec -= (_b_)->tv_sec; \ 116abb0f93cSkardel (_a_)->tv_usec -= (_b_)->tv_usec; \ 117abb0f93cSkardel timernormalize((_a_)) 118abb0f93cSkardel 119abb0f93cSkardel /* 120abb0f93cSkardel * debug macros 121abb0f93cSkardel */ 122abb0f93cSkardel #define PRINTF if (interactive) printf 123abb0f93cSkardel #define LPRINTF if (interactive && loop_filter_debug) printf 124abb0f93cSkardel 125abb0f93cSkardel #ifdef DEBUG 126cdfa2a7eSchristos #define DPRINTF(_x_) LPRINTF _x_ 127abb0f93cSkardel #else 128cdfa2a7eSchristos #define DPRINTF(_x_) 129abb0f93cSkardel #endif 130abb0f93cSkardel 131abb0f93cSkardel #ifdef DECL_ERRNO 132abb0f93cSkardel extern int errno; 133abb0f93cSkardel #endif 134abb0f93cSkardel 135abb0f93cSkardel static char *revision = "4.18"; 136abb0f93cSkardel 137abb0f93cSkardel /* 138abb0f93cSkardel * display received data (avoids also detaching from tty) 139abb0f93cSkardel */ 140abb0f93cSkardel static int interactive = 0; 141abb0f93cSkardel 142abb0f93cSkardel /* 143abb0f93cSkardel * display loopfilter (clock control) variables 144abb0f93cSkardel */ 145abb0f93cSkardel static int loop_filter_debug = 0; 146abb0f93cSkardel 147abb0f93cSkardel /* 148abb0f93cSkardel * do not set/adjust system time 149abb0f93cSkardel */ 150abb0f93cSkardel static int no_set = 0; 151abb0f93cSkardel 152abb0f93cSkardel /* 153abb0f93cSkardel * time that passes between start of DCF impulse and time stamping (fine 154abb0f93cSkardel * adjustment) in microseconds (receiver/OS dependent) 155abb0f93cSkardel */ 156abb0f93cSkardel #define DEFAULT_DELAY 230000 /* rough estimate */ 157abb0f93cSkardel 158abb0f93cSkardel /* 159abb0f93cSkardel * The two states we can be in - eithe we receive nothing 160abb0f93cSkardel * usable or we have the correct time 161abb0f93cSkardel */ 162abb0f93cSkardel #define NO_SYNC 0x01 163abb0f93cSkardel #define SYNC 0x02 164abb0f93cSkardel 165abb0f93cSkardel static int sync_state = NO_SYNC; 166abb0f93cSkardel static time_t last_sync; 167abb0f93cSkardel 168abb0f93cSkardel static unsigned long ticks = 0; 169abb0f93cSkardel 170abb0f93cSkardel static char pat[] = "-\\|/"; 171abb0f93cSkardel 172abb0f93cSkardel #define LINES (24-2) /* error lines after which the two headlines are repeated */ 173abb0f93cSkardel 174abb0f93cSkardel #define MAX_UNSYNC (10*60) /* allow synchronisation loss for 10 minutes */ 175abb0f93cSkardel #define NOTICE_INTERVAL (20*60) /* mention missing synchronisation every 20 minutes */ 176abb0f93cSkardel 177abb0f93cSkardel /* 178abb0f93cSkardel * clock adjustment PLL - see NTP protocol spec (RFC1305) for details 179abb0f93cSkardel */ 180abb0f93cSkardel 181abb0f93cSkardel #define USECSCALE 10 182abb0f93cSkardel #define TIMECONSTANT 2 183abb0f93cSkardel #define ADJINTERVAL 0 184abb0f93cSkardel #define FREQ_WEIGHT 18 185abb0f93cSkardel #define PHASE_WEIGHT 7 186abb0f93cSkardel #define MAX_DRIFT 0x3FFFFFFF 187abb0f93cSkardel 188abb0f93cSkardel #define R_SHIFT(_X_, _Y_) (((_X_) < 0) ? -(-(_X_) >> (_Y_)) : ((_X_) >> (_Y_))) 189abb0f93cSkardel 190abb0f93cSkardel static long max_adj_offset_usec = 128000; 191abb0f93cSkardel 192abb0f93cSkardel static long clock_adjust = 0; /* current adjustment value (usec * 2^USECSCALE) */ 193abb0f93cSkardel static long accum_drift = 0; /* accumulated drift value (usec / ADJINTERVAL) */ 194abb0f93cSkardel static long adjustments = 0; 195abb0f93cSkardel static char skip_adjust = 1; /* discard first adjustment (bad samples) */ 196abb0f93cSkardel 197abb0f93cSkardel /* 198abb0f93cSkardel * DCF77 state flags 199abb0f93cSkardel */ 200abb0f93cSkardel #define DCFB_ANNOUNCE 0x0001 /* switch time zone warning (DST switch) */ 201abb0f93cSkardel #define DCFB_DST 0x0002 /* DST in effect */ 202abb0f93cSkardel #define DCFB_LEAP 0x0004 /* LEAP warning (1 hour prior to occurrence) */ 2037476e6e4Schristos #define DCFB_CALLBIT 0x0008 /* "call bit" used to signalize irregularities in the control facilities */ 204abb0f93cSkardel 205abb0f93cSkardel struct clocktime /* clock time broken up from time code */ 206abb0f93cSkardel { 207abb0f93cSkardel long wday; /* Day of week: 1: Monday - 7: Sunday */ 208abb0f93cSkardel long day; 209abb0f93cSkardel long month; 210abb0f93cSkardel long year; 211abb0f93cSkardel long hour; 212abb0f93cSkardel long minute; 213abb0f93cSkardel long second; 214abb0f93cSkardel long usecond; 215abb0f93cSkardel long utcoffset; /* in minutes */ 216abb0f93cSkardel long flags; /* current clock status (DCF77 state flags) */ 217abb0f93cSkardel }; 218abb0f93cSkardel 219abb0f93cSkardel typedef struct clocktime clocktime_t; 220abb0f93cSkardel 221abb0f93cSkardel /* 222abb0f93cSkardel * (usually) quick constant multiplications 223abb0f93cSkardel */ 2247476e6e4Schristos #ifndef TIMES10 225abb0f93cSkardel #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1)) /* *8 + *2 */ 2267476e6e4Schristos #endif 2277476e6e4Schristos #ifndef TIMES24 228abb0f93cSkardel #define TIMES24(_X_) (((_X_) << 4) + ((_X_) << 3)) /* *16 + *8 */ 2297476e6e4Schristos #endif 2307476e6e4Schristos #ifndef TIMES60 231abb0f93cSkardel #define TIMES60(_X_) ((((_X_) << 4) - (_X_)) << 2) /* *(16 - 1) *4 */ 2327476e6e4Schristos #endif 2337476e6e4Schristos 234abb0f93cSkardel /* 235abb0f93cSkardel * generic l_abs() function 236abb0f93cSkardel */ 237abb0f93cSkardel #define l_abs(_x_) (((_x_) < 0) ? -(_x_) : (_x_)) 238abb0f93cSkardel 239abb0f93cSkardel /* 240abb0f93cSkardel * conversion related return/error codes 241abb0f93cSkardel */ 242abb0f93cSkardel #define CVT_MASK 0x0000000F /* conversion exit code */ 243abb0f93cSkardel #define CVT_NONE 0x00000001 /* format not applicable */ 244abb0f93cSkardel #define CVT_FAIL 0x00000002 /* conversion failed - error code returned */ 245abb0f93cSkardel #define CVT_OK 0x00000004 /* conversion succeeded */ 246abb0f93cSkardel #define CVT_BADFMT 0x00000010 /* general format error - (unparsable) */ 247abb0f93cSkardel #define CVT_BADDATE 0x00000020 /* invalid date */ 248abb0f93cSkardel #define CVT_BADTIME 0x00000040 /* invalid time */ 249abb0f93cSkardel 250abb0f93cSkardel /* 251abb0f93cSkardel * DCF77 raw time code 252abb0f93cSkardel * 253abb0f93cSkardel * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig 254abb0f93cSkardel * und Berlin, Maerz 1989 255abb0f93cSkardel * 256abb0f93cSkardel * Timecode transmission: 257abb0f93cSkardel * AM: 258abb0f93cSkardel * time marks are send every second except for the second before the 259abb0f93cSkardel * next minute mark 260abb0f93cSkardel * time marks consist of a reduction of transmitter power to 25% 261abb0f93cSkardel * of the nominal level 262abb0f93cSkardel * the falling edge is the time indication (on time) 263abb0f93cSkardel * time marks of a 100ms duration constitute a logical 0 264abb0f93cSkardel * time marks of a 200ms duration constitute a logical 1 265abb0f93cSkardel * FM: 266abb0f93cSkardel * see the spec. (basically a (non-)inverted psuedo random phase shift) 267abb0f93cSkardel * 268abb0f93cSkardel * Encoding: 269abb0f93cSkardel * Second Contents 270abb0f93cSkardel * 0 - 10 AM: free, FM: 0 271abb0f93cSkardel * 11 - 14 free 2727476e6e4Schristos * 15 R - "call bit" used to signalize irregularities in the control facilities 2737476e6e4Schristos * (until 2003 indicated transmission via alternate antenna) 274abb0f93cSkardel * 16 A1 - expect zone change (1 hour before) 275abb0f93cSkardel * 17 - 18 Z1,Z2 - time zone 276abb0f93cSkardel * 0 0 illegal 277abb0f93cSkardel * 0 1 MEZ (MET) 278abb0f93cSkardel * 1 0 MESZ (MED, MET DST) 279abb0f93cSkardel * 1 1 illegal 280abb0f93cSkardel * 19 A2 - expect leap insertion/deletion (1 hour before) 281abb0f93cSkardel * 20 S - start of time code (1) 282abb0f93cSkardel * 21 - 24 M1 - BCD (lsb first) Minutes 283abb0f93cSkardel * 25 - 27 M10 - BCD (lsb first) 10 Minutes 284abb0f93cSkardel * 28 P1 - Minute Parity (even) 285abb0f93cSkardel * 29 - 32 H1 - BCD (lsb first) Hours 286abb0f93cSkardel * 33 - 34 H10 - BCD (lsb first) 10 Hours 287abb0f93cSkardel * 35 P2 - Hour Parity (even) 288abb0f93cSkardel * 36 - 39 D1 - BCD (lsb first) Days 289abb0f93cSkardel * 40 - 41 D10 - BCD (lsb first) 10 Days 290abb0f93cSkardel * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday) 291abb0f93cSkardel * 45 - 49 MO - BCD (lsb first) Month 292abb0f93cSkardel * 50 MO0 - 10 Months 293abb0f93cSkardel * 51 - 53 Y1 - BCD (lsb first) Years 294abb0f93cSkardel * 54 - 57 Y10 - BCD (lsb first) 10 Years 295abb0f93cSkardel * 58 P3 - Date Parity (even) 296abb0f93cSkardel * 59 - usually missing (minute indication), except for leap insertion 297abb0f93cSkardel */ 298abb0f93cSkardel 299abb0f93cSkardel /*----------------------------------------------------------------------- 300abb0f93cSkardel * conversion table to map DCF77 bit stream into data fields. 301abb0f93cSkardel * Encoding: 302abb0f93cSkardel * Each field of the DCF77 code is described with two adjacent entries in 303abb0f93cSkardel * this table. The first entry specifies the offset into the DCF77 data stream 304abb0f93cSkardel * while the length is given as the difference between the start index and 305abb0f93cSkardel * the start index of the following field. 306abb0f93cSkardel */ 307abb0f93cSkardel static struct rawdcfcode 308abb0f93cSkardel { 309abb0f93cSkardel char offset; /* start bit */ 310abb0f93cSkardel } rawdcfcode[] = 311abb0f93cSkardel { 312abb0f93cSkardel { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 }, 313abb0f93cSkardel { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 } 314abb0f93cSkardel }; 315abb0f93cSkardel 316abb0f93cSkardel /*----------------------------------------------------------------------- 317abb0f93cSkardel * symbolic names for the fields of DCF77 describes in "rawdcfcode". 318abb0f93cSkardel * see comment above for the structure of the DCF77 data 319abb0f93cSkardel */ 320abb0f93cSkardel #define DCF_M 0 321abb0f93cSkardel #define DCF_R 1 322abb0f93cSkardel #define DCF_A1 2 323abb0f93cSkardel #define DCF_Z 3 324abb0f93cSkardel #define DCF_A2 4 325abb0f93cSkardel #define DCF_S 5 326abb0f93cSkardel #define DCF_M1 6 327abb0f93cSkardel #define DCF_M10 7 328abb0f93cSkardel #define DCF_P1 8 329abb0f93cSkardel #define DCF_H1 9 330abb0f93cSkardel #define DCF_H10 10 331abb0f93cSkardel #define DCF_P2 11 332abb0f93cSkardel #define DCF_D1 12 333abb0f93cSkardel #define DCF_D10 13 334abb0f93cSkardel #define DCF_DW 14 335abb0f93cSkardel #define DCF_MO 15 336abb0f93cSkardel #define DCF_MO0 16 337abb0f93cSkardel #define DCF_Y1 17 338abb0f93cSkardel #define DCF_Y10 18 339abb0f93cSkardel #define DCF_P3 19 340abb0f93cSkardel 341abb0f93cSkardel /*----------------------------------------------------------------------- 342abb0f93cSkardel * parity field table (same encoding as rawdcfcode) 343abb0f93cSkardel * This table describes the sections of the DCF77 code that are 344abb0f93cSkardel * parity protected 345abb0f93cSkardel */ 346abb0f93cSkardel static struct partab 347abb0f93cSkardel { 348abb0f93cSkardel char offset; /* start bit of parity field */ 349abb0f93cSkardel } partab[] = 350abb0f93cSkardel { 351abb0f93cSkardel { 21 }, { 29 }, { 36 }, { 59 } 352abb0f93cSkardel }; 353abb0f93cSkardel 354abb0f93cSkardel /*----------------------------------------------------------------------- 355abb0f93cSkardel * offsets for parity field descriptions 356abb0f93cSkardel */ 357abb0f93cSkardel #define DCF_P_P1 0 358abb0f93cSkardel #define DCF_P_P2 1 359abb0f93cSkardel #define DCF_P_P3 2 360abb0f93cSkardel 361abb0f93cSkardel /*----------------------------------------------------------------------- 362abb0f93cSkardel * legal values for time zone information 363abb0f93cSkardel */ 364abb0f93cSkardel #define DCF_Z_MET 0x2 365abb0f93cSkardel #define DCF_Z_MED 0x1 366abb0f93cSkardel 367abb0f93cSkardel /*----------------------------------------------------------------------- 368abb0f93cSkardel * symbolic representation if the DCF77 data stream 369abb0f93cSkardel */ 370abb0f93cSkardel static struct dcfparam 371abb0f93cSkardel { 372abb0f93cSkardel unsigned char onebits[60]; 373abb0f93cSkardel unsigned char zerobits[60]; 374abb0f93cSkardel } dcfparam = 375abb0f93cSkardel { 376abb0f93cSkardel "###############RADMLS1248124P124812P1248121241248112481248P", /* 'ONE' representation */ 377abb0f93cSkardel "--------------------s-------p------p----------------------p" /* 'ZERO' representation */ 378abb0f93cSkardel }; 379abb0f93cSkardel 380abb0f93cSkardel /*----------------------------------------------------------------------- 381abb0f93cSkardel * extract a bitfield from DCF77 datastream 382abb0f93cSkardel * All numeric fields are LSB first. 383abb0f93cSkardel * buf holds a pointer to a DCF77 data buffer in symbolic 384abb0f93cSkardel * representation 385abb0f93cSkardel * idx holds the index to the field description in rawdcfcode 386abb0f93cSkardel */ 387abb0f93cSkardel static unsigned long 388abb0f93cSkardel ext_bf( 389abb0f93cSkardel register unsigned char *buf, 390abb0f93cSkardel register int idx 391abb0f93cSkardel ) 392abb0f93cSkardel { 393abb0f93cSkardel register unsigned long sum = 0; 394abb0f93cSkardel register int i, first; 395abb0f93cSkardel 396abb0f93cSkardel first = rawdcfcode[idx].offset; 397abb0f93cSkardel 398abb0f93cSkardel for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--) 399abb0f93cSkardel { 400abb0f93cSkardel sum <<= 1; 401abb0f93cSkardel sum |= (buf[i] != dcfparam.zerobits[i]); 402abb0f93cSkardel } 403abb0f93cSkardel return sum; 404abb0f93cSkardel } 405abb0f93cSkardel 406abb0f93cSkardel /*----------------------------------------------------------------------- 407abb0f93cSkardel * check even parity integrity for a bitfield 408abb0f93cSkardel * 409abb0f93cSkardel * buf holds a pointer to a DCF77 data buffer in symbolic 410abb0f93cSkardel * representation 411abb0f93cSkardel * idx holds the index to the field description in partab 412abb0f93cSkardel */ 413abb0f93cSkardel static unsigned 414abb0f93cSkardel pcheck( 415abb0f93cSkardel register unsigned char *buf, 416abb0f93cSkardel register int idx 417abb0f93cSkardel ) 418abb0f93cSkardel { 419abb0f93cSkardel register int i,last; 420abb0f93cSkardel register unsigned psum = 1; 421abb0f93cSkardel 422abb0f93cSkardel last = partab[idx+1].offset; 423abb0f93cSkardel 424abb0f93cSkardel for (i = partab[idx].offset; i < last; i++) 425abb0f93cSkardel psum ^= (buf[i] != dcfparam.zerobits[i]); 426abb0f93cSkardel 427abb0f93cSkardel return psum; 428abb0f93cSkardel } 429abb0f93cSkardel 430abb0f93cSkardel /*----------------------------------------------------------------------- 431abb0f93cSkardel * convert a DCF77 data buffer into wall clock time + flags 432abb0f93cSkardel * 433abb0f93cSkardel * buffer holds a pointer to a DCF77 data buffer in symbolic 434abb0f93cSkardel * representation 435abb0f93cSkardel * size describes the length of DCF77 information in bits (represented 436abb0f93cSkardel * as chars in symbolic notation 437abb0f93cSkardel * clock points to a wall clock time description of the DCF77 data (result) 438abb0f93cSkardel */ 439abb0f93cSkardel static unsigned long 440abb0f93cSkardel convert_rawdcf( 441abb0f93cSkardel unsigned char *buffer, 442abb0f93cSkardel int size, 443abb0f93cSkardel clocktime_t *clock_time 444abb0f93cSkardel ) 445abb0f93cSkardel { 446abb0f93cSkardel if (size < 57) 447abb0f93cSkardel { 448abb0f93cSkardel PRINTF("%-30s", "*** INCOMPLETE"); 449abb0f93cSkardel return CVT_NONE; 450abb0f93cSkardel } 451abb0f93cSkardel 452abb0f93cSkardel /* 453abb0f93cSkardel * check Start and Parity bits 454abb0f93cSkardel */ 455abb0f93cSkardel if ((ext_bf(buffer, DCF_S) == 1) && 456abb0f93cSkardel pcheck(buffer, DCF_P_P1) && 457abb0f93cSkardel pcheck(buffer, DCF_P_P2) && 458abb0f93cSkardel pcheck(buffer, DCF_P_P3)) 459abb0f93cSkardel { 460abb0f93cSkardel /* 461abb0f93cSkardel * buffer OK - extract all fields and build wall clock time from them 462abb0f93cSkardel */ 463abb0f93cSkardel 464abb0f93cSkardel clock_time->flags = 0; 465abb0f93cSkardel clock_time->usecond= 0; 466abb0f93cSkardel clock_time->second = 0; 467abb0f93cSkardel clock_time->minute = ext_bf(buffer, DCF_M10); 468abb0f93cSkardel clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1); 469abb0f93cSkardel clock_time->hour = ext_bf(buffer, DCF_H10); 470abb0f93cSkardel clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1); 471abb0f93cSkardel clock_time->day = ext_bf(buffer, DCF_D10); 472abb0f93cSkardel clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1); 473abb0f93cSkardel clock_time->month = ext_bf(buffer, DCF_MO0); 474abb0f93cSkardel clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO); 475abb0f93cSkardel clock_time->year = ext_bf(buffer, DCF_Y10); 476abb0f93cSkardel clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1); 477abb0f93cSkardel clock_time->wday = ext_bf(buffer, DCF_DW); 478abb0f93cSkardel 479abb0f93cSkardel /* 480abb0f93cSkardel * determine offset to UTC by examining the time zone 481abb0f93cSkardel */ 482abb0f93cSkardel switch (ext_bf(buffer, DCF_Z)) 483abb0f93cSkardel { 484abb0f93cSkardel case DCF_Z_MET: 485abb0f93cSkardel clock_time->utcoffset = -60; 486abb0f93cSkardel break; 487abb0f93cSkardel 488abb0f93cSkardel case DCF_Z_MED: 489abb0f93cSkardel clock_time->flags |= DCFB_DST; 490abb0f93cSkardel clock_time->utcoffset = -120; 491abb0f93cSkardel break; 492abb0f93cSkardel 493abb0f93cSkardel default: 494abb0f93cSkardel PRINTF("%-30s", "*** BAD TIME ZONE"); 495abb0f93cSkardel return CVT_FAIL|CVT_BADFMT; 496abb0f93cSkardel } 497abb0f93cSkardel 498abb0f93cSkardel /* 499abb0f93cSkardel * extract various warnings from DCF77 500abb0f93cSkardel */ 501abb0f93cSkardel if (ext_bf(buffer, DCF_A1)) 502abb0f93cSkardel clock_time->flags |= DCFB_ANNOUNCE; 503abb0f93cSkardel 504abb0f93cSkardel if (ext_bf(buffer, DCF_A2)) 505abb0f93cSkardel clock_time->flags |= DCFB_LEAP; 506abb0f93cSkardel 507abb0f93cSkardel if (ext_bf(buffer, DCF_R)) 5087476e6e4Schristos clock_time->flags |= DCFB_CALLBIT; 509abb0f93cSkardel 510abb0f93cSkardel return CVT_OK; 511abb0f93cSkardel } 512abb0f93cSkardel else 513abb0f93cSkardel { 514abb0f93cSkardel /* 515abb0f93cSkardel * bad format - not for us 516abb0f93cSkardel */ 517abb0f93cSkardel PRINTF("%-30s", "*** BAD FORMAT (invalid/parity)"); 518abb0f93cSkardel return CVT_FAIL|CVT_BADFMT; 519abb0f93cSkardel } 520abb0f93cSkardel } 521abb0f93cSkardel 522abb0f93cSkardel /*----------------------------------------------------------------------- 523abb0f93cSkardel * raw dcf input routine - fix up 50 baud 524abb0f93cSkardel * characters for 1/0 decision 525abb0f93cSkardel */ 526abb0f93cSkardel static unsigned long 527abb0f93cSkardel cvt_rawdcf( 528abb0f93cSkardel unsigned char *buffer, 529abb0f93cSkardel int size, 530abb0f93cSkardel clocktime_t *clock_time 531abb0f93cSkardel ) 532abb0f93cSkardel { 533abb0f93cSkardel register unsigned char *s = buffer; 534abb0f93cSkardel register unsigned char *e = buffer + size; 535abb0f93cSkardel register unsigned char *b = dcfparam.onebits; 536abb0f93cSkardel register unsigned char *c = dcfparam.zerobits; 537abb0f93cSkardel register unsigned rtc = CVT_NONE; 538abb0f93cSkardel register unsigned int i, lowmax, highmax, cutoff, span; 539abb0f93cSkardel #define BITS 9 540abb0f93cSkardel unsigned char histbuf[BITS]; 541abb0f93cSkardel /* 542abb0f93cSkardel * the input buffer contains characters with runs of consecutive 543abb0f93cSkardel * bits set. These set bits are an indication of the DCF77 pulse 544abb0f93cSkardel * length. We assume that we receive the pulse at 50 Baud. Thus 545abb0f93cSkardel * a 100ms pulse would generate a 4 bit train (20ms per bit and 546abb0f93cSkardel * start bit) 547abb0f93cSkardel * a 200ms pulse would create all zeroes (and probably a frame error) 548abb0f93cSkardel * 549abb0f93cSkardel * The basic idea is that on corret reception we must have two 550abb0f93cSkardel * maxima in the pulse length distribution histogram. (one for 551abb0f93cSkardel * the zero representing pulses and one for the one representing 552abb0f93cSkardel * pulses) 553abb0f93cSkardel * There will always be ones in the datastream, thus we have to see 554abb0f93cSkardel * two maxima. 555abb0f93cSkardel * The best point to cut for a 1/0 decision is the minimum between those 556abb0f93cSkardel * between the maxima. The following code tries to find this cutoff point. 557abb0f93cSkardel */ 558abb0f93cSkardel 559abb0f93cSkardel /* 560abb0f93cSkardel * clear histogram buffer 561abb0f93cSkardel */ 562abb0f93cSkardel for (i = 0; i < BITS; i++) 563abb0f93cSkardel { 564abb0f93cSkardel histbuf[i] = 0; 565abb0f93cSkardel } 566abb0f93cSkardel 567abb0f93cSkardel cutoff = 0; 568abb0f93cSkardel lowmax = 0; 569abb0f93cSkardel 570abb0f93cSkardel /* 571abb0f93cSkardel * convert sequences of set bits into bits counts updating 572abb0f93cSkardel * the histogram alongway 573abb0f93cSkardel */ 574abb0f93cSkardel while (s < e) 575abb0f93cSkardel { 576abb0f93cSkardel register unsigned int ch = *s ^ 0xFF; 577abb0f93cSkardel /* 578abb0f93cSkardel * check integrity and update histogramm 579abb0f93cSkardel */ 580abb0f93cSkardel if (!((ch+1) & ch) || !*s) 581abb0f93cSkardel { 582abb0f93cSkardel /* 583abb0f93cSkardel * character ok 584abb0f93cSkardel */ 585abb0f93cSkardel for (i = 0; ch; i++) 586abb0f93cSkardel { 587abb0f93cSkardel ch >>= 1; 588abb0f93cSkardel } 589abb0f93cSkardel 590abb0f93cSkardel *s = i; 591abb0f93cSkardel histbuf[i]++; 592abb0f93cSkardel cutoff += i; 593abb0f93cSkardel lowmax++; 594abb0f93cSkardel } 595abb0f93cSkardel else 596abb0f93cSkardel { 597abb0f93cSkardel /* 598abb0f93cSkardel * invalid character (no consecutive bit sequence) 599abb0f93cSkardel */ 600cdfa2a7eSchristos DPRINTF(("parse: cvt_rawdcf: character check for 0x%x@%ld FAILED\n", 6018585484eSchristos (u_int)*s, (long)(s - buffer))); 602abb0f93cSkardel *s = (unsigned char)~0; 603abb0f93cSkardel rtc = CVT_FAIL|CVT_BADFMT; 604abb0f93cSkardel } 605abb0f93cSkardel s++; 606abb0f93cSkardel } 607abb0f93cSkardel 608abb0f93cSkardel /* 609abb0f93cSkardel * first cutoff estimate (average bit count - must be between both 610abb0f93cSkardel * maxima) 611abb0f93cSkardel */ 612abb0f93cSkardel if (lowmax) 613abb0f93cSkardel { 614abb0f93cSkardel cutoff /= lowmax; 615abb0f93cSkardel } 616abb0f93cSkardel else 617abb0f93cSkardel { 618abb0f93cSkardel cutoff = 4; /* doesn't really matter - it'll fail anyway, but gives error output */ 619abb0f93cSkardel } 620abb0f93cSkardel 621cdfa2a7eSchristos DPRINTF(("parse: cvt_rawdcf: average bit count: %d\n", cutoff)); 622abb0f93cSkardel 623abb0f93cSkardel lowmax = 0; /* weighted sum */ 624abb0f93cSkardel highmax = 0; /* bitcount */ 625abb0f93cSkardel 626abb0f93cSkardel /* 627abb0f93cSkardel * collect weighted sum of lower bits (left of initial guess) 628abb0f93cSkardel */ 629cdfa2a7eSchristos DPRINTF(("parse: cvt_rawdcf: histogram:")); 630abb0f93cSkardel for (i = 0; i <= cutoff; i++) 631abb0f93cSkardel { 632abb0f93cSkardel lowmax += histbuf[i] * i; 633abb0f93cSkardel highmax += histbuf[i]; 634cdfa2a7eSchristos DPRINTF((" %d", histbuf[i])); 635abb0f93cSkardel } 636cdfa2a7eSchristos DPRINTF((" <M>")); 637abb0f93cSkardel 638abb0f93cSkardel /* 639abb0f93cSkardel * round up 640abb0f93cSkardel */ 641abb0f93cSkardel lowmax += highmax / 2; 642abb0f93cSkardel 643abb0f93cSkardel /* 644abb0f93cSkardel * calculate lower bit maximum (weighted sum / bit count) 645abb0f93cSkardel * 646abb0f93cSkardel * avoid divide by zero 647abb0f93cSkardel */ 648abb0f93cSkardel if (highmax) 649abb0f93cSkardel { 650abb0f93cSkardel lowmax /= highmax; 651abb0f93cSkardel } 652abb0f93cSkardel else 653abb0f93cSkardel { 654abb0f93cSkardel lowmax = 0; 655abb0f93cSkardel } 656abb0f93cSkardel 657abb0f93cSkardel highmax = 0; /* weighted sum of upper bits counts */ 658abb0f93cSkardel cutoff = 0; /* bitcount */ 659abb0f93cSkardel 660abb0f93cSkardel /* 661abb0f93cSkardel * collect weighted sum of lower bits (right of initial guess) 662abb0f93cSkardel */ 663abb0f93cSkardel for (; i < BITS; i++) 664abb0f93cSkardel { 665abb0f93cSkardel highmax+=histbuf[i] * i; 666abb0f93cSkardel cutoff +=histbuf[i]; 667cdfa2a7eSchristos DPRINTF((" %d", histbuf[i])); 668abb0f93cSkardel } 669cdfa2a7eSchristos DPRINTF(("\n")); 670abb0f93cSkardel 671abb0f93cSkardel /* 672abb0f93cSkardel * determine upper maximum (weighted sum / bit count) 673abb0f93cSkardel */ 674abb0f93cSkardel if (cutoff) 675abb0f93cSkardel { 676abb0f93cSkardel highmax /= cutoff; 677abb0f93cSkardel } 678abb0f93cSkardel else 679abb0f93cSkardel { 680abb0f93cSkardel highmax = BITS-1; 681abb0f93cSkardel } 682abb0f93cSkardel 683abb0f93cSkardel /* 684abb0f93cSkardel * following now holds: 685abb0f93cSkardel * lowmax <= cutoff(initial guess) <= highmax 686abb0f93cSkardel * best cutoff is the minimum nearest to higher bits 687abb0f93cSkardel */ 688abb0f93cSkardel 689abb0f93cSkardel /* 690abb0f93cSkardel * find the minimum between lowmax and highmax (detecting 691abb0f93cSkardel * possibly a minimum span) 692abb0f93cSkardel */ 693abb0f93cSkardel span = cutoff = lowmax; 694abb0f93cSkardel for (i = lowmax; i <= highmax; i++) 695abb0f93cSkardel { 696abb0f93cSkardel if (histbuf[cutoff] > histbuf[i]) 697abb0f93cSkardel { 698abb0f93cSkardel /* 699abb0f93cSkardel * got a new minimum move beginning of minimum (cutoff) and 700abb0f93cSkardel * end of minimum (span) there 701abb0f93cSkardel */ 702abb0f93cSkardel cutoff = span = i; 703abb0f93cSkardel } 704abb0f93cSkardel else 705abb0f93cSkardel if (histbuf[cutoff] == histbuf[i]) 706abb0f93cSkardel { 707abb0f93cSkardel /* 708abb0f93cSkardel * minimum not better yet - but it spans more than 709abb0f93cSkardel * one bit value - follow it 710abb0f93cSkardel */ 711abb0f93cSkardel span = i; 712abb0f93cSkardel } 713abb0f93cSkardel } 714abb0f93cSkardel 715abb0f93cSkardel /* 716abb0f93cSkardel * cutoff point for 1/0 decision is the middle of the minimum section 717abb0f93cSkardel * in the histogram 718abb0f93cSkardel */ 719abb0f93cSkardel cutoff = (cutoff + span) / 2; 720abb0f93cSkardel 721cdfa2a7eSchristos DPRINTF(("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff)); 722abb0f93cSkardel 723abb0f93cSkardel /* 724abb0f93cSkardel * convert the bit counts to symbolic 1/0 information for data conversion 725abb0f93cSkardel */ 726abb0f93cSkardel s = buffer; 727abb0f93cSkardel while ((s < e) && *c && *b) 728abb0f93cSkardel { 729abb0f93cSkardel if (*s == (unsigned char)~0) 730abb0f93cSkardel { 731abb0f93cSkardel /* 732abb0f93cSkardel * invalid character 733abb0f93cSkardel */ 734abb0f93cSkardel *s = '?'; 735abb0f93cSkardel } 736abb0f93cSkardel else 737abb0f93cSkardel { 738abb0f93cSkardel /* 739abb0f93cSkardel * symbolic 1/0 representation 740abb0f93cSkardel */ 741abb0f93cSkardel *s = (*s >= cutoff) ? *b : *c; 742abb0f93cSkardel } 743abb0f93cSkardel s++; 744abb0f93cSkardel b++; 745abb0f93cSkardel c++; 746abb0f93cSkardel } 747abb0f93cSkardel 748abb0f93cSkardel /* 749abb0f93cSkardel * if everything went well so far return the result of the symbolic 750abb0f93cSkardel * conversion routine else just the accumulated errors 751abb0f93cSkardel */ 752abb0f93cSkardel if (rtc != CVT_NONE) 753abb0f93cSkardel { 754abb0f93cSkardel PRINTF("%-30s", "*** BAD DATA"); 755abb0f93cSkardel } 756abb0f93cSkardel 757abb0f93cSkardel return (rtc == CVT_NONE) ? convert_rawdcf(buffer, size, clock_time) : rtc; 758abb0f93cSkardel } 759abb0f93cSkardel 760abb0f93cSkardel /*----------------------------------------------------------------------- 761abb0f93cSkardel * convert a wall clock time description of DCF77 to a Unix time (seconds 762abb0f93cSkardel * since 1.1. 1970 UTC) 763abb0f93cSkardel */ 764abb0f93cSkardel static time_t 765abb0f93cSkardel dcf_to_unixtime( 766abb0f93cSkardel clocktime_t *clock_time, 767abb0f93cSkardel unsigned *cvtrtc 768abb0f93cSkardel ) 769abb0f93cSkardel { 770abb0f93cSkardel #define SETRTC(_X_) { if (cvtrtc) *cvtrtc = (_X_); } 771abb0f93cSkardel static int days_of_month[] = 772abb0f93cSkardel { 773abb0f93cSkardel 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 774abb0f93cSkardel }; 775abb0f93cSkardel register int i; 776abb0f93cSkardel time_t t; 777abb0f93cSkardel 778abb0f93cSkardel /* 779abb0f93cSkardel * map 2 digit years to 19xx (DCF77 is a 20th century item) 780abb0f93cSkardel */ 781abb0f93cSkardel if ( clock_time->year < YEAR_PIVOT ) /* in case of Y2KFixes [ */ 782abb0f93cSkardel clock_time->year += 100; /* *year%100, make tm_year */ 783abb0f93cSkardel /* *(do we need this?) */ 784abb0f93cSkardel if ( clock_time->year < YEAR_BREAK ) /* (failsafe if) */ 785abb0f93cSkardel clock_time->year += 1900; /* Y2KFixes ] */ 786abb0f93cSkardel 787abb0f93cSkardel /* 788abb0f93cSkardel * must have been a really bad year code - drop it 789abb0f93cSkardel */ 790abb0f93cSkardel if (clock_time->year < (YEAR_PIVOT + 1900) ) /* Y2KFixes */ 791abb0f93cSkardel { 792abb0f93cSkardel SETRTC(CVT_FAIL|CVT_BADDATE); 793abb0f93cSkardel return -1; 794abb0f93cSkardel } 795abb0f93cSkardel /* 796abb0f93cSkardel * sorry, slow section here - but it's not time critical anyway 797abb0f93cSkardel */ 798abb0f93cSkardel 799abb0f93cSkardel /* 800abb0f93cSkardel * calculate days since 1970 (watching leap years) 801abb0f93cSkardel */ 802abb0f93cSkardel t = julian0( clock_time->year ) - julian0( 1970 ); 803abb0f93cSkardel 804abb0f93cSkardel /* month */ 805abb0f93cSkardel if (clock_time->month <= 0 || clock_time->month > 12) 806abb0f93cSkardel { 807abb0f93cSkardel SETRTC(CVT_FAIL|CVT_BADDATE); 808abb0f93cSkardel return -1; /* bad month */ 809abb0f93cSkardel } 810abb0f93cSkardel /* adjust current leap year */ 811abb0f93cSkardel #if 0 812abb0f93cSkardel if (clock_time->month < 3 && days_per_year(clock_time->year) == 366) 813abb0f93cSkardel t--; 814abb0f93cSkardel #endif 815abb0f93cSkardel 816abb0f93cSkardel /* 817abb0f93cSkardel * collect days from months excluding the current one 818abb0f93cSkardel */ 819abb0f93cSkardel for (i = 1; i < clock_time->month; i++) 820abb0f93cSkardel { 821abb0f93cSkardel t += days_of_month[i]; 822abb0f93cSkardel } 823abb0f93cSkardel /* day */ 824abb0f93cSkardel if (clock_time->day < 1 || ((clock_time->month == 2 && days_per_year(clock_time->year) == 366) ? 825abb0f93cSkardel clock_time->day > 29 : clock_time->day > days_of_month[clock_time->month])) 826abb0f93cSkardel { 827abb0f93cSkardel SETRTC(CVT_FAIL|CVT_BADDATE); 828abb0f93cSkardel return -1; /* bad day */ 829abb0f93cSkardel } 830abb0f93cSkardel 831abb0f93cSkardel /* 832abb0f93cSkardel * collect days from date excluding the current one 833abb0f93cSkardel */ 834abb0f93cSkardel t += clock_time->day - 1; 835abb0f93cSkardel 836abb0f93cSkardel /* hour */ 837abb0f93cSkardel if (clock_time->hour < 0 || clock_time->hour >= 24) 838abb0f93cSkardel { 839abb0f93cSkardel SETRTC(CVT_FAIL|CVT_BADTIME); 840abb0f93cSkardel return -1; /* bad hour */ 841abb0f93cSkardel } 842abb0f93cSkardel 843abb0f93cSkardel /* 844abb0f93cSkardel * calculate hours from 1. 1. 1970 845abb0f93cSkardel */ 846abb0f93cSkardel t = TIMES24(t) + clock_time->hour; 847abb0f93cSkardel 848abb0f93cSkardel /* min */ 849abb0f93cSkardel if (clock_time->minute < 0 || clock_time->minute > 59) 850abb0f93cSkardel { 851abb0f93cSkardel SETRTC(CVT_FAIL|CVT_BADTIME); 852abb0f93cSkardel return -1; /* bad min */ 853abb0f93cSkardel } 854abb0f93cSkardel 855abb0f93cSkardel /* 856abb0f93cSkardel * calculate minutes from 1. 1. 1970 857abb0f93cSkardel */ 858abb0f93cSkardel t = TIMES60(t) + clock_time->minute; 859abb0f93cSkardel /* sec */ 860abb0f93cSkardel 861abb0f93cSkardel /* 862abb0f93cSkardel * calculate UTC in minutes 863abb0f93cSkardel */ 864abb0f93cSkardel t += clock_time->utcoffset; 865abb0f93cSkardel 866abb0f93cSkardel if (clock_time->second < 0 || clock_time->second > 60) /* allow for LEAPs */ 867abb0f93cSkardel { 868abb0f93cSkardel SETRTC(CVT_FAIL|CVT_BADTIME); 869abb0f93cSkardel return -1; /* bad sec */ 870abb0f93cSkardel } 871abb0f93cSkardel 872abb0f93cSkardel /* 873abb0f93cSkardel * calculate UTC in seconds - phew ! 874abb0f93cSkardel */ 875abb0f93cSkardel t = TIMES60(t) + clock_time->second; 876abb0f93cSkardel /* done */ 877abb0f93cSkardel return t; 878abb0f93cSkardel } 879abb0f93cSkardel 880abb0f93cSkardel /*----------------------------------------------------------------------- 881abb0f93cSkardel * cheap half baked 1/0 decision - for interactive operation only 882abb0f93cSkardel */ 883abb0f93cSkardel static char 884abb0f93cSkardel type( 885abb0f93cSkardel unsigned int c 886abb0f93cSkardel ) 887abb0f93cSkardel { 888abb0f93cSkardel c ^= 0xFF; 889abb0f93cSkardel return (c > 0xF); 890abb0f93cSkardel } 891abb0f93cSkardel 892abb0f93cSkardel /*----------------------------------------------------------------------- 893abb0f93cSkardel * week day representation 894abb0f93cSkardel */ 895abb0f93cSkardel static const char *wday[8] = 896abb0f93cSkardel { 897abb0f93cSkardel "??", 898abb0f93cSkardel "Mo", 899abb0f93cSkardel "Tu", 900abb0f93cSkardel "We", 901abb0f93cSkardel "Th", 902abb0f93cSkardel "Fr", 903abb0f93cSkardel "Sa", 904abb0f93cSkardel "Su" 905abb0f93cSkardel }; 906abb0f93cSkardel 907abb0f93cSkardel /*----------------------------------------------------------------------- 908abb0f93cSkardel * generate a string representation for a timeval 909abb0f93cSkardel */ 910abb0f93cSkardel static char * 911abb0f93cSkardel pr_timeval( 912abb0f93cSkardel struct timeval *val 913abb0f93cSkardel ) 914abb0f93cSkardel { 915abb0f93cSkardel static char buf[20]; 916abb0f93cSkardel 917abb0f93cSkardel if (val->tv_sec == 0) 9188585484eSchristos snprintf(buf, sizeof(buf), "%c0.%06ld", 9198585484eSchristos (val->tv_usec < 0) ? '-' : '+', 9208585484eSchristos (long int)l_abs(val->tv_usec)); 921abb0f93cSkardel else 9228585484eSchristos snprintf(buf, sizeof(buf), "%ld.%06ld", 9238585484eSchristos (long int)val->tv_sec, 9248585484eSchristos (long int)l_abs(val->tv_usec)); 925abb0f93cSkardel return buf; 926abb0f93cSkardel } 927abb0f93cSkardel 928abb0f93cSkardel /*----------------------------------------------------------------------- 929abb0f93cSkardel * correct the current time by an offset by setting the time rigorously 930abb0f93cSkardel */ 931abb0f93cSkardel static void 932abb0f93cSkardel set_time( 933abb0f93cSkardel struct timeval *offset 934abb0f93cSkardel ) 935abb0f93cSkardel { 936abb0f93cSkardel struct timeval the_time; 937abb0f93cSkardel 938abb0f93cSkardel if (no_set) 939abb0f93cSkardel return; 940abb0f93cSkardel 941abb0f93cSkardel LPRINTF("set_time: %s ", pr_timeval(offset)); 942abb0f93cSkardel syslog(LOG_NOTICE, "setting time (offset %s)", pr_timeval(offset)); 943abb0f93cSkardel 944abb0f93cSkardel if (gettimeofday(&the_time, 0L) == -1) 945abb0f93cSkardel { 946abb0f93cSkardel perror("gettimeofday()"); 947abb0f93cSkardel } 948abb0f93cSkardel else 949abb0f93cSkardel { 950abb0f93cSkardel timeradd(&the_time, offset); 951abb0f93cSkardel if (settimeofday(&the_time, 0L) == -1) 952abb0f93cSkardel { 953abb0f93cSkardel perror("settimeofday()"); 954abb0f93cSkardel } 955abb0f93cSkardel } 956abb0f93cSkardel } 957abb0f93cSkardel 958abb0f93cSkardel /*----------------------------------------------------------------------- 959abb0f93cSkardel * slew the time by a given offset 960abb0f93cSkardel */ 961abb0f93cSkardel static void 962abb0f93cSkardel adj_time( 963abb0f93cSkardel long offset 964abb0f93cSkardel ) 965abb0f93cSkardel { 966abb0f93cSkardel struct timeval time_offset; 967abb0f93cSkardel 968abb0f93cSkardel if (no_set) 969abb0f93cSkardel return; 970abb0f93cSkardel 971abb0f93cSkardel time_offset.tv_sec = offset / 1000000; 972abb0f93cSkardel time_offset.tv_usec = offset % 1000000; 973abb0f93cSkardel 974abb0f93cSkardel LPRINTF("adj_time: %ld us ", (long int)offset); 975abb0f93cSkardel if (adjtime(&time_offset, 0L) == -1) 976abb0f93cSkardel perror("adjtime()"); 977abb0f93cSkardel } 978abb0f93cSkardel 979abb0f93cSkardel /*----------------------------------------------------------------------- 980abb0f93cSkardel * read in a possibly previously written drift value 981abb0f93cSkardel */ 982abb0f93cSkardel static void 983abb0f93cSkardel read_drift( 984abb0f93cSkardel const char *drift_file 985abb0f93cSkardel ) 986abb0f93cSkardel { 987abb0f93cSkardel FILE *df; 988abb0f93cSkardel 989abb0f93cSkardel df = fopen(drift_file, "r"); 990abb0f93cSkardel if (df != NULL) 991abb0f93cSkardel { 992abb0f93cSkardel int idrift = 0, fdrift = 0; 993abb0f93cSkardel 994*eabc0478Schristos if (2 != fscanf(df, "%4d.%03d", &idrift, &fdrift)) 995*eabc0478Schristos LPRINTF("read_drift: trouble reading drift file"); 996abb0f93cSkardel fclose(df); 997abb0f93cSkardel LPRINTF("read_drift: %d.%03d ppm ", idrift, fdrift); 998abb0f93cSkardel 999abb0f93cSkardel accum_drift = idrift << USECSCALE; 1000abb0f93cSkardel fdrift = (fdrift << USECSCALE) / 1000; 1001abb0f93cSkardel accum_drift += fdrift & (1<<USECSCALE); 1002abb0f93cSkardel LPRINTF("read_drift: drift_comp %ld ", (long int)accum_drift); 1003abb0f93cSkardel } 1004abb0f93cSkardel } 1005abb0f93cSkardel 1006abb0f93cSkardel /*----------------------------------------------------------------------- 1007abb0f93cSkardel * write out the current drift value 1008abb0f93cSkardel */ 1009abb0f93cSkardel static void 1010abb0f93cSkardel update_drift( 1011abb0f93cSkardel const char *drift_file, 1012abb0f93cSkardel long offset, 1013abb0f93cSkardel time_t reftime 1014abb0f93cSkardel ) 1015abb0f93cSkardel { 1016abb0f93cSkardel FILE *df; 1017abb0f93cSkardel 1018abb0f93cSkardel df = fopen(drift_file, "w"); 1019abb0f93cSkardel if (df != NULL) 1020abb0f93cSkardel { 1021abb0f93cSkardel int idrift = R_SHIFT(accum_drift, USECSCALE); 1022abb0f93cSkardel int fdrift = accum_drift & ((1<<USECSCALE)-1); 1023abb0f93cSkardel 1024abb0f93cSkardel LPRINTF("update_drift: drift_comp %ld ", (long int)accum_drift); 1025abb0f93cSkardel fdrift = (fdrift * 1000) / (1<<USECSCALE); 1026abb0f93cSkardel fprintf(df, "%4d.%03d %c%ld.%06ld %.24s\n", idrift, fdrift, 1027abb0f93cSkardel (offset < 0) ? '-' : '+', (long int)(l_abs(offset) / 1000000), 1028abb0f93cSkardel (long int)(l_abs(offset) % 1000000), asctime(localtime(&reftime))); 1029abb0f93cSkardel fclose(df); 1030abb0f93cSkardel LPRINTF("update_drift: %d.%03d ppm ", idrift, fdrift); 1031abb0f93cSkardel } 1032abb0f93cSkardel } 1033abb0f93cSkardel 1034abb0f93cSkardel /*----------------------------------------------------------------------- 1035abb0f93cSkardel * process adjustments derived from the DCF77 observation 1036abb0f93cSkardel * (controls clock PLL) 1037abb0f93cSkardel */ 1038abb0f93cSkardel static void 1039abb0f93cSkardel adjust_clock( 1040abb0f93cSkardel struct timeval *offset, 1041abb0f93cSkardel const char *drift_file, 1042abb0f93cSkardel time_t reftime 1043abb0f93cSkardel ) 1044abb0f93cSkardel { 1045abb0f93cSkardel struct timeval toffset; 1046abb0f93cSkardel register long usecoffset; 1047abb0f93cSkardel int tmp; 1048abb0f93cSkardel 1049abb0f93cSkardel if (no_set) 1050abb0f93cSkardel return; 1051abb0f93cSkardel 1052abb0f93cSkardel if (skip_adjust) 1053abb0f93cSkardel { 1054abb0f93cSkardel skip_adjust = 0; 1055abb0f93cSkardel return; 1056abb0f93cSkardel } 1057abb0f93cSkardel 1058abb0f93cSkardel toffset = *offset; 1059abb0f93cSkardel toffset.tv_sec = l_abs(toffset.tv_sec); 1060abb0f93cSkardel toffset.tv_usec = l_abs(toffset.tv_usec); 1061abb0f93cSkardel if (toffset.tv_sec || 1062abb0f93cSkardel (!toffset.tv_sec && toffset.tv_usec > max_adj_offset_usec)) 1063abb0f93cSkardel { 1064abb0f93cSkardel /* 1065abb0f93cSkardel * hopeless - set the clock - and clear the timing 1066abb0f93cSkardel */ 1067abb0f93cSkardel set_time(offset); 1068abb0f93cSkardel clock_adjust = 0; 1069abb0f93cSkardel skip_adjust = 1; 1070abb0f93cSkardel return; 1071abb0f93cSkardel } 1072abb0f93cSkardel 1073abb0f93cSkardel usecoffset = offset->tv_sec * 1000000 + offset->tv_usec; 1074abb0f93cSkardel 1075abb0f93cSkardel clock_adjust = R_SHIFT(usecoffset, TIMECONSTANT); /* adjustment to make for next period */ 1076abb0f93cSkardel 1077abb0f93cSkardel tmp = 0; 1078abb0f93cSkardel while (adjustments > (1 << tmp)) 1079abb0f93cSkardel tmp++; 1080abb0f93cSkardel adjustments = 0; 1081abb0f93cSkardel if (tmp > FREQ_WEIGHT) 1082abb0f93cSkardel tmp = FREQ_WEIGHT; 1083abb0f93cSkardel 1084abb0f93cSkardel accum_drift += R_SHIFT(usecoffset << USECSCALE, TIMECONSTANT+TIMECONSTANT+FREQ_WEIGHT-tmp); 1085abb0f93cSkardel 1086abb0f93cSkardel if (accum_drift > MAX_DRIFT) /* clamp into interval */ 1087abb0f93cSkardel accum_drift = MAX_DRIFT; 1088abb0f93cSkardel else 1089abb0f93cSkardel if (accum_drift < -MAX_DRIFT) 1090abb0f93cSkardel accum_drift = -MAX_DRIFT; 1091abb0f93cSkardel 1092abb0f93cSkardel update_drift(drift_file, usecoffset, reftime); 1093abb0f93cSkardel LPRINTF("clock_adjust: %s, clock_adjust %ld, drift_comp %ld(%ld) ", 1094abb0f93cSkardel pr_timeval(offset),(long int) R_SHIFT(clock_adjust, USECSCALE), 1095abb0f93cSkardel (long int)R_SHIFT(accum_drift, USECSCALE), (long int)accum_drift); 1096abb0f93cSkardel } 1097abb0f93cSkardel 1098abb0f93cSkardel /*----------------------------------------------------------------------- 1099abb0f93cSkardel * adjust the clock by a small mount to simulate frequency correction 1100abb0f93cSkardel */ 1101abb0f93cSkardel static void 1102abb0f93cSkardel periodic_adjust( 1103abb0f93cSkardel void 1104abb0f93cSkardel ) 1105abb0f93cSkardel { 1106abb0f93cSkardel register long adjustment; 1107abb0f93cSkardel 1108abb0f93cSkardel adjustments++; 1109abb0f93cSkardel 1110abb0f93cSkardel adjustment = R_SHIFT(clock_adjust, PHASE_WEIGHT); 1111abb0f93cSkardel 1112abb0f93cSkardel clock_adjust -= adjustment; 1113abb0f93cSkardel 1114abb0f93cSkardel adjustment += R_SHIFT(accum_drift, USECSCALE+ADJINTERVAL); 1115abb0f93cSkardel 1116abb0f93cSkardel adj_time(adjustment); 1117abb0f93cSkardel } 1118abb0f93cSkardel 1119abb0f93cSkardel /*----------------------------------------------------------------------- 1120abb0f93cSkardel * control synchronisation status (warnings) and do periodic adjusts 1121abb0f93cSkardel * (frequency control simulation) 1122abb0f93cSkardel */ 1123abb0f93cSkardel static void 1124abb0f93cSkardel tick( 1125abb0f93cSkardel int signum 1126abb0f93cSkardel ) 1127abb0f93cSkardel { 1128abb0f93cSkardel static unsigned long last_notice = 0; 1129abb0f93cSkardel 1130abb0f93cSkardel #if !defined(HAVE_SIGACTION) && !defined(HAVE_SIGVEC) 1131abb0f93cSkardel (void)signal(SIGALRM, tick); 1132abb0f93cSkardel #endif 1133abb0f93cSkardel 1134abb0f93cSkardel periodic_adjust(); 1135abb0f93cSkardel 1136abb0f93cSkardel ticks += 1<<ADJINTERVAL; 1137abb0f93cSkardel 1138abb0f93cSkardel if ((ticks - last_sync) > MAX_UNSYNC) 1139abb0f93cSkardel { 1140abb0f93cSkardel /* 1141abb0f93cSkardel * not getting time for a while 1142abb0f93cSkardel */ 1143abb0f93cSkardel if (sync_state == SYNC) 1144abb0f93cSkardel { 1145abb0f93cSkardel /* 1146abb0f93cSkardel * completely lost information 1147abb0f93cSkardel */ 1148abb0f93cSkardel sync_state = NO_SYNC; 1149abb0f93cSkardel syslog(LOG_INFO, "DCF77 reception lost (timeout)"); 1150abb0f93cSkardel last_notice = ticks; 1151abb0f93cSkardel } 1152abb0f93cSkardel else 1153abb0f93cSkardel /* 1154abb0f93cSkardel * in NO_SYNC state - look whether its time to speak up again 1155abb0f93cSkardel */ 1156abb0f93cSkardel if ((ticks - last_notice) > NOTICE_INTERVAL) 1157abb0f93cSkardel { 1158abb0f93cSkardel syslog(LOG_NOTICE, "still not synchronized to DCF77 - check receiver/signal"); 1159abb0f93cSkardel last_notice = ticks; 1160abb0f93cSkardel } 1161abb0f93cSkardel } 1162abb0f93cSkardel 1163abb0f93cSkardel #ifndef ITIMER_REAL 1164abb0f93cSkardel (void) alarm(1<<ADJINTERVAL); 1165abb0f93cSkardel #endif 1166abb0f93cSkardel } 1167abb0f93cSkardel 1168abb0f93cSkardel /*----------------------------------------------------------------------- 1169abb0f93cSkardel * break association from terminal to avoid catching terminal 1170abb0f93cSkardel * or process group related signals (-> daemon operation) 1171abb0f93cSkardel */ 1172abb0f93cSkardel static void 1173abb0f93cSkardel detach( 1174abb0f93cSkardel void 1175abb0f93cSkardel ) 1176abb0f93cSkardel { 1177abb0f93cSkardel # ifdef HAVE_DAEMON 1178*eabc0478Schristos if (daemon(0, 0)) { 1179*eabc0478Schristos fprintf(stderr, "'daemon()' fails: %d(%s)\n", 1180*eabc0478Schristos errno, strerror(errno)); 1181*eabc0478Schristos } 1182abb0f93cSkardel # else /* not HAVE_DAEMON */ 1183abb0f93cSkardel if (fork()) 1184abb0f93cSkardel exit(0); 1185abb0f93cSkardel 1186abb0f93cSkardel { 1187abb0f93cSkardel u_long s; 1188abb0f93cSkardel int max_fd; 1189abb0f93cSkardel 1190abb0f93cSkardel #if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX) 1191abb0f93cSkardel max_fd = sysconf(_SC_OPEN_MAX); 1192abb0f93cSkardel #else /* HAVE_SYSCONF && _SC_OPEN_MAX */ 1193abb0f93cSkardel max_fd = getdtablesize(); 1194abb0f93cSkardel #endif /* HAVE_SYSCONF && _SC_OPEN_MAX */ 1195abb0f93cSkardel for (s = 0; s < max_fd; s++) 1196abb0f93cSkardel (void) close((int)s); 1197abb0f93cSkardel (void) open("/", 0); 1198abb0f93cSkardel (void) dup2(0, 1); 1199abb0f93cSkardel (void) dup2(0, 2); 1200abb0f93cSkardel #ifdef SYS_DOMAINOS 1201abb0f93cSkardel { 1202abb0f93cSkardel uid_$t puid; 1203abb0f93cSkardel status_$t st; 1204abb0f93cSkardel 1205abb0f93cSkardel proc2_$who_am_i(&puid); 1206abb0f93cSkardel proc2_$make_server(&puid, &st); 1207abb0f93cSkardel } 1208abb0f93cSkardel #endif /* SYS_DOMAINOS */ 1209abb0f93cSkardel #if defined(HAVE_SETPGID) || defined(HAVE_SETSID) 1210abb0f93cSkardel # ifdef HAVE_SETSID 1211abb0f93cSkardel if (setsid() == (pid_t)-1) 1212abb0f93cSkardel syslog(LOG_ERR, "dcfd: setsid(): %m"); 1213abb0f93cSkardel # else 1214abb0f93cSkardel if (setpgid(0, 0) == -1) 1215abb0f93cSkardel syslog(LOG_ERR, "dcfd: setpgid(): %m"); 1216abb0f93cSkardel # endif 1217abb0f93cSkardel #else /* HAVE_SETPGID || HAVE_SETSID */ 1218abb0f93cSkardel { 1219abb0f93cSkardel int fid; 1220abb0f93cSkardel 1221abb0f93cSkardel fid = open("/dev/tty", 2); 1222abb0f93cSkardel if (fid >= 0) 1223abb0f93cSkardel { 1224abb0f93cSkardel (void) ioctl(fid, (u_long) TIOCNOTTY, (char *) 0); 1225abb0f93cSkardel (void) close(fid); 1226abb0f93cSkardel } 1227abb0f93cSkardel # ifdef HAVE_SETPGRP_0 1228abb0f93cSkardel (void) setpgrp(); 1229abb0f93cSkardel # else /* HAVE_SETPGRP_0 */ 1230abb0f93cSkardel (void) setpgrp(0, getpid()); 1231abb0f93cSkardel # endif /* HAVE_SETPGRP_0 */ 1232abb0f93cSkardel } 1233abb0f93cSkardel #endif /* HAVE_SETPGID || HAVE_SETSID */ 1234abb0f93cSkardel } 1235abb0f93cSkardel #endif /* not HAVE_DAEMON */ 1236abb0f93cSkardel } 1237abb0f93cSkardel 1238abb0f93cSkardel /*----------------------------------------------------------------------- 1239abb0f93cSkardel * list possible arguments and options 1240abb0f93cSkardel */ 1241abb0f93cSkardel static void 1242abb0f93cSkardel usage( 1243abb0f93cSkardel char *program 1244abb0f93cSkardel ) 1245abb0f93cSkardel { 1246abb0f93cSkardel fprintf(stderr, "usage: %s [-n] [-f] [-l] [-t] [-i] [-o] [-d <drift_file>] [-D <input delay>] <device>\n", program); 1247abb0f93cSkardel fprintf(stderr, "\t-n do not change time\n"); 1248abb0f93cSkardel fprintf(stderr, "\t-i interactive\n"); 1249abb0f93cSkardel fprintf(stderr, "\t-t trace (print all datagrams)\n"); 1250abb0f93cSkardel fprintf(stderr, "\t-f print all databits (includes PTB private data)\n"); 1251abb0f93cSkardel fprintf(stderr, "\t-l print loop filter debug information\n"); 1252abb0f93cSkardel fprintf(stderr, "\t-o print offet average for current minute\n"); 1253abb0f93cSkardel fprintf(stderr, "\t-Y make internal Y2K checks then exit\n"); /* Y2KFixes */ 1254abb0f93cSkardel fprintf(stderr, "\t-d <drift_file> specify alternate drift file\n"); 1255abb0f93cSkardel fprintf(stderr, "\t-D <input delay>specify delay from input edge to processing in micro seconds\n"); 1256abb0f93cSkardel } 1257abb0f93cSkardel 1258abb0f93cSkardel /*----------------------------------------------------------------------- 1259abb0f93cSkardel * check_y2k() - internal check of Y2K logic 1260abb0f93cSkardel * (a lot of this logic lifted from ../ntpd/check_y2k.c) 1261abb0f93cSkardel */ 1262abb0f93cSkardel static int 1263abb0f93cSkardel check_y2k( void ) 1264abb0f93cSkardel { 1265abb0f93cSkardel int year; /* current working year */ 1266abb0f93cSkardel int year0 = 1900; /* sarting year for NTP time */ 1267abb0f93cSkardel int yearend; /* ending year we test for NTP time. 1268abb0f93cSkardel * 32-bit systems: through 2036, the 1269abb0f93cSkardel **year in which NTP time overflows. 1270abb0f93cSkardel * 64-bit systems: a reasonable upper 1271abb0f93cSkardel **limit (well, maybe somewhat beyond 1272abb0f93cSkardel **reasonable, but well before the 1273abb0f93cSkardel **max time, by which time the earth 1274abb0f93cSkardel **will be dead.) */ 1275abb0f93cSkardel time_t Time; 1276abb0f93cSkardel struct tm LocalTime; 1277abb0f93cSkardel 1278abb0f93cSkardel int Fatals, Warnings; 1279abb0f93cSkardel #define Error(year) if ( (year)>=2036 && LocalTime.tm_year < 110 ) \ 1280abb0f93cSkardel Warnings++; else Fatals++ 1281abb0f93cSkardel 1282abb0f93cSkardel Fatals = Warnings = 0; 1283abb0f93cSkardel 1284abb0f93cSkardel Time = time( (time_t *)NULL ); 1285abb0f93cSkardel LocalTime = *localtime( &Time ); 1286abb0f93cSkardel 1287abb0f93cSkardel year = ( sizeof( u_long ) > 4 ) /* save max span using year as temp */ 1288abb0f93cSkardel ? ( 400 * 3 ) /* three greater gregorian cycles */ 1289abb0f93cSkardel : ((int)(0x7FFFFFFF / 365.242 / 24/60/60)* 2 ); /*32-bit limit*/ 1290abb0f93cSkardel /* NOTE: will automacially expand test years on 1291abb0f93cSkardel * 64 bit machines.... this may cause some of the 1292abb0f93cSkardel * existing ntp logic to fail for years beyond 1293abb0f93cSkardel * 2036 (the current 32-bit limit). If all checks 1294abb0f93cSkardel * fail ONLY beyond year 2036 you may ignore such 1295abb0f93cSkardel * errors, at least for a decade or so. */ 1296abb0f93cSkardel yearend = year0 + year; 1297abb0f93cSkardel 1298abb0f93cSkardel year = 1900+YEAR_PIVOT; 1299abb0f93cSkardel printf( " starting year %04d\n", (int) year ); 1300abb0f93cSkardel printf( " ending year %04d\n", (int) yearend ); 1301abb0f93cSkardel 1302abb0f93cSkardel for ( ; year < yearend; year++ ) 1303abb0f93cSkardel { 1304abb0f93cSkardel clocktime_t ct; 1305abb0f93cSkardel time_t Observed; 1306abb0f93cSkardel time_t Expected; 1307abb0f93cSkardel unsigned Flag; 1308abb0f93cSkardel unsigned long t; 1309abb0f93cSkardel 1310abb0f93cSkardel ct.day = 1; 1311abb0f93cSkardel ct.month = 1; 1312abb0f93cSkardel ct.year = year; 1313abb0f93cSkardel ct.hour = ct.minute = ct.second = ct.usecond = 0; 1314abb0f93cSkardel ct.utcoffset = 0; 1315abb0f93cSkardel ct.flags = 0; 1316abb0f93cSkardel 1317abb0f93cSkardel Flag = 0; 1318abb0f93cSkardel Observed = dcf_to_unixtime( &ct, &Flag ); 1319abb0f93cSkardel /* seems to be a clone of parse_to_unixtime() with 1320abb0f93cSkardel * *a minor difference to arg2 type */ 1321abb0f93cSkardel if ( ct.year != year ) 1322abb0f93cSkardel { 1323abb0f93cSkardel fprintf( stdout, 1324abb0f93cSkardel "%04d: dcf_to_unixtime(,%d) CORRUPTED ct.year: was %d\n", 1325abb0f93cSkardel (int)year, (int)Flag, (int)ct.year ); 1326abb0f93cSkardel Error(year); 1327abb0f93cSkardel break; 1328abb0f93cSkardel } 1329abb0f93cSkardel t = julian0(year) - julian0(1970); /* Julian day from 1970 */ 1330abb0f93cSkardel Expected = t * 24 * 60 * 60; 1331abb0f93cSkardel if ( Observed != Expected || Flag ) 1332abb0f93cSkardel { /* time difference */ 1333abb0f93cSkardel fprintf( stdout, 1334abb0f93cSkardel "%04d: dcf_to_unixtime(,%d) FAILURE: was=%lu s/b=%lu (%ld)\n", 1335abb0f93cSkardel year, (int)Flag, 1336abb0f93cSkardel (unsigned long)Observed, (unsigned long)Expected, 1337abb0f93cSkardel ((long)Observed - (long)Expected) ); 1338abb0f93cSkardel Error(year); 1339abb0f93cSkardel break; 1340abb0f93cSkardel } 1341abb0f93cSkardel 1342abb0f93cSkardel } 1343abb0f93cSkardel 1344abb0f93cSkardel return ( Fatals ); 1345abb0f93cSkardel } 1346abb0f93cSkardel 1347abb0f93cSkardel /*-------------------------------------------------- 1348abb0f93cSkardel * rawdcf_init - set up modem lines for RAWDCF receivers 1349abb0f93cSkardel */ 1350abb0f93cSkardel #if defined(TIOCMSET) && (defined(TIOCM_DTR) || defined(CIOCM_DTR)) 1351abb0f93cSkardel static void 1352abb0f93cSkardel rawdcf_init( 1353abb0f93cSkardel int fd 1354abb0f93cSkardel ) 1355abb0f93cSkardel { 1356abb0f93cSkardel /* 1357abb0f93cSkardel * You can use the RS232 to supply the power for a DCF77 receiver. 1358abb0f93cSkardel * Here a voltage between the DTR and the RTS line is used. Unfortunately 1359abb0f93cSkardel * the name has changed from CIOCM_DTR to TIOCM_DTR recently. 1360abb0f93cSkardel */ 1361abb0f93cSkardel 1362abb0f93cSkardel #ifdef TIOCM_DTR 1363abb0f93cSkardel int sl232 = TIOCM_DTR; /* turn on DTR for power supply */ 1364abb0f93cSkardel #else 1365abb0f93cSkardel int sl232 = CIOCM_DTR; /* turn on DTR for power supply */ 1366abb0f93cSkardel #endif 1367abb0f93cSkardel 1368abb0f93cSkardel if (ioctl(fd, TIOCMSET, (caddr_t)&sl232) == -1) 1369abb0f93cSkardel { 1370abb0f93cSkardel syslog(LOG_NOTICE, "rawdcf_init: WARNING: ioctl(fd, TIOCMSET, [C|T]IOCM_DTR): %m"); 1371abb0f93cSkardel } 1372abb0f93cSkardel } 1373abb0f93cSkardel #else 1374abb0f93cSkardel static void 1375abb0f93cSkardel rawdcf_init( 1376abb0f93cSkardel int fd 1377abb0f93cSkardel ) 1378abb0f93cSkardel { 1379abb0f93cSkardel syslog(LOG_NOTICE, "rawdcf_init: WARNING: OS interface incapable of setting DTR to power DCF modules"); 1380abb0f93cSkardel } 1381abb0f93cSkardel #endif /* DTR initialisation type */ 1382abb0f93cSkardel 1383abb0f93cSkardel /*----------------------------------------------------------------------- 1384abb0f93cSkardel * main loop - argument interpreter / setup / main loop 1385abb0f93cSkardel */ 1386abb0f93cSkardel int 1387abb0f93cSkardel main( 1388abb0f93cSkardel int argc, 1389abb0f93cSkardel char **argv 1390abb0f93cSkardel ) 1391abb0f93cSkardel { 1392abb0f93cSkardel unsigned char c; 1393abb0f93cSkardel char **a = argv; 1394abb0f93cSkardel int ac = argc; 1395abb0f93cSkardel char *file = NULL; 1396abb0f93cSkardel const char *drift_file = "/etc/dcfd.drift"; 1397abb0f93cSkardel int fd; 1398abb0f93cSkardel int offset = 15; 1399abb0f93cSkardel int offsets = 0; 1400abb0f93cSkardel int delay = DEFAULT_DELAY; /* average delay from input edge to time stamping */ 1401abb0f93cSkardel int trace = 0; 1402abb0f93cSkardel int errs = 0; 1403abb0f93cSkardel 1404abb0f93cSkardel /* 1405abb0f93cSkardel * process arguments 1406abb0f93cSkardel */ 1407abb0f93cSkardel while (--ac) 1408abb0f93cSkardel { 1409abb0f93cSkardel char *arg = *++a; 1410abb0f93cSkardel if (*arg == '-') 1411abb0f93cSkardel while ((c = *++arg)) 1412abb0f93cSkardel switch (c) 1413abb0f93cSkardel { 1414abb0f93cSkardel case 't': 1415abb0f93cSkardel trace = 1; 1416abb0f93cSkardel interactive = 1; 1417abb0f93cSkardel break; 1418abb0f93cSkardel 1419abb0f93cSkardel case 'f': 1420abb0f93cSkardel offset = 0; 1421abb0f93cSkardel interactive = 1; 1422abb0f93cSkardel break; 1423abb0f93cSkardel 1424abb0f93cSkardel case 'l': 1425abb0f93cSkardel loop_filter_debug = 1; 1426abb0f93cSkardel offsets = 1; 1427abb0f93cSkardel interactive = 1; 1428abb0f93cSkardel break; 1429abb0f93cSkardel 1430abb0f93cSkardel case 'n': 1431abb0f93cSkardel no_set = 1; 1432abb0f93cSkardel break; 1433abb0f93cSkardel 1434abb0f93cSkardel case 'o': 1435abb0f93cSkardel offsets = 1; 1436abb0f93cSkardel interactive = 1; 1437abb0f93cSkardel break; 1438abb0f93cSkardel 1439abb0f93cSkardel case 'i': 1440abb0f93cSkardel interactive = 1; 1441abb0f93cSkardel break; 1442abb0f93cSkardel 1443abb0f93cSkardel case 'D': 1444abb0f93cSkardel if (ac > 1) 1445abb0f93cSkardel { 1446abb0f93cSkardel delay = atoi(*++a); 1447abb0f93cSkardel ac--; 1448abb0f93cSkardel } 1449abb0f93cSkardel else 1450abb0f93cSkardel { 1451abb0f93cSkardel fprintf(stderr, "%s: -D requires integer argument\n", argv[0]); 1452abb0f93cSkardel errs=1; 1453abb0f93cSkardel } 1454abb0f93cSkardel break; 1455abb0f93cSkardel 1456abb0f93cSkardel case 'd': 1457abb0f93cSkardel if (ac > 1) 1458abb0f93cSkardel { 1459abb0f93cSkardel drift_file = *++a; 1460abb0f93cSkardel ac--; 1461abb0f93cSkardel } 1462abb0f93cSkardel else 1463abb0f93cSkardel { 1464abb0f93cSkardel fprintf(stderr, "%s: -d requires file name argument\n", argv[0]); 1465abb0f93cSkardel errs=1; 1466abb0f93cSkardel } 1467abb0f93cSkardel break; 1468abb0f93cSkardel 1469abb0f93cSkardel case 'Y': 1470abb0f93cSkardel errs=check_y2k(); 1471abb0f93cSkardel exit( errs ? 1 : 0 ); 1472abb0f93cSkardel 1473abb0f93cSkardel default: 1474abb0f93cSkardel fprintf(stderr, "%s: unknown option -%c\n", argv[0], c); 1475abb0f93cSkardel errs=1; 1476abb0f93cSkardel break; 1477abb0f93cSkardel } 1478abb0f93cSkardel else 1479abb0f93cSkardel if (file == NULL) 1480abb0f93cSkardel file = arg; 1481abb0f93cSkardel else 1482abb0f93cSkardel { 1483abb0f93cSkardel fprintf(stderr, "%s: device specified twice\n", argv[0]); 1484abb0f93cSkardel errs=1; 1485abb0f93cSkardel } 1486abb0f93cSkardel } 1487abb0f93cSkardel 1488abb0f93cSkardel if (errs) 1489abb0f93cSkardel { 1490abb0f93cSkardel usage(argv[0]); 1491abb0f93cSkardel exit(1); 1492abb0f93cSkardel } 1493abb0f93cSkardel else 1494abb0f93cSkardel if (file == NULL) 1495abb0f93cSkardel { 1496abb0f93cSkardel fprintf(stderr, "%s: device not specified\n", argv[0]); 1497abb0f93cSkardel usage(argv[0]); 1498abb0f93cSkardel exit(1); 1499abb0f93cSkardel } 1500abb0f93cSkardel 1501abb0f93cSkardel errs = LINES+1; 1502abb0f93cSkardel 1503abb0f93cSkardel /* 1504abb0f93cSkardel * get access to DCF77 tty port 1505abb0f93cSkardel */ 1506abb0f93cSkardel fd = open(file, O_RDONLY); 1507abb0f93cSkardel if (fd == -1) 1508abb0f93cSkardel { 1509abb0f93cSkardel perror(file); 1510abb0f93cSkardel exit(1); 1511abb0f93cSkardel } 1512abb0f93cSkardel else 1513abb0f93cSkardel { 1514abb0f93cSkardel int i, rrc; 1515abb0f93cSkardel struct timeval t, tt, tlast; 1516abb0f93cSkardel struct timeval timeout; 1517abb0f93cSkardel struct timeval phase; 1518abb0f93cSkardel struct timeval time_offset; 1519abb0f93cSkardel char pbuf[61]; /* printable version */ 1520abb0f93cSkardel char buf[61]; /* raw data */ 1521abb0f93cSkardel clocktime_t clock_time; /* wall clock time */ 1522abb0f93cSkardel time_t utc_time = 0; 1523abb0f93cSkardel time_t last_utc_time = 0; 1524abb0f93cSkardel long usecerror = 0; 1525abb0f93cSkardel long lasterror = 0; 1526abb0f93cSkardel #if defined(HAVE_TERMIOS_H) || defined(STREAM) 1527abb0f93cSkardel struct termios term; 1528abb0f93cSkardel #else /* not HAVE_TERMIOS_H || STREAM */ 1529abb0f93cSkardel # if defined(HAVE_TERMIO_H) || defined(HAVE_SYSV_TTYS) 1530abb0f93cSkardel struct termio term; 1531abb0f93cSkardel # endif/* HAVE_TERMIO_H || HAVE_SYSV_TTYS */ 1532abb0f93cSkardel #endif /* not HAVE_TERMIOS_H || STREAM */ 1533abb0f93cSkardel unsigned int rtc = CVT_NONE; 1534abb0f93cSkardel 1535abb0f93cSkardel rawdcf_init(fd); 1536abb0f93cSkardel 1537abb0f93cSkardel timeout.tv_sec = 1; 1538abb0f93cSkardel timeout.tv_usec = 500000; 1539abb0f93cSkardel 1540abb0f93cSkardel phase.tv_sec = 0; 1541abb0f93cSkardel phase.tv_usec = delay; 1542abb0f93cSkardel 1543abb0f93cSkardel /* 1544abb0f93cSkardel * setup TTY (50 Baud, Read, 8Bit, No Hangup, 1 character IO) 1545abb0f93cSkardel */ 1546abb0f93cSkardel if (TTY_GETATTR(fd, &term) == -1) 1547abb0f93cSkardel { 1548abb0f93cSkardel perror("tcgetattr"); 1549abb0f93cSkardel exit(1); 1550abb0f93cSkardel } 1551abb0f93cSkardel 1552abb0f93cSkardel memset(term.c_cc, 0, sizeof(term.c_cc)); 1553abb0f93cSkardel term.c_cc[VMIN] = 1; 1554abb0f93cSkardel #ifdef NO_PARENB_IGNPAR 1555abb0f93cSkardel term.c_cflag = CS8|CREAD|CLOCAL; 1556abb0f93cSkardel #else 1557abb0f93cSkardel term.c_cflag = CS8|CREAD|CLOCAL|PARENB; 1558abb0f93cSkardel #endif 1559abb0f93cSkardel term.c_iflag = IGNPAR; 1560abb0f93cSkardel term.c_oflag = 0; 1561abb0f93cSkardel term.c_lflag = 0; 1562abb0f93cSkardel 1563abb0f93cSkardel cfsetispeed(&term, B50); 1564abb0f93cSkardel cfsetospeed(&term, B50); 1565abb0f93cSkardel 1566abb0f93cSkardel if (TTY_SETATTR(fd, &term) == -1) 1567abb0f93cSkardel { 1568abb0f93cSkardel perror("tcsetattr"); 1569abb0f93cSkardel exit(1); 1570abb0f93cSkardel } 1571abb0f93cSkardel 1572abb0f93cSkardel /* 1573abb0f93cSkardel * lose terminal if in daemon operation 1574abb0f93cSkardel */ 1575abb0f93cSkardel if (!interactive) 1576abb0f93cSkardel detach(); 1577abb0f93cSkardel 1578abb0f93cSkardel /* 1579abb0f93cSkardel * get syslog() initialized 1580abb0f93cSkardel */ 1581abb0f93cSkardel #ifdef LOG_DAEMON 1582abb0f93cSkardel openlog("dcfd", LOG_PID, LOG_DAEMON); 1583abb0f93cSkardel #else 1584abb0f93cSkardel openlog("dcfd", LOG_PID); 1585abb0f93cSkardel #endif 1586abb0f93cSkardel 1587abb0f93cSkardel /* 1588abb0f93cSkardel * setup periodic operations (state control / frequency control) 1589abb0f93cSkardel */ 1590abb0f93cSkardel #ifdef HAVE_SIGACTION 1591abb0f93cSkardel { 1592abb0f93cSkardel struct sigaction act; 1593abb0f93cSkardel 1594abb0f93cSkardel # ifdef HAVE_SA_SIGACTION_IN_STRUCT_SIGACTION 1595abb0f93cSkardel act.sa_sigaction = (void (*) (int, siginfo_t *, void *))0; 1596abb0f93cSkardel # endif /* HAVE_SA_SIGACTION_IN_STRUCT_SIGACTION */ 1597abb0f93cSkardel act.sa_handler = tick; 1598abb0f93cSkardel sigemptyset(&act.sa_mask); 1599abb0f93cSkardel act.sa_flags = 0; 1600abb0f93cSkardel 1601abb0f93cSkardel if (sigaction(SIGALRM, &act, (struct sigaction *)0) == -1) 1602abb0f93cSkardel { 1603abb0f93cSkardel syslog(LOG_ERR, "sigaction(SIGALRM): %m"); 1604abb0f93cSkardel exit(1); 1605abb0f93cSkardel } 1606abb0f93cSkardel } 1607abb0f93cSkardel #else 1608abb0f93cSkardel #ifdef HAVE_SIGVEC 1609abb0f93cSkardel { 1610abb0f93cSkardel struct sigvec vec; 1611abb0f93cSkardel 1612abb0f93cSkardel vec.sv_handler = tick; 1613abb0f93cSkardel vec.sv_mask = 0; 1614abb0f93cSkardel vec.sv_flags = 0; 1615abb0f93cSkardel 1616abb0f93cSkardel if (sigvec(SIGALRM, &vec, (struct sigvec *)0) == -1) 1617abb0f93cSkardel { 1618abb0f93cSkardel syslog(LOG_ERR, "sigvec(SIGALRM): %m"); 1619abb0f93cSkardel exit(1); 1620abb0f93cSkardel } 1621abb0f93cSkardel } 1622abb0f93cSkardel #else 1623abb0f93cSkardel (void) signal(SIGALRM, tick); 1624abb0f93cSkardel #endif 1625abb0f93cSkardel #endif 1626abb0f93cSkardel 1627abb0f93cSkardel #ifdef ITIMER_REAL 1628abb0f93cSkardel { 1629abb0f93cSkardel struct itimerval it; 1630abb0f93cSkardel 1631abb0f93cSkardel it.it_interval.tv_sec = 1<<ADJINTERVAL; 1632abb0f93cSkardel it.it_interval.tv_usec = 0; 1633abb0f93cSkardel it.it_value.tv_sec = 1<<ADJINTERVAL; 1634abb0f93cSkardel it.it_value.tv_usec = 0; 1635abb0f93cSkardel 1636abb0f93cSkardel if (setitimer(ITIMER_REAL, &it, (struct itimerval *)0) == -1) 1637abb0f93cSkardel { 1638abb0f93cSkardel syslog(LOG_ERR, "setitimer: %m"); 1639abb0f93cSkardel exit(1); 1640abb0f93cSkardel } 1641abb0f93cSkardel } 1642abb0f93cSkardel #else 1643abb0f93cSkardel (void) alarm(1<<ADJINTERVAL); 1644abb0f93cSkardel #endif 1645abb0f93cSkardel 1646abb0f93cSkardel PRINTF(" DCF77 monitor %s - Copyright (C) 1993-2005 by Frank Kardel\n\n", revision); 1647abb0f93cSkardel 1648abb0f93cSkardel pbuf[60] = '\0'; 1649abb0f93cSkardel for ( i = 0; i < 60; i++) 1650abb0f93cSkardel pbuf[i] = '.'; 1651abb0f93cSkardel 1652abb0f93cSkardel read_drift(drift_file); 1653abb0f93cSkardel 1654abb0f93cSkardel /* 1655abb0f93cSkardel * what time is it now (for interval measurement) 1656abb0f93cSkardel */ 1657abb0f93cSkardel gettimeofday(&tlast, 0L); 1658abb0f93cSkardel i = 0; 1659abb0f93cSkardel /* 1660abb0f93cSkardel * loop until input trouble ... 1661abb0f93cSkardel */ 1662abb0f93cSkardel do 1663abb0f93cSkardel { 1664abb0f93cSkardel /* 1665abb0f93cSkardel * get an impulse 1666abb0f93cSkardel */ 1667abb0f93cSkardel while ((rrc = read(fd, &c, 1)) == 1) 1668abb0f93cSkardel { 1669abb0f93cSkardel gettimeofday(&t, 0L); 1670abb0f93cSkardel tt = t; 1671abb0f93cSkardel timersub(&t, &tlast); 1672abb0f93cSkardel 1673abb0f93cSkardel if (errs > LINES) 1674abb0f93cSkardel { 1675abb0f93cSkardel PRINTF(" %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]); 1676abb0f93cSkardel PRINTF(" %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]); 1677abb0f93cSkardel errs = 0; 1678abb0f93cSkardel } 1679abb0f93cSkardel 1680abb0f93cSkardel /* 1681abb0f93cSkardel * timeout -> possible minute mark -> interpretation 1682abb0f93cSkardel */ 1683abb0f93cSkardel if (timercmp(&t, &timeout, >)) 1684abb0f93cSkardel { 1685abb0f93cSkardel PRINTF("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &pbuf[offset]); 1686abb0f93cSkardel 1687abb0f93cSkardel if ((rtc = cvt_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK) 1688abb0f93cSkardel { 1689abb0f93cSkardel /* 1690abb0f93cSkardel * this data was bad - well - forget synchronisation for now 1691abb0f93cSkardel */ 1692abb0f93cSkardel PRINTF("\n"); 1693abb0f93cSkardel if (sync_state == SYNC) 1694abb0f93cSkardel { 1695abb0f93cSkardel sync_state = NO_SYNC; 1696abb0f93cSkardel syslog(LOG_INFO, "DCF77 reception lost (bad data)"); 1697abb0f93cSkardel } 1698abb0f93cSkardel errs++; 1699abb0f93cSkardel } 1700abb0f93cSkardel else 1701abb0f93cSkardel if (trace) 1702abb0f93cSkardel { 1703abb0f93cSkardel PRINTF("\r %.*s ", 59 - offset, &buf[offset]); 1704abb0f93cSkardel } 1705abb0f93cSkardel 1706abb0f93cSkardel 1707abb0f93cSkardel buf[0] = c; 1708abb0f93cSkardel 1709abb0f93cSkardel /* 1710abb0f93cSkardel * collect first character 1711abb0f93cSkardel */ 1712abb0f93cSkardel if (((c^0xFF)+1) & (c^0xFF)) 1713abb0f93cSkardel pbuf[0] = '?'; 1714abb0f93cSkardel else 1715abb0f93cSkardel pbuf[0] = type(c) ? '#' : '-'; 1716abb0f93cSkardel 1717abb0f93cSkardel for ( i = 1; i < 60; i++) 1718abb0f93cSkardel pbuf[i] = '.'; 1719abb0f93cSkardel 1720abb0f93cSkardel i = 0; 1721abb0f93cSkardel } 1722abb0f93cSkardel else 1723abb0f93cSkardel { 1724abb0f93cSkardel /* 1725abb0f93cSkardel * collect character 1726abb0f93cSkardel */ 1727abb0f93cSkardel buf[i] = c; 1728abb0f93cSkardel 1729abb0f93cSkardel /* 1730abb0f93cSkardel * initial guess (usually correct) 1731abb0f93cSkardel */ 1732abb0f93cSkardel if (((c^0xFF)+1) & (c^0xFF)) 1733abb0f93cSkardel pbuf[i] = '?'; 1734abb0f93cSkardel else 1735abb0f93cSkardel pbuf[i] = type(c) ? '#' : '-'; 1736abb0f93cSkardel 1737abb0f93cSkardel PRINTF("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &pbuf[offset]); 1738abb0f93cSkardel } 1739abb0f93cSkardel 1740abb0f93cSkardel if (i == 0 && rtc == CVT_OK) 1741abb0f93cSkardel { 1742abb0f93cSkardel /* 1743abb0f93cSkardel * we got a good time code here - try to convert it to 1744abb0f93cSkardel * UTC 1745abb0f93cSkardel */ 1746abb0f93cSkardel if ((utc_time = dcf_to_unixtime(&clock_time, &rtc)) == -1) 1747abb0f93cSkardel { 1748abb0f93cSkardel PRINTF("*** BAD CONVERSION\n"); 1749abb0f93cSkardel } 1750abb0f93cSkardel 1751abb0f93cSkardel if (utc_time != (last_utc_time + 60)) 1752abb0f93cSkardel { 1753abb0f93cSkardel /* 1754abb0f93cSkardel * well, two successive sucessful telegrams are not 60 seconds 1755abb0f93cSkardel * apart 1756abb0f93cSkardel */ 1757abb0f93cSkardel PRINTF("*** NO MINUTE INC\n"); 1758abb0f93cSkardel if (sync_state == SYNC) 1759abb0f93cSkardel { 1760abb0f93cSkardel sync_state = NO_SYNC; 1761abb0f93cSkardel syslog(LOG_INFO, "DCF77 reception lost (data mismatch)"); 1762abb0f93cSkardel } 1763abb0f93cSkardel errs++; 1764abb0f93cSkardel rtc = CVT_FAIL|CVT_BADTIME|CVT_BADDATE; 1765abb0f93cSkardel } 1766abb0f93cSkardel else 1767abb0f93cSkardel usecerror = 0; 1768abb0f93cSkardel 1769abb0f93cSkardel last_utc_time = utc_time; 1770abb0f93cSkardel } 1771abb0f93cSkardel 1772abb0f93cSkardel if (rtc == CVT_OK) 1773abb0f93cSkardel { 1774abb0f93cSkardel if (i == 0) 1775abb0f93cSkardel { 1776abb0f93cSkardel /* 1777abb0f93cSkardel * valid time code - determine offset and 1778abb0f93cSkardel * note regained reception 1779abb0f93cSkardel */ 1780abb0f93cSkardel last_sync = ticks; 1781abb0f93cSkardel if (sync_state == NO_SYNC) 1782abb0f93cSkardel { 1783abb0f93cSkardel syslog(LOG_INFO, "receiving DCF77"); 1784abb0f93cSkardel } 1785abb0f93cSkardel else 1786abb0f93cSkardel { 1787abb0f93cSkardel /* 1788abb0f93cSkardel * we had at least one minute SYNC - thus 1789abb0f93cSkardel * last error is valid 1790abb0f93cSkardel */ 1791abb0f93cSkardel time_offset.tv_sec = lasterror / 1000000; 1792abb0f93cSkardel time_offset.tv_usec = lasterror % 1000000; 1793abb0f93cSkardel adjust_clock(&time_offset, drift_file, utc_time); 1794abb0f93cSkardel } 1795abb0f93cSkardel sync_state = SYNC; 1796abb0f93cSkardel } 1797abb0f93cSkardel 1798abb0f93cSkardel time_offset.tv_sec = utc_time + i; 1799abb0f93cSkardel time_offset.tv_usec = 0; 1800abb0f93cSkardel 1801abb0f93cSkardel timeradd(&time_offset, &phase); 1802abb0f93cSkardel 1803abb0f93cSkardel usecerror += (time_offset.tv_sec - tt.tv_sec) * 1000000 + time_offset.tv_usec 1804abb0f93cSkardel -tt.tv_usec; 1805abb0f93cSkardel 1806abb0f93cSkardel /* 1807abb0f93cSkardel * output interpreted DCF77 data 1808abb0f93cSkardel */ 1809abb0f93cSkardel PRINTF(offsets ? "%s, %2ld:%02ld:%02d, %ld.%02ld.%02ld, <%s%s%s%s> (%c%ld.%06lds)" : 1810abb0f93cSkardel "%s, %2ld:%02ld:%02d, %ld.%02ld.%02ld, <%s%s%s%s>", 1811abb0f93cSkardel wday[clock_time.wday], 1812abb0f93cSkardel clock_time.hour, clock_time.minute, i, clock_time.day, clock_time.month, 1813abb0f93cSkardel clock_time.year, 18147476e6e4Schristos (clock_time.flags & DCFB_CALLBIT) ? "R" : "_", 1815abb0f93cSkardel (clock_time.flags & DCFB_ANNOUNCE) ? "A" : "_", 1816abb0f93cSkardel (clock_time.flags & DCFB_DST) ? "D" : "_", 1817abb0f93cSkardel (clock_time.flags & DCFB_LEAP) ? "L" : "_", 1818abb0f93cSkardel (lasterror < 0) ? '-' : '+', l_abs(lasterror) / 1000000, l_abs(lasterror) % 1000000 1819abb0f93cSkardel ); 1820abb0f93cSkardel 1821abb0f93cSkardel if (trace && (i == 0)) 1822abb0f93cSkardel { 1823abb0f93cSkardel PRINTF("\n"); 1824abb0f93cSkardel errs++; 1825abb0f93cSkardel } 1826abb0f93cSkardel lasterror = usecerror / (i+1); 1827abb0f93cSkardel } 1828abb0f93cSkardel else 1829abb0f93cSkardel { 1830abb0f93cSkardel lasterror = 0; /* we cannot calculate phase errors on bad reception */ 1831abb0f93cSkardel } 1832abb0f93cSkardel 1833abb0f93cSkardel PRINTF("\r"); 1834abb0f93cSkardel 1835abb0f93cSkardel if (i < 60) 1836abb0f93cSkardel { 1837abb0f93cSkardel i++; 1838abb0f93cSkardel } 1839abb0f93cSkardel 1840abb0f93cSkardel tlast = tt; 1841abb0f93cSkardel 1842abb0f93cSkardel if (interactive) 1843abb0f93cSkardel fflush(stdout); 1844abb0f93cSkardel } 1845abb0f93cSkardel } while ((rrc == -1) && (errno == EINTR)); 1846abb0f93cSkardel 1847abb0f93cSkardel /* 1848abb0f93cSkardel * lost IO - sorry guys 1849abb0f93cSkardel */ 1850abb0f93cSkardel syslog(LOG_ERR, "TERMINATING - cannot read from device %s (%m)", file); 1851abb0f93cSkardel 1852abb0f93cSkardel (void)close(fd); 1853abb0f93cSkardel } 1854abb0f93cSkardel 1855abb0f93cSkardel closelog(); 1856abb0f93cSkardel 1857abb0f93cSkardel return 0; 1858abb0f93cSkardel } 1859abb0f93cSkardel 1860abb0f93cSkardel /* 1861abb0f93cSkardel * History: 1862abb0f93cSkardel * 1863abb0f93cSkardel * dcfd.c,v 1864abb0f93cSkardel * Revision 4.18 2005/10/07 22:08:18 kardel 1865abb0f93cSkardel * make dcfd.c compile on NetBSD 3.99.9 again (configure/sigvec compatibility fix) 1866abb0f93cSkardel * 1867abb0f93cSkardel * Revision 4.17.2.1 2005/10/03 19:15:16 kardel 1868abb0f93cSkardel * work around configure not detecting a missing sigvec compatibility 1869abb0f93cSkardel * interface on NetBSD 3.99.9 and above 1870abb0f93cSkardel * 1871abb0f93cSkardel * Revision 4.17 2005/08/10 10:09:44 kardel 1872abb0f93cSkardel * output revision information 1873abb0f93cSkardel * 1874abb0f93cSkardel * Revision 4.16 2005/08/10 06:33:25 kardel 1875abb0f93cSkardel * cleanup warnings 1876abb0f93cSkardel * 1877abb0f93cSkardel * Revision 4.15 2005/08/10 06:28:45 kardel 1878abb0f93cSkardel * fix setting of baud rate 1879abb0f93cSkardel * 1880abb0f93cSkardel * Revision 4.14 2005/04/16 17:32:10 kardel 1881abb0f93cSkardel * update copyright 1882abb0f93cSkardel * 1883abb0f93cSkardel * Revision 4.13 2004/11/14 15:29:41 kardel 1884abb0f93cSkardel * support PPSAPI, upgrade Copyright to Berkeley style 1885abb0f93cSkardel * 1886abb0f93cSkardel */ 1887