Home | History | Annotate | Line # | Download | only in ntpd
refclock_datum.c revision 1.6
      1 /*	$NetBSD: refclock_datum.c,v 1.6 2016/01/08 21:35:39 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
    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
    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
    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
    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
    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
    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
    486 datum_pts_receive(
    487 	struct recvbuf *rbufp
    488 	)
    489 {
    490 	int i;
    491 	l_fp tstmp;
    492 	struct peer *p;
    493 	struct datum_pts_unit *datum_pts;
    494 	char *dpt;
    495 	int dpend;
    496 	int tzoff;
    497 	int timerr;
    498 	double ftimerr, abserr;
    499 #ifdef DEBUG_DATUM_PTC
    500 	double dispersion;
    501 #endif
    502 	int goodtime;
    503       /*double doffset;*/
    504 
    505 	/*
    506 	** Get the time code (maybe partial) message out of the rbufp buffer.
    507 	*/
    508 
    509 	p = rbufp->recv_peer;
    510 	datum_pts = p->procptr->unitptr;
    511 	dpt = (char *)&rbufp->recv_space;
    512 	dpend = rbufp->recv_length;
    513 
    514 #ifdef DEBUG_DATUM_PTC
    515 	if (debug)
    516 		printf("Receive Datum PTS: %d bytes\n", dpend);
    517 #endif
    518 
    519 	/*									*/
    520 	/*...... save the ntp system time when the first byte is received ......*/
    521 	/*									*/
    522 
    523 	/*
    524 	** Save the ntp system time when the first byte is received. Note that
    525 	** because it may take several calls to this routine before all seven
    526 	** bytes of our return message are finally received by the io handlers in
    527 	** ntpd, we really do want to use the time tag when the first byte is
    528 	** received to reduce the jitter.
    529 	*/
    530 
    531 	if (datum_pts->nbytes == 0) {
    532 		datum_pts->lastrec = rbufp->recv_time;
    533 	}
    534 
    535 	/*
    536 	** Increment our count to the number of bytes received so far. Return if we
    537 	** haven't gotten all seven bytes yet.
    538 	*/
    539 
    540 	for (i=0; i<dpend; i++) {
    541 		datum_pts->retbuf[datum_pts->nbytes+i] = dpt[i];
    542 	}
    543 
    544 	datum_pts->nbytes += dpend;
    545 
    546 	if (datum_pts->nbytes != 7) {
    547 		return;
    548 	}
    549 
    550 	/*
    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
    554 	** of microseconds.
    555 	*/
    556 
    557 	datum_pts->day =	100*(datum_pts->retbuf[0] & 0x0f) +
    558 		10*((datum_pts->retbuf[1] & 0xf0)>>4) +
    559 		(datum_pts->retbuf[1] & 0x0f);
    560 
    561 	datum_pts->hour =	10*((datum_pts->retbuf[2] & 0x30)>>4) +
    562 		(datum_pts->retbuf[2] & 0x0f);
    563 
    564 	datum_pts->minute =	10*((datum_pts->retbuf[3] & 0x70)>>4) +
    565 		(datum_pts->retbuf[3] & 0x0f);
    566 
    567 	datum_pts->second =	10*((datum_pts->retbuf[4] & 0x70)>>4) +
    568 		(datum_pts->retbuf[4] & 0x0f);
    569 
    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);
    573 
    574 	datum_pts->usec =	1000*datum_pts->msec;
    575 
    576 #ifdef DEBUG_DATUM_PTC
    577 	if (debug)
    578 	    printf("day %d, hour %d, minute %d, second %d, msec %d\n",
    579 		   datum_pts->day,
    580 		   datum_pts->hour,
    581 		   datum_pts->minute,
    582 		   datum_pts->second,
    583 		   datum_pts->msec);
    584 #endif
    585 
    586 	/*
    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
    590 	** base.
    591 	*/
    592 
    593 	goodtime = 0;		/* We are not sure about the time and offset yet */
    594 
    595 #ifdef GMT
    596 
    597 	/*
    598 	** This is the case where the Datum PTS is using GMT so there is no time
    599 	** zone offset.
    600 	*/
    601 
    602 	tzoff = 0;		/* set time zone offset to 0 */
    603 
    604 #else
    605 
    606 	/*
    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).
    618 	*/
    619 
    620 	for (tzoff=0; tzoff<24; tzoff++) {
    621 		if (clocktime( datum_pts->day,
    622 			       datum_pts->hour,
    623 			       datum_pts->minute,
    624 			       datum_pts->second,
    625 			       (tzoff + datum_pts->tzoff) % 24,
    626 			       datum_pts->lastrec.l_ui,
    627 			       &datum_pts->yearstart,
    628 			       &datum_pts->lastref.l_ui) ) {
    629 
    630 			datum_pts->lastref.l_uf = 0;
    631 			error = datum_pts->lastref.l_ui - datum_pts->lastrec.l_ui;
    632 
    633 #ifdef DEBUG_DATUM_PTC
    634 			printf("Time Zone (clocktime method) = %d, error = %d\n", tzoff, error);
    635 #endif
    636 
    637 			if ((error < 1799) && (error > -1799)) {
    638 				tzoff = (tzoff + datum_pts->tzoff) % 24;
    639 				datum_pts->tzoff = tzoff;
    640 				goodtime = 1;
    641 
    642 #ifdef DEBUG_DATUM_PTC
    643 				printf("Time Zone found (clocktime method) = %d\n",tzoff);
    644 #endif
    645 
    646 				break;
    647 			}
    648 
    649 		}
    650 	}
    651 
    652 #endif
    653 
    654 	/*
    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.
    658 	*/
    659 
    660 	if (!goodtime) {
    661 
    662 		if (!clocktime( datum_pts->day,
    663 				datum_pts->hour,
    664 				datum_pts->minute,
    665 				datum_pts->second,
    666 				tzoff,
    667 				datum_pts->lastrec.l_ui,
    668 				&datum_pts->yearstart,
    669 				&datum_pts->lastref.l_ui) ) {
    670 
    671 #ifdef DEBUG_DATUM_PTC
    672 			if (debug)
    673 			{
    674 				printf("Error: bad clocktime\n");
    675 				printf("GMT %d, lastrec %d, yearstart %d, lastref %d\n",
    676 				       tzoff,
    677 				       datum_pts->lastrec.l_ui,
    678 				       datum_pts->yearstart,
    679 				       datum_pts->lastref.l_ui);
    680 			}
    681 #endif
    682 
    683 			msyslog(LOG_ERR, "Datum_PTS: Bad clocktime");
    684 
    685 			return;
    686 
    687 		}else{
    688 
    689 #ifdef DEBUG_DATUM_PTC
    690 			if (debug)
    691 			    printf("Good clocktime\n");
    692 #endif
    693 
    694 		}
    695 
    696 	}
    697 
    698 	/*
    699 	** We have datum_pts->lastref.l_ui set (which is the integer part of the
    700 	** time. Now set the microseconds field.
    701 	*/
    702 
    703 	TVUTOTSF(datum_pts->usec, datum_pts->lastref.l_uf);
    704 
    705 	/*
    706 	** Compute the time correction as the difference between the reference
    707 	** time (i.e., the Datum time) minus the receive time (system time).
    708 	*/
    709 
    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 */
    713 
    714 #ifdef DEBUG_DATUM_PTC
    715 	dispersion = DATUM_DISPERSION;	/* set the dispersion to 0 */
    716 	ftimerr = dispersion;
    717 	ftimerr /= (1024.0 * 64.0);
    718 	if (debug)
    719 	    printf("dispersion = %d, %f\n", dispersion, ftimerr);
    720 #endif
    721 
    722 	/*
    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.
    730 	*/
    731       /*LFPTOD(&tstmp, doffset);*/
    732 	datum_pts->lastref = datum_pts->lastrec;
    733 	refclock_receive(datum_pts->peer);
    734 
    735 	/*
    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
    741 	** is set to 0.
    742 	*/
    743 
    744 	timerr = tstmp.l_ui<<20;
    745 	timerr |= (tstmp.l_uf>>12) & 0x000fffff;
    746 	ftimerr = timerr;
    747 	ftimerr /= 1024*1024;
    748 	abserr = ftimerr;
    749 	if (ftimerr < 0.0) abserr = -ftimerr;
    750 
    751 	if (datum_pts->sigma2 == 0.0) {
    752 		if (abserr < DATUM_MAX_ERROR) {
    753 			datum_pts->sigma2 = abserr*abserr;
    754 		}else{
    755 			datum_pts->sigma2 = DATUM_MAX_ERROR2;
    756 		}
    757 	}else{
    758 		if (abserr < DATUM_MAX_ERROR) {
    759 			datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*abserr*abserr;
    760 		}else{
    761 			datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*DATUM_MAX_ERROR2;
    762 		}
    763 	}
    764 
    765 #ifdef DEBUG_DATUM_PTC
    766 	if (debug)
    767 	    printf("Time error = %f seconds\n", ftimerr);
    768 #endif
    769 
    770 #if defined(DEBUG_DATUM_PTC) || defined(LOG_TIME_ERRORS)
    771 	if (debug)
    772 	    printf("PTS: day %d, hour %d, minute %d, second %d, msec %d, Time Error %f\n",
    773 		   datum_pts->day,
    774 		   datum_pts->hour,
    775 		   datum_pts->minute,
    776 		   datum_pts->second,
    777 		   datum_pts->msec,
    778 		   ftimerr);
    779 #endif
    780 
    781 }
    782 #else
    783 NONEMPTY_TRANSLATION_UNIT
    784 #endif /* REFCLOCK */
    785