/contrib/ntp/ntpd/refclock_pst.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 322 lines · 171 code · 29 blank · 122 comment · 26 complexity · 845583dc38948ce2a66622b38f75ef97 MD5 · raw file

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