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