⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 refclock_datum.c

📁 网络时间协议NTP 源码 版本v4.2.0b 该源码用于linux平台下
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** refclock_datum - clock driver for the Datum Programmable Time Server**** Important note: This driver assumes that you have termios. If you have** a system that does not have termios, you will have to modify this driver.**** Sorry, I have only tested this driver on SUN and HP platforms.*/#ifdef HAVE_CONFIG_H# include <config.h>#endif#if defined(REFCLOCK) && defined(CLOCK_DATUM)/*** Include Files*/#include "ntpd.h"#include "ntp_io.h"#include "ntp_refclock.h"#include "ntp_unixtime.h"#include "ntp_stdlib.h"#include <stdio.h>#include <ctype.h>#if defined(HAVE_BSD_TTYS)#include <sgtty.h>#endif /* HAVE_BSD_TTYS */#if defined(HAVE_SYSV_TTYS)#include <termio.h>#endif /* HAVE_SYSV_TTYS */#if defined(HAVE_TERMIOS)#include <termios.h>#endif#if defined(STREAM)#include <stropts.h>#if defined(WWVBCLK)#include <sys/clkdefs.h>#endif /* WWVBCLK */#endif /* STREAM */#include "ntp_stdlib.h"/*** This driver supports the Datum Programmable Time System (PTS) clock.** The clock works in very straight forward manner. When it receives a** time code request (e.g., the ascii string "//k/mn"), it responds with** a seven byte BCD time code. This clock only responds with a** time code after it first receives the "//k/mn" message. It does not** periodically send time codes back at some rate once it is started.** the returned time code can be broken down into the following fields.****            _______________________________** Bit Index | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |**            ===============================** byte 0:   | -   -   -   - |      H D      |**            ===============================** byte 1:   |      T D      |      U D      |**            ===============================** byte 2:   | -   - |  T H  |      U H      |**            ===============================** byte 3:   | - |    T M    |      U M      |**            ===============================** byte 4:   | - |    T S    |      U S      |**            ===============================** byte 5:   |      t S      |      h S      |**            ===============================** byte 6:   |      m S      | -   -   -   - |**            ===============================**** In the table above:****	"-" means don't care**	"H D", "T D", and "U D" means Hundreds, Tens, and Units of Days**	"T H", and "UH" means Tens and Units of Hours**	"T M", and "U M" means Tens and Units of Minutes**	"T S", and "U S" means Tens and Units of Seconds**	"t S", "h S", and "m S" means tenths, hundredths, and thousandths**				of seconds**** The Datum PTS communicates throught the RS232 port on your machine.** Right now, it assumes that you have termios. This driver has been tested** on SUN and HP workstations. The Datum PTS supports various IRIG and** NASA input codes. This driver assumes that the name of the device is** /dev/datum. You will need to make a soft link to your RS232 device or** create a new driver to use this refclock.*//*** Datum PTS defines*//*** Note that if GMT is defined, then the Datum PTS must use Greenwich** time. Otherwise, this driver allows the Datum PTS to use the current** wall clock for its time. It determines the time zone offset by minimizing** the error after trying several time zone offsets. If the Datum PTS** time is Greenwich time and GMT is not defined, everything should still** work since the time zone will be found to be 0. What this really means** is that your system time (at least to start with) must be within the** correct time by less than +- 30 minutes. The default is for GMT to not** defined. If you really want to force GMT without the funny +- 30 minute** stuff then you must define (uncomment) GMT below.*//*#define GMT#define DEBUG_DATUM_PTC#define LOG_TIME_ERRORS*/#define	PRECISION	(-10)		/* precision assumed 1/1024 ms */#define	REFID "DATM"			/* reference id */#define DATUM_DISPERSION 0		/* fixed dispersion = 0 ms */#define DATUM_MAX_ERROR 0.100		/* limits on sigma squared */#define DATUM_MAX_ERROR2 (DATUM_MAX_ERROR*DATUM_MAX_ERROR)/*** The Datum PTS structure*//*** I don't use a fixed array of MAXUNITS like everyone else just because** I don't like to program that way. Sorry if this bothers anyone. I assume** that you can use any id for your unit and I will search for it in a** dynamic array of units until I find it. I was worried that users might** enter a bad id in their configuration file (larger than MAXUNITS) and** besides, it is just cleaner not to have to assume that you have a fixed** number of anything in a program.*/struct datum_pts_unit {	struct peer *peer;		/* peer used by ntp */	struct refclockio io;		/* io structure used by ntp */	int PTS_fd;			/* file descriptor for PTS */	u_int unit;			/* id for unit */	u_long timestarted;		/* time started */	l_fp lastrec;			/* time tag for the receive time (system) */	l_fp lastref;			/* reference time (Datum time) */	u_long yearstart;		/* the year that this clock started */	int coderecv;			/* number of time codes received */	int day;			/* day */	int hour;			/* hour */	int minute;			/* minutes */	int second;			/* seconds */	int msec;			/* miliseconds */	int usec;			/* miliseconds */	u_char leap;			/* funny leap character code */	char retbuf[8];		/* returned time from the datum pts */	char nbytes;			/* number of bytes received from datum pts */ 	double sigma2;		/* average squared error (roughly) */	int tzoff;			/* time zone offest from GMT */};/*** PTS static constant variables for internal use*/static char TIME_REQUEST[6];	/* request message sent to datum for time */static int nunits;		/* number of active units */static struct datum_pts_unit**datum_pts_unit;	/* dynamic array of datum PTS structures *//*** Callback function prototypes that ntpd needs to know about.*/static	int	datum_pts_start		P((int, struct peer *));static	void	datum_pts_shutdown	P((int, struct peer *));static	void	datum_pts_poll		P((int, struct peer *));static	void	datum_pts_control	P((int, struct refclockstat *,					   struct refclockstat *, struct peer *));static	void	datum_pts_init		P((void));static	void	datum_pts_buginfo	P((int, struct refclockbug *, struct peer *));/*** This is the call back function structure that ntpd actually uses for** this refclock.*/struct	refclock refclock_datum = {	datum_pts_start,		/* start up a new Datum refclock */	datum_pts_shutdown,		/* shutdown a Datum refclock */	datum_pts_poll,		/* sends out the time request */	datum_pts_control,		/* not used */	datum_pts_init,		/* initialization (called first) */	datum_pts_buginfo,		/* not used */	NOFLAGS			/* we are not setting any special flags */};/*** The datum_pts_receive callback function is handled differently from the** rest. It is passed to the ntpd io data structure. Basically, every** 64 seconds, the datum_pts_poll() routine is called. It sends out the time** request message to the Datum Programmable Time System. Then, ntpd** waits on a select() call to receive data back. The datum_pts_receive()** function is called as data comes back. We expect a seven byte time** code to be returned but the datum_pts_receive() function may only get** a few bytes passed to it at a time. In other words, this routine may** get called by the io stuff in ntpd a few times before we get all seven** bytes. Once the last byte is received, we process it and then pass the** new time measurement to ntpd for updating the system time. For now,** there is no 3 state filtering done on the time measurements. The** jitter may be a little high but at least for its current use, it is not** a problem. We have tried to keep things as simple as possible. This** clock should not jitter more than 1 or 2 mseconds at the most once** things settle down. It is important to get the right drift calibrated** in the ntpd.drift file as well as getting the right tick set up right** using tickadj for SUNs. Tickadj is not used for the HP but you need to** remember to bring up the adjtime daemon because HP does not support** the adjtime() call.*/static	void	datum_pts_receive	P((struct recvbuf *));/*......................................................................*//*	datum_pts_start - start up the datum PTS. This means open the	*//*	RS232 device and set up the data structure for my unit.		*//*......................................................................*/static intdatum_pts_start(	int unit,	struct peer *peer	){	struct datum_pts_unit **temp_datum_pts_unit;	struct datum_pts_unit *datum_pts;#ifdef HAVE_TERMIOS	struct termios arg;#endif#ifdef DEBUG_DATUM_PTC	if (debug)	    printf("Starting Datum PTS unit %d\n", unit);#endif	/*	** Create the memory for the new unit	*/	temp_datum_pts_unit = (struct datum_pts_unit **)		malloc((nunits+1)*sizeof(struct datum_pts_unit *));	if (nunits > 0) memcpy(temp_datum_pts_unit, datum_pts_unit,			       nunits*sizeof(struct datum_pts_unit *));	free(datum_pts_unit);	datum_pts_unit = temp_datum_pts_unit;	datum_pts_unit[nunits] = (struct datum_pts_unit *)		malloc(sizeof(struct datum_pts_unit));	datum_pts = datum_pts_unit[nunits];	datum_pts->unit = unit;	/* set my unit id */	datum_pts->yearstart = 0;	/* initialize the yearstart to 0 */	datum_pts->sigma2 = 0.0;	/* initialize the sigma2 to 0 */	/*	** Open the Datum PTS device	*/	datum_pts->PTS_fd = open("/dev/datum",O_RDWR);	fcntl(datum_pts->PTS_fd, F_SETFL, 0); /* clear the descriptor flags */#ifdef DEBUG_DATUM_PTC	if (debug)	    printf("Opening RS232 port with file descriptor %d\n",		   datum_pts->PTS_fd);#endif	/*	** Set up the RS232 terminal device information. Note that we assume that	** we have termios. This code has only been tested on SUNs and HPs. If your	** machine does not have termios this driver cannot be initialized. You can change this	** if you want by editing this source. Please give the changes back to the	** ntp folks so that it can become part of their regular distribution.	*/#ifdef HAVE_TERMIOS	arg.c_iflag = IGNBRK;	arg.c_oflag = 0;	arg.c_cflag = B9600 | CS8 | CREAD | PARENB | CLOCAL;	arg.c_lflag = 0;	arg.c_cc[VMIN] = 0;		/* start timeout timer right away (not used) */	arg.c_cc[VTIME] = 30;		/* 3 second timout on reads (not used) */	tcsetattr(datum_pts->PTS_fd, TCSANOW, &arg);#else	msyslog(LOG_ERR, "Datum_PTS: Termios not supported in this driver");	(void)close(datum_pts->PTS_fd);	peer->precision = PRECISION;	pp->clockdesc = DESCRIPTION;	memcpy((char *)&pp->refid, REFID, 4);	return 0;#endif	/*	** Initialize the ntpd IO structure	*/	datum_pts->peer = peer;	datum_pts->io.clock_recv = datum_pts_receive;	datum_pts->io.srcclock = (caddr_t)datum_pts;	datum_pts->io.datalen = 0;	datum_pts->io.fd = datum_pts->PTS_fd;	if (!io_addclock(&(datum_pts->io))) {#ifdef DEBUG_DATUM_PTC		if (debug)		    printf("Problem adding clock\n");#endif		msyslog(LOG_ERR, "Datum_PTS: Problem adding clock");		(void)close(datum_pts->PTS_fd);		return 0;	}	/*	** Now add one to the number of units and return a successful code	*/	nunits++;	return 1;}/*......................................................................*//*	datum_pts_shutdown - this routine shuts doen the device and	*//*	removes the memory for the unit.				*//*......................................................................*/static voiddatum_pts_shutdown(	int unit,	struct peer *peer	){	int i,j;	struct datum_pts_unit **temp_datum_pts_unit;#ifdef DEBUG_DATUM_PTC	if (debug)	    printf("Shutdown Datum PTS\n");#endif	msyslog(LOG_ERR, "Datum_PTS: Shutdown Datum PTS");	/*	** First we have to find the right unit (i.e., the one with the same id).	** We do this by looping through the dynamic array of units intil we find	** it. Note, that I don't simply use an array with a maximimum number of	** Datum PTS units. Everything is completely dynamic.	*/	for (i=0; i<nunits; i++) {		if (datum_pts_unit[i]->unit == unit) {			/*			** We found the unit so close the file descriptor and free up the memory used			** by the structure.			*/			io_closeclock(&datum_pts_unit[i]->io);			close(datum_pts_unit[i]->PTS_fd);			free(datum_pts_unit[i]);			/*			** Now clean up the datum_pts_unit dynamic array so that there are no holes.			** This may mean moving pointers around, etc., to keep things compact.			*/			if (nunits > 1) {				temp_datum_pts_unit = (struct datum_pts_unit **)					malloc((nunits-1)*sizeof(struct datum_pts_unit *));				if (i!= 0) memcpy(temp_datum_pts_unit, datum_pts_unit,						  i*sizeof(struct datum_pts_unit *));				for (j=i+1; j<nunits; j++) {					temp_datum_pts_unit[j-1] = datum_pts_unit[j];				}				free(datum_pts_unit);				datum_pts_unit = temp_datum_pts_unit;			}else{				free(datum_pts_unit);				datum_pts_unit = NULL;			}			return;		}	}#ifdef DEBUG_DATUM_PTC	if (debug)	    printf("Error, could not shut down unit %d\n",unit);#endif	msyslog(LOG_ERR, "Datum_PTS: Could not shut down Datum PTS unit %d",unit);}/*......................................................................*//*	datum_pts_poll - this routine sends out the time request to the *//*	Datum PTS device. The time will be passed back in the 		*//*	datum_pts_receive() routine.					*//*......................................................................*/static voiddatum_pts_poll(	int unit,	struct peer *peer	){	int i;	int unit_index;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -