2 * /src/NTP/REPOSITORY/ntp4-dev/libparse/clk_trimtsip.c,v 4.19 2009/11/01 10:47:49 kardel RELEASE_20091101_A
4 * clk_trimtsip.c,v 4.19 2009/11/01 10:47:49 kardel RELEASE_20091101_A
7 * Thanks to Sven Dietrich for providing test hardware
9 * Copyright (c) 1995-2009 by Frank Kardel <kardel <AT> ntp.org>
10 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universitaet Erlangen-Nuernberg, Germany
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the author nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTSIP)
44 #include "ntp_syslog.h"
45 #include "ntp_types.h"
47 #include "timevalops.h"
48 #include "ntp_calendar.h"
49 #include "ntp_machine.h"
50 #include "ntp_stdlib.h"
57 # include "sys/parsestreams.h"
62 #include "ieee754io.h"
66 * Trimble low level TSIP parser / time converter
68 * The receiver uses a serial message protocol called Trimble Standard
69 * Interface Protocol (it can support others but this driver only supports
70 * TSIP). Messages in this protocol have the following form:
72 * <DLE><id> ... <data> ... <DLE><ETX>
74 * Any bytes within the <data> portion of value 10 hex (<DLE>) are doubled
75 * on transmission and compressed back to one on reception. Otherwise
76 * the values of data bytes can be anything. The serial interface is RS-422
77 * asynchronous using 9600 baud, 8 data bits with odd party (**note** 9 bits
78 * in total!), and 1 stop bit. The protocol supports byte, integer, single,
79 * and double datatypes. Integers are two bytes, sent most significant first.
80 * Singles are IEEE754 single precision floating point numbers (4 byte) sent
81 * sign & exponent first. Doubles are IEEE754 double precision floating point
82 * numbers (8 byte) sent sign & exponent first.
83 * The receiver supports a large set of messages, only a very small subset of
86 * From this module the following are recognised:
92 * 4F UTC correction data (used to get leap second warnings)
94 * All others are accepted but ignored for time conversion - they are passed up to higher layers.
98 static offsets_t trim_offsets = { 0, 1, 2, 3, 4, 5, 6, 7 };
102 u_char t_in_pkt; /* first DLE received */
103 u_char t_dle; /* subsequent DLE received */
104 u_short t_week; /* GPS week */
105 u_short t_weekleap; /* GPS week of next/last week */
106 u_short t_dayleap; /* day in week */
107 u_short t_gpsutc; /* GPS - UTC offset */
108 u_short t_gpsutcleap; /* offset at next/last leap */
109 u_char t_operable; /* receiver feels OK */
110 u_char t_mode; /* actual operating mode */
111 u_char t_leap; /* possible leap warning */
112 u_char t_utcknown; /* utc offset known */
115 #define STATUS_BAD 0 /* BAD or UNINITIALIZED receiver status */
116 #define STATUS_UNSAFE 1 /* not enough receivers for full precision */
117 #define STATUS_SYNC 2 /* enough information for good operation */
119 static unsigned long inp_tsip (parse_t *, char, timestamp_t *);
120 static unsigned long cvt_trimtsip (unsigned char *, int, struct format *, clocktime_t *, void *);
122 struct clockformat clock_trimtsip =
124 inp_tsip, /* Trimble TSIP input handler */
125 cvt_trimtsip, /* Trimble TSIP conversion */
126 pps_one, /* easy PPS monitoring */
127 0, /* no configuration data */
129 400, /* input buffer */
130 sizeof(struct trimble) /* private data */
133 #define ADDSECOND 0x01
134 #define DELSECOND 0x02
143 struct trimble *t = (struct trimble *)parseio->parse_pdata;
146 return PARSE_INP_SKIP; /* local data not allocated - sigh! */
148 if (!t->t_in_pkt && ch != DLE) {
149 /* wait for start of packet */
150 return PARSE_INP_SKIP;
153 if ((parseio->parse_index >= (parseio->parse_dsize - 2)) ||
154 (parseio->parse_dtime.parse_msglen >= (sizeof(parseio->parse_dtime.parse_msg) - 2)))
155 { /* OVERFLOW - DROP! */
156 t->t_in_pkt = t->t_dle = 0;
157 parseio->parse_index = 0;
158 parseio->parse_dtime.parse_msglen = 0;
159 return PARSE_INP_SKIP;
167 parseio->parse_index = 0;
168 parseio->parse_data[parseio->parse_index++] = ch;
169 parseio->parse_dtime.parse_msglen = 0;
170 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
171 parseio->parse_dtime.parse_stime = *tstamp; /* pick up time stamp at packet start */
172 } else if (t->t_dle) {
173 /* Double DLE -> insert a DLE */
175 parseio->parse_data[parseio->parse_index++] = DLE;
176 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = DLE;
183 /* DLE,ETX -> end of packet */
184 parseio->parse_data[parseio->parse_index++] = DLE;
185 parseio->parse_data[parseio->parse_index] = ch;
186 parseio->parse_ldsize = (u_short) (parseio->parse_index + 1);
187 memcpy(parseio->parse_ldata, parseio->parse_data, parseio->parse_ldsize);
188 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = DLE;
189 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
190 t->t_in_pkt = t->t_dle = 0;
191 return PARSE_INP_TIME|PARSE_INP_DATA;
195 default: /* collect data */
197 parseio->parse_data[parseio->parse_index++] = ch;
198 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
201 return PARSE_INP_SKIP;
209 return (short) get_msb_short(&p);
215 * convert TSIP type format
219 unsigned char *buffer,
221 struct format *format,
222 clocktime_t *clock_time,
226 register struct trimble *t = (struct trimble *)local; /* get local data space */
227 #define mb(_X_) (buffer[2+(_X_)]) /* shortcut for buffer access */
230 clock_time->flags = 0;
233 return CVT_NONE; /* local data not allocated - sigh! */
237 (buffer[0] != DLE) ||
238 (buffer[size-1] != ETX) ||
239 (buffer[size-2] != DLE))
241 printf("TRIMBLE BAD packet, size %d:\n", size);
254 u_int week = getshort((unsigned char *)&mb(4));
259 if (fetch_ieee754(&bp, IEEE_SINGLE, &secs, trim_offsets) != IEEE_OK)
260 return CVT_FAIL|CVT_BADFMT;
262 if ((secs.l_i <= 0) ||
263 (t->t_utcknown == 0))
265 clock_time->flags = PARSEB_POWERUP;
268 if (week < GPSWRAP) {
274 /* fetch UTC offset */
276 if (fetch_ieee754(&bp, IEEE_SINGLE, &utcoffset, trim_offsets) != IEEE_OK)
277 return CVT_FAIL|CVT_BADFMT;
279 L_SUB(&secs, &utcoffset); /* adjust GPS time to UTC time */
281 gpstolfp((unsigned short)week, (unsigned short)0,
282 secs.l_ui, &gpstime);
284 gpstime.l_uf = secs.l_uf;
286 clock_time->utctime = gpstime.l_ui - JAN_1970;
288 TSFTOTVU(gpstime.l_uf, clock_time->usecond);
290 if (t->t_leap == ADDSECOND)
291 clock_time->flags |= PARSEB_LEAPADD;
293 if (t->t_leap == DELSECOND)
294 clock_time->flags |= PARSEB_LEAPDEL;
296 switch (t->t_operable)
299 clock_time->flags &= ~(PARSEB_POWERUP|PARSEB_NOSYNC);
303 clock_time->flags |= PARSEB_NOSYNC;
307 clock_time->flags |= PARSEB_NOSYNC|PARSEB_POWERUP;
312 clock_time->flags |= PARSEB_POSITION;
314 clock_time->flags |= PARSEB_S_LEAP|PARSEB_S_POSITION;
320 case CMD_RRECVHEALTH:
323 u_char status = mb(0);
327 case 0x00: /* position fixes */
328 t->t_operable = STATUS_SYNC;
331 case 0x09: /* 1 satellite */
332 case 0x0A: /* 2 satellites */
333 case 0x0B: /* 3 satellites */
334 t->t_operable = STATUS_UNSAFE;
338 t->t_operable = STATUS_BAD;
350 /* UTC correction data - derive a leap warning */
351 int tls = t->t_gpsutc = (u_short) getshort((unsigned char *)&mb(12)); /* current leap correction (GPS-UTC) */
352 int tlsf = t->t_gpsutcleap = (u_short) getshort((unsigned char *)&mb(24)); /* new leap correction */
354 t->t_weekleap = (u_short) getshort((unsigned char *)&mb(20)); /* week no of leap correction */
355 if (t->t_weekleap < GPSWRAP)
356 t->t_weekleap = (u_short)(t->t_weekleap + GPSWEEKS);
358 t->t_dayleap = (u_short) getshort((unsigned char *)&mb(22)); /* day in week of leap correction */
359 t->t_week = (u_short) getshort((unsigned char *)&mb(18)); /* current week no */
360 if (t->t_week < GPSWRAP)
361 t->t_week = (u_short)(t->t_weekleap + GPSWEEKS);
363 lbp = (unsigned char *)&mb(14); /* last update time */
364 if (fetch_ieee754(&lbp, IEEE_SINGLE, &t0t, trim_offsets) != IEEE_OK)
365 return CVT_FAIL|CVT_BADFMT;
367 t->t_utcknown = t0t.l_ui != 0;
369 if ((t->t_utcknown) && /* got UTC information */
370 (tlsf != tls) && /* something will change */
371 ((t->t_weekleap - t->t_week) < 5)) /* and close in the future */
373 /* generate a leap warning */
375 t->t_leap = ADDSECOND;
377 t->t_leap = DELSECOND;
387 /* it's validly formed, but we don't care about it! */
394 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
396 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
402 * Revision 4.19 2009/11/01 10:47:49 kardel
405 * Revision 4.18 2009/11/01 08:46:46 kardel
406 * clarify case FALLTHROUGH
408 * Revision 4.17 2005/04/16 17:32:10 kardel
411 * Revision 4.16 2004/11/14 15:29:41 kardel
412 * support PPSAPI, upgrade Copyright to Berkeley style
414 * Revision 4.13 1999/11/28 09:13:51 kardel
417 * Revision 4.12 1999/02/28 13:00:08 kardel
418 * *** empty log message ***
420 * Revision 4.11 1999/02/28 11:47:54 kardel
421 * (struct trimble): new member t_utcknown
422 * (cvt_trimtsip): fixed status monitoring, bad receiver states are
425 * Revision 4.10 1999/02/27 15:57:15 kardel
426 * use mmemcpy instead of bcopy
428 * Revision 4.9 1999/02/21 12:17:42 kardel
429 * 4.91f reconcilation
431 * Revision 4.8 1998/11/15 20:27:58 kardel
432 * Release 4.0.73e13 reconcilation
434 * Revision 4.7 1998/08/16 18:49:20 kardel
435 * (cvt_trimtsip): initial kernel capable version (no more floats)
436 * (clock_trimtsip =): new format name
438 * Revision 4.6 1998/08/09 22:26:05 kardel
439 * Trimble TSIP support
441 * Revision 4.5 1998/08/02 10:37:05 kardel
442 * working TSIP parser
444 * Revision 4.4 1998/06/28 16:50:40 kardel
445 * (getflt): fixed ENDIAN issue
446 * (getdbl): fixed ENDIAN issue
447 * (getint): use get_msb_short()
448 * (cvt_trimtsip): use gpstolfp() for conversion
450 * Revision 4.3 1998/06/13 12:07:31 kardel
451 * fix SYSV clock name clash
453 * Revision 4.2 1998/06/12 15:22:30 kardel
456 * Revision 4.1 1998/05/24 09:39:54 kardel
457 * implementation of the new IO handling model
459 * Revision 4.0 1998/04/10 19:45:32 kardel
460 * Start 4.0 release version numbering
462 * from V3 1.8 loginfo deleted 1998/04/11 kardel