2 * refclock_pcf - clock driver for the Conrad parallel port radio clock
9 #if defined(REFCLOCK) && defined(CLOCK_PCF)
13 #include "ntp_refclock.h"
14 #include "ntp_calendar.h"
15 #include "ntp_stdlib.h"
18 * This driver supports the parallel port radio clock sold by Conrad
19 * Electronic under order numbers 967602 and 642002.
21 * It requires that the local timezone be CET/CEST and that the pcfclock
22 * device driver be installed. A device driver for Linux is available at
23 * http://home.pages.de/~voegele/pcf.html. Information about a FreeBSD
24 * driver is available at http://schumann.cx/pcfclock/.
28 * Interface definitions
30 #define DEVICE "/dev/pcfclocks/%d"
31 #define OLDDEVICE "/dev/pcfclock%d"
32 #define PRECISION (-1) /* precision assumed (about 0.5 s) */
34 #define DESCRIPTION "Conrad parallel port radio clock"
36 #define LENPCF 18 /* timecode length */
41 static int pcf_start P((int, struct peer *));
42 static void pcf_shutdown P((int, struct peer *));
43 static void pcf_poll P((int, struct peer *));
48 struct refclock refclock_pcf = {
49 pcf_start, /* start up driver */
50 pcf_shutdown, /* shut down driver */
51 pcf_poll, /* transmit poll message */
52 noentry, /* not used */
53 noentry, /* initialize driver (not used) */
54 noentry, /* not used */
55 NOFLAGS /* not used */
60 * pcf_start - open the device and initialize data for processing
68 struct refclockproc *pp;
73 * Open device file for reading.
75 (void)sprintf(device, DEVICE, unit);
76 fd = open(device, O_RDONLY);
78 (void)sprintf(device, OLDDEVICE, unit);
79 fd = open(device, O_RDONLY);
83 printf ("starting PCF with device %s\n",device);
90 pp->io.clock_recv = noentry;
91 pp->io.srcclock = (caddr_t)peer;
96 * Initialize miscellaneous variables
98 peer->precision = PRECISION;
99 pp->clockdesc = DESCRIPTION;
100 /* one transmission takes 172.5 milliseconds since the radio clock
101 transmits 69 bits with a period of 2.5 milliseconds per bit */
102 pp->fudgetime1 = 0.1725;
103 memcpy((char *)&pp->refid, REFID, 4);
110 * pcf_shutdown - shut down the clock
118 struct refclockproc *pp;
121 (void)close(pp->io.fd);
126 * pcf_poll - called by the transmit procedure
134 struct refclockproc *pp;
142 if (read(pp->io.fd, buf, sizeof(buf)) < sizeof(buf) || buf[0] != 9) {
143 refclock_report(peer, CEVNT_FAULT);
147 tm.tm_mday = buf[11] * 10 + buf[10];
148 tm.tm_mon = buf[13] * 10 + buf[12] - 1;
149 tm.tm_year = buf[15] * 10 + buf[14];
150 tm.tm_hour = buf[7] * 10 + buf[6];
151 tm.tm_min = buf[5] * 10 + buf[4];
152 tm.tm_sec = buf[3] * 10 + buf[2];
153 tm.tm_isdst = (buf[8] & 1) ? 1 : (buf[8] & 2) ? 0 : -1;
156 * Y2K convert the 2-digit year
162 if (t == (time_t) -1) {
163 refclock_report(peer, CEVNT_BADTIME);
167 #if defined(__GLIBC__) && defined(_BSD_SOURCE)
168 if ((tm.tm_isdst > 0 && tm.tm_gmtoff != 7200)
169 || (tm.tm_isdst == 0 && tm.tm_gmtoff != 3600)
170 || tm.tm_isdst < 0) {
173 printf ("local time zone not set to CET/CEST\n");
175 refclock_report(peer, CEVNT_BADTIME);
180 pp->lencode = strftime(pp->a_lastcode, BMAX, "%Y %m %d %H %M %S", &tm);
182 #if defined(_REENTRANT) || defined(_THREAD_SAFE)
183 tp = gmtime_r(&t, &tm);
188 refclock_report(peer, CEVNT_FAULT);
192 get_systime(&pp->lastrec);
194 pp->year = tp->tm_year + 1900;
195 pp->day = tp->tm_yday + 1;
196 pp->hour = tp->tm_hour;
197 pp->minute = tp->tm_min;
198 pp->second = tp->tm_sec;
199 pp->nsec = buf[16] * 31250000;
201 pp->nsec += 500000000;
205 printf ("pcf%d: time is %04d/%02d/%02d %02d:%02d:%02d UTC\n",
206 unit, pp->year, tp->tm_mon + 1, tp->tm_mday, pp->hour,
207 pp->minute, pp->second);
210 if (!refclock_process(pp)) {
211 refclock_report(peer, CEVNT_BADTIME);
214 record_clock_stats(&peer->srcadr, pp->a_lastcode);
215 if ((buf[1] & 1) && !(pp->sloppyclockflag & CLK_FLAG2))
216 pp->leap = LEAP_NOTINSYNC;
218 pp->leap = LEAP_NOWARNING;
219 pp->lastref = pp->lastrec;
220 refclock_receive(peer);
224 #endif /* REFCLOCK */