1 /* 2 * refclock_as2201 - clock driver for the Austron 2201A GPS 3 * Timing Receiver 4 */ 5 #ifdef HAVE_CONFIG_H 6 #include <config.h> 7 #endif 8 9 #if defined(REFCLOCK) && defined(CLOCK_AS2201) 10 11 #include <stdio.h> 12 #include <ctype.h> 13 #include <sys/time.h> 14 15 #include "ntpd.h" 16 #include "ntp_io.h" 17 #include "ntp_refclock.h" 18 #include "ntp_unixtime.h" 19 #include "ntp_stdlib.h" 20 21 /* 22 * This driver supports the Austron 2200A/2201A GPS Receiver with 23 * Buffered RS-232-C Interface Module. Note that the original 2200/2201 24 * receivers will not work reliably with this driver, since the older 25 * design cannot accept input commands at any reasonable data rate. 26 * 27 * The program sends a "*toc\r" to the radio and expects a response of 28 * the form "yy:ddd:hh:mm:ss.mmm\r" where yy = year of century, ddd = 29 * day of year, hh:mm:ss = second of day and mmm = millisecond of 30 * second. Then, it sends statistics commands to the radio and expects 31 * a multi-line reply showing the corresponding statistics or other 32 * selected data. Statistics commands are sent in order as determined by 33 * a vector of commands; these might have to be changed with different 34 * radio options. If flag4 of the fudge configuration command is set to 35 * 1, the statistics data are written to the clockstats file for later 36 * processing. 37 * 38 * In order for this code to work, the radio must be placed in non- 39 * interactive mode using the "off" command and with a single <cr> 40 * response using the "term cr" command. The setting of the "echo" 41 * and "df" commands does not matter. The radio should select UTC 42 * timescale using the "ts utc" command. 43 * 44 * There are two modes of operation for this driver. The first with 45 * default configuration is used with stock kernels and serial-line 46 * drivers and works with almost any machine. In this mode the driver 47 * assumes the radio captures a timestamp upon receipt of the "*" that 48 * begins the driver query. Accuracies in this mode are in the order of 49 * a millisecond or two and the receiver can be connected to only one 50 * host. 51 * 52 * The second mode of operation can be used for SunOS kernels that have 53 * been modified with the ppsclock streams module included in this 54 * distribution. The mode is enabled if flag3 of the fudge configuration 55 * command has been set to 1. In this mode a precise timestamp is 56 * available using a gadget box and 1-pps signal from the receiver. This 57 * improves the accuracy to the order of a few tens of microseconds. In 58 * addition, the serial output and 1-pps signal can be bussed to more 59 * than one hosts, but only one of them should be connected to the 60 * radio input data line. 61 */ 62 63 /* 64 * GPS Definitions 65 */ 66 #define SMAX 200 /* statistics buffer length */ 67 #define DEVICE "/dev/gps%d" /* device name and unit */ 68 #define SPEED232 B9600 /* uart speed (9600 baud) */ 69 #define PRECISION (-20) /* precision assumed (about 1 us) */ 70 #define REFID "GPS\0" /* reference ID */ 71 #define DESCRIPTION "Austron 2201A GPS Receiver" /* WRU */ 72 73 #define LENTOC 19 /* yy:ddd:hh:mm:ss.mmm timecode lngth */ 74 75 /* 76 * AS2201 unit control structure. 77 */ 78 struct as2201unit { 79 char *lastptr; /* statistics buffer pointer */ 80 char stats[SMAX]; /* statistics buffer */ 81 int linect; /* count of lines remaining */ 82 int index; /* current statistics command */ 83 }; 84 85 /* 86 * Radio commands to extract statitistics 87 * 88 * A command consists of an ASCII string terminated by a <cr> (\r). The 89 * command list consist of a sequence of commands terminated by a null 90 * string ("\0"). One command from the list is sent immediately 91 * following each received timecode (*toc\r command) and the ASCII 92 * strings received from the radio are saved along with the timecode in 93 * the clockstats file. Subsequent commands are sent at each timecode, 94 * with the last one in the list followed by the first one. The data 95 * received from the radio consist of ASCII strings, each terminated by 96 * a <cr> (\r) character. The number of strings for each command is 97 * specified as the first line of output as an ASCII-encode number. Note 98 * that the ETF command requires the Input Buffer Module and the LORAN 99 * commands require the LORAN Assist Module. However, if these modules 100 * are not installed, the radio and this driver will continue to operate 101 * successfuly, but no data will be captured for these commands. 102 */ 103 static char stat_command[][30] = { 104 "ITF\r", /* internal time/frequency */ 105 "ETF\r", /* external time/frequency */ 106 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */ 107 "LORAN TDATA\r", /* LORAN signal data */ 108 "ID;OPT;VER\r", /* model; options; software version */ 109 110 "ITF\r", /* internal time/frequency */ 111 "ETF\r", /* external time/frequency */ 112 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */ 113 "TRSTAT\r", /* satellite tracking status */ 114 "POS;PPS;PPSOFF\r", /* position, pps source, offsets */ 115 116 "ITF\r", /* internal time/frequency */ 117 "ETF\r", /* external time/frequency */ 118 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */ 119 "LORAN TDATA\r", /* LORAN signal data */ 120 "UTC\r", /* UTC leap info */ 121 122 "ITF\r", /* internal time/frequency */ 123 "ETF\r", /* external time/frequency */ 124 "LORAN ENSEMBLE\r", /* GPS/LORAN ensemble statistics */ 125 "TRSTAT\r", /* satellite tracking status */ 126 "OSC;ET;TEMP\r", /* osc type; tune volts; oven temp */ 127 "\0" /* end of table */ 128 }; 129 130 /* 131 * Function prototypes 132 */ 133 static int as2201_start P((int, struct peer *)); 134 static void as2201_shutdown P((int, struct peer *)); 135 static void as2201_receive P((struct recvbuf *)); 136 static void as2201_poll P((int, struct peer *)); 137 138 /* 139 * Transfer vector 140 */ 141 struct refclock refclock_as2201 = { 142 as2201_start, /* start up driver */ 143 as2201_shutdown, /* shut down driver */ 144 as2201_poll, /* transmit poll message */ 145 noentry, /* not used (old as2201_control) */ 146 noentry, /* initialize driver (not used) */ 147 noentry, /* not used (old as2201_buginfo) */ 148 NOFLAGS /* not used */ 149 }; 150 151 152 /* 153 * as2201_start - open the devices and initialize data for processing 154 */ 155 static int 156 as2201_start( 157 int unit, 158 struct peer *peer 159 ) 160 { 161 register struct as2201unit *up; 162 struct refclockproc *pp; 163 int fd; 164 char gpsdev[20]; 165 166 /* 167 * Open serial port. Use CLK line discipline, if available. 168 */ 169 (void)sprintf(gpsdev, DEVICE, unit); 170 if (!(fd = refclock_open(gpsdev, SPEED232, LDISC_CLK))) 171 return (0); 172 173 /* 174 * Allocate and initialize unit structure 175 */ 176 if (!(up = (struct as2201unit *) 177 emalloc(sizeof(struct as2201unit)))) { 178 (void) close(fd); 179 return (0); 180 } 181 memset((char *)up, 0, sizeof(struct as2201unit)); 182 pp = peer->procptr; 183 pp->io.clock_recv = as2201_receive; 184 pp->io.srcclock = (caddr_t)peer; 185 pp->io.datalen = 0; 186 pp->io.fd = fd; 187 if (!io_addclock(&pp->io)) { 188 (void) close(fd); 189 free(up); 190 return (0); 191 } 192 pp->unitptr = (caddr_t)up; 193 194 /* 195 * Initialize miscellaneous variables 196 */ 197 peer->precision = PRECISION; 198 peer->burst = NSTAGE; 199 pp->clockdesc = DESCRIPTION; 200 memcpy((char *)&pp->refid, REFID, 4); 201 up->lastptr = up->stats; 202 up->index = 0; 203 return (1); 204 } 205 206 207 /* 208 * as2201_shutdown - shut down the clock 209 */ 210 static void 211 as2201_shutdown( 212 int unit, 213 struct peer *peer 214 ) 215 { 216 register struct as2201unit *up; 217 struct refclockproc *pp; 218 219 pp = peer->procptr; 220 up = (struct as2201unit *)pp->unitptr; 221 io_closeclock(&pp->io); 222 free(up); 223 } 224 225 226 /* 227 * as2201__receive - receive data from the serial interface 228 */ 229 static void 230 as2201_receive( 231 struct recvbuf *rbufp 232 ) 233 { 234 register struct as2201unit *up; 235 struct refclockproc *pp; 236 struct peer *peer; 237 l_fp trtmp; 238 239 /* 240 * Initialize pointers and read the timecode and timestamp. 241 */ 242 peer = (struct peer *)rbufp->recv_srcclock; 243 pp = peer->procptr; 244 up = (struct as2201unit *)pp->unitptr; 245 pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp); 246 #ifdef DEBUG 247 if (debug) 248 printf("gps: timecode %d %d %s\n", 249 up->linect, pp->lencode, pp->a_lastcode); 250 #endif 251 if (pp->lencode == 0) 252 return; 253 254 /* 255 * If linect is greater than zero, we must be in the middle of a 256 * statistics operation, so simply tack the received data at the 257 * end of the statistics string. If not, we could either have 258 * just received the timecode itself or a decimal number 259 * indicating the number of following lines of the statistics 260 * reply. In the former case, write the accumulated statistics 261 * data to the clockstats file and continue onward to process 262 * the timecode; in the later case, save the number of lines and 263 * quietly return. 264 */ 265 if (pp->sloppyclockflag & CLK_FLAG2) 266 pp->lastrec = trtmp; 267 if (up->linect > 0) { 268 up->linect--; 269 if ((int)(up->lastptr - up->stats + pp->lencode) > SMAX - 2) 270 return; 271 *up->lastptr++ = ' '; 272 (void)strcpy(up->lastptr, pp->a_lastcode); 273 up->lastptr += pp->lencode; 274 return; 275 } else { 276 if (pp->lencode == 1) { 277 up->linect = atoi(pp->a_lastcode); 278 return; 279 } else { 280 record_clock_stats(&peer->srcadr, up->stats); 281 #ifdef DEBUG 282 if (debug) 283 printf("gps: stat %s\n", up->stats); 284 #endif 285 } 286 } 287 up->lastptr = up->stats; 288 *up->lastptr = '\0'; 289 290 /* 291 * We get down to business, check the timecode format and decode 292 * its contents. If the timecode has invalid length or is not in 293 * proper format, we declare bad format and exit. 294 */ 295 if (pp->lencode < LENTOC) { 296 refclock_report(peer, CEVNT_BADREPLY); 297 return; 298 } 299 300 /* 301 * Timecode format: "yy:ddd:hh:mm:ss.mmm" 302 */ 303 if (sscanf(pp->a_lastcode, "%2d:%3d:%2d:%2d:%2d.%3d", &pp->year, 304 &pp->day, &pp->hour, &pp->minute, &pp->second, &pp->msec) 305 != 6) { 306 refclock_report(peer, CEVNT_BADREPLY); 307 return; 308 } 309 310 /* 311 * Test for synchronization (this is a temporary crock). 312 */ 313 if (pp->a_lastcode[2] != ':') 314 pp->leap = LEAP_NOTINSYNC; 315 else 316 pp->leap = LEAP_NOWARNING; 317 318 /* 319 * Process the new sample in the median filter and determine the 320 * timecode timestamp. 321 */ 322 if (!refclock_process(pp)) { 323 refclock_report(peer, CEVNT_BADTIME); 324 return; 325 } 326 327 /* 328 * If CLK_FLAG4 is set, initialize the statistics buffer and 329 * send the next command. If not, simply write the timecode to 330 * the clockstats file. 331 */ 332 (void)strcpy(up->lastptr, pp->a_lastcode); 333 up->lastptr += pp->lencode; 334 if (pp->sloppyclockflag & CLK_FLAG4) { 335 *up->lastptr++ = ' '; 336 (void)strcpy(up->lastptr, stat_command[up->index]); 337 up->lastptr += strlen(stat_command[up->index]); 338 up->lastptr--; 339 *up->lastptr = '\0'; 340 (void)write(pp->io.fd, stat_command[up->index], 341 strlen(stat_command[up->index])); 342 up->index++; 343 if (*stat_command[up->index] == '\0') 344 up->index = 0; 345 } 346 } 347 348 349 /* 350 * as2201_poll - called by the transmit procedure 351 * 352 * We go to great pains to avoid changing state here, since there may be 353 * more than one eavesdropper receiving the same timecode. 354 */ 355 static void 356 as2201_poll( 357 int unit, 358 struct peer *peer 359 ) 360 { 361 struct refclockproc *pp; 362 363 /* 364 * Send a "\r*toc\r" to get things going. We go to great pains 365 * to avoid changing state, since there may be more than one 366 * eavesdropper watching the radio. 367 */ 368 pp = peer->procptr; 369 if (write(pp->io.fd, "\r*toc\r", 6) != 6) { 370 refclock_report(peer, CEVNT_FAULT); 371 } else { 372 pp->polls++; 373 if (!(pp->sloppyclockflag & CLK_FLAG2)) 374 get_systime(&pp->lastrec); 375 } 376 if (peer->burst > 0) 377 return; 378 if (pp->coderecv == pp->codeproc) { 379 refclock_report(peer, CEVNT_TIMEOUT); 380 return; 381 } 382 refclock_receive(peer); 383 peer->burst = NSTAGE; 384 } 385 386 #else 387 int refclock_as2201_bs; 388 #endif /* REFCLOCK */ 389