1 /* $NetBSD: clk_trimtsip.c,v 1.2 2003/12/04 16:23:37 drochner Exp $ */
4 * /src/NTP/ntp4-dev/libparse/clk_trimtsip.c,v 4.17 2005/04/16 17:32:10 kardel RELEASE_20050508_A
6 * clk_trimtsip.c,v 4.17 2005/04/16 17:32:10 kardel RELEASE_20050508_A
9 * Thanks to Sven Dietrich for providing test hardware
11 * Copyright (c) 1995-2005 by Frank Kardel <kardel <AT> ntp.org>
12 * Copyright (c) 1989-1994 by Frank Kardel, Friedrich-Alexander Universität Erlangen-Nürnberg, Germany
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the author nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTSIP)
46 #include "ntp_syslog.h"
47 #include "ntp_types.h"
49 #include "ntp_unixtime.h"
50 #include "ntp_calendar.h"
51 #include "ntp_machine.h"
52 #include "ntp_stdlib.h"
59 # include "sys/parsestreams.h"
64 #include "ieee754io.h"
68 * Trimble low level TSIP parser / time converter
70 * The receiver uses a serial message protocol called Trimble Standard
71 * Interface Protocol (it can support others but this driver only supports
72 * TSIP). Messages in this protocol have the following form:
74 * <DLE><id> ... <data> ... <DLE><ETX>
76 * Any bytes within the <data> portion of value 10 hex (<DLE>) are doubled
77 * on transmission and compressed back to one on reception. Otherwise
78 * the values of data bytes can be anything. The serial interface is RS-422
79 * asynchronous using 9600 baud, 8 data bits with odd party (**note** 9 bits
80 * in total!), and 1 stop bit. The protocol supports byte, integer, single,
81 * and double datatypes. Integers are two bytes, sent most significant first.
82 * Singles are IEEE754 single precision floating point numbers (4 byte) sent
83 * sign & exponent first. Doubles are IEEE754 double precision floating point
84 * numbers (8 byte) sent sign & exponent first.
85 * The receiver supports a large set of messages, only a very small subset of
88 * From this module the following are recognised:
94 * 4F UTC correction data (used to get leap second warnings)
96 * All others are accepted but ignored for time conversion - they are passed up to higher layers.
100 static offsets_t trim_offsets
= { 0, 1, 2, 3, 4, 5, 6, 7 };
104 u_char t_in_pkt
; /* first DLE received */
105 u_char t_dle
; /* subsequent DLE received */
106 u_short t_week
; /* GPS week */
107 u_short t_weekleap
; /* GPS week of next/last week */
108 u_short t_dayleap
; /* day in week */
109 u_short t_gpsutc
; /* GPS - UTC offset */
110 u_short t_gpsutcleap
; /* offset at next/last leap */
111 u_char t_operable
; /* receiver feels OK */
112 u_char t_mode
; /* actual operating mode */
113 u_char t_leap
; /* possible leap warning */
114 u_char t_utcknown
; /* utc offset known */
117 #define STATUS_BAD 0 /* BAD or UNINITIALIZED receiver status */
118 #define STATUS_UNSAFE 1 /* not enough receivers for full precision */
119 #define STATUS_SYNC 2 /* enough information for good operation */
121 static unsigned long inp_tsip
P((parse_t
*, unsigned int, timestamp_t
*));
122 static unsigned long cvt_trimtsip
P((unsigned char *, int, struct format
*, clocktime_t
*, void *));
124 struct clockformat clock_trimtsip
=
126 inp_tsip
, /* Trimble TSIP input handler */
127 cvt_trimtsip
, /* Trimble TSIP conversion */
128 pps_one
, /* easy PPS monitoring */
129 0, /* no configuration data */
131 400, /* input buffer */
132 sizeof(struct trimble
) /* private data */
135 #define ADDSECOND 0x01
136 #define DELSECOND 0x02
145 struct trimble
*t
= (struct trimble
*)parseio
->parse_pdata
;
148 return PARSE_INP_SKIP
; /* local data not allocated - sigh! */
150 if (!t
->t_in_pkt
&& ch
!= DLE
) {
151 /* wait for start of packet */
152 return PARSE_INP_SKIP
;
155 if ((parseio
->parse_index
>= (parseio
->parse_dsize
- 2)) ||
156 (parseio
->parse_dtime
.parse_msglen
>= (sizeof(parseio
->parse_dtime
.parse_msg
) - 2)))
157 { /* OVERFLOW - DROP! */
158 t
->t_in_pkt
= t
->t_dle
= 0;
159 parseio
->parse_index
= 0;
160 parseio
->parse_dtime
.parse_msglen
= 0;
161 return PARSE_INP_SKIP
;
169 parseio
->parse_index
= 0;
170 parseio
->parse_data
[parseio
->parse_index
++] = ch
;
171 parseio
->parse_dtime
.parse_msglen
= 0;
172 parseio
->parse_dtime
.parse_msg
[parseio
->parse_dtime
.parse_msglen
++] = ch
;
173 parseio
->parse_dtime
.parse_stime
= *tstamp
; /* pick up time stamp at packet start */
174 } else if (t
->t_dle
) {
175 /* Double DLE -> insert a DLE */
177 parseio
->parse_data
[parseio
->parse_index
++] = DLE
;
178 parseio
->parse_dtime
.parse_msg
[parseio
->parse_dtime
.parse_msglen
++] = DLE
;
185 /* DLE,ETX -> end of packet */
186 parseio
->parse_data
[parseio
->parse_index
++] = DLE
;
187 parseio
->parse_data
[parseio
->parse_index
] = ch
;
188 parseio
->parse_ldsize
= parseio
->parse_index
+1;
189 memcpy(parseio
->parse_ldata
, parseio
->parse_data
, parseio
->parse_ldsize
);
190 parseio
->parse_dtime
.parse_msg
[parseio
->parse_dtime
.parse_msglen
++] = DLE
;
191 parseio
->parse_dtime
.parse_msg
[parseio
->parse_dtime
.parse_msglen
++] = ch
;
192 t
->t_in_pkt
= t
->t_dle
= 0;
193 return PARSE_INP_TIME
|PARSE_INP_DATA
;
196 default: /* collect data */
198 parseio
->parse_data
[parseio
->parse_index
++] = ch
;
199 parseio
->parse_dtime
.parse_msg
[parseio
->parse_dtime
.parse_msglen
++] = ch
;
202 return PARSE_INP_SKIP
;
210 return get_msb_short(&p
);
216 * convert TSIP type format
220 unsigned char *buffer
,
222 struct format
*format
,
223 clocktime_t
*clock_time
,
227 register struct trimble
*t
= (struct trimble
*)local
; /* get local data space */
228 #define mb(_X_) (buffer[2+(_X_)]) /* shortcut for buffer access */
231 clock_time
->flags
= 0;
234 return CVT_NONE
; /* local data not allocated - sigh! */
238 (buffer
[0] != DLE
) ||
239 (buffer
[size
-1] != ETX
) ||
240 (buffer
[size
-2] != DLE
))
242 printf("TRIMBLE BAD packet, size %d:\n", size
);
255 int week
= getshort((unsigned char *)&mb(4));
260 if (fetch_ieee754(&bp
, IEEE_SINGLE
, &secs
, trim_offsets
) != IEEE_OK
)
261 return CVT_FAIL
|CVT_BADFMT
;
263 if ((secs
.l_i
<= 0) ||
264 (t
->t_utcknown
== 0))
266 clock_time
->flags
= PARSEB_POWERUP
;
275 /* fetch UTC offset */
277 if (fetch_ieee754(&bp
, IEEE_SINGLE
, &utcoffset
, trim_offsets
) != IEEE_OK
)
278 return CVT_FAIL
|CVT_BADFMT
;
280 L_SUB(&secs
, &utcoffset
); /* adjust GPS time to UTC time */
282 gpstolfp((unsigned short)week
, (unsigned short)0,
283 secs
.l_ui
, &gpstime
);
285 gpstime
.l_uf
= secs
.l_uf
;
287 clock_time
->utctime
= gpstime
.l_ui
- JAN_1970
;
289 TSFTOTVU(gpstime
.l_uf
, clock_time
->usecond
);
291 if (t
->t_leap
== ADDSECOND
)
292 clock_time
->flags
|= PARSEB_LEAPADD
;
294 if (t
->t_leap
== DELSECOND
)
295 clock_time
->flags
|= PARSEB_LEAPDEL
;
297 switch (t
->t_operable
)
300 clock_time
->flags
&= ~(PARSEB_POWERUP
|PARSEB_NOSYNC
);
304 clock_time
->flags
|= PARSEB_NOSYNC
;
308 clock_time
->flags
|= PARSEB_NOSYNC
|PARSEB_POWERUP
;
313 clock_time
->flags
|= PARSEB_POSITION
;
315 clock_time
->flags
|= PARSEB_S_LEAP
|PARSEB_S_POSITION
;
321 case CMD_RRECVHEALTH
:
324 u_char status
= mb(0);
328 case 0x00: /* position fixes */
329 t
->t_operable
= STATUS_SYNC
;
332 case 0x09: /* 1 satellite */
333 case 0x0A: /* 2 satellites */
334 case 0x0B: /* 3 satellites */
335 t
->t_operable
= STATUS_UNSAFE
;
339 t
->t_operable
= STATUS_BAD
;
351 /* UTC correction data - derive a leap warning */
352 int tls
= t
->t_gpsutc
= getshort((unsigned char *)&mb(12)); /* current leap correction (GPS-UTC) */
353 int tlsf
= t
->t_gpsutcleap
= getshort((unsigned char *)&mb(24)); /* new leap correction */
355 t
->t_weekleap
= getshort((unsigned char *)&mb(20)); /* week no of leap correction */
356 if (t
->t_weekleap
< 990)
357 t
->t_weekleap
+= 1024;
359 t
->t_dayleap
= getshort((unsigned char *)&mb(22)); /* day in week of leap correction */
360 t
->t_week
= getshort((unsigned char *)&mb(18)); /* current week no */
364 lbp
= (unsigned char *)&mb(14); /* last update time */
365 if (fetch_ieee754(&lbp
, IEEE_SINGLE
, &t0t
, trim_offsets
) != IEEE_OK
)
366 return CVT_FAIL
|CVT_BADFMT
;
368 t
->t_utcknown
= t0t
.l_ui
!= 0;
370 if ((t
->t_utcknown
) && /* got UTC information */
371 (tlsf
!= tls
) && /* something will change */
372 ((t
->t_weekleap
- t
->t_week
) < 5)) /* and close in the future */
374 /* generate a leap warning */
376 t
->t_leap
= ADDSECOND
;
378 t
->t_leap
= DELSECOND
;
388 /* it's validly formed, but we don't care about it! */
395 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
397 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
403 * Revision 4.17 2005/04/16 17:32:10 kardel
406 * Revision 4.16 2004/11/14 15:29:41 kardel
407 * support PPSAPI, upgrade Copyright to Berkeley style
409 * Revision 4.13 1999/11/28 09:13:51 kardel
412 * Revision 4.12 1999/02/28 13:00:08 kardel
413 * *** empty log message ***
415 * Revision 4.11 1999/02/28 11:47:54 kardel
416 * (struct trimble): new member t_utcknown
417 * (cvt_trimtsip): fixed status monitoring, bad receiver states are
420 * Revision 4.10 1999/02/27 15:57:15 kardel
421 * use mmemcpy instead of bcopy
423 * Revision 4.9 1999/02/21 12:17:42 kardel
424 * 4.91f reconcilation
426 * Revision 4.8 1998/11/15 20:27:58 kardel
427 * Release 4.0.73e13 reconcilation
429 * Revision 4.7 1998/08/16 18:49:20 kardel
430 * (cvt_trimtsip): initial kernel capable version (no more floats)
431 * (clock_trimtsip =): new format name
433 * Revision 4.6 1998/08/09 22:26:05 kardel
434 * Trimble TSIP support
436 * Revision 4.5 1998/08/02 10:37:05 kardel
437 * working TSIP parser
439 * Revision 4.4 1998/06/28 16:50:40 kardel
440 * (getflt): fixed ENDIAN issue
441 * (getdbl): fixed ENDIAN issue
442 * (getint): use get_msb_short()
443 * (cvt_trimtsip): use gpstolfp() for conversion
445 * Revision 4.3 1998/06/13 12:07:31 kardel
446 * fix SYSV clock name clash
448 * Revision 4.2 1998/06/12 15:22:30 kardel
451 * Revision 4.1 1998/05/24 09:39:54 kardel
452 * implementation of the new IO handling model
454 * Revision 4.0 1998/04/10 19:45:32 kardel
455 * Start 4.0 release version numbering
457 * from V3 1.8 loginfo deleted 1998/04/11 kardel