The procedure to encode a message using the C++ class interface is as follows:
Instantiate an ASN.1 XER encode buffer object (ASN1XEREncodeBuffer) to describe the buffer into which the message will be encoded. Constructors are available that allow a static message buffer to be specified and/or canonical encoding to be turned on (canonical encoding removes all encoding options from the final message to produce a single encoded representation of the data). The default constructor specifies use of a dynamic encode buffer and canonical encoding set to off.
Instantiate an ASN1T_<type> object and populate it with data to be encoded.
Instantiate an ASN1C_<type> object to associate the message buffer with the data to be encoded.
Invoke the ASN1C_<type> object Encode method.
Check the return status. The return value is a status value indicating whether encoding was successful or not. Zero indicates success. If encoding failed, the status value will be a negative number. The encode buffer method printErrorInfo can be invoked to get a textual explanation and stack trace of where the error occurred.
If encoding was successful, get the start-of-message pointer and message length. The start-of-message pointer is obtained by calling the getMsgPtr method of the encode buffer object. If static encoding was specified (i.e., a message buffer address and size were specified to the XER Encode Buffer class constructor), the start-of-message pointer is the buffer start address. The message length is obtained by calling the getMsgLen method of the encode buffer object.
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 canonical = FALSE; // step 1: instantiate an instance of the XER encode // buffer class. This example specifies a static // message buffer.. ASN1XEREncodeBuffer encodeBuffer (msgbuf, sizeof(msgbuf), canonical); // 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 ("encoded XML message:\n"); printf ((const char*)msgbuf); printf (“\n”); // 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 ...