xref: /freebsd/contrib/ntp/ntpd/refclock_heath.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
1 /*
2  * refclock_heath - clock driver for Heath GC-1000 and and GC-1000 II
3  */
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 #if defined(REFCLOCK) && defined(CLOCK_HEATH)
9 
10 #include "ntpd.h"
11 #include "ntp_io.h"
12 #include "ntp_refclock.h"
13 #include "ntp_stdlib.h"
14 
15 #include <stdio.h>
16 #include <ctype.h>
17 
18 #ifdef HAVE_SYS_IOCTL_H
19 # include <sys/ioctl.h>
20 #endif /* not HAVE_SYS_IOCTL_H */
21 
22 /*
23  * This driver supports the Heath GC-1000 Most Accurate Clock, with
24  * RS232C Output Accessory. This is a WWV/WWVH receiver somewhat less
25  * robust than other supported receivers. Its claimed accuracy is 100 ms
26  * when actually synchronized to the broadcast signal, but this doesn't
27  * happen even most of the time, due to propagation conditions, ambient
28  * noise sources, etc. When not synchronized, the accuracy is at the
29  * whim of the internal clock oscillator, which can wander into the
30  * sunset without warning. Since the indicated precision is 100 ms,
31  * expect a host synchronized only to this thing to wander to and fro,
32  * occasionally being rudely stepped when the offset exceeds the default
33  * clock_max of 128 ms.
34  *
35  * There are two GC-1000 versions supported by this driver. The original
36  * GC-1000 with RS-232 output first appeared in 1983, but dissapeared
37  * from the market a few years later. The GC-1000 II with RS-232 output
38  * first appeared circa 1990, but apparently is no longer manufactured.
39  * The two models differ considerably, both in interface and commands.
40  * The GC-1000 has a pseudo-bipolar timecode output triggered by a RTS
41  * transition. The timecode includes both the day of year and time of
42  * day. The GC-1000 II has a true bipolar output and a complement of
43  * single character commands. The timecode includes only the time of
44  * day.
45  *
46  * GC-1000
47  *
48  * The internal DIPswitches should be set to operate in MANUAL mode. The
49  * external DIPswitches should be set to GMT and 24-hour format.
50  *
51  * In MANUAL mode the clock responds to a rising edge of the request to
52  * send (RTS) modem control line by sending the timecode. Therefore, it
53  * is necessary that the operating system implement the TIOCMBIC and
54  * TIOCMBIS ioctl system calls and TIOCM_RTS control bit. Present
55  * restrictions require the use of a POSIX-compatible programming
56  * interface, although other interfaces may work as well.
57  *
58  * A simple hardware modification to the clock can be made which
59  * prevents the clock hearing the request to send (RTS) if the HI SPEC
60  * lamp is out. Route the HISPEC signal to the tone decoder board pin
61  * 19, from the display, pin 19. Isolate pin 19 of the decoder board
62  * first, but maintain connection with pin 10. Also isolate pin 38 of
63  * the CPU on the tone board, and use half an added 7400 to gate the
64  * original signal to pin 38 with that from pin 19.
65  *
66  * The clock message consists of 23 ASCII printing characters in the
67  * following format:
68  *
69  * hh:mm:ss.f AM  dd/mm/yr<cr>
70  *
71  *	hh:mm:ss.f = hours, minutes, seconds
72  *	f = deciseconds ('?' when out of spec)
73  *	AM/PM/bb = blank in 24-hour mode
74  *	dd/mm/yr = day, month, year
75  *
76  * The alarm condition is indicated by '?', rather than a digit, at f.
77  * Note that 0?:??:??.? is displayed before synchronization is first
78  * established and hh:mm:ss.? once synchronization is established and
79  * then lost again for about a day.
80  *
81  * GC-1000 II
82  *
83  * Commands consist of a single letter and are case sensitive. When
84  * enterred in lower case, a description of the action performed is
85  * displayed. When enterred in upper case the action is performed.
86  * Following is a summary of descriptions as displayed by the clock:
87  *
88  * The clock responds with a command The 'A' command returns an ASCII
89  * local time string:  HH:MM:SS.T xx<CR>, where
90  *
91  *	HH = hours
92  *	MM = minutes
93  *	SS = seconds
94  *	T = tenths-of-seconds
95  *	xx = 'AM', 'PM', or '  '
96  *	<CR> = carriage return
97  *
98  * The 'D' command returns 24 pairs of bytes containing the variable
99  * divisor value at the end of each of the previous 24 hours. This
100  * allows the timebase trimming process to be observed.  UTC hour 00 is
101  * always returned first. The first byte of each pair is the high byte
102  * of (variable divisor * 16); the second byte is the low byte of
103  * (variable divisor * 16). For example, the byte pair 3C 10 would be
104  * returned for a divisor of 03C1 hex (961 decimal).
105  *
106  * The 'I' command returns:  | TH | TL | ER | DH | DL | U1 | I1 | I2 | ,
107  * where
108  *
109  *	TH = minutes since timebase last trimmed (high byte)
110  *	TL = minutes since timebase last trimmed (low byte)
111  *	ER = last accumulated error in 1.25 ms increments
112  *	DH = high byte of (current variable divisor * 16)
113  *	DL = low byte of (current variable divisor * 16)
114  *	U1 = UT1 offset (/.1 s):  | + | 4 | 2 | 1 | 0 | 0 | 0 | 0 |
115  *	I1 = information byte 1:  | W | C | D | I | U | T | Z | 1 | ,
116  *	     where
117  *
118  *		W = set by WWV(H)
119  *		C = CAPTURE LED on
120  *		D = TRIM DN LED on
121  *		I = HI SPEC LED on
122  *		U = TRIM UP LED on
123  *		T = DST switch on
124  *		Z = UTC switch on
125  *		1 = UT1 switch on
126  *
127  *	I2 = information byte 2:  | 8 | 8 | 4 | 2 | 1 | D | d | S | ,
128  *	     where
129  *
130  *		8, 8, 4, 2, 1 = TIME ZONE switch settings
131  *		D = DST bit (#55) in last-received frame
132  *		d = DST bit (#2) in last-received frame
133  *		S = clock is in simulation mode
134  *
135  * The 'P' command returns 24 bytes containing the number of frames
136  * received without error during UTC hours 00 through 23, providing an
137  * indication of hourly propagation.  These bytes are updated each hour
138  * to reflect the previous 24 hour period.  UTC hour 00 is always
139  * returned first.
140  *
141  * The 'T' command returns the UTC time:  | HH | MM | SS | T0 | , where
142  *	HH = tens-of-hours and hours (packed BCD)
143  *	MM = tens-of-minutes and minutes (packed BCD)
144  *	SS = tens-of-seconds and seconds (packed BCD)
145  *	T = tenths-of-seconds (BCD)
146  *
147  * Fudge Factors
148  *
149  * A fudge time1 value of .04 s appears to center the clock offset
150  * residuals. The fudge time2 parameter is the local time offset east of
151  * Greenwich, which depends on DST. Sorry about that, but the clock
152  * gives no hint on what the DIPswitches say.
153  */
154 
155 /*
156  * Interface definitions
157  */
158 #define	DEVICE		"/dev/heath%d" /* device name and unit */
159 #define	PRECISION	(-4)	/* precision assumed (about 100 ms) */
160 #define	REFID		"WWV\0"	/* reference ID */
161 #define	DESCRIPTION	"Heath GC-1000 Most Accurate Clock" /* WRU */
162 
163 #define LENHEATH1	23	/* min timecode length */
164 #define LENHEATH2	13	/* min timecode length */
165 
166 /*
167  * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
168  * leap.
169  */
170 static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
171 static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
172 
173 /*
174  * Baud rate table. The GC-1000 supports 1200, 2400 and 4800; the
175  * GC-1000 II supports only 9600.
176  */
177 static int speed[] = {B1200, B2400, B4800, B9600};
178 
179 /*
180  * Unit control structure
181  */
182 struct heathunit {
183 	int	pollcnt;	/* poll message counter */
184 	l_fp	tstamp;		/* timestamp of last poll */
185 };
186 
187 /*
188  * Function prototypes
189  */
190 static	int	heath_start	P((int, struct peer *));
191 static	void	heath_shutdown	P((int, struct peer *));
192 static	void	heath_receive	P((struct recvbuf *));
193 static	void	heath_poll	P((int, struct peer *));
194 
195 /*
196  * Transfer vector
197  */
198 struct	refclock refclock_heath = {
199 	heath_start,		/* start up driver */
200 	heath_shutdown,		/* shut down driver */
201 	heath_poll,		/* transmit poll message */
202 	noentry,		/* not used (old heath_control) */
203 	noentry,		/* initialize driver */
204 	noentry,		/* not used (old heath_buginfo) */
205 	NOFLAGS			/* not used */
206 };
207 
208 
209 /*
210  * heath_start - open the devices and initialize data for processing
211  */
212 static int
213 heath_start(
214 	int unit,
215 	struct peer *peer
216 	)
217 {
218 	register struct heathunit *up;
219 	struct refclockproc *pp;
220 	int fd;
221 	char device[20];
222 
223 	/*
224 	 * Open serial port
225 	 */
226 	(void)sprintf(device, DEVICE, unit);
227 	if (!(fd = refclock_open(device, speed[peer->ttlmax & 0x3], 0)))
228 	    return (0);
229 
230 	/*
231 	 * Allocate and initialize unit structure
232 	 */
233 	if (!(up = (struct heathunit *)
234 	      emalloc(sizeof(struct heathunit)))) {
235 		(void) close(fd);
236 		return (0);
237 	}
238 	memset((char *)up, 0, sizeof(struct heathunit));
239 	pp = peer->procptr;
240 	pp->io.clock_recv = heath_receive;
241 	pp->io.srcclock = (caddr_t)peer;
242 	pp->io.datalen = 0;
243 	pp->io.fd = fd;
244 	if (!io_addclock(&pp->io)) {
245 		(void) close(fd);
246 		free(up);
247 		return (0);
248 	}
249 	pp->unitptr = (caddr_t)up;
250 
251 	/*
252 	 * Initialize miscellaneous variables
253 	 */
254 	peer->precision = PRECISION;
255 	peer->burst = NSTAGE;
256 	pp->clockdesc = DESCRIPTION;
257 	memcpy((char *)&pp->refid, REFID, 4);
258 	up->pollcnt = 2;
259 	return (1);
260 }
261 
262 
263 /*
264  * heath_shutdown - shut down the clock
265  */
266 static void
267 heath_shutdown(
268 	int unit,
269 	struct peer *peer
270 	)
271 {
272 	register struct heathunit *up;
273 	struct refclockproc *pp;
274 
275 	pp = peer->procptr;
276 	up = (struct heathunit *)pp->unitptr;
277 	io_closeclock(&pp->io);
278 	free(up);
279 }
280 
281 
282 /*
283  * heath_receive - receive data from the serial interface
284  */
285 static void
286 heath_receive(
287 	struct recvbuf *rbufp
288 	)
289 {
290 	register struct heathunit *up;
291 	struct refclockproc *pp;
292 	struct peer *peer;
293 	l_fp trtmp;
294 	int month, day;
295 	int i;
296 	char dsec, a[5];
297 
298 	/*
299 	 * Initialize pointers and read the timecode and timestamp
300 	 */
301 	peer = (struct peer *)rbufp->recv_srcclock;
302 	pp = peer->procptr;
303 	up = (struct heathunit *)pp->unitptr;
304 	pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX,
305 	    &trtmp);
306 
307 	/*
308 	 * We get a buffer and timestamp for each <cr>; however, we use
309 	 * the timestamp captured at the RTS modem control line toggle
310 	 * on the assumption that's what the radio bases the timecode
311 	 * on. Apparently, the radio takes about a second to make up its
312 	 * mind to send a timecode, so the receive timestamp is
313 	 * worthless.
314 	 */
315 	pp->lastrec = up->tstamp;
316 	up->pollcnt = 2;
317 #ifdef DEBUG
318 	if (debug)
319 	    printf("heath: timecode %d %s\n", pp->lencode,
320 		   pp->a_lastcode);
321 #endif
322 
323 	/*
324 	 * We get down to business, check the timecode format and decode
325 	 * its contents. If the timecode has invalid length or is not in
326 	 * proper format, we declare bad format and exit.
327 	 */
328 	switch (pp->lencode) {
329 
330 	/*
331 	 * GC-1000 timecode format: "hh:mm:ss.f AM  mm/dd/yy"
332 	 * GC-1000 II timecode format: "hh:mm:ss.f   "
333 	 */
334 	case LENHEATH1:
335 		if (sscanf(pp->a_lastcode,
336 		    "%2d:%2d:%2d.%c%5c%2d/%2d/%2d", &pp->hour,
337 		    &pp->minute, &pp->second, &dsec, a, &month, &day,
338 		    &pp->year) != 8) {
339 			refclock_report(peer, CEVNT_BADREPLY);
340 			return;
341 		}
342 		break;
343 
344 	/*
345 	 * GC-1000 II timecode format: "hh:mm:ss.f   "
346 	 */
347 	case LENHEATH2:
348 		if (sscanf(pp->a_lastcode, "%2d:%2d:%2d.%c", &pp->hour,
349 		    &pp->minute, &pp->second, &dsec) != 4) {
350 			refclock_report(peer, CEVNT_BADREPLY);
351 			return;
352 		}
353 		break;
354 
355 	default:
356 		refclock_report(peer, CEVNT_BADREPLY);
357 		return;
358 	}
359 
360 	/*
361 	 * We determine the day of the year from the DIPswitches. This
362 	 * should be fixed, since somebody might forget to set them.
363 	 * Someday this hazard will be fixed by a fiendish scheme that
364 	 * looks at the timecode and year the radio shows, then computes
365 	 * the residue of the seconds mod the seconds in a leap cycle.
366 	 * If in the third year of that cycle and the third and later
367 	 * months of that year, add one to the day. Then, correct the
368 	 * timecode accordingly. Icky pooh. This bit of nonsense could
369 	 * be avoided if the engineers had been required to write a
370 	 * device driver before finalizing the timecode format.
371 	 */
372 	if (month < 1 || month > 12 || day < 1) {
373 		refclock_report(peer, CEVNT_BADTIME);
374 		return;
375 	}
376 	if (pp->year % 4) {
377 		if (day > day1tab[month - 1]) {
378 			refclock_report(peer, CEVNT_BADTIME);
379 			return;
380 		}
381 		for (i = 0; i < month - 1; i++)
382 		    day += day1tab[i];
383 	} else {
384 		if (day > day2tab[month - 1]) {
385 			refclock_report(peer, CEVNT_BADTIME);
386 			return;
387 		}
388 		for (i = 0; i < month - 1; i++)
389 		    day += day2tab[i];
390 	}
391 	pp->day = day;
392 
393 	/*
394 	 * Determine synchronization and last update
395 	 */
396 	if (!isdigit((int)dsec))
397 		pp->leap = LEAP_NOTINSYNC;
398 	else {
399 		pp->msec = (dsec - '0') * 100;
400 		pp->leap = LEAP_NOWARNING;
401 	}
402 	if (!refclock_process(pp))
403 		refclock_report(peer, CEVNT_BADTIME);
404 }
405 
406 
407 /*
408  * heath_poll - called by the transmit procedure
409  */
410 static void
411 heath_poll(
412 	int unit,
413 	struct peer *peer
414 	)
415 {
416 	register struct heathunit *up;
417 	struct refclockproc *pp;
418 	int bits = TIOCM_RTS;
419 
420 	/*
421 	 * At each poll we check for timeout and toggle the RTS modem
422 	 * control line, then take a timestamp. Presumably, this is the
423 	 * event the radio captures to generate the timecode.
424 	 */
425 	pp = peer->procptr;
426 	up = (struct heathunit *)pp->unitptr;
427 	pp->polls++;
428 
429 	/*
430 	 * We toggle the RTS modem control lead (GC-1000) and sent a T
431 	 * (GC-1000 II) to kick a timecode loose from the radio. This
432 	 * code works only for POSIX and SYSV interfaces. With bsd you
433 	 * are on your own. We take a timestamp between the up and down
434 	 * edges to lengthen the pulse, which should be about 50 usec on
435 	 * a Sun IPC. With hotshot CPUs, the pulse might get too short.
436 	 * Later.
437 	 */
438 	if (ioctl(pp->io.fd, TIOCMBIC, (char *)&bits) < 0)
439 		refclock_report(peer, CEVNT_FAULT);
440 	get_systime(&up->tstamp);
441 	ioctl(pp->io.fd, TIOCMBIS, (char *)&bits);
442 	if (write(pp->io.fd, "T", 1) != 1)
443 		refclock_report(peer, CEVNT_FAULT);
444 	if (peer->burst > 0)
445 		return;
446 	if (pp->coderecv == pp->codeproc) {
447 		refclock_report(peer, CEVNT_TIMEOUT);
448 		return;
449 	}
450 	record_clock_stats(&peer->srcadr, pp->a_lastcode);
451 	refclock_receive(peer);
452 	peer->burst = NSTAGE;
453 }
454 
455 #else
456 int refclock_heath_bs;
457 #endif /* REFCLOCK */
458