整体流程图
ASN.1 标准简介
在电信和计算机网络领域,ASN.1(Abstract Syntax Notation One) 是一套标准,是描述数据、编码、传输、解码的灵活的抽象语义记法。它提供了一套正式、无歧义和精确的规则以描述独立于特定计算机硬件的对象结构。
3GPP协议
5G NR 的无线资源控制(RRC)协议是
38.331
LTE 的无线资源控制(RRC)协议是
36.331
提取RRC,ASN1样式
转换工具下载
将下载后的3GPP协议Word
文档去掉页眉页脚后转换成纯文本txt
, 编码选择UTF-8
使用txt2asn1
转换
请下载ZIP格式并解压,避免出现exe权限无法运行问题
在同目录下终端运行 得到xxx.asn1文件
./txt2asn1.exe XXX.txt
使用extract_asn1_from_spec.pl
下载文件后, 源文件同目录终端运行
perl extract_asn1_from_spec xxxx.txt
得到 EUTRA-RRC-Definitions.asn
, EUTRA-UE-Variables.asn
,EUTRA-InterNodeDefinitions
三个文件
ASN1C编译ASN.1文件
ASN1C 是将 ASN.1 规范转换为 C 源代码的免费开源编译器。 它支持一系列 ASN.1 语法,包括 ISO/IEC/ITU ASN.1 1988、'94、'97、2002 和以后的修正。 支持的编码规则集是
- BER: ITU-T Rec. X.690 | ISO/IEC 8825-1 (2002) (BER/DER/CER)
- PER: X.691|8825-2 (2002) (PER).
- XER: X.693|8825-3 (2001) (BASIC-XER/CXER).
ubuntu 可直接通过 sudo apt-get install asn1c
进行安装
sudo apt update
sudo apt install asn1c
使用asn1c
编译
asn1c -S /usr/share/asn1c -fcompound-names -gen-PER -pdu=auto 36331-f40.asn1
# -pdu=auto 产生pdu_colletion.c文件
# -fcompound-names 去除重定义
# -gen-PER 使用PER编码
# -S /usr/share/asn1c 指定asn1c库文件位置, 通过使用whereis asn1c命令查看其库位置
FAQ
Q.: Does asn1c support BER encoding? I see only DER encoding in documentation.
A.: Yes, the asn1c compiler fully supports BER encoding. Any DER encoded data is, by definition, a valid BER data, from the receiver’s point of view.
If you want data to be BER or DER encoded, just invoke der_encode().
Q.: My linker complains: Undefined symbols: _asn_DEF_PDU
.
A.: If you have your own file with main() routine, just delete the converter-sample.c
file, you won’t need it. If you haven’t created the main() routine yourself and wish to rely on converter-sample.c for your transcoding needs, add -DPDU=YourPdu
to the list of compiler flags.
Q.: How to compile the 3GPP Radio Resource Control specification?
A.: The asn1c distribution includes an RRC 25.331 (version 7.1.0) example, check out https://github.com/vlm/asn1c/tree/master/examples/sample.source.RRC. You can also use the pre-compiled rrc-dump.exe program, which is part of Windows ASN.1 compiler installer.
Q.: How to compile the ISO 13522 (MHEG-5) specification?
A.: The ASN.1 compiler comes with a MHEG-5 decoder. Follow the https://github.com/vlm/asn1c/tree/master/examples/sample.source.MHEG5 README file instructions. You can also use the pre-compiled mheg5dump.exe program, which is part of Windows ASN.1 compiler installer.
Q.: How to compile the XYZ decoder?
A.: If XYZ is one of MEGACO (Media Gateway Control), PKIX1/X.509, LDAP, GSM TAP3 or OAM ULP, just go to examples/sample.source.XYZ and run make
. The Windows ASN.1 compiler installer already contains the pre-compiled decoders for these encoding formats.
If you have something else, you should either use the Online ASN.1 compiler, or download the ASN.1 compiler source code.
如果不使用自己的main()
函数, 直接使用converter-sample.c
的化, 需要在其中添加一个目标PDU
#include <asn_application.h>
#include <asn_internal.h> /* for ASN__DEFAULT_STACK_MAX */
#define PDU BCCH_BCH_Message
/* Convert "Type" defined by -DPDU into "asn_DEF_Type" */
#define ASN_DEF_PDU(t) asn_DEF_ ## t
#define DEF_PDU_Type(t) ASN_DEF_PDU(t)
#define PDU_Type DEF_PDU_Type(PDU)
或者在Makefile.am.sample
文件中, 修改 CFLAGS
添加 -DPDU=
选项
CFLAGS += -DHAVE_CONFIG_H -DJUNKTEST -D_DEFAULT_SOURCE -DPDU=BCCH_BCH_Message -DASN_PDU_COLLECTION -I.
OBJS=${ASN_MODULE_SRCS:.c=.o} ${ASN_CONVERTER_SOURCES:.c=.o}
all: $(TARGET)
$(TARGET): ${OBJS}
$(CC) $(CFLAGS) -o $(TARGET) ${OBJS} $(LDFLAGS) $(LIBS)