2 * refclock_arbiter - clock driver for Arbiter 1088A/B Satellite
10 #if defined(REFCLOCK) && defined(CLOCK_ARBITER)
14 #include "ntp_refclock.h"
15 #include "ntp_stdlib.h"
21 * This driver supports the Arbiter 1088A/B Satellite Controlled Clock.
22 * The claimed accuracy of this clock is 100 ns relative to the PPS
23 * output when receiving four or more satellites.
25 * The receiver should be configured before starting the NTP daemon, in
26 * order to establish reliable position and operating conditions. It
27 * does not initiate surveying or hold mode. For use with NTP, the
28 * daylight savings time feature should be disables (D0 command) and the
29 * broadcast mode set to operate in UTC (BU command).
31 * The timecode format supported by this driver is selected by the poll
32 * sequence "B5", which initiates a line in the following format to be
33 * repeated once per second until turned off by the "B0" poll sequence.
35 * Format B5 (24 ASCII printing characters):
37 * <cr><lf>i yy ddd hh:mm:ss.000bbb
40 * i = synchronization flag (' ' = locked, '?' = unlocked)
41 * yy = year of century
43 * hh:mm:ss = hours, minutes, seconds
44 * .000 = fraction of second (not used)
45 * bbb = tailing spaces for fill
47 * The alarm condition is indicated by a '?' at i, which indicates the
48 * receiver is not synchronized. In normal operation, a line consisting
49 * of the timecode followed by the time quality character (TQ) followed
50 * by the receiver status string (SR) is written to the clockstats file.
51 * The time quality character is encoded in IEEE P1344 standard:
53 * Format TQ (IEEE P1344 estimated worst-case time quality)
55 * 0 clock locked, maximum accuracy
56 * F clock failure, time not reliable
57 * 4 clock unlocked, accuracy < 1 us
58 * 5 clock unlocked, accuracy < 10 us
59 * 6 clock unlocked, accuracy < 100 us
60 * 7 clock unlocked, accuracy < 1 ms
61 * 8 clock unlocked, accuracy < 10 ms
62 * 9 clock unlocked, accuracy < 100 ms
63 * A clock unlocked, accuracy < 1 s
64 * B clock unlocked, accuracy < 10 s
66 * The status string is encoded as follows:
68 * Format SR (25 ASCII printing characters)
70 * V=vv S=ss T=t P=pdop E=ee
72 * vv = satellites visible
73 * ss = relative signal strength
74 * t = satellites tracked
75 * pdop = position dilution of precision (meters)
76 * ee = hardware errors
78 * If flag4 is set, an additional line consisting of the receiver
79 * latitude (LA), longitude (LO) and elevation (LH) (meters) is written
80 * to this file. If channel B is enabled for deviation mode and connected
81 * to a 1-PPS signal, the last two numbers on the line are the deviation
82 * and standard deviation averaged over the last 15 seconds.
86 * Interface definitions
88 #define DEVICE "/dev/gps%d" /* device name and unit */
89 #define SPEED232 B9600 /* uart speed (9600 baud) */
90 #define PRECISION (-20) /* precision assumed (about 1 us) */
91 #define REFID "GPS " /* reference ID */
92 #define DESCRIPTION "Arbiter 1088A/B GPS Receiver" /* WRU */
94 #define LENARB 24 /* format B5 timecode length */
95 #define MAXSTA 30 /* max length of status string */
96 #define MAXPOS 70 /* max length of position string */
99 * ARB unit control structure
102 l_fp laststamp; /* last receive timestamp */
103 int tcswitch; /* timecode switch/counter */
104 char qualchar; /* IEEE P1344 quality (TQ command) */
105 char status[MAXSTA]; /* receiver status (SR command) */
106 char latlon[MAXPOS]; /* receiver position (lat/lon/alt) */
110 * Function prototypes
112 static int arb_start P((int, struct peer *));
113 static void arb_shutdown P((int, struct peer *));
114 static void arb_receive P((struct recvbuf *));
115 static void arb_poll P((int, struct peer *));
120 struct refclock refclock_arbiter = {
121 arb_start, /* start up driver */
122 arb_shutdown, /* shut down driver */
123 arb_poll, /* transmit poll message */
124 noentry, /* not used (old arb_control) */
125 noentry, /* initialize driver (not used) */
126 noentry, /* not used (old arb_buginfo) */
127 NOFLAGS /* not used */
132 * arb_start - open the devices and initialize data for processing
140 register struct arbunit *up;
141 struct refclockproc *pp;
146 * Open serial port. Use CLK line discipline, if available.
148 (void)sprintf(device, DEVICE, unit);
149 if (!(fd = refclock_open(device, SPEED232, LDISC_CLK)))
153 * Allocate and initialize unit structure
155 if (!(up = (struct arbunit *)emalloc(sizeof(struct arbunit)))) {
159 memset((char *)up, 0, sizeof(struct arbunit));
161 pp->io.clock_recv = arb_receive;
162 pp->io.srcclock = (caddr_t)peer;
165 if (!io_addclock(&pp->io)) {
170 pp->unitptr = (caddr_t)up;
173 * Initialize miscellaneous variables
175 peer->precision = PRECISION;
176 pp->clockdesc = DESCRIPTION;
177 memcpy((char *)&pp->refid, REFID, 4);
178 write(pp->io.fd, "B0", 2);
184 * arb_shutdown - shut down the clock
192 register struct arbunit *up;
193 struct refclockproc *pp;
196 up = (struct arbunit *)pp->unitptr;
197 io_closeclock(&pp->io);
203 * arb_receive - receive data from the serial interface
207 struct recvbuf *rbufp
210 register struct arbunit *up;
211 struct refclockproc *pp;
215 u_char syncchar; /* synchronization indicator */
218 * Initialize pointers and read the timecode and timestamp
220 peer = (struct peer *)rbufp->recv_srcclock;
222 up = (struct arbunit *)pp->unitptr;
223 temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
226 * Note we get a buffer and timestamp for both a <cr> and <lf>,
227 * but only the <cr> timestamp is retained. The program first
228 * sends a TQ and expects the echo followed by the time quality
229 * character. It then sends a B5 starting the timecode broadcast
230 * and expects the echo followed some time later by the on-time
231 * character <cr> and then the <lf> beginning the timecode
232 * itself. Finally, at the <cr> beginning the next timecode at
233 * the next second, the program sends a B0 shutting down the
234 * timecode broadcast.
236 * If flag4 is set, the program snatches the latitude, longitude
237 * and elevation and writes it to the clockstats file.
241 pp->lastrec = up->laststamp;
242 up->laststamp = trtmp;
245 if (up->tcswitch == 0) {
248 * Collect statistics. If nothing is recogized, just
249 * ignore; sometimes the clock doesn't stop spewing
250 * timecodes for awhile after the B0 commant.
252 if (!strncmp(pp->a_lastcode, "TQ", 2)) {
253 up->qualchar = pp->a_lastcode[2];
254 write(pp->io.fd, "SR", 2);
255 } else if (!strncmp(pp->a_lastcode, "SR", 2)) {
256 strcpy(up->status, pp->a_lastcode + 2);
257 if (pp->sloppyclockflag & CLK_FLAG4)
258 write(pp->io.fd, "LA", 2);
260 write(pp->io.fd, "B5", 2);
263 } else if (!strncmp(pp->a_lastcode, "LA", 2)) {
264 strcpy(up->latlon, pp->a_lastcode + 2);
265 write(pp->io.fd, "LO", 2);
266 } else if (!strncmp(pp->a_lastcode, "LO", 2)) {
267 strcat(up->latlon, " ");
268 strcat(up->latlon, pp->a_lastcode + 2);
269 write(pp->io.fd, "LH", 2);
270 } else if (!strncmp(pp->a_lastcode, "LH", 2)) {
271 strcat(up->latlon, " ");
272 strcat(up->latlon, pp->a_lastcode + 2);
273 write(pp->io.fd, "DB", 2);
274 } else if (!strncmp(pp->a_lastcode, "DB", 2)) {
275 strcat(up->latlon, " ");
276 strcat(up->latlon, pp->a_lastcode + 2);
277 record_clock_stats(&peer->srcadr, up->latlon);
278 write(pp->io.fd, "B5", 2);
286 * We get down to business, check the timecode format and decode
287 * its contents. If the timecode has valid length, but not in
288 * proper format, we declare bad format and exit. If the
289 * timecode has invalid length, which sometimes occurs when the
290 * B0 amputates the broadcast, we just quietly steal away. Note
291 * that the time quality character and receiver status string is
292 * tacked on the end for clockstats display.
294 if (pp->lencode == LENARB) {
296 * Timecode format B5: "i yy ddd hh:mm:ss.000 "
298 pp->a_lastcode[LENARB - 2] = up->qualchar;
299 strcat(pp->a_lastcode, up->status);
301 if (sscanf(pp->a_lastcode, "%c%2d %3d %2d:%2d:%2d",
302 &syncchar, &pp->year, &pp->day, &pp->hour,
303 &pp->minute, &pp->second) != 6) {
304 refclock_report(peer, CEVNT_BADREPLY);
305 write(pp->io.fd, "B0", 2);
309 write(pp->io.fd, "B0", 2);
315 * We decode the clock dispersion from the time quality
318 switch (up->qualchar) {
320 case '0': /* locked, max accuracy */
324 case '4': /* unlock accuracy < 1 us */
328 case '5': /* unlock accuracy < 10 us */
332 case '6': /* unlock accuracy < 100 us */
336 case '7': /* unlock accuracy < 1 ms */
340 case '8': /* unlock accuracy < 10 ms */
344 case '9': /* unlock accuracy < 100 ms */
348 case 'A': /* unlock accuracy < 1 s */
352 case 'B': /* unlock accuracy < 10 s */
356 case 'F': /* clock failure */
357 pp->disp = MAXDISPERSE;
358 refclock_report(peer, CEVNT_FAULT);
359 write(pp->io.fd, "B0", 2);
363 pp->disp = MAXDISPERSE;
364 refclock_report(peer, CEVNT_BADREPLY);
365 write(pp->io.fd, "B0", 2);
369 pp->leap = LEAP_NOTINSYNC;
371 pp->leap = LEAP_NOWARNING;
374 printf("arbiter: timecode %d %s\n", pp->lencode,
377 if (up->tcswitch >= NSTAGE)
378 write(pp->io.fd, "B0", 2);
381 * Process the new sample in the median filter and determine the
382 * timecode timestamp.
384 if (!refclock_process(pp))
385 refclock_report(peer, CEVNT_BADTIME);
390 * arb_poll - called by the transmit procedure
398 register struct arbunit *up;
399 struct refclockproc *pp;
402 * Time to poll the clock. The Arbiter clock responds to a "B5"
403 * by returning a timecode in the format specified above.
404 * Transmission occurs once per second, unless turned off by a
405 * "B0". Note there is no checking on state, since this may not
406 * be the only customer reading the clock. Only one customer
407 * need poll the clock; all others just listen in. If nothing is
408 * heard from the clock for two polls, declare a timeout and
412 up = (struct arbunit *)pp->unitptr;
414 if (write(pp->io.fd, "TQ", 2) != 2) {
415 refclock_report(peer, CEVNT_FAULT);
418 if (pp->coderecv == pp->codeproc) {
419 refclock_report(peer, CEVNT_TIMEOUT);
422 pp->lastref = pp->lastrec;
423 refclock_receive(peer);
424 record_clock_stats(&peer->srcadr, pp->a_lastcode);
428 int refclock_arbiter_bs;
429 #endif /* REFCLOCK */