1 /* $NetBSD: refclock_pst.c,v 1.6 2024/08/18 20:47:18 christos Exp $ */ 2 3 /* 4 * refclock_pst - clock driver for PSTI/Traconex WWV/WWVH receivers 5 */ 6 7 #ifdef HAVE_CONFIG_H 8 #include <config.h> 9 #endif 10 11 #if defined(REFCLOCK) && defined(CLOCK_PST) 12 13 #include "ntpd.h" 14 #include "ntp_io.h" 15 #include "ntp_refclock.h" 16 #include "ntp_stdlib.h" 17 18 #include <stdio.h> 19 #include <ctype.h> 20 21 /* 22 * This driver supports the PSTI 1010 and Traconex 1020 WWV/WWVH 23 * Receivers. No specific claim of accuracy is made for these receiver, 24 * but actual experience suggests that 10 ms would be a conservative 25 * assumption. 26 * 27 * The DIPswitches should be set for 9600 bps line speed, 24-hour day- 28 * of-year format and UTC time zone. Automatic correction for DST should 29 * be disabled. It is very important that the year be set correctly in 30 * the DIPswitches; otherwise, the day of year will be incorrect after 31 * 28 April of a normal or leap year. The propagation delay DIPswitches 32 * should be set according to the distance from the transmitter for both 33 * WWV and WWVH, as described in the instructions. While the delay can 34 * be set only to within 11 ms, the fudge time1 parameter can be used 35 * for vernier corrections. 36 * 37 * Using the poll sequence QTQDQM, the response timecode is in three 38 * sections totalling 50 ASCII printing characters, as concatenated by 39 * the driver, in the following format: 40 * 41 * ahh:mm:ss.fffs<cr> yy/dd/mm/ddd<cr> frdzycchhSSFTttttuuxx<cr> 42 * 43 * on-time = first <cr> 44 * hh:mm:ss.fff = hours, minutes, seconds, milliseconds 45 * a = AM/PM indicator (' ' for 24-hour mode) 46 * yy = year (from internal switches) 47 * dd/mm/ddd = day of month, month, day of year 48 * s = daylight-saving indicator (' ' for 24-hour mode) 49 * f = frequency enable (O = all frequencies enabled) 50 * r = baud rate (3 = 1200, 6 = 9600) 51 * d = features indicator (@ = month/day display enabled) 52 * z = time zone (0 = UTC) 53 * y = year (5 = 91) 54 * cc = WWV propagation delay (52 = 22 ms) 55 * hh = WWVH propagation delay (81 = 33 ms) 56 * SS = status (80 or 82 = operating correctly) 57 * F = current receive frequency (4 = 15 MHz) 58 * T = transmitter (C = WWV, H = WWVH) 59 * tttt = time since last update (0000 = minutes) 60 * uu = flush character (03 = ^c) 61 * xx = 94 (unknown) 62 * 63 * The alarm condition is indicated by other than '8' at A, which occurs 64 * during initial synchronization and when received signal is lost for 65 * an extended period; unlock condition is indicated by other than 66 * "0000" in the tttt subfield at Q. 67 * 68 * Fudge Factors 69 * 70 * There are no special fudge factors other than the generic. 71 */ 72 73 /* 74 * Interface definitions 75 */ 76 #define DEVICE "/dev/wwv%d" /* device name and unit */ 77 #define SPEED232 B9600 /* uart speed (9600 baud) */ 78 #define PRECISION (-10) /* precision assumed (about 1 ms) */ 79 #define WWVREFID "WWV\0" /* WWV reference ID */ 80 #define WWVHREFID "WWVH" /* WWVH reference ID */ 81 #define DESCRIPTION "PSTI/Traconex WWV/WWVH Receiver" /* WRU */ 82 #define PST_PHI (10e-6) /* max clock oscillator offset */ 83 #define LENPST 46 /* min timecode length */ 84 85 /* 86 * Unit control structure 87 */ 88 struct pstunit { 89 int tcswitch; /* timecode switch */ 90 char *lastptr; /* pointer to timecode data */ 91 }; 92 93 /* 94 * Function prototypes 95 */ 96 static int pst_start (int, struct peer *); 97 static void pst_shutdown (int, struct peer *); 98 static void pst_receive (struct recvbuf *); 99 static void pst_poll (int, struct peer *); 100 101 /* 102 * Transfer vector 103 */ 104 struct refclock refclock_pst = { 105 pst_start, /* start up driver */ 106 pst_shutdown, /* shut down driver */ 107 pst_poll, /* transmit poll message */ 108 noentry, /* not used (old pst_control) */ 109 noentry, /* initialize driver */ 110 noentry, /* not used (old pst_buginfo) */ 111 NOFLAGS /* not used */ 112 }; 113 114 115 /* 116 * pst_start - open the devices and initialize data for processing 117 */ 118 static int 119 pst_start( 120 int unit, 121 struct peer *peer 122 ) 123 { 124 register struct pstunit *up; 125 struct refclockproc *pp; 126 int fd; 127 char device[20]; 128 129 /* 130 * Open serial port. Use CLK line discipline, if available. 131 */ 132 snprintf(device, sizeof(device), DEVICE, unit); 133 fd = refclock_open(&peer->srcadr, device, SPEED232, LDISC_CLK); 134 if (fd <= 0) 135 return (0); 136 137 /* 138 * Allocate and initialize unit structure 139 */ 140 up = emalloc_zero(sizeof(*up)); 141 pp = peer->procptr; 142 pp->io.clock_recv = pst_receive; 143 pp->io.srcclock = peer; 144 pp->io.datalen = 0; 145 pp->io.fd = fd; 146 if (!io_addclock(&pp->io)) { 147 close(fd); 148 pp->io.fd = -1; 149 free(up); 150 return (0); 151 } 152 pp->unitptr = up; 153 154 /* 155 * Initialize miscellaneous variables 156 */ 157 peer->precision = PRECISION; 158 pp->clockdesc = DESCRIPTION; 159 memcpy((char *)&pp->refid, WWVREFID, 4); 160 return (1); 161 } 162 163 164 /* 165 * pst_shutdown - shut down the clock 166 */ 167 static void 168 pst_shutdown( 169 int unit, 170 struct peer *peer 171 ) 172 { 173 register struct pstunit *up; 174 struct refclockproc *pp; 175 176 pp = peer->procptr; 177 up = pp->unitptr; 178 if (-1 != pp->io.fd) 179 io_closeclock(&pp->io); 180 if (NULL != up) 181 free(up); 182 } 183 184 185 /* 186 * pst_receive - receive data from the serial interface 187 */ 188 static void 189 pst_receive( 190 struct recvbuf *rbufp 191 ) 192 { 193 register struct pstunit *up; 194 struct refclockproc *pp; 195 struct peer *peer; 196 l_fp trtmp; 197 u_long ltemp; 198 char ampmchar; /* AM/PM indicator */ 199 char daychar; /* standard/daylight indicator */ 200 char junque[10]; /* "yy/dd/mm/" discard */ 201 char info[14]; /* "frdzycchhSSFT" clock info */ 202 203 /* 204 * Initialize pointers and read the timecode and timestamp 205 */ 206 peer = rbufp->recv_peer; 207 pp = peer->procptr; 208 up = pp->unitptr; 209 up->lastptr += refclock_gtlin(rbufp, up->lastptr, pp->a_lastcode 210 + BMAX - 2 - up->lastptr, &trtmp); 211 *up->lastptr++ = ' '; 212 *up->lastptr = '\0'; 213 214 /* 215 * Note we get a buffer and timestamp for each <cr>, but only 216 * the first timestamp is retained. 217 */ 218 if (up->tcswitch == 0) 219 pp->lastrec = trtmp; 220 up->tcswitch++; 221 pp->lencode = up->lastptr - pp->a_lastcode; 222 if (up->tcswitch < 3) 223 return; 224 225 /* 226 * We get down to business, check the timecode format and decode 227 * its contents. If the timecode has invalid length or is not in 228 * proper format, we declare bad format and exit. 229 */ 230 if (pp->lencode < LENPST) { 231 refclock_report(peer, CEVNT_BADREPLY); 232 return; 233 } 234 235 /* 236 * Timecode format: 237 * "ahh:mm:ss.fffs yy/dd/mm/ddd frdzycchhSSFTttttuuxx" 238 */ 239 if (sscanf(pp->a_lastcode, 240 "%c%2d:%2d:%2d.%3ld%c %9s%3d%13s%4ld", 241 &mchar, &pp->hour, &pp->minute, &pp->second, &pp->nsec, 242 &daychar, junque, &pp->day, info, <emp) != 10) { 243 refclock_report(peer, CEVNT_BADREPLY); 244 return; 245 } 246 pp->nsec *= 1000000; 247 248 /* 249 * Decode synchronization, quality and last update. If 250 * unsynchronized, set the leap bits accordingly and exit. Once 251 * synchronized, the dispersion depends only on when the clock 252 * was last heard, which depends on the time since last update, 253 * as reported by the clock. 254 */ 255 if (info[9] != '8') 256 pp->leap = LEAP_NOTINSYNC; 257 if (info[12] == 'H') 258 memcpy((char *)&pp->refid, WWVHREFID, 4); 259 else 260 memcpy((char *)&pp->refid, WWVREFID, 4); 261 if (peer->stratum <= 1) 262 peer->refid = pp->refid; 263 if (ltemp == 0) 264 pp->lastref = pp->lastrec; 265 pp->disp = PST_PHI * ltemp * 60; 266 267 /* 268 * Process the new sample in the median filter and determine the 269 * timecode timestamp. 270 */ 271 if (!refclock_process(pp)) 272 refclock_report(peer, CEVNT_BADTIME); 273 else if (peer->disp > MAXDISTANCE) 274 refclock_receive(peer); 275 } 276 277 278 /* 279 * pst_poll - called by the transmit procedure 280 */ 281 static void 282 pst_poll( 283 int unit, 284 struct peer *peer 285 ) 286 { 287 register struct pstunit *up; 288 struct refclockproc *pp; 289 290 /* 291 * Time to poll the clock. The PSTI/Traconex clock responds to a 292 * "QTQDQMT" by returning a timecode in the format specified 293 * above. Note there is no checking on state, since this may not 294 * be the only customer reading the clock. Only one customer 295 * need poll the clock; all others just listen in. If the clock 296 * becomes unreachable, declare a timeout and keep going. 297 */ 298 pp = peer->procptr; 299 up = pp->unitptr; 300 up->tcswitch = 0; 301 up->lastptr = pp->a_lastcode; 302 if (write(pp->io.fd, "QTQDQMT", 6) != 6) 303 refclock_report(peer, CEVNT_FAULT); 304 if (pp->coderecv == pp->codeproc) { 305 refclock_report(peer, CEVNT_TIMEOUT); 306 return; 307 } 308 refclock_receive(peer); 309 record_clock_stats(&peer->srcadr, pp->a_lastcode); 310 #ifdef DEBUG 311 if (debug) 312 printf("pst: timecode %d %s\n", pp->lencode, 313 pp->a_lastcode); 314 #endif 315 pp->polls++; 316 } 317 318 #else 319 NONEMPTY_TRANSLATION_UNIT 320 #endif /* REFCLOCK */ 321