A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lora-phy.h
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#ifndef LORA_PHY_H
10#define LORA_PHY_H
11
13
14#include "ns3/mobility-model.h"
15#include "ns3/net-device.h"
16
17namespace ns3
18{
19namespace lorawan
20{
21
22class LoraChannel;
23
24/**
25 * @ingroup lorawan
26 *
27 * I/Q Polarity of LoRa transmission symbols
28 *
29 * In LoRa, uplink symbols are often called upchirps and downlink symbols downchirps. They represent
30 * symbols of opposite I/Q polarity in the LoRa Chirp Spread Spectrum (CSS) modulation.
31 */
32enum class IQPolarity
33{
34 UP, //!< Uplink / Upchirp / Normal polarity
35 DOWN //!< Downlink / Downchirp / Inverted polarity
36};
37
38/**
39 * Allow logging of IQPolarity like any other data type.
40 */
41std::ostream& operator<<(std::ostream& os, const IQPolarity& iqPolarity);
42
43/**
44 * @ingroup lorawan
45 *
46 * Enumeration of the LoRa supported coding rates
47 */
48enum class CodingRate : uint8_t
49{
50 CR_4_5 = 1, //!< Coding rate 4/5
51 CR_4_6, //!< Coding rate 4/6
52 CR_4_7, //!< Coding rate 4/7
53 CR_4_8, //!< Coding rate 4/8
54};
55
56/**
57 * Allow logging of CodingRate like any other data type.
58 */
59std::ostream& operator<<(std::ostream& os, const CodingRate& codingRate);
60
61/**
62 * Allow parsing of CodingRate from CommandLine.
63 */
64std::istream& operator>>(std::istream& is, CodingRate& codingRate);
65
66/**
67 * @ingroup lorawan
68 *
69 * Structure to collect all parameters that are used to compute the duration of
70 * a packet (excluding payload length).
71 */
73{
74 // Modulation parameters
75 uint8_t spreadingFactor = 7; //!< Symbol Spreading Factor (SF)
76 uint32_t bandwidthHz = 125'000; //!< Transmission bandwidth in Hz
77 CodingRate codingRate = CodingRate::CR_4_5; //!< Transmission coding rate
78 bool lowDataRateOptimize = false; //!< Low Data Rate Optimization (mandated for SF11 and SF12)
79 // PHY packet parameters
80 uint16_t preambleLenSymb = 8; //!< Number of symbols in the packet preamble
81 bool implicitHeader = false; //!< Whether to use implicit header mode
82 bool crcEnabled = true; //!< Whether Cyclic Redundancy Check (CRC) is enabled
83};
84
85/**
86 * Allow logging of LoraTxParameters like with any other data type.
87 */
88std::ostream& operator<<(std::ostream& os, const LoraTxParameters& params);
89
90/**
91 * @ingroup lorawan
92 *
93 * Base class for PHY layers implementing the LoRa modulation scheme.
94 *
95 * This class features common callbacks and defines the interfaces that are used
96 * to send and receive packets at the PHY layer. Furthermore, it features an
97 * implementation of the GetTimeOnAir function, used to compute the actual
98 * duration of a packet based on a series of parameters that are collected in
99 * LoraTxParameters objects.
100 */
101class LoraPhy : public Object
102{
103 public:
104 /**
105 * Type definition for a callback for when a packet is correctly received.
106 *
107 * This callback can be set by an upper layer that wishes to be informed of
108 * correct reception events.
109 */
111
112 /**
113 * Type definition for a callback for when a packet reception fails.
114 *
115 * This callback can be set by an upper layer that wishes to be informed of
116 * failed reception events.
117 */
119
120 /**
121 * Type definition for a callback to call when a packet has finished sending.
122 *
123 * This callback is used by the MAC layer, to determine when to open a receive
124 * window.
125 */
127
128 /**
129 * Compute the symbol time from spreading factor and bandwidth.
130 *
131 * @param spreadingFactor The spreading factor.
132 * @param bandwidthHz The bandwidth in Hz.
133 * @return TSym, the time required to send a LoRa modulation symbol.
134 */
135 static Time GetTSym(uint8_t spreadingFactor, uint32_t bandwidthHz);
136
137 /**
138 * Compute the total transmission time for a physical packet based on modulation parameters.
139 *
140 * Besides from the ones saved in LoraTxParameters, the packet's payload (obtained through a
141 * Packet::GetSize() call to account for the presence of Headers and Trailers) also influences
142 * the packet transmit time.
143 *
144 * @param phyPayloadLen The total number of Bytes that need to be transmitted.
145 * @param txParams The set of parameters that will be used for transmission.
146 * @return The time needed to transmit the packet.
147 */
148 static Time GetTimeOnAir(uint32_t phyPayloadLen, const LoraTxParameters& txParams);
149
150 /**
151 * Register this type.
152 * @return The object TypeId.
153 */
154 static TypeId GetTypeId();
155
156 LoraPhy(); //!< Default constructor
157 ~LoraPhy() override; //!< Destructor
158
159 /**
160 * Instruct the PHY to send a packet according to some parameters.
161 *
162 * @param packet The packet to send.
163 * @param frequencyHz The frequency on which to transmit.
164 * @param iqPolarity The transmission's I/Q polarity (uplink or downlink).
165 * @param txParams The desired transmission parameters.
166 * @param txPowerDbm The power in dBm with which to transmit the packet.
167 */
168 virtual void Send(Ptr<Packet> packet,
169 uint32_t frequencyHz,
170 IQPolarity iqPolarity,
171 const LoraTxParameters& txParams,
172 double txPowerDbm) = 0;
173
174 /**
175 * Start receiving a packet.
176 *
177 * This method is typically called by LoraChannel.
178 *
179 * @param packet The packet that is arriving at this PHY layer.
180 * @param frequencyHz The frequency this packet is being transmitted on.
181 * @param iqPolarity The transmission's I/Q polarity (uplink or downlink).
182 * @param spreadingFactor The Spreading Factor of the arriving packet.
183 * @param rxPowerDbm The power of the arriving packet (assumed to be constant for the whole
184 * reception).
185 * @param duration The on air time of this packet.
186 */
187 virtual void StartReceive(Ptr<Packet> packet,
188 uint32_t frequencyHz,
189 IQPolarity iqPolarity,
190 uint8_t spreadingFactor,
191 double rxPowerDbm,
192 Time duration) = 0;
193
194 /**
195 * Whether this device is transmitting or not.
196 *
197 * @return True if the device is currently transmitting a packet, false
198 * otherwise.
199 */
200 virtual bool IsTransmitting() const = 0;
201
202 /**
203 * Whether this device is listening on the specified frequency or not.
204 *
205 * @param frequencyHz The frequency [Hz] to query.
206 * @return True if the device is listening on that frequency, false
207 * otherwise.
208 */
209 virtual bool IsOnFrequency(uint32_t frequencyHz) const = 0;
210
211 /**
212 * Set the callback to call after transmission of a packet.
213 *
214 * This method is typically called by an upper MAC layer that wants to be
215 * notified after the transmission of a packet.
216 *
217 * @param callback The TxFinishedCallback instance.
218 */
220
221 /**
222 * Set the callback to call upon successful reception of a packet.
223 *
224 * This method is typically called by an upper MAC layer that wants to be
225 * notified after the successful reception of a packet.
226 *
227 * @param callback The RxOkCallback instance.
228 */
229 void SetReceiveOkCallback(RxOkCallback callback);
230
231 /**
232 * Set the callback to call upon failed reception of a packet we were
233 * previously locked on.
234 *
235 * This method is typically called by an upper MAC layer that wants to be
236 * notified after the failed reception of a packet.
237 *
238 * @param callback The RxFailedCallback instance.
239 */
241
242 /**
243 * Get the mobility model associated to this PHY.
244 *
245 * @return The MobilityModel associated to this PHY.
246 */
248
249 /**
250 * Set the mobility model associated to this PHY.
251 *
252 * @param mobility The mobility model to associate to this PHY.
253 */
254 void SetMobility(Ptr<MobilityModel> mobility);
255
256 /**
257 * Set the LoraChannel instance PHY transmits on.
258 *
259 * Typically, there is only one instance per simulation.
260 *
261 * @param channel The LoraChannel instance this PHY will transmit on.
262 */
263 void SetChannel(Ptr<LoraChannel> channel);
264
265 /**
266 * Get the channel instance associated to this PHY.
267 *
268 * @return The LoraChannel instance this PHY transmits on.
269 */
271
272 /**
273 * Get the NetDevice associated to this PHY.
274 *
275 * @return The NetDevice associated to this PHY.
276 */
278
279 /**
280 * Set the NetDevice that owns this PHY.
281 *
282 * @param device The NetDevice this PHY will reference as its owner.
283 */
284 void SetDevice(Ptr<NetDevice> device);
285
286 protected:
287 // Callbacks
288
289 /**
290 * The callback to perform upon the end of a transmission.
291 */
293
294 /**
295 * The callback to perform upon correct reception of a packet.
296 */
298
299 /**
300 * The callback to perform upon failed reception of a packet we were locked on.
301 */
303
304 // Member objects
305
306 Ptr<NetDevice> m_device; //!< The net device this PHY is attached to.
307
308 Ptr<LoraChannel> m_channel; //!< The channel this PHY transmits on.
309
310 LoraInterferenceHelper m_interference; //!< The LoraInterferenceHelper associated to this PHY.
311
312 // Trace sources
313
314 /**
315 * The trace source fired when a packet is sent.
316 */
318
319 /**
320 * The trace source fired when a packet begins the reception process from the
321 * medium.
322 */
324
325 /**
326 * The trace source fired when a packet reception ends.
327 */
329
330 /**
331 * The trace source fired when a packet was correctly received.
332 */
334
335 /**
336 * The trace source fired when a packet cannot be received because its power
337 * is below the sensitivity threshold.
338 */
340
341 /**
342 * The trace source fired when a packet cannot be correctly received because
343 * of interference.
344 */
346
347 private:
348 /**
349 * Internal call when transmission of a packet finishes.
350 *
351 * Calls to this function are typically scheduled by the Send function.
352 *
353 * @param packet A pointer to the packet that has been transmitted.
354 */
355 virtual void TxFinished(Ptr<const Packet> packet) = 0;
356
357 /**
358 * Finish reception of a packet.
359 *
360 * This method is scheduled by StartReceive, based on the packet duration. By
361 * passing a LoraInterferenceHelper Event to this method, the class will be
362 * able to identify the packet that is being received among all those that
363 * were registered as interference by StartReceive.
364 *
365 * @param packet The received packet.
366 * @param event The event tied to this packet in the LoraInterferenceHelper.
367 */
369
370 Ptr<MobilityModel> m_mobility; //!< The mobility model associated to this PHY.
371};
372
373} // namespace lorawan
374} // namespace ns3
375
376#endif /* LORA_PHY_H */
Callback template class.
Definition callback.h:428
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
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:50
The class that delivers packets among PHY layers.
Helper for LoraPhy that manages interference calculations.
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
virtual bool IsOnFrequency(uint32_t frequencyHz) const =0
Whether this device is listening on the specified frequency or not.
TracedCallback< Ptr< const Packet > > m_phyRxEndTrace
The trace source fired when a packet reception ends.
Definition lora-phy.h:328
LoraInterferenceHelper m_interference
The LoraInterferenceHelper associated to this PHY.
Definition lora-phy.h:310
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
virtual void TxFinished(Ptr< const Packet > packet)=0
Internal call when transmission of a packet finishes.
virtual void StartReceive(Ptr< Packet > packet, uint32_t frequencyHz, IQPolarity iqPolarity, uint8_t spreadingFactor, double rxPowerDbm, Time duration)=0
Start receiving a packet.
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
virtual void Send(Ptr< Packet > packet, uint32_t frequencyHz, IQPolarity iqPolarity, const LoraTxParameters &txParams, double txPowerDbm)=0
Instruct the PHY to send a packet according to some parameters.
virtual void EndReceive(Ptr< Packet > packet, Ptr< LoraInterferenceHelper::Event > event)=0
Finish reception of a packet.
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
virtual bool IsTransmitting() const =0
Whether this device is transmitting or not.
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
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
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