xref: /netbsd-src/external/bsd/ntp/dist/ntpd/refclock_datum.c (revision cdfa2a7ef92791ba9db70a584a1d904730e6fb46)
1*cdfa2a7eSchristos /*	$NetBSD: refclock_datum.c,v 1.8 2020/05/25 20:47:25 christos Exp $	*/
2abb0f93cSkardel 
3abb0f93cSkardel /*
4abb0f93cSkardel ** refclock_datum - clock driver for the Datum Programmable Time Server
5abb0f93cSkardel **
6abb0f93cSkardel ** Important note: This driver assumes that you have termios. If you have
7abb0f93cSkardel ** a system that does not have termios, you will have to modify this driver.
8abb0f93cSkardel **
9abb0f93cSkardel ** Sorry, I have only tested this driver on SUN and HP platforms.
10abb0f93cSkardel */
11abb0f93cSkardel 
12abb0f93cSkardel #ifdef HAVE_CONFIG_H
13abb0f93cSkardel # include <config.h>
14abb0f93cSkardel #endif
15abb0f93cSkardel 
162950cc38Schristos #include "ntp_types.h"
172950cc38Schristos 
18abb0f93cSkardel #if defined(REFCLOCK) && defined(CLOCK_DATUM)
19abb0f93cSkardel 
20abb0f93cSkardel /*
21abb0f93cSkardel ** Include Files
22abb0f93cSkardel */
23abb0f93cSkardel 
24abb0f93cSkardel #include "ntpd.h"
25abb0f93cSkardel #include "ntp_io.h"
262950cc38Schristos #include "ntp_tty.h"
27abb0f93cSkardel #include "ntp_refclock.h"
282950cc38Schristos #include "timevalops.h"
29abb0f93cSkardel #include "ntp_stdlib.h"
30abb0f93cSkardel 
31abb0f93cSkardel #include <stdio.h>
32abb0f93cSkardel #include <ctype.h>
33abb0f93cSkardel 
34abb0f93cSkardel #if defined(STREAM)
35abb0f93cSkardel #include <stropts.h>
36abb0f93cSkardel #endif /* STREAM */
37abb0f93cSkardel 
38abb0f93cSkardel #include "ntp_stdlib.h"
39abb0f93cSkardel 
40abb0f93cSkardel /*
41abb0f93cSkardel ** This driver supports the Datum Programmable Time System (PTS) clock.
42abb0f93cSkardel ** The clock works in very straight forward manner. When it receives a
43abb0f93cSkardel ** time code request (e.g., the ascii string "//k/mn"), it responds with
44abb0f93cSkardel ** a seven byte BCD time code. This clock only responds with a
45abb0f93cSkardel ** time code after it first receives the "//k/mn" message. It does not
46abb0f93cSkardel ** periodically send time codes back at some rate once it is started.
47abb0f93cSkardel ** the returned time code can be broken down into the following fields.
48abb0f93cSkardel **
49abb0f93cSkardel **            _______________________________
50abb0f93cSkardel ** Bit Index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
51abb0f93cSkardel **            ===============================
52abb0f93cSkardel ** byte 0:   | -   -   -   - |      H D      |
53abb0f93cSkardel **            ===============================
54abb0f93cSkardel ** byte 1:   |      T D      |      U D      |
55abb0f93cSkardel **            ===============================
56abb0f93cSkardel ** byte 2:   | -   - |  T H  |      U H      |
57abb0f93cSkardel **            ===============================
58abb0f93cSkardel ** byte 3:   | - |    T M    |      U M      |
59abb0f93cSkardel **            ===============================
60abb0f93cSkardel ** byte 4:   | - |    T S    |      U S      |
61abb0f93cSkardel **            ===============================
62abb0f93cSkardel ** byte 5:   |      t S      |      h S      |
63abb0f93cSkardel **            ===============================
64abb0f93cSkardel ** byte 6:   |      m S      | -   -   -   - |
65abb0f93cSkardel **            ===============================
66abb0f93cSkardel **
67abb0f93cSkardel ** In the table above:
68abb0f93cSkardel **
69abb0f93cSkardel **	"-" means don't care
70abb0f93cSkardel **	"H D", "T D", and "U D" means Hundreds, Tens, and Units of Days
71abb0f93cSkardel **	"T H", and "UH" means Tens and Units of Hours
72abb0f93cSkardel **	"T M", and "U M" means Tens and Units of Minutes
73abb0f93cSkardel **	"T S", and "U S" means Tens and Units of Seconds
74abb0f93cSkardel **	"t S", "h S", and "m S" means tenths, hundredths, and thousandths
75abb0f93cSkardel **				of seconds
76abb0f93cSkardel **
77abb0f93cSkardel ** The Datum PTS communicates throught the RS232 port on your machine.
78abb0f93cSkardel ** Right now, it assumes that you have termios. This driver has been tested
79abb0f93cSkardel ** on SUN and HP workstations. The Datum PTS supports various IRIG and
80abb0f93cSkardel ** NASA input codes. This driver assumes that the name of the device is
81abb0f93cSkardel ** /dev/datum. You will need to make a soft link to your RS232 device or
82abb0f93cSkardel ** create a new driver to use this refclock.
83abb0f93cSkardel */
84abb0f93cSkardel 
85abb0f93cSkardel /*
86abb0f93cSkardel ** Datum PTS defines
87abb0f93cSkardel */
88abb0f93cSkardel 
89abb0f93cSkardel /*
90abb0f93cSkardel ** Note that if GMT is defined, then the Datum PTS must use Greenwich
91abb0f93cSkardel ** time. Otherwise, this driver allows the Datum PTS to use the current
92abb0f93cSkardel ** wall clock for its time. It determines the time zone offset by minimizing
93abb0f93cSkardel ** the error after trying several time zone offsets. If the Datum PTS
94abb0f93cSkardel ** time is Greenwich time and GMT is not defined, everything should still
95abb0f93cSkardel ** work since the time zone will be found to be 0. What this really means
96abb0f93cSkardel ** is that your system time (at least to start with) must be within the
97abb0f93cSkardel ** correct time by less than +- 30 minutes. The default is for GMT to not
98abb0f93cSkardel ** defined. If you really want to force GMT without the funny +- 30 minute
99abb0f93cSkardel ** stuff then you must define (uncomment) GMT below.
100abb0f93cSkardel */
101abb0f93cSkardel 
102abb0f93cSkardel /*
103abb0f93cSkardel #define GMT
104abb0f93cSkardel #define DEBUG_DATUM_PTC
105abb0f93cSkardel #define LOG_TIME_ERRORS
106abb0f93cSkardel */
107abb0f93cSkardel 
108abb0f93cSkardel 
109abb0f93cSkardel #define	PRECISION	(-10)		/* precision assumed 1/1024 ms */
110abb0f93cSkardel #define	REFID "DATM"			/* reference id */
111abb0f93cSkardel #define DATUM_DISPERSION 0		/* fixed dispersion = 0 ms */
112abb0f93cSkardel #define DATUM_MAX_ERROR 0.100		/* limits on sigma squared */
113abb0f93cSkardel #define DATUM_DEV	"/dev/datum"	/* device name */
114abb0f93cSkardel 
115abb0f93cSkardel #define DATUM_MAX_ERROR2 (DATUM_MAX_ERROR*DATUM_MAX_ERROR)
116abb0f93cSkardel 
117abb0f93cSkardel /*
118abb0f93cSkardel ** The Datum PTS structure
119abb0f93cSkardel */
120abb0f93cSkardel 
121abb0f93cSkardel /*
122abb0f93cSkardel ** I don't use a fixed array of MAXUNITS like everyone else just because
123abb0f93cSkardel ** I don't like to program that way. Sorry if this bothers anyone. I assume
124abb0f93cSkardel ** that you can use any id for your unit and I will search for it in a
125abb0f93cSkardel ** dynamic array of units until I find it. I was worried that users might
126abb0f93cSkardel ** enter a bad id in their configuration file (larger than MAXUNITS) and
127abb0f93cSkardel ** besides, it is just cleaner not to have to assume that you have a fixed
128abb0f93cSkardel ** number of anything in a program.
129abb0f93cSkardel */
130abb0f93cSkardel 
131abb0f93cSkardel struct datum_pts_unit {
132abb0f93cSkardel 	struct peer *peer;		/* peer used by ntp */
133abb0f93cSkardel 	int PTS_fd;			/* file descriptor for PTS */
134abb0f93cSkardel 	u_int unit;			/* id for unit */
135abb0f93cSkardel 	u_long timestarted;		/* time started */
136abb0f93cSkardel 	l_fp lastrec;			/* time tag for the receive time (system) */
137abb0f93cSkardel 	l_fp lastref;			/* reference time (Datum time) */
138abb0f93cSkardel 	u_long yearstart;		/* the year that this clock started */
139abb0f93cSkardel 	int coderecv;			/* number of time codes received */
140abb0f93cSkardel 	int day;			/* day */
141abb0f93cSkardel 	int hour;			/* hour */
142abb0f93cSkardel 	int minute;			/* minutes */
143abb0f93cSkardel 	int second;			/* seconds */
144abb0f93cSkardel 	int msec;			/* miliseconds */
145abb0f93cSkardel 	int usec;			/* miliseconds */
146abb0f93cSkardel 	u_char leap;			/* funny leap character code */
147abb0f93cSkardel 	char retbuf[8];		/* returned time from the datum pts */
148abb0f93cSkardel 	char nbytes;			/* number of bytes received from datum pts */
149abb0f93cSkardel 	double sigma2;		/* average squared error (roughly) */
150abb0f93cSkardel 	int tzoff;			/* time zone offest from GMT */
151abb0f93cSkardel };
152abb0f93cSkardel 
153abb0f93cSkardel /*
154abb0f93cSkardel ** PTS static constant variables for internal use
155abb0f93cSkardel */
156abb0f93cSkardel 
157abb0f93cSkardel static char TIME_REQUEST[6];	/* request message sent to datum for time */
158abb0f93cSkardel static int nunits;		/* number of active units */
159abb0f93cSkardel 
160abb0f93cSkardel /*
161abb0f93cSkardel ** Callback function prototypes that ntpd needs to know about.
162abb0f93cSkardel */
163abb0f93cSkardel 
164abb0f93cSkardel static	int	datum_pts_start		(int, struct peer *);
165abb0f93cSkardel static	void	datum_pts_shutdown	(int, struct peer *);
166abb0f93cSkardel static	void	datum_pts_poll		(int, struct peer *);
1672950cc38Schristos static	void	datum_pts_control	(int, const struct refclockstat *,
168abb0f93cSkardel 					 struct refclockstat *, struct peer *);
169abb0f93cSkardel static	void	datum_pts_init		(void);
170abb0f93cSkardel static	void	datum_pts_buginfo	(int, struct refclockbug *, struct peer *);
171abb0f93cSkardel 
172abb0f93cSkardel /*
173abb0f93cSkardel ** This is the call back function structure that ntpd actually uses for
174abb0f93cSkardel ** this refclock.
175abb0f93cSkardel */
176abb0f93cSkardel 
177abb0f93cSkardel struct	refclock refclock_datum = {
178abb0f93cSkardel 	datum_pts_start,		/* start up a new Datum refclock */
179abb0f93cSkardel 	datum_pts_shutdown,		/* shutdown a Datum refclock */
180abb0f93cSkardel 	datum_pts_poll,		/* sends out the time request */
181abb0f93cSkardel 	datum_pts_control,		/* not used */
182abb0f93cSkardel 	datum_pts_init,		/* initialization (called first) */
183abb0f93cSkardel 	datum_pts_buginfo,		/* not used */
184abb0f93cSkardel 	NOFLAGS			/* we are not setting any special flags */
185abb0f93cSkardel };
186abb0f93cSkardel 
187abb0f93cSkardel /*
188abb0f93cSkardel ** The datum_pts_receive callback function is handled differently from the
189abb0f93cSkardel ** rest. It is passed to the ntpd io data structure. Basically, every
190abb0f93cSkardel ** 64 seconds, the datum_pts_poll() routine is called. It sends out the time
191abb0f93cSkardel ** request message to the Datum Programmable Time System. Then, ntpd
192abb0f93cSkardel ** waits on a select() call to receive data back. The datum_pts_receive()
193abb0f93cSkardel ** function is called as data comes back. We expect a seven byte time
194abb0f93cSkardel ** code to be returned but the datum_pts_receive() function may only get
195abb0f93cSkardel ** a few bytes passed to it at a time. In other words, this routine may
196abb0f93cSkardel ** get called by the io stuff in ntpd a few times before we get all seven
197abb0f93cSkardel ** bytes. Once the last byte is received, we process it and then pass the
198abb0f93cSkardel ** new time measurement to ntpd for updating the system time. For now,
199abb0f93cSkardel ** there is no 3 state filtering done on the time measurements. The
200abb0f93cSkardel ** jitter may be a little high but at least for its current use, it is not
201abb0f93cSkardel ** a problem. We have tried to keep things as simple as possible. This
202abb0f93cSkardel ** clock should not jitter more than 1 or 2 mseconds at the most once
203abb0f93cSkardel ** things settle down. It is important to get the right drift calibrated
204abb0f93cSkardel ** in the ntpd.drift file as well as getting the right tick set up right
205abb0f93cSkardel ** using tickadj for SUNs. Tickadj is not used for the HP but you need to
206abb0f93cSkardel ** remember to bring up the adjtime daemon because HP does not support
207abb0f93cSkardel ** the adjtime() call.
208abb0f93cSkardel */
209abb0f93cSkardel 
210abb0f93cSkardel static	void	datum_pts_receive	(struct recvbuf *);
211abb0f93cSkardel 
212abb0f93cSkardel /*......................................................................*/
213abb0f93cSkardel /*	datum_pts_start - start up the datum PTS. This means open the	*/
214abb0f93cSkardel /*	RS232 device and set up the data structure for my unit.		*/
215abb0f93cSkardel /*......................................................................*/
216abb0f93cSkardel 
217abb0f93cSkardel static int
datum_pts_start(int unit,struct peer * peer)218abb0f93cSkardel datum_pts_start(
219abb0f93cSkardel 	int unit,
220abb0f93cSkardel 	struct peer *peer
221abb0f93cSkardel 	)
222abb0f93cSkardel {
2232950cc38Schristos 	struct refclockproc *pp;
224abb0f93cSkardel 	struct datum_pts_unit *datum_pts;
225abb0f93cSkardel 	int fd;
226abb0f93cSkardel #ifdef HAVE_TERMIOS
2272950cc38Schristos 	int rc;
228abb0f93cSkardel 	struct termios arg;
229abb0f93cSkardel #endif
230abb0f93cSkardel 
231abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
232abb0f93cSkardel 	if (debug)
233abb0f93cSkardel 	    printf("Starting Datum PTS unit %d\n", unit);
234abb0f93cSkardel #endif
235abb0f93cSkardel 
236abb0f93cSkardel 	/*
237abb0f93cSkardel 	** Open the Datum PTS device
238abb0f93cSkardel 	*/
239abb0f93cSkardel 	fd = open(DATUM_DEV, O_RDWR);
240abb0f93cSkardel 
241abb0f93cSkardel 	if (fd < 0) {
242abb0f93cSkardel 		msyslog(LOG_ERR, "Datum_PTS: open(\"%s\", O_RDWR) failed: %m", DATUM_DEV);
243abb0f93cSkardel 		return 0;
244abb0f93cSkardel 	}
245abb0f93cSkardel 
246abb0f93cSkardel 	/*
247abb0f93cSkardel 	** Create the memory for the new unit
248abb0f93cSkardel 	*/
2492950cc38Schristos 	datum_pts = emalloc_zero(sizeof(*datum_pts));
250abb0f93cSkardel 	datum_pts->unit = unit;	/* set my unit id */
251abb0f93cSkardel 	datum_pts->yearstart = 0;	/* initialize the yearstart to 0 */
252abb0f93cSkardel 	datum_pts->sigma2 = 0.0;	/* initialize the sigma2 to 0 */
253abb0f93cSkardel 
254abb0f93cSkardel 	datum_pts->PTS_fd = fd;
255abb0f93cSkardel 
2562950cc38Schristos 	if (-1 == fcntl(datum_pts->PTS_fd, F_SETFL, 0)) /* clear the descriptor flags */
2572950cc38Schristos 		msyslog(LOG_ERR, "MSF_ARCRON(%d): fcntl(F_SETFL, 0): %m.",
2582950cc38Schristos 			unit);
259abb0f93cSkardel 
260abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
261abb0f93cSkardel 	if (debug)
262abb0f93cSkardel 	    printf("Opening RS232 port with file descriptor %d\n",
263abb0f93cSkardel 		   datum_pts->PTS_fd);
264abb0f93cSkardel #endif
265abb0f93cSkardel 
266abb0f93cSkardel 	/*
267abb0f93cSkardel 	** Set up the RS232 terminal device information. Note that we assume that
268abb0f93cSkardel 	** we have termios. This code has only been tested on SUNs and HPs. If your
269abb0f93cSkardel 	** machine does not have termios this driver cannot be initialized. You can change this
270abb0f93cSkardel 	** if you want by editing this source. Please give the changes back to the
271abb0f93cSkardel 	** ntp folks so that it can become part of their regular distribution.
272abb0f93cSkardel 	*/
273abb0f93cSkardel 
274abb0f93cSkardel 	memset(&arg, 0, sizeof(arg));
275abb0f93cSkardel 
276abb0f93cSkardel 	arg.c_iflag = IGNBRK;
277abb0f93cSkardel 	arg.c_oflag = 0;
278abb0f93cSkardel 	arg.c_cflag = B9600 | CS8 | CREAD | PARENB | CLOCAL;
279abb0f93cSkardel 	arg.c_lflag = 0;
280abb0f93cSkardel 	arg.c_cc[VMIN] = 0;		/* start timeout timer right away (not used) */
281abb0f93cSkardel 	arg.c_cc[VTIME] = 30;		/* 3 second timout on reads (not used) */
282abb0f93cSkardel 
2832950cc38Schristos 	rc = tcsetattr(datum_pts->PTS_fd, TCSANOW, &arg);
2842950cc38Schristos 	if (rc < 0) {
2852950cc38Schristos 		msyslog(LOG_ERR, "Datum_PTS: tcsetattr(\"%s\") failed: %m", DATUM_DEV);
2862950cc38Schristos 		close(datum_pts->PTS_fd);
2872950cc38Schristos 		free(datum_pts);
288abb0f93cSkardel 		return 0;
2892950cc38Schristos 	}
290abb0f93cSkardel 
291abb0f93cSkardel 	/*
292abb0f93cSkardel 	** Initialize the ntpd IO structure
293abb0f93cSkardel 	*/
294abb0f93cSkardel 
295abb0f93cSkardel 	datum_pts->peer = peer;
2962950cc38Schristos 	pp = peer->procptr;
2972950cc38Schristos 	pp->io.clock_recv = datum_pts_receive;
2982950cc38Schristos 	pp->io.srcclock = peer;
2992950cc38Schristos 	pp->io.datalen = 0;
3002950cc38Schristos 	pp->io.fd = datum_pts->PTS_fd;
301abb0f93cSkardel 
3022950cc38Schristos 	if (!io_addclock(&pp->io)) {
3032950cc38Schristos 		pp->io.fd = -1;
304abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
305abb0f93cSkardel 		if (debug)
306abb0f93cSkardel 		    printf("Problem adding clock\n");
307abb0f93cSkardel #endif
308abb0f93cSkardel 
309abb0f93cSkardel 		msyslog(LOG_ERR, "Datum_PTS: Problem adding clock");
3102950cc38Schristos 		close(datum_pts->PTS_fd);
3112950cc38Schristos 		free(datum_pts);
312abb0f93cSkardel 
313abb0f93cSkardel 		return 0;
314abb0f93cSkardel 	}
3152950cc38Schristos 	peer->procptr->unitptr = datum_pts;
316abb0f93cSkardel 
317abb0f93cSkardel 	/*
318abb0f93cSkardel 	** Now add one to the number of units and return a successful code
319abb0f93cSkardel 	*/
320abb0f93cSkardel 
321abb0f93cSkardel 	nunits++;
322abb0f93cSkardel 	return 1;
323abb0f93cSkardel 
324abb0f93cSkardel }
325abb0f93cSkardel 
326abb0f93cSkardel 
327abb0f93cSkardel /*......................................................................*/
328abb0f93cSkardel /*	datum_pts_shutdown - this routine shuts doen the device and	*/
329abb0f93cSkardel /*	removes the memory for the unit.				*/
330abb0f93cSkardel /*......................................................................*/
331abb0f93cSkardel 
332abb0f93cSkardel static void
datum_pts_shutdown(int unit,struct peer * peer)333abb0f93cSkardel datum_pts_shutdown(
334abb0f93cSkardel 	int unit,
335abb0f93cSkardel 	struct peer *peer
336abb0f93cSkardel 	)
337abb0f93cSkardel {
3382950cc38Schristos 	struct refclockproc *pp;
3392950cc38Schristos 	struct datum_pts_unit *datum_pts;
340abb0f93cSkardel 
341abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
342abb0f93cSkardel 	if (debug)
343abb0f93cSkardel 	    printf("Shutdown Datum PTS\n");
344abb0f93cSkardel #endif
345abb0f93cSkardel 
346abb0f93cSkardel 	msyslog(LOG_ERR, "Datum_PTS: Shutdown Datum PTS");
347abb0f93cSkardel 
348abb0f93cSkardel 	/*
349abb0f93cSkardel 	** We found the unit so close the file descriptor and free up the memory used
350abb0f93cSkardel 	** by the structure.
351abb0f93cSkardel 	*/
3522950cc38Schristos 	pp = peer->procptr;
3532950cc38Schristos 	datum_pts = pp->unitptr;
3542950cc38Schristos 	if (NULL != datum_pts) {
3552950cc38Schristos 		io_closeclock(&pp->io);
3562950cc38Schristos 		free(datum_pts);
357abb0f93cSkardel 	}
358abb0f93cSkardel }
359abb0f93cSkardel 
360abb0f93cSkardel 
361abb0f93cSkardel /*......................................................................*/
362abb0f93cSkardel /*	datum_pts_poll - this routine sends out the time request to the */
363abb0f93cSkardel /*	Datum PTS device. The time will be passed back in the 		*/
364abb0f93cSkardel /*	datum_pts_receive() routine.					*/
365abb0f93cSkardel /*......................................................................*/
366abb0f93cSkardel 
367abb0f93cSkardel static void
datum_pts_poll(int unit,struct peer * peer)368abb0f93cSkardel datum_pts_poll(
369abb0f93cSkardel 	int unit,
370abb0f93cSkardel 	struct peer *peer
371abb0f93cSkardel 	)
372abb0f93cSkardel {
373abb0f93cSkardel 	int error_code;
374abb0f93cSkardel 	struct datum_pts_unit *datum_pts;
375abb0f93cSkardel 
3762950cc38Schristos 	datum_pts = peer->procptr->unitptr;
3772950cc38Schristos 
378abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
379abb0f93cSkardel 	if (debug)
380abb0f93cSkardel 	    printf("Poll Datum PTS\n");
381abb0f93cSkardel #endif
382abb0f93cSkardel 
383abb0f93cSkardel 	/*
384abb0f93cSkardel 	** Find the right unit and send out a time request once it is found.
385abb0f93cSkardel 	*/
386abb0f93cSkardel 	error_code = write(datum_pts->PTS_fd, TIME_REQUEST, 6);
3872950cc38Schristos 	if (error_code != 6)
3882950cc38Schristos 		perror("TIME_REQUEST");
389abb0f93cSkardel 	datum_pts->nbytes = 0;
390abb0f93cSkardel }
391abb0f93cSkardel 
392abb0f93cSkardel 
393abb0f93cSkardel /*......................................................................*/
394abb0f93cSkardel /*	datum_pts_control - not used					*/
395abb0f93cSkardel /*......................................................................*/
396abb0f93cSkardel 
397abb0f93cSkardel static void
datum_pts_control(int unit,const struct refclockstat * in,struct refclockstat * out,struct peer * peer)398abb0f93cSkardel datum_pts_control(
399abb0f93cSkardel 	int unit,
4002950cc38Schristos 	const struct refclockstat *in,
401abb0f93cSkardel 	struct refclockstat *out,
402abb0f93cSkardel 	struct peer *peer
403abb0f93cSkardel 	)
404abb0f93cSkardel {
405abb0f93cSkardel 
406abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
407abb0f93cSkardel 	if (debug)
408abb0f93cSkardel 	    printf("Control Datum PTS\n");
409abb0f93cSkardel #endif
410abb0f93cSkardel 
411abb0f93cSkardel }
412abb0f93cSkardel 
413abb0f93cSkardel 
414abb0f93cSkardel /*......................................................................*/
415abb0f93cSkardel /*	datum_pts_init - initializes things for all possible Datum	*/
416abb0f93cSkardel /*	time code generators that might be used. In practice, this is	*/
417abb0f93cSkardel /*	only called once at the beginning before anything else is	*/
418abb0f93cSkardel /*	called.								*/
419abb0f93cSkardel /*......................................................................*/
420abb0f93cSkardel 
421abb0f93cSkardel static void
datum_pts_init(void)422abb0f93cSkardel datum_pts_init(void)
423abb0f93cSkardel {
424abb0f93cSkardel 
425abb0f93cSkardel 	/*									*/
426abb0f93cSkardel 	/*...... open up the log file if we are debugging ......................*/
427abb0f93cSkardel 	/*									*/
428abb0f93cSkardel 
429abb0f93cSkardel 	/*
430abb0f93cSkardel 	** Open up the log file if we are debugging. For now, send data out to the
431abb0f93cSkardel 	** screen (stdout).
432abb0f93cSkardel 	*/
433abb0f93cSkardel 
434abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
435abb0f93cSkardel 	if (debug)
436abb0f93cSkardel 	    printf("Init Datum PTS\n");
437abb0f93cSkardel #endif
438abb0f93cSkardel 
439abb0f93cSkardel 	/*
440abb0f93cSkardel 	** Initialize the time request command string. This is the only message
441abb0f93cSkardel 	** that we ever have to send to the Datum PTS (although others are defined).
442abb0f93cSkardel 	*/
443abb0f93cSkardel 
444abb0f93cSkardel 	memcpy(TIME_REQUEST, "//k/mn",6);
445abb0f93cSkardel 
446abb0f93cSkardel 	/*
447abb0f93cSkardel 	** Initialize the number of units to 0 and set the dynamic array of units to
448abb0f93cSkardel 	** NULL since there are no units defined yet.
449abb0f93cSkardel 	*/
450abb0f93cSkardel 
451abb0f93cSkardel 	nunits = 0;
452abb0f93cSkardel 
453abb0f93cSkardel }
454abb0f93cSkardel 
455abb0f93cSkardel 
456abb0f93cSkardel /*......................................................................*/
457abb0f93cSkardel /*	datum_pts_buginfo - not used					*/
458abb0f93cSkardel /*......................................................................*/
459abb0f93cSkardel 
460abb0f93cSkardel static void
datum_pts_buginfo(int unit,register struct refclockbug * bug,register struct peer * peer)461abb0f93cSkardel datum_pts_buginfo(
462abb0f93cSkardel 	int unit,
463abb0f93cSkardel 	register struct refclockbug *bug,
464abb0f93cSkardel 	register struct peer *peer
465abb0f93cSkardel 	)
466abb0f93cSkardel {
467abb0f93cSkardel 
468abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
469abb0f93cSkardel 	if (debug)
470abb0f93cSkardel 	    printf("Buginfo Datum PTS\n");
471abb0f93cSkardel #endif
472abb0f93cSkardel 
473abb0f93cSkardel }
474abb0f93cSkardel 
475abb0f93cSkardel 
476abb0f93cSkardel /*......................................................................*/
477abb0f93cSkardel /*	datum_pts_receive - receive the time buffer that was read in	*/
478abb0f93cSkardel /*	by the ntpd io handling routines. When 7 bytes have been	*/
479abb0f93cSkardel /*	received (it may take several tries before all 7 bytes are	*/
480abb0f93cSkardel /*	received), then the time code must be unpacked and sent to	*/
481abb0f93cSkardel /*	the ntpd clock_receive() routine which causes the systems	*/
482abb0f93cSkardel /*	clock to be updated (several layers down).			*/
483abb0f93cSkardel /*......................................................................*/
484abb0f93cSkardel 
485abb0f93cSkardel static void
datum_pts_receive(struct recvbuf * rbufp)486abb0f93cSkardel datum_pts_receive(
487abb0f93cSkardel 	struct recvbuf *rbufp
488abb0f93cSkardel 	)
489abb0f93cSkardel {
490abb0f93cSkardel 	int i;
491ccc794f0Schristos 	size_t nb;
492abb0f93cSkardel 	l_fp tstmp;
4932950cc38Schristos 	struct peer *p;
494abb0f93cSkardel 	struct datum_pts_unit *datum_pts;
495abb0f93cSkardel 	char *dpt;
496abb0f93cSkardel 	int dpend;
497abb0f93cSkardel 	int tzoff;
498abb0f93cSkardel 	int timerr;
499abb0f93cSkardel 	double ftimerr, abserr;
500abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
501abb0f93cSkardel 	double dispersion;
502abb0f93cSkardel #endif
503abb0f93cSkardel 	int goodtime;
504abb0f93cSkardel       /*double doffset;*/
505abb0f93cSkardel 
506abb0f93cSkardel 	/*
507abb0f93cSkardel 	** Get the time code (maybe partial) message out of the rbufp buffer.
508abb0f93cSkardel 	*/
509abb0f93cSkardel 
5102950cc38Schristos 	p = rbufp->recv_peer;
5112950cc38Schristos 	datum_pts = p->procptr->unitptr;
512abb0f93cSkardel 	dpt = (char *)&rbufp->recv_space;
513abb0f93cSkardel 	dpend = rbufp->recv_length;
514abb0f93cSkardel 
515abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
516abb0f93cSkardel 	if (debug)
517abb0f93cSkardel 		printf("Receive Datum PTS: %d bytes\n", dpend);
518abb0f93cSkardel #endif
519abb0f93cSkardel 
520abb0f93cSkardel 	/*									*/
521abb0f93cSkardel 	/*...... save the ntp system time when the first byte is received ......*/
522abb0f93cSkardel 	/*									*/
523abb0f93cSkardel 
524abb0f93cSkardel 	/*
525abb0f93cSkardel 	** Save the ntp system time when the first byte is received. Note that
526abb0f93cSkardel 	** because it may take several calls to this routine before all seven
527abb0f93cSkardel 	** bytes of our return message are finally received by the io handlers in
528abb0f93cSkardel 	** ntpd, we really do want to use the time tag when the first byte is
529abb0f93cSkardel 	** received to reduce the jitter.
530abb0f93cSkardel 	*/
531abb0f93cSkardel 
532ccc794f0Schristos 	nb = datum_pts->nbytes;
533ccc794f0Schristos 	if (nb == 0) {
534abb0f93cSkardel 		datum_pts->lastrec = rbufp->recv_time;
535abb0f93cSkardel 	}
536abb0f93cSkardel 
537abb0f93cSkardel 	/*
538abb0f93cSkardel 	** Increment our count to the number of bytes received so far. Return if we
539abb0f93cSkardel 	** haven't gotten all seven bytes yet.
540ccc794f0Schristos 	** [Sec 3388] make sure we do not overrun the buffer.
541ccc794f0Schristos 	** TODO: what to do with excessive bytes, if we ever get them?
542abb0f93cSkardel 	*/
543ccc794f0Schristos 	for (i=0; (i < dpend) && (nb < sizeof(datum_pts->retbuf)); i++, nb++) {
544ccc794f0Schristos 		datum_pts->retbuf[nb] = dpt[i];
545abb0f93cSkardel 	}
546ccc794f0Schristos 	datum_pts->nbytes = nb;
547abb0f93cSkardel 
548ccc794f0Schristos 	if (nb < 7) {
549abb0f93cSkardel 		return;
550abb0f93cSkardel 	}
551abb0f93cSkardel 
552abb0f93cSkardel 	/*
553abb0f93cSkardel 	** Convert the seven bytes received in our time buffer to day, hour, minute,
554abb0f93cSkardel 	** second, and msecond values. The usec value is not used for anything
555abb0f93cSkardel 	** currently. It is just the fractional part of the time stored in units
556abb0f93cSkardel 	** of microseconds.
557abb0f93cSkardel 	*/
558abb0f93cSkardel 
559abb0f93cSkardel 	datum_pts->day =	100*(datum_pts->retbuf[0] & 0x0f) +
560abb0f93cSkardel 		10*((datum_pts->retbuf[1] & 0xf0)>>4) +
561abb0f93cSkardel 		(datum_pts->retbuf[1] & 0x0f);
562abb0f93cSkardel 
563abb0f93cSkardel 	datum_pts->hour =	10*((datum_pts->retbuf[2] & 0x30)>>4) +
564abb0f93cSkardel 		(datum_pts->retbuf[2] & 0x0f);
565abb0f93cSkardel 
566abb0f93cSkardel 	datum_pts->minute =	10*((datum_pts->retbuf[3] & 0x70)>>4) +
567abb0f93cSkardel 		(datum_pts->retbuf[3] & 0x0f);
568abb0f93cSkardel 
569abb0f93cSkardel 	datum_pts->second =	10*((datum_pts->retbuf[4] & 0x70)>>4) +
570abb0f93cSkardel 		(datum_pts->retbuf[4] & 0x0f);
571abb0f93cSkardel 
572abb0f93cSkardel 	datum_pts->msec =	100*((datum_pts->retbuf[5] & 0xf0) >> 4) +
573abb0f93cSkardel 		10*(datum_pts->retbuf[5] & 0x0f) +
574abb0f93cSkardel 		((datum_pts->retbuf[6] & 0xf0)>>4);
575abb0f93cSkardel 
576abb0f93cSkardel 	datum_pts->usec =	1000*datum_pts->msec;
577abb0f93cSkardel 
578abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
579abb0f93cSkardel 	if (debug)
580abb0f93cSkardel 	    printf("day %d, hour %d, minute %d, second %d, msec %d\n",
581abb0f93cSkardel 		   datum_pts->day,
582abb0f93cSkardel 		   datum_pts->hour,
583abb0f93cSkardel 		   datum_pts->minute,
584abb0f93cSkardel 		   datum_pts->second,
585abb0f93cSkardel 		   datum_pts->msec);
586abb0f93cSkardel #endif
587abb0f93cSkardel 
588abb0f93cSkardel 	/*
589abb0f93cSkardel 	** Get the GMT time zone offset. Note that GMT should be zero if the Datum
590abb0f93cSkardel 	** reference time is using GMT as its time base. Otherwise we have to
591abb0f93cSkardel 	** determine the offset if the Datum PTS is using time of day as its time
592abb0f93cSkardel 	** base.
593abb0f93cSkardel 	*/
594abb0f93cSkardel 
595abb0f93cSkardel 	goodtime = 0;		/* We are not sure about the time and offset yet */
596abb0f93cSkardel 
597abb0f93cSkardel #ifdef GMT
598abb0f93cSkardel 
599abb0f93cSkardel 	/*
600abb0f93cSkardel 	** This is the case where the Datum PTS is using GMT so there is no time
601abb0f93cSkardel 	** zone offset.
602abb0f93cSkardel 	*/
603abb0f93cSkardel 
604abb0f93cSkardel 	tzoff = 0;		/* set time zone offset to 0 */
605abb0f93cSkardel 
606abb0f93cSkardel #else
607abb0f93cSkardel 
608abb0f93cSkardel 	/*
609abb0f93cSkardel 	** This is the case where the Datum PTS is using regular time of day for its
610abb0f93cSkardel 	** time so we must compute the time zone offset. The way we do it is kind of
611abb0f93cSkardel 	** funny but it works. We loop through different time zones (0 to 24) and
612abb0f93cSkardel 	** pick the one that gives the smallest error (+- one half hour). The time
613abb0f93cSkardel 	** zone offset is stored in the datum_pts structure for future use. Normally,
614abb0f93cSkardel 	** the clocktime() routine is only called once (unless the time zone offset
615abb0f93cSkardel 	** changes due to daylight savings) since the goodtime flag is set when a
616abb0f93cSkardel 	** good time is found (with a good offset). Note that even if the Datum
617abb0f93cSkardel 	** PTS is using GMT, this mechanism will still work since it should come up
618abb0f93cSkardel 	** with a value for tzoff = 0 (assuming that your system clock is within
619abb0f93cSkardel 	** a half hour of the Datum time (even with time zone differences).
620abb0f93cSkardel 	*/
621abb0f93cSkardel 
622abb0f93cSkardel 	for (tzoff=0; tzoff<24; tzoff++) {
623abb0f93cSkardel 		if (clocktime( datum_pts->day,
624abb0f93cSkardel 			       datum_pts->hour,
625abb0f93cSkardel 			       datum_pts->minute,
626abb0f93cSkardel 			       datum_pts->second,
627abb0f93cSkardel 			       (tzoff + datum_pts->tzoff) % 24,
628abb0f93cSkardel 			       datum_pts->lastrec.l_ui,
629abb0f93cSkardel 			       &datum_pts->yearstart,
630abb0f93cSkardel 			       &datum_pts->lastref.l_ui) ) {
631abb0f93cSkardel 
632abb0f93cSkardel 			datum_pts->lastref.l_uf = 0;
633abb0f93cSkardel 			error = datum_pts->lastref.l_ui - datum_pts->lastrec.l_ui;
634abb0f93cSkardel 
635abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
636abb0f93cSkardel 			printf("Time Zone (clocktime method) = %d, error = %d\n", tzoff, error);
637abb0f93cSkardel #endif
638abb0f93cSkardel 
639abb0f93cSkardel 			if ((error < 1799) && (error > -1799)) {
640abb0f93cSkardel 				tzoff = (tzoff + datum_pts->tzoff) % 24;
641abb0f93cSkardel 				datum_pts->tzoff = tzoff;
642abb0f93cSkardel 				goodtime = 1;
643abb0f93cSkardel 
644abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
645abb0f93cSkardel 				printf("Time Zone found (clocktime method) = %d\n",tzoff);
646abb0f93cSkardel #endif
647abb0f93cSkardel 
648abb0f93cSkardel 				break;
649abb0f93cSkardel 			}
650abb0f93cSkardel 
651abb0f93cSkardel 		}
652abb0f93cSkardel 	}
653abb0f93cSkardel 
654abb0f93cSkardel #endif
655abb0f93cSkardel 
656abb0f93cSkardel 	/*
657abb0f93cSkardel 	** Make sure that we have a good time from the Datum PTS. Clocktime() also
658abb0f93cSkardel 	** sets yearstart and lastref.l_ui. We will have to set astref.l_uf (i.e.,
659abb0f93cSkardel 	** the fraction of a second) stuff later.
660abb0f93cSkardel 	*/
661abb0f93cSkardel 
662abb0f93cSkardel 	if (!goodtime) {
663abb0f93cSkardel 
664abb0f93cSkardel 		if (!clocktime( datum_pts->day,
665abb0f93cSkardel 				datum_pts->hour,
666abb0f93cSkardel 				datum_pts->minute,
667abb0f93cSkardel 				datum_pts->second,
668abb0f93cSkardel 				tzoff,
669abb0f93cSkardel 				datum_pts->lastrec.l_ui,
670abb0f93cSkardel 				&datum_pts->yearstart,
671abb0f93cSkardel 				&datum_pts->lastref.l_ui) ) {
672abb0f93cSkardel 
673abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
674abb0f93cSkardel 			if (debug)
675abb0f93cSkardel 			{
676abb0f93cSkardel 				printf("Error: bad clocktime\n");
677abb0f93cSkardel 				printf("GMT %d, lastrec %d, yearstart %d, lastref %d\n",
678abb0f93cSkardel 				       tzoff,
679abb0f93cSkardel 				       datum_pts->lastrec.l_ui,
680abb0f93cSkardel 				       datum_pts->yearstart,
681abb0f93cSkardel 				       datum_pts->lastref.l_ui);
682abb0f93cSkardel 			}
683abb0f93cSkardel #endif
684abb0f93cSkardel 
685abb0f93cSkardel 			msyslog(LOG_ERR, "Datum_PTS: Bad clocktime");
686abb0f93cSkardel 
687abb0f93cSkardel 			return;
688abb0f93cSkardel 
689abb0f93cSkardel 		}else{
690abb0f93cSkardel 
691abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
692abb0f93cSkardel 			if (debug)
693abb0f93cSkardel 			    printf("Good clocktime\n");
694abb0f93cSkardel #endif
695abb0f93cSkardel 
696abb0f93cSkardel 		}
697abb0f93cSkardel 
698abb0f93cSkardel 	}
699abb0f93cSkardel 
700abb0f93cSkardel 	/*
701abb0f93cSkardel 	** We have datum_pts->lastref.l_ui set (which is the integer part of the
702abb0f93cSkardel 	** time. Now set the microseconds field.
703abb0f93cSkardel 	*/
704abb0f93cSkardel 
705abb0f93cSkardel 	TVUTOTSF(datum_pts->usec, datum_pts->lastref.l_uf);
706abb0f93cSkardel 
707abb0f93cSkardel 	/*
708abb0f93cSkardel 	** Compute the time correction as the difference between the reference
709abb0f93cSkardel 	** time (i.e., the Datum time) minus the receive time (system time).
710abb0f93cSkardel 	*/
711abb0f93cSkardel 
712abb0f93cSkardel 	tstmp = datum_pts->lastref;		/* tstmp is the datum ntp time */
713abb0f93cSkardel 	L_SUB(&tstmp, &datum_pts->lastrec);	/* tstmp is now the correction */
714abb0f93cSkardel 	datum_pts->coderecv++;		/* increment a counter */
715abb0f93cSkardel 
716abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
717abb0f93cSkardel 	dispersion = DATUM_DISPERSION;	/* set the dispersion to 0 */
718abb0f93cSkardel 	ftimerr = dispersion;
719abb0f93cSkardel 	ftimerr /= (1024.0 * 64.0);
720abb0f93cSkardel 	if (debug)
721abb0f93cSkardel 	    printf("dispersion = %d, %f\n", dispersion, ftimerr);
722abb0f93cSkardel #endif
723abb0f93cSkardel 
724abb0f93cSkardel 	/*
725abb0f93cSkardel 	** Pass the new time to ntpd through the refclock_receive function. Note
726abb0f93cSkardel 	** that we are not trying to make any corrections due to the time it takes
727abb0f93cSkardel 	** for the Datum PTS to send the message back. I am (erroneously) assuming
728abb0f93cSkardel 	** that the time for the Datum PTS to send the time back to us is negligable.
729abb0f93cSkardel 	** I suspect that this time delay may be as much as 15 ms or so (but probably
730abb0f93cSkardel 	** less). For our needs at JPL, this kind of error is ok so it is not
731abb0f93cSkardel 	** necessary to use fudge factors in the ntp.conf file. Maybe later we will.
732abb0f93cSkardel 	*/
733abb0f93cSkardel       /*LFPTOD(&tstmp, doffset);*/
734abb0f93cSkardel 	datum_pts->lastref = datum_pts->lastrec;
735abb0f93cSkardel 	refclock_receive(datum_pts->peer);
736abb0f93cSkardel 
737abb0f93cSkardel 	/*
738abb0f93cSkardel 	** Compute sigma squared (not used currently). Maybe later, this could be
739abb0f93cSkardel 	** used for the dispersion estimate. The problem is that ntpd does not link
740abb0f93cSkardel 	** in the math library so sqrt() is not available. Anyway, this is useful
741abb0f93cSkardel 	** for debugging. Maybe later I will just use absolute values for the time
742abb0f93cSkardel 	** error to come up with my dispersion estimate. Anyway, for now my dispersion
743abb0f93cSkardel 	** is set to 0.
744abb0f93cSkardel 	*/
745abb0f93cSkardel 
746abb0f93cSkardel 	timerr = tstmp.l_ui<<20;
747abb0f93cSkardel 	timerr |= (tstmp.l_uf>>12) & 0x000fffff;
748abb0f93cSkardel 	ftimerr = timerr;
749abb0f93cSkardel 	ftimerr /= 1024*1024;
750abb0f93cSkardel 	abserr = ftimerr;
751abb0f93cSkardel 	if (ftimerr < 0.0) abserr = -ftimerr;
752abb0f93cSkardel 
753abb0f93cSkardel 	if (datum_pts->sigma2 == 0.0) {
754abb0f93cSkardel 		if (abserr < DATUM_MAX_ERROR) {
755abb0f93cSkardel 			datum_pts->sigma2 = abserr*abserr;
756abb0f93cSkardel 		}else{
757abb0f93cSkardel 			datum_pts->sigma2 = DATUM_MAX_ERROR2;
758abb0f93cSkardel 		}
759abb0f93cSkardel 	}else{
760abb0f93cSkardel 		if (abserr < DATUM_MAX_ERROR) {
761abb0f93cSkardel 			datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*abserr*abserr;
762abb0f93cSkardel 		}else{
763abb0f93cSkardel 			datum_pts->sigma2 = 0.95*datum_pts->sigma2 + 0.05*DATUM_MAX_ERROR2;
764abb0f93cSkardel 		}
765abb0f93cSkardel 	}
766abb0f93cSkardel 
767abb0f93cSkardel #ifdef DEBUG_DATUM_PTC
768abb0f93cSkardel 	if (debug)
769abb0f93cSkardel 	    printf("Time error = %f seconds\n", ftimerr);
770abb0f93cSkardel #endif
771abb0f93cSkardel 
772abb0f93cSkardel #if defined(DEBUG_DATUM_PTC) || defined(LOG_TIME_ERRORS)
773abb0f93cSkardel 	if (debug)
774abb0f93cSkardel 	    printf("PTS: day %d, hour %d, minute %d, second %d, msec %d, Time Error %f\n",
775abb0f93cSkardel 		   datum_pts->day,
776abb0f93cSkardel 		   datum_pts->hour,
777abb0f93cSkardel 		   datum_pts->minute,
778abb0f93cSkardel 		   datum_pts->second,
779abb0f93cSkardel 		   datum_pts->msec,
780abb0f93cSkardel 		   ftimerr);
781abb0f93cSkardel #endif
782abb0f93cSkardel 
783abb0f93cSkardel }
784abb0f93cSkardel #else
7852950cc38Schristos NONEMPTY_TRANSLATION_UNIT
786abb0f93cSkardel #endif /* REFCLOCK */
787