2 * refclock_dumbclock - clock driver for a unknown time distribution system
3 * that only provides hh:mm:ss (in local time, yet!).
7 * Must interpolate back to local time. Very annoying.
15 #if defined(REFCLOCK) && defined(CLOCK_DUMBCLOCK)
19 #include "ntp_refclock.h"
20 #include "ntp_calendar.h"
21 #include "ntp_stdlib.h"
27 extern int async_write(int, const void *, unsigned int);
29 #define write(fd, data, octets) async_write(fd, data, octets)
33 * This driver supports a generic dumb clock that only outputs hh:mm:ss,
34 * in local time, no less.
40 * hh:mm:ss -- what you'd expect, with a 24 hour clock. (Heck, that's the only
41 * way it could get stupider.) We take time on the <cr>.
43 * The original source of this module was the WWVB module.
47 * Interface definitions
49 #define DEVICE "/dev/dumbclock%d" /* device name and unit */
50 #define SPEED232 B9600 /* uart speed (9600 baud) */
51 #define PRECISION (-13) /* precision assumed (about 100 us) */
52 #define REFID "dumbclock" /* reference ID */
53 #define DESCRIPTION "Dumb clock" /* WRU */
57 * Insanity check. Since the time is local, we need to make sure that during midnight
58 * transitions, we can convert back to Unix time. If the conversion results in some number
59 * worse than this number of seconds away, assume the next day and retry.
61 #define INSANE_SECONDS 3600
64 * Dumb clock control structure
66 struct dumbclock_unit {
67 u_char tcswitch; /* timecode switch */
68 l_fp laststamp; /* last receive timestamp */
69 u_char lasthour; /* last hour (for monitor) */
70 u_char linect; /* count ignored lines (for monitor */
71 struct tm ymd; /* struct tm for y/m/d only */
77 static int dumbclock_start (int, struct peer *);
78 static void dumbclock_shutdown (int, struct peer *);
79 static void dumbclock_receive (struct recvbuf *);
81 static void dumbclock_poll (int, struct peer *);
87 struct refclock refclock_dumbclock = {
88 dumbclock_start, /* start up driver */
89 dumbclock_shutdown, /* shut down driver */
90 noentry, /* poll the driver -- a nice fabrication */
91 noentry, /* not used */
92 noentry, /* not used */
93 noentry, /* not used */
94 NOFLAGS /* not used */
99 * dumbclock_start - open the devices and initialize data for processing
107 register struct dumbclock_unit *up;
108 struct refclockproc *pp;
111 struct tm *tm_time_p;
115 * Open serial port. Don't bother with CLK line discipline, since
116 * it's not available.
118 snprintf(device, sizeof(device), DEVICE, unit);
121 printf ("starting Dumbclock with device %s\n",device);
123 fd = refclock_open(device, SPEED232, 0);
128 * Allocate and initialize unit structure
130 up = emalloc_zero(sizeof(*up));
133 pp->io.clock_recv = dumbclock_receive;
134 pp->io.srcclock = peer;
137 if (!io_addclock(&pp->io)) {
148 tm_time_p = localtime(&now);
150 tm_time_p = gmtime(&now);
153 up->ymd = *tm_time_p;
158 * Initialize miscellaneous variables
160 peer->precision = PRECISION;
161 pp->clockdesc = DESCRIPTION;
162 memcpy((char *)&pp->refid, REFID, 4);
168 * dumbclock_shutdown - shut down the clock
176 register struct dumbclock_unit *up;
177 struct refclockproc *pp;
182 io_closeclock(&pp->io);
189 * dumbclock_receive - receive data from the serial interface
193 struct recvbuf *rbufp
196 struct dumbclock_unit *up;
197 struct refclockproc *pp;
200 l_fp trtmp; /* arrival timestamp */
201 int hours; /* hour-of-day */
202 int minutes; /* minutes-past-the-hour */
203 int seconds; /* seconds */
204 int temp; /* int temp */
205 int got_good; /* got a good time flag */
208 * Initialize pointers and read the timecode and timestamp
210 peer = rbufp->recv_peer;
213 temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
216 if (up->tcswitch == 0) {
218 up->laststamp = trtmp;
223 pp->lencode = (u_short)temp;
224 pp->lastrec = up->laststamp;
225 up->laststamp = trtmp;
230 printf("dumbclock: timecode %d %s\n",
231 pp->lencode, pp->a_lastcode);
235 * We get down to business. Check the timecode format...
238 if (sscanf(pp->a_lastcode,"%02d:%02d:%02d",
239 &hours,&minutes,&seconds) == 3)
243 time_t asserted_time; /* the SPM time based on the composite time+date */
244 struct tm asserted_tm; /* the struct tm of the same */
247 time_t reality_delta;
252 * Convert to GMT for sites that distribute localtime. This
253 * means we have to figure out what day it is. Easier said
257 memset(&asserted_tm, 0, sizeof(asserted_tm));
259 asserted_tm.tm_year = up->ymd.tm_year;
260 asserted_tm.tm_mon = up->ymd.tm_mon;
261 asserted_tm.tm_mday = up->ymd.tm_mday;
262 asserted_tm.tm_hour = hours;
263 asserted_tm.tm_min = minutes;
264 asserted_tm.tm_sec = seconds;
265 asserted_tm.tm_isdst = -1;
268 asserted_time = mktime (&asserted_tm);
271 #include "GMT unsupported for dumbclock!"
273 reality_delta = asserted_time - now;
276 * We assume that if the time is grossly wrong, it's because we got the
277 * year/month/day wrong.
279 if (reality_delta > INSANE_SECONDS)
281 asserted_time -= SECSPERDAY; /* local clock behind real time */
283 else if (-reality_delta > INSANE_SECONDS)
285 asserted_time += SECSPERDAY; /* local clock ahead of real time */
287 lt_p = localtime(&asserted_time);
294 refclock_report (peer, CEVNT_FAULT);
298 if ((gmtp = gmtime (&asserted_time)) == NULL)
300 refclock_report (peer, CEVNT_FAULT);
303 adjyear = gmtp->tm_year+1900;
304 adjmon = gmtp->tm_mon+1;
305 pp->day = ymd2yd (adjyear, adjmon, gmtp->tm_mday);
306 pp->hour = gmtp->tm_hour;
307 pp->minute = gmtp->tm_min;
308 pp->second = gmtp->tm_sec;
311 printf ("time is %04d/%02d/%02d %02d:%02d:%02d UTC\n",
312 adjyear,adjmon,gmtp->tm_mday,pp->hour,pp->minute,
324 refclock_report(peer, CEVNT_BADREPLY);
329 * Process the new sample in the median filter and determine the
330 * timecode timestamp.
332 if (!refclock_process(pp)) {
333 refclock_report(peer, CEVNT_BADTIME);
336 pp->lastref = pp->lastrec;
337 refclock_receive(peer);
338 record_clock_stats(&peer->srcadr, pp->a_lastcode);
339 up->lasthour = (u_char)pp->hour;
344 * dumbclock_poll - called by the transmit procedure
352 register struct dumbclock_unit *up;
353 struct refclockproc *pp;
357 * Time to poll the clock. The Chrono-log clock is supposed to
358 * respond to a 'T' by returning a timecode in the format(s)
359 * specified above. Ours does (can?) not, but this seems to be
360 * an installation-specific problem. This code is dyked out,
361 * but may be re-enabled if anyone ever finds a Chrono-log that
362 * actually listens to this command.
367 if (peer->reach == 0)
368 refclock_report(peer, CEVNT_TIMEOUT);
373 if (write(pp->io.fd, &pollchar, 1) != 1)
374 refclock_report(peer, CEVNT_FAULT);
382 int refclock_dumbclock_bs;
383 #endif /* defined(REFCLOCK) && defined(CLOCK_DUMBCLOCK) */