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>
47 NS_LOG_COMPONENT_DEFINE(
"LoraNetworkStatus");
49 NS_OBJECT_ENSURE_REGISTERED(LoraNetworkStatus);
54 static TypeId tid = TypeId(
"ns3::LoraNetworkStatus").AddConstructor<
LoraNetworkStatus>();
60 NS_LOG_FUNCTION_NOARGS();
63 m_uniform = CreateObject<UniformRandomVariable>();
68 NS_LOG_FUNCTION_NOARGS();
74 NS_LOG_FUNCTION(
this << edMac);
81 Ptr<LoraEndDeviceStatus> edStatus =
82 CreateObject<LoraEndDeviceStatus>(edAddress,
88 NS_LOG_DEBUG(
"Added to the list a device with address " << edAddress.
Print());
95 NS_LOG_FUNCTION(
this << address << gwStatus);
103 m_gatewayStatuses.insert(std::pair<Address, Ptr<LoraGatewayStatus>>(address, gwStatus));
104 NS_LOG_DEBUG(
"Added to the list a gateway with address " << address);
108 Ptr<PointToPointNetDevice> p2pNetDevice;
109 for (uint32_t i = 0; i < gw->GetNDevices(); i++)
111 p2pNetDevice = gw->GetDevice(i)->GetObject<PointToPointNetDevice>();
112 if (p2pNetDevice !=
nullptr)
118 m_gws.insert(std::make_pair(gw, p2pNetDevice));
124 NS_LOG_FUNCTION(
this << packet << gwAddress);
127 Ptr<Packet> myPacket = packet->Copy();
131 myPacket->RemoveHeader(macHdr);
134 myPacket->RemoveHeader(frameHdr);
138 NS_LOG_DEBUG(
"Node address: " << edAddr);
152 NS_LOG_FUNCTION(
this << deviceAddress << window);
156 double replyFrequency;
159 replyFrequency = edStatus->GetFirstReceiveWindowFrequency();
161 else if (window == 2)
163 replyFrequency = edStatus->GetSecondReceiveWindowFrequency();
167 NS_ABORT_MSG(
"Invalid window value");
174 std::map<double, Address> gwAddresses = edStatus->GetPowerGatewayMap();
178 uint8_t beamToUse = edStatus->GetBeamId();
180 std::vector<Address> possibleAddresses;
181 Ptr<SatLorawanNetDevice> lorawanNetDevice;
182 for (std::map<Ptr<Node>, Ptr<PointToPointNetDevice>>::iterator it =
m_gws.begin();
187 for (uint32_t i = 0; i < gateway->GetNDevices(); i++)
190 if (lorawanNetDevice !=
nullptr)
192 uint8_t beam = lorawanNetDevice->
GetMac()->GetBeamId();
193 if (beam == beamToUse)
195 possibleAddresses.push_back(it->second->GetAddress());
200 if (possibleAddresses.size() > 0)
202 return possibleAddresses[
m_uniform->GetInteger() % possibleAddresses.size()];
206 return gwAddresses.begin()->second;
213 Address bestGwAddress;
214 for (
auto it = gwAddresses.rbegin(); it != gwAddresses.rend(); it++)
217 m_gatewayStatuses.find(it->second)->second->IsAvailableForTransmission(replyFrequency);
220 bestGwAddress = it->second;
225 return bestGwAddress;
231 NS_LOG_FUNCTION(packet << gwAddress);
233 m_gatewayStatuses.find(gwAddress)->second->GetNetDevice()->Send(packet, gwAddress, 0x0800);
241 Ptr<Packet> packet = edStatus->GetCompleteReplyPacket();
246 switch (windowNumber)
249 tag.
SetDataRate(edStatus->GetMac()->GetFirstReceiveWindowDataRate());
250 tag.
SetFrequency(edStatus->GetFirstReceiveWindowFrequency());
254 tag.
SetDataRate(edStatus->GetMac()->GetSecondReceiveWindowDataRate());
255 tag.
SetFrequency(edStatus->GetSecondReceiveWindowFrequency());
260 packet->AddPacketTag(tag);
264 beamTag.
SetBeamId(edStatus->GetBeamId());
265 packet->AddPacketTag(beamTag);
269 Ptr<LoraEndDeviceStatus>
272 NS_LOG_FUNCTION(
this << packet);
277 Ptr<Packet> myPacket = packet->Copy();
278 myPacket->RemoveHeader(mHdr);
279 myPacket->RemoveHeader(fHdr);
287 NS_LOG_ERROR(
"EndDeviceStatus not found");
292 Ptr<LoraEndDeviceStatus>
295 NS_LOG_FUNCTION(
this << address);
304 NS_LOG_ERROR(
"EndDeviceStatus not found");
312 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.
Ptr< SatMac > GetMac(void) const
Get a Mac pointer.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.