Procedure for Calling C Decode Functions

This section describes the step-by-step procedure for calling a C JSON decode function. This method must be used if C code generation was done. This method 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 as follows:

   OSCTXT ctxt;
   int stat;

   stat = rtInitContext (&ctxt);
   if (stat != 0) {
      rtxErrPrint (&ctxt);
      rtFreeContext (&ctxt);
      return stat;
   }

The next step is to create a reader that will read from the given source. In our example, we read from a file, but it is also possible to read data from a socket or other source as well. Alternatively, a decode buffer may also be used.

A decode function can then be called to decode the message. 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.

A program fragment that could be used to decode a simple PDU type follows:

   
   /* Init context structure */

   if (rtInitContext (&ctxt) != 0) {
      printf ("Error initializing context\n");
      return -1;
   }
   stat = rtxStreamFileCreateReader (&ctxt, filename);

   if (stat != 0) {
      printf ("Unable to open %s for reading.\n", filename);
      rtxErrPrint(&ctxt);
      rtFreeContext(&ctxt);
      return stat;
   }

   rtxSetDiag (&ctxt, verbose);

   /* Decode */
   asn1Init_PersonnelRecord (&employee);

   stat = asn1JsonDec_PersonnelRecord (&ctxt, &employee);
	    

This example follows the employee sample in the distribution kit.