1 /*        $NetBSD: refclock_datum.c,v 1.8 2020/05/25 20:47:25 christos Exp $    */
2 
3 /*
4 ** refclock_datum - clock driver for the Datum Programmable Time Server
5 **
6 ** Important note: This driver assumes that you have termios. If you have
7 ** a system that does not have termios, you will have to modify this driver.
8 **
9 ** Sorry, I have only tested this driver on SUN and HP platforms.
10 */
11 
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
15 
16 #include "ntp_types.h"
17 
18 #if defined(REFCLOCK) && defined(CLOCK_DATUM)
19 
20 /*
21 ** Include Files
22 */
23 
24 #include "ntpd.h"
25 #include "ntp_io.h"
26 #include "ntp_tty.h"
27 #include "ntp_refclock.h"
28 #include "timevalops.h"
29 #include "ntp_stdlib.h"
30 
31 #include <stdio.h>
32 #include <ctype.h>
33 
34 #if defined(STREAM)
35 #include <stropts.h>
36 #endif /* STREAM */
37 
38 #include "ntp_stdlib.h"
39 
40 /*
41 ** This driver supports the Datum Programmable Time System (PTS) clock.
42 ** The clock works in very straight forward manner. When it receives a
43 ** time code request (e.g., the ascii string "//k/mn"), it responds with
44 ** a seven byte BCD time code. This clock only responds with a
45 ** time code after it first receives the "//k/mn" message. It does not
46 ** periodically send time codes back at some rate once it is started.
47 ** the returned time code can be broken down into the following fields.
48 **
49 **            _______________________________
50 ** Bit Index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
51 **            ===============================
52 ** byte 0:   | -   -   -   - |      H D      |
53 **            ===============================
54 ** byte 1:   |      T D      |      U D      |
55 **            ===============================
56 ** byte 2:   | -   - |  T H  |      U H      |
57 **            ===============================
58 ** byte 3:   | - |    T M    |      U M      |
59 **            ===============================
60 ** byte 4:   | - |    T S    |      U S      |
61 **            ===============================
62 ** byte 5:   |      t S      |      h S      |
63 **            ===============================
64 ** byte 6:   |      m S      | -   -   -   - |
65 **            ===============================
66 **
67 ** In the table above:
68 **
69 **        "-" means don't care
70 **        "H D", "T D", and "U D" means Hundreds, Tens, and Units of Days
71 **        "T H", and "UH" means Tens and Units of Hours
72 **        "T M", and "U M" means Tens and Units of Minutes
73 **        "T S", and "U S" means Tens and Units of Seconds
74 **        "t S", "h S", and "m S" means tenths, hundredths, and thousandths
75 **                                      of seconds
76 **
77 ** The Datum PTS communicates throught the RS232 port on your machine.
78 ** Right now, it assumes that you have termios. This driver has been tested
79 ** on SUN and HP workstations. The Datum PTS supports various IRIG and
80 ** NASA input codes. This driver assumes that the name of the device is
81 ** /dev/datum. You will need to make a soft link to your RS232 device or
82 ** create a new driver to use this refclock.
83 */
84 
85 /*
86 ** Datum PTS defines
87 */
88 
89 /*
90 ** Note that if GMT is defined, then the Datum PTS must use Greenwich
91 ** time. Otherwise, this driver allows the Datum PTS to use the current
92 ** wall clock for its time. It determines the time zone offset by minimizing
93 ** the error after trying several time zone offsets. If the Datum PTS
94 ** time is Greenwich time and GMT is not defined, everything should still
95 ** work since the time zone will be found to be 0. What this really means
96 ** is that your system time (at least to start with) must be within the
97 ** correct time by less than +- 30 minutes. The default is for GMT to not
98 ** defined. If you really want to force GMT without the funny +- 30 minute
99 ** stuff then you must define (uncomment) GMT below.
100 */
101 
102 /*
103 #define GMT
104 #define DEBUG_DATUM_PTC
105 #define LOG_TIME_ERRORS
106 */
107 
108 
109 #define   PRECISION (-10)               /* precision assumed 1/1024 ms */
110 #define   REFID "DATM"                            /* reference id */
111 #define DATUM_DISPERSION 0              /* fixed dispersion = 0 ms */
112 #define DATUM_MAX_ERROR 0.100           /* limits on sigma squared */
113 #define DATUM_DEV   "/dev/datum"        /* device name */
114 
115 #define DATUM_MAX_ERROR2 (DATUM_MAX_ERROR*DATUM_MAX_ERROR)
116 
117 /*
118 ** The Datum PTS structure
119 */
120 
121 /*
122 ** I don't use a fixed array of MAXUNITS like everyone else just because
123 ** I don't like to program that way. Sorry if this bothers anyone. I assume
124 ** that you can use any id for your unit and I will search for it in a
125 ** dynamic array of units until I find it. I was worried that users might
126 ** enter a bad id in their configuration file (larger than MAXUNITS) and
127 ** besides, it is just cleaner not to have to assume that you have a fixed
128 ** number of anything in a program.
129 */
130 
131 struct datum_pts_unit {
132           struct peer *peer;            /* peer used by ntp */
133           int PTS_fd;                             /* file descriptor for PTS */
134           u_int unit;                             /* id for unit */
135           u_long timestarted;           /* time started */
136           l_fp lastrec;                           /* time tag for the receive time (system) */
137           l_fp lastref;                           /* reference time (Datum time) */
138           u_long yearstart;             /* the year that this clock started */
139           int coderecv;                           /* number of time codes received */
140           int day;                      /* day */
141           int hour;                     /* hour */
142           int minute;                             /* minutes */
143           int second;                             /* seconds */
144           int msec;                     /* miliseconds */
145           int usec;                     /* miliseconds */
146           u_char leap;                            /* funny leap character code */
147           char retbuf[8];               /* returned time from the datum pts */
148           char nbytes;                            /* number of bytes received from datum pts */
149           double sigma2;                /* average squared error (roughly) */
150           int tzoff;                              /* time zone offest from GMT */
151 };
152 
153 /*
154 ** PTS static constant variables for internal use
155 */
156 
157 static char TIME_REQUEST[6];  /* request message sent to datum for time */
158 static int nunits;            /* number of active units */
159 
160 /*
161 ** Callback function prototypes that ntpd needs to know about.
162 */
163 
164 static    int       datum_pts_start               (int, struct peer *);
165 static    void      datum_pts_shutdown  (int, struct peer *);
166 static    void      datum_pts_poll                (int, struct peer *);
167 static    void      datum_pts_control   (int, const struct refclockstat *,
168                                                    struct refclockstat *, struct peer *);
169 static    void      datum_pts_init                (void);
170 static    void      datum_pts_buginfo   (int, struct refclockbug *, struct peer *);
171 
172 /*
173 ** This is the call back function structure that ntpd actually uses for
174 ** this refclock.
175 */
176 
177 struct    refclock refclock_datum = {
178           datum_pts_start,              /* start up a new Datum refclock */
179           datum_pts_shutdown,           /* shutdown a Datum refclock */
180           datum_pts_poll,               /* sends out the time request */
181           datum_pts_control,            /* not used */
182           datum_pts_init,               /* initialization (called first) */
183           datum_pts_buginfo,            /* not used */
184           NOFLAGS                       /* we are not setting any special flags */
185 };
186 
187 /*
188 ** The datum_pts_receive callback function is handled differently from the
189 ** rest. It is passed to the ntpd io data structure. Basically, every
190 ** 64 seconds, the datum_pts_poll() routine is called. It sends out the time
191 ** request message to the Datum Programmable Time System. Then, ntpd
192 ** waits on a select() call to receive data back. The datum_pts_receive()
193 ** function is called as data comes back. We expect a seven byte time
194 ** code to be returned but the datum_pts_receive() function may only get
195 ** a few bytes passed to it at a time. In other words, this routine may
196 ** get called by the io stuff in ntpd a few times before we get all seven
197 ** bytes. Once the last byte is received, we process it and then pass the
198 ** new time measurement to ntpd for updating the system time. For now,
199 ** there is no 3 state filtering done on the time measurements. The
200 ** jitter may be a little high but at least for its current use, it is not
201 ** a problem. We have tried to keep things as simple as possible. This
202 ** clock should not jitter more than 1 or 2 mseconds at the most once
203 ** things settle down. It is important to get the right drift calibrated
204 ** in the ntpd.drift file as well as getting the right tick set up right
205 ** using tickadj for SUNs. Tickadj is not used for the HP but you need to
206 ** remember to bring up the adjtime daemon because HP does not support
207 ** the adjtime() call.
208 */
209 
210 static    void      datum_pts_receive   (struct recvbuf *);
211 
212 /*......................................................................*/
213 /*        datum_pts_start - start up the datum PTS. This means open the         */
214 /*        RS232 device and set up the data structure for my unit.               */
215 /*......................................................................*/
216 
217 static int
datum_pts_start(int unit,struct peer * peer)218 datum_pts_start(
219           int unit,
220           struct peer *peer
221           )
222 {
223           struct refclockproc *pp;
224           struct datum_pts_unit *datum_pts;
225           int fd;
226 #ifdef HAVE_TERMIOS
227           int rc;
228           struct termios arg;
229 #endif
230 
231 #ifdef DEBUG_DATUM_PTC
232           if (debug)
233               printf("Starting Datum PTS unit %d\n", unit);
234 #endif
235 
236           /*
237           ** Open the Datum PTS device
238           */
239           fd = open(DATUM_DEV, O_RDWR);
240 
241           if (fd < 0) {
242                     msyslog(LOG_ERR, "Datum_PTS: open(\"%s\", O_RDWR) failed: %m", DATUM_DEV);
243                     return 0;
244           }
245 
246           /*
247           ** Create the memory for the new unit
248           */
249           datum_pts = emalloc_zero(sizeof(*datum_pts));
250           datum_pts->unit = unit;       /* set my unit id */
251           datum_pts->yearstart = 0;     /* initialize the yearstart to 0 */
252           datum_pts->sigma2 = 0.0;      /* initialize the sigma2 to 0 */
253 
254           datum_pts->PTS_fd = fd;
255 
256           if (-1 == fcntl(datum_pts->PTS_fd, F_SETFL, 0)) /* clear the descriptor flags */
257                     msyslog(LOG_ERR, "MSF_ARCRON(%d): fcntl(F_SETFL, 0): %m.",
258                               unit);
259 
260 #ifdef DEBUG_DATUM_PTC
261           if (debug)
262               printf("Opening RS232 port with file descriptor %d\n",
263                        datum_pts->PTS_fd);
264 #endif
265 
266           /*
267           ** Set up the RS232 terminal device information. Note that we assume that
268           ** we have termios. This code has only been tested on SUNs and HPs. If your
269           ** machine does not have termios this driver cannot be initialized. You can change this
270           ** if you want by editing this source. Please give the changes back to the
271           ** ntp folks so that it can become part of their regular distribution.
272           */
273 
274           memset(&arg, 0, sizeof(arg));
275 
276           arg.c_iflag = IGNBRK;
277           arg.c_oflag = 0;
278           arg.c_cflag = B9600 | CS8 | CREAD | PARENB | CLOCAL;
279           arg.c_lflag = 0;
280           arg.c_cc[VMIN] = 0;           /* start timeout timer right away (not used) */
281           arg.c_cc[VTIME] = 30;                   /* 3 second timout on reads (not used) */
282 
283           rc = tcsetattr(datum_pts->PTS_fd, TCSANOW, &arg);
284           if (rc < 0) {
285                     msyslog(LOG_ERR, "Datum_PTS: tcsetattr(\"%s\") failed: %m", DATUM_DEV);
286                     close(datum_pts->PTS_fd);
287                     free(datum_pts);
288                     return 0;
289           }
290 
291           /*
292           ** Initialize the ntpd IO structure
293           */
294 
295           datum_pts->peer = peer;
296           pp = peer->procptr;
297           pp->io.clock_recv = datum_pts_receive;
298           pp->io.srcclock = peer;
299           pp->io.datalen = 0;
300           pp->io.fd = datum_pts->PTS_fd;
301 
302           if (!io_addclock(&pp->io)) {
303                     pp->io.fd = -1;
304 #ifdef DEBUG_DATUM_PTC
305                     if (debug)
306                         printf("Problem adding clock\n");
307 #endif
308 
309                     msyslog(LOG_ERR, "Datum_PTS: Problem adding clock");
310                     close(datum_pts->PTS_fd);
311                     free(datum_pts);
312 
313                     return 0;
314           }
315           peer->procptr->unitptr = datum_pts;
316 
317           /*
318           ** Now add one to the number of units and return a successful code
319           */
320 
321           nunits++;
322           return 1;
323 
324 }
325 
326 
327 /*......................................................................*/
328 /*        datum_pts_shutdown - this routine shuts doen the device and */
329 /*        removes the memory for the unit.                                      */
330 /*......................................................................*/
331 
332 static void
datum_pts_shutdown(int unit,struct peer * peer)333 datum_pts_shutdown(
334           int unit,
335           struct peer *peer
336           )
337 {
338           struct refclockproc *pp;
339           struct datum_pts_unit *datum_pts;
340 
341 #ifdef DEBUG_DATUM_PTC
342           if (debug)
343               printf("Shutdown Datum PTS\n");
344 #endif
345 
346           msyslog(LOG_ERR, "Datum_PTS: Shutdown Datum PTS");
347 
348           /*
349           ** We found the unit so close the file descriptor and free up the memory used
350           ** by the structure.
351           */
352           pp = peer->procptr;
353           datum_pts = pp->unitptr;
354           if (NULL != datum_pts) {
355                     io_closeclock(&pp->io);
356                     free(datum_pts);
357           }
358 }
359 
360 
361 /*......................................................................*/
362 /*        datum_pts_poll - this routine sends out the time request to the */
363 /*        Datum PTS device. The time will be passed back in the                 */
364 /*        datum_pts_receive() routine.                                          */
365 /*......................................................................*/
366 
367 static void
datum_pts_poll(int unit,struct peer * peer)368 datum_pts_poll(
369           int unit,
370           struct peer *peer
371           )
372 {
373           int error_code;
374           struct datum_pts_unit *datum_pts;
375 
376           datum_pts = peer->procptr->unitptr;
377 
378 #ifdef DEBUG_DATUM_PTC
379           if (debug)
380               printf("Poll Datum PTS\n");
381 #endif
382 
383           /*
384           ** Find the right unit and send out a time request once it is found.
385           */
386           error_code = write(datum_pts->PTS_fd, TIME_REQUEST, 6);
387           if (error_code != 6)
388                     perror("TIME_REQUEST");
389           datum_pts->nbytes = 0;
390 }
391 
392 
393 /*......................................................................*/
394 /*        datum_pts_control - not used                                          */
395 /*......................................................................*/
396 
397 static void
datum_pts_control(int unit,const struct refclockstat * in,struct refclockstat * out,struct peer * peer)398 datum_pts_control(
399           int unit,
400           const struct refclockstat *in,
401           struct refclockstat *out,
402           struct peer *peer
403           )
404 {
405 
406 #ifdef DEBUG_DATUM_PTC
407           if (debug)
408               printf("Control Datum PTS\n");
409 #endif
410 
411 }
412 
413 
414 /*......................................................................*/
415 /*        datum_pts_init - initializes things for all possible Datum  */
416 /*        time code generators that might be used. In practice, this is         */
417 /*        only called once at the beginning before anything else is   */
418 /*        called.                                                                         */
419 /*......................................................................*/
420 
421 static void
datum_pts_init(void)422 datum_pts_init(void)
423 {
424 
425           /*                                                                                        */
426           /*...... open up the log file if we are debugging ......................*/
427           /*                                                                                        */
428 
429           /*
430           ** Open up the log file if we are debugging. For now, send data out to the
431           ** screen (stdout).
432           */
433 
434 #ifdef DEBUG_DATUM_PTC
435           if (debug)
436               printf("Init Datum PTS\n");
437 #endif
438 
439           /*
440           ** Initialize the time request command string. This is the only message
441           ** that we ever have to send to the Datum PTS (although others are defined).
442           */
443 
444           memcpy(TIME_REQUEST, "//k/mn",6);
445 
446           /*
447           ** Initialize the number of units to 0 and set the dynamic array of units to
448           ** NULL since there are no units defined yet.
449           */
450 
451           nunits = 0;
452 
453 }
454 
455 
456 /*......................................................................*/
457 /*        datum_pts_buginfo - not used                                          */
458 /*......................................................................*/
459 
460 static void
datum_pts_buginfo(int unit,register struct refclockbug * bug,register struct peer * peer)461 datum_pts_buginfo(
462           int unit,
463           register struct refclockbug *bug,
464           register struct peer *peer
465           )
466 {
467 
468 #ifdef DEBUG_DATUM_PTC
469           if (debug)
470               printf("Buginfo Datum PTS\n");
471 #endif
472 
473 }
474 
475 
476 /*......................................................................*/
477 /*        datum_pts_receive - receive the time buffer that was read in          */
478 /*        by the ntpd io handling routines. When 7 bytes have been    */
479 /*        received (it may take several tries before all 7 bytes are  */
480 /*        received), then the time code must be unpacked and sent to  */
481 /*        the ntpd clock_receive() routine which causes the systems   */
482 /*        clock to be updated (several layers down).                            */
483 /*......................................................................*/
484 
485 static void
datum_pts_receive(struct recvbuf * rbufp)486 datum_pts_receive(
487           struct recvbuf *rbufp
488           )
489 {
490           int i;
491           size_t nb;
492           l_fp tstmp;
493           struct peer *p;
494           struct datum_pts_unit *datum_pts;
495           char *dpt;
496           int dpend;
497           int tzoff;
498           int timerr;
499           double ftimerr, abserr;
500 #ifdef DEBUG_DATUM_PTC
501           double dispersion;
502 #endif
503           int goodtime;
504       /*double doffset;*/
505 
506           /*
507           ** Get the time code (maybe partial) message out of the rbufp buffer.
508           */
509 
510           p = rbufp->recv_peer;
511           datum_pts = p->procptr->unitptr;
512           dpt = (char *)&rbufp->recv_space;
513           dpend = rbufp->recv_length;
514 
515 #ifdef DEBUG_DATUM_PTC
516           if (debug)
517                     printf("Receive Datum PTS: %d bytes\n", dpend);
518 #endif
519 
520           /*                                                                                        */
521           /*...... save the ntp system time when the first byte is received ......*/
522           /*                                                                                        */
523 
524           /*
525           ** Save the ntp system time when the first byte is received. Note that
526           ** because it may take several calls to this routine before all seven
527           ** bytes of our return message are finally received by the io handlers in
528           ** ntpd, we really do want to use the time tag when the first byte is
529           ** received to reduce the jitter.
530           */
531 
532           nb = datum_pts->nbytes;
533           if (nb == 0) {
534                     datum_pts->lastrec = rbufp->recv_time;
535           }
536 
537           /*
538           ** Increment our count to the number of bytes received so far. Return if we
539           ** haven't gotten all seven bytes yet.
540           ** [Sec 3388] make sure we do not overrun the buffer.
541           ** TODO: what to do with excessive bytes, if we ever get them?
542           */
543           for (i=0; (i < dpend) && (nb < sizeof(datum_pts->retbuf)); i++, nb++) {
544                     datum_pts->retbuf[nb] = dpt[i];
545           }
546           datum_pts->nbytes = nb;
547 
548           if (nb < 7) {
549                     return;
550           }
551 
552           /*
553           ** Convert the seven bytes received in our time buffer to day, hour, minute,
554           ** second, and msecond values. The usec value is not used for anything
555           ** currently. It is just the fractional part of the time stored in units
556           ** of microseconds.
557           */
558 
559           datum_pts->day =    100*(datum_pts->retbuf[0] & 0x0f) +
560                     10*((datum_pts->retbuf[1] & 0xf0)>>4) +
561                     (datum_pts->retbuf[1] & 0x0f);
562 
563           datum_pts->hour =   10*((datum_pts->retbuf[2] & 0x30)>>4) +
564                     (datum_pts->retbuf[2] & 0x0f);
565 
566           datum_pts->minute = 10*((datum_pts->retbuf[3] & 0x70)>>4) +
567                     (datum_pts->retbuf[3] & 0x0f);
568 
569           datum_pts->second = 10*((datum_pts->retbuf[4] & 0x70)>>4) +
570                     (datum_pts->retbuf[4] & 0x0f);
571 
572           datum_pts->msec =   100*((datum_pts->retbuf[5] & 0xf0) >> 4) +
573                     10*(datum_pts->retbuf[5] & 0x0f) +
574                     ((datum_pts->retbuf[6] & 0xf0)>>4);
575 
576           datum_pts->usec =   1000*datum_pts->msec;
577 
578 #ifdef DEBUG_DATUM_PTC
579           if (debug)
580               printf("day %d, hour %d, minute %d, second %d, msec %d\n",
581                        datum_pts->day,
582                        datum_pts->hour,
583                        datum_pts->minute,
584                        datum_pts->second,
585                        datum_pts->msec);
586 #endif
587 
588           /*
589           ** Get the GMT time zone offset. Note that GMT should be zero if the Datum
590           ** reference time is using GMT as its time base. Otherwise we have to
591           ** determine the offset if the Datum PTS is using time of day as its time
592           ** base.
593           */
594 
595           goodtime = 0;                 /* We are not sure about the time and offset yet */
596 
597 #ifdef GMT
598 
599           /*
600           ** This is the case where the Datum PTS is using GMT so there is no time
601           ** zone offset.
602           */
603 
604           tzoff = 0;                    /* set time zone offset to 0 */
605 
606 #else
607 
608           /*
609           ** This is the case where the Datum PTS is using regular time of day for its
610           ** time so we must compute the time zone offset. The way we do it is kind of
611           ** funny but it works. We loop through different time zones (0 to 24) and
612           ** pick the one that gives the smallest error (+- one half hour). The time
613           ** zone offset is stored in the datum_pts structure for future use. Normally,
614           ** the clocktime() routine is only called once (unless the time zone offset
615           ** changes due to daylight savings) since the goodtime flag is set when a
616           ** good time is found (with a good offset). Note that even if the Datum
617           ** PTS is using GMT, this mechanism will still work since it should come up
618           ** with a value for tzoff = 0 (assuming that your system clock is within
619           ** a half hour of the Datum time (even with time zone differences).
620           */
621 
622           for (tzoff=0; tzoff<24; tzoff++) {
623                     if (clocktime( datum_pts->day,
624                                      datum_pts->hour,
625                                      datum_pts->minute,
626                                      datum_pts->second,
627                                      (tzoff + datum_pts->tzoff) % 24,
628                                      datum_pts->lastrec.l_ui,
629                                      &datum_pts->yearstart,
630                                      &datum_pts->lastref.l_ui) ) {
631 
632                               datum_pts->lastref.l_uf = 0;
633                               error = datum_pts->lastref.l_ui - datum_pts->lastrec.l_ui;
634 
635 #ifdef DEBUG_DATUM_PTC
636                               printf("Time Zone (clocktime method) = %d, error = %d\n", tzoff, error);
637 #endif
638 
639                               if ((error < 1799) && (error > -1799)) {
640                                         tzoff = (tzoff + datum_pts->tzoff) % 24;
641                                         datum_pts->tzoff = tzoff;
642                                         goodtime = 1;
643 
644 #ifdef DEBUG_DATUM_PTC
645                                         printf("Time Zone found (clocktime method) = %d\n",tzoff);
646 #endif
647 
648                                         break;
649                               }
650 
651                     }
652           }
653 
654 #endif
655 
656           /*
657           ** Make sure that we have a good time from the Datum PTS. Clocktime() also
658           ** sets yearstart and lastref.l_ui. We will have to set astref.l_uf (i.e.,
659           ** the fraction of a second) stuff later.
660           */
661 
662           if (!goodtime) {
663 
664                     if (!clocktime( datum_pts->day,
665                                         datum_pts->hour,
666                                         datum_pts->minute,
667                                         datum_pts->second,
668                                         tzoff,
669                                         datum_pts->lastrec.l_ui,
670                                         &datum_pts->yearstart,
671                                         &datum_pts->lastref.l_ui) ) {
672 
673 #ifdef DEBUG_DATUM_PTC
674                               if (debug)
675                               {
676                                         printf("Error: bad clocktime\n");
677                                         printf("GMT %d, lastrec %d, yearstart %d, lastref %d\n",
678                                                tzoff,
679                                                datum_pts->lastrec.l_ui,
680                                                datum_pts->yearstart,
681                                                datum_pts->lastref.l_ui);
682                               }
683 #endif
684 
685                               msyslog(LOG_ERR, "Datum_PTS: Bad clocktime");
686 
687                               return;
688 
689                     }else{
690 
691 #ifdef DEBUG_DATUM_PTC
692                               if (debug)
693                                   printf("Good clocktime\n");
694 #endif
695 
696                     }
697 
698           }
699 
700           /*
701           ** We have datum_pts->lastref.l_ui set (which is the integer part of the
702           ** time. Now set the microseconds field.
703           */
704 
705           TVUTOTSF(datum_pts->usec, datum_pts->lastref.l_uf);
706 
707           /*
708           ** Compute the time correction as the difference between the reference
709           ** time (i.e., the Datum time) minus the receive time (system time).
710           */
711 
712           tstmp = datum_pts->lastref;             /* tstmp is the datum ntp time */
713           L_SUB(&tstmp, &datum_pts->lastrec);     /* tstmp is now the correction */
714           datum_pts->coderecv++;                  /* increment a counter */
715 
716 #ifdef DEBUG_DATUM_PTC
717           dispersion = DATUM_DISPERSION;          /* set the dispersion to 0 */
718           ftimerr = dispersion;
719           ftimerr /= (1024.0 * 64.0);
720           if (debug)
721               printf("dispersion = %d, %f\n", dispersion, ftimerr);
722 #endif
723 
724           /*
725           ** Pass the new time to ntpd through the refclock_receive function. Note
726           ** that we are not trying to make any corrections due to the time it takes
727           ** for the Datum PTS to send the message back. I am (erroneously) assuming
728           ** that the time for the Datum PTS to send the time back to us is negligable.
729           ** I suspect that this time delay may be as much as 15 ms or so (but probably
730           ** less). For our needs at JPL, this kind of error is ok so it is not
731           ** necessary to use fudge factors in the ntp.conf file. Maybe later we will.
732           */
733       /*LFPTOD(&tstmp, doffset);*/
734           datum_pts->lastref = datum_pts->lastrec;
735           refclock_receive(datum_pts->peer);
736 
737           /*
738           ** Compute sigma squared (not used currently). Maybe later, this could be
739           ** used for the dispersion estimate. The problem is that ntpd does not link
740           ** in the math library so sqrt() is not available. Anyway, this is useful
741           ** for debugging. Maybe later I will just use absolute values for the time
742           ** error to come up with my dispersion estimate. Anyway, for now my dispersion
743           ** is set to 0.
744           */
745 
746           timerr = tstmp.l_ui<<20;
747           timerr |= (tstmp.l_uf>>12) & 0x000fffff;
748           ftimerr = timerr;
749           ftimerr /= 1024*1024;
750           abserr = ftimerr;
751           if (ftimerr < 0.0) abserr = -ftimerr;
752 
753           if (datum_pts->sigma2 == 0.0) {
754                     if (abserr < DATUM_MAX_ERROR) {
755                               datum_pts->sigma2 = abserr*abserr;
756                     }else{
757                               datum_pts->sigma2 = DATUM_MAX_ERROR2;
758                     }
759           }else{
760                     if (abserr < DATUM_MAX_ERROR) {
761                               datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*abserr*abserr;
762                     }else{
763                               datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*DATUM_MAX_ERROR2;
764                     }
765           }
766 
767 #ifdef DEBUG_DATUM_PTC
768           if (debug)
769               printf("Time error = %f seconds\n", ftimerr);
770 #endif
771 
772 #if defined(DEBUG_DATUM_PTC) || defined(LOG_TIME_ERRORS)
773           if (debug)
774               printf("PTS: day %d, hour %d, minute %d, second %d, msec %d, Time Error %f\n",
775                        datum_pts->day,
776                        datum_pts->hour,
777                        datum_pts->minute,
778                        datum_pts->second,
779                        datum_pts->msec,
780                        ftimerr);
781 #endif
782 
783 }
784 #else
785 NONEMPTY_TRANSLATION_UNIT
786 #endif /* REFCLOCK */
787