TOC PREV NEXT INDEX


Procedure for Using the C++ Control Class Encode Method



The procedure to encode a message using the C++ class interface is as follows:







A program fragment that could be used to encode an employee record is as follows:

   #include employee.h         // include file generated by ASN1C
 

 
   main ()
 
   {
 
	const OSOCTET* msgptr;
 
	OSOCTET msgbuf[1024];
 
	int     msglen, stat;
 
	OSBOOL  aligned = TRUE;
 

 
	// step 1: instantiate an instance of the PER encode 
 
	// buffer class.  This example specifies a static 
 
	// message buffer..
 

 
 	ASN1PEREncodeBuffer encodeBuffer (msgbuf, 
 
                                        sizeof(msgbuf), 
 
                                        aligned);
 

 
	// step 2: populate msgData with data to be encoded
 

 
	ASN1T_PersonnelRecord msgData;
 
	msgData.name.givenName = "SMITH";
 
	...
 

 
	// step 3: instantiate an instance of the ASN1C_<ProdName> 
 
	// class to associate the encode buffer and message data..
 

 
	ASN1C_PersonnelRecord employee (encodeBuffer, msgData);
 

 
	// steps 4 and 5: encode and check return status
 

 
	if ((stat = employee.Encode ()) == 0)
 
	{
 
	   printf ("Encoding was successful\n");
 
	   printf ("Hex dump of encoded record:\n");
 
	   encodeBuffer.hexDump ();
 
	   printf ("Binary dump:\n");
 
	   encodeBuffer.binDump ("employee");
 

 
	   // step 6: get start-of-message pointer and message length.
 
	   // start-of-message pointer is start of msgbuf 
 
	   // call getMsgLen to get message length..
 

 
	   msgptr = encodeBuffer.getMsgPtr ();  // will return &msgbuf
 
	   len = encodeBuffer.getMsgLen ();
 
	}
 
	else
 
	{
 
	   printf ("Encoding failed\n");
 
	   encodeBuffer.printErrorInfo ();
 
	   exit (0);
 
	}
 

 
	// msgptr and len now describe fully encoded message
 

 
	...
 

 
In general, static buffers should be used for encoding messages where possible as they offer a substantial performance benefit over dynamic buffer allocation. The problem with static buffers, however, is that you are required to estimate in advance the approximate size of the messages you will be encoding. There is no built-in formula to do this, the size of an ASN.1 message can vary widely based on data types and other factors.

If performance is not a significant issue, then dynamic buffer allocation is a good alternative. Using the form of the ASN1PEREncodeBuffer constructor that does not include buffer address and size arguments specifies dynamic buffer allocation. This constructor only requires a Boolean value to specify whether aligned or unaligned encoding should be performed (aligned is true).

The following code fragment illustrates PER encoding using a dynamic buffer:

   #include employee.h         // include file generated by ASN1C
 

 
   main ()
 
   {
 
	OSOCTET*  msgptr;
 
	int       msglen, stat;
 
	OSBOOL    aligned = TRUE;
 

 
	// Create an instance of the compiler generated class.  
 
	// This example does dynamic encoding (no message buffer 
 
	// is specified)..
 

 
	ASN1PEREncodeBuffer encodeBuffer (aligned);
 
	ASN1T_PersonnelRecord msgData;
 
	ASN1C_PersonnelRecord employee (encodeBuffer, msgData);
 

 
	// Populate msgData within the class variable
 

 
	msgData.name.givenName = "SMITH";
 
	...
 

 
	// Encode
 

 
	if ((stat = employee.Encode ()) == 0)
 
	{
 
	   printf ("Encoding was successful\n");
 
	   printf ("Hex dump of encoded record:\n");
 
	   encodeBuffer.hexDump ();
 
   	   printf ("Binary dump:\n");
 
	   encodeBuffer.binDump ("employee");
 

 
	   // Get start-of-message pointer and length
 

 
	   msgptr = encodeBuffer.getMsgPtr ();
 
	   len = encodeBuffer.getMsgLen ();
 
	}
 
	else
 
	{
 
	   printf ("Encoding failed\n");
 
	   encodeBuffer.printErrorInfo ();
 
	   exit (0);
 
	}
 

 
	return 0;
 
   }
 



Objective Systems, Inc.

55 Dowlin Forge Road
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