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 "lora-phy.h"
12
13#include "ns3/log.h"
14#include "ns3/node.h"
15#include "ns3/pointer.h"
16#include "ns3/simulator.h"
17
18namespace ns3
19{
20namespace lorawan
21{
22
23NS_LOG_COMPONENT_DEFINE("LoraChannel");
24
26
27TypeId
29{
30 static TypeId tid =
31 TypeId("ns3::LoraChannel")
33 .SetGroupName("lorawan")
34 .AddConstructor<LoraChannel>()
35 .AddAttribute("PropagationLossModel",
36 "A pointer to the propagation loss model attached to this channel.",
40 .AddAttribute("PropagationDelayModel",
41 "A pointer to the propagation delay model attached to this channel.",
45 .AddTraceSource("PacketSent",
46 "Trace source fired whenever a packet goes out on the channel",
48 "ns3::Packet::TracedCallback");
49 return tid;
50}
51
55
60
66
67void
69{
70 NS_LOG_FUNCTION(this << phy);
71
72 // Add the new phy to the vector
73 m_phyList.push_back(phy);
74}
75
76void
78{
79 NS_LOG_FUNCTION(this << phy);
80
81 // Remove the phy from the vector
82 m_phyList.erase(find(m_phyList.begin(), m_phyList.end(), phy));
83}
84
85std::size_t
87{
88 return m_phyList.size();
89}
90
92LoraChannel::GetDevice(std::size_t i) const
93{
94 return m_phyList[i]->GetDevice();
95}
96
97void
99 Ptr<Packet> packet,
100 uint32_t frequencyHz,
101 IQPolarity iqPolarity,
102 const LoraTxParameters& txParams,
103 double txPowerDbm,
104 Time duration) const
105{
106 NS_LOG_FUNCTION(this << sender << packet << frequencyHz << iqPolarity << txParams << txPowerDbm
107 << duration);
108
109 // Get the mobility model of the sender
110 auto senderMobility = sender->GetMobility();
111 NS_ASSERT(senderMobility); // Make sure it's available
112 NS_LOG_INFO("Sender mobility: " << senderMobility->GetPosition());
113
114 // Cycle over all registered PHYs
115 NS_LOG_INFO("Starting cycle over all " << m_phyList.size() << " PHYs");
116 for (const auto& receiver : m_phyList)
117 {
118 // Do not deliver to the sender
119 if (sender != receiver)
120 {
121 // Get the receiver's mobility model
122 auto receiverMobility = receiver->GetMobility();
123 NS_ASSERT(receiverMobility); // Make sure it's available
124 NS_LOG_INFO("Receiver mobility: " << receiverMobility->GetPosition());
125
126 // Compute delay using the delay model
127 Time delay = m_delay->GetDelay(senderMobility, receiverMobility);
128
129 // Compute received power using the loss model
130 double rxPowerDbm = GetRxPower(txPowerDbm, senderMobility, receiverMobility);
131
132 NS_LOG_DEBUG("Propagation: " << "txPower=" << txPowerDbm << "dbm, "
133 << "rxPower=" << rxPowerDbm << "dbm, "
134 << "distance="
135 << senderMobility->GetDistanceFrom(receiverMobility)
136 << "m, " << "delay=" << delay);
137
138 // Get the id of the destination PHY to correctly format the context
139 uint32_t dstNode = 0;
140 if (auto dstNetDevice = receiver->GetDevice(); dstNetDevice)
141 {
142 NS_LOG_INFO("Getting node index from NetDevice, since it exists");
143 dstNode = dstNetDevice->GetNode()->GetId();
144 NS_LOG_DEBUG("dstNode=" << dstNode);
145 }
146 else
147 {
148 NS_LOG_INFO("No net device connected to the PHY, using context 0");
149 }
150
151 // Create the parameters object based on the calculations above
152 LoraChannelParameters parameters;
153 parameters.frequencyHz = frequencyHz;
154 parameters.iqPolarity = iqPolarity;
155 parameters.spreadingFactor = txParams.spreadingFactor;
156 parameters.rxPowerDbm = rxPowerDbm;
157 parameters.duration = duration;
158
159 // Schedule the receive event
160 NS_LOG_INFO("Scheduling reception of the packet");
162 delay,
164 this,
165 receiver,
166 packet,
167 parameters);
168 // Fire the trace source for sent packet
169 m_packetSent(packet);
170 }
171 }
172}
173
174double
175LoraChannel::GetRxPower(double txPowerDbm,
176 Ptr<MobilityModel> senderMobility,
177 Ptr<MobilityModel> receiverMobility) const
178{
179 return m_loss->CalcRxPower(txPowerDbm, senderMobility, receiverMobility);
180}
181
182void
184 Ptr<Packet> packet,
185 const LoraChannelParameters& parameters) const
186{
187 NS_LOG_FUNCTION(this << receiver << packet << parameters);
188 // Call the appropriate PHY instance to let it begin reception
189 receiver->StartReceive(packet,
190 parameters.frequencyHz,
191 parameters.iqPolarity,
192 parameters.spreadingFactor,
193 parameters.rxPowerDbm,
194 parameters.duration);
195}
196
197std::ostream&
198operator<<(std::ostream& os, const LoraChannel::LoraChannelParameters& params)
199{
200 os << "LoraChannelParameters("
201 << "frequencyHz=" << params.frequencyHz << " Hz, "
202 << "IQPolarity=" << params.iqPolarity << ", "
203 << "spreadingFactor=" << unsigned(params.spreadingFactor) << ", "
204 << "rxPowerDbm=" << params.rxPowerDbm << " dBm, "
205 << "duration=" << params.duration.As(Time::MS) << ")";
206 return os;
207}
208
209} // namespace lorawan
210} // namespace ns3
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:597
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
@ MS
millisecond
Definition nstime.h:107
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1003
The class that delivers packets among PHY layers.
void Send(Ptr< LoraPhy > sender, Ptr< Packet > packet, uint32_t frequencyHz, IQPolarity iqPolarity, const LoraTxParameters &txParams, double txPowerDbm, Time duration) const
Send a packet in the channel.
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.
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.
void Receive(Ptr< LoraPhy > receiver, Ptr< Packet > packet, const LoraChannelParameters &parameters) const
Private method that is scheduled by LoraChannel's Send method to happen after the channel delay,...
Ptr< PropagationLossModel > m_loss
Pointer to the loss model.
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:250
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:273
#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(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:274
IQPolarity
I/Q Polarity of LoRa transmission symbols.
Definition lora-phy.h:33
#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 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.
A struct that holds meaningful parameters for transmission on a LoraChannel.
uint32_t frequencyHz
The frequency [Hz] of this transmission.
uint8_t spreadingFactor
The Spreading Factor of this transmission.
IQPolarity iqPolarity
The transmission's I/Q polarity (uplink or downlink).
Time duration
The duration of the transmission.
double rxPowerDbm
The average reception power.
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition lora-phy.h:73
uint8_t spreadingFactor
Symbol Spreading Factor (SF).
Definition lora-phy.h:75