2 * refclock_as2201 - clock driver for the Austron 2201A GPS
9 #if defined(REFCLOCK) && defined(CLOCK_AS2201)
13 #include "ntp_refclock.h"
14 #include "ntp_unixtime.h"
15 #include "ntp_stdlib.h"
21 * This driver supports the Austron 2200A/2201A GPS Receiver with
22 * Buffered RS-232-C Interface Module. Note that the original 2200/2201
23 * receivers will not work reliably with this driver, since the older
24 * design cannot accept input commands at any reasonable data rate.
26 * The program sends a "*toc\r" to the radio and expects a response of
27 * the form "yy:ddd:hh:mm:ss.mmm\r" where yy = year of century, ddd =
28 * day of year, hh:mm:ss = second of day and mmm = millisecond of
29 * second. Then, it sends statistics commands to the radio and expects
30 * a multi-line reply showing the corresponding statistics or other
31 * selected data. Statistics commands are sent in order as determined by
32 * a vector of commands; these might have to be changed with different
33 * radio options. If flag4 of the fudge configuration command is set to
34 * 1, the statistics data are written to the clockstats file for later
37 * In order for this code to work, the radio must be placed in non-
38 * interactive mode using the "off" command and with a single <cr>
39 * response using the "term cr" command. The setting of the "echo"
40 * and "df" commands does not matter. The radio should select UTC
41 * timescale using the "ts utc" command.
43 * There are two modes of operation for this driver. The first with
44 * default configuration is used with stock kernels and serial-line
45 * drivers and works with almost any machine. In this mode the driver
46 * assumes the radio captures a timestamp upon receipt of the "*" that
47 * begins the driver query. Accuracies in this mode are in the order of
48 * a millisecond or two and the receiver can be connected to only one
51 * The second mode of operation can be used for SunOS kernels that have
52 * been modified with the ppsclock streams module included in this
53 * distribution. The mode is enabled if flag3 of the fudge configuration
54 * command has been set to 1. In this mode a precise timestamp is
55 * available using a gadget box and 1-pps signal from the receiver. This
56 * improves the accuracy to the order of a few tens of microseconds. In
57 * addition, the serial output and 1-pps signal can be bussed to more
58 * than one hosts, but only one of them should be connected to the
59 * radio input data line.
65 #define SMAX 200 /* statistics buffer length */
66 #define DEVICE "/dev/gps%d" /* device name and unit */
67 #define SPEED232 B9600 /* uart speed (9600 baud) */
68 #define PRECISION (-20) /* precision assumed (about 1 us) */
69 #define REFID "GPS\0" /* reference ID */
70 #define DESCRIPTION "Austron 2201A GPS Receiver" /* WRU */
72 #define LENTOC 19 /* yy:ddd:hh:mm:ss.mmm timecode lngth */
75 * AS2201 unit control structure.
78 char *lastptr; /* statistics buffer pointer */
79 char stats[SMAX]; /* statistics buffer */
80 int linect; /* count of lines remaining */
81 int index; /* current statistics command */
85 * Radio commands to extract statitistics
87 * A command consists of an ASCII string terminated by a <cr> (\r). The
88 * command list consist of a sequence of commands terminated by a null
89 * string ("\0"). One command from the list is sent immediately
90 * following each received timecode (*toc\r command) and the ASCII
91 * strings received from the radio are saved along with the timecode in
92 * the clockstats file. Subsequent commands are sent at each timecode,
93 * with the last one in the list followed by the first one. The data
94 * received from the radio consist of ASCII strings, each terminated by
95 * a <cr> (\r) character. The number of strings for each command is
96 * specified as the first line of output as an ASCII-encode number. Note
97 * that the ETF command requires the Input Buffer Module and the LORAN
98 * commands require the LORAN Assist Module. However, if these modules
99 * are not installed, the radio and this driver will continue to operate
100 * successfuly, but no data will be captured for these commands.
102 static char stat_command[][30] = {
103 "ITF\r", /* internal time/frequency */
104 "ETF\r", /* external time/frequency */
105 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */
106 "LORAN TDATA\r", /* LORAN signal data */
107 "ID;OPT;VER\r", /* model; options; software version */
109 "ITF\r", /* internal time/frequency */
110 "ETF\r", /* external time/frequency */
111 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */
112 "TRSTAT\r", /* satellite tracking status */
113 "POS;PPS;PPSOFF\r", /* position, pps source, offsets */
115 "ITF\r", /* internal time/frequency */
116 "ETF\r", /* external time/frequency */
117 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */
118 "LORAN TDATA\r", /* LORAN signal data */
119 "UTC\r", /* UTC leap info */
121 "ITF\r", /* internal time/frequency */
122 "ETF\r", /* external time/frequency */
123 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */
124 "TRSTAT\r", /* satellite tracking status */
125 "OSC;ET;TEMP\r", /* osc type; tune volts; oven temp */
126 "\0" /* end of table */
130 * Function prototypes
132 static int as2201_start (int, struct peer *);
133 static void as2201_shutdown (int, struct peer *);
134 static void as2201_receive (struct recvbuf *);
135 static void as2201_poll (int, struct peer *);
140 struct refclock refclock_as2201 = {
141 as2201_start, /* start up driver */
142 as2201_shutdown, /* shut down driver */
143 as2201_poll, /* transmit poll message */
144 noentry, /* not used (old as2201_control) */
145 noentry, /* initialize driver (not used) */
146 noentry, /* not used (old as2201_buginfo) */
147 NOFLAGS /* not used */
152 * as2201_start - open the devices and initialize data for processing
160 register struct as2201unit *up;
161 struct refclockproc *pp;
166 * Open serial port. Use CLK line discipline, if available.
168 snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit);
169 fd = refclock_open(gpsdev, SPEED232, LDISC_CLK);
174 * Allocate and initialize unit structure
176 up = emalloc_zero(sizeof(*up));
178 pp->io.clock_recv = as2201_receive;
179 pp->io.srcclock = peer;
182 if (!io_addclock(&pp->io)) {
191 * Initialize miscellaneous variables
193 peer->precision = PRECISION;
194 pp->clockdesc = DESCRIPTION;
195 memcpy((char *)&pp->refid, REFID, 4);
196 up->lastptr = up->stats;
203 * as2201_shutdown - shut down the clock
211 register struct as2201unit *up;
212 struct refclockproc *pp;
217 io_closeclock(&pp->io);
224 * as2201__receive - receive data from the serial interface
228 struct recvbuf *rbufp
231 register struct as2201unit *up;
232 struct refclockproc *pp;
238 * Initialize pointers and read the timecode and timestamp.
240 peer = rbufp->recv_peer;
243 pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
246 printf("gps: timecode %d %d %s\n",
247 up->linect, pp->lencode, pp->a_lastcode);
249 if (pp->lencode == 0)
253 * If linect is greater than zero, we must be in the middle of a
254 * statistics operation, so simply tack the received data at the
255 * end of the statistics string. If not, we could either have
256 * just received the timecode itself or a decimal number
257 * indicating the number of following lines of the statistics
258 * reply. In the former case, write the accumulated statistics
259 * data to the clockstats file and continue onward to process
260 * the timecode; in the later case, save the number of lines and
263 if (pp->sloppyclockflag & CLK_FLAG2)
265 if (up->linect > 0) {
267 if ((int)(up->lastptr - up->stats + pp->lencode) > SMAX - 2)
269 *up->lastptr++ = ' ';
270 memcpy(up->lastptr, pp->a_lastcode, 1 + pp->lencode);
271 up->lastptr += pp->lencode;
274 if (pp->lencode == 1) {
275 up->linect = atoi(pp->a_lastcode);
278 record_clock_stats(&peer->srcadr, up->stats);
281 printf("gps: stat %s\n", up->stats);
285 up->lastptr = up->stats;
289 * We get down to business, check the timecode format and decode
290 * its contents. If the timecode has invalid length or is not in
291 * proper format, we declare bad format and exit.
293 if (pp->lencode < LENTOC) {
294 refclock_report(peer, CEVNT_BADREPLY);
299 * Timecode format: "yy:ddd:hh:mm:ss.mmm"
301 if (sscanf(pp->a_lastcode, "%2d:%3d:%2d:%2d:%2d.%3ld", &pp->year,
302 &pp->day, &pp->hour, &pp->minute, &pp->second, &pp->nsec)
304 refclock_report(peer, CEVNT_BADREPLY);
310 * Test for synchronization (this is a temporary crock).
312 if (pp->a_lastcode[2] != ':')
313 pp->leap = LEAP_NOTINSYNC;
315 pp->leap = LEAP_NOWARNING;
318 * Process the new sample in the median filter and determine the
319 * timecode timestamp.
321 if (!refclock_process(pp)) {
322 refclock_report(peer, CEVNT_BADTIME);
327 * If CLK_FLAG4 is set, initialize the statistics buffer and
328 * send the next command. If not, simply write the timecode to
329 * the clockstats file.
331 if ((int)(up->lastptr - up->stats + pp->lencode) > SMAX - 2)
333 memcpy(up->lastptr, pp->a_lastcode, pp->lencode);
334 up->lastptr += pp->lencode;
335 if (pp->sloppyclockflag & CLK_FLAG4) {
336 octets = strlen(stat_command[up->index]);
337 if ((int)(up->lastptr - up->stats + 1 + octets) > SMAX - 2)
339 *up->lastptr++ = ' ';
340 memcpy(up->lastptr, stat_command[up->index], octets);
341 up->lastptr += octets - 1;
343 (void)write(pp->io.fd, stat_command[up->index],
344 strlen(stat_command[up->index]));
346 if (*stat_command[up->index] == '\0')
353 * as2201_poll - called by the transmit procedure
355 * We go to great pains to avoid changing state here, since there may be
356 * more than one eavesdropper receiving the same timecode.
364 struct refclockproc *pp;
367 * Send a "\r*toc\r" to get things going. We go to great pains
368 * to avoid changing state, since there may be more than one
369 * eavesdropper watching the radio.
372 if (write(pp->io.fd, "\r*toc\r", 6) != 6) {
373 refclock_report(peer, CEVNT_FAULT);
376 if (!(pp->sloppyclockflag & CLK_FLAG2))
377 get_systime(&pp->lastrec);
379 if (pp->coderecv == pp->codeproc) {
380 refclock_report(peer, CEVNT_TIMEOUT);
383 refclock_receive(peer);
387 int refclock_as2201_bs;
388 #endif /* REFCLOCK */