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 XNTP_BIG_ENDIAN
56 #define getshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
57 #define putshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
59 #define getshort(s) (s)
60 #define putshort(s) (s)
69 * This driver supports the Rockwell Jupiter GPS Receiver board
70 * adapted to precision timing applications. It requires the
71 * ppsclock line discipline or streams module described in the
72 * Line Disciplines and Streams Drivers page. It also requires a
73 * gadget box and 1-PPS level converter, such as described in the
74 * Pulse-per-second (PPS) Signal Interfacing page.
76 * It may work (with minor modifications) with other Rockwell GPS
77 * receivers such as the CityTracker.
83 #define DEVICE "/dev/gps%d" /* device name and unit */
84 #define SPEED232 B9600 /* baud */
87 * Radio interface parameters
89 #define PRECISION (-18) /* precision assumed (about 4 us) */
90 #define REFID "GPS\0" /* reference id */
91 #define DESCRIPTION "Rockwell Jupiter GPS Receiver" /* who we are */
92 #define DEFFUDGETIME 0 /* default fudge time (ms) */
94 /* Unix timestamp for the GPS epoch: January 6, 1980 */
95 #define GPS_EPOCH 315964800
97 /* Double short to unsigned int */
98 #define DS2UI(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
100 /* Double short to signed int */
101 #define DS2I(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
103 /* One week's worth of seconds */
104 #define WEEKSECS (7 * 24 * 60 * 60)
107 * Jupiter unit control structure.
110 struct peer *peer; /* peer */
111 u_int pollcnt; /* poll message counter */
112 u_int polled; /* Hand in a time sample? */
114 pps_params_t pps_params; /* pps parameters */
115 pps_info_t pps_info; /* last pps data */
116 pps_handle_t pps_handle; /* pps handle */
117 u_int assert; /* pps edge to use */
118 u_int hardpps; /* enable kernel mode */
119 struct timespec ts; /* last timestamp */
122 u_int gpos_gweek; /* Current GPOS GPS week number */
123 u_int gpos_sweek; /* Current GPOS GPS seconds into week */
124 u_int gweek; /* current GPS week number */
125 u_int32 lastsweek; /* last seconds into GPS week */
126 time_t timecode; /* current ntp timecode */
127 u_int32 stime; /* used to detect firmware bug */
128 int wantid; /* don't reconfig on channel id msg */
129 u_int moving; /* mobile platform? */
130 u_char sloppyclockflag; /* fudge flags */
131 u_short sbuf[512]; /* local input buffer */
132 int ssize; /* space used in sbuf */
136 * Function prototypes
138 static void jupiter_canmsg P((struct instance *, u_int));
139 static u_short jupiter_cksum P((u_short *, u_int));
140 static int jupiter_config P((struct instance *));
141 static void jupiter_debug P((struct peer *, char *, char *, ...))
142 __attribute__ ((format (printf, 3, 4)));
143 static char * jupiter_parse_t P((struct instance *, u_short *));
144 static char * jupiter_parse_gpos P((struct instance *, u_short *));
145 static void jupiter_platform P((struct instance *, u_int));
146 static void jupiter_poll P((int, struct peer *));
147 static void jupiter_control P((int, struct refclockstat *, struct
148 refclockstat *, struct peer *));
150 static int jupiter_ppsapi P((struct instance *));
151 static int jupiter_pps P((struct instance *));
152 #endif /* HAVE_PPSAPI */
153 static int jupiter_recv P((struct instance *));
154 static void jupiter_receive P((struct recvbuf *rbufp));
155 static void jupiter_reqmsg P((struct instance *, u_int, u_int));
156 static void jupiter_reqonemsg P((struct instance *, u_int));
157 static char * jupiter_send P((struct instance *, struct jheader *));
158 static void jupiter_shutdown P((int, struct peer *));
159 static int jupiter_start P((int, struct peer *));
164 struct refclock refclock_jupiter = {
165 jupiter_start, /* start up driver */
166 jupiter_shutdown, /* shut down driver */
167 jupiter_poll, /* transmit poll message */
168 jupiter_control, /* (clock control) */
169 noentry, /* (clock init) */
170 noentry, /* (clock buginfo) */
171 NOFLAGS /* not used */
175 * jupiter_start - open the devices and initialize data for processing
183 struct refclockproc *pp;
184 struct instance *instance;
191 (void)sprintf(gpsdev, DEVICE, unit);
192 fd = refclock_open(gpsdev, SPEED232, LDISC_RAW);
194 jupiter_debug(peer, "jupiter_start", "open %s: %s",
195 gpsdev, strerror(errno));
199 /* Allocate unit structure */
200 if ((instance = (struct instance *)
201 emalloc(sizeof(struct instance))) == NULL) {
205 memset((char *)instance, 0, sizeof(struct instance));
206 instance->peer = peer;
208 pp->io.clock_recv = jupiter_receive;
209 pp->io.srcclock = (caddr_t)peer;
212 if (!io_addclock(&pp->io)) {
217 pp->unitptr = (caddr_t)instance;
220 * Initialize miscellaneous variables
222 peer->precision = PRECISION;
223 pp->clockdesc = DESCRIPTION;
224 memcpy((char *)&pp->refid, REFID, 4);
227 instance->assert = 1;
228 instance->hardpps = 0;
230 * Start the PPSAPI interface if it is there. Default to use
231 * the assert edge and do not enable the kernel hardpps.
233 if (time_pps_create(fd, &instance->pps_handle) < 0) {
234 instance->pps_handle = 0;
236 "refclock_jupiter: time_pps_create failed: %m");
238 else if (!jupiter_ppsapi(instance))
240 #endif /* HAVE_PPSAPI */
242 /* Ensure the receiver is properly configured */
243 if (!jupiter_config(instance))
249 jupiter_shutdown(unit, peer);
255 * jupiter_shutdown - shut down the clock
258 jupiter_shutdown(int unit, struct peer *peer)
260 struct instance *instance;
261 struct refclockproc *pp;
264 instance = (struct instance *)pp->unitptr;
269 if (instance->pps_handle) {
270 time_pps_destroy(instance->pps_handle);
271 instance->pps_handle = 0;
273 #endif /* HAVE_PPSAPI */
275 io_closeclock(&pp->io);
280 * jupiter_config - Configure the receiver
283 jupiter_config(struct instance *instance)
285 jupiter_debug(instance->peer, "jupiter_config", "init receiver");
288 * Initialize the unit variables
290 instance->sloppyclockflag = instance->peer->procptr->sloppyclockflag;
291 instance->moving = !!(instance->sloppyclockflag & CLK_FLAG2);
292 if (instance->moving)
293 jupiter_debug(instance->peer, "jupiter_config",
296 instance->pollcnt = 2;
297 instance->polled = 0;
298 instance->gpos_gweek = 0;
299 instance->gpos_sweek = 0;
301 instance->lastsweek = 2 * WEEKSECS;
302 instance->timecode = 0;
306 /* Stop outputting all messages */
307 jupiter_canmsg(instance, JUPITER_ALL);
309 /* Request the receiver id so we can syslog the firmware version */
310 jupiter_reqonemsg(instance, JUPITER_O_ID);
312 /* Flag that this the id was requested (so we don't get called again) */
313 instance->wantid = 1;
315 /* Request perodic time mark pulse messages */
316 jupiter_reqmsg(instance, JUPITER_O_PULSE, 1);
318 /* Request perodic geodetic position status */
319 jupiter_reqmsg(instance, JUPITER_O_GPOS, 1);
321 /* Set application platform type */
322 if (instance->moving)
323 jupiter_platform(instance, JUPITER_I_PLAT_MED);
325 jupiter_platform(instance, JUPITER_I_PLAT_LOW);
336 struct instance *instance /* unit structure pointer */
341 if (time_pps_getcap(instance->pps_handle, &capability) < 0) {
343 "refclock_jupiter: time_pps_getcap failed: %m");
346 memset(&instance->pps_params, 0, sizeof(pps_params_t));
347 if (!instance->assert)
348 instance->pps_params.mode = capability & PPS_CAPTURECLEAR;
350 instance->pps_params.mode = capability & PPS_CAPTUREASSERT;
351 if (!(instance->pps_params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
353 "refclock_jupiter: invalid capture edge %d",
357 instance->pps_params.mode |= PPS_TSFMT_TSPEC;
358 if (time_pps_setparams(instance->pps_handle, &instance->pps_params) < 0) {
360 "refclock_jupiter: time_pps_setparams failed: %m");
363 if (instance->hardpps) {
364 if (time_pps_kcbind(instance->pps_handle, PPS_KC_HARDPPS,
365 instance->pps_params.mode & ~PPS_TSFMT_TSPEC,
366 PPS_TSFMT_TSPEC) < 0) {
368 "refclock_jupiter: time_pps_kcbind failed: %m");
373 /* instance->peer->precision = PPS_PRECISION; */
377 time_pps_getparams(instance->pps_handle, &instance->pps_params);
378 jupiter_debug(instance->peer, "refclock_jupiter",
379 "pps capability 0x%x version %d mode 0x%x kern %d",
380 capability, instance->pps_params.api_version,
381 instance->pps_params.mode, instance->hardpps);
389 * Get PPSAPI timestamps.
391 * Return 0 on failure and 1 on success.
394 jupiter_pps(struct instance *instance)
397 struct timespec timeout, ts;
402 * Convert the timespec nanoseconds field to ntp l_fp units.
404 if (instance->pps_handle == 0)
408 memcpy(&pps_info, &instance->pps_info, sizeof(pps_info_t));
409 if (time_pps_fetch(instance->pps_handle, PPS_TSFMT_TSPEC, &instance->pps_info,
412 if (instance->pps_params.mode & PPS_CAPTUREASSERT) {
413 if (pps_info.assert_sequence ==
414 instance->pps_info.assert_sequence)
416 ts = instance->pps_info.assert_timestamp;
417 } else if (instance->pps_params.mode & PPS_CAPTURECLEAR) {
418 if (pps_info.clear_sequence ==
419 instance->pps_info.clear_sequence)
421 ts = instance->pps_info.clear_timestamp;
425 if ((instance->ts.tv_sec == ts.tv_sec) && (instance->ts.tv_nsec == ts.tv_nsec))
429 tstmp.l_ui = ts.tv_sec + JAN_1970;
430 dtemp = ts.tv_nsec * FRAC / 1e9;
431 tstmp.l_uf = (u_int32)dtemp;
432 instance->peer->procptr->lastrec = tstmp;
435 #endif /* HAVE_PPSAPI */
438 * jupiter_poll - jupiter watchdog routine
441 jupiter_poll(int unit, struct peer *peer)
443 struct instance *instance;
444 struct refclockproc *pp;
447 instance = (struct instance *)pp->unitptr;
450 * You don't need to poll this clock. It puts out timecodes
451 * once per second. If asked for a timestamp, take note.
452 * The next time a timecode comes in, it will be fed back.
456 * If we haven't had a response in a while, reset the receiver.
458 if (instance->pollcnt > 0) {
461 refclock_report(peer, CEVNT_TIMEOUT);
463 /* Request the receiver id to trigger a reconfig */
464 jupiter_reqonemsg(instance, JUPITER_O_ID);
465 instance->wantid = 0;
469 * polled every 64 seconds. Ask jupiter_receive to hand in
472 instance->polled = 1;
477 * jupiter_control - fudge control
481 int unit, /* unit (not used) */
482 struct refclockstat *in, /* input parameters (not used) */
483 struct refclockstat *out, /* output parameters (not used) */
484 struct peer *peer /* peer structure pointer */
487 struct refclockproc *pp;
488 struct instance *instance;
489 u_char sloppyclockflag;
492 instance = (struct instance *)pp->unitptr;
494 DTOLFP(pp->fudgetime2, &instance->limit);
495 /* Force positive value. */
496 if (L_ISNEG(&instance->limit))
497 L_NEG(&instance->limit);
500 instance->assert = !(pp->sloppyclockflag & CLK_FLAG3);
501 jupiter_ppsapi(instance);
502 #endif /* HAVE_PPSAPI */
504 sloppyclockflag = instance->sloppyclockflag;
505 instance->sloppyclockflag = pp->sloppyclockflag;
506 if ((instance->sloppyclockflag & CLK_FLAG2) !=
507 (sloppyclockflag & CLK_FLAG2)) {
510 "mode switch: reset receiver");
511 jupiter_config(instance);
517 * jupiter_receive - receive gps data
521 jupiter_receive(struct recvbuf *rbufp)
523 int bpcnt, cc, size, ppsret;
524 time_t last_timecode;
532 struct refclockproc *pp;
533 struct instance *instance;
536 /* Initialize pointers and read the timecode and timestamp */
537 peer = (struct peer *)rbufp->recv_srcclock;
539 instance = (struct instance *)pp->unitptr;
541 bp = (u_char *)rbufp->recv_buffer;
542 bpcnt = rbufp->recv_length;
544 /* This shouldn't happen */
545 if (bpcnt > sizeof(instance->sbuf) - instance->ssize)
546 bpcnt = sizeof(instance->sbuf) - instance->ssize;
548 /* Append to input buffer */
549 memcpy((u_char *)instance->sbuf + instance->ssize, bp, bpcnt);
550 instance->ssize += bpcnt;
552 /* While there's at least a header and we parse an intact message */
553 while (instance->ssize > sizeof(*hp) && (cc = jupiter_recv(instance)) > 0) {
554 instance->pollcnt = 2;
556 tstamp = rbufp->recv_time;
557 hp = (struct jheader *)instance->sbuf;
558 sp = (u_short *)(hp + 1);
559 size = cc - sizeof(*hp);
560 switch (getshort(hp->id)) {
562 case JUPITER_O_PULSE:
563 if (size != sizeof(struct jpulse)) {
565 "jupiter_receive", "pulse: len %d != %u",
566 size, (int)sizeof(struct jpulse));
567 refclock_report(peer, CEVNT_BADREPLY);
572 * There appears to be a firmware bug related
573 * to the pulse message; in addition to the one
574 * per second messages, we get an extra pulse
575 * message once an hour (on the anniversary of
576 * the cold start). It seems to come 200 ms
577 * after the one requested. So if we've seen a
578 * pulse message in the last 210 ms, we skip
581 laststime = instance->stime;
582 instance->stime = DS2UI(((struct jpulse *)sp)->stime);
583 if (laststime != 0 && instance->stime - laststime <= 21) {
584 jupiter_debug(peer, "jupiter_receive",
585 "avoided firmware bug (stime %.2f, laststime %.2f)",
586 (double)instance->stime * 0.01, (double)laststime * 0.01);
590 /* Retrieve pps timestamp */
591 ppsret = jupiter_pps(instance);
594 * Add one second if msg received early
595 * (i.e. before limit, a.k.a. fudgetime2) in
598 L_SUB(&tstamp, &pp->lastrec);
599 if (!L_ISGEQ(&tstamp, &instance->limit))
602 /* Parse timecode (even when there's no pps) */
603 last_timecode = instance->timecode;
604 if ((cp = jupiter_parse_t(instance, sp)) != NULL) {
606 "jupiter_receive", "pulse: %s", cp);
610 /* Bail if we didn't get a pps timestamp */
614 /* Bail if we don't have the last timecode yet */
615 if (last_timecode == 0)
618 /* Add the new sample to a median filter */
619 tstamp.l_ui = JAN_1970 + last_timecode;
622 refclock_process_offset(pp, tstamp, pp->lastrec, pp->fudgetime1);
625 * The clock will blurt a timecode every second
626 * but we only want one when polled. If we
627 * havn't been polled, bail out.
629 if (!instance->polled)
631 instance->polled = 0;
634 * It's a live one! Remember this time.
637 pp->lastref = pp->lastrec;
638 refclock_receive(peer);
641 * If we get here - what we got from the clock is
644 refclock_report(peer, CEVNT_NOMINAL);
647 * We have succeeded in answering the poll.
648 * Turn off the flag and return
650 instance->polled = 0;
654 if (size != sizeof(struct jgpos)) {
656 "jupiter_receive", "gpos: len %d != %u",
657 size, (int)sizeof(struct jgpos));
658 refclock_report(peer, CEVNT_BADREPLY);
662 if ((cp = jupiter_parse_gpos(instance, sp)) != NULL) {
664 "jupiter_receive", "gpos: %s", cp);
670 if (size != sizeof(struct jid)) {
672 "jupiter_receive", "id: len %d != %u",
673 size, (int)sizeof(struct jid));
674 refclock_report(peer, CEVNT_BADREPLY);
678 * If we got this message because the Jupiter
679 * just powered instance, it needs to be reconfigured.
681 ip = (struct jid *)sp;
683 "jupiter_receive", "%s chan ver %s, %s (%s)",
684 ip->chans, ip->vers, ip->date, ip->opts);
686 "jupiter_receive: %s chan ver %s, %s (%s)",
687 ip->chans, ip->vers, ip->date, ip->opts);
688 if (instance->wantid)
689 instance->wantid = 0;
692 "jupiter_receive", "reset receiver");
693 jupiter_config(instance);
695 * Restore since jupiter_config() just
698 instance->ssize = cc;
704 "jupiter_receive", "unknown message id %d",
708 instance->ssize -= cc;
709 if (instance->ssize < 0) {
710 fprintf(stderr, "jupiter_recv: negative ssize!\n");
712 } else if (instance->ssize > 0)
713 memcpy(instance->sbuf, (u_char *)instance->sbuf + cc, instance->ssize);
718 jupiter_parse_t(struct instance *instance, u_short *sp)
724 time_t last_timecode;
727 jp = (struct jpulse *)sp;
729 /* The timecode is presented as seconds into the current GPS week */
730 sweek = DS2UI(jp->sweek) % WEEKSECS;
733 * If we don't know the current GPS week, calculate it from the
734 * current time. (It's too bad they didn't include this
735 * important value in the pulse message). We'd like to pick it
736 * up from one of the other messages like gpos or chan but they
737 * don't appear to be synchronous with time keeping and changes
738 * too soon (something like 10 seconds before the new GPS
741 * If we already know the current GPS week, increment it when
742 * we wrap into a new week.
744 if (instance->gweek == 0) {
745 if (!instance->gpos_gweek) {
746 return ("jupiter_parse_t: Unknown gweek");
749 instance->gweek = instance->gpos_gweek;
752 * Fix warps. GPOS has GPS time and PULSE has UTC.
753 * Plus, GPOS need not be completely in synch with
756 if (instance->gpos_sweek >= sweek) {
757 if ((instance->gpos_sweek - sweek) > WEEKSECS / 2)
761 if ((sweek - instance->gpos_sweek) > WEEKSECS / 2)
765 else if (sweek == 0 && instance->lastsweek == WEEKSECS - 1) {
767 jupiter_debug(instance->peer,
768 "jupiter_parse_t", "NEW gps week %u", instance->gweek);
772 * See if the sweek stayed the same (this happens when there is
775 * Otherwise, look for time warps:
777 * - we have stored at least one lastsweek and
778 * - the sweek didn't increase by one and
779 * - we didn't wrap to a new GPS week
783 if (instance->lastsweek == sweek)
784 jupiter_debug(instance->peer,
785 "jupiter_parse_t", "gps sweek not incrementing (%d)",
787 else if (instance->lastsweek != 2 * WEEKSECS &&
788 instance->lastsweek + 1 != sweek &&
789 !(sweek == 0 && instance->lastsweek == WEEKSECS - 1))
790 jupiter_debug(instance->peer,
791 "jupiter_parse_t", "gps sweek jumped (was %d, now %d)",
792 instance->lastsweek, sweek);
793 instance->lastsweek = sweek;
795 /* This timecode describes next pulse */
796 last_timecode = instance->timecode;
798 GPS_EPOCH + (instance->gweek * WEEKSECS) + sweek;
800 if (last_timecode == 0)
802 jupiter_debug(instance->peer,
803 "jupiter_parse_t", "UTC <none> (gweek/sweek %u/%u)",
804 instance->gweek, sweek);
807 tm = gmtime(&last_timecode);
810 jupiter_debug(instance->peer,
811 "jupiter_parse_t", "UTC %.24s (gweek/sweek %u/%u)",
812 cp, instance->gweek, sweek);
814 /* Billboard last_timecode (which is now the current time) */
815 instance->peer->procptr->year = tm->tm_year + 1900;
816 instance->peer->procptr->day = tm->tm_yday + 1;
817 instance->peer->procptr->hour = tm->tm_hour;
818 instance->peer->procptr->minute = tm->tm_min;
819 instance->peer->procptr->second = tm->tm_sec;
822 flags = getshort(jp->flags);
824 /* Toss if not designated "valid" by the gps */
825 if ((flags & JUPITER_O_PULSE_VALID) == 0) {
826 refclock_report(instance->peer, CEVNT_BADTIME);
827 return ("time mark not valid");
830 /* We better be sync'ed to UTC... */
831 if ((flags & JUPITER_O_PULSE_UTC) == 0) {
832 refclock_report(instance->peer, CEVNT_BADTIME);
833 return ("time mark not sync'ed to UTC");
840 jupiter_parse_gpos(struct instance *instance, u_short *sp)
847 jg = (struct jgpos *)sp;
849 if (jg->navval != 0) {
851 * Solution not valid. Use caution and refuse
852 * to determine GPS week from this message.
854 instance->gpos_gweek = 0;
855 instance->gpos_sweek = 0;
856 return ("Navigation solution not valid");
859 instance->gpos_gweek = jg->gweek;
860 instance->gpos_sweek = DS2UI(jg->sweek);
861 while(instance->gpos_sweek >= WEEKSECS) {
862 instance->gpos_sweek -= WEEKSECS;
863 ++instance->gpos_gweek;
867 t = GPS_EPOCH + (instance->gpos_gweek * WEEKSECS) + instance->gpos_sweek;
871 jupiter_debug(instance->peer,
872 "jupiter_parse_g", "GPS %.24s (gweek/sweek %u/%u)",
873 cp, instance->gpos_gweek, instance->gpos_sweek);
878 * jupiter_debug - print debug messages
880 #if defined(__STDC__) || defined(SYS_WINNT)
882 jupiter_debug(struct peer *peer, char *function, char *fmt, ...)
885 jupiter_debug(peer, function, fmt, va_alist)
889 #endif /* __STDC__ */
894 #if defined(__STDC__) || defined(SYS_WINNT)
898 #endif /* __STDC__ */
900 * Print debug message to stdout
901 * In the future, we may want to get get more creative...
903 vsnprintf(buffer, sizeof(buffer), fmt, ap);
904 record_clock_stats(&(peer->srcadr), buffer);
907 fprintf(stdout, "%s: ", function);
908 fprintf(stdout, buffer);
909 fprintf(stdout, "\n");
917 /* Checksum and transmit a message to the Jupiter */
919 jupiter_send(struct instance *instance, struct jheader *hp)
924 static char errstr[132];
927 hp->hsum = putshort(jupiter_cksum((u_short *)hp,
928 (size / sizeof(u_short)) - 1));
929 len = getshort(hp->len);
931 sp = (u_short *)(hp + 1);
932 sp[len] = putshort(jupiter_cksum(sp, len));
933 size += (len + 1) * sizeof(u_short);
936 if ((cc = write(instance->peer->procptr->io.fd, (char *)hp, size)) < 0) {
937 (void)sprintf(errstr, "write: %s", strerror(errno));
939 } else if (cc != size) {
940 (void)sprintf(errstr, "short write (%d != %d)", cc, size);
946 /* Request periodic message output */
948 struct jheader jheader;
949 struct jrequest jrequest;
951 { putshort(JUPITER_SYNC), 0,
952 putshort((sizeof(struct jrequest) / sizeof(u_short)) - 1),
953 0, JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK |
954 JUPITER_FLAG_CONN | JUPITER_FLAG_LOG, 0 },
958 /* An interval of zero means to output on trigger */
960 jupiter_reqmsg(struct instance *instance, u_int id,
967 hp = &reqmsg.jheader;
968 hp->id = putshort(id);
969 rp = &reqmsg.jrequest;
970 rp->trigger = putshort(interval == 0);
971 rp->interval = putshort(interval);
972 if ((cp = jupiter_send(instance, hp)) != NULL)
973 jupiter_debug(instance->peer, "jupiter_reqmsg", "%u: %s", id, cp);
976 /* Cancel periodic message output */
977 static struct jheader canmsg = {
978 putshort(JUPITER_SYNC), 0, 0, 0,
979 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_DISC,
984 jupiter_canmsg(struct instance *instance, u_int id)
990 hp->id = putshort(id);
991 if ((cp = jupiter_send(instance, hp)) != NULL)
992 jupiter_debug(instance->peer, "jupiter_canmsg", "%u: %s", id, cp);
995 /* Request a single message output */
996 static struct jheader reqonemsg = {
997 putshort(JUPITER_SYNC), 0, 0, 0,
998 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_QUERY,
1003 jupiter_reqonemsg(struct instance *instance, u_int id)
1009 hp->id = putshort(id);
1010 if ((cp = jupiter_send(instance, hp)) != NULL)
1011 jupiter_debug(instance->peer, "jupiter_reqonemsg", "%u: %s", id, cp);
1014 /* Set the platform dynamics */
1016 struct jheader jheader;
1019 { putshort(JUPITER_SYNC), putshort(JUPITER_I_PLAT),
1020 putshort((sizeof(struct jplat) / sizeof(u_short)) - 1), 0,
1021 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK, 0 },
1026 jupiter_platform(struct instance *instance, u_int platform)
1032 hp = &platmsg.jheader;
1033 pp = &platmsg.jplat;
1034 pp->platform = putshort(platform);
1035 if ((cp = jupiter_send(instance, hp)) != NULL)
1036 jupiter_debug(instance->peer, "jupiter_platform", "%u: %s", platform, cp);
1039 /* Checksum "len" shorts */
1041 jupiter_cksum(u_short *sp, u_int len)
1053 /* Return the size of the next message (or zero if we don't have it all yet) */
1055 jupiter_recv(struct instance *instance)
1057 int n, len, size, cc;
1062 /* Must have at least a header's worth */
1064 size = instance->ssize;
1068 /* Search for the sync short if missing */
1069 sp = instance->sbuf;
1070 hp = (struct jheader *)sp;
1071 if (getshort(hp->sync) != JUPITER_SYNC) {
1072 /* Wasn't at the front, sync up */
1073 jupiter_debug(instance->peer, "jupiter_recv", "syncing");
1077 if (bp[0] != (JUPITER_SYNC & 0xff)) {
1079 jupiter_debug(instance->peer, "{0x%x}", bp[0]);
1085 if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff))
1088 jupiter_debug(instance->peer, "{0x%x 0x%x}", bp[0], bp[1]);
1094 jupiter_debug(instance->peer, "\n");
1096 /* Shuffle data to front of input buffer */
1100 instance->ssize = size;
1101 if (size < cc || hp->sync != JUPITER_SYNC)
1105 if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) !=
1106 getshort(hp->hsum)) {
1107 jupiter_debug(instance->peer, "jupiter_recv", "bad header checksum!");
1108 /* This is drastic but checksum errors should be rare */
1109 instance->ssize = 0;
1113 /* Check for a payload */
1114 len = getshort(hp->len);
1116 n = (len + 1) * sizeof(u_short);
1117 /* Not enough data yet */
1121 /* Check payload checksum */
1122 sp = (u_short *)(hp + 1);
1123 if (jupiter_cksum(sp, len) != getshort(sp[len])) {
1124 jupiter_debug(instance->peer,
1125 "jupiter_recv", "bad payload checksum!");
1126 /* This is drastic but checksum errors should be rare */
1127 instance->ssize = 0;
1135 #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */
1136 int refclock_jupiter_bs;
1137 #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */