testdcf.c revision 54359
1/*
2 * /src/NTP/REPOSITORY/v4/parseutil/testdcf.c,v 3.18 1996/12/01 16:05:04 kardel Exp
3 *
4 * testdcf.c,v 3.18 1996/12/01 16:05:04 kardel Exp
5 *
6 * simple DCF77 100/200ms pulse test program (via 50Baud serial line)
7 *
8 * Copyright (c) 1993,1994,1995,1996, 1998 by Frank Kardel
9 * Friedrich-Alexander Universit�t Erlangen-N�rnberg, Germany
10 *
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.
14 *
15 * This program may not be sold or used for profit without prior
16 * written consent of the author.
17 */
18
19#include <unistd.h>
20#include <stdio.h>
21#include <fcntl.h>
22#include <termios.h>
23#include <sys/types.h>
24#include <sys/time.h>
25
26#include "ntp_stdlib.h"
27
28/*
29 * state flags
30 */
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 */
35
36struct clocktime		/* clock time broken up from time code */
37{
38	long wday;
39	long day;
40	long month;
41	long year;
42	long hour;
43	long minute;
44	long second;
45	long usecond;
46	long utcoffset;	/* in minutes */
47	long flags;		/* current clock status */
48};
49
50typedef struct clocktime clocktime_t;
51
52#define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
53
54/*
55 * parser related return/error codes
56 */
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) */
62
63/*
64 * DCF77 raw time code
65 *
66 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
67 * und Berlin, Maerz 1989
68 *
69 * Timecode transmission:
70 * AM:
71 *	time marks are send every second except for the second before the
72 *	next minute mark
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
78 * FM:
79 *	see the spec. (basically a (non-)inverted psuedo random phase shift)
80 *
81 * Encoding:
82 * Second	Contents
83 * 0  - 10	AM: free, FM: 0
84 * 11 - 14	free
85 * 15		R     - alternate antenna
86 * 16		A1    - expect zone change (1 hour before)
87 * 17 - 18	Z1,Z2 - time zone
88 *		 0  0 illegal
89 *		 0  1 MEZ  (MET)
90 *		 1  0 MESZ (MED, MET DST)
91 *		 1  1 illegal
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
104 * 50           MO0   - 10 Months
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
109 */
110
111static struct rawdcfcode
112{
113	char offset;			/* start bit */
114} rawdcfcode[] =
115{
116	{  0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
117	{ 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
118};
119
120#define DCF_M	0
121#define DCF_R	1
122#define DCF_A1	2
123#define DCF_Z	3
124#define DCF_A2	4
125#define DCF_S	5
126#define DCF_M1	6
127#define DCF_M10	7
128#define DCF_P1	8
129#define DCF_H1	9
130#define DCF_H10	10
131#define DCF_P2	11
132#define DCF_D1	12
133#define DCF_D10	13
134#define DCF_DW	14
135#define DCF_MO	15
136#define DCF_MO0	16
137#define DCF_Y1	17
138#define DCF_Y10	18
139#define DCF_P3	19
140
141static struct partab
142{
143	char offset;			/* start bit of parity field */
144} partab[] =
145{
146	{ 21 }, { 29 }, { 36 }, { 59 }
147};
148
149#define DCF_P_P1	0
150#define DCF_P_P2	1
151#define DCF_P_P3	2
152
153#define DCF_Z_MET 0x2
154#define DCF_Z_MED 0x1
155
156static unsigned long
157ext_bf(
158	register unsigned char *buf,
159	register int   idx
160	)
161{
162	register unsigned long sum = 0;
163	register int i, first;
164
165	first = rawdcfcode[idx].offset;
166
167	for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
168	{
169		sum <<= 1;
170		sum |= (buf[i] != '-');
171	}
172	return sum;
173}
174
175static unsigned
176pcheck(
177	register unsigned char *buf,
178	register int   idx
179	)
180{
181	register int i,last;
182	register unsigned psum = 1;
183
184	last = partab[idx+1].offset;
185
186	for (i = partab[idx].offset; i < last; i++)
187	    psum ^= (buf[i] != '-');
188
189	return psum;
190}
191
192static unsigned long
193convert_rawdcf(
194	register unsigned char   *buffer,
195	register int              size,
196	register clocktime_t     *clock_time
197	)
198{
199	if (size < 57)
200	{
201		printf("%-30s", "*** INCOMPLETE");
202		return CVT_NONE;
203	}
204
205	/*
206	 * check Start and Parity bits
207	 */
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))
212	{
213		/*
214		 * buffer OK
215		 */
216
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);
231
232		switch (ext_bf(buffer, DCF_Z))
233		{
234		    case DCF_Z_MET:
235			clock_time->utcoffset = -60;
236			break;
237
238		    case DCF_Z_MED:
239			clock_time->flags     |= DCFB_DST;
240			clock_time->utcoffset  = -120;
241			break;
242
243		    default:
244			printf("%-30s", "*** BAD TIME ZONE");
245			return CVT_FAIL|CVT_BADFMT;
246		}
247
248		if (ext_bf(buffer, DCF_A1))
249		    clock_time->flags |= DCFB_ANNOUNCE;
250
251		if (ext_bf(buffer, DCF_A2))
252		    clock_time->flags |= DCFB_LEAP;
253
254		if (ext_bf(buffer, DCF_R))
255		    clock_time->flags |= DCFB_ALTERNATE;
256
257		return CVT_OK;
258	}
259	else
260	{
261		/*
262		 * bad format - not for us
263		 */
264		printf("%-30s", "*** BAD FORMAT (invalid/parity)");
265		return CVT_FAIL|CVT_BADFMT;
266	}
267}
268
269char
270type(
271	unsigned int c
272	)
273{
274	c ^= 0xFF;
275	return (c > 0xF);
276}
277
278static const char *wday[8] =
279{
280	"??",
281	"Mo",
282	"Tu",
283	"We",
284	"Th",
285	"Fr",
286	"Sa",
287	"Su"
288};
289
290static char pat[] = "-\\|/";
291
292#define LINES (24-2)	/* error lines after which the two headlines are repeated */
293
294int
295main(
296	int argc,
297	char *argv[]
298	)
299{
300	if ((argc != 2) && (argc != 3))
301	{
302		fprintf(stderr, "usage: %s [-f|-t|-ft|-tf] <device>\n", argv[0]);
303		exit(1);
304	}
305	else
306	{
307		unsigned char c;
308		char *file;
309		int fd;
310		int offset = 15;
311		int trace = 0;
312		int errs = LINES+1;
313
314		/*
315		 * SIMPLE(!) argument "parser"
316		 */
317		if (argc == 3)
318		{
319			if (strcmp(argv[1], "-f") == 0)
320			    offset = 0;
321			if (strcmp(argv[1], "-t") == 0)
322			    trace = 1;
323			if ((strcmp(argv[1], "-ft") == 0) ||
324			    (strcmp(argv[1], "-tf") == 0))
325			{
326				offset = 0;
327				trace = 1;
328			}
329			file = argv[2];
330		}
331		else
332		{
333			file = argv[1];
334		}
335
336		fd = open(file, O_RDONLY);
337		if (fd == -1)
338		{
339			perror(file);
340			exit(1);
341		}
342		else
343		{
344			int i;
345#ifdef TIOCM_RTS
346			int on = TIOCM_RTS;
347#endif
348			struct timeval t, tt, tlast;
349			char buf[61];
350			clocktime_t clock_time;
351			struct termios term;
352			int rtc = CVT_NONE;
353
354			if (tcgetattr(fd,  &term) == -1)
355			{
356				perror("tcgetattr");
357				exit(1);
358			}
359
360			memset(term.c_cc, 0, sizeof(term.c_cc));
361			term.c_cc[VMIN] = 1;
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;
365#else
366			term.c_cflag = B50|CS8|CREAD|CLOCAL|PARENB;
367#endif
368			term.c_iflag = IGNPAR;
369			term.c_oflag = 0;
370			term.c_lflag = 0;
371
372			if (tcsetattr(fd, TCSANOW, &term) == -1)
373			{
374				perror("tcsetattr");
375				exit(1);
376			}
377
378#ifdef I_POP
379			while (ioctl(fd, I_POP, 0) == 0)
380			    ;
381#endif
382#if defined(TIOCMBIC) && defined(TIOCM_RTS)
383			if (ioctl(fd, TIOCMBIC, (caddr_t)&on) == -1)
384			{
385				perror("TIOCM_RTS");
386			}
387#endif
388
389			printf("  DCF77 monitor - Copyright (C) 1993-1996, Frank Kardel\n\n");
390
391			clock_time.hour = 0;
392			clock_time.minute = 0;
393			clock_time.day = 0;
394			clock_time.wday = 0;
395			clock_time.month = 0;
396			clock_time.year = 0;
397			clock_time.flags = 0;
398			buf[60] = '\0';
399			for ( i = 0; i < 60; i++)
400			    buf[i] = '.';
401
402			gettimeofday(&tlast, 0L);
403			i = 0;
404			while (read(fd, &c, 1) == 1)
405			{
406				gettimeofday(&t, 0L);
407				tt = t;
408				t.tv_sec -= tlast.tv_sec;
409				t.tv_usec -= tlast.tv_usec;
410				if (t.tv_usec < 0)
411				{
412					t.tv_usec += 1000000;
413					t.tv_sec  -= 1;
414				}
415
416				if (errs > LINES)
417				{
418					printf("  %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
419					printf("  %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
420					errs = 0;
421				}
422
423				if (t.tv_sec > 1 ||
424				    (t.tv_sec == 1 &&
425				     t.tv_usec > 500000))
426				{
427					printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
428
429					if ((rtc = convert_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK)
430					{
431						printf("\n");
432						clock_time.hour = 0;
433						clock_time.minute = 0;
434						clock_time.day = 0;
435						clock_time.wday = 0;
436						clock_time.month = 0;
437						clock_time.year = 0;
438						clock_time.flags = 0;
439						errs++;
440					}
441
442					if (((c^0xFF)+1) & (c^0xFF))
443					    buf[0] = '?';
444					else
445					    buf[0] = type(c) ? '#' : '-';
446
447					for ( i = 1; i < 60; i++)
448					    buf[i] = '.';
449
450					i = 0;
451				}
452				else
453				{
454					if (((c^0xFF)+1) & (c^0xFF))
455					    buf[i] = '?';
456					else
457					    buf[i] = type(c) ? '#' : '-';
458
459					printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
460				}
461
462				if (rtc == CVT_OK)
463				{
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" : "_"
472					       );
473					if (trace && (i == 0))
474					{
475						printf("\n");
476						errs++;
477					}
478				}
479
480				printf("\r");
481
482				if (i < 60)
483				{
484					i++;
485				}
486
487				tlast = tt;
488
489				fflush(stdout);
490			}
491			close(fd);
492		}
493	}
494	return 0;
495}
496