chutest.c revision 54359
154359Sroberto/* chutest.c,v 3.1 1993/07/06 01:05:21 jbj Exp
254359Sroberto * chutest - test the CHU clock
354359Sroberto */
454359Sroberto
554359Sroberto#include <stdio.h>
654359Sroberto#include <sys/types.h>
754359Sroberto#include <sys/socket.h>
854359Sroberto#include <netinet/in.h>
954359Sroberto#include <sys/ioctl.h>
1054359Sroberto#include <sys/time.h>
1154359Sroberto#include <sys/file.h>
1254359Sroberto#include <sgtty.h>
1354359Sroberto
1454359Sroberto#include "../include/ntp_fp.h"
1554359Sroberto#include "../include/ntp.h"
1654359Sroberto#include "../include/ntp_unixtime.h"
1754359Sroberto
1854359Sroberto#ifdef CHULDISC
1954359Sroberto#ifdef STREAM
2054359Sroberto# ifdef HAVE_SYS_CHUDEFS_H
2154359Sroberto#include <sys/chudefs.h>
2254359Sroberto#endif
2354359Sroberto#include <stropts.h>
2454359Sroberto#endif
2554359Sroberto#endif
2654359Sroberto
2754359Sroberto#ifdef CHULDISC
2854359Sroberto# ifdef HAVE_SYS_CHUDEFS_H
2954359Sroberto#include <sys/chudefs.h>
3054359Sroberto#endif
3154359Sroberto#endif
3254359Sroberto
3354359Sroberto#ifndef CHULDISC
3454359Sroberto#ifndef STREAM
3554359Sroberto#define	NCHUCHARS	(10)
3654359Sroberto
3754359Srobertostruct chucode {
3854359Sroberto	u_char codechars[NCHUCHARS];	/* code characters */
3954359Sroberto	u_char ncodechars;		/* number of code characters */
4054359Sroberto	u_char chustatus;		/* not used currently */
4154359Sroberto	struct timeval codetimes[NCHUCHARS];	/* arrival times */
4254359Sroberto};
4354359Sroberto#endif
4454359Sroberto#endif
4554359Sroberto
4654359Sroberto#define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)
4754359Sroberto
4854359Srobertochar *progname;
4954359Srobertoint debug;
5054359Sroberto
5154359Srobertoint dofilter = 0;	/* set to 1 when we should run filter algorithm */
5254359Srobertoint showtimes = 0;	/* set to 1 when we should show char arrival times */
5354359Srobertoint doprocess = 0;	/* set to 1 when we do processing analogous to driver */
5454359Sroberto#ifdef CHULDISC
5554359Srobertoint usechuldisc = 0;	/* set to 1 when CHU line discipline should be used */
5654359Sroberto#endif
5754359Sroberto#ifdef STREAM
5854359Srobertoint usechuldisc = 0;	/* set to 1 when CHU line discipline should be used */
5954359Sroberto#endif
6054359Sroberto
6154359Srobertostruct timeval lasttv;
6254359Srobertostruct chucode chudata;
6354359Sroberto
6454359Srobertoextern u_long ustotslo[];
6554359Srobertoextern u_long ustotsmid[];
6654359Srobertoextern u_long ustotshi[];
6754359Sroberto
6854359Sroberto/*
6954359Sroberto * main - parse arguments and handle options
7054359Sroberto */
7154359Srobertoint
7254359Srobertomain(
7354359Sroberto	int argc,
7454359Sroberto	char *argv[]
7554359Sroberto	)
7654359Sroberto{
7754359Sroberto	int c;
7854359Sroberto	int errflg = 0;
7954359Sroberto	extern int ntp_optind;
8054359Sroberto	extern char *ntp_optarg;
8154359Sroberto	void init_chu();
8254359Sroberto
8354359Sroberto	progname = argv[0];
8454359Sroberto	while ((c = ntp_getopt(argc, argv, "cdfpt")) != EOF)
8554359Sroberto	    switch (c) {
8654359Sroberto		case 'c':
8754359Sroberto#ifdef STREAM
8854359Sroberto		    usechuldisc = 1;
8954359Sroberto		    break;
9054359Sroberto#endif
9154359Sroberto#ifdef CHULDISC
9254359Sroberto		    usechuldisc = 1;
9354359Sroberto		    break;
9454359Sroberto#endif
9554359Sroberto#ifndef STREAM
9654359Sroberto#ifndef CHULDISC
9754359Sroberto		    (void) fprintf(stderr,
9854359Sroberto				   "%s: CHU line discipline not available on this machine\n",
9954359Sroberto				   progname);
10054359Sroberto		    exit(2);
10154359Sroberto#endif
10254359Sroberto#endif
10354359Sroberto		case 'd':
10454359Sroberto		    ++debug;
10554359Sroberto		    break;
10654359Sroberto		case 'f':
10754359Sroberto		    dofilter = 1;
10854359Sroberto		    break;
10954359Sroberto		case 'p':
11054359Sroberto		    doprocess = 1;
11154359Sroberto		case 't':
11254359Sroberto		    showtimes = 1;
11354359Sroberto		    break;
11454359Sroberto		default:
11554359Sroberto		    errflg++;
11654359Sroberto		    break;
11754359Sroberto	    }
11854359Sroberto	if (errflg || ntp_optind+1 != argc) {
11954359Sroberto#ifdef STREAM
12054359Sroberto		(void) fprintf(stderr, "usage: %s [-dft] tty_device\n",
12154359Sroberto			       progname);
12254359Sroberto#endif
12354359Sroberto#ifdef CHULDISC
12454359Sroberto		(void) fprintf(stderr, "usage: %s [-dft] tty_device\n",
12554359Sroberto			       progname);
12654359Sroberto#endif
12754359Sroberto#ifndef STREAM
12854359Sroberto#ifndef CHULDISC
12954359Sroberto		(void) fprintf(stderr, "usage: %s [-cdft] tty_device\n",
13054359Sroberto			       progname);
13154359Sroberto#endif
13254359Sroberto#endif
13354359Sroberto		exit(2);
13454359Sroberto	}
13554359Sroberto
13654359Sroberto	(void) gettimeofday(&lasttv, (struct timezone *)0);
13754359Sroberto	c = openterm(argv[ntp_optind]);
13854359Sroberto	init_chu();
13954359Sroberto#ifdef STREAM
14054359Sroberto	if (usechuldisc)
14154359Sroberto	    process_ldisc(c);
14254359Sroberto	else
14354359Sroberto#endif
14454359Sroberto#ifdef CHULDISC
14554359Sroberto	    if (usechuldisc)
14654359Sroberto		process_ldisc(c);
14754359Sroberto	    else
14854359Sroberto#endif
14954359Sroberto		process_raw(c);
15054359Sroberto	/*NOTREACHED*/
15154359Sroberto}
15254359Sroberto
15354359Sroberto
15454359Sroberto/*
15554359Sroberto * openterm - open a port to the CHU clock
15654359Sroberto */
15754359Srobertoint
15854359Srobertoopenterm(
15954359Sroberto	char *dev
16054359Sroberto	)
16154359Sroberto{
16254359Sroberto	int s;
16354359Sroberto	struct sgttyb ttyb;
16454359Sroberto
16554359Sroberto	if (debug)
16654359Sroberto	    (void) fprintf(stderr, "Doing open...");
16754359Sroberto	if ((s = open(dev, O_RDONLY, 0777)) < 0)
16854359Sroberto	    error("open(%s)", dev, "");
16954359Sroberto	if (debug)
17054359Sroberto	    (void) fprintf(stderr, "open okay\n");
17154359Sroberto
17254359Sroberto	if (debug)
17354359Sroberto	    (void) fprintf(stderr, "Setting exclusive use...");
17454359Sroberto	if (ioctl(s, TIOCEXCL, (char *)0) < 0)
17554359Sroberto	    error("ioctl(TIOCEXCL)", "", "");
17654359Sroberto	if (debug)
17754359Sroberto	    (void) fprintf(stderr, "done\n");
17854359Sroberto
17954359Sroberto	ttyb.sg_ispeed = ttyb.sg_ospeed = B300;
18054359Sroberto	ttyb.sg_erase = ttyb.sg_kill = 0;
18154359Sroberto	ttyb.sg_flags = EVENP|ODDP|RAW;
18254359Sroberto	if (debug)
18354359Sroberto	    (void) fprintf(stderr, "Setting baud rate et al...");
18454359Sroberto	if (ioctl(s, TIOCSETP, (char *)&ttyb) < 0)
18554359Sroberto	    error("ioctl(TIOCSETP, raw)", "", "");
18654359Sroberto	if (debug)
18754359Sroberto	    (void) fprintf(stderr, "done\n");
18854359Sroberto
18954359Sroberto#ifdef CHULDISC
19054359Sroberto	if (usechuldisc) {
19154359Sroberto		int ldisc;
19254359Sroberto
19354359Sroberto		if (debug)
19454359Sroberto		    (void) fprintf(stderr, "Switching to CHU ldisc...");
19554359Sroberto		ldisc = CHULDISC;
19654359Sroberto		if (ioctl(s, TIOCSETD, (char *)&ldisc) < 0)
19754359Sroberto		    error("ioctl(TIOCSETD, CHULDISC)", "", "");
19854359Sroberto		if (debug)
19954359Sroberto		    (void) fprintf(stderr, "okay\n");
20054359Sroberto	}
20154359Sroberto#endif
20254359Sroberto#ifdef STREAM
20354359Sroberto	if (usechuldisc) {
20454359Sroberto
20554359Sroberto		if (debug)
20654359Sroberto		    (void) fprintf(stderr, "Poping off streams...");
20754359Sroberto		while (ioctl(s, I_POP, 0) >=0) ;
20854359Sroberto		if (debug)
20954359Sroberto		    (void) fprintf(stderr, "okay\n");
21054359Sroberto		if (debug)
21154359Sroberto		    (void) fprintf(stderr, "Pushing CHU stream...");
21254359Sroberto		if (ioctl(s, I_PUSH, "chu") < 0)
21354359Sroberto		    error("ioctl(I_PUSH, \"chu\")", "", "");
21454359Sroberto		if (debug)
21554359Sroberto		    (void) fprintf(stderr, "okay\n");
21654359Sroberto	}
21754359Sroberto#endif
21854359Sroberto	return s;
21954359Sroberto}
22054359Sroberto
22154359Sroberto
22254359Sroberto/*
22354359Sroberto * process_raw - process characters in raw mode
22454359Sroberto */
22554359Srobertoint
22654359Srobertoprocess_raw(
22754359Sroberto	int s
22854359Sroberto	)
22954359Sroberto{
23054359Sroberto	u_char c;
23154359Sroberto	int n;
23254359Sroberto	struct timeval tv;
23354359Sroberto	struct timeval difftv;
23454359Sroberto
23554359Sroberto	while ((n = read(s, &c, sizeof(char))) > 0) {
23654359Sroberto		(void) gettimeofday(&tv, (struct timezone *)0);
23754359Sroberto		if (dofilter)
23854359Sroberto		    raw_filter((unsigned int)c, &tv);
23954359Sroberto		else {
24054359Sroberto			difftv.tv_sec = tv.tv_sec - lasttv.tv_sec;
24154359Sroberto			difftv.tv_usec = tv.tv_usec - lasttv.tv_usec;
24254359Sroberto			if (difftv.tv_usec < 0) {
24354359Sroberto				difftv.tv_sec--;
24454359Sroberto				difftv.tv_usec += 1000000;
24554359Sroberto			}
24654359Sroberto			(void) printf("%02x\t%lu.%06lu\t%lu.%06lu\n",
24754359Sroberto				      c, tv.tv_sec, tv.tv_usec, difftv.tv_sec,
24854359Sroberto				      difftv.tv_usec);
24954359Sroberto			lasttv = tv;
25054359Sroberto		}
25154359Sroberto	}
25254359Sroberto
25354359Sroberto	if (n == 0) {
25454359Sroberto		(void) fprintf(stderr, "%s: zero returned on read\n", progname);
25554359Sroberto		exit(1);
25654359Sroberto	} else
25754359Sroberto	    error("read()", "", "");
25854359Sroberto}
25954359Sroberto
26054359Sroberto
26154359Sroberto/*
26254359Sroberto * raw_filter - run the line discipline filter over raw data
26354359Sroberto */
26454359Srobertoint
26554359Srobertoraw_filter(
26654359Sroberto	unsigned int c,
26754359Sroberto	struct timeval *tv
26854359Sroberto	)
26954359Sroberto{
27054359Sroberto	static struct timeval diffs[10] = { 0 };
27154359Sroberto	struct timeval diff;
27254359Sroberto	l_fp ts;
27354359Sroberto	void chufilter();
27454359Sroberto
27554359Sroberto	if ((c & 0xf) > 9 || ((c>>4)&0xf) > 9) {
27654359Sroberto		if (debug)
27754359Sroberto		    (void) fprintf(stderr,
27854359Sroberto				   "character %02x failed BCD test\n");
27954359Sroberto		chudata.ncodechars = 0;
28054359Sroberto		return;
28154359Sroberto	}
28254359Sroberto
28354359Sroberto	if (chudata.ncodechars > 0) {
28454359Sroberto		diff.tv_sec = tv->tv_sec
28554359Sroberto			- chudata.codetimes[chudata.ncodechars].tv_sec;
28654359Sroberto		diff.tv_usec = tv->tv_usec
28754359Sroberto			- chudata.codetimes[chudata.ncodechars].tv_usec;
28854359Sroberto		if (diff.tv_usec < 0) {
28954359Sroberto			diff.tv_sec--;
29054359Sroberto			diff.tv_usec += 1000000;
29154359Sroberto		} /*
29254359Sroberto		    if (diff.tv_sec != 0 || diff.tv_usec > 900000) {
29354359Sroberto		    if (debug)
29454359Sroberto		    (void) fprintf(stderr,
29554359Sroberto		    "character %02x failed time test\n");
29654359Sroberto		    chudata.ncodechars = 0;
29754359Sroberto		    return;
29854359Sroberto		    } */
29954359Sroberto	}
30054359Sroberto
30154359Sroberto	chudata.codechars[chudata.ncodechars] = c;
30254359Sroberto	chudata.codetimes[chudata.ncodechars] = *tv;
30354359Sroberto	if (chudata.ncodechars > 0)
30454359Sroberto	    diffs[chudata.ncodechars] = diff;
30554359Sroberto	if (++chudata.ncodechars == 10) {
30654359Sroberto		if (doprocess) {
30754359Sroberto			TVTOTS(&chudata.codetimes[NCHUCHARS-1], &ts);
30854359Sroberto			ts.l_ui += JAN_1970;
30954359Sroberto			chufilter(&chudata, &chudata.codetimes[NCHUCHARS-1]);
31054359Sroberto		} else {
31154359Sroberto			register int i;
31254359Sroberto
31354359Sroberto			for (i = 0; i < chudata.ncodechars; i++) {
31454359Sroberto				(void) printf("%x%x\t%lu.%06lu\t%lu.%06lu\n",
31554359Sroberto					      chudata.codechars[i] & 0xf,
31654359Sroberto					      (chudata.codechars[i] >>4 ) & 0xf,
31754359Sroberto					      chudata.codetimes[i].tv_sec,
31854359Sroberto					      chudata.codetimes[i].tv_usec,
31954359Sroberto					      diffs[i].tv_sec, diffs[i].tv_usec);
32054359Sroberto			}
32154359Sroberto		}
32254359Sroberto		chudata.ncodechars = 0;
32354359Sroberto	}
32454359Sroberto}
32554359Sroberto
32654359Sroberto
32754359Sroberto/* #ifdef CHULDISC*/
32854359Sroberto/*
32954359Sroberto * process_ldisc - process line discipline
33054359Sroberto */
33154359Srobertoint
33254359Srobertoprocess_ldisc(
33354359Sroberto	int s
33454359Sroberto	)
33554359Sroberto{
33654359Sroberto	struct chucode chu;
33754359Sroberto	int n;
33854359Sroberto	register int i;
33954359Sroberto	struct timeval diff;
34054359Sroberto	l_fp ts;
34154359Sroberto	void chufilter();
34254359Sroberto
34354359Sroberto	while ((n = read(s, (char *)&chu, sizeof chu)) > 0) {
34454359Sroberto		if (n != sizeof chu) {
34554359Sroberto			(void) fprintf(stderr, "Expected %d, got %d\n",
34654359Sroberto				       sizeof chu, n);
34754359Sroberto			continue;
34854359Sroberto		}
34954359Sroberto
35054359Sroberto		if (doprocess) {
35154359Sroberto			TVTOTS(&chu.codetimes[NCHUCHARS-1], &ts);
35254359Sroberto			ts.l_ui += JAN_1970;
35354359Sroberto			chufilter(&chu, &ts);
35454359Sroberto		} else {
35554359Sroberto			for (i = 0; i < NCHUCHARS; i++) {
35654359Sroberto				if (i == 0)
35754359Sroberto				    diff.tv_sec = diff.tv_usec = 0;
35854359Sroberto				else {
35954359Sroberto					diff.tv_sec = chu.codetimes[i].tv_sec
36054359Sroberto						- chu.codetimes[i-1].tv_sec;
36154359Sroberto					diff.tv_usec = chu.codetimes[i].tv_usec
36254359Sroberto						- chu.codetimes[i-1].tv_usec;
36354359Sroberto					if (diff.tv_usec < 0) {
36454359Sroberto						diff.tv_sec--;
36554359Sroberto						diff.tv_usec += 1000000;
36654359Sroberto					}
36754359Sroberto				}
36854359Sroberto				(void) printf("%x%x\t%lu.%06lu\t%lu.%06lu\n",
36954359Sroberto					      chu.codechars[i] & 0xf, (chu.codechars[i]>>4)&0xf,
37054359Sroberto					      chu.codetimes[i].tv_sec, chu.codetimes[i].tv_usec,
37154359Sroberto					      diff.tv_sec, diff.tv_usec);
37254359Sroberto			}
37354359Sroberto		}
37454359Sroberto	}
37554359Sroberto	if (n == 0) {
37654359Sroberto		(void) fprintf(stderr, "%s: zero returned on read\n", progname);
37754359Sroberto		exit(1);
37854359Sroberto	} else
37954359Sroberto	    error("read()", "", "");
38054359Sroberto}
38154359Sroberto/*#endif*/
38254359Sroberto
38354359Sroberto
38454359Sroberto/*
38554359Sroberto * error - print an error message
38654359Sroberto */
38754359Srobertovoid
38854359Srobertoerror(
38954359Sroberto	char *fmt,
39054359Sroberto	char *s1,
39154359Sroberto	char *s2
39254359Sroberto	)
39354359Sroberto{
39454359Sroberto	(void) fprintf(stderr, "%s: ", progname);
39554359Sroberto	(void) fprintf(stderr, fmt, s1, s2);
39654359Sroberto	(void) fprintf(stderr, ": ");
39754359Sroberto	perror("");
39854359Sroberto	exit(1);
39954359Sroberto}
40054359Sroberto
40154359Sroberto/*
40254359Sroberto * Definitions
40354359Sroberto */
40454359Sroberto#define	MAXUNITS	4	/* maximum number of CHU units permitted */
40554359Sroberto#define	CHUDEV	"/dev/chu%d"	/* device we open.  %d is unit number */
40654359Sroberto#define	NCHUCODES	9	/* expect 9 CHU codes per minute */
40754359Sroberto
40854359Sroberto/*
40954359Sroberto * When CHU is operating optimally we want the primary clock distance
41054359Sroberto * to come out at 300 ms.  Thus, peer.distance in the CHU peer structure
41154359Sroberto * is set to 290 ms and we compute delays which are at least 10 ms long.
41254359Sroberto * The following are 290 ms and 10 ms expressed in u_fp format
41354359Sroberto */
41454359Sroberto#define	CHUDISTANCE	0x00004a3d
41554359Sroberto#define	CHUBASEDELAY	0x0000028f
41654359Sroberto
41754359Sroberto/*
41854359Sroberto * To compute a quality for the estimate (a pseudo delay) we add a
41954359Sroberto * fixed 10 ms for each missing code in the minute and add to this
42054359Sroberto * the sum of the differences between the remaining offsets and the
42154359Sroberto * estimated sample offset.
42254359Sroberto */
42354359Sroberto#define	CHUDELAYPENALTY	0x0000028f
42454359Sroberto
42554359Sroberto/*
42654359Sroberto * Other constant stuff
42754359Sroberto */
42854359Sroberto#define	CHUPRECISION	(-9)		/* what the heck */
42954359Sroberto#define	CHUREFID	"CHU\0"
43054359Sroberto
43154359Sroberto/*
43254359Sroberto * Default fudge factors
43354359Sroberto */
43454359Sroberto#define	DEFPROPDELAY	0x00624dd3	/* 0.0015 seconds, 1.5 ms */
43554359Sroberto#define	DEFFILTFUDGE	0x000d1b71	/* 0.0002 seconds, 200 us */
43654359Sroberto
43754359Sroberto/*
43854359Sroberto * Hacks to avoid excercising the multiplier.  I have no pride.
43954359Sroberto */
44054359Sroberto#define	MULBY10(x)	(((x)<<3) + ((x)<<1))
44154359Sroberto#define	MULBY60(x)	(((x)<<6) - ((x)<<2))	/* watch overflow */
44254359Sroberto#define	MULBY24(x)	(((x)<<4) + ((x)<<3))
44354359Sroberto
44454359Sroberto/*
44554359Sroberto * Constants for use when multiplying by 0.1.  ZEROPTONE is 0.1
44654359Sroberto * as an l_fp fraction, NZPOBITS is the number of significant bits
44754359Sroberto * in ZEROPTONE.
44854359Sroberto */
44954359Sroberto#define	ZEROPTONE	0x1999999a
45054359Sroberto#define	NZPOBITS	29
45154359Sroberto
45254359Sroberto/*
45354359Sroberto * The CHU table.  This gives the expected time of arrival of each
45454359Sroberto * character after the on-time second and is computed as follows:
45554359Sroberto * The CHU time code is sent at 300 bps.  Your average UART will
45654359Sroberto * synchronize at the edge of the start bit and will consider the
45754359Sroberto * character complete at the center of the first stop bit, i.e.
45854359Sroberto * 0.031667 ms later.  Thus the expected time of each interrupt
45954359Sroberto * is the start bit time plus 0.031667 seconds.  These times are
46054359Sroberto * in chutable[].  To this we add such things as propagation delay
46154359Sroberto * and delay fudge factor.
46254359Sroberto */
46354359Sroberto#define	CHARDELAY	0x081b4e80
46454359Sroberto
46554359Srobertostatic u_long chutable[NCHUCHARS] = {
46654359Sroberto	0x2147ae14 + CHARDELAY,		/* 0.130 (exactly) */
46754359Sroberto	0x2ac08312 + CHARDELAY,		/* 0.167 (exactly) */
46854359Sroberto	0x34395810 + CHARDELAY,		/* 0.204 (exactly) */
46954359Sroberto	0x3db22d0e + CHARDELAY,		/* 0.241 (exactly) */
47054359Sroberto	0x472b020c + CHARDELAY,		/* 0.278 (exactly) */
47154359Sroberto	0x50a3d70a + CHARDELAY,		/* 0.315 (exactly) */
47254359Sroberto	0x5a1cac08 + CHARDELAY,		/* 0.352 (exactly) */
47354359Sroberto	0x63958106 + CHARDELAY,		/* 0.389 (exactly) */
47454359Sroberto	0x6d0e5604 + CHARDELAY,		/* 0.426 (exactly) */
47554359Sroberto	0x76872b02 + CHARDELAY,		/* 0.463 (exactly) */
47654359Sroberto};
47754359Sroberto
47854359Sroberto/*
47954359Sroberto * Keep the fudge factors separately so they can be set even
48054359Sroberto * when no clock is configured.
48154359Sroberto */
48254359Srobertostatic l_fp propagation_delay;
48354359Srobertostatic l_fp fudgefactor;
48454359Srobertostatic l_fp offset_fudge;
48554359Sroberto
48654359Sroberto/*
48754359Sroberto * We keep track of the start of the year, watching for changes.
48854359Sroberto * We also keep track of whether the year is a leap year or not.
48954359Sroberto * All because stupid CHU doesn't include the year in the time code.
49054359Sroberto */
49154359Srobertostatic u_long yearstart;
49254359Sroberto
49354359Sroberto/*
49454359Sroberto * Imported from the timer module
49554359Sroberto */
49654359Srobertoextern u_long current_time;
49754359Srobertoextern struct event timerqueue[];
49854359Sroberto
49954359Sroberto/*
50054359Sroberto * Time conversion tables imported from the library
50154359Sroberto */
50254359Srobertoextern u_long ustotslo[];
50354359Srobertoextern u_long ustotsmid[];
50454359Srobertoextern u_long ustotshi[];
50554359Sroberto
50654359Sroberto
50754359Sroberto/*
50854359Sroberto * init_chu - initialize internal chu driver data
50954359Sroberto */
51054359Srobertovoid
51154359Srobertoinit_chu(void)
51254359Sroberto{
51354359Sroberto
51454359Sroberto	/*
51554359Sroberto	 * Initialize fudge factors to default.
51654359Sroberto	 */
51754359Sroberto	propagation_delay.l_ui = 0;
51854359Sroberto	propagation_delay.l_uf = DEFPROPDELAY;
51954359Sroberto	fudgefactor.l_ui = 0;
52054359Sroberto	fudgefactor.l_uf = DEFFILTFUDGE;
52154359Sroberto	offset_fudge = propagation_delay;
52254359Sroberto	L_ADD(&offset_fudge, &fudgefactor);
52354359Sroberto
52454359Sroberto	yearstart = 0;
52554359Sroberto}
52654359Sroberto
52754359Sroberto
52854359Srobertovoid
52954359Srobertochufilter(
53054359Sroberto	struct chucode *chuc,
53154359Sroberto	l_fp *rtime
53254359Sroberto	)
53354359Sroberto{
53454359Sroberto	register int i;
53554359Sroberto	register u_long date_ui;
53654359Sroberto	register u_long tmp;
53754359Sroberto	register u_char *code;
53854359Sroberto	int isneg;
53954359Sroberto	int imin;
54054359Sroberto	int imax;
54154359Sroberto	u_long reftime;
54254359Sroberto	l_fp off[NCHUCHARS];
54354359Sroberto	l_fp ts;
54454359Sroberto	int day, hour, minute, second;
54554359Sroberto	static u_char lastcode[NCHUCHARS];
54654359Sroberto	extern u_long calyearstart();
54754359Sroberto	extern char *mfptoa();
54854359Sroberto	void chu_process();
54954359Sroberto	extern char *prettydate();
55054359Sroberto
55154359Sroberto	/*
55254359Sroberto	 * We'll skip the checks made in the kernel, but assume they've
55354359Sroberto	 * been done.  This means that all characters are BCD and
55454359Sroberto	 * the intercharacter spacing isn't unreasonable.
55554359Sroberto	 */
55654359Sroberto
55754359Sroberto	/*
55854359Sroberto	 * print the code
55954359Sroberto	 */
56054359Sroberto	for (i = 0; i < NCHUCHARS; i++)
56154359Sroberto	    printf("%c%c", (chuc->codechars[i] & 0xf) + '0',
56254359Sroberto		   ((chuc->codechars[i]>>4) & 0xf) + '0');
56354359Sroberto	printf("\n");
56454359Sroberto
56554359Sroberto	/*
56654359Sroberto	 * Format check.  Make sure the two halves match.
56754359Sroberto	 */
56854359Sroberto	for (i = 0; i < NCHUCHARS/2; i++)
56954359Sroberto	    if (chuc->codechars[i] != chuc->codechars[i+(NCHUCHARS/2)]) {
57054359Sroberto		    (void) printf("Bad format, halves don't match\n");
57154359Sroberto		    return;
57254359Sroberto	    }
57354359Sroberto
57454359Sroberto	/*
57554359Sroberto	 * Break out the code into the BCD nibbles.  Only need to fiddle
57654359Sroberto	 * with the first half since both are identical.  Note the first
57754359Sroberto	 * BCD character is the low order nibble, the second the high order.
57854359Sroberto	 */
57954359Sroberto	code = lastcode;
58054359Sroberto	for (i = 0; i < NCHUCHARS/2; i++) {
58154359Sroberto		*code++ = chuc->codechars[i] & 0xf;
58254359Sroberto		*code++ = (chuc->codechars[i] >> 4) & 0xf;
58354359Sroberto	}
58454359Sroberto
58554359Sroberto	/*
58654359Sroberto	 * If the first nibble isn't a 6, we're up the creek
58754359Sroberto	 */
58854359Sroberto	code = lastcode;
58954359Sroberto	if (*code++ != 6) {
59054359Sroberto		(void) printf("Bad format, no 6 at start\n");
59154359Sroberto		return;
59254359Sroberto	}
59354359Sroberto
59454359Sroberto	/*
59554359Sroberto	 * Collect the day, the hour, the minute and the second.
59654359Sroberto	 */
59754359Sroberto	day = *code++;
59854359Sroberto	day = MULBY10(day) + *code++;
59954359Sroberto	day = MULBY10(day) + *code++;
60054359Sroberto	hour = *code++;
60154359Sroberto	hour = MULBY10(hour) + *code++;
60254359Sroberto	minute = *code++;
60354359Sroberto	minute = MULBY10(minute) + *code++;
60454359Sroberto	second = *code++;
60554359Sroberto	second = MULBY10(second) + *code++;
60654359Sroberto
60754359Sroberto	/*
60854359Sroberto	 * Sanity check the day and time.  Note that this
60954359Sroberto	 * only occurs on the 31st through the 39th second
61054359Sroberto	 * of the minute.
61154359Sroberto	 */
61254359Sroberto	if (day < 1 || day > 366
61354359Sroberto	    || hour > 23 || minute > 59
61454359Sroberto	    || second < 31 || second > 39) {
61554359Sroberto		(void) printf("Failed date sanity check: %d %d %d %d\n",
61654359Sroberto			      day, hour, minute, second);
61754359Sroberto		return;
61854359Sroberto	}
61954359Sroberto
62054359Sroberto	/*
62154359Sroberto	 * Compute seconds into the year.
62254359Sroberto	 */
62354359Sroberto	tmp = (u_long)(MULBY24((day-1)) + hour);	/* hours */
62454359Sroberto	tmp = MULBY60(tmp) + (u_long)minute;		/* minutes */
62554359Sroberto	tmp = MULBY60(tmp) + (u_long)second;		/* seconds */
62654359Sroberto
62754359Sroberto	/*
62854359Sroberto	 * Now the fun begins.  We demand that the received time code
62954359Sroberto	 * be within CLOCK_WAYTOOBIG of the receive timestamp, but
63054359Sroberto	 * there is uncertainty about the year the timestamp is in.
63154359Sroberto	 * Use the current year start for the first check, this should
63254359Sroberto	 * work most of the time.
63354359Sroberto	 */
63454359Sroberto	date_ui = tmp + yearstart;
63554359Sroberto	if (date_ui < (rtime->l_ui + CLOCK_WAYTOOBIG)
63654359Sroberto	    && date_ui > (rtime->l_ui - CLOCK_WAYTOOBIG))
63754359Sroberto	    goto codeokay;	/* looks good */
63854359Sroberto
63954359Sroberto	/*
64054359Sroberto	 * Trouble.  Next check is to see if the year rolled over and, if
64154359Sroberto	 * so, try again with the new year's start.
64254359Sroberto	 */
64354359Sroberto	date_ui = calyearstart(rtime->l_ui);
64454359Sroberto	if (date_ui != yearstart) {
64554359Sroberto		yearstart = date_ui;
64654359Sroberto		date_ui += tmp;
64754359Sroberto		(void) printf("time %u, code %u, difference %d\n",
64854359Sroberto			      date_ui, rtime->l_ui, (long)date_ui-(long)rtime->l_ui);
64954359Sroberto		if (date_ui < (rtime->l_ui + CLOCK_WAYTOOBIG)
65054359Sroberto		    && date_ui > (rtime->l_ui - CLOCK_WAYTOOBIG))
65154359Sroberto		    goto codeokay;	/* okay this time */
65254359Sroberto	}
65354359Sroberto
65454359Sroberto	ts.l_uf = 0;
65554359Sroberto	ts.l_ui = yearstart;
65654359Sroberto	printf("yearstart %s\n", prettydate(&ts));
65754359Sroberto	printf("received %s\n", prettydate(rtime));
65854359Sroberto	ts.l_ui = date_ui;
65954359Sroberto	printf("date_ui %s\n", prettydate(&ts));
66054359Sroberto
66154359Sroberto	/*
66254359Sroberto	 * Here we know the year start matches the current system
66354359Sroberto	 * time.  One remaining possibility is that the time code
66454359Sroberto	 * is in the year previous to that of the system time.  This
66554359Sroberto	 * is only worth checking if the receive timestamp is less
66654359Sroberto	 * than CLOCK_WAYTOOBIG seconds into the new year.
66754359Sroberto	 */
66854359Sroberto	if ((rtime->l_ui - yearstart) < CLOCK_WAYTOOBIG) {
66954359Sroberto		date_ui = tmp + calyearstart(yearstart - CLOCK_WAYTOOBIG);
67054359Sroberto		if ((rtime->l_ui - date_ui) < CLOCK_WAYTOOBIG)
67154359Sroberto		    goto codeokay;
67254359Sroberto	}
67354359Sroberto
67454359Sroberto	/*
67554359Sroberto	 * One last possibility is that the time stamp is in the year
67654359Sroberto	 * following the year the system is in.  Try this one before
67754359Sroberto	 * giving up.
67854359Sroberto	 */
67954359Sroberto	date_ui = tmp + calyearstart(yearstart + (400*24*60*60)); /* 400 days */
68054359Sroberto	if ((date_ui - rtime->l_ui) >= CLOCK_WAYTOOBIG) {
68154359Sroberto		printf("Date hopelessly off\n");
68254359Sroberto		return;		/* hopeless, let it sync to other peers */
68354359Sroberto	}
68454359Sroberto
68554359Sroberto    codeokay:
68654359Sroberto	reftime = date_ui;
68754359Sroberto	/*
68854359Sroberto	 * We've now got the integral seconds part of the time code (we hope).
68954359Sroberto	 * The fractional part comes from the table.  We next compute
69054359Sroberto	 * the offsets for each character.
69154359Sroberto	 */
69254359Sroberto	for (i = 0; i < NCHUCHARS; i++) {
69354359Sroberto		register u_long tmp2;
69454359Sroberto
69554359Sroberto		off[i].l_ui = date_ui;
69654359Sroberto		off[i].l_uf = chutable[i];
69754359Sroberto		tmp = chuc->codetimes[i].tv_sec + JAN_1970;
69854359Sroberto		TVUTOTSF(chuc->codetimes[i].tv_usec, tmp2);
69954359Sroberto		M_SUB(off[i].l_ui, off[i].l_uf, tmp, tmp2);
70054359Sroberto	}
70154359Sroberto
70254359Sroberto	/*
70354359Sroberto	 * Here is a *big* problem.  What one would normally
70454359Sroberto	 * do here on a machine with lots of clock bits (say
70554359Sroberto	 * a Vax or the gizmo board) is pick the most positive
70654359Sroberto	 * offset and the estimate, since this is the one that
70754359Sroberto	 * is most likely suffered the smallest interrupt delay.
70854359Sroberto	 * The trouble is that the low order clock bit on an IBM
70954359Sroberto	 * RT, which is the machine I had in mind when doing this,
71054359Sroberto	 * ticks at just under the millisecond mark.  This isn't
71154359Sroberto	 * precise enough.  What we can do to improve this is to
71254359Sroberto	 * average all 10 samples and rely on the second level
71354359Sroberto	 * filtering to pick the least delayed estimate.  Trouble
71454359Sroberto	 * is, this means we have to divide a 64 bit fixed point
71554359Sroberto	 * number by 10, a procedure which really sucks.  Oh, well.
71654359Sroberto	 * First compute the sum.
71754359Sroberto	 */
71854359Sroberto	date_ui = 0;
71954359Sroberto	tmp = 0;
72054359Sroberto	for (i = 0; i < NCHUCHARS; i++)
72154359Sroberto	    M_ADD(date_ui, tmp, off[i].l_ui, off[i].l_uf);
72254359Sroberto	if (M_ISNEG(date_ui, tmp))
72354359Sroberto	    isneg = 1;
72454359Sroberto	else
72554359Sroberto	    isneg = 0;
72654359Sroberto
72754359Sroberto	/*
72854359Sroberto	 * Here is a multiply-by-0.1 optimization that should apply
72954359Sroberto	 * just about everywhere.  If the magnitude of the sum
73054359Sroberto	 * is less than 9 we don't have to worry about overflow
73154359Sroberto	 * out of a 64 bit product, even after rounding.
73254359Sroberto	 */
73354359Sroberto	if (date_ui < 9 || date_ui > 0xfffffff7) {
73454359Sroberto		register u_long prod_ui;
73554359Sroberto		register u_long prod_uf;
73654359Sroberto
73754359Sroberto		prod_ui = prod_uf = 0;
73854359Sroberto		/*
73954359Sroberto		 * This code knows the low order bit in 0.1 is zero
74054359Sroberto		 */
74154359Sroberto		for (i = 1; i < NZPOBITS; i++) {
74254359Sroberto			M_LSHIFT(date_ui, tmp);
74354359Sroberto			if (ZEROPTONE & (1<<i))
74454359Sroberto			    M_ADD(prod_ui, prod_uf, date_ui, tmp);
74554359Sroberto		}
74654359Sroberto
74754359Sroberto		/*
74854359Sroberto		 * Done, round it correctly.  Prod_ui contains the
74954359Sroberto		 * fraction.
75054359Sroberto		 */
75154359Sroberto		if (prod_uf & 0x80000000)
75254359Sroberto		    prod_ui++;
75354359Sroberto		if (isneg)
75454359Sroberto		    date_ui = 0xffffffff;
75554359Sroberto		else
75654359Sroberto		    date_ui = 0;
75754359Sroberto		tmp = prod_ui;
75854359Sroberto		/*
75954359Sroberto		 * date_ui is integral part, tmp is fraction.
76054359Sroberto		 */
76154359Sroberto	} else {
76254359Sroberto		register u_long prod_ovr;
76354359Sroberto		register u_long prod_ui;
76454359Sroberto		register u_long prod_uf;
76554359Sroberto		register u_long highbits;
76654359Sroberto
76754359Sroberto		prod_ovr = prod_ui = prod_uf = 0;
76854359Sroberto		if (isneg)
76954359Sroberto		    highbits = 0xffffffff;	/* sign extend */
77054359Sroberto		else
77154359Sroberto		    highbits = 0;
77254359Sroberto		/*
77354359Sroberto		 * This code knows the low order bit in 0.1 is zero
77454359Sroberto		 */
77554359Sroberto		for (i = 1; i < NZPOBITS; i++) {
77654359Sroberto			M_LSHIFT3(highbits, date_ui, tmp);
77754359Sroberto			if (ZEROPTONE & (1<<i))
77854359Sroberto			    M_ADD3(prod_ovr, prod_uf, prod_ui,
77954359Sroberto				   highbits, date_ui, tmp);
78054359Sroberto		}
78154359Sroberto
78254359Sroberto		if (prod_uf & 0x80000000)
78354359Sroberto		    M_ADDUF(prod_ovr, prod_ui, (u_long)1);
78454359Sroberto		date_ui = prod_ovr;
78554359Sroberto		tmp = prod_ui;
78654359Sroberto	}
78754359Sroberto
78854359Sroberto	/*
78954359Sroberto	 * At this point we have the mean offset, with the integral
79054359Sroberto	 * part in date_ui and the fractional part in tmp.  Store
79154359Sroberto	 * it in the structure.
79254359Sroberto	 */
79354359Sroberto	/*
79454359Sroberto	 * Add in fudge factor.
79554359Sroberto	 */
79654359Sroberto	M_ADD(date_ui, tmp, offset_fudge.l_ui, offset_fudge.l_uf);
79754359Sroberto
79854359Sroberto	/*
79954359Sroberto	 * Find the minimun and maximum offset
80054359Sroberto	 */
80154359Sroberto	imin = imax = 0;
80254359Sroberto	for (i = 1; i < NCHUCHARS; i++) {
80354359Sroberto		if (L_ISGEQ(&off[i], &off[imax])) {
80454359Sroberto			imax = i;
80554359Sroberto		} else if (L_ISGEQ(&off[imin], &off[i])) {
80654359Sroberto			imin = i;
80754359Sroberto		}
80854359Sroberto	}
80954359Sroberto
81054359Sroberto	L_ADD(&off[imin], &offset_fudge);
81154359Sroberto	if (imin != imax)
81254359Sroberto	    L_ADD(&off[imax], &offset_fudge);
81354359Sroberto	(void) printf("mean %s, min %s, max %s\n",
81454359Sroberto		      mfptoa(date_ui, tmp, 8), lfptoa(&off[imin], 8),
81554359Sroberto		      lfptoa(&off[imax], 8));
81654359Sroberto}
817