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 "lora-channel.h"
12
13#include "ns3/node.h"
14
15namespace ns3
16{
17namespace lorawan
18{
19
21
23
24TypeId
26{
27 static TypeId tid =
28 TypeId("ns3::LoraPhy")
30 .SetGroupName("lorawan")
31 .AddTraceSource("StartSending",
32 "Trace source indicating the PHY layer"
33 "has begun the sending process for a packet",
35 "ns3::Packet::TracedCallback")
36 .AddTraceSource("PhyRxBegin",
37 "Trace source indicating a packet "
38 "is now being received from the channel medium "
39 "by the device",
41 "ns3::Packet::TracedCallback")
42 .AddTraceSource("PhyRxEnd",
43 "Trace source indicating the PHY has finished "
44 "the reception process for a packet",
46 "ns3::Packet::TracedCallback")
47 .AddTraceSource("ReceivedPacket",
48 "Trace source indicating a packet "
49 "was correctly received",
51 "ns3::Packet::TracedCallback")
52 .AddTraceSource("LostPacketBecauseInterference",
53 "Trace source indicating a packet "
54 "could not be correctly decoded because of interfering"
55 "signals",
57 "ns3::Packet::TracedCallback")
58 .AddTraceSource("LostPacketBecauseUnderSensitivity",
59 "Trace source indicating a packet "
60 "could not be correctly received because"
61 "its received power is below the sensitivity of the receiver",
63 "ns3::Packet::TracedCallback");
64 return tid;
65}
66
70
74
77{
78 return m_device;
79}
80
81void
83{
84 NS_LOG_FUNCTION(this << device);
85
86 m_device = device;
87}
88
91{
93
94 return m_channel;
95}
96
99{
101
102 // If there is a mobility model associated to this PHY, take the mobility from
103 // there
104 if (m_mobility)
105 {
106 return m_mobility;
107 }
108 else // Else, take it from the node
109 {
110 return m_device->GetNode()->GetObject<MobilityModel>();
111 }
112}
113
114void
116{
118
119 m_mobility = mobility;
120}
121
122void
124{
125 NS_LOG_FUNCTION(this << channel);
126
127 m_channel = channel;
128}
129
130void
135
136void
141
142void
147
148Time
150{
151 return Seconds(pow(2, int(txParams.sf)) / (txParams.bandwidthHz));
152}
153
154Time
156{
157 NS_LOG_FUNCTION(packet << txParams);
158
159 // The contents of this function are based on [1].
160 // [1] SX1272 LoRa modem designer's guide.
161
162 // Compute the symbol duration
163 // Bandwidth is in Hz
164 double tSym = GetTSym(txParams).GetSeconds();
165
166 // Compute the preamble duration
167 auto nPreamble = static_cast<double>(txParams.nPreamble);
168 double tPreamble = (nPreamble + 4.25) * tSym;
169
170 // Payload size
171 uint32_t payloadLen = packet->GetSize(); // Size in bytes
172 NS_LOG_DEBUG("Packet of size " << payloadLen << " bytes");
173
174 // Safety casts since the formula deals with double values.
175 auto pl = static_cast<double>(payloadLen);
176 auto sf = static_cast<double>(txParams.sf);
177 auto cr = static_cast<double>(txParams.codingRate);
178 // de = 1 when the low data rate optimization is enabled, 0 otherwise
179 // h = 1 when header is implicit, 0 otherwise
180 // crc = 1 when cyclic redundancy check is present, 0 otherwise
181 double de = txParams.lowDataRateOptimizationEnabled ? 1.0 : 0.0;
182 double h = txParams.headerDisabled ? 1.0 : 0.0;
183 double crc = txParams.crcEnabled ? 1.0 : 0.0;
184
185 // num and den refer to numerator and denominator of the time on air formula
186 double num = 8.0 * pl - 4.0 * sf + 28.0 + 16.0 * crc - 20.0 * h;
187 double den = 4.0 * (sf - 2.0 * de);
188 double payloadSymbNb = 8.0 + std::max(std::ceil(num / den) * (cr + 4.0), 0.0);
189
190 // Time to transmit the payload
191 double tPayload = payloadSymbNb * tSym;
192
193 NS_LOG_DEBUG("Time computation: num = " << num << ", den = " << den << ", payloadSymbNb = "
194 << payloadSymbNb << ", tSym = " << tSym);
195 NS_LOG_DEBUG("tPreamble = " << tPreamble);
196 NS_LOG_DEBUG("tPayload = " << tPayload);
197 NS_LOG_DEBUG("Total time = " << tPreamble + tPayload);
198
199 // Compute and return the total packet on-air time
200 return Seconds(tPreamble + tPayload);
201}
202
203std::ostream&
204operator<<(std::ostream& os, const CodingRate& codingRate)
205{
206 switch (codingRate)
207 {
209 return os << "4/5";
211 return os << "4/6";
213 return os << "4/7";
215 return os << "4/8";
216 default:
217 NS_FATAL_ERROR("Unknown coding rate value");
218 return (os << "UNKNOWN");
219 }
220}
221
222std::istream&
223operator>>(std::istream& is, CodingRate& codingRate)
224{
225 std::string value;
226 is >> value;
227 if (value == "4/5")
228 {
229 codingRate = CodingRate::CR_4_5;
230 }
231 else if (value == "4/6")
232 {
233 codingRate = CodingRate::CR_4_6;
234 }
235 else if (value == "4/7")
236 {
237 codingRate = CodingRate::CR_4_7;
238 }
239 else if (value == "4/8")
240 {
241 codingRate = CodingRate::CR_4_8;
242 }
243 else
244 {
245 is.setstate(std::ios_base::failbit);
246 }
247 NS_ABORT_MSG_IF(is.bad(), "Failure to parse " << value);
248 return is;
249}
250
251std::ostream&
252operator<<(std::ostream& os, const LoraTxParameters& params)
253{
254 os << "SF: " << unsigned(params.sf) << ", headerDisabled: " << params.headerDisabled
255 << ", codingRate: " << params.codingRate << ", bandwidthHz: " << params.bandwidthHz
256 << ", nPreamble: " << params.nPreamble << ", crcEnabled: " << params.crcEnabled
257 << ", lowDataRateOptimizationEnabled: " << params.lowDataRateOptimizationEnabled << ")";
258
259 return os;
260}
261
262} // namespace lorawan
263} // namespace ns3
Keep track of the current position and velocity of an object.
Object()
Caller graph was not generated because of its size.
Definition object.cc:93
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:398
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
Base class for PHY layers implementing the LoRa modulation scheme.
Definition lora-phy.h:81
Ptr< LoraChannel > GetChannel() const
Get the channel instance associated to this PHY.
Definition lora-phy.cc:90
~LoraPhy() override
Destructor.
Definition lora-phy.cc:71
TracedCallback< Ptr< const Packet > > m_phyRxEndTrace
The trace source fired when a packet reception ends.
Definition lora-phy.h:312
Callback< void, Ptr< const Packet > > RxFailedCallback
Type definition for a callback for when a packet reception fails.
Definition lora-phy.h:106
void SetReceiveOkCallback(RxOkCallback callback)
Set the callback to call upon successful reception of a packet.
Definition lora-phy.cc:131
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
RxFailedCallback m_rxFailedCallback
The callback to perform upon failed reception of a packet we were locked on.
Definition lora-phy.h:341
Callback< void, Ptr< const Packet > > TxFinishedCallback
Type definition for a callback to call when a packet has finished sending.
Definition lora-phy.h:114
static Time GetTSym(LoraTxParameters txParams)
Compute the symbol time from spreading factor and bandwidth.
Definition lora-phy.cc:149
TxFinishedCallback m_txFinishedCallback
The callback to perform upon the end of a transmission.
Definition lora-phy.h:346
void SetMobility(Ptr< MobilityModel > mobility)
Set the mobility model associated to this PHY.
Definition lora-phy.cc:115
void SetReceiveFailedCallback(RxFailedCallback callback)
Set the callback to call upon failed reception of a packet we were previously locked on.
Definition lora-phy.cc:137
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice that owns this PHY.
Definition lora-phy.cc:82
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
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated to this PHY.
Definition lora-phy.cc:76
static TypeId GetTypeId()
Register this type.
Definition lora-phy.cc:25
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< MobilityModel > m_mobility
The mobility model associated to this PHY.
Definition lora-phy.h:285
void SetChannel(Ptr< LoraChannel > channel)
Set the LoraChannel instance PHY transmits on.
Definition lora-phy.cc:123
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< MobilityModel > GetMobility()
Get the mobility model associated to this PHY.
Definition lora-phy.cc:98
LoraPhy()
Default constructor.
Definition lora-phy.cc:67
Callback< void, Ptr< const Packet > > RxOkCallback
Type definition for a callback for when a packet is correctly received.
Definition lora-phy.h:98
void SetTxFinishedCallback(TxFinishedCallback callback)
Set the callback to call after transmission of a packet.
Definition lora-phy.cc:143
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
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#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 ",...
CodingRate
Enumeration of the LoRa supported coding rates.
Definition lora-phy.h:30
@ CR_4_5
Coding rate 4/5.
Definition lora-phy.h:31
@ CR_4_7
Coding rate 4/7.
Definition lora-phy.h:33
@ CR_4_8
Coding rate 4/8.
Definition lora-phy.h:34
@ CR_4_6
Coding rate 4/6.
Definition lora-phy.h:32
#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:1273
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.
std::istream & operator>>(std::istream &is, CodingRate &codingRate)
Allow parsing of CodingRate from CommandLine.
Definition lora-phy.cc:223
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
uint32_t nPreamble
Number of preamble symbols.
Definition lora-phy.h:59
CodingRate codingRate
Code rate (obtained as 4/(codingRate+4)).
Definition lora-phy.h:57
uint32_t bandwidthHz
Bandwidth in Hz.
Definition lora-phy.h:58
bool headerDisabled
Whether to use implicit header mode.
Definition lora-phy.h:56
bool lowDataRateOptimizationEnabled
Whether low data rate optimization is enabled.
Definition lora-phy.h:61
bool crcEnabled
Whether Cyclic Redundancy Check (CRC) is enabled.
Definition lora-phy.h:60
uint8_t sf
Spreading Factor.
Definition lora-phy.h:55