1 /* $NetBSD: refclock_wwvb.c,v 1.1.1.1 2009/12/13 16:56:07 kardel Exp $ */ 2 3 /* 4 * refclock_wwvb - clock driver for Spectracom WWVB and GPS receivers 5 */ 6 7 #ifdef HAVE_CONFIG_H 8 #include <config.h> 9 #endif 10 11 #if defined(REFCLOCK) && defined(CLOCK_SPECTRACOM) 12 13 #include "ntpd.h" 14 #include "ntp_io.h" 15 #include "ntp_refclock.h" 16 #include "ntp_calendar.h" 17 #include "ntp_stdlib.h" 18 19 #include <stdio.h> 20 #include <ctype.h> 21 22 #ifdef HAVE_PPSAPI 23 #include "ppsapi_timepps.h" 24 #include "refclock_atom.h" 25 #endif /* HAVE_PPSAPI */ 26 27 /* 28 * This driver supports the Spectracom Model 8170 and Netclock/2 WWVB 29 * Synchronized Clocks and the Netclock/GPS Master Clock. Both the WWVB 30 * and GPS clocks have proven reliable sources of time; however, the 31 * WWVB clocks have proven vulnerable to high ambient conductive RF 32 * interference. The claimed accuracy of the WWVB clocks is 100 us 33 * relative to the broadcast signal, while the claimed accuracy of the 34 * GPS clock is 50 ns; however, in most cases the actual accuracy is 35 * limited by the resolution of the timecode and the latencies of the 36 * serial interface and operating system. 37 * 38 * The WWVB and GPS clocks should be configured for 24-hour display, 39 * AUTO DST off, time zone 0 (UTC), data format 0 or 2 (see below) and 40 * baud rate 9600. If the clock is to used as the source for the IRIG 41 * Audio Decoder (refclock_irig.c in this distribution), it should be 42 * configured for AM IRIG output and IRIG format 1 (IRIG B with 43 * signature control). The GPS clock can be configured either to respond 44 * to a 'T' poll character or left running continuously. 45 * 46 * There are two timecode formats used by these clocks. Format 0, which 47 * is available with both the Netclock/2 and 8170, and format 2, which 48 * is available only with the Netclock/2, specially modified 8170 and 49 * GPS. 50 * 51 * Format 0 (22 ASCII printing characters): 52 * 53 * <cr><lf>i ddd hh:mm:ss TZ=zz<cr><lf> 54 * 55 * on-time = first <cr> 56 * hh:mm:ss = hours, minutes, seconds 57 * i = synchronization flag (' ' = in synch, '?' = out of synch) 58 * 59 * The alarm condition is indicated by other than ' ' at a, which occurs 60 * during initial synchronization and when received signal is lost for 61 * about ten hours. 62 * 63 * Format 2 (24 ASCII printing characters): 64 * 65 * <cr><lf>iqyy ddd hh:mm:ss.fff ld 66 * 67 * on-time = <cr> 68 * i = synchronization flag (' ' = in synch, '?' = out of synch) 69 * q = quality indicator (' ' = locked, 'A'...'D' = unlocked) 70 * yy = year (as broadcast) 71 * ddd = day of year 72 * hh:mm:ss.fff = hours, minutes, seconds, milliseconds 73 * 74 * The alarm condition is indicated by other than ' ' at a, which occurs 75 * during initial synchronization and when received signal is lost for 76 * about ten hours. The unlock condition is indicated by other than ' ' 77 * at q. 78 * 79 * The q is normally ' ' when the time error is less than 1 ms and a 80 * character in the set 'A'...'D' when the time error is less than 10, 81 * 100, 500 and greater than 500 ms respectively. The l is normally ' ', 82 * but is set to 'L' early in the month of an upcoming UTC leap second 83 * and reset to ' ' on the first day of the following month. The d is 84 * set to 'S' for standard time 'I' on the day preceding a switch to 85 * daylight time, 'D' for daylight time and 'O' on the day preceding a 86 * switch to standard time. The start bit of the first <cr> is 87 * synchronized to the indicated time as returned. 88 * 89 * This driver does not need to be told which format is in use - it 90 * figures out which one from the length of the message. The driver 91 * makes no attempt to correct for the intrinsic jitter of the radio 92 * itself, which is a known problem with the older radios. 93 * 94 * PPS Signal Processing 95 * 96 * When PPS signal processing is enabled, and when the system clock has 97 * been set by this or another driver and the PPS signal offset is 98 * within 0.4 s of the system clock offset, the PPS signal replaces the 99 * timecode for as long as the PPS signal is active. If for some reason 100 * the PPS signal fails for one or more poll intervals, the driver 101 * reverts to the timecode. If the timecode fails for one or more poll 102 * intervals, the PPS signal is disconnected. 103 * 104 * Fudge Factors 105 * 106 * This driver can retrieve a table of quality data maintained 107 * internally by the Netclock/2 clock. If flag4 of the fudge 108 * configuration command is set to 1, the driver will retrieve this 109 * table and write it to the clockstats file when the first timecode 110 * message of a new day is received. 111 * 112 * PPS calibration fudge time 1: format 0 .003134, format 2 .004034 113 */ 114 /* 115 * Interface definitions 116 */ 117 #define DEVICE "/dev/wwvb%d" /* device name and unit */ 118 #define SPEED232 B9600 /* uart speed (9600 baud) */ 119 #define PRECISION (-13) /* precision assumed (about 100 us) */ 120 #define PPS_PRECISION (-13) /* precision assumed (about 100 us) */ 121 #define REFID "WWVB" /* reference ID */ 122 #define DESCRIPTION "Spectracom WWVB/GPS Receiver" /* WRU */ 123 124 #define LENWWVB0 22 /* format 0 timecode length */ 125 #define LENWWVB1 22 /* format 1 timecode length */ 126 #define LENWWVB2 24 /* format 2 timecode length */ 127 #define LENWWVB3 29 /* format 3 timecode length */ 128 #define MONLIN 15 /* number of monitoring lines */ 129 130 /* 131 * WWVB unit control structure 132 */ 133 struct wwvbunit { 134 #ifdef HAVE_PPSAPI 135 struct refclock_atom atom; /* PPSAPI structure */ 136 int ppsapi_tried; /* attempt PPSAPI once */ 137 int ppsapi_lit; /* time_pps_create() worked */ 138 int tcount; /* timecode sample counter */ 139 int pcount; /* PPS sample counter */ 140 #endif /* HAVE_PPSAPI */ 141 l_fp laststamp; /* last receive timestamp */ 142 u_char lasthour; /* last hour (for monitor) */ 143 u_char linect; /* count ignored lines (for monitor */ 144 }; 145 146 /* 147 * Function prototypes 148 */ 149 static int wwvb_start (int, struct peer *); 150 static void wwvb_shutdown (int, struct peer *); 151 static void wwvb_receive (struct recvbuf *); 152 static void wwvb_poll (int, struct peer *); 153 static void wwvb_timer (int, struct peer *); 154 #ifdef HAVE_PPSAPI 155 static void wwvb_control (int, struct refclockstat *, 156 struct refclockstat *, struct peer *); 157 #define WWVB_CONTROL wwvb_control 158 #else 159 #define WWVB_CONTROL noentry 160 #endif /* HAVE_PPSAPI */ 161 162 /* 163 * Transfer vector 164 */ 165 struct refclock refclock_wwvb = { 166 wwvb_start, /* start up driver */ 167 wwvb_shutdown, /* shut down driver */ 168 wwvb_poll, /* transmit poll message */ 169 WWVB_CONTROL, /* fudge set/change notification */ 170 noentry, /* initialize driver (not used) */ 171 noentry, /* not used (old wwvb_buginfo) */ 172 wwvb_timer /* called once per second */ 173 }; 174 175 176 /* 177 * wwvb_start - open the devices and initialize data for processing 178 */ 179 static int 180 wwvb_start( 181 int unit, 182 struct peer *peer 183 ) 184 { 185 register struct wwvbunit *up; 186 struct refclockproc *pp; 187 int fd; 188 char device[20]; 189 190 /* 191 * Open serial port. Use CLK line discipline, if available. 192 */ 193 sprintf(device, DEVICE, unit); 194 if (-1 == (fd = refclock_open(device, SPEED232, LDISC_CLK))) 195 return (0); 196 197 /* 198 * Allocate and initialize unit structure 199 */ 200 up = (struct wwvbunit *)emalloc(sizeof(struct wwvbunit)); 201 memset((char *)up, 0, sizeof(struct wwvbunit)); 202 pp = peer->procptr; 203 pp->unitptr = (caddr_t)up; 204 pp->io.clock_recv = wwvb_receive; 205 pp->io.srcclock = (caddr_t)peer; 206 pp->io.datalen = 0; 207 pp->io.fd = fd; 208 if (!io_addclock(&pp->io)) { 209 close(fd); 210 free(up); 211 return (0); 212 } 213 214 /* 215 * Initialize miscellaneous variables 216 */ 217 peer->precision = PRECISION; 218 pp->clockdesc = DESCRIPTION; 219 memcpy((char *)&pp->refid, REFID, 4); 220 return (1); 221 } 222 223 224 /* 225 * wwvb_shutdown - shut down the clock 226 */ 227 static void 228 wwvb_shutdown( 229 int unit, 230 struct peer *peer 231 ) 232 { 233 register struct wwvbunit *up; 234 struct refclockproc *pp; 235 236 pp = peer->procptr; 237 up = (struct wwvbunit *)pp->unitptr; 238 io_closeclock(&pp->io); 239 free(up); 240 } 241 242 243 /* 244 * wwvb_receive - receive data from the serial interface 245 */ 246 static void 247 wwvb_receive( 248 struct recvbuf *rbufp 249 ) 250 { 251 struct wwvbunit *up; 252 struct refclockproc *pp; 253 struct peer *peer; 254 255 l_fp trtmp; /* arrival timestamp */ 256 int tz; /* time zone */ 257 int day, month; /* ddd conversion */ 258 int temp; /* int temp */ 259 char syncchar; /* synchronization indicator */ 260 char qualchar; /* quality indicator */ 261 char leapchar; /* leap indicator */ 262 char dstchar; /* daylight/standard indicator */ 263 char tmpchar; /* trashbin */ 264 265 /* 266 * Initialize pointers and read the timecode and timestamp 267 */ 268 peer = (struct peer *)rbufp->recv_srcclock; 269 pp = peer->procptr; 270 up = (struct wwvbunit *)pp->unitptr; 271 temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp); 272 273 /* 274 * Note we get a buffer and timestamp for both a <cr> and <lf>, 275 * but only the <cr> timestamp is retained. Note: in format 0 on 276 * a Netclock/2 or upgraded 8170 the start bit is delayed 100 277 * +-50 us relative to the pps; however, on an unmodified 8170 278 * the start bit can be delayed up to 10 ms. In format 2 the 279 * reading precision is only to the millisecond. Thus, unless 280 * you have a PPS gadget and don't have to have the year, format 281 * 0 provides the lowest jitter. 282 */ 283 if (temp == 0) { 284 up->laststamp = trtmp; 285 return; 286 } 287 pp->lencode = temp; 288 pp->lastrec = up->laststamp; 289 290 /* 291 * We get down to business, check the timecode format and decode 292 * its contents. This code uses the timecode length to determine 293 * format 0, 2 or 3. If the timecode has invalid length or is 294 * not in proper format, we declare bad format and exit. 295 */ 296 syncchar = qualchar = leapchar = dstchar = ' '; 297 tz = 0; 298 switch (pp->lencode) { 299 300 case LENWWVB0: 301 302 /* 303 * Timecode format 0: "I ddd hh:mm:ss DTZ=nn" 304 */ 305 if (sscanf(pp->a_lastcode, 306 "%c %3d %2d:%2d:%2d%c%cTZ=%2d", 307 &syncchar, &pp->day, &pp->hour, &pp->minute, 308 &pp->second, &tmpchar, &dstchar, &tz) == 8) 309 pp->nsec = 0; 310 break; 311 312 case LENWWVB2: 313 314 /* 315 * Timecode format 2: "IQyy ddd hh:mm:ss.mmm LD" */ 316 if (sscanf(pp->a_lastcode, 317 "%c%c %2d %3d %2d:%2d:%2d.%3ld %c", 318 &syncchar, &qualchar, &pp->year, &pp->day, 319 &pp->hour, &pp->minute, &pp->second, &pp->nsec, 320 &leapchar) == 9) 321 pp->nsec *= 1000000; 322 break; 323 324 case LENWWVB3: 325 326 /* 327 * Timecode format 3: "0003I yyyymmdd hhmmss+0000SL#" 328 */ 329 if (sscanf(pp->a_lastcode, 330 "0003%c %4d%2d%2d %2d%2d%2d+0000%c%c", 331 &syncchar, &pp->year, &month, &day, &pp->hour, 332 &pp->minute, &pp->second, &dstchar, &leapchar) == 8) 333 { 334 pp->day = ymd2yd(pp->year, month, day); 335 pp->nsec = 0; 336 break; 337 } 338 339 default: 340 341 /* 342 * Unknown format: If dumping internal table, record 343 * stats; otherwise, declare bad format. 344 */ 345 if (up->linect > 0) { 346 up->linect--; 347 record_clock_stats(&peer->srcadr, 348 pp->a_lastcode); 349 } else { 350 refclock_report(peer, CEVNT_BADREPLY); 351 } 352 return; 353 } 354 355 /* 356 * Decode synchronization, quality and leap characters. If 357 * unsynchronized, set the leap bits accordingly and exit. 358 * Otherwise, set the leap bits according to the leap character. 359 * Once synchronized, the dispersion depends only on the 360 * quality character. 361 */ 362 switch (qualchar) { 363 364 case ' ': 365 pp->disp = .001; 366 pp->lastref = pp->lastrec; 367 break; 368 369 case 'A': 370 pp->disp = .01; 371 break; 372 373 case 'B': 374 pp->disp = .1; 375 break; 376 377 case 'C': 378 pp->disp = .5; 379 break; 380 381 case 'D': 382 pp->disp = MAXDISPERSE; 383 break; 384 385 default: 386 pp->disp = MAXDISPERSE; 387 refclock_report(peer, CEVNT_BADREPLY); 388 break; 389 } 390 if (syncchar != ' ') 391 pp->leap = LEAP_NOTINSYNC; 392 else if (leapchar == 'L') 393 pp->leap = LEAP_ADDSECOND; 394 else 395 pp->leap = LEAP_NOWARNING; 396 397 /* 398 * Process the new sample in the median filter and determine the 399 * timecode timestamp, but only if the PPS is not in control. 400 */ 401 #ifdef HAVE_PPSAPI 402 up->tcount++; 403 if (peer->flags & FLAG_PPS) 404 return; 405 406 #endif /* HAVE_PPSAPI */ 407 if (!refclock_process_f(pp, pp->fudgetime2)) 408 refclock_report(peer, CEVNT_BADTIME); 409 } 410 411 412 /* 413 * wwvb_timer - called once per second by the transmit procedure 414 */ 415 static void 416 wwvb_timer( 417 int unit, 418 struct peer *peer 419 ) 420 { 421 register struct wwvbunit *up; 422 struct refclockproc *pp; 423 char pollchar; /* character sent to clock */ 424 425 /* 426 * Time to poll the clock. The Spectracom clock responds to a 427 * 'T' by returning a timecode in the format(s) specified above. 428 * Note there is no checking on state, since this may not be the 429 * only customer reading the clock. Only one customer need poll 430 * the clock; all others just listen in. 431 */ 432 pp = peer->procptr; 433 up = (struct wwvbunit *)pp->unitptr; 434 if (up->linect > 0) 435 pollchar = 'R'; 436 else 437 pollchar = 'T'; 438 if (write(pp->io.fd, &pollchar, 1) != 1) 439 refclock_report(peer, CEVNT_FAULT); 440 #ifdef HAVE_PPSAPI 441 if (up->ppsapi_lit && 442 refclock_pps(peer, &up->atom, pp->sloppyclockflag) > 0) { 443 up->pcount++, 444 peer->flags |= FLAG_PPS; 445 peer->precision = PPS_PRECISION; 446 } 447 #endif /* HAVE_PPSAPI */ 448 } 449 450 451 /* 452 * wwvb_poll - called by the transmit procedure 453 */ 454 static void 455 wwvb_poll( 456 int unit, 457 struct peer *peer 458 ) 459 { 460 register struct wwvbunit *up; 461 struct refclockproc *pp; 462 463 /* 464 * Sweep up the samples received since the last poll. If none 465 * are received, declare a timeout and keep going. 466 */ 467 pp = peer->procptr; 468 up = (struct wwvbunit *)pp->unitptr; 469 pp->polls++; 470 471 /* 472 * If the monitor flag is set (flag4), we dump the internal 473 * quality table at the first timecode beginning the day. 474 */ 475 if (pp->sloppyclockflag & CLK_FLAG4 && pp->hour < 476 (int)up->lasthour) 477 up->linect = MONLIN; 478 up->lasthour = (u_char)pp->hour; 479 480 /* 481 * Process median filter samples. If none received, declare a 482 * timeout and keep going. 483 */ 484 #ifdef HAVE_PPSAPI 485 if (up->pcount == 0) { 486 peer->flags &= ~FLAG_PPS; 487 peer->precision = PRECISION; 488 } 489 if (up->tcount == 0) { 490 pp->coderecv = pp->codeproc; 491 refclock_report(peer, CEVNT_TIMEOUT); 492 return; 493 } 494 up->pcount = up->tcount = 0; 495 #else /* HAVE_PPSAPI */ 496 if (pp->coderecv == pp->codeproc) { 497 refclock_report(peer, CEVNT_TIMEOUT); 498 return; 499 } 500 #endif /* HAVE_PPSAPI */ 501 refclock_receive(peer); 502 record_clock_stats(&peer->srcadr, pp->a_lastcode); 503 #ifdef DEBUG 504 if (debug) 505 printf("wwvb: timecode %d %s\n", pp->lencode, 506 pp->a_lastcode); 507 #endif 508 } 509 510 511 /* 512 * wwvb_control - fudge parameters have been set or changed 513 */ 514 #ifdef HAVE_PPSAPI 515 static void 516 wwvb_control( 517 int unit, 518 struct refclockstat *in_st, 519 struct refclockstat *out_st, 520 struct peer *peer 521 ) 522 { 523 register struct wwvbunit *up; 524 struct refclockproc *pp; 525 526 pp = peer->procptr; 527 up = (struct wwvbunit *)pp->unitptr; 528 529 if (!(pp->sloppyclockflag & CLK_FLAG1)) { 530 if (!up->ppsapi_tried) 531 return; 532 up->ppsapi_tried = 0; 533 if (!up->ppsapi_lit) 534 return; 535 peer->flags &= ~FLAG_PPS; 536 peer->precision = PRECISION; 537 time_pps_destroy(up->atom.handle); 538 up->atom.handle = 0; 539 up->ppsapi_lit = 0; 540 return; 541 } 542 543 if (up->ppsapi_tried) 544 return; 545 /* 546 * Light up the PPSAPI interface. 547 */ 548 up->ppsapi_tried = 1; 549 if (refclock_ppsapi(pp->io.fd, &up->atom)) { 550 up->ppsapi_lit = 1; 551 return; 552 } 553 554 NLOG(NLOG_CLOCKINFO) 555 msyslog(LOG_WARNING, "%s flag1 1 but PPSAPI fails", 556 refnumtoa(&peer->srcadr)); 557 } 558 #endif /* HAVE_PPSAPI */ 559 560 #else 561 int refclock_wwvb_bs; 562 #endif /* REFCLOCK */ 563