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