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), elevation (LH) (meters), and data
80 * buffer (DB) is written to this file. If channel B is enabled for
81 * deviation mode and connected to a 1-PPS signal, the last two numbers
82 * on the line are the deviation and standard deviation averaged over
83 * the last 15 seconds.
85 * PPS calibration fudge time1 .001240
89 * Interface definitions
91 #define DEVICE "/dev/gps%d" /* device name and unit */
92 #define SPEED232 B9600 /* uart speed (9600 baud) */
93 #define PRECISION (-20) /* precision assumed (about 1 us) */
94 #define REFID "GPS " /* reference ID */
95 #define DESCRIPTION "Arbiter 1088A/B GPS Receiver" /* WRU */
96 #define LENARB 24 /* format B5 timecode length */
97 #define MAXSTA 40 /* max length of status string */
98 #define MAXPOS 80 /* max length of position string */
101 * ARB unit control structure
104 l_fp laststamp; /* last receive timestamp */
105 int tcswitch; /* timecode switch/counter */
106 char qualchar; /* IEEE P1344 quality (TQ command) */
107 char status[MAXSTA]; /* receiver status (SR command) */
108 char latlon[MAXPOS]; /* receiver position (lat/lon/alt) */
112 * Function prototypes
114 static int arb_start P((int, struct peer *));
115 static void arb_shutdown P((int, struct peer *));
116 static void arb_receive P((struct recvbuf *));
117 static void arb_poll P((int, struct peer *));
122 struct refclock refclock_arbiter = {
123 arb_start, /* start up driver */
124 arb_shutdown, /* shut down driver */
125 arb_poll, /* transmit poll message */
126 noentry, /* not used (old arb_control) */
127 noentry, /* initialize driver (not used) */
128 noentry, /* not used (old arb_buginfo) */
129 NOFLAGS /* not used */
134 * arb_start - open the devices and initialize data for processing
142 register struct arbunit *up;
143 struct refclockproc *pp;
148 * Open serial port. Use CLK line discipline, if available.
150 (void)sprintf(device, DEVICE, unit);
151 if (!(fd = refclock_open(device, SPEED232, LDISC_CLK)))
155 * Allocate and initialize unit structure
157 if (!(up = (struct arbunit *)emalloc(sizeof(struct arbunit)))) {
161 memset((char *)up, 0, sizeof(struct arbunit));
163 pp->io.clock_recv = arb_receive;
164 pp->io.srcclock = (caddr_t)peer;
167 if (!io_addclock(&pp->io)) {
172 pp->unitptr = (caddr_t)up;
175 * Initialize miscellaneous variables
177 peer->precision = PRECISION;
178 pp->clockdesc = DESCRIPTION;
179 memcpy((char *)&pp->refid, REFID, 4);
180 write(pp->io.fd, "B0", 2);
186 * arb_shutdown - shut down the clock
194 register struct arbunit *up;
195 struct refclockproc *pp;
198 up = (struct arbunit *)pp->unitptr;
199 io_closeclock(&pp->io);
205 * arb_receive - receive data from the serial interface
209 struct recvbuf *rbufp
212 register struct arbunit *up;
213 struct refclockproc *pp;
217 u_char syncchar; /* synch indicator */
218 char tbuf[BMAX]; /* temp buffer */
221 * Initialize pointers and read the timecode and timestamp
223 peer = (struct peer *)rbufp->recv_srcclock;
225 up = (struct arbunit *)pp->unitptr;
226 temp = refclock_gtlin(rbufp, tbuf, BMAX, &trtmp);
229 * Note we get a buffer and timestamp for both a <cr> and <lf>,
230 * but only the <cr> timestamp is retained. The program first
231 * sends a TQ and expects the echo followed by the time quality
232 * character. It then sends a B5 starting the timecode broadcast
233 * and expects the echo followed some time later by the on-time
234 * character <cr> and then the <lf> beginning the timecode
235 * itself. Finally, at the <cr> beginning the next timecode at
236 * the next second, the program sends a B0 shutting down the
237 * timecode broadcast.
239 * If flag4 is set, the program snatches the latitude, longitude
240 * and elevation and writes it to the clockstats file.
245 pp->lastrec = up->laststamp;
246 up->laststamp = trtmp;
250 if (up->tcswitch == 0) {
253 * Collect statistics. If nothing is recogized, just
254 * ignore; sometimes the clock doesn't stop spewing
255 * timecodes for awhile after the B0 command.
257 * If flag4 is not set, send TQ, SR, B5. If flag4 is
258 * sset, send TQ, SR, LA, LO, LH, DB, B5. When the
259 * median filter is full, send B0.
261 if (!strncmp(tbuf, "TQ", 2)) {
262 up->qualchar = tbuf[2];
263 write(pp->io.fd, "SR", 2);
266 } else if (!strncmp(tbuf, "SR", 2)) {
267 strcpy(up->status, tbuf + 2);
268 if (pp->sloppyclockflag & CLK_FLAG4)
269 write(pp->io.fd, "LA", 2);
271 write(pp->io.fd, "B5", 2);
274 } else if (!strncmp(tbuf, "LA", 2)) {
275 strcpy(up->latlon, tbuf + 2);
276 write(pp->io.fd, "LO", 2);
279 } else if (!strncmp(tbuf, "LO", 2)) {
280 strcat(up->latlon, " ");
281 strcat(up->latlon, tbuf + 2);
282 write(pp->io.fd, "LH", 2);
285 } else if (!strncmp(tbuf, "LH", 2)) {
286 strcat(up->latlon, " ");
287 strcat(up->latlon, tbuf + 2);
288 write(pp->io.fd, "DB", 2);
291 } else if (!strncmp(tbuf, "DB", 2)) {
292 strcat(up->latlon, " ");
293 strcat(up->latlon, tbuf + 2);
294 record_clock_stats(&peer->srcadr, up->latlon);
297 printf("arbiter: %s\n", up->latlon);
299 write(pp->io.fd, "B5", 2);
304 * We get down to business, check the timecode format and decode
305 * its contents. If the timecode has valid length, but not in
306 * proper format, we declare bad format and exit. If the
307 * timecode has invalid length, which sometimes occurs when the
308 * B0 amputates the broadcast, we just quietly steal away. Note
309 * that the time quality character and receiver status string is
310 * tacked on the end for clockstats display.
313 if (up->tcswitch <= 1 || temp < LENARB)
317 * Timecode format B5: "i yy ddd hh:mm:ss.000 "
319 strncpy(pp->a_lastcode, tbuf, BMAX);
320 pp->a_lastcode[LENARB - 2] = up->qualchar;
321 strcat(pp->a_lastcode, up->status);
322 pp->lencode = strlen(pp->a_lastcode);
324 if (sscanf(pp->a_lastcode, "%c%2d %3d %2d:%2d:%2d",
325 &syncchar, &pp->year, &pp->day, &pp->hour,
326 &pp->minute, &pp->second) != 6) {
327 refclock_report(peer, CEVNT_BADREPLY);
328 write(pp->io.fd, "B0", 2);
333 * We decode the clock dispersion from the time quality
336 switch (up->qualchar) {
338 case '0': /* locked, max accuracy */
340 pp->lastref = pp->lastrec;
343 case '4': /* unlock accuracy < 1 us */
347 case '5': /* unlock accuracy < 10 us */
351 case '6': /* unlock accuracy < 100 us */
355 case '7': /* unlock accuracy < 1 ms */
359 case '8': /* unlock accuracy < 10 ms */
363 case '9': /* unlock accuracy < 100 ms */
367 case 'A': /* unlock accuracy < 1 s */
371 case 'B': /* unlock accuracy < 10 s */
375 case 'F': /* clock failure */
376 pp->disp = MAXDISPERSE;
377 refclock_report(peer, CEVNT_FAULT);
378 write(pp->io.fd, "B0", 2);
382 pp->disp = MAXDISPERSE;
383 refclock_report(peer, CEVNT_BADREPLY);
384 write(pp->io.fd, "B0", 2);
388 pp->leap = LEAP_NOTINSYNC;
390 pp->leap = LEAP_NOWARNING;
393 * Process the new sample in the median filter and determine the
394 * timecode timestamp.
396 if (!refclock_process(pp))
397 refclock_report(peer, CEVNT_BADTIME);
398 else if (peer->disp > MAXDISTANCE)
399 refclock_receive(peer);
401 if (up->tcswitch >= MAXSTAGE) {
402 write(pp->io.fd, "B0", 2);
408 * arb_poll - called by the transmit procedure
416 register struct arbunit *up;
417 struct refclockproc *pp;
420 * Time to poll the clock. The Arbiter clock responds to a "B5"
421 * by returning a timecode in the format specified above.
422 * Transmission occurs once per second, unless turned off by a
423 * "B0". Note there is no checking on state, since this may not
424 * be the only customer reading the clock. Only one customer
425 * need poll the clock; all others just listen in.
428 up = (struct arbunit *)pp->unitptr;
431 if (write(pp->io.fd, "TQ", 2) != 2)
432 refclock_report(peer, CEVNT_FAULT);
435 * Process median filter samples. If none received, declare a
436 * timeout and keep going.
438 if (pp->coderecv == pp->codeproc) {
439 refclock_report(peer, CEVNT_TIMEOUT);
442 refclock_receive(peer);
443 record_clock_stats(&peer->srcadr, pp->a_lastcode);
446 printf("arbiter: timecode %d %s\n",
447 pp->lencode, pp->a_lastcode);
452 int refclock_arbiter_bs;
453 #endif /* REFCLOCK */