A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
simple-gateway-lora-phy.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
10
11#include "lora-channel.h"
12#include "lora-tag.h"
13
14#include "ns3/node.h"
15#include "ns3/simulator.h"
16
17namespace ns3
18{
19namespace lorawan
20{
21
22NS_LOG_COMPONENT_DEFINE("SimpleGatewayLoraPhy");
23
25
26/***********************************************************************
27 * Implementation of gateway methods *
28 ***********************************************************************/
29
30TypeId
32{
33 static TypeId tid = TypeId("ns3::SimpleGatewayLoraPhy")
35 .SetGroupName("lorawan")
36 .AddConstructor<SimpleGatewayLoraPhy>();
37
38 return tid;
39}
40
45
50
51void
53 LoraTxParameters txParams,
54 uint32_t frequencyHz,
55 double txPowerDbm)
56{
57 NS_LOG_FUNCTION(this << packet << frequencyHz << txPowerDbm);
58
59 // Get the time a packet with these parameters will take to be transmitted
60 Time duration = GetOnAirTime(packet, txParams);
61
62 NS_LOG_DEBUG("Duration of packet: " << duration << ", SF" << unsigned(txParams.sf));
63
64 // Interrupt all receive operations
65 std::list<Ptr<SimpleGatewayLoraPhy::ReceptionPath>>::iterator it;
66 for (it = m_receptionPaths.begin(); it != m_receptionPaths.end(); ++it)
67 {
69
70 if (!currentPath->IsAvailable()) // Reception path is occupied
71 {
72 // Call the callback for reception interrupted by transmission
73 // Fire the trace source
74 if (m_device)
75 {
76 m_noReceptionBecauseTransmitting(currentPath->GetEvent()->GetPacket(),
77 m_device->GetNode()->GetId());
78 }
79 else
80 {
81 m_noReceptionBecauseTransmitting(currentPath->GetEvent()->GetPacket(), 0);
82 }
83
84 // Cancel the scheduled EndReceive call
85 Simulator::Cancel(currentPath->GetEndReceive());
86
87 // Free it
88 // This also resets all parameters like packet and endReceive call
89 currentPath->Free();
90 }
91 }
92
93 // Send the packet in the channel
94 m_channel->Send(this, packet, txPowerDbm, txParams, duration, frequencyHz);
95
97
98 m_isTransmitting = true;
99
100 // Fire the trace source
101 if (m_device)
102 {
103 m_startSending(packet, m_device->GetNode()->GetId());
104 }
105 else
106 {
107 m_startSending(packet, 0);
108 }
109}
110
111void
113 double rxPowerDbm,
114 uint8_t sf,
115 Time duration,
116 uint32_t frequencyHz)
117{
118 NS_LOG_FUNCTION(this << packet << rxPowerDbm << duration << frequencyHz);
119
120 // Fire the trace source
121 m_phyRxBeginTrace(packet);
122
124 {
125 // If we get to this point, there are no demodulators we can use
126 NS_LOG_INFO("Dropping packet reception of packet with sf = "
127 << unsigned(sf) << " because we are in TX mode");
128
129 m_phyRxEndTrace(packet);
130
131 // Fire the trace source
132 if (m_device)
133 {
134 m_noReceptionBecauseTransmitting(packet, m_device->GetNode()->GetId());
135 }
136 else
137 {
139 }
140
141 return;
142 }
143
144 // Add the event to the LoraInterferenceHelper
146 event = m_interference.Add(duration, rxPowerDbm, sf, packet, frequencyHz);
147
148 // Cycle over the receive paths to check availability to receive the packet
149 std::list<Ptr<SimpleGatewayLoraPhy::ReceptionPath>>::iterator it;
150
151 for (it = m_receptionPaths.begin(); it != m_receptionPaths.end(); ++it)
152 {
154
155 // If the receive path is available and listening on the channel of
156 // interest, we have a candidate
157 if (currentPath->IsAvailable())
158 {
159 // See whether the reception power is above or below the sensitivity
160 // for that spreading factor
161 double sensitivity = SimpleGatewayLoraPhy::sensitivity[unsigned(sf) - 7];
162
163 if (rxPowerDbm < sensitivity) // Packet arrived below sensitivity
164 {
165 NS_LOG_INFO("Dropping packet reception of packet with sf = "
166 << unsigned(sf) << " because under the sensitivity of " << sensitivity
167 << " dBm");
168
169 if (m_device)
170 {
171 m_underSensitivity(packet, m_device->GetNode()->GetId());
172 }
173 else
174 {
175 m_underSensitivity(packet, 0);
176 }
177
178 // Since the packet is below sensitivity, it makes no sense to
179 // search for another ReceivePath
180 return;
181 }
182 else // We have sufficient sensitivity to start receiving
183 {
184 NS_LOG_INFO("Scheduling reception of a packet, occupying one demodulator");
185
186 // Block this resource
187 currentPath->LockOnEvent(event);
189
190 // Schedule the end of the reception of the packet
191 EventId endReceiveEventId =
192 Simulator::Schedule(duration, &LoraPhy::EndReceive, this, packet, event);
193
194 currentPath->SetEndReceive(endReceiveEventId);
195
196 // Make sure we don't go on searching for other ReceivePaths
197 return;
198 }
199 }
200 }
201 // If we get to this point, there are no demodulators we can use
202 NS_LOG_INFO("Dropping packet reception of packet with sf = "
203 << unsigned(sf) << " and frequency " << frequencyHz
204 << "Hz because no suitable demodulator was found");
205
206 // Fire the trace source
207 if (m_device)
208 {
209 m_noMoreDemodulators(packet, m_device->GetNode()->GetId());
210 }
211 else
212 {
213 m_noMoreDemodulators(packet, 0);
214 }
215}
216
217void
219{
220 NS_LOG_FUNCTION(this << packet << *event);
221
222 // Call the trace source
223 m_phyRxEndTrace(packet);
224
225 // Call the LoraInterferenceHelper to determine whether there was
226 // destructive interference. If the packet is correctly received, this
227 // method returns a 0.
228 uint8_t packetDestroyed = 0;
229 packetDestroyed = m_interference.IsDestroyedByInterference(event);
230
231 // Check whether the packet was destroyed
232 if (packetDestroyed != uint8_t(0))
233 {
234 NS_LOG_DEBUG("packetDestroyed by " << unsigned(packetDestroyed));
235
236 // Update the packet's LoraTag
237 LoraTag tag;
238 packet->RemovePacketTag(tag);
239 tag.SetDestroyedBy(packetDestroyed);
240 packet->AddPacketTag(tag);
241
242 // Fire the trace source
243 if (m_device)
244 {
245 m_interferedPacket(packet, m_device->GetNode()->GetId());
246 }
247 else
248 {
249 m_interferedPacket(packet, 0);
250 }
251 }
252 else // Reception was correct
253 {
254 NS_LOG_INFO("Packet with SF " << unsigned(event->GetSpreadingFactor())
255 << " received correctly");
256
257 // Fire the trace source
258 if (m_device)
259 {
260 m_successfullyReceivedPacket(packet, m_device->GetNode()->GetId());
261 }
262 else
263 {
265 }
266
267 // Forward the packet to the upper layer
268 if (!m_rxOkCallback.IsNull())
269 {
270 // Make a copy of the packet
271 // Ptr<Packet> packetCopy = packet->Copy ();
272
273 // Set the receive power and frequency of this packet in the LoraTag: this
274 // information can be useful for upper layers trying to control link
275 // quality.
276 LoraTag tag;
277 packet->RemovePacketTag(tag);
278 tag.SetReceivePower(event->GetRxPowerdBm());
279 tag.SetFrequency(event->GetFrequency());
280 packet->AddPacketTag(tag);
281
282 m_rxOkCallback(packet);
283 }
284 }
285
286 // Search for the demodulator that was locked on this event to free it.
287
288 std::list<Ptr<SimpleGatewayLoraPhy::ReceptionPath>>::iterator it;
289
290 for (it = m_receptionPaths.begin(); it != m_receptionPaths.end(); ++it)
291 {
293
294 if (currentPath->GetEvent() == event)
295 {
296 currentPath->Free();
298 return;
299 }
300 }
301}
302
303} // namespace lorawan
304} // namespace ns3
An identifier for simulation events.
Definition event-id.h:45
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
static void Cancel(const EventId &id)
Set the cancel bit on this event: the event's associated function will not be invoked when it expires...
Definition simulator.cc:268
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
GatewayLoraPhy()
Default constructor.
bool m_isTransmitting
Flag indicating whether a transmission is going on.
TracedValue< int > m_occupiedReceptionPaths
The number of occupied reception paths.
static const double sensitivity[6]
A vector containing the sensitivities required to correctly decode different spreading factors.
TracedCallback< Ptr< const Packet >, uint32_t > m_noReceptionBecauseTransmitting
Trace source fired when a packet cannot be received because the gateway is in transmission state.
void TxFinished(Ptr< const Packet > packet) override
Signals the end of a transmission by the GatewayLoraPhy.
std::list< Ptr< ReceptionPath > > m_receptionPaths
A list containing the various parallel receivers that are managed by this gateway.
TracedCallback< Ptr< const Packet >, uint32_t > m_noMoreDemodulators
Trace source fired when a packet cannot be received because all available ReceivePath instances are b...
TracedCallback< Ptr< const Packet > > m_phyRxEndTrace
The trace source fired when a packet reception ends.
Definition lora-phy.h:312
LoraInterferenceHelper m_interference
The LoraInterferenceHelper associated to this PHY.
Definition lora-phy.h:294
static Time GetOnAirTime(Ptr< Packet > packet, LoraTxParameters txParams)
Compute the time that a packet with certain characteristics will take to be transmitted.
Definition lora-phy.cc:155
TracedCallback< Ptr< const Packet >, uint32_t > m_interferedPacket
The trace source fired when a packet cannot be correctly received because of interference.
Definition lora-phy.h:329
virtual void EndReceive(Ptr< Packet > packet, Ptr< LoraInterferenceHelper::Event > event)=0
Finish reception of a packet.
TracedCallback< Ptr< const Packet >, uint32_t > m_successfullyReceivedPacket
The trace source fired when a packet was correctly received.
Definition lora-phy.h:317
TracedCallback< Ptr< const Packet >, uint32_t > m_startSending
The trace source fired when a packet is sent.
Definition lora-phy.h:301
TracedCallback< Ptr< const Packet >, uint32_t > m_underSensitivity
The trace source fired when a packet cannot be received because its power is below the sensitivity th...
Definition lora-phy.h:323
Ptr< NetDevice > m_device
The net device this PHY is attached to.
Definition lora-phy.h:290
TracedCallback< Ptr< const Packet > > m_phyRxBeginTrace
The trace source fired when a packet begins the reception process from the medium.
Definition lora-phy.h:307
Ptr< LoraChannel > m_channel
The channel this PHY transmits on.
Definition lora-phy.h:292
RxOkCallback m_rxOkCallback
The callback to perform upon correct reception of a packet.
Definition lora-phy.h:336
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
Definition lora-tag.h:26
void SetFrequency(uint32_t frequencyHz)
Set the frequency of the packet.
Definition lora-tag.cc:116
void SetReceivePower(double receivePower)
Set the power this packet was received with.
Definition lora-tag.cc:110
void SetDestroyedBy(uint8_t sf)
Set which Spreading Factor this packet was destroyed by.
Definition lora-tag.cc:98
Class modeling a Lora SX1301 chip.
static TypeId GetTypeId()
Register this type.
void Send(Ptr< Packet > packet, LoraTxParameters txParams, uint32_t frequencyHz, double txPowerDbm) override
Instruct the PHY to send a packet according to some parameters.
void EndReceive(Ptr< Packet > packet, Ptr< LoraInterferenceHelper::Event > event) override
Finish reception of a packet.
void StartReceive(Ptr< Packet > packet, double rxPowerDbm, uint8_t sf, Time duration, uint32_t frequencyHz) override
Start receiving a packet.
#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_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 ",...
#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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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