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 * 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-channel.h"
21
22#include "end-device-lora-phy.h"
23#include "gateway-lora-phy.h"
24
25#include "ns3/log.h"
26#include "ns3/object-factory.h"
27#include "ns3/packet.h"
28#include "ns3/pointer.h"
29#include "ns3/simulator.h"
30
31#include <algorithm>
32
33namespace ns3
34{
35namespace lorawan
36{
37
38NS_LOG_COMPONENT_DEFINE("LoraChannel");
39
41
42TypeId
44{
45 static TypeId tid =
46 TypeId("ns3::LoraChannel")
48 .SetGroupName("lorawan")
49 .AddConstructor<LoraChannel>()
50 .AddAttribute("PropagationLossModel",
51 "A pointer to the propagation loss model attached to this channel.",
54 MakePointerChecker<PropagationLossModel>())
55 .AddAttribute("PropagationDelayModel",
56 "A pointer to the propagation delay model attached to this channel.",
59 MakePointerChecker<PropagationDelayModel>())
60 .AddTraceSource("PacketSent",
61 "Trace source fired whenever a packet goes out on the channel",
63 "ns3::Packet::TracedCallback");
64 return tid;
65}
66
68{
69}
70
72{
73 m_phyList.clear();
74}
75
77 : m_loss(loss),
78 m_delay(delay)
79{
80}
81
82void
84{
85 NS_LOG_FUNCTION(this << phy);
86
87 // Add the new phy to the vector
88 m_phyList.push_back(phy);
89}
90
91void
93{
94 NS_LOG_FUNCTION(this << phy);
95
96 // Remove the phy from the vector
97 m_phyList.erase(find(m_phyList.begin(), m_phyList.end(), phy));
98}
99
100std::size_t
102{
103 return m_phyList.size();
104}
105
107LoraChannel::GetDevice(std::size_t i) const
108{
109 return m_phyList[i]->GetDevice()->GetObject<NetDevice>();
110}
111
112void
114 Ptr<Packet> packet,
115 double txPowerDbm,
116 LoraTxParameters txParams,
117 Time duration,
118 double frequencyMHz) const
119{
120 NS_LOG_FUNCTION(this << sender << packet << txPowerDbm << txParams << duration << frequencyMHz);
121
122 // Get the mobility model of the sender
123 Ptr<MobilityModel> senderMobility = sender->GetMobility()->GetObject<MobilityModel>();
124
125 NS_ASSERT(senderMobility); // Make sure it's available
126
127 NS_LOG_INFO("Starting cycle over all " << m_phyList.size() << " PHYs");
128 NS_LOG_INFO("Sender mobility: " << senderMobility->GetPosition());
129
130 // Cycle over all registered PHYs
131 uint32_t j = 0;
132 std::vector<Ptr<LoraPhy>>::const_iterator i;
133 for (i = m_phyList.begin(); i != m_phyList.end(); i++, j++)
134 {
135 // Do not deliver to the sender (*i is the current PHY)
136 if (sender != (*i))
137 {
138 // Get the receiver's mobility model
139 Ptr<MobilityModel> receiverMobility = (*i)->GetMobility()->GetObject<MobilityModel>();
140
141 NS_LOG_INFO("Receiver mobility: " << receiverMobility->GetPosition());
142
143 // Compute delay using the delay model
144 Time delay = m_delay->GetDelay(senderMobility, receiverMobility);
145
146 // Compute received power using the loss model
147 double rxPowerDbm = GetRxPower(txPowerDbm, senderMobility, receiverMobility);
148
149 NS_LOG_DEBUG("Propagation: txPower="
150 << txPowerDbm << "dbm, rxPower=" << rxPowerDbm << "dbm, "
151 << "distance=" << senderMobility->GetDistanceFrom(receiverMobility)
152 << "m, delay=" << delay);
153
154 // Get the id of the destination PHY to correctly format the context
155 Ptr<NetDevice> dstNetDevice = m_phyList[j]->GetDevice();
156 uint32_t dstNode = 0;
157 if (dstNetDevice)
158 {
159 NS_LOG_INFO("Getting node index from NetDevice, since it exists");
160 dstNode = dstNetDevice->GetNode()->GetId();
161 NS_LOG_DEBUG("dstNode = " << dstNode);
162 }
163 else
164 {
165 NS_LOG_INFO("No net device connected to the PHY, using context 0");
166 }
167
168 // Create the parameters object based on the calculations above
169 LoraChannelParameters parameters;
170 parameters.rxPowerDbm = rxPowerDbm;
171 parameters.sf = txParams.sf;
172 parameters.duration = duration;
173 parameters.frequencyMHz = frequencyMHz;
174
175 // Schedule the receive event
176 NS_LOG_INFO("Scheduling reception of the packet");
178 delay,
180 this,
181 j,
182 packet,
183 parameters);
184
185 // Fire the trace source for sent packet
186 m_packetSent(packet);
187 }
188 }
189}
190
191void
193{
194 NS_LOG_FUNCTION(this << i << packet << parameters);
195
196 // Call the appropriate PHY instance to let it begin reception
197 m_phyList[i]->StartReceive(packet,
198 parameters.rxPowerDbm,
199 parameters.sf,
200 parameters.duration,
201 parameters.frequencyMHz);
202}
203
204double
205LoraChannel::GetRxPower(double txPowerDbm,
206 Ptr<MobilityModel> senderMobility,
207 Ptr<MobilityModel> receiverMobility) const
208{
209 return m_loss->CalcRxPower(txPowerDbm, senderMobility, receiverMobility);
210}
211
212std::ostream&
213operator<<(std::ostream& os, const LoraChannelParameters& params)
214{
215 os << "(rxPowerDbm: " << params.rxPowerDbm << ", SF: " << unsigned(params.sf)
216 << ", durationSec: " << params.duration.GetSeconds()
217 << ", frequencyMHz: " << params.frequencyMHz << ")";
218 return os;
219}
220} // namespace lorawan
221} // namespace ns3
Abstract Channel Base Class.
Definition: channel.h:45
Keep track of the current position and velocity of an object.
Network layer to device interface.
Definition: net-device.h:98
AttributeValue implementation for Pointer.
Definition: pointer.h:48
double CalcRxPower(double txPowerDbm, Ptr< MobilityModel > a, Ptr< MobilityModel > b) const
Returns the Rx Power taking into account all the PropagationLossModel(s) chained to the current one.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
The class that delivers packets among PHY layers.
Definition: lora-channel.h:82
std::size_t GetNDevices() const override
~LoraChannel() override
Destructor.
Definition: lora-channel.cc:71
std::vector< Ptr< LoraPhy > > m_phyList
The vector containing the PHYs that are currently connected to the channel.
Definition: lora-channel.h:187
void Remove(Ptr< LoraPhy > phy)
Remove a physical layer from the LoraChannel.
Definition: lora-channel.cc:92
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.
Definition: lora-channel.h:200
LoraChannel()
Default constructor.
Definition: lora-channel.cc:67
TracedCallback< Ptr< const Packet > > m_packetSent
Callback for when a packet is being sent on the channel.
Definition: lora-channel.h:205
Ptr< NetDevice > GetDevice(std::size_t i) const override
void Add(Ptr< LoraPhy > phy)
Connect a LoraPhy object to the LoraChannel.
Definition: lora-channel.cc:83
Ptr< PropagationLossModel > m_loss
Pointer to the loss model.
Definition: lora-channel.h:195
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.
Definition: lora-channel.cc:43
#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
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
#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(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:275
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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.
A struct that holds meaningful parameters for transmission on a LoraChannel.
Definition: lora-channel.h:58
uint8_t sf
The Spreading Factor of this transmission.
Definition: lora-channel.h:60
double rxPowerDbm
The reception power.
Definition: lora-channel.h:59
double frequencyMHz
The frequency [MHz] of this transmission.
Definition: lora-channel.h:62
Time duration
The duration of the transmission.
Definition: lora-channel.h:61
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition: lora-phy.h:49
uint8_t sf
Spreading Factor.
Definition: lora-phy.h:50