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/class-a-end-device-lorawan-mac.h"
12#include "ns3/log.h"
13#include "ns3/lora-net-device.h"
14#include "ns3/lora-phy.h"
15#include "ns3/simulator.h"
16
17#include <fstream>
18
19namespace ns3
20{
21namespace lorawan
22{
23
24NS_LOG_COMPONENT_DEFINE("LoraHelper");
25
31
35
38 const LorawanMacHelper& macHelper,
39 NodeContainer c) const
40{
42
43 NetDeviceContainer devices;
44
45 // Go over the various nodes in which to install the NetDevice
46 for (auto i = c.Begin(); i != c.End(); ++i)
47 {
48 Ptr<Node> node = *i;
49
50 // Create the LoraNetDevice
52
53 // Create the PHY
54 Ptr<LoraPhy> phy = phyHelper.Install(node, device);
55 NS_ASSERT(phy);
56 device->SetPhy(phy);
57 NS_LOG_DEBUG("Done creating the PHY");
58
59 // Connect Trace Sources if necessary
61 {
62 if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleEndDeviceLoraPhy"))
63 {
64 phy->TraceConnectWithoutContext(
65 "StartSending",
67 }
68 else if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleGatewayLoraPhy"))
69 {
70 phy->TraceConnectWithoutContext(
71 "StartSending",
73 phy->TraceConnectWithoutContext(
74 "ReceivedPacket",
76 phy->TraceConnectWithoutContext(
77 "LostPacketBecauseInterference",
79 phy->TraceConnectWithoutContext(
80 "LostPacketBecauseNoMoreReceivers",
82 phy->TraceConnectWithoutContext(
83 "LostPacketBecauseUnderSensitivity",
85 phy->TraceConnectWithoutContext(
86 "NoReceptionBecauseTransmitting",
88 }
89 }
90
91 // Create the MAC
92 Ptr<LorawanMac> mac = macHelper.Install(node, device);
93 NS_ASSERT(mac);
94 mac->SetPhy(phy);
95 NS_LOG_DEBUG("Done creating the MAC");
96 device->SetMac(mac);
97
99 {
100 if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleEndDeviceLoraPhy"))
101 {
102 mac->TraceConnectWithoutContext(
103 "SentNewPacket",
105
106 mac->TraceConnectWithoutContext(
107 "ConfirmedTransmissionOutcome",
110 }
111 else if (phyHelper.GetDeviceType() == TypeId::LookupByName("ns3::SimpleGatewayLoraPhy"))
112 {
113 mac->TraceConnectWithoutContext(
114 "SentNewPacket",
116
117 mac->TraceConnectWithoutContext(
118 "ReceivedPacket",
120 }
121 }
122
123 node->AddDevice(device);
124 devices.Add(device);
125 NS_LOG_DEBUG("node=" << node
126 << ", mob=" << node->GetObject<MobilityModel>()->GetPosition());
127 }
128 return devices;
129}
130
133{
134 return Install(phy, mac, NodeContainer(node));
135}
136
137void
139{
140 NS_LOG_FUNCTION(this);
141
142 // Create the packet tracker
144}
145
148{
149 NS_LOG_FUNCTION(this);
150
151 return *m_packetTracker;
152}
153
154void
156{
157 m_oldtime = std::time(nullptr);
159}
160
161void
163 NodeContainer gateways,
164 std::string filename,
165 Time interval)
166{
167 NS_LOG_FUNCTION(this);
168
169 DoPrintDeviceStatus(endDevices, gateways, filename);
170
171 // Schedule periodic printing
172 Simulator::Schedule(interval,
174 this,
175 endDevices,
176 gateways,
177 filename,
178 interval);
179}
180
181void
183 NodeContainer gateways,
184 std::string filename)
185{
186 const char* c = filename.c_str();
187 std::ofstream outputFile;
188 if (Now().IsZero())
189 {
190 // Delete contents of the file as it is opened
191 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
192 }
193 else
194 {
195 // Only append to the file
196 outputFile.open(c, std::ofstream::out | std::ofstream::app);
197 }
198
199 Time currentTime = Now();
200 for (auto j = endDevices.Begin(); j != endDevices.End(); ++j)
201 {
202 Ptr<Node> object = *j;
203 Ptr<MobilityModel> position = object->GetObject<MobilityModel>();
204 NS_ASSERT(position);
205 Ptr<NetDevice> netDevice = object->GetDevice(0);
206 Ptr<LoraNetDevice> loraNetDevice = DynamicCast<LoraNetDevice>(netDevice);
207 NS_ASSERT(loraNetDevice);
209 DynamicCast<ClassAEndDeviceLorawanMac>(loraNetDevice->GetMac());
210 int dr = int(mac->GetDataRate());
211 double txPower = mac->GetTransmissionPowerDbm();
212 Vector pos = position->GetPosition();
213 outputFile << currentTime.As(Time::S) << " " << object->GetId() << " " << pos.x << " "
214 << pos.y << " " << dr << " " << unsigned(txPower) << std::endl;
215 }
216 // for (NodeContainer::Iterator j = gateways.Begin (); j != gateways.End (); ++j)
217 // {
218 // Ptr<Node> object = *j;
219 // Ptr<MobilityModel> position = object->GetObject<MobilityModel> ();
220 // Vector pos = position->GetPosition ();
221 // outputFile << currentTime.GetSeconds () << " "
222 // << object->GetId () << " "
223 // << pos.x << " " << pos.y << " " << "-1 -1" << std::endl;
224 // }
225 outputFile.close();
226}
227
228void
230 std::string filename,
231 Time interval)
232{
233 NS_LOG_FUNCTION(this);
234
235 DoPrintPhyPerformance(gateways, filename);
236
237 Simulator::Schedule(interval,
239 this,
240 gateways,
241 filename,
242 interval);
243}
244
245void
247{
248 NS_LOG_FUNCTION(this);
249
250 const char* c = filename.c_str();
251 std::ofstream outputFile;
252 if (Now().IsZero())
253 {
254 // Delete contents of the file as it is opened
255 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
256 }
257 else
258 {
259 // Only append to the file
260 outputFile.open(c, std::ofstream::out | std::ofstream::app);
261 }
262
263 for (auto it = gateways.Begin(); it != gateways.End(); ++it)
264 {
265 int systemId = (*it)->GetId();
266 outputFile << Now().As(Time::S) << " " << std::to_string(systemId) << " "
267 << m_packetTracker->PrintPhyPacketsPerGw(m_lastPhyPerformanceUpdate,
268 Now(),
269 systemId)
270 << std::endl;
271 }
272
274
275 outputFile.close();
276}
277
278void
280{
281 NS_LOG_FUNCTION(this << filename << interval);
282
283 DoPrintGlobalPerformance(filename);
284
285 Simulator::Schedule(interval,
287 this,
288 filename,
289 interval);
290}
291
292void
294{
295 NS_LOG_FUNCTION(this);
296
297 const char* c = filename.c_str();
298 std::ofstream outputFile;
299 if (Now().IsZero())
300 {
301 // Delete contents of the file as it is opened
302 outputFile.open(c, std::ofstream::out | std::ofstream::trunc);
303 }
304 else
305 {
306 // Only append to the file
307 outputFile.open(c, std::ofstream::out | std::ofstream::app);
308 }
309
310 outputFile << Now().As(Time::S) << " "
311 << m_packetTracker->CountMacPacketsGlobally(m_lastGlobalPerformanceUpdate, Now())
312 << std::endl;
313
315
316 outputFile.close();
317}
318
319void
321{
322 // NS_LOG_INFO ("Time: " << Now().As(Time::H));
323 std::cout << "Simulated time: " << Now().As(Time::H) << std::endl;
324 std::cout << "Real time from last call: " << std::time(nullptr) - m_oldtime << " seconds"
325 << std::endl;
326 m_oldtime = std::time(nullptr);
327 Simulator::Schedule(interval, &LoraHelper::DoPrintSimulationTime, this, interval);
328}
329
330} // namespace lorawan
331} // 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:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
@ H
hour, 60 minutes
Definition nstime.h:104
@ S
second
Definition nstime.h:106
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:422
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:874
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 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.
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.
void MacConfirmedTransmissionOutcomeCallback(uint8_t txCount, bool ack, Time firstAttempt, Ptr< Packet > packet)
Trace the outcome of MAC layer's confirmed packet retransmission and acknowledgement.
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 > Install(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 > Install(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
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:690
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:267
#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
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition simulator.cc:288
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:643