ASN.1官网:http://lionet.info/asn1c/examples.html
1、编译环境
实验平台:ubuntu16.04
2、ASN.1介绍
ASN.1是定义抽象数据类型规格形式的标准。是用于描述数据的表示、编码、传输、解码的灵活的记法。它提供了一套正式、无歧义和精确的规则,以描述独立于特定计算机硬件的对象结构。
ASN.1是通信协议中描述数据传输的正式标记(notation),它与语言实现和物理表示无关,与应用的复杂度无关。ASN.1特别适合表示现代通信应用中那些复杂的、变化的及可扩展的数据结构。
ASN.1发送任何形式(音频、视频、数据等等)的信息都必须用数字传送。ASN.1只能包含信息的结构方面(没有已经定义的或考虑到的处理数据值的操作)。它不是一个编程语言。 ASN.1格式数据有多种编码方式,包括BER、DER、XER、PER/UPER等。
3、ASN.1实例
3.1 ASD1C
ASD1C是ASN.1的c/c++实例。
实例功能:
(1)本工程将ASN文件编译为.c和.h文件,通过调用API实现UPER编码和解码。
(2)编写了一个UDP的client和server端,对数据传输进行了测试。
(3)网上的例程只涉及到BER编码,没有关于UPER编码的历程,本工程采用UPER编码。
3.2 下载ADN1C工具
下载地址:http://lionet.info/asn1c/download.html
3.3 编译ADN1C工具
下载之后进行解压
sudo tar xf asn1c-0.9.28.tar.gz
然后进入解压目录,执行
cd asn1c-0.9.28/
编译安装
参考该目录文件INSTALL.md:
## Compiling From Sources
### Configure Configure with the default settings: test -f configure || autoreconf -iv ./configure makeConfigure with non-standard settings: asn1c specific ./configure options include: --enable-Werror --enable-ASN_DEBUGinvoke `./configure --help` for details.
### Build Build the libraries and the compiler: makeEnsure asn1c is still behaving well after compiling on your platform: make check
### Install Install the compiler into a standard location: make install # Use ./configure --prefix to override install location.Display the `asn1c` manual page: man asn1c
## Quick Usage Guide For a usage guide and more information please refer to: * the [README.md](README.md) file * the asn1c manual page `man asn1c` * the included quick start PDF [doc/asn1c-quick.pdf](doc/asn1c-quick.pdf) * the comprehensive usage documentation [doc/asn1c-usage.pdf](doc/asn1c-usage.pdf) In case of any difficulties with installing the compiler, consider usingthe Online ASN.1 Compiler at http://lionet.info/asn1c. -- Lev [email protected]
最后执行man ans1c,进行验证:
3.4 创建一个.asn文件
创建一个目录asn_send
sudo mkdir asn_sendcd asn_send/
创建一个.asn文件,以Traffic_light.asn为例
sudo touch Traffic_light.asnvim Traffic_light.asn
里面按照asn格式填充数据:
MsgTest DEFINITIONS ::=BEGIN Msg ::= SEQUENCE{ length INTEGER, latitude INTEGER, -- latitude of traffic longitude INTEGER, -- longitude of traffic heading INTEGER, state INTEGER, time INTEGER, pading INTEGER }END
3.5 编译.asn文件
利用ans1c工具进行编译,选择PER编码格式
asn1c -gen-PER *.asn
编译之后出现很多.c和.h文件,可以从Msg.h看到我们定义的结构体。