xref: /freebsd/contrib/ntp/libparse/clk_trimtsip.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
1 /*
2  * /src/NTP/ntp-4/libparse/clk_trimtsip.c,v 4.13 1999/11/28 09:13:51 kardel RELEASE_19991128_A
3  *
4  * clk_trimtsip.c,v 4.13 1999/11/28 09:13:51 kardel RELEASE_19991128_A
5  *
6  * Trimble TSIP support - CURRENTLY VERY MUCH UNDER CONSTRUCTION
7  */
8 
9 #ifdef HAVE_CONFIG_H
10 # include <config.h>
11 #endif
12 
13 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTSIP)
14 
15 #include "ntp_syslog.h"
16 #include "ntp_types.h"
17 #include "ntp_fp.h"
18 #include "ntp_unixtime.h"
19 #include "ntp_calendar.h"
20 #include "ntp_machine.h"
21 #include "ntp_stdlib.h"
22 
23 #include "parse.h"
24 
25 #ifndef PARSESTREAM
26 # include <stdio.h>
27 #else
28 # include "sys/parsestreams.h"
29 #endif
30 
31 #include "ascii.h"
32 #include "binio.h"
33 #include "ieee754io.h"
34 #include "trimble.h"
35 
36 /*
37  * Trimble low level TSIP parser / time converter
38  *
39  * The receiver uses a serial message protocol called Trimble Standard
40  * Interface Protocol (it can support others but this driver only supports
41  * TSIP). Messages in this protocol have the following form:
42  *
43  * <DLE><id> ... <data> ... <DLE><ETX>
44  *
45  * Any bytes within the <data> portion of value 10 hex (<DLE>) are doubled
46  * on transmission and compressed back to one on reception. Otherwise
47  * the values of data bytes can be anything. The serial interface is RS-422
48  * asynchronous using 9600 baud, 8 data bits with odd party (**note** 9 bits
49  * in total!), and 1 stop bit. The protocol supports byte, integer, single,
50  * and double datatypes. Integers are two bytes, sent most significant first.
51  * Singles are IEEE754 single precision floating point numbers (4 byte) sent
52  * sign & exponent first. Doubles are IEEE754 double precision floating point
53  * numbers (8 byte) sent sign & exponent first.
54  * The receiver supports a large set of messages, only a very small subset of
55  * which is used here.
56  *
57  * From this module the following are recognised:
58  *
59  *  ID    Description
60  *
61  *  41    GPS Time
62  *  46    Receiver health
63  *  4F    UTC correction data (used to get leap second warnings)
64  *
65  * All others are accepted but ignored for time conversion - they are passed up to higher layers.
66  *
67  */
68 
69 static offsets_t trim_offsets = { 0, 1, 2, 3, 4, 5, 6, 7 };
70 
71 struct trimble
72 {
73 	u_char  t_in_pkt;	/* first DLE received */
74 	u_char  t_dle;		/* subsequent DLE received */
75 	u_short t_week;		/* GPS week */
76 	u_short t_weekleap;	/* GPS week of next/last week */
77 	u_short t_dayleap;	/* day in week */
78 	u_short t_gpsutc;	/* GPS - UTC offset */
79 	u_short t_gpsutcleap;	/* offset at next/last leap */
80 	u_char  t_operable;	/* receiver feels OK */
81 	u_char  t_mode;		/* actual operating mode */
82 	u_char  t_leap;		/* possible leap warning */
83         u_char  t_utcknown;	/* utc offset known */
84 };
85 
86 #define STATUS_BAD    0		/* BAD or UNINITIALIZED receiver status */
87 #define STATUS_UNSAFE 1		/* not enough receivers for full precision */
88 #define STATUS_SYNC   2		/* enough information for good operation */
89 
90 static unsigned long inp_tsip P((parse_t *, unsigned int, timestamp_t *));
91 static unsigned long cvt_trimtsip P((unsigned char *, int, struct format *, clocktime_t *, void *));
92 
93 struct clockformat clock_trimtsip =
94 {
95 	inp_tsip,		/* Trimble TSIP input handler */
96 	cvt_trimtsip,		/* Trimble TSIP conversion */
97 	pps_one,		/* easy PPS monitoring */
98 	0,			/* no configuration data */
99 	"Trimble TSIP",
100 	400,			/* input buffer */
101 	sizeof(struct trimble)	/* private data */
102 };
103 
104 #define ADDSECOND	0x01
105 #define DELSECOND	0x02
106 
107 static unsigned long
108 inp_tsip(
109 	 parse_t      *parseio,
110 	 unsigned int ch,
111 	 timestamp_t  *tstamp
112 	)
113 {
114 	struct trimble *t = (struct trimble *)parseio->parse_pdata;
115 
116 	if (!t)
117 	    return PARSE_INP_SKIP;		/* local data not allocated - sigh! */
118 
119 	if (!t->t_in_pkt && ch != DLE) {
120 		/* wait for start of packet */
121 		return PARSE_INP_SKIP;
122 	}
123 
124 	if ((parseio->parse_index >= (parseio->parse_dsize - 2)) ||
125 	    (parseio->parse_dtime.parse_msglen >= (sizeof(parseio->parse_dtime.parse_msg) - 2)))
126 		{		/* OVERFLOW - DROP! */
127 			t->t_in_pkt = t->t_dle = 0;
128 			parseio->parse_index = 0;
129 			parseio->parse_dtime.parse_msglen = 0;
130 		return PARSE_INP_SKIP;
131 	}
132 
133 	switch (ch) {
134 	    case DLE:
135 		if (!t->t_in_pkt) {
136 			t->t_dle = 0;
137 			t->t_in_pkt = 1;
138 			parseio->parse_index = 0;
139 			parseio->parse_data[parseio->parse_index++] = ch;
140 			parseio->parse_dtime.parse_msglen = 0;
141 			parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
142 			parseio->parse_dtime.parse_stime = *tstamp; /* pick up time stamp at packet start */
143 		} else if (t->t_dle) {
144 			/* Double DLE -> insert a DLE */
145 			t->t_dle = 0;
146 			parseio->parse_data[parseio->parse_index++] = DLE;
147 			parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = DLE;
148 		} else
149 		    t->t_dle = 1;
150 		break;
151 
152 	    case ETX:
153 		if (t->t_dle) {
154 			/* DLE,ETX -> end of packet */
155 			parseio->parse_data[parseio->parse_index++] = DLE;
156 			parseio->parse_data[parseio->parse_index] = ch;
157 			parseio->parse_ldsize = parseio->parse_index+1;
158 			memcpy(parseio->parse_ldata, parseio->parse_data, parseio->parse_ldsize);
159 			parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = DLE;
160 			parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
161 			t->t_in_pkt = t->t_dle = 0;
162 			return PARSE_INP_TIME|PARSE_INP_DATA;
163 		}
164 
165 	    default:		/* collect data */
166 		t->t_dle = 0;
167 		parseio->parse_data[parseio->parse_index++] = ch;
168 		parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
169 	}
170 
171   return PARSE_INP_SKIP;
172 }
173 
174 static int
175 getshort(
176 	 unsigned char *p
177 	 )
178 {
179 	return get_msb_short(&p);
180 }
181 
182 /*
183  * cvt_trimtsip
184  *
185  * convert TSIP type format
186  */
187 static unsigned long
188 cvt_trimtsip(
189 	     unsigned char *buffer,
190 	     int            size,
191 	     struct format *format,
192 	     clocktime_t   *clock_time,
193 	     void          *local
194 	     )
195 {
196         register struct trimble *t = (struct trimble *)local; /* get local data space */
197 #define mb(_X_) (buffer[2+(_X_)]) /* shortcut for buffer access */
198 	register u_char cmd;
199 
200 	clock_time->flags = 0;
201 
202 	if (!t) {
203 		return CVT_NONE;		/* local data not allocated - sigh! */
204 	}
205 
206 	if ((size < 4) ||
207 	    (buffer[0]      != DLE) ||
208 	    (buffer[size-1] != ETX) ||
209 	    (buffer[size-2] != DLE))
210 	{
211 		printf("TRIMBLE BAD packet, size %d:\n", size);
212 		return CVT_NONE;
213 	}
214 	else
215 	{
216 		unsigned char *bp;
217 		cmd = buffer[1];
218 
219 		    switch(cmd)
220 		    {
221 		    case CMD_RCURTIME:
222 			    {			/* GPS time */
223 				    l_fp secs;
224 				    int   week = getshort((unsigned char *)&mb(4));
225 				    l_fp utcoffset;
226 				    l_fp gpstime;
227 
228 				    bp = &mb(0);
229 				    if (fetch_ieee754(&bp, IEEE_SINGLE, &secs, trim_offsets) != IEEE_OK)
230 					    return CVT_FAIL|CVT_BADFMT;
231 
232 				    if ((secs.l_i <= 0) ||
233 					(t->t_utcknown == 0))
234 				    {
235 					    clock_time->flags = PARSEB_POWERUP;
236 					    return CVT_OK;
237 				    }
238 				    if (week < 990) {
239 					    week += 1024;
240 				    }
241 
242 				    /* time OK */
243 
244 				    /* fetch UTC offset */
245 				    bp = &mb(6);
246 				    if (fetch_ieee754(&bp, IEEE_SINGLE, &utcoffset, trim_offsets) != IEEE_OK)
247 					    return CVT_FAIL|CVT_BADFMT;
248 
249 				    L_SUB(&secs, &utcoffset); /* adjust GPS time to UTC time */
250 
251 				    gpstolfp((unsigned short)week, (unsigned short)0,
252 					     secs.l_ui, &gpstime);
253 
254 				    gpstime.l_uf = secs.l_uf;
255 
256 				    clock_time->utctime = gpstime.l_ui - JAN_1970;
257 
258 				    TSFTOTVU(gpstime.l_uf, clock_time->usecond);
259 
260 				    if (t->t_leap == ADDSECOND)
261 					clock_time->flags |= PARSEB_LEAPADD;
262 
263 				    if (t->t_leap == DELSECOND)
264 					clock_time->flags |= PARSEB_LEAPDEL;
265 
266 				    switch (t->t_operable)
267 				      {
268 				      case STATUS_SYNC:
269 					clock_time->flags &= ~(PARSEB_POWERUP|PARSEB_NOSYNC);
270 					break;
271 
272 				      case STATUS_UNSAFE:
273 					clock_time->flags |= PARSEB_NOSYNC;
274 					break;
275 
276 				      case STATUS_BAD:
277 					clock_time->flags |= PARSEB_NOSYNC|PARSEB_POWERUP;
278 					break;
279 				      }
280 
281 				    if (t->t_mode == 0)
282 					    clock_time->flags |= PARSEB_POSITION;
283 
284 				    clock_time->flags |= PARSEB_S_LEAP|PARSEB_S_POSITION;
285 
286 				    return CVT_OK;
287 
288 			    } /* case 0x41 */
289 
290 		    case CMD_RRECVHEALTH:
291 			    {
292 				    /* TRIMBLE health */
293 				    u_char status = mb(0);
294 
295 				    switch (status)
296 				    {
297 				      case 0x00: /* position fixes */
298 					t->t_operable = STATUS_SYNC;
299 					break;
300 
301 				      case 0x09: /* 1 satellite */
302 				      case 0x0A: /* 2 satellites */
303 				      case 0x0B: /* 3 satellites */
304 					t->t_operable = STATUS_UNSAFE;
305 					break;
306 
307 				      default:
308 					t->t_operable = STATUS_BAD;
309 					break;
310 				    }
311 				    t->t_mode = status;
312 			    }
313 			    break;
314 
315 		    case CMD_RUTCPARAM:
316 			    {
317 			            l_fp t0t;
318 				    unsigned char *lbp;
319 
320 				    /* UTC correction data - derive a leap warning */
321 				    int tls   = t->t_gpsutc     = getshort((unsigned char *)&mb(12)); /* current leap correction (GPS-UTC) */
322 				    int tlsf  = t->t_gpsutcleap = getshort((unsigned char *)&mb(24)); /* new leap correction */
323 
324 				    t->t_weekleap   = getshort((unsigned char *)&mb(20)); /* week no of leap correction */
325 				    if (t->t_weekleap < 990)
326 				      t->t_weekleap += 1024;
327 
328 				    t->t_dayleap    = getshort((unsigned char *)&mb(22)); /* day in week of leap correction */
329 				    t->t_week = getshort((unsigned char *)&mb(18)); /* current week no */
330 				    if (t->t_week < 990)
331 				      t->t_week += 1024;
332 
333 				    lbp = (unsigned char *)&mb(14); /* last update time */
334 				    if (fetch_ieee754(&lbp, IEEE_SINGLE, &t0t, trim_offsets) != IEEE_OK)
335 					    return CVT_FAIL|CVT_BADFMT;
336 
337 				    t->t_utcknown = t0t.l_ui != 0;
338 
339 				    if ((t->t_utcknown) && /* got UTC information */
340 					(tlsf != tls)   && /* something will change */
341 					((t->t_weekleap - t->t_week) < 5)) /* and close in the future */
342 				    {
343 					    /* generate a leap warning */
344 					    if (tlsf > tls)
345 						t->t_leap = ADDSECOND;
346 					    else
347 						t->t_leap = DELSECOND;
348 				    }
349 				    else
350 				    {
351 					    t->t_leap = 0;
352 				    }
353 			    }
354 			    break;
355 
356 		    default:
357 			    /* it's validly formed, but we don't care about it! */
358 			    break;
359 		}
360 	}
361 	return CVT_SKIP;
362 }
363 
364 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
365 int clk_trimtsip_bs;
366 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
367 
368 /*
369  * History:
370  *
371  * clk_trimtsip.c,v
372  * Revision 4.13  1999/11/28 09:13:51  kardel
373  * RECON_4_0_98F
374  *
375  * Revision 4.12  1999/02/28 13:00:08  kardel
376  * *** empty log message ***
377  *
378  * Revision 4.11  1999/02/28 11:47:54  kardel
379  * (struct trimble): new member t_utcknown
380  * (cvt_trimtsip): fixed status monitoring, bad receiver states are
381  * now recognized
382  *
383  * Revision 4.10  1999/02/27 15:57:15  kardel
384  * use mmemcpy instead of bcopy
385  *
386  * Revision 4.9  1999/02/21 12:17:42  kardel
387  * 4.91f reconcilation
388  *
389  * Revision 4.8  1998/11/15 20:27:58  kardel
390  * Release 4.0.73e13 reconcilation
391  *
392  * Revision 4.7  1998/08/16 18:49:20  kardel
393  * (cvt_trimtsip): initial kernel capable version (no more floats)
394  * (clock_trimtsip =): new format name
395  *
396  * Revision 4.6  1998/08/09 22:26:05  kardel
397  * Trimble TSIP support
398  *
399  * Revision 4.5  1998/08/02 10:37:05  kardel
400  * working TSIP parser
401  *
402  * Revision 4.4  1998/06/28 16:50:40  kardel
403  * (getflt): fixed ENDIAN issue
404  * (getdbl): fixed ENDIAN issue
405  * (getint): use get_msb_short()
406  * (cvt_trimtsip): use gpstolfp() for conversion
407  *
408  * Revision 4.3  1998/06/13 12:07:31  kardel
409  * fix SYSV clock name clash
410  *
411  * Revision 4.2  1998/06/12 15:22:30  kardel
412  * fix prototypes
413  *
414  * Revision 4.1  1998/05/24 09:39:54  kardel
415  * implementation of the new IO handling model
416  *
417  * Revision 4.0  1998/04/10 19:45:32  kardel
418  * Start 4.0 release version numbering
419  *
420  * from V3 1.8 loginfo deleted 1998/04/11 kardel
421  */
422