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(SYS_WINNT)
19 #define close closesocket
22 #if defined(REFCLOCK) && (defined(CLOCK_HOPF_SERIAL))
26 #include "ntp_control.h"
27 #include "ntp_refclock.h"
28 #include "ntp_unixtime.h"
29 #include "ntp_stdlib.h"
31 #if defined HAVE_SYS_MODEM_H
32 # include <sys/modem.h>
33 # define TIOCMSET MCSETA
34 # define TIOCMGET MCGETA
35 # define TIOCM_RTS MRTS
39 # ifdef TERMIOS_NEEDS__SVID3
43 # ifdef TERMIOS_NEEDS__SVID3
48 #ifdef HAVE_SYS_IOCTL_H
49 # include <sys/ioctl.h>
55 #define DESCRIPTION "hopf Elektronik serial clock" /* Long name */
56 #define PRECISION (-10) /* precision assumed (about 1 ms) */
57 #define REFID "hopf\0" /* reference ID */
61 #define DEVICE "/dev/hopfclock%d" /* device name and unit */
62 #define SPEED232 B9600 /* uart speed (9600 baud) */
71 #define REC_QUEUE_EMPTY 0
72 #define REC_QUEUE_FULL 1
74 #define HOPF_OPMODE 0x0C /* operation mode mask */
75 #define HOPF_INVALID 0x00 /* no time code available */
76 #define HOPF_INTERNAL 0x04 /* internal clock */
77 #define HOPF_RADIO 0x08 /* radio clock */
78 #define HOPF_RADIOHP 0x0C /* high precision radio clock */
81 * hopfclock unit control structure.
83 struct hopfclock_unit {
84 l_fp laststamp; /* last receive timestamp */
85 short unit; /* NTP refclock unit number */
86 u_long polled; /* flag to detect noreplies */
87 char leap_status; /* leap second flag */
95 static int hopfserial_start P((int, struct peer *));
96 static void hopfserial_shutdown P((int, struct peer *));
97 static void hopfserial_receive P((struct recvbuf *));
98 static void hopfserial_poll P((int, struct peer *));
99 /* static void hopfserial_io P((struct recvbuf *)); */
103 struct refclock refclock_hopfser = {
104 hopfserial_start, /* start up driver */
105 hopfserial_shutdown, /* shut down driver */
106 hopfserial_poll, /* transmit poll message */
107 noentry, /* not used */
108 noentry, /* initialize driver (not used) */
109 noentry, /* not used */
110 NOFLAGS /* not used */
114 * hopfserial_start - open the devices and initialize data for processing
122 register struct hopfclock_unit *up;
123 struct refclockproc *pp;
128 (void) sprintf(gpsdev, "COM%d:", unit);
130 (void) sprintf(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 = (struct hopfclock_unit *) emalloc(sizeof(struct hopfclock_unit));
152 msyslog(LOG_ERR, "hopfSerialClock(%d) emalloc: %m",unit);
154 printf("hopfSerialClock(%d) emalloc\n",unit);
160 memset((char *)up, 0, sizeof(struct hopfclock_unit));
162 pp->unitptr = (caddr_t)up;
163 pp->io.clock_recv = hopfserial_receive;
164 pp->io.srcclock = (caddr_t)peer;
167 if (!io_addclock(&pp->io)) {
169 printf("hopfSerialClock(%d) io_addclock\n",unit);
177 * Initialize miscellaneous variables
179 pp->clockdesc = DESCRIPTION;
180 peer->precision = PRECISION;
181 peer->burst = NSTAGE;
182 memcpy((char *)&pp->refid, REFID, 4);
185 up->unit = (short) unit;
192 * hopfserial_shutdown - shut down the clock
195 hopfserial_shutdown (
200 register struct hopfclock_unit *up;
201 struct refclockproc *pp;
204 up = (struct hopfclock_unit *)pp->unitptr;
205 io_closeclock(&pp->io);
212 * hopfserial_receive - receive data from the serial interface
217 struct recvbuf *rbufp
220 struct hopfclock_unit *up;
221 struct refclockproc *pp;
224 int synch; /* synchhronization indicator */
227 int day, month; /* ddd conversion */
230 * Initialize pointers and read the timecode and timestamp.
232 peer = (struct peer *)rbufp->recv_srcclock;
234 up = (struct hopfclock_unit *)pp->unitptr;
236 if (up->rpt_next == 0 )
240 up->rpt_next = 0; /* wait until next poll interval occur */
242 pp->lencode = (u_short)refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &pp->lastrec);
244 if (pp->lencode == 0)
247 sscanf(pp->a_lastcode,
249 "%1x%1x%2d%2d%2d%2d%2d%2d", /* ...cr,lf */
251 "%*c%1x%1x%2d%2d%2d%2d%2d%2d", /* stx...cr,lf,etx */
264 Validate received values at least enough to prevent internal
265 array-bounds problems, etc.
267 if((pp->hour < 0) || (pp->hour > 23) ||
268 (pp->minute < 0) || (pp->minute > 59) ||
269 (pp->second < 0) || (pp->second > 60) /*Allow for leap seconds.*/ ||
270 (day < 1) || (day > 31) ||
271 (month < 1) || (month > 12) ||
272 (pp->year < 0) || (pp->year > 99)) {
273 /* Data out of range. */
274 refclock_report(peer, CEVNT_BADREPLY);
280 pp->day = ymd2yd(pp->year,month,day);
283 /* Year-2000 check! */
284 /* wrap 2-digit date into 4-digit */
286 if(pp->year < YEAR_PIVOT) { pp->year += 100; } /* < 98 */
289 /* preparation for timecode ntpq rl command ! */
292 wsprintf(pp->a_lastcode,
293 "STATUS: %1X%1X, DATE: %02d.%02d.%04d TIME: %02d:%02d:%02d",
303 pp->lencode = strlen(pp->a_lastcode);
304 if ((synch && 0xc) == 0 ){ /* time ok? */
305 refclock_report(peer, CEVNT_BADTIME);
306 pp->leap = LEAP_NOTINSYNC;
311 * If clock has no valid status then report error and exit
313 if ((synch & HOPF_OPMODE) == HOPF_INVALID ){ /* time ok? */
314 refclock_report(peer, CEVNT_BADTIME);
315 pp->leap = LEAP_NOTINSYNC;
320 * Test if time is running on internal quarz
321 * if CLK_FLAG1 is set, sychronize even if no radio operation
324 if ((synch & HOPF_OPMODE) == HOPF_INTERNAL){
325 if ((pp->sloppyclockflag & CLK_FLAG1) == 0) {
326 refclock_report(peer, CEVNT_BADTIME);
327 pp->leap = LEAP_NOTINSYNC;
333 if (!refclock_process(pp)) {
334 refclock_report(peer, CEVNT_BADTIME);
337 pp->lastref = pp->lastrec;
338 refclock_receive(peer);
341 msyslog(LOG_ERR, " D:%x D:%d D:%d",synch,pp->minute,pp->second);
344 record_clock_stats(&peer->srcadr, pp->a_lastcode);
351 * hopfserial_poll - called by the transmit procedure
360 register struct hopfclock_unit *up;
361 struct refclockproc *pp;
364 up = (struct hopfclock_unit *)pp->unitptr;
370 record_clock_stats(&peer->srcadr, pp->a_lastcode);
377 int refclock_hopfser_bs;
378 #endif /* REFCLOCK */