Search code examples
c++omnet++

Wireless Communication in Omnet++


I am trying to send a packet from sender to receiver using C++ codes.

I have created a Ned file with 2 wireless host, and a c++ file for each.

I am having this error:

***check_and_cast(): Cannot cast (omnetpp::cPacket*)Packet to type 'inet::physicallayer::WirelessSignal *' -- in module (inet::physicallayer::Ieee80211Radio) Network1.receiver.wlan[0].radio (id=96), at t=0***

Can someone help me with this error?

Here are the codes:

#include <string.h>
#include <omnetpp.h>


using namespace omnetpp;
using namespace inet;

class Sender : public cSimpleModule
{
  private:
    cMessage *packet;
    simtime_t packetGenerationInterval;
    int packetSize;
    cModule *server;
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(Sender);

void Sender::initialize()
{
    packetGenerationInterval = par("packetGenerationInterval");
    packetSize = par("packetSize");
    packet = new cMessage("Packet");
    server =getModuleByPath("receiver");
    scheduleAt(simTime(), packet);  // Schedule the first packet generation
}

void Sender::handleMessage(cMessage *msg)
{
    if (msg == packet) {
        // Generate packet content and header
        std::string packetContent = "Packet from Sender1";

        cPacket *newPacket = new cPacket("Packet");
        newPacket->setByteLength(packetSize);
        
        sendDirect(newPacket, server, "radioIn", 0);

        scheduleAt(simTime() + packetGenerationInterval, packet);
    }

}


#include <string.h>
#include <omnetpp.h>

using namespace omnetpp;

class Receiver : public cSimpleModule
{
  protected:
    virtual void handleMessage(cMessage *msg) override;
};

Define_Module(Receiver);

void Receiver::handleMessage(cMessage *msg)
{
    if (msg->isPacket()) {
        //cPacket *packet = check_and_cast<cPacket *>(msg);
        EV << "Received packet: " << msg << std::endl;
       }
}

I think the issue is with the type of packet created.


Solution

  • There is (quite) a bit of confusion here. You have not mentioned, but you are also using the INET Framework. Based on the error message, youre receiver module is a StandardHost with a configured wireless (WiFi) interface and with all the protocol levels built in (i.e. TCP/IP, link layer etc). On the other hand, your Sender module is NOT a StandardHost, but just a pure OMNeT++ simple module. (The Receiver module you have provided is not used at all in your simulation).

    So, your basic simple module tries to send something to the StandardHost, but its wireless interface does not understand it, which is to be expected. The INET framework has quite a complex architecture to simulate the physical reality and all the protocol behavior. On the other hand, your model is the most basic one, one can imagine. I would suggest take a look at the INET Wireless Tutorial and use StandardHosts both for the sender and receiver modules to be compatible.