summaryrefslogtreecommitdiff
path: root/mbglib/common/gpsutils.c
blob: 2388df35a2f1d00fed86976527066b4326070d69 (plain)
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492

/**************************************************************************
 *
 *  $Id: gpsutils.c 1.9.1.7 2016/08/11 12:45:54 martin TEST $
 *
 *  Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
 *
 *  Description:
 *    Utility functions useful with GPS data.
 *
 * -----------------------------------------------------------------------
 *  $Log: gpsutils.c $
 *  Revision 1.9.1.7  2016/08/11 12:45:54  martin
 *  Revision 1.9.1.6  2016/08/09 15:56:50Z  martin
 *  Fixed format bug.
 *  Revision 1.9.1.5  2016/07/22 09:57:11  martin
 *  Quieted some compiler warninges.
 *  Revision 1.9.1.4  2015/09/07 09:19:58  martin
 *  Revision 1.9.1.3  2015/08/27 16:24:41  martin
 *  Revision 1.9.1.2  2015/08/25 15:34:48  martin
 *  Use save string functions from str_util.c.
 *  Revision 1.9.1.1  2015/08/21 14:21:57  martin
 *  Revision 1.9  2013/01/30 16:10:08  martin
 *  Exclude some code from compiling by default, and
 *  thus don't require pcpslstr.h by default.
 *  Revision 1.8  2012/10/15 14:27:05Z  martin
 *  Exclude sprint_fixed_freq() from build except for Borland C / Windows.
 *  Revision 1.7  2010/07/15 09:32:09  martin
 *  Use DEG character definition from pcpslstr.h.
 *  Revision 1.6  2004/12/28 11:21:26Z  martin
 *  Omit trap if fixed_freq is 0.
 *  Use C99 fixed-size data types were required.
 *  Revision 1.5  2003/02/04 09:20:04Z  MARTIN
 *  New functions sprint_alt(), sprint_fixed_freq().
 *  Revision 1.4  2003/01/31 13:45:19  MARTIN
 *  sprint_pos_geo() returns N/A if position not valid.
 *  Revision 1.3  2002/12/12 16:07:04  martin
 *  New functions swap_pos_doubles(), sprint_dms(),
 *  and sprint_pos_geo().
 *  Revision 1.2  2001/02/05 09:39:12Z  MARTIN
 *  New file header.
 *  Change include file name to lower case.
 *  Source code cleanup.
 *
 **************************************************************************/

#define _GPSUTILS
 #include <gpsutils.h>
#undef _GPSUTILS

#if defined( USE_SPRINTF )
  #error USE_SPRINTF was obsoleted by USE_SNPRINTF. Please update project settings.
#endif

#if !defined( USE_SNPRINTF )
  #define USE_SNPRINTF 0
#endif

#if !defined( _USE_GPSUTILS_FULL )
  #if defined( MBG_TGT_WIN32 ) && defined( __BORLANDC__ )
    #define _USE_GPSUTILS_FULL  1
  #else
    #define _USE_GPSUTILS_FULL  0
  #endif
#endif

#if USE_SNPRINTF
  #include <str_util.h>
//  #include <pcpslstr.h>
#define DEG "deg"
#endif

#if  _USE_GPSUTILS_FULL
  #include <str_util.h>
  #include <math.h>
#endif

#include <stdio.h>
#include <string.h>


#if USE_SNPRINTF || _USE_GPSUTILS_FULL

static const char str_na[] = "N/A";

#endif



/*HDR*/
/**
 * @brief Swap the bytes of a single variable of type "double"
 *
 * The memory layout of a "double" on Meinberg bus level devices
 * and computers usually differs. This function can be used to
 * fix this and is usually called from inside API functions,
 * if required.
 *
 * @param[in,out] p   Pointer to a "double" to be swapped
 *
 * @see ::swap_eph_doubles
 * @see ::swap_alm_doubles
 * @see ::swap_utc_doubles
 * @see ::swap_iono_doubles
 * @see ::swap_pos_doubles
 */
void swap_double( double *p )
{
  uint16_t *wp1;
  uint16_t *wp2;
  uint16_t w;
  int i;

  wp1 = (uint16_t *) p;
  wp2 = ( (uint16_t *) p ) + 3;

  for ( i = 0; i < 2; i++ )
  {
    w = *wp1;
    *wp1 = *wp2;
    *wp2 = w;
    wp1++;
    wp2--;
  }

}  // swap_double



/*HDR*/
/**
 * @brief Swap the "double" fields in an ::EPH structure
 *
 * See comments for ::swap_double
 *
 * @param[in,out] p  Pointer to an ::EPH structure to be converted
 *
 * @see ::swap_double
 * @see ::swap_alm_doubles
 * @see ::swap_utc_doubles
 * @see ::swap_iono_doubles
 * @see ::swap_pos_doubles
 */
void swap_eph_doubles( EPH *p )
{
  swap_double( &p->sqrt_A );
  swap_double( &p->e );
  swap_double( &p->M0 );
  swap_double( &p->omega );
  swap_double( &p->i0 );
  swap_double( &p->OMEGA0 );
  swap_double( &p->OMEGADOT );

  swap_double( &p->deltan );
  swap_double( &p->idot );

  swap_double( &p->crc );
  swap_double( &p->crs );
  swap_double( &p->cuc );
  swap_double( &p->cus );
  swap_double( &p->cic );
  swap_double( &p->cis );

  swap_double( &p->af0 );
  swap_double( &p->af1 );
  swap_double( &p->af2 );

  swap_double( &p->tgd );

}  /* swap_eph_doubles */



/*HDR*/
/**
 * @brief Swap the "double" fields in an ::ALM structure
 *
 * See comments for ::swap_double
 *
 * @param[in,out] p  Pointer to an ::ALM structure to be converted
 *
 * @see ::swap_double
 * @see ::swap_eph_doubles
 * @see ::swap_utc_doubles
 * @see ::swap_iono_doubles
 * @see ::swap_pos_doubles
 */
void swap_alm_doubles( ALM *p )
{
  swap_double( &p->sqrt_A );
  swap_double( &p->e );
  swap_double( &p->deltai );
  swap_double( &p->OMEGA0 );
  swap_double( &p->OMEGADOT );
  swap_double( &p->omega );
  swap_double( &p->M0 );
  swap_double( &p->af0 );
  swap_double( &p->af1 );

}  /* swap_alm_doubles */



/*HDR*/
/**
 * @brief Swap the "double" fields in a ::UTC structure
 *
 * See comments for ::swap_double
 *
 * @param[in,out] p  Pointer to a ::UTC structure to be converted
 *
 * @see ::swap_double
 * @see ::swap_eph_doubles
 * @see ::swap_alm_doubles
 * @see ::swap_iono_doubles
 * @see ::swap_pos_doubles
 */
void swap_utc_doubles( UTC *p )
{
  swap_double( &p->A0 );
  swap_double( &p->A1 );

}  /* swap_utc_doubles */



/*HDR*/
/**
 * @brief Swap the "double" fields in a ::IONO structure
 *
 * See comments for ::swap_double
 *
 * @param[in,out] p  Pointer to a ::IONO structure to be converted
 *
 * @see ::swap_double
 * @see ::swap_eph_doubles
 * @see ::swap_alm_doubles
 * @see ::swap_utc_doubles
 * @see ::swap_pos_doubles
 */
void swap_iono_doubles( IONO *p )
{
  swap_double( &p->alpha_0 );
  swap_double( &p->alpha_1 );
  swap_double( &p->alpha_2 );
  swap_double( &p->alpha_3 );

  swap_double( &p->beta_0 );
  swap_double( &p->beta_1 );
  swap_double( &p->beta_2 );
  swap_double( &p->beta_3 );

}  /* swap_iono_doubles */



/*HDR*/
/**
 * @brief Swap the "double" fields in a ::POS structure
 *
 * See comments for ::swap_double
 *
 * @param[in,out] p  Pointer to a ::POS structure to be converted
 *
 * @see ::swap_double
 * @see ::swap_eph_doubles
 * @see ::swap_alm_doubles
 * @see ::swap_utc_doubles
 * @see ::swap_iono_doubles
 */
void swap_pos_doubles( POS *p )
{
  int i;

  for ( i = 0; i < N_XYZ; i++ )
    swap_double( &p->xyz[i] );

  for ( i = 0; i < N_LLA; i++ )
    swap_double( &p->lla[i] );

  swap_double( &p->longitude.sec );
  swap_double( &p->latitude.sec );

}  /* swap_pos_doubles */



#if USE_SNPRINTF

/*HDR*/
/**
 * @brief Print the ::DMS part of a geo position into a string buffer
 *
 * @param[out] s        The string buffer to be filled
 * @param[in]  max_len  Size of the output buffer for 0-terminated string
 * @param[in]  p        Pointer to a ::DMS structure to be printed
 * @param[in]  prec     Precision, i.e. number of fractions of the seconds
 *
 * @return Length of the string in the buffer
 *
 * @see snprint_dms
 * @see snprint_alt
 * @see snprint_pos_geo
 * @see snprint_fixed_freq
 */
size_t snprint_dms( char *s, size_t max_len, const DMS *p, int prec )
{
  size_t n = snprintf_safe( s, max_len, "%c %i" DEG "%02i'%02.*f\"",
                            p->prefix, p->deg, p->min,
                            prec, p->sec );

  return n;

}  // snprint_dms



/*HDR*/
/**
 * @brief Print the altitude part of a geo position into a string buffer
 *
 * @param[out] s        The string buffer to be filled
 * @param[in]  max_len  Size of the output buffer for 0-terminated string
 * @param[in]  alt      The altitude value to be printed, in [m]
 *
 * @return Length of the string in the buffer
 *
 * @see snprint_dms
 * @see snprint_pos_geo
 * @see snprint_fixed_freq
 */
size_t snprint_alt( char *s, size_t max_len, double alt )
{
  size_t n = snprintf_safe( s, max_len, "%.0fm", alt );

  return n;

}  // snprint_alt



/*HDR*/
/**
 * @brief Print a geo position in ::POS format into a string buffer
 *
 * @param[out] s        The string buffer to be filled
 * @param[in]  max_len  Size of the output buffer for 0-terminated string
 * @param[in]  p        Pointer to a ::POS structure to be printed
 * @param[in]  sep      Separator character for the ::DMS part
 * @param[in]  prec     Precision, i.e. number of fractions of the seconds of the ::DMS part
 *
 * @return Length of the string in the buffer
 *
 * @see snprint_dms
 * @see snprint_alt
 * @see snprint_fixed_freq
 */
size_t snprint_pos_geo( char *s, size_t max_len, const POS *p, char sep, int prec )
{
  size_t n = 0;

  if ( p->lla[LON] && p->lla[LAT] && p->lla[ALT] )
  {
    n += snprint_dms( &s[n], max_len - n, &p->latitude, prec );
    n += snprintf_safe( &s[n], max_len - n, "%c", sep );
    n += snprint_dms( &s[n], max_len - n, &p->longitude, prec );
    n += snprintf_safe( &s[n], max_len - n, "%c", sep );
    n += snprint_alt( &s[n], max_len - n, p->lla[ALT] );
  }
  else
    n = sn_cpy_str_safe( s, max_len, str_na );

  return n;

}  // sprint_pos_geo

#endif  // USE_SNPRINTF



#if _USE_GPSUTILS_FULL

/*HDR*/
/**
 * @brief Print a formatted ::FIXED_FREQ_INFO into a string buffer
 *
 * @param[out] s        The string buffer to be filled
 * @param[in]  max_len  Size of the output buffer for 0-terminated string
 * @param[in]  p_ff     Pointer to a ::FIXED_FREQ_INFO structure to be printed
 *
 * @return Length of the string in the buffer
 *
 * @see snprint_dms
 * @see snprint_alt
 * @see snprint_pos_geo
 */
size_t snprint_fixed_freq( char *s, size_t max_len, FIXED_FREQ_INFO *p_ff )
{
  double freq;
  int range;
  ushort unit;
  ushort format;
  size_t n = 0;

  // Before re-calculating frequency, range is the base 10 exponent
  // to the frequency value which is represented in kHz.
  // After calculating range from real frequency, range is represented
  // as follows:
  //    range  display  format  divisor    format index calculation
  //     -3    100mHz   1.000  [/1e-3]   -3 % 3 = -3 + 3 = 0 % 3 = 0
  //     -2    100mHz   10.00  [/1e-3]   -2 % 3 = -2 + 3 = 1 % 3 = 1
  //     -1    100mHz   100.0  [/1e-3]   -1 % 3 = -1 + 3 = 2 % 3 = 2
  //      0    1Hz      1.000  [/1e0]     0 % 3 =  0 + 3 = 3 % 3 = 0
  //      1    10Hz     10.00  [/1e0]     1 % 3 =  1 + 3 = 1 % 3 = 1
  //      2    100Hz    100.0  [/1e0]     2 % 3 =  2 + 3 = 2 % 3 = 2
  //      3    1kHz     1.000  [/1e3]     3 % 3 =  0 + 3 = 3 % 3 = 0
  //      4    10kHz    10.00  [/1e3]     4 % 3 =  1 + 3 = 4 % 3 = 1
  //      5    100kHz   100.0  [/1e3]     5 % 3 =  2 + 3 = 5 % 3 = 2
  //      6    1MHz     1.000  [/1e6]     6 % 3 =  0 + 3 = 3 % 3 = 0
  //      7    10MHz    10.00  [/1e6]     7 % 3 =  1 + 3 = 4 % 3 = 1
  //      8    100MHz   100.0  [/1e6]     8 % 3 =  2 + 3 = 5 % 3 = 2

  // format string for fp output
  static const char *fmt_str[] =
  {
    "%4.3lf%s",
    "%4.2lf%s",
    "%4.1lf%s",
  };

  // Unit index and divisor are calculated as follows:
  // range  unit        index calculation            divisor calculation
  // -3      mHz   ( int )( ( -3 + 3 ) / 3 ) =  0
  // -2      mHz   ( int )( ( -2 + 3 ) / 3 ) =  0
  // -1      mHz   ( int )( ( -1 + 3 ) / 3 ) =  0    / 10e-3 = 10e( 3 * 0 - 3 )
  //  0      Hz    ( int )( (  0 + 3 ) / 3 ) =  1
  //  1      Hz    ( int )( (  1 + 3 ) / 3 ) =  1
  //  2      Hz    ( int )( (  2 + 3 ) / 3 ) =  1    / 1e0   = 10e( 3 * 1 - 3 )
  //  3      kHz   ( int )( (  3 + 3 ) / 3 ) =  2
  //  4      kHz   ( int )( (  4 + 3 ) / 3 ) =  2
  //  5      kHz   ( int )( (  5 + 3 ) / 3 ) =  2    / 10e3  = 10e( 3 * 2 - 3 )
  //  6      MHz   ( int )( (  6 + 3 ) / 3 ) =  3
  //  7      MHz   ( int )( (  7 + 3 ) / 3 ) =  3
  //  8      MHz   ( int )( (  8 + 3 ) / 3 ) =  3    / 10e6   =10e( 3 * 3 - 3 )

  // unit string
  static const char *unit_str[] =
  {
    "mHz",
    "Hz",
    "kHz",
    "MHz"
  };


  if ( p_ff->khz_val )
  {
    // calculate frequency in Hz
    freq = ( double ) p_ff->khz_val * pow( 10, ( p_ff->range + 3 ) );

    // calculate decimal exponent
    range = ( ( int ) log10( freq ) );

    // check whether range is in the allowed range
    // if so display frequency in broken format
    if ( ( range >= -3 ) && ( range <= 8 ) )
    {
      // calculate format index ( see above )
      format = ( ( ( range % 3 ) + 3 ) % 3 );

      // calculate unit index
      unit =  ( ushort )( range + 3 ) / 3;

      // calculate display value
      freq = freq / pow( 10, ( ( 3 * unit ) - 3 ) );
      n = snprintf_safe( s, max_len, fmt_str[format], freq, unit_str[unit] );
    }
    else
    {
      // out of range display fequency in Hz
      n = snprintf_safe( s, max_len, "%fHz", freq );
    }
  }
  else
    n = sn_cpy_str_safe( s, max_len, str_na );

  return n;

}  // snprint_fixed_freq

#endif  // _USE_GPSUTILS_FULL