xref: /freebsd/contrib/ntp/ntpd/refclock_msfees.c (revision 5129159789cc9d7bc514e4546b88e3427695002d)
1 /* refclock_ees - clock driver for the EES M201 receiver */
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #if defined(REFCLOCK) && defined(CLOCK_MSFEES) && defined(PPS)
8 
9 /* Currently REQUIRES STREAM and PPSCD. CLK and CBREAK modes
10  * were removed as the code was overly hairy, they weren't in use
11  * (hence probably didn't work).  Still in RCS file at cl.cam.ac.uk
12  */
13 
14 #include <ctype.h>
15 #ifdef HAVE_SYS_TIME_H
16 # include <sys/time.h>
17 #endif
18 
19 #include "ntpd.h"
20 #include "ntp_io.h"
21 #include "ntp_refclock.h"
22 #include "ntp_unixtime.h"
23 #include "ntp_calendar.h"
24 #if defined(HAVE_BSD_TTYS)
25 #include <sgtty.h>
26 #endif /* HAVE_BSD_TTYS */
27 #if defined(HAVE_SYSV_TTYS)
28 #include <termio.h>
29 #endif /* HAVE_SYSV_TTYS */
30 #if defined(HAVE_TERMIOS)
31 #include <termios.h>
32 #endif
33 #if defined(STREAM)
34 #include <stropts.h>
35 #endif
36 
37 #ifdef HAVE_SYS_TERMIOS_H
38 # include <sys/termios.h>
39 #endif
40 #ifdef HAVE_SYS_PPSCLOCK_H
41 # include <sys/ppsclock.h>
42 #endif
43 
44 #include "ntp_stdlib.h"
45 
46 /*
47 	fudgefactor	= fudgetime1;
48 	os_delay	= fudgetime2;
49 	   offset_fudge	= os_delay + fudgefactor + inherent_delay;
50 	stratumtouse	= fudgeval1 & 0xf
51 	debug		= fudgeval2;
52 	sloppyclockflag	= flags & CLK_FLAG1;
53 		1	  log smoothing summary when processing sample
54 		4	  dump the buffer from the clock
55 		8	  EIOGETKD the last n uS time stamps
56 	if (flags & CLK_FLAG2 && unitinuse) ees->leaphold = 0;
57 	ees->dump_vals	= flags & CLK_FLAG3;
58 	ees->usealldata	= flags & CLK_FLAG4;
59 
60 
61 	bug->values[0] = (ees->lasttime) ? current_time - ees->lasttime : 0;
62 	bug->values[1] = (ees->clocklastgood)?current_time-ees->clocklastgood:0;
63 	bug->values[2] = (u_long)ees->status;
64 	bug->values[3] = (u_long)ees->lastevent;
65 	bug->values[4] = (u_long)ees->reason;
66 	bug->values[5] = (u_long)ees->nsamples;
67 	bug->values[6] = (u_long)ees->codestate;
68 	bug->values[7] = (u_long)ees->day;
69 	bug->values[8] = (u_long)ees->hour;
70 	bug->values[9] = (u_long)ees->minute;
71 	bug->values[10] = (u_long)ees->second;
72 	bug->values[11] = (u_long)ees->tz;
73 	bug->values[12] = ees->yearstart;
74 	bug->values[13] = (ees->leaphold > current_time) ?
75 				ees->leaphold - current_time : 0;
76 	bug->values[14] = inherent_delay[unit].l_uf;
77 	bug->values[15] = offset_fudge[unit].l_uf;
78 
79 	bug->times[0] = ees->reftime;
80 	bug->times[1] = ees->arrvtime;
81 	bug->times[2] = ees->lastsampletime;
82 	bug->times[3] = ees->offset;
83 	bug->times[4] = ees->lowoffset;
84 	bug->times[5] = ees->highoffset;
85 	bug->times[6] = inherent_delay[unit];
86 	bug->times[8] = os_delay[unit];
87 	bug->times[7] = fudgefactor[unit];
88 	bug->times[9] = offset_fudge[unit];
89 	bug->times[10]= ees->yearstart, 0;
90 	*/
91 
92 /* This should support the use of an EES M201 receiver with RS232
93  * output (modified to transmit time once per second).
94  *
95  * For the format of the message sent by the clock, see the EESM_
96  * definitions below.
97  *
98  * It appears to run free for an integral number of minutes, until the error
99  * reaches 4mS, at which point it steps at second = 01.
100  * It appears that sometimes it steps 4mS (say at 7 min interval),
101  * then the next minute it decides that it was an error, so steps back.
102  * On the next minute it steps forward again :-(
103  * This is typically 16.5uS/S then 3975uS at the 4min re-sync,
104  * or 9.5uS/S then 3990.5uS at a 7min re-sync,
105  * at which point it may loose the "00" second time stamp.
106  * I assume that the most accurate time is just AFTER the re-sync.
107  * Hence remember the last cycle interval,
108  *
109  * Can run in any one of:
110  *
111  *	PPSCD	PPS signal sets CD which interupts, and grabs the current TOD
112  *	(sun)		*in the interupt code*, so as to avoid problems with
113  *			the STREAMS scheduling.
114  *
115  * It appears that it goes 16.5 uS slow each second, then every 4 mins it
116  * generates no "00" second tick, and gains 3975 uS. Ho Hum ! (93/2/7)
117  */
118 
119 /* Definitions */
120 #ifndef	MAXUNITS
121 #define	MAXUNITS	4	/* maximum number of EES units permitted */
122 #endif
123 
124 #ifndef	EES232
125 #define	EES232	"/dev/ees%d"	/* Device to open to read the data */
126 #endif
127 
128 /* Other constant stuff */
129 #ifndef	EESPRECISION
130 #define	EESPRECISION	(-10)		/* what the heck - 2**-10 = 1ms */
131 #endif
132 #ifndef	EESREFID
133 #define	EESREFID	"MSF\0"		/* String to identify the clock */
134 #endif
135 #ifndef	EESHSREFID
136 #define	EESHSREFID	(0x7f7f0000 | ((REFCLK_MSF_EES) << 8)) /* Numeric refid */
137 #endif
138 
139 /* Description of clock */
140 #define	EESDESCRIPTION		"EES M201 MSF Receiver"
141 
142 /* Speed we run the clock port at. If this is changed the UARTDELAY
143  * value should be recomputed to suit.
144  */
145 #ifndef	SPEED232
146 #define	SPEED232	B9600	/* 9600 baud */
147 #endif
148 
149 /* What is the inherent delay for this mode of working, i.e. when is the
150  * data time stamped.
151  */
152 #define	SAFETY_SHIFT	10	/* Split the shift to avoid overflow */
153 #define	BITS_TO_L_FP(bits, baud) \
154 (((((bits)*2 +1) << (FRACTION_PREC-SAFETY_SHIFT)) / (2*baud)) << SAFETY_SHIFT)
155 #define	INH_DELAY_CBREAK	BITS_TO_L_FP(119, 9600)
156 #define	INH_DELAY_PPS		BITS_TO_L_FP(  0, 9600)
157 
158 #ifndef	STREAM_PP1
159 #define	STREAM_PP1	"ppsclocd\0<-- patch space for module name1 -->"
160 #endif
161 #ifndef	STREAM_PP2
162 #define	STREAM_PP2	"ppsclock\0<-- patch space for module name2 -->"
163 #endif
164 
165      /* Offsets of the bytes of the serial line code.  The clock gives
166  * local time with a GMT/BST indication. The EESM_ definitions
167  * give offsets into ees->lastcode.
168  */
169 #define EESM_CSEC	 0	/* centiseconds - always zero in our clock  */
170 #define EESM_SEC	 1	/* seconds in BCD			    */
171 #define EESM_MIN	 2	/* minutes in BCD			    */
172 #define EESM_HOUR	 3	/* hours in BCD				    */
173 #define EESM_DAYWK	 4	/* day of week (Sun = 0 etc)		    */
174 #define EESM_DAY	 5	/* day of month in BCD			    */
175 #define EESM_MON	 6	/* month in BCD				    */
176 #define EESM_YEAR	 7	/* year MOD 100 in BCD			    */
177 #define EESM_LEAP	 8	/* 0x0f if leap year, otherwise zero        */
178 #define EESM_BST	 9	/* 0x03 if BST, 0x00 if GMT		    */
179 #define EESM_MSFOK	10	/* 0x3f if radio good, otherwise zero	    */
180 				/* followed by a frame alignment byte (0xff) /
181 				/  which is not put into the lastcode buffer*/
182 
183 /* Length of the serial time code, in characters.  The first length
184  * is less the frame alignment byte.
185  */
186 #define	LENEESPRT	(EESM_MSFOK+1)
187 #define	LENEESCODE	(LENEESPRT+1)
188 
189      /* Code state. */
190 #define	EESCS_WAIT	0       /* waiting for start of timecode */
191 #define	EESCS_GOTSOME	1	/* have an incomplete time code buffered */
192 
193      /* Default fudge factor and character to receive */
194 #define	DEFFUDGETIME	0	/* Default user supplied fudge factor */
195 #ifndef	DEFOSTIME
196 #define	DEFOSTIME	0	/* Default OS delay -- passed by Make ? */
197 #endif
198 #define	DEFINHTIME	INH_DELAY_PPS /* inherent delay due to sample point*/
199 
200      /* Limits on things.  Reduce the number of samples to SAMPLEREDUCE by median
201  * elimination.  If we're running with an accurate clock, chose the BESTSAMPLE
202  * as the estimated offset, otherwise average the remainder.
203  */
204 #define	FULLSHIFT	6			/* NCODES root 2 */
205 #define NCODES		(1<< FULLSHIFT)		/* 64 */
206 #define	REDUCESHIFT	(FULLSHIFT -1)		/* SAMPLEREDUCE root 2 */
207 
208      /* Towards the high ( Why ?) end of half */
209 #define	BESTSAMPLE	((samplereduce * 3) /4)	/* 24 */
210 
211      /* Leap hold time.  After a leap second the clock will no longer be
212  * reliable until it resynchronizes.  Hope 40 minutes is enough. */
213 #define	EESLEAPHOLD	(40 * 60)
214 
215 #define	EES_STEP_F	(1 << 24) /* the receiver steps in units of about 4ms */
216 #define	EES_STEP_F_GRACE (EES_STEP_F/8) /*Allow for slop of 1/8 which is .5ms*/
217 #define	EES_STEP_NOTE	(1 << 21)/* Log any unexpected jumps, say .5 ms .... */
218 #define	EES_STEP_NOTES	50	/* Only do a limited number */
219 #define	MAX_STEP	16	/* Max number of steps to remember */
220 
221      /* debug is a bit mask of debugging that is wanted */
222 #define	DB_SYSLOG_SMPLI		0x0001
223 #define	DB_SYSLOG_SMPLE		0x0002
224 #define	DB_SYSLOG_SMTHI		0x0004
225 #define	DB_SYSLOG_NSMTHE	0x0008
226 #define	DB_SYSLOG_NSMTHI	0x0010
227 #define	DB_SYSLOG_SMTHE		0x0020
228 #define	DB_PRINT_EV		0x0040
229 #define	DB_PRINT_CDT		0x0080
230 #define	DB_PRINT_CDTC		0x0100
231 #define	DB_SYSLOG_KEEPD		0x0800
232 #define	DB_SYSLOG_KEEPE		0x1000
233 #define	DB_LOG_DELTAS		0x2000
234 #define	DB_PRINT_DELTAS		0x4000
235 #define	DB_LOG_AWAITMORE	0x8000
236 #define	DB_LOG_SAMPLES		0x10000
237 #define	DB_NO_PPS		0x20000
238 #define	DB_INC_PPS		0x40000
239 #define	DB_DUMP_DELTAS		0x80000
240 
241      struct eesunit {			/* EES unit control structure. */
242 	     struct peer *peer;		/* associated peer structure */
243 	     struct refclockio io;		/* given to the I/O handler */
244 	     l_fp	reftime;		/* reference time */
245 	     l_fp	lastsampletime;		/* time as in txt from last EES msg */
246 	     l_fp	arrvtime;		/* Time at which pkt arrived */
247 	     l_fp	codeoffsets[NCODES];	/* the time of arrival of 232 codes */
248 	     l_fp	offset;			/* chosen offset        (for clkbug) */
249 	     l_fp	lowoffset;		/* lowest sample offset (for clkbug) */
250 	     l_fp	highoffset;		/* highest   "     "    (for clkbug) */
251 	     char	lastcode[LENEESCODE+6];	/* last time code we received */
252 	     u_long	lasttime;		/* last time clock heard from */
253 	     u_long	clocklastgood;		/* last time good radio seen */
254 	     u_char	lencode;		/* length of code in buffer */
255 	     u_char	nsamples;		/* number of samples we've collected */
256 	     u_char	codestate;		/* state of 232 code reception */
257 	     u_char	unit;			/* unit number for this guy */
258 	     u_char	status;			/* clock status */
259 	     u_char	lastevent;		/* last clock event */
260 	     u_char	reason;			/* reason for last abort */
261 	     u_char	hour;			/* hour of day */
262 	     u_char	minute;			/* minute of hour */
263 	     u_char	second;			/* seconds of minute */
264 	     char	tz;			/* timezone from clock */
265 	     u_char	ttytype;		/* method used */
266 	     u_char	dump_vals;		/* Should clock values be dumped */
267 	     u_char	usealldata;		/* Use ALL samples */
268 	     u_short	day;			/* day of year from last code */
269 	     u_long	yearstart;		/* start of current year */
270 	     u_long	leaphold;		/* time of leap hold expiry */
271 	     u_long	badformat;		/* number of bad format codes */
272 	     u_long	baddata;		/* number of invalid time codes */
273 	     u_long	timestarted;		/* time we started this */
274 	     long	last_pps_no;		/* The serial # of the last PPS */
275 	     char	fix_pending;		/* Is a "sync to time" pending ? */
276 	     /* Fine tuning - compensate for 4 mS ramping .... */
277 	     l_fp	last_l;			/* last time stamp */
278 	     u_char	last_steps[MAX_STEP];	/* Most recent n steps */
279 	     int	best_av_step;		/* Best guess at average step */
280 	     char	best_av_step_count;	/* # of steps over used above */
281 	     char	this_step;		/* Current pos in buffer */
282 	     int	last_step_late;		/* How late the last step was (0-59) */
283 	     long	jump_fsecs;		/* # of fractions of a sec last jump */
284 	     u_long	last_step;		/* time of last step */
285 	     int	last_step_secs;		/* Number of seconds in last step */
286 	     int	using_ramp;		/* 1 -> noemal, -1 -> over stepped */
287      };
288 #define	last_sec	last_l.l_ui
289 #define	last_sfsec	last_l.l_f
290 #define	this_uisec	((ees->arrvtime).l_ui)
291 #define	this_sfsec	((ees->arrvtime).l_f)
292 #define	msec(x)		((x) / (1<<22))
293 #define	LAST_STEPS	(sizeof ees->last_steps / sizeof ees->last_steps[0])
294 #define	subms(x)	((((((x < 0) ? (-(x)) : (x)) % (1<<22))/2) * 625) / (1<<(22 -5)))
295 
296 /* Bitmask for what methods to try to use -- currently only PPS enabled */
297 #define	T_CBREAK	1
298 #define	T_PPS		8
299 /* macros to test above */
300 #define	is_cbreak(x)	((x)->ttytype & T_CBREAK)
301 #define	is_pps(x)	((x)->ttytype & T_PPS)
302 #define	is_any(x)	((x)->ttytype)
303 
304 #define	CODEREASON	20	/* reason codes */
305 
306 /* Data space for the unit structures.  Note that we allocate these on
307  * the fly, but never give them back. */
308 static struct eesunit *eesunits[MAXUNITS];
309 static u_char unitinuse[MAXUNITS];
310 
311 /* Keep the fudge factors separately so they can be set even
312  * when no clock is configured. */
313 static l_fp inherent_delay[MAXUNITS];		/* when time stamp is taken */
314 static l_fp fudgefactor[MAXUNITS];		/* fudgetime1 */
315 static l_fp os_delay[MAXUNITS];			/* fudgetime2 */
316 static l_fp offset_fudge[MAXUNITS];		/* Sum of above */
317 static u_char stratumtouse[MAXUNITS];
318 static u_char sloppyclockflag[MAXUNITS];
319 
320 static int deltas[60];
321 
322 static l_fp acceptable_slop; /* = { 0, 1 << (FRACTION_PREC -2) }; */
323 static l_fp onesec; /* = { 1, 0 }; */
324 
325 #ifdef	DEBUG
326 static int debug;
327 #endif
328 
329 #ifndef	DUMP_BUF_SIZE	/* Size of buffer to be used by dump_buf */
330 #define	DUMP_BUF_SIZE	10112
331 #endif
332 
333 /* ees_reset - reset the count back to zero */
334 #define	ees_reset(ees) (ees)->nsamples = 0; \
335 (ees)->codestate = EESCS_WAIT
336 
337 /* ees_event - record and report an event */
338 #define	ees_event(ees, evcode) if ((ees)->status != (u_char)(evcode)) \
339 ees_report_event((ees), (evcode))
340 
341      /* Find the precision of the system clock by reading it */
342 #define	USECS	1000000
343 #define	MINSTEP	5	/* some systems increment uS on each call */
344 #define	MAXLOOPS (USECS/9)
345 
346 /*
347  * Function prototypes
348  */
349 
350 static	int	msfees_start	P((int unit, struct peer *peer));
351 static	void	msfees_shutdown	P((int unit, struct peer *peer));
352 static	void	msfees_poll	P((int unit, struct peer *peer));
353 static	void	msfees_init	P((void));
354 static	void	dump_buf	P((l_fp *coffs, int from, int to, char *text));
355 static	void	ees_report_event P((struct eesunit *ees, int code));
356 static	void	ees_receive	P((struct recvbuf *rbufp));
357 static	int	offcompare	P((l_fp *a, l_fp *b));
358 static	void	ees_process	P((struct eesunit *ees));
359 
360 /*
361  * Transfer vector
362  */
363 struct	refclock refclock_msfees = {
364 	msfees_start,		/* start up driver */
365 	msfees_shutdown,	/* shut down driver */
366 	msfees_poll,		/* transmit poll message */
367 	noentry,		/* not used */
368 	msfees_init,		/* initialize driver */
369 	noentry,		/* not used */
370 	NOFLAGS			/* not used */
371 };
372 
373 
374 static void
375 dump_buf(
376 	l_fp *coffs,
377 	int from,
378 	int to,
379 	char *text
380 	)
381 {
382 	char buff[DUMP_BUF_SIZE + 80];
383 	int i;
384 	register char *ptr = buff;
385 
386 	sprintf(ptr, text);
387 	for (i=from; i<to; i++)
388 	{	while (*ptr) ptr++;
389 	if ((ptr-buff) > DUMP_BUF_SIZE) msyslog(LOG_DEBUG, "D: %s", ptr=buff);
390 	sprintf(ptr, " %06d", ((int)coffs[i].l_f) / 4295);
391 	}
392 	msyslog(LOG_DEBUG, "D: %s", buff);
393 }
394 
395 /* msfees_init - initialize internal ees driver data */
396 static void
397 msfees_init(void)
398 {
399 	register int i;
400 	/* Just zero the data arrays */
401 	memset((char *)eesunits, 0, sizeof eesunits);
402 	memset((char *)unitinuse, 0, sizeof unitinuse);
403 
404 	acceptable_slop.l_ui = 0;
405 	acceptable_slop.l_uf = 1 << (FRACTION_PREC -2);
406 
407 	onesec.l_ui = 1;
408 	onesec.l_uf = 0;
409 
410 	/* Initialize fudge factors to default. */
411 	for (i = 0; i < MAXUNITS; i++) {
412 		fudgefactor[i].l_ui	= 0;
413 		fudgefactor[i].l_uf	= DEFFUDGETIME;
414 		os_delay[i].l_ui	= 0;
415 		os_delay[i].l_uf	= DEFOSTIME;
416 		inherent_delay[i].l_ui	= 0;
417 		inherent_delay[i].l_uf	= DEFINHTIME;
418 		offset_fudge[i]		= os_delay[i];
419 		L_ADD(&offset_fudge[i], &fudgefactor[i]);
420 		L_ADD(&offset_fudge[i], &inherent_delay[i]);
421 		stratumtouse[i]		= 0;
422 		sloppyclockflag[i]	= 0;
423 	}
424 }
425 
426 
427 /* msfees_start - open the EES devices and initialize data for processing */
428 static int
429 msfees_start(
430 	int unit,
431 	struct peer *peer
432 	)
433 {
434 	register struct eesunit *ees;
435 	register int i;
436 	int fd232 = -1;
437 	char eesdev[20];
438 	struct termios ttyb, *ttyp;
439 	struct refclockproc *pp;
440 	pp = peer->procptr;
441 
442 	if (unit >= MAXUNITS) {
443 		msyslog(LOG_ERR, "ees clock: unit number %d invalid (max %d)",
444 			unit, MAXUNITS-1);
445 		return 0;
446 	}
447 	if (unitinuse[unit]) {
448 		msyslog(LOG_ERR, "ees clock: unit number %d in use", unit);
449 		return 0;
450 	}
451 
452 	/* Unit okay, attempt to open the devices.  We do them both at
453 	 * once to make sure we can */
454 	(void) sprintf(eesdev, EES232, unit);
455 
456 	fd232 = open(eesdev, O_RDWR, 0777);
457 	if (fd232 == -1) {
458 		msyslog(LOG_ERR, "ees clock: open of %s failed: %m", eesdev);
459 		return 0;
460 	}
461 
462 #ifdef	TIOCEXCL
463 	/* Set for exclusive use */
464 	if (ioctl(fd232, TIOCEXCL, (char *)0) < 0) {
465 		msyslog(LOG_ERR, "ees clock: ioctl(%s, TIOCEXCL): %m", eesdev);
466 		goto screwed;
467 	}
468 #endif
469 
470 	/* STRIPPED DOWN VERSION: Only PPS CD is supported at the moment */
471 
472 	/* Set port characteristics.  If we don't have a STREAMS module or
473 	 * a clock line discipline, cooked mode is just usable, even though it
474 	 * strips the top bit.  The only EES byte which uses the top
475 	 * bit is the year, and we don't use that anyway. If we do
476 	 * have the line discipline, we choose raw mode, and the
477 	 * line discipline code will block up the messages.
478 	 */
479 
480 	/* STIPPED DOWN VERSION: Only PPS CD is supported at the moment */
481 
482 	ttyp = &ttyb;
483 	if (tcgetattr(fd232, ttyp) < 0) {
484 		msyslog(LOG_ERR, "msfees_start: tcgetattr(%s): %m", eesdev);
485 		goto screwed;
486 	}
487 
488 	ttyp->c_iflag = IGNBRK|IGNPAR|ICRNL;
489 	ttyp->c_cflag = SPEED232|CS8|CLOCAL|CREAD;
490 	ttyp->c_oflag = 0;
491 	ttyp->c_lflag = ICANON;
492 	ttyp->c_cc[VERASE] = ttyp->c_cc[VKILL] = '\0';
493 	if (tcsetattr(fd232, TCSANOW, ttyp) < 0) {
494 		msyslog(LOG_ERR, "msfees_start: tcsetattr(%s): %m", eesdev);
495 		goto screwed;
496 	}
497 
498 	if (tcflush(fd232, TCIOFLUSH) < 0) {
499 		msyslog(LOG_ERR, "msfees_start: tcflush(%s): %m", eesdev);
500 		goto screwed;
501 	}
502 
503 	inherent_delay[unit].l_uf = INH_DELAY_PPS;
504 
505 	/* offset fudge (how *late* the timestamp is) = fudge + os delays */
506 	offset_fudge[unit] = os_delay[unit];
507 	L_ADD(&offset_fudge[unit], &fudgefactor[unit]);
508 	L_ADD(&offset_fudge[unit], &inherent_delay[unit]);
509 
510 	/* Looks like this might succeed.  Find memory for the structure.
511 	 * Look to see if there are any unused ones, if not we malloc() one.
512 	 */
513 	if (eesunits[unit] != 0) /* The one we want is okay */
514 	    ees = eesunits[unit];
515 	else {
516 		/* Look for an unused, but allocated struct */
517 		for (i = 0; i < MAXUNITS; i++) {
518 			if (!unitinuse[i] && eesunits[i] != 0)
519 			    break;
520 		}
521 
522 		if (i < MAXUNITS) {	/* Reclaim this one */
523 			ees = eesunits[i];
524 			eesunits[i] = 0;
525 		}			/* no spare -- make a new one */
526 		else ees = (struct eesunit *) emalloc(sizeof(struct eesunit));
527 	}
528 	memset((char *)ees, 0, sizeof(struct eesunit));
529 	eesunits[unit] = ees;
530 
531 	/* Set up the structures */
532 	ees->peer	= peer;
533 	ees->unit	= (u_char)unit;
534 	ees->timestarted= current_time;
535 	ees->ttytype	= 0;
536 	ees->io.clock_recv= ees_receive;
537 	ees->io.srcclock= (caddr_t)ees;
538 	ees->io.datalen	= 0;
539 	ees->io.fd	= fd232;
540 
541 	/* Okay.  Push one of the two (linked into the kernel, or dynamically
542 	 * loaded) STREAMS module, and give it to the I/O code to start
543 	 * receiving stuff.
544 	 */
545 
546 #ifdef STREAM
547 	{
548 		int rc1;
549 		/* Pop any existing onews first ... */
550 		while (ioctl(fd232, I_POP, 0 ) >= 0) ;
551 
552 		/* Now try pushing either of the possible modules */
553 		if ((rc1=ioctl(fd232, I_PUSH, STREAM_PP1)) < 0 &&
554 		    ioctl(fd232, I_PUSH, STREAM_PP2) < 0) {
555 			msyslog(LOG_ERR,
556 				"ees clock: Push of `%s' and `%s' to %s failed %m",
557 				STREAM_PP1, STREAM_PP2, eesdev);
558 			goto screwed;
559 		}
560 		else {
561 			NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
562 				msyslog(LOG_INFO, "I: ees clock: PUSHed %s on %s",
563 					(rc1 >= 0) ? STREAM_PP1 : STREAM_PP2, eesdev);
564 			ees->ttytype |= T_PPS;
565 		}
566 	}
567 #endif /* STREAM */
568 
569 	/* Add the clock */
570 	if (!io_addclock(&ees->io)) {
571 		/* Oh shit.  Just close and return. */
572 		msyslog(LOG_ERR, "ees clock: io_addclock(%s): %m", eesdev);
573 		goto screwed;
574 	}
575 
576 
577 	/* All done.  Initialize a few random peer variables, then
578 	 * return success. */
579 	peer->precision	= sys_precision;
580 	peer->stratum	= stratumtouse[unit];
581 	if (stratumtouse[unit] <= 1) {
582 		memcpy((char *)&pp->refid, EESREFID, 4);
583 		if (unit > 0 && unit < 10)
584 		    ((char *)&pp->refid)[3] = '0' + unit;
585 	} else {
586 		peer->refid = htonl(EESHSREFID);
587 	}
588 	unitinuse[unit] = 1;
589 	pp->unitptr = (caddr_t) &eesunits[unit];
590 	pp->clockdesc = EESDESCRIPTION;
591 	msyslog(LOG_ERR, "ees clock: %s OK on %d", eesdev, unit);
592 	return (1);
593 
594     screwed:
595 	if (fd232 != -1)
596 	    (void) close(fd232);
597 	return (0);
598 }
599 
600 
601 /* msfees_shutdown - shut down a EES clock */
602 static void
603 msfees_shutdown(
604 	int unit,
605 	struct peer *peer
606 	)
607 {
608 	register struct eesunit *ees;
609 
610 	if (unit >= MAXUNITS) {
611 		msyslog(LOG_ERR,
612 			"ees clock: INTERNAL ERROR, unit number %d invalid (max %d)",
613 			unit, MAXUNITS);
614 		return;
615 	}
616 	if (!unitinuse[unit]) {
617 		msyslog(LOG_ERR,
618 			"ees clock: INTERNAL ERROR, unit number %d not in use", unit);
619 		return;
620 	}
621 
622 	/* Tell the I/O module to turn us off.  We're history. */
623 	ees = eesunits[unit];
624 	io_closeclock(&ees->io);
625 	unitinuse[unit] = 0;
626 }
627 
628 
629 /* ees_report_event - note the occurance of an event */
630 static void
631 ees_report_event(
632 	struct eesunit *ees,
633 	int code
634 	)
635 {
636 	if (ees->status != (u_char)code) {
637 		ees->status = (u_char)code;
638 		if (code != CEVNT_NOMINAL)
639 		    ees->lastevent = (u_char)code;
640 		/* Should report event to trap handler in here.
641 		 * Soon...
642 		 */
643 	}
644 }
645 
646 
647 /* ees_receive - receive data from the serial interface on an EES clock */
648 static void
649 ees_receive(
650 	struct recvbuf *rbufp
651 	)
652 {
653 	register int n_sample;
654 	register int day;
655 	register struct eesunit *ees;
656 	register u_char *dpt;		/* Data PoinTeR: move along ... */
657 	register u_char *dpend;		/* Points just *after* last data char */
658 	register char *cp;
659 	l_fp tmp;
660 	int call_pps_sample = 0;
661 	l_fp pps_arrvstamp;
662 	int	sincelast;
663 	int	pps_step = 0;
664 	int	suspect_4ms_step = 0;
665 	struct ppsclockev ppsclockev;
666 	long *ptr = (long *) &ppsclockev;
667 	int rc;
668 	int request;
669 #ifdef HAVE_CIOGETEV
670 	request = CIOGETEV;
671 #endif
672 #ifdef HAVE_TIOCGPPSEV
673 	request = TIOCGPPSEV;
674 #endif
675 
676 	/* Get the clock this applies to and a pointer to the data */
677 	ees = (struct eesunit *)rbufp->recv_srcclock;
678 	dpt = (u_char *)&rbufp->recv_space;
679 	dpend = dpt + rbufp->recv_length;
680 	if ((debug & DB_LOG_AWAITMORE) && (rbufp->recv_length != LENEESCODE))
681 	    printf("[%d] ", rbufp->recv_length);
682 
683 	/* Check out our state and process appropriately */
684 	switch (ees->codestate) {
685 	    case EESCS_WAIT:
686 		/* Set an initial guess at the timestamp as the recv time.
687 		 * If just running in CBREAK mode, we can't improve this.
688 		 * If we have the CLOCK Line Discipline, PPSCD, or sime such,
689 		 * then we will do better later ....
690 		 */
691 		ees->arrvtime = rbufp->recv_time;
692 		ees->codestate = EESCS_GOTSOME;
693 		ees->lencode = 0;
694 		/*FALLSTHROUGH*/
695 
696 	    case EESCS_GOTSOME:
697 		cp = &(ees->lastcode[ees->lencode]);
698 
699 		/* Gobble the bytes until the final (possibly stripped) 0xff */
700 		while (dpt < dpend && (*dpt & 0x7f) != 0x7f) {
701 			*cp++ = (char)*dpt++;
702 			ees->lencode++;
703 			/* Oh dear -- too many bytes .. */
704 			if (ees->lencode > LENEESPRT) {
705 				NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
706 					msyslog(LOG_INFO,
707 						"I: ees clock: %d + %d > %d [%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x]",
708 						ees->lencode, dpend - dpt, LENEESPRT,
709 #define D(x) (ees->lastcode[x])
710 						D(0), D(1), D(2), D(3), D(4), D(5), D(6),
711 						D(7), D(8), D(9), D(10), D(11), D(12));
712 #undef	D
713 				ees->badformat++;
714 				ees->reason = CODEREASON + 1;
715 				ees_event(ees, CEVNT_BADREPLY);
716 				ees_reset(ees);
717 				return;
718 			}
719 		}
720 		/* Gave up because it was end of the buffer, rather than ff */
721 		if (dpt == dpend) {
722 			/* Incomplete.  Wait for more. */
723 			if (debug & DB_LOG_AWAITMORE)
724 			    msyslog(LOG_INFO,
725 				    "I: ees clock %d: %x == %x: await more",
726 				    ees->unit, dpt, dpend);
727 			return;
728 		}
729 
730 		/* This shouldn't happen ... ! */
731 		if ((*dpt & 0x7f) != 0x7f) {
732 			msyslog(LOG_INFO, "I: ees clock: %0x & 0x7f != 0x7f", *dpt);
733 			ees->badformat++;
734 			ees->reason = CODEREASON + 2;
735 			ees_event(ees, CEVNT_BADREPLY);
736 			ees_reset(ees);
737 			return;
738 		}
739 
740 		/* Skip the 0xff */
741 		dpt++;
742 
743 		/* Finally, got a complete buffer.  Mainline code will
744 		 * continue on. */
745 		cp = ees->lastcode;
746 		break;
747 
748 	    default:
749 		msyslog(LOG_ERR, "ees clock: INTERNAL ERROR: %d state %d",
750 			ees->unit, ees->codestate);
751 		ees->reason = CODEREASON + 5;
752 		ees_event(ees, CEVNT_FAULT);
753 		ees_reset(ees);
754 		return;
755 	}
756 
757 	/* Boy!  After all that crap, the lastcode buffer now contains
758 	 * something we hope will be a valid time code.  Do length
759 	 * checks and sanity checks on constant data.
760 	 */
761 	ees->codestate = EESCS_WAIT;
762 	ees->lasttime = current_time;
763 	if (ees->lencode != LENEESPRT) {
764 		ees->badformat++;
765 		ees->reason = CODEREASON + 6;
766 		ees_event(ees, CEVNT_BADREPLY);
767 		ees_reset(ees);
768 		return;
769 	}
770 
771 	cp = ees->lastcode;
772 
773 	/* Check that centisecond is zero */
774 	if (cp[EESM_CSEC] != 0) {
775 		ees->baddata++;
776 		ees->reason = CODEREASON + 7;
777 		ees_event(ees, CEVNT_BADREPLY);
778 		ees_reset(ees);
779 		return;
780 	}
781 
782 	/* Check flag formats */
783 	if (cp[EESM_LEAP] != 0 && cp[EESM_LEAP] != 0x0f) {
784 		ees->badformat++;
785 		ees->reason = CODEREASON + 8;
786 		ees_event(ees, CEVNT_BADREPLY);
787 		ees_reset(ees);
788 		return;
789 	}
790 
791 	if (cp[EESM_BST] != 0 && cp[EESM_BST] != 0x03) {
792 		ees->badformat++;
793 		ees->reason = CODEREASON + 9;
794 		ees_event(ees, CEVNT_BADREPLY);
795 		ees_reset(ees);
796 		return;
797 	}
798 
799 	if (cp[EESM_MSFOK] != 0 && cp[EESM_MSFOK] != 0x3f) {
800 		ees->badformat++;
801 		ees->reason = CODEREASON + 10;
802 		ees_event(ees, CEVNT_BADREPLY);
803 		ees_reset(ees);
804 		return;
805 	}
806 
807 	/* So far, so good.  Compute day, hours, minutes, seconds,
808 	 * time zone.  Do range checks on these.
809 	 */
810 
811 #define bcdunpack(val)	( (((val)>>4) & 0x0f) * 10 + ((val) & 0x0f) )
812 #define istrue(x)	((x)?1:0)
813 
814 	ees->second  = bcdunpack(cp[EESM_SEC]);  /* second       */
815 	ees->minute  = bcdunpack(cp[EESM_MIN]);  /* minute       */
816 	ees->hour    = bcdunpack(cp[EESM_HOUR]); /* hour         */
817 
818 	day          = bcdunpack(cp[EESM_DAY]);  /* day of month */
819 
820 	switch (bcdunpack(cp[EESM_MON])) {       /* month        */
821 
822 		/*  Add in lengths of all previous months.  Add one more
823 		    if it is a leap year and after February.
824 		*/
825 	    case 12:	day += NOV;			  /*FALLSTHROUGH*/
826 	    case 11:	day += OCT;			  /*FALLSTHROUGH*/
827 	    case 10:	day += SEP;			  /*FALLSTHROUGH*/
828 	    case  9:	day += AUG;			  /*FALLSTHROUGH*/
829 	    case  8:	day += JUL;			  /*FALLSTHROUGH*/
830 	    case  7:	day += JUN;			  /*FALLSTHROUGH*/
831 	    case  6:	day += MAY;			  /*FALLSTHROUGH*/
832 	    case  5:	day += APR;			  /*FALLSTHROUGH*/
833 	    case  4:	day += MAR;			  /*FALLSTHROUGH*/
834 	    case  3:	day += FEB;
835 		if (istrue(cp[EESM_LEAP])) day++; /*FALLSTHROUGH*/
836 	    case  2:	day += JAN;			  /*FALLSTHROUGH*/
837 	    case  1:	break;
838 	    default:	ees->baddata++;
839 		ees->reason = CODEREASON + 11;
840 		ees_event(ees, CEVNT_BADDATE);
841 		ees_reset(ees);
842 		return;
843 	}
844 
845 	ees->day     = day;
846 
847 	/* Get timezone. The clocktime routine wants the number
848 	 * of hours to add to the delivered time to get UT.
849 	 * Currently -1 if BST flag set, 0 otherwise.  This
850 	 * is the place to tweak things if double summer time
851 	 * ever happens.
852 	 */
853 	ees->tz      = istrue(cp[EESM_BST]) ? -1 : 0;
854 
855 	if (ees->day > 366 || ees->day < 1 ||
856 	    ees->hour > 23 || ees->minute > 59 || ees->second > 59) {
857 		ees->baddata++;
858 		ees->reason = CODEREASON + 12;
859 		ees_event(ees, CEVNT_BADDATE);
860 		ees_reset(ees);
861 		return;
862 	}
863 
864 	n_sample = ees->nsamples;
865 
866 	/* Now, compute the reference time value: text -> tmp.l_ui */
867 	if (!clocktime(ees->day, ees->hour, ees->minute, ees->second,
868 		       ees->tz, rbufp->recv_time.l_ui, &ees->yearstart,
869 		       &tmp.l_ui)) {
870 		ees->baddata++;
871 		ees->reason = CODEREASON + 13;
872 		ees_event(ees, CEVNT_BADDATE);
873 		ees_reset(ees);
874 		return;
875 	}
876 	tmp.l_uf = 0;
877 
878 	/*  DON'T use ees->arrvtime -- it may be < reftime */
879 	ees->lastsampletime = tmp;
880 
881 	/* If we are synchronised to the radio, update the reference time.
882 	 * Also keep a note of when clock was last good.
883 	 */
884 	if (istrue(cp[EESM_MSFOK])) {
885 		ees->reftime = tmp;
886 		ees->clocklastgood = current_time;
887 	}
888 
889 
890 	/* Compute the offset.  For the fractional part of the
891 	 * offset we use the expected delay for the message.
892 	 */
893 	ees->codeoffsets[n_sample].l_ui = tmp.l_ui;
894 	ees->codeoffsets[n_sample].l_uf = 0;
895 
896 	/* Number of seconds since the last step */
897 	sincelast = this_uisec - ees->last_step;
898 
899 	memset((char *) &ppsclockev, 0, sizeof ppsclockev);
900 
901 	rc = ioctl(ees->io.fd, request, (char *) &ppsclockev);
902 	if (debug & DB_PRINT_EV) fprintf(stderr,
903 					 "[%x] CIOGETEV u%d %d (%x %d) gave %d (%d): %08lx %08lx %ld\n",
904 					 DB_PRINT_EV, ees->unit, ees->io.fd, request, is_pps(ees),
905 					 rc, errno, ptr[0], ptr[1], ptr[2]);
906 
907 	/* If we managed to get the time of arrival, process the info */
908 	if (rc >= 0) {
909 		int conv = -1;
910 		pps_step = ppsclockev.serial - ees->last_pps_no;
911 
912 		/* Possible that PPS triggered, but text message didn't */
913 		if (pps_step == 2) msyslog(LOG_ERR, "pps step = 2 @ %02d", ees->second);
914 		if (pps_step == 2 && ees->second == 1) suspect_4ms_step |= 1;
915 		if (pps_step == 2 && ees->second == 2) suspect_4ms_step |= 4;
916 
917 		/* allow for single loss of PPS only */
918 		if (pps_step != 1 && pps_step != 2)
919 		    fprintf(stderr, "PPS step: %d too far off %ld (%d)\n",
920 			    ppsclockev.serial, ees->last_pps_no, pps_step);
921 		else if (!buftvtots((char *) &(ppsclockev.tv), &pps_arrvstamp))
922 		    fprintf(stderr, "buftvtots failed\n");
923 		else {	/* if ((ABS(time difference) - 0.25) < 0)
924 			 * then believe it ...
925 			 */
926 			l_fp diff;
927 			diff = pps_arrvstamp;
928 			conv = 0;
929 			L_SUB(&diff, &ees->arrvtime);
930 			if (debug & DB_PRINT_CDT)
931 			    printf("[%x] Have %lx.%08lx and %lx.%08lx -> %lx.%08lx @ %s",
932 				   DB_PRINT_CDT, (long)ees->arrvtime.l_ui, (long)ees->arrvtime.l_uf,
933 				   (long)pps_arrvstamp.l_ui, (long)pps_arrvstamp.l_uf,
934 				   (long)diff.l_ui, (long)diff.l_uf,
935 				   ctime(&(ppsclockev.tv.tv_sec)));
936 			if (L_ISNEG(&diff)) M_NEG(diff.l_ui, diff.l_uf);
937 			L_SUB(&diff, &acceptable_slop);
938 			if (L_ISNEG(&diff)) {	/* AOK -- pps_sample */
939 				ees->arrvtime = pps_arrvstamp;
940 				conv++;
941 				call_pps_sample++;
942 			}
943 			/* Some loss of some signals around sec = 1 */
944 			else if (ees->second == 1) {
945 				diff = pps_arrvstamp;
946 				L_ADD(&diff, &onesec);
947 				L_SUB(&diff, &ees->arrvtime);
948 				if (L_ISNEG(&diff)) M_NEG(diff.l_ui, diff.l_uf);
949 				L_SUB(&diff, &acceptable_slop);
950 				msyslog(LOG_ERR, "Have sec==1 slip %ds a=%08x-p=%08x -> %x.%08x (u=%d) %s",
951 					pps_arrvstamp.l_ui - ees->arrvtime.l_ui,
952 					pps_arrvstamp.l_uf,
953 					ees->arrvtime.l_uf,
954 					diff.l_ui, diff.l_uf,
955 					(int)ppsclockev.tv.tv_usec,
956 					ctime(&(ppsclockev.tv.tv_sec)));
957 				if (L_ISNEG(&diff)) {	/* AOK -- pps_sample */
958 					suspect_4ms_step |= 2;
959 					ees->arrvtime = pps_arrvstamp;
960 					L_ADD(&ees->arrvtime, &onesec);
961 					conv++;
962 					call_pps_sample++;
963 				}
964 			}
965 		}
966 		ees->last_pps_no = ppsclockev.serial;
967 		if (debug & DB_PRINT_CDTC)
968 		    printf(
969 			    "[%x] %08lx %08lx %d u%d (%d %d)\n",
970 			    DB_PRINT_CDTC, (long)pps_arrvstamp.l_ui,
971 			    (long)pps_arrvstamp.l_uf, conv, ees->unit,
972 			    call_pps_sample, pps_step);
973 	}
974 
975 	/* See if there has been a 4ms jump at a minute boundry */
976 	{	l_fp	delta;
977 #define	delta_isec	delta.l_ui
978 #define	delta_ssec	delta.l_i
979 #define	delta_sfsec	delta.l_f
980 	long	delta_f_abs;
981 
982 	delta.l_i = ees->arrvtime.l_i;
983 	delta.l_f = ees->arrvtime.l_f;
984 
985 	L_SUB(&delta, &ees->last_l);
986 	delta_f_abs = delta_sfsec;
987 	if (delta_f_abs < 0) delta_f_abs = -delta_f_abs;
988 
989 	/* Dump the deltas each minute */
990 	if (debug & DB_DUMP_DELTAS)
991 	{	if (/*0 <= ees->second && */
992 		ees->second < ((sizeof deltas) / (sizeof deltas[0]))) deltas[ees->second] = delta_sfsec;
993 	/* Dump on second 1, as second 0 sometimes missed */
994 	if (ees->second == 1) {
995 		char text[16 * ((sizeof deltas) / (sizeof deltas[0]))];
996 		char *cptr=text;
997 		int i;
998 		for (i=0; i<((sizeof deltas) / (sizeof deltas[0])); i++) {
999 			sprintf(cptr, " %d.%04d",
1000 				msec(deltas[i]), subms(deltas[i]));
1001 			while (*cptr) cptr++;
1002 		}
1003 		msyslog(LOG_ERR, "Deltas: %d.%04d<->%d.%04d: %s",
1004 			msec(EES_STEP_F - EES_STEP_F_GRACE), subms(EES_STEP_F - EES_STEP_F_GRACE),
1005 			msec(EES_STEP_F + EES_STEP_F_GRACE), subms(EES_STEP_F + EES_STEP_F_GRACE),
1006 			text+1);
1007 		for (i=0; i<((sizeof deltas) / (sizeof deltas[0])); i++) deltas[i] = 0;
1008 	}
1009 	}
1010 
1011 	/* Lets see if we have a 4 mS step at a minute boundaary */
1012 	if (	((EES_STEP_F - EES_STEP_F_GRACE) < delta_f_abs) &&
1013 		(delta_f_abs < (EES_STEP_F + EES_STEP_F_GRACE)) &&
1014 		(ees->second == 0 || ees->second == 1 || ees->second == 2) &&
1015 		(sincelast < 0 || sincelast > 122)
1016 		) {	/* 4ms jump at min boundry */
1017 		int old_sincelast;
1018 		int count=0;
1019 		int sum = 0;
1020 		/* Yes -- so compute the ramp time */
1021 		if (ees->last_step == 0) sincelast = 0;
1022 		old_sincelast = sincelast;
1023 
1024 		/* First time in, just set "ees->last_step" */
1025 		if(ees->last_step) {
1026 			int other_step = 0;
1027 			int third_step = 0;
1028 			int this_step = (sincelast + (60 /2)) / 60;
1029 			int p_step = ees->this_step;
1030 			int p;
1031 			ees->last_steps[p_step] = this_step;
1032 			p= p_step;
1033 			p_step++;
1034 			if (p_step >= LAST_STEPS) p_step = 0;
1035 			ees->this_step = p_step;
1036 				/* Find the "average" interval */
1037 			while (p != p_step) {
1038 				int this = ees->last_steps[p];
1039 				if (this == 0) break;
1040 				if (this != this_step) {
1041 					if (other_step == 0 && (
1042 						this== (this_step +2) ||
1043 						this== (this_step -2) ||
1044 						this== (this_step +1) ||
1045 						this== (this_step -1)))
1046 					    other_step = this;
1047 					if (other_step != this) {
1048 						int idelta = (this_step - other_step);
1049 						if (idelta < 0) idelta = - idelta;
1050 						if (third_step == 0 && (
1051 							(idelta == 1) ? (
1052 								this == (other_step +1) ||
1053 								this == (other_step -1) ||
1054 								this == (this_step +1) ||
1055 								this == (this_step -1))
1056 							:
1057 							(
1058 								this == (this_step + other_step)/2
1059 								)
1060 							)) third_step = this;
1061 						if (third_step != this) break;
1062 					}
1063 				}
1064 				sum += this;
1065 				p--;
1066 				if (p < 0) p += LAST_STEPS;
1067 				count++;
1068 			}
1069 			msyslog(LOG_ERR, "MSF%d: %d: This=%d (%d), other=%d/%d, sum=%d, count=%d, pps_step=%d, suspect=%x", ees->unit, p, ees->last_steps[p], this_step, other_step, third_step, sum, count, pps_step, suspect_4ms_step);
1070 			if (count != 0) sum = ((sum * 60) + (count /2)) / count;
1071 #define	SV(x) (ees->last_steps[(x + p_step) % LAST_STEPS])
1072 			msyslog(LOG_ERR, "MSF%d: %x steps %d: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
1073 				ees->unit, suspect_4ms_step, p_step, SV(0), SV(1), SV(2), SV(3), SV(4), SV(5), SV(6),
1074 				SV(7), SV(8), SV(9), SV(10), SV(11), SV(12), SV(13), SV(14), SV(15));
1075 			printf("MSF%d: steps %d: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",
1076 			       ees->unit, p_step, SV(0), SV(1), SV(2), SV(3), SV(4), SV(5), SV(6),
1077 			       SV(7), SV(8), SV(9), SV(10), SV(11), SV(12), SV(13), SV(14), SV(15));
1078 #undef SV
1079 			ees->jump_fsecs = delta_sfsec;
1080 			ees->using_ramp = 1;
1081 			if (sincelast > 170)
1082 			    ees->last_step_late += sincelast - ((sum) ? sum : ees->last_step_secs);
1083 			else ees->last_step_late = 30;
1084 			if (ees->last_step_late < -60 || ees->last_step_late > 120) ees->last_step_late = 30;
1085 			if (ees->last_step_late < 0) ees->last_step_late = 0;
1086 			if (ees->last_step_late >= 60) ees->last_step_late = 59;
1087 			sincelast = 0;
1088 		}
1089 		else {	/* First time in -- just save info */
1090 			ees->last_step_late = 30;
1091 			ees->jump_fsecs = delta_sfsec;
1092 			ees->using_ramp = 1;
1093 			sum = 4 * 60;
1094 		}
1095 		ees->last_step = this_uisec;
1096 		printf("MSF%d: d=%3ld.%04ld@%d :%d:%d:$%d:%d:%d\n",
1097 		       ees->unit, (long)msec(delta_sfsec), (long)subms(delta_sfsec),
1098 		       ees->second, old_sincelast, ees->last_step_late, count, sum,
1099 		       ees->last_step_secs);
1100 		msyslog(LOG_ERR, "MSF%d: d=%3d.%04d@%d :%d:%d:%d:%d:%d",
1101 			ees->unit, msec(delta_sfsec), subms(delta_sfsec), ees->second,
1102 			old_sincelast, ees->last_step_late, count, sum, ees->last_step_secs);
1103 		if (sum) ees->last_step_secs = sum;
1104 	}
1105 	/* OK, so not a 4ms step at a minute boundry */
1106 	else {
1107 		if (suspect_4ms_step) msyslog(LOG_ERR,
1108 					      "MSF%d: suspect = %x, but delta of %d.%04d [%d.%04d<%d.%04d<%d.%04d: %d %d]",
1109 					      ees->unit, suspect_4ms_step, msec(delta_sfsec), subms(delta_sfsec),
1110 					      msec(EES_STEP_F - EES_STEP_F_GRACE),
1111 					      subms(EES_STEP_F - EES_STEP_F_GRACE),
1112 					      (int)msec(delta_f_abs),
1113 					      (int)subms(delta_f_abs),
1114 					      msec(EES_STEP_F + EES_STEP_F_GRACE),
1115 					      subms(EES_STEP_F + EES_STEP_F_GRACE),
1116 					      ees->second,
1117 					      sincelast);
1118 		if ((delta_f_abs > EES_STEP_NOTE) && ees->last_l.l_i) {
1119 			static int ees_step_notes = EES_STEP_NOTES;
1120 			if (ees_step_notes > 0) {
1121 				ees_step_notes--;
1122 				printf("MSF%d: D=%3ld.%04ld@%02d :%d%s\n",
1123 				       ees->unit, (long)msec(delta_sfsec), (long)subms(delta_sfsec),
1124 				       ees->second, sincelast, ees_step_notes ? "" : " -- NO MORE !");
1125 				msyslog(LOG_ERR, "MSF%d: D=%3d.%04d@%02d :%d%s",
1126 					ees->unit, msec(delta_sfsec), subms(delta_sfsec), ees->second, (ees->last_step) ? sincelast : -1, ees_step_notes ? "" : " -- NO MORE !");
1127 			}
1128 		}
1129 	}
1130 	}
1131 	ees->last_l = ees->arrvtime;
1132 
1133 	/* IF we have found that it's ramping
1134 	 * && it's within twice the expected ramp period
1135 	 * && there is a non zero step size (avoid /0 !)
1136 	 * THEN we twiddle things
1137 	 */
1138 	if (ees->using_ramp &&
1139 	    sincelast < (ees->last_step_secs)*2 &&
1140 	    ees->last_step_secs)
1141 	{	long	sec_of_ramp = sincelast + ees->last_step_late;
1142 	long	fsecs;
1143 	l_fp	inc;
1144 
1145 	/* Ramp time may vary, so may ramp for longer than last time */
1146 	if (sec_of_ramp > (ees->last_step_secs + 120))
1147 	    sec_of_ramp =  ees->last_step_secs;
1148 
1149 	/* sec_of_ramp * ees->jump_fsecs may overflow 2**32 */
1150 	fsecs = sec_of_ramp * (ees->jump_fsecs /  ees->last_step_secs);
1151 
1152 	if (debug & DB_LOG_DELTAS) msyslog(LOG_ERR,
1153 					   "[%x] MSF%d: %3ld/%03d -> d=%11ld (%d|%ld)",
1154 					   DB_LOG_DELTAS,
1155 					   ees->unit, sec_of_ramp, ees->last_step_secs, fsecs,
1156 					   pps_arrvstamp.l_f, pps_arrvstamp.l_f + fsecs);
1157 	if (debug & DB_PRINT_DELTAS) printf(
1158 		"MSF%d: %3ld/%03d -> d=%11ld (%ld|%ld)\n",
1159 		ees->unit, sec_of_ramp, ees->last_step_secs, fsecs,
1160 		(long)pps_arrvstamp.l_f, pps_arrvstamp.l_f + fsecs);
1161 
1162 	/* Must sign extend the result */
1163 	inc.l_i = (fsecs < 0) ? -1 : 0;
1164 	inc.l_f = fsecs;
1165 	if (debug & DB_INC_PPS)
1166 	{	L_SUB(&pps_arrvstamp, &inc);
1167 	L_SUB(&ees->arrvtime, &inc);
1168 	}
1169 	else
1170 	{	L_ADD(&pps_arrvstamp, &inc);
1171 	L_ADD(&ees->arrvtime, &inc);
1172 	}
1173 	}
1174 	else {
1175 		if (debug & DB_LOG_DELTAS) msyslog(LOG_ERR,
1176 						   "[%x] MSF%d: ees->using_ramp=%d, sincelast=%x / %x, ees->last_step_secs=%x",
1177 						   DB_LOG_DELTAS,
1178 						   ees->unit, ees->using_ramp,
1179 						   sincelast,
1180 						   (ees->last_step_secs)*2,
1181 						   ees->last_step_secs);
1182 		if (debug & DB_PRINT_DELTAS) printf(
1183 			"[%x] MSF%d: ees->using_ramp=%d, sincelast=%x / %x, ees->last_step_secs=%x\n",
1184 			DB_LOG_DELTAS,
1185 			ees->unit, ees->using_ramp,
1186 			sincelast,
1187 			(ees->last_step_secs)*2,
1188 			ees->last_step_secs);
1189 	}
1190 
1191 	L_SUB(&ees->arrvtime, &offset_fudge[ees->unit]);
1192 	L_SUB(&pps_arrvstamp, &offset_fudge[ees->unit]);
1193 
1194 	if (call_pps_sample && !(debug & DB_NO_PPS)) {
1195 		/* Sigh -- it expects its args negated */
1196 		L_NEG(&pps_arrvstamp);
1197 		/*
1198 		 * I had to disable this here, since it appears there is no pointer to the
1199 		 * peer structure.
1200 		 *
1201 		 (void) pps_sample(peer, &pps_arrvstamp);
1202 		*/
1203 	}
1204 
1205 	/* Subtract off the local clock time stamp */
1206 	L_SUB(&ees->codeoffsets[n_sample], &ees->arrvtime);
1207 	if (debug & DB_LOG_SAMPLES) msyslog(LOG_ERR,
1208 					    "MSF%d: [%x] %d (ees: %d %d) (pps: %d %d)%s",
1209 					    ees->unit, DB_LOG_DELTAS, n_sample,
1210 					    ees->codeoffsets[n_sample].l_f,
1211 					    ees->codeoffsets[n_sample].l_f / 4295,
1212 					    pps_arrvstamp.l_f,
1213 					    pps_arrvstamp.l_f /4295,
1214 					    (debug & DB_NO_PPS) ? " [no PPS]" : "");
1215 
1216 	if (ees->nsamples++ == NCODES-1) ees_process(ees);
1217 
1218 	/* Done! */
1219 }
1220 
1221 
1222 /* offcompare - auxiliary comparison routine for offset sort */
1223 
1224 static int
1225 offcompare(
1226 	l_fp *a,
1227 	l_fp *b
1228 	)
1229 {
1230 	return(L_ISGEQ(a, b) ? (L_ISEQU(a, b) ? 0 : 1) : -1);
1231 }
1232 
1233 
1234 /* ees_process - process a pile of samples from the clock */
1235 static void
1236 ees_process(
1237 	struct eesunit *ees
1238 	)
1239 {
1240 	static int last_samples = -1;
1241 	register int i, j;
1242 	register int noff;
1243 	register l_fp *coffs = ees->codeoffsets;
1244 	l_fp offset, tmp;
1245 	double dispersion;	/* ++++ */
1246 	int lostsync, isinsync;
1247 	int samples = ees->nsamples;
1248 	int samplelog = 0;	/* keep "gcc -Wall" happy ! */
1249 	int samplereduce = (samples + 1) / 2;
1250 	double doffset;
1251 
1252 	/* Reset things to zero so we don't have to worry later */
1253 	ees_reset(ees);
1254 
1255 	if (sloppyclockflag[ees->unit]) {
1256 		samplelog = (samples <  2) ? 0 :
1257 			(samples <  5) ? 1 :
1258 			(samples <  9) ? 2 :
1259 			(samples < 17) ? 3 :
1260 			(samples < 33) ? 4 : 5;
1261 		samplereduce = (1 << samplelog);
1262 	}
1263 
1264 	if (samples != last_samples &&
1265 	    ((samples != (last_samples-1)) || samples < 3)) {
1266 		msyslog(LOG_ERR, "Samples=%d (%d), samplereduce=%d ....",
1267 			samples, last_samples, samplereduce);
1268 		last_samples = samples;
1269 	}
1270 	if (samples < 1) return;
1271 
1272 	/* If requested, dump the raw data we have in the buffer */
1273 	if (ees->dump_vals) dump_buf(coffs, 0, samples, "Raw  data  is:");
1274 
1275 	/* Sort the offsets, trim off the extremes, then choose one. */
1276 	qsort((char *) coffs, (u_int)samples, sizeof(l_fp), offcompare);
1277 
1278 	noff = samples;
1279 	i = 0;
1280 	while ((noff - i) > samplereduce) {
1281 		/* Trim off the sample which is further away
1282 		 * from the median.  We work this out by doubling
1283 		 * the median, subtracting off the end samples, and
1284 		 * looking at the sign of the answer, using the
1285 		 * identity (c-b)-(b-a) == 2*b-a-c
1286 		 */
1287 		tmp = coffs[(noff + i)/2];
1288 		L_ADD(&tmp, &tmp);
1289 		L_SUB(&tmp, &coffs[i]);
1290 		L_SUB(&tmp, &coffs[noff-1]);
1291 		if (L_ISNEG(&tmp)) noff--; else i++;
1292 	}
1293 
1294 	/* If requested, dump the reduce data we have in the buffer */
1295 	if (ees->dump_vals) dump_buf(coffs, i, noff, "Reduced    to:");
1296 
1297 	/* What we do next depends on the setting of the sloppy clock flag.
1298 	 * If it is on, average the remainder to derive our estimate.
1299 	 * Otherwise, just pick a representative value from the remaining stuff
1300 	 */
1301 	if (sloppyclockflag[ees->unit]) {
1302 		offset.l_ui = offset.l_uf = 0;
1303 		for (j = i; j < noff; j++)
1304 		    L_ADD(&offset, &coffs[j]);
1305 		for (j = samplelog; j > 0; j--)
1306 		    L_RSHIFTU(&offset);
1307 	}
1308 	else offset = coffs[i+BESTSAMPLE];
1309 
1310 	/* Compute the dispersion as the difference between the
1311 	 * lowest and highest offsets that remain in the
1312 	 * consideration list.
1313 	 *
1314 	 * It looks like MOST clocks have MOD (max error), so halve it !
1315 	 */
1316 	tmp = coffs[noff-1];
1317 	L_SUB(&tmp, &coffs[i]);
1318 #define	FRACT_SEC(n) ((1 << 30) / (n/2))
1319 	dispersion = LFPTOFP(&tmp) / 2; /* ++++ */
1320 	if (debug & (DB_SYSLOG_SMPLI | DB_SYSLOG_SMPLE)) msyslog(
1321 		(debug & DB_SYSLOG_SMPLE) ? LOG_ERR : LOG_INFO,
1322 		"I: [%x] Offset=%06d (%d), disp=%f%s [%d], %d %d=%d %d:%d %d=%d %d",
1323 		debug & (DB_SYSLOG_SMPLI | DB_SYSLOG_SMPLE),
1324 		offset.l_f / 4295, offset.l_f,
1325 		(dispersion * 1526) / 100,
1326 		(sloppyclockflag[ees->unit]) ? " by averaging" : "",
1327 		FRACT_SEC(10) / 4295,
1328 		(coffs[0].l_f) / 4295,
1329 		i,
1330 		(coffs[i].l_f) / 4295,
1331 		(coffs[samples/2].l_f) / 4295,
1332 		(coffs[i+BESTSAMPLE].l_f) / 4295,
1333 		noff-1,
1334 		(coffs[noff-1].l_f) / 4295,
1335 		(coffs[samples-1].l_f) / 4295);
1336 
1337 	/* Are we playing silly wotsits ?
1338 	 * If we are using all data, see if there is a "small" delta,
1339 	 * and if so, blurr this with 3/4 of the delta from the last value
1340 	 */
1341 	if (ees->usealldata && ees->offset.l_uf) {
1342 		long diff = (long) (ees->offset.l_uf - offset.l_uf);
1343 
1344 		/* is the delta small enough ? */
1345 		if ((- FRACT_SEC(100)) < diff && diff < FRACT_SEC(100)) {
1346 			int samd = (64 * 4) / samples;
1347 			long new;
1348 			if (samd < 2) samd = 2;
1349 			new = offset.l_uf + ((diff * (samd -1)) / samd);
1350 
1351 			/* Sign change -> need to fix up int part */
1352 			if ((new & (1 << 31)) !=
1353 			    (((long) offset.l_uf) & ( 1 << 31)))
1354 			{	NLOG(NLOG_CLOCKINFO) /* conditional if clause for conditional syslog */
1355 					msyslog(LOG_INFO, "I: %lx != %lx (%lx %lx), so add %d",
1356 						new & (1 << 31),
1357 						((long) offset.l_uf) & ( 1 << 31),
1358 						new, (long) offset.l_uf,
1359 						(new < 0) ? -1 : 1);
1360 				offset.l_ui += (new < 0) ? -1 : 1;
1361 			}
1362 			dispersion /= 4;
1363 			if (debug & (DB_SYSLOG_SMTHI | DB_SYSLOG_SMTHE)) msyslog(
1364 				(debug & DB_SYSLOG_SMTHE) ? LOG_ERR : LOG_INFO,
1365 				"I: [%x] Smooth data: %ld -> %ld, dispersion now %f",
1366 				debug & (DB_SYSLOG_SMTHI | DB_SYSLOG_SMTHE),
1367 				((long) offset.l_uf) / 4295, new / 4295,
1368 				(dispersion * 1526) / 100);
1369 			offset.l_uf = (unsigned long) new;
1370 		}
1371 		else if (debug & (DB_SYSLOG_NSMTHI | DB_SYSLOG_NSMTHE)) msyslog(
1372 			(debug & DB_SYSLOG_NSMTHE) ? LOG_ERR : LOG_INFO,
1373 			"[%x] No smooth as delta not %d < %ld < %d",
1374 			debug & (DB_SYSLOG_NSMTHI | DB_SYSLOG_NSMTHE),
1375 			- FRACT_SEC(100), diff, FRACT_SEC(100));
1376 	}
1377 	else if (debug & (DB_SYSLOG_NSMTHI | DB_SYSLOG_NSMTHE)) msyslog(
1378 		(debug & DB_SYSLOG_NSMTHE) ? LOG_ERR : LOG_INFO,
1379 		"I: [%x] No smooth as flag=%x and old=%x=%d (%d:%d)",
1380 		debug & (DB_SYSLOG_NSMTHI | DB_SYSLOG_NSMTHE),
1381 		ees->usealldata, ees->offset.l_f, ees->offset.l_uf,
1382 		offset.l_f, ees->offset.l_f - offset.l_f);
1383 
1384 	/* Collect offset info for debugging info */
1385 	ees->offset = offset;
1386 	ees->lowoffset = coffs[i];
1387 	ees->highoffset = coffs[noff-1];
1388 
1389 	/* Determine synchronization status.  Can be unsync'd either
1390 	 * by a report from the clock or by a leap hold.
1391 	 *
1392 	 * Loss of the radio signal for a short time does not cause
1393 	 * us to go unsynchronised, since the receiver keeps quite
1394 	 * good time on its own.  The spec says 20ms in 4 hours; the
1395 	 * observed drift in our clock (Cambridge) is about a second
1396 	 * a day, but even that keeps us within the inherent tolerance
1397 	 * of the clock for about 15 minutes. Observation shows that
1398 	 * the typical "short" outage is 3 minutes, so to allow us
1399 	 * to ride out those, we will give it 5 minutes.
1400 	 */
1401 	lostsync = current_time - ees->clocklastgood > 300 ? 1 : 0;
1402 	isinsync = (lostsync || ees->leaphold > current_time) ? 0 : 1;
1403 
1404 	/* Done.  Use time of last good, synchronised code as the
1405 	 * reference time, and lastsampletime as the receive time.
1406 	 */
1407 	if (ees->fix_pending) {
1408 		msyslog(LOG_ERR, "MSF%d: fix_pending=%d -> jump %x.%08x\n",
1409 			ees->fix_pending, ees->unit, offset.l_i, offset.l_f);
1410 		ees->fix_pending = 0;
1411 	}
1412 	LFPTOD(&offset, doffset);
1413 	refclock_receive(ees->peer);
1414 	ees_event(ees, lostsync ? CEVNT_PROP : CEVNT_NOMINAL);
1415 }
1416 
1417 /* msfees_poll - called by the transmit procedure */
1418 static void
1419 msfees_poll(
1420 	int unit,
1421 	struct peer *peer
1422 	)
1423 {
1424 	if (unit >= MAXUNITS) {
1425 		msyslog(LOG_ERR, "ees clock poll: INTERNAL: unit %d invalid",
1426 			unit);
1427 		return;
1428 	}
1429 	if (!unitinuse[unit]) {
1430 		msyslog(LOG_ERR, "ees clock poll: INTERNAL: unit %d unused",
1431 			unit);
1432 		return;
1433 	}
1434 
1435 	ees_process(eesunits[unit]);
1436 
1437 	if ((current_time - eesunits[unit]->lasttime) > 150)
1438 	    ees_event(eesunits[unit], CEVNT_FAULT);
1439 }
1440 
1441 
1442 #else
1443 int refclock_msfees_bs;
1444 #endif /* REFCLOCK */
1445