最近在做3GPP的编解码,发现有两个第三方库比较好用。一个是ASN1C(c语言编译环境),一个是python第三方库asn1tools。这里介绍下asn1tools的使用方法:
1 第一步:生成asn文件
将需要编码的数据结构保存在asn后缀名的文件中
3GPP中的结构如下:
-- ASN1START
BCCH-BCH-Message-NB ::= SEQUENCE {
message BCCH-BCH-MessageType-NB
}
BCCH-BCH-MessageType-NB::= MasterInformationBlock-NB
-- ASN1STOP
对应的.asn文件的基本结构如下:也就是讲ASN1START和ASN1STOP中的数据提取出来。然后上asn自己的头信息
EUTRA-RRC-Definitions DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
BCCH-BCH-Message ::= SEQUENCE {
message BCCH-BCH-MessageType
}
END
在3GPP中有大量的类似结构,如果一个个手动的拷贝,太耗费时间了。因此用下面的代码将3GPP中的数据结构自动提取出来保存在asn文件中。代码如下:
#include
#include
#include
#include
using namespace std;
int main()
{
std::string output_file;
std::string input_file = "D:/code_block_prj/gen_asn/protol.txt";
std::cout<
int pos = input_file.find(‘.‘);
if (pos == std::string::npos )
{
output_file = input_file + ".asn";
}
else
{
output_file = input_file.substr(0,pos) + ".asn";
}
std::fstream input;
input.open(input_file.c_str(), std::fstream::in );
if ( input.fail() == true)
{
std::cout<
return 1;
}
std::fstream output;
output.open(output_file.c_str(), std::fstream::out );
if ( output.fail() == true)
{
std::c