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