1 /* $NetBSD: refclock_jupiter.c,v 1.3 2006/06/11 19:34:12 kardel Exp $ */
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
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
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
40 #if defined(REFCLOCK) && defined(CLOCK_JUPITER) && defined(HAVE_PPSAPI)
44 #include "ntp_refclock.h"
45 #include "ntp_unixtime.h"
46 #include "ntp_stdlib.h"
54 # include "ppsapi_timepps.h"
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))
61 #define getshort(s) (s)
62 #define putshort(s) (s)
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.
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.
112 struct peer
*peer
; /* peer */
113 u_int pollcnt
; /* poll message counter */
114 u_int polled
; /* Hand in a time sample? */
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 */
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
*));
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
*));
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
185 struct refclockproc
*pp
;
186 struct instance
*instance
;
193 (void)sprintf(gpsdev
, DEVICE
, unit
);
194 fd
= refclock_open(gpsdev
, SPEED232
, LDISC_RAW
);
196 jupiter_debug(peer
, "jupiter_start", "open %s: %s",
197 gpsdev
, strerror(errno
));
201 /* Allocate unit structure */
202 if ((instance
= (struct instance
*)
203 emalloc(sizeof(struct instance
))) == NULL
) {
207 memset((char *)instance
, 0, sizeof(struct instance
));
208 instance
->peer
= peer
;
210 pp
->io
.clock_recv
= jupiter_receive
;
211 pp
->io
.srcclock
= (caddr_t
)peer
;
214 if (!io_addclock(&pp
->io
)) {
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);
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;
238 "refclock_jupiter: time_pps_create failed: %m");
240 else if (!jupiter_ppsapi(instance
))
242 #endif /* HAVE_PPSAPI */
244 /* Ensure the receiver is properly configured */
245 if (!jupiter_config(instance
))
251 jupiter_shutdown(unit
, peer
);
257 * jupiter_shutdown - shut down the clock
260 jupiter_shutdown(int unit
, struct peer
*peer
)
262 struct instance
*instance
;
263 struct refclockproc
*pp
;
266 instance
= (struct instance
*)pp
->unitptr
;
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
);
282 * jupiter_config - Configure the receiver
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",
298 instance
->pollcnt
= 2;
299 instance
->polled
= 0;
300 instance
->gpos_gweek
= 0;
301 instance
->gpos_sweek
= 0;
303 instance
->lastsweek
= 2 * WEEKSECS
;
304 instance
->timecode
= 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
);
327 jupiter_platform(instance
, JUPITER_I_PLAT_LOW
);
338 struct instance
*instance
/* unit structure pointer */
343 if (time_pps_getcap(instance
->pps_handle
, &capability
) < 0) {
345 "refclock_jupiter: time_pps_getcap failed: %m");
348 memset(&instance
->pps_params
, 0, sizeof(pps_params_t
));
349 if (!instance
->assert)
350 instance
->pps_params
.mode
= capability
& PPS_CAPTURECLEAR
;
352 instance
->pps_params
.mode
= capability
& PPS_CAPTUREASSERT
;
353 if (!(instance
->pps_params
.mode
& (PPS_CAPTUREASSERT
| PPS_CAPTURECLEAR
))) {
355 "refclock_jupiter: invalid capture edge %d",
359 instance
->pps_params
.mode
|= PPS_TSFMT_TSPEC
;
360 if (time_pps_setparams(instance
->pps_handle
, &instance
->pps_params
) < 0) {
362 "refclock_jupiter: time_pps_setparams failed: %m");
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) {
370 "refclock_jupiter: time_pps_kcbind failed: %m");
375 /* instance->peer->precision = PPS_PRECISION; */
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
);
391 * Get PPSAPI timestamps.
393 * Return 0 on failure and 1 on success.
396 jupiter_pps(struct instance
*instance
)
399 struct timespec timeout
, ts
;
404 * Convert the timespec nanoseconds field to ntp l_fp units.
406 if (instance
->pps_handle
== 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
,
414 if (instance
->pps_params
.mode
& PPS_CAPTUREASSERT
) {
415 if (pps_info
.assert_sequence
==
416 instance
->pps_info
.assert_sequence
)
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
)
423 ts
= instance
->pps_info
.clear_timestamp
;
427 if ((instance
->ts
.tv_sec
== ts
.tv_sec
) && (instance
->ts
.tv_nsec
== ts
.tv_nsec
))
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
;
437 #endif /* HAVE_PPSAPI */
440 * jupiter_poll - jupiter watchdog routine
443 jupiter_poll(int unit
, struct peer
*peer
)
445 struct instance
*instance
;
446 struct refclockproc
*pp
;
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) {
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
474 instance
->polled
= 1;
479 * jupiter_control - fudge 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
;
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
);
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
)) {
512 "mode switch: reset receiver");
513 jupiter_config(instance
);
519 * jupiter_receive - receive gps data
523 jupiter_receive(struct recvbuf
*rbufp
)
525 int bpcnt
, cc
, size
, ppsret
;
526 time_t last_timecode
;
534 struct refclockproc
*pp
;
535 struct instance
*instance
;
538 /* Initialize pointers and read the timecode and timestamp */
539 peer
= (struct peer
*)rbufp
->recv_srcclock
;
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
)) {
567 "jupiter_receive", "pulse: len %d != %u",
568 size
, (int)sizeof(struct jpulse
));
569 refclock_report(peer
, CEVNT_BADREPLY
);
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
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);
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
600 L_SUB(&tstamp
, &pp
->lastrec
);
601 if (!L_ISGEQ(&tstamp
, &instance
->limit
))
604 /* Parse timecode (even when there's no pps) */
605 last_timecode
= instance
->timecode
;
606 if ((cp
= jupiter_parse_t(instance
, sp
)) != NULL
) {
608 "jupiter_receive", "pulse: %s", cp
);
612 /* Bail if we didn't get a pps timestamp */
616 /* Bail if we don't have the last timecode yet */
617 if (last_timecode
== 0)
620 /* Add the new sample to a median filter */
621 tstamp
.l_ui
= JAN_1970
+ last_timecode
;
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
)
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
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;
656 if (size
!= sizeof(struct jgpos
)) {
658 "jupiter_receive", "gpos: len %d != %u",
659 size
, (int)sizeof(struct jgpos
));
660 refclock_report(peer
, CEVNT_BADREPLY
);
664 if ((cp
= jupiter_parse_gpos(instance
, sp
)) != NULL
) {
666 "jupiter_receive", "gpos: %s", cp
);
672 if (size
!= sizeof(struct jid
)) {
674 "jupiter_receive", "id: len %d != %u",
675 size
, (int)sizeof(struct jid
));
676 refclock_report(peer
, CEVNT_BADREPLY
);
680 * If we got this message because the Jupiter
681 * just powered instance, it needs to be reconfigured.
683 ip
= (struct jid
*)sp
;
685 "jupiter_receive", "%s chan ver %s, %s (%s)",
686 ip
->chans
, ip
->vers
, ip
->date
, ip
->opts
);
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;
694 "jupiter_receive", "reset receiver");
695 jupiter_config(instance
);
697 * Restore since jupiter_config() just
700 instance
->ssize
= cc
;
706 "jupiter_receive", "unknown message id %d",
710 instance
->ssize
-= cc
;
711 if (instance
->ssize
< 0) {
712 fprintf(stderr
, "jupiter_recv: negative ssize!\n");
714 } else if (instance
->ssize
> 0)
715 memcpy(instance
->sbuf
, (u_char
*)instance
->sbuf
+ cc
, instance
->ssize
);
720 jupiter_parse_t(struct instance
*instance
, u_short
*sp
)
726 time_t last_timecode
;
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
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
758 if (instance
->gpos_sweek
>= sweek
) {
759 if ((instance
->gpos_sweek
- sweek
) > WEEKSECS
/ 2)
763 if ((sweek
- instance
->gpos_sweek
) > WEEKSECS
/ 2)
767 else if (sweek
== 0 && instance
->lastsweek
== WEEKSECS
- 1) {
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
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
785 if (instance
->lastsweek
== sweek
)
786 jupiter_debug(instance
->peer
,
787 "jupiter_parse_t", "gps sweek not incrementing (%d)",
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
;
800 GPS_EPOCH
+ (instance
->gweek
* WEEKSECS
) + sweek
;
802 if (last_timecode
== 0)
804 jupiter_debug(instance
->peer
,
805 "jupiter_parse_t", "UTC <none> (gweek/sweek %u/%u)",
806 instance
->gweek
, sweek
);
809 tm
= gmtime(&last_timecode
);
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");
842 jupiter_parse_gpos(struct instance
*instance
, u_short
*sp
)
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
;
869 t
= GPS_EPOCH
+ (instance
->gpos_gweek
* WEEKSECS
) + instance
->gpos_sweek
;
873 jupiter_debug(instance
->peer
,
874 "jupiter_parse_g", "GPS %.24s (gweek/sweek %u/%u)",
875 cp
, instance
->gpos_gweek
, instance
->gpos_sweek
);
880 * jupiter_debug - print debug messages
882 #if defined(__STDC__) || defined(SYS_WINNT)
884 jupiter_debug(struct peer
*peer
, char *function
, char *fmt
, ...)
887 jupiter_debug(peer
, function
, fmt
, va_alist
)
891 #endif /* __STDC__ */
896 #if defined(__STDC__) || defined(SYS_WINNT)
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
);
909 fprintf(stdout
, "%s: ", function
);
910 fprintf(stdout
, buffer
);
911 fprintf(stdout
, "\n");
919 /* Checksum and transmit a message to the Jupiter */
921 jupiter_send(struct instance
*instance
, struct jheader
*hp
)
926 static char errstr
[132];
929 hp
->hsum
= putshort(jupiter_cksum((u_short
*)hp
,
930 (size
/ sizeof(u_short
)) - 1));
931 len
= getshort(hp
->len
);
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
));
941 } else if (cc
!= size
) {
942 (void)sprintf(errstr
, "short write (%d != %d)", cc
, size
);
948 /* Request periodic message output */
950 struct jheader jheader
;
951 struct jrequest jrequest
;
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 },
960 /* An interval of zero means to output on trigger */
962 jupiter_reqmsg(struct instance
*instance
, u_int id
,
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
,
986 jupiter_canmsg(struct instance
*instance
, u_int id
)
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
,
1005 jupiter_reqonemsg(struct instance
*instance
, u_int id
)
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 */
1018 struct jheader jheader
;
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 },
1028 jupiter_platform(struct instance
*instance
, u_int platform
)
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 */
1043 jupiter_cksum(u_short
*sp
, u_int len
)
1055 /* Return the size of the next message (or zero if we don't have it all yet) */
1057 jupiter_recv(struct instance
*instance
)
1059 int n
, len
, size
, cc
;
1064 /* Must have at least a header's worth */
1066 size
= instance
->ssize
;
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");
1079 if (bp
[0] != (JUPITER_SYNC
& 0xff)) {
1081 jupiter_debug(instance->peer, "{0x%x}", bp[0]);
1087 if (bp
[1] == ((JUPITER_SYNC
>> 8) & 0xff))
1090 jupiter_debug(instance->peer, "{0x%x 0x%x}", bp[0], bp[1]);
1096 jupiter_debug(instance->peer, "\n");
1098 /* Shuffle data to front of input buffer */
1102 instance
->ssize
= size
;
1103 if (size
< cc
|| hp
->sync
!= JUPITER_SYNC
)
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;
1115 /* Check for a payload */
1116 len
= getshort(hp
->len
);
1118 n
= (len
+ 1) * sizeof(u_short
);
1119 /* Not enough data yet */
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;
1137 #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */
1138 int refclock_jupiter_bs
;
1139 #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */