diff options
author | Ollivier Robert <roberto@FreeBSD.org> | 1999-12-09 13:01:21 +0000 |
---|---|---|
committer | Ollivier Robert <roberto@FreeBSD.org> | 1999-12-09 13:01:21 +0000 |
commit | c0b746e5e8d9479f05b3749cbf1f73b8928719bd (patch) | |
tree | fc0cfa1aab0ff6b228f511b410733ef4f35d1ead /contrib/ntp/clockstuff | |
download | src-c0b746e5e8d9479f05b3749cbf1f73b8928719bd.tar.gz src-c0b746e5e8d9479f05b3749cbf1f73b8928719bd.zip |
Virgin import of ntpd 4.0.98fvendor/ntp/4.0.98f
Notes
Notes:
svn path=/vendor/ntp/dist/; revision=54359
svn path=/vendor/ntp/4.0.98f/; revision=54361; tag=vendor/ntp/4.0.98f
Diffstat (limited to 'contrib/ntp/clockstuff')
-rw-r--r-- | contrib/ntp/clockstuff/Makefile.am | 16 | ||||
-rw-r--r-- | contrib/ntp/clockstuff/Makefile.in | 342 | ||||
-rw-r--r-- | contrib/ntp/clockstuff/README | 31 | ||||
-rw-r--r-- | contrib/ntp/clockstuff/chutest.c | 816 | ||||
-rw-r--r-- | contrib/ntp/clockstuff/clktest.c | 529 | ||||
-rw-r--r-- | contrib/ntp/clockstuff/propdelay.c | 544 |
6 files changed, 2278 insertions, 0 deletions
diff --git a/contrib/ntp/clockstuff/Makefile.am b/contrib/ntp/clockstuff/Makefile.am new file mode 100644 index 000000000000..44e85a842214 --- /dev/null +++ b/contrib/ntp/clockstuff/Makefile.am @@ -0,0 +1,16 @@ +#AUTOMAKE_OPTIONS = ../ansi2knr no-dependencies +AUTOMAKE_OPTIONS = ../util/ansi2knr +noinst_PROGRAMS = @PROPDELAY@ @CHUTEST@ @CLKTEST@ +EXTRA_PROGRAMS = propdelay chutest clktest + +INCLUDES = -I$(top_srcdir)/include +# We need -lm (and perhaps $(COMPAT) for propdelay, -lntp for {chu,clk}test +propdelay_LDADD = -lm +chutest_LDADD = ../libntp/libntp.a +clktest_LDADD = ../libntp/libntp.a +ETAGS_ARGS = Makefile.am +#EXTRA_DIST = TAGS + +chutest: ../libntp/libntp.a + +clktest: ../libntp/libntp.a diff --git a/contrib/ntp/clockstuff/Makefile.in b/contrib/ntp/clockstuff/Makefile.in new file mode 100644 index 000000000000..67797b8b9a68 --- /dev/null +++ b/contrib/ntp/clockstuff/Makefile.in @@ -0,0 +1,342 @@ +# Makefile.in generated automatically by automake 1.4a from Makefile.am + +# Copyright (C) 1994, 1995-8, 1999 Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +SHELL = @SHELL@ + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ +prefix = @prefix@ +exec_prefix = @exec_prefix@ + +bindir = @bindir@ +sbindir = @sbindir@ +libexecdir = @libexecdir@ +datadir = @datadir@ +sysconfdir = @sysconfdir@ +sharedstatedir = @sharedstatedir@ +localstatedir = @localstatedir@ +libdir = @libdir@ +infodir = @infodir@ +mandir = @mandir@ +includedir = @includedir@ +oldincludedir = /usr/include + +DESTDIR = + +pkgdatadir = $(datadir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ + +top_builddir = .. + +ACLOCAL = @ACLOCAL@ +AUTOCONF = @AUTOCONF@ +AUTOMAKE = @AUTOMAKE@ +AUTOHEADER = @AUTOHEADER@ + +INSTALL = @INSTALL@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_FLAG = +transform = @program_transform_name@ + +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_alias = @build_alias@ +build_triplet = @build@ +host_alias = @host_alias@ +host_triplet = @host@ +target_alias = @target_alias@ +target_triplet = @target@ +AMTAR = @AMTAR@ +AMTARFLAGS = @AMTARFLAGS@ +AWK = @AWK@ +CC = @CC@ +CFLAGS = @CFLAGS@ +CHUTEST = @CHUTEST@ +CLKTEST = @CLKTEST@ +CPP = @CPP@ +DCFD = @DCFD@ +LDFLAGS = @LDFLAGS@ +LIBPARSE = @LIBPARSE@ +LIBRSAREF = @LIBRSAREF@ +LN_S = @LN_S@ +MAKEINFO = @MAKEINFO@ +MAKE_ADJTIMED = @MAKE_ADJTIMED@ +MAKE_CHECK_Y2K = @MAKE_CHECK_Y2K@ +MAKE_LIBPARSE = @MAKE_LIBPARSE@ +MAKE_LIBPARSE_KERNEL = @MAKE_LIBPARSE_KERNEL@ +MAKE_LIBRSAREF = @MAKE_LIBRSAREF@ +MAKE_NTPTIME = @MAKE_NTPTIME@ +MAKE_PARSEKMODULE = @MAKE_PARSEKMODULE@ +MAKE_TICKADJ = @MAKE_TICKADJ@ +PACKAGE = @PACKAGE@ +PATH_SH = @PATH_SH@ +PROPDELAY = @PROPDELAY@ +RANLIB = @RANLIB@ +RSAREF = @RSAREF@ +TESTDCF = @TESTDCF@ +U = @U@ +VERSION = @VERSION@ + +#AUTOMAKE_OPTIONS = ../ansi2knr no-dependencies + + +AUTOMAKE_OPTIONS = ../util/ansi2knr +noinst_PROGRAMS = @PROPDELAY@ @CHUTEST@ @CLKTEST@ +EXTRA_PROGRAMS = propdelay chutest clktest + +INCLUDES = -I$(top_srcdir)/include +# We need -lm (and perhaps $(COMPAT) for propdelay, -lntp for {chu,clk}test +propdelay_LDADD = -lm +chutest_LDADD = ../libntp/libntp.a +clktest_LDADD = ../libntp/libntp.a +ETAGS_ARGS = Makefile.am +subdir = clockstuff +mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs +CONFIG_HEADER = ../config.h +CONFIG_CLEAN_FILES = +PROGRAMS = $(noinst_PROGRAMS) + + +DEFS = @DEFS@ -I. -I$(srcdir) -I.. +CPPFLAGS = @CPPFLAGS@ +LIBS = @LIBS@ +ANSI2KNR = ../util/ansi2knr +chutest_SOURCES = chutest.c +chutest_OBJECTS = chutest$U.o +chutest_DEPENDENCIES = ../libntp/libntp.a +chutest_LDFLAGS = +clktest_SOURCES = clktest.c +clktest_OBJECTS = clktest$U.o +clktest_DEPENDENCIES = ../libntp/libntp.a +clktest_LDFLAGS = +propdelay_SOURCES = propdelay.c +propdelay_OBJECTS = propdelay$U.o +propdelay_DEPENDENCIES = +propdelay_LDFLAGS = +COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ +DIST_SOURCES = chutest.c clktest.c propdelay.c +DIST_COMMON = README Makefile.am Makefile.in + + +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) + +GZIP_ENV = --best +SOURCES = chutest.c clktest.c propdelay.c +OBJECTS = chutest$U.o clktest$U.o propdelay$U.o + +all: all-redirect +.SUFFIXES: +.SUFFIXES: .c .o +$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) + cd $(top_srcdir) && $(AUTOMAKE) --gnu --include-deps clockstuff/Makefile + +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + cd $(top_builddir) \ + && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status + + +mostlyclean-noinstPROGRAMS: + +clean-noinstPROGRAMS: + -test -z "$(noinst_PROGRAMS)" || rm -f $(noinst_PROGRAMS) + +distclean-noinstPROGRAMS: + +maintainer-clean-noinstPROGRAMS: + +.c.o: + $(COMPILE) -c $< + +mostlyclean-compile: + -rm -f *.o core *.core + +clean-compile: + +distclean-compile: + -rm -f *.tab.c + +maintainer-clean-compile: +../util/ansi2knr: ../util/ansi2knr.o + cd ../util && $(MAKE) $(AM_MAKEFLAGS) ansi2knr + +../util/ansi2knr.o: + cd ../util && $(MAKE) $(AM_MAKEFLAGS) ansi2knr.o + + +mostlyclean-kr: + -rm -f *_.c + +clean-kr: + +distclean-kr: + +maintainer-clean-kr: +chutest$U.o: +clktest$U.o: +propdelay$U.o: + +propdelay: $(propdelay_OBJECTS) $(propdelay_DEPENDENCIES) + @rm -f propdelay + $(LINK) $(propdelay_LDFLAGS) $(propdelay_OBJECTS) $(propdelay_LDADD) $(LIBS) +chutest_.c: chutest.c $(ANSI2KNR) + $(CPP) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/chutest.c; then echo $(srcdir)/chutest.c; else echo chutest.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > chutest_.c +clktest_.c: clktest.c $(ANSI2KNR) + $(CPP) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/clktest.c; then echo $(srcdir)/clktest.c; else echo clktest.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > clktest_.c +propdelay_.c: propdelay.c $(ANSI2KNR) + $(CPP) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) `if test -f $(srcdir)/propdelay.c; then echo $(srcdir)/propdelay.c; else echo propdelay.c; fi` | sed 's/^# \([0-9]\)/#line \1/' | $(ANSI2KNR) > propdelay_.c +chutest_.o clktest_.o propdelay_.o : $(ANSI2KNR) + +tags: TAGS + +ID: $(HEADERS) $(SOURCES) $(LISP) + list='$(SOURCES) $(HEADERS)'; \ + unique=`for i in $$list; do echo $$i; done | \ + ${AWK:-awk} ' { files[$$0] = 1; } \ + END { for (i in files) print i; }'`; \ + here=`pwd` && cd $(srcdir) \ + && mkid -f$$here/ID $$unique $(LISP) + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) $(LISP) + tags=; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS)'; \ + unique=`for i in $$list; do echo $$i; done | \ + ${AWK:-awk} ' { files[$$0] = 1; } \ + END { for (i in files) print i; }'`; \ + test -z "$(ETAGS_ARGS)$$unique$(LISP)$$tags" \ + || (cd $(srcdir) && etags $(ETAGS_ARGS) $$tags $$unique $(LISP) -o $$here/TAGS) + +mostlyclean-tags: + +clean-tags: + +distclean-tags: + -rm -f TAGS ID + +maintainer-clean-tags: + +distdir = $(top_builddir)/$(PACKAGE)-$(VERSION)/$(subdir) + +distdir: $(DISTFILES) + @for file in $(DISTFILES); do \ + d=$(srcdir); \ + if test -d $$d/$$file; then \ + cp -pr $$d/$$file $(distdir)/$$file; \ + else \ + test -f $(distdir)/$$file \ + || ln $$d/$$file $(distdir)/$$file 2> /dev/null \ + || cp -p $$d/$$file $(distdir)/$$file || :; \ + fi; \ + done +chutest.o chutest.lo: chutest.c ../include/ntp_fp.h \ + ../include/ntp_types.h ../include/ntp_machine.h ../config.h \ + ../include/ntp_proto.h ../include/ntp.h \ + ../include/ntp_unixtime.h +clktest.o clktest.lo: clktest.c ../include/ntp_fp.h \ + ../include/ntp_types.h ../include/ntp_machine.h ../config.h \ + ../include/ntp_proto.h ../include/ntp.h \ + ../include/ntp_unixtime.h +propdelay.o propdelay.lo: propdelay.c ../include/ntp_stdlib.h \ + ../include/ntp_types.h ../include/ntp_machine.h ../config.h \ + ../include/ntp_proto.h ../include/ntp_string.h \ + ../include/l_stdlib.h + +info-am: +info: info-am +dvi-am: +dvi: dvi-am +check-am: all-am +check: check-am +installcheck-am: +installcheck: installcheck-am +install-exec-am: +install-exec: install-exec-am + +install-data-am: +install-data: install-data-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am +install: install-am +uninstall-am: +uninstall: uninstall-am +all-am: Makefile $(PROGRAMS) +all-redirect: all-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_STRIP_FLAG=-s install +installdirs: + + +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -rm -f Makefile $(CONFIG_CLEAN_FILES) + -rm -f config.cache config.log stamp-h stamp-h[0-9]* + +maintainer-clean-generic: +mostlyclean-am: mostlyclean-noinstPROGRAMS mostlyclean-compile \ + mostlyclean-kr mostlyclean-tags mostlyclean-generic + +mostlyclean: mostlyclean-am + +clean-am: clean-noinstPROGRAMS clean-compile clean-kr clean-tags \ + clean-generic mostlyclean-am + +clean: clean-am + +distclean-am: distclean-noinstPROGRAMS distclean-compile distclean-kr \ + distclean-tags distclean-generic clean-am + +distclean: distclean-am + +maintainer-clean-am: maintainer-clean-noinstPROGRAMS \ + maintainer-clean-compile maintainer-clean-kr \ + maintainer-clean-tags maintainer-clean-generic \ + distclean-am + @echo "This command is intended for maintainers to use;" + @echo "it deletes files that may require special tools to rebuild." + +maintainer-clean: maintainer-clean-am + +.PHONY: mostlyclean-noinstPROGRAMS distclean-noinstPROGRAMS \ +clean-noinstPROGRAMS maintainer-clean-noinstPROGRAMS \ +mostlyclean-compile distclean-compile clean-compile \ +maintainer-clean-compile mostlyclean-kr distclean-kr clean-kr \ +maintainer-clean-kr tags mostlyclean-tags distclean-tags clean-tags \ +maintainer-clean-tags distdir info-am info dvi-am dvi check check-am \ +installcheck-am installcheck install-exec-am install-exec \ +install-data-am install-data install-am install uninstall-am uninstall \ +all-redirect all-am all install-strip installdirs mostlyclean-generic \ +distclean-generic clean-generic maintainer-clean-generic clean \ +mostlyclean distclean maintainer-clean + +#EXTRA_DIST = TAGS + +chutest: ../libntp/libntp.a + +clktest: ../libntp/libntp.a + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/contrib/ntp/clockstuff/README b/contrib/ntp/clockstuff/README new file mode 100644 index 000000000000..c7f972773aec --- /dev/null +++ b/contrib/ntp/clockstuff/README @@ -0,0 +1,31 @@ +README file for directory ./clockstuff of the NTP Version 4 distribution + +This directory contains the sources for utility programs designed to +support radio clocks. The chutest.c and clktest.c are desgined to +test the chu_clk and tty_clk line disciplines and STREAMS modules in +the ../kernel directory. + +These files have been modified to work with either the line disciplines +or the STREAMS modules. Be sure to define -DSTREAM if appropriate. + +These are random bits of things written to help with clocks. You can +make things in here by typing one or more of: + + make propdelay (or `make') + make chutest + make clktest + +Propdelay computes high frequency propagation delays, given the +longitude and latitude of the transmitter and receiver. Use +this for WWV/H and CHU. Don't use it for WWVB (the computation +is easier for that). + +Chutest can be used to input and process data from a CHU modem +attached to a serial port. It will use the CHU line discipline +(if installed), or raw mode otherwise. This was used to test +out the initial reduction algorithms, and may not be up to date. + +Clktest can be used to test the clock line discipline (CLKLDISC, +it must be available), and to take a look at radio clocks attached to a +serial port. + diff --git a/contrib/ntp/clockstuff/chutest.c b/contrib/ntp/clockstuff/chutest.c new file mode 100644 index 000000000000..785c253edbce --- /dev/null +++ b/contrib/ntp/clockstuff/chutest.c @@ -0,0 +1,816 @@ +/* chutest.c,v 3.1 1993/07/06 01:05:21 jbj Exp + * chutest - test the CHU clock + */ + +#include <stdio.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <sys/ioctl.h> +#include <sys/time.h> +#include <sys/file.h> +#include <sgtty.h> + +#include "../include/ntp_fp.h" +#include "../include/ntp.h" +#include "../include/ntp_unixtime.h" + +#ifdef CHULDISC +#ifdef STREAM +# ifdef HAVE_SYS_CHUDEFS_H +#include <sys/chudefs.h> +#endif +#include <stropts.h> +#endif +#endif + +#ifdef CHULDISC +# ifdef HAVE_SYS_CHUDEFS_H +#include <sys/chudefs.h> +#endif +#endif + +#ifndef CHULDISC +#ifndef STREAM +#define NCHUCHARS (10) + +struct chucode { + u_char codechars[NCHUCHARS]; /* code characters */ + u_char ncodechars; /* number of code characters */ + u_char chustatus; /* not used currently */ + struct timeval codetimes[NCHUCHARS]; /* arrival times */ +}; +#endif +#endif + +#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) + +char *progname; +int debug; + +int dofilter = 0; /* set to 1 when we should run filter algorithm */ +int showtimes = 0; /* set to 1 when we should show char arrival times */ +int doprocess = 0; /* set to 1 when we do processing analogous to driver */ +#ifdef CHULDISC +int usechuldisc = 0; /* set to 1 when CHU line discipline should be used */ +#endif +#ifdef STREAM +int usechuldisc = 0; /* set to 1 when CHU line discipline should be used */ +#endif + +struct timeval lasttv; +struct chucode chudata; + +extern u_long ustotslo[]; +extern u_long ustotsmid[]; +extern u_long ustotshi[]; + +/* + * main - parse arguments and handle options + */ +int +main( + int argc, + char *argv[] + ) +{ + int c; + int errflg = 0; + extern int ntp_optind; + extern char *ntp_optarg; + void init_chu(); + + progname = argv[0]; + while ((c = ntp_getopt(argc, argv, "cdfpt")) != EOF) + switch (c) { + case 'c': +#ifdef STREAM + usechuldisc = 1; + break; +#endif +#ifdef CHULDISC + usechuldisc = 1; + break; +#endif +#ifndef STREAM +#ifndef CHULDISC + (void) fprintf(stderr, + "%s: CHU line discipline not available on this machine\n", + progname); + exit(2); +#endif +#endif + case 'd': + ++debug; + break; + case 'f': + dofilter = 1; + break; + case 'p': + doprocess = 1; + case 't': + showtimes = 1; + break; + default: + errflg++; + break; + } + if (errflg || ntp_optind+1 != argc) { +#ifdef STREAM + (void) fprintf(stderr, "usage: %s [-dft] tty_device\n", + progname); +#endif +#ifdef CHULDISC + (void) fprintf(stderr, "usage: %s [-dft] tty_device\n", + progname); +#endif +#ifndef STREAM +#ifndef CHULDISC + (void) fprintf(stderr, "usage: %s [-cdft] tty_device\n", + progname); +#endif +#endif + exit(2); + } + + (void) gettimeofday(&lasttv, (struct timezone *)0); + c = openterm(argv[ntp_optind]); + init_chu(); +#ifdef STREAM + if (usechuldisc) + process_ldisc(c); + else +#endif +#ifdef CHULDISC + if (usechuldisc) + process_ldisc(c); + else +#endif + process_raw(c); + /*NOTREACHED*/ +} + + +/* + * openterm - open a port to the CHU clock + */ +int +openterm( + char *dev + ) +{ + int s; + struct sgttyb ttyb; + + if (debug) + (void) fprintf(stderr, "Doing open..."); + if ((s = open(dev, O_RDONLY, 0777)) < 0) + error("open(%s)", dev, ""); + if (debug) + (void) fprintf(stderr, "open okay\n"); + + if (debug) + (void) fprintf(stderr, "Setting exclusive use..."); + if (ioctl(s, TIOCEXCL, (char *)0) < 0) + error("ioctl(TIOCEXCL)", "", ""); + if (debug) + (void) fprintf(stderr, "done\n"); + + ttyb.sg_ispeed = ttyb.sg_ospeed = B300; + ttyb.sg_erase = ttyb.sg_kill = 0; + ttyb.sg_flags = EVENP|ODDP|RAW; + if (debug) + (void) fprintf(stderr, "Setting baud rate et al..."); + if (ioctl(s, TIOCSETP, (char *)&ttyb) < 0) + error("ioctl(TIOCSETP, raw)", "", ""); + if (debug) + (void) fprintf(stderr, "done\n"); + +#ifdef CHULDISC + if (usechuldisc) { + int ldisc; + + if (debug) + (void) fprintf(stderr, "Switching to CHU ldisc..."); + ldisc = CHULDISC; + if (ioctl(s, TIOCSETD, (char *)&ldisc) < 0) + error("ioctl(TIOCSETD, CHULDISC)", "", ""); + if (debug) + (void) fprintf(stderr, "okay\n"); + } +#endif +#ifdef STREAM + if (usechuldisc) { + + if (debug) + (void) fprintf(stderr, "Poping off streams..."); + while (ioctl(s, I_POP, 0) >=0) ; + if (debug) + (void) fprintf(stderr, "okay\n"); + if (debug) + (void) fprintf(stderr, "Pushing CHU stream..."); + if (ioctl(s, I_PUSH, "chu") < 0) + error("ioctl(I_PUSH, \"chu\")", "", ""); + if (debug) + (void) fprintf(stderr, "okay\n"); + } +#endif + return s; +} + + +/* + * process_raw - process characters in raw mode + */ +int +process_raw( + int s + ) +{ + u_char c; + int n; + struct timeval tv; + struct timeval difftv; + + while ((n = read(s, &c, sizeof(char))) > 0) { + (void) gettimeofday(&tv, (struct timezone *)0); + if (dofilter) + raw_filter((unsigned int)c, &tv); + else { + difftv.tv_sec = tv.tv_sec - lasttv.tv_sec; + difftv.tv_usec = tv.tv_usec - lasttv.tv_usec; + if (difftv.tv_usec < 0) { + difftv.tv_sec--; + difftv.tv_usec += 1000000; + } + (void) printf("%02x\t%lu.%06lu\t%lu.%06lu\n", + c, tv.tv_sec, tv.tv_usec, difftv.tv_sec, + difftv.tv_usec); + lasttv = tv; + } + } + + if (n == 0) { + (void) fprintf(stderr, "%s: zero returned on read\n", progname); + exit(1); + } else + error("read()", "", ""); +} + + +/* + * raw_filter - run the line discipline filter over raw data + */ +int +raw_filter( + unsigned int c, + struct timeval *tv + ) +{ + static struct timeval diffs[10] = { 0 }; + struct timeval diff; + l_fp ts; + void chufilter(); + + if ((c & 0xf) > 9 || ((c>>4)&0xf) > 9) { + if (debug) + (void) fprintf(stderr, + "character %02x failed BCD test\n"); + chudata.ncodechars = 0; + return; + } + + if (chudata.ncodechars > 0) { + diff.tv_sec = tv->tv_sec + - chudata.codetimes[chudata.ncodechars].tv_sec; + diff.tv_usec = tv->tv_usec + - chudata.codetimes[chudata.ncodechars].tv_usec; + if (diff.tv_usec < 0) { + diff.tv_sec--; + diff.tv_usec += 1000000; + } /* + if (diff.tv_sec != 0 || diff.tv_usec > 900000) { + if (debug) + (void) fprintf(stderr, + "character %02x failed time test\n"); + chudata.ncodechars = 0; + return; + } */ + } + + chudata.codechars[chudata.ncodechars] = c; + chudata.codetimes[chudata.ncodechars] = *tv; + if (chudata.ncodechars > 0) + diffs[chudata.ncodechars] = diff; + if (++chudata.ncodechars == 10) { + if (doprocess) { + TVTOTS(&chudata.codetimes[NCHUCHARS-1], &ts); + ts.l_ui += JAN_1970; + chufilter(&chudata, &chudata.codetimes[NCHUCHARS-1]); + } else { + register int i; + + for (i = 0; i < chudata.ncodechars; i++) { + (void) printf("%x%x\t%lu.%06lu\t%lu.%06lu\n", + chudata.codechars[i] & 0xf, + (chudata.codechars[i] >>4 ) & 0xf, + chudata.codetimes[i].tv_sec, + chudata.codetimes[i].tv_usec, + diffs[i].tv_sec, diffs[i].tv_usec); + } + } + chudata.ncodechars = 0; + } +} + + +/* #ifdef CHULDISC*/ +/* + * process_ldisc - process line discipline + */ +int +process_ldisc( + int s + ) +{ + struct chucode chu; + int n; + register int i; + struct timeval diff; + l_fp ts; + void chufilter(); + + while ((n = read(s, (char *)&chu, sizeof chu)) > 0) { + if (n != sizeof chu) { + (void) fprintf(stderr, "Expected %d, got %d\n", + sizeof chu, n); + continue; + } + + if (doprocess) { + TVTOTS(&chu.codetimes[NCHUCHARS-1], &ts); + ts.l_ui += JAN_1970; + chufilter(&chu, &ts); + } else { + for (i = 0; i < NCHUCHARS; i++) { + if (i == 0) + diff.tv_sec = diff.tv_usec = 0; + else { + diff.tv_sec = chu.codetimes[i].tv_sec + - chu.codetimes[i-1].tv_sec; + diff.tv_usec = chu.codetimes[i].tv_usec + - chu.codetimes[i-1].tv_usec; + if (diff.tv_usec < 0) { + diff.tv_sec--; + diff.tv_usec += 1000000; + } + } + (void) printf("%x%x\t%lu.%06lu\t%lu.%06lu\n", + chu.codechars[i] & 0xf, (chu.codechars[i]>>4)&0xf, + chu.codetimes[i].tv_sec, chu.codetimes[i].tv_usec, + diff.tv_sec, diff.tv_usec); + } + } + } + if (n == 0) { + (void) fprintf(stderr, "%s: zero returned on read\n", progname); + exit(1); + } else + error("read()", "", ""); +} +/*#endif*/ + + +/* + * error - print an error message + */ +void +error( + char *fmt, + char *s1, + char *s2 + ) +{ + (void) fprintf(stderr, "%s: ", progname); + (void) fprintf(stderr, fmt, s1, s2); + (void) fprintf(stderr, ": "); + perror(""); + exit(1); +} + +/* + * Definitions + */ +#define MAXUNITS 4 /* maximum number of CHU units permitted */ +#define CHUDEV "/dev/chu%d" /* device we open. %d is unit number */ +#define NCHUCODES 9 /* expect 9 CHU codes per minute */ + +/* + * When CHU is operating optimally we want the primary clock distance + * to come out at 300 ms. Thus, peer.distance in the CHU peer structure + * is set to 290 ms and we compute delays which are at least 10 ms long. + * The following are 290 ms and 10 ms expressed in u_fp format + */ +#define CHUDISTANCE 0x00004a3d +#define CHUBASEDELAY 0x0000028f + +/* + * To compute a quality for the estimate (a pseudo delay) we add a + * fixed 10 ms for each missing code in the minute and add to this + * the sum of the differences between the remaining offsets and the + * estimated sample offset. + */ +#define CHUDELAYPENALTY 0x0000028f + +/* + * Other constant stuff + */ +#define CHUPRECISION (-9) /* what the heck */ +#define CHUREFID "CHU\0" + +/* + * Default fudge factors + */ +#define DEFPROPDELAY 0x00624dd3 /* 0.0015 seconds, 1.5 ms */ +#define DEFFILTFUDGE 0x000d1b71 /* 0.0002 seconds, 200 us */ + +/* + * Hacks to avoid excercising the multiplier. I have no pride. + */ +#define MULBY10(x) (((x)<<3) + ((x)<<1)) +#define MULBY60(x) (((x)<<6) - ((x)<<2)) /* watch overflow */ +#define MULBY24(x) (((x)<<4) + ((x)<<3)) + +/* + * Constants for use when multiplying by 0.1. ZEROPTONE is 0.1 + * as an l_fp fraction, NZPOBITS is the number of significant bits + * in ZEROPTONE. + */ +#define ZEROPTONE 0x1999999a +#define NZPOBITS 29 + +/* + * The CHU table. This gives the expected time of arrival of each + * character after the on-time second and is computed as follows: + * The CHU time code is sent at 300 bps. Your average UART will + * synchronize at the edge of the start bit and will consider the + * character complete at the center of the first stop bit, i.e. + * 0.031667 ms later. Thus the expected time of each interrupt + * is the start bit time plus 0.031667 seconds. These times are + * in chutable[]. To this we add such things as propagation delay + * and delay fudge factor. + */ +#define CHARDELAY 0x081b4e80 + +static u_long chutable[NCHUCHARS] = { + 0x2147ae14 + CHARDELAY, /* 0.130 (exactly) */ + 0x2ac08312 + CHARDELAY, /* 0.167 (exactly) */ + 0x34395810 + CHARDELAY, /* 0.204 (exactly) */ + 0x3db22d0e + CHARDELAY, /* 0.241 (exactly) */ + 0x472b020c + CHARDELAY, /* 0.278 (exactly) */ + 0x50a3d70a + CHARDELAY, /* 0.315 (exactly) */ + 0x5a1cac08 + CHARDELAY, /* 0.352 (exactly) */ + 0x63958106 + CHARDELAY, /* 0.389 (exactly) */ + 0x6d0e5604 + CHARDELAY, /* 0.426 (exactly) */ + 0x76872b02 + CHARDELAY, /* 0.463 (exactly) */ +}; + +/* + * Keep the fudge factors separately so they can be set even + * when no clock is configured. + */ +static l_fp propagation_delay; +static l_fp fudgefactor; +static l_fp offset_fudge; + +/* + * We keep track of the start of the year, watching for changes. + * We also keep track of whether the year is a leap year or not. + * All because stupid CHU doesn't include the year in the time code. + */ +static u_long yearstart; + +/* + * Imported from the timer module + */ +extern u_long current_time; +extern struct event timerqueue[]; + +/* + * Time conversion tables imported from the library + */ +extern u_long ustotslo[]; +extern u_long ustotsmid[]; +extern u_long ustotshi[]; + + +/* + * init_chu - initialize internal chu driver data + */ +void +init_chu(void) +{ + + /* + * Initialize fudge factors to default. + */ + propagation_delay.l_ui = 0; + propagation_delay.l_uf = DEFPROPDELAY; + fudgefactor.l_ui = 0; + fudgefactor.l_uf = DEFFILTFUDGE; + offset_fudge = propagation_delay; + L_ADD(&offset_fudge, &fudgefactor); + + yearstart = 0; +} + + +void +chufilter( + struct chucode *chuc, + l_fp *rtime + ) +{ + register int i; + register u_long date_ui; + register u_long tmp; + register u_char *code; + int isneg; + int imin; + int imax; + u_long reftime; + l_fp off[NCHUCHARS]; + l_fp ts; + int day, hour, minute, second; + static u_char lastcode[NCHUCHARS]; + extern u_long calyearstart(); + extern char *mfptoa(); + void chu_process(); + extern char *prettydate(); + + /* + * We'll skip the checks made in the kernel, but assume they've + * been done. This means that all characters are BCD and + * the intercharacter spacing isn't unreasonable. + */ + + /* + * print the code + */ + for (i = 0; i < NCHUCHARS; i++) + printf("%c%c", (chuc->codechars[i] & 0xf) + '0', + ((chuc->codechars[i]>>4) & 0xf) + '0'); + printf("\n"); + + /* + * Format check. Make sure the two halves match. + */ + for (i = 0; i < NCHUCHARS/2; i++) + if (chuc->codechars[i] != chuc->codechars[i+(NCHUCHARS/2)]) { + (void) printf("Bad format, halves don't match\n"); + return; + } + + /* + * Break out the code into the BCD nibbles. Only need to fiddle + * with the first half since both are identical. Note the first + * BCD character is the low order nibble, the second the high order. + */ + code = lastcode; + for (i = 0; i < NCHUCHARS/2; i++) { + *code++ = chuc->codechars[i] & 0xf; + *code++ = (chuc->codechars[i] >> 4) & 0xf; + } + + /* + * If the first nibble isn't a 6, we're up the creek + */ + code = lastcode; + if (*code++ != 6) { + (void) printf("Bad format, no 6 at start\n"); + return; + } + + /* + * Collect the day, the hour, the minute and the second. + */ + day = *code++; + day = MULBY10(day) + *code++; + day = MULBY10(day) + *code++; + hour = *code++; + hour = MULBY10(hour) + *code++; + minute = *code++; + minute = MULBY10(minute) + *code++; + second = *code++; + second = MULBY10(second) + *code++; + + /* + * Sanity check the day and time. Note that this + * only occurs on the 31st through the 39th second + * of the minute. + */ + if (day < 1 || day > 366 + || hour > 23 || minute > 59 + || second < 31 || second > 39) { + (void) printf("Failed date sanity check: %d %d %d %d\n", + day, hour, minute, second); + return; + } + + /* + * Compute seconds into the year. + */ + tmp = (u_long)(MULBY24((day-1)) + hour); /* hours */ + tmp = MULBY60(tmp) + (u_long)minute; /* minutes */ + tmp = MULBY60(tmp) + (u_long)second; /* seconds */ + + /* + * Now the fun begins. We demand that the received time code + * be within CLOCK_WAYTOOBIG of the receive timestamp, but + * there is uncertainty about the year the timestamp is in. + * Use the current year start for the first check, this should + * work most of the time. + */ + date_ui = tmp + yearstart; + if (date_ui < (rtime->l_ui + CLOCK_WAYTOOBIG) + && date_ui > (rtime->l_ui - CLOCK_WAYTOOBIG)) + goto codeokay; /* looks good */ + + /* + * Trouble. Next check is to see if the year rolled over and, if + * so, try again with the new year's start. + */ + date_ui = calyearstart(rtime->l_ui); + if (date_ui != yearstart) { + yearstart = date_ui; + date_ui += tmp; + (void) printf("time %u, code %u, difference %d\n", + date_ui, rtime->l_ui, (long)date_ui-(long)rtime->l_ui); + if (date_ui < (rtime->l_ui + CLOCK_WAYTOOBIG) + && date_ui > (rtime->l_ui - CLOCK_WAYTOOBIG)) + goto codeokay; /* okay this time */ + } + + ts.l_uf = 0; + ts.l_ui = yearstart; + printf("yearstart %s\n", prettydate(&ts)); + printf("received %s\n", prettydate(rtime)); + ts.l_ui = date_ui; + printf("date_ui %s\n", prettydate(&ts)); + + /* + * Here we know the year start matches the current system + * time. One remaining possibility is that the time code + * is in the year previous to that of the system time. This + * is only worth checking if the receive timestamp is less + * than CLOCK_WAYTOOBIG seconds into the new year. + */ + if ((rtime->l_ui - yearstart) < CLOCK_WAYTOOBIG) { + date_ui = tmp + calyearstart(yearstart - CLOCK_WAYTOOBIG); + if ((rtime->l_ui - date_ui) < CLOCK_WAYTOOBIG) + goto codeokay; + } + + /* + * One last possibility is that the time stamp is in the year + * following the year the system is in. Try this one before + * giving up. + */ + date_ui = tmp + calyearstart(yearstart + (400*24*60*60)); /* 400 days */ + if ((date_ui - rtime->l_ui) >= CLOCK_WAYTOOBIG) { + printf("Date hopelessly off\n"); + return; /* hopeless, let it sync to other peers */ + } + + codeokay: + reftime = date_ui; + /* + * We've now got the integral seconds part of the time code (we hope). + * The fractional part comes from the table. We next compute + * the offsets for each character. + */ + for (i = 0; i < NCHUCHARS; i++) { + register u_long tmp2; + + off[i].l_ui = date_ui; + off[i].l_uf = chutable[i]; + tmp = chuc->codetimes[i].tv_sec + JAN_1970; + TVUTOTSF(chuc->codetimes[i].tv_usec, tmp2); + M_SUB(off[i].l_ui, off[i].l_uf, tmp, tmp2); + } + + /* + * Here is a *big* problem. What one would normally + * do here on a machine with lots of clock bits (say + * a Vax or the gizmo board) is pick the most positive + * offset and the estimate, since this is the one that + * is most likely suffered the smallest interrupt delay. + * The trouble is that the low order clock bit on an IBM + * RT, which is the machine I had in mind when doing this, + * ticks at just under the millisecond mark. This isn't + * precise enough. What we can do to improve this is to + * average all 10 samples and rely on the second level + * filtering to pick the least delayed estimate. Trouble + * is, this means we have to divide a 64 bit fixed point + * number by 10, a procedure which really sucks. Oh, well. + * First compute the sum. + */ + date_ui = 0; + tmp = 0; + for (i = 0; i < NCHUCHARS; i++) + M_ADD(date_ui, tmp, off[i].l_ui, off[i].l_uf); + if (M_ISNEG(date_ui, tmp)) + isneg = 1; + else + isneg = 0; + + /* + * Here is a multiply-by-0.1 optimization that should apply + * just about everywhere. If the magnitude of the sum + * is less than 9 we don't have to worry about overflow + * out of a 64 bit product, even after rounding. + */ + if (date_ui < 9 || date_ui > 0xfffffff7) { + register u_long prod_ui; + register u_long prod_uf; + + prod_ui = prod_uf = 0; + /* + * This code knows the low order bit in 0.1 is zero + */ + for (i = 1; i < NZPOBITS; i++) { + M_LSHIFT(date_ui, tmp); + if (ZEROPTONE & (1<<i)) + M_ADD(prod_ui, prod_uf, date_ui, tmp); + } + + /* + * Done, round it correctly. Prod_ui contains the + * fraction. + */ + if (prod_uf & 0x80000000) + prod_ui++; + if (isneg) + date_ui = 0xffffffff; + else + date_ui = 0; + tmp = prod_ui; + /* + * date_ui is integral part, tmp is fraction. + */ + } else { + register u_long prod_ovr; + register u_long prod_ui; + register u_long prod_uf; + register u_long highbits; + + prod_ovr = prod_ui = prod_uf = 0; + if (isneg) + highbits = 0xffffffff; /* sign extend */ + else + highbits = 0; + /* + * This code knows the low order bit in 0.1 is zero + */ + for (i = 1; i < NZPOBITS; i++) { + M_LSHIFT3(highbits, date_ui, tmp); + if (ZEROPTONE & (1<<i)) + M_ADD3(prod_ovr, prod_uf, prod_ui, + highbits, date_ui, tmp); + } + + if (prod_uf & 0x80000000) + M_ADDUF(prod_ovr, prod_ui, (u_long)1); + date_ui = prod_ovr; + tmp = prod_ui; + } + + /* + * At this point we have the mean offset, with the integral + * part in date_ui and the fractional part in tmp. Store + * it in the structure. + */ + /* + * Add in fudge factor. + */ + M_ADD(date_ui, tmp, offset_fudge.l_ui, offset_fudge.l_uf); + + /* + * Find the minimun and maximum offset + */ + imin = imax = 0; + for (i = 1; i < NCHUCHARS; i++) { + if (L_ISGEQ(&off[i], &off[imax])) { + imax = i; + } else if (L_ISGEQ(&off[imin], &off[i])) { + imin = i; + } + } + + L_ADD(&off[imin], &offset_fudge); + if (imin != imax) + L_ADD(&off[imax], &offset_fudge); + (void) printf("mean %s, min %s, max %s\n", + mfptoa(date_ui, tmp, 8), lfptoa(&off[imin], 8), + lfptoa(&off[imax], 8)); +} diff --git a/contrib/ntp/clockstuff/clktest.c b/contrib/ntp/clockstuff/clktest.c new file mode 100644 index 000000000000..86edf59d52d2 --- /dev/null +++ b/contrib/ntp/clockstuff/clktest.c @@ -0,0 +1,529 @@ +/* clktest.c,v 3.1 1993/07/06 01:05:23 jbj Exp + * clktest - test the clock line discipline + * + * usage: clktest -b bps -f -t timeo -s cmd -c char1 -a char2 /dev/whatever + */ + +#include <stdio.h> +#include <ctype.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <signal.h> +#include <netinet/in.h> +#include <sys/ioctl.h> +#include <sys/time.h> +#include <sys/file.h> +#include <sgtty.h> + +#include "../include/ntp_fp.h" +#include "../include/ntp.h" +#include "../include/ntp_unixtime.h" + +#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) + +#if defined(ULT_2_0_SUCKS) +#ifndef sigmask +#define sigmask(m) (1<<(m)) +#endif +#endif + +#ifndef STREAM +#ifndef CLKLDISC +CLOCK_LINE_DISCIPLINE_NEEDED_BY_THIS_PROGRAM; +#endif +#endif + +/* + * Mask for blocking SIGIO and SIGALRM + */ +#define BLOCKSIGMASK (sigmask(SIGIO)|sigmask(SIGALRM)) + +/* + * speed table + */ +struct speeds { + int bps; + int rate; +} speedtab[] = { + { 300, B300 }, + { 1200, B1200 }, + { 2400, B2400 }, + { 4800, B4800 }, + { 9600, B9600 }, + { 19200, EXTA }, + { 38400, EXTB }, + { 0, 0 } +}; + +char *progname; +int debug; + +#ifdef CLKLDISC +#define DEFMAGIC '\r' +#endif + +#ifdef CLKLDISC +# ifdef STREAM +# include <stropts.h> +# ifdef HAVE_SYS_CLKDEFS_H +# include <sys/clkdefs.h> +# endif +# define DEFMAGIC "\r" +# endif +#endif + +struct timeval timeout = { 0 }; +char *cmd = NULL; +int cmdlen; +int docmd = 0; +#ifdef CLKLDISC +u_long magic1 = DEFMAGIC; +u_long magic2 = DEFMAGIC; +#endif +#ifdef STREAM +char magic[32]; +#endif +int speed = B9600; +int ttflags = RAW|EVENP|ODDP; + +volatile int wasalarmed; +volatile int iosig; + +struct timeval lasttv; + +extern u_long ustotslo[]; +extern u_long ustotsmid[]; +extern u_long ustotshi[]; + +/* + * main - parse arguments and handle options + */ +int +main( + int argc, + char *argv[] + ) +{ + int c; + int errflg = 0; + struct speeds *spd; + u_long tmp; + int fd; + struct sgttyb ttyb; + struct itimerval itimer; + extern int ntp_optind; + extern char *ntp_optarg; + int alarming(); + int ioready(); + + progname = argv[0]; +#ifdef STREAM + magic[0] = 0; +#endif + while ((c = ntp_getopt(argc, argv, "a:b:c:dfs:t:")) != EOF) + switch (c) { +#ifdef CLKLDISC + case 'a': +#endif + case 'c': + if (!atouint(ntp_optarg, &tmp)) { + (void) fprintf(stderr, + "%s: argument for -%c must be integer\n", + progname, c); + errflg++; + break; + } +#ifdef CLKLDISC + if (c == 'c') + magic1 = tmp; + else + magic2 = tmp; +#endif +#ifdef STREAM + magic[strlen(magic)+1] = '\0'; + magic[strlen(magic)] = tmp; +#endif + break; + case 'b': + if (!atouint(ntp_optarg, &tmp)) { + errflg++; + break; + } + spd = speedtab; + while (spd->bps != 0) + if ((int)tmp == spd->bps) + break; + if (spd->bps == 0) { + (void) fprintf(stderr, + "%s: speed %lu is unsupported\n", + progname, tmp); + errflg++; + } else { + speed = spd->rate; + } + break; + case 'd': + ++debug; + break; + case 'f': + ttflags |= CRMOD; + break; + case 's': + cmdlen = strlen(ntp_optarg); + if (cmdlen == 0) + errflg++; + else + cmd = ntp_optarg; + break; + case 't': + if (!atouint(ntp_optarg, &tmp)) + errflg++; + else { + timeout.tv_sec = (long)tmp; + docmd = 1; + } + break; + default: + errflg++; + break; + } + if (errflg || ntp_optind+1 != argc) { + (void) fprintf(stderr, +#ifdef CLKLDISC + "usage: %s [-b bps] [-c magic1] [-a magic2] [-f] [-s cmd] [-t timeo] tty_device\n", +#endif +#ifdef STREAM + "usage: %s [-b bps] [-c magic1] [-c magic2]... [-f] [-s cmd] [-t timeo] tty_device\n", +#endif + progname); + exit(2); + } + +#ifdef STREAM + if (!strlen(magic)) + strcpy(magic,DEFMAGIC); +#endif + + if (docmd) + fd = open(argv[ntp_optind], O_RDWR, 0777); + else + fd = open(argv[ntp_optind], O_RDONLY, 0777); + if (fd == -1) { + (void) fprintf(stderr, "%s: open(%s): ", progname, + argv[ntp_optind]); + perror(""); + exit(1); + } + + if (ioctl(fd, TIOCEXCL, (char *)0) < 0) { + (void) fprintf(stderr, "%s: ioctl(TIOCEXCL): ", progname); + perror(""); + exit(1); + } + + /* + * If we have the clock discipline, set the port to raw. Otherwise + * we run cooked. + */ + ttyb.sg_ispeed = ttyb.sg_ospeed = speed; +#ifdef CLKLDISC + ttyb.sg_erase = (char)magic1; + ttyb.sg_kill = (char)magic2; +#endif + ttyb.sg_flags = (short)ttflags; + if (ioctl(fd, TIOCSETP, (char *)&ttyb) < 0) { + (void) fprintf(stderr, "%s: ioctl(TIOCSETP): ", progname); + perror(""); + exit(1); + } + + if (fcntl(fd, F_SETOWN, getpid()) == -1) { + (void) fprintf(stderr, "%s: fcntl(F_SETOWN): ", progname); + perror(""); + exit(1); + } + +#ifdef CLKLDISC + { + int ldisc; + ldisc = CLKLDISC; + if (ioctl(fd, TIOCSETD, (char *)&ldisc) < 0) { + (void) fprintf(stderr, "%s: ioctl(TIOCSETD): ", progname); + perror(""); + exit(1); + } + } +#endif +#ifdef STREAM + if (ioctl(fd, I_POP, 0) >=0 ) ; + if (ioctl(fd, I_PUSH, "clk") < 0) { + (void) fprintf(stderr, "%s: ioctl(I_PUSH): ", progname); + perror(""); + exit(1); + } + if (ioctl(fd, CLK_SETSTR, magic) < 0) { + (void) fprintf(stderr, "%s: ioctl(CLK_SETSTR): ", progname); + perror(""); + exit(1); + } +#endif + + + (void) gettimeofday(&lasttv, (struct timezone *)0); + if (docmd) { + /* + * set non-blocking, async I/O on the descriptor + */ + iosig = 0; + (void) signal(SIGIO, ioready); + if (fcntl(fd, F_SETFL, FNDELAY|FASYNC) < 0) { + (void) fprintf(stderr, "%s: fcntl(F_SETFL): ", + progname); + perror(""); + exit(1); + } + + /* + * Set up the alarm interrupt. + */ + wasalarmed = 0; + (void) signal(SIGALRM, alarming); + itimer.it_interval = itimer.it_value = timeout; + setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0); + doboth(fd); + } + doioonly(fd); +} + + +/* + * doboth - handle both I/O and alarms via SIGIO + */ +int +doboth( + int fd + ) +{ + int n; + int sawalarm; + int sawiosig; + int omask; + fd_set fds; + struct timeval tvzero; + + sawalarm = 0; + sawiosig = 0; + FD_ZERO(&fds); + for (;;) { + omask = sigblock(BLOCKSIGMASK); + if (wasalarmed) { /* alarmed? */ + sawalarm = 1; + wasalarmed = 0; + } + if (iosig) { + sawiosig = 1; + iosig = 0; + } + + if (!sawalarm && !sawiosig) { + /* + * Nothing to do. Wait for something. + */ + sigpause(omask); + if (wasalarmed) { /* alarmed? */ + sawalarm = 1; + wasalarmed = 0; + } + if (iosig) { + sawiosig = 1; + iosig = 0; + } + } + (void)sigsetmask(omask); + + if (sawiosig) { + + do { + tvzero.tv_sec = tvzero.tv_usec = 0; + FD_SET(fd, &fds); + n = select(fd+1, &fds, (fd_set *)0, + (fd_set *)0, &tvzero); + if (n > 0) + doio(fd); + } while (n > 0); + + if (n == -1) { + (void) fprintf(stderr, "%s: select: ", + progname); + perror(""); + exit(1); + } + sawiosig = 0; + } + if (sawalarm) { + doalarm(fd); + sawalarm = 0; + } + } +} + + +/* + * doioonly - do I/O. This avoids the use of signals + */ +int +doioonly( + int fd + ) +{ + int n; + fd_set fds; + + FD_ZERO(&fds); + for (;;) { + FD_SET(fd, &fds); + n = select(fd+1, &fds, (fd_set *)0, (fd_set *)0, + (struct timeval *)0); + if (n > 0) + doio(fd); + } +} + + +/* + * doio - read a buffer full of stuff and print it out + */ +int +doio( + int fd + ) +{ + register char *rp, *rpend; + register char *cp; + register int i; + char raw[512]; + struct timeval tv, tvd; + int rlen; + int ind; + char cooked[2049]; + static char *digits = "0123456789abcdef"; + + rlen = read(fd, raw, sizeof(raw)); + if (rlen < 0) { + (void) fprintf(stderr, "%s: read(): ", progname); + perror(""); + return; + } + if (rlen == 0) { + (void) printf("Zero length read\n"); + return; + } + + cp = cooked; + rp = raw; + rpend = &raw[rlen]; + ind = 0; + + while (rp < rpend) { + ind = 1; + if (isprint(*rp)) + *cp++ = *rp; + else { + *cp++ = '<'; + *cp++ = digits[((*rp)>>4) & 0xf]; + *cp++ = digits[*rp & 0xf]; + *cp++ = '>'; + } + if ( +#ifdef CLKLDISC + (*rp == (char)magic1 || *rp == (char)magic2) +#else + ( strchr( magic, *rp) != NULL ) +#endif + ) { + rp++; + ind = 0; + *cp = '\0'; + if ((rpend - rp) < sizeof(struct timeval)) { + (void)printf( + "Too little data (%d): %s\n", + rpend-rp, cooked); + return; + } + + tv.tv_sec = 0; + for (i = 0; i < 4; i++) { + tv.tv_sec <<= 8; + tv.tv_sec |= ((long)*rp++) & 0xff; + } + tv.tv_usec = 0; + for (i = 0; i < 4; i++) { + tv.tv_usec <<= 8; + tv.tv_usec |= ((long)*rp++) & 0xff; + } + + tvd.tv_sec = tv.tv_sec - lasttv.tv_sec; + tvd.tv_usec = tv.tv_usec - lasttv.tv_usec; + if (tvd.tv_usec < 0) { + tvd.tv_usec += 1000000; + tvd.tv_sec--; + } + + (void)printf("%lu.%06lu %lu.%06lu %s\n", + tv.tv_sec, tv.tv_usec, tvd.tv_sec, tvd.tv_usec, + cooked); + lasttv = tv; + } else { + rp++; + } + } + + if (ind) { + *cp = '\0'; + (void)printf("Incomplete data: %s\n", cooked); + } +} + + +/* + * doalarm - send a string out the port, if we have one. + */ +int +doalarm( + int fd + ) +{ + int n; + + if (cmd == NULL || cmdlen <= 0) + return; + + n = write(fd, cmd, cmdlen); + + if (n < 0) { + (void) fprintf(stderr, "%s: write(): ", progname); + perror(""); + } else if (n < cmdlen) { + (void) printf("Short write (%d bytes, should be %d)\n", + n, cmdlen); + } +} + + +/* + * alarming - receive alarm interupt + */ +void +alarming(void) +{ + wasalarmed = 1; +} + +/* + * ioready - handle SIGIO interrupt + */ +void +ioready(void) +{ + iosig = 1; +} diff --git a/contrib/ntp/clockstuff/propdelay.c b/contrib/ntp/clockstuff/propdelay.c new file mode 100644 index 000000000000..3ce571c2510e --- /dev/null +++ b/contrib/ntp/clockstuff/propdelay.c @@ -0,0 +1,544 @@ +/* propdelay.c,v 3.1 1993/07/06 01:05:24 jbj Exp + * propdelay - compute propagation delays + * + * cc -o propdelay propdelay.c -lm + * + * "Time and Frequency Users' Manual", NBS Technical Note 695 (1977). + */ + +/* + * This can be used to get a rough idea of the HF propagation delay + * between two points (usually between you and the radio station). + * The usage is + * + * propdelay latitudeA longitudeA latitudeB longitudeB + * + * where points A and B are the locations in question. You obviously + * need to know the latitude and longitude of each of the places. + * The program expects the latitude to be preceded by an 'n' or 's' + * and the longitude to be preceded by an 'e' or 'w'. It understands + * either decimal degrees or degrees:minutes:seconds. Thus to compute + * the delay between the WWVH (21:59:26N, 159:46:00W) and WWV (40:40:49N, + * 105:02:27W) you could use: + * + * propdelay n21:59:26 w159:46 n40:40:49 w105:02:27 + * + * By default it prints out a summer (F2 average virtual height 350 km) and + * winter (F2 average virtual height 250 km) number. The results will be + * quite approximate but are about as good as you can do with HF time anyway. + * You might pick a number between the values to use, or use the summer + * value in the summer and switch to the winter value when the static + * above 10 MHz starts to drop off in the fall. You can also use the + * -h switch if you want to specify your own virtual height. + * + * You can also do a + * + * propdelay -W n45:17:47 w75:45:22 + * + * to find the propagation delays to WWV and WWVH (from CHU in this + * case), a + * + * propdelay -C n40:40:49 w105:02:27 + * + * to find the delays to CHU, and a + * + * propdelay -G n52:03:17 w98:34:18 + * + * to find delays to GOES via each of the three satellites. + */ + +#include <stdio.h> +#include <string.h> + +#include "ntp_stdlib.h" + +extern double sin (double); +extern double cos (double); +extern double acos (double); +extern double tan (double); +extern double atan (double); +extern double sqrt (double); + +#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) + +/* + * Program constants + */ +#define EARTHRADIUS (6370.0) /* raduis of earth (km) */ +#define LIGHTSPEED (299800.0) /* speed of light, km/s */ +#define PI (3.1415926536) +#define RADPERDEG (PI/180.0) /* radians per degree */ +#define MILE (1.609344) /* km in a mile */ + +#define SUMMERHEIGHT (350.0) /* summer height in km */ +#define WINTERHEIGHT (250.0) /* winter height in km */ + +#define SATHEIGHT (6.6110 * 6378.0) /* geosync satellite height in km + from centre of earth */ + +#define WWVLAT "n40:40:49" +#define WWVLONG "w105:02:27" + +#define WWVHLAT "n21:59:26" +#define WWVHLONG "w159:46:00" + +#define CHULAT "n45:17:47" +#define CHULONG "w75:45:22" + +#define GOES_UP_LAT "n37:52:00" +#define GOES_UP_LONG "w75:27:00" +#define GOES_EAST_LONG "w75:00:00" +#define GOES_STBY_LONG "w105:00:00" +#define GOES_WEST_LONG "w135:00:00" +#define GOES_SAT_LAT "n00:00:00" + +char *wwvlat = WWVLAT; +char *wwvlong = WWVLONG; + +char *wwvhlat = WWVHLAT; +char *wwvhlong = WWVHLONG; + +char *chulat = CHULAT; +char *chulong = CHULONG; + +char *goes_up_lat = GOES_UP_LAT; +char *goes_up_long = GOES_UP_LONG; +char *goes_east_long = GOES_EAST_LONG; +char *goes_stby_long = GOES_STBY_LONG; +char *goes_west_long = GOES_WEST_LONG; +char *goes_sat_lat = GOES_SAT_LAT; + +int hflag = 0; +int Wflag = 0; +int Cflag = 0; +int Gflag = 0; +int height; + +char *progname; +int debug; + +static void doit (double, double, double, double, double, char *); +static double latlong (char *, int); +static double greatcircle (double, double, double, double); +static double waveangle (double, double, int); +static double propdelay (double, double, int); +static int finddelay (double, double, double, double, double, double *); +static void satdoit (double, double, double, double, double, double, char *); +static void satfinddelay (double, double, double, double, double *); +static double satpropdelay (double); + +/* + * main - parse arguments and handle options + */ +int +main( + int argc, + char *argv[] + ) +{ + int c; + int errflg = 0; + double lat1, long1; + double lat2, long2; + double lat3, long3; + + progname = argv[0]; + while ((c = ntp_getopt(argc, argv, "dh:CWG")) != EOF) + switch (c) { + case 'd': + ++debug; + break; + case 'h': + hflag++; + height = atof(ntp_optarg); + if (height <= 0.0) { + (void) fprintf(stderr, "height %s unlikely\n", + ntp_optarg); + errflg++; + } + break; + case 'C': + Cflag++; + break; + case 'W': + Wflag++; + break; + case 'G': + Gflag++; + break; + default: + errflg++; + break; + } + if (errflg || (!(Cflag || Wflag || Gflag) && ntp_optind+4 != argc) || + ((Cflag || Wflag || Gflag) && ntp_optind+2 != argc)) { + (void) fprintf(stderr, + "usage: %s [-d] [-h height] lat1 long1 lat2 long2\n", + progname); + (void) fprintf(stderr," - or -\n"); + (void) fprintf(stderr, + "usage: %s -CWG [-d] lat long\n", + progname); + exit(2); + } + + + if (!(Cflag || Wflag || Gflag)) { + lat1 = latlong(argv[ntp_optind], 1); + long1 = latlong(argv[ntp_optind + 1], 0); + lat2 = latlong(argv[ntp_optind + 2], 1); + long2 = latlong(argv[ntp_optind + 3], 0); + if (hflag) { + doit(lat1, long1, lat2, long2, height, ""); + } else { + doit(lat1, long1, lat2, long2, (double)SUMMERHEIGHT, + "summer propagation, "); + doit(lat1, long1, lat2, long2, (double)WINTERHEIGHT, + "winter propagation, "); + } + } else if (Wflag) { + /* + * Compute delay from WWV + */ + lat1 = latlong(argv[ntp_optind], 1); + long1 = latlong(argv[ntp_optind + 1], 0); + lat2 = latlong(wwvlat, 1); + long2 = latlong(wwvlong, 0); + if (hflag) { + doit(lat1, long1, lat2, long2, height, "WWV "); + } else { + doit(lat1, long1, lat2, long2, (double)SUMMERHEIGHT, + "WWV summer propagation, "); + doit(lat1, long1, lat2, long2, (double)WINTERHEIGHT, + "WWV winter propagation, "); + } + + /* + * Compute delay from WWVH + */ + lat2 = latlong(wwvhlat, 1); + long2 = latlong(wwvhlong, 0); + if (hflag) { + doit(lat1, long1, lat2, long2, height, "WWVH "); + } else { + doit(lat1, long1, lat2, long2, (double)SUMMERHEIGHT, + "WWVH summer propagation, "); + doit(lat1, long1, lat2, long2, (double)WINTERHEIGHT, + "WWVH winter propagation, "); + } + } else if (Cflag) { + lat1 = latlong(argv[ntp_optind], 1); + long1 = latlong(argv[ntp_optind + 1], 0); + lat2 = latlong(chulat, 1); + long2 = latlong(chulong, 0); + if (hflag) { + doit(lat1, long1, lat2, long2, height, "CHU "); + } else { + doit(lat1, long1, lat2, long2, (double)SUMMERHEIGHT, + "CHU summer propagation, "); + doit(lat1, long1, lat2, long2, (double)WINTERHEIGHT, + "CHU winter propagation, "); + } + } else if (Gflag) { + lat1 = latlong(goes_up_lat, 1); + long1 = latlong(goes_up_long, 0); + lat3 = latlong(argv[ntp_optind], 1); + long3 = latlong(argv[ntp_optind + 1], 0); + + lat2 = latlong(goes_sat_lat, 1); + + long2 = latlong(goes_west_long, 0); + satdoit(lat1, long1, lat2, long2, lat3, long3, + "GOES Delay via WEST"); + + long2 = latlong(goes_stby_long, 0); + satdoit(lat1, long1, lat2, long2, lat3, long3, + "GOES Delay via STBY"); + + long2 = latlong(goes_east_long, 0); + satdoit(lat1, long1, lat2, long2, lat3, long3, + "GOES Delay via EAST"); + + } + exit(0); +} + + +/* + * doit - compute a delay and print it + */ +static void +doit( + double lat1, + double long1, + double lat2, + double long2, + double h, + char *str + ) +{ + int hops; + double delay; + + hops = finddelay(lat1, long1, lat2, long2, h, &delay); + printf("%sheight %g km, hops %d, delay %g seconds\n", + str, h, hops, delay); +} + + +/* + * latlong - decode a latitude/longitude value + */ +static double +latlong( + char *str, + int islat + ) +{ + register char *cp; + register char *bp; + double arg; + double div; + int isneg; + char buf[32]; + char *colon; + + if (islat) { + /* + * Must be north or south + */ + if (*str == 'N' || *str == 'n') + isneg = 0; + else if (*str == 'S' || *str == 's') + isneg = 1; + else + isneg = -1; + } else { + /* + * East is positive, west is negative + */ + if (*str == 'E' || *str == 'e') + isneg = 0; + else if (*str == 'W' || *str == 'w') + isneg = 1; + else + isneg = -1; + } + + if (isneg >= 0) + str++; + + colon = strchr(str, ':'); + if (colon != NULL) { + /* + * in hhh:mm:ss form + */ + cp = str; + bp = buf; + while (cp < colon) + *bp++ = *cp++; + *bp = '\0'; + cp++; + arg = atof(buf); + div = 60.0; + colon = strchr(cp, ':'); + if (colon != NULL) { + bp = buf; + while (cp < colon) + *bp++ = *cp++; + *bp = '\0'; + cp++; + arg += atof(buf) / div; + div = 3600.0; + } + if (*cp != '\0') + arg += atof(cp) / div; + } else { + arg = atof(str); + } + + if (isneg == 1) + arg = -arg; + + if (debug > 2) + (void) printf("latitude/longitude %s = %g\n", str, arg); + + return arg; +} + + +/* + * greatcircle - compute the great circle distance in kilometers + */ +static double +greatcircle( + double lat1, + double long1, + double lat2, + double long2 + ) +{ + double dg; + double l1r, l2r; + + l1r = lat1 * RADPERDEG; + l2r = lat2 * RADPERDEG; + dg = EARTHRADIUS * acos( + (cos(l1r) * cos(l2r) * cos((long2-long1)*RADPERDEG)) + + (sin(l1r) * sin(l2r))); + if (debug >= 2) + printf( + "greatcircle lat1 %g long1 %g lat2 %g long2 %g dist %g\n", + lat1, long1, lat2, long2, dg); + return dg; +} + + +/* + * waveangle - compute the wave angle for the given distance, virtual + * height and number of hops. + */ +static double +waveangle( + double dg, + double h, + int n + ) +{ + double theta; + double delta; + + theta = dg / (EARTHRADIUS * (double)(2 * n)); + delta = atan((h / (EARTHRADIUS * sin(theta))) + tan(theta/2)) - theta; + if (debug >= 2) + printf("waveangle dist %g height %g hops %d angle %g\n", + dg, h, n, delta / RADPERDEG); + return delta; +} + + +/* + * propdelay - compute the propagation delay + */ +static double +propdelay( + double dg, + double h, + int n + ) +{ + double phi; + double theta; + double td; + + theta = dg / (EARTHRADIUS * (double)(2 * n)); + phi = (PI/2.0) - atan((h / (EARTHRADIUS * sin(theta))) + tan(theta/2)); + td = dg / (LIGHTSPEED * sin(phi)); + if (debug >= 2) + printf("propdelay dist %g height %g hops %d time %g\n", + dg, h, n, td); + return td; +} + + +/* + * finddelay - find the propagation delay + */ +static int +finddelay( + double lat1, + double long1, + double lat2, + double long2, + double h, + double *delay + ) +{ + double dg; /* great circle distance */ + double delta; /* wave angle */ + int n; /* number of hops */ + + dg = greatcircle(lat1, long1, lat2, long2); + if (debug) + printf("great circle distance %g km %g miles\n", dg, dg/MILE); + + n = 1; + while ((delta = waveangle(dg, h, n)) < 0.0) { + if (debug) + printf("tried %d hop%s, no good\n", n, n>1?"s":""); + n++; + } + if (debug) + printf("%d hop%s okay, wave angle is %g\n", n, n>1?"s":"", + delta / RADPERDEG); + + *delay = propdelay(dg, h, n); + return n; +} + +/* + * satdoit - compute a delay and print it + */ +static void +satdoit( + double lat1, + double long1, + double lat2, + double long2, + double lat3, + double long3, + char *str + ) +{ + double up_delay,down_delay; + + satfinddelay(lat1, long1, lat2, long2, &up_delay); + satfinddelay(lat3, long3, lat2, long2, &down_delay); + + printf("%s, delay %g seconds\n", str, up_delay + down_delay); +} + +/* + * satfinddelay - calculate the one-way delay time between a ground station + * and a satellite + */ +static void +satfinddelay( + double lat1, + double long1, + double lat2, + double long2, + double *delay + ) +{ + double dg; /* great circle distance */ + + dg = greatcircle(lat1, long1, lat2, long2); + + *delay = satpropdelay(dg); +} + +/* + * satpropdelay - calculate the one-way delay time between a ground station + * and a satellite + */ +static double +satpropdelay( + double dg + ) +{ + double k1, k2, dist; + double theta; + double td; + + theta = dg / (EARTHRADIUS); + k1 = EARTHRADIUS * sin(theta); + k2 = SATHEIGHT - (EARTHRADIUS * cos(theta)); + if (debug >= 2) + printf("Theta %g k1 %g k2 %g\n", theta, k1, k2); + dist = sqrt(k1*k1 + k2*k2); + td = dist / LIGHTSPEED; + if (debug >= 2) + printf("propdelay dist %g height %g time %g\n", dg, dist, td); + return td; +} |