Search code examples
omnet++veins

Create a WSM inherented message


I would like to make a new message type called wsm_info.

In this message type, I want to include a vehicle structure, as it follows:

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

In the veins example there is a function called: prepareWSM that's declared in BaseWaveApplLayer.h. This function is a virtual WaveShortMessage* type.

If the wsm_info was inherented from WaveShortMessage I wouldn't need to write and declare a new prepareWSM for wsm_info, right?

So how can I make this wsm_info message inherented of WaveShortMessage?

I tried to write like this in the wsm_info.h:

class wsm_info : public WaveShortMessage

Instead of, that was written previously:

class wsm_info : public ::omnetpp::cPacket

But the error that I get is the following one:

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

The full code of my msg_info is below:

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;
}

Can anyone take a look on my code and point me where is wrong and why? Thanks!


Solution

  • The msg_info.msg should have the following content:

    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;
    }
    

    You cannot use prepareWSM() because it creates a WaveShortMessage object which cannot be cast to wsm_info. Instead you may write a new method, for example:

    1. In /veins/src/veins/modules/application/ieee80211p/BaseWaveApplLayer.h add:

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

      and in the class add the declaration:

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

      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. In order to set vehicle structure you can just write:

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

    Alternatively you can add parameters for vehicle in the definition of prepare_wsm_info().