Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / libparse / data_mbg.c
blobcfba770fbf9c98e6c157e98d27c1e32cd109ccb1
1 /* $NetBSD: data_mbg.c,v 1.3 2006/06/11 19:34:10 kardel Exp $ */
3 /*
4 * /src/NTP/REPOSITORY/ntp4-dev/libparse/data_mbg.c,v 4.8 2006/06/22 18:40:01 kardel RELEASE_20060622_A
6 * data_mbg.c,v 4.8 2006/06/22 18:40:01 kardel RELEASE_20060622_A
8 * Created: Sun Jul 20 12:08:14 1997
10 * Copyright (c) 1997-2005 by Frank Kardel <kardel <AT> ntp.org>
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.
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.
38 #ifdef PARSESTREAM
39 #define NEED_BOPS
40 #include "ntp_string.h"
41 #else
42 #include <stdio.h>
43 #endif
44 #include "ntp_types.h"
45 #include "ntp_stdlib.h"
46 #include "ntp_fp.h"
47 #include "mbg_gps166.h"
48 #include "binio.h"
49 #include "ieee754io.h"
51 static void get_mbg_tzname P((unsigned char **, char *));
52 static void mbg_time_status_str P((char **, unsigned int, int));
54 #if 0 /* no actual floats on Meinberg binary interface */
55 static offsets_t mbg_float = { 1, 0, 3, 2, 0, 0, 0, 0 }; /* byte order for meinberg floats */
56 #endif
57 static offsets_t mbg_double = { 1, 0, 3, 2, 5, 4, 7, 6 }; /* byte order for meinberg doubles */
58 static int32 rad2deg_i = 57;
59 static u_int32 rad2deg_f = 0x4BB834C7; /* 57.2957795131 == 180/PI */
61 void
62 put_mbg_header(
63 unsigned char **bufpp,
64 GPS_MSG_HDR *headerp
67 put_lsb_short(bufpp, headerp->gps_cmd);
68 put_lsb_short(bufpp, headerp->gps_len);
69 put_lsb_short(bufpp, headerp->gps_data_csum);
70 put_lsb_short(bufpp, headerp->gps_hdr_csum);
73 void
74 get_mbg_sw_rev(
75 unsigned char **bufpp,
76 SW_REV *sw_revp
79 sw_revp->code = get_lsb_short(bufpp);
80 memcpy(sw_revp->name, *bufpp, sizeof(sw_revp->name));
81 *bufpp += sizeof(sw_revp->name);
84 void
85 get_mbg_ascii_msg(
86 unsigned char **bufpp,
87 ASCII_MSG *ascii_msgp
90 ascii_msgp->csum = get_lsb_short(bufpp);
91 ascii_msgp->valid = get_lsb_short(bufpp);
92 memcpy(ascii_msgp->s, *bufpp, sizeof(ascii_msgp->s));
93 *bufpp += sizeof(ascii_msgp->s);
96 void
97 get_mbg_svno(
98 unsigned char **bufpp,
99 SVNO *svnop
102 *svnop = get_lsb_short(bufpp);
105 void
106 get_mbg_health(
107 unsigned char **bufpp,
108 HEALTH *healthp
111 *healthp = get_lsb_short(bufpp);
114 void
115 get_mbg_cfg(
116 unsigned char **bufpp,
117 CFG *cfgp
120 *cfgp = get_lsb_short(bufpp);
123 void
124 get_mbg_tgps(
125 unsigned char **bufpp,
126 T_GPS *tgpsp
129 tgpsp->wn = get_lsb_short(bufpp);
130 tgpsp->sec = get_lsb_long(bufpp);
131 tgpsp->tick = get_lsb_long(bufpp);
134 void
135 get_mbg_tm(
136 unsigned char **buffpp,
137 TM *tmp
140 tmp->year = get_lsb_short(buffpp);
141 tmp->month = *(*buffpp)++;
142 tmp->mday = *(*buffpp)++;
143 tmp->yday = get_lsb_short(buffpp);
144 tmp->wday = *(*buffpp)++;
145 tmp->hour = *(*buffpp)++;
146 tmp->minute = *(*buffpp)++;
147 tmp->second = *(*buffpp)++;
148 tmp->frac = get_lsb_long(buffpp);
149 tmp->offs_from_utc = get_lsb_long(buffpp);
150 tmp->status= get_lsb_short(buffpp);
153 void
154 get_mbg_ttm(
155 unsigned char **buffpp,
156 TTM *ttmp
159 ttmp->channel = get_lsb_short(buffpp);
160 get_mbg_tgps(buffpp, &ttmp->t);
161 get_mbg_tm(buffpp, &ttmp->tm);
164 void
165 get_mbg_synth(
166 unsigned char **buffpp,
167 SYNTH *synthp
170 synthp->freq = get_lsb_short(buffpp);
171 synthp->range = get_lsb_short(buffpp);
172 synthp->phase = get_lsb_short(buffpp);
175 static void
176 get_mbg_tzname(
177 unsigned char **buffpp,
178 char *tznamep
181 strncpy(tznamep, (char *)*buffpp, sizeof(TZ_NAME));
182 *buffpp += sizeof(TZ_NAME);
185 void
186 get_mbg_tzdl(
187 unsigned char **buffpp,
188 TZDL *tzdlp
191 tzdlp->offs = get_lsb_long(buffpp);
192 tzdlp->offs_dl = get_lsb_long(buffpp);
193 get_mbg_tm(buffpp, &tzdlp->tm_on);
194 get_mbg_tm(buffpp, &tzdlp->tm_off);
195 get_mbg_tzname(buffpp, (char *)tzdlp->name[0]);
196 get_mbg_tzname(buffpp, (char *)tzdlp->name[1]);
199 void
200 get_mbg_antinfo(
201 unsigned char **buffpp,
202 ANT_INFO *antinfop
205 antinfop->status = get_lsb_short(buffpp);
206 get_mbg_tm(buffpp, &antinfop->tm_disconn);
207 get_mbg_tm(buffpp, &antinfop->tm_reconn);
208 antinfop->delta_t = get_lsb_long(buffpp);
211 static void
212 mbg_time_status_str(
213 char **buffpp,
214 unsigned int status,
215 int size
218 static struct state
220 int flag; /* bit flag */
221 const char *string; /* bit name */
222 } states[] =
224 { TM_UTC, "UTC CORR" },
225 { TM_LOCAL, "LOCAL TIME" },
226 { TM_DL_ANN, "DST WARN" },
227 { TM_DL_ENB, "DST" },
228 { TM_LS_ANN, "LEAP WARN" },
229 { TM_LS_ENB, "LEAP SEC" },
230 { 0, "" }
233 if (status)
235 char *start, *p;
236 struct state *s;
238 start = p = *buffpp;
240 for (s = states; s->flag; s++)
242 if (s->flag & status)
244 if (p != *buffpp)
246 strncpy((char *)p, ", ", size - (p - start));
247 p += 2;
249 strncpy(p, s->string, size - (p - start));
250 p += strlen(p);
253 *buffpp = p;
257 void
258 mbg_tm_str(
259 char **buffpp,
260 TM *tmp,
261 int size
264 char *s = *buffpp;
266 snprintf(*buffpp, size, "%04d-%02d-%02d %02d:%02d:%02d.%07ld (%c%02d%02d) ",
267 tmp->year, tmp->month, tmp->mday,
268 tmp->hour, tmp->minute, tmp->second, tmp->frac,
269 (tmp->offs_from_utc < 0) ? '-' : '+',
270 abs(tmp->offs_from_utc) / 3600,
271 (abs(tmp->offs_from_utc) / 60) % 60);
272 *buffpp += strlen(*buffpp);
274 mbg_time_status_str(buffpp, tmp->status, size - (*buffpp - s));
277 void
278 mbg_tgps_str(
279 char **buffpp,
280 T_GPS *tgpsp,
281 int size
284 snprintf(*buffpp, size, "week %d + %ld days + %ld.%07ld sec",
285 tgpsp->wn, tgpsp->sec / 86400,
286 tgpsp->sec % 86400, tgpsp->tick);
287 *buffpp += strlen(*buffpp);
290 void
291 get_mbg_cfgh(
292 unsigned char **buffpp,
293 CFGH *cfghp
296 int i;
298 cfghp->csum = get_lsb_short(buffpp);
299 cfghp->valid = get_lsb_short(buffpp);
300 get_mbg_tgps(buffpp, &cfghp->tot_51);
301 get_mbg_tgps(buffpp, &cfghp->tot_63);
302 get_mbg_tgps(buffpp, &cfghp->t0a);
304 for (i = MIN_SVNO; i <= MAX_SVNO; i++)
306 get_mbg_cfg(buffpp, &cfghp->cfg[i]);
309 for (i = MIN_SVNO; i <= MAX_SVNO; i++)
311 get_mbg_health(buffpp, &cfghp->health[i]);
315 void
316 get_mbg_utc(
317 unsigned char **buffpp,
318 UTC *utcp
321 utcp->csum = get_lsb_short(buffpp);
322 utcp->valid = get_lsb_short(buffpp);
324 get_mbg_tgps(buffpp, &utcp->t0t);
326 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &utcp->A0, mbg_double) != IEEE_OK)
328 L_CLR(&utcp->A0);
331 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &utcp->A1, mbg_double) != IEEE_OK)
333 L_CLR(&utcp->A1);
336 utcp->WNlsf = get_lsb_short(buffpp);
337 utcp->DNt = get_lsb_short(buffpp);
338 utcp->delta_tls = *(*buffpp)++;
339 utcp->delta_tlsf = *(*buffpp)++;
342 void
343 get_mbg_lla(
344 unsigned char **buffpp,
345 LLA lla
348 int i;
350 for (i = LAT; i <= ALT; i++)
352 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &lla[i], mbg_double) != IEEE_OK)
354 L_CLR(&lla[i]);
356 else
357 if (i != ALT)
358 { /* convert to degrees (* 180/PI) */
359 mfp_mul(&lla[i].l_i, &lla[i].l_uf, lla[i].l_i, lla[i].l_uf, rad2deg_i, rad2deg_f);
364 void
365 get_mbg_xyz(
366 unsigned char **buffpp,
367 XYZ xyz
370 int i;
372 for (i = XP; i <= ZP; i++)
374 if (fetch_ieee754(buffpp, IEEE_DOUBLE, &xyz[i], mbg_double) != IEEE_OK)
376 L_CLR(&xyz[i]);
381 static void
382 get_mbg_comparam(
383 unsigned char **buffpp,
384 COM_PARM *comparamp
387 int i;
389 comparamp->baud_rate = get_lsb_long(buffpp);
390 for (i = 0; i < sizeof(comparamp->framing); i++)
392 comparamp->framing[i] = *(*buffpp)++;
394 comparamp->handshake = get_lsb_short(buffpp);
397 void
398 get_mbg_portparam(
399 unsigned char **buffpp,
400 PORT_PARM *portparamp
403 int i;
405 for (i = 0; i < N_COM; i++)
407 get_mbg_comparam(buffpp, &portparamp->com[i]);
409 for (i = 0; i < N_COM; i++)
411 portparamp->mode[i] = *(*buffpp)++;
415 #define FETCH_DOUBLE(src, addr) \
416 if (fetch_ieee754(src, IEEE_DOUBLE, addr, mbg_double) != IEEE_OK) \
418 L_CLR(addr); \
421 void
422 get_mbg_eph(
423 unsigned char ** buffpp,
424 EPH *ephp
427 ephp->csum = get_lsb_short(buffpp);
428 ephp->valid = get_lsb_short(buffpp);
430 ephp->health = get_lsb_short(buffpp);
431 ephp->IODC = get_lsb_short(buffpp);
432 ephp->IODE2 = get_lsb_short(buffpp);
433 ephp->IODE3 = get_lsb_short(buffpp);
435 get_mbg_tgps(buffpp, &ephp->tt);
436 get_mbg_tgps(buffpp, &ephp->t0c);
437 get_mbg_tgps(buffpp, &ephp->t0e);
439 FETCH_DOUBLE(buffpp, &ephp->sqrt_A);
440 FETCH_DOUBLE(buffpp, &ephp->e);
441 FETCH_DOUBLE(buffpp, &ephp->M0);
442 FETCH_DOUBLE(buffpp, &ephp->omega);
443 FETCH_DOUBLE(buffpp, &ephp->OMEGA0);
444 FETCH_DOUBLE(buffpp, &ephp->OMEGADOT);
445 FETCH_DOUBLE(buffpp, &ephp->deltan);
446 FETCH_DOUBLE(buffpp, &ephp->i0);
447 FETCH_DOUBLE(buffpp, &ephp->idot);
448 FETCH_DOUBLE(buffpp, &ephp->crc);
449 FETCH_DOUBLE(buffpp, &ephp->crs);
450 FETCH_DOUBLE(buffpp, &ephp->cuc);
451 FETCH_DOUBLE(buffpp, &ephp->cus);
452 FETCH_DOUBLE(buffpp, &ephp->cic);
453 FETCH_DOUBLE(buffpp, &ephp->cis);
455 FETCH_DOUBLE(buffpp, &ephp->af0);
456 FETCH_DOUBLE(buffpp, &ephp->af1);
457 FETCH_DOUBLE(buffpp, &ephp->af2);
458 FETCH_DOUBLE(buffpp, &ephp->tgd);
460 ephp->URA = get_lsb_short(buffpp);
462 ephp->L2code = *(*buffpp)++;
463 ephp->L2flag = *(*buffpp)++;
466 void
467 get_mbg_alm(
468 unsigned char **buffpp,
469 ALM *almp
472 almp->csum = get_lsb_short(buffpp);
473 almp->valid = get_lsb_short(buffpp);
475 almp->health = get_lsb_short(buffpp);
476 get_mbg_tgps(buffpp, &almp->t0a);
479 FETCH_DOUBLE(buffpp, &almp->sqrt_A);
480 FETCH_DOUBLE(buffpp, &almp->e);
482 FETCH_DOUBLE(buffpp, &almp->M0);
483 FETCH_DOUBLE(buffpp, &almp->omega);
484 FETCH_DOUBLE(buffpp, &almp->OMEGA0);
485 FETCH_DOUBLE(buffpp, &almp->OMEGADOT);
486 FETCH_DOUBLE(buffpp, &almp->deltai);
487 FETCH_DOUBLE(buffpp, &almp->af0);
488 FETCH_DOUBLE(buffpp, &almp->af1);
491 void
492 get_mbg_iono(
493 unsigned char **buffpp,
494 IONO *ionop
497 ionop->csum = get_lsb_short(buffpp);
498 ionop->valid = get_lsb_short(buffpp);
500 FETCH_DOUBLE(buffpp, &ionop->alpha_0);
501 FETCH_DOUBLE(buffpp, &ionop->alpha_1);
502 FETCH_DOUBLE(buffpp, &ionop->alpha_2);
503 FETCH_DOUBLE(buffpp, &ionop->alpha_3);
505 FETCH_DOUBLE(buffpp, &ionop->beta_0);
506 FETCH_DOUBLE(buffpp, &ionop->beta_1);
507 FETCH_DOUBLE(buffpp, &ionop->beta_2);
508 FETCH_DOUBLE(buffpp, &ionop->beta_3);
512 * data_mbg.c,v
513 * Revision 4.8 2006/06/22 18:40:01 kardel
514 * clean up signedness (gcc 4)
516 * Revision 4.7 2005/10/07 22:11:10 kardel
517 * bounded buffer implementation
519 * Revision 4.6.2.1 2005/09/25 10:23:06 kardel
520 * support bounded buffers
522 * Revision 4.6 2005/04/16 17:32:10 kardel
523 * update copyright
525 * Revision 4.5 2004/11/14 15:29:41 kardel
526 * support PPSAPI, upgrade Copyright to Berkeley style
528 * Revision 4.3 1999/02/21 12:17:42 kardel
529 * 4.91f reconcilation
531 * Revision 4.2 1998/06/14 21:09:39 kardel
532 * Sun acc cleanup
534 * Revision 4.1 1998/05/24 08:02:06 kardel
535 * trimmed version log
537 * Revision 4.0 1998/04/10 19:45:33 kardel
538 * Start 4.0 release version numbering