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>
28 # define TIOCMSET MCSETA
29 # define TIOCMGET MCGETA
30 # define TIOCM_RTS MRTS
34 # ifdef TERMIOS_NEEDS__SVID3
38 # ifdef TERMIOS_NEEDS__SVID3
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
50 #define DESCRIPTION "hopf Elektronik serial clock" /* Long name */
51 #define PRECISION (-10) /* precision assumed (about 1 ms) */
52 #define REFID "hopf\0" /* reference ID */
56 #define DEVICE "/dev/hopfclock%d" /* device name and unit */
57 #define SPEED232 B9600 /* uart speed (9600 baud) */
66 #define REC_QUEUE_EMPTY 0
67 #define REC_QUEUE_FULL 1
69 #define HOPF_OPMODE 0x0C /* operation mode mask */
70 #define HOPF_INVALID 0x00 /* no time code available */
71 #define HOPF_INTERNAL 0x04 /* internal clock */
72 #define HOPF_RADIO 0x08 /* radio clock */
73 #define HOPF_RADIOHP 0x0C /* high precision radio clock */
76 * hopfclock unit control structure.
78 struct hopfclock_unit {
79 l_fp laststamp; /* last receive timestamp */
80 short unit; /* NTP refclock unit number */
81 u_long polled; /* flag to detect noreplies */
82 char leap_status; /* leap second flag */
90 static int hopfserial_start P((int, struct peer *));
91 static void hopfserial_shutdown P((int, struct peer *));
92 static void hopfserial_receive P((struct recvbuf *));
93 static void hopfserial_poll P((int, struct peer *));
94 /* static void hopfserial_io P((struct recvbuf *)); */
98 struct refclock refclock_hopfser = {
99 hopfserial_start, /* start up driver */
100 hopfserial_shutdown, /* shut down driver */
101 hopfserial_poll, /* transmit poll message */
102 noentry, /* not used */
103 noentry, /* initialize driver (not used) */
104 noentry, /* not used */
105 NOFLAGS /* not used */
109 * hopfserial_start - open the devices and initialize data for processing
117 register struct hopfclock_unit *up;
118 struct refclockproc *pp;
123 (void) sprintf(gpsdev, "COM%d:", unit);
125 (void) sprintf(gpsdev, DEVICE, unit);
127 /* LDISC_STD, LDISC_RAW
128 * Open serial port. Use CLK line discipline, if available.
130 fd = refclock_open(gpsdev, SPEED232, LDISC_CLK);
133 printf("hopfSerialClock(%d) start: open %s failed\n", unit, gpsdev);
138 msyslog(LOG_NOTICE, "hopfSerialClock(%d) fd: %d dev: %s", unit, fd,
142 * Allocate and initialize unit structure
144 up = (struct hopfclock_unit *) emalloc(sizeof(struct hopfclock_unit));
147 msyslog(LOG_ERR, "hopfSerialClock(%d) emalloc: %m",unit);
149 printf("hopfSerialClock(%d) emalloc\n",unit);
155 memset((char *)up, 0, sizeof(struct hopfclock_unit));
157 pp->unitptr = (caddr_t)up;
158 pp->io.clock_recv = hopfserial_receive;
159 pp->io.srcclock = (caddr_t)peer;
162 if (!io_addclock(&pp->io)) {
164 printf("hopfSerialClock(%d) io_addclock\n",unit);
172 * Initialize miscellaneous variables
174 pp->clockdesc = DESCRIPTION;
175 peer->precision = PRECISION;
176 peer->burst = NSTAGE;
177 memcpy((char *)&pp->refid, REFID, 4);
180 up->unit = (short) unit;
187 * hopfserial_shutdown - shut down the clock
190 hopfserial_shutdown (
195 register struct hopfclock_unit *up;
196 struct refclockproc *pp;
199 up = (struct hopfclock_unit *)pp->unitptr;
200 io_closeclock(&pp->io);
207 * hopfserial_receive - receive data from the serial interface
212 struct recvbuf *rbufp
215 struct hopfclock_unit *up;
216 struct refclockproc *pp;
219 int sync; /* synchronization indicator */
222 int day, month; /* ddd conversion */
225 * Initialize pointers and read the timecode and timestamp.
227 peer = (struct peer *)rbufp->recv_srcclock;
229 up = (struct hopfclock_unit *)pp->unitptr;
231 if (up->rpt_next == 0 )
235 up->rpt_next = 0; /* wait until next poll interval occur */
237 pp->lencode = refclock_gtlin(rbufp, pp->a_lastcode, BMAX, &pp->lastrec);
239 if (pp->lencode == 0)
242 sscanf(pp->a_lastcode,
244 "%1x%1x%2d%2d%2d%2d%2d%2d", /* ...cr,lf */
246 "%*c%1x%1x%2d%2d%2d%2d%2d%2d", /* stx...cr,lf,etx */
259 Validate received values at least enough to prevent internal
260 array-bounds problems, etc.
262 if((pp->hour < 0) || (pp->hour > 23) ||
263 (pp->minute < 0) || (pp->minute > 59) ||
264 (pp->second < 0) || (pp->second > 60) /*Allow for leap seconds.*/ ||
265 (day < 1) || (day > 31) ||
266 (month < 1) || (month > 12) ||
267 (pp->year < 0) || (pp->year > 99)) {
268 /* Data out of range. */
269 refclock_report(peer, CEVNT_BADREPLY);
275 pp->day = ymd2yd(pp->year,month,day);
278 /* Year-2000 check! */
279 /* wrap 2-digit date into 4-digit */
281 if(pp->year < YEAR_PIVOT) { pp->year += 100; } /* < 98 */
284 /* preparation for timecode ntpq rl command ! */
287 wsprintf(pp->a_lastcode,
288 "STATUS: %1X%1X, DATE: %02d.%02d.%04d TIME: %02d:%02d:%02d",
298 pp->lencode = strlen(pp->a_lastcode);
299 if ((sync && 0xc) == 0 ){ /* time ok? */
300 refclock_report(peer, CEVNT_BADTIME);
301 pp->leap = LEAP_NOTINSYNC;
306 * If clock has no valid status then report error and exit
308 if ((sync & HOPF_OPMODE) == HOPF_INVALID ){ /* time ok? */
309 refclock_report(peer, CEVNT_BADTIME);
310 pp->leap = LEAP_NOTINSYNC;
315 * Test if time is running on internal quarz
316 * if CLK_FLAG1 is set, sychronize even if no radio operation
319 if ((sync & HOPF_OPMODE) == HOPF_INTERNAL){
320 if ((pp->sloppyclockflag & CLK_FLAG1) == 0) {
321 refclock_report(peer, CEVNT_BADTIME);
322 pp->leap = LEAP_NOTINSYNC;
328 if (!refclock_process(pp)) {
329 refclock_report(peer, CEVNT_BADTIME);
332 refclock_receive(peer);
335 msyslog(LOG_ERR, " D:%x D:%d D:%d",sync,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;
358 up = (struct hopfclock_unit *)pp->unitptr;
364 record_clock_stats(&peer->srcadr, pp->a_lastcode);
371 int refclock_hopfser_bs;
372 #endif /* REFCLOCK */