如何在 SEQUENCE OF 中对 SEQUENCE OF 进行编码(asn1 到 c)

How to code a SEQUENCE OF inside a SEQUENCE OF (asn1 to c)

提问人:Santiago 提问时间:10/7/2022 最后编辑:Santiago 更新时间:10/7/2022 访问量:219

问:

我正在与 vanetza 一起制作 cpms 消息,我已经从 asn1 构建了 .c 和 .cpp 文件,因此消息的结构具有 asn1 的结构。我有一个结构(PerceptionDataContainer),它是 PerceptionData 的 SEQUENCE,Sequence of 是一种特殊类型的列表。每个 PerceptionData 都有一个 PerceivedObjects 结构,它是 SEQUENCE OF PerceivedObject 结构,我制作了此代码来向这些结构添加信息;

    vanetza::asn1::Cpm cpm;

    auto object = asn1::allocate<PerceivedObject_t>(); //this fill the struct PerceivedObject with basic info
    auto perception = asn1::allocate<PerceptionData_t>();

    PerceivedObject *arrayobjeto[2];

    cpm->cpm.cpmParameters.perceptionData = asn1::allocate<CpmParameters::CpmParameters__perceptionData>();

    cpm->header.protocolVersion=2;
    cpm->header.messageID=ItsPduHeader__messageID_cpm;
    cpm->header.stationID=1;
    cpm->cpm.cpmParameters.managementContainer.stationType=2;

    for(int i=0; i<2;i++) {
        arrayobjeto[i]=asn1::allocate<PerceivedObject>();
        arrayobjeto[i]->objectID= i+1;
        arrayobjeto[i]->timeOfMeasurement = TimeOfMeasurement_oneMilliSecond;
        arrayobjeto[i]->xDistance.value = DistanceValue_oneMeter;
        arrayobjeto[i]->xDistance.confidence = DistanceConfidence_oneMeter;
        arrayobjeto[i]->yDistance.value = DistanceValue_oneMeter;
        arrayobjeto[i]->yDistance.confidence = DistanceConfidence_oneMeter;
        arrayobjeto[i]->xSpeed.value = SpeedValueExtended_oneCentimeterPerSec;
        arrayobjeto[i]->xSpeed.confidence = SpeedConfidence_equalOrWithinOneMeterPerSec;
        arrayobjeto[i]->ySpeed.value = SpeedValueExtended_oneCentimeterPerSec;
        arrayobjeto[i]->ySpeed.confidence = SpeedConfidence_equalOrWithinOneMeterPerSec;
    }
    perception->containerId=1;
    perception->containerData.present=PerceptionData__containerData_PR_PerceivedObjectContainer;
    perception->containerData.choice.PerceivedObjectContainer.numberOfPerceivedObjects=1;

    for(int i=0; i<2;i++) {
        EXPECT_EQ(0, ASN_SEQUENCE_ADD(&perception->containerData.choice.PerceivedObjectContainer.perceivedObjects,arrayobjeto[i]));
    }
    EXPECT_EQ(0, ASN_SEQUENCE_ADD(&cpm->cpm.cpmParameters.perceptionData->list, perception));
    EXPECT_EQ(1, cpm->cpm.cpmParameters.perceptionData->list.count);
    EXPECT_EQ(2, cpm->cpm.cpmParameters.perceptionData->list.array[0]->containerData.choice.PerceivedObjectContainer.perceivedObjects.list.count);
    EXPECT_EQ(perception, cpm->cpm.cpmParameters.perceptionData->list.array[0]);
    EXPECT_EQ(arrayobjeto[0], cpm->cpm.cpmParameters.perceptionData->list.array[0]->containerData.choice.PerceivedObjectContainer.perceivedObjects.list.array[0]);


    EXPECT_TRUE(cpm.validate());
    EXPECT_FALSE(cpm.encode().empty());
    ByteBuffer buffer = cpm.encode();
    std::cout << "tamaño: " << buffer.size() << "\n";
    for (const auto byte:buffer){
        printf("%02x ",byte);
    }
    
    ASSERT_TRUE(cpm.decode(buffer));
    std::cout << cpm.size();

结构的定义如下:

typedef struct CpmParameters {
    CpmManagementContainer_t     managementContainer;
    struct OriginatingStationData   *stationDataContainer;  /* OPTIONAL */
    struct CpmParameters__perceptionData {
        A_SEQUENCE_OF(struct PerceptionData) list;
        
        /* Context for parsing across buffer boundaries */
        asn_struct_ctx_t _asn_ctx;
    } *perceptionData;

....

typedef struct PerceptionData {
    CpmContainerId_t     containerId;
    struct PerceptionData__containerData {
        PerceptionData__containerData_PR present;
        union PerceptionData__containerData_u {
            SensorInformationContainer_t     SensorInformationContainer;
            PerceivedObjectContainer_t   PerceivedObjectContainer;
            FreeSpaceAddendumContainer_t     FreeSpaceAddendumContainer;
        } choice;
        
        /* Context for parsing across buffer boundaries */
        asn_struct_ctx_t _asn_ctx;
    } containerData;

....

typedef struct PerceivedObjectContainer {
    NumberOfPerceivedObjects_t   numberOfPerceivedObjects;  /* DEFAULT 0 */
    struct PerceivedObjectContainer__perceivedObjects {
        A_SEQUENCE_OF(struct PerceivedObject) list;
        
        /* Context for parsing across buffer boundaries */
        asn_struct_ctx_t _asn_ctx;
    } perceivedObjects;
    
    /* Context for parsing across buffer boundaries */
    asn_struct_ctx_t _asn_ctx;
} PerceivedObjectContainer_t;

我已经达到了一个很好的编码,但它无法正确解码,而且似乎我没有完美地将数据加载到结构中,有人知道如何正确操作吗?

C +11 可视化 C++ 序列 ASN.1

评论

0赞 Gerhardh 10/7/2022
欢迎来到 SO。请选择与您的语言相匹配的语言标签。此代码是 C++,而不是 C。它们是截然不同的语言。
0赞 bazza 11/13/2022
该测试代码是否真的断言了正确的编码?我不认识生成的代码的样式。这两行似乎首先测试要序列化的对象不是空的,然后对 cpm 进行编码,但没有检查编码是否确实成功。验证事物的一种方法是使用 OSS-Nokalva 网站上的 ASN.1 playground。把你的模式放进去,也放进你的序列表,看看它是否可以解码它。

答: 暂无答案