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