The procedure to encode a message using the C++ class interface is as follows:
Instantiate an XML encode buffer object (OSXMLEncodeBuffer) to describe the buffer into which the message will be encoded. Constructors are available that allow a static message buffer to be specified. The default constructor specifies use of a dynamic encode buffer.
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 or EncodeTo 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 XML 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 ()
{
OSOCTET msgbuf[4096];
int msglen, stat;
// step 1: instantiate an instance of the XML encode
// buffer class. This example specifies a static
// message buffer..
OSXMLEncodeBuffer encodeBuffer (msgbuf, sizeof(msgbuf));
// 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
...