This section describes the step-by-step procedure for calling a C MDER encode function.
Before any encode function can be called; the user must first
initialize an encoding context. This is a variable of type OSCTXT.
This variable holds all of the working data used during the encoding
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 rtInitContext
function as
follows:
OSCTXT ctxt; if (rtInitContext (&ctxt) != 0) { /* initialization failed, could be a license problem */ printf (“context initialization failed (check license)\n”); return -1; }
After initializing the context and populating a variable of the structure to be encoded, an encode function can be called to encode the message.
A program fragment that could be used to encode a simple release request PDU is as follows:
#include "IEEE-11073-20601-ASN1.h" /* include file generated by ASN1C */ int main (void) { ApduType *pdata; /* typedef generated by ASN1C */ OSCTXT ctxt; OSOCTET* msgptr; int stat; /* Step 1: Initialize the context and set the buffer pointer */ stat = rtInitContext (&ctxt); if (stat != 0) { /* initialization failed, could be a license problem */ rtxErrPrint (&ctxt); return stat; } /* Step 2: Populate the structure to be encoded */ pdata = rtxMemAllocType (&ctxt, ApduType); asn1Init_ApduType (pdata); pdata->t = T_ApduType_rlrq; pdata->u.rlrq = rtxMemAllocTypeZ (&ctxt, RlrqApdu); pdata->u.rlrq->reason = normal; /* Step 3: Call the generated encode function */ stat = MDEREnc_ApduType (&ctxt, pdata); /* Step 4: Check the return status. */ if (stat < 0) { rtxErrPrint (&ctxt); return stat; } stat = rtFreeContext (&ctxt); if (stat != 0) { printf ("Error freeing context!\n"); return stat; } return 0; }