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/node.h"
14#include "ns3/pointer.h"
15#include "ns3/simulator.h"
16
17namespace ns3
18{
19namespace lorawan
20{
21
22NS_LOG_COMPONENT_DEFINE("LoraChannel");
23
25
26TypeId
28{
29 static TypeId tid =
30 TypeId("ns3::LoraChannel")
32 .SetGroupName("lorawan")
33 .AddConstructor<LoraChannel>()
34 .AddAttribute("PropagationLossModel",
35 "A pointer to the propagation loss model attached to this channel.",
39 .AddAttribute("PropagationDelayModel",
40 "A pointer to the propagation delay model attached to this channel.",
44 .AddTraceSource("PacketSent",
45 "Trace source fired whenever a packet goes out on the channel",
47 "ns3::Packet::TracedCallback");
48 return tid;
49}
50
54
59
65
66void
68{
69 NS_LOG_FUNCTION(this << phy);
70
71 // Add the new phy to the vector
72 m_phyList.push_back(phy);
73}
74
75void
77{
78 NS_LOG_FUNCTION(this << phy);
79
80 // Remove the phy from the vector
81 m_phyList.erase(find(m_phyList.begin(), m_phyList.end(), phy));
82}
83
84std::size_t
86{
87 return m_phyList.size();
88}
89
91LoraChannel::GetDevice(std::size_t i) const
92{
94}
95
96void
98 Ptr<Packet> packet,
99 double txPowerDbm,
100 LoraTxParameters txParams,
101 Time duration,
102 uint32_t frequencyHz) const
103{
104 NS_LOG_FUNCTION(this << sender << packet << txPowerDbm << txParams << duration << frequencyHz);
105
106 // Get the mobility model of the sender
107 Ptr<MobilityModel> senderMobility = sender->GetMobility()->GetObject<MobilityModel>();
108
109 NS_ASSERT(senderMobility); // Make sure it's available
110
111 NS_LOG_INFO("Starting cycle over all " << m_phyList.size() << " PHYs");
112 NS_LOG_INFO("Sender mobility: " << senderMobility->GetPosition());
113
114 // Cycle over all registered PHYs
115 uint32_t j = 0;
116 std::vector<Ptr<LoraPhy>>::const_iterator i;
117 for (i = m_phyList.begin(); i != m_phyList.end(); i++, j++)
118 {
119 // Do not deliver to the sender (*i is the current PHY)
120 if (sender != (*i))
121 {
122 // Get the receiver's mobility model
123 Ptr<MobilityModel> receiverMobility = (*i)->GetMobility()->GetObject<MobilityModel>();
124
125 NS_LOG_INFO("Receiver mobility: " << receiverMobility->GetPosition());
126
127 // Compute delay using the delay model
128 Time delay = m_delay->GetDelay(senderMobility, receiverMobility);
129
130 // Compute received power using the loss model
131 double rxPowerDbm = GetRxPower(txPowerDbm, senderMobility, receiverMobility);
132
133 NS_LOG_DEBUG("Propagation: txPower="
134 << txPowerDbm << "dbm, rxPower=" << rxPowerDbm << "dbm, "
135 << "distance=" << senderMobility->GetDistanceFrom(receiverMobility)
136 << "m, delay=" << delay);
137
138 // Get the id of the destination PHY to correctly format the context
139 Ptr<NetDevice> dstNetDevice = m_phyList[j]->GetDevice();
140 uint32_t dstNode = 0;
141 if (dstNetDevice)
142 {
143 NS_LOG_INFO("Getting node index from NetDevice, since it exists");
144 dstNode = dstNetDevice->GetNode()->GetId();
145 NS_LOG_DEBUG("dstNode = " << dstNode);
146 }
147 else
148 {
149 NS_LOG_INFO("No net device connected to the PHY, using context 0");
150 }
151
152 // Create the parameters object based on the calculations above
153 LoraChannelParameters parameters;
154 parameters.rxPowerDbm = rxPowerDbm;
155 parameters.sf = txParams.sf;
156 parameters.duration = duration;
157 parameters.frequencyHz = frequencyHz;
158
159 // Schedule the receive event
160 NS_LOG_INFO("Scheduling reception of the packet");
162 delay,
164 this,
165 j,
166 packet,
167 parameters);
168
169 // Fire the trace source for sent packet
170 m_packetSent(packet);
171 }
172 }
173}
174
175void
177{
178 NS_LOG_FUNCTION(this << i << packet << parameters);
179
180 // Call the appropriate PHY instance to let it begin reception
181 m_phyList[i]->StartReceive(packet,
182 parameters.rxPowerDbm,
183 parameters.sf,
184 parameters.duration,
185 parameters.frequencyHz);
186}
187
188double
189LoraChannel::GetRxPower(double txPowerDbm,
190 Ptr<MobilityModel> senderMobility,
191 Ptr<MobilityModel> receiverMobility) const
192{
193 return m_loss->CalcRxPower(txPowerDbm, senderMobility, receiverMobility);
194}
195
196std::ostream&
197operator<<(std::ostream& os, const LoraChannelParameters& params)
198{
199 os << "(rxPowerDbm: " << params.rxPowerDbm << ", SF: " << unsigned(params.sf)
200 << ", duration: " << params.duration.As(Time::MS) << ", frequencyHz: " << params.frequencyHz
201 << ")";
202 return os;
203}
204
205} // namespace lorawan
206} // namespace ns3
Keep track of the current position and velocity of an object.
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:999
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 Send(Ptr< LoraPhy > sender, Ptr< Packet > packet, double txPowerDbm, LoraTxParameters txParams, Time duration, uint32_t frequencyHz) const
Send a packet in 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.
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: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:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#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:267
#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.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:605
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.
Time duration
The duration of the transmission.
uint32_t frequencyHz
The frequency [Hz] of this transmission.
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition lora-phy.h:54
uint8_t sf
Spreading Factor.
Definition lora-phy.h:55