xref: /freebsd/contrib/ntp/parseutil/testdcf.c (revision b740c88bfb6453416926271c089262e7164dace3)
1 /*
2  * /src/NTP/ntp4-dev/parseutil/testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
3  *
4  * testdcf.c,v 4.10 2005/08/06 14:18:43 kardel RELEASE_20050806_A
5  *
6  * simple DCF77 100/200ms pulse test program (via 50Baud serial line)
7  *
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
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
22  *
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
33  * SUCH DAMAGE.
34  *
35  */
36 
37 #include <config.h>
38 #include "ntp_stdlib.h"
39 
40 #include <sys/ioctl.h>
41 #include <unistd.h>
42 #include <stdio.h>
43 #include <fcntl.h>
44 #include <termios.h>
45 
46 /*
47  * state flags
48  */
49 #define DCFB_ANNOUNCE           0x0001 /* switch time zone warning (DST switch) */
50 #define DCFB_DST                0x0002 /* DST in effect */
51 #define DCFB_LEAP		0x0004 /* LEAP warning (1 hour prior to occurrence) */
52 #define DCFB_ALTERNATE		0x0008 /* alternate antenna used */
53 
54 struct clocktime		/* clock time broken up from time code */
55 {
56 	long wday;
57 	long day;
58 	long month;
59 	long year;
60 	long hour;
61 	long minute;
62 	long second;
63 	long usecond;
64 	long utcoffset;	/* in minutes */
65 	long flags;		/* current clock status */
66 };
67 
68 typedef struct clocktime clocktime_t;
69 
70 static char type(unsigned int);
71 
72 #define TIMES10(_X_) (((_X_) << 3) + ((_X_) << 1))
73 
74 /*
75  * parser related return/error codes
76  */
77 #define CVT_MASK	0x0000000F /* conversion exit code */
78 #define   CVT_NONE	0x00000001 /* format not applicable */
79 #define   CVT_FAIL	0x00000002 /* conversion failed - error code returned */
80 #define   CVT_OK	0x00000004 /* conversion succeeded */
81 #define CVT_BADFMT	0x00000010 /* general format error - (unparsable) */
82 
83 /*
84  * DCF77 raw time code
85  *
86  * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig
87  * und Berlin, Maerz 1989
88  *
89  * Timecode transmission:
90  * AM:
91  *	time marks are send every second except for the second before the
92  *	next minute mark
93  *	time marks consist of a reduction of transmitter power to 25%
94  *	of the nominal level
95  *	the falling edge is the time indication (on time)
96  *	time marks of a 100ms duration constitute a logical 0
97  *	time marks of a 200ms duration constitute a logical 1
98  * FM:
99  *	see the spec. (basically a (non-)inverted psuedo random phase shift)
100  *
101  * Encoding:
102  * Second	Contents
103  * 0  - 10	AM: free, FM: 0
104  * 11 - 14	free
105  * 15		R     - alternate antenna
106  * 16		A1    - expect zone change (1 hour before)
107  * 17 - 18	Z1,Z2 - time zone
108  *		 0  0 illegal
109  *		 0  1 MEZ  (MET)
110  *		 1  0 MESZ (MED, MET DST)
111  *		 1  1 illegal
112  * 19		A2    - expect leap insertion/deletion (1 hour before)
113  * 20		S     - start of time code (1)
114  * 21 - 24	M1    - BCD (lsb first) Minutes
115  * 25 - 27	M10   - BCD (lsb first) 10 Minutes
116  * 28		P1    - Minute Parity (even)
117  * 29 - 32	H1    - BCD (lsb first) Hours
118  * 33 - 34      H10   - BCD (lsb first) 10 Hours
119  * 35		P2    - Hour Parity (even)
120  * 36 - 39	D1    - BCD (lsb first) Days
121  * 40 - 41	D10   - BCD (lsb first) 10 Days
122  * 42 - 44	DW    - BCD (lsb first) day of week (1: Monday -> 7: Sunday)
123  * 45 - 49	MO    - BCD (lsb first) Month
124  * 50           MO0   - 10 Months
125  * 51 - 53	Y1    - BCD (lsb first) Years
126  * 54 - 57	Y10   - BCD (lsb first) 10 Years
127  * 58 		P3    - Date Parity (even)
128  * 59		      - usually missing (minute indication), except for leap insertion
129  */
130 
131 static char revision[] = "4.10";
132 
133 static struct rawdcfcode
134 {
135 	char offset;			/* start bit */
136 } rawdcfcode[] =
137 {
138 	{  0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 },
139 	{ 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 }
140 };
141 
142 #define DCF_M	0
143 #define DCF_R	1
144 #define DCF_A1	2
145 #define DCF_Z	3
146 #define DCF_A2	4
147 #define DCF_S	5
148 #define DCF_M1	6
149 #define DCF_M10	7
150 #define DCF_P1	8
151 #define DCF_H1	9
152 #define DCF_H10	10
153 #define DCF_P2	11
154 #define DCF_D1	12
155 #define DCF_D10	13
156 #define DCF_DW	14
157 #define DCF_MO	15
158 #define DCF_MO0	16
159 #define DCF_Y1	17
160 #define DCF_Y10	18
161 #define DCF_P3	19
162 
163 static struct partab
164 {
165 	char offset;			/* start bit of parity field */
166 } partab[] =
167 {
168 	{ 21 }, { 29 }, { 36 }, { 59 }
169 };
170 
171 #define DCF_P_P1	0
172 #define DCF_P_P2	1
173 #define DCF_P_P3	2
174 
175 #define DCF_Z_MET 0x2
176 #define DCF_Z_MED 0x1
177 
178 static unsigned long
179 ext_bf(
180 	register unsigned char *buf,
181 	register int   idx
182 	)
183 {
184 	register unsigned long sum = 0;
185 	register int i, first;
186 
187 	first = rawdcfcode[idx].offset;
188 
189 	for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--)
190 	{
191 		sum <<= 1;
192 		sum |= (buf[i] != '-');
193 	}
194 	return sum;
195 }
196 
197 static unsigned
198 pcheck(
199 	register unsigned char *buf,
200 	register int   idx
201 	)
202 {
203 	register int i,last;
204 	register unsigned psum = 1;
205 
206 	last = partab[idx+1].offset;
207 
208 	for (i = partab[idx].offset; i < last; i++)
209 	    psum ^= (buf[i] != '-');
210 
211 	return psum;
212 }
213 
214 static unsigned long
215 convert_rawdcf(
216 	register unsigned char   *buffer,
217 	register int              size,
218 	register clocktime_t     *clock_time
219 	)
220 {
221 	if (size < 57)
222 	{
223 		printf("%-30s", "*** INCOMPLETE");
224 		return CVT_NONE;
225 	}
226 
227 	/*
228 	 * check Start and Parity bits
229 	 */
230 	if ((ext_bf(buffer, DCF_S) == 1) &&
231 	    pcheck(buffer, DCF_P_P1) &&
232 	    pcheck(buffer, DCF_P_P2) &&
233 	    pcheck(buffer, DCF_P_P3))
234 	{
235 		/*
236 		 * buffer OK
237 		 */
238 
239 		clock_time->flags  = 0;
240 		clock_time->usecond= 0;
241 		clock_time->second = 0;
242 		clock_time->minute = ext_bf(buffer, DCF_M10);
243 		clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1);
244 		clock_time->hour   = ext_bf(buffer, DCF_H10);
245 		clock_time->hour   = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1);
246 		clock_time->day    = ext_bf(buffer, DCF_D10);
247 		clock_time->day    = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1);
248 		clock_time->month  = ext_bf(buffer, DCF_MO0);
249 		clock_time->month  = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO);
250 		clock_time->year   = ext_bf(buffer, DCF_Y10);
251 		clock_time->year   = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1);
252 		clock_time->wday   = ext_bf(buffer, DCF_DW);
253 
254 		switch (ext_bf(buffer, DCF_Z))
255 		{
256 		    case DCF_Z_MET:
257 			clock_time->utcoffset = -60;
258 			break;
259 
260 		    case DCF_Z_MED:
261 			clock_time->flags     |= DCFB_DST;
262 			clock_time->utcoffset  = -120;
263 			break;
264 
265 		    default:
266 			printf("%-30s", "*** BAD TIME ZONE");
267 			return CVT_FAIL|CVT_BADFMT;
268 		}
269 
270 		if (ext_bf(buffer, DCF_A1))
271 		    clock_time->flags |= DCFB_ANNOUNCE;
272 
273 		if (ext_bf(buffer, DCF_A2))
274 		    clock_time->flags |= DCFB_LEAP;
275 
276 		if (ext_bf(buffer, DCF_R))
277 		    clock_time->flags |= DCFB_ALTERNATE;
278 
279 		return CVT_OK;
280 	}
281 	else
282 	{
283 		/*
284 		 * bad format - not for us
285 		 */
286 		printf("%-30s", "*** BAD FORMAT (invalid/parity)");
287 		return CVT_FAIL|CVT_BADFMT;
288 	}
289 }
290 
291 static char
292 type(
293 	unsigned int c
294 	)
295 {
296 	c ^= 0xFF;
297 	return (c >= 0xF);
298 }
299 
300 static const char *wday[8] =
301 {
302 	"??",
303 	"Mo",
304 	"Tu",
305 	"We",
306 	"Th",
307 	"Fr",
308 	"Sa",
309 	"Su"
310 };
311 
312 static char pat[] = "-\\|/";
313 
314 #define LINES (24-2)	/* error lines after which the two headlines are repeated */
315 
316 int
317 main(
318 	int argc,
319 	char *argv[]
320 	)
321 {
322 	if ((argc != 2) && (argc != 3))
323 	{
324 		fprintf(stderr, "usage: %s [-f|-t|-ft|-tf] <device>\n", argv[0]);
325 		exit(1);
326 	}
327 	else
328 	{
329 		unsigned char c;
330 		char *file;
331 		int fd;
332 		int offset = 15;
333 		int trace = 0;
334 		int errs = LINES+1;
335 
336 		/*
337 		 * SIMPLE(!) argument "parser"
338 		 */
339 		if (argc == 3)
340 		{
341 			if (strcmp(argv[1], "-f") == 0)
342 			    offset = 0;
343 			if (strcmp(argv[1], "-t") == 0)
344 			    trace = 1;
345 			if ((strcmp(argv[1], "-ft") == 0) ||
346 			    (strcmp(argv[1], "-tf") == 0))
347 			{
348 				offset = 0;
349 				trace = 1;
350 			}
351 			file = argv[2];
352 		}
353 		else
354 		{
355 			file = argv[1];
356 		}
357 
358 		fd = open(file, O_RDONLY);
359 		if (fd == -1)
360 		{
361 			perror(file);
362 			exit(1);
363 		}
364 		else
365 		{
366 			int i;
367 #ifdef TIOCM_RTS
368 			int on = TIOCM_RTS;
369 #endif
370 			struct timeval t, tt, tlast;
371 			char buf[61];
372 			clocktime_t clock_time;
373 			struct termios term;
374 			int rtc = CVT_NONE;
375 
376 			if (tcgetattr(fd,  &term) == -1)
377 			{
378 				perror("tcgetattr");
379 				exit(1);
380 			}
381 
382 			memset(term.c_cc, 0, sizeof(term.c_cc));
383 			term.c_cc[VMIN] = 1;
384 #ifdef NO_PARENB_IGNPAR /* Was: defined(SYS_IRIX4) || defined (SYS_IRIX5) */
385 			/* somehow doesn't grok PARENB & IGNPAR (mj) */
386 			term.c_cflag = CS8|CREAD|CLOCAL;
387 #else
388 			term.c_cflag = CS8|CREAD|CLOCAL|PARENB;
389 #endif
390 			term.c_iflag = IGNPAR;
391 			term.c_oflag = 0;
392 			term.c_lflag = 0;
393 
394 			cfsetispeed(&term, B50);
395 			cfsetospeed(&term, B50);
396 
397 			if (tcsetattr(fd, TCSANOW, &term) == -1)
398 			{
399 				perror("tcsetattr");
400 				exit(1);
401 			}
402 
403 #ifdef I_POP
404 			while (ioctl(fd, I_POP, 0) == 0)
405 			    ;
406 #endif
407 #if defined(TIOCMBIC) && defined(TIOCM_RTS)
408 			if (ioctl(fd, TIOCMBIC, (caddr_t)&on) == -1)
409 			{
410 				perror("TIOCM_RTS");
411 			}
412 #endif
413 
414 			printf("  DCF77 monitor %s - Copyright (C) 1993-2005, Frank Kardel\n\n", revision);
415 
416 			clock_time.hour = 0;
417 			clock_time.minute = 0;
418 			clock_time.day = 0;
419 			clock_time.wday = 0;
420 			clock_time.month = 0;
421 			clock_time.year = 0;
422 			clock_time.flags = 0;
423 			buf[60] = '\0';
424 			for ( i = 0; i < 60; i++)
425 			    buf[i] = '.';
426 
427 			gettimeofday(&tlast, 0L);
428 			i = 0;
429 			while (read(fd, &c, 1) == 1)
430 			{
431 				gettimeofday(&t, 0L);
432 				tt = t;
433 				t.tv_sec -= tlast.tv_sec;
434 				t.tv_usec -= tlast.tv_usec;
435 				if (t.tv_usec < 0)
436 				{
437 					t.tv_usec += 1000000;
438 					t.tv_sec  -= 1;
439 				}
440 
441 				if (errs > LINES)
442 				{
443 					printf("  %s", &"PTB private....RADMLSMin....PHour..PMDay..DayMonthYear....P\n"[offset]);
444 					printf("  %s", &"---------------RADMLS1248124P124812P1248121241248112481248P\n"[offset]);
445 					errs = 0;
446 				}
447 
448 				if (t.tv_sec > 1 ||
449 				    (t.tv_sec == 1 &&
450 				     t.tv_usec > 500000))
451 				{
452 					printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
453 
454 					if ((rtc = convert_rawdcf((unsigned char *)buf, i, &clock_time)) != CVT_OK)
455 					{
456 						printf("\n");
457 						clock_time.hour = 0;
458 						clock_time.minute = 0;
459 						clock_time.day = 0;
460 						clock_time.wday = 0;
461 						clock_time.month = 0;
462 						clock_time.year = 0;
463 						clock_time.flags = 0;
464 						errs++;
465 					}
466 
467 					if (((c^0xFF)+1) & (c^0xFF))
468 					    buf[0] = '?';
469 					else
470 					    buf[0] = type(c) ? '#' : '-';
471 
472 					for ( i = 1; i < 60; i++)
473 					    buf[i] = '.';
474 
475 					i = 0;
476 				}
477 				else
478 				{
479 					if (((c^0xFF)+1) & (c^0xFF))
480 					    buf[i] = '?';
481 					else
482 					    buf[i] = type(c) ? '#' : '-';
483 
484 					printf("%c %.*s ", pat[i % (sizeof(pat)-1)], 59 - offset, &buf[offset]);
485 				}
486 
487 				if (rtc == CVT_OK)
488 				{
489 					printf("%s, %2d:%02d:%02d, %d.%02d.%02d, <%s%s%s%s>",
490 					       wday[clock_time.wday],
491 					       (int)clock_time.hour, (int)clock_time.minute, (int)i, (int)clock_time.day, (int)clock_time.month,
492 					       (int)clock_time.year,
493 					       (clock_time.flags & DCFB_ALTERNATE) ? "R" : "_",
494 					       (clock_time.flags & DCFB_ANNOUNCE) ? "A" : "_",
495 					       (clock_time.flags & DCFB_DST) ? "D" : "_",
496 					       (clock_time.flags & DCFB_LEAP) ? "L" : "_"
497 					       );
498 					if (trace && (i == 0))
499 					{
500 						printf("\n");
501 						errs++;
502 					}
503 				}
504 
505 				printf("\r");
506 
507 				if (i < 60)
508 				{
509 					i++;
510 				}
511 
512 				tlast = tt;
513 
514 				fflush(stdout);
515 			}
516 			close(fd);
517 		}
518 	}
519 	return 0;
520 }
521 
522 /*
523  * History:
524  *
525  * testdcf.c,v
526  * Revision 4.10  2005/08/06 14:18:43  kardel
527  * cleanup warnings
528  *
529  * Revision 4.9  2005/08/06 14:14:38  kardel
530  * document revision on startup
531  *
532  * Revision 4.8  2005/08/06 14:10:08  kardel
533  * fix setting of baud rate
534  *
535  * Revision 4.7  2005/04/16 17:32:10  kardel
536  * update copyright
537  *
538  * Revision 4.6  2004/11/14 15:29:42  kardel
539  * support PPSAPI, upgrade Copyright to Berkeley style
540  *
541  */
542