2 * refclock_acts - clock driver for the NIST/PTB Automated Computer Time
3 * Service aka Amalgamated Containerized Trash Service (ACTS)
10 #if defined(REFCLOCK) && (defined(CLOCK_ACTS) || defined(CLOCK_PTBACTS))
14 #include "ntp_unixtime.h"
15 #include "ntp_refclock.h"
16 #include "ntp_stdlib.h"
17 #include "ntp_control.h"
21 #ifdef HAVE_SYS_IOCTL_H
22 # include <sys/ioctl.h>
23 #endif /* HAVE_SYS_IOCTL_H */
25 /* MUST BE AFTER LAST #include <config.h> !!! */
27 #if defined(CLOCK_ACTS) && defined(CLOCK_PTBACTS)
28 # if defined(KEEPPTBACTS)
30 # else /* not KEEPPTBACTS */
32 # endif /* not KEEPPTBACTS */
33 #endif /* CLOCK_ACTS && CLOCK_PTBACTS */
36 * This driver supports the NIST Automated Computer Time Service (ACTS).
37 * It periodically dials a prespecified telephone number, receives the
38 * NIST timecode data and calculates the local clock correction. It is
39 * designed primarily for use as a backup when neither a radio clock nor
40 * connectivity to Internet time servers is available. For the best
41 * accuracy, the individual telephone line/modem delay needs to be
42 * calibrated using outside sources.
44 * The ACTS is located at NIST Boulder, CO, telephone 303 494 4774. A
45 * toll call from a residence telephone in Newark, DE, costs between 14
46 * and 27 cents, depending on time of day, and from a campus telephone
47 * between 3 and 4 cents, although it is not clear what carrier and time
48 * of day discounts apply in this case. The modem dial string will
49 * differ depending on local telephone configuration, etc., and is
50 * specified by the phone command in the configuration file. The
51 * argument to this command is an AT command for a Hayes compatible
54 * The accuracy produced by this driver should be in the range of a
55 * millisecond or two, but may need correction due to the delay
56 * characteristics of the individual modem involved. For undetermined
57 * reasons, some modems work with the ACTS echo-delay measurement scheme
58 * and some don't. This driver tries to do the best it can with what it
59 * gets. Initial experiments with a Practical Peripherals 9600SA modem
60 * here in Delaware suggest an accuracy of a millisecond or two can be
61 * achieved without the scheme by using a fudge time1 value of 65.0 ms.
62 * In either case, the dispersion for a single call involving ten
63 * samples is about 1.3 ms.
65 * The driver can operate in either of three modes, as determined by
66 * the mode parameter in the server configuration command. In mode 0
67 * (automatic) the driver operates continuously at intervals depending
68 * on the prediction error, as measured by the driver, usually in the
69 * order of several hours. In mode 1 (backup) the driver is enabled in
70 * automatic mode only when no other source of synchronization is
71 * available and when more than MAXOUTAGE (3600 s) have elapsed since
72 * last synchronized by other sources. In mode 2 (manual) the driver
73 * operates only when enabled using a fudge flags switch, as described
76 * For reliable call management, this driver requires a 1200-bps modem
77 * with a Hayes-compatible command set and control over the modem data
78 * terminal ready (DTR) control line. Present restrictions require the
79 * use of a POSIX-compatible programming interface, although other
80 * interfaces may work as well. The modem setup string is hard-coded in
81 * the driver and may require changes for nonstandard modems or special
84 * Further information can be found in the README.refclock file in the
85 * ntp - Version 3 distribution.
89 * Ordinarily, the propagation time correction is computed automatically
90 * by ACTS and the driver. When this is not possible or erratic due to
91 * individual modem characteristics, the fudge flag2 switch should be
92 * set to disable the ACTS echo-delay scheme. In any case, the fudge
93 * time1 parameter can be used to adjust the propagation delay as
96 * The ACTS call interval is determined in one of three ways. In manual
97 * mode a call is initiated by setting fudge flag1 using ntpdc, either
98 * manually or via a cron job. In AUTO mode this flag is set by the peer
99 * timer, which is controlled by the sys_poll variable in response to
100 * measured errors. In backup mode the driver is ordinarily asleep, but
101 * awakes (in auto mode) if all other synchronization sources are lost.
102 * In either auto or backup modes, the call interval increases as long
103 * as the measured errors do not exceed the value of the fudge time2
106 * When the fudge flag1 is set, the ACTS calling program is activated.
107 * This program dials each number listed in the phones command of the
108 * configuration file in turn. If a call attempt fails, the next number
109 * in the list is dialed. The fudge flag1 and counter are reset and the
110 * calling program terminated if (a) a valid clock update has been
111 * determined, (b) no more numbers remain in the list, (c) a device
112 * fault or timeout occurs or (d) fudge flag1 is reset manually using
115 * In automatic and backup modes, the driver determines the call
116 * interval using a procedure depending on the measured prediction
117 * error and the fudge time2 parameter. If the error exceeds time2 for a
118 * number of times depending on the current interval, the interval is
119 * decreased, but not less than about 1000 s. If the error is less than
120 * time2 for some number of times, the interval is increased, but not
121 * more than about 18 h. With the default value of zero for fudge time2,
122 * the interval will increase from 1000 s to the 4000-8000-s range, in
123 * which the expected accuracy should be in the 1-2-ms range. Setting
124 * fudge time2 to a large value, like 0.1 s, may result in errors of
125 * that order, but increase the call interval to the maximum. The exact
126 * value for each configuration will depend on the modem and operating
127 * system involved, so some experimentation may be necessary.
131 * DESCRIPTION OF THE AUTOMATED COMPUTER TELEPHONE SERVICE (ACTS)
132 * (reformatted from ACTS on-line computer help information)
134 * The following is transmitted (at 1200 baud) following completion of
135 * the telephone connection.
137 * National Institute of Standards and Technology
138 * Telephone Time Service, Generator 3B
139 * Enter question mark "?" for HELP
141 * MJD YR MO DA H M S ST S UT1 msADV <OTM>
142 * 47999 90-04-18 21:39:15 50 0 +.1 045.0 UTC(NIST) *
143 * 47999 90-04-18 21:39:16 50 0 +.1 045.0 UTC(NIST) *
144 * 47999 90-04-18 21:39:17 50 0 +.1 045.0 UTC(NIST) *
145 * 47999 90-04-18 21:39:18 50 0 +.1 045.0 UTC(NIST) *
146 * 47999 90-04-18 21:39:19 50 0 +.1 037.6 UTC(NIST) #
147 * 47999 90-04-18 21:39:20 50 0 +.1 037.6 UTC(NIST) #
148 * etc..etc...etc.......
150 * UTC = Universal Time Coordinated, the official world time referred to
153 * DST Daylight savings time characters, valid for the continental
154 * U.S., are set as follows:
156 * 00 We are on standard time (ST).
157 * 01-49 Now on DST, go to ST when your local time is 2:00 am and
158 * the count is 01. The count is decremented daily at 00
161 * 51-99 Now on ST, go to DST when your local time is 2:00 am and
162 * the count is 51. The count is decremented daily at 00
165 * The two DST characters provide up to 48 days advance notice of a
166 * change in time. The count remains at 00 or 50 at other times.
168 * LS Leap second flag is set to "1" to indicate that a leap second is
169 * to be added as 23:59:60 (UTC) on the last day of the current UTC
170 * month. The LS flag will be reset to "0" starting with 23:59:60
171 * (UTC). The flag will remain on for the entire month before the
172 * second is added. Leap seconds are added as needed at the end of
173 * any month. Usually June and/or December are chosen.
175 * The leap second flag will be set to a "2" to indicate that a
176 * leap second is to be deleted at 23:59:58--00:00:00 on the last
177 * day of the current month. (This latter provision is included per
178 * international recommendation, however it is not likely to be
179 * required in the near future.)
181 * DUT1 Approximate difference between earth rotation time (UT1) and
182 * UTC, in steps of 0.1 second: DUT1 = UT1 - UTC.
184 * MJD Modified Julian Date, often used to tag certain scientific data.
186 * The full time format is sent at 1200 baud, 8 bit, 1 stop, no parity.
187 * The format at 300 Baud is also 8 bit, 1 stop, no parity. At 300 Baud
188 * the MJD and DUT1 values are deleted and the time is transmitted only
191 * Maximum on line time will be 56 seconds. If all lines are busy at any
192 * time, the oldest call will be terminated if it has been on line more
193 * than 28 seconds, otherwise, the call that first reaches 28 seconds
194 * will be terminated.
196 * Current time is valid at the "on-time" marker (OTM), either "*" or
197 * "#". The nominal on-time marker (*) will be transmitted 45 ms early
198 * to account for the 8 ms required to send 1 character at 1200 Baud,
199 * plus an additional 7 ms for delay from NIST to the user, and
200 * approximately 30 ms "scrambler" delay inherent in 1200 Baud modems.
201 * If the caller echoes all characters, NIST will measure the round trip
202 * delay and advance the on-time marker so that the midpoint of the stop
203 * bit arrives at the user on time. The amount of msADV will reflect the
204 * actual required advance in milliseconds and the OTM will be a "#".
206 * (The NIST system requires 4 or 5 consecutive delay measurements which
207 * are consistent before switching from "*" to "#". If the user has a
208 * 1200 Baud modem with the same internal delay as that used by NIST,
209 * then the "#" OTM should arrive at the user within +-2 ms of the
212 * However, NIST has studied different brands of 1200 Baud modems and
213 * found internal delays from 24 ms to 40 ms and offsets of the "#" OTM
214 * of +-10 ms. For many computer users, +-10 ms accuracy should be more
215 * than adequate since many computer internal clocks can only be set
216 * with granularity of 20 to 50 ms. In any case, the repeatability of
217 * the offset for the "#" OTM should be within +-2 ms, if the dial-up
218 * path is reciprocal and the user doesn't change the brand or model of
221 * This should be true even if the dial-up path on one day is a land-
222 * line of less than 40 ms (one way) and on the next day is a satellite
223 * link of 260 to 300 ms. In the rare event that the path is one way by
224 * satellite and the other way by land line with a round trip
225 * measurement in the range of 90 to 260 ms, the OTM will remain a "*"
226 * indicating 45 ms advance.
228 * For user comments write:
230 * Time and Frequency Division
235 * Software for setting (PC)DOS compatable machines is available on a
236 * 360-kbyte diskette for $35.00 from: NIST Office of Standard Reference
237 * Materials B311-Chemistry Bldg, NIST, Gaithersburg, MD, 20899, (301)
240 * PTB timecode service (+49 531 512038)
241 * The Physikalisch-Technische Bundesanstalt (Germany)
242 * also supports a modem time service
243 * as the data formats are very similar this driver can also be compiled for
244 * utilizing the PTB time code service.
247 * 0000000000111111111122222222223333333333444444444455555555556666666666777777777 7
248 * 0123456789012345678901234567890123456789012345678901234567890123456789012345678 9
249 * 1995-01-23 20:58:51 MEZ 10402303260219950123195849740+40000500 *
250 * A B C D EF G H IJ K L M N O P Q R S T U V W XY Z<CR><LF>
257 * A for DST to ST switch first hour
258 * B for DST to ST switch second hour if not marked in H
265 * L month for next ST/DST changes
273 * T modified julian day (MJD)
275 * V direction and month if leap second
276 * W signal delay (assumed/measured)
277 * X sequence number for additional text line in Y
279 * Z on time marker (* - assumed delay / # measured delay)
280 * <CR>!<LF> ! is second change !
282 * This format is also used by the National Physical Laboratory (NPL)'s
283 * TRUETIME service in the UK. In this case the timezone field is
284 * UTC+0 or UTC+1 for standard and daylight saving time. The phone
285 * number for this service (a premium rate number) is 0891 516 333.
286 * It is not clear whether the echo check is implemented.
288 * For more detail, see http://www.npl.co.uk/npl/cetm/taf/truetime.html.
292 * Interface definitions
294 #define SPEED232 B1200 /* uart speed (1200 cowardly baud) */
295 #define PRECISION (-10) /* precision assumed (about 1 ms) */
297 # define REFID "ACTS" /* reference ID */
298 # define DESCRIPTION "NIST Automated Computer Time Service" /* WRU */
299 # define LENCODE 50 /* length of valid timecode string */
300 # define DEVICE "/dev/acts%d" /* device name and unit */
301 # define REF_ENTRY refclock_acts
302 #else /* not CLOCK_ACTS */
303 # define REFID "TPTB" /* reference ID */
304 # define DESCRIPTION "PTB Automated Computer Time Service"
305 # define LENCODE 78 /* length of valid timecode string */
306 # define DEVICE "/dev/ptb%d" /* device name and unit */
307 # define REF_ENTRY refclock_ptb
308 #endif /* not CLOCK_ACTS */
309 #define MODE_AUTO 0 /* automatic mode */
310 #define MODE_BACKUP 1 /* backup mode */
311 #define MODE_MANUAL 2 /* manual mode */
313 #define MSGCNT 10 /* we need this many ACTS messages */
314 #define SMAX 80 /* max token string length */
315 #define ACTS_MINPOLL 10 /* log2 min poll interval (1024 s) */
316 #define ACTS_MAXPOLL 18 /* log2 max poll interval (16384 s) */
317 #define MAXOUTAGE 3600 /* max before ACTS kicks in (s) */
320 * Modem control strings. These may have to be changed for some modems.
323 * B1 initiate call negotiation using Bell 212A
324 * &C1 enable carrier detect
325 * &D2 hang up and return to command mode on DTR transition
326 * E0 modem command echo disabled
327 * l1 set modem speaker volume to low level
328 * M1 speaker enabled untill carrier detect
329 * Q0 return result codes
330 * V1 return result codes as English words
332 #define MODEM_SETUP "ATB1&C1&D2E0L1M1Q0V1" /* modem setup */
333 #define MODEM_HANGUP "ATH" /* modem disconnect */
338 #define IDLE 60 /* idle timeout (s) */
339 #define WAIT 2 /* wait timeout (s) */
340 #define ANSWER 30 /* answer timeout (s) */
341 #define CONNECT 10 /* connect timeout (s) */
342 #define TIMECODE 15 /* timecode timeout (s) */
345 * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
348 static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
349 static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
352 * Unit control structure
355 int pollcnt; /* poll message counter */
356 int state; /* the first one was Delaware */
357 int run; /* call program run switch */
358 int msgcnt; /* count of ACTS messages received */
359 long redial; /* interval to next automatic call */
360 double msADV; /* millisecond advance of last message */
364 * Function prototypes
366 static int acts_start P((int, struct peer *));
367 static void acts_shutdown P((int, struct peer *));
368 static void acts_receive P((struct recvbuf *));
369 static void acts_poll P((int, struct peer *));
370 static void acts_timeout P((struct peer *));
371 static void acts_disc P((struct peer *));
372 static int acts_write P((struct peer *, const char *));
375 * Transfer vector (conditional structure name)
377 struct refclock REF_ENTRY = {
378 acts_start, /* start up driver */
379 acts_shutdown, /* shut down driver */
380 acts_poll, /* transmit poll message */
381 noentry, /* not used (old acts_control) */
382 noentry, /* not used (old acts_init) */
383 noentry, /* not used (old acts_buginfo) */
384 NOFLAGS /* not used */
389 * acts_start - open the devices and initialize data for processing
398 register struct actsunit *up;
399 struct refclockproc *pp;
405 * Open serial port. Use ACTS line discipline, if available. It
406 * pumps a timestamp into the data stream at every on-time
407 * character '*' found. Note: the port must have modem control
408 * or deep pockets for the phone bill. HP-UX 9.03 users should
409 * have very deep pockets.
411 (void)sprintf(device, DEVICE, unit);
412 if (!(fd = refclock_open(device, SPEED232, LDISC_ACTS)))
414 if (ioctl(fd, TIOCMBIS, (char *)&dtr) < 0) {
415 msyslog(LOG_ERR, "clock %s ACTS no modem control",
416 ntoa(&peer->srcadr));
421 * Allocate and initialize unit structure
423 if (!(up = (struct actsunit *)
424 emalloc(sizeof(struct actsunit)))) {
428 memset((char *)up, 0, sizeof(struct actsunit));
430 pp->io.clock_recv = acts_receive;
431 pp->io.srcclock = (caddr_t)peer;
434 if (!io_addclock(&pp->io)) {
439 pp->unitptr = (caddr_t)up;
442 * Initialize miscellaneous variables
444 peer->precision = PRECISION;
445 pp->clockdesc = DESCRIPTION;
446 memcpy((char *)&pp->refid, REFID, 4);
447 peer->minpoll = ACTS_MINPOLL;
448 peer->maxpoll = ACTS_MAXPOLL;
449 peer->sstclktype = CTL_SST_TS_TELEPHONE;
452 * Initialize modem and kill DTR. We skedaddle if this comes
455 if (!acts_write(peer, MODEM_SETUP)) {
462 * Set up the driver timeout
464 peer->nextdate = current_time + WAIT;
470 * acts_shutdown - shut down the clock
478 register struct actsunit *up;
479 struct refclockproc *pp;
482 up = (struct actsunit *)pp->unitptr;
483 io_closeclock(&pp->io);
489 * acts_receive - receive data from the serial interface
493 struct recvbuf *rbufp
496 register struct actsunit *up;
497 struct refclockproc *pp;
501 char hangup = '%'; /* ACTS hangup */
502 int day; /* day of the month */
503 int month; /* month of the year */
504 u_long mjd; /* Modified Julian Day */
505 double dut1; /* DUT adjustment */
506 double msADV; /* ACTS transmit advance (ms) */
507 char flag; /* calibration flag */
508 #ifndef CLOCK_PTBACTS
509 char utc[10]; /* this is NIST and you're not */
510 u_int dst; /* daylight/standard time indicator */
511 u_int leap; /* leap-second indicator */
513 char leapdir; /* leap direction */
514 u_int leapmonth; /* month of leap */
517 * Initialize pointers and read the timecode and timestamp. If
518 * the OK modem status code, leave it where folks can find it.
520 peer = (struct peer *)rbufp->recv_srcclock;
522 up = (struct actsunit *)pp->unitptr;
523 pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX,
525 if (pp->lencode == 0) {
526 if (strcmp(pp->a_lastcode, "OK") == 0)
532 printf("acts: state %d timecode %d %*s\n", up->state,
533 pp->lencode, pp->lencode, pp->a_lastcode);
541 * State 0. We are not expecting anything. Probably
542 * modem disconnect noise. Go back to sleep.
549 * State 1. We are waiting for the call to be answered.
550 * All we care about here is CONNECT as the first token
551 * in the string. If the modem signals BUSY, ERROR, NO
552 * ANSWER, NO CARRIER or NO DIALTONE, we immediately
553 * hang up the phone. If CONNECT doesn't happen after
554 * ANSWER seconds, hang up the phone. If everything is
555 * okay, start the connect timeout and slide into state
558 if( strcmp(pp->a_lastcode, " ") == 0) {
562 if( strcmp(sys_phone[0],"DIRECT") != 0 ) {
563 (void)strncpy(str, strtok(pp->a_lastcode, " "), SMAX);
564 if (strcmp(str, "BUSY") == 0 || strcmp(str, "ERROR") ==
565 0 || strcmp(str, "NO") == 0) {
566 NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
568 "clock %s ACTS modem status %s",
569 ntoa(&peer->srcadr), pp->a_lastcode);
571 } else if (strcmp(str, "CONNECT") == 0) {
572 peer->nextdate = current_time + CONNECT;
577 (void) strncpy(str,"CONNECT",7);
578 peer->nextdate = current_time + CONNECT;
587 * State 2. The call has been answered and we are
588 * waiting for the first ACTS message. If this doesn't
589 * happen within the timecode timeout, hang up the
590 * phone. We probably got a wrong number or ACTS is
593 peer->nextdate = current_time + TIMECODE;
598 * Real yucky things here. Ignore everything except timecode
599 * messages, as determined by the message length. We told the
600 * terminal routines to end the line with '*' and the line
601 * discipline to strike a timestamp on that character. However,
602 * when the ACTS echo-delay scheme works, the '*' eventually
603 * becomes a '#'. In this case the message is ended by the <CR>
604 * that comes about 200 ms after the '#' and the '#' cannot be
605 * echoed at the proper time. But, this may not be a lose, since
606 * we already have good data from prior messages and only need
607 * the millisecond advance calculated by ACTS. So, if the
608 * message is long enough and has an on-time character at the
609 * right place, we consider the message (but not neccesarily the
610 * timestmap) to be valid.
612 if (pp->lencode != LENCODE)
615 #ifndef CLOCK_PTBACTS
617 * We apparently have a valid timecode message, so dismember it
618 * with sscan(). This routine does a good job in spotting syntax
619 * errors without becoming overly pedantic.
622 * MJD YR MO DA H M S ST S UT1 msADV OTM
623 * 47222 88-03-02 21:39:15 83 0 +.3 045.0 UTC(NBS) *
625 if (sscanf(pp->a_lastcode,
626 "%5ld %2d-%2d-%2d %2d:%2d:%2d %2d %1d %3lf %5lf %s %c",
627 &mjd, &pp->year, &month, &day, &pp->hour, &pp->minute,
628 &pp->second, &dst, &leap, &dut1, &msADV, utc, &flag) != 13) {
629 refclock_report(peer, CEVNT_BADREPLY);
635 * 0000000000111111111122222222223333333333444444444455555555556666666666777777777 7
636 * 0123456789012345678901234567890123456789012345678901234567890123456789012345678 9
637 * 1995-01-23 20:58:51 MEZ 10402303260219950123195849740+40000500 *
639 if (sscanf(pp->a_lastcode,
640 "%*4d-%*2d-%*2d %*2d:%*2d:%2d %*5c%*12c%4d%2d%2d%2d%2d%5ld%2lf%c%2d%3lf%*15c%c",
641 &pp->second, &pp->year, &month, &day, &pp->hour, &pp->minute, &mjd, &dut1, &leapdir, &leapmonth, &msADV, &flag) != 12) {
642 refclock_report(peer, CEVNT_BADREPLY);
647 * Some modems can't be trusted (the Practical Peripherals
648 * 9600SA comes to mind) and, even if they manage to unstick
649 * ACTS, the millisecond advance is wrong, so we use CLK_FLAG2
650 * to disable echoes, if neccessary.
652 if ((flag == '*' || flag == '#') && !(pp->sloppyclockflag &
654 (void)write(pp->io.fd, &flag, 1);
657 * The ACTS timecode format croaks in 2000. Life is short.
658 * Would only the timecode mavens resist the urge to express months
659 * of the year and days of the month in favor of days of the year.
661 if (month < 1 || month > 12 || day < 1) {
662 refclock_report(peer, CEVNT_BADTIME);
667 * Depending on the driver, at this point we have a two-digit year
668 * or a four-digit year. Make sure we have a four-digit year.
670 if ( pp->year < YEAR_PIVOT ) pp->year += 100; /* Y2KFixes */
671 if ( pp->year < YEAR_BREAK ) pp->year += 1900; /* Y2KFixes */
672 if ( !isleap_4(pp->year) ) { /* Y2KFixes */
673 if (day > day1tab[month - 1]) {
674 refclock_report(peer, CEVNT_BADTIME);
677 for (i = 0; i < month - 1; i++)
680 if (day > day2tab[month - 1]) {
681 refclock_report(peer, CEVNT_BADTIME);
684 for (i = 0; i < month - 1; i++)
689 #ifndef CLOCK_PTBACTS
691 pp->leap = LEAP_ADDSECOND;
692 else if (pp->leap == 2)
693 pp->leap = LEAP_DELSECOND;
695 if (leapmonth == month) {
697 pp->leap = LEAP_ADDSECOND;
698 else if (leapdir == '-')
699 pp->leap = LEAP_DELSECOND;
704 * Colossal hack here. We process each sample in a trimmed-mean
705 * filter and determine the reference clock offset and
706 * dispersion. The fudge time1 value is added to each sample as
707 * received. If we collect MSGCNT samples before the '#' on-time
708 * character, we use the results of the filter as is. If the '#'
709 * is found before that, the adjusted msADV is used to correct
710 * the propagation delay.
714 pp->offset += (msADV - up->msADV) * 1000 * 1e-6;
717 if (!refclock_process(pp)) {
718 refclock_report(peer, CEVNT_BADTIME);
720 } else if (up->msgcnt < MSGCNT)
725 * We have a filtered sample offset ready for peer processing.
726 * We use lastrec as both the reference time and receive time in
727 * order to avoid being cute, like setting the reference time
728 * later than the receive time, which may cause a paranoid
729 * protocol module to chuck out the data. Finaly, we unhook the
730 * timeout, arm for the next call, fold the tent and go home.
731 * The little dance with the '%' character is an undocumented
732 * ACTS feature that hangs up the phone real quick without
733 * waiting for carrier loss or long-space disconnect, but we do
734 * these clumsy things anyway.
736 record_clock_stats(&peer->srcadr, pp->a_lastcode);
737 refclock_receive(peer);
738 pp->sloppyclockflag &= ~CLK_FLAG1;
740 (void)write(pp->io.fd, &hangup, 1);
747 * acts_poll - called by the transmit routine
755 register struct actsunit *up;
756 struct refclockproc *pp;
759 * If the driver is running, we set the enable flag (fudge
760 * flag1), which causes the driver timeout routine to initiate a
761 * call to ACTS. If not, the enable flag can be set using
762 * ntpdc. If this is the sustem peer, then follow the system
766 up = (struct actsunit *)pp->unitptr;
769 pp->sloppyclockflag |= CLK_FLAG1;
770 if (peer == sys_peer)
771 peer->hpoll = sys_poll;
773 peer->hpoll = peer->minpoll;
781 * acts_timeout - called by the timer interrupt
788 register struct actsunit *up;
789 struct refclockproc *pp;
793 * If a timeout occurs in other than state 0, the call has
794 * failed. If in state 0, we just see if there is other work to
798 up = (struct actsunit *)pp->unitptr;
803 switch (peer->ttlmax) {
806 * In manual mode the ACTS calling program is activated
807 * by the ntpdc program using the enable flag (fudge
808 * flag1), either manually or by a cron job.
815 * In automatic mode the ACTS calling program runs
816 * continuously at intervals determined by the sys_poll
821 pp->sloppyclockflag |= CLK_FLAG1;
826 * In backup mode the ACTS calling program is disabled,
827 * unless no system peer has been selected for MAXOUTAGE
828 * (3600 s). Once enabled, it runs until some other NTP
832 if (!up->run && sys_peer == 0) {
833 if (current_time - last_time > MAXOUTAGE) {
835 peer->hpoll = peer->minpoll;
836 NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
838 "clock %s ACTS backup started ",
839 ntoa(&peer->srcadr));
841 } else if (up->run && sys_peer->sstclktype != CTL_SST_TS_TELEPHONE) {
842 peer->hpoll = peer->minpoll;
844 NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
846 "clock %s ACTS backup stopped",
847 ntoa(&peer->srcadr));
853 "clock %s ACTS invalid mode", ntoa(&peer->srcadr));
857 * The fudge flag1 is used as an enable/disable; if set either
858 * by the code or via ntpdc, the ACTS calling program is
859 * started; if reset, the phones stop ringing.
861 if (!(pp->sloppyclockflag & CLK_FLAG1)) {
863 peer->nextdate = current_time + IDLE;
868 * Initiate a call to the ACTS service. If we wind up here in
869 * other than state 0, a successful call could not be completed
870 * within minpoll seconds. We advance to the next modem dial
871 * string. If none are left, we log a notice and clear the
872 * enable flag. For future enhancement: call the site RP and
873 * leave an obscene message in his voicemail.
875 if (sys_phone[up->pollcnt][0] == '\0') {
876 refclock_report(peer, CEVNT_TIMEOUT);
877 NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
879 "clock %s ACTS calling program terminated",
880 ntoa(&peer->srcadr));
881 pp->sloppyclockflag &= ~CLK_FLAG1;
884 printf("acts: calling program terminated\n");
887 peer->nextdate = current_time + IDLE;
892 * Raise DTR, call ACTS and start the answer timeout. We think
893 * it strange if the OK status has not been received from the
894 * modem, but plow ahead anyway.
896 if (strcmp(pp->a_lastcode, "OK") != 0)
897 NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
898 msyslog(LOG_NOTICE, "clock %s ACTS no modem status",
899 ntoa(&peer->srcadr));
900 (void)ioctl(pp->io.fd, TIOCMBIS, (char *)&dtr);
901 (void)acts_write(peer, sys_phone[up->pollcnt]);
902 NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
903 msyslog(LOG_NOTICE, "clock %s ACTS calling %s\n",
904 ntoa(&peer->srcadr), sys_phone[up->pollcnt]);
908 peer->nextdate = current_time + ANSWER;
914 * acts_disc - disconnect the call and wait for the ruckus to cool
921 register struct actsunit *up;
922 struct refclockproc *pp;
926 * We should never get here other than in state 0, unless a call
927 * has timed out. We drop DTR, which will reliably get the modem
928 * off the air, even while ACTS is hammering away full tilt.
931 up = (struct actsunit *)pp->unitptr;
932 (void)ioctl(pp->io.fd, TIOCMBIC, (char *)&dtr);
934 NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
935 msyslog(LOG_NOTICE, "clock %s ACTS call failed %d",
936 ntoa(&peer->srcadr), up->state);
939 printf("acts: call failed %d\n", up->state);
943 peer->nextdate = current_time + WAIT;
948 * acts_write - write a message to the serial port
956 register struct actsunit *up;
957 struct refclockproc *pp;
963 * Not much to do here, other than send the message, handle
964 * debug and report faults.
967 up = (struct actsunit *)pp->unitptr;
971 printf("acts: state %d send %d %s\n", up->state, len,
974 code = write(pp->io.fd, str, (unsigned)len) == len;
975 code &= write(pp->io.fd, &cr, 1) == 1;
977 refclock_report(peer, CEVNT_FAULT);
982 int refclock_acts_bs;
983 #endif /* REFCLOCK */