A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
9#include "lora-phy.h"
10
11#include "ns3/log.h"
12#include "ns3/simulator.h"
13
14#include <algorithm>
15
16namespace ns3
17{
18namespace lorawan
19{
20
22
24
25TypeId
27{
28 static TypeId tid =
29 TypeId("ns3::LoraPhy")
31 .SetGroupName("lorawan")
32 .AddTraceSource("StartSending",
33 "Trace source indicating the PHY layer"
34 "has begun the sending process for a packet",
36 "ns3::Packet::TracedCallback")
37 .AddTraceSource("PhyRxBegin",
38 "Trace source indicating a packet "
39 "is now being received from the channel medium "
40 "by the device",
42 "ns3::Packet::TracedCallback")
43 .AddTraceSource("PhyRxEnd",
44 "Trace source indicating the PHY has finished "
45 "the reception process for a packet",
47 "ns3::Packet::TracedCallback")
48 .AddTraceSource("ReceivedPacket",
49 "Trace source indicating a packet "
50 "was correctly received",
52 "ns3::Packet::TracedCallback")
53 .AddTraceSource("LostPacketBecauseInterference",
54 "Trace source indicating a packet "
55 "could not be correctly decoded because of interfering"
56 "signals",
58 "ns3::Packet::TracedCallback")
59 .AddTraceSource("LostPacketBecauseUnderSensitivity",
60 "Trace source indicating a packet "
61 "could not be correctly received because"
62 "its received power is below the sensitivity of the receiver",
64 "ns3::Packet::TracedCallback");
65 return tid;
66}
67
71
75
78{
79 return m_device;
80}
81
82void
84{
85 NS_LOG_FUNCTION(this << device);
86
87 m_device = device;
88}
89
92{
94
95 return m_channel;
96}
97
100{
102
103 // If there is a mobility model associated to this PHY, take the mobility from
104 // there
105 if (m_mobility)
106 {
107 return m_mobility;
108 }
109 else // Else, take it from the node
110 {
111 return m_device->GetNode()->GetObject<MobilityModel>();
112 }
113}
114
115void
117{
119
120 m_mobility = mobility;
121}
122
123void
125{
126 NS_LOG_FUNCTION(this << channel);
127
128 m_channel = channel;
129}
130
131void
136
137void
142
143void
148
149Time
151{
152 return Seconds(pow(2, int(txParams.sf)) / (txParams.bandwidthHz));
153}
154
155Time
157{
158 NS_LOG_FUNCTION(packet << txParams);
159
160 // The contents of this function are based on [1].
161 // [1] SX1272 LoRa modem designer's guide.
162
163 // Compute the symbol duration
164 // Bandwidth is in Hz
165 double tSym = GetTSym(txParams).GetSeconds();
166
167 // Compute the preamble duration
168 double tPreamble = (double(txParams.nPreamble) + 4.25) * tSym;
169
170 // Payload size
171 uint32_t pl = packet->GetSize(); // Size in bytes
172 NS_LOG_DEBUG("Packet of size " << pl << " bytes");
173
174 // This step is needed since the formula deals with double values.
175 // de = 1 when the low data rate optimization is enabled, 0 otherwise
176 // h = 1 when header is implicit, 0 otherwise
177 double de = txParams.lowDataRateOptimizationEnabled ? 1 : 0;
178 double h = txParams.headerDisabled ? 1 : 0;
179 double crc = txParams.crcEnabled ? 1 : 0;
180
181 // num and den refer to numerator and denominator of the time on air formula
182 double num = 8 * pl - 4 * txParams.sf + 28 + 16 * crc - 20 * h;
183 double den = 4 * (txParams.sf - 2 * de);
184 double payloadSymbNb =
185 8 + std::max(std::ceil(num / den) * (txParams.codingRate + 4), double(0));
186
187 // Time to transmit the payload
188 double tPayload = payloadSymbNb * tSym;
189
190 NS_LOG_DEBUG("Time computation: num = " << num << ", den = " << den << ", payloadSymbNb = "
191 << payloadSymbNb << ", tSym = " << tSym);
192 NS_LOG_DEBUG("tPreamble = " << tPreamble);
193 NS_LOG_DEBUG("tPayload = " << tPayload);
194 NS_LOG_DEBUG("Total time = " << tPreamble + tPayload);
195
196 // Compute and return the total packet on-air time
197 return Seconds(tPreamble + tPayload);
198}
199
200std::ostream&
201operator<<(std::ostream& os, const LoraTxParameters& params)
202{
203 os << "SF: " << unsigned(params.sf) << ", headerDisabled: " << params.headerDisabled
204 << ", codingRate: " << unsigned(params.codingRate) << ", bandwidthHz: " << params.bandwidthHz
205 << ", nPreamble: " << params.nPreamble << ", crcEnabled: " << params.crcEnabled
206 << ", lowDataRateOptimizationEnabled: " << params.lowDataRateOptimizationEnabled << ")";
207
208 return os;
209}
210} // namespace lorawan
211} // namespace ns3
Keep track of the current position and velocity of an object.
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
Ptr< LoraChannel > GetChannel() const
Get the channel instance associated to this PHY.
Definition lora-phy.cc:91
~LoraPhy() override
Destructor.
Definition lora-phy.cc:72
TracedCallback< Ptr< const Packet > > m_phyRxEndTrace
The trace source fired when a packet reception ends.
Definition lora-phy.h:296
void SetReceiveOkCallback(RxOkCallback callback)
Set the callback to call upon successful reception of a packet.
Definition lora-phy.cc:132
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:156
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:313
RxFailedCallback m_rxFailedCallback
The callback to perform upon failed reception of a packet we were locked on.
Definition lora-phy.h:325
static Time GetTSym(LoraTxParameters txParams)
Compute the symbol time from spreading factor and bandwidth.
Definition lora-phy.cc:150
TxFinishedCallback m_txFinishedCallback
The callback to perform upon the end of a transmission.
Definition lora-phy.h:330
void SetMobility(Ptr< MobilityModel > mobility)
Set the mobility model associated to this PHY.
Definition lora-phy.cc:116
void SetReceiveFailedCallback(RxFailedCallback callback)
Set the callback to call upon failed reception of a packet we were previously locked on.
Definition lora-phy.cc:138
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice that owns this PHY.
Definition lora-phy.cc:83
TracedCallback< Ptr< const Packet >, uint32_t > m_successfullyReceivedPacket
The trace source fired when a packet was correctly received.
Definition lora-phy.h:301
TracedCallback< Ptr< const Packet >, uint32_t > m_startSending
The trace source fired when a packet is sent.
Definition lora-phy.h:285
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated to this PHY.
Definition lora-phy.cc:77
static TypeId GetTypeId()
Register this type.
Definition lora-phy.cc:26
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:307
Ptr< MobilityModel > m_mobility
The mobility model associated to this PHY.
Definition lora-phy.h:269
void SetChannel(Ptr< LoraChannel > channel)
Set the LoraChannel instance PHY transmits on.
Definition lora-phy.cc:124
Ptr< NetDevice > m_device
The net device this PHY is attached to.
Definition lora-phy.h:274
TracedCallback< Ptr< const Packet > > m_phyRxBeginTrace
The trace source fired when a packet begins the reception process from the medium.
Definition lora-phy.h:291
Ptr< MobilityModel > GetMobility()
Get the mobility model associated to this PHY.
Definition lora-phy.cc:99
LoraPhy()
Default constructor.
Definition lora-phy.cc:68
void SetTxFinishedCallback(TxFinishedCallback callback)
Set the callback to call after transmission of a packet.
Definition lora-phy.cc:144
Ptr< LoraChannel > m_channel
The channel this PHY transmits on.
Definition lora-phy.h:276
RxOkCallback m_rxOkCallback
The callback to perform upon correct reception of a packet.
Definition lora-phy.h:320
#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_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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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.
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition lora-phy.h:38
uint8_t codingRate
Code rate (obtained as 4/(codingRate+4))
Definition lora-phy.h:41
uint32_t nPreamble
Number of preamble symbols.
Definition lora-phy.h:43
bool headerDisabled
Whether to use implicit header mode.
Definition lora-phy.h:40
double bandwidthHz
Bandwidth in Hz.
Definition lora-phy.h:42
bool lowDataRateOptimizationEnabled
Whether low data rate optimization is enabled.
Definition lora-phy.h:45
bool crcEnabled
Whether Cyclic Redundancy Check (CRC) is enabled.
Definition lora-phy.h:44
uint8_t sf
Spreading Factor.
Definition lora-phy.h:39