netmsgs  1.2.2
RoadNarrows Robotics Network Messaging Package
nmLibUtils.c File Reference

NetMsgs library utilities. More...

#include <stdio.h>
#include <stdlib.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/netmsgs.h"
#include "nmLibInternal.h"

Go to the source code of this file.

Functions

const char * nmStrError (int ecode)
 Get the error string describing the BotSense error code. More...
 
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. More...
 
void nmPrintBits (FILE *fp, const char *sPreface, ulonglong_t uVal, uint_t uMsb, uint_t uCnt)
 Pretty print bits in value. More...
 
size_t nmGetFieldValSize (NMFType_T eFType)
 Get the field value byte size. More...
 
const NMFieldDef_TnmFindFieldDef (const NMMsgDef_T *pMsgDef, byte_t byFId)
 Find the field definition in the message definition, given the field id. More...
 

Variables

static const char * nmEcodeStrTbl []
 NetMsgs Error Code String Table. More...
 
static size_t NMFValLenLookupTbl []
 

Detailed Description

NetMsgs library utilities.

LastChangedDate
2010-07-31 08:48:56 -0600 (Sat, 31 Jul 2010)
Rev
521
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file nmLibUtils.c.

Function Documentation

const NMFieldDef_T* nmFindFieldDef ( const NMMsgDef_T pMsgDef,
byte_t  byFId 
)

Find the field definition in the message definition, given the field id.

Parameters
pMsgDefPointer to message definition.
byFIdField Id.
Returns
If the field definition is found, then the pointer to the field definition is returned. Else NULL is return.

Definition at line 235 of file nmLibUtils.c.

References _nm_field_def_struct::m_eFId, _nm_msg_def_struct::m_pFields, and _nm_msg_def_struct::m_uCount.

Referenced by nmUnpackITVStream().

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 }
size_t m_uCount
number of message fields
Definition: netmsgs.h:408
const NMFieldDef_T * m_pFields
pointer to array of msg field definitions
Definition: netmsgs.h:409
uint_t m_eFId
filed id (message/struct unique)
Definition: netmsgs.h:382
size_t nmGetFieldValSize ( NMFType_T  eFType)

Get the field value byte size.

Parameters
eFTypeField type.
Returns
Returns field value byte size. Returns 0 if field type is unknown or is variable in length.

Definition at line 209 of file nmLibUtils.c.

References NMFValLenLookupTbl, NMHashFType(), and NMHashNoIdx.

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 }
const int NMHashNoIdx
hash no index value
static size_t NMFValLenLookupTbl[]
Definition: nmLibUtils.c:90
INLINE_IN_H int NMHashFType(NMFType_T eFType)
Field Type to Index hash function.
void nmPrintBits ( FILE *  fp,
const char *  sPreface,
ulonglong_t  uVal,
uint_t  uMsb,
uint_t  uCnt 
)

Pretty print bits in value.

Parameters
fpFile pointer.
sPrefaceOptional bit preface string (set to NULL for no preface).
uValBits to print.
uMsbStarting most significant bit.
uCntNumber of bits.

Definition at line 174 of file nmLibUtils.c.

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 }
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.

Parameters
fpFile pointer.
sPrefaceOptional buffer preface string (set to NULL for no preface).
bufBuffer to print.
uCountNumber of bytes to print.
uNLFreqNewline frequency (set to 0 for no newlines).
uColColumn alignment number.

Definition at line 138 of file nmLibUtils.c.

Referenced by nmPackFlatMsgDebug(), nmPackITVMsgDebug(), nmTraceField(), nmUnpackFlatMsgDebug(), and nmUnpackITVMsgDebug().

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 }
const char* nmStrError ( int  ecode)

Get the error string describing the BotSense error code.

The absolute value of the error code is taken prior retrieving the string. An unknown or out-of-range error code will be mapped to NM_ECODE_BADEC.

Parameters
ecodeBotSense error code.
Returns
Returns the appropriate error code string.

Definition at line 114 of file nmLibUtils.c.

References NM_ECODE_BADEC, and nmEcodeStrTbl.

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 }
#define NM_ECODE_BADEC
internal inconsistency or bug
Definition: netmsgs.h:81
static const char * nmEcodeStrTbl[]
NetMsgs Error Code String Table.
Definition: nmLibUtils.c:69

Variable Documentation

const char* nmEcodeStrTbl[]
static
Initial value:
=
{
"Ok",
"Error",
"Insufficient memory available",
"Machine architecture not supported",
"Value out-of-range",
"Unknown field type",
"Bad message format",
"Unknown message id",
"Internal error",
"Invalid error code",
NULL, 0
}

NetMsgs Error Code String Table.

Table is indexed by NetMsgs error codes (see NetMsgs Error Codes). Keep in sync.

Definition at line 69 of file nmLibUtils.c.

Referenced by nmStrError().

size_t NMFValLenLookupTbl[]
static
Initial value:
=
{
}
#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
#define NMFVAL_LEN_S32
signed 32-bit field value length
Definition: netmsgs.h:178
#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
#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
#define NMFVAL_LEN_F32
32-bit floating-point number field val len
Definition: netmsgs.h:181
#define NMFVAL_LEN_P32
32-bit pointer field value length
Definition: netmsgs.h:183
#define NMFVAL_LEN_U16
unsigned 16-bit field value length
Definition: netmsgs.h:175
#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
#define NMFVAL_LEN_P64
64-bit pointer field value length
Definition: netmsgs.h:184
#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
#define NMFVAL_LEN_S64
signed 64-bit field value length
Definition: netmsgs.h:180

Field Value Byte Size Lookup Table

Warning
Keep in asending order by ftype.

Definition at line 90 of file nmLibUtils.c.

Referenced by nmGetFieldValSize().