2 * /src/NTP/ntp-4/libparse/clk_trimtsip.c,v 4.13 1999/11/28 09:13:51 kardel RELEASE_19991128_A
4 * clk_trimtsip.c,v 4.13 1999/11/28 09:13:51 kardel RELEASE_19991128_A
6 * Trimble TSIP support - CURRENTLY VERY MUCH UNDER CONSTRUCTION
13 #if defined(REFCLOCK) && defined(CLOCK_PARSE) && defined(CLOCK_TRIMTSIP)
15 #include "ntp_syslog.h"
16 #include "ntp_types.h"
18 #include "ntp_unixtime.h"
19 #include "ntp_calendar.h"
20 #include "ntp_machine.h"
21 #include "ntp_stdlib.h"
28 # include "sys/parsestreams.h"
33 #include "ieee754io.h"
37 * Trimble low level TSIP parser / time converter
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:
43 * <DLE><id> ... <data> ... <DLE><ETX>
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
57 * From this module the following are recognised:
63 * 4F UTC correction data (used to get leap second warnings)
65 * All others are accepted but ignored for time conversion - they are passed up to higher layers.
69 static offsets_t trim_offsets = { 0, 1, 2, 3, 4, 5, 6, 7 };
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 */
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 */
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 *));
93 struct clockformat clock_trimtsip =
95 inp_tsip, /* Trimble TSIP input handler */
96 cvt_trimtsip, /* Trimble TSIP conversion */
97 pps_one, /* easy PPS monitoring */
98 0, /* no configuration data */
100 400, /* input buffer */
101 sizeof(struct trimble) /* private data */
104 #define ADDSECOND 0x01
105 #define DELSECOND 0x02
114 struct trimble *t = (struct trimble *)parseio->parse_pdata;
117 return PARSE_INP_SKIP; /* local data not allocated - sigh! */
119 if (!t->t_in_pkt && ch != DLE) {
120 /* wait for start of packet */
121 return PARSE_INP_SKIP;
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;
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 */
146 parseio->parse_data[parseio->parse_index++] = DLE;
147 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = 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;
165 default: /* collect data */
167 parseio->parse_data[parseio->parse_index++] = ch;
168 parseio->parse_dtime.parse_msg[parseio->parse_dtime.parse_msglen++] = ch;
171 return PARSE_INP_SKIP;
179 return get_msb_short(&p);
185 * convert TSIP type format
189 unsigned char *buffer,
191 struct format *format,
192 clocktime_t *clock_time,
196 register struct trimble *t = (struct trimble *)local; /* get local data space */
197 #define mb(_X_) (buffer[2+(_X_)]) /* shortcut for buffer access */
200 clock_time->flags = 0;
203 return CVT_NONE; /* local data not allocated - sigh! */
207 (buffer[0] != DLE) ||
208 (buffer[size-1] != ETX) ||
209 (buffer[size-2] != DLE))
211 printf("TRIMBLE BAD packet, size %d:\n", size);
224 int week = getshort((unsigned char *)&mb(4));
229 if (fetch_ieee754(&bp, IEEE_SINGLE, &secs, trim_offsets) != IEEE_OK)
230 return CVT_FAIL|CVT_BADFMT;
232 if ((secs.l_i <= 0) ||
233 (t->t_utcknown == 0))
235 clock_time->flags = PARSEB_POWERUP;
244 /* fetch UTC offset */
246 if (fetch_ieee754(&bp, IEEE_SINGLE, &utcoffset, trim_offsets) != IEEE_OK)
247 return CVT_FAIL|CVT_BADFMT;
249 L_SUB(&secs, &utcoffset); /* adjust GPS time to UTC time */
251 gpstolfp((unsigned short)week, (unsigned short)0,
252 secs.l_ui, &gpstime);
254 gpstime.l_uf = secs.l_uf;
256 clock_time->utctime = gpstime.l_ui - JAN_1970;
258 TSFTOTVU(gpstime.l_uf, clock_time->usecond);
260 if (t->t_leap == ADDSECOND)
261 clock_time->flags |= PARSEB_LEAPADD;
263 if (t->t_leap == DELSECOND)
264 clock_time->flags |= PARSEB_LEAPDEL;
266 switch (t->t_operable)
269 clock_time->flags &= ~(PARSEB_POWERUP|PARSEB_NOSYNC);
273 clock_time->flags |= PARSEB_NOSYNC;
277 clock_time->flags |= PARSEB_NOSYNC|PARSEB_POWERUP;
282 clock_time->flags |= PARSEB_POSITION;
284 clock_time->flags |= PARSEB_S_LEAP|PARSEB_S_POSITION;
290 case CMD_RRECVHEALTH:
293 u_char status = mb(0);
297 case 0x00: /* position fixes */
298 t->t_operable = STATUS_SYNC;
301 case 0x09: /* 1 satellite */
302 case 0x0A: /* 2 satellites */
303 case 0x0B: /* 3 satellites */
304 t->t_operable = STATUS_UNSAFE;
308 t->t_operable = STATUS_BAD;
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 */
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;
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 */
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;
337 t->t_utcknown = t0t.l_ui != 0;
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 */
343 /* generate a leap warning */
345 t->t_leap = ADDSECOND;
347 t->t_leap = DELSECOND;
357 /* it's validly formed, but we don't care about it! */
364 #else /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
366 #endif /* not (REFCLOCK && CLOCK_PARSE && CLOCK_TRIMTSIP && !PARSESTREAM) */
372 * Revision 4.13 1999/11/28 09:13:51 kardel
375 * Revision 4.12 1999/02/28 13:00:08 kardel
376 * *** empty log message ***
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
383 * Revision 4.10 1999/02/27 15:57:15 kardel
384 * use mmemcpy instead of bcopy
386 * Revision 4.9 1999/02/21 12:17:42 kardel
387 * 4.91f reconcilation
389 * Revision 4.8 1998/11/15 20:27:58 kardel
390 * Release 4.0.73e13 reconcilation
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
396 * Revision 4.6 1998/08/09 22:26:05 kardel
397 * Trimble TSIP support
399 * Revision 4.5 1998/08/02 10:37:05 kardel
400 * working TSIP parser
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
408 * Revision 4.3 1998/06/13 12:07:31 kardel
409 * fix SYSV clock name clash
411 * Revision 4.2 1998/06/12 15:22:30 kardel
414 * Revision 4.1 1998/05/24 09:39:54 kardel
415 * implementation of the new IO handling model
417 * Revision 4.0 1998/04/10 19:45:32 kardel
418 * Start 4.0 release version numbering
420 * from V3 1.8 loginfo deleted 1998/04/11 kardel