netmsgs  1.2.2
RoadNarrows Robotics Network Messaging Package
nmLibUtils.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: netmsgs
4 //
5 // Library: libnetmsgs
6 //
7 // File: nmLibUtils.c
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2010-07-31 08:48:56 -0600 (Sat, 31 Jul 2010) $
12  * $Rev: 521 $
13  *
14  * \brief NetMsgs library utilities.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2010-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 // Permission is hereby granted, without written agreement and without
24 // license or royalty fees, to use, copy, modify, and distribute this
25 // software and its documentation for any purpose, provided that
26 // (1) The above copyright notice and the following two paragraphs
27 // appear in all copies of the source code and (2) redistributions
28 // including binaries reproduces these notices in the supporting
29 // documentation. Substantial modifications to this software may be
30 // copyrighted by their authors and need not follow the licensing terms
31 // described here, provided that the new terms are clearly indicated in
32 // all files where they apply.
33 //
34 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
35 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
36 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
37 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
38 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
39 // THE POSSIBILITY OF SUCH DAMAGE.
40 //
41 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46 //
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 
52 #include "rnr/rnrconfig.h"
53 #include "rnr/log.h"
54 
55 #include "rnr/netmsgs.h"
56 
57 #include "nmLibInternal.h"
58 
59 // ---------------------------------------------------------------------------
60 // Private Interface
61 // ---------------------------------------------------------------------------
62 
63 /*!
64  * \brief NetMsgs Error Code String Table.
65  *
66  * Table is indexed by NetMsgs error codes (see \ref man_libnetmsgs_ecodes).
67  * Keep in sync.
68  */
69 static const char *nmEcodeStrTbl[] =
70 {
71  "Ok", ///< [NM_OK]
72 
73  "Error", ///< [NM_ECODE_GEN]
74  "Insufficient memory available", ///< [NM_ECODE_NOMEM]
75  "Machine architecture not supported", ///< [NM_ECODE_ARCH_NOTSUP]
76  "Value out-of-range", ///< [NM_ECODE_RANGE]
77  "Unknown field type", ///< [NM_ECODE_FTYPE]
78  "Bad message format", ///< [NM_ECODE_EMSG]
79  "Unknown message id", ///< [NM_ECODE_MSGID]
80  "Internal error", ///< [NM_ECODE_INTERNAL]
81  "Invalid error code", ///< [NM_ECODE_BADEC]
82 
83  NULL, 0
84 };
85 /*!
86  * Field Value Byte Size Lookup Table
87  *
88  * \warning Keep in asending order by ftype.
89  */
90 static size_t NMFValLenLookupTbl[] =
91 {
97 };
98 
99 // ---------------------------------------------------------------------------
100 // Public Interface
101 // ---------------------------------------------------------------------------
102 
103 /*!
104  * \brief Get the error string describing the BotSense error code.
105  *
106  * The absolute value of the error code is taken prior retrieving the string.
107  * An unknown or out-of-range error code will be mapped to
108  * \ref NM_ECODE_BADEC.
109  *
110  * \param ecode BotSense error code.
111  *
112  * \return Returns the appropriate error code string.
113  */
114 const char *nmStrError(int ecode)
115 {
116  if( ecode < 0 )
117  {
118  ecode = -ecode;
119  }
120 
121  if( ecode >= arraysize(nmEcodeStrTbl) )
122  {
123  ecode = NM_ECODE_BADEC;
124  }
125  return nmEcodeStrTbl[ecode];
126 }
127 
128 /*!
129  * \brief Pretty print buffer to opened file stream.
130  *
131  * \param fp File pointer.
132  * \param sPreface Optional buffer preface string (set to NULL for no preface).
133  * \param buf Buffer to print.
134  * \param uCount Number of bytes to print.
135  * \param uNLFreq Newline frequency (set to 0 for no newlines).
136  * \param uCol Column alignment number.
137  */
138 void nmPrintBuf(FILE *fp,
139  const char *sPreface,
140  byte_t buf[],
141  size_t uCount,
142  size_t uNLFreq,
143  uint_t uCol)
144 {
145  size_t i;
146 
147  if( sPreface && *sPreface )
148  {
149  fprintf(fp, "%s:", sPreface);
150  }
151 
152  for(i=0; i<uCount; ++i)
153  {
154  if( (uNLFreq > 0) && ((i % uNLFreq) == 0) )
155  {
156  if( i != 0 )
157  {
158  fprintf(fp, "\n%*s", uCol, "");
159  }
160  }
161  fprintf(fp, " 0x%02x", buf[i]);
162  }
163 }
164 
165 /*!
166  * \brief Pretty print bits in value.
167  *
168  * \param fp File pointer.
169  * \param sPreface Optional bit preface string (set to NULL for no preface).
170  * \param uVal Bits to print.
171  * \param uMsb Starting most significant bit.
172  * \param uCnt Number of bits.
173  */
174 void nmPrintBits(FILE *fp,
175  const char *sPreface,
176  ulonglong_t uVal,
177  uint_t uMsb,
178  uint_t uCnt)
179 {
180  uint_t i;
181 
182  if( sPreface && *sPreface )
183  {
184  fprintf(fp, "%s:", sPreface);
185  }
186 
187  for(i=0; i<uCnt; ++i)
188  {
189  if( (uMsb % 8) == 7 )
190  {
191  if( i != 0 )
192  {
193  printf(" ");
194  }
195  }
196  (uVal >> uMsb) & 0x01? printf("1"): printf("0");
197  --uMsb;
198  }
199 }
200 
201 /*!
202  * \brief Get the field value byte size.
203  *
204  * \param eFType Field type.
205  *
206  * \return Returns field value byte size. Returns 0 if field type is unknown
207  * or is variable in length.
208  */
210 {
211  int idx;
212 
213  idx = NMHashFType(eFType);
214 
215  if( (idx != NMHashNoIdx) && (idx < arraysize(NMFValLenLookupTbl)) )
216  {
217  return NMFValLenLookupTbl[idx];
218  }
219  else
220  {
221  return (size_t)0;
222  }
223 }
224 
225 /*!
226  * \brief Find the field definition in the message definition,
227  * given the field id.
228  *
229  * \param pMsgDef Pointer to message definition.
230  * \param byFId Field Id.
231  *
232  * \return If the field definition is found, then the pointer to the
233  * field definition is returned. Else NULL is return.
234  */
235 const NMFieldDef_T *nmFindFieldDef(const NMMsgDef_T *pMsgDef, byte_t byFId)
236 {
237  size_t i;
238 
239  for(i=0; i<pMsgDef->m_uCount; ++i)
240  {
241  if( pMsgDef->m_pFields[i].m_eFId == byFId )
242  {
243  return &(pMsgDef->m_pFields[i]);
244  }
245  }
246 
247  return NULL;
248 }
#define NMFVAL_LEN_U64
unsigned 64-bit field value length
Definition: netmsgs.h:179
#define NMFVAL_LEN_S8
signed 8-bit field value length
Definition: netmsgs.h:173
const NMFieldDef_T * nmFindFieldDef(const NMMsgDef_T *pMsgDef, byte_t byFId)
Find the field definition in the message definition, given the field id.
Definition: nmLibUtils.c:235
#define NMFVAL_LEN_S32
signed 32-bit field value length
Definition: netmsgs.h:178
const int NMHashNoIdx
hash no index value
#define NMFVAL_LEN_F64
64-bit floating-point number field val len
Definition: netmsgs.h:182
#define NMFVAL_LEN_CHAR
character field length
Definition: netmsgs.h:171
void nmPrintBuf(FILE *fp, const char *sPreface, byte_t buf[], size_t uCount, size_t uNLFreq, uint_t uCol)
Pretty print buffer to opened file stream.
Definition: nmLibUtils.c:138
size_t m_uCount
number of message fields
Definition: netmsgs.h:408
#define NMFVAL_LEN_S16
signed 16-bit field value length
Definition: netmsgs.h:176
#define NMFVAL_LEN_VECTOR
VType[[] variable field length.
Definition: netmsgs.h:204
void nmPrintBits(FILE *fp, const char *sPreface, ulonglong_t uVal, uint_t uMsb, uint_t uCnt)
Pretty print bits in value.
Definition: nmLibUtils.c:174
static size_t NMFValLenLookupTbl[]
Definition: nmLibUtils.c:90
#define NMFVAL_LEN_F32
32-bit floating-point number field val len
Definition: netmsgs.h:181
#define NM_ECODE_BADEC
internal inconsistency or bug
Definition: netmsgs.h:81
#define NMFVAL_LEN_P32
32-bit pointer field value length
Definition: netmsgs.h:183
NMFType_T
Definition: netmsgs.h:110
#define NMFVAL_LEN_U16
unsigned 16-bit field value length
Definition: netmsgs.h:175
const NMFieldDef_T * m_pFields
pointer to array of msg field definitions
Definition: netmsgs.h:409
#define NMFVAL_LEN_STRUCT
struct T variable field length
Definition: netmsgs.h:203
#define NMFVAL_LEN_U32
unsigned 32-bit field value length
Definition: netmsgs.h:177
#define NMFVAL_LEN_BOOL
boolean field length
Definition: netmsgs.h:174
const char * nmStrError(int ecode)
Get the error string describing the BotSense error code.
Definition: nmLibUtils.c:114
size_t nmGetFieldValSize(NMFType_T eFType)
Get the field value byte size.
Definition: nmLibUtils.c:209
#define NMFVAL_LEN_P64
64-bit pointer field value length
Definition: netmsgs.h:184
static const char * nmEcodeStrTbl[]
NetMsgs Error Code String Table.
Definition: nmLibUtils.c:69
Internal intra-library declarations.
INLINE_IN_H int NMHashFType(NMFType_T eFType)
Field Type to Index hash function.
#define NMFVAL_LEN_STRING
char[] variable field length
Definition: netmsgs.h:201
#define NMFVAL_LEN_U8
unsigned 8-bit field value length
Definition: netmsgs.h:172
Network Messaging declarations.
#define NMFVAL_LEN_S64
signed 64-bit field value length
Definition: netmsgs.h:180
uint_t m_eFId
filed id (message/struct unique)
Definition: netmsgs.h:382