The sample application described here illustrates the use of the OSS ASN.1/C++ Compiler to compile a simple ASN.1 module, and the use of the OSS C++ runtime APIs to encode and decode data using ASN.1 binary and text encoding rules. You can download the complete sample described below for Unix tbcascpp.unix.zip or Windows tbcascpp.windows.zip.
The C++ application program in this example is tbcas.cpp and the associated ASN.1 source file is bcas.asn. The ASN.1 file contains a value notation "mycard". The application program shows how this value notation can be encoded using various ASN.1 encoding rules, and then decodes encoded data into C++ objects.
The following steps show how you can create and run a sample executable program using OSS ASN.1/C++ tools.
The following describes how to compile abstract syntax defined in the bcas.asn file.
BCAS DEFINITIONS ::= BEGIN BBCard ::= SEQUENCE { name IA5String (SIZE (1..60)), team IA5String (SIZE (1..60)), age INTEGER (1..100), position IA5String (SIZE (1..60)), handedness ENUMERATED {left-handed(0), right-handed(1), ambidextrous(2)}, batting-average REAL --<DECIMAL>-- } myCard BBCard ::= { name "Casey", team "Mudville Nine", age 32, position "left field", handedness ambidextrous, batting-average {mantissa 250, base 10, exponent -3} } END
The following command compiles bcas.asn, and generates the bcas.h and bcas.cpp files.
asn1cpp -per -ber -xer bcas.asn
or
asn1cpp -per -ber -xer bcas
The -ber, -per and -xer compiler options notify the compiler that BER (Basic Encoding Rules), PER (Packed Encoding Rules) and XER (XML Encoding Rules) will be used at runtime. Detailed description of all the compiler options supported by the OSS ASN.1/C++ Compiler can be found in the ASN.1/C++ Compiler Options Reference section.
The bcas.h file contains the classes corresponding to the components of ASN.1 file.
The bcas.cpp file contains the information needed by the runtime APIs to encode and decode PDUs and must be compiled and linked with the sample application.
The sample C++ program, tbcas.cpp , encodes the PDU described by the bcas.asn abstract syntax using BER (Basic Encoding Rules), and then decodes it. Similarly, other encoding rules can be used instead of BER by changing the encoding rule parameter of the ctl.setEncodingRules() method call. For instance, OSS_PER_ALIGNED or OSS_PER_UNALIGNED can be passed for Packed Encoding Rules (PER), or OSS_XER can be passed in order to use Basic XML Encoding Rules (Basic-XER).
Note that bcas.h (generated by the compiler) has been #included in the application program. This header file contains the generated declarations for the application. (The function calls to the OSS Encoder/Decoder functions are in bold font.)
After the application program is compiled, the OSS Encoder/Decoder runtime library must be linked into the resulting executable file.
The sample program can be compiled and linked using the included makefile (Unix) and Visual Studio solution file (Windows), which compile the application code, the generated .cpp file, and link the object files with the OSS runtime libraries to generate sample executable.
On Windows:
start_ide.bat
On Unix:
make -f *.mak
An executable tbcas (tbcas.exe for Windows) will be generated after step 2.2. This executable can be run on the command line by following command:
tbcas
Running tbcas will generate the following output: tbcas.out.
The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.
Test drive the OSS Nokalva ASN.1, LTE, and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.
Our expert personnel can help you learn ASN.1!
We offer 4-day ASN.1 courses at our headquarters or your premises.