2 * Copyright (c) 1997, 1998
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(PPS)
42 #include "ntp_refclock.h"
43 #include "ntp_unixtime.h"
44 #include "ntp_stdlib.h"
45 #include "ntp_calendar.h"
52 #include <sys/ppsclock.h>
54 #ifdef XNTP_BIG_ENDIAN
55 #define getshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
56 #define putshort(s) ((((s) & 0xff) << 8) | (((s) >> 8) & 0xff))
58 #define getshort(s) (s)
59 #define putshort(s) (s)
68 * This driver supports the Rockwell Jupiter GPS Receiver board
69 * adapted to precision timing applications. It requires the
70 * ppsclock line discipline or streams module described in the
71 * Line Disciplines and Streams Drivers page. It also requires a
72 * gadget box and 1-PPS level converter, such as described in the
73 * Pulse-per-second (PPS) Signal Interfacing page.
75 * It may work (with minor modifications) with other Rockwell GPS
76 * receivers such as the CityTracker.
82 #define DEVICE "/dev/gps%d" /* device name and unit */
83 #define SPEED232 B9600 /* baud */
86 * The number of raw samples which we acquire to derive a single estimate.
87 * NSAMPLES ideally should not exceed the default poll interval 64.
88 * NKEEP must be a power of 2 to simplify the averaging process.
92 #define REFCLOCKMAXDISPERSE .25 /* max sample dispersion */
95 * Radio interface parameters
97 #define PRECISION (-18) /* precision assumed (about 4 us) */
98 #define REFID "GPS\0" /* reference id */
99 #define DESCRIPTION "Rockwell Jupiter GPS Receiver" /* who we are */
100 #define DEFFUDGETIME 0 /* default fudge time (ms) */
102 /* Unix timestamp for the GPS epoch: January 6, 1980 */
103 #define GPS_EPOCH 315964800
105 /* Double short to unsigned int */
106 #define DS2UI(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
108 /* Double short to signed int */
109 #define DS2I(p) ((getshort((p)[1]) << 16) | getshort((p)[0]))
111 /* One week's worth of seconds */
112 #define WEEKSECS (7 * 24 * 60 * 60)
115 * Jupiter unit control structure.
118 u_int pollcnt; /* poll message counter */
119 u_int polled; /* Hand in a time sample? */
120 u_int lastserial; /* last pps serial number */
121 struct ppsclockev ppsev; /* PPS control structure */
122 u_int gweek; /* current GPS week number */
123 u_int32 lastsweek; /* last seconds into GPS week */
124 u_int32 timecode; /* current ntp timecode */
125 u_int32 stime; /* used to detect firmware bug */
126 int wantid; /* don't reconfig on channel id msg */
127 u_int moving; /* mobile platform? */
128 u_long sloppyclockflag; /* fudge flags */
129 u_int known; /* position known yet? */
130 int coderecv; /* total received samples */
131 int nkeep; /* number of samples to preserve */
132 int rshift; /* number of rshifts for division */
133 l_fp filter[NSAMPLES]; /* offset filter */
134 l_fp lastref; /* last reference timestamp */
135 u_short sbuf[512]; /* local input buffer */
136 int ssize; /* space used in sbuf */
140 * Function prototypes
142 static void jupiter_canmsg P((struct peer *, u_int));
143 static u_short jupiter_cksum P((u_short *, u_int));
144 #ifdef QSORT_USES_VOID_P
145 int jupiter_cmpl_fp P((const void *, const void *));
147 int jupiter_cmpl_fp P((const l_fp *, const l_fp *));
148 #endif /* not QSORT_USES_VOID_P */
149 static void jupiter_config P((struct peer *));
150 static void jupiter_debug P((struct peer *, char *, ...))
151 __attribute__ ((format (printf, 2, 3)));
152 static char * jupiter_offset P((struct peer *));
153 static char * jupiter_parse_t P((struct peer *, u_short *));
154 static void jupiter_platform P((struct peer *, u_int));
155 static void jupiter_poll P((int, struct peer *));
156 static int jupiter_pps P((struct peer *));
157 static char * jupiter_process P((struct peer *));
158 static int jupiter_recv P((struct peer *));
159 static void jupiter_receive P((register struct recvbuf *rbufp));
160 static void jupiter_reqmsg P((struct peer *, u_int, u_int));
161 static void jupiter_reqonemsg P((struct peer *, u_int));
162 static char * jupiter_send P((struct peer *, struct jheader *));
163 static void jupiter_shutdown P((int, struct peer *));
164 static int jupiter_start P((int, struct peer *));
165 static int jupiter_ttyinit P((struct peer *, int));
170 struct refclock refclock_jupiter = {
171 jupiter_start, /* start up driver */
172 jupiter_shutdown, /* shut down driver */
173 jupiter_poll, /* transmit poll message */
174 noentry, /* (clock control) */
175 noentry, /* (clock init) */
176 noentry, /* (clock buginfo) */
177 NOFLAGS /* not used */
181 * jupiter_start - open the devices and initialize data for processing
186 register struct peer *peer
189 struct refclockproc *pp;
190 register struct jupiterunit *up;
197 (void)sprintf(gpsdev, DEVICE, unit);
198 fd = open(gpsdev, O_RDWR
204 jupiter_debug(peer, "jupiter_start: open %s: %s\n",
205 gpsdev, strerror(errno));
208 if (!jupiter_ttyinit(peer, fd))
211 /* Allocate unit structure */
212 if ((up = (struct jupiterunit *)
213 emalloc(sizeof(struct jupiterunit))) == NULL) {
217 memset((char *)up, 0, sizeof(struct jupiterunit));
219 pp->io.clock_recv = jupiter_receive;
220 pp->io.srcclock = (caddr_t)peer;
223 if (!io_addclock(&pp->io)) {
228 pp->unitptr = (caddr_t)up;
231 * Initialize miscellaneous variables
233 peer->precision = PRECISION;
234 pp->clockdesc = DESCRIPTION;
235 memcpy((char *)&pp->refid, REFID, 4);
238 /* Ensure the receiver is properly configured */
239 jupiter_config(peer);
241 /* Turn on pulse gathering by requesting the first sample */
242 if (ioctl(fd, CIOGETEV, (caddr_t)&up->ppsev) < 0) {
243 jupiter_debug(peer, "jupiter_ttyinit: CIOGETEV: %s\n",
249 up->lastserial = up->ppsev.serial;
250 memset(&up->ppsev, 0, sizeof(up->ppsev));
255 * jupiter_shutdown - shut down the clock
258 jupiter_shutdown(register int unit, register struct peer *peer)
260 register struct jupiterunit *up;
261 struct refclockproc *pp;
264 up = (struct jupiterunit *)pp->unitptr;
265 io_closeclock(&pp->io);
270 * jupiter_config - Configure the receiver
273 jupiter_config(register struct peer *peer)
276 register struct jupiterunit *up;
277 register struct refclockproc *pp;
280 up = (struct jupiterunit *)pp->unitptr;
283 * Initialize the unit variables
285 * STRANGE BEHAVIOUR WARNING: The fudge flags are not available
286 * at the time jupiter_start is called. These are set later,
287 * and so the code must be prepared to handle changing flags.
289 up->sloppyclockflag = pp->sloppyclockflag;
290 if (pp->sloppyclockflag & CLK_FLAG2) {
291 up->moving = 1; /* Receiver on mobile platform */
292 msyslog(LOG_DEBUG, "jupiter_config: mobile platform");
294 up->moving = 0; /* Static Installation */
297 /* XXX fludge flags don't make the trip from the config to here... */
299 /* Configure for trailing edge triggers */
301 i = ((pp->sloppyclockflag & CLK_FLAG3) != 0);
302 jupiter_debug(peer, "jupiter_configure: (sloppyclockflag 0x%lx)\n",
303 pp->sloppyclockflag);
304 if (ioctl(pp->io.fd, CIOSETTET, (char *)&i) < 0)
305 msyslog(LOG_DEBUG, "jupiter_configure: CIOSETTET %d: %m", i);
307 if (pp->sloppyclockflag & CLK_FLAG3)
308 msyslog(LOG_DEBUG, "jupiter_configure: \
309 No kernel support for trailing edge trigger");
317 up->lastsweek = 2 * WEEKSECS;
323 if (up->nkeep > NSAMPLES)
324 up->nkeep = NSAMPLES;
346 /* Stop outputting all messages */
347 jupiter_canmsg(peer, JUPITER_ALL);
349 /* Request the receiver id so we can syslog the firmware version */
350 jupiter_reqonemsg(peer, JUPITER_O_ID);
352 /* Flag that this the id was requested (so we don't get called again) */
355 /* Request perodic time mark pulse messages */
356 jupiter_reqmsg(peer, JUPITER_O_PULSE, 1);
358 /* Set application platform type */
360 jupiter_platform(peer, JUPITER_I_PLAT_MED);
362 jupiter_platform(peer, JUPITER_I_PLAT_LOW);
366 * jupiter_poll - jupiter watchdog routine
369 jupiter_poll(register int unit, register struct peer *peer)
371 register struct jupiterunit *up;
372 register struct refclockproc *pp;
375 up = (struct jupiterunit *)pp->unitptr;
378 * You don't need to poll this clock. It puts out timecodes
379 * once per second. If asked for a timestamp, take note.
380 * The next time a timecode comes in, it will be fed back.
384 * If we haven't had a response in a while, reset the receiver.
386 if (up->pollcnt > 0) {
389 refclock_report(peer, CEVNT_TIMEOUT);
391 /* Request the receiver id to trigger a reconfig */
392 jupiter_reqonemsg(peer, JUPITER_O_ID);
397 * polled every 64 seconds. Ask jupiter_receive to hand in
405 * jupiter_receive - receive gps data
409 jupiter_receive(register struct recvbuf *rbufp)
411 register int bpcnt, cc, size, ppsret;
412 register u_int32 last_timecode, laststime;
415 register u_short *sp;
416 register u_long sloppyclockflag;
417 register struct jupiterunit *up;
418 register struct jid *ip;
419 register struct jheader *hp;
420 register struct refclockproc *pp;
421 register struct peer *peer;
423 /* Initialize pointers and read the timecode and timestamp */
424 peer = (struct peer *)rbufp->recv_srcclock;
426 up = (struct jupiterunit *)pp->unitptr;
429 * If operating mode has been changed, then reinitialize the receiver
430 * before doing anything else.
432 /* XXX Sloppy clock flags are broken!! */
433 sloppyclockflag = up->sloppyclockflag;
434 up->sloppyclockflag = pp->sloppyclockflag;
435 if ((pp->sloppyclockflag & CLK_FLAG2) !=
436 (sloppyclockflag & CLK_FLAG2)) {
438 "jupiter_receive: mode switch: reset receiver\n");
439 jupiter_config(peer);
445 bp = (u_char *)rbufp->recv_buffer;
446 bpcnt = rbufp->recv_length;
448 /* This shouldn't happen */
449 if (bpcnt > sizeof(up->sbuf) - up->ssize)
450 bpcnt = sizeof(up->sbuf) - up->ssize;
452 /* Append to input buffer */
453 memcpy((u_char *)up->sbuf + up->ssize, bp, bpcnt);
456 /* While there's at least a header and we parse a intact message */
457 while (up->ssize > sizeof(*hp) && (cc = jupiter_recv(peer)) > 0) {
458 hp = (struct jheader *)up->sbuf;
459 sp = (u_short *)(hp + 1);
460 size = cc - sizeof(*hp);
461 switch (getshort(hp->id)) {
463 case JUPITER_O_PULSE:
464 if (size != sizeof(struct jpulse)) {
466 "jupiter_receive: pulse: len %d != %u\n",
467 size, (int)sizeof(struct jpulse));
468 refclock_report(peer, CEVNT_BADREPLY);
473 * There appears to be a firmware bug related
474 * to the pulse message; in addition to the one
475 * per second messages, we get an extra pulse
476 * message once an hour (on the anniversary of
477 * the cold start). It seems to come 200 ms
478 * after the one requested. So if we've seen a
479 * pulse message in the last 210 ms, we skip
482 laststime = up->stime;
483 up->stime = DS2UI(((struct jpulse *)sp)->stime);
484 if (laststime != 0 && up->stime - laststime <= 21) {
485 jupiter_debug(peer, "jupiter_receive: \
486 avoided firmware bug (stime %.2f, laststime %.2f)\n",
487 (double)up->stime * 0.01, (double)laststime * 0.01);
491 /* Retrieve pps timestamp */
492 ppsret = jupiter_pps(peer);
494 /* Parse timecode (even when there's no pps) */
495 last_timecode = up->timecode;
496 if ((cp = jupiter_parse_t(peer, sp)) != NULL) {
498 "jupiter_receive: pulse: %s\n", cp);
502 /* Bail if we didn't get a pps timestamp */
506 /* Bail if we don't have the last timecode yet */
507 if (last_timecode == 0)
510 /* Add the new sample to a median filter */
511 if ((cp = jupiter_offset(peer)) != NULL) {
513 "jupiter_receive: offset: %s\n", cp);
514 refclock_report(peer, CEVNT_BADTIME);
519 * The clock will blurt a timecode every second
520 * but we only want one when polled. If we
521 * havn't been polled, bail out.
527 * It's a live one! Remember this time.
529 pp->lasttime = current_time;
532 * Determine the reference clock offset and
533 * dispersion. NKEEP of NSAMPLE offsets are
534 * passed through a median filter.
535 * Save the (filtered) offset and dispersion in
536 * pp->offset and pp->disp.
538 if ((cp = jupiter_process(peer)) != NULL) {
540 "jupiter_receive: process: %s\n", cp);
541 refclock_report(peer, CEVNT_BADTIME);
545 * Return offset and dispersion to control
546 * module. We use lastrec as both the reference
547 * time and receive time in order to avoid
548 * being cute, like setting the reference time
549 * later than the receive time, which may cause
550 * a paranoid protocol module to chuck out the
554 "jupiter_receive: process time: \
555 %4d-%03d %02d:%02d:%02d at %s, %s\n",
557 pp->hour, pp->minute, pp->second,
558 prettydate(&pp->lastrec), lfptoa(&pp->offset, 6));
560 refclock_receive(peer);
563 * We have succeeded in answering the poll.
564 * Turn off the flag and return
570 if (size != sizeof(struct jid)) {
572 "jupiter_receive: id: len %d != %u\n",
573 size, (int)sizeof(struct jid));
574 refclock_report(peer, CEVNT_BADREPLY);
578 * If we got this message because the Jupiter
579 * just powered up, it needs to be reconfigured.
581 ip = (struct jid *)sp;
583 "jupiter_receive: >> %s chan ver %s, %s (%s)\n",
584 ip->chans, ip->vers, ip->date, ip->opts);
586 "jupiter_receive: %s chan ver %s, %s (%s)\n",
587 ip->chans, ip->vers, ip->date, ip->opts);
592 "jupiter_receive: reset receiver\n");
593 jupiter_config(peer);
594 /* Rese since jupiter_config() just zeroed it */
601 "jupiter_receive: >> unknown message id %d\n",
607 fprintf(stderr, "jupiter_recv: negative ssize!\n");
609 } else if (up->ssize > 0)
610 memcpy(up->sbuf, (u_char *)up->sbuf + cc, up->ssize);
612 record_clock_stats(&peer->srcadr, "<timecode is binary>");
616 * jupiter_offset - Calculate the offset, and add to the rolling filter.
619 jupiter_offset(register struct peer *peer)
621 register struct jupiterunit *up;
622 register struct refclockproc *pp;
627 up = (struct jupiterunit *)pp->unitptr;
630 * Calculate the offset
632 if (!clocktime(pp->day, pp->hour, pp->minute, pp->second, GMT,
633 pp->lastrec.l_ui, &pp->yearstart, &offset.l_ui)) {
634 return ("jupiter_process: clocktime failed");
637 TVUTOTSF(pp->usec, offset.l_uf);
639 MSUTOTSF(pp->msec, offset.l_uf);
641 L_ADD(&offset, &pp->fudgetime1);
642 up->lastref = offset; /* save last reference time */
643 L_SUB(&offset, &pp->lastrec); /* form true offset */
646 * A rolling filter. Initialize first time around.
648 i = ((up->coderecv)) % NSAMPLES;
650 up->filter[i] = offset;
651 if (up->coderecv == 0)
652 for (i = 1; (u_int) i < NSAMPLES; i++)
653 up->filter[i] = up->filter[0];
660 * jupiter_process - process the sample from the clock,
661 * passing it through a median filter and optionally averaging
662 * the samples. Returns offset and dispersion in "up" structure.
665 jupiter_process(register struct peer *peer)
667 register struct jupiterunit *up;
668 register struct refclockproc *pp;
671 l_fp offset, median, lftmp;
676 up = (struct jupiterunit *)pp->unitptr;
679 * Copy the raw offsets and sort into ascending order
681 for (i = 0; i < NSAMPLES; i++)
682 off[i] = up->filter[i];
683 qsort((char *)off, (size_t)NSAMPLES, sizeof(l_fp), jupiter_cmpl_fp);
686 * Reject the furthest from the median of NSAMPLES samples until
687 * NKEEP samples remain.
691 while ((n - i) > up->nkeep) {
693 median = off[(n + i) / 2];
694 L_SUB(&lftmp, &median);
695 L_SUB(&median, &off[i]);
696 if (L_ISHIS(&median, &lftmp)) {
700 /* reject high end */
706 * Copy key values to the billboard to measure performance.
708 pp->lastref = up->lastref;
709 pp->coderecv = up->coderecv;
710 pp->filter[0] = off[0]; /* smallest offset */
711 pp->filter[1] = off[NSAMPLES-1]; /* largest offset */
712 for (j = 2, k = i; k < n; j++, k++)
713 pp->filter[j] = off[k]; /* offsets actually examined */
716 * Compute the dispersion based on the difference between the
717 * extremes of the remaining offsets. Add to this the time since
718 * the last clock update, which represents the dispersion
719 * increase with time. We know that NTP_MAXSKEW is 16. If the
720 * sum is greater than the allowed sample dispersion, bail out.
721 * If the loop is unlocked, return the most recent offset;
722 * otherwise, return the median offset.
725 L_SUB(&lftmp, &off[i]);
726 disp = LFPTOFP(&lftmp);
727 if (disp > REFCLOCKMAXDISPERSE)
728 return ("Maximum dispersion exceeded");
731 * Now compute the offset estimate. If fudge flag 1
732 * is set, average the remainder, otherwise pick the
735 if (pp->sloppyclockflag & CLK_FLAG1) {
738 L_ADD(&lftmp, &off[i]);
753 * The payload: filtered offset and dispersion.
763 /* Compare two l_fp's, used with qsort() */
764 #ifdef QSORT_USES_VOID_P
766 jupiter_cmpl_fp(register const void *p1, register const void *p2)
769 jupiter_cmpl_fp(register const l_fp *fp1, register const l_fp *fp2)
772 #ifdef QSORT_USES_VOID_P
773 register const l_fp *fp1 = (const l_fp *)p1;
774 register const l_fp *fp2 = (const l_fp *)p2;
777 if (!L_ISGEQ(fp1, fp2))
779 if (L_ISEQU(fp1, fp2))
785 jupiter_parse_t(register struct peer *peer, register u_short *sp)
787 register struct refclockproc *pp;
788 register struct jupiterunit *up;
789 register struct tm *tm;
791 register struct jpulse *jp;
792 register struct calendar *jt;
793 register u_int32 sweek;
794 register u_int32 last_timecode;
795 register u_short flags;
800 up = (struct jupiterunit *)pp->unitptr;
801 jp = (struct jpulse *)sp;
803 /* The timecode is presented as seconds into the current GPS week */
804 sweek = DS2UI(jp->sweek);
807 * If we don't know the current GPS week, calculate it from the
808 * current time. (It's too bad they didn't include this
809 * important value in the pulse message). We'd like to pick it
810 * up from one of the other messages like gpos or chan but they
811 * don't appear to be synchronous with time keeping and changes
812 * too soon (something like 10 seconds before the new GPS
815 * If we already know the current GPS week, increment it when
816 * we wrap into a new week.
819 up->gweek = (time(NULL) - GPS_EPOCH) / WEEKSECS;
820 else if (sweek == 0 && up->lastsweek == WEEKSECS - 1) {
823 "jupiter_parse_t: NEW gps week %u\n", up->gweek);
827 * See if the sweek stayed the same (this happens when there is
830 * Otherwise, look for time warps:
832 * - we have stored at least one lastsweek and
833 * - the sweek didn't increase by one and
834 * - we didn't wrap to a new GPS week
838 if (up->lastsweek == sweek)
840 "jupiter_parse_t: gps sweek not incrementing (%d)\n",
842 else if (up->lastsweek != 2 * WEEKSECS &&
843 up->lastsweek + 1 != sweek &&
844 !(sweek == 0 && up->lastsweek == WEEKSECS - 1))
846 "jupiter_parse_t: gps sweek jumped (was %d, now %d)\n",
847 up->lastsweek, sweek);
848 up->lastsweek = sweek;
850 /* This timecode describes next pulse */
851 last_timecode = up->timecode;
852 up->timecode = (u_int32)JAN_1970 +
853 GPS_EPOCH + (up->gweek * WEEKSECS) + sweek;
855 if (last_timecode == 0)
858 "jupiter_parse_t: UTC <none> (gweek/sweek %u/%u)\n",
862 t = last_timecode - (u_int32)JAN_1970;
867 "jupiter_parse_t: UTC %.24s (gweek/sweek %u/%u)\n",
868 cp, up->gweek, sweek);
870 /* Billboard last_timecode (which is now the current time) */
872 caljulian(last_timecode, jt);
875 pp->day = jt->yearday;
877 pp->minute = jt->minute;
878 pp->second = jt->second;
884 tm = gmtime(&up->ppsev.tv.tv_sec);
886 flags = getshort(jp->flags);
888 "jupiter_parse_t: PPS %.19s.%06lu %.4s (serial %u)%s\n",
889 cp, up->ppsev.tv.tv_usec, cp + 20, up->ppsev.serial,
890 (flags & JUPITER_O_PULSE_VALID) == 0 ?
893 /* Toss if not designated "valid" by the gps */
894 if ((flags & JUPITER_O_PULSE_VALID) == 0) {
895 refclock_report(peer, CEVNT_BADTIME);
896 return ("time mark not valid");
899 /* We better be sync'ed to UTC... */
900 if ((flags & JUPITER_O_PULSE_UTC) == 0) {
901 refclock_report(peer, CEVNT_BADTIME);
902 return ("time mark not sync'ed to UTC");
909 * Process a PPS signal, returning a timestamp.
912 jupiter_pps(register struct peer *peer)
914 register struct refclockproc *pp;
915 register struct jupiterunit *up;
916 register int firsttime;
917 struct timeval ntp_tv;
920 up = (struct jupiterunit *)pp->unitptr;
923 * Grab the timestamp of the PPS signal.
925 firsttime = (up->ppsev.tv.tv_sec == 0);
926 if (ioctl(pp->io.fd, CIOGETEV, (caddr_t)&up->ppsev) < 0) {
927 /* XXX Actually, if this fails, we're pretty much screwed */
928 jupiter_debug(peer, "jupiter_pps: CIOGETEV: %s\n",
930 refclock_report(peer, CEVNT_FAULT);
935 * Check pps serial number against last one
937 if (!firsttime && up->lastserial + 1 != up->ppsev.serial) {
938 if (up->ppsev.serial == up->lastserial)
939 jupiter_debug(peer, "jupiter_pps: no new pps event\n");
942 "jupiter_pps: missed %d pps events\n",
943 up->ppsev.serial - up->lastserial - 1);
944 up->lastserial = up->ppsev.serial;
945 refclock_report(peer, CEVNT_FAULT);
948 up->lastserial = up->ppsev.serial;
951 * Return the timestamp in pp->lastrec
953 ntp_tv = up->ppsev.tv;
954 ntp_tv.tv_sec += (u_int32)JAN_1970;
955 TVTOTS(&ntp_tv, &pp->lastrec);
961 * jupiter_debug - print debug messages
963 #if defined(__STDC__)
965 jupiter_debug(struct peer *peer, char *fmt, ...)
968 jupiter_debug(peer, fmt, va_alist)
971 #endif /* __STDC__ */
977 #if defined(__STDC__)
981 #endif /* __STDC__ */
983 * Print debug message to stdout
984 * In the future, we may want to get get more creative...
986 vfprintf(stderr, fmt, ap);
992 /* Checksum and transmit a message to the Jupiter */
994 jupiter_send(register struct peer *peer, register struct jheader *hp)
996 register u_int len, size;
998 register u_short *sp;
999 static char errstr[132];
1002 hp->hsum = putshort(jupiter_cksum((u_short *)hp,
1003 (size / sizeof(u_short)) - 1));
1004 len = getshort(hp->len);
1006 sp = (u_short *)(hp + 1);
1007 sp[len] = putshort(jupiter_cksum(sp, len));
1008 size += (len + 1) * sizeof(u_short);
1011 if ((cc = write(peer->procptr->io.fd, (char *)hp, size)) < 0) {
1012 (void)sprintf(errstr, "write: %s", strerror(errno));
1014 } else if (cc != size) {
1015 (void)sprintf(errstr, "short write (%d != %d)", cc, size);
1021 /* Request periodic message output */
1023 struct jheader jheader;
1024 struct jrequest jrequest;
1026 { putshort(JUPITER_SYNC), 0,
1027 putshort((sizeof(struct jrequest) / sizeof(u_short)) - 1),
1028 0, putshort(JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK |
1029 JUPITER_FLAG_CONN | JUPITER_FLAG_LOG), 0 },
1033 /* An interval of zero means to output on trigger */
1035 jupiter_reqmsg(register struct peer *peer, register u_int id,
1036 register u_int interval)
1038 register struct jheader *hp;
1039 register struct jrequest *rp;
1042 hp = &reqmsg.jheader;
1043 hp->id = putshort(id);
1044 rp = &reqmsg.jrequest;
1045 rp->trigger = putshort(interval == 0);
1046 rp->interval = putshort(interval);
1047 if ((cp = jupiter_send(peer, hp)) != NULL)
1048 jupiter_debug(peer, "jupiter_reqmsg: %u: %s\n", id, cp);
1051 /* Cancel periodic message output */
1052 static struct jheader canmsg = {
1053 putshort(JUPITER_SYNC), 0, 0, 0,
1054 putshort(JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_DISC),
1059 jupiter_canmsg(register struct peer *peer, register u_int id)
1061 register struct jheader *hp;
1065 hp->id = putshort(id);
1066 if ((cp = jupiter_send(peer, hp)) != NULL)
1067 jupiter_debug(peer, "jupiter_canmsg: %u: %s\n", id, cp);
1070 /* Request a single message output */
1071 static struct jheader reqonemsg = {
1072 putshort(JUPITER_SYNC), 0, 0, 0,
1073 putshort(JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK | JUPITER_FLAG_QUERY),
1078 jupiter_reqonemsg(register struct peer *peer, register u_int id)
1080 register struct jheader *hp;
1084 hp->id = putshort(id);
1085 if ((cp = jupiter_send(peer, hp)) != NULL)
1086 jupiter_debug(peer, "jupiter_reqonemsg: %u: %s\n", id, cp);
1089 /* Set the platform dynamics */
1091 struct jheader jheader;
1094 { putshort(JUPITER_SYNC), putshort(JUPITER_I_PLAT),
1095 putshort((sizeof(struct jplat) / sizeof(u_short)) - 1), 0,
1096 putshort(JUPITER_FLAG_REQUEST | JUPITER_FLAG_NAK), 0 },
1101 jupiter_platform(register struct peer *peer, register u_int platform)
1103 register struct jheader *hp;
1104 register struct jplat *pp;
1107 hp = &platmsg.jheader;
1108 pp = &platmsg.jplat;
1109 pp->platform = putshort(platform);
1110 if ((cp = jupiter_send(peer, hp)) != NULL)
1111 jupiter_debug(peer, "jupiter_platform: %u: %s\n", platform, cp);
1114 /* Checksum "len" shorts */
1116 jupiter_cksum(register u_short *sp, register u_int len)
1118 register u_short sum, x;
1128 /* Return the size of the next message (or zero if we don't have it all yet) */
1130 jupiter_recv(register struct peer *peer)
1132 register int n, len, size, cc;
1133 register struct refclockproc *pp;
1134 register struct jupiterunit *up;
1135 register struct jheader *hp;
1136 register u_char *bp;
1137 register u_short *sp;
1140 up = (struct jupiterunit *)pp->unitptr;
1142 /* Must have at least a header's worth */
1148 /* Search for the sync short if missing */
1150 hp = (struct jheader *)sp;
1151 if (getshort(hp->sync) != JUPITER_SYNC) {
1152 /* Wasn't at the front, sync up */
1153 jupiter_debug(peer, "syncing");
1157 if (bp[0] != (JUPITER_SYNC & 0xff)) {
1158 jupiter_debug(peer, "{0x%x}", bp[0]);
1163 if (bp[1] == ((JUPITER_SYNC >> 8) & 0xff))
1165 jupiter_debug(peer, "{0x%x 0x%x}", bp[0], bp[1]);
1169 jupiter_debug(peer, "\n");
1170 /* Shuffle data to front of input buffer */
1175 if (size < cc || hp->sync != JUPITER_SYNC)
1179 if (jupiter_cksum(sp, (cc / sizeof(u_short) - 1)) !=
1180 getshort(hp->hsum)) {
1181 jupiter_debug(peer, "jupiter_recv: bad header checksum!\n");
1182 /* This is drastic but checksum errors should be rare */
1187 /* Check for a payload */
1188 len = getshort(hp->len);
1190 n = (len + 1) * sizeof(u_short);
1191 /* Not enough data yet */
1195 /* Check payload checksum */
1196 sp = (u_short *)(hp + 1);
1197 if (jupiter_cksum(sp, len) != getshort(sp[len])) {
1199 "jupiter_recv: bad payload checksum!\n");
1200 /* This is drastic but checksum errors should be rare */
1210 jupiter_ttyinit(register struct peer *peer, register int fd)
1212 struct termios termios;
1214 memset((char *)&termios, 0, sizeof(termios));
1215 if (cfsetispeed(&termios, B9600) < 0 ||
1216 cfsetospeed(&termios, B9600) < 0) {
1218 "jupiter_ttyinit: cfsetispeed/cfgetospeed: %s\n",
1222 #ifdef HAVE_CFMAKERAW
1223 cfmakeraw(&termios);
1225 termios.c_iflag &= ~(IMAXBEL | IXOFF | INPCK | BRKINT | PARMRK |
1226 ISTRIP | INLCR | IGNCR | ICRNL | IXON | IGNPAR);
1227 termios.c_iflag |= IGNBRK;
1228 termios.c_oflag &= ~OPOST;
1229 termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ICANON | ISIG |
1230 IEXTEN | NOFLSH | TOSTOP | PENDIN);
1231 termios.c_cflag &= ~(CSIZE | PARENB);
1232 termios.c_cflag |= CS8 | CREAD;
1233 termios.c_cc[VMIN] = 1;
1235 termios.c_cflag |= CLOCAL;
1236 if (tcsetattr(fd, TCSANOW, &termios) < 0) {
1237 jupiter_debug(peer, "jupiter_ttyinit: tcsetattr: %s\n",
1243 if (ioctl(fd, TIOCSPPS, (char *)&fdpps) < 0) {
1244 jupiter_debug(peer, "jupiter_ttyinit: TIOCSPPS: %s\n",
1250 if (ioctl(fd, I_PUSH, "ppsclock") < 0) {
1251 jupiter_debug(peer, "jupiter_ttyinit: push ppsclock: %s\n",
1260 #else /* not (REFCLOCK && CLOCK_JUPITER && PPS) */
1261 int refclock_jupiter_bs;
1262 #endif /* not (REFCLOCK && CLOCK_JUPITER && PPS) */