summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/ntpd/refclock_ulink.c
blob: 347040dea64ec810a2f422ed2eef852a89bd3aa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * refclock_ulink - clock driver for Ultralink Model 320 WWVB receivers
 * By Dave Strout <dstrout@linuxfoundary.com>
 *
 * Latest version is always on www.linuxfoundary.com
 *
 * Based on the Spectracom driver
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#if defined(REFCLOCK) && defined(CLOCK_ULINK)

#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
#include <time.h>

#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_refclock.h"
#include "ntp_calendar.h"
#include "ntp_stdlib.h"

/*
 * This driver supports the Ultralink Model 320 WWVB receiver.  The Model 320 is
 * an RS-232 powered unit which consists of two parts:  a DB-25 shell that contains
 * a microprocessor, and an approx 2"x4" plastic box that contains the antenna.
 * The two are connected by a 6-wire RJ-25 cable of length up to 1000'.  The 
 * microprocessor steals power from the RS-232 port, which means that the port must 
 * be kept open all of the time.  The unit also has an internal clock for loss of signal
 * periods.  Claimed accuracy is 0.1 sec.
 * 
 * The timecode format is:
 *
 *  <cr><lf>SQRYYYYDDD+HH:MM:SS.mmLT<cr>
 *
 * where:
 *
 * S = 'S' -- sync'd in last hour, '0'-'9' - hours x 10 since last update, else '?'
 * Q = Number of correlating time-frames, from 0 to 5
 * R = 'R' -- reception in progress, 'N' -- Noisy reception, ' ' -- standby mode
 * YYYY = year from 1990 to 2089
 * DDD = current day from 1 to 366
 * + = '+' if current year is a leap year, else ' '
 * HH = UTC hour 0 to 23
 * MM = Minutes of current hour from 0 to 59
 * SS = Seconds of current minute from 0 to 59
 * mm = 10's milliseconds of the current second from 00 to 99
 * L  = Leap second pending at end of month -- 'I' = inset, 'D'=delete
 * T  = DST <-> STD transition indicators
 *
 * Note that this driver does not do anything with the L or T flags.
 *
 * The M320 also has a 'U' command which returns UT1 correction information.  It
 * is not used in this driver.
 *
 */

/*
 * Interface definitions
 */
#define	DEVICE		"/dev/ulink%d" /* device name and unit */
#define	SPEED232	B9600	/* uart speed (9600 baud) */
#define	PRECISION	(-13)	/* precision assumed (about 100 us) */
#define	REFID		"M320"	/* reference ID */
#define	DESCRIPTION	"Ultralink WWVB Receiver" /* WRU */

#define	LENWWVB0	28	/* format 0 timecode length */
#define	LENWWVB2	24	/* format 2 timecode length */
#define LENWWVB3        29      /* format 3 timecode length */

#define MONLIN		15	/* number of monitoring lines */

/*
 * ULINK unit control structure
 */
struct ulinkunit {
	u_char	tcswitch;	/* timecode switch */
	l_fp	laststamp;	/* last receive timestamp */
	u_char	lasthour;	/* last hour (for monitor) */
	u_char	linect;		/* count ignored lines (for monitor */
};

/*
 * Function prototypes
 */
static	int	ulink_start	P((int, struct peer *));
static	void	ulink_shutdown	P((int, struct peer *));
static	void	ulink_receive	P((struct recvbuf *));
static	void	ulink_poll	P((int, struct peer *));
static  int     fd; /* We need to keep the serial port open to power the ULM320 */

/*
 * Transfer vector
 */
struct	refclock refclock_ulink = {
	ulink_start,		/* start up driver */
	ulink_shutdown,		/* shut down driver */
	ulink_poll,		/* transmit poll message */
	noentry,		/* not used (old wwvb_control) */
	noentry,		/* initialize driver (not used) */
	noentry,		/* not used (old wwvb_buginfo) */
	NOFLAGS			/* not used */
};


/*
 * ulink_start - open the devices and initialize data for processing
 */
static int
ulink_start(
	int unit,
	struct peer *peer
	)
{
	register struct ulinkunit *up;
	struct refclockproc *pp;
	char device[20];
	fprintf(stderr, "Starting Ulink driver\n");
	/*
	 * Open serial port. Use CLK line discipline, if available.
	 */
	(void)sprintf(device, DEVICE, unit);
	if (!(fd = refclock_open(device, SPEED232, LDISC_CLK)))
		return (0);

	/*
	 * Allocate and initialize unit structure
	 */
	if (!(up = (struct ulinkunit *)
	      emalloc(sizeof(struct ulinkunit)))) {
		(void) close(fd);
		return (0);
	}
	memset((char *)up, 0, sizeof(struct ulinkunit));
	pp = peer->procptr;
	pp->unitptr = (caddr_t)up;
	pp->io.clock_recv = ulink_receive;
	pp->io.srcclock = (caddr_t)peer;
	pp->io.datalen = 0;
	pp->io.fd = fd;
	if (!io_addclock(&pp->io)) {
		(void) close(fd);
		free(up);
		return (0);
	}

	/*
	 * Initialize miscellaneous variables
	 */
	peer->precision = PRECISION;
	peer->flags |= FLAG_BURST;
	peer->burst = NSTAGE;
	pp->clockdesc = DESCRIPTION;
	memcpy((char *)&pp->refid, REFID, 4);
	return (1);
}


/*
 * ulink_shutdown - shut down the clock
 */
static void
ulink_shutdown(
	int unit,
	struct peer *peer
	)
{
	register struct ulinkunit *up;
	struct refclockproc *pp;

	pp = peer->procptr;
	up = (struct ulinkunit *)pp->unitptr;
	io_closeclock(&pp->io);
	free(up);
	close(fd);
}


/*
 * ulink_receive - receive data from the serial interface
 */
static void
ulink_receive(
	struct recvbuf *rbufp
	)
{
	struct ulinkunit *up;
	struct refclockproc *pp;
	struct peer *peer;

	l_fp	trtmp;		/* arrival timestamp */
	char	syncchar;	/* synchronization indicator */
	char	qualchar;	/* quality indicator */
	char    modechar;       /* Modes: 'R'=rx, 'N'=noise, ' '=standby */
	char	leapchar;	/* leap indicator */
	int	temp;		/* int temp */

	/*
	 * Initialize pointers and read the timecode and timestamp
	 */
	peer = (struct peer *)rbufp->recv_srcclock;
	pp = peer->procptr;
	up = (struct ulinkunit *)pp->unitptr;
	temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);

	/*
	 * Note we get a buffer and timestamp for both a <cr> and <lf>,
	 * but only the <cr> timestamp is retained. Note: in format 0 on
	 * a Netclock/2 or upgraded 8170 the start bit is delayed 100
	 * +-50 us relative to the pps; however, on an unmodified 8170
	 * the start bit can be delayed up to 10 ms. In format 2 the
	 * reading precision is only to the millisecond. Thus, unless
	 * you have a pps gadget and don't have to have the year, format
	 * 0 provides the lowest jitter.
	 */
	if (temp == 0) {
		if (up->tcswitch == 0) {
			up->tcswitch = 1;
			up->laststamp = trtmp;
		} else
		    up->tcswitch = 0;
		return;
	}
	pp->lencode = temp;
	pp->lastrec = up->laststamp;
	up->laststamp = trtmp;
	up->tcswitch = 1;
#ifdef DEBUG
	if (debug)
		printf("ulink: timecode %d %s\n", pp->lencode,
		    pp->a_lastcode);
#endif

	/*
	 * We get down to business, check the timecode format and decode
	 * its contents. This code uses the timecode length to determine
	 * whether format 0 or format 2. If the timecode has invalid
	 * length or is not in proper format, we declare bad format and
	 * exit.
	 */
	syncchar = qualchar = leapchar = ' ';
	pp->msec = 0;
	
	/*
	 * Timecode format SQRYYYYDDD+HH:MM:SS.mmLT
	 */
	sscanf(pp->a_lastcode, "%c%c%c%4d%3d%c%2d:%2d:%2d.%2d",
	       &syncchar, &qualchar, &modechar, &pp->year, &pp->day,
	       &leapchar,&pp->hour, &pp->minute, &pp->second,&pp->msec);

	pp->msec *= 10; /* M320 returns 10's of msecs */
	qualchar = ' ';

	/*
	 * Decode synchronization, quality and leap characters. If
	 * unsynchronized, set the leap bits accordingly and exit.
	 * Otherwise, set the leap bits according to the leap character.
	 * Once synchronized, the dispersion depends only on the
	 * quality character.
	 */
	pp->disp = .001;
	pp->leap = LEAP_NOWARNING;

	/*
	 * Process the new sample in the median filter and determine the
	 * timecode timestamp.
	 */
	if (!refclock_process(pp))
		refclock_report(peer, CEVNT_BADTIME);
}


/*
 * ulink_poll - called by the transmit procedure
 */
static void
ulink_poll(
	int unit,
	struct peer *peer
	)
{
	register struct ulinkunit *up;
	struct refclockproc *pp;
	char pollchar;

	pp = peer->procptr;
	up = (struct ulinkunit *)pp->unitptr;
	pollchar = 'T';
	if (write(pp->io.fd, &pollchar, 1) != 1)
		refclock_report(peer, CEVNT_FAULT);
	else
		pp->polls++;
	if (peer->burst > 0)
		return;
	if (pp->coderecv == pp->codeproc) {
		refclock_report(peer, CEVNT_TIMEOUT);
		return;
	}
	record_clock_stats(&peer->srcadr, pp->a_lastcode);
	refclock_receive(peer);
	peer->burst = NSTAGE;

	/*
	 * If the monitor flag is set (flag4), we dump the internal
	 * quality table at the first timecode beginning the day.
	 */
	if (pp->sloppyclockflag & CLK_FLAG4 && pp->hour <
	    (int)up->lasthour)
		up->linect = MONLIN;
	up->lasthour = pp->hour;
}

#else
int refclock_ulink_bs;
#endif /* REFCLOCK */
OpenPOWER on IntegriCloud