1 /* 2 * /src/NTP/ntp-4/libparse/clk_rawdcf.c,v 4.9 1999/12/06 13:42:23 kardel Exp 3 * 4 * clk_rawdcf.c,v 4.9 1999/12/06 13:42:23 kardel Exp 5 * 6 * Raw DCF77 pulse clock support 7 * 8 * Copyright (C) 1992-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 */ 16 17 #ifdef HAVE_CONFIG_H 18 # include <config.h> 19 #endif 20 21 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_RAWDCF) 22 23 #include <sys/types.h> 24 #include <sys/time.h> 25 26 #include "ntp_fp.h" 27 #include "ntp_unixtime.h" 28 #include "ntp_calendar.h" 29 30 #include "parse.h" 31 #ifdef PARSESTREAM 32 # include <sys/parsestreams.h> 33 #endif 34 35 #ifndef PARSEKERNEL 36 # include "ntp_stdlib.h" 37 #endif 38 39 /* 40 * DCF77 raw time code 41 * 42 * From "Zur Zeit", Physikalisch-Technische Bundesanstalt (PTB), Braunschweig 43 * und Berlin, Maerz 1989 44 * 45 * Timecode transmission: 46 * AM: 47 * time marks are send every second except for the second before the 48 * next minute mark 49 * time marks consist of a reduction of transmitter power to 25% 50 * of the nominal level 51 * the falling edge is the time indication (on time) 52 * time marks of a 100ms duration constitute a logical 0 53 * time marks of a 200ms duration constitute a logical 1 54 * FM: 55 * see the spec. (basically a (non-)inverted psuedo random phase shift) 56 * 57 * Encoding: 58 * Second Contents 59 * 0 - 10 AM: free, FM: 0 60 * 11 - 14 free 61 * 15 R - alternate antenna 62 * 16 A1 - expect zone change (1 hour before) 63 * 17 - 18 Z1,Z2 - time zone 64 * 0 0 illegal 65 * 0 1 MEZ (MET) 66 * 1 0 MESZ (MED, MET DST) 67 * 1 1 illegal 68 * 19 A2 - expect leap insertion/deletion (1 hour before) 69 * 20 S - start of time code (1) 70 * 21 - 24 M1 - BCD (lsb first) Minutes 71 * 25 - 27 M10 - BCD (lsb first) 10 Minutes 72 * 28 P1 - Minute Parity (even) 73 * 29 - 32 H1 - BCD (lsb first) Hours 74 * 33 - 34 H10 - BCD (lsb first) 10 Hours 75 * 35 P2 - Hour Parity (even) 76 * 36 - 39 D1 - BCD (lsb first) Days 77 * 40 - 41 D10 - BCD (lsb first) 10 Days 78 * 42 - 44 DW - BCD (lsb first) day of week (1: Monday -> 7: Sunday) 79 * 45 - 49 MO - BCD (lsb first) Month 80 * 50 MO0 - 10 Months 81 * 51 - 53 Y1 - BCD (lsb first) Years 82 * 54 - 57 Y10 - BCD (lsb first) 10 Years 83 * 58 P3 - Date Parity (even) 84 * 59 - usually missing (minute indication), except for leap insertion 85 */ 86 87 static u_long pps_rawdcf P((parse_t *, int, timestamp_t *)); 88 static u_long cvt_rawdcf P((unsigned char *, int, struct format *, clocktime_t *, void *)); 89 static u_long inp_rawdcf P((parse_t *, unsigned int, timestamp_t *)); 90 91 typedef struct last_tcode { 92 time_t tcode; /* last converted time code */ 93 } last_tcode_t; 94 95 clockformat_t clock_rawdcf = 96 { 97 inp_rawdcf, /* DCF77 input handling */ 98 cvt_rawdcf, /* raw dcf input conversion */ 99 pps_rawdcf, /* examining PPS information */ 100 0, /* no private configuration data */ 101 "RAW DCF77 Timecode", /* direct decoding / time synthesis */ 102 103 61, /* bit buffer */ 104 sizeof(last_tcode_t) 105 }; 106 107 static struct dcfparam 108 { 109 unsigned char onebits[60]; 110 unsigned char zerobits[60]; 111 } dcfparameter = 112 { 113 "###############RADMLS1248124P124812P1248121241248112481248P", /* 'ONE' representation */ 114 "--------------------s-------p------p----------------------p" /* 'ZERO' representation */ 115 }; 116 117 static struct rawdcfcode 118 { 119 char offset; /* start bit */ 120 } rawdcfcode[] = 121 { 122 { 0 }, { 15 }, { 16 }, { 17 }, { 19 }, { 20 }, { 21 }, { 25 }, { 28 }, { 29 }, 123 { 33 }, { 35 }, { 36 }, { 40 }, { 42 }, { 45 }, { 49 }, { 50 }, { 54 }, { 58 }, { 59 } 124 }; 125 126 #define DCF_M 0 127 #define DCF_R 1 128 #define DCF_A1 2 129 #define DCF_Z 3 130 #define DCF_A2 4 131 #define DCF_S 5 132 #define DCF_M1 6 133 #define DCF_M10 7 134 #define DCF_P1 8 135 #define DCF_H1 9 136 #define DCF_H10 10 137 #define DCF_P2 11 138 #define DCF_D1 12 139 #define DCF_D10 13 140 #define DCF_DW 14 141 #define DCF_MO 15 142 #define DCF_MO0 16 143 #define DCF_Y1 17 144 #define DCF_Y10 18 145 #define DCF_P3 19 146 147 static struct partab 148 { 149 char offset; /* start bit of parity field */ 150 } partab[] = 151 { 152 { 21 }, { 29 }, { 36 }, { 59 } 153 }; 154 155 #define DCF_P_P1 0 156 #define DCF_P_P2 1 157 #define DCF_P_P3 2 158 159 #define DCF_Z_MET 0x2 160 #define DCF_Z_MED 0x1 161 162 static u_long 163 ext_bf( 164 register unsigned char *buf, 165 register int idx, 166 register unsigned char *zero 167 ) 168 { 169 register u_long sum = 0; 170 register int i, first; 171 172 first = rawdcfcode[idx].offset; 173 174 for (i = rawdcfcode[idx+1].offset - 1; i >= first; i--) 175 { 176 sum <<= 1; 177 sum |= (buf[i] != zero[i]); 178 } 179 return sum; 180 } 181 182 static unsigned 183 pcheck( 184 unsigned char *buf, 185 int idx, 186 unsigned char *zero 187 ) 188 { 189 int i,last; 190 unsigned psum = 1; 191 192 last = partab[idx+1].offset; 193 194 for (i = partab[idx].offset; i < last; i++) 195 psum ^= (buf[i] != zero[i]); 196 197 return psum; 198 } 199 200 static u_long 201 convert_rawdcf( 202 unsigned char *buffer, 203 int size, 204 struct dcfparam *dcfprm, 205 clocktime_t *clock_time 206 ) 207 { 208 register unsigned char *s = buffer; 209 register unsigned char *b = dcfprm->onebits; 210 register unsigned char *c = dcfprm->zerobits; 211 register int i; 212 213 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: \"%s\"\n", buffer)); 214 215 if (size < 57) 216 { 217 #ifndef PARSEKERNEL 218 msyslog(LOG_ERR, "parse: convert_rawdcf: INCOMPLETE DATA - time code only has %d bits\n", size); 219 #endif 220 return CVT_NONE; 221 } 222 223 for (i = 0; i < 58; i++) 224 { 225 if ((*s != *b) && (*s != *c)) 226 { 227 /* 228 * we only have two types of bytes (ones and zeros) 229 */ 230 #ifndef PARSEKERNEL 231 msyslog(LOG_ERR, "parse: convert_rawdcf: BAD DATA - no conversion for \"%s\"\n", buffer); 232 #endif 233 return CVT_NONE; 234 } 235 b++; 236 c++; 237 s++; 238 } 239 240 /* 241 * check Start and Parity bits 242 */ 243 if ((ext_bf(buffer, DCF_S, dcfprm->zerobits) == 1) && 244 pcheck(buffer, DCF_P_P1, dcfprm->zerobits) && 245 pcheck(buffer, DCF_P_P2, dcfprm->zerobits) && 246 pcheck(buffer, DCF_P_P3, dcfprm->zerobits)) 247 { 248 /* 249 * buffer OK 250 */ 251 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: parity check passed\n")); 252 253 clock_time->flags = PARSEB_S_ANTENNA|PARSEB_S_LEAP; 254 clock_time->utctime= 0; 255 clock_time->usecond= 0; 256 clock_time->second = 0; 257 clock_time->minute = ext_bf(buffer, DCF_M10, dcfprm->zerobits); 258 clock_time->minute = TIMES10(clock_time->minute) + ext_bf(buffer, DCF_M1, dcfprm->zerobits); 259 clock_time->hour = ext_bf(buffer, DCF_H10, dcfprm->zerobits); 260 clock_time->hour = TIMES10(clock_time->hour) + ext_bf(buffer, DCF_H1, dcfprm->zerobits); 261 clock_time->day = ext_bf(buffer, DCF_D10, dcfprm->zerobits); 262 clock_time->day = TIMES10(clock_time->day) + ext_bf(buffer, DCF_D1, dcfprm->zerobits); 263 clock_time->month = ext_bf(buffer, DCF_MO0, dcfprm->zerobits); 264 clock_time->month = TIMES10(clock_time->month) + ext_bf(buffer, DCF_MO, dcfprm->zerobits); 265 clock_time->year = ext_bf(buffer, DCF_Y10, dcfprm->zerobits); 266 clock_time->year = TIMES10(clock_time->year) + ext_bf(buffer, DCF_Y1, dcfprm->zerobits); 267 268 switch (ext_bf(buffer, DCF_Z, dcfprm->zerobits)) 269 { 270 case DCF_Z_MET: 271 clock_time->utcoffset = -1*60*60; 272 break; 273 274 case DCF_Z_MED: 275 clock_time->flags |= PARSEB_DST; 276 clock_time->utcoffset = -2*60*60; 277 break; 278 279 default: 280 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: BAD TIME ZONE\n")); 281 return CVT_FAIL|CVT_BADFMT; 282 } 283 284 if (ext_bf(buffer, DCF_A1, dcfprm->zerobits)) 285 clock_time->flags |= PARSEB_ANNOUNCE; 286 287 if (ext_bf(buffer, DCF_A2, dcfprm->zerobits)) 288 clock_time->flags |= PARSEB_LEAPADD; /* default: DCF77 data format deficiency */ 289 290 if (ext_bf(buffer, DCF_R, dcfprm->zerobits)) 291 clock_time->flags |= PARSEB_ALTERNATE; 292 293 parseprintf(DD_RAWDCF,("parse: convert_rawdcf: TIME CODE OK: %d:%d, %d.%d.%d, flags 0x%lx\n", 294 (int)clock_time->hour, (int)clock_time->minute, (int)clock_time->day, (int)clock_time->month,(int) clock_time->year, 295 (u_long)clock_time->flags)); 296 return CVT_OK; 297 } 298 else 299 { 300 /* 301 * bad format - not for us 302 */ 303 #ifndef PARSEKERNEL 304 msyslog(LOG_ERR, "parse: convert_rawdcf: parity check FAILED for \"%s\"\n", buffer); 305 #endif 306 return CVT_FAIL|CVT_BADFMT; 307 } 308 } 309 310 /* 311 * raw dcf input routine - needs to fix up 50 baud 312 * characters for 1/0 decision 313 */ 314 static u_long 315 cvt_rawdcf( 316 unsigned char *buffer, 317 int size, 318 struct format *param, 319 clocktime_t *clock_time, 320 void *local 321 ) 322 { 323 last_tcode_t *t = (last_tcode_t *)local; 324 register unsigned char *s = (unsigned char *)buffer; 325 register unsigned char *e = s + size; 326 register unsigned char *b = dcfparameter.onebits; 327 register unsigned char *c = dcfparameter.zerobits; 328 u_long rtc = CVT_NONE; 329 register unsigned int i, lowmax, highmax, cutoff, span; 330 #define BITS 9 331 unsigned char histbuf[BITS]; 332 /* 333 * the input buffer contains characters with runs of consecutive 334 * bits set. These set bits are an indication of the DCF77 pulse 335 * length. We assume that we receive the pulse at 50 Baud. Thus 336 * a 100ms pulse would generate a 4 bit train (20ms per bit and 337 * start bit) 338 * a 200ms pulse would create all zeroes (and probably a frame error) 339 */ 340 341 for (i = 0; i < BITS; i++) 342 { 343 histbuf[i] = 0; 344 } 345 346 cutoff = 0; 347 lowmax = 0; 348 349 while (s < e) 350 { 351 register unsigned int ch = *s ^ 0xFF; 352 /* 353 * these lines are left as an excercise to the reader 8-) 354 */ 355 if (!((ch+1) & ch) || !*s) 356 { 357 358 for (i = 0; ch; i++) 359 { 360 ch >>= 1; 361 } 362 363 *s = i; 364 histbuf[i]++; 365 cutoff += i; 366 lowmax++; 367 } 368 else 369 { 370 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: character check for 0x%x@%d FAILED\n", *s, (int)(s - (unsigned char *)buffer))); 371 *s = (unsigned char)~0; 372 rtc = CVT_FAIL|CVT_BADFMT; 373 } 374 s++; 375 } 376 377 if (lowmax) 378 { 379 cutoff /= lowmax; 380 } 381 else 382 { 383 cutoff = 4; /* doesn't really matter - it'll fail anyway, but gives error output */ 384 } 385 386 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: average bit count: %d\n", cutoff)); 387 388 lowmax = 0; 389 highmax = 0; 390 391 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: histogram:")); 392 for (i = 0; i <= cutoff; i++) 393 { 394 lowmax+=histbuf[i] * i; 395 highmax += histbuf[i]; 396 parseprintf(DD_RAWDCF,(" %d", histbuf[i])); 397 } 398 parseprintf(DD_RAWDCF, (" <M>")); 399 400 lowmax += highmax / 2; 401 402 if (highmax) 403 { 404 lowmax /= highmax; 405 } 406 else 407 { 408 lowmax = 0; 409 } 410 411 highmax = 0; 412 cutoff = 0; 413 414 for (; i < BITS; i++) 415 { 416 highmax+=histbuf[i] * i; 417 cutoff +=histbuf[i]; 418 parseprintf(DD_RAWDCF,(" %d", histbuf[i])); 419 } 420 parseprintf(DD_RAWDCF,("\n")); 421 422 if (cutoff) 423 { 424 highmax /= cutoff; 425 } 426 else 427 { 428 highmax = BITS-1; 429 } 430 431 span = cutoff = lowmax; 432 for (i = lowmax; i <= highmax; i++) 433 { 434 if (histbuf[cutoff] > histbuf[i]) 435 { 436 cutoff = i; 437 span = i; 438 } 439 else 440 if (histbuf[cutoff] == histbuf[i]) 441 { 442 span = i; 443 } 444 } 445 446 cutoff = (cutoff + span) / 2; 447 448 parseprintf(DD_RAWDCF,("parse: cvt_rawdcf: lower maximum %d, higher maximum %d, cutoff %d\n", lowmax, highmax, cutoff)); 449 450 s = (unsigned char *)buffer; 451 while ((s < e) && *c && *b) 452 { 453 if (*s == (unsigned char)~0) 454 { 455 *s = '?'; 456 } 457 else 458 { 459 *s = (*s >= cutoff) ? *b : *c; 460 } 461 s++; 462 b++; 463 c++; 464 } 465 466 if (rtc == CVT_NONE) 467 { 468 rtc = convert_rawdcf(buffer, size, &dcfparameter, clock_time); 469 if (rtc == CVT_OK) 470 { 471 time_t newtime; 472 473 newtime = parse_to_unixtime(clock_time, &rtc); 474 if ((rtc == CVT_OK) && t) 475 { 476 if ((newtime - t->tcode) == 60) /* guard against multi bit errors */ 477 { 478 clock_time->utctime = newtime; 479 } 480 else 481 { 482 rtc = CVT_FAIL|CVT_BADTIME; 483 } 484 t->tcode = newtime; 485 } 486 } 487 } 488 489 return rtc; 490 } 491 492 /* 493 * pps_rawdcf 494 * 495 * currently a very stupid version - should be extended to decode 496 * also ones and zeros (which is easy) 497 */ 498 /*ARGSUSED*/ 499 static u_long 500 pps_rawdcf( 501 register parse_t *parseio, 502 register int status, 503 register timestamp_t *ptime 504 ) 505 { 506 if (!status) /* negative edge for simpler wiring (Rx->DCD) */ 507 { 508 parseio->parse_dtime.parse_ptime = *ptime; 509 parseio->parse_dtime.parse_state |= PARSEB_PPS|PARSEB_S_PPS; 510 } 511 512 return CVT_NONE; 513 } 514 515 static u_long 516 snt_rawdcf( 517 register parse_t *parseio, 518 register timestamp_t *ptime 519 ) 520 { 521 if ((parseio->parse_dtime.parse_status & CVT_MASK) == CVT_OK) 522 { 523 parseio->parse_dtime.parse_stime = *ptime; 524 525 #ifdef PARSEKERNEL 526 parseio->parse_dtime.parse_time.tv.tv_sec++; 527 #else 528 parseio->parse_dtime.parse_time.fp.l_ui++; 529 #endif 530 531 parseprintf(DD_RAWDCF,("parse: snt_rawdcf: time stamp synthesized offset %d seconds\n", parseio->parse_index - 1)); 532 533 return updatetimeinfo(parseio, parseio->parse_lstate); 534 } 535 return CVT_NONE; 536 } 537 538 /* 539 * inp_rawdcf 540 * 541 * grep DCF77 data from input stream 542 */ 543 static u_long 544 inp_rawdcf( 545 parse_t *parseio, 546 unsigned int ch, 547 timestamp_t *tstamp 548 ) 549 { 550 static struct timeval timeout = { 1, 500000 }; /* 1.5 secongs denote second #60 */ 551 552 parseprintf(DD_PARSE, ("inp_rawdcf(0x%x, 0x%x, ...)\n", (int)parseio, (int)ch)); 553 554 parseio->parse_dtime.parse_stime = *tstamp; /* collect timestamp */ 555 556 if (parse_timedout(parseio, tstamp, &timeout)) 557 { 558 parseprintf(DD_PARSE, ("inp_rawdcf: time out seen\n")); 559 560 (void) parse_end(parseio); 561 (void) parse_addchar(parseio, ch); 562 return PARSE_INP_TIME; 563 } 564 else 565 { 566 unsigned int rtc; 567 568 rtc = parse_addchar(parseio, ch); 569 if (rtc == PARSE_INP_SKIP) 570 { 571 if (snt_rawdcf(parseio, tstamp) == CVT_OK) 572 return PARSE_INP_SYNTH; 573 } 574 return rtc; 575 } 576 } 577 578 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */ 579 int clk_rawdcf_bs; 580 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_RAWDCF) */ 581 582 /* 583 * History: 584 * 585 * clk_rawdcf.c,v 586 * Revision 4.9 1999/12/06 13:42:23 kardel 587 * transfer correctly converted time codes always into tcode 588 * 589 * Revision 4.8 1999/11/28 09:13:50 kardel 590 * RECON_4_0_98F 591 * 592 * Revision 4.7 1999/04/01 20:07:20 kardel 593 * added checking for minutie increment of timestamps in clk_rawdcf.c 594 * 595 * Revision 4.6 1998/06/14 21:09:37 kardel 596 * Sun acc cleanup 597 * 598 * Revision 4.5 1998/06/13 12:04:16 kardel 599 * fix SYSV clock name clash 600 * 601 * Revision 4.4 1998/06/12 15:22:28 kardel 602 * fix prototypes 603 * 604 * Revision 4.3 1998/06/06 18:33:36 kardel 605 * simplified condidional compile expression 606 * 607 * Revision 4.2 1998/05/24 11:04:18 kardel 608 * triggering PPS on negative edge for simpler wiring (Rx->DCD) 609 * 610 * Revision 4.1 1998/05/24 09:39:53 kardel 611 * implementation of the new IO handling model 612 * 613 * Revision 4.0 1998/04/10 19:45:30 kardel 614 * Start 4.0 release version numbering 615 * 616 * from V3 3.24 log info deleted 1998/04/11 kardel 617 * 618 */ 619