Lines Matching defs:??

11 #if defined(REFCLOCK) && defined(CLOCK_ARCRON_MSF)
13 static const char arc_version[] = { "V1.3 2003/02/21" };
15 /* define PRE_NTP420 for compatibility to previous versions of NTP (at least
16 to 4.1.0 */
20 #define ARCRON_KEEN 1 /* Be keen, and trusting of the clock, if defined. */
24 #define ARCRON_MULTIPLE_SAMPLES 1 /* Use all timestamp bytes as samples. */
29 #undef ARCRON_LEAPSECOND_KEEN /* Respond quickly to leap seconds: doesn't work yet. */
34 Code by Derek Mulcahy, <derek@toybox.demon.co.uk>, 1997.
35 Modifications by Damon Hart-Davis, <d@hd.org>, 1997.
36 Modifications by Paul Alfille, <palfille@partners.org>, 2003.
37 Modifications by Christopher Price, <cprice@cs-home.com>, 2003.
38 Modifications by Nigel Roles <nigel@9fs.org>, 2003.
41 THIS CODE IS SUPPLIED AS IS, WITH NO WARRANTY OF ANY KIND. USE AT
44 Orginally developed and used with ntp3-5.85 by Derek Mulcahy.
46 Built against ntp3-5.90 on Solaris 2.5 using gcc 2.7.2.
48 This code may be freely copied and used and incorporated in other
49 systems providing the disclaimer and notice of authorship are
56 1) Called tcgetattr() before modifying, so that fields correctly initialised
59 2) Altered parsing of timestamp line so that it copes with fields which are
66 MAJOR CHANGES SINCE V1.2
68 1) Applied patch by Andrey Bray <abuse@madhouse.demon.co.uk>
69 2001-02-17 comp.protocols.time.ntp
76 4) Added simplified conversion from localtime to UTC with dst/bst translation
80 6) Fixed a badformat error when no code is available due to stripping
83 7) Fixed a badformat error when clearing lencode & memset a_lastcode in poll
86 8) Lots of code cleanup, including standardized DEBUG macros and removal
87 of unused code
93 I enclose my ntp driver for the Galleon Systems Arc MSF receiver.
95 It works (after a fashion) on both Solaris-1 and Solaris-2.
97 I am currently using ntp3-5.85. I have been running the code for
98 about 7 months without any problems. Even coped with the change to BST!
100 I had to do some funky things to read from the clock because it uses the
101 power from the receive lines to drive the transmit lines. This makes the
102 code look a bit stupid but it works. I also had to put in some delays to
103 allow for the turnaround time from receive to transmit. These delays
104 are between characters when requesting a time stamp so that shouldn't affect
109 The bottom line is that it works but could easily be improved. You are
110 free to do what you will with the code. I haven't been able to determine
111 how good the clock is. I think that this requires a known good clock
112 to compare it against.
118 MAJOR CHANGES SINCE V1.0
120 1) Removal of pollcnt variable that made the clock go permanently
121 off-line once two time polls failed to gain responses.
123 2) Avoiding (at least on Solaris-2) terminal becoming the controlling
124 terminal of the process when we do a low-level open().
126 3) Additional logic (conditional on ARCRON_LEAPSECOND_KEEN being
127 defined) to try to resync quickly after a potential leap-second
128 insertion or deletion.
130 4) Code significantly slimmer at run-time than V1.0.
136 1) The C preprocessor symbol to have the clock built has been changed
137 from ARC to ARCRON_MSF to CLOCK_ARCRON_MSF to minimise the
138 possiblity of clashes with other symbols in the future.
140 2) PRECISION should be -4/-5 (63ms/31ms) for the following reasons:
142 a) The ARC documentation claims the internal clock is (only)
143 accurate to about 20ms relative to Rugby (plus there must be
144 noticable drift and delay in the ms range due to transmission
145 delays and changing atmospheric effects). This clock is not
146 designed for ms accuracy as NTP has spoilt us all to expect.
149 crystal of the sort used in digital watches (ie 32768Hz) which
150 can have large temperature coefficients and drifts; it is not
151 clear if this oscillator is properly disciplined to the MSF
152 transmission, but as the default is to resync only once per
153 *day*, we can imagine that it is not, and is free-running. We
154 can minimise drift by resyncing more often (at the cost of
155 reduced battery life), but drift/wander may still be
158 c) Note that the bit time of 3.3ms adds to the potential error in
159 the the clock timestamp, since the bit clock of the serial link
160 may effectively be free-running with respect to the host clock
161 and the MSF clock. Actually, the error is probably 1/16th of
162 the above, since the input data is probably sampled at at least
163 16x the bit rate.
165 By keeping the clock marked as not very precise, it will have a
166 fairly large dispersion, and thus will tend to be used as a
167 `backup' time source and sanity checker, which this clock is
168 probably ideal for. For an isolated network without other time
169 sources, this clock can probably be expected to provide *much*
170 better than 1s accuracy, which will be fine.
172 By default, PRECISION is set to -4, but experience, especially at a
174 this to be altered to -5. (Note that skews of +/- 10ms are to be
175 expected from the clock from time-to-time.) This improvement of
176 reported precision can be instigated by setting flag3 to 1, though
177 the PRECISION will revert to the normal value while the clock
178 signal quality is unknown whatever the flag3 setting.
180 IN ANY CASE, BE SURE TO SET AN APPROPRIATE FUDGE FACTOR TO REMOVE
181 ANY RESIDUAL SKEW, eg:
184 # Fudge timestamps by about 20ms.
187 You will need to observe your system's behaviour, assuming you have
188 some other NTP source to compare it with, to work out what the
189 fudge factor should be. For my Sun SS1 running SunOS 4.1.3_U1 with
190 my MSF clock with my distance from the MSF transmitter, +20ms
193 3) REFID has been made "MSFa" to reflect the MSF time source and the
196 4) DEFAULT_RESYNC_TIME is the time in seconds (by default) before
197 forcing a resync since the last attempt. This is picked to give a
198 little less than an hour between resyncs and to try to avoid
199 clashing with any regular event at a regular time-past-the-hour
202 The INITIAL_RESYNC_DELAY is to avoid bothering the clock and
203 running down its batteries unnecesarily if ntpd is going to crash
204 or be killed or reconfigured quickly. If ARCRON_KEEN is defined
205 then this period is long enough for (with normal polling rates)
206 enough time samples to have been taken to allow ntpd to sync to
207 the clock before the interruption for the clock to resync to MSF.
208 This avoids ntpd syncing to another peer first and then
209 almost immediately hopping to the MSF clock.
211 The RETRY_RESYNC_TIME is used before rescheduling a resync after a
212 resync failed to reveal a statisfatory signal quality (too low or
215 5) The clock seems quite jittery, so I have increased the
216 median-filter size from the typical (previous) value of 3. I
217 discard up to half the results in the filter. It looks like maybe
218 1 sample in 10 or so (maybe less) is a spike, so allow the median
219 filter to discard at least 10% of its entries or 1 entry, whichever
220 is greater.
222 6) Sleeping *before* each character sent to the unit to allow required
223 inter-character time but without introducting jitter and delay in
224 handling the response if possible.
226 7) If the flag ARCRON_KEEN is defined, take time samples whenever
227 possible, even while resyncing, etc. We rely, in this case, on the
228 clock always giving us a reasonable time or else telling us in the
229 status byte at the end of the timestamp that it failed to sync to
230 MSF---thus we should never end up syncing to completely the wrong
233 8) If the flag ARCRON_OWN_FILTER is defined, use own versions of
234 refclock median-filter routines to get round small bug in 3-5.90
238 9) We would appear to have a year-2000 problem with this clock since
239 it returns only the two least-significant digits of the year. But
240 ntpd ignores the year and uses the local-system year instead, so
241 this is in fact not a problem. Nevertheless, we attempt to do a
245 10)Logs stats information that can be used by Derek's Tcl/Tk utility
246 to show the status of the clock.
248 11)The clock documentation insists that the number of bits per
249 character to be sent to the clock, and sent by it, is 11, including
250 one start bit and two stop bits. The data format is either 7+even
251 or 8+none.
254 TO-DO LIST
257 * Eliminate use of scanf(), and maybe sprintf().
259 * Allow user setting of resync interval to trade battery life for
260 accuracy; maybe could be done via fudge factor or unit number.
262 * Possibly note the time since the last resync of the MSF clock to
263 MSF as the age of the last reference timestamp, ie trust the
266 * Add very slow auto-adjustment up to a value of +/- time2 to correct
267 for long-term errors in the clock value (time2 defaults to 0 so the
268 correction would be disabled by default).
270 * Consider trying to use the tty_clk/ppsclock support.
272 * Possibly use average or maximum signal quality reported during
273 resync, rather than just the last one, which may be atypical.
280 /* These notes seem also to apply usefully to the ARCRON clock. */
282 /* The HKW clock module is a radio receiver tuned into the Rugby */
283 /* MSF time signal tranmitted on 60 kHz. The clock module connects */
284 /* to the computer via a serial line and transmits the time encoded */
285 /* in 15 bytes at 300 baud 7 bits two stop bits even parity */
288 /* All characters sent to the clock are echoed back to the controlling */
291 /* syntax ASCII o<cr> */
292 /* Character o may be replaced if neccesary by a character whose code */
293 /* contains the lowest four bits f(hex) eg */
297 You have to wait for character echo + 10ms before sending next character.
300 /* The clock replies to this command with a sequence of 15 characters */
301 /* which contain the complete time and a final <cr> making 16 characters */
302 /* in total. */
303 /* The RC computer clock will not reply immediately to this command because */
304 /* the start bit edge of the first reply character marks the beginning of */
305 /* the second. So the RC Computer Clock will reply to this command at the */
306 /* start of the next second */
314 /* 7. day of week 1-monday 7-sunday */
315 /* 8. day of month tens */
316 /* 9. day of month units */
317 /* 10. month tens */
318 /* 11. month units */
319 /* 12. year tens */
320 /* 13. year units */
321 /* 14. BST/UTC status */
327 /* bit 2 =1 if UTC is in effect, complementary to the BST bit */
328 /* bit 1 =1 if BST is in effect, according to the BST bit */
329 /* bit 0 BST/UTC change impending bit=1 in case of change impending */
330 /* 15. status */
335 /* bit 3 =1 if low battery is detected */
336 /* bit 2 =1 if the very last reception attempt failed and a valid */
338 /* =0 if the last reception attempt was successful */
339 /* bit 1 =1 if at least one reception since 2:30 am was successful */
340 /* =0 if no reception attempt since 2:30 am was successful */
341 /* bit 0 =1 if the RC Computer Clock contains valid time information */
342 /* This bit is zero after reset and one after the first */
346 Also note g<cr> command which confirms that a resync is in progress, and
347 if so what signal quality (0--5) is available.
348 Also note h<cr> command which starts a resync to MSF signal.
361 #if defined(HAVE_BSD_TTYS)
365 #if defined(HAVE_SYSV_TTYS)
369 #if defined(HAVE_TERMIOS)
382 #define PRECISION (-4) /* Precision (~63 ms). */
383 #define HIGHPRECISION (-5) /* If things are going well... */
384 #define REFID "MSFa" /* Reference ID. */
385 #define REFID_MSF "MSF" /* Reference ID. */
386 #define REFID_DCF77 "DCF" /* Reference ID. */
387 #define REFID_WWVB "WWVB" /* Reference ID. */
396 #define LENARC 16 /* Format `o' timecode length. */
398 #define BITSPERCHAR 11 /* Bits per character. */
399 #define BITTIME 0x0DA740E /* Time for 1 bit at 300bps. */
400 #define CHARTIME10 0x8888888 /* Time for 10-bit char at 300bps. */
401 #define CHARTIME11 0x962FC96 /* Time for 11-bit char at 300bps. */
402 #define CHARTIME /* Time for char at 300bps. */ \
403 ( (BITSPERCHAR == 11) ? CHARTIME11 : ( (BITSPERCHAR == 10) ? CHARTIME10 : \
406 /* Allow for UART to accept char half-way through final stop bit. */
410 charoffsets[x] is the time after the start of the second that byte
411 x (with the first byte being byte 1) is received by the UART,
412 assuming that the initial edge of the start bit of the first byte
413 is on-time. The values are represented as the fractional part of
414 an l_fp.
416 We store enough values to have the offset of each byte including
417 the trailing \r, on the assumption that the bytes follow one
421 #if BITSPERCHAR == 11 /* Usual case. */
422 /* Offsets computed as accurately as possible... */
424 INITIALOFFSET + 0x0962fc96, /* 1 chars, 11 bits */
425 INITIALOFFSET + 0x12c5f92c, /* 2 chars, 22 bits */
426 INITIALOFFSET + 0x1c28f5c3, /* 3 chars, 33 bits */
427 INITIALOFFSET + 0x258bf259, /* 4 chars, 44 bits */
428 INITIALOFFSET + 0x2eeeeeef, /* 5 chars, 55 bits */
429 INITIALOFFSET + 0x3851eb85, /* 6 chars, 66 bits */
430 INITIALOFFSET + 0x41b4e81b, /* 7 chars, 77 bits */
431 INITIALOFFSET + 0x4b17e4b1, /* 8 chars, 88 bits */
432 INITIALOFFSET + 0x547ae148, /* 9 chars, 99 bits */
433 INITIALOFFSET + 0x5dddddde, /* 10 chars, 110 bits */
434 INITIALOFFSET + 0x6740da74, /* 11 chars, 121 bits */
435 INITIALOFFSET + 0x70a3d70a, /* 12 chars, 132 bits */
436 INITIALOFFSET + 0x7a06d3a0, /* 13 chars, 143 bits */
437 INITIALOFFSET + 0x8369d037, /* 14 chars, 154 bits */
438 INITIALOFFSET + 0x8ccccccd, /* 15 chars, 165 bits */
439 INITIALOFFSET + 0x962fc963 /* 16 chars, 176 bits */
452 INITIALOFFSET + 10 * CHARTIME,
453 INITIALOFFSET + 11 * CHARTIME,
454 INITIALOFFSET + 12 * CHARTIME,
455 INITIALOFFSET + 13 * CHARTIME,
456 INITIALOFFSET + 14 * CHARTIME,
457 INITIALOFFSET + 15 * CHARTIME,
458 INITIALOFFSET + 16 * CHARTIME
462 #define DEFAULT_RESYNC_TIME (57*60) /* Gap between resync attempts (s). */
463 #define RETRY_RESYNC_TIME (27*60) /* Gap to emergency resync attempt. */
467 #define INITIAL_RESYNC_DELAY 50 /* Delay before first resync. */
470 static const int moff[12] =
471 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
472 /* Flags for a raw open() of the clock serial device. */
473 #ifdef O_NOCTTY /* Good, we can avoid tty becoming controlling tty. */
475 #else /* Oh well, it may not matter... */
480 /* Length of queue of command bytes to be sent. */
482 /* Queue tick time; interval in seconds between chars taken off queue. */
483 /* Must be >= 2 to allow o\r response to come back uninterrupted. */
484 #define QUEUETICK 2 /* Allow o\r reply to finish. */
493 int quality; /* Quality of reception 0--5 for unit. */
494 /* We may also use the values -1 or 6 internally. */
495 u_long quality_stamp; /* Next time to reset quality average. */
497 u_long next_resync; /* Next resync time (s) compared to current_time. */
498 int resyncing; /* Resync in progress if true. */
500 /* In the outgoing queue, cmdqueue[0] is next to be sent. */
501 char cmdqueue[CMDQUEUELEN+1]; /* Queue of outgoing commands + \0. */
507 /* The flag `possible_leap' is set non-zero when any MSF unit
510 Set whenever we receive a valid time sample in the first hour of
511 the first day of the first/seventh months.
513 Outside the special hour this value is unconditionally set
514 to zero by the receive routine.
516 On finding itself in this timeslot, as long as the value is
517 non-negative, the receive routine sets it to a positive value to
518 indicate a resync to MSF should be performed.
520 In the poll routine, if this value is positive and we are not
521 already resyncing (eg from a sync that started just before
522 midnight), start resyncing and set this value negative to
524 set this negative prevents the receive routine setting it
528 static int possible_leap = 0; /* No resync required by default. */
531 #if 0
538 #define MIN_CLOCK_QUALITY_OK 3 /* Min quality for OK reception. */
553 arc_start, /* start up driver */
562 /* Queue us up for the next tick. */
563 #define ENQUEUE(up) \
564 do { \
568 /* Placeholder event handler---does nothing safely---soaks up loose tick. */
575 if(debug) { printf("arc: dummy_event_handler() called.\n"); }
582 Take first character off queue and send to clock if not a null.
584 Shift characters down and put a null on the end.
586 We assume that there is no parallelism so no race condition, but even
587 if there is nothing bad will happen except that we might send some bad
588 data to the clock once in a while.
595 struct refclockproc *pp = peer->procptr;
596 register struct arcunit *up = pp->unitptr;
600 if(debug > 2) { printf("arc: arc_event_handler() called.\n"); }
603 c = up->cmdqueue[0]; /* Next char to be sent. */
604 /* Shift down characters, shifting trailing \0 in at end. */
606 { up->cmdqueue[i] = up->cmdqueue[i+1]; }
609 if(c != '\0') {
610 if(write(pp->io.fd, &c, 1) != 1) {
611 msyslog(LOG_NOTICE, "ARCRON: write to fd %d failed", pp->io.fd);
614 else if(debug) { printf("arc: sent `%2.2x', fd %d.\n", c, pp->io.fd); }
618 ENQUEUE(up);
630 register struct arcunit *up;
631 struct refclockproc *pp;
633 int fd;
634 char device[20];
641 DPRINTF(1, ("arc: %s: attempt to open unit %d.\n", arc_version,
645 * Open serial port. Use CLK line discipline, if available.
649 if (temp_fd <= 0)
652 fd = tty_open(device, OPEN_FLAGS, 0777);
653 if (fd < 0) {
660 temp_fd = -1; /* not used after this, at *this* time. */
663 if (-1 == fcntl(fd, F_SETFL, 0)) /* clear the descriptor flags */
668 DPRINTF(1, ("arc: opened RS232 port with file descriptor %d.\n", fd));
672 if (tcgetattr(fd, &arg) < 0) {
675 close(fd);
686 if (tcsetattr(fd, TCSANOW, &arg) < 0) {
689 close(fd);
695 msyslog(LOG_ERR, "ARCRON: termios required by this driver");
696 (void)close(fd);
702 /* Set structure to all zeros... */
703 up = emalloc_zero(sizeof(*up));
704 pp = peer->procptr;
705 pp->io.clock_recv = arc_receive;
706 pp->io.srcclock = peer;
707 pp->io.datalen = 0;
708 pp->io.fd = fd;
709 if (!io_addclock(&pp->io)) {
710 close(fd);
711 pp->io.fd = -1;
712 free(up);
715 pp->unitptr = up;
721 peer->stratum = 2; /* Default to stratum 2 not 0. */
722 pp->clockdesc = DESCRIPTION;
723 if (peer->MODE > 3) {
728 if(debug) { printf("arc: mode = %d.\n", peer->MODE); }
732 memcpy((char *)&pp->refid, REFID_MSF, 4);
735 memcpy((char *)&pp->refid, REFID_DCF77, 4);
738 memcpy((char *)&pp->refid, REFID_WWVB, 4);
741 memcpy((char *)&pp->refid, REFID, 4);
744 /* Spread out resyncs so that they should remain separated. */
745 up->next_resync = current_time + INITIAL_RESYNC_DELAY + (67*unit)%1009;
747 #if 0 /* Not needed because of zeroing of arcunit structure... */
748 up->resyncing = 0; /* Not resyncing yet. */
749 up->saved_flags = 0; /* Default is all flags off. */
753 for(i = CMDQUEUELEN; i >= 0; --i) { up->cmdqueue[i] = '\0'; }
758 up->quality = QUALITY_UNKNOWN; /* Trust the clock immediately. */
760 up->quality = MIN_CLOCK_QUALITY;/* Don't trust the clock yet. */
765 ENQUEUE(up);
780 register struct arcunit *up;
781 struct refclockproc *pp;
785 pp = peer->procptr;
786 up = pp->unitptr;
787 if (-1 != pp->io.fd)
788 io_closeclock(&pp->io);
789 if (NULL != up)
790 free(up);
794 Compute space left in output buffer.
798 register struct arcunit *up
803 /* Compute space left in buffer after any pending output. */
805 { if(up->cmdqueue[CMDQUEUELEN - 1 - spaceleft] != '\0') { break; } }
810 Send command by copying into command buffer as far forward as possible,
813 Indicate an error by returning 0 if there is not space for the command.
817 register struct arcunit *up,
818 int fd,
822 int sl = strlen(s);
823 int spaceleft = space_left(up);
826 if(debug > 1) { printf("arc: spaceleft = %d.\n", spaceleft); }
828 if(spaceleft < sl) { /* Should not normally happen... */
831 sl, spaceleft);
836 /* Copy in the command to be sent. */
837 while(*s && spaceleft > 0) { up->cmdqueue[CMDQUEUELEN - spaceleft--] = *s++; }
846 if (!isdigit((unsigned char)p[0]) || !isdigit((unsigned char)p[1])) return 0;
847 *val = (p[0] - '0') * 10 + p[1] - '0';
854 if (!isdigit((unsigned char)p[0])) return 0;
859 /* Macro indicating action we will take for different quality values. */
863 "OK, will use clock"))
873 register struct arcunit *up;
874 struct refclockproc *pp;
887 pp = peer->procptr;
888 up = pp->unitptr;
892 If the command buffer is empty, and we are resyncing, insert a
893 g\r quality request into it to poll for signal quality again.
895 if((up->resyncing) && (space_left(up) == CMDQUEUELEN)) {
897 if(debug > 1) { printf("arc: inserting signal-quality poll.\n"); }
899 send_slow(up, pp->io.fd, "g\r");
903 The `arc_last_offset' is the offset in lastcode[] of the last byte
904 received, and which we assume actually received the input
907 (When we get round to using tty_clk and it is available, we
908 assume that we will receive the whole timecode with the
909 trailing \r, and that that \r will be timestamped. But this
910 assumption also works if receive the characters one-by-one.)
912 arc_last_offset = pp->lencode+rbufp->recv_length - 1;
915 We catch a timestamp iff:
917 * The command code is `o' for a timestamp.
919 * If ARCRON_MULTIPLE_SAMPLES is undefined then we must have
920 exactly char in the buffer (the command code) so that we
921 only sample the first character of the timecode as our
922 `on-time' character.
924 * The first character in the buffer is not the echoed `\r'
925 from the `o` command (so if we are to timestamp an `\r' it
926 must not be first in the receive buffer with lencode==1.
927 (Even if we had other characters following it, we probably
928 would have a premature timestamp on the '\r'.)
930 * We have received at least one character (I cannot imagine
931 how it could be otherwise, but anyway...).
934 if((pp->a_lastcode[0] == 'o') &&
936 (pp->lencode == 1) &&
938 ((pp->lencode != 1) || (c != '\r')) &&
940 /* Note that the timestamp should be corrected if >1 char rcvd. */
944 if(debug) { /* Show \r as `R', other non-printing char as `?'. */
952 Now correct timestamp by offset of last byte received---we
953 subtract from the receive time the delay implied by the
956 Reject the input if the resulting code is too long, but
958 handle for tty_clk or somesuch kernel timestamper.
960 if(arc_last_offset > LENARC) {
962 if(debug) {
963 printf("arc: input code too long (%d cf %d); rejected.\n",
967 pp->lencode = 0;
974 if(debug > 1) {
988 If taking multiple samples, capture the current adjusted
991 * No timestamp has yet been captured (it is zero), OR
993 * This adjusted timestamp is earlier than the one already
994 captured, on the grounds that this one suffered less
995 delay in being delivered to us and is more accurate.
998 if(L_ISZERO(&(up->lastrec)) ||
999 L_ISGEQ(&(up->lastrec), &timestamp))
1003 if(debug > 1) {
1006 if(!L_ISZERO(&(up->lastrec))) {
1008 diff = up->lastrec;
1010 printf("arc: adjusted timestamp by -%sms.\n",
1016 up->lastrec = timestamp;
1021 /* Just in case we still have lots of rubbish in the buffer... */
1022 /* ...and to avoid the same timestamp being reused by mistake, */
1023 /* eg on receipt of the \r coming in on its own after the */
1025 if(pp->lencode >= LENARC) {
1027 if(debug && (rbufp->recv_buffer[0] != '\r'))
1028 { printf("arc: rubbish in pp->a_lastcode[].\n"); }
1030 pp->lencode = 0;
1034 /* Append input to code buffer, avoiding overflow. */
1036 if(pp->lencode >= LENARC) { break; } /* Avoid overflow... */
1040 if(c != '\r' && c != 'h') { pp->a_lastcode[pp->lencode++] = c; }
1043 If we've just put an `o' in the lastcode[0], clear the
1044 timestamp in anticipation of a timecode arriving soon.
1046 We would expect to get to process this before any of the
1049 if((c == 'o') && (pp->lencode == 1)) {
1050 L_CLR(&(up->lastrec));
1052 if(debug > 1) { printf("arc: clearing timestamp.\n"); }
1056 if (pp->lencode == 0) return;
1059 if(pp->a_lastcode[0] == 'g') {
1062 if(pp->lencode < 3) { return; } /* Need more data... */
1063 r = (pp->a_lastcode[1] & 0x7f); /* Strip parity. */
1064 q = (pp->a_lastcode[2] & 0x7f); /* Strip parity. */
1065 if(((q & 0x70) != 0x30) || ((q & 0xf) > MAX_CLOCK_QUALITY) ||
1069 if(debug) { printf("arc: bad `g' response %2x %2x.\n", r, q); }
1073 if(r == '3') { /* Only use quality value whilst sync in progress. */
1074 if (up->quality_stamp < current_time) {
1080 up->quality_stamp =
1081 current_time + 60 - cal.second + 5;
1085 quality_sum += (q & 0xf);
1089 if(debug) { printf("arc: signal quality %d (%d).\n", quality_average, (q & 0xf)); }
1091 } else if( /* (r == '2') && */ up->resyncing) {
1092 up->quality = quality_average;
1094 if(debug)
1097 up->quality,
1098 quality_action(up->quality));
1103 up->quality,
1104 quality_action(up->quality));
1105 up->resyncing = 0; /* Resync is over. */
1112 if((up->quality == QUALITY_UNKNOWN) ||
1113 (up->quality < MIN_CLOCK_QUALITY_OK))
1114 { up->next_resync = current_time + RETRY_RESYNC_TIME; }
1117 pp->lencode = 0;
1121 /* Stop now if this is not a timecode message. */
1122 if(pp->a_lastcode[0] != 'o') {
1123 pp->lencode = 0;
1128 /* If we don't have enough data, wait for more... */
1129 if(pp->lencode < LENARC) { return; }
1132 /* WE HAVE NOW COLLECTED ONE TIMESTAMP (phew)... */
1134 if(debug > 1) { printf("arc: NOW HAVE TIMESTAMP...\n"); }
1137 /* But check that we actually captured a system timestamp on it. */
1138 if(L_ISZERO(&(up->lastrec))) {
1140 if(debug) { printf("arc: FAILED TO GET SYSTEM TIMESTAMP\n"); }
1142 pp->lencode = 0;
1147 Append a mark of the clock's received signal quality for the
1148 benefit of Derek Mulcahy's Tcl/Tk utility (we map the `unknown'
1149 quality value to `6' for his s/w) and terminate the string for
1150 sure. This should not go off the buffer end.
1152 pp->a_lastcode[pp->lencode] = ((up->quality == QUALITY_UNKNOWN) ?
1153 '6' : ('0' + up->quality));
1154 pp->a_lastcode[pp->lencode + 1] = '\0'; /* Terminate for printf(). */
1157 /* We don't use the micro-/milli- second part... */
1158 pp->usec = 0;
1159 pp->msec = 0;
1161 /* We don't use the nano-second part... */
1162 pp->nsec = 0;
1165 if (pp->a_lastcode[0] != 'o'
1166 || !get2(pp->a_lastcode + 1, &pp->hour)
1167 || !get2(pp->a_lastcode + 3, &pp->minute)
1168 || !get2(pp->a_lastcode + 5, &pp->second)
1169 || !get1(pp->a_lastcode + 7, &wday)
1170 || !get2(pp->a_lastcode + 8, &pp->day)
1171 || !get2(pp->a_lastcode + 10, &month)
1172 || !get2(pp->a_lastcode + 12, &pp->year)) {
1174 /* Would expect to have caught major problems already... */
1175 if(debug) { printf("arc: badly formatted data.\n"); }
1177 pp->lencode = 0;
1181 flags = pp->a_lastcode[14];
1182 status = pp->a_lastcode[15];
1184 if(debug) { printf("arc: status 0x%.2x flags 0x%.2x\n", flags, status); }
1189 Validate received values at least enough to prevent internal
1192 if((pp->hour < 0) || (pp->hour > 23) ||
1193 (pp->minute < 0) || (pp->minute > 59) ||
1194 (pp->second < 0) || (pp->second > 60) /*Allow for leap seconds.*/ ||
1196 (pp->day < 1) || (pp->day > 31) ||
1197 (month < 1) || (month > 12) ||
1198 (pp->year < 0) || (pp->year > 99)) {
1199 /* Data out of range. */
1200 pp->lencode = 0;
1206 if(peer->MODE == 0) { /* compatiblity to original version */
1208 /* Check that BST/UTC bits are the complement of one another. */
1209 if(!(bst & 2) == !(bst & 4)) {
1210 pp->lencode = 0;
1215 if(status & 0x8) { msyslog(LOG_NOTICE, "ARCRON: battery low"); }
1218 /* Attempt to wrap 2-digit date into sensible window. */
1219 if(pp->year < YEAR_PIVOT) { pp->year += 100; } /* Y2KFixes */
1220 pp->year += 1900; /* use full four-digit year */ /* Y2KFixes */
1222 Attempt to do the right thing by screaming that the code will
1223 soon break when we get to the end of its useful life. What a
1224 hero I am... PLEASE FIX LEAP-YEAR AND WRAP CODE IN 209X!
1226 if(pp->year >= YEAR_PIVOT+2000-2 ) { /* Y2KFixes */
1229 "ARCRON: fix me! EITHER YOUR DATE IS BADLY WRONG or else I will break soon!");
1232 if(debug) {
1233 printf("arc: n=%d %02d:%02d:%02d %02d/%02d/%04d %1d %1d\n",
1235 pp->hour, pp->minute, pp->second,
1236 pp->day, month, pp->year, flags, status);
1241 The status value tested for is not strictly supported by the
1242 clock spec since the value of bit 2 (0x4) is claimed to be
1243 undefined for MSF, yet does seem to indicate if the last resync
1244 was successful or not.
1246 pp->leap = LEAP_NOWARNING;
1247 status &= 0x7;
1248 if(status == 0x3) {
1249 if(status != up->status)
1252 if(status != up->status) {
1254 pp->leap = LEAP_NOTINSYNC; /* MSF clock is free-running. */
1255 up->status = status;
1256 pp->lencode = 0;
1261 up->status = status;
1263 if (peer->MODE == 0) { /* compatiblity to original version */
1266 pp->day += moff[month - 1];
1268 if(isleap_4(pp->year) && month > 2) { pp->day++; }/* Y2KFixes */
1270 /* Convert to UTC if required */
1271 if(bst & 2) {
1272 pp->hour--;
1273 if (pp->hour < 0) {
1274 pp->hour = 23;
1275 pp->day--;
1276 /* If we try to wrap round the year
1277 * (BST on 1st Jan), reject.*/
1278 if(pp->day < 0) {
1279 pp->lencode = 0;
1287 if(peer->MODE > 0) {
1288 if(pp->sloppyclockflag & CLK_FLAG1) {
1289 struct tm local;
1290 struct tm *gmtp;
1294 * Convert to GMT for sites that distribute localtime.
1295 * This means we have to do Y2K conversion on the
1296 * 2-digit year; otherwise, we get the time wrong.
1301 local.tm_year = pp->year-1900;
1303 local.tm_mday = pp->day;
1304 local.tm_hour = pp->hour;
1305 local.tm_min = pp->minute;
1306 local.tm_sec = pp->second;
1316 case 0: /* It is unclear exactly when the
1317 Arcron changes from DST->ST and
1318 ST->DST. Testing has shown this
1319 to be irregular. For the time
1320 being, let the OS decide. */
1323 if (debug)
1324 printf ("arc: DST = 00 (0)\n");
1327 case 1: /* dst->st time */
1330 if (debug)
1331 printf ("arc: DST = 01 (1)\n");
1334 case 2: /* st->dst time */
1337 if (debug)
1338 printf ("arc: DST = 10 (2)\n");
1344 if (debug)
1345 printf ("arc: DST = 11 (3)\n");
1357 if ((gmtp = gmtime (&unixtime)) == NULL)
1359 pp->lencode = 0;
1363 pp->year = gmtp->tm_year+1900;
1365 pp->day = ymd2yd(pp->year,month,gmtp->tm_mday);
1366 /* pp->day = gmtp->tm_yday; */
1367 pp->hour = gmtp->tm_hour;
1368 pp->minute = gmtp->tm_min;
1369 pp->second = gmtp->tm_sec;
1371 if (debug)
1373 printf ("arc: time is %04d/%02d/%02d %02d:%02d:%02d UTC\n",
1374 pp->year,month,gmtp->tm_mday,pp->hour,pp->minute,
1375 pp->second);
1383 pp->day = ymd2yd(pp->year,month,pp->day);
1387 if (peer->MODE == 0) { /* compatiblity to original version */
1388 /* If clock signal quality is
1389 * unknown, revert to default PRECISION...*/
1390 if(up->quality == QUALITY_UNKNOWN) {
1392 } else { /* ...else improve precision if flag3 is set... */
1393 peer->precision = ((pp->sloppyclockflag & CLK_FLAG3) ?
1397 if ((status == 0x3) && (pp->sloppyclockflag & CLK_FLAG2)) {
1398 peer->precision = ((pp->sloppyclockflag & CLK_FLAG3) ?
1400 } else if (up->quality == QUALITY_UNKNOWN) {
1403 peer->precision = ((pp->sloppyclockflag & CLK_FLAG3) ?
1408 /* Notice and log any change (eg from initial defaults) for flags. */
1409 if(up->saved_flags != pp->sloppyclockflag) {
1412 ((pp->sloppyclockflag & CLK_FLAG1) ? "1" : "."),
1413 ((pp->sloppyclockflag & CLK_FLAG2) ? "2" : "."),
1414 ((pp->sloppyclockflag & CLK_FLAG3) ? "3" : "."),
1415 ((pp->sloppyclockflag & CLK_FLAG4) ? "4" : "."));
1416 /* Note effects of flags changing... */
1417 if(debug) {
1421 up->saved_flags = pp->sloppyclockflag;
1424 /* Note time of last believable timestamp. */
1425 pp->lastrec = up->lastrec;
1428 /* Find out if a leap-second might just have happened...
1429 (ie is this the first hour of the first day of Jan or Jul?)
1431 if((pp->hour == 0) &&
1432 (pp->day == 1) &&
1434 if(possible_leap >= 0) {
1435 /* A leap may have happened, and no resync has started yet...*/
1444 if (!refclock_process(pp)) {
1445 pp->lencode = 0;
1449 record_clock_stats(&peer->srcadr, pp->a_lastcode);
1454 /* request_time() sends a time request to the clock with given peer. */
1455 /* This automatically reports a fault if necessary. */
1456 /* No data should be sent after this until arc_poll() returns. */
1464 struct refclockproc *pp = peer->procptr;
1465 register struct arcunit *up = pp->unitptr;
1467 if(debug) { printf("arc: unit %d: requesting time.\n", unit); }
1469 if (!send_slow(up, pp->io.fd, "o\r")) {
1471 if (debug) {
1475 pp->lencode = 0;
1479 pp->polls++;
1483 * arc_poll - called by the transmit procedure
1491 register struct arcunit *up;
1492 struct refclockproc *pp;
1493 int resync_needed; /* Should we start a resync? */
1495 pp = peer->procptr;
1496 up = pp->unitptr;
1497 #if 0
1498 pp->lencode = 0;
1499 memset(pp->a_lastcode, 0, sizeof(pp->a_lastcode));
1502 #if 0
1504 tcflush(pp->io.fd, TCIFLUSH);
1507 /* Resync if our next scheduled resync time is here or has passed. */
1508 resync_needed = ( !(pp->sloppyclockflag & CLK_FLAG2) &&
1509 (up->next_resync <= current_time) );
1513 Try to catch a potential leap-second insertion or deletion quickly.
1515 In addition to the normal NTP fun of clocks that don't report
1517 sample the radio sugnal the whole time, so may miss a
1518 leap-second insertion or deletion for up to a whole sample
1521 To try to minimise this effect, if in the first few minutes of
1523 (ie in the first hour of the first day of the first and sixth
1524 months), and if the last resync was in the previous day, and a
1525 resync is not already in progress, resync the clock
1529 if((possible_leap > 0) && /* Must be 00:XX 01/0{1,7}/XXXX. */
1530 (!up->resyncing)) { /* No resync in progress yet. */
1537 /* Do a resync if required... */
1538 if(resync_needed) {
1539 /* First, reset quality value to `unknown' so we can detect */
1540 /* when a quality message has been responded to by this */
1541 /* being set to some other value. */
1542 up->quality = QUALITY_UNKNOWN;
1544 /* Note that we are resyncing... */
1545 up->resyncing = 1;
1547 /* Now actually send the resync command and an immediate poll. */
1549 if(debug) { printf("arc: sending resync command (h\\r).\n"); }
1552 send_slow(up, pp->io.fd, "h\r");
1555 up->next_resync = current_time + DEFAULT_RESYNC_TIME;
1557 /* Drop through to request time if appropriate. */
1560 /* If clock quality is too poor to trust, indicate a fault. */
1561 /* If quality is QUALITY_UNKNOWN and ARCRON_KEEN is defined,*/
1562 /* we'll cross our fingers and just hope that the thing */
1563 /* synced so quickly we did not catch it---we'll */
1564 /* double-check the clock is OK elsewhere. */
1565 if(
1567 (up->quality != QUALITY_UNKNOWN) &&
1569 (up->quality == QUALITY_UNKNOWN) ||
1571 (up->quality < MIN_CLOCK_QUALITY_OK)) {
1573 if(debug) {
1574 printf("arc: clock quality %d too poor.\n", up->quality);
1577 pp->lencode = 0;
1581 /* This is the normal case: request a timestamp. */