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