2 * refclock_tpro - clock driver for the KSI/Odetics TPRO-S IRIG-B reader
9 #if defined(REFCLOCK) && defined(CLOCK_TPRO)
13 #include "ntp_refclock.h"
14 #include "ntp_unixtime.h"
16 #include "ntp_stdlib.h"
22 * This driver supports the KSI/Odetecs TPRO-S IRIG-B reader and TPRO-
23 * SAT GPS receiver for the Sun Microsystems SBus. It requires that the
24 * tpro.o device driver be installed and loaded.
28 * TPRO interface definitions
30 #define DEVICE "/dev/tpro%d" /* device name and unit */
31 #define PRECISION (-20) /* precision assumed (1 us) */
32 #define REFID "IRIG" /* reference ID */
33 #define DESCRIPTION "KSI/Odetics TPRO/S IRIG Interface" /* WRU */
36 * Unit control structure
39 struct tproval tprodata; /* data returned from tpro read */
45 static int tpro_start P((int, struct peer *));
46 static void tpro_shutdown P((int, struct peer *));
47 static void tpro_poll P((int unit, struct peer *));
52 struct refclock refclock_tpro = {
53 tpro_start, /* start up driver */
54 tpro_shutdown, /* shut down driver */
55 tpro_poll, /* transmit poll message */
56 noentry, /* not used (old tpro_control) */
57 noentry, /* initialize driver (not used) */
58 noentry, /* not used (old tpro_buginfo) */
59 NOFLAGS /* not used */
64 * tpro_start - open the TPRO device and initialize data for processing
72 register struct tprounit *up;
73 struct refclockproc *pp;
80 (void)sprintf(device, DEVICE, unit);
81 fd = open(device, O_RDONLY | O_NDELAY, 0777);
83 msyslog(LOG_ERR, "tpro_start: open of %s: %m", device);
88 * Allocate and initialize unit structure
90 if (!(up = (struct tprounit *) emalloc(sizeof(struct tprounit)))) {
94 memset((char *)up, 0, sizeof(struct tprounit));
96 pp->io.clock_recv = noentry;
97 pp->io.srcclock = (caddr_t)peer;
100 pp->unitptr = (caddr_t)up;
103 * Initialize miscellaneous peer variables
105 peer->precision = PRECISION;
106 peer->burst = NSTAGE;
107 pp->clockdesc = DESCRIPTION;
108 memcpy((char *)&pp->refid, REFID, 4);
114 * tpro_shutdown - shut down the clock
122 register struct tprounit *up;
123 struct refclockproc *pp;
126 up = (struct tprounit *)pp->unitptr;
127 io_closeclock(&pp->io);
133 * tpro_poll - called by the transmit procedure
141 register struct tprounit *up;
142 struct refclockproc *pp;
146 * This is the main routine. It snatches the time from the TPRO
147 * board and tacks on a local timestamp.
150 up = (struct tprounit *)pp->unitptr;
153 if (read(pp->io.fd, (char *)tp, sizeof(struct tproval)) < 0) {
154 refclock_report(peer, CEVNT_FAULT);
157 get_systime(&pp->lastrec);
161 * We get down to business, check the timecode format and decode
162 * its contents. If the timecode has invalid length or is not in
163 * proper format, we declare bad format and exit. Note: we
164 * can't use the sec/usec conversion produced by the driver,
165 * since the year may be suspect. All format error checking is
166 * done by the sprintf() and sscanf() routines.
168 * Note that the refclockproc usec member has now become nsec.
169 * We could either multiply the read-in usec value by 1000 or
170 * we could pad the written string appropriately and read the
171 * resulting value in already scaled.
173 sprintf(pp->a_lastcode,
174 "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x",
175 tp->day100, tp->day10, tp->day1, tp->hour10, tp->hour1,
176 tp->min10, tp->min1, tp->sec10, tp->sec1, tp->ms100,
177 tp->ms10, tp->ms1, tp->usec100, tp->usec10, tp->usec1,
179 pp->lencode = strlen(pp->a_lastcode);
182 printf("tpro: time %s timecode %d %s\n",
183 ulfptoa(&pp->lastrec, 6), pp->lencode,
186 if (sscanf(pp->a_lastcode, "%3d %2d:%2d:%2d.%6ld", &pp->day,
187 &pp->hour, &pp->minute, &pp->second, &pp->nsec)
189 refclock_report(peer, CEVNT_BADTIME);
192 pp->nsec *= 1000; /* Convert usec to nsec */
193 if (!tp->status & 0x3)
194 pp->leap = LEAP_NOTINSYNC;
196 pp->leap = LEAP_NOWARNING;
197 if (!refclock_process(pp)) {
198 refclock_report(peer, CEVNT_BADTIME);
203 if (pp->coderecv == pp->codeproc) {
204 refclock_report(peer, CEVNT_TIMEOUT);
207 refclock_receive(peer);
208 pp->lastref = pp->lastrec;
209 record_clock_stats(&peer->srcadr, pp->a_lastcode);
210 refclock_receive(peer);
211 peer->burst = NSTAGE;
215 int refclock_tpro_bs;
216 #endif /* REFCLOCK */