A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
end-device-lorawan-mac.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 * Martina Capuzzo <capuzzom@dei.unipd.it>
8 *
9 * Modified by: Peggy Anderson <peggy.anderson@usask.ca>
10 */
11
12#ifndef END_DEVICE_LORAWAN_MAC_H
13#define END_DEVICE_LORAWAN_MAC_H
14
15#include "lora-device-address.h"
16#include "lora-frame-header.h"
17#include "lorawan-mac-header.h"
18#include "lorawan-mac.h"
19
20#include "ns3/random-variable-stream.h"
21#include "ns3/traced-value.h"
22
23namespace ns3
24{
25namespace lorawan
26{
27
28/**
29 * @ingroup lorawan
30 *
31 * Class representing the MAC layer of a LoRaWAN device.
32 */
34{
35 public:
36 /**
37 * Register this type.
38 * @return The object TypeId.
39 */
40 static TypeId GetTypeId();
41
42 EndDeviceLorawanMac(); //!< Default constructor
43 ~EndDeviceLorawanMac() override; //!< Destructor
44
45 /////////////////////
46 // Sending methods //
47 /////////////////////
48
49 /**
50 * Send a packet.
51 *
52 * The MAC layer of the end device will take care of using the right parameters.
53 *
54 * @param packet The packet to send.
55 */
56 void Send(Ptr<Packet> packet) override;
57
58 /**
59 * Checking if we are performing the transmission of a new packet or a retransmission, and call
60 * SendToPhy function.
61 *
62 * @param packet The packet to send.
63 */
64 virtual void DoSend(Ptr<Packet> packet);
65
66 /**
67 * Add headers and send a packet with the sending function of the physical layer.
68 *
69 * @param packet The packet to send.
70 */
71 virtual void SendToPhy(Ptr<Packet> packet);
72
73 /**
74 * Postpone transmission to the specified time and delete previously scheduled transmissions if
75 * present.
76 *
77 * @param nextTxDelay Delay at which the transmission will be performed.
78 * @param packet The packet to delay the transmission of.
79 */
80 virtual void postponeTransmission(Time nextTxDelay, Ptr<Packet> packet);
81
82 ///////////////////////
83 // Receiving methods //
84 ///////////////////////
85
86 void Receive(Ptr<const Packet> packet) override;
87
88 void FailedReception(Ptr<const Packet> packet) override;
89
90 void TxFinished(Ptr<const Packet> packet) override;
91
92 /////////////////////////
93 // Getters and Setters //
94 /////////////////////////
95
96 /**
97 * Reset retransmission parameters contained in the structure LoraRetxParams.
98 */
99 virtual void resetRetransmissionParameters();
100
101 /**
102 * Signals to the network server that this device will or may not comply with LinkADRReq
103 * settings (data rate, transmission power and number of retransmissions) received in downlink.
104 *
105 * @param adr The ADR bit.
106 */
107 void SetUplinkAdrBit(bool adr);
108
109 /**
110 * Get the current value of the device's uplink ADR bit of the LoRaWAN FHDR.
111 *
112 * @return true The device will comply with data rate, transmission power and number of
113 * retransmissions settings received from the network server via LikADRReq.
114 * @return false Signals to the network server that the device may not comply with the data
115 * rate, transmission power and number of retransmissions settings received via LikADRReq.
116 */
117 bool GetUplinkAdrBit() const;
118
119 /**
120 * Set the max number of unacknowledged redundant transmissions of each packet. If,
121 * after a transmission, any acknowledgement is received, no more are sent for that packet.
122 *
123 * @param nbTrans The number of transmissions.
124 */
125 void SetMaxNumberOfTransmissions(uint8_t nbTrans);
126
127 /**
128 * Get the max number of unacknowledged redundant transmissions of each packet. If,
129 * after a transmission, any acknowledgement is received, no more are sent for that packet.
130 *
131 * @return The number of transmissions as uint8_t.
132 */
134
135 /**
136 * Set the data rate this end device will use when transmitting. For End
137 * Devices, this value is assumed to be fixed, and can be modified via MAC
138 * commands issued by the gateway.
139 *
140 * @param dataRate The dataRate to use when transmitting.
141 */
142 void SetDataRate(uint8_t dataRate);
143
144 /**
145 * Get the data rate this end device is set to use.
146 *
147 * @return The data rate this device uses when transmitting.
148 */
149 uint8_t GetDataRate();
150
151 /**
152 * Get the transmission power this end device is set to use.
153 *
154 * @return The transmission ERP [dBm] this device uses when transmitting.
155 */
157
158 /**
159 * Set the transmission power of this end device.
160 *
161 * @param txPowerDbm The transmission ERP [dBm] value.
162 */
163 void SetTransmissionPowerDbm(double txPowerDbm);
164
165 /**
166 * Set the network address of this device.
167 *
168 * @param address The address to set.
169 */
171
172 /**
173 * Get the network address of this device.
174 *
175 * @return This device's address.
176 */
178
179 // void SetRx1DrOffset (uint8_t rx1DrOffset);
180
181 // uint8_t GetRx1DrOffset ();
182
183 /**
184 * Get the last known link margin from the demodulation floor.
185 *
186 * This is intended for asynchronous polling by the Application layer of the device. For
187 * synchronous behavior provide a callback using the trace system.
188 *
189 * @return The last known link margin [dB]
190 */
191 uint8_t GetLastKnownLinkMarginDb() const;
192
193 /**
194 * Get the last known number of gateways concurrently receiving transmissions from the device.
195 *
196 * This is intended for asynchronous polling by the Application layer of the device. For
197 * synchronous behavior provide a callback using the trace system.
198 *
199 * @return The last known number of receiver gateways.
200 */
201 uint8_t GetLastKnownGatewayCount() const;
202
203 /**
204 * Get the aggregated duty cycle.
205 *
206 * @return A time instance containing the aggregated duty cycle in fractional form.
207 */
208 double GetAggregatedDutyCycle();
209
210 /////////////////////////
211 // MAC command methods //
212 /////////////////////////
213
214 /**
215 * Add the necessary options and MAC commands to the LoraFrameHeader.
216 *
217 * @param frameHeader The frame header on which to apply the options.
218 */
219 void ApplyNecessaryOptions(LoraFrameHeader& frameHeader);
220
221 /**
222 * Add the necessary options and MAC commands to the LorawanMacHeader.
223 *
224 * @param macHeader The mac header on which to apply the options.
225 */
227
228 /**
229 * Set the message type to send when the Send method is called.
230 *
231 * @param mType The message type.
232 */
234
235 /**
236 * Get the message type to send when the Send method is called.
237 *
238 * @return The message type.
239 */
241
242 /**
243 * Parse and take action on the commands contained on this FrameHeader.
244 *
245 * @param frameHeader The frame header.
246 */
247 void ParseCommands(LoraFrameHeader frameHeader);
248
249 /**
250 * Perform the actions that need to be taken when receiving a LinkCheckAns command.
251 *
252 * @param margin The margin value of the command.
253 * @param gwCnt The gateway count value of the command.
254 */
255 void OnLinkCheckAns(uint8_t margin, uint8_t gwCnt);
256
257 /**
258 * Perform the actions that need to be taken when receiving a LinkAdrReq command.
259 *
260 * @param dataRate The data rate value of the command.
261 * @param txPower The transmission power value of the command.
262 * @param chMask Mask of enabled channels of the command.
263 * @param chMaskCntl Indicator of the 16 channel bank to apply the chMask to.
264 * @param nbTrans The number of repetitions prescribed by the command.
265 */
266 void OnLinkAdrReq(uint8_t dataRate,
267 uint8_t txPower,
268 uint16_t chMask,
269 uint8_t chMaskCntl,
270 uint8_t nbTrans);
271
272 /**
273 * Perform the actions that need to be taken when receiving a DutyCycleReq command.
274 *
275 * @param maxDutyCycle The aggregate duty cycle encoded by the command.
276 */
277 void OnDutyCycleReq(uint8_t maxDutyCycle);
278
279 /**
280 * Perform the actions that need to be taken when receiving a RxParamSetupReq
281 * command based on the Device's Class Type.
282 *
283 * @param rx1DrOffset The first reception window data rate offset to set.
284 * @param rx2DataRate The data rate to use for the second receive window.
285 * @param frequencyHz The frequency [Hz] to use for the second receive window.
286 */
287 virtual void OnRxParamSetupReq(uint8_t rx1DrOffset,
288 uint8_t rx2DataRate,
289 double frequencyHz) = 0;
290
291 /**
292 * Perform the actions that need to be taken when receiving a DevStatusReq command.
293 */
294 void OnDevStatusReq();
295
296 /**
297 * Perform the actions that need to be taken when receiving a NewChannelReq command.
298 *
299 * @param chIndex The ChIndex field of the received NewChannelReq command.
300 * @param frequencyHz The Frequency [Hz] field of the received NewChannelReq command.
301 * @param minDataRate The MinDR field of the received NewChannelReq command.
302 * @param maxDataRate The MaxDR field of the received NewChannelReq command.
303 */
304 void OnNewChannelReq(uint8_t chIndex,
305 uint32_t frequencyHz,
306 uint8_t minDataRate,
307 uint8_t maxDataRate);
308
309 /**
310 * Add a MAC command to the list of those that will be sent out in the next
311 * packet.
312 *
313 * @param macCommand A pointer to the MAC command.
314 */
315 void AddMacCommand(Ptr<MacCommand> macCommand);
316
317 protected:
318 /**
319 * Structure representing the parameters that will be used in the
320 * retransmission procedure.
321 */
323 {
324 Time firstAttempt; //!< Timestamp of the first transmission of the packet
325 Ptr<Packet> packet = nullptr; //!< A pointer to the packet being retransmitted
326 bool waitingAck = false; //!< Whether the packet requires explicit acknowledgment
327 uint8_t retxLeft; //!< Number of retransmission attempts left
328 };
329
330 bool
331 m_enableDRAdapt; //!< Enable data rate adaptation (ADR) during the retransmission procedure.
332 uint8_t m_nbTrans; //!< Default number of unacknowledged redundant transmissions of each packet.
333 TracedValue<uint8_t> m_dataRate; //!< The data rate this device is using to transmit.
335 m_txPowerDbm; //!< The transmission ERP [dBm] this device is currently using.
336 uint8_t m_codingRate; //!< The coding rate used by this device.
337 bool m_headerDisabled; //!< Whether or not the LoRa PHY header is disabled for communications by
338 //!< this device.
339 LoraDeviceAddress m_address; //!< The address of this device.
340
341 /**
342 * Find the minimum wait time before the next possible transmission based
343 * on end device's Class Type.
344 *
345 * @param waitTime Currently known minimum wait time, possibly raised by this function.
346 * @return The updated minimum wait time in Time format.
347 */
348 virtual Time GetNextClassTransmissionDelay(Time waitTime);
349
350 /**
351 * Find a suitable channel for transmission. The channel is chosen among the
352 * ones that are available in the end device, based on their duty
353 * cycle limitations.
354 *
355 * @return A pointer to the channel.
356 */
358
359 /**
360 * The duration of a receive window in number of symbols. This should be
361 * converted to time based or the reception parameter used.
362 *
363 * The downlink preamble transmitted by the gateways contains 8 symbols.
364 * The receiver requires 5 symbols to detect the preamble and synchronize.
365 * Therefore there must be a 5 symbols overlap between the receive window
366 * and the transmitted preamble.
367 * (Ref: Recommended SX1272/76 Settings for EU868 LoRaWAN Network Operation )
368 */
370
371 /**
372 * List of the MAC commands that need to be applied to the next UL packet.
373 */
374 std::list<Ptr<MacCommand>> m_macCommandList;
375
376 /**
377 * Structure containing the retransmission parameters for this device.
378 */
380
381 /**
382 * An uniform random variable, used to randomly pick from the channel list.
383 */
385
386 /**
387 * Used to record the last reception SNR measurement to be included in the DevStatusAns.
388 */
390
391 /////////////////
392 // Callbacks //
393 /////////////////
394
395 /**
396 * The trace source fired when the transmission procedure is finished.
397 */
399
400 private:
401 /**
402 * Find the base minimum wait time before the next possible transmission.
403 *
404 * @return The base minimum wait time.
405 */
407
408 bool m_adr; //!< Uplink ADR bit contained in the FCtrl field of the LoRaWAN FHDR.
409 //!< Controlled by the device, if set to false signals the network server
410 //!< that the device may not accept attempts to control the number of
411 //!< retransmissions, the data rate, or the TX power with downlink
412 //!< LinkADRReq commands.
413
414 /**
415 * The event of retransmitting a packet in a consecutive moment if an ACK is not received.
416 *
417 * This Event is used to cancel the retransmission if the ACK is found in ParseCommand function
418 * and if a newer packet is delivered from the application to be sent.
419 */
421
422 /**
423 * The event of transmitting a packet in a consecutive moment, when the duty cycle let us
424 * transmit.
425 *
426 * This Event is used to cancel the transmission of this packet if a newer packet is delivered
427 * from the application to be sent.
428 */
430
431 /**
432 * The last known link margin in dB from the demodulation floor.
433 *
434 * This value is obtained (and updated) when a LinkCheckAns Mac command is
435 * received.
436 */
438
439 /**
440 * The last known gateway count (i.e., gateways that are in communication
441 * range with this end device).
442 *
443 * This value is obtained (and updated) when a LinkCheckAns Mac command is
444 * received.
445 */
447
448 /**
449 * The aggregated duty cycle this device needs to respect across all sub-bands.
450 */
452
453 /**
454 * The message type to apply to packets sent with the Send method.
455 */
457
458 /**
459 * current value of the device frame counter.
460 */
462};
463
464} // namespace lorawan
465
466} // namespace ns3
467#endif /* END_DEVICE_LORAWAN_MAC_H */
An identifier for simulation events.
Definition event-id.h:45
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Forward calls to a chain of Callback.
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:49
Class representing the MAC layer of a LoRaWAN device.
LorawanMacHeader::MType m_mType
The message type to apply to packets sent with the Send method.
void FailedReception(Ptr< const Packet > packet) override
Function called by lower layers to inform this layer that reception of a packet we were locked on fai...
bool m_headerDisabled
Whether or not the LoRa PHY header is disabled for communications by this device.
void OnLinkAdrReq(uint8_t dataRate, uint8_t txPower, uint16_t chMask, uint8_t chMaskCntl, uint8_t nbTrans)
Perform the actions that need to be taken when receiving a LinkAdrReq command.
void OnDutyCycleReq(uint8_t maxDutyCycle)
Perform the actions that need to be taken when receiving a DutyCycleReq command.
TracedValue< double > m_txPowerDbm
The transmission ERP [dBm] this device is currently using.
virtual void postponeTransmission(Time nextTxDelay, Ptr< Packet > packet)
Postpone transmission to the specified time and delete previously scheduled transmissions if present.
virtual void SendToPhy(Ptr< Packet > packet)
Add headers and send a packet with the sending function of the physical layer.
Ptr< LogicalLoraChannel > GetChannelForTx()
Find a suitable channel for transmission.
double GetTransmissionPowerDbm()
Get the transmission power this end device is set to use.
uint8_t m_codingRate
The coding rate used by this device.
bool m_adr
Uplink ADR bit contained in the FCtrl field of the LoRaWAN FHDR.
bool m_enableDRAdapt
Enable data rate adaptation (ADR) during the retransmission procedure.
void TxFinished(Ptr< const Packet > packet) override
Perform actions after sending a packet.
void SetUplinkAdrBit(bool adr)
Signals to the network server that this device will or may not comply with LinkADRReq settings (data ...
Ptr< UniformRandomVariable > m_uniformRV
An uniform random variable, used to randomly pick from the channel list.
static TypeId GetTypeId()
Register this type.
uint8_t GetLastKnownLinkMarginDb() const
Get the last known link margin from the demodulation floor.
TracedValue< double > m_aggregatedDutyCycle
The aggregated duty cycle this device needs to respect across all sub-bands.
EventId m_nextRetx
The event of transmitting a packet in a consecutive moment, when the duty cycle let us transmit.
double GetAggregatedDutyCycle()
Get the aggregated duty cycle.
void Receive(Ptr< const Packet > packet) override
Receive a packet from the lower layer.
void SetMType(LorawanMacHeader::MType mType)
Set the message type to send when the Send method is called.
virtual void resetRetransmissionParameters()
Reset retransmission parameters contained in the structure LoraRetxParams.
void ParseCommands(LoraFrameHeader frameHeader)
Parse and take action on the commands contained on this FrameHeader.
virtual void OnRxParamSetupReq(uint8_t rx1DrOffset, uint8_t rx2DataRate, double frequencyHz)=0
Perform the actions that need to be taken when receiving a RxParamSetupReq command based on the Devic...
void OnNewChannelReq(uint8_t chIndex, uint32_t frequencyHz, uint8_t minDataRate, uint8_t maxDataRate)
Perform the actions that need to be taken when receiving a NewChannelReq command.
void ApplyNecessaryOptions(LoraFrameHeader &frameHeader)
Add the necessary options and MAC commands to the LoraFrameHeader.
TracedCallback< uint8_t, bool, Time, Ptr< Packet > > m_requiredTxCallback
The trace source fired when the transmission procedure is finished.
uint8_t GetMaxNumberOfTransmissions()
Get the max number of unacknowledged redundant transmissions of each packet.
void OnLinkCheckAns(uint8_t margin, uint8_t gwCnt)
Perform the actions that need to be taken when receiving a LinkCheckAns command.
EventId m_nextTx
The event of retransmitting a packet in a consecutive moment if an ACK is not received.
void SetMaxNumberOfTransmissions(uint8_t nbTrans)
Set the max number of unacknowledged redundant transmissions of each packet.
std::list< Ptr< MacCommand > > m_macCommandList
List of the MAC commands that need to be applied to the next UL packet.
TracedValue< uint8_t > m_dataRate
The data rate this device is using to transmit.
LoraDeviceAddress m_address
The address of this device.
bool GetUplinkAdrBit() const
Get the current value of the device's uplink ADR bit of the LoRaWAN FHDR.
void Send(Ptr< Packet > packet) override
Send a packet.
uint16_t m_currentFCnt
current value of the device frame counter.
uint8_t GetLastKnownGatewayCount() const
Get the last known number of gateways concurrently receiving transmissions from the device.
void AddMacCommand(Ptr< MacCommand > macCommand)
Add a MAC command to the list of those that will be sent out in the next packet.
Time GetNextTransmissionDelay()
Find the base minimum wait time before the next possible transmission.
LorawanMacHeader::MType GetMType()
Get the message type to send when the Send method is called.
virtual void DoSend(Ptr< Packet > packet)
Checking if we are performing the transmission of a new packet or a retransmission,...
void OnDevStatusReq()
Perform the actions that need to be taken when receiving a DevStatusReq command.
LoraDeviceAddress GetDeviceAddress()
Get the network address of this device.
double m_lastRxSnr
Used to record the last reception SNR measurement to be included in the DevStatusAns.
TracedValue< uint8_t > m_lastKnownGatewayCount
The last known gateway count (i.e., gateways that are in communication range with this end device).
uint8_t m_nbTrans
Default number of unacknowledged redundant transmissions of each packet.
void SetDataRate(uint8_t dataRate)
Set the data rate this end device will use when transmitting.
uint8_t m_receiveWindowDurationInSymbols
The duration of a receive window in number of symbols.
TracedValue< uint8_t > m_lastKnownLinkMarginDb
The last known link margin in dB from the demodulation floor.
void SetTransmissionPowerDbm(double txPowerDbm)
Set the transmission power of this end device.
uint8_t GetDataRate()
Get the data rate this end device is set to use.
virtual Time GetNextClassTransmissionDelay(Time waitTime)
Find the minimum wait time before the next possible transmission based on end device's Class Type.
void SetDeviceAddress(LoraDeviceAddress address)
Set the network address of this device.
struct LoraRetxParameters m_retxParams
Structure containing the retransmission parameters for this device.
This class represents the device address of a LoraWAN end device.
This class represents the Frame header (FHDR) used in a LoraWAN network.
This class represents the Mac header of a LoRaWAN packet.
Class representing the LoRaWAN MAC layer.
Definition lorawan-mac.h:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Structure representing the parameters that will be used in the retransmission procedure.
bool waitingAck
Whether the packet requires explicit acknowledgment.
uint8_t retxLeft
Number of retransmission attempts left.
Time firstAttempt
Timestamp of the first transmission of the packet.
Ptr< Packet > packet
A pointer to the packet being retransmitted.