I'm trying to implement a small example in veins: the RSU broadcasts its own ID and other information, and the vehicle receives the RSU'S ID and records it. I have created a new msg file named BeaconRSU.msg, and the application layer's cc files of RSU and vehicle are shown below:
//MyVeinsAppRSU.cc
#include "veins/modules/application/traci/MyVeinsAppRSU.h"
#include "veins/modules/application/traci/BeaconRSU_m.h"
using namespace veins;
Define_Module(veins::MyVeinsAppRSU);
void MyVeinsAppRSU::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
if (stage == 0) {
sendBeacon= new cMessage("send Beacon");
EV << "Initializing " << par("appName").stringValue() << std::endl;
}
else if (stage == 1) {
// Initializing members that require initialized other modules goes here
if (sendBeacon->isScheduled())
{
cancelEvent(sendBeacon);
}
scheduleAt(simTime()+5,sendBeacon);
}
}
void MyVeinsAppRSU::handleSelfMsg(cMessage* msg)
{
if (msg == sendBeacon){
BeaconRSU* rsuBeacon = new BeaconRSU();
rsuBeacon->setRSUId(this->getParentModule()->getIndex());
rsuBeacon->setMyDemoData("RSU message!!");
BaseFrame1609_4* WSM = new BaseFrame1609_4();
WSM->encapsulate(rsuBeacon);
populateWSM(WSM);
send(WSM,lowerLayerOut);
EV << "rsu send success" <<endl;
if (simTime() < 2000) {
scheduleAt(simTime()+1,sendBeacon);
}
return;
}
}
//MyVeinsAppCar.cc
#include "veins/modules/application/traci/MyVeinsAppCar.h"
#include "veins/modules/application/traci/BeaconRSU_m.h"
#include "veins/modules/application/traci/MyVeinsAppRSU.h"
using namespace veins;
Define_Module(veins::MyVeinsAppCar);
void MyVeinsAppCar::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
if (stage == 0) {
// Initializing members and pointers of your application goes here
EV << "Initializing " << par("appName").stringValue() << std::endl;
int a = INT_MIN;
}
else if (stage == 1) {
// Initializing members that require initialized other modules goes here
RSUIndex.setName("test");
int a= INT_MIN;
EV << "MyVeinsAppCar is initializing" << std::endl;
}
}
void MyVeinsAppCar::handleLowerMsg(cMessage* msg)
{
BaseFrame1609_4* WSM = check_and_cast<BaseFrame1609_4*>(msg);
cPacket* enc = WSM->getEncapsulatedPacket();
BeaconRSU* bc = dynamic_cast<BeaconRSU*>(enc);
EV << "receive message !!!" << endl;
if(a!=bc->getRSUId())
{
RSUIndex.record(bc->getRSUId());
a=bc->getRSUId();
}
EV << "my message = " <<bc->getMyDemoData()<<endl;
EV <<"send message RSU id:" <<bc->getRSUId() << " Receive successfully !!!!!!!!!!!" << endl; }
When I run the simulation, I can see the msg sent from RSU successfully, but nodes cannot receive the msg (The contents in MyVeinsAppCar::handleLowerMsg(cMessage* msg) are not printed out). And there some error in log: enter image description here
Why did this happen? Is someone help me? Thank you in advance!
//BeaconRSU.msg
cplusplus{{
#import "veins/base/utils/Coord.h"
#import "veins/modules/utility/Consts80211p.h"
#include "veins/modules/messages/BaseFrame1609_4_m.h"
#include "veins/base/utils/SimpleAddress.h"
}};
namespace veins;
// TODO generated message class
class noncobject Coord;
class BaseFrame1609_4;
packet BeaconRSU extends BaseFrame1609_4
{
//id of the originator
int RSUId = 0;
Coord position[100];
double beaconrate[100];
string myDemoData;
Coord slotpos;
simtime_t timestamp=0;
}
Part of the debug mode log is shown below: