2 * Copyright (c) 1997, 1998, 2003
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Lawrence Berkeley Laboratory.
17 * 4. The name of the University may not be used to endorse or promote
18 * products derived from this software without specific prior
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(REFCLOCK) && defined(CLOCK_JUPITER) && defined(HAVE_PPSAPI)
42 #include "ntp_refclock.h"
43 #include "ntp_unixtime.h"
44 #include "ntp_stdlib.h"
52 # include "ppsapi_timepps.h"
55 #ifdef WORDS_BIGENDIAN
56 #define getshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
57 #define putshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
59 #define getshort(s) ((u_short)(s))
60 #define putshort(s) ((u_short)(s))
64 * This driver supports the Rockwell Jupiter GPS Receiver board
65 * adapted to precision timing applications. It requires the
66 * ppsclock line discipline or streams module described in the
67 * Line Disciplines and Streams Drivers page. It also requires a
68 * gadget box and 1-PPS level converter, such as described in the
69 * Pulse-per-second (PPS) Signal Interfacing page.
71 * It may work (with minor modifications) with other Rockwell GPS
72 * receivers such as the CityTracker.
78 #define DEVICE "/dev/gps%d" /* device name and unit */
79 #define SPEED232 B9600 /* baud */
82 * Radio interface parameters
84 #define PRECISION (-18) /* precision assumed (about 4 us) */
85 #define REFID "GPS\0" /* reference id */
86 #define DESCRIPTION "Rockwell Jupiter GPS Receiver" /* who we are */
87 #define DEFFUDGETIME 0 /* default fudge time (ms) */
89 /* Unix timestamp for the GPS epoch: January 6, 1980 */
90 #define GPS_EPOCH 315964800
92 /* Double short to unsigned int */
93 #define DS2UI(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
95 /* Double short to signed int */
96 #define DS2I(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
98 /* One week's worth of seconds */
99 #define WEEKSECS (7 * 24 * 60 * 60)
102 * Jupiter unit control structure.
105 struct peer *peer; /* peer */
106 u_int pollcnt; /* poll message counter */
107 u_int polled; /* Hand in a time sample? */
109 pps_params_t pps_params; /* pps parameters */
110 pps_info_t pps_info; /* last pps data */
111 pps_handle_t pps_handle; /* pps handle */
112 u_int assert; /* pps edge to use */
113 u_int hardpps; /* enable kernel mode */
114 struct timespec ts; /* last timestamp */
117 u_int gpos_gweek; /* Current GPOS GPS week number */
118 u_int gpos_sweek; /* Current GPOS GPS seconds into week */
119 u_int gweek; /* current GPS week number */
120 u_int32 lastsweek; /* last seconds into GPS week */
121 time_t timecode; /* current ntp timecode */
122 u_int32 stime; /* used to detect firmware bug */
123 int wantid; /* don't reconfig on channel id msg */
124 u_int moving; /* mobile platform? */
125 u_char sloppyclockflag; /* fudge flags */
126 u_short sbuf[512]; /* local input buffer */
127 int ssize; /* space used in sbuf */
131 * Function prototypes
133 static void jupiter_canmsg (struct instance *, u_int);
134 static u_short jupiter_cksum (u_short *, u_int);
135 static int jupiter_config (struct instance *);
136 static void jupiter_debug (struct peer *, const char *,
138 __attribute__ ((format (printf, 3, 4)));
139 static const char * jupiter_parse_t (struct instance *, u_short *);
140 static const char * jupiter_parse_gpos (struct instance *, u_short *);
141 static void jupiter_platform (struct instance *, u_int);
142 static void jupiter_poll (int, struct peer *);
143 static void jupiter_control (int, const struct refclockstat *,
144 struct refclockstat *, struct peer *);
146 static int jupiter_ppsapi (struct instance *);
147 static int jupiter_pps (struct instance *);
148 #endif /* HAVE_PPSAPI */
149 static int jupiter_recv (struct instance *);
150 static void jupiter_receive (struct recvbuf *rbufp);
151 static void jupiter_reqmsg (struct instance *, u_int, u_int);
152 static void jupiter_reqonemsg(struct instance *, u_int);
153 static char * jupiter_send (struct instance *, struct jheader *);
154 static void jupiter_shutdown(int, struct peer *);
155 static int jupiter_start (int, struct peer *);
160 struct refclock refclock_jupiter = {
161 jupiter_start, /* start up driver */
162 jupiter_shutdown, /* shut down driver */
163 jupiter_poll, /* transmit poll message */
164 jupiter_control, /* (clock control) */
165 noentry, /* (clock init) */
166 noentry, /* (clock buginfo) */
167 NOFLAGS /* not used */
171 * jupiter_start - open the devices and initialize data for processing
179 struct refclockproc *pp;
180 struct instance *instance;
187 snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit);
188 fd = refclock_open(gpsdev, SPEED232, LDISC_RAW);
190 jupiter_debug(peer, "jupiter_start", "open %s: %m",
195 /* Allocate unit structure */
196 instance = emalloc_zero(sizeof(*instance));
197 instance->peer = peer;
199 pp->io.clock_recv = jupiter_receive;
200 pp->io.srcclock = peer;
203 if (!io_addclock(&pp->io)) {
209 pp->unitptr = instance;
212 * Initialize miscellaneous variables
214 peer->precision = PRECISION;
215 pp->clockdesc = DESCRIPTION;
216 memcpy((char *)&pp->refid, REFID, 4);
219 instance->assert = 1;
220 instance->hardpps = 0;
222 * Start the PPSAPI interface if it is there. Default to use
223 * the assert edge and do not enable the kernel hardpps.
225 if (time_pps_create(fd, &instance->pps_handle) < 0) {
226 instance->pps_handle = 0;
228 "refclock_jupiter: time_pps_create failed: %m");
230 else if (!jupiter_ppsapi(instance))
232 #endif /* HAVE_PPSAPI */
234 /* Ensure the receiver is properly configured */
235 if (!jupiter_config(instance))
241 jupiter_shutdown(unit, peer);
247 * jupiter_shutdown - shut down the clock
250 jupiter_shutdown(int unit, struct peer *peer)
252 struct instance *instance;
253 struct refclockproc *pp;
256 instance = pp->unitptr;
261 if (instance->pps_handle) {
262 time_pps_destroy(instance->pps_handle);
263 instance->pps_handle = 0;
265 #endif /* HAVE_PPSAPI */
268 io_closeclock(&pp->io);
273 * jupiter_config - Configure the receiver
276 jupiter_config(struct instance *instance)
278 jupiter_debug(instance->peer, __func__, "init receiver");
281 * Initialize the unit variables
283 instance->sloppyclockflag = instance->peer->procptr->sloppyclockflag;
284 instance->moving = !!(instance->sloppyclockflag & CLK_FLAG2);
285 if (instance->moving)
286 jupiter_debug(instance->peer, __func__, "mobile platform");
288 instance->pollcnt = 2;
289 instance->polled = 0;
290 instance->gpos_gweek = 0;
291 instance->gpos_sweek = 0;
293 instance->lastsweek = 2 * WEEKSECS;
294 instance->timecode = 0;
298 /* Stop outputting all messages */
299 jupiter_canmsg(instance, JUPITER_ALL);
301 /* Request the receiver id so we can syslog the firmware version */
302 jupiter_reqonemsg(instance, JUPITER_O_ID);
304 /* Flag that this the id was requested (so we don't get called again) */
305 instance->wantid = 1;
307 /* Request perodic time mark pulse messages */
308 jupiter_reqmsg(instance, JUPITER_O_PULSE, 1);
310 /* Request perodic geodetic position status */
311 jupiter_reqmsg(instance, JUPITER_O_GPOS, 1);
313 /* Set application platform type */
314 if (instance->moving)
315 jupiter_platform(instance, JUPITER_I_PLAT_MED);
317 jupiter_platform(instance, JUPITER_I_PLAT_LOW);
328 struct instance *instance /* unit structure pointer */
333 if (time_pps_getcap(instance->pps_handle, &capability) < 0) {
335 "refclock_jupiter: time_pps_getcap failed: %m");
338 memset(&instance->pps_params, 0, sizeof(pps_params_t));
339 if (!instance->assert)
340 instance->pps_params.mode = capability & PPS_CAPTURECLEAR;
342 instance->pps_params.mode = capability & PPS_CAPTUREASSERT;
343 if (!(instance->pps_params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
345 "refclock_jupiter: invalid capture edge %d",
349 instance->pps_params.mode |= PPS_TSFMT_TSPEC;
350 if (time_pps_setparams(instance->pps_handle, &instance->pps_params) < 0) {
352 "refclock_jupiter: time_pps_setparams failed: %m");
355 if (instance->hardpps) {
356 if (time_pps_kcbind(instance->pps_handle, PPS_KC_HARDPPS,
357 instance->pps_params.mode & ~PPS_TSFMT_TSPEC,
358 PPS_TSFMT_TSPEC) < 0) {
360 "refclock_jupiter: time_pps_kcbind failed: %m");
365 /* instance->peer->precision = PPS_PRECISION; */
369 time_pps_getparams(instance->pps_handle, &instance->pps_params);
370 jupiter_debug(instance->peer, __func__,
371 "pps capability 0x%x version %d mode 0x%x kern %d",
372 capability, instance->pps_params.api_version,
373 instance->pps_params.mode, instance->hardpps);
381 * Get PPSAPI timestamps.
383 * Return 0 on failure and 1 on success.
386 jupiter_pps(struct instance *instance)
389 struct timespec timeout, ts;
394 * Convert the timespec nanoseconds field to ntp l_fp units.
396 if (instance->pps_handle == 0)
400 memcpy(&pps_info, &instance->pps_info, sizeof(pps_info_t));
401 if (time_pps_fetch(instance->pps_handle, PPS_TSFMT_TSPEC, &instance->pps_info,
404 if (instance->pps_params.mode & PPS_CAPTUREASSERT) {
405 if (pps_info.assert_sequence ==
406 instance->pps_info.assert_sequence)
408 ts = instance->pps_info.assert_timestamp;
409 } else if (instance->pps_params.mode & PPS_CAPTURECLEAR) {
410 if (pps_info.clear_sequence ==
411 instance->pps_info.clear_sequence)
413 ts = instance->pps_info.clear_timestamp;
417 if ((instance->ts.tv_sec == ts.tv_sec) && (instance->ts.tv_nsec == ts.tv_nsec))
421 tstmp.l_ui = (u_int32)ts.tv_sec + JAN_1970;
422 dtemp = ts.tv_nsec * FRAC / 1e9;
423 tstmp.l_uf = (u_int32)dtemp;
424 instance->peer->procptr->lastrec = tstmp;
427 #endif /* HAVE_PPSAPI */
430 * jupiter_poll - jupiter watchdog routine
433 jupiter_poll(int unit, struct peer *peer)
435 struct instance *instance;
436 struct refclockproc *pp;
439 instance = pp->unitptr;
442 * You don't need to poll this clock. It puts out timecodes
443 * once per second. If asked for a timestamp, take note.
444 * The next time a timecode comes in, it will be fed back.
448 * If we haven't had a response in a while, reset the receiver.
450 if (instance->pollcnt > 0) {
453 refclock_report(peer, CEVNT_TIMEOUT);
455 /* Request the receiver id to trigger a reconfig */
456 jupiter_reqonemsg(instance, JUPITER_O_ID);
457 instance->wantid = 0;
461 * polled every 64 seconds. Ask jupiter_receive to hand in
464 instance->polled = 1;
469 * jupiter_control - fudge control
473 int unit, /* unit (not used) */
474 const struct refclockstat *in, /* input parameters (not used) */
475 struct refclockstat *out, /* output parameters (not used) */
476 struct peer *peer /* peer structure pointer */
479 struct refclockproc *pp;
480 struct instance *instance;
481 u_char sloppyclockflag;
484 instance = pp->unitptr;
486 DTOLFP(pp->fudgetime2, &instance->limit);
487 /* Force positive value. */
488 if (L_ISNEG(&instance->limit))
489 L_NEG(&instance->limit);
492 instance->assert = !(pp->sloppyclockflag & CLK_FLAG3);
493 jupiter_ppsapi(instance);
494 #endif /* HAVE_PPSAPI */
496 sloppyclockflag = instance->sloppyclockflag;
497 instance->sloppyclockflag = pp->sloppyclockflag;
498 if ((instance->sloppyclockflag & CLK_FLAG2) !=
499 (sloppyclockflag & CLK_FLAG2)) {
500 jupiter_debug(peer, __func__,
501 "mode switch: reset receiver");
502 jupiter_config(instance);
508 * jupiter_receive - receive gps data
512 jupiter_receive(struct recvbuf *rbufp)
515 int cc, size, ppsret;
516 time_t last_timecode;
524 struct refclockproc *pp;
525 struct instance *instance;
528 /* Initialize pointers and read the timecode and timestamp */
529 peer = rbufp->recv_peer;
531 instance = pp->unitptr;
533 bp = (u_char *)rbufp->recv_buffer;
534 bpcnt = rbufp->recv_length;
536 /* This shouldn't happen */
537 if (bpcnt > sizeof(instance->sbuf) - instance->ssize)
538 bpcnt = sizeof(instance->sbuf) - instance->ssize;
540 /* Append to input buffer */
541 memcpy((u_char *)instance->sbuf + instance->ssize, bp, bpcnt);
542 instance->ssize += bpcnt;
544 /* While there's at least a header and we parse an intact message */
545 while (instance->ssize > (int)sizeof(*hp) && (cc = jupiter_recv(instance)) > 0) {
546 instance->pollcnt = 2;
548 tstamp = rbufp->recv_time;
549 hp = (struct jheader *)instance->sbuf;
550 sp = (u_short *)(hp + 1);
551 size = cc - sizeof(*hp);
552 switch (getshort(hp->id)) {
554 case JUPITER_O_PULSE:
555 if (size != sizeof(struct jpulse)) {
556 jupiter_debug(peer, __func__,
557 "pulse: len %d != %u",
558 size, (int)sizeof(struct jpulse));
559 refclock_report(peer, CEVNT_BADREPLY);
564 * There appears to be a firmware bug related
565 * to the pulse message; in addition to the one
566 * per second messages, we get an extra pulse
567 * message once an hour (on the anniversary of
568 * the cold start). It seems to come 200 ms
569 * after the one requested. So if we've seen a
570 * pulse message in the last 210 ms, we skip
573 laststime = instance->stime;
574 instance->stime = DS2UI(((struct jpulse *)sp)->stime);
575 if (laststime != 0 && instance->stime - laststime <= 21) {
576 jupiter_debug(peer, __func__,
577 "avoided firmware bug (stime %.2f, laststime %.2f)",
578 (double)instance->stime * 0.01, (double)laststime * 0.01);
582 /* Retrieve pps timestamp */
583 ppsret = jupiter_pps(instance);
586 * Add one second if msg received early
587 * (i.e. before limit, a.k.a. fudgetime2) in
590 L_SUB(&tstamp, &pp->lastrec);
591 if (!L_ISGEQ(&tstamp, &instance->limit))
594 /* Parse timecode (even when there's no pps) */
595 last_timecode = instance->timecode;
596 if ((cp = jupiter_parse_t(instance, sp)) != NULL) {
597 jupiter_debug(peer, __func__,
602 /* Bail if we didn't get a pps timestamp */
606 /* Bail if we don't have the last timecode yet */
607 if (last_timecode == 0)
610 /* Add the new sample to a median filter */
611 tstamp.l_ui = JAN_1970 + (u_int32)last_timecode;
614 refclock_process_offset(pp, tstamp, pp->lastrec, pp->fudgetime1);
617 * The clock will blurt a timecode every second
618 * but we only want one when polled. If we
619 * havn't been polled, bail out.
621 if (!instance->polled)
623 instance->polled = 0;
626 * It's a live one! Remember this time.
629 pp->lastref = pp->lastrec;
630 refclock_receive(peer);
633 * If we get here - what we got from the clock is
636 refclock_report(peer, CEVNT_NOMINAL);
639 * We have succeeded in answering the poll.
640 * Turn off the flag and return
642 instance->polled = 0;
646 if (size != sizeof(struct jgpos)) {
647 jupiter_debug(peer, __func__,
648 "gpos: len %d != %u",
649 size, (int)sizeof(struct jgpos));
650 refclock_report(peer, CEVNT_BADREPLY);
654 if ((cp = jupiter_parse_gpos(instance, sp)) != NULL) {
655 jupiter_debug(peer, __func__,
662 if (size != sizeof(struct jid)) {
663 jupiter_debug(peer, __func__,
665 size, (int)sizeof(struct jid));
666 refclock_report(peer, CEVNT_BADREPLY);
670 * If we got this message because the Jupiter
671 * just powered instance, it needs to be reconfigured.
673 ip = (struct jid *)sp;
674 jupiter_debug(peer, __func__,
675 "%s chan ver %s, %s (%s)",
676 ip->chans, ip->vers, ip->date, ip->opts);
678 "jupiter_receive: %s chan ver %s, %s (%s)",
679 ip->chans, ip->vers, ip->date, ip->opts);
680 if (instance->wantid)
681 instance->wantid = 0;
683 jupiter_debug(peer, __func__, "reset receiver");
684 jupiter_config(instance);
686 * Restore since jupiter_config() just
689 instance->ssize = cc;
694 jupiter_debug(peer, __func__, "unknown message id %d",
698 instance->ssize -= cc;
699 if (instance->ssize < 0) {
700 fprintf(stderr, "jupiter_recv: negative ssize!\n");
702 } else if (instance->ssize > 0)
703 memcpy(instance->sbuf, (u_char *)instance->sbuf + cc, instance->ssize);
708 jupiter_parse_t(struct instance *instance, u_short *sp)
714 time_t last_timecode;
717 jp = (struct jpulse *)sp;
719 /* The timecode is presented as seconds into the current GPS week */
720 sweek = DS2UI(jp->sweek) % WEEKSECS;
723 * If we don't know the current GPS week, calculate it from the
724 * current time. (It's too bad they didn't include this
725 * important value in the pulse message). We'd like to pick it
726 * up from one of the other messages like gpos or chan but they
727 * don't appear to be synchronous with time keeping and changes
728 * too soon (something like 10 seconds before the new GPS
731 * If we already know the current GPS week, increment it when
732 * we wrap into a new week.
734 if (instance->gweek == 0) {
735 if (!instance->gpos_gweek) {
736 return ("jupiter_parse_t: Unknown gweek");
739 instance->gweek = instance->gpos_gweek;
742 * Fix warps. GPOS has GPS time and PULSE has UTC.
743 * Plus, GPOS need not be completely in synch with
746 if (instance->gpos_sweek >= sweek) {
747 if ((instance->gpos_sweek - sweek) > WEEKSECS / 2)
751 if ((sweek - instance->gpos_sweek) > WEEKSECS / 2)
755 else if (sweek == 0 && instance->lastsweek == WEEKSECS - 1) {
757 jupiter_debug(instance->peer, __func__,
758 "NEW gps week %u", instance->gweek);
762 * See if the sweek stayed the same (this happens when there is
765 * Otherwise, look for time warps:
767 * - we have stored at least one lastsweek and
768 * - the sweek didn't increase by one and
769 * - we didn't wrap to a new GPS week
773 if (instance->lastsweek == sweek)
774 jupiter_debug(instance->peer, __func__,
775 "gps sweek not incrementing (%d)",
777 else if (instance->lastsweek != 2 * WEEKSECS &&
778 instance->lastsweek + 1 != sweek &&
779 !(sweek == 0 && instance->lastsweek == WEEKSECS - 1))
780 jupiter_debug(instance->peer, __func__,
781 "gps sweek jumped (was %d, now %d)",
782 instance->lastsweek, sweek);
783 instance->lastsweek = sweek;
785 /* This timecode describes next pulse */
786 last_timecode = instance->timecode;
788 GPS_EPOCH + (instance->gweek * WEEKSECS) + sweek;
790 if (last_timecode == 0)
792 jupiter_debug(instance->peer, __func__,
793 "UTC <none> (gweek/sweek %u/%u)",
794 instance->gweek, sweek);
797 tm = gmtime(&last_timecode);
800 jupiter_debug(instance->peer, __func__,
801 "UTC %.24s (gweek/sweek %u/%u)",
802 cp, instance->gweek, sweek);
804 /* Billboard last_timecode (which is now the current time) */
805 instance->peer->procptr->year = tm->tm_year + 1900;
806 instance->peer->procptr->day = tm->tm_yday + 1;
807 instance->peer->procptr->hour = tm->tm_hour;
808 instance->peer->procptr->minute = tm->tm_min;
809 instance->peer->procptr->second = tm->tm_sec;
812 flags = getshort(jp->flags);
814 /* Toss if not designated "valid" by the gps */
815 if ((flags & JUPITER_O_PULSE_VALID) == 0) {
816 refclock_report(instance->peer, CEVNT_BADTIME);
817 return ("time mark not valid");
820 /* We better be sync'ed to UTC... */
821 if ((flags & JUPITER_O_PULSE_UTC) == 0) {
822 refclock_report(instance->peer, CEVNT_BADTIME);
823 return ("time mark not sync'ed to UTC");
830 jupiter_parse_gpos(struct instance *instance, u_short *sp)
837 jg = (struct jgpos *)sp;
839 if (jg->navval != 0) {
841 * Solution not valid. Use caution and refuse
842 * to determine GPS week from this message.
844 instance->gpos_gweek = 0;
845 instance->gpos_sweek = 0;
846 return ("Navigation solution not valid");
849 instance->gpos_gweek = jg->gweek;
850 instance->gpos_sweek = DS2UI(jg->sweek);
851 while(instance->gpos_sweek >= WEEKSECS) {
852 instance->gpos_sweek -= WEEKSECS;
853 ++instance->gpos_gweek;
857 t = GPS_EPOCH + (instance->gpos_gweek * WEEKSECS) + instance->gpos_sweek;
861 jupiter_debug(instance->peer, __func__,
862 "GPS %.24s (gweek/sweek %u/%u)",
863 cp, instance->gpos_gweek, instance->gpos_sweek);
868 * jupiter_debug - print debug messages
873 const char * function,
883 * Print debug message to stdout
884 * In the future, we may want to get get more creative...
886 mvsnprintf(buffer, sizeof(buffer), fmt, ap);
887 record_clock_stats(&peer->srcadr, buffer);
890 printf("%s: %s\n", function, buffer);
898 /* Checksum and transmit a message to the Jupiter */
900 jupiter_send(struct instance *instance, struct jheader *hp)
905 static char errstr[132];
908 hp->hsum = putshort(jupiter_cksum((u_short *)hp,
909 (size / sizeof(u_short)) - 1));
910 len = getshort(hp->len);
912 sp = (u_short *)(hp + 1);
913 sp[len] = putshort(jupiter_cksum(sp, len));
914 size += (len + 1) * sizeof(u_short);
917 if ((cc = write(instance->peer->procptr->io.fd, (char *)hp, size)) < 0) {
918 msnprintf(errstr, sizeof(errstr), "write: %m");
920 } else if (cc != (int)size) {
921 snprintf(errstr, sizeof(errstr), "short write (%zd != %u)", cc, size);
927 /* Request periodic message output */
929 struct jheader jheader;
930 struct jrequest jrequest;
932 { putshort(JUPITER_SYNC), 0,
933 putshort((sizeof(struct jrequest) / sizeof(u_short)) - 1),
934 0, JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK |
935 JUPITER_FLAG_CONN | JUPITER_FLAG_LOG, 0 },
939 /* An interval of zero means to output on trigger */
941 jupiter_reqmsg(struct instance *instance, u_int id,
948 hp = &reqmsg.jheader;
949 hp->id = putshort(id);
950 rp = &reqmsg.jrequest;
951 rp->trigger = putshort(interval == 0);
952 rp->interval = putshort(interval);
953 if ((cp = jupiter_send(instance, hp)) != NULL)
954 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
957 /* Cancel periodic message output */
958 static struct jheader canmsg = {
959 putshort(JUPITER_SYNC), 0, 0, 0,
960 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_DISC,
965 jupiter_canmsg(struct instance *instance, u_int id)
971 hp->id = putshort(id);
972 if ((cp = jupiter_send(instance, hp)) != NULL)
973 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
976 /* Request a single message output */
977 static struct jheader reqonemsg = {
978 putshort(JUPITER_SYNC), 0, 0, 0,
979 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_QUERY,
984 jupiter_reqonemsg(struct instance *instance, u_int id)
990 hp->id = putshort(id);
991 if ((cp = jupiter_send(instance, hp)) != NULL)
992 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
995 /* Set the platform dynamics */
997 struct jheader jheader;
1000 { putshort(JUPITER_SYNC), putshort(JUPITER_I_PLAT),
1001 putshort((sizeof(struct jplat) / sizeof(u_short)) - 1), 0,
1002 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK, 0 },
1007 jupiter_platform(struct instance *instance, u_int platform)
1013 hp = &platmsg.jheader;
1014 pp = &platmsg.jplat;
1015 pp->platform = putshort(platform);
1016 if ((cp = jupiter_send(instance, hp)) != NULL)
1017 jupiter_debug(instance->peer, __func__, "%u: %s", platform, cp);
1020 /* Checksum "len" shorts */
1022 jupiter_cksum(u_short *sp, u_int len)
1034 /* Return the size of the next message (or zero if we don't have it all yet) */
1036 jupiter_recv(struct instance *instance)
1038 int n, len, size, cc;
1043 /* Must have at least a header's worth */
1045 size = instance->ssize;
1049 /* Search for the sync short if missing */
1050 sp = instance->sbuf;
1051 hp = (struct jheader *)sp;
1052 if (getshort(hp->sync) != JUPITER_SYNC) {
1053 /* Wasn't at the front, sync up */
1054 jupiter_debug(instance->peer, __func__, "syncing");
1058 if (bp[0] != (JUPITER_SYNC & 0xff)) {
1060 jupiter_debug(instance->peer, __func__,
1067 if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff))
1070 jupiter_debug(instance->peer, __func__,
1071 "{0x%x 0x%x}", bp[0], bp[1]);
1077 jupiter_debug(instance->peer, __func__, "\n");
1079 /* Shuffle data to front of input buffer */
1083 instance->ssize = size;
1084 if (size < cc || hp->sync != JUPITER_SYNC)
1088 if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) !=
1089 getshort(hp->hsum)) {
1090 jupiter_debug(instance->peer, __func__, "bad header checksum!");
1091 /* This is drastic but checksum errors should be rare */
1092 instance->ssize = 0;
1096 /* Check for a payload */
1097 len = getshort(hp->len);
1099 n = (len + 1) * sizeof(u_short);
1100 /* Not enough data yet */
1104 /* Check payload checksum */
1105 sp = (u_short *)(hp + 1);
1106 if (jupiter_cksum(sp, len) != getshort(sp[len])) {
1107 jupiter_debug(instance->peer,
1108 __func__, "bad payload checksum!");
1109 /* This is drastic but checksum errors should be rare */
1110 instance->ssize = 0;
1118 #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */
1119 int refclock_jupiter_bs;
1120 #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */