refclock_as2201.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1996 by Sun Microsystems, Inc.
* All Rights Reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* refclock_as2201 - clock driver for the Austron 2201A GPS
* Timing Receiver
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_refclock.h"
#include "ntp_unixtime.h"
#include "ntp_stdlib.h"
#ifdef SYS_SOLARIS
#define CIOGETEV TIOCGPPSEV
#else
#ifdef PPS
#include <sys/ppsclock.h>
#endif /* PPS */
#endif /* not SYS_SOLARIS */
/*
* This driver supports the Austron 2200A/2201A GPS Receiver with
* Buffered RS-232-C Interface Module. Note that the original 2200/2201
* receivers will not work reliably with this driver, since the older
* design cannot accept input commands at any reasonable data rate.
*
* The program sends a "*toc\r" to the radio and expects a response of
* the form "yy:ddd:hh:mm:ss.mmm\r" where yy = year of century, ddd =
* day of year, hh:mm:ss = second of day and mmm = millisecond of
* second. Then, it sends statistics commands to the radio and expects
* a multi-line reply showing the corresponding statistics or other
* selected data. Statistics commands are sent in order as determined by
* a vector of commands; these might have to be changed with different
* radio options. If flag4 of the fudge configuration command is set to
* 1, the statistics data are written to the clockstats file for later
* processing.
*
* In order for this code to work, the radio must be placed in non-
* interactive mode using the "off" command and with a single <cr>
* resonse using the "term cr" command. The setting of the "echo"
* and "df" commands does not matter. The radio should select UTC
* timescale using the "ts utc" command.
*
* There are two modes of operation for this driver. The first with
* default configuration is used with stock kernels and serial-line
* drivers and works with almost any machine. In this mode the driver
* assumes the radio captures a timestamp upon receipt of the "*" that
* begins the driver query. Accuracies in this mode are in the order of
* a millisecond or two and the receiver can be connected to only one
* host.
*
* The second mode of operation can be used for SunOS kernels that have
* been modified with the ppsclock streams module included in this
* distribution. The mode is enabled if flag3 of the fudge configuration
* command has been set to 1. In this mode a precise timestamp is
* available using a gadget box and 1-pps signal from the receiver. This
* improves the accuracy to the order of a few tens of microseconds. In
* addition, the serial output and 1-pps signal can be bussed to more
* than one hosts, but only one of them should be connected to the
* radio input data line.
*/
/*
* GPS Definitions
*/
/*
* Imported from ntp_timer module
*/
/*
* Imported from ntpd module
*/
extern int debug; /* global debug flag */
#ifdef PPS
/*
* Imported from loop_filter module
*/
extern int fdpps; /* ppsclock file descriptor */
#endif /* PPS */
/*
* AS2201 unit control structure.
*/
struct as2201unit {
int pollcnt; /* poll message counter */
char *lastptr; /* statistics buffer pointer */
#ifdef PPS
#endif /* PPS */
int linect; /* count of lines remaining */
int index; /* current statistics command */
};
/*
* Radio commands to extract statitistics
*
* A command consists of an ASCII string terminated by a <cr> (\r). The
* command list consist of a sequence of commands terminated by a null
* string ("\0"). One command from the list is sent immediately
* following each received timecode (*toc\r command) and the ASCII
* strings received from the radio are saved along with the timecode in
* the clockstats file. Subsequent commands are sent at each timecode,
* with the last one in the list followed by the first one. The data
* received from the radio consist of ASCII strings, each terminated by
* a <cr> (\r) character. The number of strings for each command is
* specified as the first line of output as an ASCII-encode number. Note
* that the ETF command requires the Input Buffer Module and the LORAN
* commands require the LORAN Assist Module. However, if these modules
* are not installed, the radio and this driver will continue to operate
* successfuly, but no data will be captured for these commands.
*/
static char stat_command[][30] = {
"LORAN TDATA\r", /* LORAN signal data */
"ID;OPT;VER\r", /* model; options; software version */
"TRSTAT\r", /* satellite tracking status */
"POS;PPS;PPSOFF\r", /* position, pps source, offsets */
"LORAN TDATA\r", /* LORAN signal data */
"UTC\r", /* UTC leap info */
"TRSTAT\r", /* satellite tracking status */
"OSC;ET;TEMP\r", /* osc type; tune volts; oven temp */
"\0" /* end of table */
};
/*
* Function prototypes
*/
static int as2201_start P((int, struct peer *));
static void as2201_shutdown P((int, struct peer *));
static void as2201_receive P((struct recvbuf *));
static void as2201_poll P((int, struct peer *));
/*
* Transfer vector
*/
struct refclock refclock_as2201 = {
as2201_start, /* start up driver */
as2201_shutdown, /* shut down driver */
as2201_poll, /* transmit poll message */
noentry, /* not used (old as2201_control) */
noentry, /* initialize driver (not used) */
noentry, /* not used (old as2201_buginfo) */
NOFLAGS /* not used */
};
/*
* as2201_start - open the devices and initialize data for processing
*/
static int
int unit;
{
register struct as2201unit *up;
struct refclockproc *pp;
int fd;
char gpsdev[20];
/*
* Open serial port. Use CLK line discipline, if available.
*/
#ifdef TTYCLK
#else
#endif /* TTYCLK */
return (0);
/*
* Allocate and initialize unit structure
*/
if (!(up = (struct as2201unit *)
emalloc(sizeof(struct as2201unit)))) {
return (0);
}
return (0);
}
/*
* Initialize miscellaneous variables
*/
return (1);
}
/*
* as2201_shutdown - shut down the clock
*/
static void
int unit;
{
register struct as2201unit *up;
struct refclockproc *pp;
}
/*
* as2201__receive - receive data from the serial interface
*/
static void
{
register struct as2201unit *up;
struct refclockproc *pp;
#ifdef PPS
long ltemp;
struct ppsclockev ev;
#endif /* PPS */
/*
* Initialize pointers and read the timecode and timestamp.
*/
#ifdef DEBUG
if (debug)
printf("gps: timecode %d %d %s\n",
#endif
return;
/*
* If linect is greater than zero, we must be in the middle of a
* statistics operation, so simply tack the received data at the
* end of the statistics string. If not, we could either have
* just received the timecode itself or a decimal number
* indicating the number of following lines of the statistics
* reply. In the former case, write the accumulated statistics
* data to the clockstats file and continue onward to process
* the timecode; in the later case, save the number of lines and
* quietly return.
*/
return;
return;
} else {
return;
} else {
#ifdef DEBUG
if (debug)
#endif
}
}
/*
* We get down to business, check the timecode format and decode
* its contents. If the timecode has invalid length or is not in
* proper format, we declare bad format and exit.
*/
return;
}
/*
* Timecode format: "yy:ddd:hh:mm:ss.mmm"
*/
!= 6) {
return;
}
/*
* Test for synchronization (this is a temporary crock).
*/
} else {
}
#ifdef PPS
/*
* If CLK_FLAG3 is set and the local time is within +-0.5 second
* of the timecode, use the pps offset instead. Note that we
* believe the ppsclock timestamp only if the ioctl works and
* the new timestamp is greater than the previous one. If the
* new timestamp is not greater than the previous one, the
* PPS signal has faile, in which case declare a hardware
* error.
*/
return;
}
return;
}
}
}
}
#endif /* PPS */
#ifdef DEBUG
if (debug)
printf("gps: times %s %s %s\n",
#endif
/*
* Process the new sample in the median filter and determine the
* reference clock offset and dispersion. We use lastrec as both
* the reference time and receive time in order to avoid being
* cute, like setting the reference time later than the receive
* time, which may cause a paranoid protocol module to chuck out
* the data.
*/
return;
}
/*
* If CLK_FLAG4 is set, initialize the statistics buffer and
* send the next command. If not, simply write the timecode to
* the clockstats file.
*/
}
}
/*
* as2201_poll - called by the transmit procedure
*
* We go to great pains to avoid changing state here, since there may be
* more than one eavesdropper receiving the same timecode.
*/
static void
int unit;
{
register struct as2201unit *up;
struct refclockproc *pp;
/*
* Send a "\r*toc\r" to get things going. We go to great pains
* to avoid changing state, since there may be more than one
* eavesdropper watching the radio.
*/
else
} else
}
#else /* not (REFCLOCK && AS2201) */
int refclock_as2201_bs;
#endif /* not (REFCLOCK && AS2201) */