mbgtools-lx  4.2.8
identdec.c
Go to the documentation of this file.
1 
2 /**************************************************************************
3  *
4  * $Id: identdec.c 1.3 2009/04/01 14:15:05 martin REL_M $
5  *
6  * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany
7  *
8  * Description:
9  * Supplies a function to decode various types of a GPS receiver's
10  * IDENT structure and generate a group code + S/N string.
11  *
12  * -----------------------------------------------------------------------
13  * $Log: identdec.c $
14  * Revision 1.3 2009/04/01 14:15:05 martin
15  * Fixed compiler warning.
16  * Revision 1.2 2002/11/21 08:11:59Z martin
17  * Avoid usage of strcpy functions since they may not
18  * be available in kernel space for some targets.
19  * Revision 1.1 2002/02/19 13:46:19 MARTIN
20  * Initial revision
21  *
22  **************************************************************************/
23 
24 #define _IDENTDEC
25  #include <identdec.h>
26 #undef _IDENTDEC
27 
28 #include <qsdefs.h>
29 
30 
31 // Some targets don't support isdigit() from ctype.h:
32 #define _is_digit( _c ) ( (_c) >= '0' && (_c) <= '9' )
33 
34 
35 // Some targets are unable to call functions from the
36 // stancard C library, so we provide necessary functions locally.
37 
38 static /*HDR*/
39 char *do_strnpcpy( char *p_dst, const char *p_src, int n )
40 {
41  int i;
42 
43  for ( i = 0; i < n; i++ )
44  {
45  char c = *p_src++;
46  *p_dst++ = c;
47 
48  if ( c == 0 )
49  break;
50  }
51 
52  return p_dst;
53 
54 } // do_strnpcpy
55 
56 
57 
58 /*--------------------------------------------------------------
59  * Name: mbg_gps_ident_decode()
60  *
61  * Purpose: Convert a GPS ident code to a string with
62  * serial number.
63  *
64  * Input: p_id pointer to the IDENT
65  *
66  * Output: s the resulting string
67  *
68  * Ret value: --
69  *+-------------------------------------------------------------*/
70 
71 /*HDR*/
72 char *mbg_gps_ident_swap( char *p_dst, const char *p_src )
73 {
74  int i;
75 
76  for ( i = 0; i < 4; i++ )
77  {
78  *p_dst++ = *( p_src + 3 );
79  *p_dst++ = *( p_src + 2 );
80  *p_dst++ = *( p_src + 1 );
81  *p_dst++ = *( p_src );
82  p_src += 4;
83  }
84 
85  return( p_dst );
86 
87 } // mbg_gps_ident_swap
88 
89 
90 
91 /*HDR*/
92 void mbg_gps_ident_decode( char *s, const IDENT *p_id )
93 {
94  char ws[sizeof( *p_id ) + 1]; // tmp buffer
95  char *cp;
96  char c = 0;
97  int n_spaces = 0;
98  int i;
99 
100  // get string from binary format used by firmware
101  mbg_gps_ident_swap( ws, (const char *) p_id );
102 
103  // make sure the resulting string is terminated by 0
104  ws[sizeof( *p_id )] = 0;
105 
106  // Now ws contains a raw string which may be in one
107  // of the following formats. The first one, which includes
108  // a group code is the preferred format:
109  //
110  // "gggg nnnnnnnn" group code, gap filled with spaces, S/N
111  // "nnnnnnnn........" S/N, plus non-digits
112  // "........nnnnnnnn" non-digits, followed by S/N
113 
114  cp = ws;
115 
116  // test for the number of digits at the beginning
117  for ( i = 0; i < MBG_SERNUM_LEN; i++ )
118  {
119  c = *cp++;
120 
121  if ( !_is_digit( c ) )
122  break;
123  }
124 
125  if ( i != MBG_GRP_CODE_LEN )
126  goto copy; // not a group code
127 
128 
129  // expect a number of spaces
130  n_spaces = sizeof( *p_id ) - MBG_GRP_SERNUM_LEN;
131 
132  for ( i = 0; i < n_spaces; i++ )
133  {
134  if ( c != ' ' )
135  {
136  n_spaces = 0;
137  goto copy;
138  }
139 
140  c = *cp++;
141  }
142 
143 
144  // test number of S/N digits
145  for ( i = 0; i < MBG_SERNUM_LEN; i++ )
146  {
147  if ( !_is_digit( c ) )
148  {
149  n_spaces = 0;
150  goto copy;
151  }
152 
153  c = *cp++;
154  }
155 
156 copy:
157  cp = do_strnpcpy( s, ws, MBG_GRP_CODE_LEN );
158  i = MBG_GRP_CODE_LEN + n_spaces;
159  do_strnpcpy( cp, &ws[i], sizeof( ws ) - i );
160 
161 } // mbg_gps_ident_decode
162 
163 
164 
#define MBG_GRP_CODE_LEN
Definition: qsdefs.h:27
static char * do_strnpcpy(char *p_dst, const char *p_src, int n)
Definition: identdec.c:39
#define MBG_SERNUM_LEN
Definition: qsdefs.h:38
#define MBG_GRP_SERNUM_LEN
Definition: qsdefs.h:41
char * mbg_gps_ident_swap(char *p_dst, const char *p_src)
Definition: identdec.c:72
void mbg_gps_ident_decode(char *s, const IDENT *p_id)
Definition: identdec.c:92
#define _is_digit(_c)
Definition: identdec.c:32