2 ** refclock_datum - clock driver for the Datum Programmable Time Server
4 ** Important note: This driver assumes that you have termios. If you have
5 ** a system that does not have termios, you will have to modify this driver.
7 ** Sorry, I have only tested this driver on SUN and HP platforms.
14 #include "ntp_types.h"
16 #if defined(REFCLOCK) && defined(CLOCK_DATUM)
25 #include "ntp_refclock.h"
26 #include "timevalops.h"
27 #include "ntp_stdlib.h"
36 #include "ntp_stdlib.h"
39 ** This driver supports the Datum Programmable Time System (PTS) clock.
40 ** The clock works in very straight forward manner. When it receives a
41 ** time code request (e.g., the ascii string "//k/mn"), it responds with
42 ** a seven byte BCD time code. This clock only responds with a
43 ** time code after it first receives the "//k/mn" message. It does not
44 ** periodically send time codes back at some rate once it is started.
45 ** the returned time code can be broken down into the following fields.
47 ** _______________________________
48 ** Bit Index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
49 ** ===============================
50 ** byte 0: | - - - - | H D |
51 ** ===============================
52 ** byte 1: | T D | U D |
53 ** ===============================
54 ** byte 2: | - - | T H | U H |
55 ** ===============================
56 ** byte 3: | - | T M | U M |
57 ** ===============================
58 ** byte 4: | - | T S | U S |
59 ** ===============================
60 ** byte 5: | t S | h S |
61 ** ===============================
62 ** byte 6: | m S | - - - - |
63 ** ===============================
65 ** In the table above:
67 ** "-" means don't care
68 ** "H D", "T D", and "U D" means Hundreds, Tens, and Units of Days
69 ** "T H", and "UH" means Tens and Units of Hours
70 ** "T M", and "U M" means Tens and Units of Minutes
71 ** "T S", and "U S" means Tens and Units of Seconds
72 ** "t S", "h S", and "m S" means tenths, hundredths, and thousandths
75 ** The Datum PTS communicates throught the RS232 port on your machine.
76 ** Right now, it assumes that you have termios. This driver has been tested
77 ** on SUN and HP workstations. The Datum PTS supports various IRIG and
78 ** NASA input codes. This driver assumes that the name of the device is
79 ** /dev/datum. You will need to make a soft link to your RS232 device or
80 ** create a new driver to use this refclock.
88 ** Note that if GMT is defined, then the Datum PTS must use Greenwich
89 ** time. Otherwise, this driver allows the Datum PTS to use the current
90 ** wall clock for its time. It determines the time zone offset by minimizing
91 ** the error after trying several time zone offsets. If the Datum PTS
92 ** time is Greenwich time and GMT is not defined, everything should still
93 ** work since the time zone will be found to be 0. What this really means
94 ** is that your system time (at least to start with) must be within the
95 ** correct time by less than +- 30 minutes. The default is for GMT to not
96 ** defined. If you really want to force GMT without the funny +- 30 minute
97 ** stuff then you must define (uncomment) GMT below.
102 #define DEBUG_DATUM_PTC
103 #define LOG_TIME_ERRORS
107 #define PRECISION (-10) /* precision assumed 1/1024 ms */
108 #define REFID "DATM" /* reference id */
109 #define DATUM_DISPERSION 0 /* fixed dispersion = 0 ms */
110 #define DATUM_MAX_ERROR 0.100 /* limits on sigma squared */
111 #define DATUM_DEV "/dev/datum" /* device name */
113 #define DATUM_MAX_ERROR2 (DATUM_MAX_ERROR*DATUM_MAX_ERROR)
116 ** The Datum PTS structure
120 ** I don't use a fixed array of MAXUNITS like everyone else just because
121 ** I don't like to program that way. Sorry if this bothers anyone. I assume
122 ** that you can use any id for your unit and I will search for it in a
123 ** dynamic array of units until I find it. I was worried that users might
124 ** enter a bad id in their configuration file (larger than MAXUNITS) and
125 ** besides, it is just cleaner not to have to assume that you have a fixed
126 ** number of anything in a program.
129 struct datum_pts_unit {
130 struct peer *peer; /* peer used by ntp */
131 int PTS_fd; /* file descriptor for PTS */
132 u_int unit; /* id for unit */
133 u_long timestarted; /* time started */
134 l_fp lastrec; /* time tag for the receive time (system) */
135 l_fp lastref; /* reference time (Datum time) */
136 u_long yearstart; /* the year that this clock started */
137 int coderecv; /* number of time codes received */
140 int minute; /* minutes */
141 int second; /* seconds */
142 int msec; /* miliseconds */
143 int usec; /* miliseconds */
144 u_char leap; /* funny leap character code */
145 char retbuf[8]; /* returned time from the datum pts */
146 char nbytes; /* number of bytes received from datum pts */
147 double sigma2; /* average squared error (roughly) */
148 int tzoff; /* time zone offest from GMT */
152 ** PTS static constant variables for internal use
155 static char TIME_REQUEST[6]; /* request message sent to datum for time */
156 static int nunits; /* number of active units */
159 ** Callback function prototypes that ntpd needs to know about.
162 static int datum_pts_start (int, struct peer *);
163 static void datum_pts_shutdown (int, struct peer *);
164 static void datum_pts_poll (int, struct peer *);
165 static void datum_pts_control (int, const struct refclockstat *,
166 struct refclockstat *, struct peer *);
167 static void datum_pts_init (void);
168 static void datum_pts_buginfo (int, struct refclockbug *, struct peer *);
171 ** This is the call back function structure that ntpd actually uses for
175 struct refclock refclock_datum = {
176 datum_pts_start, /* start up a new Datum refclock */
177 datum_pts_shutdown, /* shutdown a Datum refclock */
178 datum_pts_poll, /* sends out the time request */
179 datum_pts_control, /* not used */
180 datum_pts_init, /* initialization (called first) */
181 datum_pts_buginfo, /* not used */
182 NOFLAGS /* we are not setting any special flags */
186 ** The datum_pts_receive callback function is handled differently from the
187 ** rest. It is passed to the ntpd io data structure. Basically, every
188 ** 64 seconds, the datum_pts_poll() routine is called. It sends out the time
189 ** request message to the Datum Programmable Time System. Then, ntpd
190 ** waits on a select() call to receive data back. The datum_pts_receive()
191 ** function is called as data comes back. We expect a seven byte time
192 ** code to be returned but the datum_pts_receive() function may only get
193 ** a few bytes passed to it at a time. In other words, this routine may
194 ** get called by the io stuff in ntpd a few times before we get all seven
195 ** bytes. Once the last byte is received, we process it and then pass the
196 ** new time measurement to ntpd for updating the system time. For now,
197 ** there is no 3 state filtering done on the time measurements. The
198 ** jitter may be a little high but at least for its current use, it is not
199 ** a problem. We have tried to keep things as simple as possible. This
200 ** clock should not jitter more than 1 or 2 mseconds at the most once
201 ** things settle down. It is important to get the right drift calibrated
202 ** in the ntpd.drift file as well as getting the right tick set up right
203 ** using tickadj for SUNs. Tickadj is not used for the HP but you need to
204 ** remember to bring up the adjtime daemon because HP does not support
205 ** the adjtime() call.
208 static void datum_pts_receive (struct recvbuf *);
210 /*......................................................................*/
211 /* datum_pts_start - start up the datum PTS. This means open the */
212 /* RS232 device and set up the data structure for my unit. */
213 /*......................................................................*/
221 struct refclockproc *pp;
222 struct datum_pts_unit *datum_pts;
229 #ifdef DEBUG_DATUM_PTC
231 printf("Starting Datum PTS unit %d\n", unit);
235 ** Open the Datum PTS device
237 fd = open(DATUM_DEV, O_RDWR);
240 msyslog(LOG_ERR, "Datum_PTS: open(\"%s\", O_RDWR) failed: %m", DATUM_DEV);
245 ** Create the memory for the new unit
247 datum_pts = emalloc_zero(sizeof(*datum_pts));
248 datum_pts->unit = unit; /* set my unit id */
249 datum_pts->yearstart = 0; /* initialize the yearstart to 0 */
250 datum_pts->sigma2 = 0.0; /* initialize the sigma2 to 0 */
252 datum_pts->PTS_fd = fd;
254 if (-1 == fcntl(datum_pts->PTS_fd, F_SETFL, 0)) /* clear the descriptor flags */
255 msyslog(LOG_ERR, "MSF_ARCRON(%d): fcntl(F_SETFL, 0): %m.",
258 #ifdef DEBUG_DATUM_PTC
260 printf("Opening RS232 port with file descriptor %d\n",
265 ** Set up the RS232 terminal device information. Note that we assume that
266 ** we have termios. This code has only been tested on SUNs and HPs. If your
267 ** machine does not have termios this driver cannot be initialized. You can change this
268 ** if you want by editing this source. Please give the changes back to the
269 ** ntp folks so that it can become part of their regular distribution.
272 memset(&arg, 0, sizeof(arg));
274 arg.c_iflag = IGNBRK;
276 arg.c_cflag = B9600 | CS8 | CREAD | PARENB | CLOCAL;
278 arg.c_cc[VMIN] = 0; /* start timeout timer right away (not used) */
279 arg.c_cc[VTIME] = 30; /* 3 second timout on reads (not used) */
281 rc = tcsetattr(datum_pts->PTS_fd, TCSANOW, &arg);
283 msyslog(LOG_ERR, "Datum_PTS: tcsetattr(\"%s\") failed: %m", DATUM_DEV);
284 close(datum_pts->PTS_fd);
290 ** Initialize the ntpd IO structure
293 datum_pts->peer = peer;
295 pp->io.clock_recv = datum_pts_receive;
296 pp->io.srcclock = peer;
298 pp->io.fd = datum_pts->PTS_fd;
300 if (!io_addclock(&pp->io)) {
302 #ifdef DEBUG_DATUM_PTC
304 printf("Problem adding clock\n");
307 msyslog(LOG_ERR, "Datum_PTS: Problem adding clock");
308 close(datum_pts->PTS_fd);
313 peer->procptr->unitptr = datum_pts;
316 ** Now add one to the number of units and return a successful code
325 /*......................................................................*/
326 /* datum_pts_shutdown - this routine shuts doen the device and */
327 /* removes the memory for the unit. */
328 /*......................................................................*/
336 struct refclockproc *pp;
337 struct datum_pts_unit *datum_pts;
339 #ifdef DEBUG_DATUM_PTC
341 printf("Shutdown Datum PTS\n");
344 msyslog(LOG_ERR, "Datum_PTS: Shutdown Datum PTS");
347 ** We found the unit so close the file descriptor and free up the memory used
351 datum_pts = pp->unitptr;
352 if (NULL != datum_pts) {
353 io_closeclock(&pp->io);
359 /*......................................................................*/
360 /* datum_pts_poll - this routine sends out the time request to the */
361 /* Datum PTS device. The time will be passed back in the */
362 /* datum_pts_receive() routine. */
363 /*......................................................................*/
372 struct datum_pts_unit *datum_pts;
374 datum_pts = peer->procptr->unitptr;
376 #ifdef DEBUG_DATUM_PTC
378 printf("Poll Datum PTS\n");
382 ** Find the right unit and send out a time request once it is found.
384 error_code = write(datum_pts->PTS_fd, TIME_REQUEST, 6);
386 perror("TIME_REQUEST");
387 datum_pts->nbytes = 0;
391 /*......................................................................*/
392 /* datum_pts_control - not used */
393 /*......................................................................*/
398 const struct refclockstat *in,
399 struct refclockstat *out,
404 #ifdef DEBUG_DATUM_PTC
406 printf("Control Datum PTS\n");
412 /*......................................................................*/
413 /* datum_pts_init - initializes things for all possible Datum */
414 /* time code generators that might be used. In practice, this is */
415 /* only called once at the beginning before anything else is */
417 /*......................................................................*/
424 /*...... open up the log file if we are debugging ......................*/
428 ** Open up the log file if we are debugging. For now, send data out to the
432 #ifdef DEBUG_DATUM_PTC
434 printf("Init Datum PTS\n");
438 ** Initialize the time request command string. This is the only message
439 ** that we ever have to send to the Datum PTS (although others are defined).
442 memcpy(TIME_REQUEST, "//k/mn",6);
445 ** Initialize the number of units to 0 and set the dynamic array of units to
446 ** NULL since there are no units defined yet.
454 /*......................................................................*/
455 /* datum_pts_buginfo - not used */
456 /*......................................................................*/
461 register struct refclockbug *bug,
462 register struct peer *peer
466 #ifdef DEBUG_DATUM_PTC
468 printf("Buginfo Datum PTS\n");
474 /*......................................................................*/
475 /* datum_pts_receive - receive the time buffer that was read in */
476 /* by the ntpd io handling routines. When 7 bytes have been */
477 /* received (it may take several tries before all 7 bytes are */
478 /* received), then the time code must be unpacked and sent to */
479 /* the ntpd clock_receive() routine which causes the systems */
480 /* clock to be updated (several layers down). */
481 /*......................................................................*/
485 struct recvbuf *rbufp
492 struct datum_pts_unit *datum_pts;
497 double ftimerr, abserr;
498 #ifdef DEBUG_DATUM_PTC
505 ** Get the time code (maybe partial) message out of the rbufp buffer.
508 p = rbufp->recv_peer;
509 datum_pts = p->procptr->unitptr;
510 dpt = (char *)&rbufp->recv_space;
511 dpend = rbufp->recv_length;
513 #ifdef DEBUG_DATUM_PTC
515 printf("Receive Datum PTS: %d bytes\n", dpend);
519 /*...... save the ntp system time when the first byte is received ......*/
523 ** Save the ntp system time when the first byte is received. Note that
524 ** because it may take several calls to this routine before all seven
525 ** bytes of our return message are finally received by the io handlers in
526 ** ntpd, we really do want to use the time tag when the first byte is
527 ** received to reduce the jitter.
530 nb = datum_pts->nbytes;
532 datum_pts->lastrec = rbufp->recv_time;
536 ** Increment our count to the number of bytes received so far. Return if we
537 ** haven't gotten all seven bytes yet.
538 ** [Sec 3388] make sure we do not overrun the buffer.
539 ** TODO: what to do with excessive bytes, if we ever get them?
541 for (i=0; (i < dpend) && (nb < sizeof(datum_pts->retbuf)); i++, nb++) {
542 datum_pts->retbuf[nb] = dpt[i];
544 datum_pts->nbytes = nb;
551 ** Convert the seven bytes received in our time buffer to day, hour, minute,
552 ** second, and msecond values. The usec value is not used for anything
553 ** currently. It is just the fractional part of the time stored in units
557 datum_pts->day = 100*(datum_pts->retbuf[0] & 0x0f) +
558 10*((datum_pts->retbuf[1] & 0xf0)>>4) +
559 (datum_pts->retbuf[1] & 0x0f);
561 datum_pts->hour = 10*((datum_pts->retbuf[2] & 0x30)>>4) +
562 (datum_pts->retbuf[2] & 0x0f);
564 datum_pts->minute = 10*((datum_pts->retbuf[3] & 0x70)>>4) +
565 (datum_pts->retbuf[3] & 0x0f);
567 datum_pts->second = 10*((datum_pts->retbuf[4] & 0x70)>>4) +
568 (datum_pts->retbuf[4] & 0x0f);
570 datum_pts->msec = 100*((datum_pts->retbuf[5] & 0xf0) >> 4) +
571 10*(datum_pts->retbuf[5] & 0x0f) +
572 ((datum_pts->retbuf[6] & 0xf0)>>4);
574 datum_pts->usec = 1000*datum_pts->msec;
576 #ifdef DEBUG_DATUM_PTC
578 printf("day %d, hour %d, minute %d, second %d, msec %d\n",
587 ** Get the GMT time zone offset. Note that GMT should be zero if the Datum
588 ** reference time is using GMT as its time base. Otherwise we have to
589 ** determine the offset if the Datum PTS is using time of day as its time
593 goodtime = 0; /* We are not sure about the time and offset yet */
598 ** This is the case where the Datum PTS is using GMT so there is no time
602 tzoff = 0; /* set time zone offset to 0 */
607 ** This is the case where the Datum PTS is using regular time of day for its
608 ** time so we must compute the time zone offset. The way we do it is kind of
609 ** funny but it works. We loop through different time zones (0 to 24) and
610 ** pick the one that gives the smallest error (+- one half hour). The time
611 ** zone offset is stored in the datum_pts structure for future use. Normally,
612 ** the clocktime() routine is only called once (unless the time zone offset
613 ** changes due to daylight savings) since the goodtime flag is set when a
614 ** good time is found (with a good offset). Note that even if the Datum
615 ** PTS is using GMT, this mechanism will still work since it should come up
616 ** with a value for tzoff = 0 (assuming that your system clock is within
617 ** a half hour of the Datum time (even with time zone differences).
620 for (tzoff=0; tzoff<24; tzoff++) {
621 if (clocktime( datum_pts->day,
625 (tzoff + datum_pts->tzoff) % 24,
626 datum_pts->lastrec.l_ui,
627 &datum_pts->yearstart,
628 &datum_pts->lastref.l_ui) ) {
630 datum_pts->lastref.l_uf = 0;
631 error = datum_pts->lastref.l_ui - datum_pts->lastrec.l_ui;
633 #ifdef DEBUG_DATUM_PTC
634 printf("Time Zone (clocktime method) = %d, error = %d\n", tzoff, error);
637 if ((error < 1799) && (error > -1799)) {
638 tzoff = (tzoff + datum_pts->tzoff) % 24;
639 datum_pts->tzoff = tzoff;
642 #ifdef DEBUG_DATUM_PTC
643 printf("Time Zone found (clocktime method) = %d\n",tzoff);
655 ** Make sure that we have a good time from the Datum PTS. Clocktime() also
656 ** sets yearstart and lastref.l_ui. We will have to set astref.l_uf (i.e.,
657 ** the fraction of a second) stuff later.
662 if (!clocktime( datum_pts->day,
667 datum_pts->lastrec.l_ui,
668 &datum_pts->yearstart,
669 &datum_pts->lastref.l_ui) ) {
671 #ifdef DEBUG_DATUM_PTC
674 printf("Error: bad clocktime\n");
675 printf("GMT %d, lastrec %d, yearstart %d, lastref %d\n",
677 datum_pts->lastrec.l_ui,
678 datum_pts->yearstart,
679 datum_pts->lastref.l_ui);
683 msyslog(LOG_ERR, "Datum_PTS: Bad clocktime");
689 #ifdef DEBUG_DATUM_PTC
691 printf("Good clocktime\n");
699 ** We have datum_pts->lastref.l_ui set (which is the integer part of the
700 ** time. Now set the microseconds field.
703 TVUTOTSF(datum_pts->usec, datum_pts->lastref.l_uf);
706 ** Compute the time correction as the difference between the reference
707 ** time (i.e., the Datum time) minus the receive time (system time).
710 tstmp = datum_pts->lastref; /* tstmp is the datum ntp time */
711 L_SUB(&tstmp, &datum_pts->lastrec); /* tstmp is now the correction */
712 datum_pts->coderecv++; /* increment a counter */
714 #ifdef DEBUG_DATUM_PTC
715 dispersion = DATUM_DISPERSION; /* set the dispersion to 0 */
716 ftimerr = dispersion;
717 ftimerr /= (1024.0 * 64.0);
719 printf("dispersion = %d, %f\n", dispersion, ftimerr);
723 ** Pass the new time to ntpd through the refclock_receive function. Note
724 ** that we are not trying to make any corrections due to the time it takes
725 ** for the Datum PTS to send the message back. I am (erroneously) assuming
726 ** that the time for the Datum PTS to send the time back to us is negligable.
727 ** I suspect that this time delay may be as much as 15 ms or so (but probably
728 ** less). For our needs at JPL, this kind of error is ok so it is not
729 ** necessary to use fudge factors in the ntp.conf file. Maybe later we will.
731 /*LFPTOD(&tstmp, doffset);*/
732 datum_pts->lastref = datum_pts->lastrec;
733 refclock_receive(datum_pts->peer);
736 ** Compute sigma squared (not used currently). Maybe later, this could be
737 ** used for the dispersion estimate. The problem is that ntpd does not link
738 ** in the math library so sqrt() is not available. Anyway, this is useful
739 ** for debugging. Maybe later I will just use absolute values for the time
740 ** error to come up with my dispersion estimate. Anyway, for now my dispersion
744 timerr = tstmp.l_ui<<20;
745 timerr |= (tstmp.l_uf>>12) & 0x000fffff;
747 ftimerr /= 1024*1024;
749 if (ftimerr < 0.0) abserr = -ftimerr;
751 if (datum_pts->sigma2 == 0.0) {
752 if (abserr < DATUM_MAX_ERROR) {
753 datum_pts->sigma2 = abserr*abserr;
755 datum_pts->sigma2 = DATUM_MAX_ERROR2;
758 if (abserr < DATUM_MAX_ERROR) {
759 datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*abserr*abserr;
761 datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*DATUM_MAX_ERROR2;
765 #ifdef DEBUG_DATUM_PTC
767 printf("Time error = %f seconds\n", ftimerr);
770 #if defined(DEBUG_DATUM_PTC) || defined(LOG_TIME_ERRORS)
772 printf("PTS: day %d, hour %d, minute %d, second %d, msec %d, Time Error %f\n",
783 NONEMPTY_TRANSLATION_UNIT
784 #endif /* REFCLOCK */