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(SYS_WINNT)
17 #define close closesocket
20 #if defined(REFCLOCK) && defined(CLOCK_DUMBCLOCK)
24 #include "ntp_refclock.h"
25 #include "ntp_calendar.h"
26 #include "ntp_stdlib.h"
32 * This driver supports a generic dumb clock that only outputs hh:mm:ss,
33 * in local time, no less.
39 * hh:mm:ss -- what you'd expect, with a 24 hour clock. (Heck, that's the only
40 * way it could get stupider.) We take time on the <cr>.
42 * The original source of this module was the WWVB module.
46 * Interface definitions
48 #define DEVICE "/dev/dumbclock%d" /* device name and unit */
49 #define SPEED232 B9600 /* uart speed (9600 baud) */
50 #define PRECISION (-13) /* precision assumed (about 100 us) */
51 #define REFID "dumbclock" /* reference ID */
52 #define DESCRIPTION "Dumb clock" /* WRU */
56 * Insanity check. Since the time is local, we need to make sure that during midnight
57 * transitions, we can convert back to Unix time. If the conversion results in some number
58 * worse than this number of seconds away, assume the next day and retry.
60 #define INSANE_SECONDS 3600
63 * Dumb clock control structure
65 struct dumbclock_unit {
66 u_char tcswitch; /* timecode switch */
67 l_fp laststamp; /* last receive timestamp */
68 u_char lasthour; /* last hour (for monitor) */
69 u_char linect; /* count ignored lines (for monitor */
70 struct tm ymd; /* struct tm for y/m/d only */
76 static int dumbclock_start P((int, struct peer *));
77 static void dumbclock_shutdown P((int, struct peer *));
78 static void dumbclock_receive P((struct recvbuf *));
80 static void dumbclock_poll P((int, struct peer *));
86 struct refclock refclock_dumbclock = {
87 dumbclock_start, /* start up driver */
88 dumbclock_shutdown, /* shut down driver */
89 noentry, /* poll the driver -- a nice fabrication */
90 noentry, /* not used */
91 noentry, /* not used */
92 noentry, /* not used */
93 NOFLAGS /* not used */
98 * dumbclock_start - open the devices and initialize data for processing
106 register struct dumbclock_unit *up;
107 struct refclockproc *pp;
110 struct tm *tm_time_p;
114 * Open serial port. Don't bother with CLK line discipline, since
115 * it's not available.
117 (void)sprintf(device, DEVICE, unit);
120 printf ("starting Dumbclock with device %s\n",device);
122 fd = refclock_open(device, SPEED232, 0);
127 * Allocate and initialize unit structure
129 up = (struct dumbclock_unit *)emalloc(sizeof(struct dumbclock_unit));
134 memset((char *)up, 0, sizeof(struct dumbclock_unit));
136 pp->unitptr = (caddr_t)up;
137 pp->io.clock_recv = dumbclock_receive;
138 pp->io.srcclock = (caddr_t)peer;
141 if (!io_addclock(&pp->io)) {
150 tm_time_p = localtime(&now);
152 tm_time_p = gmtime(&now);
156 up->ymd = *tm_time_p;
164 * Initialize miscellaneous variables
166 peer->precision = PRECISION;
167 pp->clockdesc = DESCRIPTION;
168 memcpy((char *)&pp->refid, REFID, 4);
174 * dumbclock_shutdown - shut down the clock
182 register struct dumbclock_unit *up;
183 struct refclockproc *pp;
186 up = (struct dumbclock_unit *)pp->unitptr;
187 io_closeclock(&pp->io);
193 * dumbclock_receive - receive data from the serial interface
197 struct recvbuf *rbufp
200 struct dumbclock_unit *up;
201 struct refclockproc *pp;
204 l_fp trtmp; /* arrival timestamp */
205 int hours; /* hour-of-day */
206 int minutes; /* minutes-past-the-hour */
207 int seconds; /* seconds */
208 int temp; /* int temp */
209 int got_good; /* got a good time flag */
212 * Initialize pointers and read the timecode and timestamp
214 peer = (struct peer *)rbufp->recv_srcclock;
216 up = (struct dumbclock_unit *)pp->unitptr;
217 temp = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &trtmp);
220 if (up->tcswitch == 0) {
222 up->laststamp = trtmp;
227 pp->lencode = (u_short)temp;
228 pp->lastrec = up->laststamp;
229 up->laststamp = trtmp;
234 printf("dumbclock: timecode %d %s\n",
235 pp->lencode, pp->a_lastcode);
239 * We get down to business. Check the timecode format...
242 if (sscanf(pp->a_lastcode,"%02d:%02d:%02d",
243 &hours,&minutes,&seconds) == 3)
247 time_t asserted_time; /* the SPM time based on the composite time+date */
248 struct tm asserted_tm; /* the struct tm of the same */
256 * Convert to GMT for sites that distribute localtime. This
257 * means we have to figure out what day it is. Easier said
261 asserted_tm.tm_year = up->ymd.tm_year;
262 asserted_tm.tm_mon = up->ymd.tm_mon;
263 asserted_tm.tm_mday = up->ymd.tm_mday;
264 asserted_tm.tm_hour = hours;
265 asserted_tm.tm_min = minutes;
266 asserted_tm.tm_sec = seconds;
267 asserted_tm.tm_isdst = -1;
270 asserted_time = mktime (&asserted_tm);
273 #include "GMT unsupported for dumbclock!"
275 reality_delta = asserted_time - now;
278 * We assume that if the time is grossly wrong, it's because we got the
279 * year/month/day wrong.
281 if (reality_delta > INSANE_SECONDS)
283 asserted_time -= SECSPERDAY; /* local clock behind real time */
285 else if (-reality_delta > INSANE_SECONDS)
287 asserted_time += SECSPERDAY; /* local clock ahead of real time */
289 lt_p = localtime(&asserted_time);
296 refclock_report (peer, CEVNT_FAULT);
300 if ((gmtp = gmtime (&asserted_time)) == NULL)
302 refclock_report (peer, CEVNT_FAULT);
305 adjyear = gmtp->tm_year+1900;
306 adjmon = gmtp->tm_mon+1;
307 pp->day = ymd2yd (adjyear, adjmon, gmtp->tm_mday);
308 pp->hour = gmtp->tm_hour;
309 pp->minute = gmtp->tm_min;
310 pp->second = gmtp->tm_sec;
313 printf ("time is %04d/%02d/%02d %02d:%02d:%02d UTC\n",
314 adjyear,adjmon,gmtp->tm_mday,pp->hour,pp->minute,
326 refclock_report(peer, CEVNT_BADREPLY);
331 * Process the new sample in the median filter and determine the
332 * timecode timestamp.
334 if (!refclock_process(pp)) {
335 refclock_report(peer, CEVNT_BADTIME);
338 pp->lastref = pp->lastrec;
339 refclock_receive(peer);
340 record_clock_stats(&peer->srcadr, pp->a_lastcode);
341 up->lasthour = (u_char)pp->hour;
346 * dumbclock_poll - called by the transmit procedure
354 register struct dumbclock_unit *up;
355 struct refclockproc *pp;
359 * Time to poll the clock. The Chrono-log clock is supposed to
360 * respond to a 'T' by returning a timecode in the format(s)
361 * specified above. Ours does (can?) not, but this seems to be
362 * an installation-specific problem. This code is dyked out,
363 * but may be re-enabled if anyone ever finds a Chrono-log that
364 * actually listens to this command.
368 up = (struct dumbclock_unit *)pp->unitptr;
369 if (peer->reach == 0)
370 refclock_report(peer, CEVNT_TIMEOUT);
375 if (write(pp->io.fd, &pollchar, 1) != 1)
376 refclock_report(peer, CEVNT_FAULT);
384 int refclock_dumbclock_bs;
385 #endif /* REFCLOCK */