A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
end-device-status.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Martina Capuzzo <capuzzom@dei.unipd.it>
7 * Davide Magrin <magrinda@dei.unipd.it>
8 */
9
10#include "end-device-status.h"
11
13#include "lora-tag.h"
14
15#include "ns3/simulator.h"
16
17namespace ns3
18{
19namespace lorawan
20{
21
22NS_LOG_COMPONENT_DEFINE("EndDeviceStatus");
23
24TypeId
26{
27 static TypeId tid = TypeId("ns3::EndDeviceStatus")
29 .AddConstructor<EndDeviceStatus>()
30 .SetGroupName("lorawan");
31 return tid;
32}
33
37 m_endDeviceAddress(endDeviceAddress),
39 m_mac(endDeviceMac)
40{
41 NS_LOG_FUNCTION(endDeviceAddress);
42}
43
52
57
58///////////////
59// Getters //
60///////////////
61
62uint8_t
69
77
78uint8_t
85
92
95{
97
98 // Start from reply payload
99 Ptr<Packet> replyPacket;
100 if (m_reply.payload) // If it has APP data to send
101 {
102 NS_LOG_DEBUG("Crafting reply packet from existing payload");
103 replyPacket = m_reply.payload->Copy();
104 }
105 else // If no APP data needs to be sent, use an empty payload
106 {
107 NS_LOG_DEBUG("Crafting reply packet using an empty payload");
108 replyPacket = Create<Packet>(0);
109 }
110
111 // Add headers
112 m_reply.frameHeader.SetAddress(m_endDeviceAddress);
113 Ptr<Packet> lastPacket = GetLastPacketReceivedFromDevice()->Copy();
114 LorawanMacHeader mHdr;
115 LoraFrameHeader fHdr;
116 fHdr.SetAsUplink();
117 lastPacket->RemoveHeader(mHdr);
118 lastPacket->RemoveHeader(fHdr);
119 m_reply.frameHeader.SetFCnt(fHdr.GetFCnt());
121 replyPacket->AddHeader(m_reply.frameHeader);
122 replyPacket->AddHeader(m_reply.macHeader);
123
124 NS_LOG_DEBUG("Added MAC header" << m_reply.macHeader);
125 NS_LOG_DEBUG("Added frame header" << m_reply.frameHeader);
126
127 return replyPacket;
128}
129
130bool
132{
134
135 return m_reply.needsReply;
136}
137
140{
142 return m_reply.macHeader;
143}
144
147{
149 return m_reply.frameHeader;
150}
151
154{
156 return m_reply.payload->Copy();
157}
158
161{
162 return m_mac;
163}
164
171
172void
178
179void
185
186void
192
193void
199
200void
202{
204 m_reply.macHeader = macHeader;
205}
206
207void
209{
211 m_reply.frameHeader = frameHeader;
212}
213
214void
216{
218 m_reply.payload = replyPayload;
219}
220
221///////////////////////
222// Other methods //
223///////////////////////
224
225void
227{
229
230 // Create a copy of the packet
231 Ptr<Packet> myPacket = receivedPacket->Copy();
232
233 // Extract the headers
234 LorawanMacHeader macHdr;
235 myPacket->RemoveHeader(macHdr);
236
237 LoraFrameHeader frameHdr;
238 frameHdr.SetAsUplink();
239 myPacket->RemoveHeader(frameHdr);
240
241 // Update current parameters
242 LoraTag tag;
243 myPacket->RemovePacketTag(tag);
246
247 // Update Information on the received packet
249 info.sf = tag.GetSpreadingFactor();
250 info.frequencyHz = tag.GetFrequency();
251 info.packet = receivedPacket;
252
253 double rcvPower = tag.GetReceivePower();
254
255 // Perform insertion in list, also checking that the packet isn't already in
256 // the list (it could have been already received by another gateway)
257
258 // Start searching from the end
259 auto it = m_receivedPacketList.rbegin();
260 for (; it != m_receivedPacketList.rend(); it++)
261 {
262 // Get the frame counter of the current packet to compare it with the
263 // newly received one
264 Ptr<Packet> packetCopy = ((*it).first)->Copy();
265 LorawanMacHeader currentMacHdr;
266 packetCopy->RemoveHeader(currentMacHdr);
267 LoraFrameHeader currentFrameHdr;
268 frameHdr.SetAsUplink();
269 packetCopy->RemoveHeader(currentFrameHdr);
270
271 NS_LOG_DEBUG("Received packet's frame counter: " << unsigned(frameHdr.GetFCnt())
272 << "\nCurrent packet's frame counter: "
273 << unsigned(currentFrameHdr.GetFCnt()));
274
275 if (frameHdr.GetFCnt() == currentFrameHdr.GetFCnt())
276 {
277 NS_LOG_INFO("Packet was already received by another gateway");
278
279 // This packet had already been received from another gateway:
280 // add this gateway's reception information.
281 GatewayList& gwList = it->second.gwList;
282
283 PacketInfoPerGw gwInfo;
284 gwInfo.receivedTime = Now();
285 gwInfo.rxPower = rcvPower;
286 gwInfo.gwAddress = gwAddress;
287 gwList.insert(std::pair<Address, PacketInfoPerGw>(gwAddress, gwInfo));
288
289 NS_LOG_DEBUG("Size of gateway list: " << gwList.size());
290
291 break; // Exit from the cycle
292 }
293 }
294 if (it == m_receivedPacketList.rend())
295 {
296 NS_LOG_INFO("Packet was received for the first time");
297 PacketInfoPerGw gwInfo;
298 gwInfo.receivedTime = Now();
299 gwInfo.rxPower = rcvPower;
300 gwInfo.gwAddress = gwAddress;
301 info.gwList.insert(std::pair<Address, PacketInfoPerGw>(gwAddress, gwInfo));
302 m_receivedPacketList.emplace_back(receivedPacket, info);
303 }
304 NS_LOG_DEBUG(*this);
305}
306
309{
311 auto it = m_receivedPacketList.rbegin();
312 if (it != m_receivedPacketList.rend())
313 {
314 return it->second;
315 }
316 else
317 {
319 }
320}
321
324{
326 auto it = m_receivedPacketList.rbegin();
327 if (it != m_receivedPacketList.rend())
328 {
329 return it->first;
330 }
331 else
332 {
333 return nullptr;
334 }
335}
336
337void
339{
341 m_reply = Reply();
342 m_reply.needsReply = false;
343}
344
345void
347{
348 m_reply.frameHeader.AddCommand(macCommand);
349}
350
351bool
356
357void
362
363void
368
369std::map<double, Address>
371{
372 // Create a map of the gateways
373 // Key: received power
374 // Value: address of the corresponding gateway
375 ReceivedPacketInfo info = m_receivedPacketList.back().second;
376 GatewayList gwList = info.gwList;
377
378 std::map<double, Address> gatewayPowers;
379
380 for (auto it = gwList.begin(); it != gwList.end(); it++)
381 {
382 Address currentGwAddress = (*it).first;
383 double currentRxPower = (*it).second.rxPower;
384 gatewayPowers.insert(std::pair<double, Address>(currentRxPower, currentGwAddress));
385 }
386
387 return gatewayPowers;
388}
389
390std::ostream&
391operator<<(std::ostream& os, const EndDeviceStatus& status)
392{
393 os << "Total packets received: " << status.m_receivedPacketList.size() << std::endl;
394
395 for (auto j = status.m_receivedPacketList.begin(); j != status.m_receivedPacketList.end(); j++)
396 {
397 EndDeviceStatus::ReceivedPacketInfo info = (*j).second;
398 EndDeviceStatus::GatewayList gatewayList = info.gwList;
399 Ptr<const Packet> pkt = (*j).first;
400 os << pkt << " " << gatewayList.size() << std::endl;
401 for (auto k = gatewayList.begin(); k != gatewayList.end(); k++)
402 {
403 EndDeviceStatus::PacketInfoPerGw infoPerGw = (*k).second;
404 os << " " << infoPerGw.gwAddress << " " << infoPerGw.rxPower << std::endl;
405 }
406 }
407
408 return os;
409}
410
411} // namespace lorawan
412} // namespace ns3
a polymophic address class
Definition address.h:114
An identifier for simulation events.
Definition event-id.h:45
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
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:268
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
EndDeviceStatus::ReceivedPacketInfo GetLastReceivedPacketInfo()
Return the information about the last packet that was received from the device.
uint8_t m_secondReceiveWindowSpreadingFactor
Spreading Factor (SF) for RX2 window.
bool HasReceiveWindowOpportunityScheduled()
Check if there is already a running reception window event scheduled for this end device.
void SetReplyMacHeader(LorawanMacHeader macHeader)
Set the reply packet mac header.
void SetReplyFrameHeader(LoraFrameHeader frameHeader)
Set the reply packet frame header.
static TypeId GetTypeId()
Register this type.
Ptr< Packet > GetReplyPayload()
Get the data of the reply packet.
std::list< std::pair< Ptr< const Packet >, ReceivedPacketInfo > > ReceivedPacketList
typedef of a list of packets paired to their reception info.
uint32_t GetFirstReceiveWindowFrequency() const
Get the first window frequency of this device.
void SetSecondReceiveWindowSpreadingFactor(uint8_t sf)
Set the spreading factor this device is using in the second receive window.
LorawanMacHeader GetReplyMacHeader() const
Get the reply packet mac header.
uint8_t m_firstReceiveWindowSpreadingFactor
Spreading Factor (SF) for RX1 window.
void InsertReceivedPacket(Ptr< const Packet > receivedPacket, const Address &gwAddress)
Insert a received packet in the packet list.
EventId m_receiveWindowEvent
Event storing the next scheduled downlink transmission.
struct Reply m_reply
Next reply intended for this device.
LoraDeviceAddress m_endDeviceAddress
The address of this device.
bool NeedsReply() const
Whether the end device needs a reply.
LoraFrameHeader GetReplyFrameHeader() const
Get the reply packet frame header.
uint32_t GetSecondReceiveWindowFrequency() const
Return the second window frequency of this device.
std::map< Address, PacketInfoPerGw > GatewayList
typedef of a list of gateways with relative reception information.
ReceivedPacketList m_receivedPacketList
List of received packets.
void RemoveReceiveWindowOpportunity()
Cancel next scheduled reception window event.
void SetReceiveWindowOpportunity(EventId event)
Store next scheduled reception window event.
ReceivedPacketList GetReceivedPacketList() const
Get the received packet list.
void SetFirstReceiveWindowFrequency(uint32_t frequencyHz)
Set the first window frequency of this device.
Ptr< const Packet > GetLastPacketReceivedFromDevice()
Return the last packet that was received from this device.
void SetReplyPayload(Ptr< Packet > replyPayload)
Set the packet reply payload.
uint32_t m_firstReceiveWindowFrequencyHz
Frequency [Hz] for RX1 window.
void AddMACCommand(Ptr< MacCommand > macCommand)
Add MAC command to the frame header of next reply.
uint8_t GetFirstReceiveWindowSpreadingFactor() const
Get the spreading factor this device is using in the first receive window.
void SetFirstReceiveWindowSpreadingFactor(uint8_t sf)
Set the spreading factor this device is using in the first receive window.
Ptr< ClassAEndDeviceLorawanMac > m_mac
Pointer to the MAC layer of this device.
void InitializeReply()
Reset the next reply state.
std::map< double, Address > GetPowerGatewayMap()
Get the gateways which received the last packet from the end device.
uint8_t GetSecondReceiveWindowSpreadingFactor() const
Get the spreading factor this device is using in the second receive window.
uint32_t m_secondReceiveWindowFrequencyHz
Frequency [Hz] for RX2 window.
void SetSecondReceiveWindowFrequency(uint32_t frequencyHz)
Set the second window frequency of this device.
Ptr< Packet > GetCompleteReplyPacket()
Get the reply packet.
~EndDeviceStatus() override
Destructor.
EndDeviceStatus()
Default constructor.
Ptr< ClassAEndDeviceLorawanMac > GetMac()
Get the MAC layer of the end device.
This class represents the device address of a LoraWAN end device.
This class represents the Frame header (FHDR) used in a LoraWAN network.
uint16_t GetFCnt() const
Get the FCnt value.
void SetAsUplink()
State that this is an uplink message.
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
Definition lora-tag.h:26
double GetReceivePower() const
Read the power this packet arrived with.
Definition lora-tag.cc:92
uint32_t GetFrequency() const
Get the frequency of the packet.
Definition lora-tag.cc:122
uint8_t GetSpreadingFactor() const
Read which Spreading Factor this packet was transmitted with.
Definition lora-tag.cc:80
This class represents the Mac header of a LoRaWAN packet.
#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 ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:267
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:288
std::ostream & operator<<(std::ostream &os, const EndDeviceLoraPhy::State &state)
Overloaded operator to print the value of a EndDeviceLoraPhy::State.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition ptr.h:629
Structure saving information regarding the packet reception in each gateway.
Time receivedTime
Time at which the packet was received by this gateway.
Address gwAddress
Address of the gateway that received the packet.
double rxPower
Reception power of the packet at this gateway.
Structure saving information regarding all packet receptions.
uint32_t frequencyHz
Carrier frequency [Hz] used to send this packet.
GatewayList gwList
List of gateways that received this packet.
Ptr< const Packet > packet
The received packet.
uint8_t sf
Spreading factor used to send this packet.
Structure representing the reply that the network server will send this device at the first opportuni...