1 /* 2 * refclock_arc - clock driver for ARCRON MSF receivers 3 */ 4 5 #ifdef HAVE_CONFIG_H 6 #include <config.h> 7 #endif 8 9 #if defined(REFCLOCK) && defined(CLOCK_ARCRON_MSF) 10 static const char arc_version[] = { "V1.1 1997/06/23" }; 11 12 #undef ARCRON_DEBUG /* Define only while in development... */ 13 14 #ifndef ARCRON_NOT_KEEN 15 #define ARCRON_KEEN 1 /* Be keen, and trusting of the clock, if defined. */ 16 #endif 17 18 #ifndef ARCRON_NOT_MULTIPLE_SAMPLES 19 #define ARCRON_MULTIPLE_SAMPLES 1 /* Use all timestamp bytes as samples. */ 20 #endif 21 22 #ifndef ARCRON_NOT_LEAPSECOND_KEEN 23 #ifndef ARCRON_LEAPSECOND_KEEN 24 #undef ARCRON_LEAPSECOND_KEEN /* Respond quickly to leap seconds: doesn't work yet. */ 25 #endif 26 #endif 27 28 /* 29 Code by Derek Mulcahy, <derek@toybox.demon.co.uk>, 1997. 30 Modifications by Damon Hart-Davis, <d@hd.org>, 1997. 31 32 THIS CODE IS SUPPLIED AS IS, WITH NO WARRANTY OF ANY KIND. USE AT 33 YOUR OWN RISK. 34 35 Orginally developed and used with ntp3-5.85 by Derek Mulcahy. 36 37 Built against ntp3-5.90 on Solaris 2.5 using gcc 2.7.2. 38 39 This code may be freely copied and used and incorporated in other 40 systems providing the disclaimer and notice of authorship are 41 reproduced. 42 43 ------------------------------------------------------------------------------- 44 45 Author's original note: 46 47 I enclose my ntp driver for the Galleon Systems Arc MSF receiver. 48 49 It works (after a fashion) on both Solaris-1 and Solaris-2. 50 51 I am currently using ntp3-5.85. I have been running the code for 52 about 7 months without any problems. Even coped with the change to BST! 53 54 I had to do some funky things to read from the clock because it uses the 55 power from the receive lines to drive the transmit lines. This makes the 56 code look a bit stupid but it works. I also had to put in some delays to 57 allow for the turnaround time from receive to transmit. These delays 58 are between characters when requesting a time stamp so that shouldn't affect 59 the results too drastically. 60 61 ... 62 63 The bottom line is that it works but could easily be improved. You are 64 free to do what you will with the code. I haven't been able to determine 65 how good the clock is. I think that this requires a known good clock 66 to compare it against. 67 68 ------------------------------------------------------------------------------- 69 70 Damon's notes for adjustments: 71 72 MAJOR CHANGES SINCE V1.0 73 ======================== 74 1) Removal of pollcnt variable that made the clock go permanently 75 off-line once two time polls failed to gain responses. 76 77 2) Avoiding (at least on Solaris-2) terminal becoming the controlling 78 terminal of the process when we do a low-level open(). 79 80 3) Additional logic (conditional on ARCRON_LEAPSECOND_KEEN being 81 defined) to try to resync quickly after a potential leap-second 82 insertion or deletion. 83 84 4) Code significantly slimmer at run-time than V1.0. 85 86 87 GENERAL 88 ======= 89 90 1) The C preprocessor symbol to have the clock built has been changed 91 from ARC to ARCRON_MSF to CLOCK_ARCRON_MSF to minimise the 92 possiblity of clashes with other symbols in the future. 93 94 2) PRECISION should be -4/-5 (63ms/31ms) for the following reasons: 95 96 a) The ARC documentation claims the internal clock is (only) 97 accurate to about 20ms relative to Rugby (plus there must be 98 noticable drift and delay in the ms range due to transmission 99 delays and changing atmospheric effects). This clock is not 100 designed for ms accuracy as NTP has spoilt us all to expect. 101 102 b) The clock oscillator looks like a simple uncompensated quartz 103 crystal of the sort used in digital watches (ie 32768Hz) which 104 can have large temperature coefficients and drifts; it is not 105 clear if this oscillator is properly disciplined to the MSF 106 transmission, but as the default is to resync only once per 107 *day*, we can imagine that it is not, and is free-running. We 108 can minimise drift by resyncing more often (at the cost of 109 reduced battery life), but drift/wander may still be 110 significant. 111 112 c) Note that the bit time of 3.3ms adds to the potential error in 113 the the clock timestamp, since the bit clock of the serial link 114 may effectively be free-running with respect to the host clock 115 and the MSF clock. Actually, the error is probably 1/16th of 116 the above, since the input data is probably sampled at at least 117 16x the bit rate. 118 119 By keeping the clock marked as not very precise, it will have a 120 fairly large dispersion, and thus will tend to be used as a 121 `backup' time source and sanity checker, which this clock is 122 probably ideal for. For an isolated network without other time 123 sources, this clock can probably be expected to provide *much* 124 better than 1s accuracy, which will be fine. 125 126 By default, PRECISION is set to -4, but experience, especially at a 127 particular geographic location with a particular clock, may allow 128 this to be altered to -5. (Note that skews of +/- 10ms are to be 129 expected from the clock from time-to-time.) This improvement of 130 reported precision can be instigated by setting flag3 to 1, though 131 the PRECISION will revert to the normal value while the clock 132 signal quality is unknown whatever the flag3 setting. 133 134 IN ANY CASE, BE SURE TO SET AN APPROPRIATE FUDGE FACTOR TO REMOVE 135 ANY RESIDUAL SKEW, eg: 136 137 server 127.127.27.0 # ARCRON MSF radio clock unit 0. 138 # Fudge timestamps by about 20ms. 139 fudge 127.127.27.0 time1 0.020 140 141 You will need to observe your system's behaviour, assuming you have 142 some other NTP source to compare it with, to work out what the 143 fudge factor should be. For my Sun SS1 running SunOS 4.1.3_U1 with 144 my MSF clock with my distance from the MSF transmitter, +20ms 145 seemed about right, after some observation. 146 147 3) REFID has been made "MSFa" to reflect the MSF time source and the 148 ARCRON receiver. 149 150 4) DEFAULT_RESYNC_TIME is the time in seconds (by default) before 151 forcing a resync since the last attempt. This is picked to give a 152 little less than an hour between resyncs and to try to avoid 153 clashing with any regular event at a regular time-past-the-hour 154 which might cause systematic errors. 155 156 The INITIAL_RESYNC_DELAY is to avoid bothering the clock and 157 running down its batteries unnecesarily if ntpd is going to crash 158 or be killed or reconfigured quickly. If ARCRON_KEEN is defined 159 then this period is long enough for (with normal polling rates) 160 enough time samples to have been taken to allow ntpd to sync to 161 the clock before the interruption for the clock to resync to MSF. 162 This avoids ntpd syncing to another peer first and then 163 almost immediately hopping to the MSF clock. 164 165 The RETRY_RESYNC_TIME is used before rescheduling a resync after a 166 resync failed to reveal a statisfatory signal quality (too low or 167 unknown). 168 169 5) The clock seems quite jittery, so I have increased the 170 median-filter size from the typical (previous) value of 3. I 171 discard up to half the results in the filter. It looks like maybe 172 1 sample in 10 or so (maybe less) is a spike, so allow the median 173 filter to discard at least 10% of its entries or 1 entry, whichever 174 is greater. 175 176 6) Sleeping *before* each character sent to the unit to allow required 177 inter-character time but without introducting jitter and delay in 178 handling the response if possible. 179 180 7) If the flag ARCRON_KEEN is defined, take time samples whenever 181 possible, even while resyncing, etc. We rely, in this case, on the 182 clock always giving us a reasonable time or else telling us in the 183 status byte at the end of the timestamp that it failed to sync to 184 MSF---thus we should never end up syncing to completely the wrong 185 time. 186 187 8) If the flag ARCRON_OWN_FILTER is defined, use own versions of 188 refclock median-filter routines to get round small bug in 3-5.90 189 code which does not return the median offset. XXX Removed this 190 bit due NTP Version 4 upgrade - dlm. 191 192 9) We would appear to have a year-2000 problem with this clock since 193 it returns only the two least-significant digits of the year. But 194 ntpd ignores the year and uses the local-system year instead, so 195 this is in fact not a problem. Nevertheless, we attempt to do a 196 sensible thing with the dates, wrapping them into a 100-year 197 window. 198 199 10)Logs stats information that can be used by Derek's Tcl/Tk utility 200 to show the status of the clock. 201 202 11)The clock documentation insists that the number of bits per 203 character to be sent to the clock, and sent by it, is 11, including 204 one start bit and two stop bits. The data format is either 7+even 205 or 8+none. 206 207 208 TO-DO LIST 209 ========== 210 211 * Eliminate use of scanf(), and maybe sprintf(). 212 213 * Allow user setting of resync interval to trade battery life for 214 accuracy; maybe could be done via fudge factor or unit number. 215 216 * Possibly note the time since the last resync of the MSF clock to 217 MSF as the age of the last reference timestamp, ie trust the 218 clock's oscillator not very much... 219 220 * Add very slow auto-adjustment up to a value of +/- time2 to correct 221 for long-term errors in the clock value (time2 defaults to 0 so the 222 correction would be disabled by default). 223 224 * Consider trying to use the tty_clk/ppsclock support. 225 226 * Possibly use average or maximum signal quality reported during 227 resync, rather than just the last one, which may be atypical. 228 229 */ 230 231 232 /* Notes for HKW Elektronik GmBH Radio clock driver */ 233 /* Author Lyndon David, Sentinet Ltd, Feb 1997 */ 234 /* These notes seem also to apply usefully to the ARCRON clock. */ 235 236 /* The HKW clock module is a radio receiver tuned into the Rugby */ 237 /* MSF time signal tranmitted on 60 kHz. The clock module connects */ 238 /* to the computer via a serial line and transmits the time encoded */ 239 /* in 15 bytes at 300 baud 7 bits two stop bits even parity */ 240 241 /* Clock communications, from the datasheet */ 242 /* All characters sent to the clock are echoed back to the controlling */ 243 /* device. */ 244 /* Transmit time/date information */ 245 /* syntax ASCII o<cr> */ 246 /* Character o may be replaced if neccesary by a character whose code */ 247 /* contains the lowest four bits f(hex) eg */ 248 /* syntax binary: xxxx1111 00001101 */ 249 250 /* DHD note: 251 You have to wait for character echo + 10ms before sending next character. 252 */ 253 254 /* The clock replies to this command with a sequence of 15 characters */ 255 /* which contain the complete time and a final <cr> making 16 characters */ 256 /* in total. */ 257 /* The RC computer clock will not reply immediately to this command because */ 258 /* the start bit edge of the first reply character marks the beginning of */ 259 /* the second. So the RC Computer Clock will reply to this command at the */ 260 /* start of the next second */ 261 /* The characters have the following meaning */ 262 /* 1. hours tens */ 263 /* 2. hours units */ 264 /* 3. minutes tens */ 265 /* 4. minutes units */ 266 /* 5. seconds tens */ 267 /* 6. seconds units */ 268 /* 7. day of week 1-monday 7-sunday */ 269 /* 8. day of month tens */ 270 /* 9. day of month units */ 271 /* 10. month tens */ 272 /* 11. month units */ 273 /* 12. year tens */ 274 /* 13. year units */ 275 /* 14. BST/UTC status */ 276 /* bit 7 parity */ 277 /* bit 6 always 0 */ 278 /* bit 5 always 1 */ 279 /* bit 4 always 1 */ 280 /* bit 3 always 0 */ 281 /* bit 2 =1 if UTC is in effect, complementary to the BST bit */ 282 /* bit 1 =1 if BST is in effect, according to the BST bit */ 283 /* bit 0 BST/UTC change impending bit=1 in case of change impending */ 284 /* 15. status */ 285 /* bit 7 parity */ 286 /* bit 6 always 0 */ 287 /* bit 5 always 1 */ 288 /* bit 4 always 1 */ 289 /* bit 3 =1 if low battery is detected */ 290 /* bit 2 =1 if the very last reception attempt failed and a valid */ 291 /* time information already exists (bit0=1) */ 292 /* =0 if the last reception attempt was successful */ 293 /* bit 1 =1 if at least one reception since 2:30 am was successful */ 294 /* =0 if no reception attempt since 2:30 am was successful */ 295 /* bit 0 =1 if the RC Computer Clock contains valid time information */ 296 /* This bit is zero after reset and one after the first */ 297 /* successful reception attempt */ 298 299 /* DHD note: 300 Also note g<cr> command which confirms that a resync is in progress, and 301 if so what signal quality (0--5) is available. 302 Also note h<cr> command which starts a resync to MSF signal. 303 */ 304 305 306 307 #include <stdio.h> 308 #include <ctype.h> 309 #include <sys/time.h> 310 311 #if defined(HAVE_BSD_TTYS) 312 #include <sgtty.h> 313 #endif /* HAVE_BSD_TTYS */ 314 315 #if defined(HAVE_SYSV_TTYS) 316 #include <termio.h> 317 #endif /* HAVE_SYSV_TTYS */ 318 319 #if defined(HAVE_TERMIOS) 320 #include <termios.h> 321 #endif 322 323 #include "ntpd.h" 324 #include "ntp_io.h" 325 #include "ntp_refclock.h" 326 #include "ntp_stdlib.h" 327 328 /* 329 * This driver supports the ARCRON MSF Radio Controlled Clock 330 */ 331 332 /* 333 * Interface definitions 334 */ 335 #define DEVICE "/dev/arc%d" /* Device name and unit. */ 336 #define SPEED B300 /* UART speed (300 baud) */ 337 #define PRECISION (-4) /* Precision (~63 ms). */ 338 #define HIGHPRECISION (-5) /* If things are going well... */ 339 #define REFID "MSFa" /* Reference ID. */ 340 #define DESCRIPTION "ARCRON MSF Receiver" 341 342 #define NSAMPLESLONG 8 /* Stages of long filter. */ 343 344 #define LENARC 16 /* Format `o' timecode length. */ 345 346 #define BITSPERCHAR 11 /* Bits per character. */ 347 #define BITTIME 0x0DA740E /* Time for 1 bit at 300bps. */ 348 #define CHARTIME10 0x8888888 /* Time for 10-bit char at 300bps. */ 349 #define CHARTIME11 0x962FC96 /* Time for 11-bit char at 300bps. */ 350 #define CHARTIME /* Time for char at 300bps. */ \ 351 ( (BITSPERCHAR == 11) ? CHARTIME11 : ( (BITSPERCHAR == 10) ? CHARTIME10 : \ 352 (BITSPERCHAR * BITTIME) ) ) 353 354 /* Allow for UART to accept char half-way through final stop bit. */ 355 #define INITIALOFFSET (u_int32)(-BITTIME/2) 356 357 /* 358 charoffsets[x] is the time after the start of the second that byte 359 x (with the first byte being byte 1) is received by the UART, 360 assuming that the initial edge of the start bit of the first byte 361 is on-time. The values are represented as the fractional part of 362 an l_fp. 363 364 We store enough values to have the offset of each byte including 365 the trailing \r, on the assumption that the bytes follow one 366 another without gaps. 367 */ 368 static const u_int32 charoffsets[LENARC+1] = { 369 #if BITSPERCHAR == 11 /* Usual case. */ 370 /* Offsets computed as accurately as possible... */ 371 0, 372 INITIALOFFSET + 0x0962fc96, /* 1 chars, 11 bits */ 373 INITIALOFFSET + 0x12c5f92c, /* 2 chars, 22 bits */ 374 INITIALOFFSET + 0x1c28f5c3, /* 3 chars, 33 bits */ 375 INITIALOFFSET + 0x258bf259, /* 4 chars, 44 bits */ 376 INITIALOFFSET + 0x2eeeeeef, /* 5 chars, 55 bits */ 377 INITIALOFFSET + 0x3851eb85, /* 6 chars, 66 bits */ 378 INITIALOFFSET + 0x41b4e81b, /* 7 chars, 77 bits */ 379 INITIALOFFSET + 0x4b17e4b1, /* 8 chars, 88 bits */ 380 INITIALOFFSET + 0x547ae148, /* 9 chars, 99 bits */ 381 INITIALOFFSET + 0x5dddddde, /* 10 chars, 110 bits */ 382 INITIALOFFSET + 0x6740da74, /* 11 chars, 121 bits */ 383 INITIALOFFSET + 0x70a3d70a, /* 12 chars, 132 bits */ 384 INITIALOFFSET + 0x7a06d3a0, /* 13 chars, 143 bits */ 385 INITIALOFFSET + 0x8369d037, /* 14 chars, 154 bits */ 386 INITIALOFFSET + 0x8ccccccd, /* 15 chars, 165 bits */ 387 INITIALOFFSET + 0x962fc963 /* 16 chars, 176 bits */ 388 #else 389 /* Offsets computed with a small rounding error... */ 390 0, 391 INITIALOFFSET + 1 * CHARTIME, 392 INITIALOFFSET + 2 * CHARTIME, 393 INITIALOFFSET + 3 * CHARTIME, 394 INITIALOFFSET + 4 * CHARTIME, 395 INITIALOFFSET + 5 * CHARTIME, 396 INITIALOFFSET + 6 * CHARTIME, 397 INITIALOFFSET + 7 * CHARTIME, 398 INITIALOFFSET + 8 * CHARTIME, 399 INITIALOFFSET + 9 * CHARTIME, 400 INITIALOFFSET + 10 * CHARTIME, 401 INITIALOFFSET + 11 * CHARTIME, 402 INITIALOFFSET + 12 * CHARTIME, 403 INITIALOFFSET + 13 * CHARTIME, 404 INITIALOFFSET + 14 * CHARTIME, 405 INITIALOFFSET + 15 * CHARTIME, 406 INITIALOFFSET + 16 * CHARTIME 407 #endif 408 }; 409 410 /* Chose filter length dependent on fudge flag 4. */ 411 #define CHOSENSAMPLES(pp) \ 412 (((pp)->sloppyclockflag & CLK_FLAG4) ? NSAMPLESLONG : NSAMPLES) 413 /* 414 Chose how many filter samples to keep. Several factors are in play. 415 416 1) Discard at least one sample to allow a spike value to be 417 discarded. 418 419 2) Discard about 1-in-8 to 1-in-30 samples to handle spikes. 420 421 3) Keep an odd number of samples to avoid median value being biased 422 high or low. 423 */ 424 #define NKEEP(pp) ((CHOSENSAMPLES(pp) - 1 - (CHOSENSAMPLES(pp)>>3)) | 1) 425 426 #define DEFAULT_RESYNC_TIME (57*60) /* Gap between resync attempts (s). */ 427 #define RETRY_RESYNC_TIME (27*60) /* Gap to emergency resync attempt. */ 428 #ifdef ARCRON_KEEN 429 #define INITIAL_RESYNC_DELAY 500 /* Delay before first resync. */ 430 #else 431 #define INITIAL_RESYNC_DELAY 50 /* Delay before first resync. */ 432 #endif 433 434 static const int moff[12] = 435 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; 436 /* Flags for a raw open() of the clock serial device. */ 437 #ifdef O_NOCTTY /* Good, we can avoid tty becoming controlling tty. */ 438 #define OPEN_FLAGS (O_RDWR | O_NOCTTY) 439 #else /* Oh well, it may not matter... */ 440 #define OPEN_FLAGS (O_RDWR) 441 #endif 442 443 444 /* Length of queue of command bytes to be sent. */ 445 #define CMDQUEUELEN 4 /* Enough for two cmds + each \r. */ 446 /* Queue tick time; interval in seconds between chars taken off queue. */ 447 /* Must be >= 2 to allow o\r response to come back uninterrupted. */ 448 #define QUEUETICK 2 /* Allow o\r reply to finish. */ 449 450 /* 451 * ARC unit control structure 452 */ 453 struct arcunit { 454 l_fp lastrec; /* Time tag for the receive time (system). */ 455 int status; /* Clock status. */ 456 457 int quality; /* Quality of reception 0--5 for unit. */ 458 /* We may also use the values -1 or 6 internally. */ 459 460 u_long next_resync; /* Next resync time (s) compared to current_time. */ 461 int resyncing; /* Resync in progress if true. */ 462 463 /* In the outgoing queue, cmdqueue[0] is next to be sent. */ 464 char cmdqueue[CMDQUEUELEN+1]; /* Queue of outgoing commands + \0. */ 465 466 u_long saved_flags; /* Saved fudge flags. */ 467 }; 468 #ifdef ARCRON_LEAPSECOND_KEEN 469 /* The flag `possible_leap' is set non-zero when any MSF unit 470 thinks a leap-second may have happened. 471 472 Set whenever we receive a valid time sample in the first hour of 473 the first day of the first/seventh months. 474 475 Outside the special hour this value is unconditionally set 476 to zero by the receive routine. 477 478 On finding itself in this timeslot, as long as the value is 479 non-negative, the receive routine sets it to a positive value to 480 indicate a resync to MSF should be performed. 481 482 In the poll routine, if this value is positive and we are not 483 already resyncing (eg from a sync that started just before 484 midnight), start resyncing and set this value negative to 485 indicate that a leap-triggered resync has been started. Having 486 set this negative prevents the receive routine setting it 487 positive and thus prevents multiple resyncs during the witching 488 hour. 489 */ 490 static int possible_leap = 0; /* No resync required by default. */ 491 #endif 492 493 #if 0 494 static void dummy_event_handler P((struct peer *)); 495 static void arc_event_handler P((struct peer *)); 496 #endif /* 0 */ 497 498 #define QUALITY_UNKNOWN -1 /* Indicates unknown clock quality. */ 499 #define MIN_CLOCK_QUALITY 0 /* Min quality clock will return. */ 500 #define MIN_CLOCK_QUALITY_OK 3 /* Min quality for OK reception. */ 501 #define MAX_CLOCK_QUALITY 5 /* Max quality clock will return. */ 502 503 /* 504 * Function prototypes 505 */ 506 static int arc_start P((int, struct peer *)); 507 static void arc_shutdown P((int, struct peer *)); 508 static void arc_receive P((struct recvbuf *)); 509 static void arc_poll P((int, struct peer *)); 510 511 /* 512 * Transfer vector 513 */ 514 struct refclock refclock_arc = { 515 arc_start, /* start up driver */ 516 arc_shutdown, /* shut down driver */ 517 arc_poll, /* transmit poll message */ 518 noentry, /* not used (old arc_control) */ 519 noentry, /* initialize driver (not used) */ 520 noentry, /* not used (old arc_buginfo) */ 521 NOFLAGS /* not used */ 522 }; 523 524 /* Queue us up for the next tick. */ 525 #define ENQUEUE(up) \ 526 do { \ 527 if((up)->ev.next != 0) { break; } /* WHOOPS! */ \ 528 peer->nextdate = current_time + QUEUETICK; \ 529 } while(0) 530 531 #if 0 532 /* Placeholder event handler---does nothing safely---soaks up lose tick. */ 533 static void 534 dummy_event_handler( 535 struct peer *peer 536 ) 537 { 538 #ifdef ARCRON_DEBUG 539 if(debug) { printf("arc: dummy_event_handler() called.\n"); } 540 #endif 541 } 542 543 /* 544 Normal event handler. 545 546 Take first character off queue and send to clock if not a null. 547 548 Shift characters down and put a null on the end. 549 550 We assume that there is no parallelism so no race condition, but even 551 if there is nothing bad will happen except that we might send some bad 552 data to the clock once in a while. 553 */ 554 static void 555 arc_event_handler( 556 struct peer *peer 557 ) 558 { 559 struct refclockproc *pp = peer->procptr; 560 register struct arcunit *up = (struct arcunit *)pp->unitptr; 561 int i; 562 char c; 563 #ifdef ARCRON_DEBUG 564 if(debug > 2) { printf("arc: arc_event_handler() called.\n"); } 565 #endif 566 567 c = up->cmdqueue[0]; /* Next char to be sent. */ 568 /* Shift down characters, shifting trailing \0 in at end. */ 569 for(i = 0; i < CMDQUEUELEN; ++i) 570 { up->cmdqueue[i] = up->cmdqueue[i+1]; } 571 572 /* Don't send '\0' characters. */ 573 if(c != '\0') { 574 if(write(pp->io.fd, &c, 1) != 1) { 575 msyslog(LOG_NOTICE, "ARCRON: write to fd %d failed", pp->io.fd); 576 } 577 #ifdef ARCRON_DEBUG 578 else if(debug) { printf("arc: sent `%2.2x', fd %d.\n", c, pp->io.fd); } 579 #endif 580 } 581 } 582 #endif /* 0 */ 583 584 /* 585 * arc_start - open the devices and initialize data for processing 586 */ 587 static int 588 arc_start( 589 int unit, 590 struct peer *peer 591 ) 592 { 593 register struct arcunit *up; 594 struct refclockproc *pp; 595 int fd; 596 char device[20]; 597 #ifdef HAVE_TERMIOS 598 struct termios arg; 599 #endif 600 601 msyslog(LOG_NOTICE, "ARCRON: %s: opening unit %d", arc_version, unit); 602 #ifdef ARCRON_DEBUG 603 if(debug) { 604 printf("arc: %s: attempt to open unit %d.\n", arc_version, unit); 605 } 606 #endif 607 608 /* Prevent a ridiculous device number causing overflow of device[]. */ 609 if((unit < 0) || (unit > 255)) { return(0); } 610 611 /* 612 * Open serial port. Use CLK line discipline, if available. 613 */ 614 (void)sprintf(device, DEVICE, unit); 615 if (!(fd = refclock_open(device, SPEED, LDISC_CLK))) 616 return(0); 617 #ifdef ARCRON_DEBUG 618 if(debug) { printf("arc: unit %d using open().\n", unit); } 619 #endif 620 fd = open(device, OPEN_FLAGS); 621 if(fd < 0) { 622 #ifdef DEBUG 623 if(debug) { printf("arc: failed [open()] to open %s.\n", device); } 624 #endif 625 return(0); 626 } 627 628 fcntl(fd, F_SETFL, 0); /* clear the descriptor flags */ 629 #ifdef ARCRON_DEBUG 630 if(debug) 631 { printf("Opened RS232 port with file descriptor %d.\n", fd); } 632 #endif 633 634 #ifdef HAVE_TERMIOS 635 636 arg.c_iflag = IGNBRK | ISTRIP; 637 arg.c_oflag = 0; 638 arg.c_cflag = B300 | CS8 | CREAD | CLOCAL | CSTOPB; 639 arg.c_lflag = 0; 640 arg.c_cc[VMIN] = 1; 641 arg.c_cc[VTIME] = 0; 642 643 tcsetattr(fd, TCSANOW, &arg); 644 645 #else 646 647 msyslog(LOG_ERR, "ARCRON: termios not supported in this driver"); 648 (void)close(fd); 649 650 return 0; 651 652 #endif 653 654 up = (struct arcunit *) emalloc(sizeof(struct arcunit)); 655 if(!up) { (void) close(fd); return(0); } 656 /* Set structure to all zeros... */ 657 memset((char *)up, 0, sizeof(struct arcunit)); 658 pp = peer->procptr; 659 pp->io.clock_recv = arc_receive; 660 pp->io.srcclock = (caddr_t)peer; 661 pp->io.datalen = 0; 662 pp->io.fd = fd; 663 if(!io_addclock(&pp->io)) { (void) close(fd); free(up); return(0); } 664 pp->unitptr = (caddr_t)up; 665 666 /* 667 * Initialize miscellaneous variables 668 */ 669 peer->precision = PRECISION; 670 peer->stratum = 2; /* Default to stratum 2 not 0. */ 671 pp->clockdesc = DESCRIPTION; 672 memcpy((char *)&pp->refid, REFID, 4); 673 /* Spread out resyncs so that they should remain separated. */ 674 up->next_resync = current_time + INITIAL_RESYNC_DELAY + (67*unit)%1009; 675 676 #if 0 /* Not needed because of zeroing of arcunit structure... */ 677 up->resyncing = 0; /* Not resyncing yet. */ 678 up->saved_flags = 0; /* Default is all flags off. */ 679 /* Clear send buffer out... */ 680 { 681 int i; 682 for(i = CMDQUEUELEN; i >= 0; --i) { up->cmdqueue[i] = '\0'; } 683 } 684 #endif 685 686 #ifdef ARCRON_KEEN 687 up->quality = QUALITY_UNKNOWN; /* Trust the clock immediately. */ 688 #else 689 up->quality = MIN_CLOCK_QUALITY;/* Don't trust the clock yet. */ 690 #endif 691 return(1); 692 } 693 694 695 /* 696 * arc_shutdown - shut down the clock 697 */ 698 static void 699 arc_shutdown( 700 int unit, 701 struct peer *peer 702 ) 703 { 704 register struct arcunit *up; 705 struct refclockproc *pp; 706 707 pp = peer->procptr; 708 up = (struct arcunit *)pp->unitptr; 709 io_closeclock(&pp->io); 710 free(up); 711 } 712 713 /* 714 Compute space left in output buffer. 715 */ 716 static int 717 space_left( 718 register struct arcunit *up 719 ) 720 { 721 int spaceleft; 722 723 /* Compute space left in buffer after any pending output. */ 724 for(spaceleft = 0; spaceleft < CMDQUEUELEN; ++spaceleft) 725 { if(up->cmdqueue[CMDQUEUELEN - 1 - spaceleft] != '\0') { break; } } 726 return(spaceleft); 727 } 728 729 /* 730 Send command by copying into command buffer as far forward as possible, 731 after any pending output. 732 733 Indicate an error by returning 0 if there is not space for the command. 734 */ 735 static int 736 send_slow( 737 register struct arcunit *up, 738 int fd, 739 const char *s 740 ) 741 { 742 int sl = strlen(s); 743 int spaceleft = space_left(up); 744 745 #ifdef ARCRON_DEBUG 746 if(debug > 1) { printf("arc: spaceleft = %d.\n", spaceleft); } 747 #endif 748 if(spaceleft < sl) { /* Should not normally happen... */ 749 #ifdef ARCRON_DEBUG 750 msyslog(LOG_NOTICE, "ARCRON: send-buffer overrun (%d/%d)", 751 sl, spaceleft); 752 #endif 753 return(0); /* FAILED! */ 754 } 755 756 /* Copy in the command to be sent. */ 757 while(*s) { up->cmdqueue[CMDQUEUELEN - spaceleft--] = *s++; } 758 759 return(1); 760 } 761 762 763 /* Macro indicating action we will take for different quality values. */ 764 #define quality_action(q) \ 765 (((q) == QUALITY_UNKNOWN) ? "UNKNOWN, will use clock anyway" : \ 766 (((q) < MIN_CLOCK_QUALITY_OK) ? "TOO POOR, will not use clock" : \ 767 "OK, will use clock")) 768 769 /* 770 * arc_receive - receive data from the serial interface 771 */ 772 static void 773 arc_receive( 774 struct recvbuf *rbufp 775 ) 776 { 777 register struct arcunit *up; 778 struct refclockproc *pp; 779 struct peer *peer; 780 char c; 781 int i, n, wday, month, bst, status; 782 int arc_last_offset; 783 784 /* 785 * Initialize pointers and read the timecode and timestamp 786 */ 787 peer = (struct peer *)rbufp->recv_srcclock; 788 pp = peer->procptr; 789 up = (struct arcunit *)pp->unitptr; 790 791 792 /* 793 If the command buffer is empty, and we are resyncing, insert a 794 g\r quality request into it to poll for signal quality again. 795 */ 796 if((up->resyncing) && (space_left(up) == CMDQUEUELEN)) { 797 #ifdef DEBUG 798 if(debug > 1) { printf("arc: inserting signal-quality poll.\n"); } 799 #endif 800 send_slow(up, pp->io.fd, "g\r"); 801 } 802 803 /* 804 The `arc_last_offset' is the offset in lastcode[] of the last byte 805 received, and which we assume actually received the input 806 timestamp. 807 808 (When we get round to using tty_clk and it is available, we 809 assume that we will receive the whole timecode with the 810 trailing \r, and that that \r will be timestamped. But this 811 assumption also works if receive the characters one-by-one.) 812 */ 813 arc_last_offset = pp->lencode+rbufp->recv_length - 1; 814 815 /* 816 We catch a timestamp iff: 817 818 * The command code is `o' for a timestamp. 819 820 * If ARCRON_MULTIPLE_SAMPLES is undefined then we must have 821 exactly char in the buffer (the command code) so that we 822 only sample the first character of the timecode as our 823 `on-time' character. 824 825 * The first character in the buffer is not the echoed `\r' 826 from the `o` command (so if we are to timestamp an `\r' it 827 must not be first in the receive buffer with lencode==1. 828 (Even if we had other characters following it, we probably 829 would have a premature timestamp on the '\r'.) 830 831 * We have received at least one character (I cannot imagine 832 how it could be otherwise, but anyway...). 833 */ 834 c = rbufp->recv_buffer[0]; 835 if((pp->a_lastcode[0] == 'o') && 836 #ifndef ARCRON_MULTIPLE_SAMPLES 837 (pp->lencode == 1) && 838 #endif 839 ((pp->lencode != 1) || (c != '\r')) && 840 (arc_last_offset >= 1)) { 841 /* Note that the timestamp should be corrected if >1 char rcvd. */ 842 l_fp timestamp; 843 timestamp = rbufp->recv_time; 844 #ifdef DEBUG 845 if(debug) { /* Show \r as `R', other non-printing char as `?'. */ 846 printf("arc: stamp -->%c<-- (%d chars rcvd)\n", 847 ((c == '\r') ? 'R' : (isgraph((int)c) ? c : '?')), 848 rbufp->recv_length); 849 } 850 #endif 851 852 /* 853 Now correct timestamp by offset of last byte received---we 854 subtract from the receive time the delay implied by the 855 extra characters received. 856 857 Reject the input if the resulting code is too long, but 858 allow for the trailing \r, normally not used but a good 859 handle for tty_clk or somesuch kernel timestamper. 860 */ 861 if(arc_last_offset > LENARC) { 862 #ifdef ARCRON_DEBUG 863 if(debug) { 864 printf("arc: input code too long (%d cf %d); rejected.\n", 865 arc_last_offset, LENARC); 866 } 867 #endif 868 pp->lencode = 0; 869 refclock_report(peer, CEVNT_BADREPLY); 870 return; 871 } 872 873 L_SUBUF(×tamp, charoffsets[arc_last_offset]); 874 #ifdef ARCRON_DEBUG 875 if(debug > 1) { 876 printf( 877 "arc: %s%d char(s) rcvd, the last for lastcode[%d]; -%sms offset applied.\n", 878 ((rbufp->recv_length > 1) ? "*** " : ""), 879 rbufp->recv_length, 880 arc_last_offset, 881 mfptoms((unsigned long)0, 882 charoffsets[arc_last_offset], 883 1)); 884 } 885 #endif 886 887 #ifdef ARCRON_MULTIPLE_SAMPLES 888 /* 889 If taking multiple samples, capture the current adjusted 890 sample iff: 891 892 * No timestamp has yet been captured (it is zero), OR 893 894 * This adjusted timestamp is earlier than the one already 895 captured, on the grounds that this one suffered less 896 delay in being delivered to us and is more accurate. 897 898 */ 899 if(L_ISZERO(&(up->lastrec)) || 900 L_ISGEQ(&(up->lastrec), ×tamp)) 901 #endif 902 { 903 #ifdef ARCRON_DEBUG 904 if(debug > 1) { 905 printf("arc: system timestamp captured.\n"); 906 #ifdef ARCRON_MULTIPLE_SAMPLES 907 if(!L_ISZERO(&(up->lastrec))) { 908 l_fp diff; 909 diff = up->lastrec; 910 L_SUB(&diff, ×tamp); 911 printf("arc: adjusted timestamp by -%sms.\n", 912 mfptoms(diff.l_i, diff.l_f, 3)); 913 } 914 #endif 915 } 916 #endif 917 up->lastrec = timestamp; 918 } 919 920 } 921 922 /* Just in case we still have lots of rubbish in the buffer... */ 923 /* ...and to avoid the same timestamp being reused by mistake, */ 924 /* eg on receipt of the \r coming in on its own after the */ 925 /* timecode. */ 926 if(pp->lencode >= LENARC) { 927 #ifdef ARCRON_DEBUG 928 if(debug && (rbufp->recv_buffer[0] != '\r')) 929 { printf("arc: rubbish in pp->a_lastcode[].\n"); } 930 #endif 931 pp->lencode = 0; 932 return; 933 } 934 935 /* Append input to code buffer, avoiding overflow. */ 936 for(i = 0; i < rbufp->recv_length; i++) { 937 if(pp->lencode >= LENARC) { break; } /* Avoid overflow... */ 938 c = rbufp->recv_buffer[i]; 939 940 /* Drop trailing '\r's and drop `h' command echo totally. */ 941 if(c != '\r' && c != 'h') { pp->a_lastcode[pp->lencode++] = c; } 942 943 /* 944 If we've just put an `o' in the lastcode[0], clear the 945 timestamp in anticipation of a timecode arriving soon. 946 947 We would expect to get to process this before any of the 948 timecode arrives. 949 */ 950 if((c == 'o') && (pp->lencode == 1)) { 951 L_CLR(&(up->lastrec)); 952 #ifdef ARCRON_DEBUG 953 if(debug > 1) { printf("arc: clearing timestamp.\n"); } 954 #endif 955 } 956 } 957 958 /* Handle a quality message. */ 959 if(pp->a_lastcode[0] == 'g') { 960 int r, q; 961 962 if(pp->lencode < 3) { return; } /* Need more data... */ 963 r = (pp->a_lastcode[1] & 0x7f); /* Strip parity. */ 964 q = (pp->a_lastcode[2] & 0x7f); /* Strip parity. */ 965 if(((q & 0x70) != 0x30) || ((q & 0xf) > MAX_CLOCK_QUALITY) || 966 ((r & 0x70) != 0x30)) { 967 /* Badly formatted response. */ 968 #ifdef ARCRON_DEBUG 969 if(debug) { printf("arc: bad `g' response %2x %2x.\n", r, q); } 970 #endif 971 return; 972 } 973 if(r == '3') { /* Only use quality value whilst sync in progress. */ 974 up->quality = (q & 0xf); 975 #ifdef DEBUG 976 if(debug) { printf("arc: signal quality %d.\n", up->quality); } 977 #endif 978 } else if( /* (r == '2') && */ up->resyncing) { 979 #ifdef DEBUG 980 if(debug) 981 { 982 printf("arc: sync finished, signal quality %d: %s\n", 983 up->quality, 984 quality_action(up->quality)); 985 } 986 #endif 987 msyslog(LOG_NOTICE, 988 "ARCRON: sync finished, signal quality %d: %s", 989 up->quality, 990 quality_action(up->quality)); 991 up->resyncing = 0; /* Resync is over. */ 992 993 #ifdef ARCRON_KEEN 994 /* Clock quality dubious; resync earlier than usual. */ 995 if((up->quality == QUALITY_UNKNOWN) || 996 (up->quality < MIN_CLOCK_QUALITY_OK)) 997 { up->next_resync = current_time + RETRY_RESYNC_TIME; } 998 #endif 999 } 1000 pp->lencode = 0; 1001 return; 1002 } 1003 1004 /* Stop now if this is not a timecode message. */ 1005 if(pp->a_lastcode[0] != 'o') { 1006 pp->lencode = 0; 1007 refclock_report(peer, CEVNT_BADREPLY); 1008 return; 1009 } 1010 1011 /* If we don't have enough data, wait for more... */ 1012 if(pp->lencode < LENARC) { return; } 1013 1014 1015 /* WE HAVE NOW COLLECTED ONE TIMESTAMP (phew)... */ 1016 #ifdef ARCRON_DEBUG 1017 if(debug > 1) { printf("arc: NOW HAVE TIMESTAMP...\n"); } 1018 #endif 1019 1020 /* But check that we actually captured a system timestamp on it. */ 1021 if(L_ISZERO(&(up->lastrec))) { 1022 #ifdef ARCRON_DEBUG 1023 if(debug) { printf("arc: FAILED TO GET SYSTEM TIMESTAMP\n"); } 1024 #endif 1025 pp->lencode = 0; 1026 refclock_report(peer, CEVNT_BADREPLY); 1027 return; 1028 } 1029 /* 1030 Append a mark of the clock's received signal quality for the 1031 benefit of Derek Mulcahy's Tcl/Tk utility (we map the `unknown' 1032 quality value to `6' for his s/w) and terminate the string for 1033 sure. This should not go off the buffer end. 1034 */ 1035 pp->a_lastcode[pp->lencode] = ((up->quality == QUALITY_UNKNOWN) ? 1036 '6' : ('0' + up->quality)); 1037 pp->a_lastcode[pp->lencode + 1] = '\0'; /* Terminate for printf(). */ 1038 record_clock_stats(&peer->srcadr, pp->a_lastcode); 1039 1040 /* We don't use the micro-/milli- second part... */ 1041 pp->usec = 0; 1042 pp->msec = 0; 1043 1044 n = sscanf(pp->a_lastcode, "o%2d%2d%2d%1d%2d%2d%2d%1d%1d", 1045 &pp->hour, &pp->minute, &pp->second, 1046 &wday, &pp->day, &month, &pp->year, &bst, &status); 1047 1048 /* Validate format and numbers. */ 1049 if(n != 9) { 1050 #ifdef ARCRON_DEBUG 1051 /* Would expect to have caught major problems already... */ 1052 if(debug) { printf("arc: badly formatted data.\n"); } 1053 #endif 1054 refclock_report(peer, CEVNT_BADREPLY); 1055 return; 1056 } 1057 /* 1058 Validate received values at least enough to prevent internal 1059 array-bounds problems, etc. 1060 */ 1061 if((pp->hour < 0) || (pp->hour > 23) || 1062 (pp->minute < 0) || (pp->minute > 59) || 1063 (pp->second < 0) || (pp->second > 60) /*Allow for leap seconds.*/ || 1064 (wday < 1) || (wday > 7) || 1065 (pp->day < 1) || (pp->day > 31) || 1066 (month < 1) || (month > 12) || 1067 (pp->year < 0) || (pp->year > 99)) { 1068 /* Data out of range. */ 1069 refclock_report(peer, CEVNT_BADREPLY); 1070 return; 1071 } 1072 /* Check that BST/UTC bits are the complement of one another. */ 1073 if(!(bst & 2) == !(bst & 4)) { 1074 refclock_report(peer, CEVNT_BADREPLY); 1075 return; 1076 } 1077 1078 if(status & 0x8) { msyslog(LOG_NOTICE, "ARCRON: battery low"); } 1079 1080 /* Year-2000 alert! */ 1081 /* Attempt to wrap 2-digit date into sensible window. */ 1082 if(pp->year < YEAR_PIVOT) { pp->year += 100; } /* Y2KFixes */ 1083 pp->year += 1900; /* use full four-digit year */ /* Y2KFixes */ 1084 /* 1085 Attempt to do the right thing by screaming that the code will 1086 soon break when we get to the end of its useful life. What a 1087 hero I am... PLEASE FIX LEAP-YEAR AND WRAP CODE IN 209X! 1088 */ 1089 if(pp->year >= YEAR_PIVOT+2000-2 ) { /* Y2KFixes */ 1090 /*This should get attention B^> */ 1091 msyslog(LOG_NOTICE, 1092 "ARCRON: fix me! EITHER YOUR DATE IS BADLY WRONG or else I will break soon!"); 1093 } 1094 #ifdef DEBUG 1095 if(debug) { 1096 printf("arc: n=%d %02d:%02d:%02d %02d/%02d/%04d %1d %1d\n", 1097 n, 1098 pp->hour, pp->minute, pp->second, 1099 pp->day, month, pp->year, bst, status); 1100 } 1101 #endif 1102 1103 /* 1104 The status value tested for is not strictly supported by the 1105 clock spec since the value of bit 2 (0x4) is claimed to be 1106 undefined for MSF, yet does seem to indicate if the last resync 1107 was successful or not. 1108 */ 1109 pp->leap = LEAP_NOWARNING; 1110 status &= 0x7; 1111 if(status == 0x3) { 1112 if(status != up->status) 1113 { msyslog(LOG_NOTICE, "ARCRON: signal acquired"); } 1114 } else { 1115 if(status != up->status) { 1116 msyslog(LOG_NOTICE, "ARCRON: signal lost"); 1117 pp->leap = LEAP_NOTINSYNC; /* MSF clock is free-running. */ 1118 up->status = status; 1119 refclock_report(peer, CEVNT_FAULT); 1120 return; 1121 } 1122 } 1123 up->status = status; 1124 1125 pp->day += moff[month - 1]; 1126 1127 if(isleap_4(pp->year) && month > 2) { pp->day++; } /* Y2KFixes */ 1128 1129 /* Convert to UTC if required */ 1130 if(bst & 2) { 1131 pp->hour--; 1132 if (pp->hour < 0) { 1133 pp->hour = 23; 1134 pp->day--; 1135 /* If we try to wrap round the year (BST on 1st Jan), reject.*/ 1136 if(pp->day < 0) { 1137 refclock_report(peer, CEVNT_BADTIME); 1138 return; 1139 } 1140 } 1141 } 1142 1143 /* If clock signal quality is unknown, revert to default PRECISION...*/ 1144 if(up->quality == QUALITY_UNKNOWN) { peer->precision = PRECISION; } 1145 /* ...else improve precision if flag3 is set... */ 1146 else { 1147 peer->precision = ((pp->sloppyclockflag & CLK_FLAG3) ? 1148 HIGHPRECISION : PRECISION); 1149 } 1150 1151 /* Notice and log any change (eg from initial defaults) for flags. */ 1152 if(up->saved_flags != pp->sloppyclockflag) { 1153 #ifdef ARCRON_DEBUG 1154 msyslog(LOG_NOTICE, "ARCRON: flags enabled: %s%s%s%s", 1155 ((pp->sloppyclockflag & CLK_FLAG1) ? "1" : "."), 1156 ((pp->sloppyclockflag & CLK_FLAG2) ? "2" : "."), 1157 ((pp->sloppyclockflag & CLK_FLAG3) ? "3" : "."), 1158 ((pp->sloppyclockflag & CLK_FLAG4) ? "4" : ".")); 1159 /* Note effects of flags changing... */ 1160 if(debug) { 1161 printf("arc: CHOSENSAMPLES(pp) = %d.\n", CHOSENSAMPLES(pp)); 1162 printf("arc: NKEEP(pp) = %d.\n", NKEEP(pp)); 1163 printf("arc: PRECISION = %d.\n", peer->precision); 1164 } 1165 #endif 1166 up->saved_flags = pp->sloppyclockflag; 1167 } 1168 1169 /* Note time of last believable timestamp. */ 1170 pp->lastrec = up->lastrec; 1171 1172 #ifdef ARCRON_LEAPSECOND_KEEN 1173 /* Find out if a leap-second might just have happened... 1174 (ie is this the first hour of the first day of Jan or Jul?) 1175 */ 1176 if((pp->hour == 0) && 1177 (pp->day == 1) && 1178 ((month == 1) || (month == 7))) { 1179 if(possible_leap >= 0) { 1180 /* A leap may have happened, and no resync has started yet...*/ 1181 possible_leap = 1; 1182 } 1183 } else { 1184 /* Definitely not leap-second territory... */ 1185 possible_leap = 0; 1186 } 1187 #endif 1188 1189 if (!refclock_process(pp)) { 1190 refclock_report(peer, CEVNT_BADTIME); 1191 return; 1192 } 1193 refclock_receive(peer); 1194 } 1195 1196 1197 /* request_time() sends a time request to the clock with given peer. */ 1198 /* This automatically reports a fault if necessary. */ 1199 /* No data should be sent after this until arc_poll() returns. */ 1200 static void request_time P((int, struct peer *)); 1201 static void 1202 request_time( 1203 int unit, 1204 struct peer *peer 1205 ) 1206 { 1207 struct refclockproc *pp = peer->procptr; 1208 register struct arcunit *up = (struct arcunit *)pp->unitptr; 1209 #ifdef DEBUG 1210 if(debug) { printf("arc: unit %d: requesting time.\n", unit); } 1211 #endif 1212 if (!send_slow(up, pp->io.fd, "o\r")) { 1213 #ifdef ARCRON_DEBUG 1214 msyslog(LOG_NOTICE, "ARCRON: unit %d: problem sending", unit); 1215 #endif 1216 refclock_report(peer, CEVNT_FAULT); 1217 return; 1218 } 1219 pp->polls++; 1220 } 1221 1222 /* 1223 * arc_poll - called by the transmit procedure 1224 */ 1225 static void 1226 arc_poll( 1227 int unit, 1228 struct peer *peer 1229 ) 1230 { 1231 register struct arcunit *up; 1232 struct refclockproc *pp; 1233 int resync_needed; /* Should we start a resync? */ 1234 1235 pp = peer->procptr; 1236 up = (struct arcunit *)pp->unitptr; 1237 pp->lencode = 0; 1238 memset(pp->a_lastcode, 0, sizeof(pp->a_lastcode)); 1239 1240 #if 0 1241 /* Flush input. */ 1242 tcflush(pp->io.fd, TCIFLUSH); 1243 #endif 1244 1245 /* Resync if our next scheduled resync time is here or has passed. */ 1246 resync_needed = (up->next_resync <= current_time); 1247 1248 #ifdef ARCRON_LEAPSECOND_KEEN 1249 /* 1250 Try to catch a potential leap-second insertion or deletion quickly. 1251 1252 In addition to the normal NTP fun of clocks that don't report 1253 leap-seconds spooking their hosts, this clock does not even 1254 sample the radio sugnal the whole time, so may miss a 1255 leap-second insertion or deletion for up to a whole sample 1256 time. 1257 1258 To try to minimise this effect, if in the first few minutes of 1259 the day immediately following a leap-second-insertion point 1260 (ie in the first hour of the first day of the first and sixth 1261 months), and if the last resync was in the previous day, and a 1262 resync is not already in progress, resync the clock 1263 immediately. 1264 1265 */ 1266 if((possible_leap > 0) && /* Must be 00:XX 01/0{1,7}/XXXX. */ 1267 (!up->resyncing)) { /* No resync in progress yet. */ 1268 resync_needed = 1; 1269 possible_leap = -1; /* Prevent multiple resyncs. */ 1270 msyslog(LOG_NOTICE,"ARCRON: unit %d: checking for leap second",unit); 1271 } 1272 #endif 1273 1274 /* Do a resync if required... */ 1275 if(resync_needed) { 1276 /* First, reset quality value to `unknown' so we can detect */ 1277 /* when a quality message has been responded to by this */ 1278 /* being set to some other value. */ 1279 up->quality = QUALITY_UNKNOWN; 1280 1281 /* Note that we are resyncing... */ 1282 up->resyncing = 1; 1283 1284 /* Now actually send the resync command and an immediate poll. */ 1285 #ifdef DEBUG 1286 if(debug) { printf("arc: sending resync command (h\\r).\n"); } 1287 #endif 1288 msyslog(LOG_NOTICE, "ARCRON: unit %d: sending resync command", unit); 1289 send_slow(up, pp->io.fd, "h\r"); 1290 1291 /* Schedule our next resync... */ 1292 up->next_resync = current_time + DEFAULT_RESYNC_TIME; 1293 1294 /* Drop through to request time if appropriate. */ 1295 } 1296 1297 /* If clock quality is too poor to trust, indicate a fault. */ 1298 /* If quality is QUALITY_UNKNOWN and ARCRON_KEEN is defined,*/ 1299 /* we'll cross our fingers and just hope that the thing */ 1300 /* synced so quickly we did not catch it---we'll */ 1301 /* double-check the clock is OK elsewhere. */ 1302 if( 1303 #ifdef ARCRON_KEEN 1304 (up->quality != QUALITY_UNKNOWN) && 1305 #else 1306 (up->quality == QUALITY_UNKNOWN) || 1307 #endif 1308 (up->quality < MIN_CLOCK_QUALITY_OK)) { 1309 #ifdef DEBUG 1310 if(debug) { 1311 printf("arc: clock quality %d too poor.\n", up->quality); 1312 } 1313 #endif 1314 refclock_report(peer, CEVNT_FAULT); 1315 return; 1316 } 1317 /* This is the normal case: request a timestamp. */ 1318 request_time(unit, peer); 1319 } 1320 1321 #else 1322 int refclock_arc_bs; 1323 #endif 1324