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 *,
143 __attribute__ ((format (printf, 3, 4)));
144 static const char * jupiter_parse_t (struct instance *, u_short *);
145 static const char * jupiter_parse_gpos (struct instance *, u_short *);
146 static void jupiter_platform (struct instance *, u_int);
147 static void jupiter_poll (int, struct peer *);
148 static void jupiter_control (int, const struct refclockstat *,
149 struct refclockstat *, struct peer *);
151 static int jupiter_ppsapi (struct instance *);
152 static int jupiter_pps (struct instance *);
153 #endif /* HAVE_PPSAPI */
154 static int jupiter_recv (struct instance *);
155 static void jupiter_receive (struct recvbuf *rbufp);
156 static void jupiter_reqmsg (struct instance *, u_int, u_int);
157 static void jupiter_reqonemsg(struct instance *, u_int);
158 static char * jupiter_send (struct instance *, struct jheader *);
159 static void jupiter_shutdown(int, struct peer *);
160 static int jupiter_start (int, struct peer *);
162 static u_int get_full_week(u_int base_week, u_int gpos_week);
163 static u_int get_base_week(void);
169 struct refclock refclock_jupiter = {
170 jupiter_start, /* start up driver */
171 jupiter_shutdown, /* shut down driver */
172 jupiter_poll, /* transmit poll message */
173 jupiter_control, /* (clock control) */
174 noentry, /* (clock init) */
175 noentry, /* (clock buginfo) */
176 NOFLAGS /* not used */
180 * jupiter_start - open the devices and initialize data for processing
188 struct refclockproc *pp;
189 struct instance *instance;
196 snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit);
197 fd = refclock_open(gpsdev, SPEED232, LDISC_RAW);
199 jupiter_debug(peer, "jupiter_start", "open %s: %m",
204 /* Allocate unit structure */
205 instance = emalloc_zero(sizeof(*instance));
206 instance->peer = peer;
208 pp->io.clock_recv = jupiter_receive;
209 pp->io.srcclock = peer;
212 if (!io_addclock(&pp->io)) {
218 pp->unitptr = instance;
221 * Initialize miscellaneous variables
223 peer->precision = PRECISION;
224 pp->clockdesc = DESCRIPTION;
225 memcpy((char *)&pp->refid, REFID, 4);
228 instance->assert = 1;
229 instance->hardpps = 0;
231 * Start the PPSAPI interface if it is there. Default to use
232 * the assert edge and do not enable the kernel hardpps.
234 if (time_pps_create(fd, &instance->pps_handle) < 0) {
235 instance->pps_handle = 0;
237 "refclock_jupiter: time_pps_create failed: %m");
239 else if (!jupiter_ppsapi(instance))
241 #endif /* HAVE_PPSAPI */
243 /* Ensure the receiver is properly configured */
244 if (!jupiter_config(instance))
250 jupiter_shutdown(unit, peer);
256 * jupiter_shutdown - shut down the clock
259 jupiter_shutdown(int unit, struct peer *peer)
261 struct instance *instance;
262 struct refclockproc *pp;
265 instance = pp->unitptr;
270 if (instance->pps_handle) {
271 time_pps_destroy(instance->pps_handle);
272 instance->pps_handle = 0;
274 #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, __func__, "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, __func__, "mobile platform");
297 instance->pollcnt = 2;
298 instance->polled = 0;
299 instance->gpos_gweek = 0;
300 instance->gpos_sweek = 0;
302 instance->lastsweek = 2 * WEEKSECS;
303 instance->timecode = 0;
307 /* Stop outputting all messages */
308 jupiter_canmsg(instance, JUPITER_ALL);
310 /* Request the receiver id so we can syslog the firmware version */
311 jupiter_reqonemsg(instance, JUPITER_O_ID);
313 /* Flag that this the id was requested (so we don't get called again) */
314 instance->wantid = 1;
316 /* Request perodic time mark pulse messages */
317 jupiter_reqmsg(instance, JUPITER_O_PULSE, 1);
319 /* Request perodic geodetic position status */
320 jupiter_reqmsg(instance, JUPITER_O_GPOS, 1);
322 /* Set application platform type */
323 if (instance->moving)
324 jupiter_platform(instance, JUPITER_I_PLAT_MED);
326 jupiter_platform(instance, JUPITER_I_PLAT_LOW);
337 struct instance *instance /* unit structure pointer */
342 if (time_pps_getcap(instance->pps_handle, &capability) < 0) {
344 "refclock_jupiter: time_pps_getcap failed: %m");
347 memset(&instance->pps_params, 0, sizeof(pps_params_t));
348 if (!instance->assert)
349 instance->pps_params.mode = capability & PPS_CAPTURECLEAR;
351 instance->pps_params.mode = capability & PPS_CAPTUREASSERT;
352 if (!(instance->pps_params.mode & (PPS_CAPTUREASSERT | PPS_CAPTURECLEAR))) {
354 "refclock_jupiter: invalid capture edge %d",
358 instance->pps_params.mode |= PPS_TSFMT_TSPEC;
359 if (time_pps_setparams(instance->pps_handle, &instance->pps_params) < 0) {
361 "refclock_jupiter: time_pps_setparams failed: %m");
364 if (instance->hardpps) {
365 if (time_pps_kcbind(instance->pps_handle, PPS_KC_HARDPPS,
366 instance->pps_params.mode & ~PPS_TSFMT_TSPEC,
367 PPS_TSFMT_TSPEC) < 0) {
369 "refclock_jupiter: time_pps_kcbind failed: %m");
374 /* instance->peer->precision = PPS_PRECISION; */
378 time_pps_getparams(instance->pps_handle, &instance->pps_params);
379 jupiter_debug(instance->peer, __func__,
380 "pps capability 0x%x version %d mode 0x%x kern %d",
381 capability, instance->pps_params.api_version,
382 instance->pps_params.mode, instance->hardpps);
390 * Get PPSAPI timestamps.
392 * Return 0 on failure and 1 on success.
395 jupiter_pps(struct instance *instance)
398 struct timespec timeout, ts;
403 * Convert the timespec nanoseconds field to ntp l_fp units.
405 if (instance->pps_handle == 0)
409 memcpy(&pps_info, &instance->pps_info, sizeof(pps_info_t));
410 if (time_pps_fetch(instance->pps_handle, PPS_TSFMT_TSPEC, &instance->pps_info,
413 if (instance->pps_params.mode & PPS_CAPTUREASSERT) {
414 if (pps_info.assert_sequence ==
415 instance->pps_info.assert_sequence)
417 ts = instance->pps_info.assert_timestamp;
418 } else if (instance->pps_params.mode & PPS_CAPTURECLEAR) {
419 if (pps_info.clear_sequence ==
420 instance->pps_info.clear_sequence)
422 ts = instance->pps_info.clear_timestamp;
426 if ((instance->ts.tv_sec == ts.tv_sec) && (instance->ts.tv_nsec == ts.tv_nsec))
430 tstmp.l_ui = (u_int32)ts.tv_sec + JAN_1970;
431 dtemp = ts.tv_nsec * FRAC / 1e9;
432 tstmp.l_uf = (u_int32)dtemp;
433 instance->peer->procptr->lastrec = tstmp;
436 #endif /* HAVE_PPSAPI */
439 * jupiter_poll - jupiter watchdog routine
442 jupiter_poll(int unit, struct peer *peer)
444 struct instance *instance;
445 struct refclockproc *pp;
448 instance = pp->unitptr;
451 * You don't need to poll this clock. It puts out timecodes
452 * once per second. If asked for a timestamp, take note.
453 * The next time a timecode comes in, it will be fed back.
457 * If we haven't had a response in a while, reset the receiver.
459 if (instance->pollcnt > 0) {
462 refclock_report(peer, CEVNT_TIMEOUT);
464 /* Request the receiver id to trigger a reconfig */
465 jupiter_reqonemsg(instance, JUPITER_O_ID);
466 instance->wantid = 0;
470 * polled every 64 seconds. Ask jupiter_receive to hand in
473 instance->polled = 1;
478 * jupiter_control - fudge control
482 int unit, /* unit (not used) */
483 const struct refclockstat *in, /* input parameters (not used) */
484 struct refclockstat *out, /* output parameters (not used) */
485 struct peer *peer /* peer structure pointer */
488 struct refclockproc *pp;
489 struct instance *instance;
490 u_char sloppyclockflag;
493 instance = pp->unitptr;
495 DTOLFP(pp->fudgetime2, &instance->limit);
496 /* Force positive value. */
497 if (L_ISNEG(&instance->limit))
498 L_NEG(&instance->limit);
501 instance->assert = !(pp->sloppyclockflag & CLK_FLAG3);
502 jupiter_ppsapi(instance);
503 #endif /* HAVE_PPSAPI */
505 sloppyclockflag = instance->sloppyclockflag;
506 instance->sloppyclockflag = pp->sloppyclockflag;
507 if ((instance->sloppyclockflag & CLK_FLAG2) !=
508 (sloppyclockflag & CLK_FLAG2)) {
509 jupiter_debug(peer, __func__,
510 "mode switch: reset receiver");
511 jupiter_config(instance);
517 * jupiter_receive - receive gps data
521 jupiter_receive(struct recvbuf *rbufp)
524 int cc, size, ppsret;
525 time_t last_timecode;
533 struct refclockproc *pp;
534 struct instance *instance;
537 /* Initialize pointers and read the timecode and timestamp */
538 peer = rbufp->recv_peer;
540 instance = pp->unitptr;
542 bp = (u_char *)rbufp->recv_buffer;
543 bpcnt = rbufp->recv_length;
545 /* This shouldn't happen */
546 if (bpcnt > sizeof(instance->sbuf) - instance->ssize)
547 bpcnt = sizeof(instance->sbuf) - instance->ssize;
549 /* Append to input buffer */
550 memcpy((u_char *)instance->sbuf + instance->ssize, bp, bpcnt);
551 instance->ssize += bpcnt;
553 /* While there's at least a header and we parse an intact message */
554 while (instance->ssize > (int)sizeof(*hp) && (cc = jupiter_recv(instance)) > 0) {
555 instance->pollcnt = 2;
557 tstamp = rbufp->recv_time;
558 hp = (struct jheader *)instance->sbuf;
559 sp = (u_short *)(hp + 1);
560 size = cc - sizeof(*hp);
561 switch (getshort(hp->id)) {
563 case JUPITER_O_PULSE:
564 if (size != sizeof(struct jpulse)) {
565 jupiter_debug(peer, __func__,
566 "pulse: len %d != %u",
567 size, (int)sizeof(struct jpulse));
568 refclock_report(peer, CEVNT_BADREPLY);
573 * There appears to be a firmware bug related
574 * to the pulse message; in addition to the one
575 * per second messages, we get an extra pulse
576 * message once an hour (on the anniversary of
577 * the cold start). It seems to come 200 ms
578 * after the one requested. So if we've seen a
579 * pulse message in the last 210 ms, we skip
582 laststime = instance->stime;
583 instance->stime = DS2UI(((struct jpulse *)sp)->stime);
584 if (laststime != 0 && instance->stime - laststime <= 21) {
585 jupiter_debug(peer, __func__,
586 "avoided firmware bug (stime %.2f, laststime %.2f)",
587 (double)instance->stime * 0.01, (double)laststime * 0.01);
591 /* Retrieve pps timestamp */
592 ppsret = jupiter_pps(instance);
595 * Add one second if msg received early
596 * (i.e. before limit, a.k.a. fudgetime2) in
599 L_SUB(&tstamp, &pp->lastrec);
600 if (!L_ISGEQ(&tstamp, &instance->limit))
603 /* Parse timecode (even when there's no pps) */
604 last_timecode = instance->timecode;
605 if ((cp = jupiter_parse_t(instance, sp)) != NULL) {
606 jupiter_debug(peer, __func__,
611 /* Bail if we didn't get a pps timestamp */
615 /* Bail if we don't have the last timecode yet */
616 if (last_timecode == 0)
619 /* Add the new sample to a median filter */
620 tstamp.l_ui = JAN_1970 + (u_int32)last_timecode;
623 refclock_process_offset(pp, tstamp, pp->lastrec, pp->fudgetime1);
626 * The clock will blurt a timecode every second
627 * but we only want one when polled. If we
628 * havn't been polled, bail out.
630 if (!instance->polled)
632 instance->polled = 0;
635 * It's a live one! Remember this time.
638 pp->lastref = pp->lastrec;
639 refclock_receive(peer);
642 * If we get here - what we got from the clock is
645 refclock_report(peer, CEVNT_NOMINAL);
648 * We have succeeded in answering the poll.
649 * Turn off the flag and return
651 instance->polled = 0;
655 if (size != sizeof(struct jgpos)) {
656 jupiter_debug(peer, __func__,
657 "gpos: len %d != %u",
658 size, (int)sizeof(struct jgpos));
659 refclock_report(peer, CEVNT_BADREPLY);
663 if ((cp = jupiter_parse_gpos(instance, sp)) != NULL) {
664 jupiter_debug(peer, __func__,
671 if (size != sizeof(struct jid)) {
672 jupiter_debug(peer, __func__,
674 size, (int)sizeof(struct jid));
675 refclock_report(peer, CEVNT_BADREPLY);
679 * If we got this message because the Jupiter
680 * just powered instance, it needs to be reconfigured.
682 ip = (struct jid *)sp;
683 jupiter_debug(peer, __func__,
684 "%s chan ver %s, %s (%s)",
685 ip->chans, ip->vers, ip->date, ip->opts);
687 "jupiter_receive: %s chan ver %s, %s (%s)",
688 ip->chans, ip->vers, ip->date, ip->opts);
689 if (instance->wantid)
690 instance->wantid = 0;
692 jupiter_debug(peer, __func__, "reset receiver");
693 jupiter_config(instance);
695 * Restore since jupiter_config() just
698 instance->ssize = cc;
703 jupiter_debug(peer, __func__, "unknown message id %d",
707 instance->ssize -= cc;
708 if (instance->ssize < 0) {
709 fprintf(stderr, "jupiter_recv: negative ssize!\n");
711 } else if (instance->ssize > 0)
712 memcpy(instance->sbuf, (u_char *)instance->sbuf + cc, instance->ssize);
717 jupiter_parse_t(struct instance *instance, u_short *sp)
723 time_t last_timecode;
726 jp = (struct jpulse *)sp;
728 /* The timecode is presented as seconds into the current GPS week */
729 sweek = DS2UI(jp->sweek) % WEEKSECS;
732 * If we don't know the current GPS week, calculate it from the
733 * current time. (It's too bad they didn't include this
734 * important value in the pulse message). We'd like to pick it
735 * up from one of the other messages like gpos or chan but they
736 * don't appear to be synchronous with time keeping and changes
737 * too soon (something like 10 seconds before the new GPS
740 * If we already know the current GPS week, increment it when
741 * we wrap into a new week.
743 if (instance->gweek == 0) {
744 if (!instance->gpos_gweek) {
745 return ("jupiter_parse_t: Unknown gweek");
748 instance->gweek = instance->gpos_gweek;
751 * Fix warps. GPOS has GPS time and PULSE has UTC.
752 * Plus, GPOS need not be completely in synch with
755 if (instance->gpos_sweek >= sweek) {
756 if ((instance->gpos_sweek - sweek) > WEEKSECS / 2)
760 if ((sweek - instance->gpos_sweek) > WEEKSECS / 2)
764 else if (sweek == 0 && instance->lastsweek == WEEKSECS - 1) {
766 jupiter_debug(instance->peer, __func__,
767 "NEW gps week %u", instance->gweek);
771 * See if the sweek stayed the same (this happens when there is
774 * Otherwise, look for time warps:
776 * - we have stored at least one lastsweek and
777 * - the sweek didn't increase by one and
778 * - we didn't wrap to a new GPS week
782 if (instance->lastsweek == sweek)
783 jupiter_debug(instance->peer, __func__,
784 "gps sweek not incrementing (%d)",
786 else if (instance->lastsweek != 2 * WEEKSECS &&
787 instance->lastsweek + 1 != sweek &&
788 !(sweek == 0 && instance->lastsweek == WEEKSECS - 1))
789 jupiter_debug(instance->peer, __func__,
790 "gps sweek jumped (was %d, now %d)",
791 instance->lastsweek, sweek);
792 instance->lastsweek = sweek;
794 /* This timecode describes next pulse */
795 last_timecode = instance->timecode;
797 GPS_EPOCH + (instance->gweek * WEEKSECS) + sweek;
799 if (last_timecode == 0)
801 jupiter_debug(instance->peer, __func__,
802 "UTC <none> (gweek/sweek %u/%u)",
803 instance->gweek, sweek);
806 tm = gmtime(&last_timecode);
809 jupiter_debug(instance->peer, __func__,
810 "UTC %.24s (gweek/sweek %u/%u)",
811 cp, instance->gweek, sweek);
813 /* Billboard last_timecode (which is now the current time) */
814 instance->peer->procptr->year = tm->tm_year + 1900;
815 instance->peer->procptr->day = tm->tm_yday + 1;
816 instance->peer->procptr->hour = tm->tm_hour;
817 instance->peer->procptr->minute = tm->tm_min;
818 instance->peer->procptr->second = tm->tm_sec;
821 flags = getshort(jp->flags);
823 /* Toss if not designated "valid" by the gps */
824 if ((flags & JUPITER_O_PULSE_VALID) == 0) {
825 refclock_report(instance->peer, CEVNT_BADTIME);
826 return ("time mark not valid");
829 /* We better be sync'ed to UTC... */
830 if ((flags & JUPITER_O_PULSE_UTC) == 0) {
831 refclock_report(instance->peer, CEVNT_BADTIME);
832 return ("time mark not sync'ed to UTC");
839 jupiter_parse_gpos(struct instance *instance, u_short *sp)
846 jg = (struct jgpos *)sp;
848 if (jg->navval != 0) {
850 * Solution not valid. Use caution and refuse
851 * to determine GPS week from this message.
853 instance->gpos_gweek = 0;
854 instance->gpos_sweek = 0;
855 return ("Navigation solution not valid");
858 instance->gpos_sweek = DS2UI(jg->sweek);
859 instance->gpos_gweek = get_full_week(get_base_week(),
860 getshort(jg->gweek));
862 /* according to the protocol spec, the seconds-in-week cannot
863 * exceed the nominal value: Is it really necessary to normalise
866 while(instance->gpos_sweek >= WEEKSECS) {
867 instance->gpos_sweek -= WEEKSECS;
868 ++instance->gpos_gweek;
872 t = GPS_EPOCH + (instance->gpos_gweek * WEEKSECS) + instance->gpos_sweek;
876 jupiter_debug(instance->peer, __func__,
877 "GPS %.24s (gweek/sweek %u/%u)",
878 cp, instance->gpos_gweek, instance->gpos_sweek);
883 * jupiter_debug - print debug messages
888 const char * function,
898 * Print debug message to stdout
899 * In the future, we may want to get get more creative...
901 mvsnprintf(buffer, sizeof(buffer), fmt, ap);
902 record_clock_stats(&peer->srcadr, buffer);
905 printf("%s: %s\n", function, buffer);
913 /* Checksum and transmit a message to the Jupiter */
915 jupiter_send(struct instance *instance, struct jheader *hp)
920 static char errstr[132];
923 hp->hsum = putshort(jupiter_cksum((u_short *)hp,
924 (size / sizeof(u_short)) - 1));
925 len = getshort(hp->len);
927 sp = (u_short *)(hp + 1);
928 sp[len] = putshort(jupiter_cksum(sp, len));
929 size += (len + 1) * sizeof(u_short);
932 if ((cc = write(instance->peer->procptr->io.fd, (char *)hp, size)) < 0) {
933 msnprintf(errstr, sizeof(errstr), "write: %m");
935 } else if (cc != (int)size) {
936 snprintf(errstr, sizeof(errstr), "short write (%zd != %u)", cc, size);
942 /* Request periodic message output */
944 struct jheader jheader;
945 struct jrequest jrequest;
947 { putshort(JUPITER_SYNC), 0,
948 putshort((sizeof(struct jrequest) / sizeof(u_short)) - 1),
949 0, JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK |
950 JUPITER_FLAG_CONN | JUPITER_FLAG_LOG, 0 },
954 /* An interval of zero means to output on trigger */
956 jupiter_reqmsg(struct instance *instance, u_int id,
963 hp = &reqmsg.jheader;
964 hp->id = putshort(id);
965 rp = &reqmsg.jrequest;
966 rp->trigger = putshort(interval == 0);
967 rp->interval = putshort(interval);
968 if ((cp = jupiter_send(instance, hp)) != NULL)
969 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
972 /* Cancel periodic message output */
973 static struct jheader canmsg = {
974 putshort(JUPITER_SYNC), 0, 0, 0,
975 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_DISC,
980 jupiter_canmsg(struct instance *instance, u_int id)
986 hp->id = putshort(id);
987 if ((cp = jupiter_send(instance, hp)) != NULL)
988 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
991 /* Request a single message output */
992 static struct jheader reqonemsg = {
993 putshort(JUPITER_SYNC), 0, 0, 0,
994 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_QUERY,
999 jupiter_reqonemsg(struct instance *instance, u_int id)
1005 hp->id = putshort(id);
1006 if ((cp = jupiter_send(instance, hp)) != NULL)
1007 jupiter_debug(instance->peer, __func__, "%u: %s", id, cp);
1010 /* Set the platform dynamics */
1012 struct jheader jheader;
1015 { putshort(JUPITER_SYNC), putshort(JUPITER_I_PLAT),
1016 putshort((sizeof(struct jplat) / sizeof(u_short)) - 1), 0,
1017 JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK, 0 },
1022 jupiter_platform(struct instance *instance, u_int platform)
1028 hp = &platmsg.jheader;
1029 pp = &platmsg.jplat;
1030 pp->platform = putshort(platform);
1031 if ((cp = jupiter_send(instance, hp)) != NULL)
1032 jupiter_debug(instance->peer, __func__, "%u: %s", platform, cp);
1035 /* Checksum "len" shorts */
1037 jupiter_cksum(u_short *sp, u_int len)
1049 /* Return the size of the next message (or zero if we don't have it all yet) */
1051 jupiter_recv(struct instance *instance)
1053 int n, len, size, cc;
1058 /* Must have at least a header's worth */
1060 size = instance->ssize;
1064 /* Search for the sync short if missing */
1065 sp = instance->sbuf;
1066 hp = (struct jheader *)sp;
1067 if (getshort(hp->sync) != JUPITER_SYNC) {
1068 /* Wasn't at the front, sync up */
1069 jupiter_debug(instance->peer, __func__, "syncing");
1073 if (bp[0] != (JUPITER_SYNC & 0xff)) {
1075 jupiter_debug(instance->peer, __func__,
1082 if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff))
1085 jupiter_debug(instance->peer, __func__,
1086 "{0x%x 0x%x}", bp[0], bp[1]);
1092 jupiter_debug(instance->peer, __func__, "\n");
1094 /* Shuffle data to front of input buffer */
1098 instance->ssize = size;
1099 if (size < cc || hp->sync != JUPITER_SYNC)
1103 if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) !=
1104 getshort(hp->hsum)) {
1105 jupiter_debug(instance->peer, __func__, "bad header checksum!");
1106 /* This is drastic but checksum errors should be rare */
1107 instance->ssize = 0;
1111 /* Check for a payload */
1112 len = getshort(hp->len);
1114 n = (len + 1) * sizeof(u_short);
1115 /* Not enough data yet */
1119 /* Check payload checksum */
1120 sp = (u_short *)(hp + 1);
1121 if (jupiter_cksum(sp, len) != getshort(sp[len])) {
1122 jupiter_debug(instance->peer,
1123 __func__, "bad payload checksum!");
1124 /* This is drastic but checksum errors should be rare */
1125 instance->ssize = 0;
1136 static int init_done /* = 0 */;
1137 static u_int base_week;
1139 /* Get the build date, convert to days since GPS epoch and
1140 * finally weeks since GPS epoch. Note that the build stamp is
1141 * trusted once it is fetched -- only dates before the GPS epoch
1142 * are not permitted. This will permit proper synchronisation
1143 * for a time range of 1024 weeks starting with 00:00:00 of the
1144 * last Sunday on or before the build time.
1146 * If the impossible happens and fetching the build date fails,
1147 * a 1024-week cycle starting with 2016-01-03 is assumed to
1148 * avoid catastropic errors. This will work until 2035-08-19.
1152 if (ntpcal_get_build_date(&bd)) {
1153 int32_t days = ntpcal_date_to_rd(&bd);
1154 if (days > RDN_GPS_EPOCH)
1155 days -= RDN_GPS_EPOCH;
1158 base_week = days / 7;
1160 base_week = 1878; /* 2016-01-03, Sunday */
1162 "refclock_jupiter: ntpcal_get_build_date() failed: %s",
1163 "using 2016-01-03 as GPS base!");
1176 /* Periodic extension on base week. Since the period is 1024
1177 * weeks and we do unsigned arithmetic here, we can do wonderful
1178 * things with masks and the well-defined overflow behaviour.
1180 return base_week + ((gpos_week - base_week) & 1023);
1183 #else /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */
1184 int refclock_jupiter_bs;
1185 #endif /* not (REFCLOCK && CLOCK_JUPITER && HAVE_PPSAPI) */