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