/contrib/ntp/ntpd/refclock_heath.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 451 lines · 189 code · 34 blank · 228 comment · 30 complexity · 81ab7ef9a19a46fa473160374e57eb82 MD5 · raw file

  1. /*
  2. * refclock_heath - clock driver for Heath GC-1000
  3. * (but no longer the GC-1001 Model II, which apparently never worked)
  4. */
  5. #ifdef HAVE_CONFIG_H
  6. # include <config.h>
  7. #endif
  8. #if defined(REFCLOCK) && defined(CLOCK_HEATH)
  9. #include "ntpd.h"
  10. #include "ntp_io.h"
  11. #include "ntp_refclock.h"
  12. #include "ntp_stdlib.h"
  13. #include <stdio.h>
  14. #include <ctype.h>
  15. #ifdef HAVE_SYS_IOCTL_H
  16. # include <sys/ioctl.h>
  17. #endif /* not HAVE_SYS_IOCTL_H */
  18. /*
  19. * This driver supports the Heath GC-1000 Most Accurate Clock, with
  20. * RS232C Output Accessory. This is a WWV/WWVH receiver somewhat less
  21. * robust than other supported receivers. Its claimed accuracy is 100 ms
  22. * when actually synchronized to the broadcast signal, but this doesn't
  23. * happen even most of the time, due to propagation conditions, ambient
  24. * noise sources, etc. When not synchronized, the accuracy is at the
  25. * whim of the internal clock oscillator, which can wander into the
  26. * sunset without warning. Since the indicated precision is 100 ms,
  27. * expect a host synchronized only to this thing to wander to and fro,
  28. * occasionally being rudely stepped when the offset exceeds the default
  29. * clock_max of 128 ms.
  30. *
  31. * There were two GC-1000 versions supported by this driver. The original
  32. * GC-1000 with RS-232 output first appeared in 1983, but dissapeared
  33. * from the market a few years later. The GC-1001 II with RS-232 output
  34. * first appeared circa 1990, but apparently is no longer manufactured.
  35. * The two models differ considerably, both in interface and commands.
  36. * The GC-1000 has a pseudo-bipolar timecode output triggered by a RTS
  37. * transition. The timecode includes both the day of year and time of
  38. * day. The GC-1001 II has a true bipolar output and a complement of
  39. * single character commands. The timecode includes only the time of
  40. * day.
  41. *
  42. * The GC-1001 II was apparently never tested and, based on a Coverity
  43. * scan, apparently never worked [Bug 689]. Related code has been disabled.
  44. *
  45. * GC-1000
  46. *
  47. * The internal DIPswitches should be set to operate in MANUAL mode. The
  48. * external DIPswitches should be set to GMT and 24-hour format.
  49. *
  50. * In MANUAL mode the clock responds to a rising edge of the request to
  51. * send (RTS) modem control line by sending the timecode. Therefore, it
  52. * is necessary that the operating system implement the TIOCMBIC and
  53. * TIOCMBIS ioctl system calls and TIOCM_RTS control bit. Present
  54. * restrictions require the use of a POSIX-compatible programming
  55. * interface, although other interfaces may work as well.
  56. *
  57. * A simple hardware modification to the clock can be made which
  58. * prevents the clock hearing the request to send (RTS) if the HI SPEC
  59. * lamp is out. Route the HISPEC signal to the tone decoder board pin
  60. * 19, from the display, pin 19. Isolate pin 19 of the decoder board
  61. * first, but maintain connection with pin 10. Also isolate pin 38 of
  62. * the CPU on the tone board, and use half an added 7400 to gate the
  63. * original signal to pin 38 with that from pin 19.
  64. *
  65. * The clock message consists of 23 ASCII printing characters in the
  66. * following format:
  67. *
  68. * hh:mm:ss.f AM dd/mm/yr<cr>
  69. *
  70. * hh:mm:ss.f = hours, minutes, seconds
  71. * f = deciseconds ('?' when out of spec)
  72. * AM/PM/bb = blank in 24-hour mode
  73. * dd/mm/yr = day, month, year
  74. *
  75. * The alarm condition is indicated by '?', rather than a digit, at f.
  76. * Note that 0?:??:??.? is displayed before synchronization is first
  77. * established and hh:mm:ss.? once synchronization is established and
  78. * then lost again for about a day.
  79. *
  80. * GC-1001 II
  81. *
  82. * Commands consist of a single letter and are case sensitive. When
  83. * enterred in lower case, a description of the action performed is
  84. * displayed. When enterred in upper case the action is performed.
  85. * Following is a summary of descriptions as displayed by the clock:
  86. *
  87. * The clock responds with a command The 'A' command returns an ASCII
  88. * local time string: HH:MM:SS.T xx<CR>, where
  89. *
  90. * HH = hours
  91. * MM = minutes
  92. * SS = seconds
  93. * T = tenths-of-seconds
  94. * xx = 'AM', 'PM', or ' '
  95. * <CR> = carriage return
  96. *
  97. * The 'D' command returns 24 pairs of bytes containing the variable
  98. * divisor value at the end of each of the previous 24 hours. This
  99. * allows the timebase trimming process to be observed. UTC hour 00 is
  100. * always returned first. The first byte of each pair is the high byte
  101. * of (variable divisor * 16); the second byte is the low byte of
  102. * (variable divisor * 16). For example, the byte pair 3C 10 would be
  103. * returned for a divisor of 03C1 hex (961 decimal).
  104. *
  105. * The 'I' command returns: | TH | TL | ER | DH | DL | U1 | I1 | I2 | ,
  106. * where
  107. *
  108. * TH = minutes since timebase last trimmed (high byte)
  109. * TL = minutes since timebase last trimmed (low byte)
  110. * ER = last accumulated error in 1.25 ms increments
  111. * DH = high byte of (current variable divisor * 16)
  112. * DL = low byte of (current variable divisor * 16)
  113. * U1 = UT1 offset (/.1 s): | + | 4 | 2 | 1 | 0 | 0 | 0 | 0 |
  114. * I1 = information byte 1: | W | C | D | I | U | T | Z | 1 | ,
  115. * where
  116. *
  117. * W = set by WWV(H)
  118. * C = CAPTURE LED on
  119. * D = TRIM DN LED on
  120. * I = HI SPEC LED on
  121. * U = TRIM UP LED on
  122. * T = DST switch on
  123. * Z = UTC switch on
  124. * 1 = UT1 switch on
  125. *
  126. * I2 = information byte 2: | 8 | 8 | 4 | 2 | 1 | D | d | S | ,
  127. * where
  128. *
  129. * 8, 8, 4, 2, 1 = TIME ZONE switch settings
  130. * D = DST bit (#55) in last-received frame
  131. * d = DST bit (#2) in last-received frame
  132. * S = clock is in simulation mode
  133. *
  134. * The 'P' command returns 24 bytes containing the number of frames
  135. * received without error during UTC hours 00 through 23, providing an
  136. * indication of hourly propagation. These bytes are updated each hour
  137. * to reflect the previous 24 hour period. UTC hour 00 is always
  138. * returned first.
  139. *
  140. * The 'T' command returns the UTC time: | HH | MM | SS | T0 | , where
  141. * HH = tens-of-hours and hours (packed BCD)
  142. * MM = tens-of-minutes and minutes (packed BCD)
  143. * SS = tens-of-seconds and seconds (packed BCD)
  144. * T = tenths-of-seconds (BCD)
  145. *
  146. * Fudge Factors
  147. *
  148. * A fudge time1 value of .04 s appears to center the clock offset
  149. * residuals. The fudge time2 parameter is the local time offset east of
  150. * Greenwich, which depends on DST. Sorry about that, but the clock
  151. * gives no hint on what the DIPswitches say.
  152. */
  153. /*
  154. * Interface definitions
  155. */
  156. #define DEVICE "/dev/heath%d" /* device name and unit */
  157. #define PRECISION (-4) /* precision assumed (about 100 ms) */
  158. #define REFID "WWV\0" /* reference ID */
  159. #define DESCRIPTION "Heath GC-1000 Most Accurate Clock" /* WRU */
  160. #define LENHEATH1 23 /* min timecode length */
  161. #if 0 /* BUG 689 */
  162. #define LENHEATH2 13 /* min timecode length */
  163. #endif
  164. /*
  165. * Tables to compute the ddd of year form icky dd/mm timecode. Viva la
  166. * leap.
  167. */
  168. static int day1tab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  169. static int day2tab[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  170. /*
  171. * Baud rate table. The GC-1000 supports 1200, 2400 and 4800; the
  172. * GC-1001 II supports only 9600.
  173. */
  174. static int speed[] = {B1200, B2400, B4800, B9600};
  175. /*
  176. * Function prototypes
  177. */
  178. static int heath_start P((int, struct peer *));
  179. static void heath_shutdown P((int, struct peer *));
  180. static void heath_receive P((struct recvbuf *));
  181. static void heath_poll P((int, struct peer *));
  182. /*
  183. * Transfer vector
  184. */
  185. struct refclock refclock_heath = {
  186. heath_start, /* start up driver */
  187. heath_shutdown, /* shut down driver */
  188. heath_poll, /* transmit poll message */
  189. noentry, /* not used (old heath_control) */
  190. noentry, /* initialize driver */
  191. noentry, /* not used (old heath_buginfo) */
  192. NOFLAGS /* not used */
  193. };
  194. /*
  195. * heath_start - open the devices and initialize data for processing
  196. */
  197. static int
  198. heath_start(
  199. int unit,
  200. struct peer *peer
  201. )
  202. {
  203. struct refclockproc *pp;
  204. int fd;
  205. char device[20];
  206. /*
  207. * Open serial port
  208. */
  209. sprintf(device, DEVICE, unit);
  210. if (!(fd = refclock_open(device, speed[peer->ttl & 0x3],
  211. LDISC_REMOTE)))
  212. return (0);
  213. pp = peer->procptr;
  214. pp->io.clock_recv = heath_receive;
  215. pp->io.srcclock = (caddr_t)peer;
  216. pp->io.datalen = 0;
  217. pp->io.fd = fd;
  218. if (!io_addclock(&pp->io)) {
  219. (void) close(fd);
  220. return (0);
  221. }
  222. /*
  223. * Initialize miscellaneous variables
  224. */
  225. peer->precision = PRECISION;
  226. peer->burst = NSTAGE;
  227. pp->clockdesc = DESCRIPTION;
  228. memcpy((char *)&pp->refid, REFID, 4);
  229. return (1);
  230. }
  231. /*
  232. * heath_shutdown - shut down the clock
  233. */
  234. static void
  235. heath_shutdown(
  236. int unit,
  237. struct peer *peer
  238. )
  239. {
  240. struct refclockproc *pp;
  241. pp = peer->procptr;
  242. io_closeclock(&pp->io);
  243. }
  244. /*
  245. * heath_receive - receive data from the serial interface
  246. */
  247. static void
  248. heath_receive(
  249. struct recvbuf *rbufp
  250. )
  251. {
  252. struct refclockproc *pp;
  253. struct peer *peer;
  254. l_fp trtmp;
  255. int month, day;
  256. int i;
  257. char dsec, a[5];
  258. /*
  259. * Initialize pointers and read the timecode and timestamp
  260. */
  261. peer = (struct peer *)rbufp->recv_srcclock;
  262. pp = peer->procptr;
  263. pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX,
  264. &trtmp);
  265. /*
  266. * We get down to business, check the timecode format and decode
  267. * its contents. If the timecode has invalid length or is not in
  268. * proper format, we declare bad format and exit.
  269. */
  270. switch (pp->lencode) {
  271. /*
  272. * GC-1000 timecode format: "hh:mm:ss.f AM mm/dd/yy"
  273. * GC-1001 II timecode format: "hh:mm:ss.f "
  274. */
  275. case LENHEATH1:
  276. if (sscanf(pp->a_lastcode,
  277. "%2d:%2d:%2d.%c%5c%2d/%2d/%2d", &pp->hour,
  278. &pp->minute, &pp->second, &dsec, a, &month, &day,
  279. &pp->year) != 8) {
  280. refclock_report(peer, CEVNT_BADREPLY);
  281. return;
  282. }
  283. break;
  284. #if 0 /* BUG 689 */
  285. /*
  286. * GC-1001 II timecode format: "hh:mm:ss.f "
  287. */
  288. case LENHEATH2:
  289. if (sscanf(pp->a_lastcode, "%2d:%2d:%2d.%c", &pp->hour,
  290. &pp->minute, &pp->second, &dsec) != 4) {
  291. refclock_report(peer, CEVNT_BADREPLY);
  292. return;
  293. } else {
  294. struct tm *tm_time_p;
  295. time_t now;
  296. time(&now); /* we should grab 'now' earlier */
  297. tm_time_p = gmtime(&now);
  298. /*
  299. * There is a window of time around midnight
  300. * where this will Do The Wrong Thing.
  301. */
  302. if (tm_time_p) {
  303. month = tm_time_p->tm_mon + 1;
  304. day = tm_time_p->tm_mday;
  305. } else {
  306. refclock_report(peer, CEVNT_FAULT);
  307. return;
  308. }
  309. }
  310. break;
  311. #endif
  312. default:
  313. refclock_report(peer, CEVNT_BADREPLY);
  314. return;
  315. }
  316. /*
  317. * We determine the day of the year from the DIPswitches. This
  318. * should be fixed, since somebody might forget to set them.
  319. * Someday this hazard will be fixed by a fiendish scheme that
  320. * looks at the timecode and year the radio shows, then computes
  321. * the residue of the seconds mod the seconds in a leap cycle.
  322. * If in the third year of that cycle and the third and later
  323. * months of that year, add one to the day. Then, correct the
  324. * timecode accordingly. Icky pooh. This bit of nonsense could
  325. * be avoided if the engineers had been required to write a
  326. * device driver before finalizing the timecode format.
  327. */
  328. if (month < 1 || month > 12 || day < 1) {
  329. refclock_report(peer, CEVNT_BADTIME);
  330. return;
  331. }
  332. if (pp->year % 4) {
  333. if (day > day1tab[month - 1]) {
  334. refclock_report(peer, CEVNT_BADTIME);
  335. return;
  336. }
  337. for (i = 0; i < month - 1; i++)
  338. day += day1tab[i];
  339. } else {
  340. if (day > day2tab[month - 1]) {
  341. refclock_report(peer, CEVNT_BADTIME);
  342. return;
  343. }
  344. for (i = 0; i < month - 1; i++)
  345. day += day2tab[i];
  346. }
  347. pp->day = day;
  348. /*
  349. * Determine synchronization and last update
  350. */
  351. if (!isdigit((int)dsec))
  352. pp->leap = LEAP_NOTINSYNC;
  353. else {
  354. pp->nsec = (dsec - '0') * 100000000;
  355. pp->leap = LEAP_NOWARNING;
  356. }
  357. if (!refclock_process(pp))
  358. refclock_report(peer, CEVNT_BADTIME);
  359. }
  360. /*
  361. * heath_poll - called by the transmit procedure
  362. */
  363. static void
  364. heath_poll(
  365. int unit,
  366. struct peer *peer
  367. )
  368. {
  369. struct refclockproc *pp;
  370. int bits = TIOCM_RTS;
  371. /*
  372. * At each poll we check for timeout and toggle the RTS modem
  373. * control line, then take a timestamp. Presumably, this is the
  374. * event the radio captures to generate the timecode.
  375. * Apparently, the radio takes about a second to make up its
  376. * mind to send a timecode, so the receive timestamp is
  377. * worthless.
  378. */
  379. pp = peer->procptr;
  380. /*
  381. * We toggle the RTS modem control lead (GC-1000) and sent a T
  382. * (GC-1001 II) to kick a timecode loose from the radio. This
  383. * code works only for POSIX and SYSV interfaces. With bsd you
  384. * are on your own. We take a timestamp between the up and down
  385. * edges to lengthen the pulse, which should be about 50 usec on
  386. * a Sun IPC. With hotshot CPUs, the pulse might get too short.
  387. * Later.
  388. *
  389. * Bug 689: Even though we no longer support the GC-1001 II,
  390. * I'm leaving the 'T' write in for timing purposes.
  391. */
  392. if (ioctl(pp->io.fd, TIOCMBIC, (char *)&bits) < 0)
  393. refclock_report(peer, CEVNT_FAULT);
  394. get_systime(&pp->lastrec);
  395. if (write(pp->io.fd, "T", 1) != 1)
  396. refclock_report(peer, CEVNT_FAULT);
  397. ioctl(pp->io.fd, TIOCMBIS, (char *)&bits);
  398. if (peer->burst > 0)
  399. return;
  400. if (pp->coderecv == pp->codeproc) {
  401. refclock_report(peer, CEVNT_TIMEOUT);
  402. return;
  403. }
  404. pp->lastref = pp->lastrec;
  405. refclock_receive(peer);
  406. record_clock_stats(&peer->srcadr, pp->a_lastcode);
  407. #ifdef DEBUG
  408. if (debug)
  409. printf("heath: timecode %d %s\n", pp->lencode,
  410. pp->a_lastcode);
  411. #endif
  412. peer->burst = MAXSTAGE;
  413. pp->polls++;
  414. }
  415. #else
  416. int refclock_heath_bs;
  417. #endif /* REFCLOCK */