1 /* $NetBSD: testdcf.c,v 1.2 2003/12/04 16:23:38 drochner Exp $ */
4 * /src/NTP/ntp4-dev/parseutil/testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
6 * testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
8 * simple DCF77 100/200ms pulse test program (via 50Baud serial line)
10 * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
11 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the author nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "ntp_stdlib.h"
41 #include <sys/ioctl.h>
50 #define DCFB_ANNOUNCE 0x0001 /* switch time zone warning (DST switch) */
51 #define DCFB_DST 0x0002 /* DST in effect */
52 #define DCFB_LEAP 0x0004 /* LEAP warning (1 hour prior to occurrence) */
53 #define DCFB_ALTERNATE 0x0008 /* alternate antenna used */
55 struct clocktime
/* clock time broken up from time code */
65 long utcoffset
; /* in minutes */
66 long flags
; /* current clock status */
69 typedef struct clocktime clocktime_t
;
71 static char type(unsigned int);
73 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
76 * parser related return/error codes
78 #define CVT_MASK 0x0000000F /* conversion exit code */
79 #define CVT_NONE 0x00000001 /* format not applicable */
80 #define CVT_FAIL 0x00000002 /* conversion failed - error code returned */
81 #define CVT_OK 0x00000004 /* conversion succeeded */
82 #define CVT_BADFMT 0x00000010 /* general format error - (unparsable) */
87 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
88 * und Berlin, Maerz 1989
90 * Timecode transmission:
92 * time marks are send every second except for the second before the
94 * time marks consist of a reduction of transmitter power to 25%
95 * of the nominal level
96 * the falling edge is the time indication (on time)
97 * time marks of a 100ms duration constitute a logical 0
98 * time marks of a 200ms duration constitute a logical 1
100 * see the spec. (basically a (non-)inverted psuedo random phase shift)
104 * 0 - 10 AM: free, FM: 0
106 * 15 R - alternate antenna
107 * 16 A1 - expect zone change (1 hour before)
108 * 17 - 18 Z1,Z2 - time zone
111 * 1 0 MESZ (MED, MET DST)
113 * 19 A2 - expect leap insertion/deletion (1 hour before)
114 * 20 S - start of time code (1)
115 * 21 - 24 M1 - BCD (lsb first) Minutes
116 * 25 - 27 M10 - BCD (lsb first) 10 Minutes
117 * 28 P1 - Minute Parity (even)
118 * 29 - 32 H1 - BCD (lsb first) Hours
119 * 33 - 34 H10 - BCD (lsb first) 10 Hours
120 * 35 P2 - Hour Parity (even)
121 * 36 - 39 D1 - BCD (lsb first) Days
122 * 40 - 41 D10 - BCD (lsb first) 10 Days
123 * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
124 * 45 - 49 MO - BCD (lsb first) Month
126 * 51 - 53 Y1 - BCD (lsb first) Years
127 * 54 - 57 Y10 - BCD (lsb first) 10 Years
128 * 58 P3 - Date Parity (even)
129 * 59 - usually missing (minute indication), except for leap insertion
132 static char revision
[] = "4.10";
134 static struct rawdcfcode
136 char offset
; /* start bit */
139 { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
140 { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
166 char offset
; /* start bit of parity field */
169 { 21 }, { 29 }, { 36 }, { 59 }
176 #define DCF_Z_MET 0x2
177 #define DCF_Z_MED 0x1
181 register unsigned char *buf
,
185 register unsigned long sum
= 0;
186 register int i
, first
;
188 first
= rawdcfcode
[idx
].offset
;
190 for (i
= rawdcfcode
[idx
+1].offset
- 1; i
>= first
; i
--)
193 sum
|= (buf
[i
] != '-');
200 register unsigned char *buf
,
205 register unsigned psum
= 1;
207 last
= partab
[idx
+1].offset
;
209 for (i
= partab
[idx
].offset
; i
< last
; i
++)
210 psum
^= (buf
[i
] != '-');
217 register unsigned char *buffer
,
219 register clocktime_t
*clock_time
224 printf("%-30s", "*** INCOMPLETE");
229 * check Start and Parity bits
231 if ((ext_bf(buffer
, DCF_S
) == 1) &&
232 pcheck(buffer
, DCF_P_P1
) &&
233 pcheck(buffer
, DCF_P_P2
) &&
234 pcheck(buffer
, DCF_P_P3
))
240 clock_time
->flags
= 0;
241 clock_time
->usecond
= 0;
242 clock_time
->second
= 0;
243 clock_time
->minute
= ext_bf(buffer
, DCF_M10
);
244 clock_time
->minute
= TIMES10(clock_time
->minute
) + ext_bf(buffer
, DCF_M1
);
245 clock_time
->hour
= ext_bf(buffer
, DCF_H10
);
246 clock_time
->hour
= TIMES10(clock_time
->hour
) + ext_bf(buffer
, DCF_H1
);
247 clock_time
->day
= ext_bf(buffer
, DCF_D10
);
248 clock_time
->day
= TIMES10(clock_time
->day
) + ext_bf(buffer
, DCF_D1
);
249 clock_time
->month
= ext_bf(buffer
, DCF_MO0
);
250 clock_time
->month
= TIMES10(clock_time
->month
) + ext_bf(buffer
, DCF_MO
);
251 clock_time
->year
= ext_bf(buffer
, DCF_Y10
);
252 clock_time
->year
= TIMES10(clock_time
->year
) + ext_bf(buffer
, DCF_Y1
);
253 clock_time
->wday
= ext_bf(buffer
, DCF_DW
);
255 switch (ext_bf(buffer
, DCF_Z
))
258 clock_time
->utcoffset
= -60;
262 clock_time
->flags
|= DCFB_DST
;
263 clock_time
->utcoffset
= -120;
267 printf("%-30s", "*** BAD TIME ZONE");
268 return CVT_FAIL
|CVT_BADFMT
;
271 if (ext_bf(buffer
, DCF_A1
))
272 clock_time
->flags
|= DCFB_ANNOUNCE
;
274 if (ext_bf(buffer
, DCF_A2
))
275 clock_time
->flags
|= DCFB_LEAP
;
277 if (ext_bf(buffer
, DCF_R
))
278 clock_time
->flags
|= DCFB_ALTERNATE
;
285 * bad format - not for us
287 printf("%-30s", "*** BAD FORMAT (invalid/parity)");
288 return CVT_FAIL
|CVT_BADFMT
;
301 static const char *wday
[8] =
313 static char pat
[] = "-\\|/";
315 #define LINES (24-2) /* error lines after which the two headlines are repeated */
323 if ((argc
!= 2) && (argc
!= 3))
325 fprintf(stderr
, "usage: %s [-f|-t|-ft|-tf] <device>\n", argv
[0]);
338 * SIMPLE(!) argument "parser"
342 if (strcmp(argv
[1], "-f") == 0)
344 if (strcmp(argv
[1], "-t") == 0)
346 if ((strcmp(argv
[1], "-ft") == 0) ||
347 (strcmp(argv
[1], "-tf") == 0))
359 fd
= open(file
, O_RDONLY
);
371 struct timeval t
, tt
, tlast
;
373 clocktime_t clock_time
;
377 if (tcgetattr(fd
, &term
) == -1)
383 memset(term
.c_cc
, 0, sizeof(term
.c_cc
));
385 #ifdef NO_PARENB_IGNPAR /* Was: defined(SYS_IRIX4) || defined (SYS_IRIX5) */
386 /* somehow doesn't grok PARENB & IGNPAR (mj) */
387 term
.c_cflag
= CS8
|CREAD
|CLOCAL
;
389 term
.c_cflag
= CS8
|CREAD
|CLOCAL
|PARENB
;
391 term
.c_iflag
= IGNPAR
;
395 cfsetispeed(&term
, B50
);
396 cfsetospeed(&term
, B50
);
398 if (tcsetattr(fd
, TCSANOW
, &term
) == -1)
405 while (ioctl(fd
, I_POP
, 0) == 0)
408 #if defined(TIOCMBIC) && defined(TIOCM_RTS)
409 if (ioctl(fd
, TIOCMBIC
, (caddr_t
)&on
) == -1)
415 printf(" DCF77 monitor %s - Copyright (C) 1993-2005, Frank Kardel\n\n", revision
);
418 clock_time
.minute
= 0;
421 clock_time
.month
= 0;
423 clock_time
.flags
= 0;
425 for ( i
= 0; i
< 60; i
++)
428 gettimeofday(&tlast
, 0L);
430 while (read(fd
, &c
, 1) == 1)
432 gettimeofday(&t
, 0L);
434 t
.tv_sec
-= tlast
.tv_sec
;
435 t
.tv_usec
-= tlast
.tv_usec
;
438 t
.tv_usec
+= 1000000;
444 printf(" %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset
]);
445 printf(" %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset
]);
453 printf("%c %.*s ", pat
[i
% (sizeof(pat
)-1)], 59 - offset
, &buf
[offset
]);
455 if ((rtc
= convert_rawdcf((unsigned char *)buf
, i
, &clock_time
)) != CVT_OK
)
459 clock_time
.minute
= 0;
462 clock_time
.month
= 0;
464 clock_time
.flags
= 0;
468 if (((c
^0xFF)+1) & (c
^0xFF))
471 buf
[0] = type(c
) ? '#' : '-';
473 for ( i
= 1; i
< 60; i
++)
480 if (((c
^0xFF)+1) & (c
^0xFF))
483 buf
[i
] = type(c
) ? '#' : '-';
485 printf("%c %.*s ", pat
[i
% (sizeof(pat
)-1)], 59 - offset
, &buf
[offset
]);
490 printf("%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s>",
491 wday
[clock_time
.wday
],
492 (int)clock_time
.hour
, (int)clock_time
.minute
, (int)i
, (int)clock_time
.day
, (int)clock_time
.month
,
493 (int)clock_time
.year
,
494 (clock_time
.flags
& DCFB_ALTERNATE
) ? "R" : "_",
495 (clock_time
.flags
& DCFB_ANNOUNCE
) ? "A" : "_",
496 (clock_time
.flags
& DCFB_DST
) ? "D" : "_",
497 (clock_time
.flags
& DCFB_LEAP
) ? "L" : "_"
499 if (trace
&& (i
== 0))
527 * Revision 4.10 2005/08/06 14:18:43 kardel
530 * Revision 4.9 2005/08/06 14:14:38 kardel
531 * document revision on startup
533 * Revision 4.8 2005/08/06 14:10:08 kardel
534 * fix setting of baud rate
536 * Revision 4.7 2005/04/16 17:32:10 kardel
539 * Revision 4.6 2004/11/14 15:29:42 kardel
540 * support PPSAPI, upgrade Copyright to Berkeley style