4 * refclock_dumbclock - clock driver for a unknown time distribution system
5 * that only provides hh:mm:ss (in local time, yet!).
9 * Must interpolate back to local time. Very annoying.
17 #if defined(REFCLOCK) && defined(CLOCK_DUMBCLOCK)
21 #include "ntp_refclock.h"
22 #include "ntp_calendar.h"
23 #include "ntp_stdlib.h"
29 extern int async_write(int, const void *, unsigned int);
31 #define write(fd, data, octets) async_write(fd, data, octets)
35 * This driver supports a generic dumb clock that only outputs hh:mm:ss,
36 * in local time, no less.
42 * hh:mm:ss -- what you'd expect, with a 24 hour clock. (Heck, that's the only
43 * way it could get stupider.) We take time on the <cr>.
45 * The original source of this module was the WWVB module.
49 * Interface definitions
51 #define DEVICE "/dev/dumbclock%d" /* device name and unit */
52 #define SPEED232 B9600 /* uart speed (9600 baud) */
53 #define PRECISION (-13) /* precision assumed (about 100 us) */
54 #define REFID "dumbclock" /* reference ID */
55 #define DESCRIPTION "Dumb clock" /* WRU */
59 * Insanity check. Since the time is local, we need to make sure that during midnight
60 * transitions, we can convert back to Unix time. If the conversion results in some number
61 * worse than this number of seconds away, assume the next day and retry.
63 #define INSANE_SECONDS 3600
66 * Dumb clock control structure
68 struct dumbclock_unit
{
69 u_char tcswitch
; /* timecode switch */
70 l_fp laststamp
; /* last receive timestamp */
71 u_char lasthour
; /* last hour (for monitor) */
72 u_char linect
; /* count ignored lines (for monitor */
73 struct tm ymd
; /* struct tm for y/m/d only */
79 static int dumbclock_start (int, struct peer
*);
80 static void dumbclock_shutdown (int, struct peer
*);
81 static void dumbclock_receive (struct recvbuf
*);
83 static void dumbclock_poll (int, struct peer
*);
89 struct refclock refclock_dumbclock
= {
90 dumbclock_start
, /* start up driver */
91 dumbclock_shutdown
, /* shut down driver */
92 noentry
, /* poll the driver -- a nice fabrication */
93 noentry
, /* not used */
94 noentry
, /* not used */
95 noentry
, /* not used */
96 NOFLAGS
/* not used */
101 * dumbclock_start - open the devices and initialize data for processing
109 register struct dumbclock_unit
*up
;
110 struct refclockproc
*pp
;
113 struct tm
*tm_time_p
;
117 * Open serial port. Don't bother with CLK line discipline, since
118 * it's not available.
120 (void)sprintf(device
, DEVICE
, unit
);
123 printf ("starting Dumbclock with device %s\n",device
);
125 fd
= refclock_open(device
, SPEED232
, 0);
130 * Allocate and initialize unit structure
132 up
= (struct dumbclock_unit
*)emalloc(sizeof(struct dumbclock_unit
));
133 memset((char *)up
, 0, sizeof(struct dumbclock_unit
));
135 pp
->unitptr
= (caddr_t
)up
;
136 pp
->io
.clock_recv
= dumbclock_receive
;
137 pp
->io
.srcclock
= (caddr_t
)peer
;
140 if (!io_addclock(&pp
->io
)) {
149 tm_time_p
= localtime(&now
);
151 tm_time_p
= gmtime(&now
);
154 up
->ymd
= *tm_time_p
;
159 * Initialize miscellaneous variables
161 peer
->precision
= PRECISION
;
162 pp
->clockdesc
= DESCRIPTION
;
163 memcpy((char *)&pp
->refid
, REFID
, 4);
169 * dumbclock_shutdown - shut down the clock
177 register struct dumbclock_unit
*up
;
178 struct refclockproc
*pp
;
181 up
= (struct dumbclock_unit
*)pp
->unitptr
;
182 io_closeclock(&pp
->io
);
188 * dumbclock_receive - receive data from the serial interface
192 struct recvbuf
*rbufp
195 struct dumbclock_unit
*up
;
196 struct refclockproc
*pp
;
199 l_fp trtmp
; /* arrival timestamp */
200 int hours
; /* hour-of-day */
201 int minutes
; /* minutes-past-the-hour */
202 int seconds
; /* seconds */
203 int temp
; /* int temp */
204 int got_good
; /* got a good time flag */
207 * Initialize pointers and read the timecode and timestamp
209 peer
= (struct peer
*)rbufp
->recv_srcclock
;
211 up
= (struct dumbclock_unit
*)pp
->unitptr
;
212 temp
= refclock_gtlin(rbufp
, pp
->a_lastcode
, BMAX
, &trtmp
);
215 if (up
->tcswitch
== 0) {
217 up
->laststamp
= trtmp
;
222 pp
->lencode
= (u_short
)temp
;
223 pp
->lastrec
= up
->laststamp
;
224 up
->laststamp
= trtmp
;
229 printf("dumbclock: timecode %d %s\n",
230 pp
->lencode
, pp
->a_lastcode
);
234 * We get down to business. Check the timecode format...
237 if (sscanf(pp
->a_lastcode
,"%02d:%02d:%02d",
238 &hours
,&minutes
,&seconds
) == 3)
242 time_t asserted_time
; /* the SPM time based on the composite time+date */
243 struct tm asserted_tm
; /* the struct tm of the same */
246 time_t reality_delta
;
251 * Convert to GMT for sites that distribute localtime. This
252 * means we have to figure out what day it is. Easier said
256 memset(&asserted_tm
, 0, sizeof(asserted_tm
));
258 asserted_tm
.tm_year
= up
->ymd
.tm_year
;
259 asserted_tm
.tm_mon
= up
->ymd
.tm_mon
;
260 asserted_tm
.tm_mday
= up
->ymd
.tm_mday
;
261 asserted_tm
.tm_hour
= hours
;
262 asserted_tm
.tm_min
= minutes
;
263 asserted_tm
.tm_sec
= seconds
;
264 asserted_tm
.tm_isdst
= -1;
267 asserted_time
= mktime (&asserted_tm
);
270 #include "GMT unsupported for dumbclock!"
272 reality_delta
= asserted_time
- now
;
275 * We assume that if the time is grossly wrong, it's because we got the
276 * year/month/day wrong.
278 if (reality_delta
> INSANE_SECONDS
)
280 asserted_time
-= SECSPERDAY
; /* local clock behind real time */
282 else if (-reality_delta
> INSANE_SECONDS
)
284 asserted_time
+= SECSPERDAY
; /* local clock ahead of real time */
286 lt_p
= localtime(&asserted_time
);
293 refclock_report (peer
, CEVNT_FAULT
);
297 if ((gmtp
= gmtime (&asserted_time
)) == NULL
)
299 refclock_report (peer
, CEVNT_FAULT
);
302 adjyear
= gmtp
->tm_year
+1900;
303 adjmon
= gmtp
->tm_mon
+1;
304 pp
->day
= ymd2yd (adjyear
, adjmon
, gmtp
->tm_mday
);
305 pp
->hour
= gmtp
->tm_hour
;
306 pp
->minute
= gmtp
->tm_min
;
307 pp
->second
= gmtp
->tm_sec
;
310 printf ("time is %04d/%02d/%02d %02d:%02d:%02d UTC\n",
311 adjyear
,adjmon
,gmtp
->tm_mday
,pp
->hour
,pp
->minute
,
323 refclock_report(peer
, CEVNT_BADREPLY
);
328 * Process the new sample in the median filter and determine the
329 * timecode timestamp.
331 if (!refclock_process(pp
)) {
332 refclock_report(peer
, CEVNT_BADTIME
);
335 pp
->lastref
= pp
->lastrec
;
336 refclock_receive(peer
);
337 record_clock_stats(&peer
->srcadr
, pp
->a_lastcode
);
338 up
->lasthour
= (u_char
)pp
->hour
;
343 * dumbclock_poll - called by the transmit procedure
351 register struct dumbclock_unit
*up
;
352 struct refclockproc
*pp
;
356 * Time to poll the clock. The Chrono-log clock is supposed to
357 * respond to a 'T' by returning a timecode in the format(s)
358 * specified above. Ours does (can?) not, but this seems to be
359 * an installation-specific problem. This code is dyked out,
360 * but may be re-enabled if anyone ever finds a Chrono-log that
361 * actually listens to this command.
365 up
= (struct dumbclock_unit
*)pp
->unitptr
;
366 if (peer
->reach
== 0)
367 refclock_report(peer
, CEVNT_TIMEOUT
);
372 if (write(pp
->io
.fd
, &pollchar
, 1) != 1)
373 refclock_report(peer
, CEVNT_FAULT
);
381 int refclock_dumbclock_bs
;
382 #endif /* defined(REFCLOCK) && defined(CLOCK_DUMBCLOCK) */