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