Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / ntpd / refclock_pcf.c
blobabb8617a148932d589bd8c38eb46f035b0851bf9
1 /* $NetBSD$ */
3 /*
4 * refclock_pcf - clock driver for the Conrad parallel port radio clock
5 */
7 #ifdef HAVE_CONFIG_H
8 # include <config.h>
9 #endif
11 #if defined(REFCLOCK) && defined(CLOCK_PCF)
13 #include "ntpd.h"
14 #include "ntp_io.h"
15 #include "ntp_refclock.h"
16 #include "ntp_calendar.h"
17 #include "ntp_stdlib.h"
20 * This driver supports the parallel port radio clock sold by Conrad
21 * Electronic under order numbers 967602 and 642002.
23 * It requires that the local timezone be CET/CEST and that the pcfclock
24 * device driver be installed. A device driver for Linux is available at
25 * http://home.pages.de/~voegele/pcf.html. Information about a FreeBSD
26 * driver is available at http://schumann.cx/pcfclock/.
30 * Interface definitions
32 #define DEVICE "/dev/pcfclocks/%d"
33 #define OLDDEVICE "/dev/pcfclock%d"
34 #define PRECISION (-1) /* precision assumed (about 0.5 s) */
35 #define REFID "PCF"
36 #define DESCRIPTION "Conrad parallel port radio clock"
38 #define LENPCF 18 /* timecode length */
41 * Function prototypes
43 static int pcf_start P((int, struct peer *));
44 static void pcf_shutdown P((int, struct peer *));
45 static void pcf_poll P((int, struct peer *));
48 * Transfer vector
50 struct refclock refclock_pcf = {
51 pcf_start, /* start up driver */
52 pcf_shutdown, /* shut down driver */
53 pcf_poll, /* transmit poll message */
54 noentry, /* not used */
55 noentry, /* initialize driver (not used) */
56 noentry, /* not used */
57 NOFLAGS /* not used */
62 * pcf_start - open the device and initialize data for processing
64 static int
65 pcf_start(
66 int unit,
67 struct peer *peer
70 struct refclockproc *pp;
71 int fd;
72 char device[128];
75 * Open device file for reading.
77 (void)sprintf(device, DEVICE, unit);
78 fd = open(device, O_RDONLY);
79 if (fd == -1) {
80 (void)sprintf(device, OLDDEVICE, unit);
81 fd = open(device, O_RDONLY);
83 #ifdef DEBUG
84 if (debug)
85 printf ("starting PCF with device %s\n",device);
86 #endif
87 if (fd == -1) {
88 return (0);
91 pp = peer->procptr;
92 pp->io.clock_recv = noentry;
93 pp->io.srcclock = (caddr_t)peer;
94 pp->io.datalen = 0;
95 pp->io.fd = fd;
98 * Initialize miscellaneous variables
100 peer->precision = PRECISION;
101 pp->clockdesc = DESCRIPTION;
102 /* one transmission takes 172.5 milliseconds since the radio clock
103 transmits 69 bits with a period of 2.5 milliseconds per bit */
104 pp->fudgetime1 = 0.1725;
105 memcpy((char *)&pp->refid, REFID, 4);
107 return (1);
112 * pcf_shutdown - shut down the clock
114 static void
115 pcf_shutdown(
116 int unit,
117 struct peer *peer
120 struct refclockproc *pp;
122 pp = peer->procptr;
123 (void)close(pp->io.fd);
128 * pcf_poll - called by the transmit procedure
130 static void
131 pcf_poll(
132 int unit,
133 struct peer *peer
136 struct refclockproc *pp;
137 char buf[LENPCF];
138 struct tm tm, *tp;
139 time_t t;
141 pp = peer->procptr;
143 buf[0] = 0;
144 if (read(pp->io.fd, buf, sizeof(buf)) < sizeof(buf) || buf[0] != 9) {
145 refclock_report(peer, CEVNT_FAULT);
146 return;
149 tm.tm_mday = buf[11] * 10 + buf[10];
150 tm.tm_mon = buf[13] * 10 + buf[12] - 1;
151 tm.tm_year = buf[15] * 10 + buf[14];
152 tm.tm_hour = buf[7] * 10 + buf[6];
153 tm.tm_min = buf[5] * 10 + buf[4];
154 tm.tm_sec = buf[3] * 10 + buf[2];
155 tm.tm_isdst = (buf[8] & 1) ? 1 : (buf[8] & 2) ? 0 : -1;
158 * Y2K convert the 2-digit year
160 if (tm.tm_year < 99)
161 tm.tm_year += 100;
163 t = mktime(&tm);
164 if (t == (time_t) -1) {
165 refclock_report(peer, CEVNT_BADTIME);
166 return;
169 #if defined(__GLIBC__) && defined(_BSD_SOURCE)
170 if ((tm.tm_isdst > 0 && tm.tm_gmtoff != 7200)
171 || (tm.tm_isdst == 0 && tm.tm_gmtoff != 3600)
172 || tm.tm_isdst < 0) {
173 #ifdef DEBUG
174 if (debug)
175 printf ("local time zone not set to CET/CEST\n");
176 #endif
177 refclock_report(peer, CEVNT_BADTIME);
178 return;
180 #endif
182 pp->lencode = strftime(pp->a_lastcode, BMAX, "%Y %m %d %H %M %S", &tm);
184 #if defined(_REENTRANT) || defined(_THREAD_SAFE)
185 tp = gmtime_r(&t, &tm);
186 #else
187 tp = gmtime(&t);
188 #endif
189 if (!tp) {
190 refclock_report(peer, CEVNT_FAULT);
191 return;
194 get_systime(&pp->lastrec);
195 pp->polls++;
196 pp->year = tp->tm_year + 1900;
197 pp->day = tp->tm_yday + 1;
198 pp->hour = tp->tm_hour;
199 pp->minute = tp->tm_min;
200 pp->second = tp->tm_sec;
201 pp->nsec = buf[16] * 31250000;
202 if (buf[17] & 1)
203 pp->nsec += 500000000;
205 #ifdef DEBUG
206 if (debug)
207 printf ("pcf%d: time is %04d/%02d/%02d %02d:%02d:%02d UTC\n",
208 unit, pp->year, tp->tm_mon + 1, tp->tm_mday, pp->hour,
209 pp->minute, pp->second);
210 #endif
212 if (!refclock_process(pp)) {
213 refclock_report(peer, CEVNT_BADTIME);
214 return;
216 record_clock_stats(&peer->srcadr, pp->a_lastcode);
217 if ((buf[1] & 1) && !(pp->sloppyclockflag & CLK_FLAG2))
218 pp->leap = LEAP_NOTINSYNC;
219 else
220 pp->leap = LEAP_NOWARNING;
221 pp->lastref = pp->lastrec;
222 refclock_receive(peer);
224 #else
225 int refclock_pcf_bs;
226 #endif /* REFCLOCK */