The following sample application 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.
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
1.1 Input ASN.1 Syntax (bcas.asn)
1.2 Command to compile bcas.asn
1.3 Output files generated by the ASN.1 Compiler
Step 2: Compiling and Linking the sample application
2.1 Input application program tbcas.cpp
2.2 Commands to compile and link the sample program
Step 3: Running the sample program
3.1 Command to run the executable
3.2 Output of the sample program
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. The bcas.h file contains the classes corresponding to the components of ASN.1 file. The bcas.cpp file contains information needed by the encoder and the decoder at the runtime. The file, bcas.cpp , must be compiled and linked with the sample application.
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 OSS ASN.1 Compiler for C++ Reference Manual .
1.3 Output files generated by the ASN.1 Compiler
#ifndef OSS_bcas
#define OSS_bcas
#include "oss.h"
#include "asn1.h"
/* Representation types */
enum _enum1 {
left_handed = 0,
right_handed = 1,
ambidextrous = 2
};
class OSS_PUBLIC BBCard /* SEQUENCE */
{
public:
void * operator new(size_t size);
void operator delete(void *ptr);
typedef OssString name;
typedef OssString team;
typedef OSS_UINT32 age;
typedef OssString position;
typedef enum _enum1 handedness;
typedef OssDecimal batting_average;
BBCard();
BBCard(const BBCard &);
BBCard(const name &, const team &, age, const position &, handedness, const
batting_average &);
BBCard & operator = (const BBCard &);
int operator == (const BBCard &) const;
int operator != (const BBCard &) const;
name& get_name();
const name& get_name() const;
void set_name(const name & );
team& get_team();
const team& get_team() const;
void set_team(const team & );
age& get_age();
age get_age() const;
void set_age(age );
position& get_position();
const position& get_position() const;
void set_position(const position & );
handedness& get_handedness();
handedness get_handedness() const;
void set_handedness(handedness );
batting_average& get_batting_average();
const batting_average& get_batting_average() const;
void set_batting_average(const batting_average & );
private:
name name_field;
team team_field;
age age_field;
position position_field;
handedness handedness_field;
batting_average batting_average_field;
};
/* Universal PDU class */
class OSS_PUBLIC bcas_PDU : public UniversalPDU {
public:
bcas_PDU( );
void set_BBCard(BBCard &);
BBCard *get_BBCard( ) const;
};
/* Specific PDU classes */
class OSS_PUBLIC BBCard_PDU : public ConcretePDU {
public:
BBCard_PDU( );
void set_data(BBCard &);
BBCard * get_data() const;
protected:
OssTypeIndex get_index( ) const;
};
/* Control object class */
class OSS_PUBLIC bcas_Control : public OssControl {
public:
bcas_Control();
bcas_Control(const bcas_Control &);
};
/* External definitions for named values */
extern OSS_PUBLIC const BBCard& myCard;
#endif // OSS_bcas
The bcas.cpp file contains the information needed by the runtime APIs to encode and decode PDUs. Since this file contains internal information, it is not shown here. This file must be compiled and linked with the sample application.
Step 2: Compiling and Linking the sample application
The following 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 makefiles asn1cpl.mak and tbcas.mak . The asn1cpl.mak file compiles ASN.1 syntax and generates .h and .cpp files. Another makefile, tbcas.mak, compiles application code, generated .cpp file, and link the object files with OSS runtime libraries to generate sample executable. These makefiles are located under the samples/bcas directory. For example, if you have installed OSS ASN.1/C++ tools under /asn1cpp directory, these makefiles can be found under /asn1cpp/samples/bcas directory. An executable for the sample program can be created by using following commands on Windows and UNIX:
For Windows:
nmake -f asn1cpl.mak
nmake -f tbcas.mak
For Unix:
make -f asn1cpl.mak
make -f tbcas.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
Copyright © 2010 OSS Nokalva, Inc. All Rights Reserved.