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

Example NetMsgs message packing/unpacking application. More...

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/new.h"
#include "rnr/opts.h"
#include "rnr/log.h"
#include "rnr/netmsgs.h"
#include "nmExample.h"
#include "AstroMsgs.h"
#include "version.h"

Go to the source code of this file.

Macros

#define APP_EC_OK   0
 success exit code
 
#define APP_EC_USAGE   2
 usage error exit code
 
#define APP_EC_EXEC   4
 execution error exit code
 
#define MSG_NS   "Astro"
 message namespace
 
#define CAT(a, b)   a ## b
 Concatenate two tokens. More...
 
#define CAT3(a, b, c)   a ## b ## c
 Concatenate three tokens. More...
 
#define PRCMP(mem, fmt)
 Print side-by-side comparision of prepacked tx and post-unpacked rx member. More...
 
#define IDENTITY(xmlname, txmsg, rxmsg)
 Execute the pack, unpack identiy function. More...
 

Typedefs

typedef int(* Packer_T) (void *, byte_t[], size_t, bool_t)
 packer type
 
typedef int(* Unpacker_T) (byte_t[], size_t, void *, bool_t)
 unpacker type
 
typedef int(* tstfunc_t) ()
 

Functions

static int MainInit (int argc, char *argv[])
 Initialize simple_khr2 command. More...
 
int Identity (const char *sXmlName, Packer_T fnPacker, void *pTxMsg, Unpacker_T fnUnpacker, void *pRxMsg)
 Execute the pack, unpack identiy function. More...
 
int tstRspErr ()
 Test AstroRspMsg message packing and unpacking. More...
 
int tstCmdZodiac ()
 Test CmdZodiac message packing and unpacking. More...
 
int tstCmdUniverse ()
 Test CmdUniverse message packing and unpacking. More...
 
int main (int argc, char *argv[])
 Simple NetMsg example main. More...
 

Variables

static char * Argv0
 this command basename
 
static bool_t OptsTrace = false
 do [not] trace packing/unpacking
 
static OptsPgmInfo_T PgmInfo
 Program information. More...
 
static OptsInfo_T OptsInfo []
 Command-line options information. More...
 
tstfunc_t TstTbl []
 

Detailed Description

Example NetMsgs message packing/unpacking application.

LastChangedDate
2010-05-03 13:49:15 -0600 (Mon, 03 May 2010)
Rev
365
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 nmExample.c.

Macro Definition Documentation

#define CAT (   a,
 
)    a ## b

Concatenate two tokens.

Parameters
aToken a.
bToken b.

Definition at line 169 of file nmExample.c.

#define CAT3 (   a,
  b,
 
)    a ## b ## c

Concatenate three tokens.

Parameters
aToken a.
bToken b.
cToken c.

Definition at line 177 of file nmExample.c.

#define IDENTITY (   xmlname,
  txmsg,
  rxmsg 
)
Value:
Identity(#xmlname, (Packer_T)CAT3(Astro, Pack, xmlname), &txmsg, \
(Unpacker_T)CAT3(Astro, Unpack, xmlname), &rxmsg)
int(* Unpacker_T)(byte_t[], size_t, void *, bool_t)
unpacker type
Definition: nmExample.c:160
int Identity(const char *sXmlName, Packer_T fnPacker, void *pTxMsg, Unpacker_T fnUnpacker, void *pRxMsg)
Execute the pack, unpack identiy function.
Definition: nmExample.c:227
#define CAT3(a, b, c)
Concatenate three tokens.
Definition: nmExample.c:177
int(* Packer_T)(void *, byte_t[], size_t, bool_t)
packer type
Definition: nmExample.c:159

Execute the pack, unpack identiy function.

Parameters
xmlnameXML name token
txmsgPre-packed, populated message structure.
rxmsgPost-unacked, populated message structure.

Definition at line 209 of file nmExample.c.

Referenced by tstCmdUniverse(), tstCmdZodiac(), and tstRspErr().

#define PRCMP (   mem,
  fmt 
)
Value:
do \
{ \
int k; \
k = fprintf(stderr, "%s: ", #mem); \
if( k > 38 ) \
{ \
fprintf(stderr, "\n"); \
k = 0; \
} \
fprintf(stderr, "%*stx " fmt "\n", 40-k, "", txmsg.mem); \
fprintf(stderr, "%*srx " fmt "\n\n", 40, "", rxmsg.mem); \
} while(0)

Print side-by-side comparision of prepacked tx and post-unpacked rx member.

Note
Implicit use of txmsg and rxmst external variables.
Parameters
memStructure member of txmsg and rxmsg.
fmtMember print format.

Definition at line 188 of file nmExample.c.

Referenced by tstCmdUniverse(), tstCmdZodiac(), and tstRspErr().

Typedef Documentation

typedef int(* tstfunc_t) ()

Test Function Type

Definition at line 248 of file nmExample.c.

Function Documentation

int Identity ( const char *  sXmlName,
Packer_T  fnPacker,
void *  pTxMsg,
Unpacker_T  fnUnpacker,
void *  pRxMsg 
)

Execute the pack, unpack identiy function.

Parameters
sXmlNameXML name string
fnPackerPacking function.
pTxMsgPointer to pre-packed, populated message structure.
fnUnpackerUnpacking function.
pRxMsgPointer to post-unacked, message structure.
Returns
Number of bytes unpacked (== packed) on success.
< 0 on error.

Definition at line 227 of file nmExample.c.

References MSG_NS, and OptsTrace.

229 {
230  byte_t buf[2048];
231  int n;
232 
233  if( (n = fnPacker(pTxMsg, buf, sizeof(buf), OptsTrace)) < 0 )
234  {
235  LOGERROR("%s(ecode=%d): %sPack%s()", nmStrError(n), n, MSG_NS, sXmlName);
236  }
237 
238 
239  else if( (n = fnUnpacker(buf, (size_t)n, pRxMsg, OptsTrace)) < 0 )
240  {
241  LOGERROR("%s(ecode=%d): %sUnpack%s()", nmStrError(n), n, MSG_NS, sXmlName);
242  }
243 
244  return n;
245 }
#define MSG_NS
message namespace
Definition: nmExample.c:162
static bool_t OptsTrace
do [not] trace packing/unpacking
Definition: nmExample.c:82
int main ( int  argc,
char *  argv[] 
)

Simple NetMsg example main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exit Status:
Program exits with 0 success, > 0 on failure.

Definition at line 464 of file nmExample.c.

References APP_EC_EXEC, APP_EC_OK, MainInit(), NM_OK, and TstTbl.

465 {
466  tstfunc_t fnTst;
467  int i;
468  int n = NM_OK;
469 
470  // Initialize command.
471  MainInit(argc, argv);
472 
473  for(i=0; (fnTst=TstTbl[i])!=NULL; ++i)
474  {
475  if( (n = fnTst()) < 0 )
476  {
477  break;
478  }
479  }
480 
481  return n>=0? APP_EC_OK: APP_EC_EXEC;
482 }
#define APP_EC_OK
success exit code
Definition: nmExample.c:74
#define APP_EC_EXEC
execution error exit code
Definition: nmExample.c:76
#define NM_OK
not an error, success
Definition: netmsgs.h:70
static int MainInit(int argc, char *argv[])
Initialize simple_khr2 command.
Definition: nmExample.c:141
int(* tstfunc_t)()
Definition: nmExample.c:248
tstfunc_t TstTbl[]
Definition: nmExample.c:447
static int MainInit ( int  argc,
char *  argv[] 
)
static

Initialize simple_khr2 command.

Any command-line error immediately terminates application.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Returns
Returns APP_EC_OK on success, >0 exit code on failure.

Definition at line 141 of file nmExample.c.

References APP_EC_OK, Argv0, OptsInfo, PgmInfo, and PkgInfo.

Referenced by main().

142 {
143  // Name of this process.
144  Argv0 = basename(argv[0]);
145 
146  //
147  // Parse input options.
148  //
149  argv = OptsGet(Argv0, &PkgInfo, &PgmInfo, OptsInfo, true, &argc, argv);
150 
151  return APP_EC_OK;
152 }
static OptsInfo_T OptsInfo[]
Command-line options information.
Definition: nmExample.c:102
static char * Argv0
this command basename
Definition: nmExample.c:81
#define APP_EC_OK
success exit code
Definition: nmExample.c:74
static const PkgInfo_T PkgInfo
Definition: version.h:45
static OptsPgmInfo_T PgmInfo
Program information.
Definition: nmExample.c:88
int tstCmdUniverse ( )

Test CmdUniverse message packing and unpacking.

Returns
Number of bytes unpacked (== packed) on success.
< 0 on error.

Definition at line 363 of file nmExample.c.

References IDENTITY, and PRCMP.

364 {
365  static char *p1 = "p-branes";
366  static char *p2 = "bad captain kirk";
367 
368  AstroCmdUniverse_T txmsg =
369  {
370  .m_Truth = true,
371  .m_Aura = 'z',
372  .m_Gluon = 3,
373  .m_Electron = -3,
374  .m_Planet = 16433,
375  .m_StellarObj = -31000,
376  .m_StarSystem = 999999,
377  .m_Galaxy = -11,
378  .m_GalaxyGroup = 0xffffffff,
379  .m_Filament = 85,
380  .m_VisUnivLY = (float)-53.42,
381  .m_FullUnivLY = 53e12,
382  .m_Math.m_One = 1,
383  .m_Math.m_Two = 2,
384  .m_Math.m_DotDotDot = 101,
385  .m_StarType = 'O',
386  .m_StarInfo.m_Type = 'M',
387  .m_StarInfo.m_TempK = 10000,
388  .m_StarInfo.m_MassSun = (float)2.6,
389  };
390  AstroCmdUniverse_T rxmsg;
391  int n;
392 
393  txmsg.m_HiddenDim = p1;
394  txmsg.m_ParallelUniv = p2;
395  strcpy(txmsg.m_TheUnamed, "God");
396  txmsg.m_Physics.m_count = 3;
397  txmsg.m_Physics.u.m_buf[0] = 31415;
398  txmsg.m_Physics.u.m_buf[1] = 214;
399  txmsg.m_Physics.u.m_buf[2] = 317;
400  txmsg.m_Physics.u.m_buf[3] = 555555;
401  strcpy(txmsg.m_StarInfo.m_Name, "Rigel");
402  strcpy(txmsg.m_StarInfo.m_Color, "Blue");
403 
404  n = IDENTITY(CmdUniverse, txmsg, rxmsg);
405 
406  if( n >= 0 )
407  {
408  fprintf(stderr, "\n--- AstroCmdUniverse_T Results\n");
409  PRCMP(m_Truth, "%hhu");
410  PRCMP(m_Aura, "'%c'");
411  PRCMP(m_Gluon, "%hhu");
412  PRCMP(m_Electron, "%hhd");
413  PRCMP(m_Planet, "%hu");
414  PRCMP(m_StellarObj, "%hd");
415  PRCMP(m_StarSystem, "%u");
416  PRCMP(m_Galaxy, "%d");
417  PRCMP(m_GalaxyGroup, "%llu");
418  PRCMP(m_Filament, "%lld");
419  PRCMP(m_VisUnivLY, "%f");
420  PRCMP(m_FullUnivLY, "%e");
421  fprintf(stderr, "%*s%p->\"%s\"\n", 43, "", p1, p1);
422  PRCMP(m_HiddenDim, "%p");
423  fprintf(stderr, "%*s%p->\"%s\"\n", 43, "", p2, p2);
424  PRCMP(m_ParallelUniv, "%p");
425  PRCMP(m_TheUnamed, "%s");
426  PRCMP(m_Math.m_One, "%hu");
427  PRCMP(m_Math.m_Two, "%u");
428  PRCMP(m_Math.m_DotDotDot, "%llu");
429  PRCMP(m_Physics.m_count, "%zu");
430  PRCMP(m_Physics.u.m_buf[0], "%u");
431  PRCMP(m_Physics.u.m_buf[1], "%u");
432  PRCMP(m_Physics.u.m_buf[2], "%u");
433  PRCMP(m_StarType, "'%c'");
434  PRCMP(m_StarInfo.m_Type, "'%c'");
435  PRCMP(m_StarInfo.m_TempK, "%u");
436  PRCMP(m_StarInfo.m_MassSun, "%f");
437  PRCMP(m_StarInfo.m_Name, "%s");
438  PRCMP(m_StarInfo.m_Color, "%s");
439  }
440 
441  return n;
442 }
#define PRCMP(mem, fmt)
Print side-by-side comparision of prepacked tx and post-unpacked rx member.
Definition: nmExample.c:188
#define IDENTITY(xmlname, txmsg, rxmsg)
Execute the pack, unpack identiy function.
Definition: nmExample.c:209
int tstCmdZodiac ( )

Test CmdZodiac message packing and unpacking.

Returns
Number of bytes unpacked (== packed) on success.
< 0 on error.

Definition at line 284 of file nmExample.c.

References ALPHA, BETA, DELTA, EPSILON, GAMMA, IDENTITY, and PRCMP.

285 {
286  int n = 0;
287  size_t i;
288 
289  AstroCmdZodiac_T txmsg, rxmsg;
290 
291  strcpy(txmsg.m_Family, "test const");
292 
293  strcpy(txmsg.m_Constellation.m_Name, "Orion");
294 
295  txmsg.m_Constellation.m_Designation.u.m_buf[ALPHA] = ALPHA;
296  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[ALPHA].m_Name, "Betelgeuse");
297  txmsg.m_Constellation.m_Star.u.m_buf[ALPHA].m_Type = 'M';
298  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[ALPHA].m_Color, "Red");
299  txmsg.m_Constellation.m_Star.u.m_buf[ALPHA].m_TempK = 3500;
300  txmsg.m_Constellation.m_Star.u.m_buf[ALPHA].m_MassSun = 19.0;
301 
302  txmsg.m_Constellation.m_Designation.u.m_buf[BETA] = BETA;
303  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[BETA].m_Name, "Rigel");
304  txmsg.m_Constellation.m_Star.u.m_buf[BETA].m_Type = 'B';
305  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[BETA].m_Color, "Blue");
306  txmsg.m_Constellation.m_Star.u.m_buf[BETA].m_TempK = 11000;
307  txmsg.m_Constellation.m_Star.u.m_buf[BETA].m_MassSun = 17.0;
308 
309  txmsg.m_Constellation.m_Designation.u.m_buf[GAMMA] = GAMMA;
310  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[GAMMA].m_Name, "Bellatrix");
311  txmsg.m_Constellation.m_Star.u.m_buf[GAMMA].m_Type = 'B';
312  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[GAMMA].m_Color, "Blue");
313  txmsg.m_Constellation.m_Star.u.m_buf[GAMMA].m_TempK = 21500;
314  txmsg.m_Constellation.m_Star.u.m_buf[GAMMA].m_MassSun = 8.5;
315 
316  txmsg.m_Constellation.m_Designation.u.m_buf[DELTA] = DELTA;
317  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[DELTA].m_Name, "Mintaka");
318  txmsg.m_Constellation.m_Star.u.m_buf[DELTA].m_Type = 'B';
319  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[DELTA].m_Color, "Blue");
320  txmsg.m_Constellation.m_Star.u.m_buf[DELTA].m_TempK = 30000;
321  txmsg.m_Constellation.m_Star.u.m_buf[DELTA].m_MassSun = 20.0;
322 
323  txmsg.m_Constellation.m_Designation.u.m_buf[EPSILON] = EPSILON;
324  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[EPSILON].m_Name, "Alnilam");
325  txmsg.m_Constellation.m_Star.u.m_buf[EPSILON].m_Type = 'B';
326  strcpy(txmsg.m_Constellation.m_Star.u.m_buf[EPSILON].m_Color, "Blue");
327  txmsg.m_Constellation.m_Star.u.m_buf[EPSILON].m_TempK = 25000;
328  txmsg.m_Constellation.m_Star.u.m_buf[EPSILON].m_MassSun = 40.0;
329 
330  txmsg.m_Constellation.m_Designation.m_count = EPSILON+1;
331  txmsg.m_Constellation.m_Star.m_count = EPSILON+1;
332 
333  n = IDENTITY(CmdZodiac, txmsg, rxmsg);
334 
335  if( n >= 0 )
336  {
337  fprintf(stderr, "\n--- AstroCmdZodiac_T Results\n");
338  PRCMP(m_Family, "%s");
339  PRCMP(m_Constellation.m_Name, "%s");
340  PRCMP(m_Constellation.m_Designation.m_count, "%zu");
341  PRCMP(m_Constellation.m_Star.m_count, "%zu");
342  for(i=0; i<txmsg.m_Constellation.m_Star.m_count; ++i)
343  {
344  PRCMP(m_Constellation.m_Designation.u.m_buf[i], "%hhu");
345  PRCMP(m_Constellation.m_Star.u.m_buf[i].m_Name, "%s");
346  PRCMP(m_Constellation.m_Star.u.m_buf[i].m_Type, "'%c'");
347  PRCMP(m_Constellation.m_Star.u.m_buf[i].m_Color, "%s");
348  PRCMP(m_Constellation.m_Star.u.m_buf[i].m_TempK, "%u");
349  PRCMP(m_Constellation.m_Star.u.m_buf[i].m_MassSun, "%f");
350  }
351  }
352 
353  return n;
354 }
#define BETA
2nd star
Definition: nmExample.h:63
#define ALPHA
1st star
Definition: nmExample.h:62
#define GAMMA
3rd star
Definition: nmExample.h:64
#define PRCMP(mem, fmt)
Print side-by-side comparision of prepacked tx and post-unpacked rx member.
Definition: nmExample.c:188
#define DELTA
4th star
Definition: nmExample.h:65
#define IDENTITY(xmlname, txmsg, rxmsg)
Execute the pack, unpack identiy function.
Definition: nmExample.c:209
#define EPSILON
5th star
Definition: nmExample.h:66
int tstRspErr ( )

Test AstroRspMsg message packing and unpacking.

Returns
Number of bytes unpacked (== packed) on success.
< 0 on error.

Definition at line 257 of file nmExample.c.

References IDENTITY, and PRCMP.

258 {
259  AstroRspErr_T txmsg, rxmsg;
260  int n;
261 
262  txmsg.m_ECode = 55;
263  strcpy(txmsg.m_EMsg, "this is an error message");
264 
265  n = IDENTITY(RspErr, txmsg, rxmsg);
266 
267  if( n >= 0 )
268  {
269  fprintf(stderr, "\n--- AstroRspErr_T Results\n");
270  PRCMP(m_ECode, "%u");
271  PRCMP(m_EMsg, "'%s'");
272  }
273 
274  return n;
275 }
#define PRCMP(mem, fmt)
Print side-by-side comparision of prepacked tx and post-unpacked rx member.
Definition: nmExample.c:188
#define IDENTITY(xmlname, txmsg, rxmsg)
Execute the pack, unpack identiy function.
Definition: nmExample.c:209

Variable Documentation

OptsInfo_T OptsInfo[]
static
Initial value:
=
{
{
.long_opt = "trace",
.short_opt = 't',
.has_arg = no_argument,
.has_default= true,
.opt_addr = &OptsTrace,
.fn_cvt = OptsCvtArgBool,
.fn_fmt = OptsFmtBool,
.arg_name = NULL,
.opt_desc = "Do [not] enable tracing of packing and unpacking "
"operations. If enabled, tracing is printed to stderr."
},
{NULL, }
}
static bool_t OptsTrace
do [not] trace packing/unpacking
Definition: nmExample.c:82

Command-line options information.

Definition at line 102 of file nmExample.c.

Referenced by MainInit().

OptsPgmInfo_T PgmInfo
static
Initial value:
=
{
.synopsis =
"Simple example packing and unpacking NetMsgs messages.",
.long_desc =
"The %P application uses the python NetMsgs package generated C code "
"from the AstroMsgs.xml NetMsgs XML specification and the libnetmsgs "
"library to pack and unpack the messages."
}

Program information.

Definition at line 88 of file nmExample.c.

Referenced by MainInit().

tstfunc_t TstTbl[]
Initial value:
=
{
NULL
}
int tstCmdZodiac()
Test CmdZodiac message packing and unpacking.
Definition: nmExample.c:284
int tstCmdUniverse()
Test CmdUniverse message packing and unpacking.
Definition: nmExample.c:363
int tstRspErr()
Test AstroRspMsg message packing and unpacking.
Definition: nmExample.c:257

Table of Test Functions.

Definition at line 447 of file nmExample.c.

Referenced by main().