A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lora-channel.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-channel.h"
10
11#include "end-device-lora-phy.h"
12#include "gateway-lora-phy.h"
13
14#include "ns3/log.h"
15#include "ns3/object-factory.h"
16#include "ns3/packet.h"
17#include "ns3/pointer.h"
18#include "ns3/simulator.h"
19
20#include <algorithm>
21
22namespace ns3
23{
24namespace lorawan
25{
26
27NS_LOG_COMPONENT_DEFINE("LoraChannel");
28
30
31TypeId
33{
34 static TypeId tid =
35 TypeId("ns3::LoraChannel")
37 .SetGroupName("lorawan")
38 .AddConstructor<LoraChannel>()
39 .AddAttribute("PropagationLossModel",
40 "A pointer to the propagation loss model attached to this channel.",
44 .AddAttribute("PropagationDelayModel",
45 "A pointer to the propagation delay model attached to this channel.",
49 .AddTraceSource("PacketSent",
50 "Trace source fired whenever a packet goes out on the channel",
52 "ns3::Packet::TracedCallback");
53 return tid;
54}
55
59
64
66 : m_loss(loss),
67 m_delay(delay)
68{
69}
70
71void
73{
74 NS_LOG_FUNCTION(this << phy);
75
76 // Add the new phy to the vector
77 m_phyList.push_back(phy);
78}
79
80void
82{
83 NS_LOG_FUNCTION(this << phy);
84
85 // Remove the phy from the vector
86 m_phyList.erase(find(m_phyList.begin(), m_phyList.end(), phy));
87}
88
89std::size_t
91{
92 return m_phyList.size();
93}
94
96LoraChannel::GetDevice(std::size_t i) const
97{
99}
100
101void
103 Ptr<Packet> packet,
104 double txPowerDbm,
105 LoraTxParameters txParams,
106 Time duration,
107 double frequencyMHz) const
108{
109 NS_LOG_FUNCTION(this << sender << packet << txPowerDbm << txParams << duration << frequencyMHz);
110
111 // Get the mobility model of the sender
112 Ptr<MobilityModel> senderMobility = sender->GetMobility()->GetObject<MobilityModel>();
113
114 NS_ASSERT(senderMobility); // Make sure it's available
115
116 NS_LOG_INFO("Starting cycle over all " << m_phyList.size() << " PHYs");
117 NS_LOG_INFO("Sender mobility: " << senderMobility->GetPosition());
118
119 // Cycle over all registered PHYs
120 uint32_t j = 0;
121 std::vector<Ptr<LoraPhy>>::const_iterator i;
122 for (i = m_phyList.begin(); i != m_phyList.end(); i++, j++)
123 {
124 // Do not deliver to the sender (*i is the current PHY)
125 if (sender != (*i))
126 {
127 // Get the receiver's mobility model
128 Ptr<MobilityModel> receiverMobility = (*i)->GetMobility()->GetObject<MobilityModel>();
129
130 NS_LOG_INFO("Receiver mobility: " << receiverMobility->GetPosition());
131
132 // Compute delay using the delay model
133 Time delay = m_delay->GetDelay(senderMobility, receiverMobility);
134
135 // Compute received power using the loss model
136 double rxPowerDbm = GetRxPower(txPowerDbm, senderMobility, receiverMobility);
137
138 NS_LOG_DEBUG("Propagation: txPower="
139 << txPowerDbm << "dbm, rxPower=" << rxPowerDbm << "dbm, "
140 << "distance=" << senderMobility->GetDistanceFrom(receiverMobility)
141 << "m, delay=" << delay);
142
143 // Get the id of the destination PHY to correctly format the context
144 Ptr<NetDevice> dstNetDevice = m_phyList[j]->GetDevice();
145 uint32_t dstNode = 0;
146 if (dstNetDevice)
147 {
148 NS_LOG_INFO("Getting node index from NetDevice, since it exists");
149 dstNode = dstNetDevice->GetNode()->GetId();
150 NS_LOG_DEBUG("dstNode = " << dstNode);
151 }
152 else
153 {
154 NS_LOG_INFO("No net device connected to the PHY, using context 0");
155 }
156
157 // Create the parameters object based on the calculations above
158 LoraChannelParameters parameters;
159 parameters.rxPowerDbm = rxPowerDbm;
160 parameters.sf = txParams.sf;
161 parameters.duration = duration;
162 parameters.frequencyMHz = frequencyMHz;
163
164 // Schedule the receive event
165 NS_LOG_INFO("Scheduling reception of the packet");
167 delay,
169 this,
170 j,
171 packet,
172 parameters);
173
174 // Fire the trace source for sent packet
175 m_packetSent(packet);
176 }
177 }
178}
179
180void
182{
183 NS_LOG_FUNCTION(this << i << packet << parameters);
184
185 // Call the appropriate PHY instance to let it begin reception
186 m_phyList[i]->StartReceive(packet,
187 parameters.rxPowerDbm,
188 parameters.sf,
189 parameters.duration,
190 parameters.frequencyMHz);
191}
192
193double
194LoraChannel::GetRxPower(double txPowerDbm,
195 Ptr<MobilityModel> senderMobility,
196 Ptr<MobilityModel> receiverMobility) const
197{
198 return m_loss->CalcRxPower(txPowerDbm, senderMobility, receiverMobility);
199}
200
201std::ostream&
202operator<<(std::ostream& os, const LoraChannelParameters& params)
203{
204 os << "(rxPowerDbm: " << params.rxPowerDbm << ", SF: " << unsigned(params.sf)
205 << ", durationSec: " << params.duration.GetSeconds()
206 << ", frequencyMHz: " << params.frequencyMHz << ")";
207 return os;
208}
209} // namespace lorawan
210} // namespace ns3
Abstract Channel Base Class.
Definition channel.h:34
Keep track of the current position and velocity of an object.
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
The class that delivers packets among PHY layers.
std::size_t GetNDevices() const override
~LoraChannel() override
Destructor.
std::vector< Ptr< LoraPhy > > m_phyList
The vector containing the PHYs that are currently connected to the channel.
void Remove(Ptr< LoraPhy > phy)
Remove a physical layer from the LoraChannel.
void Send(Ptr< LoraPhy > sender, Ptr< Packet > packet, double txPowerDbm, LoraTxParameters txParams, Time duration, double frequencyMHz) const
Send a packet in the channel.
double GetRxPower(double txPowerDbm, Ptr< MobilityModel > senderMobility, Ptr< MobilityModel > receiverMobility) const
Compute the received power when transmitting from a point to another one.
Ptr< PropagationDelayModel > m_delay
Pointer to the delay model.
LoraChannel()
Default constructor.
TracedCallback< Ptr< const Packet > > m_packetSent
Callback for when a packet is being sent on the channel.
Ptr< NetDevice > GetDevice(std::size_t i) const override
void Add(Ptr< LoraPhy > phy)
Connect a LoraPhy object to the LoraChannel.
Ptr< PropagationLossModel > m_loss
Pointer to the loss model.
void Receive(uint32_t i, Ptr< Packet > packet, LoraChannelParameters parameters) const
Private method that is scheduled by LoraChannel's Send method to happen after the channel delay,...
static TypeId GetTypeId()
Register this type.
#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
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#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(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:264
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
std::ostream & operator<<(std::ostream &os, const EndDeviceStatus &status)
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:580
A struct that holds meaningful parameters for transmission on a LoraChannel.
uint8_t sf
The Spreading Factor of this transmission.
double rxPowerDbm
The reception power.
double frequencyMHz
The frequency [MHz] of this transmission.
Time duration
The duration of the transmission.
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition lora-phy.h:38
uint8_t sf
Spreading Factor.
Definition lora-phy.h:39