2 * /src/NTP/ntp4-dev/parseutil/testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
4 * testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
6 * simple DCF77 100/200ms pulse test program (via 50Baud serial line)
8 * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
9 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the author nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include "ntp_stdlib.h"
39 #include <sys/ioctl.h>
48 #define DCFB_ANNOUNCE 0x0001 /* switch time zone warning (DST switch) */
49 #define DCFB_DST 0x0002 /* DST in effect */
50 #define DCFB_LEAP 0x0004 /* LEAP warning (1 hour prior to occurrence) */
51 #define DCFB_ALTERNATE 0x0008 /* alternate antenna used */
53 struct clocktime /* clock time broken up from time code */
63 long utcoffset; /* in minutes */
64 long flags; /* current clock status */
67 typedef struct clocktime clocktime_t;
69 static char type(unsigned int);
71 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
74 * parser related return/error codes
76 #define CVT_MASK 0x0000000F /* conversion exit code */
77 #define CVT_NONE 0x00000001 /* format not applicable */
78 #define CVT_FAIL 0x00000002 /* conversion failed - error code returned */
79 #define CVT_OK 0x00000004 /* conversion succeeded */
80 #define CVT_BADFMT 0x00000010 /* general format error - (unparsable) */
85 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
86 * und Berlin, Maerz 1989
88 * Timecode transmission:
90 * time marks are send every second except for the second before the
92 * time marks consist of a reduction of transmitter power to 25%
93 * of the nominal level
94 * the falling edge is the time indication (on time)
95 * time marks of a 100ms duration constitute a logical 0
96 * time marks of a 200ms duration constitute a logical 1
98 * see the spec. (basically a (non-)inverted psuedo random phase shift)
102 * 0 - 10 AM: free, FM: 0
104 * 15 R - alternate antenna
105 * 16 A1 - expect zone change (1 hour before)
106 * 17 - 18 Z1,Z2 - time zone
109 * 1 0 MESZ (MED, MET DST)
111 * 19 A2 - expect leap insertion/deletion (1 hour before)
112 * 20 S - start of time code (1)
113 * 21 - 24 M1 - BCD (lsb first) Minutes
114 * 25 - 27 M10 - BCD (lsb first) 10 Minutes
115 * 28 P1 - Minute Parity (even)
116 * 29 - 32 H1 - BCD (lsb first) Hours
117 * 33 - 34 H10 - BCD (lsb first) 10 Hours
118 * 35 P2 - Hour Parity (even)
119 * 36 - 39 D1 - BCD (lsb first) Days
120 * 40 - 41 D10 - BCD (lsb first) 10 Days
121 * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
122 * 45 - 49 MO - BCD (lsb first) Month
124 * 51 - 53 Y1 - BCD (lsb first) Years
125 * 54 - 57 Y10 - BCD (lsb first) 10 Years
126 * 58 P3 - Date Parity (even)
127 * 59 - usually missing (minute indication), except for leap insertion
130 static char revision[] = "4.10";
132 static struct rawdcfcode
134 char offset; /* start bit */
137 { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
138 { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
164 char offset; /* start bit of parity field */
167 { 21 }, { 29 }, { 36 }, { 59 }
174 #define DCF_Z_MET 0x2
175 #define DCF_Z_MED 0x1
179 register unsigned char *buf,
183 register unsigned long sum = 0;
184 register int i, first;
186 first = rawdcfcode[idx].offset;
188 for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
191 sum |= (buf[i] != '-');
198 register unsigned char *buf,
203 register unsigned psum = 1;
205 last = partab[idx+1].offset;
207 for (i = partab[idx].offset; i < last; i++)
208 psum ^= (buf[i] != '-');
215 register unsigned char *buffer,
217 register clocktime_t *clock_time
222 printf("%-30s", "*** INCOMPLETE");
227 * check Start and Parity bits
229 if ((ext_bf(buffer, DCF_S) == 1) &&
230 pcheck(buffer, DCF_P_P1) &&
231 pcheck(buffer, DCF_P_P2) &&
232 pcheck(buffer, DCF_P_P3))
238 clock_time->flags = 0;
239 clock_time->usecond= 0;
240 clock_time->second = 0;
241 clock_time->minute = ext_bf(buffer, DCF_M10);
242 clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1);
243 clock_time->hour = ext_bf(buffer, DCF_H10);
244 clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1);
245 clock_time->day = ext_bf(buffer, DCF_D10);
246 clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1);
247 clock_time->month = ext_bf(buffer, DCF_MO0);
248 clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO);
249 clock_time->year = ext_bf(buffer, DCF_Y10);
250 clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1);
251 clock_time->wday = ext_bf(buffer, DCF_DW);
253 switch (ext_bf(buffer, DCF_Z))
256 clock_time->utcoffset = -60;
260 clock_time->flags |= DCFB_DST;
261 clock_time->utcoffset = -120;
265 printf("%-30s", "*** BAD TIME ZONE");
266 return CVT_FAIL|CVT_BADFMT;
269 if (ext_bf(buffer, DCF_A1))
270 clock_time->flags |= DCFB_ANNOUNCE;
272 if (ext_bf(buffer, DCF_A2))
273 clock_time->flags |= DCFB_LEAP;
275 if (ext_bf(buffer, DCF_R))
276 clock_time->flags |= DCFB_ALTERNATE;
283 * bad format - not for us
285 printf("%-30s", "*** BAD FORMAT (invalid/parity)");
286 return CVT_FAIL|CVT_BADFMT;
299 static const char *wday[8] =
311 static char pat[] = "-\\|/";
313 #define LINES (24-2) /* error lines after which the two headlines are repeated */
321 if ((argc != 2) && (argc != 3))
323 fprintf(stderr, "usage: %s [-f|-t|-ft|-tf] <device>\n", argv[0]);
336 * SIMPLE(!) argument "parser"
340 if (strcmp(argv[1], "-f") == 0)
342 if (strcmp(argv[1], "-t") == 0)
344 if ((strcmp(argv[1], "-ft") == 0) ||
345 (strcmp(argv[1], "-tf") == 0))
357 fd = open(file, O_RDONLY);
369 struct timeval t, tt, tlast;
371 clocktime_t clock_time;
375 if (tcgetattr(fd, &term) == -1)
381 memset(term.c_cc, 0, sizeof(term.c_cc));
383 #ifdef NO_PARENB_IGNPAR /* Was: defined(SYS_IRIX4) || defined (SYS_IRIX5) */
384 /* somehow doesn't grok PARENB & IGNPAR (mj) */
385 term.c_cflag = CS8|CREAD|CLOCAL;
387 term.c_cflag = CS8|CREAD|CLOCAL|PARENB;
389 term.c_iflag = IGNPAR;
393 cfsetispeed(&term, B50);
394 cfsetospeed(&term, B50);
396 if (tcsetattr(fd, TCSANOW, &term) == -1)
403 while (ioctl(fd, I_POP, 0) == 0)
406 #if defined(TIOCMBIC) && defined(TIOCM_RTS)
407 if (ioctl(fd, TIOCMBIC, (caddr_t)&on) == -1)
413 printf(" DCF77 monitor %s - Copyright (C) 1993-2005, Frank Kardel\n\n", revision);
416 clock_time.minute = 0;
419 clock_time.month = 0;
421 clock_time.flags = 0;
423 for ( i = 0; i < 60; i++)
426 gettimeofday(&tlast, 0L);
428 while (read(fd, &c, 1) == 1)
430 gettimeofday(&t, 0L);
432 t.tv_sec -= tlast.tv_sec;
433 t.tv_usec -= tlast.tv_usec;
436 t.tv_usec += 1000000;
442 printf(" %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
443 printf(" %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
451 printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
453 if ((rtc = convert_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK)
457 clock_time.minute = 0;
460 clock_time.month = 0;
462 clock_time.flags = 0;
466 if (((c^0xFF)+1) & (c^0xFF))
469 buf[0] = type(c) ? '#' : '-';
471 for ( i = 1; i < 60; i++)
478 if (((c^0xFF)+1) & (c^0xFF))
481 buf[i] = type(c) ? '#' : '-';
483 printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
488 printf("%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s>",
489 wday[clock_time.wday],
490 (int)clock_time.hour, (int)clock_time.minute, (int)i, (int)clock_time.day, (int)clock_time.month,
491 (int)clock_time.year,
492 (clock_time.flags & DCFB_ALTERNATE) ? "R" : "_",
493 (clock_time.flags & DCFB_ANNOUNCE) ? "A" : "_",
494 (clock_time.flags & DCFB_DST) ? "D" : "_",
495 (clock_time.flags & DCFB_LEAP) ? "L" : "_"
497 if (trace && (i == 0))
525 * Revision 4.10 2005/08/06 14:18:43 kardel
528 * Revision 4.9 2005/08/06 14:14:38 kardel
529 * document revision on startup
531 * Revision 4.8 2005/08/06 14:10:08 kardel
532 * fix setting of baud rate
534 * Revision 4.7 2005/04/16 17:32:10 kardel
537 * Revision 4.6 2004/11/14 15:29:42 kardel
538 * support PPSAPI, upgrade Copyright to Berkeley style