4 * - clock driver for hopf serial boards (GPS or DCF77)
6 * Date: 30.03.2000 Revision: 01.10
8 * latest source and further information can be found at:
9 * http://www.ATLSoft.de/ntp
17 #if defined(REFCLOCK) && (defined(CLOCK_HOPF_SERIAL))
21 #include "ntp_control.h"
22 #include "ntp_refclock.h"
23 #include "ntp_unixtime.h"
24 #include "ntp_stdlib.h"
26 #if defined HAVE_SYS_MODEM_H
27 # include <sys/modem.h>
29 # define TIOCMSET MCSETA
30 # define TIOCMGET MCGETA
31 # define TIOCM_RTS MRTS
36 # ifdef TERMIOS_NEEDS__SVID3
40 # ifdef TERMIOS_NEEDS__SVID3
45 #ifdef HAVE_SYS_IOCTL_H
46 # include <sys/ioctl.h>
50 extern int async_write(int, const void *, unsigned int);
52 #define write(fd, data, octets) async_write(fd, data, octets)
58 #define DESCRIPTION "hopf Elektronik serial clock" /* Long name */
59 #define PRECISION (-10) /* precision assumed (about 1 ms) */
60 #define REFID "hopf\0" /* reference ID */
64 #define DEVICE "/dev/hopfclock%d" /* device name and unit */
65 #define SPEED232 B9600 /* uart speed (9600 baud) */
74 #define REC_QUEUE_EMPTY 0
75 #define REC_QUEUE_FULL 1
77 #define HOPF_OPMODE 0x0C /* operation mode mask */
78 #define HOPF_INVALID 0x00 /* no time code available */
79 #define HOPF_INTERNAL 0x04 /* internal clock */
80 #define HOPF_RADIO 0x08 /* radio clock */
81 #define HOPF_RADIOHP 0x0C /* high precision radio clock */
84 * hopfclock unit control structure.
86 struct hopfclock_unit {
87 l_fp laststamp; /* last receive timestamp */
88 short unit; /* NTP refclock unit number */
89 u_long polled; /* flag to detect noreplies */
90 char leap_status; /* leap second flag */
98 static int hopfserial_start (int, struct peer *);
99 static void hopfserial_shutdown (int, struct peer *);
100 static void hopfserial_receive (struct recvbuf *);
101 static void hopfserial_poll (int, struct peer *);
102 /* static void hopfserial_io (struct recvbuf *); */
106 struct refclock refclock_hopfser = {
107 hopfserial_start, /* start up driver */
108 hopfserial_shutdown, /* shut down driver */
109 hopfserial_poll, /* transmit poll message */
110 noentry, /* not used */
111 noentry, /* initialize driver (not used) */
112 noentry, /* not used */
113 NOFLAGS /* not used */
117 * hopfserial_start - open the devices and initialize data for processing
125 register struct hopfclock_unit *up;
126 struct refclockproc *pp;
130 snprintf(gpsdev, sizeof(gpsdev), DEVICE, unit);
132 /* LDISC_STD, LDISC_RAW
133 * Open serial port. Use CLK line discipline, if available.
135 fd = refclock_open(gpsdev, SPEED232, LDISC_CLK);
138 printf("hopfSerialClock(%d) start: open %s failed\n", unit, gpsdev);
143 msyslog(LOG_NOTICE, "hopfSerialClock(%d) fd: %d dev: %s", unit, fd,
147 * Allocate and initialize unit structure
149 up = emalloc_zero(sizeof(*up));
152 pp->io.clock_recv = hopfserial_receive;
153 pp->io.srcclock = peer;
156 if (!io_addclock(&pp->io)) {
158 printf("hopfSerialClock(%d) io_addclock\n", unit);
168 * Initialize miscellaneous variables
170 pp->clockdesc = DESCRIPTION;
171 peer->precision = PRECISION;
172 memcpy((char *)&pp->refid, REFID, 4);
175 up->unit = (short) unit;
182 * hopfserial_shutdown - shut down the clock
185 hopfserial_shutdown (
190 register struct hopfclock_unit *up;
191 struct refclockproc *pp;
197 io_closeclock(&pp->io);
205 * hopfserial_receive - receive data from the serial interface
210 struct recvbuf *rbufp
213 struct hopfclock_unit *up;
214 struct refclockproc *pp;
217 int synch; /* synchhronization indicator */
218 int DoW; /* Day of Week */
220 int day, month; /* ddd conversion */
224 * Initialize pointers and read the timecode and timestamp.
226 peer = rbufp->recv_peer;
230 if (up->rpt_next == 0 )
233 up->rpt_next = 0; /* wait until next poll interval occur */
235 pp->lencode = (u_short)refclock_gtlin(rbufp, pp->a_lastcode,
236 sizeof(pp->a_lastcode),
238 if (pp->lencode == 0)
241 converted = sscanf(pp->a_lastcode,
243 "%1x%1x%2d%2d%2d%2d%2d%2d", /* ...cr,lf */
245 "%*c%1x%1x%2d%2d%2d%2d%2d%2d", /* stx...cr,lf,etx */
258 Validate received values at least enough to prevent internal
259 array-bounds problems, etc.
261 if ((8 != converted) || (pp->hour < 0) || (pp->hour > 23) ||
262 (pp->minute < 0) || (pp->minute > 59) || (pp->second < 0) ||
263 (pp->second > 60) /*Allow for leap seconds.*/ ||
264 (day < 1) || (day > 31) ||
265 (month < 1) || (month > 12) ||
266 (pp->year < 0) || (pp->year > 99)) {
267 /* Data out of range. */
268 refclock_report(peer, CEVNT_BADREPLY);
274 pp->day = ymd2yd(pp->year,month,day);
277 /* Year-2000 check! */
278 /* wrap 2-digit date into 4-digit */
280 if(pp->year < YEAR_PIVOT) { pp->year += 100; } /* < 98 */
283 /* preparation for timecode ntpq rl command ! */
286 snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
287 "STATUS: %1X%1X, DATE: %02d.%02d.%04d TIME: %02d:%02d:%02d",
297 pp->lencode = strlen(pp->a_lastcode);
298 if ((synch && 0xc) == 0 ){ /* time ok? */
299 refclock_report(peer, CEVNT_BADTIME);
300 pp->leap = LEAP_NOTINSYNC;
305 * If clock has no valid status then report error and exit
307 if ((synch & HOPF_OPMODE) == HOPF_INVALID ){ /* time ok? */
308 refclock_report(peer, CEVNT_BADTIME);
309 pp->leap = LEAP_NOTINSYNC;
314 * Test if time is running on internal quarz
315 * if CLK_FLAG1 is set, sychronize even if no radio operation
318 if ((synch & HOPF_OPMODE) == HOPF_INTERNAL){
319 if ((pp->sloppyclockflag & CLK_FLAG1) == 0) {
320 refclock_report(peer, CEVNT_BADTIME);
321 pp->leap = LEAP_NOTINSYNC;
327 if (!refclock_process(pp)) {
328 refclock_report(peer, CEVNT_BADTIME);
331 pp->lastref = pp->lastrec;
332 refclock_receive(peer);
335 msyslog(LOG_ERR, " D:%x D:%d D:%d",synch,pp->minute,pp->second);
338 record_clock_stats(&peer->srcadr, pp->a_lastcode);
345 * hopfserial_poll - called by the transmit procedure
354 register struct hopfclock_unit *up;
355 struct refclockproc *pp;
364 record_clock_stats(&peer->srcadr, pp->a_lastcode);
371 int refclock_hopfser_bs;
372 #endif /* REFCLOCK */