1 /* 2 * refclock_tpro - clock driver for the KSI/Odetics TPRO-S IRIG-B reader 3 */ 4 5 #ifdef HAVE_CONFIG_H 6 #include <config.h> 7 #endif 8 9 #if defined(REFCLOCK) && defined(CLOCK_TPRO) 10 11 #include <stdio.h> 12 #include <ctype.h> 13 #include <sys/time.h> 14 15 #include "ntpd.h" 16 #include "ntp_io.h" 17 #include "ntp_refclock.h" 18 #include "ntp_unixtime.h" 19 #include "sys/tpro.h" 20 #include "ntp_stdlib.h" 21 22 /* 23 * This driver supports the KSI/Odetecs TPRO-S IRIG-B reader and TPRO- 24 * SAT GPS receiver for the Sun Microsystems SBus. It requires that the 25 * tpro.o device driver be installed and loaded. 26 */ 27 28 /* 29 * TPRO interface definitions 30 */ 31 #define DEVICE "/dev/tpro%d" /* device name and unit */ 32 #define PRECISION (-20) /* precision assumed (1 us) */ 33 #define REFID "IRIG" /* reference ID */ 34 #define DESCRIPTION "KSI/Odetics TPRO/S IRIG Interface" /* WRU */ 35 36 /* 37 * Unit control structure 38 */ 39 struct tprounit { 40 struct tproval tprodata; /* data returned from tpro read */ 41 }; 42 43 /* 44 * Function prototypes 45 */ 46 static int tpro_start P((int, struct peer *)); 47 static void tpro_shutdown P((int, struct peer *)); 48 static void tpro_poll P((int unit, struct peer *)); 49 50 /* 51 * Transfer vector 52 */ 53 struct refclock refclock_tpro = { 54 tpro_start, /* start up driver */ 55 tpro_shutdown, /* shut down driver */ 56 tpro_poll, /* transmit poll message */ 57 noentry, /* not used (old tpro_control) */ 58 noentry, /* initialize driver (not used) */ 59 noentry, /* not used (old tpro_buginfo) */ 60 NOFLAGS /* not used */ 61 }; 62 63 64 /* 65 * tpro_start - open the TPRO device and initialize data for processing 66 */ 67 static int 68 tpro_start( 69 int unit, 70 struct peer *peer 71 ) 72 { 73 register struct tprounit *up; 74 struct refclockproc *pp; 75 char device[20]; 76 int fd; 77 78 /* 79 * Open TPRO device 80 */ 81 (void)sprintf(device, DEVICE, unit); 82 fd = open(device, O_RDONLY | O_NDELAY, 0777); 83 if (fd == -1) { 84 msyslog(LOG_ERR, "tpro_start: open of %s: %m", device); 85 return (0); 86 } 87 88 /* 89 * Allocate and initialize unit structure 90 */ 91 if (!(up = (struct tprounit *) emalloc(sizeof(struct tprounit)))) { 92 (void) close(fd); 93 return (0); 94 } 95 memset((char *)up, 0, sizeof(struct tprounit)); 96 pp = peer->procptr; 97 pp->io.clock_recv = noentry; 98 pp->io.srcclock = (caddr_t)peer; 99 pp->io.datalen = 0; 100 pp->io.fd = fd; 101 pp->unitptr = (caddr_t)up; 102 103 /* 104 * Initialize miscellaneous peer variables 105 */ 106 peer->precision = PRECISION; 107 peer->burst = NSTAGE; 108 pp->clockdesc = DESCRIPTION; 109 memcpy((char *)&pp->refid, REFID, 4); 110 return (1); 111 } 112 113 114 /* 115 * tpro_shutdown - shut down the clock 116 */ 117 static void 118 tpro_shutdown( 119 int unit, 120 struct peer *peer 121 ) 122 { 123 register struct tprounit *up; 124 struct refclockproc *pp; 125 126 pp = peer->procptr; 127 up = (struct tprounit *)pp->unitptr; 128 io_closeclock(&pp->io); 129 free(up); 130 } 131 132 133 /* 134 * tpro_poll - called by the transmit procedure 135 */ 136 static void 137 tpro_poll( 138 int unit, 139 struct peer *peer 140 ) 141 { 142 register struct tprounit *up; 143 struct refclockproc *pp; 144 struct tproval *tp; 145 146 /* 147 * This is the main routine. It snatches the time from the TPRO 148 * board and tacks on a local timestamp. 149 */ 150 pp = peer->procptr; 151 up = (struct tprounit *)pp->unitptr; 152 153 tp = &up->tprodata; 154 if (read(pp->io.fd, (char *)tp, sizeof(struct tproval)) < 0) { 155 refclock_report(peer, CEVNT_FAULT); 156 return; 157 } 158 get_systime(&pp->lastrec); 159 pp->polls++; 160 161 /* 162 * We get down to business, check the timecode format and decode 163 * its contents. If the timecode has invalid length or is not in 164 * proper format, we declare bad format and exit. Note: we 165 * can't use the sec/usec conversion produced by the driver, 166 * since the year may be suspect. All format error checking is 167 * done by the sprintf() and sscanf() routines. 168 */ 169 sprintf(pp->a_lastcode, 170 "%1x%1x%1x %1x%1x:%1x%1x:%1x%1x.%1x%1x%1x%1x%1x%1x %1x", 171 tp->day100, tp->day10, tp->day1, tp->hour10, tp->hour1, 172 tp->min10, tp->min1, tp->sec10, tp->sec1, tp->ms100, 173 tp->ms10, tp->ms1, tp->usec100, tp->usec10, tp->usec1, 174 tp->status); 175 pp->lencode = strlen(pp->a_lastcode); 176 #ifdef DEBUG 177 if (debug) 178 printf("tpro: time %s timecode %d %s\n", 179 ulfptoa(&pp->lastrec, 6), pp->lencode, 180 pp->a_lastcode); 181 #endif 182 if (sscanf(pp->a_lastcode, "%3d %2d:%2d:%2d.%6ld", &pp->day, 183 &pp->hour, &pp->minute, &pp->second, &pp->usec) 184 != 5) { 185 refclock_report(peer, CEVNT_BADTIME); 186 return; 187 } 188 if (!tp->status & 0x3) 189 pp->leap = LEAP_NOTINSYNC; 190 else 191 pp->leap = LEAP_NOWARNING; 192 if (!refclock_process(pp)) { 193 refclock_report(peer, CEVNT_BADTIME); 194 return; 195 } 196 if (peer->burst > 0) 197 return; 198 if (pp->coderecv == pp->codeproc) { 199 refclock_report(peer, CEVNT_TIMEOUT); 200 return; 201 } 202 record_clock_stats(&peer->srcadr, pp->a_lastcode); 203 refclock_receive(peer); 204 peer->burst = NSTAGE; 205 } 206 207 #else 208 int refclock_tpro_bs; 209 #endif /* REFCLOCK */ 210