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
20std::ostream&
21operator<<(std::ostream& os, const IQPolarity& iqPolarity)
22{
23 switch (iqPolarity)
24 {
25 case IQPolarity::UP:
26 return os << "UP";
28 return os << "DOWN";
29 default:
30 NS_FATAL_ERROR("Unknown I/Q polarity value");
31 return (os << "UNKNOWN");
32 }
33}
34
35std::ostream&
36operator<<(std::ostream& os, const CodingRate& codingRate)
37{
38 switch (codingRate)
39 {
41 return os << "4/5";
43 return os << "4/6";
45 return os << "4/7";
47 return os << "4/8";
48 default:
49 NS_FATAL_ERROR("Unknown coding rate value");
50 return (os << "UNKNOWN");
51 }
52}
53
54std::istream&
55operator>>(std::istream& is, CodingRate& codingRate)
56{
57 std::string value;
58 is >> value;
59 if (value == "4/5")
60 {
61 codingRate = CodingRate::CR_4_5;
62 }
63 else if (value == "4/6")
64 {
65 codingRate = CodingRate::CR_4_6;
66 }
67 else if (value == "4/7")
68 {
69 codingRate = CodingRate::CR_4_7;
70 }
71 else if (value == "4/8")
72 {
73 codingRate = CodingRate::CR_4_8;
74 }
75 else
76 {
77 is.setstate(std::ios_base::failbit);
78 }
79 NS_ABORT_MSG_IF(is.bad(), "Failure to parse " << value);
80 return is;
81}
82
83std::ostream&
84operator<<(std::ostream& os, const LoraTxParameters& params)
85{
86 os << "LoraTxParameters("
87 << "spreadingFactor=" << unsigned(params.spreadingFactor) << ", "
88 << "bandwidthHz=" << params.bandwidthHz << ", "
89 << "codingRate=" << params.codingRate << ", "
90 << "lowDataRateOptimize=" << params.lowDataRateOptimize << ", "
91 << "preambleLenSymb: " << params.preambleLenSymb << ", "
92 << "implicitHeader=" << params.implicitHeader << ", "
93 << "crcEnabled=" << params.crcEnabled << ")";
94 return os;
95}
96
98
100
101Time
102LoraPhy::GetTSym(uint8_t spreadingFactor, uint32_t bandwidthHz)
103{
104 auto sf = static_cast<double>(spreadingFactor);
105 auto bw = static_cast<double>(bandwidthHz);
106 return Seconds(pow(2, sf) / bw);
107}
108
109Time
110LoraPhy::GetTimeOnAir(uint32_t phyPayloadLen, const LoraTxParameters& txParams)
111{
112 NS_LOG_FUNCTION(phyPayloadLen << txParams);
113
114 // The contents of this function are based on [1].
115 // [1] SX1272 LoRa modem designer's guide.
116
117 // Compute the symbol duration in seconds
118 double tSym = GetTSym(txParams.spreadingFactor, txParams.bandwidthHz).GetSeconds();
119
120 // Compute the preamble duration
121 auto nPreamble = static_cast<double>(txParams.preambleLenSymb);
122 double tPreamble = (nPreamble + 4.25) * tSym;
123
124 // Payload size
125 NS_LOG_DEBUG("PHY Packet of size " << phyPayloadLen << " bytes");
126
127 // Safety casts since the formula deals with double values.
128 auto pl = static_cast<double>(phyPayloadLen);
129 auto sf = static_cast<double>(txParams.spreadingFactor);
130 auto cr = static_cast<double>(txParams.codingRate);
131 // de = 1 when the low data rate optimization is enabled, 0 otherwise
132 // h = 1 when header is implicit, 0 otherwise
133 // crc = 1 when cyclic redundancy check is present, 0 otherwise
134 double de = txParams.lowDataRateOptimize ? 1.0 : 0.0;
135 double h = txParams.implicitHeader ? 1.0 : 0.0;
136 double crc = txParams.crcEnabled ? 1.0 : 0.0;
137
138 // num and den refer to numerator and denominator of the time on air formula
139 double num = 8.0 * pl - 4.0 * sf + 28.0 + 16.0 * crc - 20.0 * h;
140 double den = 4.0 * (sf - 2.0 * de);
141 double payloadSymbNb = 8.0 + std::max(std::ceil(num / den) * (cr + 4.0), 0.0);
142
143 // Time to transmit the payload
144 double tPayload = payloadSymbNb * tSym;
145
146 NS_LOG_DEBUG("Time computation: num = " << num << ", den = " << den << ", payloadSymbNb = "
147 << payloadSymbNb << ", tSym = " << tSym);
148 NS_LOG_DEBUG("tPreamble = " << tPreamble);
149 NS_LOG_DEBUG("tPayload = " << tPayload);
150 NS_LOG_DEBUG("Total time = " << tPreamble + tPayload);
151
152 // Compute and return the total packet on-air time
153 return Seconds(tPreamble + tPayload);
154}
155
156TypeId
158{
159 static TypeId tid =
160 TypeId("ns3::LoraPhy")
161 .SetParent<Object>()
162 .SetGroupName("lorawan")
163 .AddTraceSource("StartSending",
164 "Trace source indicating the PHY layer"
165 "has begun the sending process for a packet",
167 "ns3::Packet::TracedCallback")
168 .AddTraceSource("PhyRxBegin",
169 "Trace source indicating a packet "
170 "is now being received from the channel medium "
171 "by the device",
173 "ns3::Packet::TracedCallback")
174 .AddTraceSource("PhyRxEnd",
175 "Trace source indicating the PHY has finished "
176 "the reception process for a packet",
178 "ns3::Packet::TracedCallback")
179 .AddTraceSource("ReceivedPacket",
180 "Trace source indicating a packet "
181 "was correctly received",
183 "ns3::Packet::TracedCallback")
184 .AddTraceSource("LostPacketBecauseInterference",
185 "Trace source indicating a packet "
186 "could not be correctly decoded because of interfering"
187 "signals",
189 "ns3::Packet::TracedCallback")
190 .AddTraceSource("LostPacketBecauseUnderSensitivity",
191 "Trace source indicating a packet "
192 "could not be correctly received because"
193 "its received power is below the sensitivity of the receiver",
195 "ns3::Packet::TracedCallback");
196 return tid;
197}
198
202
206
207void
212
213void
218
219void
224
227{
229
230 // Return mobility model associated to this PHY, else, take it from the node
231 return (m_mobility) ? m_mobility : m_device->GetNode()->GetObject<MobilityModel>();
232}
233
234void
236{
238
239 m_mobility = mobility;
240}
241
244{
246
247 return m_channel;
248}
249
250void
252{
253 NS_LOG_FUNCTION(this << channel);
254
255 m_channel = channel;
256}
257
260{
261 return m_device;
262}
263
264void
266{
267 NS_LOG_FUNCTION(this << device);
268
269 m_device = device;
270}
271
272} // namespace lorawan
273} // 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
Ptr< LoraChannel > GetChannel() const
Get the channel instance associated to this PHY.
Definition lora-phy.cc:243
~LoraPhy() override
Destructor.
Definition lora-phy.cc:203
TracedCallback< Ptr< const Packet > > m_phyRxEndTrace
The trace source fired when a packet reception ends.
Definition lora-phy.h:328
Callback< void, Ptr< const Packet > > RxFailedCallback
Type definition for a callback for when a packet reception fails.
Definition lora-phy.h:118
void SetReceiveOkCallback(RxOkCallback callback)
Set the callback to call upon successful reception of a packet.
Definition lora-phy.cc:214
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:345
RxFailedCallback m_rxFailedCallback
The callback to perform upon failed reception of a packet we were locked on.
Definition lora-phy.h:302
Callback< void, Ptr< const Packet > > TxFinishedCallback
Type definition for a callback to call when a packet has finished sending.
Definition lora-phy.h:126
static Time GetTimeOnAir(uint32_t phyPayloadLen, const LoraTxParameters &txParams)
Compute the total transmission time for a physical packet based on modulation parameters.
Definition lora-phy.cc:110
TxFinishedCallback m_txFinishedCallback
The callback to perform upon the end of a transmission.
Definition lora-phy.h:292
void SetMobility(Ptr< MobilityModel > mobility)
Set the mobility model associated to this PHY.
Definition lora-phy.cc:235
void SetReceiveFailedCallback(RxFailedCallback callback)
Set the callback to call upon failed reception of a packet we were previously locked on.
Definition lora-phy.cc:220
void SetDevice(Ptr< NetDevice > device)
Set the NetDevice that owns this PHY.
Definition lora-phy.cc:265
TracedCallback< Ptr< const Packet >, uint32_t > m_successfullyReceivedPacket
The trace source fired when a packet was correctly received.
Definition lora-phy.h:333
TracedCallback< Ptr< const Packet >, uint32_t > m_startSending
The trace source fired when a packet is sent.
Definition lora-phy.h:317
Ptr< NetDevice > GetDevice() const
Get the NetDevice associated to this PHY.
Definition lora-phy.cc:259
static Time GetTSym(uint8_t spreadingFactor, uint32_t bandwidthHz)
Compute the symbol time from spreading factor and bandwidth.
Definition lora-phy.cc:102
static TypeId GetTypeId()
Register this type.
Definition lora-phy.cc:157
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:339
Ptr< MobilityModel > m_mobility
The mobility model associated to this PHY.
Definition lora-phy.h:370
void SetChannel(Ptr< LoraChannel > channel)
Set the LoraChannel instance PHY transmits on.
Definition lora-phy.cc:251
Ptr< NetDevice > m_device
The net device this PHY is attached to.
Definition lora-phy.h:306
TracedCallback< Ptr< const Packet > > m_phyRxBeginTrace
The trace source fired when a packet begins the reception process from the medium.
Definition lora-phy.h:323
Ptr< MobilityModel > GetMobility()
Get the mobility model associated to this PHY.
Definition lora-phy.cc:226
LoraPhy()
Default constructor.
Definition lora-phy.cc:199
Callback< void, Ptr< const Packet > > RxOkCallback
Type definition for a callback for when a packet is correctly received.
Definition lora-phy.h:110
void SetTxFinishedCallback(TxFinishedCallback callback)
Set the callback to call after transmission of a packet.
Definition lora-phy.cc:208
Ptr< LoraChannel > m_channel
The channel this PHY transmits on.
Definition lora-phy.h:308
RxOkCallback m_rxOkCallback
The callback to perform upon correct reception of a packet.
Definition lora-phy.h:297
#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:49
IQPolarity
I/Q Polarity of LoRa transmission symbols.
Definition lora-phy.h:33
@ CR_4_5
Coding rate 4/5.
Definition lora-phy.h:50
@ CR_4_7
Coding rate 4/7.
Definition lora-phy.h:52
@ CR_4_8
Coding rate 4/8.
Definition lora-phy.h:53
@ CR_4_6
Coding rate 4/6.
Definition lora-phy.h:51
@ DOWN
Downlink / Downchirp / Inverted polarity.
Definition lora-phy.h:35
@ UP
Uplink / Upchirp / Normal polarity.
Definition lora-phy.h:34
#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:55
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:73
CodingRate codingRate
Transmission coding rate.
Definition lora-phy.h:77
uint32_t bandwidthHz
Transmission bandwidth in Hz.
Definition lora-phy.h:76
bool implicitHeader
Whether to use implicit header mode.
Definition lora-phy.h:81
uint8_t spreadingFactor
Symbol Spreading Factor (SF).
Definition lora-phy.h:75
bool crcEnabled
Whether Cyclic Redundancy Check (CRC) is enabled.
Definition lora-phy.h:82
bool lowDataRateOptimize
Low Data Rate Optimization (mandated for SF11 and SF12).
Definition lora-phy.h:78
uint16_t preambleLenSymb
Number of symbols in the packet preamble.
Definition lora-phy.h:80