xref: /netbsd-src/external/bsd/ntp/dist/ntpd/refclock_tpro.c (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 /*	$NetBSD: refclock_tpro.c,v 1.1.1.1 2009/12/13 16:56:03 kardel Exp $	*/
2 
3 /*
4  * refclock_tpro - clock driver for the KSI/Odetics TPRO-S IRIG-B reader
5  */
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #if defined(REFCLOCK) && defined(CLOCK_TPRO)
12 
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"
19 
20 #include <stdio.h>
21 #include <ctype.h>
22 
23 /*
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  */
28 
29 /*
30  * TPRO interface definitions
31  */
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 */
36 
37 /*
38  * Unit control structure
39  */
40 struct tprounit {
41 	struct	tproval tprodata; /* data returned from tpro read */
42 };
43 
44 /*
45  * Function prototypes
46  */
47 static	int	tpro_start	(int, struct peer *);
48 static	void	tpro_shutdown	(int, struct peer *);
49 static	void	tpro_poll	(int unit, struct peer *);
50 
51 /*
52  * Transfer vector
53  */
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 */
62 };
63 
64 
65 /*
66  * tpro_start - open the TPRO device and initialize data for processing
67  */
68 static int
69 tpro_start(
70 	int unit,
71 	struct peer *peer
72 	)
73 {
74 	register struct tprounit *up;
75 	struct refclockproc *pp;
76 	char device[20];
77 	int fd;
78 
79 	/*
80 	 * Open TPRO device
81 	 */
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);
87 	}
88 
89 	/*
90 	 * Allocate and initialize unit structure
91 	 */
92 	if (!(up = (struct tprounit *) emalloc(sizeof(struct tprounit)))) {
93 		(void) close(fd);
94 		return (0);
95 	}
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;
103 
104 	/*
105 	 * Initialize miscellaneous peer variables
106 	 */
107 	peer->precision = PRECISION;
108 	peer->burst = NSTAGE;
109 	pp->clockdesc = DESCRIPTION;
110 	memcpy((char *)&pp->refid, REFID, 4);
111 	return (1);
112 }
113 
114 
115 /*
116  * tpro_shutdown - shut down the clock
117  */
118 static void
119 tpro_shutdown(
120 	int unit,
121 	struct peer *peer
122 	)
123 {
124 	register struct tprounit *up;
125 	struct refclockproc *pp;
126 
127 	pp = peer->procptr;
128 	up = (struct tprounit *)pp->unitptr;
129 	io_closeclock(&pp->io);
130 	free(up);
131 }
132 
133 
134 /*
135  * tpro_poll - called by the transmit procedure
136  */
137 static void
138 tpro_poll(
139 	int unit,
140 	struct peer *peer
141 	)
142 {
143 	register struct tprounit *up;
144 	struct refclockproc *pp;
145 	struct tproval *tp;
146 
147 	/*
148 	 * This is the main routine. It snatches the time from the TPRO
149 	 * board and tacks on a local timestamp.
150 	 */
151 	pp = peer->procptr;
152 	up = (struct tprounit *)pp->unitptr;
153 
154 	tp = &up->tprodata;
155 	if (read(pp->io.fd, (char *)tp, sizeof(struct tproval)) < 0) {
156 		refclock_report(peer, CEVNT_FAULT);
157 		return;
158 	}
159 	get_systime(&pp->lastrec);
160 	pp->polls++;
161 
162 	/*
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.
169 	 *
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.
174 	 */
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;
193 	}
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;
202 	}
203 	if (peer->burst > 0)
204 		return;
205 	if (pp->coderecv == pp->codeproc) {
206 		refclock_report(peer, CEVNT_TIMEOUT);
207 		return;
208 	}
209 	pp->lastref = pp->lastrec;
210 	record_clock_stats(&peer->srcadr, pp->a_lastcode);
211 	refclock_receive(peer);
212 	peer->burst = NSTAGE;
213 }
214 
215 #else
216 int refclock_tpro_bs;
217 #endif /* REFCLOCK */
218