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