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 (int, struct peer *);
42 static void pcf_shutdown (int, struct peer *);
43 static void pcf_poll (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 snprintf(device, sizeof(device), DEVICE, unit);
76 fd = open(device, O_RDONLY);
78 snprintf(device, sizeof(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 = 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;
127 * pcf_poll - called by the transmit procedure
135 struct refclockproc *pp;
143 if (read(pp->io.fd, buf, sizeof(buf)) < (ssize_t)sizeof(buf) || buf[0] != 9) {
144 refclock_report(peer, CEVNT_FAULT);
150 tm.tm_mday = buf[11] * 10 + buf[10];
151 tm.tm_mon = buf[13] * 10 + buf[12] - 1;
152 tm.tm_year = buf[15] * 10 + buf[14];
153 tm.tm_hour = buf[7] * 10 + buf[6];
154 tm.tm_min = buf[5] * 10 + buf[4];
155 tm.tm_sec = buf[3] * 10 + buf[2];
156 tm.tm_isdst = (buf[8] & 1) ? 1 : (buf[8] & 2) ? 0 : -1;
159 * Y2K convert the 2-digit year
165 if (t == (time_t) -1) {
166 refclock_report(peer, CEVNT_BADTIME);
170 #if defined(__GLIBC__) && defined(_BSD_SOURCE)
171 if ((tm.tm_isdst > 0 && tm.tm_gmtoff != 7200)
172 || (tm.tm_isdst == 0 && tm.tm_gmtoff != 3600)
173 || tm.tm_isdst < 0) {
176 printf ("local time zone not set to CET/CEST\n");
178 refclock_report(peer, CEVNT_BADTIME);
183 pp->lencode = strftime(pp->a_lastcode, BMAX, "%Y %m %d %H %M %S", &tm);
185 #if defined(_REENTRANT) || defined(_THREAD_SAFE)
186 tp = gmtime_r(&t, &tm);
191 refclock_report(peer, CEVNT_FAULT);
195 get_systime(&pp->lastrec);
197 pp->year = tp->tm_year + 1900;
198 pp->day = tp->tm_yday + 1;
199 pp->hour = tp->tm_hour;
200 pp->minute = tp->tm_min;
201 pp->second = tp->tm_sec;
202 pp->nsec = buf[16] * 31250000;
204 pp->nsec += 500000000;
208 printf ("pcf%d: time is %04d/%02d/%02d %02d:%02d:%02d UTC\n",
209 unit, pp->year, tp->tm_mon + 1, tp->tm_mday, pp->hour,
210 pp->minute, pp->second);
213 if (!refclock_process(pp)) {
214 refclock_report(peer, CEVNT_BADTIME);
217 record_clock_stats(&peer->srcadr, pp->a_lastcode);
218 if ((buf[1] & 1) && !(pp->sloppyclockflag & CLK_FLAG2))
219 pp->leap = LEAP_NOTINSYNC;
221 pp->leap = LEAP_NOWARNING;
222 pp->lastref = pp->lastrec;
223 refclock_receive(peer);
227 #endif /* REFCLOCK */