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.
19 #include "ntp_stdlib.h"
29 #define DCFB_ANNOUNCE 0x0001 /* switch time zone warning (DST switch) */
30 #define DCFB_DST 0x0002 /* DST in effect */
31 #define DCFB_LEAP 0x0004 /* LEAP warning (1 hour prior to occurence) */
32 #define DCFB_ALTERNATE 0x0008 /* alternate antenna used */
34 struct clocktime /* clock time broken up from time code */
44 long utcoffset; /* in minutes */
45 long flags; /* current clock status */
48 typedef struct clocktime clocktime_t;
50 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
53 * parser related return/error codes
55 #define CVT_MASK 0x0000000F /* conversion exit code */
56 #define CVT_NONE 0x00000001 /* format not applicable */
57 #define CVT_FAIL 0x00000002 /* conversion failed - error code returned */
58 #define CVT_OK 0x00000004 /* conversion succeeded */
59 #define CVT_BADFMT 0x00000010 /* general format error - (unparsable) */
64 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
65 * und Berlin, Maerz 1989
67 * Timecode transmission:
69 * time marks are send every second except for the second before the
71 * time marks consist of a reduction of transmitter power to 25%
72 * of the nominal level
73 * the falling edge is the time indication (on time)
74 * time marks of a 100ms duration constitute a logical 0
75 * time marks of a 200ms duration constitute a logical 1
77 * see the spec. (basically a (non-)inverted psuedo random phase shift)
81 * 0 - 10 AM: free, FM: 0
83 * 15 R - alternate antenna
84 * 16 A1 - expect zone change (1 hour before)
85 * 17 - 18 Z1,Z2 - time zone
88 * 1 0 MESZ (MED, MET DST)
90 * 19 A2 - expect leap insertion/deletion (1 hour before)
91 * 20 S - start of time code (1)
92 * 21 - 24 M1 - BCD (lsb first) Minutes
93 * 25 - 27 M10 - BCD (lsb first) 10 Minutes
94 * 28 P1 - Minute Parity (even)
95 * 29 - 32 H1 - BCD (lsb first) Hours
96 * 33 - 34 H10 - BCD (lsb first) 10 Hours
97 * 35 P2 - Hour Parity (even)
98 * 36 - 39 D1 - BCD (lsb first) Days
99 * 40 - 41 D10 - BCD (lsb first) 10 Days
100 * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
101 * 45 - 49 MO - BCD (lsb first) Month
103 * 51 - 53 Y1 - BCD (lsb first) Years
104 * 54 - 57 Y10 - BCD (lsb first) 10 Years
105 * 58 P3 - Date Parity (even)
106 * 59 - usually missing (minute indication), except for leap insertion
109 static struct rawdcfcode
111 char offset; /* start bit */
114 { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
115 { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
141 char offset; /* start bit of parity field */
144 { 21 }, { 29 }, { 36 }, { 59 }
151 #define DCF_Z_MET 0x2
152 #define DCF_Z_MED 0x1
156 register unsigned char *buf,
160 register unsigned long sum = 0;
161 register int i, first;
163 first = rawdcfcode[idx].offset;
165 for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
168 sum |= (buf[i] != '-');
175 register unsigned char *buf,
180 register unsigned psum = 1;
182 last = partab[idx+1].offset;
184 for (i = partab[idx].offset; i < last; i++)
185 psum ^= (buf[i] != '-');
192 register unsigned char *buffer,
194 register clocktime_t *clock_time
199 printf("%-30s", "*** INCOMPLETE");
204 * check Start and Parity bits
206 if ((ext_bf(buffer, DCF_S) == 1) &&
207 pcheck(buffer, DCF_P_P1) &&
208 pcheck(buffer, DCF_P_P2) &&
209 pcheck(buffer, DCF_P_P3))
215 clock_time->flags = 0;
216 clock_time->usecond= 0;
217 clock_time->second = 0;
218 clock_time->minute = ext_bf(buffer, DCF_M10);
219 clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1);
220 clock_time->hour = ext_bf(buffer, DCF_H10);
221 clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1);
222 clock_time->day = ext_bf(buffer, DCF_D10);
223 clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1);
224 clock_time->month = ext_bf(buffer, DCF_MO0);
225 clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO);
226 clock_time->year = ext_bf(buffer, DCF_Y10);
227 clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1);
228 clock_time->wday = ext_bf(buffer, DCF_DW);
230 switch (ext_bf(buffer, DCF_Z))
233 clock_time->utcoffset = -60;
237 clock_time->flags |= DCFB_DST;
238 clock_time->utcoffset = -120;
242 printf("%-30s", "*** BAD TIME ZONE");
243 return CVT_FAIL|CVT_BADFMT;
246 if (ext_bf(buffer, DCF_A1))
247 clock_time->flags |= DCFB_ANNOUNCE;
249 if (ext_bf(buffer, DCF_A2))
250 clock_time->flags |= DCFB_LEAP;
252 if (ext_bf(buffer, DCF_R))
253 clock_time->flags |= DCFB_ALTERNATE;
260 * bad format - not for us
262 printf("%-30s", "*** BAD FORMAT (invalid/parity)");
263 return CVT_FAIL|CVT_BADFMT;
276 static const char *wday[8] =
288 static char pat[] = "-\\|/";
290 #define LINES (24-2) /* error lines after which the two headlines are repeated */
298 if ((argc != 2) && (argc != 3))
300 fprintf(stderr, "usage: %s [-f|-t|-ft|-tf] <device>\n", argv[0]);
313 * SIMPLE(!) argument "parser"
317 if (strcmp(argv[1], "-f") == 0)
319 if (strcmp(argv[1], "-t") == 0)
321 if ((strcmp(argv[1], "-ft") == 0) ||
322 (strcmp(argv[1], "-tf") == 0))
334 fd = open(file, O_RDONLY);
346 struct timeval t, tt, tlast;
348 clocktime_t clock_time;
352 if (tcgetattr(fd, &term) == -1)
358 memset(term.c_cc, 0, sizeof(term.c_cc));
360 #ifdef NO_PARENB_IGNPAR /* Was: defined(SYS_IRIX4) || defined (SYS_IRIX5) */
361 /* somehow doesn't grok PARENB & IGNPAR (mj) */
362 term.c_cflag = B50|CS8|CREAD|CLOCAL;
364 term.c_cflag = B50|CS8|CREAD|CLOCAL|PARENB;
366 term.c_iflag = IGNPAR;
370 if (tcsetattr(fd, TCSANOW, &term) == -1)
377 while (ioctl(fd, I_POP, 0) == 0)
380 #if defined(TIOCMBIC) && defined(TIOCM_RTS)
381 if (ioctl(fd, TIOCMBIC, (caddr_t)&on) == -1)
387 printf(" DCF77 monitor - Copyright (C) 1993-1996, Frank Kardel\n\n");
390 clock_time.minute = 0;
393 clock_time.month = 0;
395 clock_time.flags = 0;
397 for ( i = 0; i < 60; i++)
400 gettimeofday(&tlast, 0L);
402 while (read(fd, &c, 1) == 1)
404 gettimeofday(&t, 0L);
406 t.tv_sec -= tlast.tv_sec;
407 t.tv_usec -= tlast.tv_usec;
410 t.tv_usec += 1000000;
416 printf(" %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
417 printf(" %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
425 printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
427 if ((rtc = convert_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK)
431 clock_time.minute = 0;
434 clock_time.month = 0;
436 clock_time.flags = 0;
440 if (((c^0xFF)+1) & (c^0xFF))
443 buf[0] = type(c) ? '#' : '-';
445 for ( i = 1; i < 60; i++)
452 if (((c^0xFF)+1) & (c^0xFF))
455 buf[i] = type(c) ? '#' : '-';
457 printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
462 printf("%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s>",
463 wday[clock_time.wday],
464 (int)clock_time.hour, (int)clock_time.minute, (int)i, (int)clock_time.day, (int)clock_time.month,
465 (int)clock_time.year,
466 (clock_time.flags & DCFB_ALTERNATE) ? "R" : "_",
467 (clock_time.flags & DCFB_ANNOUNCE) ? "A" : "_",
468 (clock_time.flags & DCFB_DST) ? "D" : "_",
469 (clock_time.flags & DCFB_LEAP) ? "L" : "_"
471 if (trace && (i == 0))