xref: /freebsd/contrib/ntp/ntpd/refclock_acts.c (revision 224ba2bd37e182b64f7d78defef8a6cacaad3415)
1 /*
2  * refclock_acts - clock driver for the NIST/PTB Automated Computer Time
3  *	Service aka Amalgamated Containerized Trash Service (ACTS)
4  */
5 
6 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9 
10 #if defined(REFCLOCK) && (defined(CLOCK_ACTS) || defined(CLOCK_PTBACTS))
11 
12 #include "ntpd.h"
13 #include "ntp_io.h"
14 #include "ntp_unixtime.h"
15 #include "ntp_refclock.h"
16 #include "ntp_stdlib.h"
17 #include "ntp_control.h"
18 
19 #include <stdio.h>
20 #include <ctype.h>
21 #ifdef HAVE_SYS_IOCTL_H
22 # include <sys/ioctl.h>
23 #endif /* HAVE_SYS_IOCTL_H */
24 
25 /* MUST BE AFTER LAST #include <config.h> !!! */
26 
27 #if defined(CLOCK_ACTS) && defined(CLOCK_PTBACTS)
28 # if defined(KEEPPTBACTS)
29 #   undef CLOCK_ACTS
30 # else /* not KEEPPTBACTS */
31 #   undef CLOCK_PTBACTS
32 # endif /* not KEEPPTBACTS */
33 #endif /* CLOCK_ACTS && CLOCK_PTBACTS */
34 
35 /*
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.
43  *
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
52  * modem.
53  *
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.
64  *
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
74  * below.
75  *
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
82  * circumstances.
83  *
84  * Further information can be found in the README.refclock file in the
85  * ntp - Version 3 distribution.
86  *
87  * Fudge Factors
88  *
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
94  * required.
95  *
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
104  * parameter.
105  *
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
113  * ntpdc.
114  *
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.
128  */
129 
130 /*
131  * DESCRIPTION OF THE AUTOMATED COMPUTER TELEPHONE SERVICE (ACTS)
132  * (reformatted from ACTS on-line computer help information)
133  *
134  * The following is transmitted (at 1200 baud) following completion of
135  * the telephone connection.
136  *
137  * National Institute of Standards and Technology
138  * Telephone Time Service, Generator 3B
139  * Enter question mark "?" for HELP
140  *                         D  L D
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.......
149  *
150  * UTC = Universal Time Coordinated, the official world time referred to
151  * the zero meridian.
152  *
153  * DST	Daylight savings time characters, valid for the continental
154  *	U.S., are set as follows:
155  *
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
159  *		(UTC).
160  *	50	We are on DST.
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
163  *		(UTC).
164  *
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.
167  *
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.
174  *
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.)
180  *
181  * DUT1	Approximate difference between earth rotation time (UT1) and
182  *	UTC, in steps of 0.1 second: DUT1 = UT1 - UTC.
183  *
184  * MJD	Modified Julian Date, often used to tag certain scientific data.
185  *
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
189  * on even seconds.
190  *
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.
195  *
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 "#".
205  *
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
210  * correct time.
211  *
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
219  * modem used.
220  *
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.
227  *
228  * For user comments write:
229  * NIST-ACTS
230  * Time and Frequency Division
231  * Mail Stop 847
232  * 325 Broadway
233  * Boulder, CO 80303
234  *
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)
238  * 975-6776
239  *
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.
245  *
246  * Data format
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>
251  *
252  * A year
253  * B month
254  * C day
255  * D hour
256  * E : normally
257  *   A for DST to ST switch first hour
258  *   B for DST to ST switch second hour if not marked in H
259  * F minute
260  * G second
261  * H timezone
262  * I day of week
263  * J week of year
264  * K day of year
265  * L month for next ST/DST changes
266  * M day
267  * N hour
268  * O UTC year
269  * P UTC month
270  * Q UTC day
271  * R UTC hour
272  * S UTC minute
273  * T modified julian day (MJD)
274  * U DUT1
275  * V direction and month if leap second
276  * W signal delay (assumed/measured)
277  * X sequence number for additional text line in Y
278  * Y additional text
279  * Z on time marker (* - assumed delay / # measured delay)
280  * <CR>!<LF> ! is second change !
281  *
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.
287  *
288  * For more detail, see http://www.npl.co.uk/npl/cetm/taf/truetime.html.
289  */
290 
291 /*
292  * Interface definitions
293  */
294 #define	SPEED232	B1200	/* uart speed (1200 cowardly baud) */
295 #define	PRECISION	(-10)	/* precision assumed (about 1 ms) */
296 #ifdef CLOCK_ACTS
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 */
312 
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) */
318 
319 /*
320  * Modem control strings. These may have to be changed for some modems.
321  *
322  * AT	command prefix
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
331  */
332 #define MODEM_SETUP	"ATB1&C1&D2E0L1M1Q0V1" /* modem setup */
333 #define MODEM_HANGUP	"ATH"	/* modem disconnect */
334 
335 /*
336  * Timeouts
337  */
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) */
343 
344 /*
345  * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
346  * leap.
347  */
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};
350 
351 /*
352  * Unit control structure
353  */
354 struct actsunit {
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 */
361 };
362 
363 /*
364  * Function prototypes
365  */
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 *));
373 
374 /*
375  * Transfer vector (conditional structure name)
376  */
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 */
385 };
386 
387 
388 /*
389  * acts_start - open the devices and initialize data for processing
390  */
391 
392 static int
393 acts_start (
394 	int unit,
395 	struct peer *peer
396 	)
397 {
398 	register struct actsunit *up;
399 	struct refclockproc *pp;
400 	int fd;
401 	char device[20];
402 	int dtr = TIOCM_DTR;
403 
404 	/*
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.
410 	 */
411 	(void)sprintf(device, DEVICE, unit);
412 	if (!(fd = refclock_open(device, SPEED232, LDISC_ACTS)))
413 	    return (0);
414 	if (ioctl(fd, TIOCMBIS, (char *)&dtr) < 0) {
415 		msyslog(LOG_ERR, "clock %s ACTS no modem control",
416 			ntoa(&peer->srcadr));
417 		return (0);
418 	}
419 
420 	/*
421 	 * Allocate and initialize unit structure
422 	 */
423 	if (!(up = (struct actsunit *)
424 	      emalloc(sizeof(struct actsunit)))) {
425 		(void) close(fd);
426 		return (0);
427 	}
428 	memset((char *)up, 0, sizeof(struct actsunit));
429 	pp = peer->procptr;
430 	pp->io.clock_recv = acts_receive;
431 	pp->io.srcclock = (caddr_t)peer;
432 	pp->io.datalen = 0;
433 	pp->io.fd = fd;
434 	if (!io_addclock(&pp->io)) {
435 		(void) close(fd);
436 		free(up);
437 		return (0);
438 	}
439 	pp->unitptr = (caddr_t)up;
440 
441 	/*
442 	 * Initialize miscellaneous variables
443 	 */
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;
450 
451 	/*
452 	 * Initialize modem and kill DTR. We skedaddle if this comes
453 	 * bum.
454 	 */
455 	if (!acts_write(peer, MODEM_SETUP)) {
456 		(void) close(fd);
457 		free(up);
458 		return (0);
459 	}
460 
461 	/*
462 	 * Set up the driver timeout
463 	 */
464 	peer->nextdate = current_time + WAIT;
465 	return (1);
466 }
467 
468 
469 /*
470  * acts_shutdown - shut down the clock
471  */
472 static void
473 acts_shutdown (
474 	int unit,
475 	struct peer *peer
476 	)
477 {
478 	register struct actsunit *up;
479 	struct refclockproc *pp;
480 
481 	pp = peer->procptr;
482 	up = (struct actsunit *)pp->unitptr;
483 	io_closeclock(&pp->io);
484 	free(up);
485 }
486 
487 
488 /*
489  * acts_receive - receive data from the serial interface
490  */
491 static void
492 acts_receive (
493 	struct recvbuf *rbufp
494 	)
495 {
496 	register struct actsunit *up;
497 	struct refclockproc *pp;
498 	struct peer *peer;
499 	char str[SMAX];
500 	int i;
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 */
512 #else
513 	char leapdir;		/* leap direction */
514 	u_int leapmonth;	/* month of leap */
515 #endif
516 	/*
517 	 * Initialize pointers and read the timecode and timestamp. If
518 	 * the OK modem status code, leave it where folks can find it.
519 	 */
520 	peer = (struct peer *)rbufp->recv_srcclock;
521 	pp = peer->procptr;
522 	up = (struct actsunit *)pp->unitptr;
523 	pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX,
524 				     &pp->lastrec);
525 	if (pp->lencode == 0) {
526 		if (strcmp(pp->a_lastcode, "OK") == 0)
527 		    pp->lencode = 2;
528 		return;
529 	}
530 #ifdef DEBUG
531 	if (debug)
532 	    printf("acts: state %d timecode %d %*s\n", up->state,
533 		   pp->lencode, pp->lencode, pp->a_lastcode);
534 #endif
535 
536 	switch (up->state) {
537 
538 	    case 0:
539 
540 		/*
541 		 * State 0. We are not expecting anything. Probably
542 		 * modem disconnect noise. Go back to sleep.
543 		 */
544 		return;
545 
546 	    case 1:
547 
548 		/*
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
556 		 * 2.
557 		 */
558 		if( strcmp(pp->a_lastcode, " ") == 0) {
559 			acts_disc(peer);
560 			return;
561 		}
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 */
567 					msyslog(LOG_NOTICE,
568 						"clock %s ACTS modem status %s",
569 						ntoa(&peer->srcadr), pp->a_lastcode);
570 				acts_disc(peer);
571 			} else if (strcmp(str, "CONNECT") == 0) {
572 				peer->nextdate = current_time + CONNECT;
573 				up->msgcnt = 0;
574 				up->state++;
575 			}
576 		} else {
577 			(void) strncpy(str,"CONNECT",7);
578 			peer->nextdate = current_time + CONNECT;
579 			up->msgcnt = 0;
580 			up->state++;
581 		}
582 		return;
583 
584 	    case 2:
585 
586 		/*
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
591 		 * down.
592 		 */
593 		peer->nextdate = current_time + TIMECODE;
594 		up->state++;
595 	}
596 
597 	/*
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.
611 	 */
612 	if (pp->lencode != LENCODE)
613 	    return;
614 
615 #ifndef CLOCK_PTBACTS
616 	/*
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.
620 	 *
621 	 *                         D  L D
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) *
624 	 */
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);
630 		return;
631 	}
632 #else
633 	/*
634 	 * Data format
635 	 * 0000000000111111111122222222223333333333444444444455555555556666666666777777777   7
636 	 * 0123456789012345678901234567890123456789012345678901234567890123456789012345678   9
637 	 * 1995-01-23 20:58:51 MEZ  10402303260219950123195849740+40000500              *
638 	 */
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);
643 		return;
644 	}
645 #endif
646 	/*
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.
651 	 */
652 	if ((flag == '*' || flag == '#') && !(pp->sloppyclockflag &
653 					      CLK_FLAG2))
654 	    (void)write(pp->io.fd, &flag, 1);
655 
656 	/*
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.
660 	 */
661 	if (month < 1 || month > 12 || day < 1) {
662 		refclock_report(peer, CEVNT_BADTIME);
663 		return;
664 	}
665 
666 	/*
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.
669 	 */
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);
675 			return;
676 		}
677 		for (i = 0; i < month - 1; i++)
678 		    day += day1tab[i];
679 	} else {
680 		if (day > day2tab[month - 1]) {
681 			refclock_report(peer, CEVNT_BADTIME);
682 			return;
683 		}
684 		for (i = 0; i < month - 1; i++)
685 		    day += day2tab[i];
686 	}
687 	pp->day = day;
688 
689 #ifndef CLOCK_PTBACTS
690 	if (leap == 1)
691 	    pp->leap = LEAP_ADDSECOND;
692 	else if (pp->leap == 2)
693 	    pp->leap = LEAP_DELSECOND;
694 #else
695 	if (leapmonth == month) {
696 		if (leapdir == '+')
697 		    pp->leap = LEAP_ADDSECOND;
698 		else if (leapdir == '-')
699 		    pp->leap = LEAP_DELSECOND;
700 	}
701 #endif
702 
703 	/*
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.
711 	 */
712 	up->msgcnt++;
713 	if (flag == '#') {
714 		pp->offset += (msADV - up->msADV) * 1000 * 1e-6;
715 	} else {
716 		up->msADV = msADV;
717 		if (!refclock_process(pp)) {
718 			refclock_report(peer, CEVNT_BADTIME);
719 			return;
720 		} else if (up->msgcnt < MSGCNT)
721 		    return;
722 	}
723 
724 	/*
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.
735 	 */
736 	record_clock_stats(&peer->srcadr, pp->a_lastcode);
737 	refclock_receive(peer);
738 	pp->sloppyclockflag &= ~CLK_FLAG1;
739 	up->pollcnt = 0;
740 	(void)write(pp->io.fd, &hangup, 1);
741 	up->state = 0;
742 	acts_disc(peer);
743 }
744 
745 
746 /*
747  * acts_poll - called by the transmit routine
748  */
749 static void
750 acts_poll (
751 	int unit,
752 	struct peer *peer
753 	)
754 {
755 	register struct actsunit *up;
756 	struct refclockproc *pp;
757 
758 	/*
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
763 	 * poll interval.
764 	 */
765 	pp = peer->procptr;
766 	up = (struct actsunit *)pp->unitptr;
767 
768 	if (up->run) {
769 		pp->sloppyclockflag |= CLK_FLAG1;
770 		if (peer == sys_peer)
771 		    peer->hpoll = sys_poll;
772 		else
773 		    peer->hpoll = peer->minpoll;
774 	}
775 	acts_timeout (peer);
776 	return;
777 }
778 
779 
780 /*
781  * acts_timeout - called by the timer interrupt
782  */
783 static void
784 acts_timeout (
785 	struct peer *peer
786 	)
787 {
788 	register struct actsunit *up;
789 	struct refclockproc *pp;
790 	int dtr = TIOCM_DTR;
791 
792 	/*
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
795 	 * do.
796 	 */
797 	pp = peer->procptr;
798 	up = (struct actsunit *)pp->unitptr;
799 	if (up->state) {
800 		acts_disc(peer);
801 		return;
802 	}
803 	switch (peer->ttlmax) {
804 
805 		/*
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.
809 		 */
810 	    case MODE_MANUAL:
811 		up->run = 0;
812 		break;
813 
814 		/*
815 		 * In automatic mode the ACTS calling program runs
816 		 * continuously at intervals determined by the sys_poll
817 		 * variable.
818 		 */
819 	    case MODE_AUTO:
820 		if (!up->run)
821 		    pp->sloppyclockflag |= CLK_FLAG1;
822 		up->run = 1;
823 		break;
824 
825 		/*
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
829 		 * peer shows up.
830 		 */
831 	    case MODE_BACKUP:
832 		if (!up->run && sys_peer == 0) {
833 			if (current_time - last_time > MAXOUTAGE) {
834 				up->run = 1;
835 				peer->hpoll = peer->minpoll;
836 				NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
837 					msyslog(LOG_NOTICE,
838 						"clock %s ACTS backup started ",
839 						ntoa(&peer->srcadr));
840 			}
841 		} else if (up->run && sys_peer->sstclktype != CTL_SST_TS_TELEPHONE) {
842 			peer->hpoll = peer->minpoll;
843 			up->run = 0;
844 			NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
845 				msyslog(LOG_NOTICE,
846 					"clock %s ACTS backup stopped",
847 					ntoa(&peer->srcadr));
848 		}
849 		break;
850 
851 	    default:
852 		msyslog(LOG_ERR,
853 			"clock %s ACTS invalid mode", ntoa(&peer->srcadr));
854 	}
855 
856 	/*
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.
860 	 */
861 	if (!(pp->sloppyclockflag & CLK_FLAG1)) {
862 		up->pollcnt = 0;
863 		peer->nextdate = current_time + IDLE;
864 		return;
865 	}
866 
867 	/*
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.
874 	 */
875 	if (sys_phone[up->pollcnt][0] == '\0') {
876 		refclock_report(peer, CEVNT_TIMEOUT);
877 		NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
878 			msyslog(LOG_NOTICE,
879 				"clock %s ACTS calling program terminated",
880 				ntoa(&peer->srcadr));
881 		pp->sloppyclockflag &= ~CLK_FLAG1;
882 #ifdef DEBUG
883 		if (debug)
884 		    printf("acts: calling program terminated\n");
885 #endif
886 		up->pollcnt = 0;
887 		peer->nextdate = current_time + IDLE;
888 		return;
889 	}
890 
891 	/*
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.
895 	 */
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]);
905 	up->state = 1;
906 	up->pollcnt++;
907 	pp->polls++;
908 	peer->nextdate = current_time + ANSWER;
909 	return;
910 }
911 
912 
913 /*
914  * acts_disc - disconnect the call and wait for the ruckus to cool
915  */
916 static void
917 acts_disc (
918 	struct peer *peer
919 	)
920 {
921 	register struct actsunit *up;
922 	struct refclockproc *pp;
923 	int dtr = TIOCM_DTR;
924 
925 	/*
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.
929 	 */
930 	pp = peer->procptr;
931 	up = (struct actsunit *)pp->unitptr;
932 	(void)ioctl(pp->io.fd, TIOCMBIC, (char *)&dtr);
933 	if (up->state > 0) {
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);
937 #ifdef DEBUG
938 		if (debug)
939 			printf("acts: call failed %d\n", up->state);
940 #endif
941 		up->state = 0;
942 	}
943 	peer->nextdate = current_time + WAIT;
944 }
945 
946 
947 /*
948  * acts_write - write a message to the serial port
949  */
950 static int
951 acts_write (
952 	struct peer *peer,
953 	const char *str
954 	)
955 {
956 	register struct actsunit *up;
957 	struct refclockproc *pp;
958 	int len;
959 	int code;
960 	char cr = '\r';
961 
962 	/*
963 	 * Not much to do here, other than send the message, handle
964 	 * debug and report faults.
965 	 */
966 	pp = peer->procptr;
967 	up = (struct actsunit *)pp->unitptr;
968 	len = strlen(str);
969 #ifdef DEBUG
970 	if (debug)
971 		printf("acts: state %d send %d %s\n", up->state, len,
972 		    str);
973 #endif
974 	code = write(pp->io.fd, str, (unsigned)len) == len;
975 	code &= write(pp->io.fd, &cr, 1) == 1;
976 	if (!code)
977 		refclock_report(peer, CEVNT_FAULT);
978 	return (code);
979 }
980 
981 #else
982 int refclock_acts_bs;
983 #endif /* REFCLOCK */
984