/contrib/ntp/ntpd/refclock_jupiter.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1137 lines · 756 code · 131 blank · 250 comment · 132 complexity · ffc13e462a638cb26acf8ef407ac6428 MD5 · raw file

  1. /*
  2. * Copyright (c) 1997, 1998, 2003
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Lawrence Berkeley Laboratory.
  17. * 4. The name of the University may not be used to endorse or promote
  18. * products derived from this software without specific prior
  19. * written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. # include <config.h>
  35. #endif
  36. #if defined(REFCLOCK) && defined(CLOCK_JUPITER) && defined(HAVE_PPSAPI)
  37. #include "ntpd.h"
  38. #include "ntp_io.h"
  39. #include "ntp_refclock.h"
  40. #include "ntp_unixtime.h"
  41. #include "ntp_stdlib.h"
  42. #include <stdio.h>
  43. #include <ctype.h>
  44. #include "jupiter.h"
  45. #ifdef HAVE_PPSAPI
  46. # include "ppsapi_timepps.h"
  47. #endif
  48. #ifdef XNTP_BIG_ENDIAN
  49. #define getshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
  50. #define putshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
  51. #else
  52. #define getshort(s) (s)
  53. #define putshort(s) (s)
  54. #endif
  55. /* XXX */
  56. #ifdef sun
  57. char *strerror(int);
  58. #endif
  59. /*
  60. * This driver supports the Rockwell Jupiter GPS Receiver board
  61. * adapted to precision timing applications. It requires the
  62. * ppsclock line discipline or streams module described in the
  63. * Line Disciplines and Streams Drivers page. It also requires a
  64. * gadget box and 1-PPS level converter, such as described in the
  65. * Pulse-per-second (PPS) Signal Interfacing page.
  66. *
  67. * It may work (with minor modifications) with other Rockwell GPS
  68. * receivers such as the CityTracker.
  69. */
  70. /*
  71. * GPS Definitions
  72. */
  73. #define DEVICE "/dev/gps%d" /* device name and unit */
  74. #define SPEED232 B9600 /* baud */
  75. /*
  76. * Radio interface parameters
  77. */
  78. #define PRECISION (-18) /* precision assumed (about 4 us) */
  79. #define REFID "GPS\0" /* reference id */
  80. #define DESCRIPTION "Rockwell Jupiter GPS Receiver" /* who we are */
  81. #define DEFFUDGETIME 0 /* default fudge time (ms) */
  82. /* Unix timestamp for the GPS epoch: January 6, 1980 */
  83. #define GPS_EPOCH 315964800
  84. /* Double short to unsigned int */
  85. #define DS2UI(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
  86. /* Double short to signed int */
  87. #define DS2I(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
  88. /* One week's worth of seconds */
  89. #define WEEKSECS (7 * 24 * 60 * 60)
  90. /*
  91. * Jupiter unit control structure.
  92. */
  93. struct instance {
  94. struct peer *peer; /* peer */
  95. u_int pollcnt; /* poll message counter */
  96. u_int polled; /* Hand in a time sample? */
  97. #ifdef HAVE_PPSAPI
  98. pps_params_t pps_params; /* pps parameters */
  99. pps_info_t pps_info; /* last pps data */
  100. pps_handle_t pps_handle; /* pps handle */
  101. u_int assert; /* pps edge to use */
  102. u_int hardpps; /* enable kernel mode */
  103. struct timespec ts; /* last timestamp */
  104. #endif
  105. l_fp limit;
  106. u_int gpos_gweek; /* Current GPOS GPS week number */
  107. u_int gpos_sweek; /* Current GPOS GPS seconds into week */
  108. u_int gweek; /* current GPS week number */
  109. u_int32 lastsweek; /* last seconds into GPS week */
  110. time_t timecode; /* current ntp timecode */
  111. u_int32 stime; /* used to detect firmware bug */
  112. int wantid; /* don't reconfig on channel id msg */
  113. u_int moving; /* mobile platform? */
  114. u_char sloppyclockflag; /* fudge flags */
  115. u_short sbuf[512]; /* local input buffer */
  116. int ssize; /* space used in sbuf */
  117. };
  118. /*
  119. * Function prototypes
  120. */
  121. static void jupiter_canmsg P((struct instance *, u_int));
  122. static u_short jupiter_cksum P((u_short *, u_int));
  123. static int jupiter_config P((struct instance *));
  124. static void jupiter_debug P((struct peer *, char *, char *, ...))
  125. __attribute__ ((format (printf, 3, 4)));
  126. static char * jupiter_parse_t P((struct instance *, u_short *));
  127. static char * jupiter_parse_gpos P((struct instance *, u_short *));
  128. static void jupiter_platform P((struct instance *, u_int));
  129. static void jupiter_poll P((int, struct peer *));
  130. static void jupiter_control P((int, struct refclockstat *, struct
  131. refclockstat *, struct peer *));
  132. #ifdef HAVE_PPSAPI
  133. static int jupiter_ppsapi P((struct instance *));
  134. static int jupiter_pps P((struct instance *));
  135. #endif /* HAVE_PPSAPI */
  136. static int jupiter_recv P((struct instance *));
  137. static void jupiter_receive P((struct recvbuf *rbufp));
  138. static void jupiter_reqmsg P((struct instance *, u_int, u_int));
  139. static void jupiter_reqonemsg P((struct instance *, u_int));
  140. static char * jupiter_send P((struct instance *, struct jheader *));
  141. static void jupiter_shutdown P((int, struct peer *));
  142. static int jupiter_start P((int, struct peer *));
  143. /*
  144. * Transfer vector
  145. */
  146. struct refclock refclock_jupiter = {
  147. jupiter_start, /* start up driver */
  148. jupiter_shutdown, /* shut down driver */
  149. jupiter_poll, /* transmit poll message */
  150. jupiter_control, /* (clock control) */
  151. noentry, /* (clock init) */
  152. noentry, /* (clock buginfo) */
  153. NOFLAGS /* not used */
  154. };
  155. /*
  156. * jupiter_start - open the devices and initialize data for processing
  157. */
  158. static int
  159. jupiter_start(
  160. int unit,
  161. struct peer *peer
  162. )
  163. {
  164. struct refclockproc *pp;
  165. struct instance *instance;
  166. int fd = -1;
  167. char gpsdev[20];
  168. /*
  169. * Open serial port
  170. */
  171. (void)sprintf(gpsdev, DEVICE, unit);
  172. fd = refclock_open(gpsdev, SPEED232, LDISC_RAW);
  173. if (fd == 0) {
  174. jupiter_debug(peer, "jupiter_start", "open %s: %s",
  175. gpsdev, strerror(errno));
  176. return (0);
  177. }
  178. /* Allocate unit structure */
  179. if ((instance = (struct instance *)
  180. emalloc(sizeof(struct instance))) == NULL) {
  181. (void) close(fd);
  182. return (0);
  183. }
  184. memset((char *)instance, 0, sizeof(struct instance));
  185. instance->peer = peer;
  186. pp = peer->procptr;
  187. pp->io.clock_recv = jupiter_receive;
  188. pp->io.srcclock = (caddr_t)peer;
  189. pp->io.datalen = 0;
  190. pp->io.fd = fd;
  191. if (!io_addclock(&pp->io)) {
  192. (void) close(fd);
  193. free(instance);
  194. return (0);
  195. }
  196. pp->unitptr = (caddr_t)instance;
  197. /*
  198. * Initialize miscellaneous variables
  199. */
  200. peer->precision = PRECISION;
  201. pp->clockdesc = DESCRIPTION;
  202. memcpy((char *)&pp->refid, REFID, 4);
  203. #ifdef HAVE_PPSAPI
  204. instance->assert = 1;
  205. instance->hardpps = 0;
  206. /*
  207. * Start the PPSAPI interface if it is there. Default to use
  208. * the assert edge and do not enable the kernel hardpps.
  209. */
  210. if (time_pps_create(fd, &instance->pps_handle) < 0) {
  211. instance->pps_handle = 0;
  212. msyslog(LOG_ERR,
  213. "refclock_jupiter: time_pps_create failed: %m");
  214. }
  215. else if (!jupiter_ppsapi(instance))
  216. goto clean_up;
  217. #endif /* HAVE_PPSAPI */
  218. /* Ensure the receiver is properly configured */
  219. if (!jupiter_config(instance))
  220. goto clean_up;
  221. return (1);
  222. clean_up:
  223. jupiter_shutdown(unit, peer);
  224. pp->unitptr = 0;
  225. return (0);
  226. }
  227. /*
  228. * jupiter_shutdown - shut down the clock
  229. */
  230. static void
  231. jupiter_shutdown(int unit, struct peer *peer)
  232. {
  233. struct instance *instance;
  234. struct refclockproc *pp;
  235. pp = peer->procptr;
  236. instance = (struct instance *)pp->unitptr;
  237. if (!instance)
  238. return;
  239. #ifdef HAVE_PPSAPI
  240. if (instance->pps_handle) {
  241. time_pps_destroy(instance->pps_handle);
  242. instance->pps_handle = 0;
  243. }
  244. #endif /* HAVE_PPSAPI */
  245. io_closeclock(&pp->io);
  246. free(instance);
  247. }
  248. /*
  249. * jupiter_config - Configure the receiver
  250. */
  251. static int
  252. jupiter_config(struct instance *instance)
  253. {
  254. jupiter_debug(instance->peer, "jupiter_config", "init receiver");
  255. /*
  256. * Initialize the unit variables
  257. */
  258. instance->sloppyclockflag = instance->peer->procptr->sloppyclockflag;
  259. instance->moving = !!(instance->sloppyclockflag & CLK_FLAG2);
  260. if (instance->moving)
  261. jupiter_debug(instance->peer, "jupiter_config",
  262. "mobile platform");
  263. instance->pollcnt = 2;
  264. instance->polled = 0;
  265. instance->gpos_gweek = 0;
  266. instance->gpos_sweek = 0;
  267. instance->gweek = 0;
  268. instance->lastsweek = 2 * WEEKSECS;
  269. instance->timecode = 0;
  270. instance->stime = 0;
  271. instance->ssize = 0;
  272. /* Stop outputting all messages */
  273. jupiter_canmsg(instance, JUPITER_ALL);
  274. /* Request the receiver id so we can syslog the firmware version */
  275. jupiter_reqonemsg(instance, JUPITER_O_ID);
  276. /* Flag that this the id was requested (so we don't get called again) */
  277. instance->wantid = 1;
  278. /* Request perodic time mark pulse messages */
  279. jupiter_reqmsg(instance, JUPITER_O_PULSE, 1);
  280. /* Request perodic geodetic position status */
  281. jupiter_reqmsg(instance, JUPITER_O_GPOS, 1);
  282. /* Set application platform type */
  283. if (instance->moving)
  284. jupiter_platform(instance, JUPITER_I_PLAT_MED);
  285. else
  286. jupiter_platform(instance, JUPITER_I_PLAT_LOW);
  287. return (1);
  288. }
  289. #ifdef HAVE_PPSAPI
  290. /*
  291. * Initialize PPSAPI
  292. */
  293. int
  294. jupiter_ppsapi(
  295. struct instance *instance /* unit structure pointer */
  296. )
  297. {
  298. int capability;
  299. if (time_pps_getcap(instance->pps_handle, &capability) < 0) {
  300. msyslog(LOG_ERR,
  301. "refclock_jupiter: time_pps_getcap failed: %m");
  302. return (0);
  303. }
  304. memset(&instance->pps_params, 0, sizeof(pps_params_t));
  305. if (!instance->assert)
  306. instance->pps_params.mode = capability & PPS_CAPTURECLEAR;
  307. else
  308. instance->pps_params.mode = capability & PPS_CAPTUREASSERT;
  309. if (!(instance->pps_params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
  310. msyslog(LOG_ERR,
  311. "refclock_jupiter: invalid capture edge %d",
  312. instance->assert);
  313. return (0);
  314. }
  315. instance->pps_params.mode |= PPS_TSFMT_TSPEC;
  316. if (time_pps_setparams(instance->pps_handle, &instance->pps_params) < 0) {
  317. msyslog(LOG_ERR,
  318. "refclock_jupiter: time_pps_setparams failed: %m");
  319. return (0);
  320. }
  321. if (instance->hardpps) {
  322. if (time_pps_kcbind(instance->pps_handle, PPS_KC_HARDPPS,
  323. instance->pps_params.mode & ~PPS_TSFMT_TSPEC,
  324. PPS_TSFMT_TSPEC) < 0) {
  325. msyslog(LOG_ERR,
  326. "refclock_jupiter: time_pps_kcbind failed: %m");
  327. return (0);
  328. }
  329. pps_enable = 1;
  330. }
  331. /* instance->peer->precision = PPS_PRECISION; */
  332. #if DEBUG
  333. if (debug) {
  334. time_pps_getparams(instance->pps_handle, &instance->pps_params);
  335. jupiter_debug(instance->peer, "refclock_jupiter",
  336. "pps capability 0x%x version %d mode 0x%x kern %d",
  337. capability, instance->pps_params.api_version,
  338. instance->pps_params.mode, instance->hardpps);
  339. }
  340. #endif
  341. return (1);
  342. }
  343. /*
  344. * Get PPSAPI timestamps.
  345. *
  346. * Return 0 on failure and 1 on success.
  347. */
  348. static int
  349. jupiter_pps(struct instance *instance)
  350. {
  351. pps_info_t pps_info;
  352. struct timespec timeout, ts;
  353. double dtemp;
  354. l_fp tstmp;
  355. /*
  356. * Convert the timespec nanoseconds field to ntp l_fp units.
  357. */
  358. if (instance->pps_handle == 0)
  359. return 1;
  360. timeout.tv_sec = 0;
  361. timeout.tv_nsec = 0;
  362. memcpy(&pps_info, &instance->pps_info, sizeof(pps_info_t));
  363. if (time_pps_fetch(instance->pps_handle, PPS_TSFMT_TSPEC, &instance->pps_info,
  364. &timeout) < 0)
  365. return 1;
  366. if (instance->pps_params.mode & PPS_CAPTUREASSERT) {
  367. if (pps_info.assert_sequence ==
  368. instance->pps_info.assert_sequence)
  369. return 1;
  370. ts = instance->pps_info.assert_timestamp;
  371. } else if (instance->pps_params.mode & PPS_CAPTURECLEAR) {
  372. if (pps_info.clear_sequence ==
  373. instance->pps_info.clear_sequence)
  374. return 1;
  375. ts = instance->pps_info.clear_timestamp;
  376. } else {
  377. return 1;
  378. }
  379. if ((instance->ts.tv_sec == ts.tv_sec) && (instance->ts.tv_nsec == ts.tv_nsec))
  380. return 1;
  381. instance->ts = ts;
  382. tstmp.l_ui = ts.tv_sec + JAN_1970;
  383. dtemp = ts.tv_nsec * FRAC / 1e9;
  384. tstmp.l_uf = (u_int32)dtemp;
  385. instance->peer->procptr->lastrec = tstmp;
  386. return 0;
  387. }
  388. #endif /* HAVE_PPSAPI */
  389. /*
  390. * jupiter_poll - jupiter watchdog routine
  391. */
  392. static void
  393. jupiter_poll(int unit, struct peer *peer)
  394. {
  395. struct instance *instance;
  396. struct refclockproc *pp;
  397. pp = peer->procptr;
  398. instance = (struct instance *)pp->unitptr;
  399. /*
  400. * You don't need to poll this clock. It puts out timecodes
  401. * once per second. If asked for a timestamp, take note.
  402. * The next time a timecode comes in, it will be fed back.
  403. */
  404. /*
  405. * If we haven't had a response in a while, reset the receiver.
  406. */
  407. if (instance->pollcnt > 0) {
  408. instance->pollcnt--;
  409. } else {
  410. refclock_report(peer, CEVNT_TIMEOUT);
  411. /* Request the receiver id to trigger a reconfig */
  412. jupiter_reqonemsg(instance, JUPITER_O_ID);
  413. instance->wantid = 0;
  414. }
  415. /*
  416. * polled every 64 seconds. Ask jupiter_receive to hand in
  417. * a timestamp.
  418. */
  419. instance->polled = 1;
  420. pp->polls++;
  421. }
  422. /*
  423. * jupiter_control - fudge control
  424. */
  425. static void
  426. jupiter_control(
  427. int unit, /* unit (not used) */
  428. struct refclockstat *in, /* input parameters (not used) */
  429. struct refclockstat *out, /* output parameters (not used) */
  430. struct peer *peer /* peer structure pointer */
  431. )
  432. {
  433. struct refclockproc *pp;
  434. struct instance *instance;
  435. u_char sloppyclockflag;
  436. pp = peer->procptr;
  437. instance = (struct instance *)pp->unitptr;
  438. DTOLFP(pp->fudgetime2, &instance->limit);
  439. /* Force positive value. */
  440. if (L_ISNEG(&instance->limit))
  441. L_NEG(&instance->limit);
  442. #ifdef HAVE_PPSAPI
  443. instance->assert = !(pp->sloppyclockflag & CLK_FLAG3);
  444. jupiter_ppsapi(instance);
  445. #endif /* HAVE_PPSAPI */
  446. sloppyclockflag = instance->sloppyclockflag;
  447. instance->sloppyclockflag = pp->sloppyclockflag;
  448. if ((instance->sloppyclockflag & CLK_FLAG2) !=
  449. (sloppyclockflag & CLK_FLAG2)) {
  450. jupiter_debug(peer,
  451. "jupiter_control",
  452. "mode switch: reset receiver");
  453. jupiter_config(instance);
  454. return;
  455. }
  456. }
  457. /*
  458. * jupiter_receive - receive gps data
  459. * Gag me!
  460. */
  461. static void
  462. jupiter_receive(struct recvbuf *rbufp)
  463. {
  464. int bpcnt, cc, size, ppsret;
  465. time_t last_timecode;
  466. u_int32 laststime;
  467. char *cp;
  468. u_char *bp;
  469. u_short *sp;
  470. struct jid *ip;
  471. struct jheader *hp;
  472. struct peer *peer;
  473. struct refclockproc *pp;
  474. struct instance *instance;
  475. l_fp tstamp;
  476. /* Initialize pointers and read the timecode and timestamp */
  477. peer = (struct peer *)rbufp->recv_srcclock;
  478. pp = peer->procptr;
  479. instance = (struct instance *)pp->unitptr;
  480. bp = (u_char *)rbufp->recv_buffer;
  481. bpcnt = rbufp->recv_length;
  482. /* This shouldn't happen */
  483. if (bpcnt > sizeof(instance->sbuf) - instance->ssize)
  484. bpcnt = sizeof(instance->sbuf) - instance->ssize;
  485. /* Append to input buffer */
  486. memcpy((u_char *)instance->sbuf + instance->ssize, bp, bpcnt);
  487. instance->ssize += bpcnt;
  488. /* While there's at least a header and we parse an intact message */
  489. while (instance->ssize > sizeof(*hp) && (cc = jupiter_recv(instance)) > 0) {
  490. instance->pollcnt = 2;
  491. tstamp = rbufp->recv_time;
  492. hp = (struct jheader *)instance->sbuf;
  493. sp = (u_short *)(hp + 1);
  494. size = cc - sizeof(*hp);
  495. switch (getshort(hp->id)) {
  496. case JUPITER_O_PULSE:
  497. if (size != sizeof(struct jpulse)) {
  498. jupiter_debug(peer,
  499. "jupiter_receive", "pulse: len %d != %u",
  500. size, (int)sizeof(struct jpulse));
  501. refclock_report(peer, CEVNT_BADREPLY);
  502. break;
  503. }
  504. /*
  505. * There appears to be a firmware bug related
  506. * to the pulse message; in addition to the one
  507. * per second messages, we get an extra pulse
  508. * message once an hour (on the anniversary of
  509. * the cold start). It seems to come 200 ms
  510. * after the one requested. So if we've seen a
  511. * pulse message in the last 210 ms, we skip
  512. * this one.
  513. */
  514. laststime = instance->stime;
  515. instance->stime = DS2UI(((struct jpulse *)sp)->stime);
  516. if (laststime != 0 && instance->stime - laststime <= 21) {
  517. jupiter_debug(peer, "jupiter_receive",
  518. "avoided firmware bug (stime %.2f, laststime %.2f)",
  519. (double)instance->stime * 0.01, (double)laststime * 0.01);
  520. break;
  521. }
  522. /* Retrieve pps timestamp */
  523. ppsret = jupiter_pps(instance);
  524. /*
  525. * Add one second if msg received early
  526. * (i.e. before limit, a.k.a. fudgetime2) in
  527. * the second.
  528. */
  529. L_SUB(&tstamp, &pp->lastrec);
  530. if (!L_ISGEQ(&tstamp, &instance->limit))
  531. ++pp->lastrec.l_ui;
  532. /* Parse timecode (even when there's no pps) */
  533. last_timecode = instance->timecode;
  534. if ((cp = jupiter_parse_t(instance, sp)) != NULL) {
  535. jupiter_debug(peer,
  536. "jupiter_receive", "pulse: %s", cp);
  537. break;
  538. }
  539. /* Bail if we didn't get a pps timestamp */
  540. if (ppsret)
  541. break;
  542. /* Bail if we don't have the last timecode yet */
  543. if (last_timecode == 0)
  544. break;
  545. /* Add the new sample to a median filter */
  546. tstamp.l_ui = JAN_1970 + last_timecode;
  547. tstamp.l_uf = 0;
  548. refclock_process_offset(pp, tstamp, pp->lastrec, pp->fudgetime1);
  549. /*
  550. * The clock will blurt a timecode every second
  551. * but we only want one when polled. If we
  552. * havn't been polled, bail out.
  553. */
  554. if (!instance->polled)
  555. break;
  556. instance->polled = 0;
  557. /*
  558. * It's a live one! Remember this time.
  559. */
  560. pp->lastref = pp->lastrec;
  561. refclock_receive(peer);
  562. /*
  563. * If we get here - what we got from the clock is
  564. * OK, so say so
  565. */
  566. refclock_report(peer, CEVNT_NOMINAL);
  567. /*
  568. * We have succeeded in answering the poll.
  569. * Turn off the flag and return
  570. */
  571. instance->polled = 0;
  572. break;
  573. case JUPITER_O_GPOS:
  574. if (size != sizeof(struct jgpos)) {
  575. jupiter_debug(peer,
  576. "jupiter_receive", "gpos: len %d != %u",
  577. size, (int)sizeof(struct jgpos));
  578. refclock_report(peer, CEVNT_BADREPLY);
  579. break;
  580. }
  581. if ((cp = jupiter_parse_gpos(instance, sp)) != NULL) {
  582. jupiter_debug(peer,
  583. "jupiter_receive", "gpos: %s", cp);
  584. break;
  585. }
  586. break;
  587. case JUPITER_O_ID:
  588. if (size != sizeof(struct jid)) {
  589. jupiter_debug(peer,
  590. "jupiter_receive", "id: len %d != %u",
  591. size, (int)sizeof(struct jid));
  592. refclock_report(peer, CEVNT_BADREPLY);
  593. break;
  594. }
  595. /*
  596. * If we got this message because the Jupiter
  597. * just powered instance, it needs to be reconfigured.
  598. */
  599. ip = (struct jid *)sp;
  600. jupiter_debug(peer,
  601. "jupiter_receive", "%s chan ver %s, %s (%s)",
  602. ip->chans, ip->vers, ip->date, ip->opts);
  603. msyslog(LOG_DEBUG,
  604. "jupiter_receive: %s chan ver %s, %s (%s)",
  605. ip->chans, ip->vers, ip->date, ip->opts);
  606. if (instance->wantid)
  607. instance->wantid = 0;
  608. else {
  609. jupiter_debug(peer,
  610. "jupiter_receive", "reset receiver");
  611. jupiter_config(instance);
  612. /*
  613. * Restore since jupiter_config() just
  614. * zeroed it
  615. */
  616. instance->ssize = cc;
  617. }
  618. break;
  619. default:
  620. jupiter_debug(peer,
  621. "jupiter_receive", "unknown message id %d",
  622. getshort(hp->id));
  623. break;
  624. }
  625. instance->ssize -= cc;
  626. if (instance->ssize < 0) {
  627. fprintf(stderr, "jupiter_recv: negative ssize!\n");
  628. abort();
  629. } else if (instance->ssize > 0)
  630. memcpy(instance->sbuf, (u_char *)instance->sbuf + cc, instance->ssize);
  631. }
  632. }
  633. static char *
  634. jupiter_parse_t(struct instance *instance, u_short *sp)
  635. {
  636. struct tm *tm;
  637. char *cp;
  638. struct jpulse *jp;
  639. u_int32 sweek;
  640. time_t last_timecode;
  641. u_short flags;
  642. jp = (struct jpulse *)sp;
  643. /* The timecode is presented as seconds into the current GPS week */
  644. sweek = DS2UI(jp->sweek) % WEEKSECS;
  645. /*
  646. * If we don't know the current GPS week, calculate it from the
  647. * current time. (It's too bad they didn't include this
  648. * important value in the pulse message). We'd like to pick it
  649. * up from one of the other messages like gpos or chan but they
  650. * don't appear to be synchronous with time keeping and changes
  651. * too soon (something like 10 seconds before the new GPS
  652. * week).
  653. *
  654. * If we already know the current GPS week, increment it when
  655. * we wrap into a new week.
  656. */
  657. if (instance->gweek == 0) {
  658. if (!instance->gpos_gweek) {
  659. return ("jupiter_parse_t: Unknown gweek");
  660. }
  661. instance->gweek = instance->gpos_gweek;
  662. /*
  663. * Fix warps. GPOS has GPS time and PULSE has UTC.
  664. * Plus, GPOS need not be completely in synch with
  665. * the PPS signal.
  666. */
  667. if (instance->gpos_sweek >= sweek) {
  668. if ((instance->gpos_sweek - sweek) > WEEKSECS / 2)
  669. ++instance->gweek;
  670. }
  671. else {
  672. if ((sweek - instance->gpos_sweek) > WEEKSECS / 2)
  673. --instance->gweek;
  674. }
  675. }
  676. else if (sweek == 0 && instance->lastsweek == WEEKSECS - 1) {
  677. ++instance->gweek;
  678. jupiter_debug(instance->peer,
  679. "jupiter_parse_t", "NEW gps week %u", instance->gweek);
  680. }
  681. /*
  682. * See if the sweek stayed the same (this happens when there is
  683. * no pps pulse).
  684. *
  685. * Otherwise, look for time warps:
  686. *
  687. * - we have stored at least one lastsweek and
  688. * - the sweek didn't increase by one and
  689. * - we didn't wrap to a new GPS week
  690. *
  691. * Then we warped.
  692. */
  693. if (instance->lastsweek == sweek)
  694. jupiter_debug(instance->peer,
  695. "jupiter_parse_t", "gps sweek not incrementing (%d)",
  696. sweek);
  697. else if (instance->lastsweek != 2 * WEEKSECS &&
  698. instance->lastsweek + 1 != sweek &&
  699. !(sweek == 0 && instance->lastsweek == WEEKSECS - 1))
  700. jupiter_debug(instance->peer,
  701. "jupiter_parse_t", "gps sweek jumped (was %d, now %d)",
  702. instance->lastsweek, sweek);
  703. instance->lastsweek = sweek;
  704. /* This timecode describes next pulse */
  705. last_timecode = instance->timecode;
  706. instance->timecode =
  707. GPS_EPOCH + (instance->gweek * WEEKSECS) + sweek;
  708. if (last_timecode == 0)
  709. /* XXX debugging */
  710. jupiter_debug(instance->peer,
  711. "jupiter_parse_t", "UTC <none> (gweek/sweek %u/%u)",
  712. instance->gweek, sweek);
  713. else {
  714. /* XXX debugging */
  715. tm = gmtime(&last_timecode);
  716. cp = asctime(tm);
  717. jupiter_debug(instance->peer,
  718. "jupiter_parse_t", "UTC %.24s (gweek/sweek %u/%u)",
  719. cp, instance->gweek, sweek);
  720. /* Billboard last_timecode (which is now the current time) */
  721. instance->peer->procptr->year = tm->tm_year + 1900;
  722. instance->peer->procptr->day = tm->tm_yday + 1;
  723. instance->peer->procptr->hour = tm->tm_hour;
  724. instance->peer->procptr->minute = tm->tm_min;
  725. instance->peer->procptr->second = tm->tm_sec;
  726. }
  727. flags = getshort(jp->flags);
  728. /* Toss if not designated "valid" by the gps */
  729. if ((flags & JUPITER_O_PULSE_VALID) == 0) {
  730. refclock_report(instance->peer, CEVNT_BADTIME);
  731. return ("time mark not valid");
  732. }
  733. /* We better be sync'ed to UTC... */
  734. if ((flags & JUPITER_O_PULSE_UTC) == 0) {
  735. refclock_report(instance->peer, CEVNT_BADTIME);
  736. return ("time mark not sync'ed to UTC");
  737. }
  738. return (NULL);
  739. }
  740. static char *
  741. jupiter_parse_gpos(struct instance *instance, u_short *sp)
  742. {
  743. struct jgpos *jg;
  744. time_t t;
  745. struct tm *tm;
  746. char *cp;
  747. jg = (struct jgpos *)sp;
  748. if (jg->navval != 0) {
  749. /*
  750. * Solution not valid. Use caution and refuse
  751. * to determine GPS week from this message.
  752. */
  753. instance->gpos_gweek = 0;
  754. instance->gpos_sweek = 0;
  755. return ("Navigation solution not valid");
  756. }
  757. instance->gpos_gweek = jg->gweek;
  758. instance->gpos_sweek = DS2UI(jg->sweek);
  759. while(instance->gpos_sweek >= WEEKSECS) {
  760. instance->gpos_sweek -= WEEKSECS;
  761. ++instance->gpos_gweek;
  762. }
  763. instance->gweek = 0;
  764. t = GPS_EPOCH + (instance->gpos_gweek * WEEKSECS) + instance->gpos_sweek;
  765. tm = gmtime(&t);
  766. cp = asctime(tm);
  767. jupiter_debug(instance->peer,
  768. "jupiter_parse_g", "GPS %.24s (gweek/sweek %u/%u)",
  769. cp, instance->gpos_gweek, instance->gpos_sweek);
  770. return (NULL);
  771. }
  772. /*
  773. * jupiter_debug - print debug messages
  774. */
  775. #if defined(__STDC__) || defined(SYS_WINNT)
  776. static void
  777. jupiter_debug(struct peer *peer, char *function, char *fmt, ...)
  778. #else
  779. static void
  780. jupiter_debug(peer, function, fmt, va_alist)
  781. struct peer *peer;
  782. char *function;
  783. char *fmt;
  784. #endif /* __STDC__ */
  785. {
  786. char buffer[200];
  787. va_list ap;
  788. #if defined(__STDC__) || defined(SYS_WINNT)
  789. va_start(ap, fmt);
  790. #else
  791. va_start(ap);
  792. #endif /* __STDC__ */
  793. /*
  794. * Print debug message to stdout
  795. * In the future, we may want to get get more creative...
  796. */
  797. vsnprintf(buffer, sizeof(buffer), fmt, ap);
  798. record_clock_stats(&(peer->srcadr), buffer);
  799. #ifdef DEBUG
  800. if (debug) {
  801. fprintf(stdout, "%s: ", function);
  802. fprintf(stdout, buffer);
  803. fprintf(stdout, "\n");
  804. fflush(stdout);
  805. }
  806. #endif
  807. va_end(ap);
  808. }
  809. /* Checksum and transmit a message to the Jupiter */
  810. static char *
  811. jupiter_send(struct instance *instance, struct jheader *hp)
  812. {
  813. u_int len, size;
  814. int cc;
  815. u_short *sp;
  816. static char errstr[132];
  817. size = sizeof(*hp);
  818. hp->hsum = putshort(jupiter_cksum((u_short *)hp,
  819. (size / sizeof(u_short)) - 1));
  820. len = getshort(hp->len);
  821. if (len > 0) {
  822. sp = (u_short *)(hp + 1);
  823. sp[len] = putshort(jupiter_cksum(sp, len));
  824. size += (len + 1) * sizeof(u_short);
  825. }
  826. if ((cc = write(instance->peer->procptr->io.fd, (char *)hp, size)) < 0) {
  827. (void)sprintf(errstr, "write: %s", strerror(errno));
  828. return (errstr);
  829. } else if (cc != size) {
  830. (void)sprintf(errstr, "short write (%d != %d)", cc, size);
  831. return (errstr);
  832. }
  833. return (NULL);
  834. }
  835. /* Request periodic message output */
  836. static struct {
  837. struct jheader jheader;
  838. struct jrequest jrequest;
  839. } reqmsg = {
  840. { putshort(JUPITER_SYNC), 0,
  841. putshort((sizeof(struct jrequest) / sizeof(u_short)) - 1),
  842. 0, JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK |
  843. JUPITER_FLAG_CONN | JUPITER_FLAG_LOG, 0 },
  844. { 0, 0, 0, 0 }
  845. };
  846. /* An interval of zero means to output on trigger */
  847. static void
  848. jupiter_reqmsg(struct instance *instance, u_int id,
  849. u_int interval)
  850. {
  851. struct jheader *hp;
  852. struct jrequest *rp;
  853. char *cp;
  854. hp = &reqmsg.jheader;
  855. hp->id = putshort(id);
  856. rp = &reqmsg.jrequest;
  857. rp->trigger = putshort(interval == 0);
  858. rp->interval = putshort(interval);
  859. if ((cp = jupiter_send(instance, hp)) != NULL)
  860. jupiter_debug(instance->peer, "jupiter_reqmsg", "%u: %s", id, cp);
  861. }
  862. /* Cancel periodic message output */
  863. static struct jheader canmsg = {
  864. putshort(JUPITER_SYNC), 0, 0, 0,
  865. JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_DISC,
  866. 0
  867. };
  868. static void
  869. jupiter_canmsg(struct instance *instance, u_int id)
  870. {
  871. struct jheader *hp;
  872. char *cp;
  873. hp = &canmsg;
  874. hp->id = putshort(id);
  875. if ((cp = jupiter_send(instance, hp)) != NULL)
  876. jupiter_debug(instance->peer, "jupiter_canmsg", "%u: %s", id, cp);
  877. }
  878. /* Request a single message output */
  879. static struct jheader reqonemsg = {
  880. putshort(JUPITER_SYNC), 0, 0, 0,
  881. JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_QUERY,
  882. 0
  883. };
  884. static void
  885. jupiter_reqonemsg(struct instance *instance, u_int id)
  886. {
  887. struct jheader *hp;
  888. char *cp;
  889. hp = &reqonemsg;
  890. hp->id = putshort(id);
  891. if ((cp = jupiter_send(instance, hp)) != NULL)
  892. jupiter_debug(instance->peer, "jupiter_reqonemsg", "%u: %s", id, cp);
  893. }
  894. /* Set the platform dynamics */
  895. static struct {
  896. struct jheader jheader;
  897. struct jplat jplat;
  898. } platmsg = {
  899. { putshort(JUPITER_SYNC), putshort(JUPITER_I_PLAT),
  900. putshort((sizeof(struct jplat) / sizeof(u_short)) - 1), 0,
  901. JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK, 0 },
  902. { 0, 0, 0 }
  903. };
  904. static void
  905. jupiter_platform(struct instance *instance, u_int platform)
  906. {
  907. struct jheader *hp;
  908. struct jplat *pp;
  909. char *cp;
  910. hp = &platmsg.jheader;
  911. pp = &platmsg.jplat;
  912. pp->platform = putshort(platform);
  913. if ((cp = jupiter_send(instance, hp)) != NULL)
  914. jupiter_debug(instance->peer, "jupiter_platform", "%u: %s", platform, cp);
  915. }
  916. /* Checksum "len" shorts */
  917. static u_short
  918. jupiter_cksum(u_short *sp, u_int len)
  919. {
  920. u_short sum, x;
  921. sum = 0;
  922. while (len-- > 0) {
  923. x = *sp++;
  924. sum += getshort(x);
  925. }
  926. return (~sum + 1);
  927. }
  928. /* Return the size of the next message (or zero if we don't have it all yet) */
  929. static int
  930. jupiter_recv(struct instance *instance)
  931. {
  932. int n, len, size, cc;
  933. struct jheader *hp;
  934. u_char *bp;
  935. u_short *sp;
  936. /* Must have at least a header's worth */
  937. cc = sizeof(*hp);
  938. size = instance->ssize;
  939. if (size < cc)
  940. return (0);
  941. /* Search for the sync short if missing */
  942. sp = instance->sbuf;
  943. hp = (struct jheader *)sp;
  944. if (getshort(hp->sync) != JUPITER_SYNC) {
  945. /* Wasn't at the front, sync up */
  946. jupiter_debug(instance->peer, "jupiter_recv", "syncing");
  947. bp = (u_char *)sp;
  948. n = size;
  949. while (n >= 2) {
  950. if (bp[0] != (JUPITER_SYNC & 0xff)) {
  951. /*
  952. jupiter_debug(instance->peer, "{0x%x}", bp[0]);
  953. */
  954. ++bp;
  955. --n;
  956. continue;
  957. }
  958. if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff))
  959. break;
  960. /*
  961. jupiter_debug(instance->peer, "{0x%x 0x%x}", bp[0], bp[1]);
  962. */
  963. bp += 2;
  964. n -= 2;
  965. }
  966. /*
  967. jupiter_debug(instance->peer, "\n");
  968. */
  969. /* Shuffle data to front of input buffer */
  970. if (n > 0)
  971. memcpy(sp, bp, n);
  972. size = n;
  973. instance->ssize = size;
  974. if (size < cc || hp->sync != JUPITER_SYNC)
  975. return (0);
  976. }
  977. if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) !=
  978. getshort(hp->hsum)) {
  979. jupiter_debug(instance->peer, "jupiter_recv", "bad header checksum!");
  980. /* This is drastic but checksum errors should be rare */
  981. instance->ssize = 0;
  982. return (0);
  983. }
  984. /* Check for a payload */
  985. len = getshort(hp->len);
  986. if (len > 0) {
  987. n = (len + 1) * sizeof(u_short);
  988. /* Not enough data yet */
  989. if (size < cc + n)
  990. return (0);
  991. /* Check payload checksum */
  992. sp = (u_short *)(hp + 1);
  993. if (jupiter_cksum(sp, len) != getshort(sp[len])) {
  994. jupiter_debug(instance->peer,
  995. "jupiter_recv", "bad payload checksum!");
  996. /* This is drastic but checksum errors should be rare */
  997. instance->ssize = 0;
  998. return (0);
  999. }
  1000. cc += n;
  1001. }
  1002. return (cc);
  1003. }
  1004. #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */
  1005. int refclock_jupiter_bs;
  1006. #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */