netmsgs  1.2.2
RoadNarrows Robotics Network Messaging Package
nmLibInternal.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: netmsgs
4 //
5 // Library: libnetmsgs
6 //
7 // File: nmLibInternal.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2011-11-18 13:32:30 -0700 (Fri, 18 Nov 2011) $
12  * $Rev: 1578 $
13  *
14  * \brief Internal intra-library declarations.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2009-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 #ifndef _NMLIBINTERNAL_H
50 #define _NMLIBINTERNAL_H
51 
52 #include <stdio.h>
53 #include <ctype.h>
54 
55 #include "rnr/rnrconfig.h"
56 #include "rnr/log.h"
57 
58 #include "rnr/netmsgs.h"
59 
60 /*!
61  * \brief Print debugging info. Undef in release version. See Makefile.
62  *
63  * \param fmt Output format string.
64  * \param ... Variable arguments.
65  */
66 #ifdef NMLIB_DEBUG_ENABLE // debugging compile in/out switch
67 #define NMLIB_DEBUG(fmt, ...) \
68  fprintf(stderr, "DEBUG: %s[%u]: %s(): " fmt, \
69  __FILE__, __LINE__, LOGFUNCNAME, ##__VA_ARGS__)
70 #else
71 #define NMLIB_DEBUG(fmt, ...)
72 #endif // NMLIB_DEBUG_ENABLE
73 
74 /*!
75  * \brief Log libnetmsgs warning.
76  *
77  * \param fmt Output format string.
78  * \param ... Variable arguments.
79  */
80 #define NMLIB_WARNING(fmt, ...) \
81  LOGDIAG1("Warning: " fmt, ##__VA_ARGS__)
82 
83 /*!
84  * \brief Log libnetmsgs field warning.
85  *
86  * \param p Pointer to field definition (may be NULL).
87  * \param fmt Output format string.
88  * \param ... Variable arguments.
89  */
90 #define NMLIB_FIELD_WARNING(p, fmt, ...) \
91  ((p)!=NULL?: \
92  NMLIB_WARNING("%s(%u): " fmt, (p)->m_sFName, (p)->m_eFId, ##__VA_ARGS__): \
93  NMLIB_WARNING(fmt, ##__VA_ARGS__))
94 
95 /*!
96  * \brief Log libnetmsgs error.
97  *
98  * \param ecode NetMsgs error code.
99  * \param fmt Output format string.
100  * \param ... Variable arguments.
101  */
102 #define NMLIB_ERROR(ecode, fmt, ...) \
103  LOGERROR("%s(ecode=%d): " fmt, \
104  nmStrError(ecode), (ecode>=0? ecode: -ecode), ##__VA_ARGS__)
105 
106 /*!
107  * \brief Raise libnetmsgs error (i.e. return from calling function).
108  *
109  * \param ecode NetMsgs error code.
110  * \param fmt Output format string.
111  * \param ... Variable arguments.
112  */
113 #define NMLIB_RAISE_ERROR(ecode, fmt, ...) \
114  do \
115  { \
116  NMLIB_ERROR(ecode, fmt, ##__VA_ARGS__); \
117  return ecode>0? -ecode: ecode; \
118  } while(0)
119 
120 /*!
121  * \brief Log libnetmsgs field error.
122  *
123  * Error is logged prior to return.
124  *
125  * \param ecode NetMsgs error code.
126  * \param p Pointer to field definition (may be NULL).
127  * \param fmt Output format string.
128  * \param ... Variable arguments.
129  */
130 #define NMLIB_FIELD_ERROR(ecode, p, fmt, ...) \
131  do \
132  { \
133  if( (p) !=NULL ) \
134  { \
135  NMLIB_ERROR(ecode, "%s(%u): " fmt, \
136  (p)->m_sFName, (p)->m_eFId, ##__VA_ARGS__); \
137  } \
138  else \
139  { \
140  NMLIB_ERROR(ecode, fmt, ##__VA_ARGS__); \
141  } \
142  } while(0)
143 
144 /*!
145  * \brief Raise libnetmsgs field error (i.e. return from calling function).
146  *
147  * Error is logged prior to return.
148  *
149  * \param ecode NetMsgs error code.
150  * \param p Pointer to field definition (may be NULL).
151  * \param fmt Output format string.
152  * \param ... Variable arguments.
153  */
154 #define NMLIB_RAISE_FIELD_ERROR(ecode, p, fmt, ...) \
155  do \
156  { \
157  NMLIB_FIELD_ERROR(ecode, p, fmt, ##__VA_ARGS__); \
158  return ecode>0? -ecode: ecode; \
159  } while(0)
160 
161 /*!
162  * \brief Printable ASCII Field Type.
163  *
164  * \param ftype Field Id
165  */
166 #define NMLIB_ASCII_FTYPE(ftype) (isgraph((int)ftype)? ftype: ' ')
167 
168 
169 /*!
170  * \brief Internal Control Structure
171  */
172 typedef struct
173 {
174  byte_t m_bNoHdr; ///< do [not] include field header in byte stream
175  byte_t m_bNoExec; ///< do [not] execute assignment of value
176  byte_t m_bTrace; ///< do [not] trace packing/unpacking
177  byte_t m_uDepth; ///< structured message depth
178 } NMCtl_T;
179 
180 /*!
181  * \brief Default Internal control declartion list.
182  */
183 #define NMCTL_INIT_DECL {false, false, false, 0}
184 
185 /*!
186  * \brief Trace field.
187  *
188  * \param p Pointer to field definition (may be NULL).
189  * \param buf Input/output buffer.
190  * \param n Number of bytes in buffer to trace.
191  * \param ctl Internal Control.
192  * \param fmt Field representation format string.
193  * \param ... Field representation variable arguments.
194  */
195 #ifdef NMLIB_DEBUG_ENABLE
196 #define NMLIB_TRACE_FIELD(p, buf, n, ctl, fmt, ...) \
197  do \
198  { \
199  if( ((n) >= 0) && ((ctl)->m_bTrace) ) \
200  { \
201  nmTraceField(p, (byte_t *)buf, (size_t)n, \
202  (uint_t)(ctl)->m_uDepth, \
203  fmt, ##__VA_ARGS__); \
204  } \
205  } while(0)
206 #else
207 #define NMLIB_TRACE_FIELD(p, buf, n, ctl, fmt, ...)
208 #endif // NMLIB_DEBUG_ENABLE
209 
210 //
211 // Hash Table Variables
212 //
213 extern const int NMHashOffset;
214 extern const int NMHashNoIdx;
215 extern const byte_t NMHashTbl[];
216 extern const size_t NMHashNumEntries;
217 
218 /*!
219  * \brief Field Type to Index hash function.
220  *
221  * \param eFType Field type.
222  *
223  * \return Returns index on success, NMHasNoIdx on failure.
224  */
225 INLINE_IN_H int NMHashFType(NMFType_T eFType)
226 {
227  int i = (int)((int)eFType - (int)NMHashOffset);
228 
229 #ifdef LOG
230  if( LOGABLE(LOG_LEVEL_DIAG4) )
231  {
232  int idx = ((i >= 0) && (i < (int)NMHashNumEntries))? (int)NMHashTbl[i]:
233  NMHashNoIdx;
234  LOGDIAG4("hash %c --> %d --> %d", NMLIB_ASCII_FTYPE(eFType), i, idx);
235  }
236 #endif // LOG
237 
238  return ((i >= 0) && (i < (int)NMHashNumEntries))? (int)NMHashTbl[i]:
239  NMHashNoIdx;
240 }
241 
242 /*! Message Field Packer Function Type */
243 typedef int (*NMPackFunc_T)(const NMFieldDef_T *,
244  void *,
245  byte_t [],
246  size_t,
247  NMEndian_T,
248  NMCtl_T *);
249 
250 /*! Message Field Unpacker Function Type */
251 typedef int (*NMUnpackFunc_T)(const NMFieldDef_T *,
252  byte_t [],
253  size_t,
254  void *,
255  NMEndian_T,
256  NMCtl_T *);
257 
258 /*!
259  * Message Field Packer/Unpacker Lookup Table Entry Type
260  */
261 typedef struct
262 {
263  uint_t m_eFType; ///< message field type
264  NMPackFunc_T m_fnPack; ///< packer
265  NMUnpackFunc_T m_fnUnpack; ///< unpacker
267 
268 
269 // ---------------------------------------------------------------------------
270 // Prototypes
271 // ---------------------------------------------------------------------------
272 
273 extern int nmSetU8(const NMFieldDef_T *pFieldDef,
274  void *pValIn,
275  byte_t *pValOut);
276 
277 extern int nmSetS8(const NMFieldDef_T *pFieldDef,
278  void *pValIn,
279  signed char *pValOut);
280 
281 extern int nmSetU16(const NMFieldDef_T *pFieldDef,
282  void *pValIn,
283  ushort_t *pValOut);
284 
285 extern int nmSetS16(const NMFieldDef_T *pFieldDef,
286  void *pValIn,
287  short *pValOut);
288 
289 extern int nmSetU32(const NMFieldDef_T *pFieldDef,
290  void *pValIn,
291  uint_t *pValOut);
292 
293 extern int nmSetS32(const NMFieldDef_T *pFieldDef,
294  void *pValIn,
295  int *pValOut);
296 
297 extern int nmSetU64(const NMFieldDef_T *pFieldDef,
298  void *pValIn,
299  ulonglong_t *pValOut);
300 
301 extern int nmSetS64(const NMFieldDef_T *pFieldDef,
302  void *pValIn,
303  long long *pValOut);
304 
305 extern int nmSetF32(const NMFieldDef_T *pFieldDef,
306  void *pValIn,
307  float *pValOut);
308 
309 extern int nmSetF64(const NMFieldDef_T *pFieldDef,
310  void *pValIn,
311  double *pValOut);
312 
313 #ifdef NMLIB_DEBUG_ENABLE
314 extern void nmTraceField(const NMFieldDef_T *pFieldDef,
315  byte_t buf[],
316  size_t uCount,
317  uint_t uDepth,
318  const char *sFmt,
319  ...);
320 #endif // NMLIB_DEBUG_ENABLE
321 
322 
323 #endif // _NMLIBINTERNAL_H
byte_t m_bNoHdr
do [not] include field header in byte stream
const size_t NMHashNumEntries
byte_t m_uDepth
structured message depth
int nmSetF64(const NMFieldDef_T *pFieldDef, void *pValIn, double *pValOut)
Helper function to set a 64-bit floating point number.
byte_t m_bTrace
do [not] trace packing/unpacking
int nmSetS8(const NMFieldDef_T *pFieldDef, void *pValIn, signed char *pValOut)
Helper function to set a signed 8-bit integer.
int nmSetU32(const NMFieldDef_T *pFieldDef, void *pValIn, uint_t *pValOut)
Helper function to set an unsigned 32-bit integer.
int(* NMUnpackFunc_T)(const NMFieldDef_T *, byte_t[], size_t, void *, NMEndian_T, NMCtl_T *)
int nmSetS16(const NMFieldDef_T *pFieldDef, void *pValIn, short *pValOut)
Helper function to set a signed 16-bit integer.
byte_t m_bNoExec
do [not] execute assignment of value
const int NMHashNoIdx
hash no index value
NMFType_T
Definition: netmsgs.h:110
int nmSetU8(const NMFieldDef_T *pFieldDef, void *pValIn, byte_t *pValOut)
Helper function to set an unsigned 8-bit integer.
NMEndian_T
Definition: netmsgs.h:100
int(* NMPackFunc_T)(const NMFieldDef_T *, void *, byte_t[], size_t, NMEndian_T, NMCtl_T *)
int nmSetS32(const NMFieldDef_T *pFieldDef, void *pValIn, int *pValOut)
Helper function to set a signed 32-bit integer.
void nmTraceField(const NMFieldDef_T *pFieldDef, byte_t buf[], size_t uCount, uint_t uDepth, const char *sFmt,...)
Trace packing/unpacking of a message field.
int nmSetU64(const NMFieldDef_T *pFieldDef, void *pValIn, ulonglong_t *pValOut)
Helper function to set an unsigned 64-bit integer.
const int NMHashOffset
hash base offset
int nmSetF32(const NMFieldDef_T *pFieldDef, void *pValIn, float *pValOut)
Helper function to set a 32-bit floating point number.
const byte_t NMHashTbl[]
uint_t m_eFType
message field type
int nmSetU16(const NMFieldDef_T *pFieldDef, void *pValIn, ushort_t *pValOut)
Helper function to set an unsigned 16-bit integer.
INLINE_IN_H int NMHashFType(NMFType_T eFType)
Field Type to Index hash function.
#define NMLIB_ASCII_FTYPE(ftype)
Printable ASCII Field Type.
Network Messaging declarations.
int nmSetS64(const NMFieldDef_T *pFieldDef, void *pValIn, long long *pValOut)
Helper function to set a signed 64-bit integer.
Internal Control Structure.
NMPackFunc_T m_fnPack
packer
NMUnpackFunc_T m_fnUnpack
unpacker