34#include "ns3/net-device.h"
35#include "ns3/node-container.h"
36#include "ns3/packet.h"
37#include "ns3/pointer.h"
38#include "ns3/singleton.h"
47NS_LOG_COMPONENT_DEFINE(
"LoraNetworkStatus");
55 TypeId(
"ns3::LoraNetworkStatus").SetParent<Object>().AddConstructor<LoraNetworkStatus>();
61 NS_LOG_FUNCTION_NOARGS();
64 m_uniform = CreateObject<UniformRandomVariable>();
69 NS_LOG_FUNCTION_NOARGS();
75 NS_LOG_FUNCTION(
this << edMac);
82 Ptr<LoraEndDeviceStatus> edStatus =
83 CreateObject<LoraEndDeviceStatus>(edAddress,
89 NS_LOG_DEBUG(
"Added to the list a device with address " << edAddress.
Print());
96 NS_LOG_FUNCTION(
this << address << gwStatus);
104 m_gatewayStatuses.insert(std::pair<Address, Ptr<LoraGatewayStatus>>(address, gwStatus));
105 NS_LOG_DEBUG(
"Added to the list a gateway with address " << address);
109 Ptr<PointToPointNetDevice> p2pNetDevice;
110 for (uint32_t i = 0; i < gw->GetNDevices(); i++)
112 p2pNetDevice = gw->GetDevice(i)->GetObject<PointToPointNetDevice>();
113 if (p2pNetDevice !=
nullptr)
119 m_gws.insert(std::make_pair(gw, p2pNetDevice));
125 NS_LOG_FUNCTION(
this << packet << gwAddress);
128 Ptr<Packet> myPacket = packet->Copy();
132 myPacket->RemoveHeader(macHdr);
135 myPacket->RemoveHeader(frameHdr);
139 NS_LOG_DEBUG(
"Node address: " << edAddr);
153 NS_LOG_FUNCTION(
this << deviceAddress << window);
157 double replyFrequency;
160 replyFrequency = edStatus->GetFirstReceiveWindowFrequency();
162 else if (window == 2)
164 replyFrequency = edStatus->GetSecondReceiveWindowFrequency();
168 NS_ABORT_MSG(
"Invalid window value");
175 std::map<double, Address> gwAddresses = edStatus->GetPowerGatewayMap();
179 uint8_t beamToUse = edStatus->GetBeamId();
181 std::vector<Address> possibleAddresses;
182 Ptr<SatLorawanNetDevice> lorawanNetDevice;
183 for (std::map<Ptr<Node>, Ptr<PointToPointNetDevice>>::iterator it =
m_gws.begin();
188 for (uint32_t i = 0; i < gateway->GetNDevices(); i++)
191 if (lorawanNetDevice !=
nullptr)
193 uint8_t beam = lorawanNetDevice->GetMac()->GetBeamId();
194 if (beam == beamToUse)
196 possibleAddresses.push_back(it->second->GetAddress());
201 if (possibleAddresses.size() > 0)
203 return possibleAddresses[
m_uniform->GetInteger() % possibleAddresses.size()];
207 return gwAddresses.begin()->second;
214 Address bestGwAddress;
215 for (
auto it = gwAddresses.rbegin(); it != gwAddresses.rend(); it++)
218 m_gatewayStatuses.find(it->second)->second->IsAvailableForTransmission(replyFrequency);
221 bestGwAddress = it->second;
226 return bestGwAddress;
232 NS_LOG_FUNCTION(packet << gwAddress);
234 m_gatewayStatuses.find(gwAddress)->second->GetNetDevice()->Send(packet, gwAddress, 0x0800);
242 Ptr<Packet> packet = edStatus->GetCompleteReplyPacket();
247 switch (windowNumber)
250 tag.
SetDataRate(edStatus->GetMac()->GetFirstReceiveWindowDataRate());
251 tag.
SetFrequency(edStatus->GetFirstReceiveWindowFrequency());
255 tag.
SetDataRate(edStatus->GetMac()->GetSecondReceiveWindowDataRate());
256 tag.
SetFrequency(edStatus->GetSecondReceiveWindowFrequency());
261 packet->AddPacketTag(tag);
265 beamTag.
SetBeamId(edStatus->GetBeamId());
266 packet->AddPacketTag(beamTag);
270Ptr<LoraEndDeviceStatus>
273 NS_LOG_FUNCTION(
this << packet);
278 Ptr<Packet> myPacket = packet->Copy();
279 myPacket->RemoveHeader(mHdr);
280 myPacket->RemoveHeader(fHdr);
288 NS_LOG_ERROR(
"EndDeviceStatus not found");
293Ptr<LoraEndDeviceStatus>
296 NS_LOG_FUNCTION(
this << address);
305 NS_LOG_ERROR(
"EndDeviceStatus not found");
313 NS_LOG_FUNCTION(
this);
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
void SetBeamId(uint8_t beamId)
Set which beamId this packet was transmitted with.
This class represents the device address of a LoraWAN End Device.
std::string Print(void) const
Print the address bit-by-bit to a human-readable string.
This class represents the knowledge about the state of the network that is available at the Network S...
void AddNode(Ptr< LorawanMacEndDeviceClassA > edMac)
Add a device to the ones that are tracked by this LoraNetworkStatus object.
void OnReceivedPacket(Ptr< const Packet > packet, const Address &gwaddress)
Update network status on the received packet.
std::map< Ptr< Node >, Ptr< PointToPointNetDevice > > m_gws
void AddGateway(Ptr< Node > gw, Address &address, Ptr< LoraGatewayStatus > gwStatus)
Add this gateway to the list of gateways connected to the network.
Ptr< Packet > GetReplyForDevice(LoraDeviceAddress edAddress, int windowNumber)
Get the reply for the specified device address.
Address GetBestGatewayForDevice(LoraDeviceAddress deviceAddress, int window)
Return whether we have a gateway that is available to send a reply to the specified device.
static TypeId GetTypeId(void)
Ptr< LoraEndDeviceStatus > GetEndDeviceStatus(Ptr< const Packet > packet)
Get the EndDeviceStatus for the device that sent a packet.
Ptr< UniformRandomVariable > m_uniform
SatEnums::RegenerationMode_t m_forwardLinkRegenerationMode
bool NeedsReply(LoraDeviceAddress deviceAddress)
Return whether the specified device needs a reply.
int CountEndDevices(void)
Return the number of end devices currently managed by the server.
void SendThroughGateway(Ptr< Packet > packet, Address gwAddress)
Send a packet through a Gateway.
virtual ~LoraNetworkStatus()
std::map< Address, Ptr< LoraGatewayStatus > > m_gatewayStatuses
std::map< LoraDeviceAddress, Ptr< LoraEndDeviceStatus > > m_endDeviceStatuses
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
void SetDataRate(uint8_t dataRate)
Set the data rate for this packet.
void SetFrequency(double frequency)
Set the frequency of the packet.
void SetModcod(uint8_t modcod)
Set the modcod for this packet.
Class representing the MAC layer of a Class A LoRaWAN device.
SatLorawanNetDevice to be utilized in the UT and GW nodes for IoT configuration.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.