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