Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / ntpd / refclock_tpro.c
blob10c7acb9764e1c9db8dedf24bf5105fc165c8712
1 /* $NetBSD$ */
3 /*
4 * refclock_tpro - clock driver for the KSI/Odetics TPRO-S IRIG-B reader
5 */
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
11 #if defined(REFCLOCK) && defined(CLOCK_TPRO)
13 #include "ntpd.h"
14 #include "ntp_io.h"
15 #include "ntp_refclock.h"
16 #include "ntp_unixtime.h"
17 #include "sys/tpro.h"
18 #include "ntp_stdlib.h"
20 #include <stdio.h>
21 #include <ctype.h>
24 * This driver supports the KSI/Odetecs TPRO-S IRIG-B reader and TPRO-
25 * SAT GPS receiver for the Sun Microsystems SBus. It requires that the
26 * tpro.o device driver be installed and loaded.
27 */
30 * TPRO interface definitions
32 #define DEVICE "/dev/tpro%d" /* device name and unit */
33 #define PRECISION (-20) /* precision assumed (1 us) */
34 #define REFID "IRIG" /* reference ID */
35 #define DESCRIPTION "KSI/Odetics TPRO/S IRIG Interface" /* WRU */
38 * Unit control structure
40 struct tprounit {
41 struct tproval tprodata; /* data returned from tpro read */
45 * Function prototypes
47 static int tpro_start P((int, struct peer *));
48 static void tpro_shutdown P((int, struct peer *));
49 static void tpro_poll P((int unit, struct peer *));
52 * Transfer vector
54 struct refclock refclock_tpro = {
55 tpro_start, /* start up driver */
56 tpro_shutdown, /* shut down driver */
57 tpro_poll, /* transmit poll message */
58 noentry, /* not used (old tpro_control) */
59 noentry, /* initialize driver (not used) */
60 noentry, /* not used (old tpro_buginfo) */
61 NOFLAGS /* not used */
66 * tpro_start - open the TPRO device and initialize data for processing
68 static int
69 tpro_start(
70 int unit,
71 struct peer *peer
74 register struct tprounit *up;
75 struct refclockproc *pp;
76 char device[20];
77 int fd;
80 * Open TPRO device
82 (void)sprintf(device, DEVICE, unit);
83 fd = open(device, O_RDONLY | O_NDELAY, 0777);
84 if (fd == -1) {
85 msyslog(LOG_ERR, "tpro_start: open of %s: %m", device);
86 return (0);
90 * Allocate and initialize unit structure
92 if (!(up = (struct tprounit *) emalloc(sizeof(struct tprounit)))) {
93 (void) close(fd);
94 return (0);
96 memset((char *)up, 0, sizeof(struct tprounit));
97 pp = peer->procptr;
98 pp->io.clock_recv = noentry;
99 pp->io.srcclock = (caddr_t)peer;
100 pp->io.datalen = 0;
101 pp->io.fd = fd;
102 pp->unitptr = (caddr_t)up;
105 * Initialize miscellaneous peer variables
107 peer->precision = PRECISION;
108 peer->burst = NSTAGE;
109 pp->clockdesc = DESCRIPTION;
110 memcpy((char *)&pp->refid, REFID, 4);
111 return (1);
116 * tpro_shutdown - shut down the clock
118 static void
119 tpro_shutdown(
120 int unit,
121 struct peer *peer
124 register struct tprounit *up;
125 struct refclockproc *pp;
127 pp = peer->procptr;
128 up = (struct tprounit *)pp->unitptr;
129 io_closeclock(&pp->io);
130 free(up);
135 * tpro_poll - called by the transmit procedure
137 static void
138 tpro_poll(
139 int unit,
140 struct peer *peer
143 register struct tprounit *up;
144 struct refclockproc *pp;
145 struct tproval *tp;
148 * This is the main routine. It snatches the time from the TPRO
149 * board and tacks on a local timestamp.
151 pp = peer->procptr;
152 up = (struct tprounit *)pp->unitptr;
154 tp = &up->tprodata;
155 if (read(pp->io.fd, (char *)tp, sizeof(struct tproval)) < 0) {
156 refclock_report(peer, CEVNT_FAULT);
157 return;
159 get_systime(&pp->lastrec);
160 pp->polls++;
163 * We get down to business, check the timecode format and decode
164 * its contents. If the timecode has invalid length or is not in
165 * proper format, we declare bad format and exit. Note: we
166 * can't use the sec/usec conversion produced by the driver,
167 * since the year may be suspect. All format error checking is
168 * done by the sprintf() and sscanf() routines.
170 * Note that the refclockproc usec member has now become nsec.
171 * We could either multiply the read-in usec value by 1000 or
172 * we could pad the written string appropriately and read the
173 * resulting value in already scaled.
175 sprintf(pp->a_lastcode,
176 "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x",
177 tp->day100, tp->day10, tp->day1, tp->hour10, tp->hour1,
178 tp->min10, tp->min1, tp->sec10, tp->sec1, tp->ms100,
179 tp->ms10, tp->ms1, tp->usec100, tp->usec10, tp->usec1,
180 tp->status);
181 pp->lencode = strlen(pp->a_lastcode);
182 #ifdef DEBUG
183 if (debug)
184 printf("tpro: time %s timecode %d %s\n",
185 ulfptoa(&pp->lastrec, 6), pp->lencode,
186 pp->a_lastcode);
187 #endif
188 if (sscanf(pp->a_lastcode, "%3d %2d:%2d:%2d.%6ld", &pp->day,
189 &pp->hour, &pp->minute, &pp->second, &pp->nsec)
190 != 5) {
191 refclock_report(peer, CEVNT_BADTIME);
192 return;
194 pp->nsec *= 1000; /* Convert usec to nsec */
195 if (!tp->status & 0x3)
196 pp->leap = LEAP_NOTINSYNC;
197 else
198 pp->leap = LEAP_NOWARNING;
199 if (!refclock_process(pp)) {
200 refclock_report(peer, CEVNT_BADTIME);
201 return;
203 if (peer->burst > 0)
204 return;
205 if (pp->coderecv == pp->codeproc) {
206 refclock_report(peer, CEVNT_TIMEOUT);
207 return;
209 refclock_receive(peer);
210 pp->lastref = pp->lastrec;
211 record_clock_stats(&peer->srcadr, pp->a_lastcode);
212 refclock_receive(peer);
213 peer->burst = NSTAGE;
216 #else
217 int refclock_tpro_bs;
218 #endif /* REFCLOCK */