xref: /freebsd/contrib/ntp/ntpd/refclock_heath.c (revision f5f40dd63bc7acbb5312b26ac1ea1103c12352a6)
1c0b746e5SOllivier Robert /*
2ea906c41SOllivier Robert  * refclock_heath - clock driver for Heath GC-1000
3ea906c41SOllivier Robert  * (but no longer the GC-1001 Model II, which apparently never worked)
4c0b746e5SOllivier Robert  */
5ea906c41SOllivier Robert 
6c0b746e5SOllivier Robert #ifdef HAVE_CONFIG_H
7c0b746e5SOllivier Robert # include <config.h>
8c0b746e5SOllivier Robert #endif
9c0b746e5SOllivier Robert 
10c0b746e5SOllivier Robert #if defined(REFCLOCK) && defined(CLOCK_HEATH)
11c0b746e5SOllivier Robert 
12c0b746e5SOllivier Robert #include "ntpd.h"
13c0b746e5SOllivier Robert #include "ntp_io.h"
14c0b746e5SOllivier Robert #include "ntp_refclock.h"
15c0b746e5SOllivier Robert #include "ntp_stdlib.h"
16c0b746e5SOllivier Robert 
17224ba2bdSOllivier Robert #include <stdio.h>
18224ba2bdSOllivier Robert #include <ctype.h>
19224ba2bdSOllivier Robert 
20224ba2bdSOllivier Robert #ifdef HAVE_SYS_IOCTL_H
21224ba2bdSOllivier Robert # include <sys/ioctl.h>
22224ba2bdSOllivier Robert #endif /* not HAVE_SYS_IOCTL_H */
23224ba2bdSOllivier Robert 
24c0b746e5SOllivier Robert /*
25c0b746e5SOllivier Robert  * This driver supports the Heath GC-1000 Most Accurate Clock, with
26c0b746e5SOllivier Robert  * RS232C Output Accessory. This is a WWV/WWVH receiver somewhat less
27c0b746e5SOllivier Robert  * robust than other supported receivers. Its claimed accuracy is 100 ms
28c0b746e5SOllivier Robert  * when actually synchronized to the broadcast signal, but this doesn't
29c0b746e5SOllivier Robert  * happen even most of the time, due to propagation conditions, ambient
30c0b746e5SOllivier Robert  * noise sources, etc. When not synchronized, the accuracy is at the
31c0b746e5SOllivier Robert  * whim of the internal clock oscillator, which can wander into the
32c0b746e5SOllivier Robert  * sunset without warning. Since the indicated precision is 100 ms,
33c0b746e5SOllivier Robert  * expect a host synchronized only to this thing to wander to and fro,
34c0b746e5SOllivier Robert  * occasionally being rudely stepped when the offset exceeds the default
35c0b746e5SOllivier Robert  * clock_max of 128 ms.
36c0b746e5SOllivier Robert  *
37ea906c41SOllivier Robert  * There were two GC-1000 versions supported by this driver. The original
38224ba2bdSOllivier Robert  * GC-1000 with RS-232 output first appeared in 1983, but dissapeared
39ea906c41SOllivier Robert  * from the market a few years later. The GC-1001 II with RS-232 output
40224ba2bdSOllivier Robert  * first appeared circa 1990, but apparently is no longer manufactured.
41224ba2bdSOllivier Robert  * The two models differ considerably, both in interface and commands.
42224ba2bdSOllivier Robert  * The GC-1000 has a pseudo-bipolar timecode output triggered by a RTS
43224ba2bdSOllivier Robert  * transition. The timecode includes both the day of year and time of
44ea906c41SOllivier Robert  * day. The GC-1001 II has a true bipolar output and a complement of
45224ba2bdSOllivier Robert  * single character commands. The timecode includes only the time of
46224ba2bdSOllivier Robert  * day.
47224ba2bdSOllivier Robert  *
48ea906c41SOllivier Robert  * The GC-1001 II was apparently never tested and, based on a Coverity
49ea906c41SOllivier Robert  * scan, apparently never worked [Bug 689].  Related code has been disabled.
50ea906c41SOllivier Robert  *
51224ba2bdSOllivier Robert  * GC-1000
52224ba2bdSOllivier Robert  *
53224ba2bdSOllivier Robert  * The internal DIPswitches should be set to operate in MANUAL mode. The
54224ba2bdSOllivier Robert  * external DIPswitches should be set to GMT and 24-hour format.
55c0b746e5SOllivier Robert  *
56c0b746e5SOllivier Robert  * In MANUAL mode the clock responds to a rising edge of the request to
57c0b746e5SOllivier Robert  * send (RTS) modem control line by sending the timecode. Therefore, it
58c0b746e5SOllivier Robert  * is necessary that the operating system implement the TIOCMBIC and
59c0b746e5SOllivier Robert  * TIOCMBIS ioctl system calls and TIOCM_RTS control bit. Present
60c0b746e5SOllivier Robert  * restrictions require the use of a POSIX-compatible programming
61c0b746e5SOllivier Robert  * interface, although other interfaces may work as well.
62c0b746e5SOllivier Robert  *
63c0b746e5SOllivier Robert  * A simple hardware modification to the clock can be made which
64c0b746e5SOllivier Robert  * prevents the clock hearing the request to send (RTS) if the HI SPEC
65c0b746e5SOllivier Robert  * lamp is out. Route the HISPEC signal to the tone decoder board pin
66c0b746e5SOllivier Robert  * 19, from the display, pin 19. Isolate pin 19 of the decoder board
67c0b746e5SOllivier Robert  * first, but maintain connection with pin 10. Also isolate pin 38 of
68c0b746e5SOllivier Robert  * the CPU on the tone board, and use half an added 7400 to gate the
69c0b746e5SOllivier Robert  * original signal to pin 38 with that from pin 19.
70c0b746e5SOllivier Robert  *
71c0b746e5SOllivier Robert  * The clock message consists of 23 ASCII printing characters in the
72c0b746e5SOllivier Robert  * following format:
73c0b746e5SOllivier Robert  *
74c0b746e5SOllivier Robert  * hh:mm:ss.f AM  dd/mm/yr<cr>
75c0b746e5SOllivier Robert  *
76c0b746e5SOllivier Robert  *	hh:mm:ss.f = hours, minutes, seconds
77c0b746e5SOllivier Robert  *	f = deciseconds ('?' when out of spec)
78c0b746e5SOllivier Robert  *	AM/PM/bb = blank in 24-hour mode
79c0b746e5SOllivier Robert  *	dd/mm/yr = day, month, year
80c0b746e5SOllivier Robert  *
81c0b746e5SOllivier Robert  * The alarm condition is indicated by '?', rather than a digit, at f.
82c0b746e5SOllivier Robert  * Note that 0?:??:??.? is displayed before synchronization is first
83c0b746e5SOllivier Robert  * established and hh:mm:ss.? once synchronization is established and
84c0b746e5SOllivier Robert  * then lost again for about a day.
85c0b746e5SOllivier Robert  *
86ea906c41SOllivier Robert  * GC-1001 II
87224ba2bdSOllivier Robert  *
88224ba2bdSOllivier Robert  * Commands consist of a single letter and are case sensitive. When
89224ba2bdSOllivier Robert  * enterred in lower case, a description of the action performed is
90224ba2bdSOllivier Robert  * displayed. When enterred in upper case the action is performed.
91224ba2bdSOllivier Robert  * Following is a summary of descriptions as displayed by the clock:
92224ba2bdSOllivier Robert  *
93224ba2bdSOllivier Robert  * The clock responds with a command The 'A' command returns an ASCII
94224ba2bdSOllivier Robert  * local time string:  HH:MM:SS.T xx<CR>, where
95224ba2bdSOllivier Robert  *
96224ba2bdSOllivier Robert  *	HH = hours
97224ba2bdSOllivier Robert  *	MM = minutes
98224ba2bdSOllivier Robert  *	SS = seconds
99224ba2bdSOllivier Robert  *	T = tenths-of-seconds
100224ba2bdSOllivier Robert  *	xx = 'AM', 'PM', or '  '
101224ba2bdSOllivier Robert  *	<CR> = carriage return
102224ba2bdSOllivier Robert  *
103224ba2bdSOllivier Robert  * The 'D' command returns 24 pairs of bytes containing the variable
104224ba2bdSOllivier Robert  * divisor value at the end of each of the previous 24 hours. This
105224ba2bdSOllivier Robert  * allows the timebase trimming process to be observed.  UTC hour 00 is
106224ba2bdSOllivier Robert  * always returned first. The first byte of each pair is the high byte
107224ba2bdSOllivier Robert  * of (variable divisor * 16); the second byte is the low byte of
108224ba2bdSOllivier Robert  * (variable divisor * 16). For example, the byte pair 3C 10 would be
109224ba2bdSOllivier Robert  * returned for a divisor of 03C1 hex (961 decimal).
110224ba2bdSOllivier Robert  *
111224ba2bdSOllivier Robert  * The 'I' command returns:  | TH | TL | ER | DH | DL | U1 | I1 | I2 | ,
112224ba2bdSOllivier Robert  * where
113224ba2bdSOllivier Robert  *
114224ba2bdSOllivier Robert  *	TH = minutes since timebase last trimmed (high byte)
115224ba2bdSOllivier Robert  *	TL = minutes since timebase last trimmed (low byte)
116224ba2bdSOllivier Robert  *	ER = last accumulated error in 1.25 ms increments
117224ba2bdSOllivier Robert  *	DH = high byte of (current variable divisor * 16)
118224ba2bdSOllivier Robert  *	DL = low byte of (current variable divisor * 16)
119224ba2bdSOllivier Robert  *	U1 = UT1 offset (/.1 s):  | + | 4 | 2 | 1 | 0 | 0 | 0 | 0 |
120224ba2bdSOllivier Robert  *	I1 = information byte 1:  | W | C | D | I | U | T | Z | 1 | ,
121224ba2bdSOllivier Robert  *	     where
122224ba2bdSOllivier Robert  *
123224ba2bdSOllivier Robert  *		W = set by WWV(H)
124224ba2bdSOllivier Robert  *		C = CAPTURE LED on
125224ba2bdSOllivier Robert  *		D = TRIM DN LED on
126224ba2bdSOllivier Robert  *		I = HI SPEC LED on
127224ba2bdSOllivier Robert  *		U = TRIM UP LED on
128224ba2bdSOllivier Robert  *		T = DST switch on
129224ba2bdSOllivier Robert  *		Z = UTC switch on
130224ba2bdSOllivier Robert  *		1 = UT1 switch on
131224ba2bdSOllivier Robert  *
132224ba2bdSOllivier Robert  *	I2 = information byte 2:  | 8 | 8 | 4 | 2 | 1 | D | d | S | ,
133224ba2bdSOllivier Robert  *	     where
134224ba2bdSOllivier Robert  *
135224ba2bdSOllivier Robert  *		8, 8, 4, 2, 1 = TIME ZONE switch settings
136224ba2bdSOllivier Robert  *		D = DST bit (#55) in last-received frame
137224ba2bdSOllivier Robert  *		d = DST bit (#2) in last-received frame
138224ba2bdSOllivier Robert  *		S = clock is in simulation mode
139224ba2bdSOllivier Robert  *
140224ba2bdSOllivier Robert  * The 'P' command returns 24 bytes containing the number of frames
141224ba2bdSOllivier Robert  * received without error during UTC hours 00 through 23, providing an
142224ba2bdSOllivier Robert  * indication of hourly propagation.  These bytes are updated each hour
143224ba2bdSOllivier Robert  * to reflect the previous 24 hour period.  UTC hour 00 is always
144224ba2bdSOllivier Robert  * returned first.
145224ba2bdSOllivier Robert  *
146224ba2bdSOllivier Robert  * The 'T' command returns the UTC time:  | HH | MM | SS | T0 | , where
147224ba2bdSOllivier Robert  *	HH = tens-of-hours and hours (packed BCD)
148224ba2bdSOllivier Robert  *	MM = tens-of-minutes and minutes (packed BCD)
149224ba2bdSOllivier Robert  *	SS = tens-of-seconds and seconds (packed BCD)
150224ba2bdSOllivier Robert  *	T = tenths-of-seconds (BCD)
151224ba2bdSOllivier Robert  *
152c0b746e5SOllivier Robert  * Fudge Factors
153c0b746e5SOllivier Robert  *
154c0b746e5SOllivier Robert  * A fudge time1 value of .04 s appears to center the clock offset
155c0b746e5SOllivier Robert  * residuals. The fudge time2 parameter is the local time offset east of
156c0b746e5SOllivier Robert  * Greenwich, which depends on DST. Sorry about that, but the clock
157c0b746e5SOllivier Robert  * gives no hint on what the DIPswitches say.
158c0b746e5SOllivier Robert  */
159c0b746e5SOllivier Robert 
160c0b746e5SOllivier Robert /*
161c0b746e5SOllivier Robert  * Interface definitions
162c0b746e5SOllivier Robert  */
163c0b746e5SOllivier Robert #define	DEVICE		"/dev/heath%d" /* device name and unit */
164c0b746e5SOllivier Robert #define	PRECISION	(-4)	/* precision assumed (about 100 ms) */
165c0b746e5SOllivier Robert #define	REFID		"WWV\0"	/* reference ID */
166c0b746e5SOllivier Robert #define	DESCRIPTION	"Heath GC-1000 Most Accurate Clock" /* WRU */
167c0b746e5SOllivier Robert 
168224ba2bdSOllivier Robert #define LENHEATH1	23	/* min timecode length */
169ea906c41SOllivier Robert #if 0	/* BUG 689 */
170224ba2bdSOllivier Robert #define LENHEATH2	13	/* min timecode length */
171ea906c41SOllivier Robert #endif
172c0b746e5SOllivier Robert 
173c0b746e5SOllivier Robert /*
174c0b746e5SOllivier Robert  * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
175c0b746e5SOllivier Robert  * leap.
176c0b746e5SOllivier Robert  */
177c0b746e5SOllivier Robert static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
178c0b746e5SOllivier Robert static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
179c0b746e5SOllivier Robert 
180c0b746e5SOllivier Robert /*
181224ba2bdSOllivier Robert  * Baud rate table. The GC-1000 supports 1200, 2400 and 4800; the
182ea906c41SOllivier Robert  * GC-1001 II supports only 9600.
183224ba2bdSOllivier Robert  */
184224ba2bdSOllivier Robert static int speed[] = {B1200, B2400, B4800, B9600};
185224ba2bdSOllivier Robert 
186224ba2bdSOllivier Robert /*
187c0b746e5SOllivier Robert  * Function prototypes
188c0b746e5SOllivier Robert  */
1892b15cb3dSCy Schubert static	int	heath_start	(int, struct peer *);
1902b15cb3dSCy Schubert static	void	heath_shutdown	(int, struct peer *);
1912b15cb3dSCy Schubert static	void	heath_receive	(struct recvbuf *);
1922b15cb3dSCy Schubert static	void	heath_poll	(int, struct peer *);
193c0b746e5SOllivier Robert 
194c0b746e5SOllivier Robert /*
195c0b746e5SOllivier Robert  * Transfer vector
196c0b746e5SOllivier Robert  */
197c0b746e5SOllivier Robert struct	refclock refclock_heath = {
198c0b746e5SOllivier Robert 	heath_start,		/* start up driver */
199c0b746e5SOllivier Robert 	heath_shutdown,		/* shut down driver */
200c0b746e5SOllivier Robert 	heath_poll,		/* transmit poll message */
201c0b746e5SOllivier Robert 	noentry,		/* not used (old heath_control) */
202c0b746e5SOllivier Robert 	noentry,		/* initialize driver */
203c0b746e5SOllivier Robert 	noentry,		/* not used (old heath_buginfo) */
204c0b746e5SOllivier Robert 	NOFLAGS			/* not used */
205c0b746e5SOllivier Robert };
206c0b746e5SOllivier Robert 
207c0b746e5SOllivier Robert 
208c0b746e5SOllivier Robert /*
209c0b746e5SOllivier Robert  * heath_start - open the devices and initialize data for processing
210c0b746e5SOllivier Robert  */
211c0b746e5SOllivier Robert static int
212c0b746e5SOllivier Robert heath_start(
213c0b746e5SOllivier Robert 	int unit,
214c0b746e5SOllivier Robert 	struct peer *peer
215c0b746e5SOllivier Robert 	)
216c0b746e5SOllivier Robert {
217c0b746e5SOllivier Robert 	struct refclockproc *pp;
218c0b746e5SOllivier Robert 	int fd;
219c0b746e5SOllivier Robert 	char device[20];
220c0b746e5SOllivier Robert 
221c0b746e5SOllivier Robert 	/*
222c0b746e5SOllivier Robert 	 * Open serial port
223c0b746e5SOllivier Robert 	 */
2242b15cb3dSCy Schubert 	snprintf(device, sizeof(device), DEVICE, unit);
225a466cc55SCy Schubert 	fd = refclock_open(&peer->srcadr, device, speed[peer->ttl & 0x3],
2262b15cb3dSCy Schubert 			   LDISC_REMOTE);
2272b15cb3dSCy Schubert 	if (fd <= 0)
228c0b746e5SOllivier Robert 		return (0);
229c0b746e5SOllivier Robert 	pp = peer->procptr;
230c0b746e5SOllivier Robert 	pp->io.clock_recv = heath_receive;
2312b15cb3dSCy Schubert 	pp->io.srcclock = peer;
232c0b746e5SOllivier Robert 	pp->io.datalen = 0;
233c0b746e5SOllivier Robert 	pp->io.fd = fd;
234c0b746e5SOllivier Robert 	if (!io_addclock(&pp->io)) {
2352b15cb3dSCy Schubert 		close(fd);
2362b15cb3dSCy Schubert 		pp->io.fd = -1;
237c0b746e5SOllivier Robert 		return (0);
238c0b746e5SOllivier Robert 	}
239c0b746e5SOllivier Robert 
240c0b746e5SOllivier Robert 	/*
241c0b746e5SOllivier Robert 	 * Initialize miscellaneous variables
242c0b746e5SOllivier Robert 	 */
243c0b746e5SOllivier Robert 	peer->precision = PRECISION;
244c0b746e5SOllivier Robert 	pp->clockdesc = DESCRIPTION;
2452b15cb3dSCy Schubert 	memcpy(&pp->refid, REFID, 4);
246c0b746e5SOllivier Robert 	return (1);
247c0b746e5SOllivier Robert }
248c0b746e5SOllivier Robert 
249c0b746e5SOllivier Robert 
250c0b746e5SOllivier Robert /*
251c0b746e5SOllivier Robert  * heath_shutdown - shut down the clock
252c0b746e5SOllivier Robert  */
253c0b746e5SOllivier Robert static void
254c0b746e5SOllivier Robert heath_shutdown(
255c0b746e5SOllivier Robert 	int unit,
256c0b746e5SOllivier Robert 	struct peer *peer
257c0b746e5SOllivier Robert 	)
258c0b746e5SOllivier Robert {
259c0b746e5SOllivier Robert 	struct refclockproc *pp;
260c0b746e5SOllivier Robert 
261c0b746e5SOllivier Robert 	pp = peer->procptr;
2622b15cb3dSCy Schubert 	if (-1 != pp->io.fd)
263c0b746e5SOllivier Robert 		io_closeclock(&pp->io);
264c0b746e5SOllivier Robert }
265c0b746e5SOllivier Robert 
266c0b746e5SOllivier Robert 
267c0b746e5SOllivier Robert /*
268c0b746e5SOllivier Robert  * heath_receive - receive data from the serial interface
269c0b746e5SOllivier Robert  */
270c0b746e5SOllivier Robert static void
271c0b746e5SOllivier Robert heath_receive(
272c0b746e5SOllivier Robert 	struct recvbuf *rbufp
273c0b746e5SOllivier Robert 	)
274c0b746e5SOllivier Robert {
275c0b746e5SOllivier Robert 	struct refclockproc *pp;
276c0b746e5SOllivier Robert 	struct peer *peer;
277c0b746e5SOllivier Robert 	l_fp trtmp;
278c0b746e5SOllivier Robert 	int month, day;
279c0b746e5SOllivier Robert 	int i;
280c0b746e5SOllivier Robert 	char dsec, a[5];
281c0b746e5SOllivier Robert 
282c0b746e5SOllivier Robert 	/*
283c0b746e5SOllivier Robert 	 * Initialize pointers and read the timecode and timestamp
284c0b746e5SOllivier Robert 	 */
2852b15cb3dSCy Schubert 	peer = rbufp->recv_peer;
286c0b746e5SOllivier Robert 	pp = peer->procptr;
287224ba2bdSOllivier Robert 	pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX,
288224ba2bdSOllivier Robert 	    &trtmp);
289c0b746e5SOllivier Robert 
290c0b746e5SOllivier Robert 	/*
291c0b746e5SOllivier Robert 	 * We get down to business, check the timecode format and decode
292c0b746e5SOllivier Robert 	 * its contents. If the timecode has invalid length or is not in
293c0b746e5SOllivier Robert 	 * proper format, we declare bad format and exit.
294c0b746e5SOllivier Robert 	 */
295224ba2bdSOllivier Robert 	switch (pp->lencode) {
296c0b746e5SOllivier Robert 
297c0b746e5SOllivier Robert 	/*
298224ba2bdSOllivier Robert 	 * GC-1000 timecode format: "hh:mm:ss.f AM  mm/dd/yy"
299ea906c41SOllivier Robert 	 * GC-1001 II timecode format: "hh:mm:ss.f   "
300c0b746e5SOllivier Robert 	 */
301224ba2bdSOllivier Robert 	case LENHEATH1:
302224ba2bdSOllivier Robert 		if (sscanf(pp->a_lastcode,
303224ba2bdSOllivier Robert 		    "%2d:%2d:%2d.%c%5c%2d/%2d/%2d", &pp->hour,
304224ba2bdSOllivier Robert 		    &pp->minute, &pp->second, &dsec, a, &month, &day,
305c0b746e5SOllivier Robert 		    &pp->year) != 8) {
306c0b746e5SOllivier Robert 			refclock_report(peer, CEVNT_BADREPLY);
307c0b746e5SOllivier Robert 			return;
308c0b746e5SOllivier Robert 		}
309c0b746e5SOllivier Robert 		break;
310c0b746e5SOllivier Robert 
311ea906c41SOllivier Robert #if 0	/* BUG 689 */
312c0b746e5SOllivier Robert 	/*
313ea906c41SOllivier Robert 	 * GC-1001 II timecode format: "hh:mm:ss.f   "
314c0b746e5SOllivier Robert 	 */
315224ba2bdSOllivier Robert 	case LENHEATH2:
316224ba2bdSOllivier Robert 		if (sscanf(pp->a_lastcode, "%2d:%2d:%2d.%c", &pp->hour,
317224ba2bdSOllivier Robert 		    &pp->minute, &pp->second, &dsec) != 4) {
318224ba2bdSOllivier Robert 			refclock_report(peer, CEVNT_BADREPLY);
319c0b746e5SOllivier Robert 			return;
320ea906c41SOllivier Robert 		} else {
321ea906c41SOllivier Robert 			struct tm *tm_time_p;
322ea906c41SOllivier Robert 			time_t     now;
323ea906c41SOllivier Robert 
324ea906c41SOllivier Robert 			time(&now);	/* we should grab 'now' earlier */
325ea906c41SOllivier Robert 			tm_time_p = gmtime(&now);
326ea906c41SOllivier Robert 			/*
327ea906c41SOllivier Robert 			 * There is a window of time around midnight
328ea906c41SOllivier Robert 			 * where this will Do The Wrong Thing.
329ea906c41SOllivier Robert 			 */
330ea906c41SOllivier Robert 			if (tm_time_p) {
331ea906c41SOllivier Robert 				month = tm_time_p->tm_mon + 1;
332ea906c41SOllivier Robert 				day = tm_time_p->tm_mday;
333ea906c41SOllivier Robert 			} else {
334ea906c41SOllivier Robert 				refclock_report(peer, CEVNT_FAULT);
335ea906c41SOllivier Robert 				return;
336ea906c41SOllivier Robert 			}
337c0b746e5SOllivier Robert 		}
338224ba2bdSOllivier Robert 		break;
339ea906c41SOllivier Robert #endif
340c0b746e5SOllivier Robert 
341224ba2bdSOllivier Robert 	default:
342224ba2bdSOllivier Robert 		refclock_report(peer, CEVNT_BADREPLY);
343224ba2bdSOllivier Robert 		return;
344c0b746e5SOllivier Robert 	}
345c0b746e5SOllivier Robert 
346c0b746e5SOllivier Robert 	/*
347c0b746e5SOllivier Robert 	 * We determine the day of the year from the DIPswitches. This
348c0b746e5SOllivier Robert 	 * should be fixed, since somebody might forget to set them.
349c0b746e5SOllivier Robert 	 * Someday this hazard will be fixed by a fiendish scheme that
350c0b746e5SOllivier Robert 	 * looks at the timecode and year the radio shows, then computes
351c0b746e5SOllivier Robert 	 * the residue of the seconds mod the seconds in a leap cycle.
352c0b746e5SOllivier Robert 	 * If in the third year of that cycle and the third and later
353c0b746e5SOllivier Robert 	 * months of that year, add one to the day. Then, correct the
354c0b746e5SOllivier Robert 	 * timecode accordingly. Icky pooh. This bit of nonsense could
355c0b746e5SOllivier Robert 	 * be avoided if the engineers had been required to write a
356c0b746e5SOllivier Robert 	 * device driver before finalizing the timecode format.
357c0b746e5SOllivier Robert 	 */
358c0b746e5SOllivier Robert 	if (month < 1 || month > 12 || day < 1) {
359c0b746e5SOllivier Robert 		refclock_report(peer, CEVNT_BADTIME);
360c0b746e5SOllivier Robert 		return;
361c0b746e5SOllivier Robert 	}
362c0b746e5SOllivier Robert 	if (pp->year % 4) {
363c0b746e5SOllivier Robert 		if (day > day1tab[month - 1]) {
364c0b746e5SOllivier Robert 			refclock_report(peer, CEVNT_BADTIME);
365c0b746e5SOllivier Robert 			return;
366c0b746e5SOllivier Robert 		}
367c0b746e5SOllivier Robert 		for (i = 0; i < month - 1; i++)
368c0b746e5SOllivier Robert 		    day += day1tab[i];
369c0b746e5SOllivier Robert 	} else {
370c0b746e5SOllivier Robert 		if (day > day2tab[month - 1]) {
371c0b746e5SOllivier Robert 			refclock_report(peer, CEVNT_BADTIME);
372c0b746e5SOllivier Robert 			return;
373c0b746e5SOllivier Robert 		}
374c0b746e5SOllivier Robert 		for (i = 0; i < month - 1; i++)
375c0b746e5SOllivier Robert 		    day += day2tab[i];
376c0b746e5SOllivier Robert 	}
377c0b746e5SOllivier Robert 	pp->day = day;
378c0b746e5SOllivier Robert 
379c0b746e5SOllivier Robert 	/*
380c0b746e5SOllivier Robert 	 * Determine synchronization and last update
381c0b746e5SOllivier Robert 	 */
3822b15cb3dSCy Schubert 	if (!isdigit((unsigned char)dsec))
383c0b746e5SOllivier Robert 		pp->leap = LEAP_NOTINSYNC;
384c0b746e5SOllivier Robert 	else {
3859c2daa00SOllivier Robert 		pp->nsec = (dsec - '0') * 100000000;
386c0b746e5SOllivier Robert 		pp->leap = LEAP_NOWARNING;
387c0b746e5SOllivier Robert 	}
388c0b746e5SOllivier Robert 	if (!refclock_process(pp))
389c0b746e5SOllivier Robert 		refclock_report(peer, CEVNT_BADTIME);
390c0b746e5SOllivier Robert }
391c0b746e5SOllivier Robert 
392c0b746e5SOllivier Robert 
393c0b746e5SOllivier Robert /*
394c0b746e5SOllivier Robert  * heath_poll - called by the transmit procedure
395c0b746e5SOllivier Robert  */
396c0b746e5SOllivier Robert static void
397c0b746e5SOllivier Robert heath_poll(
398c0b746e5SOllivier Robert 	int unit,
399c0b746e5SOllivier Robert 	struct peer *peer
400c0b746e5SOllivier Robert 	)
401c0b746e5SOllivier Robert {
402c0b746e5SOllivier Robert 	struct refclockproc *pp;
403c0b746e5SOllivier Robert 	int bits = TIOCM_RTS;
404c0b746e5SOllivier Robert 
405c0b746e5SOllivier Robert 	/*
406c0b746e5SOllivier Robert 	 * At each poll we check for timeout and toggle the RTS modem
407c0b746e5SOllivier Robert 	 * control line, then take a timestamp. Presumably, this is the
408c0b746e5SOllivier Robert 	 * event the radio captures to generate the timecode.
4099c2daa00SOllivier Robert 	 * Apparently, the radio takes about a second to make up its
4109c2daa00SOllivier Robert 	 * mind to send a timecode, so the receive timestamp is
4119c2daa00SOllivier Robert 	 * worthless.
412c0b746e5SOllivier Robert 	 */
413c0b746e5SOllivier Robert 	pp = peer->procptr;
414c0b746e5SOllivier Robert 
415c0b746e5SOllivier Robert 	/*
416224ba2bdSOllivier Robert 	 * We toggle the RTS modem control lead (GC-1000) and sent a T
417ea906c41SOllivier Robert 	 * (GC-1001 II) to kick a timecode loose from the radio. This
418224ba2bdSOllivier Robert 	 * code works only for POSIX and SYSV interfaces. With bsd you
419224ba2bdSOllivier Robert 	 * are on your own. We take a timestamp between the up and down
420224ba2bdSOllivier Robert 	 * edges to lengthen the pulse, which should be about 50 usec on
421224ba2bdSOllivier Robert 	 * a Sun IPC. With hotshot CPUs, the pulse might get too short.
422224ba2bdSOllivier Robert 	 * Later.
423ea906c41SOllivier Robert 	 *
424ea906c41SOllivier Robert 	 * Bug 689: Even though we no longer support the GC-1001 II,
425ea906c41SOllivier Robert 	 * I'm leaving the 'T' write in for timing purposes.
426c0b746e5SOllivier Robert 	 */
427c0b746e5SOllivier Robert 	if (ioctl(pp->io.fd, TIOCMBIC, (char *)&bits) < 0)
428c0b746e5SOllivier Robert 		refclock_report(peer, CEVNT_FAULT);
4299c2daa00SOllivier Robert 	get_systime(&pp->lastrec);
430a466cc55SCy Schubert 	if (refclock_write(peer, "T", 1, "T") != 1)
431224ba2bdSOllivier Robert 		refclock_report(peer, CEVNT_FAULT);
4329c2daa00SOllivier Robert 	ioctl(pp->io.fd, TIOCMBIS, (char *)&bits);
433c0b746e5SOllivier Robert 	if (pp->coderecv == pp->codeproc) {
434c0b746e5SOllivier Robert 		refclock_report(peer, CEVNT_TIMEOUT);
435c0b746e5SOllivier Robert 		return;
436c0b746e5SOllivier Robert 	}
4379c2daa00SOllivier Robert 	pp->lastref = pp->lastrec;
438c0b746e5SOllivier Robert 	refclock_receive(peer);
4399c2daa00SOllivier Robert 	record_clock_stats(&peer->srcadr, pp->a_lastcode);
4409c2daa00SOllivier Robert #ifdef DEBUG
4419c2daa00SOllivier Robert 	if (debug)
4429c2daa00SOllivier Robert 	    printf("heath: timecode %d %s\n", pp->lencode,
4439c2daa00SOllivier Robert 		   pp->a_lastcode);
4449c2daa00SOllivier Robert #endif
4459c2daa00SOllivier Robert 	pp->polls++;
446c0b746e5SOllivier Robert }
447c0b746e5SOllivier Robert 
448c0b746e5SOllivier Robert #else
449*f5f40dd6SCy Schubert NONEMPTY_TRANSLATION_UNIT
450c0b746e5SOllivier Robert #endif /* REFCLOCK */
451