创建 WSM 固有消息

Create a WSM inherented message

我想创建一个名为 wsm_info 的新消息类型。

在这个消息类型中,我想包含一个车辆结构,如下:

struct vehicle{
   int vehicle_id;
   Coord vehicle_pos;
   float speed;
};

在静脉示例中,有一个名为 prepareWSM 的函数,它在 BaseWaveApplLayer.h 中声明。这个函数是virtual WaveShortMessage*类型的。

如果 wsm_infoWaveShortMessage 固有的,我就不需要为 wsm_info 编写和声明一个新的 prepareWSM,对吗?

那么如何使 WaveShortMessage 固有的 wsm_info 消息呢?

我试过在wsm_info.h中这样写:

class wsm_info : public WaveShortMessage

而不是之前写的:

class wsm_info : public ::omnetpp::cPacket

但我得到的错误是以下错误:

cannot initialize a variable of type wsm_info * with an rvalue of type WaveShortMessage

我的msg_info的完整代码如下:

cplusplus {{
#include "veins/base/utils/Coord.h"
#include "veins/modules/messages/WaveShortMessage_m.h"
}}

class noncobject Coord;

class WaveShortMessage;

struct vehicle {
        int vehicle_id;
        Coord vehicle_pos;
        float speed;
    };

message wsm_info extends WaveShortMessage {
    //Version of the Wave Short Message
    int wsmVersion = 0;
    //Determine which security mechanism was used
    int securityType = 0;
    //Channel Number on which this packet was sent
    int channelNumber;
    //Data rate with which this packet was sent
    int dataRate = 1;
    //Power Level with which this packet was sent
    int priority = 3;
    //Unique number to identify the service
    int psid = 0;
    //Provider Service Context
    string psc = "Service with some Data";
    //Length of Wave Short Message
    int wsmLength;

    vehicle data;

    int senderAddress = 0;
    int recipientAddress = -1;
    int serial = 0;
    Coord senderPos;
    simtime_t timestamp = 0;
}

任何人都可以看一下我的代码并指出错误的地方和原因吗?谢谢!

如果我没看错,你想延长 wsm_info.msg,对吗?

根据问题,您可以按以下方式修改您的wsm_info.msg:

cplusplus {{
#include "veins/modules/messages/WaveShortMessage_m.h"
}}

class WaveShortMessage;

message wsm_info extends WaveShortMessage {
   int vehicle_id;
   Coord vehicle_pos;
   float speed;
}

msg_info.msg应该有以下内容:

cplusplus {{
#include "veins/modules/messages/WaveShortMessage_m.h"
}}

class noncobject Coord;

struct vehicle {
   int vehicle_id;
   Coord vehicle_pos;
   float speed;
};

class WaveShortMessage;

packet wsm_info extends WaveShortMessage {
    vehicle data;
}

您不能使用 prepareWSM(),因为它会创建一个无法转换为 wsm_infoWaveShortMessage 对象。相反,您可以编写一个新方法,例如:

  1. /veins/src/veins/modules/application/ieee80211p/BaseWaveApplLayer.h中添加:

    #include "veins/modules/messages/wsm_info_m.h"
    

    并在 class 中添加声明:

    wsm_info* prepare_wsm_info(std::string name, int dataLengthBits, t_channel channel, int priority, int rcvId, int serial=0);
    
  2. /veins/src/veins/modules/application/ieee80211p/BaseWaveApplLayer.cc中添加:

    wsm_info*  BaseWaveApplLayer::prepare_wsm_info(std::string name, int lengthBits, t_channel channel, int priority, int rcvId, int serial) {
    wsm_info* wsm = new wsm_info(name.c_str());
    // ... content similar to prepareWSM()
    }
    
  3. 为了设置vehicle结构你可以这样写:

    wsm_info* info = prepare_wsm_info(/* parameters */);
    vehicle veh;
    veh.speed = 60;
    veh.vehicle_id = 3;
    // ...
    
    info->setData(veh);
    

或者,您可以在 prepare_wsm_info() 的定义中为 vehicle 添加参数。

必须在哪里声明wsm_info.msg?在哪个文件夹中?