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 /* Rata Die Number of first day of GPS epoch. This is the number of days
93 * since 0000-12-31 to 1980-01-06 in the proleptic Gregorian Calendar.
95 #define RDN_GPS_EPOCH (4*146097 + 138431 + 1)
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 (struct instance *, u_int);
139 static u_short jupiter_cksum (u_short *, u_int);
140 static int jupiter_config (struct instance *);
141 static void jupiter_debug (struct peer *, const char *,
142 const char *, ...) NTP_PRINTF(3, 4);
143 static const char * jupiter_parse_t (struct instance *, u_short *);
144 static const char * jupiter_parse_gpos (struct instance *, u_short *);
145 static void jupiter_platform (struct instance *, u_int);
146 static void jupiter_poll (int, struct peer *);
147 static void jupiter_control (int, const struct refclockstat *,
148 struct refclockstat *, struct peer *);
150 static int jupiter_ppsapi (struct instance *);
151 static int jupiter_pps (struct instance *);
152 #endif /* HAVE_PPSAPI */
153 static int jupiter_recv (struct instance *);
154 static void jupiter_receive (struct recvbuf *rbufp);
155 static void jupiter_reqmsg (struct instance *, u_int, u_int);
156 static void jupiter_reqonemsg(struct instance *, u_int);
157 static char * jupiter_send (struct instance *, struct jheader *);
158 static void jupiter_shutdown(int, struct peer *);
159 static int jupiter_start (int, struct peer *);
161 static u_int get_full_week(u_int base_week, u_int gpos_week);
162 static u_int get_base_week(void);
168 struct refclock refclock_jupiter = {
169 jupiter_start, /* start up driver */
170 jupiter_shutdown, /* shut down driver */
171 jupiter_poll, /* transmit poll message */
172 jupiter_control, /* (clock control) */
173 noentry, /* (clock init) */
174 noentry, /* (clock buginfo) */
175 NOFLAGS /* not used */
179 * jupiter_start - open the devices and initialize data for processing
187 struct refclockproc *pp;
188 struct instance *instance;
195 snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit);
196 fd = refclock_open(gpsdev, SPEED232, LDISC_RAW);
198 jupiter_debug(peer, "jupiter_start", "open %s: %m",
203 /* Allocate unit structure */
204 instance = emalloc_zero(sizeof(*instance));
205 instance->peer = peer;
207 pp->io.clock_recv = jupiter_receive;
208 pp->io.srcclock = peer;
211 if (!io_addclock(&pp->io)) {
217 pp->unitptr = 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 = pp->unitptr;
269 if (instance->pps_handle) {
270 time_pps_destroy(instance->pps_handle);
271 instance->pps_handle = 0;
273 #endif /* HAVE_PPSAPI */
276 io_closeclock(&pp->io);
281 * jupiter_config - Configure the receiver
284 jupiter_config(struct instance *instance)
286 jupiter_debug(instance->peer, __func__, "init receiver");
289 * Initialize the unit variables
291 instance->sloppyclockflag = instance->peer->procptr->sloppyclockflag;
292 instance->moving = !!(instance->sloppyclockflag & CLK_FLAG2);
293 if (instance->moving)
294 jupiter_debug(instance->peer, __func__, "mobile platform");
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, __func__,
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 = (u_int32)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 = 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 const 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 = 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)) {
508 jupiter_debug(peer, __func__,
509 "mode switch: reset receiver");
510 jupiter_config(instance);
516 * jupiter_receive - receive gps data
520 jupiter_receive(struct recvbuf *rbufp)
523 int 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 = rbufp->recv_peer;
539 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 > (int)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)) {
564 jupiter_debug(peer, __func__,
565 "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, __func__,
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) {
605 jupiter_debug(peer, __func__,
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 + (u_int32)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)) {
655 jupiter_debug(peer, __func__,
656 "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) {
663 jupiter_debug(peer, __func__,
670 if (size != sizeof(struct jid)) {
671 jupiter_debug(peer, __func__,
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;
682 jupiter_debug(peer, __func__,
683 "%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;
691 jupiter_debug(peer, __func__, "reset receiver");
692 jupiter_config(instance);
694 * Restore since jupiter_config() just
697 instance->ssize = cc;
702 jupiter_debug(peer, __func__, "unknown message id %d",
706 instance->ssize -= cc;
707 if (instance->ssize < 0) {
708 fprintf(stderr, "jupiter_recv: negative ssize!\n");
710 } else if (instance->ssize > 0)
711 memcpy(instance->sbuf, (u_char *)instance->sbuf + cc, instance->ssize);
716 jupiter_parse_t(struct instance *instance, u_short *sp)
722 time_t last_timecode;
725 jp = (struct jpulse *)sp;
727 /* The timecode is presented as seconds into the current GPS week */
728 sweek = DS2UI(jp->sweek) % WEEKSECS;
731 * If we don't know the current GPS week, calculate it from the
732 * current time. (It's too bad they didn't include this
733 * important value in the pulse message). We'd like to pick it
734 * up from one of the other messages like gpos or chan but they
735 * don't appear to be synchronous with time keeping and changes
736 * too soon (something like 10 seconds before the new GPS
739 * If we already know the current GPS week, increment it when
740 * we wrap into a new week.
742 if (instance->gweek == 0) {
743 if (!instance->gpos_gweek) {
744 return ("jupiter_parse_t: Unknown gweek");
747 instance->gweek = instance->gpos_gweek;
750 * Fix warps. GPOS has GPS time and PULSE has UTC.
751 * Plus, GPOS need not be completely in synch with
754 if (instance->gpos_sweek >= sweek) {
755 if ((instance->gpos_sweek - sweek) > WEEKSECS / 2)
759 if ((sweek - instance->gpos_sweek) > WEEKSECS / 2)
763 else if (sweek == 0 && instance->lastsweek == WEEKSECS - 1) {
765 jupiter_debug(instance->peer, __func__,
766 "NEW gps week %u", instance->gweek);
770 * See if the sweek stayed the same (this happens when there is
773 * Otherwise, look for time warps:
775 * - we have stored at least one lastsweek and
776 * - the sweek didn't increase by one and
777 * - we didn't wrap to a new GPS week
781 if (instance->lastsweek == sweek)
782 jupiter_debug(instance->peer, __func__,
783 "gps sweek not incrementing (%d)",
785 else if (instance->lastsweek != 2 * WEEKSECS &&
786 instance->lastsweek + 1 != sweek &&
787 !(sweek == 0 && instance->lastsweek == WEEKSECS - 1))
788 jupiter_debug(instance->peer, __func__,
789 "gps sweek jumped (was %d, now %d)",
790 instance->lastsweek, sweek);
791 instance->lastsweek = sweek;
793 /* This timecode describes next pulse */
794 last_timecode = instance->timecode;
796 GPS_EPOCH + (instance->gweek * WEEKSECS) + sweek;
798 if (last_timecode == 0)
800 jupiter_debug(instance->peer, __func__,
801 "UTC <none> (gweek/sweek %u/%u)",
802 instance->gweek, sweek);
805 tm = gmtime(&last_timecode);
808 jupiter_debug(instance->peer, __func__,
809 "UTC %.24s (gweek/sweek %u/%u)",
810 cp, instance->gweek, sweek);
812 /* Billboard last_timecode (which is now the current time) */
813 instance->peer->procptr->year = tm->tm_year + 1900;
814 instance->peer->procptr->day = tm->tm_yday + 1;
815 instance->peer->procptr->hour = tm->tm_hour;
816 instance->peer->procptr->minute = tm->tm_min;
817 instance->peer->procptr->second = tm->tm_sec;
820 flags = getshort(jp->flags);
822 /* Toss if not designated "valid" by the gps */
823 if ((flags & JUPITER_O_PULSE_VALID) == 0) {
824 refclock_report(instance->peer, CEVNT_BADTIME);
825 return ("time mark not valid");
828 /* We better be sync'ed to UTC... */
829 if ((flags & JUPITER_O_PULSE_UTC) == 0) {
830 refclock_report(instance->peer, CEVNT_BADTIME);
831 return ("time mark not sync'ed to UTC");
838 jupiter_parse_gpos(struct instance *instance, u_short *sp)
845 jg = (struct jgpos *)sp;
847 if (jg->navval != 0) {
849 * Solution not valid. Use caution and refuse
850 * to determine GPS week from this message.
852 instance->gpos_gweek = 0;
853 instance->gpos_sweek = 0;
854 return ("Navigation solution not valid");
857 instance->gpos_sweek = DS2UI(jg->sweek);
858 instance->gpos_gweek = get_full_week(get_base_week(),
859 getshort(jg->gweek));
861 /* according to the protocol spec, the seconds-in-week cannot
862 * exceed the nominal value: Is it really necessary to normalise
865 while(instance->gpos_sweek >= WEEKSECS) {
866 instance->gpos_sweek -= WEEKSECS;
867 ++instance->gpos_gweek;
871 t = GPS_EPOCH + (instance->gpos_gweek * WEEKSECS) + instance->gpos_sweek;
875 jupiter_debug(instance->peer, __func__,
876 "GPS %.24s (gweek/sweek %u/%u)",
877 cp, instance->gpos_gweek, instance->gpos_sweek);
882 * jupiter_debug - print debug messages
887 const char * function,
897 * Print debug message to stdout
898 * In the future, we may want to get get more creative...
900 mvsnprintf(buffer, sizeof(buffer), fmt, ap);
901 record_clock_stats(&peer->srcadr, buffer);
904 printf("%s: %s\n", function, buffer);
912 /* Checksum and transmit a message to the Jupiter */
914 jupiter_send(struct instance *instance, struct jheader *hp)
919 static char errstr[132];
922 hp->hsum = putshort(jupiter_cksum((u_short *)hp,
923 (size / sizeof(u_short)) - 1));
924 len = getshort(hp->len);
926 sp = (u_short *)(hp + 1);
927 sp[len] = putshort(jupiter_cksum(sp, len));
928 size += (len + 1) * sizeof(u_short);
931 if ((cc = write(instance->peer->procptr->io.fd, (char *)hp, size)) < 0) {
932 msnprintf(errstr, sizeof(errstr), "write: %m");
934 } else if (cc != (int)size) {
935 snprintf(errstr, sizeof(errstr), "short write (%zd != %u)", cc, size);
941 /* Request periodic message output */
943 struct jheader jheader;
944 struct jrequest jrequest;
946 { putshort(JUPITER_SYNC), 0,
947 putshort((sizeof(struct jrequest) / sizeof(u_short)) - 1),
948 0, JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK |
949 JUPITER_FLAG_CONN | JUPITER_FLAG_LOG, 0 },
953 /* An interval of zero means to output on trigger */
955 jupiter_reqmsg(struct instance *instance, u_int id,
962 hp = &reqmsg.jheader;
963 hp->id = putshort(id);
964 rp = &reqmsg.jrequest;
965 rp->trigger = putshort(interval == 0);
966 rp->interval = putshort(interval);
967 if ((cp = jupiter_send(instance, hp)) != NULL)
968 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
971 /* Cancel periodic message output */
972 static struct jheader canmsg = {
973 putshort(JUPITER_SYNC), 0, 0, 0,
974 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_DISC,
979 jupiter_canmsg(struct instance *instance, u_int id)
985 hp->id = putshort(id);
986 if ((cp = jupiter_send(instance, hp)) != NULL)
987 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
990 /* Request a single message output */
991 static struct jheader reqonemsg = {
992 putshort(JUPITER_SYNC), 0, 0, 0,
993 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_QUERY,
998 jupiter_reqonemsg(struct instance *instance, u_int id)
1004 hp->id = putshort(id);
1005 if ((cp = jupiter_send(instance, hp)) != NULL)
1006 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
1009 /* Set the platform dynamics */
1011 struct jheader jheader;
1014 { putshort(JUPITER_SYNC), putshort(JUPITER_I_PLAT),
1015 putshort((sizeof(struct jplat) / sizeof(u_short)) - 1), 0,
1016 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK, 0 },
1021 jupiter_platform(struct instance *instance, u_int platform)
1027 hp = &platmsg.jheader;
1028 pp = &platmsg.jplat;
1029 pp->platform = putshort(platform);
1030 if ((cp = jupiter_send(instance, hp)) != NULL)
1031 jupiter_debug(instance->peer, __func__, "%u: %s", platform, cp);
1034 /* Checksum "len" shorts */
1036 jupiter_cksum(u_short *sp, u_int len)
1048 /* Return the size of the next message (or zero if we don't have it all yet) */
1050 jupiter_recv(struct instance *instance)
1052 int n, len, size, cc;
1057 /* Must have at least a header's worth */
1059 size = instance->ssize;
1063 /* Search for the sync short if missing */
1064 sp = instance->sbuf;
1065 hp = (struct jheader *)sp;
1066 if (getshort(hp->sync) != JUPITER_SYNC) {
1067 /* Wasn't at the front, sync up */
1068 jupiter_debug(instance->peer, __func__, "syncing");
1072 if (bp[0] != (JUPITER_SYNC & 0xff)) {
1074 jupiter_debug(instance->peer, __func__,
1081 if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff))
1084 jupiter_debug(instance->peer, __func__,
1085 "{0x%x 0x%x}", bp[0], bp[1]);
1091 jupiter_debug(instance->peer, __func__, "\n");
1093 /* Shuffle data to front of input buffer */
1097 instance->ssize = size;
1098 if (size < cc || hp->sync != JUPITER_SYNC)
1102 if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) !=
1103 getshort(hp->hsum)) {
1104 jupiter_debug(instance->peer, __func__, "bad header checksum!");
1105 /* This is drastic but checksum errors should be rare */
1106 instance->ssize = 0;
1110 /* Check for a payload */
1111 len = getshort(hp->len);
1113 n = (len + 1) * sizeof(u_short);
1114 /* Not enough data yet */
1118 /* Check payload checksum */
1119 sp = (u_short *)(hp + 1);
1120 if (jupiter_cksum(sp, len) != getshort(sp[len])) {
1121 jupiter_debug(instance->peer,
1122 __func__, "bad payload checksum!");
1123 /* This is drastic but checksum errors should be rare */
1124 instance->ssize = 0;
1135 static int init_done /* = 0 */;
1136 static u_int base_week;
1138 /* Get the build date, convert to days since GPS epoch and
1139 * finally weeks since GPS epoch. Note that the build stamp is
1140 * trusted once it is fetched -- only dates before the GPS epoch
1141 * are not permitted. This will permit proper synchronisation
1142 * for a time range of 1024 weeks starting with 00:00:00 of the
1143 * last Sunday on or before the build time.
1145 * If the impossible happens and fetching the build date fails,
1146 * a 1024-week cycle starting with 2016-01-03 is assumed to
1147 * avoid catastropic errors. This will work until 2035-08-19.
1151 if (ntpcal_get_build_date(&bd)) {
1152 int32_t days = ntpcal_date_to_rd(&bd);
1153 if (days > RDN_GPS_EPOCH)
1154 days -= RDN_GPS_EPOCH;
1157 base_week = days / 7;
1159 base_week = 1878; /* 2016-01-03, Sunday */
1161 "refclock_jupiter: ntpcal_get_build_date() failed: %s",
1162 "using 2016-01-03 as GPS base!");
1175 /* Periodic extension on base week. Since the period is 1024
1176 * weeks and we do unsigned arithmetic here, we can do wonderful
1177 * things with masks and the well-defined overflow behaviour.
1179 return base_week + ((gpos_week - base_week) & 1023);
1182 #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */
1183 int refclock_jupiter_bs;
1184 #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */