refclock_datum.c revision 316722
1829SN/A/* 27424SN/A** refclock_datum - clock driver for the Datum Programmable Time Server 3829SN/A** 4829SN/A** Important note: This driver assumes that you have termios. If you have 5829SN/A** a system that does not have termios, you will have to modify this driver. 6829SN/A** 72362SN/A** Sorry, I have only tested this driver on SUN and HP platforms. 8829SN/A*/ 92362SN/A 10829SN/A#ifdef HAVE_CONFIG_H 11829SN/A# include <config.h> 12829SN/A#endif 13829SN/A 14829SN/A#include "ntp_types.h" 15829SN/A 16829SN/A#if defined(REFCLOCK) && defined(CLOCK_DATUM) 17829SN/A 18829SN/A/* 19829SN/A** Include Files 20829SN/A*/ 212362SN/A 222362SN/A#include "ntpd.h" 232362SN/A#include "ntp_io.h" 24829SN/A#include "ntp_tty.h" 2515409Sserb#include "ntp_refclock.h" 26829SN/A#include "timevalops.h" 27829SN/A#include "ntp_stdlib.h" 28829SN/A 29829SN/A#include <stdio.h> 30829SN/A#include <ctype.h> 31829SN/A 32829SN/A#if defined(STREAM) 33829SN/A#include <stropts.h> 347424SN/A#endif /* STREAM */ 35829SN/A 36829SN/A#include "ntp_stdlib.h" 37829SN/A 38829SN/A/* 39829SN/A** This driver supports the Datum Programmable Time System (PTS) clock. 40829SN/A** The clock works in very straight forward manner. When it receives a 41829SN/A** time code request (e.g., the ascii string "//k/mn"), it responds with 42829SN/A** a seven byte BCD time code. This clock only responds with a 43829SN/A** time code after it first receives the "//k/mn" message. It does not 44829SN/A** periodically send time codes back at some rate once it is started. 45829SN/A** the returned time code can be broken down into the following fields. 46829SN/A** 4715409Sserb** _______________________________ 48829SN/A** Bit Index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 49829SN/A** =============================== 50829SN/A** byte 0: | - - - - | H D | 51829SN/A** =============================== 5215409Sserb** byte 1: | T D | U D | 53829SN/A** =============================== 54829SN/A** byte 2: | - - | T H | U H | 55829SN/A** =============================== 56829SN/A** byte 3: | - | T M | U M | 57829SN/A** =============================== 58829SN/A** byte 4: | - | T S | U S | 59829SN/A** =============================== 6015409Sserb** byte 5: | t S | h S | 61829SN/A** =============================== 62829SN/A** byte 6: | m S | - - - - | 63829SN/A** =============================== 64829SN/A** 65829SN/A** In the table above: 66829SN/A** 67829SN/A** "-" means don't care 6815409Sserb** "H D", "T D", and "U D" means Hundreds, Tens, and Units of Days 69829SN/A** "T H", and "UH" means Tens and Units of Hours 70829SN/A** "T M", and "U M" means Tens and Units of Minutes 71829SN/A** "T S", and "U S" means Tens and Units of Seconds 72829SN/A** "t S", "h S", and "m S" means tenths, hundredths, and thousandths 7315409Sserb** of seconds 74829SN/A** 7515409Sserb** The Datum PTS communicates throught the RS232 port on your machine. 76829SN/A** Right now, it assumes that you have termios. This driver has been tested 77829SN/A** on SUN and HP workstations. The Datum PTS supports various IRIG and 78829SN/A** NASA input codes. This driver assumes that the name of the device is 79829SN/A** /dev/datum. You will need to make a soft link to your RS232 device or 8015409Sserb** create a new driver to use this refclock. 81829SN/A*/ 82829SN/A 83829SN/A/* 84829SN/A** Datum PTS defines 85829SN/A*/ 86829SN/A 87829SN/A/* 88829SN/A** Note that if GMT is defined, then the Datum PTS must use Greenwich 891990SN/A** time. Otherwise, this driver allows the Datum PTS to use the current 90829SN/A** wall clock for its time. It determines the time zone offset by minimizing 91829SN/A** the error after trying several time zone offsets. If the Datum PTS 92829SN/A** time is Greenwich time and GMT is not defined, everything should still 93829SN/A** work since the time zone will be found to be 0. What this really means 94829SN/A** is that your system time (at least to start with) must be within the 95829SN/A** correct time by less than +- 30 minutes. The default is for GMT to not 96829SN/A** defined. If you really want to force GMT without the funny +- 30 minute 97829SN/A** stuff then you must define (uncomment) GMT below. 98829SN/A*/ 99829SN/A 100829SN/A/* 101829SN/A#define GMT 102829SN/A#define DEBUG_DATUM_PTC 103829SN/A#define LOG_TIME_ERRORS 104829SN/A*/ 105829SN/A 106829SN/A 107829SN/A#define PRECISION (-10) /* precision assumed 1/1024 ms */ 108829SN/A#define REFID "DATM" /* reference id */ 109829SN/A#define DATUM_DISPERSION 0 /* fixed dispersion = 0 ms */ 110829SN/A#define DATUM_MAX_ERROR 0.100 /* limits on sigma squared */ 111829SN/A#define DATUM_DEV "/dev/datum" /* device name */ 112829SN/A 113829SN/A#define DATUM_MAX_ERROR2 (DATUM_MAX_ERROR*DATUM_MAX_ERROR) 114829SN/A 115829SN/A/* 116829SN/A** The Datum PTS structure 117829SN/A*/ 118829SN/A 119829SN/A/* 120829SN/A** I don't use a fixed array of MAXUNITS like everyone else just because 121829SN/A** I don't like to program that way. Sorry if this bothers anyone. I assume 122829SN/A** that you can use any id for your unit and I will search for it in a 123829SN/A** dynamic array of units until I find it. I was worried that users might 124829SN/A** enter a bad id in their configuration file (larger than MAXUNITS) and 125829SN/A** besides, it is just cleaner not to have to assume that you have a fixed 126829SN/A** number of anything in a program. 127829SN/A*/ 128829SN/A 129829SN/Astruct datum_pts_unit { 130829SN/A struct peer *peer; /* peer used by ntp */ 131829SN/A int PTS_fd; /* file descriptor for PTS */ 132829SN/A u_int unit; /* id for unit */ 133829SN/A u_long timestarted; /* time started */ 134829SN/A l_fp lastrec; /* time tag for the receive time (system) */ 135829SN/A l_fp lastref; /* reference time (Datum time) */ 136829SN/A u_long yearstart; /* the year that this clock started */ 137829SN/A int coderecv; /* number of time codes received */ 138829SN/A int day; /* day */ 139829SN/A int hour; /* hour */ 140829SN/A int minute; /* minutes */ 141829SN/A int second; /* seconds */ 142829SN/A int msec; /* miliseconds */ 143829SN/A int usec; /* miliseconds */ 144829SN/A u_char leap; /* funny leap character code */ 145829SN/A char retbuf[8]; /* returned time from the datum pts */ 146829SN/A char nbytes; /* number of bytes received from datum pts */ 147829SN/A double sigma2; /* average squared error (roughly) */ 148829SN/A int tzoff; /* time zone offest from GMT */ 149829SN/A}; 150829SN/A 151829SN/A/* 152829SN/A** PTS static constant variables for internal use 153829SN/A*/ 154829SN/A 155829SN/Astatic char TIME_REQUEST[6]; /* request message sent to datum for time */ 156829SN/Astatic int nunits; /* number of active units */ 157829SN/A 158829SN/A/* 159829SN/A** Callback function prototypes that ntpd needs to know about. 160829SN/A*/ 161829SN/A 162829SN/Astatic int datum_pts_start (int, struct peer *); 163829SN/Astatic void datum_pts_shutdown (int, struct peer *); 164829SN/Astatic void datum_pts_poll (int, struct peer *); 165829SN/Astatic void datum_pts_control (int, const struct refclockstat *, 166829SN/A struct refclockstat *, struct peer *); 167829SN/Astatic void datum_pts_init (void); 168829SN/Astatic void datum_pts_buginfo (int, struct refclockbug *, struct peer *); 169829SN/A 170829SN/A/* 171829SN/A** This is the call back function structure that ntpd actually uses for 172829SN/A** this refclock. 173829SN/A*/ 174829SN/A 175829SN/Astruct refclock refclock_datum = { 176829SN/A datum_pts_start, /* start up a new Datum refclock */ 177829SN/A datum_pts_shutdown, /* shutdown a Datum refclock */ 178829SN/A datum_pts_poll, /* sends out the time request */ 179829SN/A datum_pts_control, /* not used */ 180829SN/A datum_pts_init, /* initialization (called first) */ 181829SN/A datum_pts_buginfo, /* not used */ 182829SN/A NOFLAGS /* we are not setting any special flags */ 183829SN/A}; 184829SN/A 185829SN/A/* 186829SN/A** The datum_pts_receive callback function is handled differently from the 187829SN/A** rest. It is passed to the ntpd io data structure. Basically, every 188829SN/A** 64 seconds, the datum_pts_poll() routine is called. It sends out the time 189829SN/A** request message to the Datum Programmable Time System. Then, ntpd 190829SN/A** waits on a select() call to receive data back. The datum_pts_receive() 191829SN/A** function is called as data comes back. We expect a seven byte time 192829SN/A** code to be returned but the datum_pts_receive() function may only get 193829SN/A** a few bytes passed to it at a time. In other words, this routine may 194829SN/A** get called by the io stuff in ntpd a few times before we get all seven 195829SN/A** bytes. Once the last byte is received, we process it and then pass the 19615409Sserb** new time measurement to ntpd for updating the system time. For now, 197829SN/A** there is no 3 state filtering done on the time measurements. The 198829SN/A** jitter may be a little high but at least for its current use, it is not 199829SN/A** 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. 206*/ 207 208static void datum_pts_receive (struct recvbuf *); 209 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/*......................................................................*/ 214 215static int 216datum_pts_start( 217 int unit, 218 struct peer *peer 219 ) 220{ 221 struct refclockproc *pp; 222 struct datum_pts_unit *datum_pts; 223 int fd; 224#ifdef HAVE_TERMIOS 225 int rc; 226 struct termios arg; 227#endif 228 229#ifdef DEBUG_DATUM_PTC 230 if (debug) 231 printf("Starting Datum PTS unit %d\n", unit); 232#endif 233 234 /* 235 ** Open the Datum PTS device 236 */ 237 fd = open(DATUM_DEV, O_RDWR); 238 239 if (fd < 0) { 240 msyslog(LOG_ERR, "Datum_PTS: open(\"%s\", O_RDWR) failed: %m", DATUM_DEV); 241 return 0; 242 } 243 244 /* 245 ** Create the memory for the new unit 246 */ 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 */ 251 252 datum_pts->PTS_fd = fd; 253 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.", 256 unit); 257 258#ifdef DEBUG_DATUM_PTC 259 if (debug) 260 printf("Opening RS232 port with file descriptor %d\n", 261 datum_pts->PTS_fd); 262#endif 263 264 /* 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. 270 */ 271 272 memset(&arg, 0, sizeof(arg)); 273 274 arg.c_iflag = IGNBRK; 275 arg.c_oflag = 0; 276 arg.c_cflag = B9600 | CS8 | CREAD | PARENB | CLOCAL; 277 arg.c_lflag = 0; 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) */ 280 281 rc = tcsetattr(datum_pts->PTS_fd, TCSANOW, &arg); 282 if (rc < 0) { 283 msyslog(LOG_ERR, "Datum_PTS: tcsetattr(\"%s\") failed: %m", DATUM_DEV); 284 close(datum_pts->PTS_fd); 285 free(datum_pts); 286 return 0; 287 } 288 289 /* 290 ** Initialize the ntpd IO structure 291 */ 292 293 datum_pts->peer = peer; 294 pp = peer->procptr; 295 pp->io.clock_recv = datum_pts_receive; 296 pp->io.srcclock = peer; 297 pp->io.datalen = 0; 298 pp->io.fd = datum_pts->PTS_fd; 299 300 if (!io_addclock(&pp->io)) { 301 pp->io.fd = -1; 302#ifdef DEBUG_DATUM_PTC 303 if (debug) 304 printf("Problem adding clock\n"); 305#endif 306 307 msyslog(LOG_ERR, "Datum_PTS: Problem adding clock"); 308 close(datum_pts->PTS_fd); 309 free(datum_pts); 310 311 return 0; 312 } 313 peer->procptr->unitptr = datum_pts; 314 315 /* 316 ** Now add one to the number of units and return a successful code 317 */ 318 319 nunits++; 320 return 1; 321 322} 323 324 325/*......................................................................*/ 326/* datum_pts_shutdown - this routine shuts doen the device and */ 327/* removes the memory for the unit. */ 328/*......................................................................*/ 329 330static void 331datum_pts_shutdown( 332 int unit, 333 struct peer *peer 334 ) 335{ 336 struct refclockproc *pp; 337 struct datum_pts_unit *datum_pts; 338 339#ifdef DEBUG_DATUM_PTC 340 if (debug) 341 printf("Shutdown Datum PTS\n"); 342#endif 343 344 msyslog(LOG_ERR, "Datum_PTS: Shutdown Datum PTS"); 345 346 /* 347 ** We found the unit so close the file descriptor and free up the memory used 348 ** by the structure. 349 */ 350 pp = peer->procptr; 351 datum_pts = pp->unitptr; 352 if (NULL != datum_pts) { 353 io_closeclock(&pp->io); 354 free(datum_pts); 355 } 356} 357 358 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/*......................................................................*/ 364 365static void 366datum_pts_poll( 367 int unit, 368 struct peer *peer 369 ) 370{ 371 int error_code; 372 struct datum_pts_unit *datum_pts; 373 374 datum_pts = peer->procptr->unitptr; 375 376#ifdef DEBUG_DATUM_PTC 377 if (debug) 378 printf("Poll Datum PTS\n"); 379#endif 380 381 /* 382 ** Find the right unit and send out a time request once it is found. 383 */ 384 error_code = write(datum_pts->PTS_fd, TIME_REQUEST, 6); 385 if (error_code != 6) 386 perror("TIME_REQUEST"); 387 datum_pts->nbytes = 0; 388} 389 390 391/*......................................................................*/ 392/* datum_pts_control - not used */ 393/*......................................................................*/ 394 395static void 396datum_pts_control( 397 int unit, 398 const struct refclockstat *in, 399 struct refclockstat *out, 400 struct peer *peer 401 ) 402{ 403 404#ifdef DEBUG_DATUM_PTC 405 if (debug) 406 printf("Control Datum PTS\n"); 407#endif 408 409} 410 411 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 */ 416/* called. */ 417/*......................................................................*/ 418 419static void 420datum_pts_init(void) 421{ 422 423 /* */ 424 /*...... open up the log file if we are debugging ......................*/ 425 /* */ 426 427 /* 428 ** Open up the log file if we are debugging. For now, send data out to the 429 ** screen (stdout). 430 */ 431 432#ifdef DEBUG_DATUM_PTC 433 if (debug) 434 printf("Init Datum PTS\n"); 435#endif 436 437 /* 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). 440 */ 441 442 memcpy(TIME_REQUEST, "//k/mn",6); 443 444 /* 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. 447 */ 448 449 nunits = 0; 450 451} 452 453 454/*......................................................................*/ 455/* datum_pts_buginfo - not used */ 456/*......................................................................*/ 457 458static void 459datum_pts_buginfo( 460 int unit, 461 register struct refclockbug *bug, 462 register struct peer *peer 463 ) 464{ 465 466#ifdef DEBUG_DATUM_PTC 467 if (debug) 468 printf("Buginfo Datum PTS\n"); 469#endif 470 471} 472 473 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/*......................................................................*/ 482 483static void 484datum_pts_receive( 485 struct recvbuf *rbufp 486 ) 487{ 488 int i, nb; 489 l_fp tstmp; 490 struct peer *p; 491 struct datum_pts_unit *datum_pts; 492 char *dpt; 493 int dpend; 494 int tzoff; 495 int timerr; 496 double ftimerr, abserr; 497#ifdef DEBUG_DATUM_PTC 498 double dispersion; 499#endif 500 int goodtime; 501 /*double doffset;*/ 502 503 /* 504 ** Get the time code (maybe partial) message out of the rbufp buffer. 505 */ 506 507 p = rbufp->recv_peer; 508 datum_pts = p->procptr->unitptr; 509 dpt = (char *)&rbufp->recv_space; 510 dpend = rbufp->recv_length; 511 512#ifdef DEBUG_DATUM_PTC 513 if (debug) 514 printf("Receive Datum PTS: %d bytes\n", dpend); 515#endif 516 517 /* */ 518 /*...... save the ntp system time when the first byte is received ......*/ 519 /* */ 520 521 /* 522 ** Save the ntp system time when the first byte is received. Note that 523 ** because it may take several calls to this routine before all seven 524 ** bytes of our return message are finally received by the io handlers in 525 ** ntpd, we really do want to use the time tag when the first byte is 526 ** received to reduce the jitter. 527 */ 528 529 nb = datum_pts->nbytes; 530 if (nb == 0) { 531 datum_pts->lastrec = rbufp->recv_time; 532 } 533 534 /* 535 ** Increment our count to the number of bytes received so far. Return if we 536 ** haven't gotten all seven bytes yet. 537 ** [Sec 3388] make sure we do not overrun the buffer. 538 ** TODO: what to do with excessive bytes, if we ever get them? 539 */ 540 for (i=0; (i < dpend) && (nb < sizeof(datum_pts->retbuf)); i++, nb++) { 541 datum_pts->retbuf[nb] = dpt[i]; 542 } 543 datum_pts->nbytes = nb; 544 545 if (nb < 7) { 546 return; 547 } 548 549 /* 550 ** Convert the seven bytes received in our time buffer to day, hour, minute, 551 ** second, and msecond values. The usec value is not used for anything 552 ** currently. It is just the fractional part of the time stored in units 553 ** of microseconds. 554 */ 555 556 datum_pts->day = 100*(datum_pts->retbuf[0] & 0x0f) + 557 10*((datum_pts->retbuf[1] & 0xf0)>>4) + 558 (datum_pts->retbuf[1] & 0x0f); 559 560 datum_pts->hour = 10*((datum_pts->retbuf[2] & 0x30)>>4) + 561 (datum_pts->retbuf[2] & 0x0f); 562 563 datum_pts->minute = 10*((datum_pts->retbuf[3] & 0x70)>>4) + 564 (datum_pts->retbuf[3] & 0x0f); 565 566 datum_pts->second = 10*((datum_pts->retbuf[4] & 0x70)>>4) + 567 (datum_pts->retbuf[4] & 0x0f); 568 569 datum_pts->msec = 100*((datum_pts->retbuf[5] & 0xf0) >> 4) + 570 10*(datum_pts->retbuf[5] & 0x0f) + 571 ((datum_pts->retbuf[6] & 0xf0)>>4); 572 573 datum_pts->usec = 1000*datum_pts->msec; 574 575#ifdef DEBUG_DATUM_PTC 576 if (debug) 577 printf("day %d, hour %d, minute %d, second %d, msec %d\n", 578 datum_pts->day, 579 datum_pts->hour, 580 datum_pts->minute, 581 datum_pts->second, 582 datum_pts->msec); 583#endif 584 585 /* 586 ** Get the GMT time zone offset. Note that GMT should be zero if the Datum 587 ** reference time is using GMT as its time base. Otherwise we have to 588 ** determine the offset if the Datum PTS is using time of day as its time 589 ** base. 590 */ 591 592 goodtime = 0; /* We are not sure about the time and offset yet */ 593 594#ifdef GMT 595 596 /* 597 ** This is the case where the Datum PTS is using GMT so there is no time 598 ** zone offset. 599 */ 600 601 tzoff = 0; /* set time zone offset to 0 */ 602 603#else 604 605 /* 606 ** This is the case where the Datum PTS is using regular time of day for its 607 ** time so we must compute the time zone offset. The way we do it is kind of 608 ** funny but it works. We loop through different time zones (0 to 24) and 609 ** pick the one that gives the smallest error (+- one half hour). The time 610 ** zone offset is stored in the datum_pts structure for future use. Normally, 611 ** the clocktime() routine is only called once (unless the time zone offset 612 ** changes due to daylight savings) since the goodtime flag is set when a 613 ** good time is found (with a good offset). Note that even if the Datum 614 ** PTS is using GMT, this mechanism will still work since it should come up 615 ** with a value for tzoff = 0 (assuming that your system clock is within 616 ** a half hour of the Datum time (even with time zone differences). 617 */ 618 619 for (tzoff=0; tzoff<24; tzoff++) { 620 if (clocktime( datum_pts->day, 621 datum_pts->hour, 622 datum_pts->minute, 623 datum_pts->second, 624 (tzoff + datum_pts->tzoff) % 24, 625 datum_pts->lastrec.l_ui, 626 &datum_pts->yearstart, 627 &datum_pts->lastref.l_ui) ) { 628 629 datum_pts->lastref.l_uf = 0; 630 error = datum_pts->lastref.l_ui - datum_pts->lastrec.l_ui; 631 632#ifdef DEBUG_DATUM_PTC 633 printf("Time Zone (clocktime method) = %d, error = %d\n", tzoff, error); 634#endif 635 636 if ((error < 1799) && (error > -1799)) { 637 tzoff = (tzoff + datum_pts->tzoff) % 24; 638 datum_pts->tzoff = tzoff; 639 goodtime = 1; 640 641#ifdef DEBUG_DATUM_PTC 642 printf("Time Zone found (clocktime method) = %d\n",tzoff); 643#endif 644 645 break; 646 } 647 648 } 649 } 650 651#endif 652 653 /* 654 ** Make sure that we have a good time from the Datum PTS. Clocktime() also 655 ** sets yearstart and lastref.l_ui. We will have to set astref.l_uf (i.e., 656 ** the fraction of a second) stuff later. 657 */ 658 659 if (!goodtime) { 660 661 if (!clocktime( datum_pts->day, 662 datum_pts->hour, 663 datum_pts->minute, 664 datum_pts->second, 665 tzoff, 666 datum_pts->lastrec.l_ui, 667 &datum_pts->yearstart, 668 &datum_pts->lastref.l_ui) ) { 669 670#ifdef DEBUG_DATUM_PTC 671 if (debug) 672 { 673 printf("Error: bad clocktime\n"); 674 printf("GMT %d, lastrec %d, yearstart %d, lastref %d\n", 675 tzoff, 676 datum_pts->lastrec.l_ui, 677 datum_pts->yearstart, 678 datum_pts->lastref.l_ui); 679 } 680#endif 681 682 msyslog(LOG_ERR, "Datum_PTS: Bad clocktime"); 683 684 return; 685 686 }else{ 687 688#ifdef DEBUG_DATUM_PTC 689 if (debug) 690 printf("Good clocktime\n"); 691#endif 692 693 } 694 695 } 696 697 /* 698 ** We have datum_pts->lastref.l_ui set (which is the integer part of the 699 ** time. Now set the microseconds field. 700 */ 701 702 TVUTOTSF(datum_pts->usec, datum_pts->lastref.l_uf); 703 704 /* 705 ** Compute the time correction as the difference between the reference 706 ** time (i.e., the Datum time) minus the receive time (system time). 707 */ 708 709 tstmp = datum_pts->lastref; /* tstmp is the datum ntp time */ 710 L_SUB(&tstmp, &datum_pts->lastrec); /* tstmp is now the correction */ 711 datum_pts->coderecv++; /* increment a counter */ 712 713#ifdef DEBUG_DATUM_PTC 714 dispersion = DATUM_DISPERSION; /* set the dispersion to 0 */ 715 ftimerr = dispersion; 716 ftimerr /= (1024.0 * 64.0); 717 if (debug) 718 printf("dispersion = %d, %f\n", dispersion, ftimerr); 719#endif 720 721 /* 722 ** Pass the new time to ntpd through the refclock_receive function. Note 723 ** that we are not trying to make any corrections due to the time it takes 724 ** for the Datum PTS to send the message back. I am (erroneously) assuming 725 ** that the time for the Datum PTS to send the time back to us is negligable. 726 ** I suspect that this time delay may be as much as 15 ms or so (but probably 727 ** less). For our needs at JPL, this kind of error is ok so it is not 728 ** necessary to use fudge factors in the ntp.conf file. Maybe later we will. 729 */ 730 /*LFPTOD(&tstmp, doffset);*/ 731 datum_pts->lastref = datum_pts->lastrec; 732 refclock_receive(datum_pts->peer); 733 734 /* 735 ** Compute sigma squared (not used currently). Maybe later, this could be 736 ** used for the dispersion estimate. The problem is that ntpd does not link 737 ** in the math library so sqrt() is not available. Anyway, this is useful 738 ** for debugging. Maybe later I will just use absolute values for the time 739 ** error to come up with my dispersion estimate. Anyway, for now my dispersion 740 ** is set to 0. 741 */ 742 743 timerr = tstmp.l_ui<<20; 744 timerr |= (tstmp.l_uf>>12) & 0x000fffff; 745 ftimerr = timerr; 746 ftimerr /= 1024*1024; 747 abserr = ftimerr; 748 if (ftimerr < 0.0) abserr = -ftimerr; 749 750 if (datum_pts->sigma2 == 0.0) { 751 if (abserr < DATUM_MAX_ERROR) { 752 datum_pts->sigma2 = abserr*abserr; 753 }else{ 754 datum_pts->sigma2 = DATUM_MAX_ERROR2; 755 } 756 }else{ 757 if (abserr < DATUM_MAX_ERROR) { 758 datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*abserr*abserr; 759 }else{ 760 datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*DATUM_MAX_ERROR2; 761 } 762 } 763 764#ifdef DEBUG_DATUM_PTC 765 if (debug) 766 printf("Time error = %f seconds\n", ftimerr); 767#endif 768 769#if defined(DEBUG_DATUM_PTC) || defined(LOG_TIME_ERRORS) 770 if (debug) 771 printf("PTS: day %d, hour %d, minute %d, second %d, msec %d, Time Error %f\n", 772 datum_pts->day, 773 datum_pts->hour, 774 datum_pts->minute, 775 datum_pts->second, 776 datum_pts->msec, 777 ftimerr); 778#endif 779 780} 781#else 782NONEMPTY_TRANSLATION_UNIT 783#endif /* REFCLOCK */ 784