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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 */
19
20#include "lora-helper.h"
21
22#include "ns3/log.h"
23
24#include <fstream>
25
26namespace ns3
27{
28namespace lorawan
29{
30
31NS_LOG_COMPONENT_DEFINE("LoraHelper");
32
34 : m_lastPhyPerformanceUpdate(Seconds(0)),
35 m_lastGlobalPerformanceUpdate(Seconds(0))
36{
37}
38
40{
41}
42
45 const LorawanMacHelper& macHelper,
46 NodeContainer c) const
47{
49
50 NetDeviceContainer devices;
51
52 // Go over the various nodes in which to install the NetDevice
53 for (auto i = c.Begin(); i != c.End(); ++i)
54 {
55 Ptr<Node> node = *i;
56
57 // Create the LoraNetDevice
58 Ptr<LoraNetDevice> device = CreateObject<LoraNetDevice>();
59
60 // Create the PHY
61 Ptr<LoraPhy> phy = phyHelper.Create(node, device);
62 NS_ASSERT(phy);
63 device->SetPhy(phy);
64 NS_LOG_DEBUG("Done creating the PHY");
65
66 // Connect Trace Sources if necessary
68 {
69 if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleEndDeviceLoraPhy"))
70 {
71 phy->TraceConnectWithoutContext(
72 "StartSending",
74 }
75 else if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleGatewayLoraPhy"))
76 {
77 phy->TraceConnectWithoutContext(
78 "StartSending",
80 phy->TraceConnectWithoutContext(
81 "ReceivedPacket",
83 phy->TraceConnectWithoutContext(
84 "LostPacketBecauseInterference",
86 phy->TraceConnectWithoutContext(
87 "LostPacketBecauseNoMoreReceivers",
89 phy->TraceConnectWithoutContext(
90 "LostPacketBecauseUnderSensitivity",
92 phy->TraceConnectWithoutContext(
93 "NoReceptionBecauseTransmitting",
95 }
96 }
97
98 // Create the MAC
99 Ptr<LorawanMac> mac = macHelper.Create(node, device);
100 NS_ASSERT(mac);
101 mac->SetPhy(phy);
102 NS_LOG_DEBUG("Done creating the MAC");
103 device->SetMac(mac);
104
105 if (m_packetTracker)
106 {
107 if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleEndDeviceLoraPhy"))
108 {
109 mac->TraceConnectWithoutContext(
110 "SentNewPacket",
112
113 mac->TraceConnectWithoutContext(
114 "RequiredTransmissions",
117 }
118 else if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleGatewayLoraPhy"))
119 {
120 mac->TraceConnectWithoutContext(
121 "SentNewPacket",
123
124 mac->TraceConnectWithoutContext(
125 "ReceivedPacket",
127 }
128 }
129
130 node->AddDevice(device);
131 devices.Add(device);
132 NS_LOG_DEBUG("node=" << node
133 << ", mob=" << node->GetObject<MobilityModel>()->GetPosition());
134 }
135 return devices;
136}
137
140{
141 return Install(phy, mac, NodeContainer(node));
142}
143
144void
146{
147 NS_LOG_FUNCTION(this);
148
149 // Create the packet tracker
151}
152
155{
156 NS_LOG_FUNCTION(this);
157
158 return *m_packetTracker;
159}
160
161void
163{
164 m_oldtime = std::time(nullptr);
166}
167
168void
170 NodeContainer gateways,
171 std::string filename,
172 Time interval)
173{
174 NS_LOG_FUNCTION(this);
175
176 DoPrintDeviceStatus(endDevices, gateways, filename);
177
178 // Schedule periodic printing
179 Simulator::Schedule(interval,
181 this,
182 endDevices,
183 gateways,
184 filename,
185 interval);
186}
187
188void
190 NodeContainer gateways,
191 std::string filename)
192{
193 const char* c = filename.c_str();
194 std::ofstream outputFile;
195 if (Simulator::Now() == Seconds(0))
196 {
197 // Delete contents of the file as it is opened
198 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
199 }
200 else
201 {
202 // Only append to the file
203 outputFile.open(c, std::ofstream::out | std::ofstream::app);
204 }
205
206 Time currentTime = Simulator::Now();
207 for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
208 {
209 Ptr<Node> object = *j;
210 Ptr<MobilityModel> position = object->GetObject<MobilityModel>();
211 NS_ASSERT(position);
212 Ptr<NetDevice> netDevice = object->GetDevice(0);
213 Ptr<LoraNetDevice> loraNetDevice = netDevice->GetObject<LoraNetDevice>();
214 NS_ASSERT(loraNetDevice);
216 loraNetDevice->GetMac()->GetObject<ClassAEndDeviceLorawanMac>();
217 int dr = int(mac->GetDataRate());
218 double txPower = mac->GetTransmissionPower();
219 Vector pos = position->GetPosition();
220 outputFile << currentTime.GetSeconds() << " " << object->GetId() << " " << pos.x << " "
221 << pos.y << " " << dr << " " << unsigned(txPower) << std::endl;
222 }
223 // for (NodeContainer::Iterator j = gateways.Begin (); j != gateways.End (); ++j)
224 // {
225 // Ptr<Node> object = *j;
226 // Ptr<MobilityModel> position = object->GetObject<MobilityModel> ();
227 // Vector pos = position->GetPosition ();
228 // outputFile << currentTime.GetSeconds () << " "
229 // << object->GetId () << " "
230 // << pos.x << " " << pos.y << " " << "-1 -1" << std::endl;
231 // }
232 outputFile.close();
233}
234
235void
237 std::string filename,
238 Time interval)
239{
240 NS_LOG_FUNCTION(this);
241
242 DoPrintPhyPerformance(gateways, filename);
243
244 Simulator::Schedule(interval,
246 this,
247 gateways,
248 filename,
249 interval);
250}
251
252void
254{
255 NS_LOG_FUNCTION(this);
256
257 const char* c = filename.c_str();
258 std::ofstream outputFile;
259 if (Simulator::Now() == Seconds(0))
260 {
261 // Delete contents of the file as it is opened
262 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
263 }
264 else
265 {
266 // Only append to the file
267 outputFile.open(c, std::ofstream::out | std::ofstream::app);
268 }
269
270 for (auto it = gateways.Begin(); it != gateways.End(); ++it)
271 {
272 int systemId = (*it)->GetId();
273 outputFile << Simulator::Now().GetSeconds() << " " << std::to_string(systemId) << " "
276 systemId)
277 << std::endl;
278 }
279
281
282 outputFile.close();
283}
284
285void
287{
288 NS_LOG_FUNCTION(this << filename << interval);
289
290 DoPrintGlobalPerformance(filename);
291
292 Simulator::Schedule(interval,
294 this,
295 filename,
296 interval);
297}
298
299void
301{
302 NS_LOG_FUNCTION(this);
303
304 const char* c = filename.c_str();
305 std::ofstream outputFile;
306 if (Simulator::Now() == Seconds(0))
307 {
308 // Delete contents of the file as it is opened
309 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
310 }
311 else
312 {
313 // Only append to the file
314 outputFile.open(c, std::ofstream::out | std::ofstream::app);
315 }
316
317 outputFile << Simulator::Now().GetSeconds() << " "
320 << std::endl;
321
323
324 outputFile.close();
325}
326
327void
329{
330 // NS_LOG_INFO ("Time: " << Simulator::Now().GetHours());
331 std::cout << "Simulated time: " << Simulator::Now().GetHours() << " hours" << std::endl;
332 std::cout << "Real time from last call: " << std::time(nullptr) - m_oldtime << " seconds"
333 << std::endl;
334 m_oldtime = std::time(nullptr);
335 Simulator::Schedule(interval, &LoraHelper::DoPrintSimulationTime, this, interval);
336}
337
338} // namespace lorawan
339} // 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.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
double GetHours() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:393
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
Class representing the MAC layer of a Class A LoRaWAN device.
void EnablePeriodicDeviceStatusPrinting(NodeContainer endDevices, NodeContainer gateways, std::string filename, Time interval)
Periodically prints the status of devices in the network to a file.
Definition: lora-helper.cc:169
LoraHelper()
Default constructor.
Definition: lora-helper.cc:33
void DoPrintSimulationTime(Time interval)
Actually print the simulation time and re-schedule execution of this function.
Definition: lora-helper.cc:328
void DoPrintDeviceStatus(NodeContainer endDevices, NodeContainer gateways, std::string filename)
Print a summary of the current status of input devices.
Definition: lora-helper.cc:189
time_t m_oldtime
Real time (i.e., physical) of the last simulation time print.
Definition: lora-helper.h:160
virtual ~LoraHelper()
Destructor.
Definition: lora-helper.cc:39
Time m_lastGlobalPerformanceUpdate
Timestamp of the last global performance update.
Definition: lora-helper.h:187
void EnablePeriodicPhyPerformancePrinting(NodeContainer gateways, std::string filename, Time interval)
Periodically prints PHY-level performance at every gateway in the container.
Definition: lora-helper.cc:236
void DoPrintPhyPerformance(NodeContainer gateways, std::string filename)
Print the PHY-level performance of every gateway in the container since the last performance update.
Definition: lora-helper.cc:253
void DoPrintGlobalPerformance(std::string filename)
Print global performance as the total number of send and received packets since last performance upda...
Definition: lora-helper.cc:300
LoraPacketTracker * m_packetTracker
Pointer to the Packet Tracker object.
Definition: lora-helper.h:159
void EnablePacketTracking()
Enable tracking of packets via trace sources.
Definition: lora-helper.cc:145
void EnableSimulationTimePrinting(Time interval)
Periodically prints the simulation time to the standard output.
Definition: lora-helper.cc:162
LoraPacketTracker & GetPacketTracker()
Get a reference to the Packet Tracker object.
Definition: lora-helper.cc:154
Time m_lastPhyPerformanceUpdate
Timestamp of the last PHY performance update.
Definition: lora-helper.h:186
virtual NetDeviceContainer Install(const LoraPhyHelper &phyHelper, const LorawanMacHelper &macHelper, NodeContainer c) const
Install LoraNetDevices on a list of nodes.
Definition: lora-helper.cc:44
void EnablePeriodicGlobalPerformancePrinting(std::string filename, Time interval)
Periodically print global performance as the total number of send and received packets.
Definition: lora-helper.cc:286
Hold together all LoRa related objects.
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:66
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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 ",...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
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:700