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