
Procedure for Calling Stream-Oriented C Decode Functions
This section describes the step-by-step procedure for calling a stream-oriented C BER decode function. This procedure must be followed if C code generation was done. This procedure can also be used as an alternative to using the control class interface if C++ code generation was done.
Before any decode function can be called; the user must first initialize a context variable. This is a variable of type OSCTXT. This variable holds all of the working data used during the decoding of a message. The context variable is declared as a normal automatic variable within the top-level calling function. It must be initialized before use. This can be accomplished by using the berStrmInitContext function.
OSCTXT ctxt; // context variable if (berStrmInitContext (&ctxt) != 0) { /* initialization failed, could be a license problem */ printf ("context initialization failed (check license)\n"); return -1; }The next step is to create a stream object within the context. This object is an abstraction of the output device to which the data is to be encoded and is initialized by calling one of the following functions:
- rtxStreamFileOpen
- rtxStreamFileAttach
- rtxStreamSocketAttach
- rtxStreamMemoryCreate
- rtxStreamMemoryAttach
The flags parameter of these functions should be set to the OSRTSTRMF_INPUT constant value to indicate an input stream is being created (see the C/C++ Common Run-Time Library Reference Manual for a full description of these functions).
After initializing the context and populating a variable of the structure to be encoded, a decode function can be called to decode a message from the stream. If the return status indicates success, the C variable that was passed as an argument will contain the decoded message contents. Note that the decoder may have allocated dynamic memory and stored pointers to objects in the C structure. After processing on the C structure is complete, the run-time library function rtxMemFree should be called to free the allocated memory.
#include "employee.h" /* include file generated by ASN1C */ #include "rtxsrc/rtxStreamFile.h" main () { ASN1TAG msgtag; int msglen; OSCTXT ctxt; PersonnelRecord employee; const char* filename = "message.dat" /* Step 1: Initialize a context variable for decoding */ if (berStrmInitContext (&ctxt) != 0) { /* initialization failed, could be a license problem */ printf ("context initialization failed (check license)\n"); return -1; } /* Step 2: Open the input stream to read data */ stat = rtxStreamFileCreateReader (&ctxt, filename); if (stat != 0) { rtxErrPrint (&ctxt); return stat; } /* Step 3: Test message tag for type of message received */ /* (note: this is optional, the decode function can be */ /* called directly if the type of message is known).. */ if ((stat = berDecStrmPeekTagAndLen (&ctxt, &tag, &len)) != 0) { rtxErrPrint (&ctxt); return stat; } if (msgtag == TV_PersonnelRecord) { /* Step 4: Call decode function (note: last two args */ /* should always be ASN1EXPL and 0).. */ status = asn1BSD_PersonnelRecord (&ctxt, &employee, ASN1EXPL, 0); /* Step 5: Check return status */ if (status == 0) { process received data in `employee' variable.. } else error processing... } else check for other known message types.. /* Step 6: Close the stream */ rtxStreamClose (&ctxt); /* Remember to release dynamic memory when done! */ rtFreeContext (&ctxt); }
Objective Systems, Inc.55 Dowlin Forge RoadExton, Pennsylvania 19341 http://www.obj-sys.com Phone: (484) 875-9841 Toll-free: (877) 307-6855 (US only) Fax: (484) 875-9830 info@obj-sys.com |