1 /* $NetBSD: data_mbg.c,v 1.3 2013/12/28 03:20:14 christos Exp $ */ 2 3 /* 4 * /src/NTP/REPOSITORY/ntp4-dev/libparse/data_mbg.c,v 4.8 2006/06/22 18:40:01 kardel RELEASE_20060622_A 5 * 6 * data_mbg.c,v 4.8 2006/06/22 18:40:01 kardel RELEASE_20060622_A 7 * 8 * $Created: Sun Jul 20 12:08:14 1997 $ 9 * 10 * Copyright (c) 1997-2005 by Frank Kardel <kardel <AT> ntp.org> 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 * 36 */ 37 38 #include <config.h> 39 #ifdef PARSESTREAM 40 #define NEED_BOPS 41 #include "ntp_string.h" 42 #else 43 #include <stdio.h> 44 #endif 45 #include "ntp_types.h" 46 #include "ntp_stdlib.h" 47 #include "ntp_fp.h" 48 #include "mbg_gps166.h" 49 #include "binio.h" 50 #include "ieee754io.h" 51 52 static void get_mbg_tzname (unsigned char **, char *); 53 static void mbg_time_status_str (char **, unsigned int, int); 54 55 #if 0 /* no actual floats on Meinberg binary interface */ 56 static offsets_t mbg_float = { 1, 0, 3, 2, 0, 0, 0, 0 }; /* byte order for meinberg floats */ 57 #endif 58 static offsets_t mbg_double = { 1, 0, 3, 2, 5, 4, 7, 6 }; /* byte order for meinberg doubles */ 59 static int32 rad2deg_i = 57; 60 static u_int32 rad2deg_f = 0x4BB834C7; /* 57.2957795131 == 180/PI */ 61 62 void 63 put_mbg_header( 64 unsigned char **bufpp, 65 GPS_MSG_HDR *headerp 66 ) 67 { 68 put_lsb_short(bufpp, headerp->gps_cmd); 69 put_lsb_short(bufpp, headerp->gps_len); 70 put_lsb_short(bufpp, headerp->gps_data_csum); 71 put_lsb_short(bufpp, headerp->gps_hdr_csum); 72 } 73 74 void 75 get_mbg_sw_rev( 76 unsigned char **bufpp, 77 SW_REV *sw_revp 78 ) 79 { 80 sw_revp->code = get_lsb_short(bufpp); 81 memcpy(sw_revp->name, *bufpp, sizeof(sw_revp->name)); 82 *bufpp += sizeof(sw_revp->name); 83 } 84 85 void 86 get_mbg_ascii_msg( 87 unsigned char **bufpp, 88 ASCII_MSG *ascii_msgp 89 ) 90 { 91 ascii_msgp->csum = get_lsb_short(bufpp); 92 ascii_msgp->valid = get_lsb_short(bufpp); 93 memcpy(ascii_msgp->s, *bufpp, sizeof(ascii_msgp->s)); 94 *bufpp += sizeof(ascii_msgp->s); 95 } 96 97 void 98 get_mbg_svno( 99 unsigned char **bufpp, 100 SVNO *svnop 101 ) 102 { 103 *svnop = get_lsb_short(bufpp); 104 } 105 106 void 107 get_mbg_health( 108 unsigned char **bufpp, 109 HEALTH *healthp 110 ) 111 { 112 *healthp = get_lsb_short(bufpp); 113 } 114 115 void 116 get_mbg_cfg( 117 unsigned char **bufpp, 118 CFG *cfgp 119 ) 120 { 121 *cfgp = get_lsb_short(bufpp); 122 } 123 124 void 125 get_mbg_tgps( 126 unsigned char **bufpp, 127 T_GPS *tgpsp 128 ) 129 { 130 tgpsp->wn = get_lsb_short(bufpp); 131 tgpsp->sec = get_lsb_long(bufpp); 132 tgpsp->tick = get_lsb_long(bufpp); 133 } 134 135 void 136 get_mbg_tm( 137 unsigned char **buffpp, 138 TM *tmp 139 ) 140 { 141 tmp->year = get_lsb_short(buffpp); 142 tmp->month = *(*buffpp)++; 143 tmp->mday = *(*buffpp)++; 144 tmp->yday = get_lsb_short(buffpp); 145 tmp->wday = *(*buffpp)++; 146 tmp->hour = *(*buffpp)++; 147 tmp->minute = *(*buffpp)++; 148 tmp->second = *(*buffpp)++; 149 tmp->frac = get_lsb_long(buffpp); 150 tmp->offs_from_utc = get_lsb_long(buffpp); 151 tmp->status= get_lsb_short(buffpp); 152 } 153 154 void 155 get_mbg_ttm( 156 unsigned char **buffpp, 157 TTM *ttmp 158 ) 159 { 160 ttmp->channel = get_lsb_short(buffpp); 161 get_mbg_tgps(buffpp, &ttmp->t); 162 get_mbg_tm(buffpp, &ttmp->tm); 163 } 164 165 void 166 get_mbg_synth( 167 unsigned char **buffpp, 168 SYNTH *synthp 169 ) 170 { 171 synthp->freq = get_lsb_short(buffpp); 172 synthp->range = get_lsb_short(buffpp); 173 synthp->phase = get_lsb_short(buffpp); 174 } 175 176 static void 177 get_mbg_tzname( 178 unsigned char **buffpp, 179 char *tznamep 180 ) 181 { 182 strlcpy(tznamep, (char *)*buffpp, sizeof(TZ_NAME)); 183 *buffpp += sizeof(TZ_NAME); 184 } 185 186 void 187 get_mbg_tzdl( 188 unsigned char **buffpp, 189 TZDL *tzdlp 190 ) 191 { 192 tzdlp->offs = get_lsb_long(buffpp); 193 tzdlp->offs_dl = get_lsb_long(buffpp); 194 get_mbg_tm(buffpp, &tzdlp->tm_on); 195 get_mbg_tm(buffpp, &tzdlp->tm_off); 196 get_mbg_tzname(buffpp, (char *)tzdlp->name[0]); 197 get_mbg_tzname(buffpp, (char *)tzdlp->name[1]); 198 } 199 200 void 201 get_mbg_antinfo( 202 unsigned char **buffpp, 203 ANT_INFO *antinfop 204 ) 205 { 206 antinfop->status = get_lsb_short(buffpp); 207 get_mbg_tm(buffpp, &antinfop->tm_disconn); 208 get_mbg_tm(buffpp, &antinfop->tm_reconn); 209 antinfop->delta_t = get_lsb_long(buffpp); 210 } 211 212 static void 213 mbg_time_status_str( 214 char **buffpp, 215 unsigned int status, 216 int size 217 ) 218 { 219 static struct state 220 { 221 int flag; /* bit flag */ 222 const char *string; /* bit name */ 223 } states[] = 224 { 225 { TM_UTC, "UTC CORR" }, 226 { TM_LOCAL, "LOCAL TIME" }, 227 { TM_DL_ANN, "DST WARN" }, 228 { TM_DL_ENB, "DST" }, 229 { TM_LS_ANN, "LEAP WARN" }, 230 { TM_LS_ENB, "LEAP SEC" }, 231 { 0, "" } 232 }; 233 234 if (status) 235 { 236 char *start, *p; 237 struct state *s; 238 239 start = p = *buffpp; 240 241 for (s = states; s->flag; s++) 242 { 243 if (s->flag & status) 244 { 245 if (p != *buffpp) 246 { 247 strlcpy(p, ", ", size - (p - start)); 248 p += 2; 249 } 250 strlcpy(p, s->string, size - (p - start)); 251 p += strlen(p); 252 } 253 } 254 *buffpp = p; 255 } 256 } 257 258 void 259 mbg_tm_str( 260 char **buffpp, 261 TM *tmp, 262 int size 263 ) 264 { 265 char *s = *buffpp; 266 267 snprintf(*buffpp, size, "%04d-%02d-%02d %02d:%02d:%02d.%07ld (%c%02d%02d) ", 268 tmp->year, tmp->month, tmp->mday, 269 tmp->hour, tmp->minute, tmp->second, tmp->frac, 270 (tmp->offs_from_utc < 0) ? '-' : '+', 271 abs(tmp->offs_from_utc) / 3600, 272 (abs(tmp->offs_from_utc) / 60) % 60); 273 *buffpp += strlen(*buffpp); 274 275 mbg_time_status_str(buffpp, tmp->status, size - (*buffpp - s)); 276 } 277 278 void 279 mbg_tgps_str( 280 char **buffpp, 281 T_GPS *tgpsp, 282 int size 283 ) 284 { 285 snprintf(*buffpp, size, "week %d + %ld days + %ld.%07ld sec", 286 tgpsp->wn, tgpsp->sec / 86400, 287 tgpsp->sec % 86400, tgpsp->tick); 288 *buffpp += strlen(*buffpp); 289 } 290 291 void 292 get_mbg_cfgh( 293 unsigned char **buffpp, 294 CFGH *cfghp 295 ) 296 { 297 int i; 298 299 cfghp->csum = get_lsb_short(buffpp); 300 cfghp->valid = get_lsb_short(buffpp); 301 get_mbg_tgps(buffpp, &cfghp->tot_51); 302 get_mbg_tgps(buffpp, &cfghp->tot_63); 303 get_mbg_tgps(buffpp, &cfghp->t0a); 304 305 for (i = MIN_SVNO; i <= MAX_SVNO; i++) 306 { 307 get_mbg_cfg(buffpp, &cfghp->cfg[i]); 308 } 309 310 for (i = MIN_SVNO; i <= MAX_SVNO; i++) 311 { 312 get_mbg_health(buffpp, &cfghp->health[i]); 313 } 314 } 315 316 void 317 get_mbg_utc( 318 unsigned char **buffpp, 319 UTC *utcp 320 ) 321 { 322 utcp->csum = get_lsb_short(buffpp); 323 utcp->valid = get_lsb_short(buffpp); 324 325 get_mbg_tgps(buffpp, &utcp->t0t); 326 327 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &utcp->A0, mbg_double) != IEEE_OK) 328 { 329 L_CLR(&utcp->A0); 330 } 331 332 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &utcp->A1, mbg_double) != IEEE_OK) 333 { 334 L_CLR(&utcp->A1); 335 } 336 337 utcp->WNlsf = get_lsb_short(buffpp); 338 utcp->DNt = get_lsb_short(buffpp); 339 utcp->delta_tls = *(*buffpp)++; 340 utcp->delta_tlsf = *(*buffpp)++; 341 } 342 343 void 344 get_mbg_lla( 345 unsigned char **buffpp, 346 LLA lla 347 ) 348 { 349 int i; 350 351 for (i = LAT; i <= ALT; i++) 352 { 353 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &lla[i], mbg_double) != IEEE_OK) 354 { 355 L_CLR(&lla[i]); 356 } 357 else 358 if (i != ALT) 359 { /* convert to degrees (* 180/PI) */ 360 mfp_mul(&lla[i].l_i, &lla[i].l_uf, lla[i].l_i, lla[i].l_uf, rad2deg_i, rad2deg_f); 361 } 362 } 363 } 364 365 void 366 get_mbg_xyz( 367 unsigned char **buffpp, 368 XYZ xyz 369 ) 370 { 371 int i; 372 373 for (i = XP; i <= ZP; i++) 374 { 375 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &xyz[i], mbg_double) != IEEE_OK) 376 { 377 L_CLR(&xyz[i]); 378 } 379 } 380 } 381 382 static void 383 get_mbg_comparam( 384 unsigned char **buffpp, 385 COM_PARM *comparamp 386 ) 387 { 388 size_t i; 389 390 comparamp->baud_rate = get_lsb_long(buffpp); 391 for (i = 0; i < sizeof(comparamp->framing); i++) 392 { 393 comparamp->framing[i] = *(*buffpp)++; 394 } 395 comparamp->handshake = get_lsb_short(buffpp); 396 } 397 398 void 399 get_mbg_portparam( 400 unsigned char **buffpp, 401 PORT_PARM *portparamp 402 ) 403 { 404 int i; 405 406 for (i = 0; i < N_COM; i++) 407 { 408 get_mbg_comparam(buffpp, &portparamp->com[i]); 409 } 410 for (i = 0; i < N_COM; i++) 411 { 412 portparamp->mode[i] = *(*buffpp)++; 413 } 414 } 415 416 #define FETCH_DOUBLE(src, addr) \ 417 if (fetch_ieee754(src, IEEE_DOUBLE, addr, mbg_double) != IEEE_OK) \ 418 { \ 419 L_CLR(addr); \ 420 } 421 422 void 423 get_mbg_eph( 424 unsigned char ** buffpp, 425 EPH *ephp 426 ) 427 { 428 ephp->csum = get_lsb_short(buffpp); 429 ephp->valid = get_lsb_short(buffpp); 430 431 ephp->health = get_lsb_short(buffpp); 432 ephp->IODC = get_lsb_short(buffpp); 433 ephp->IODE2 = get_lsb_short(buffpp); 434 ephp->IODE3 = get_lsb_short(buffpp); 435 436 get_mbg_tgps(buffpp, &ephp->tt); 437 get_mbg_tgps(buffpp, &ephp->t0c); 438 get_mbg_tgps(buffpp, &ephp->t0e); 439 440 FETCH_DOUBLE(buffpp, &ephp->sqrt_A); 441 FETCH_DOUBLE(buffpp, &ephp->e); 442 FETCH_DOUBLE(buffpp, &ephp->M0); 443 FETCH_DOUBLE(buffpp, &ephp->omega); 444 FETCH_DOUBLE(buffpp, &ephp->OMEGA0); 445 FETCH_DOUBLE(buffpp, &ephp->OMEGADOT); 446 FETCH_DOUBLE(buffpp, &ephp->deltan); 447 FETCH_DOUBLE(buffpp, &ephp->i0); 448 FETCH_DOUBLE(buffpp, &ephp->idot); 449 FETCH_DOUBLE(buffpp, &ephp->crc); 450 FETCH_DOUBLE(buffpp, &ephp->crs); 451 FETCH_DOUBLE(buffpp, &ephp->cuc); 452 FETCH_DOUBLE(buffpp, &ephp->cus); 453 FETCH_DOUBLE(buffpp, &ephp->cic); 454 FETCH_DOUBLE(buffpp, &ephp->cis); 455 456 FETCH_DOUBLE(buffpp, &ephp->af0); 457 FETCH_DOUBLE(buffpp, &ephp->af1); 458 FETCH_DOUBLE(buffpp, &ephp->af2); 459 FETCH_DOUBLE(buffpp, &ephp->tgd); 460 461 ephp->URA = get_lsb_short(buffpp); 462 463 ephp->L2code = *(*buffpp)++; 464 ephp->L2flag = *(*buffpp)++; 465 } 466 467 void 468 get_mbg_alm( 469 unsigned char **buffpp, 470 ALM *almp 471 ) 472 { 473 almp->csum = get_lsb_short(buffpp); 474 almp->valid = get_lsb_short(buffpp); 475 476 almp->health = get_lsb_short(buffpp); 477 get_mbg_tgps(buffpp, &almp->t0a); 478 479 480 FETCH_DOUBLE(buffpp, &almp->sqrt_A); 481 FETCH_DOUBLE(buffpp, &almp->e); 482 483 FETCH_DOUBLE(buffpp, &almp->M0); 484 FETCH_DOUBLE(buffpp, &almp->omega); 485 FETCH_DOUBLE(buffpp, &almp->OMEGA0); 486 FETCH_DOUBLE(buffpp, &almp->OMEGADOT); 487 FETCH_DOUBLE(buffpp, &almp->deltai); 488 FETCH_DOUBLE(buffpp, &almp->af0); 489 FETCH_DOUBLE(buffpp, &almp->af1); 490 } 491 492 void 493 get_mbg_iono( 494 unsigned char **buffpp, 495 IONO *ionop 496 ) 497 { 498 ionop->csum = get_lsb_short(buffpp); 499 ionop->valid = get_lsb_short(buffpp); 500 501 FETCH_DOUBLE(buffpp, &ionop->alpha_0); 502 FETCH_DOUBLE(buffpp, &ionop->alpha_1); 503 FETCH_DOUBLE(buffpp, &ionop->alpha_2); 504 FETCH_DOUBLE(buffpp, &ionop->alpha_3); 505 506 FETCH_DOUBLE(buffpp, &ionop->beta_0); 507 FETCH_DOUBLE(buffpp, &ionop->beta_1); 508 FETCH_DOUBLE(buffpp, &ionop->beta_2); 509 FETCH_DOUBLE(buffpp, &ionop->beta_3); 510 } 511 512 /* 513 * data_mbg.c,v 514 * Revision 4.8 2006/06/22 18:40:01 kardel 515 * clean up signedness (gcc 4) 516 * 517 * Revision 4.7 2005/10/07 22:11:10 kardel 518 * bounded buffer implementation 519 * 520 * Revision 4.6.2.1 2005/09/25 10:23:06 kardel 521 * support bounded buffers 522 * 523 * Revision 4.6 2005/04/16 17:32:10 kardel 524 * update copyright 525 * 526 * Revision 4.5 2004/11/14 15:29:41 kardel 527 * support PPSAPI, upgrade Copyright to Berkeley style 528 * 529 * Revision 4.3 1999/02/21 12:17:42 kardel 530 * 4.91f reconcilation 531 * 532 * Revision 4.2 1998/06/14 21:09:39 kardel 533 * Sun acc cleanup 534 * 535 * Revision 4.1 1998/05/24 08:02:06 kardel 536 * trimmed version log 537 * 538 * Revision 4.0 1998/04/10 19:45:33 kardel 539 * Start 4.0 release version numbering 540 */ 541 542