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