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