/contrib/ntp/ntpd/refclock_arbiter.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 453 lines · 241 code · 50 blank · 162 comment · 38 complexity · 219ed0ce806246946ecbc80f3b742436 MD5 · raw file

  1. /*
  2. * refclock_arbiter - clock driver for Arbiter 1088A/B Satellite
  3. * Controlled Clock
  4. */
  5. #ifdef HAVE_CONFIG_H
  6. #include <config.h>
  7. #endif
  8. #if defined(REFCLOCK) && defined(CLOCK_ARBITER)
  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. /*
  16. * This driver supports the Arbiter 1088A/B Satellite Controlled Clock.
  17. * The claimed accuracy of this clock is 100 ns relative to the PPS
  18. * output when receiving four or more satellites.
  19. *
  20. * The receiver should be configured before starting the NTP daemon, in
  21. * order to establish reliable position and operating conditions. It
  22. * does not initiate surveying or hold mode. For use with NTP, the
  23. * daylight savings time feature should be disables (D0 command) and the
  24. * broadcast mode set to operate in UTC (BU command).
  25. *
  26. * The timecode format supported by this driver is selected by the poll
  27. * sequence "B5", which initiates a line in the following format to be
  28. * repeated once per second until turned off by the "B0" poll sequence.
  29. *
  30. * Format B5 (24 ASCII printing characters):
  31. *
  32. * <cr><lf>i yy ddd hh:mm:ss.000bbb
  33. *
  34. * on-time = <cr>
  35. * i = synchronization flag (' ' = locked, '?' = unlocked)
  36. * yy = year of century
  37. * ddd = day of year
  38. * hh:mm:ss = hours, minutes, seconds
  39. * .000 = fraction of second (not used)
  40. * bbb = tailing spaces for fill
  41. *
  42. * The alarm condition is indicated by a '?' at i, which indicates the
  43. * receiver is not synchronized. In normal operation, a line consisting
  44. * of the timecode followed by the time quality character (TQ) followed
  45. * by the receiver status string (SR) is written to the clockstats file.
  46. * The time quality character is encoded in IEEE P1344 standard:
  47. *
  48. * Format TQ (IEEE P1344 estimated worst-case time quality)
  49. *
  50. * 0 clock locked, maximum accuracy
  51. * F clock failure, time not reliable
  52. * 4 clock unlocked, accuracy < 1 us
  53. * 5 clock unlocked, accuracy < 10 us
  54. * 6 clock unlocked, accuracy < 100 us
  55. * 7 clock unlocked, accuracy < 1 ms
  56. * 8 clock unlocked, accuracy < 10 ms
  57. * 9 clock unlocked, accuracy < 100 ms
  58. * A clock unlocked, accuracy < 1 s
  59. * B clock unlocked, accuracy < 10 s
  60. *
  61. * The status string is encoded as follows:
  62. *
  63. * Format SR (25 ASCII printing characters)
  64. *
  65. * V=vv S=ss T=t P=pdop E=ee
  66. *
  67. * vv = satellites visible
  68. * ss = relative signal strength
  69. * t = satellites tracked
  70. * pdop = position dilution of precision (meters)
  71. * ee = hardware errors
  72. *
  73. * If flag4 is set, an additional line consisting of the receiver
  74. * latitude (LA), longitude (LO), elevation (LH) (meters), and data
  75. * buffer (DB) is written to this file. If channel B is enabled for
  76. * deviation mode and connected to a 1-PPS signal, the last two numbers
  77. * on the line are the deviation and standard deviation averaged over
  78. * the last 15 seconds.
  79. *
  80. * PPS calibration fudge time1 .001240
  81. */
  82. /*
  83. * Interface definitions
  84. */
  85. #define DEVICE "/dev/gps%d" /* device name and unit */
  86. #define SPEED232 B9600 /* uart speed (9600 baud) */
  87. #define PRECISION (-20) /* precision assumed (about 1 us) */
  88. #define REFID "GPS " /* reference ID */
  89. #define DESCRIPTION "Arbiter 1088A/B GPS Receiver" /* WRU */
  90. #define LENARB 24 /* format B5 timecode length */
  91. #define MAXSTA 40 /* max length of status string */
  92. #define MAXPOS 80 /* max length of position string */
  93. /*
  94. * ARB unit control structure
  95. */
  96. struct arbunit {
  97. l_fp laststamp; /* last receive timestamp */
  98. int tcswitch; /* timecode switch/counter */
  99. char qualchar; /* IEEE P1344 quality (TQ command) */
  100. char status[MAXSTA]; /* receiver status (SR command) */
  101. char latlon[MAXPOS]; /* receiver position (lat/lon/alt) */
  102. };
  103. /*
  104. * Function prototypes
  105. */
  106. static int arb_start P((int, struct peer *));
  107. static void arb_shutdown P((int, struct peer *));
  108. static void arb_receive P((struct recvbuf *));
  109. static void arb_poll P((int, struct peer *));
  110. /*
  111. * Transfer vector
  112. */
  113. struct refclock refclock_arbiter = {
  114. arb_start, /* start up driver */
  115. arb_shutdown, /* shut down driver */
  116. arb_poll, /* transmit poll message */
  117. noentry, /* not used (old arb_control) */
  118. noentry, /* initialize driver (not used) */
  119. noentry, /* not used (old arb_buginfo) */
  120. NOFLAGS /* not used */
  121. };
  122. /*
  123. * arb_start - open the devices and initialize data for processing
  124. */
  125. static int
  126. arb_start(
  127. int unit,
  128. struct peer *peer
  129. )
  130. {
  131. register struct arbunit *up;
  132. struct refclockproc *pp;
  133. int fd;
  134. char device[20];
  135. /*
  136. * Open serial port. Use CLK line discipline, if available.
  137. */
  138. (void)sprintf(device, DEVICE, unit);
  139. if (!(fd = refclock_open(device, SPEED232, LDISC_CLK)))
  140. return (0);
  141. /*
  142. * Allocate and initialize unit structure
  143. */
  144. if (!(up = (struct arbunit *)emalloc(sizeof(struct arbunit)))) {
  145. (void) close(fd);
  146. return (0);
  147. }
  148. memset((char *)up, 0, sizeof(struct arbunit));
  149. pp = peer->procptr;
  150. pp->io.clock_recv = arb_receive;
  151. pp->io.srcclock = (caddr_t)peer;
  152. pp->io.datalen = 0;
  153. pp->io.fd = fd;
  154. if (!io_addclock(&pp->io)) {
  155. (void) close(fd);
  156. free(up);
  157. return (0);
  158. }
  159. pp->unitptr = (caddr_t)up;
  160. /*
  161. * Initialize miscellaneous variables
  162. */
  163. peer->precision = PRECISION;
  164. pp->clockdesc = DESCRIPTION;
  165. memcpy((char *)&pp->refid, REFID, 4);
  166. write(pp->io.fd, "B0", 2);
  167. return (1);
  168. }
  169. /*
  170. * arb_shutdown - shut down the clock
  171. */
  172. static void
  173. arb_shutdown(
  174. int unit,
  175. struct peer *peer
  176. )
  177. {
  178. register struct arbunit *up;
  179. struct refclockproc *pp;
  180. pp = peer->procptr;
  181. up = (struct arbunit *)pp->unitptr;
  182. io_closeclock(&pp->io);
  183. free(up);
  184. }
  185. /*
  186. * arb_receive - receive data from the serial interface
  187. */
  188. static void
  189. arb_receive(
  190. struct recvbuf *rbufp
  191. )
  192. {
  193. register struct arbunit *up;
  194. struct refclockproc *pp;
  195. struct peer *peer;
  196. l_fp trtmp;
  197. int temp;
  198. u_char syncchar; /* synch indicator */
  199. char tbuf[BMAX]; /* temp buffer */
  200. /*
  201. * Initialize pointers and read the timecode and timestamp
  202. */
  203. peer = (struct peer *)rbufp->recv_srcclock;
  204. pp = peer->procptr;
  205. up = (struct arbunit *)pp->unitptr;
  206. temp = refclock_gtlin(rbufp, tbuf, BMAX, &trtmp);
  207. /*
  208. * Note we get a buffer and timestamp for both a <cr> and <lf>,
  209. * but only the <cr> timestamp is retained. The program first
  210. * sends a TQ and expects the echo followed by the time quality
  211. * character. It then sends a B5 starting the timecode broadcast
  212. * and expects the echo followed some time later by the on-time
  213. * character <cr> and then the <lf> beginning the timecode
  214. * itself. Finally, at the <cr> beginning the next timecode at
  215. * the next second, the program sends a B0 shutting down the
  216. * timecode broadcast.
  217. *
  218. * If flag4 is set, the program snatches the latitude, longitude
  219. * and elevation and writes it to the clockstats file.
  220. */
  221. if (temp == 0)
  222. return;
  223. pp->lastrec = up->laststamp;
  224. up->laststamp = trtmp;
  225. if (temp < 3)
  226. return;
  227. if (up->tcswitch == 0) {
  228. /*
  229. * Collect statistics. If nothing is recogized, just
  230. * ignore; sometimes the clock doesn't stop spewing
  231. * timecodes for awhile after the B0 command.
  232. *
  233. * If flag4 is not set, send TQ, SR, B5. If flag4 is
  234. * sset, send TQ, SR, LA, LO, LH, DB, B5. When the
  235. * median filter is full, send B0.
  236. */
  237. if (!strncmp(tbuf, "TQ", 2)) {
  238. up->qualchar = tbuf[2];
  239. write(pp->io.fd, "SR", 2);
  240. return;
  241. } else if (!strncmp(tbuf, "SR", 2)) {
  242. strcpy(up->status, tbuf + 2);
  243. if (pp->sloppyclockflag & CLK_FLAG4)
  244. write(pp->io.fd, "LA", 2);
  245. else
  246. write(pp->io.fd, "B5", 2);
  247. return;
  248. } else if (!strncmp(tbuf, "LA", 2)) {
  249. strcpy(up->latlon, tbuf + 2);
  250. write(pp->io.fd, "LO", 2);
  251. return;
  252. } else if (!strncmp(tbuf, "LO", 2)) {
  253. strcat(up->latlon, " ");
  254. strcat(up->latlon, tbuf + 2);
  255. write(pp->io.fd, "LH", 2);
  256. return;
  257. } else if (!strncmp(tbuf, "LH", 2)) {
  258. strcat(up->latlon, " ");
  259. strcat(up->latlon, tbuf + 2);
  260. write(pp->io.fd, "DB", 2);
  261. return;
  262. } else if (!strncmp(tbuf, "DB", 2)) {
  263. strcat(up->latlon, " ");
  264. strcat(up->latlon, tbuf + 2);
  265. record_clock_stats(&peer->srcadr, up->latlon);
  266. #ifdef DEBUG
  267. if (debug)
  268. printf("arbiter: %s\n", up->latlon);
  269. #endif
  270. write(pp->io.fd, "B5", 2);
  271. }
  272. }
  273. /*
  274. * We get down to business, check the timecode format and decode
  275. * its contents. If the timecode has valid length, but not in
  276. * proper format, we declare bad format and exit. If the
  277. * timecode has invalid length, which sometimes occurs when the
  278. * B0 amputates the broadcast, we just quietly steal away. Note
  279. * that the time quality character and receiver status string is
  280. * tacked on the end for clockstats display.
  281. */
  282. up->tcswitch++;
  283. if (up->tcswitch <= 1 || temp < LENARB)
  284. return;
  285. /*
  286. * Timecode format B5: "i yy ddd hh:mm:ss.000 "
  287. */
  288. strncpy(pp->a_lastcode, tbuf, BMAX);
  289. pp->a_lastcode[LENARB - 2] = up->qualchar;
  290. strcat(pp->a_lastcode, up->status);
  291. pp->lencode = strlen(pp->a_lastcode);
  292. syncchar = ' ';
  293. if (sscanf(pp->a_lastcode, "%c%2d %3d %2d:%2d:%2d",
  294. &syncchar, &pp->year, &pp->day, &pp->hour,
  295. &pp->minute, &pp->second) != 6) {
  296. refclock_report(peer, CEVNT_BADREPLY);
  297. write(pp->io.fd, "B0", 2);
  298. return;
  299. }
  300. /*
  301. * We decode the clock dispersion from the time quality
  302. * character.
  303. */
  304. switch (up->qualchar) {
  305. case '0': /* locked, max accuracy */
  306. pp->disp = 1e-7;
  307. pp->lastref = pp->lastrec;
  308. break;
  309. case '4': /* unlock accuracy < 1 us */
  310. pp->disp = 1e-6;
  311. break;
  312. case '5': /* unlock accuracy < 10 us */
  313. pp->disp = 1e-5;
  314. break;
  315. case '6': /* unlock accuracy < 100 us */
  316. pp->disp = 1e-4;
  317. break;
  318. case '7': /* unlock accuracy < 1 ms */
  319. pp->disp = .001;
  320. break;
  321. case '8': /* unlock accuracy < 10 ms */
  322. pp->disp = .01;
  323. break;
  324. case '9': /* unlock accuracy < 100 ms */
  325. pp->disp = .1;
  326. break;
  327. case 'A': /* unlock accuracy < 1 s */
  328. pp->disp = 1;
  329. break;
  330. case 'B': /* unlock accuracy < 10 s */
  331. pp->disp = 10;
  332. break;
  333. case 'F': /* clock failure */
  334. pp->disp = MAXDISPERSE;
  335. refclock_report(peer, CEVNT_FAULT);
  336. write(pp->io.fd, "B0", 2);
  337. return;
  338. default:
  339. pp->disp = MAXDISPERSE;
  340. refclock_report(peer, CEVNT_BADREPLY);
  341. write(pp->io.fd, "B0", 2);
  342. return;
  343. }
  344. if (syncchar != ' ')
  345. pp->leap = LEAP_NOTINSYNC;
  346. else
  347. pp->leap = LEAP_NOWARNING;
  348. /*
  349. * Process the new sample in the median filter and determine the
  350. * timecode timestamp.
  351. */
  352. if (!refclock_process(pp))
  353. refclock_report(peer, CEVNT_BADTIME);
  354. else if (peer->disp > MAXDISTANCE)
  355. refclock_receive(peer);
  356. if (up->tcswitch >= MAXSTAGE) {
  357. write(pp->io.fd, "B0", 2);
  358. }
  359. }
  360. /*
  361. * arb_poll - called by the transmit procedure
  362. */
  363. static void
  364. arb_poll(
  365. int unit,
  366. struct peer *peer
  367. )
  368. {
  369. register struct arbunit *up;
  370. struct refclockproc *pp;
  371. /*
  372. * Time to poll the clock. The Arbiter clock responds to a "B5"
  373. * by returning a timecode in the format specified above.
  374. * Transmission occurs once per second, unless turned off by a
  375. * "B0". Note there is no checking on state, since this may not
  376. * be the only customer reading the clock. Only one customer
  377. * need poll the clock; all others just listen in.
  378. */
  379. pp = peer->procptr;
  380. up = (struct arbunit *)pp->unitptr;
  381. pp->polls++;
  382. up->tcswitch = 0;
  383. if (write(pp->io.fd, "TQ", 2) != 2)
  384. refclock_report(peer, CEVNT_FAULT);
  385. /*
  386. * Process median filter samples. If none received, declare a
  387. * timeout and keep going.
  388. */
  389. if (pp->coderecv == pp->codeproc) {
  390. refclock_report(peer, CEVNT_TIMEOUT);
  391. return;
  392. }
  393. refclock_receive(peer);
  394. record_clock_stats(&peer->srcadr, pp->a_lastcode);
  395. #ifdef DEBUG
  396. if (debug)
  397. printf("arbiter: timecode %d %s\n",
  398. pp->lencode, pp->a_lastcode);
  399. #endif
  400. }
  401. #else
  402. int refclock_arbiter_bs;
  403. #endif /* REFCLOCK */