A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lora-helper.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 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
9#include "lora-helper.h"
10
11#include "ns3/log.h"
12
13#include <fstream>
14
15namespace ns3
16{
17namespace lorawan
18{
19
20NS_LOG_COMPONENT_DEFINE("LoraHelper");
21
23 : m_lastPhyPerformanceUpdate(Seconds(0)),
24 m_lastGlobalPerformanceUpdate(Seconds(0))
25{
26}
27
31
34 const LorawanMacHelper& macHelper,
35 NodeContainer c) const
36{
38
39 NetDeviceContainer devices;
40
41 // Go over the various nodes in which to install the NetDevice
42 for (auto i = c.Begin(); i != c.End(); ++i)
43 {
44 Ptr<Node> node = *i;
45
46 // Create the LoraNetDevice
48
49 // Create the PHY
50 Ptr<LoraPhy> phy = phyHelper.Create(node, device);
51 NS_ASSERT(phy);
52 device->SetPhy(phy);
53 NS_LOG_DEBUG("Done creating the PHY");
54
55 // Connect Trace Sources if necessary
57 {
58 if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleEndDeviceLoraPhy"))
59 {
60 phy->TraceConnectWithoutContext(
61 "StartSending",
63 }
64 else if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleGatewayLoraPhy"))
65 {
66 phy->TraceConnectWithoutContext(
67 "StartSending",
69 phy->TraceConnectWithoutContext(
70 "ReceivedPacket",
72 phy->TraceConnectWithoutContext(
73 "LostPacketBecauseInterference",
75 phy->TraceConnectWithoutContext(
76 "LostPacketBecauseNoMoreReceivers",
78 phy->TraceConnectWithoutContext(
79 "LostPacketBecauseUnderSensitivity",
81 phy->TraceConnectWithoutContext(
82 "NoReceptionBecauseTransmitting",
84 }
85 }
86
87 // Create the MAC
88 Ptr<LorawanMac> mac = macHelper.Create(node, device);
89 NS_ASSERT(mac);
90 mac->SetPhy(phy);
91 NS_LOG_DEBUG("Done creating the MAC");
92 device->SetMac(mac);
93
95 {
96 if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleEndDeviceLoraPhy"))
97 {
98 mac->TraceConnectWithoutContext(
99 "SentNewPacket",
101
102 mac->TraceConnectWithoutContext(
103 "RequiredTransmissions",
106 }
107 else if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleGatewayLoraPhy"))
108 {
109 mac->TraceConnectWithoutContext(
110 "SentNewPacket",
112
113 mac->TraceConnectWithoutContext(
114 "ReceivedPacket",
116 }
117 }
118
119 node->AddDevice(device);
120 devices.Add(device);
121 NS_LOG_DEBUG("node=" << node
122 << ", mob=" << node->GetObject<MobilityModel>()->GetPosition());
123 }
124 return devices;
125}
126
129{
130 return Install(phy, mac, NodeContainer(node));
131}
132
133void
135{
136 NS_LOG_FUNCTION(this);
137
138 // Create the packet tracker
140}
141
144{
145 NS_LOG_FUNCTION(this);
146
147 return *m_packetTracker;
148}
149
150void
152{
153 m_oldtime = std::time(nullptr);
155}
156
157void
159 NodeContainer gateways,
160 std::string filename,
161 Time interval)
162{
163 NS_LOG_FUNCTION(this);
164
165 DoPrintDeviceStatus(endDevices, gateways, filename);
166
167 // Schedule periodic printing
168 Simulator::Schedule(interval,
170 this,
171 endDevices,
172 gateways,
173 filename,
174 interval);
175}
176
177void
179 NodeContainer gateways,
180 std::string filename)
181{
182 const char* c = filename.c_str();
183 std::ofstream outputFile;
184 if (Simulator::Now() == Seconds(0))
185 {
186 // Delete contents of the file as it is opened
187 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
188 }
189 else
190 {
191 // Only append to the file
192 outputFile.open(c, std::ofstream::out | std::ofstream::app);
193 }
194
195 Time currentTime = Simulator::Now();
196 for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
197 {
198 Ptr<Node> object = *j;
199 Ptr<MobilityModel> position = object->GetObject<MobilityModel>();
200 NS_ASSERT(position);
201 Ptr<NetDevice> netDevice = object->GetDevice(0);
202 Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(netDevice);
203 NS_ASSERT(loraNetDevice);
205 DynamicCast<ClassAEndDeviceLorawanMac>(loraNetDevice->GetMac());
206 int dr = int(mac->GetDataRate());
207 double txPower = mac->GetTransmissionPower();
208 Vector pos = position->GetPosition();
209 outputFile << currentTime.GetSeconds() << " " << object->GetId() << " " << pos.x << " "
210 << pos.y << " " << dr << " " << unsigned(txPower) << std::endl;
211 }
212 // for (NodeContainer::Iterator j = gateways.Begin (); j != gateways.End (); ++j)
213 // {
214 // Ptr<Node> object = *j;
215 // Ptr<MobilityModel> position = object->GetObject<MobilityModel> ();
216 // Vector pos = position->GetPosition ();
217 // outputFile << currentTime.GetSeconds () << " "
218 // << object->GetId () << " "
219 // << pos.x << " " << pos.y << " " << "-1 -1" << std::endl;
220 // }
221 outputFile.close();
222}
223
224void
226 std::string filename,
227 Time interval)
228{
229 NS_LOG_FUNCTION(this);
230
231 DoPrintPhyPerformance(gateways, filename);
232
233 Simulator::Schedule(interval,
235 this,
236 gateways,
237 filename,
238 interval);
239}
240
241void
243{
244 NS_LOG_FUNCTION(this);
245
246 const char* c = filename.c_str();
247 std::ofstream outputFile;
248 if (Simulator::Now() == Seconds(0))
249 {
250 // Delete contents of the file as it is opened
251 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
252 }
253 else
254 {
255 // Only append to the file
256 outputFile.open(c, std::ofstream::out | std::ofstream::app);
257 }
258
259 for (auto it = gateways.Begin(); it != gateways.End(); ++it)
260 {
261 int systemId = (*it)->GetId();
262 outputFile << Simulator::Now().GetSeconds() << " " << std::to_string(systemId) << " "
265 systemId)
266 << std::endl;
267 }
268
270
271 outputFile.close();
272}
273
274void
276{
277 NS_LOG_FUNCTION(this << filename << interval);
278
279 DoPrintGlobalPerformance(filename);
280
281 Simulator::Schedule(interval,
283 this,
284 filename,
285 interval);
286}
287
288void
290{
291 NS_LOG_FUNCTION(this);
292
293 const char* c = filename.c_str();
294 std::ofstream outputFile;
295 if (Simulator::Now() == Seconds(0))
296 {
297 // Delete contents of the file as it is opened
298 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
299 }
300 else
301 {
302 // Only append to the file
303 outputFile.open(c, std::ofstream::out | std::ofstream::app);
304 }
305
306 outputFile << Simulator::Now().GetSeconds() << " "
309 << std::endl;
310
312
313 outputFile.close();
314}
315
316void
318{
319 // NS_LOG_INFO ("Time: " << Simulator::Now().GetHours());
320 std::cout << "Simulated time: " << Simulator::Now().GetHours() << " hours" << std::endl;
321 std::cout << "Real time from last call: " << std::time(nullptr) - m_oldtime << " seconds"
322 << std::endl;
323 m_oldtime = std::time(nullptr);
324 Simulator::Schedule(interval, &LoraHelper::DoPrintSimulationTime, this, interval);
325}
326
327} // namespace lorawan
328} // namespace ns3
Keep track of the current position and velocity of an object.
Vector GetPosition() const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
double GetHours() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:382
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
void EnablePeriodicDeviceStatusPrinting(NodeContainer endDevices, NodeContainer gateways, std::string filename, Time interval)
Periodically prints the status of devices in the network to a file.
LoraHelper()
Default constructor.
void DoPrintSimulationTime(Time interval)
Actually print the simulation time and re-schedule execution of this function.
void DoPrintDeviceStatus(NodeContainer endDevices, NodeContainer gateways, std::string filename)
Print a summary of the current status of input devices.
time_t m_oldtime
Real time (i.e., physical) of the last simulation time print.
virtual ~LoraHelper()
Destructor.
Time m_lastGlobalPerformanceUpdate
Timestamp of the last global performance update.
void EnablePeriodicPhyPerformancePrinting(NodeContainer gateways, std::string filename, Time interval)
Periodically prints PHY-level performance at every gateway in the container.
void DoPrintPhyPerformance(NodeContainer gateways, std::string filename)
Print the PHY-level performance of every gateway in the container since the last performance update.
void DoPrintGlobalPerformance(std::string filename)
Print global performance as the total number of send and received packets since last performance upda...
LoraPacketTracker * m_packetTracker
Pointer to the Packet Tracker object.
void EnablePacketTracking()
Enable tracking of packets via trace sources.
void EnableSimulationTimePrinting(Time interval)
Periodically prints the simulation time to the standard output.
LoraPacketTracker & GetPacketTracker()
Get a reference to the Packet Tracker object.
Time m_lastPhyPerformanceUpdate
Timestamp of the last PHY performance update.
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
void EnablePeriodicGlobalPerformancePrinting(std::string filename, Time interval)
Periodically print global performance as the total number of send and received packets.
Tracks and stores packets sent in the simulation and provides aggregation functionality.
void NoMoreReceiversCallback(Ptr< const Packet > packet, uint32_t systemId)
Trace a gateway packet loss caused by lack of free reception paths.
void MacTransmissionCallback(Ptr< const Packet > packet)
Trace a packet leaving a node's MAC layer to go down the stack and be sent by the PHY layer.
void RequiredTransmissionsCallback(uint8_t reqTx, bool success, Time firstAttempt, Ptr< Packet > packet)
Trace the exit status of a MAC layer packet retransmission process of an end device.
void UnderSensitivityCallback(Ptr< const Packet > packet, uint32_t systemId)
Trace a gateway packet loss caused by signal strength under sensitivity.
void LostBecauseTxCallback(Ptr< const Packet > packet, uint32_t systemId)
Trace a gateway packet loss caused by concurrent downlink transmission.
std::string PrintPhyPacketsPerGw(Time startTime, Time stopTime, int systemId)
Count packets in a time interval to evaluate the performance at PHY level of a specific gateway.
void MacGwReceptionCallback(Ptr< const Packet > packet)
Trace a packet leaving a gateway's MAC layer to go up the stack and be delivered to the node's applic...
void PacketReceptionCallback(Ptr< const Packet > packet, uint32_t systemId)
Trace a correct packet RX by the PHY layer of a gateway.
std::string CountMacPacketsGlobally(Time startTime, Time stopTime)
In a time interval, count packets to evaluate the global performance at MAC level of the whole networ...
void InterferenceCallback(Ptr< const Packet > packet, uint32_t systemId)
Trace a gateway packet loss caused by interference.
void TransmissionCallback(Ptr< const Packet > packet, uint32_t systemId)
Trace a packet TX start by the PHY layer of an end device.
Helper to install LoraPhy instances on multiple Nodes.
TypeId GetDeviceType() const
Get the TypeId of the object to be created with LoraPhyHelper.
Ptr< LoraPhy > Create(Ptr< Node > node, Ptr< NetDevice > device) const
Create a LoraPhy and connect it to a device on a node.
Helper class for configuring and installing the LorawanMac class on devices and gateways.
Ptr< LorawanMac > Create(Ptr< Node > node, Ptr< NetDevice > device) const
Create the LorawanMac instance and connect it to a device.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580