A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
network-status.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 University of Padova
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Davide Magrin <magrinda@dei.unipd.it>
7 * Martina Capuzzo <capuzzom@dei.unipd.it>
8 */
9
10#include "network-status.h"
11
13#include "lora-tag.h"
14
15namespace ns3
16{
17namespace lorawan
18{
19
20NS_LOG_COMPONENT_DEFINE("NetworkStatus");
21
23
24TypeId
26{
27 static TypeId tid = TypeId("ns3::NetworkStatus")
29 .AddConstructor<NetworkStatus>()
30 .SetGroupName("lorawan");
31 return tid;
32}
33
38
43
44void
46{
47 NS_LOG_FUNCTION(this << edMac);
48
49 // Check whether this device already exists in our list
50 LoraDeviceAddress edAddress = edMac->GetDeviceAddress();
51 if (m_endDeviceStatuses.find(edAddress) == m_endDeviceStatuses.end())
52 {
53 // The device doesn't exist. Create new EndDeviceStatus
54 Ptr<EndDeviceStatus> edStatus =
56
57 // Add it to the map
59 std::pair<LoraDeviceAddress, Ptr<EndDeviceStatus>>(edAddress, edStatus));
60 NS_LOG_DEBUG("Added to the list a device with address " << edAddress.Print());
61 }
62}
63
64void
66{
67 NS_LOG_FUNCTION(this);
68
69 // Check whether this device already exists in the list
70 if (m_gatewayStatuses.find(address) == m_gatewayStatuses.end())
71 {
72 // The device doesn't exist.
73
74 // Add it to the map
75 m_gatewayStatuses.insert(std::pair<Address, Ptr<GatewayStatus>>(address, gwStatus));
76 NS_LOG_DEBUG("Added to the list a gateway with address " << address);
77 }
78}
79
80void
82{
83 NS_LOG_FUNCTION(this << packet << gwAddress);
84
85 // Create a copy of the packet
86 Ptr<Packet> myPacket = packet->Copy();
87
88 // Extract the headers
89 LorawanMacHeader macHdr;
90 myPacket->RemoveHeader(macHdr);
91 LoraFrameHeader frameHdr;
92 frameHdr.SetAsUplink();
93 myPacket->RemoveHeader(frameHdr);
94
95 // Update the correct EndDeviceStatus object
96 LoraDeviceAddress edAddr = frameHdr.GetAddress();
97 NS_LOG_DEBUG("Node address: " << edAddr);
98 m_endDeviceStatuses.at(edAddr)->InsertReceivedPacket(packet, gwAddress);
99}
100
101bool
103{
104 // Throws out of range if no device is found
105 return m_endDeviceStatuses.at(deviceAddress)->NeedsReply();
106}
107
110{
111 // Get the endDeviceStatus we are interested in
112 Ptr<EndDeviceStatus> edStatus = m_endDeviceStatuses.at(deviceAddress);
113 uint32_t replyFrequency;
114 if (window == 1)
115 {
116 replyFrequency = edStatus->GetFirstReceiveWindowFrequency();
117 }
118 else if (window == 2)
119 {
120 replyFrequency = edStatus->GetSecondReceiveWindowFrequency();
121 }
122 else
123 {
124 NS_ABORT_MSG("Invalid window value");
125 }
126
127 // Get the list of gateways that this device can reach
128 // NOTE: At this point, we could also take into account the whole network to
129 // identify the best gateway according to various metrics. For now, we just
130 // ask the EndDeviceStatus to pick the best gateway for us via its method.
131 std::map<double, Address> gwAddresses = edStatus->GetPowerGatewayMap();
132
133 // By iterating on the map in reverse, we go from the 'best'
134 // gateway, i.e. the one with the highest received power, to the
135 // worst.
136 Address bestGwAddress;
137 for (auto it = gwAddresses.rbegin(); it != gwAddresses.rend(); it++)
138 {
139 bool isAvailable =
140 m_gatewayStatuses.find(it->second)->second->IsAvailableForTransmission(replyFrequency);
141 if (isAvailable)
142 {
143 bestGwAddress = it->second;
144 break;
145 }
146 }
147
148 return bestGwAddress;
149}
150
151void
153{
154 NS_LOG_FUNCTION(packet << gwAddress);
155
156 m_gatewayStatuses.find(gwAddress)->second->GetNetDevice()->Send(packet, gwAddress, 0x0800);
157}
158
161{
162 // Get the reply packet
163 Ptr<EndDeviceStatus> edStatus = m_endDeviceStatuses.find(edAddress)->second;
164 Ptr<Packet> packet = edStatus->GetCompleteReplyPacket();
165
166 // Apply the appropriate tag
167 LoraTag tag;
168 switch (windowNumber)
169 {
170 case 1:
171 tag.SetDataRate(edStatus->GetMac()->GetFirstReceiveWindowDataRate());
172 tag.SetFrequency(edStatus->GetFirstReceiveWindowFrequency());
173 break;
174 case 2:
175 tag.SetDataRate(edStatus->GetMac()->GetSecondReceiveWindowDataRate());
176 tag.SetFrequency(edStatus->GetSecondReceiveWindowFrequency());
177 break;
178 }
179
180 packet->AddPacketTag(tag);
181 return packet;
182}
183
186{
187 NS_LOG_FUNCTION(this << packet);
188
189 // Get the address
190 LorawanMacHeader mHdr;
191 LoraFrameHeader fHdr;
192 Ptr<Packet> myPacket = packet->Copy();
193 myPacket->RemoveHeader(mHdr);
194 myPacket->RemoveHeader(fHdr);
195 auto it = m_endDeviceStatuses.find(fHdr.GetAddress());
196 if (it != m_endDeviceStatuses.end())
197 {
198 return (*it).second;
199 }
200 else
201 {
202 NS_LOG_ERROR("EndDeviceStatus not found");
203 return nullptr;
204 }
205}
206
209{
210 NS_LOG_FUNCTION(this << address);
211
212 auto it = m_endDeviceStatuses.find(address);
213 if (it != m_endDeviceStatuses.end())
214 {
215 return (*it).second;
216 }
217 else
218 {
219 NS_LOG_ERROR("EndDeviceStatus not found");
220 return nullptr;
221 }
222}
223
224int
226{
227 NS_LOG_FUNCTION(this);
228
229 return m_endDeviceStatuses.size();
230}
231
232} // namespace lorawan
233} // namespace ns3
a polymophic address class
Definition address.h:114
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
This class represents the device address of a LoraWAN end device.
std::string Print() const
Print the address bit-by-bit to a human-readable string.
This class represents the Frame header (FHDR) used in a LoraWAN network.
void SetAsUplink()
State that this is an uplink message.
LoraDeviceAddress GetAddress() const
Get this header's device address value.
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
Definition lora-tag.h:26
void SetFrequency(uint32_t frequencyHz)
Set the frequency of the packet.
Definition lora-tag.cc:116
void SetDataRate(uint8_t dataRate)
Set the data rate for this packet.
Definition lora-tag.cc:134
This class represents the Mac header of a LoRaWAN packet.
This class represents the knowledge about the state of the network that is available at the network s...
~NetworkStatus() override
Destructor.
Ptr< EndDeviceStatus > GetEndDeviceStatus(Ptr< const Packet > packet)
Get the EndDeviceStatus of the device that sent a packet.
void OnReceivedPacket(Ptr< const Packet > packet, const Address &gwaddress)
Update network status on a received packet.
NetworkStatus()
Default constructor.
bool NeedsReply(LoraDeviceAddress deviceAddress)
Return whether the specified device needs a reply.
void AddNode(Ptr< ClassAEndDeviceLorawanMac > edMac)
Add a device to the ones that are tracked by this NetworkStatus object.
int CountEndDevices()
Return the number of end devices currently managed by the server.
std::map< LoraDeviceAddress, Ptr< EndDeviceStatus > > m_endDeviceStatuses
Map tracking the state of devices connected to this network server.
void SendThroughGateway(Ptr< Packet > packet, Address gwAddress)
Send a packet through a gateway.
std::map< Address, Ptr< GatewayStatus > > m_gatewayStatuses
Map tracking the state of gateways connected to this network server.
void AddGateway(Address &address, Ptr< GatewayStatus > gwStatus)
Add a new gateway to the list of gateways connected to the network.
static TypeId GetTypeId()
Register this type.
Ptr< Packet > GetReplyForDevice(LoraDeviceAddress edAddress, int windowNumber)
Get the reply packet prepared for a reception window of a device.
Address GetBestGatewayForDevice(LoraDeviceAddress deviceAddress, int window)
Return whether we have a gateway that is available to send a reply to the specified device.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:246
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:605