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