2 * /src/NTP/REPOSITORY/v4/parseutil/testdcf.c,v 3.18 1996/12/01 16:05:04 kardel Exp
4 * testdcf.c,v 3.18 1996/12/01 16:05:04 kardel Exp
6 * simple DCF77 100/200ms pulse test program (via 50Baud serial line)
8 * Copyright (c) 1993,1994,1995,1996, 1998 by Frank Kardel
9 * Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * This program may not be sold or used for profit without prior
16 * written consent of the author.
23 #include <sys/types.h>
26 #include "ntp_stdlib.h"
31 #define DCFB_ANNOUNCE 0x0001 /* switch time zone warning (DST switch) */
32 #define DCFB_DST 0x0002 /* DST in effect */
33 #define DCFB_LEAP 0x0004 /* LEAP warning (1 hour prior to occurence) */
34 #define DCFB_ALTERNATE 0x0008 /* alternate antenna used */
36 struct clocktime /* clock time broken up from time code */
46 long utcoffset; /* in minutes */
47 long flags; /* current clock status */
50 typedef struct clocktime clocktime_t;
52 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
55 * parser related return/error codes
57 #define CVT_MASK 0x0000000F /* conversion exit code */
58 #define CVT_NONE 0x00000001 /* format not applicable */
59 #define CVT_FAIL 0x00000002 /* conversion failed - error code returned */
60 #define CVT_OK 0x00000004 /* conversion succeeded */
61 #define CVT_BADFMT 0x00000010 /* general format error - (unparsable) */
66 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
67 * und Berlin, Maerz 1989
69 * Timecode transmission:
71 * time marks are send every second except for the second before the
73 * time marks consist of a reduction of transmitter power to 25%
74 * of the nominal level
75 * the falling edge is the time indication (on time)
76 * time marks of a 100ms duration constitute a logical 0
77 * time marks of a 200ms duration constitute a logical 1
79 * see the spec. (basically a (non-)inverted psuedo random phase shift)
83 * 0 - 10 AM: free, FM: 0
85 * 15 R - alternate antenna
86 * 16 A1 - expect zone change (1 hour before)
87 * 17 - 18 Z1,Z2 - time zone
90 * 1 0 MESZ (MED, MET DST)
92 * 19 A2 - expect leap insertion/deletion (1 hour before)
93 * 20 S - start of time code (1)
94 * 21 - 24 M1 - BCD (lsb first) Minutes
95 * 25 - 27 M10 - BCD (lsb first) 10 Minutes
96 * 28 P1 - Minute Parity (even)
97 * 29 - 32 H1 - BCD (lsb first) Hours
98 * 33 - 34 H10 - BCD (lsb first) 10 Hours
99 * 35 P2 - Hour Parity (even)
100 * 36 - 39 D1 - BCD (lsb first) Days
101 * 40 - 41 D10 - BCD (lsb first) 10 Days
102 * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
103 * 45 - 49 MO - BCD (lsb first) Month
105 * 51 - 53 Y1 - BCD (lsb first) Years
106 * 54 - 57 Y10 - BCD (lsb first) 10 Years
107 * 58 P3 - Date Parity (even)
108 * 59 - usually missing (minute indication), except for leap insertion
111 static struct rawdcfcode
113 char offset; /* start bit */
116 { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
117 { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
143 char offset; /* start bit of parity field */
146 { 21 }, { 29 }, { 36 }, { 59 }
153 #define DCF_Z_MET 0x2
154 #define DCF_Z_MED 0x1
158 register unsigned char *buf,
162 register unsigned long sum = 0;
163 register int i, first;
165 first = rawdcfcode[idx].offset;
167 for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
170 sum |= (buf[i] != '-');
177 register unsigned char *buf,
182 register unsigned psum = 1;
184 last = partab[idx+1].offset;
186 for (i = partab[idx].offset; i < last; i++)
187 psum ^= (buf[i] != '-');
194 register unsigned char *buffer,
196 register clocktime_t *clock_time
201 printf("%-30s", "*** INCOMPLETE");
206 * check Start and Parity bits
208 if ((ext_bf(buffer, DCF_S) == 1) &&
209 pcheck(buffer, DCF_P_P1) &&
210 pcheck(buffer, DCF_P_P2) &&
211 pcheck(buffer, DCF_P_P3))
217 clock_time->flags = 0;
218 clock_time->usecond= 0;
219 clock_time->second = 0;
220 clock_time->minute = ext_bf(buffer, DCF_M10);
221 clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1);
222 clock_time->hour = ext_bf(buffer, DCF_H10);
223 clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1);
224 clock_time->day = ext_bf(buffer, DCF_D10);
225 clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1);
226 clock_time->month = ext_bf(buffer, DCF_MO0);
227 clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO);
228 clock_time->year = ext_bf(buffer, DCF_Y10);
229 clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1);
230 clock_time->wday = ext_bf(buffer, DCF_DW);
232 switch (ext_bf(buffer, DCF_Z))
235 clock_time->utcoffset = -60;
239 clock_time->flags |= DCFB_DST;
240 clock_time->utcoffset = -120;
244 printf("%-30s", "*** BAD TIME ZONE");
245 return CVT_FAIL|CVT_BADFMT;
248 if (ext_bf(buffer, DCF_A1))
249 clock_time->flags |= DCFB_ANNOUNCE;
251 if (ext_bf(buffer, DCF_A2))
252 clock_time->flags |= DCFB_LEAP;
254 if (ext_bf(buffer, DCF_R))
255 clock_time->flags |= DCFB_ALTERNATE;
262 * bad format - not for us
264 printf("%-30s", "*** BAD FORMAT (invalid/parity)");
265 return CVT_FAIL|CVT_BADFMT;
278 static const char *wday[8] =
290 static char pat[] = "-\\|/";
292 #define LINES (24-2) /* error lines after which the two headlines are repeated */
300 if ((argc != 2) && (argc != 3))
302 fprintf(stderr, "usage: %s [-f|-t|-ft|-tf] <device>\n", argv[0]);
315 * SIMPLE(!) argument "parser"
319 if (strcmp(argv[1], "-f") == 0)
321 if (strcmp(argv[1], "-t") == 0)
323 if ((strcmp(argv[1], "-ft") == 0) ||
324 (strcmp(argv[1], "-tf") == 0))
336 fd = open(file, O_RDONLY);
348 struct timeval t, tt, tlast;
350 clocktime_t clock_time;
354 if (tcgetattr(fd, &term) == -1)
360 memset(term.c_cc, 0, sizeof(term.c_cc));
362 #ifdef NO_PARENB_IGNPAR /* Was: defined(SYS_IRIX4) || defined (SYS_IRIX5) */
363 /* somehow doesn't grok PARENB & IGNPAR (mj) */
364 term.c_cflag = B50|CS8|CREAD|CLOCAL;
366 term.c_cflag = B50|CS8|CREAD|CLOCAL|PARENB;
368 term.c_iflag = IGNPAR;
372 if (tcsetattr(fd, TCSANOW, &term) == -1)
379 while (ioctl(fd, I_POP, 0) == 0)
382 #if defined(TIOCMBIC) && defined(TIOCM_RTS)
383 if (ioctl(fd, TIOCMBIC, (caddr_t)&on) == -1)
389 printf(" DCF77 monitor - Copyright (C) 1993-1996, Frank Kardel\n\n");
392 clock_time.minute = 0;
395 clock_time.month = 0;
397 clock_time.flags = 0;
399 for ( i = 0; i < 60; i++)
402 gettimeofday(&tlast, 0L);
404 while (read(fd, &c, 1) == 1)
406 gettimeofday(&t, 0L);
408 t.tv_sec -= tlast.tv_sec;
409 t.tv_usec -= tlast.tv_usec;
412 t.tv_usec += 1000000;
418 printf(" %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
419 printf(" %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
427 printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
429 if ((rtc = convert_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK)
433 clock_time.minute = 0;
436 clock_time.month = 0;
438 clock_time.flags = 0;
442 if (((c^0xFF)+1) & (c^0xFF))
445 buf[0] = type(c) ? '#' : '-';
447 for ( i = 1; i < 60; i++)
454 if (((c^0xFF)+1) & (c^0xFF))
457 buf[i] = type(c) ? '#' : '-';
459 printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
464 printf("%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s>",
465 wday[clock_time.wday],
466 (int)clock_time.hour, (int)clock_time.minute, (int)i, (int)clock_time.day, (int)clock_time.month,
467 (int)clock_time.year,
468 (clock_time.flags & DCFB_ALTERNATE) ? "R" : "_",
469 (clock_time.flags & DCFB_ANNOUNCE) ? "A" : "_",
470 (clock_time.flags & DCFB_DST) ? "D" : "_",
471 (clock_time.flags & DCFB_LEAP) ? "L" : "_"
473 if (trace && (i == 0))