1
votes

I'm simulating network in Omnet++, with INET framework. I want to get the position (coordination x & y) of node. So I do this code:

    cModule *host = getContainingNode(this);
    IMobility *mobility = check_and_cast<IMobility *>(host->getSubmodule("mobility"));
    ... = mobility -> getCurrentPosition();

But, when I ran the simulation, I got this error

check_and_cast(): cannot cast nullptr to type 'inet::IMobility *'

Can you explain to me this error? As I see, if the simulator notify that, so host->getSubmodule("mobility") is nullptr?

By the way, I have define mobilityType in the NED file and include IMobility.h

1
It seems that there is no mobility submodule in your host. Could you show ned file of your host? - Jerzy D.
@JerzyD. Here is my host in ned file: simple Txc like IMobility { parameters: bool isSource = default(false); bool isDest = default(false); string mobilityType = default("StationaryMobility"); @display("i=device/hub_vs"); @networkNode; gates: inout gate[]; } - Khanh Lê
Sorry I don't know how to add code or picture or file in comment - Khanh Lê
@JerzyD. I think that error occurred because the type of host Txc is simple, so it don't have Submodule. But IMobility is an interface module, it has no gate - Khanh Lê

1 Answers

2
votes

The NED code you shared shows that you have a simple module that implements the IMobility interface, while the C++ code looks like something that is inside a simple module which is INSIDE a compound module that represent the network host and this compound module also contains a separate simple module called "mobility" that implements the IMobility interface. (this is how code is implemented in INET).

You should either:

  • arrange your code so you have a compound module as a networkNode with several simple modules inside it (one of them is the mobility module).
  • if you insist having a simple module which already implements the IMobility interface (in C++) then you already have access to the position on that object using the getCurrentPosition method.

I assume (and recommend) that you co on the first route, so you should reaarange your NED file.