/contrib/ntp/ntpd/refclock_datum.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 877 lines · 425 code · 168 blank · 284 comment · 51 complexity · 9e0de6bb2668e65794a9722f23533ea2 MD5 · raw file

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