Obfuscate RCS ID matching so that CVS doesn't expand it.
[netbsd-mini2440.git] / dist / ntp / ntpd / refclock_jupiter.c
blob503df33af5a23c264d2b55f97ae4abc16ba0f37b
1 /* $NetBSD: refclock_jupiter.c,v 1.3 2006/06/11 19:34:12 kardel Exp $ */
3 /*
4 * Copyright (c) 1997, 1998, 2003
5 * The Regents of the University of California. All rights reserved.
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.
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.
36 #ifdef HAVE_CONFIG_H
37 # include <config.h>
38 #endif
40 #if defined(REFCLOCK) && defined(CLOCK_JUPITER) && defined(HAVE_PPSAPI)
42 #include "ntpd.h"
43 #include "ntp_io.h"
44 #include "ntp_refclock.h"
45 #include "ntp_unixtime.h"
46 #include "ntp_stdlib.h"
48 #include <stdio.h>
49 #include <ctype.h>
51 #include "jupiter.h"
53 #ifdef HAVE_PPSAPI
54 # include "ppsapi_timepps.h"
55 #endif
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
65 /* XXX */
66 #ifdef sun
67 char *strerror(int);
68 #endif
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.
78 * It may work (with minor modifications) with other Rockwell GPS
79 * receivers such as the CityTracker.
83 * GPS Definitions
85 #define DEVICE "/dev/gps%d" /* device name and unit */
86 #define SPEED232 B9600 /* baud */
89 * Radio interface parameters
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) */
96 /* Unix timestamp for the GPS epoch: January 6, 1980 */
97 #define GPS_EPOCH 315964800
99 /* Double short to unsigned int */
100 #define DS2UI(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
102 /* Double short to signed int */
103 #define DS2I(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
105 /* One week's worth of seconds */
106 #define WEEKSECS (7 * 24 * 60 * 60)
109 * Jupiter unit control structure.
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 */
138 * Function prototypes
140 static void jupiter_canmsg P((struct instance *, u_int));
141 static u_short jupiter_cksum P((u_short *, u_int));
142 static int jupiter_config P((struct instance *));
143 static void jupiter_debug P((struct peer *, char *, char *, ...))
144 __attribute__ ((format (printf, 3, 4)));
145 static char * jupiter_parse_t P((struct instance *, u_short *));
146 static char * jupiter_parse_gpos P((struct instance *, u_short *));
147 static void jupiter_platform P((struct instance *, u_int));
148 static void jupiter_poll P((int, struct peer *));
149 static void jupiter_control P((int, struct refclockstat *, struct
150 refclockstat *, struct peer *));
151 #ifdef HAVE_PPSAPI
152 static int jupiter_ppsapi P((struct instance *));
153 static int jupiter_pps P((struct instance *));
154 #endif /* HAVE_PPSAPI */
155 static int jupiter_recv P((struct instance *));
156 static void jupiter_receive P((struct recvbuf *rbufp));
157 static void jupiter_reqmsg P((struct instance *, u_int, u_int));
158 static void jupiter_reqonemsg P((struct instance *, u_int));
159 static char * jupiter_send P((struct instance *, struct jheader *));
160 static void jupiter_shutdown P((int, struct peer *));
161 static int jupiter_start P((int, struct peer *));
164 * Transfer vector
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 */
177 * jupiter_start - open the devices and initialize data for processing
179 static int
180 jupiter_start(
181 int unit,
182 struct peer *peer
185 struct refclockproc *pp;
186 struct instance *instance;
187 int fd = -1;
188 char gpsdev[20];
191 * Open serial port
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);
201 /* Allocate unit structure */
202 if ((instance = (struct instance *)
203 emalloc(sizeof(struct instance))) == NULL) {
204 (void) close(fd);
205 return (0);
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);
219 pp->unitptr = (caddr_t)instance;
222 * Initialize miscellaneous variables
224 peer->precision = PRECISION;
225 pp->clockdesc = DESCRIPTION;
226 memcpy((char *)&pp->refid, REFID, 4);
228 #ifdef HAVE_PPSAPI
229 instance->assert = 1;
230 instance->hardpps = 0;
232 * Start the PPSAPI interface if it is there. Default to use
233 * the assert edge and do not enable the kernel hardpps.
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");
240 else if (!jupiter_ppsapi(instance))
241 goto clean_up;
242 #endif /* HAVE_PPSAPI */
244 /* Ensure the receiver is properly configured */
245 if (!jupiter_config(instance))
246 goto clean_up;
248 return (1);
250 clean_up:
251 jupiter_shutdown(unit, peer);
252 pp->unitptr = 0;
253 return (0);
257 * jupiter_shutdown - shut down the clock
259 static void
260 jupiter_shutdown(int unit, struct peer *peer)
262 struct instance *instance;
263 struct refclockproc *pp;
265 pp = peer->procptr;
266 instance = (struct instance *)pp->unitptr;
267 if (!instance)
268 return;
270 #ifdef HAVE_PPSAPI
271 if (instance->pps_handle) {
272 time_pps_destroy(instance->pps_handle);
273 instance->pps_handle = 0;
275 #endif /* HAVE_PPSAPI */
277 io_closeclock(&pp->io);
278 free(instance);
282 * jupiter_config - Configure the receiver
284 static int
285 jupiter_config(struct instance *instance)
287 jupiter_debug(instance->peer, "jupiter_config", "init receiver");
290 * Initialize the unit variables
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");
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;
308 /* Stop outputting all messages */
309 jupiter_canmsg(instance, JUPITER_ALL);
311 /* Request the receiver id so we can syslog the firmware version */
312 jupiter_reqonemsg(instance, JUPITER_O_ID);
314 /* Flag that this the id was requested (so we don't get called again) */
315 instance->wantid = 1;
317 /* Request perodic time mark pulse messages */
318 jupiter_reqmsg(instance, JUPITER_O_PULSE, 1);
320 /* Request perodic geodetic position status */
321 jupiter_reqmsg(instance, JUPITER_O_GPOS, 1);
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);
329 return (1);
332 #ifdef HAVE_PPSAPI
334 * Initialize PPSAPI
337 jupiter_ppsapi(
338 struct instance *instance /* unit structure pointer */
341 int capability;
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);
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);
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);
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);
373 pps_enable = 1;
375 /* instance->peer->precision = PPS_PRECISION; */
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);
385 #endif
387 return (1);
391 * Get PPSAPI timestamps.
393 * Return 0 on failure and 1 on success.
395 static int
396 jupiter_pps(struct instance *instance)
398 pps_info_t pps_info;
399 struct timespec timeout, ts;
400 double dtemp;
401 l_fp tstmp;
404 * Convert the timespec nanoseconds field to ntp l_fp units.
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;
427 if ((instance->ts.tv_sec == ts.tv_sec) && (instance->ts.tv_nsec == ts.tv_nsec))
428 return 1;
429 instance->ts = ts;
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;
437 #endif /* HAVE_PPSAPI */
440 * jupiter_poll - jupiter watchdog routine
442 static void
443 jupiter_poll(int unit, struct peer *peer)
445 struct instance *instance;
446 struct refclockproc *pp;
448 pp = peer->procptr;
449 instance = (struct instance *)pp->unitptr;
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.
458 * If we haven't had a response in a while, reset the receiver.
460 if (instance->pollcnt > 0) {
461 instance->pollcnt--;
462 } else {
463 refclock_report(peer, CEVNT_TIMEOUT);
465 /* Request the receiver id to trigger a reconfig */
466 jupiter_reqonemsg(instance, JUPITER_O_ID);
467 instance->wantid = 0;
471 * polled every 64 seconds. Ask jupiter_receive to hand in
472 * a timestamp.
474 instance->polled = 1;
475 pp->polls++;
479 * jupiter_control - fudge control
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 */
489 struct refclockproc *pp;
490 struct instance *instance;
491 u_char sloppyclockflag;
493 pp = peer->procptr;
494 instance = (struct instance *)pp->unitptr;
496 DTOLFP(pp->fudgetime2, &instance->limit);
497 /* Force positive value. */
498 if (L_ISNEG(&instance->limit))
499 L_NEG(&instance->limit);
501 #ifdef HAVE_PPSAPI
502 instance->assert = !(pp->sloppyclockflag & CLK_FLAG3);
503 jupiter_ppsapi(instance);
504 #endif /* HAVE_PPSAPI */
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;
519 * jupiter_receive - receive gps data
520 * Gag me!
522 static void
523 jupiter_receive(struct recvbuf *rbufp)
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;
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;
543 bp = (u_char *)rbufp->recv_buffer;
544 bpcnt = rbufp->recv_length;
546 /* This shouldn't happen */
547 if (bpcnt > sizeof(instance->sbuf) - instance->ssize)
548 bpcnt = sizeof(instance->sbuf) - instance->ssize;
550 /* Append to input buffer */
551 memcpy((u_char *)instance->sbuf + instance->ssize, bp, bpcnt);
552 instance->ssize += bpcnt;
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;
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)) {
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;
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.
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;
592 /* Retrieve pps timestamp */
593 ppsret = jupiter_pps(instance);
596 * Add one second if msg received early
597 * (i.e. before limit, a.k.a. fudgetime2) in
598 * the second.
600 L_SUB(&tstamp, &pp->lastrec);
601 if (!L_ISGEQ(&tstamp, &instance->limit))
602 ++pp->lastrec.l_ui;
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;
612 /* Bail if we didn't get a pps timestamp */
613 if (ppsret)
614 break;
616 /* Bail if we don't have the last timecode yet */
617 if (last_timecode == 0)
618 break;
620 /* Add the new sample to a median filter */
621 tstamp.l_ui = JAN_1970 + last_timecode;
622 tstamp.l_uf = 0;
624 refclock_process_offset(pp, tstamp, pp->lastrec, pp->fudgetime1);
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.
631 if (!instance->polled)
632 break;
633 instance->polled = 0;
636 * It's a live one! Remember this time.
639 pp->lastref = pp->lastrec;
640 refclock_receive(peer);
643 * If we get here - what we got from the clock is
644 * OK, so say so
646 refclock_report(peer, CEVNT_NOMINAL);
649 * We have succeeded in answering the poll.
650 * Turn off the flag and return
652 instance->polled = 0;
653 break;
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;
664 if ((cp = jupiter_parse_gpos(instance, sp)) != NULL) {
665 jupiter_debug(peer,
666 "jupiter_receive", "gpos: %s", cp);
667 break;
669 break;
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;
680 * If we got this message because the Jupiter
681 * just powered instance, it needs to be reconfigured.
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);
697 * Restore since jupiter_config() just
698 * zeroed it
700 instance->ssize = cc;
702 break;
704 default:
705 jupiter_debug(peer,
706 "jupiter_receive", "unknown message id %d",
707 getshort(hp->id));
708 break;
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);
719 static char *
720 jupiter_parse_t(struct instance *instance, u_short *sp)
722 struct tm *tm;
723 char *cp;
724 struct jpulse *jp;
725 u_int32 sweek;
726 time_t last_timecode;
727 u_short flags;
729 jp = (struct jpulse *)sp;
731 /* The timecode is presented as seconds into the current GPS week */
732 sweek = DS2UI(jp->sweek) % WEEKSECS;
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).
743 * If we already know the current GPS week, increment it when
744 * we wrap into a new week.
746 if (instance->gweek == 0) {
747 if (!instance->gpos_gweek) {
748 return ("jupiter_parse_t: Unknown gweek");
751 instance->gweek = instance->gpos_gweek;
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.
758 if (instance->gpos_sweek >= sweek) {
759 if ((instance->gpos_sweek - sweek) > WEEKSECS / 2)
760 ++instance->gweek;
762 else {
763 if ((sweek - instance->gpos_sweek) > WEEKSECS / 2)
764 --instance->gweek;
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);
774 * See if the sweek stayed the same (this happens when there is
775 * no pps pulse).
777 * Otherwise, look for time warps:
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
783 * Then we warped.
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;
797 /* This timecode describes next pulse */
798 last_timecode = instance->timecode;
799 instance->timecode =
800 GPS_EPOCH + (instance->gweek * WEEKSECS) + sweek;
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);
812 jupiter_debug(instance->peer,
813 "jupiter_parse_t", "UTC %.24s (gweek/sweek %u/%u)",
814 cp, instance->gweek, sweek);
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;
824 flags = getshort(jp->flags);
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");
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");
838 return (NULL);
841 static char *
842 jupiter_parse_gpos(struct instance *instance, u_short *sp)
844 struct jgpos *jg;
845 time_t t;
846 struct tm *tm;
847 char *cp;
849 jg = (struct jgpos *)sp;
851 if (jg->navval != 0) {
853 * Solution not valid. Use caution and refuse
854 * to determine GPS week from this message.
856 instance->gpos_gweek = 0;
857 instance->gpos_sweek = 0;
858 return ("Navigation solution not valid");
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;
867 instance->gweek = 0;
869 t = GPS_EPOCH + (instance->gpos_gweek * WEEKSECS) + instance->gpos_sweek;
870 tm = gmtime(&t);
871 cp = asctime(tm);
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);
880 * jupiter_debug - print debug messages
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__ */
893 char buffer[200];
894 va_list ap;
896 #if defined(__STDC__) || defined(SYS_WINNT)
897 va_start(ap, fmt);
898 #else
899 va_start(ap);
900 #endif /* __STDC__ */
902 * Print debug message to stdout
903 * In the future, we may want to get get more creative...
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);
914 #endif
916 va_end(ap);
919 /* Checksum and transmit a message to the Jupiter */
920 static char *
921 jupiter_send(struct instance *instance, struct jheader *hp)
923 u_int len, size;
924 int cc;
925 u_short *sp;
926 static char errstr[132];
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);
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);
945 return (NULL);
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 }
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)
965 struct jheader *hp;
966 struct jrequest *rp;
967 char *cp;
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);
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,
985 static void
986 jupiter_canmsg(struct instance *instance, u_int id)
988 struct jheader *hp;
989 char *cp;
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);
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,
1004 static void
1005 jupiter_reqonemsg(struct instance *instance, u_int id)
1007 struct jheader *hp;
1008 char *cp;
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);
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 }
1027 static void
1028 jupiter_platform(struct instance *instance, u_int platform)
1030 struct jheader *hp;
1031 struct jplat *pp;
1032 char *cp;
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);
1041 /* Checksum "len" shorts */
1042 static u_short
1043 jupiter_cksum(u_short *sp, u_int len)
1045 u_short sum, x;
1047 sum = 0;
1048 while (len-- > 0) {
1049 x = *sp++;
1050 sum += getshort(x);
1052 return (~sum + 1);
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)
1059 int n, len, size, cc;
1060 struct jheader *hp;
1061 u_char *bp;
1062 u_short *sp;
1064 /* Must have at least a header's worth */
1065 cc = sizeof(*hp);
1066 size = instance->ssize;
1067 if (size < cc)
1068 return (0);
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)) {
1081 jupiter_debug(instance->peer, "{0x%x}", bp[0]);
1083 ++bp;
1084 --n;
1085 continue;
1087 if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff))
1088 break;
1090 jupiter_debug(instance->peer, "{0x%x 0x%x}", bp[0], bp[1]);
1092 bp += 2;
1093 n -= 2;
1096 jupiter_debug(instance->peer, "\n");
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);
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);
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);
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);
1132 cc += n;
1134 return (cc);
1137 #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */
1138 int refclock_jupiter_bs;
1139 #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */