Rate This Document
Findability
Accuracy
Completeness
Readability

Using the Compilation Tool

The compilation tool hiasn1cli is stored in /usr/local/ksl/bin. You can use it to parse ASN.1 scripts and convert ASN.1 scripts into C intermediate files (containing multiple source files and header files).

This section describes how to use hiasn1cli to convert an .asn file into a C source file.

Getting Help

Run the following command to obtain help information:

1
./hiasn1cli --help 

The following information is displayed:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
ASN.1 Cpp Compiler hiasn1cli 2.1.0

USAGE:
    hiasn1cli [OPTIONS] --input <ASN.1 file (list)> --method <Transmission syntax (aper|uper|ber|xer...)> [SUBCOMMAND]

OPTIONS:
    -h, --help
            Display the help information of the tool.

    -i, --input <ASN.1 file (list)>
            Select one or more ASN.1 text files, for example, -i s1.asn x2.asn. You can also use wildcard characters, for example, -i *.asn.

        --merge <Directory name. A multi-level directory can be specified, for example, a/b/c. The merging result is output to exports/a/b/c.>
            Merge compilation. If ASN.1 files are not merged, each ASN.1 file is compiled and output independently. If this option is used, all input ASN.1 files are merged to generate a target file.

    -m, --method <Transmission syntax (aper|uper|ber|xer...)>
            Select a rule for encoding and decoding. The selected rule applies to all input files in the current CLI.

SUBCOMMANDS:
    cc       [Core] Compile ASN.1 files into C code.

Sub-parameters

The tool has many sub-parameters. For example, you can run the following command to view the help information about the cc sub-parameter:

1
./hiasn1cli cc --help

The following information is displayed:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
hiasn1cli-cc
[Core] Compile ASN.1 files into C code.

USAGE:
    hiasn1cli --input <ASN.1 file (list)> --method <Transmission syntax (aper|uper|ber|xer...)> cc [FLAGS]

FLAGS:
    -h, --help                  Display the help information of the sub-command: hiasn1cli cc --help
        --testcases             Generate the C file and header file of the testcase based on the defined values. This flag relies on --values.
        --values                Generate the definition of all values. This flag relies on --mode full.
        --enum-as-uint          Set the C interface of the ENUMERATED type to u8, u16, or u32. Do not use the enum type of C.

Example

Convert an ASN.1 script test.asn into a C code file in BER format.

  1. Create a test directory test_demo and go to the directory.
    1
    2
    mkdir test_demo
    cd test_demo
    
  2. Create a test.asn file in test_demo.
    1
    vim test.asn
    
  3. Press i to enter the insert mode and add the following content to the file:
    MyModule DEFINITIONS AUTOMATIC TAGS ::= BEGIN 
    
    MyType ::= SEQUENCE { 
        boolType BOOLEAN, 
        intType INTEGER(0..128), 
        enumType ENUMERATED { a, b }, 
        bitStrType BIT STRING(SIZE(0..128)), 
        octStrType OCTET STRING(SIZE(0..64)), 
        seqOfType SEQUENCE OF INTEGER, 
        choiceType CHOICE { 
            a INTEGER(0..3), 
            b BOOLEAN 
        } 
    } 
    
    END
  4. Press Esc, type :wq and press Enter to save the file and exit.
  5. Run the hiasn1cli command to perform the conversion.
    1
    ./hiasn1cli -i test.asn -m ber cc
    
    The generated code file is stored in the test_demo/exports directory.
    1
    2
    3
    4
    5
    exports 
    └── test 
        ├── codec_index.h 
        ├── codec_interfaces.h 
        └── codec_tables.c
    
    • codec_index.h: defines the unique identifier of each data unit, which will be used in encoding and decoding interfaces.
    • codec_interfaces.h: provides C program interfaces for data description in abstract syntax.
    • codec_tables.c: defines static variables for describing each data type.

    If --values and --testcases are specified, codec_testcases.h, codec_values.c, and codec_values.h are also generated to define and declare the values in the script and generate test cases.