Skip to content

Commit

Permalink
Prefer explicit DynamicCast in place of GetObject for downcasting (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
non-det-alle authored Oct 21, 2024
1 parent ae219bb commit 82ea58a
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 98 deletions.
14 changes: 7 additions & 7 deletions examples/aloha-throughput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ main(int argc, char* argv[])
for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
{
Ptr<Node> node = *j;
Ptr<LoraNetDevice> loraNetDevice = node->GetDevice(0)->GetObject<LoraNetDevice>();
Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(node->GetDevice(0));
Ptr<LoraPhy> phy = loraNetDevice->GetPhy();
}

Expand Down Expand Up @@ -325,17 +325,17 @@ main(int argc, char* argv[])
// Install trace sources
for (auto node = gateways.Begin(); node != gateways.End(); node++)
{
(*node)->GetDevice(0)->GetObject<LoraNetDevice>()->GetPhy()->TraceConnectWithoutContext(
"ReceivedPacket",
MakeCallback(OnPacketReceptionCallback));
DynamicCast<LoraNetDevice>((*node)->GetDevice(0))
->GetPhy()
->TraceConnectWithoutContext("ReceivedPacket", MakeCallback(OnPacketReceptionCallback));
}

// Install trace sources
for (auto node = endDevices.Begin(); node != endDevices.End(); node++)
{
(*node)->GetDevice(0)->GetObject<LoraNetDevice>()->GetPhy()->TraceConnectWithoutContext(
"StartSending",
MakeCallback(OnTransmissionCallback));
DynamicCast<LoraNetDevice>((*node)->GetDevice(0))
->GetPhy()
->TraceConnectWithoutContext("StartSending", MakeCallback(OnTransmissionCallback));
}

LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
Expand Down
2 changes: 1 addition & 1 deletion examples/complete-network-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ main(int argc, char* argv[])
for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
{
Ptr<Node> node = *j;
Ptr<LoraNetDevice> loraNetDevice = node->GetDevice(0)->GetObject<LoraNetDevice>();
Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(node->GetDevice(0));
Ptr<LoraPhy> phy = loraNetDevice->GetPhy();
}

Expand Down
4 changes: 2 additions & 2 deletions examples/frame-counter-update.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ main(int argc, char* argv[])
for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
{
Ptr<Node> node = *j;
Ptr<LoraNetDevice> loraNetDevice = node->GetDevice(0)->GetObject<LoraNetDevice>();
Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(node->GetDevice(0));
Ptr<LoraPhy> phy = loraNetDevice->GetPhy();
Ptr<EndDeviceLorawanMac> mac = loraNetDevice->GetMac()->GetObject<EndDeviceLorawanMac>();
Ptr<EndDeviceLorawanMac> mac = DynamicCast<EndDeviceLorawanMac>(loraNetDevice->GetMac());
phy->TraceConnectWithoutContext("StartSending", MakeCallback(&OnPhySentPacket));
mac->TraceConnectWithoutContext("RequiredTransmissions", MakeCallback(&OnMacPacketOutcome));
}
Expand Down
4 changes: 2 additions & 2 deletions examples/network-server-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ main(int argc, char* argv[])
helper.Install(phyHelper, macHelper, endDevices);

// Set message type (Default is unconfirmed)
Ptr<LorawanMac> edMac1 = endDevices.Get(1)->GetDevice(0)->GetObject<LoraNetDevice>()->GetMac();
Ptr<ClassAEndDeviceLorawanMac> edLorawanMac1 = edMac1->GetObject<ClassAEndDeviceLorawanMac>();
Ptr<LorawanMac> edMac1 = DynamicCast<LoraNetDevice>(endDevices.Get(1)->GetDevice(0))->GetMac();
Ptr<ClassAEndDeviceLorawanMac> edLorawanMac1 = DynamicCast<ClassAEndDeviceLorawanMac>(edMac1);
edLorawanMac1->SetMType(LorawanMacHeader::CONFIRMED_DATA_UP);

// Install applications in end devices
Expand Down
7 changes: 2 additions & 5 deletions examples/parallel-reception-example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,8 @@ main(int argc, char* argv[])
******************/
for (uint32_t i = 0; i < endDevices.GetN(); i++)
{
endDevices.Get(i)
->GetDevice(0)
->GetObject<LoraNetDevice>()
->GetMac()
->GetObject<EndDeviceLorawanMac>()
DynamicCast<EndDeviceLorawanMac>(
DynamicCast<LoraNetDevice>(endDevices.Get(i)->GetDevice(0))->GetMac())
->SetDataRate(5 - i);
}

Expand Down
4 changes: 2 additions & 2 deletions helper/lora-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ LoraHelper::DoPrintDeviceStatus(NodeContainer endDevices,
Ptr<MobilityModel> position = object->GetObject<MobilityModel>();
NS_ASSERT(position);
Ptr<NetDevice> netDevice = object->GetDevice(0);
Ptr<LoraNetDevice> loraNetDevice = netDevice->GetObject<LoraNetDevice>();
Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(netDevice);
NS_ASSERT(loraNetDevice);
Ptr<ClassAEndDeviceLorawanMac> mac =
loraNetDevice->GetMac()->GetObject<ClassAEndDeviceLorawanMac>();
DynamicCast<ClassAEndDeviceLorawanMac>(loraNetDevice->GetMac());
int dr = int(mac->GetDataRate());
double txPower = mac->GetTransmissionPower();
Vector pos = position->GetPosition();
Expand Down
4 changes: 2 additions & 2 deletions helper/lora-phy-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ LoraPhyHelper::Create(Ptr<Node> node, Ptr<NetDevice> device) const

for (auto& f : frequencies)
{
phy->GetObject<SimpleGatewayLoraPhy>()->AddFrequency(f);
DynamicCast<SimpleGatewayLoraPhy>(phy)->AddFrequency(f);
}

int receptionPaths = 0;
// Set maxReceptionPaths as a parameter
// int maxReceptionPaths = 8;
while (receptionPaths < m_maxReceptionPaths)
{
phy->GetObject<SimpleGatewayLoraPhy>()->AddReceptionPath();
DynamicCast<SimpleGatewayLoraPhy>(phy)->AddReceptionPath();
receptionPaths++;
}
}
Expand Down
6 changes: 3 additions & 3 deletions helper/lora-radio-energy-model-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ LoraRadioEnergyModelHelper::DoInstall(Ptr<NetDevice> device, Ptr<EnergySource> s
NS_FATAL_ERROR("NetDevice type is not LoraNetDevice!");
}
Ptr<Node> node = device->GetNode();
Ptr<LoraRadioEnergyModel> model = m_radioEnergy.Create()->GetObject<LoraRadioEnergyModel>();
Ptr<LoraRadioEnergyModel> model = m_radioEnergy.Create<LoraRadioEnergyModel>();
NS_ASSERT(model);
// set energy source pointer
model->SetEnergySource(source);

// set energy depletion callback
// if none is specified, make a callback to EndDeviceLoraPhy::SetSleepMode
Ptr<LoraNetDevice> loraDevice = device->GetObject<LoraNetDevice>();
Ptr<EndDeviceLoraPhy> loraPhy = loraDevice->GetPhy()->GetObject<EndDeviceLoraPhy>();
Ptr<LoraNetDevice> loraDevice = DynamicCast<LoraNetDevice>(device);
Ptr<EndDeviceLoraPhy> loraPhy = DynamicCast<EndDeviceLoraPhy>(loraDevice->GetPhy());
// add model to device model list in energy source
source->AppendDeviceEnergyModel(model);
// create and register energy model phy listener
Expand Down
36 changes: 18 additions & 18 deletions helper/lorawan-mac-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ LorawanMacHelper::Create(Ptr<Node> node, Ptr<NetDevice> device) const
// If we are operating on an end device, add an address to it
if (m_deviceType == ED_A && m_addrGen)
{
mac->GetObject<ClassAEndDeviceLorawanMac>()->SetDeviceAddress(m_addrGen->NextAddress());
DynamicCast<ClassAEndDeviceLorawanMac>(mac)->SetDeviceAddress(m_addrGen->NextAddress());
}

// Add a basic list of channels based on the region where the device is
// operating
if (m_deviceType == ED_A)
{
Ptr<ClassAEndDeviceLorawanMac> edMac = mac->GetObject<ClassAEndDeviceLorawanMac>();
Ptr<ClassAEndDeviceLorawanMac> edMac = DynamicCast<ClassAEndDeviceLorawanMac>(mac);
switch (m_region)
{
case LorawanMacHelper::EU: {
Expand All @@ -101,7 +101,7 @@ LorawanMacHelper::Create(Ptr<Node> node, Ptr<NetDevice> device) const
}
else
{
Ptr<GatewayLorawanMac> gwMac = mac->GetObject<GatewayLorawanMac>();
Ptr<GatewayLorawanMac> gwMac = DynamicCast<GatewayLorawanMac>(mac);
switch (m_region)
{
case LorawanMacHelper::EU: {
Expand Down Expand Up @@ -171,7 +171,7 @@ LorawanMacHelper::ConfigureForAlohaRegion(Ptr<GatewayLorawanMac> gwMac) const
// ReceivePath configuration //
///////////////////////////////
Ptr<GatewayLoraPhy> gwPhy =
gwMac->GetDevice()->GetObject<LoraNetDevice>()->GetPhy()->GetObject<GatewayLoraPhy>();
DynamicCast<GatewayLoraPhy>(DynamicCast<LoraNetDevice>(gwMac->GetDevice())->GetPhy());

ApplyCommonAlohaConfigurations(gwMac);

Expand All @@ -184,7 +184,7 @@ LorawanMacHelper::ConfigureForAlohaRegion(Ptr<GatewayLorawanMac> gwMac) const
int maxReceptionPaths = 1;
while (receptionPaths < maxReceptionPaths)
{
gwPhy->GetObject<GatewayLoraPhy>()->AddReceptionPath();
DynamicCast<GatewayLoraPhy>(gwPhy)->AddReceptionPath();
receptionPaths++;
}
gwPhy->AddFrequency(868.1);
Expand Down Expand Up @@ -268,7 +268,7 @@ LorawanMacHelper::ConfigureForEuRegion(Ptr<GatewayLorawanMac> gwMac) const
// ReceivePath configuration //
///////////////////////////////
Ptr<GatewayLoraPhy> gwPhy =
gwMac->GetDevice()->GetObject<LoraNetDevice>()->GetPhy()->GetObject<GatewayLoraPhy>();
DynamicCast<GatewayLoraPhy>(DynamicCast<LoraNetDevice>(gwMac->GetDevice())->GetPhy());

ApplyCommonEuConfigurations(gwMac);

Expand All @@ -291,7 +291,7 @@ LorawanMacHelper::ConfigureForEuRegion(Ptr<GatewayLorawanMac> gwMac) const
int maxReceptionPaths = 8;
while (receptionPaths < maxReceptionPaths)
{
gwPhy->GetObject<GatewayLoraPhy>()->AddReceptionPath();
DynamicCast<GatewayLoraPhy>(gwPhy)->AddReceptionPath();
receptionPaths++;
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ LorawanMacHelper::ConfigureForSingleChannelRegion(Ptr<GatewayLorawanMac> gwMac)
// ReceivePath configuration //
///////////////////////////////
Ptr<GatewayLoraPhy> gwPhy =
gwMac->GetDevice()->GetObject<LoraNetDevice>()->GetPhy()->GetObject<GatewayLoraPhy>();
DynamicCast<GatewayLoraPhy>(DynamicCast<LoraNetDevice>(gwMac->GetDevice())->GetPhy());

ApplyCommonEuConfigurations(gwMac);

Expand All @@ -403,7 +403,7 @@ LorawanMacHelper::ConfigureForSingleChannelRegion(Ptr<GatewayLorawanMac> gwMac)
int maxReceptionPaths = 8;
while (receptionPaths < maxReceptionPaths)
{
gwPhy->GetObject<GatewayLoraPhy>()->AddReceptionPath();
gwPhy->AddReceptionPath();
receptionPaths++;
}
}
Expand Down Expand Up @@ -456,10 +456,10 @@ LorawanMacHelper::SetSpreadingFactorsUp(NodeContainer endDevices,
Ptr<MobilityModel> position = object->GetObject<MobilityModel>();
NS_ASSERT(position);
Ptr<NetDevice> netDevice = object->GetDevice(0);
Ptr<LoraNetDevice> loraNetDevice = netDevice->GetObject<LoraNetDevice>();
Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(netDevice);
NS_ASSERT(loraNetDevice);
Ptr<ClassAEndDeviceLorawanMac> mac =
loraNetDevice->GetMac()->GetObject<ClassAEndDeviceLorawanMac>();
DynamicCast<ClassAEndDeviceLorawanMac>(loraNetDevice->GetMac());
NS_ASSERT(mac);

// Try computing the distance from each gateway and find the best one
Expand All @@ -479,7 +479,7 @@ LorawanMacHelper::SetSpreadingFactorsUp(NodeContainer endDevices,
if (currentRxPower > highestRxPower)
{
bestGateway = curr;
bestGatewayPosition = curr->GetObject<MobilityModel>();
bestGatewayPosition = currPosition;
highestRxPower = currentRxPower;
}
}
Expand All @@ -488,7 +488,7 @@ LorawanMacHelper::SetSpreadingFactorsUp(NodeContainer endDevices,
double rxPower = highestRxPower;

// Get the end device sensitivity
Ptr<EndDeviceLoraPhy> edPhy = loraNetDevice->GetPhy()->GetObject<EndDeviceLoraPhy>();
Ptr<EndDeviceLoraPhy> edPhy = DynamicCast<EndDeviceLoraPhy>(loraNetDevice->GetPhy());
const double* edSensitivity = EndDeviceLoraPhy::sensitivity;

if (rxPower > *edSensitivity)
Expand Down Expand Up @@ -533,9 +533,9 @@ LorawanMacHelper::SetSpreadingFactorsUp(NodeContainer endDevices,
// Get the Gw sensitivity
Ptr<NetDevice> gatewayNetDevice = bestGateway->GetDevice (0);
Ptr<LoraNetDevice> gatewayLoraNetDevice = gatewayNetDevice->GetObject<LoraNetDevice> ();
Ptr<GatewayLoraPhy> gatewayPhy = gatewayLoraNetDevice->GetPhy ()->GetObject<GatewayLoraPhy>
(); const double *gwSensitivity = gatewayPhy->sensitivity;
Ptr<LoraNetDevice> gatewayLoraNetDevice = DynamicCast<LoraNetDevice>(gatewayNetDevice);
Ptr<GatewayLoraPhy> gatewayPhy = DynamicCast<GatewayLoraPhy>
(gatewayLoraNetDevice->GetPhy ()); const double *gwSensitivity = gatewayPhy->sensitivity;
if(rxPower > *gwSensitivity)
{
Expand Down Expand Up @@ -615,10 +615,10 @@ LorawanMacHelper::SetSpreadingFactorsGivenDistribution(NodeContainer endDevices,
Ptr<MobilityModel> position = object->GetObject<MobilityModel>();
NS_ASSERT(position);
Ptr<NetDevice> netDevice = object->GetDevice(0);
Ptr<LoraNetDevice> loraNetDevice = netDevice->GetObject<LoraNetDevice>();
Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(netDevice);
NS_ASSERT(loraNetDevice);
Ptr<ClassAEndDeviceLorawanMac> mac =
loraNetDevice->GetMac()->GetObject<ClassAEndDeviceLorawanMac>();
DynamicCast<ClassAEndDeviceLorawanMac>(loraNetDevice->GetMac());
NS_ASSERT(mac);

double prob = uniformRV->GetValue(0, 1);
Expand Down
26 changes: 13 additions & 13 deletions model/class-a-end-device-lorawan-mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ClassAEndDeviceLorawanMac::SendToPhy(Ptr<Packet> packetToSend)
//////////////////////////////

// Switch the PHY to the channel so that it will listen here for downlink
m_phy->GetObject<EndDeviceLoraPhy>()->SetFrequency(txChannel->GetFrequency());
DynamicCast<EndDeviceLoraPhy>(m_phy)->SetFrequency(txChannel->GetFrequency());

// Instruct the PHY on the right Spreading Factor to listen for during the window
// create a SetReplyDataRate function?
Expand All @@ -123,7 +123,7 @@ ClassAEndDeviceLorawanMac::SendToPhy(Ptr<Packet> packetToSend)
<< ", m_rx1DrOffset: " << unsigned(m_rx1DrOffset)
<< ", replyDataRate: " << unsigned(replyDataRate) << ".");

m_phy->GetObject<EndDeviceLoraPhy>()->SetSpreadingFactor(GetSfFromDataRate(replyDataRate));
DynamicCast<EndDeviceLoraPhy>(m_phy)->SetSpreadingFactor(GetSfFromDataRate(replyDataRate));
}

//////////////////////////
Expand Down Expand Up @@ -228,7 +228,7 @@ ClassAEndDeviceLorawanMac::Receive(Ptr<const Packet> packet)
}
}

m_phy->GetObject<EndDeviceLoraPhy>()->SwitchToSleep();
DynamicCast<EndDeviceLoraPhy>(m_phy)->SwitchToSleep();
}

void
Expand All @@ -237,7 +237,7 @@ ClassAEndDeviceLorawanMac::FailedReception(Ptr<const Packet> packet)
NS_LOG_FUNCTION(this << packet);

// Switch to sleep after a failed reception
m_phy->GetObject<EndDeviceLoraPhy>()->SwitchToSleep();
DynamicCast<EndDeviceLoraPhy>(m_phy)->SwitchToSleep();

if (m_secondReceiveWindow.IsExpired() && m_retxParams.waitingAck)
{
Expand Down Expand Up @@ -282,7 +282,7 @@ ClassAEndDeviceLorawanMac::TxFinished(Ptr<const Packet> packet)
// this);

// Switch the PHY to sleep
m_phy->GetObject<EndDeviceLoraPhy>()->SwitchToSleep();
DynamicCast<EndDeviceLoraPhy>(m_phy)->SwitchToSleep();
}

void
Expand All @@ -291,7 +291,7 @@ ClassAEndDeviceLorawanMac::OpenFirstReceiveWindow()
NS_LOG_FUNCTION_NOARGS();

// Set Phy in Standby mode
m_phy->GetObject<EndDeviceLoraPhy>()->SwitchToStandby();
DynamicCast<EndDeviceLoraPhy>(m_phy)->SwitchToStandby();

// Calculate the duration of a single symbol for the first receive window data rate
double tSym = pow(2, GetSfFromDataRate(GetFirstReceiveWindowDataRate())) /
Expand All @@ -310,7 +310,7 @@ ClassAEndDeviceLorawanMac::CloseFirstReceiveWindow()
{
NS_LOG_FUNCTION_NOARGS();

Ptr<EndDeviceLoraPhy> phy = m_phy->GetObject<EndDeviceLoraPhy>();
Ptr<EndDeviceLoraPhy> phy = DynamicCast<EndDeviceLoraPhy>(m_phy);

// Check the Phy layer's state:
// - RX -> We are receiving a preamble.
Expand Down Expand Up @@ -341,22 +341,22 @@ ClassAEndDeviceLorawanMac::OpenSecondReceiveWindow()

// Check for receiver status: if it's locked on a packet, don't open this
// window at all.
if (m_phy->GetObject<EndDeviceLoraPhy>()->GetState() == EndDeviceLoraPhy::RX)
if (DynamicCast<EndDeviceLoraPhy>(m_phy)->GetState() == EndDeviceLoraPhy::RX)
{
NS_LOG_INFO("Won't open second receive window since we are in RX mode.");

return;
}

// Set Phy in Standby mode
m_phy->GetObject<EndDeviceLoraPhy>()->SwitchToStandby();
DynamicCast<EndDeviceLoraPhy>(m_phy)->SwitchToStandby();

// Switch to appropriate channel and data rate
NS_LOG_INFO("Using parameters: " << m_secondReceiveWindowFrequency << "Hz, DR"
<< unsigned(m_secondReceiveWindowDataRate));

m_phy->GetObject<EndDeviceLoraPhy>()->SetFrequency(m_secondReceiveWindowFrequency);
m_phy->GetObject<EndDeviceLoraPhy>()->SetSpreadingFactor(
DynamicCast<EndDeviceLoraPhy>(m_phy)->SetFrequency(m_secondReceiveWindowFrequency);
DynamicCast<EndDeviceLoraPhy>(m_phy)->SetSpreadingFactor(
GetSfFromDataRate(m_secondReceiveWindowDataRate));

// Calculate the duration of a single symbol for the second receive window data rate
Expand All @@ -376,7 +376,7 @@ ClassAEndDeviceLorawanMac::CloseSecondReceiveWindow()
{
NS_LOG_FUNCTION_NOARGS();

Ptr<EndDeviceLoraPhy> phy = m_phy->GetObject<EndDeviceLoraPhy>();
Ptr<EndDeviceLoraPhy> phy = DynamicCast<EndDeviceLoraPhy>(m_phy);

// NS_ASSERT (phy->m_state != EndDeviceLoraPhy::TX &&
// phy->m_state != EndDeviceLoraPhy::SLEEP);
Expand Down Expand Up @@ -410,7 +410,7 @@ ClassAEndDeviceLorawanMac::CloseSecondReceiveWindow()
}

else if (m_retxParams.retxLeft == 0 &&
m_phy->GetObject<EndDeviceLoraPhy>()->GetState() != EndDeviceLoraPhy::RX)
DynamicCast<EndDeviceLoraPhy>(m_phy)->GetState() != EndDeviceLoraPhy::RX)
{
uint8_t txs = m_maxNumbTx - (m_retxParams.retxLeft);
m_requiredTxCallback(txs, false, m_retxParams.firstAttempt, m_retxParams.packet);
Expand Down
Loading

0 comments on commit 82ea58a

Please sign in to comment.