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