2 * refclock_acts - clock driver for the NIST/USNO/PTB/NPL Computer Time
9 #if defined(REFCLOCK) && (defined(CLOCK_ACTS) || defined(CLOCK_PTBACTS))
13 #include "ntp_unixtime.h"
14 #include "ntp_refclock.h"
15 #include "ntp_stdlib.h"
16 #include "ntp_control.h"
20 #ifdef HAVE_SYS_IOCTL_H
21 # include <sys/ioctl.h>
22 #endif /* HAVE_SYS_IOCTL_H */
25 * This driver supports the US (NIST, USNO) and European (PTB, NPL,
26 * etc.) modem time services, as well as Spectracom GPS and WWVB
27 * receivers connected via a modem. The driver periodically dials a
28 * number from a telephone list, receives the timecode data and
29 * calculates the local clock correction. It is designed primarily for
30 * use as backup when neither a radio clock nor connectivity to Internet
31 * time servers is available.
33 * This driver requires a modem with a Hayes-compatible command set and
34 * control over the modem data terminal ready (DTR) control line. The
35 * modem setup string is hard-coded in the driver and may require
36 * changes for nonstandard modems or special circumstances. For reasons
37 * unrelated to this driver, the data set ready (DSR) control line
38 * should not be set when this driver is first started.
40 * The calling program is initiated by setting fudge flag1, either
41 * manually or automatically. When flag1 is set, the calling program
42 * dials the first number in the phone command of the configuration
43 * file. If that call fails, the calling program dials the second number
44 * and so on. The number is specified by the Hayes ATDT prefix followed
45 * by the number itself, including the prefix and long-distance digits
46 * and delay code, if necessary. The flag1 is reset and the calling
47 * program terminated if (a) a valid clock update has been determined,
48 * (b) no more numbers remain in the list, (c) a device fault or timeout
49 * occurs or (d) fudge flag1 is reset manually.
51 * The driver is transparent to each of the modem time services and
52 * Spectracom radios. It selects the parsing algorithm depending on the
53 * message length. There is some hazard should the message be corrupted.
54 * However, the data format is checked carefully and only if all checks
55 * succeed is the message accepted. Corrupted lines are discarded
60 * flag1 force a call in manual mode
61 * flag2 enable port locking (not verified)
62 * flag3 no modem; port is directly connected to device
65 * time1 offset adjustment (s)
67 * Ordinarily, the serial port is connected to a modem; however, it can
68 * be connected directly to a device or another computer for testing and
69 * calibration. In this case set fudge flag3 and the driver will send a
70 * single character 'T' at each poll event. In principle, fudge flag2
71 * enables port locking, allowing the modem to be shared when not in use
72 * by this driver. At least on Solaris with the current NTP I/O
73 * routines, this results only in lots of ugly error messages.
76 * National Institute of Science and Technology (NIST)
78 * Phone: (303) 494-4774 (Boulder, CO); (808) 335-4721 (Hawaii)
82 * National Institute of Standards and Technology
83 * Telephone Time Service, Generator 3B
84 * Enter question mark "?" for HELP
86 * MJD YR MO DA H M S ST S UT1 msADV <OTM>
87 * 47999 90-04-18 21:39:15 50 0 +.1 045.0 UTC(NIST) *<CR><LF>
90 * MJD, DST, DUT1 and UTC are not used by this driver. The "*" or "#" is
91 * the on-time markers echoed by the driver and used by NIST to measure
92 * and correct for the propagation delay.
94 * US Naval Observatory (USNO)
96 * Phone: (202) 762-1594 (Washington, DC); (719) 567-6742 (Boulder, CO)
98 * Data Format (two lines, repeating at one-second intervals)
100 * jjjjj nnn hhmmss UTC<CR><LF>
103 * jjjjj modified Julian day number (not used)
105 * hhmmss second of day
106 * * on-time marker for previous timecode
109 * USNO does not correct for the propagation delay. A fudge time1 of
110 * about .06 s is advisable.
112 * European Services (PTB, NPL, etc.)
114 * PTB: +49 531 512038 (Germany)
115 * NPL: 0906 851 6333 (UK only)
117 * Data format (see the documentation for phone numbers and formats.)
119 * 1995-01-23 20:58:51 MEZ 10402303260219950123195849740+40000500<CR><LF>
121 * Spectracom GPS and WWVB Receivers
123 * If a modem is connected to a Spectracom receiver, this driver will
124 * call it up and retrieve the time in one of two formats. As this
125 * driver does not send anything, the radio will have to either be
126 * configured in continuous mode or be polled by another local driver.
129 * Interface definitions
131 #define DEVICE "/dev/acts%d" /* device name and unit */
132 #define SPEED232 B9600 /* uart speed (9600 baud) */
133 #define PRECISION (-10) /* precision assumed (about 1 ms) */
134 #define LOCKFILE "/var/spool/locks/LCK..cua%d"
135 #define DESCRIPTION "Automated Computer Time Service" /* WRU */
136 #define REFID "NONE" /* default reference ID */
137 #define MSGCNT 20 /* max message count */
138 #define SMAX 256 /* max clockstats line length */
141 * Calling program modes
143 #define MODE_AUTO 0 /* automatic mode */
144 #define MODE_BACKUP 1 /* backup mode */
145 #define MODE_MANUAL 2 /* manual mode */
148 * Service identifiers.
150 #define REFACTS "NIST" /* NIST reference ID */
151 #define LENACTS 50 /* NIST format */
152 #define REFUSNO "USNO" /* USNO reference ID */
153 #define LENUSNO 20 /* USNO */
154 #define REFPTB "PTB\0" /* PTB/NPL reference ID */
155 #define LENPTB 78 /* PTB/NPL format */
156 #define REFWWVB "WWVB" /* WWVB reference ID */
157 #define LENWWVB0 22 /* WWVB format 0 */
158 #define LENWWVB2 24 /* WWVB format 2 */
159 #define LF 0x0a /* ASCII LF */
162 * Modem setup strings. These may have to be changed for some modems.
166 * &C0 disable carrier detect
167 * &D2 hang up and return to command mode on DTR transition
168 * E0 modem command echo disabled
169 * l1 set modem speaker volume to low level
170 * M1 speaker enabled until carrier detect
171 * Q0 return result codes
172 * V1 return result codes as English words
174 #define MODEM_SETUP "ATB1&C0&D2E0L1M1Q0V1\r" /* modem setup */
175 #define MODEM_HANGUP "ATH\r" /* modem disconnect */
178 * Timeouts (all in seconds)
180 #define SETUP 3 /* setup timeout */
181 #define DTR 1 /* DTR timeout */
182 #define ANSWER 60 /* answer timeout */
183 #define CONNECT 20 /* first valid message timeout */
184 #define TIMECODE 30 /* all valid messages timeout */
187 * State machine codes
189 #define S_IDLE 0 /* wait for poll */
190 #define S_OK 1 /* wait for modem setup */
191 #define S_DTR 2 /* wait for modem DTR */
192 #define S_CONNECT 3 /* wait for answer*/
193 #define S_FIRST 4 /* wait for first valid message */
194 #define S_MSG 5 /* wait for all messages */
195 #define S_CLOSE 6 /* wait after sending disconnect */
198 * Unit control structure
201 int unit; /* unit number */
202 int state; /* the first one was Delaware */
203 int timer; /* timeout counter */
204 int retry; /* retry index */
205 int msgcnt; /* count of messages received */
206 l_fp tstamp; /* on-time timestamp */
207 char *bufptr; /* buffer pointer */
211 * Function prototypes
213 static int acts_start P((int, struct peer *));
214 static void acts_shutdown P((int, struct peer *));
215 static void acts_receive P((struct recvbuf *));
216 static void acts_message P((struct peer *));
217 static void acts_timecode P((struct peer *, char *));
218 static void acts_poll P((int, struct peer *));
219 static void acts_timeout P((struct peer *));
220 static void acts_disc P((struct peer *));
221 static void acts_timer P((int, struct peer *));
224 * Transfer vector (conditional structure name)
226 struct refclock refclock_acts = {
227 acts_start, /* start up driver */
228 acts_shutdown, /* shut down driver */
229 acts_poll, /* transmit poll message */
230 noentry, /* not used */
231 noentry, /* not used */
232 noentry, /* not used */
233 acts_timer /* housekeeping timer */
236 struct refclock refclock_ptb;
239 * Initialize data for processing
248 struct refclockproc *pp;
251 * Allocate and initialize unit structure
253 up = emalloc(sizeof(struct actsunit));
257 memset(up, 0, sizeof(struct actsunit));
260 pp->unitptr = (caddr_t)up;
261 pp->io.clock_recv = acts_receive;
262 pp->io.srcclock = (caddr_t)peer;
266 * Initialize miscellaneous variables
268 peer->precision = PRECISION;
269 pp->clockdesc = DESCRIPTION;
270 memcpy((char *)&pp->refid, REFID, 4);
271 peer->sstclktype = CTL_SST_TS_TELEPHONE;
272 peer->flags &= ~FLAG_FIXPOLL;
273 up->bufptr = pp->a_lastcode;
279 * acts_shutdown - shut down the clock
288 struct refclockproc *pp;
291 * Warning: do this only when a call is not in progress.
294 up = (struct actsunit *)pp->unitptr;
300 * acts_receive - receive data from the serial interface
304 struct recvbuf *rbufp
308 struct refclockproc *pp;
314 * Initialize pointers and read the timecode and timestamp. Note
315 * we are in raw mode and victim of whatever the terminal
316 * interface kicks up; so, we have to reassemble messages from
317 * arbitrary fragments. Capture the timecode at the beginning of
318 * the message and at the '*' and '#' on-time characters.
320 peer = (struct peer *)rbufp->recv_srcclock;
322 up = (struct actsunit *)pp->unitptr;
323 pp->lencode = refclock_gtraw(rbufp, tbuf, BMAX - (up->bufptr -
324 pp->a_lastcode), &pp->lastrec);
325 for (tptr = tbuf; *tptr != '\0'; tptr++) {
327 if (up->bufptr == pp->a_lastcode) {
328 up->tstamp = pp->lastrec;
334 up->bufptr = pp->a_lastcode;
336 } else if (!iscntrl(*tptr)) {
337 *up->bufptr++ = *tptr;
338 if (*tptr == '*' || *tptr == '#') {
339 up->tstamp = pp->lastrec;
340 write(pp->io.fd, tptr, 1);
348 * acts_message - process message
356 struct refclockproc *pp;
364 * What to do depends on the state and the first token in the
365 * message. A NO token sends the message to the clockstats.
368 up = (struct actsunit *)pp->unitptr;
370 ioctl(pp->io.fd, TIOCMGET, (char *)&modem);
371 sprintf(tbuf, "acts: %04x (%d %d) %lu %s", modem, up->state,
372 up->timer, strlen(pp->a_lastcode), pp->a_lastcode);
374 printf("%s\n", tbuf);
376 strncpy(tbuf, pp->a_lastcode, SMAX);
378 if (strcmp(tbuf, "NO") == 0)
379 record_clock_stats(&peer->srcadr, pp->a_lastcode);
383 * We are waiting for the OK response to the modem setup
384 * command. When this happens, raise DTR and dial the number
388 if (strcmp(tbuf, "OK") != 0) {
389 msyslog(LOG_ERR, "acts: setup error %s",
394 ioctl(pp->io.fd, TIOCMBIS, (char *)&dtr);
400 * We are waiting for the call to be answered. All we care about
401 * here is token CONNECT. Send the message to the clockstats.
404 record_clock_stats(&peer->srcadr, pp->a_lastcode);
405 if (strcmp(tbuf, "CONNECT") != 0) {
414 * We are waiting for a timecode. Pass it to the parser.
418 acts_timecode(peer, pp->a_lastcode);
424 * acts_timecode - identify the service and parse the timecode message
428 struct peer *peer, /* peer structure pointer */
429 char *str /* timecode string */
433 struct refclockproc *pp;
434 int day; /* day of the month */
435 int month; /* month of the year */
436 u_long mjd; /* Modified Julian Day */
437 double dut1; /* DUT adjustment */
439 u_int dst; /* ACTS daylight/standard time */
440 u_int leap; /* ACTS leap indicator */
441 double msADV; /* ACTS transmit advance (ms) */
442 char utc[10]; /* ACTS timescale */
443 char flag; /* ACTS on-time character (* or #) */
445 char synchar; /* WWVB synchronized indicator */
446 char qualchar; /* WWVB quality indicator */
447 char leapchar; /* WWVB leap indicator */
448 char dstchar; /* WWVB daylight/savings indicator */
449 int tz; /* WWVB timezone */
451 u_int leapmonth; /* PTB/NPL month of leap */
452 char leapdir; /* PTB/NPL leap direction */
455 * The parser selects the modem format based on the message
456 * length. Since the data are checked carefully, occasional
457 * errors due noise are forgivable.
460 up = (struct actsunit *)pp->unitptr;
462 switch(strlen(str)) {
465 * For USNO format on-time character '*', which is on a line by
466 * itself. Be sure a timecode has been received.
469 if (*str == '*' && up->msgcnt > 0)
475 * ACTS format: "jjjjj yy-mm-dd hh:mm:ss ds l uuu aaaaa
480 "%5ld %2d-%2d-%2d %2d:%2d:%2d %2d %1d %3lf %5lf %9s %c",
481 &mjd, &pp->year, &month, &day, &pp->hour,
482 &pp->minute, &pp->second, &dst, &leap, &dut1,
483 &msADV, utc, &flag) != 13) {
484 refclock_report(peer, CEVNT_BADREPLY);
489 * Wait until ACTS has calculated the roundtrip delay.
490 * We don't need to do anything, as ACTS adjusts the
496 pp->day = ymd2yd(pp->year, month, day);
497 pp->leap = LEAP_NOWARNING;
499 pp->leap = LEAP_ADDSECOND;
500 else if (pp->leap == 2)
501 pp->leap = LEAP_DELSECOND;
502 memcpy(&pp->refid, REFACTS, 4);
504 record_clock_stats(&peer->srcadr, str);
509 * USNO format: "jjjjj nnn hhmmss UTC"
512 if (sscanf(str, "%5ld %3d %2d%2d%2d %3s",
513 &mjd, &pp->day, &pp->hour, &pp->minute,
514 &pp->second, utc) != 6) {
515 refclock_report(peer, CEVNT_BADREPLY);
520 * Wait for the on-time character, which follows in a
521 * separate message. There is no provision for leap
524 pp->leap = LEAP_NOWARNING;
525 memcpy(&pp->refid, REFUSNO, 4);
527 record_clock_stats(&peer->srcadr, str);
532 * PTB/NPL format: "yyyy-mm-dd hh:mm:ss MEZ"
536 "%*4d-%*2d-%*2d %*2d:%*2d:%2d %*5c%*12c%4d%2d%2d%2d%2d%5ld%2lf%c%2d%3lf%*15c%c",
537 &pp->second, &pp->year, &month, &day, &pp->hour,
538 &pp->minute, &mjd, &dut1, &leapdir, &leapmonth,
539 &msADV, &flag) != 12) {
540 refclock_report(peer, CEVNT_BADREPLY);
543 pp->leap = LEAP_NOWARNING;
544 if (leapmonth == month) {
546 pp->leap = LEAP_ADDSECOND;
547 else if (leapdir == '-')
548 pp->leap = LEAP_DELSECOND;
550 pp->day = ymd2yd(pp->year, month, day);
551 memcpy(&pp->refid, REFPTB, 4);
553 record_clock_stats(&peer->srcadr, str);
559 * WWVB format 0: "I ddd hh:mm:ss DTZ=nn"
562 if (sscanf(str, "%c %3d %2d:%2d:%2d %cTZ=%2d",
563 &synchar, &pp->day, &pp->hour, &pp->minute,
564 &pp->second, &dstchar, &tz) != 7) {
565 refclock_report(peer, CEVNT_BADREPLY);
568 pp->leap = LEAP_NOWARNING;
570 pp->leap = LEAP_NOTINSYNC;
571 memcpy(&pp->refid, REFWWVB, 4);
573 record_clock_stats(&peer->srcadr, str);
578 * WWVB format 2: "IQyy ddd hh:mm:ss.mmm LD"
581 if (sscanf(str, "%c%c%2d %3d %2d:%2d:%2d.%3ld%c%c%c",
582 &synchar, &qualchar, &pp->year, &pp->day,
583 &pp->hour, &pp->minute, &pp->second, &pp->nsec,
584 &dstchar, &leapchar, &dstchar) != 11) {
585 refclock_report(peer, CEVNT_BADREPLY);
589 pp->leap = LEAP_NOWARNING;
591 pp->leap = LEAP_NOTINSYNC;
592 else if (leapchar == 'L')
593 pp->leap = LEAP_ADDSECOND;
594 memcpy(&pp->refid, REFWWVB, 4);
596 record_clock_stats(&peer->srcadr, str);
601 * None of the above. Just forget about it and wait for the next
602 * message or timeout.
609 * We have a valid timecode. The fudge time1 value is added to
610 * each sample by the main line routines. Note that in current
611 * telephone networks the propatation time can be different for
612 * each call and can reach 200 ms for some calls.
614 peer->refid = pp->refid;
615 pp->lastrec = up->tstamp;
616 if (!refclock_process(pp)) {
617 refclock_report(peer, CEVNT_BADTIME);
620 pp->lastref = pp->lastrec;
621 if (peer->disp > MAXDISTANCE)
622 refclock_receive(peer);
623 if (up->state != S_MSG) {
625 up->timer = TIMECODE;
631 * acts_poll - called by the transmit routine
640 struct refclockproc *pp;
643 * This routine is called at every system poll. All it does is
644 * set flag1 under certain conditions. The real work is done by
645 * the timeout routine and state machine.
648 up = (struct actsunit *)pp->unitptr;
652 * In manual mode the calling program is activated by the ntpdc
653 * program using the enable flag (fudge flag1), either manually
661 * In automatic mode the calling program runs continuously at
662 * intervals determined by the poll event or specified timeout.
665 pp->sloppyclockflag |= CLK_FLAG1;
669 * In backup mode the calling program runs continuously as long
670 * as either no peers are available or this peer is selected.
673 if (sys_peer == NULL || sys_peer == peer)
674 pp->sloppyclockflag |= CLK_FLAG1;
681 * acts_timer - called at one-second intervals
690 struct refclockproc *pp;
693 * This routine implments a timeout which runs for a programmed
694 * interval. The counter is initialized by the state machine and
695 * counts down to zero. Upon reaching zero, the state machine is
696 * called. If flag1 is set while in S_IDLE state, force a
700 up = (struct actsunit *)pp->unitptr;
701 if (pp->sloppyclockflag & CLK_FLAG1 && up->state == S_IDLE) {
715 * acts_timeout - called on timeout
723 struct refclockproc *pp;
726 char lockfile[128], pidbuf[8];
730 * The state machine is driven by messages from the modem, when
731 * first stated and at timeout.
734 up = (struct actsunit *)pp->unitptr;
735 pp->sloppyclockflag &= ~CLK_FLAG1;
736 if (sys_phone[up->retry] == NULL && !(pp->sloppyclockflag &
738 msyslog(LOG_ERR, "acts: no phones");
744 * System poll event. Lock the modem port and open the device.
749 * Lock the modem port. If busy, retry later. Note: if
750 * something fails between here and the close, the lock
751 * file may not be removed.
753 if (pp->sloppyclockflag & CLK_FLAG2) {
754 sprintf(lockfile, LOCKFILE, up->unit);
755 fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL,
758 msyslog(LOG_ERR, "acts: port busy");
761 sprintf(pidbuf, "%d\n", (u_int)getpid());
762 write(fd, pidbuf, strlen(pidbuf));
767 * Open the device in raw mode and link the I/O.
770 sprintf(device, DEVICE, up->unit);
771 fd = refclock_open(device, SPEED232,
772 LDISC_ACTS | LDISC_RAW | LDISC_REMOTE);
777 if (!io_addclock(&pp->io)) {
779 "acts: addclock fails");
787 * If the port is directly connected to the device, skip
788 * the modem business and send 'T' for Spectrabum.
790 if (pp->sloppyclockflag & CLK_FLAG3) {
791 if (write(pp->io.fd, "T", 1) < 0) {
792 msyslog(LOG_ERR, "acts: write %m");
801 * Initialize the modem. This works with Hayes commands.
805 printf("acts: setup %s\n", MODEM_SETUP);
807 if (write(pp->io.fd, MODEM_SETUP, strlen(MODEM_SETUP)) <
809 msyslog(LOG_ERR, "acts: write %m");
817 * In OK state the modem did not respond to setup.
820 msyslog(LOG_ERR, "acts: no modem");
824 * In DTR state we are waiting for the modem to settle down
825 * before hammering it with a dial command.
828 sprintf(tbuf, "DIAL #%d %s", up->retry,
829 sys_phone[up->retry]);
830 record_clock_stats(&peer->srcadr, tbuf);
833 printf("%s\n", tbuf);
835 write(pp->io.fd, sys_phone[up->retry],
836 strlen(sys_phone[up->retry]));
837 write(pp->io.fd, "\r", 1);
838 up->state = S_CONNECT;
843 * In CONNECT state the call did not complete.
846 msyslog(LOG_ERR, "acts: no answer");
850 * In FIRST state no messages were received.
853 msyslog(LOG_ERR, "acts: no messages");
857 * In CLOSE state hangup is complete. Close the doors and
858 * windows and get some air.
863 * Close the device and unlock a shared modem.
866 io_closeclock(&pp->io);
868 if (pp->sloppyclockflag & CLK_FLAG2) {
869 sprintf(lockfile, LOCKFILE, up->unit);
876 * If messages were received, fold the tent and wait for
877 * the next poll. If no messages and there are more
878 * numbers to dial, retry after a short wait.
880 up->bufptr = pp->a_lastcode;
883 if ( up->msgcnt == 0) {
885 if (sys_phone[up->retry] == NULL)
900 * acts_disc - disconnect the call and clean the place up.
908 struct refclockproc *pp;
912 * We get here if the call terminated successfully or if an
913 * error occured. If the median filter has something in it,feed
914 * the data to the clock filter. If a modem port, drop DTR to
915 * force command mode and send modem hangup.
918 up = (struct actsunit *)pp->unitptr;
920 refclock_receive(peer);
921 if (!(pp->sloppyclockflag & CLK_FLAG3)) {
922 ioctl(pp->io.fd, TIOCMBIC, (char *)&dtr);
923 write(pp->io.fd, MODEM_HANGUP, strlen(MODEM_HANGUP));
930 int refclock_acts_bs;
931 #endif /* REFCLOCK */