1 /* $NetBSD: refclock_jupiter.c,v 1.1.1.1 2009/12/13 16:55:44 kardel 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 *, char *, char *, ...) 144 __attribute__ ((format (printf, 3, 4))); 145 static char * jupiter_parse_t (struct instance *, u_short *); 146 static 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, "jupiter_start", "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, "jupiter_config", "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, "jupiter_config", 296 "mobile platform"); 297 298 instance->pollcnt = 2; 299 instance->polled = 0; 300 instance->gpos_gweek = 0; 301 instance->gpos_sweek = 0; 302 instance->gweek = 0; 303 instance->lastsweek = 2 * WEEKSECS; 304 instance->timecode = 0; 305 instance->stime = 0; 306 instance->ssize = 0; 307 308 /* Stop outputting all messages */ 309 jupiter_canmsg(instance, JUPITER_ALL); 310 311 /* Request the receiver id so we can syslog the firmware version */ 312 jupiter_reqonemsg(instance, JUPITER_O_ID); 313 314 /* Flag that this the id was requested (so we don't get called again) */ 315 instance->wantid = 1; 316 317 /* Request perodic time mark pulse messages */ 318 jupiter_reqmsg(instance, JUPITER_O_PULSE, 1); 319 320 /* Request perodic geodetic position status */ 321 jupiter_reqmsg(instance, JUPITER_O_GPOS, 1); 322 323 /* Set application platform type */ 324 if (instance->moving) 325 jupiter_platform(instance, JUPITER_I_PLAT_MED); 326 else 327 jupiter_platform(instance, JUPITER_I_PLAT_LOW); 328 329 return (1); 330 } 331 332 #ifdef HAVE_PPSAPI 333 /* 334 * Initialize PPSAPI 335 */ 336 int 337 jupiter_ppsapi( 338 struct instance *instance /* unit structure pointer */ 339 ) 340 { 341 int capability; 342 343 if (time_pps_getcap(instance->pps_handle, &capability) < 0) { 344 msyslog(LOG_ERR, 345 "refclock_jupiter: time_pps_getcap failed: %m"); 346 return (0); 347 } 348 memset(&instance->pps_params, 0, sizeof(pps_params_t)); 349 if (!instance->assert) 350 instance->pps_params.mode = capability & PPS_CAPTURECLEAR; 351 else 352 instance->pps_params.mode = capability & PPS_CAPTUREASSERT; 353 if (!(instance->pps_params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) { 354 msyslog(LOG_ERR, 355 "refclock_jupiter: invalid capture edge %d", 356 instance->assert); 357 return (0); 358 } 359 instance->pps_params.mode |= PPS_TSFMT_TSPEC; 360 if (time_pps_setparams(instance->pps_handle, &instance->pps_params) < 0) { 361 msyslog(LOG_ERR, 362 "refclock_jupiter: time_pps_setparams failed: %m"); 363 return (0); 364 } 365 if (instance->hardpps) { 366 if (time_pps_kcbind(instance->pps_handle, PPS_KC_HARDPPS, 367 instance->pps_params.mode & ~PPS_TSFMT_TSPEC, 368 PPS_TSFMT_TSPEC) < 0) { 369 msyslog(LOG_ERR, 370 "refclock_jupiter: time_pps_kcbind failed: %m"); 371 return (0); 372 } 373 pps_enable = 1; 374 } 375 /* instance->peer->precision = PPS_PRECISION; */ 376 377 #if DEBUG 378 if (debug) { 379 time_pps_getparams(instance->pps_handle, &instance->pps_params); 380 jupiter_debug(instance->peer, "refclock_jupiter", 381 "pps capability 0x%x version %d mode 0x%x kern %d", 382 capability, instance->pps_params.api_version, 383 instance->pps_params.mode, instance->hardpps); 384 } 385 #endif 386 387 return (1); 388 } 389 390 /* 391 * Get PPSAPI timestamps. 392 * 393 * Return 0 on failure and 1 on success. 394 */ 395 static int 396 jupiter_pps(struct instance *instance) 397 { 398 pps_info_t pps_info; 399 struct timespec timeout, ts; 400 double dtemp; 401 l_fp tstmp; 402 403 /* 404 * Convert the timespec nanoseconds field to ntp l_fp units. 405 */ 406 if (instance->pps_handle == 0) 407 return 1; 408 timeout.tv_sec = 0; 409 timeout.tv_nsec = 0; 410 memcpy(&pps_info, &instance->pps_info, sizeof(pps_info_t)); 411 if (time_pps_fetch(instance->pps_handle, PPS_TSFMT_TSPEC, &instance->pps_info, 412 &timeout) < 0) 413 return 1; 414 if (instance->pps_params.mode & PPS_CAPTUREASSERT) { 415 if (pps_info.assert_sequence == 416 instance->pps_info.assert_sequence) 417 return 1; 418 ts = instance->pps_info.assert_timestamp; 419 } else if (instance->pps_params.mode & PPS_CAPTURECLEAR) { 420 if (pps_info.clear_sequence == 421 instance->pps_info.clear_sequence) 422 return 1; 423 ts = instance->pps_info.clear_timestamp; 424 } else { 425 return 1; 426 } 427 if ((instance->ts.tv_sec == ts.tv_sec) && (instance->ts.tv_nsec == ts.tv_nsec)) 428 return 1; 429 instance->ts = ts; 430 431 tstmp.l_ui = ts.tv_sec + JAN_1970; 432 dtemp = ts.tv_nsec * FRAC / 1e9; 433 tstmp.l_uf = (u_int32)dtemp; 434 instance->peer->procptr->lastrec = tstmp; 435 return 0; 436 } 437 #endif /* HAVE_PPSAPI */ 438 439 /* 440 * jupiter_poll - jupiter watchdog routine 441 */ 442 static void 443 jupiter_poll(int unit, struct peer *peer) 444 { 445 struct instance *instance; 446 struct refclockproc *pp; 447 448 pp = peer->procptr; 449 instance = (struct instance *)pp->unitptr; 450 451 /* 452 * You don't need to poll this clock. It puts out timecodes 453 * once per second. If asked for a timestamp, take note. 454 * The next time a timecode comes in, it will be fed back. 455 */ 456 457 /* 458 * If we haven't had a response in a while, reset the receiver. 459 */ 460 if (instance->pollcnt > 0) { 461 instance->pollcnt--; 462 } else { 463 refclock_report(peer, CEVNT_TIMEOUT); 464 465 /* Request the receiver id to trigger a reconfig */ 466 jupiter_reqonemsg(instance, JUPITER_O_ID); 467 instance->wantid = 0; 468 } 469 470 /* 471 * polled every 64 seconds. Ask jupiter_receive to hand in 472 * a timestamp. 473 */ 474 instance->polled = 1; 475 pp->polls++; 476 } 477 478 /* 479 * jupiter_control - fudge control 480 */ 481 static void 482 jupiter_control( 483 int unit, /* unit (not used) */ 484 struct refclockstat *in, /* input parameters (not used) */ 485 struct refclockstat *out, /* output parameters (not used) */ 486 struct peer *peer /* peer structure pointer */ 487 ) 488 { 489 struct refclockproc *pp; 490 struct instance *instance; 491 u_char sloppyclockflag; 492 493 pp = peer->procptr; 494 instance = (struct instance *)pp->unitptr; 495 496 DTOLFP(pp->fudgetime2, &instance->limit); 497 /* Force positive value. */ 498 if (L_ISNEG(&instance->limit)) 499 L_NEG(&instance->limit); 500 501 #ifdef HAVE_PPSAPI 502 instance->assert = !(pp->sloppyclockflag & CLK_FLAG3); 503 jupiter_ppsapi(instance); 504 #endif /* HAVE_PPSAPI */ 505 506 sloppyclockflag = instance->sloppyclockflag; 507 instance->sloppyclockflag = pp->sloppyclockflag; 508 if ((instance->sloppyclockflag & CLK_FLAG2) != 509 (sloppyclockflag & CLK_FLAG2)) { 510 jupiter_debug(peer, 511 "jupiter_control", 512 "mode switch: reset receiver"); 513 jupiter_config(instance); 514 return; 515 } 516 } 517 518 /* 519 * jupiter_receive - receive gps data 520 * Gag me! 521 */ 522 static void 523 jupiter_receive(struct recvbuf *rbufp) 524 { 525 int bpcnt, cc, size, ppsret; 526 time_t last_timecode; 527 u_int32 laststime; 528 char *cp; 529 u_char *bp; 530 u_short *sp; 531 struct jid *ip; 532 struct jheader *hp; 533 struct peer *peer; 534 struct refclockproc *pp; 535 struct instance *instance; 536 l_fp tstamp; 537 538 /* Initialize pointers and read the timecode and timestamp */ 539 peer = (struct peer *)rbufp->recv_srcclock; 540 pp = peer->procptr; 541 instance = (struct instance *)pp->unitptr; 542 543 bp = (u_char *)rbufp->recv_buffer; 544 bpcnt = rbufp->recv_length; 545 546 /* This shouldn't happen */ 547 if (bpcnt > sizeof(instance->sbuf) - instance->ssize) 548 bpcnt = sizeof(instance->sbuf) - instance->ssize; 549 550 /* Append to input buffer */ 551 memcpy((u_char *)instance->sbuf + instance->ssize, bp, bpcnt); 552 instance->ssize += bpcnt; 553 554 /* While there's at least a header and we parse an intact message */ 555 while (instance->ssize > sizeof(*hp) && (cc = jupiter_recv(instance)) > 0) { 556 instance->pollcnt = 2; 557 558 tstamp = rbufp->recv_time; 559 hp = (struct jheader *)instance->sbuf; 560 sp = (u_short *)(hp + 1); 561 size = cc - sizeof(*hp); 562 switch (getshort(hp->id)) { 563 564 case JUPITER_O_PULSE: 565 if (size != sizeof(struct jpulse)) { 566 jupiter_debug(peer, 567 "jupiter_receive", "pulse: len %d != %u", 568 size, (int)sizeof(struct jpulse)); 569 refclock_report(peer, CEVNT_BADREPLY); 570 break; 571 } 572 573 /* 574 * There appears to be a firmware bug related 575 * to the pulse message; in addition to the one 576 * per second messages, we get an extra pulse 577 * message once an hour (on the anniversary of 578 * the cold start). It seems to come 200 ms 579 * after the one requested. So if we've seen a 580 * pulse message in the last 210 ms, we skip 581 * this one. 582 */ 583 laststime = instance->stime; 584 instance->stime = DS2UI(((struct jpulse *)sp)->stime); 585 if (laststime != 0 && instance->stime - laststime <= 21) { 586 jupiter_debug(peer, "jupiter_receive", 587 "avoided firmware bug (stime %.2f, laststime %.2f)", 588 (double)instance->stime * 0.01, (double)laststime * 0.01); 589 break; 590 } 591 592 /* Retrieve pps timestamp */ 593 ppsret = jupiter_pps(instance); 594 595 /* 596 * Add one second if msg received early 597 * (i.e. before limit, a.k.a. fudgetime2) in 598 * the second. 599 */ 600 L_SUB(&tstamp, &pp->lastrec); 601 if (!L_ISGEQ(&tstamp, &instance->limit)) 602 ++pp->lastrec.l_ui; 603 604 /* Parse timecode (even when there's no pps) */ 605 last_timecode = instance->timecode; 606 if ((cp = jupiter_parse_t(instance, sp)) != NULL) { 607 jupiter_debug(peer, 608 "jupiter_receive", "pulse: %s", cp); 609 break; 610 } 611 612 /* Bail if we didn't get a pps timestamp */ 613 if (ppsret) 614 break; 615 616 /* Bail if we don't have the last timecode yet */ 617 if (last_timecode == 0) 618 break; 619 620 /* Add the new sample to a median filter */ 621 tstamp.l_ui = JAN_1970 + last_timecode; 622 tstamp.l_uf = 0; 623 624 refclock_process_offset(pp, tstamp, pp->lastrec, pp->fudgetime1); 625 626 /* 627 * The clock will blurt a timecode every second 628 * but we only want one when polled. If we 629 * havn't been polled, bail out. 630 */ 631 if (!instance->polled) 632 break; 633 instance->polled = 0; 634 635 /* 636 * It's a live one! Remember this time. 637 */ 638 639 pp->lastref = pp->lastrec; 640 refclock_receive(peer); 641 642 /* 643 * If we get here - what we got from the clock is 644 * OK, so say so 645 */ 646 refclock_report(peer, CEVNT_NOMINAL); 647 648 /* 649 * We have succeeded in answering the poll. 650 * Turn off the flag and return 651 */ 652 instance->polled = 0; 653 break; 654 655 case JUPITER_O_GPOS: 656 if (size != sizeof(struct jgpos)) { 657 jupiter_debug(peer, 658 "jupiter_receive", "gpos: len %d != %u", 659 size, (int)sizeof(struct jgpos)); 660 refclock_report(peer, CEVNT_BADREPLY); 661 break; 662 } 663 664 if ((cp = jupiter_parse_gpos(instance, sp)) != NULL) { 665 jupiter_debug(peer, 666 "jupiter_receive", "gpos: %s", cp); 667 break; 668 } 669 break; 670 671 case JUPITER_O_ID: 672 if (size != sizeof(struct jid)) { 673 jupiter_debug(peer, 674 "jupiter_receive", "id: len %d != %u", 675 size, (int)sizeof(struct jid)); 676 refclock_report(peer, CEVNT_BADREPLY); 677 break; 678 } 679 /* 680 * If we got this message because the Jupiter 681 * just powered instance, it needs to be reconfigured. 682 */ 683 ip = (struct jid *)sp; 684 jupiter_debug(peer, 685 "jupiter_receive", "%s chan ver %s, %s (%s)", 686 ip->chans, ip->vers, ip->date, ip->opts); 687 msyslog(LOG_DEBUG, 688 "jupiter_receive: %s chan ver %s, %s (%s)", 689 ip->chans, ip->vers, ip->date, ip->opts); 690 if (instance->wantid) 691 instance->wantid = 0; 692 else { 693 jupiter_debug(peer, 694 "jupiter_receive", "reset receiver"); 695 jupiter_config(instance); 696 /* 697 * Restore since jupiter_config() just 698 * zeroed it 699 */ 700 instance->ssize = cc; 701 } 702 break; 703 704 default: 705 jupiter_debug(peer, 706 "jupiter_receive", "unknown message id %d", 707 getshort(hp->id)); 708 break; 709 } 710 instance->ssize -= cc; 711 if (instance->ssize < 0) { 712 fprintf(stderr, "jupiter_recv: negative ssize!\n"); 713 abort(); 714 } else if (instance->ssize > 0) 715 memcpy(instance->sbuf, (u_char *)instance->sbuf + cc, instance->ssize); 716 } 717 } 718 719 static char * 720 jupiter_parse_t(struct instance *instance, u_short *sp) 721 { 722 struct tm *tm; 723 char *cp; 724 struct jpulse *jp; 725 u_int32 sweek; 726 time_t last_timecode; 727 u_short flags; 728 729 jp = (struct jpulse *)sp; 730 731 /* The timecode is presented as seconds into the current GPS week */ 732 sweek = DS2UI(jp->sweek) % WEEKSECS; 733 734 /* 735 * If we don't know the current GPS week, calculate it from the 736 * current time. (It's too bad they didn't include this 737 * important value in the pulse message). We'd like to pick it 738 * up from one of the other messages like gpos or chan but they 739 * don't appear to be synchronous with time keeping and changes 740 * too soon (something like 10 seconds before the new GPS 741 * week). 742 * 743 * If we already know the current GPS week, increment it when 744 * we wrap into a new week. 745 */ 746 if (instance->gweek == 0) { 747 if (!instance->gpos_gweek) { 748 return ("jupiter_parse_t: Unknown gweek"); 749 } 750 751 instance->gweek = instance->gpos_gweek; 752 753 /* 754 * Fix warps. GPOS has GPS time and PULSE has UTC. 755 * Plus, GPOS need not be completely in synch with 756 * the PPS signal. 757 */ 758 if (instance->gpos_sweek >= sweek) { 759 if ((instance->gpos_sweek - sweek) > WEEKSECS / 2) 760 ++instance->gweek; 761 } 762 else { 763 if ((sweek - instance->gpos_sweek) > WEEKSECS / 2) 764 --instance->gweek; 765 } 766 } 767 else if (sweek == 0 && instance->lastsweek == WEEKSECS - 1) { 768 ++instance->gweek; 769 jupiter_debug(instance->peer, 770 "jupiter_parse_t", "NEW gps week %u", instance->gweek); 771 } 772 773 /* 774 * See if the sweek stayed the same (this happens when there is 775 * no pps pulse). 776 * 777 * Otherwise, look for time warps: 778 * 779 * - we have stored at least one lastsweek and 780 * - the sweek didn't increase by one and 781 * - we didn't wrap to a new GPS week 782 * 783 * Then we warped. 784 */ 785 if (instance->lastsweek == sweek) 786 jupiter_debug(instance->peer, 787 "jupiter_parse_t", "gps sweek not incrementing (%d)", 788 sweek); 789 else if (instance->lastsweek != 2 * WEEKSECS && 790 instance->lastsweek + 1 != sweek && 791 !(sweek == 0 && instance->lastsweek == WEEKSECS - 1)) 792 jupiter_debug(instance->peer, 793 "jupiter_parse_t", "gps sweek jumped (was %d, now %d)", 794 instance->lastsweek, sweek); 795 instance->lastsweek = sweek; 796 797 /* This timecode describes next pulse */ 798 last_timecode = instance->timecode; 799 instance->timecode = 800 GPS_EPOCH + (instance->gweek * WEEKSECS) + sweek; 801 802 if (last_timecode == 0) 803 /* XXX debugging */ 804 jupiter_debug(instance->peer, 805 "jupiter_parse_t", "UTC <none> (gweek/sweek %u/%u)", 806 instance->gweek, sweek); 807 else { 808 /* XXX debugging */ 809 tm = gmtime(&last_timecode); 810 cp = asctime(tm); 811 812 jupiter_debug(instance->peer, 813 "jupiter_parse_t", "UTC %.24s (gweek/sweek %u/%u)", 814 cp, instance->gweek, sweek); 815 816 /* Billboard last_timecode (which is now the current time) */ 817 instance->peer->procptr->year = tm->tm_year + 1900; 818 instance->peer->procptr->day = tm->tm_yday + 1; 819 instance->peer->procptr->hour = tm->tm_hour; 820 instance->peer->procptr->minute = tm->tm_min; 821 instance->peer->procptr->second = tm->tm_sec; 822 } 823 824 flags = getshort(jp->flags); 825 826 /* Toss if not designated "valid" by the gps */ 827 if ((flags & JUPITER_O_PULSE_VALID) == 0) { 828 refclock_report(instance->peer, CEVNT_BADTIME); 829 return ("time mark not valid"); 830 } 831 832 /* We better be sync'ed to UTC... */ 833 if ((flags & JUPITER_O_PULSE_UTC) == 0) { 834 refclock_report(instance->peer, CEVNT_BADTIME); 835 return ("time mark not sync'ed to UTC"); 836 } 837 838 return (NULL); 839 } 840 841 static char * 842 jupiter_parse_gpos(struct instance *instance, u_short *sp) 843 { 844 struct jgpos *jg; 845 time_t t; 846 struct tm *tm; 847 char *cp; 848 849 jg = (struct jgpos *)sp; 850 851 if (jg->navval != 0) { 852 /* 853 * Solution not valid. Use caution and refuse 854 * to determine GPS week from this message. 855 */ 856 instance->gpos_gweek = 0; 857 instance->gpos_sweek = 0; 858 return ("Navigation solution not valid"); 859 } 860 861 instance->gpos_gweek = jg->gweek; 862 instance->gpos_sweek = DS2UI(jg->sweek); 863 while(instance->gpos_sweek >= WEEKSECS) { 864 instance->gpos_sweek -= WEEKSECS; 865 ++instance->gpos_gweek; 866 } 867 instance->gweek = 0; 868 869 t = GPS_EPOCH + (instance->gpos_gweek * WEEKSECS) + instance->gpos_sweek; 870 tm = gmtime(&t); 871 cp = asctime(tm); 872 873 jupiter_debug(instance->peer, 874 "jupiter_parse_g", "GPS %.24s (gweek/sweek %u/%u)", 875 cp, instance->gpos_gweek, instance->gpos_sweek); 876 return (NULL); 877 } 878 879 /* 880 * jupiter_debug - print debug messages 881 */ 882 #if defined(__STDC__) || defined(SYS_WINNT) 883 static void 884 jupiter_debug(struct peer *peer, char *function, char *fmt, ...) 885 #else 886 static void 887 jupiter_debug(peer, function, fmt, va_alist) 888 struct peer *peer; 889 char *function; 890 char *fmt; 891 #endif /* __STDC__ */ 892 { 893 char buffer[200]; 894 va_list ap; 895 896 #if defined(__STDC__) || defined(SYS_WINNT) 897 va_start(ap, fmt); 898 #else 899 va_start(ap); 900 #endif /* __STDC__ */ 901 /* 902 * Print debug message to stdout 903 * In the future, we may want to get get more creative... 904 */ 905 vsnprintf(buffer, sizeof(buffer), fmt, ap); 906 record_clock_stats(&(peer->srcadr), buffer); 907 #ifdef DEBUG 908 if (debug) { 909 fprintf(stdout, "%s: ", function); 910 fprintf(stdout, buffer); 911 fprintf(stdout, "\n"); 912 fflush(stdout); 913 } 914 #endif 915 916 va_end(ap); 917 } 918 919 /* Checksum and transmit a message to the Jupiter */ 920 static char * 921 jupiter_send(struct instance *instance, struct jheader *hp) 922 { 923 u_int len, size; 924 int cc; 925 u_short *sp; 926 static char errstr[132]; 927 928 size = sizeof(*hp); 929 hp->hsum = putshort(jupiter_cksum((u_short *)hp, 930 (size / sizeof(u_short)) - 1)); 931 len = getshort(hp->len); 932 if (len > 0) { 933 sp = (u_short *)(hp + 1); 934 sp[len] = putshort(jupiter_cksum(sp, len)); 935 size += (len + 1) * sizeof(u_short); 936 } 937 938 if ((cc = write(instance->peer->procptr->io.fd, (char *)hp, size)) < 0) { 939 (void)sprintf(errstr, "write: %s", strerror(errno)); 940 return (errstr); 941 } else if (cc != size) { 942 (void)sprintf(errstr, "short write (%d != %d)", cc, size); 943 return (errstr); 944 } 945 return (NULL); 946 } 947 948 /* Request periodic message output */ 949 static struct { 950 struct jheader jheader; 951 struct jrequest jrequest; 952 } reqmsg = { 953 { putshort(JUPITER_SYNC), 0, 954 putshort((sizeof(struct jrequest) / sizeof(u_short)) - 1), 955 0, JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | 956 JUPITER_FLAG_CONN | JUPITER_FLAG_LOG, 0 }, 957 { 0, 0, 0, 0 } 958 }; 959 960 /* An interval of zero means to output on trigger */ 961 static void 962 jupiter_reqmsg(struct instance *instance, u_int id, 963 u_int interval) 964 { 965 struct jheader *hp; 966 struct jrequest *rp; 967 char *cp; 968 969 hp = &reqmsg.jheader; 970 hp->id = putshort(id); 971 rp = &reqmsg.jrequest; 972 rp->trigger = putshort(interval == 0); 973 rp->interval = putshort(interval); 974 if ((cp = jupiter_send(instance, hp)) != NULL) 975 jupiter_debug(instance->peer, "jupiter_reqmsg", "%u: %s", id, cp); 976 } 977 978 /* Cancel periodic message output */ 979 static struct jheader canmsg = { 980 putshort(JUPITER_SYNC), 0, 0, 0, 981 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_DISC, 982 0 983 }; 984 985 static void 986 jupiter_canmsg(struct instance *instance, u_int id) 987 { 988 struct jheader *hp; 989 char *cp; 990 991 hp = &canmsg; 992 hp->id = putshort(id); 993 if ((cp = jupiter_send(instance, hp)) != NULL) 994 jupiter_debug(instance->peer, "jupiter_canmsg", "%u: %s", id, cp); 995 } 996 997 /* Request a single message output */ 998 static struct jheader reqonemsg = { 999 putshort(JUPITER_SYNC), 0, 0, 0, 1000 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_QUERY, 1001 0 1002 }; 1003 1004 static void 1005 jupiter_reqonemsg(struct instance *instance, u_int id) 1006 { 1007 struct jheader *hp; 1008 char *cp; 1009 1010 hp = &reqonemsg; 1011 hp->id = putshort(id); 1012 if ((cp = jupiter_send(instance, hp)) != NULL) 1013 jupiter_debug(instance->peer, "jupiter_reqonemsg", "%u: %s", id, cp); 1014 } 1015 1016 /* Set the platform dynamics */ 1017 static struct { 1018 struct jheader jheader; 1019 struct jplat jplat; 1020 } platmsg = { 1021 { putshort(JUPITER_SYNC), putshort(JUPITER_I_PLAT), 1022 putshort((sizeof(struct jplat) / sizeof(u_short)) - 1), 0, 1023 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK, 0 }, 1024 { 0, 0, 0 } 1025 }; 1026 1027 static void 1028 jupiter_platform(struct instance *instance, u_int platform) 1029 { 1030 struct jheader *hp; 1031 struct jplat *pp; 1032 char *cp; 1033 1034 hp = &platmsg.jheader; 1035 pp = &platmsg.jplat; 1036 pp->platform = putshort(platform); 1037 if ((cp = jupiter_send(instance, hp)) != NULL) 1038 jupiter_debug(instance->peer, "jupiter_platform", "%u: %s", platform, cp); 1039 } 1040 1041 /* Checksum "len" shorts */ 1042 static u_short 1043 jupiter_cksum(u_short *sp, u_int len) 1044 { 1045 u_short sum, x; 1046 1047 sum = 0; 1048 while (len-- > 0) { 1049 x = *sp++; 1050 sum += getshort(x); 1051 } 1052 return (~sum + 1); 1053 } 1054 1055 /* Return the size of the next message (or zero if we don't have it all yet) */ 1056 static int 1057 jupiter_recv(struct instance *instance) 1058 { 1059 int n, len, size, cc; 1060 struct jheader *hp; 1061 u_char *bp; 1062 u_short *sp; 1063 1064 /* Must have at least a header's worth */ 1065 cc = sizeof(*hp); 1066 size = instance->ssize; 1067 if (size < cc) 1068 return (0); 1069 1070 /* Search for the sync short if missing */ 1071 sp = instance->sbuf; 1072 hp = (struct jheader *)sp; 1073 if (getshort(hp->sync) != JUPITER_SYNC) { 1074 /* Wasn't at the front, sync up */ 1075 jupiter_debug(instance->peer, "jupiter_recv", "syncing"); 1076 bp = (u_char *)sp; 1077 n = size; 1078 while (n >= 2) { 1079 if (bp[0] != (JUPITER_SYNC & 0xff)) { 1080 /* 1081 jupiter_debug(instance->peer, "{0x%x}", bp[0]); 1082 */ 1083 ++bp; 1084 --n; 1085 continue; 1086 } 1087 if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff)) 1088 break; 1089 /* 1090 jupiter_debug(instance->peer, "{0x%x 0x%x}", bp[0], bp[1]); 1091 */ 1092 bp += 2; 1093 n -= 2; 1094 } 1095 /* 1096 jupiter_debug(instance->peer, "\n"); 1097 */ 1098 /* Shuffle data to front of input buffer */ 1099 if (n > 0) 1100 memcpy(sp, bp, n); 1101 size = n; 1102 instance->ssize = size; 1103 if (size < cc || hp->sync != JUPITER_SYNC) 1104 return (0); 1105 } 1106 1107 if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) != 1108 getshort(hp->hsum)) { 1109 jupiter_debug(instance->peer, "jupiter_recv", "bad header checksum!"); 1110 /* This is drastic but checksum errors should be rare */ 1111 instance->ssize = 0; 1112 return (0); 1113 } 1114 1115 /* Check for a payload */ 1116 len = getshort(hp->len); 1117 if (len > 0) { 1118 n = (len + 1) * sizeof(u_short); 1119 /* Not enough data yet */ 1120 if (size < cc + n) 1121 return (0); 1122 1123 /* Check payload checksum */ 1124 sp = (u_short *)(hp + 1); 1125 if (jupiter_cksum(sp, len) != getshort(sp[len])) { 1126 jupiter_debug(instance->peer, 1127 "jupiter_recv", "bad payload checksum!"); 1128 /* This is drastic but checksum errors should be rare */ 1129 instance->ssize = 0; 1130 return (0); 1131 } 1132 cc += n; 1133 } 1134 return (cc); 1135 } 1136 1137 #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */ 1138 int refclock_jupiter_bs; 1139 #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */ 1140