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. By following these steps you can produce and run an executable program that encodes and then decodes a message. You can download the complete sample described below here: tbcas.windows.zip.
The C# application program in this example is tbcas.cs and the associated ASN.1 source file is bcas.asn.
The following describes how to compile the abstract syntax defined in bcas.asn:
INPUT
-- Baseball Card Abstract Syntax (BCAS) 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 } myCard BBCard ::= { name "Casey", team "Mudville Nine", age 32, position "left field", handedness ambidextrous, batting-average {mantissa 250, base 10, exponent -3} } END
COMMAND
You enter the following command:
asn1cs bcas.asn
or equivalently:
asn1cs bcas
The above command informs the OSS ASN.1/C# Compiler that we want to convert the ASN.1 file bcas.asn to C#. This is implied by default when no command line option is specified.
The OSS ASN.1/C# Compiler will generate the following files and C# classes:
Bcas\FileList.rsp | // FileList.rsp | |
Bcas\TypeMapping.txt | // TypeMapping.txt file | |
Bcas\Bcas\BerCodec.cs | // BerCodec class | |
Bcas\Bcas\_InternalMetadata.cs | // Internal metadata class | |
Bcas\Bcas\BCASModule\BBCard.cs | // BBCard type class | |
Bcas\Bcas\BCASModule\Values.cs | // BCAS module type values class |
The C# program tbcas.cs validates the myCard value of the BBCard PDU described by the bcas.asn abstract syntax, encodes the PDU (using BER), decodes it, and compares the unencoded and decoded PDUs. Note that the bcas C# classes (generated by the compiler) have been used by the application program.
INPUT
C# File, tbcas.cs
COMMAND
Next, build the sample application program as follows:
>csc -out:tbcas.exe tbcas.cs -recurse:bcas\*.cs -r:"%OSS_CSHARP_HOME%"\bin\asn1csrt.dll
OUTPUT
The above command C#-compiles the compiler-generated and tbcas.cs C# files and builds the resulting application tbcas.exe using the OSS runtime asn1csrt.dll.
Step 3: Invoking the encoder/decoder
INPUT
(None: For this example, the encoder input is generated by the ASN.1 compiler as a C# class instance initialized from the ASN.1 value notation.)
COMMAND
First, copy your asn1csrt.dll to your tbcas folder. The following command simply runs tbcas:
>tbcas
OUTPUT
EncodeDecodeAndPrint MockObject Input PDU: { name "name", team "team", age 1, position "position", handedness left-handed, batting-average 0 } Encoding MockObject PDU using BerCodec: PDU successfully encoded, in 32 bytes Encoded value: 301E8004 6E616D65 81047465 616D8201 01830870 6F736974 696F6E84 01008500 Decoding MockObject PDU using BerCodec: Decoded PDU: { name "name", team "team", age 1, position "position", handedness "left-handed", batting-average 0 } EncodeDecodeAndPrint MyCard Input PDU: { name "Casey", team "Mudville Nine", age 32, position "left field", handedness ambidextrous, batting-average { mantissa 1, base 2, exponent -2 } } Encoding MyCard PDU using BerCodec: PDU successfully encoded, in 47 bytes Encoded value: 302D8005 43617365 79810D4D 75647669 6C6C6520 4E696E65 82012083 0A6C6566 74206669 656C6484 01028503 80FE01 Decoding MyCard PDU using BerCodec: Decoded PDU: { name "Casey", team "Mudville Nine", age 32, position "left field", handedness "ambidextrous", batting-average { mantissa 1, base 2, exponent -2 } } All values encoded and decoded successfully.
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.