TOC PREV NEXT INDEX


Decoding a Series of Messages Using the C Decode Functions



The above example is fine as a sample for decoding a single message, but what happens in the more typical scenario of having a long-running loop that continuously decodes messages? It will be necessary to put the decoding logic into a loop. A code fragment showing a way to do this is as follows:

    main ()
 
    {
 
        ASN1OCTET msgbuf[1024];
 
        ASN1TAG   msgtag;
 
        int       msglen;
 
        ASN1CTXT  ctxt;
 
        PersonnelRecord employee;
 

 
        /* Step 1: Initialize a context variable for decoding */
 

 
        if (rtInitContext (&ctxt) != ASN_OK) {
 
           /* initialization failed, could be a license problem */
 
           printf ("context initialization failed (check license)\n");
 
           return -1;
 
        }
 

for (;;) {
 
 
           .. logic to read message into msgbuf ..
 

 
           xd_setp (&ctxt, msgbuf, 0, &msgtag, &msglen);
 

 
           /* Step 2: 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)..	*/
 

 
           /* Now switch on initial tag value to determine what type of 
 
              message was received.. */
 

 
           switch (msgtag)
 
           {
 
              case TV_PersonnelRecord:  // compiler generated constant
 
              {
 
                 status = asn1D_PersonnelRecord (&ctxt, 
 
                                                 &employee, 
 
                                                 ASN1EXPL, 0);
 
                 if (status == ASN_OK)
 
                 {
 
                    /* decoding successful, data in employee */
 

 
                    process received data..
 
                 }
 
                 else
 
                    error processing...
 
              }
 
              break;
 

 
              default:
 
                 /* handle unknown message type here */
 

 
           }  /* switch */
 
 
 
           /* Need to reinitialize objects for next iteration */
 

 
           rtMemReset (&ctxt);
 
        }
 
    }
 

The only changes were the addition of the for (;;) loop and the call to rtMemReset that was added at the bottom of the loop. This function resets the memory tracking parameters within the context to allow previously allocated memory to be reused for the next decode operation. Optionally, rtMemFree can be called to release all memory. This will allow the loop to start again with no outstanding memory allocations for the next pass.

The example above assumes that logic existed that would read each message to be processed into the same buffer for every message processed inside the loop (i.e the buffer is reused each time). In the case in which the buffer already contains multiple messages, encoded back-to-back, it is necessary to advance the buffer pointer in each iteration:

    main ()
 
    {
 
        ASN1OCTET msgbuf[1024];
 
        ASN1TAG   msgtag;
 
        int       offset = 0, msglen, len;
 
        ASN1CTXT  ctxt;
 
        PersonnelRecord employee;
 
	  FILE* fp;
 

 
        /* Step 1: Initialize a context variable for decoding */
 

 
        if (rtInitContext (&ctxt) != ASN_OK) {
 
           /* initialization failed, could be a license problem */
 
           printf ("context initialization failed (check license)\n");
 
           return -1;
 
        }
 

if (fp = fopen (filename, "rb")) {
msglen = fread (msgbuf, 1, sizeof(msgbuf), fp);
}
else {
... handle error ...
}

for (;offset < msglen; ) {
xd_setp (&ctxt, msgbuf + offset, msglen - offset, &msgtag, &len);

/* Decode */

if (tag == TV_PersonnelRecord) {

/* Call compiler generated decode function */

stat = asn1D_PersonnelRecord (&ctxt, &employee, ASN1EXPL, 0);
if (stat == ASN_OK) {

/* decoding successful, data in employee */

}
else {

/* error handling */
return -1;
}
}
else {
printf ("unexpected tag %hx received\n", tag);
}
offset += ctxt.buffer.byteIndex;
rtMemReset (&ctxt);
}
    }
 


Objective Systems, Inc.

102 Pickering Way, Suite #506
Exton, 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
TOC PREV NEXT INDEX