Do you have a sample application that illustrates step-by-step the use of the OSS ASN.1/C++ Compiler to compile a simple ASN.1 module, and the use of the OSS C++ API to invoke the encoder/decoder?

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.

Step 1: Invoking the ASN.1 Compiler

The following describes how to compile abstract syntax defined in the bcas.asn file.

1.1 Input ASN.1 Syntax (bcas.asn)
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

1.2 Command to compile bcas.asn

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.


1.3 Output files generated by the ASN.1 Compiler

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.


Step 2: Compiling and Linking 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.

2.1 Input application program tbcas.cpp

2.2 Command to compile and link sample program

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:

  • update the OSS_ASN1_CPP_DIR environment variable in start_ide.bat so that it points to the OSS ASN1/C++ installation directory.
  • open the Visual Studio solution (project) by running
    start_ide.bat
  • build the project. The pre-build event will invoke asn1cpp.exe to generate the *.cpp and *.h files from the ASN.1 specification, compile all files, link the appropriate libraries and create the executable.

On Unix:

  • update the OSS_ASN1_CPP_DIR in the sample makefile (*.mak) so that it points to the ASN1/C++ installation directory.
  • build the executable by typing
    make -f *.mak

Step 3: Running the sample program

3.1 Command to run the executable

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

3.2 Output of the sample program

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.



Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email: support@oss.com
Free Trial
download

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.




Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.