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 * This also controls whether the local ADR backoff procedure can reset configurations in case
105 * of connectivity loss. A false value effectively allows for a fully static transmission
106 * parameters configuration.
107 *
108 * @note Setting this to false does not prevent the device from setting the ADRACKReq bit
109 * in its FHDR to request a keepalive downlink message from the server.
110 *
111 * @param adr The ADR bit.
112 */
113 void SetUplinkAdrBit(bool adr);
114
115 /**
116 * Get the current value of the device's uplink ADR bit of the LoRaWAN FHDR.
117 *
118 * @return true The device will comply with data rate, transmission power and number of
119 * retransmissions settings received from the network server via LinkADRReq.
120 * @return false Signals to the network server that the device may not comply with the data
121 * rate, transmission power and number of retransmissions settings received via LikADRReq.
122 */
123 bool GetUplinkAdrBit() const;
124
125 /**
126 * Set the max number of unacknowledged redundant transmissions of each packet. If,
127 * after a transmission, any acknowledgement is received, no more are sent for that packet.
128 *
129 * @param nbTrans The number of transmissions.
130 */
131 void SetMaxNumberOfTransmissions(uint8_t nbTrans);
132
133 /**
134 * Get the max number of unacknowledged redundant transmissions of each packet. If,
135 * after a transmission, any acknowledgement is received, no more are sent for that packet.
136 *
137 * @return The number of transmissions as uint8_t.
138 */
140
141 /**
142 * Set the data rate this end device will use when transmitting. For End
143 * Devices, this value is assumed to be fixed, and can be modified via MAC
144 * commands issued by the gateway.
145 *
146 * @param dataRate The dataRate to use when transmitting.
147 */
148 void SetDataRate(uint8_t dataRate);
149
150 /**
151 * Get the data rate this end device is set to use.
152 *
153 * @return The data rate this device uses when transmitting.
154 */
155 uint8_t GetDataRate();
156
157 /**
158 * Get the transmission power this end device is set to use.
159 *
160 * @return The transmission ERP [dBm] this device uses when transmitting.
161 */
163
164 /**
165 * Set the transmission power of this end device.
166 *
167 * @param txPowerDbm The transmission ERP [dBm] value.
168 */
169 void SetTransmissionPowerDbm(double txPowerDbm);
170
171 /**
172 * Set the network address of this device.
173 *
174 * @param address The address to set.
175 */
177
178 /**
179 * Get the network address of this device.
180 *
181 * @return This device's address.
182 */
184
185 // void SetRx1DrOffset (uint8_t rx1DrOffset);
186
187 // uint8_t GetRx1DrOffset ();
188
189 /**
190 * Get the last known link margin from the demodulation floor.
191 *
192 * This is intended for asynchronous polling by the Application layer of the device. For
193 * synchronous behavior provide a callback using the trace system.
194 *
195 * @return The last known link margin [dB]
196 */
197 uint8_t GetLastKnownLinkMarginDb() const;
198
199 /**
200 * Get the last known number of gateways concurrently receiving transmissions from the device.
201 *
202 * This is intended for asynchronous polling by the Application layer of the device. For
203 * synchronous behavior provide a callback using the trace system.
204 *
205 * @return The last known number of receiver gateways.
206 */
207 uint8_t GetLastKnownGatewayCount() const;
208
209 /**
210 * Get the aggregated duty cycle.
211 *
212 * @return A time instance containing the aggregated duty cycle in fractional form.
213 */
214 double GetAggregatedDutyCycle();
215
216 /////////////////////////
217 // MAC command methods //
218 /////////////////////////
219
220 /**
221 * Add the necessary options and MAC commands to the LoraFrameHeader.
222 *
223 * @param frameHeader The frame header on which to apply the options.
224 */
225 void ApplyNecessaryOptions(LoraFrameHeader& frameHeader);
226
227 /**
228 * Add the necessary options and MAC commands to the LorawanMacHeader.
229 *
230 * @param macHeader The mac header on which to apply the options.
231 */
233
234 /**
235 * Set the message type to send when the Send method is called.
236 *
237 * @param mType The message type.
238 */
240
241 /**
242 * Get the message type to send when the Send method is called.
243 *
244 * @return The message type.
245 */
247
248 /**
249 * Parse and take action on the commands contained on this FrameHeader.
250 *
251 * @param frameHeader The frame header.
252 */
253 void ParseCommands(LoraFrameHeader frameHeader);
254
255 /**
256 * Perform the actions that need to be taken when receiving a LinkCheckAns command.
257 *
258 * @param margin The margin value of the command.
259 * @param gwCnt The gateway count value of the command.
260 */
261 void OnLinkCheckAns(uint8_t margin, uint8_t gwCnt);
262
263 /**
264 * Perform the actions that need to be taken when receiving a LinkAdrReq command.
265 *
266 * @param dataRate The data rate value of the command.
267 * @param txPower The transmission power value of the command.
268 * @param chMask Mask of enabled channels of the command.
269 * @param chMaskCntl Indicator of the 16 channel bank to apply the chMask to.
270 * @param nbTrans The number of repetitions prescribed by the command.
271 */
272 void OnLinkAdrReq(uint8_t dataRate,
273 uint8_t txPower,
274 uint16_t chMask,
275 uint8_t chMaskCntl,
276 uint8_t nbTrans);
277
278 /**
279 * Perform the actions that need to be taken when receiving a DutyCycleReq command.
280 *
281 * @param maxDutyCycle The aggregate duty cycle encoded by the command.
282 */
283 void OnDutyCycleReq(uint8_t maxDutyCycle);
284
285 /**
286 * Perform the actions that need to be taken when receiving a RxParamSetupReq
287 * command based on the Device's Class Type.
288 *
289 * @param rx1DrOffset The first reception window data rate offset to set.
290 * @param rx2DataRate The data rate to use for the second receive window.
291 * @param frequencyHz The frequency [Hz] to use for the second receive window.
292 */
293 virtual void OnRxParamSetupReq(uint8_t rx1DrOffset,
294 uint8_t rx2DataRate,
295 double frequencyHz) = 0;
296
297 /**
298 * Perform the actions that need to be taken when receiving a DevStatusReq command.
299 */
300 void OnDevStatusReq();
301
302 /**
303 * Perform the actions that need to be taken when receiving a NewChannelReq command.
304 *
305 * @param chIndex The ChIndex field of the received NewChannelReq command.
306 * @param frequencyHz The Frequency [Hz] field of the received NewChannelReq command.
307 * @param minDataRate The MinDR field of the received NewChannelReq command.
308 * @param maxDataRate The MaxDR field of the received NewChannelReq command.
309 */
310 void OnNewChannelReq(uint8_t chIndex,
311 uint32_t frequencyHz,
312 uint8_t minDataRate,
313 uint8_t maxDataRate);
314
315 /**
316 * Add a MAC command to the list of those that will be sent out in the next
317 * packet.
318 *
319 * @param macCommand A pointer to the MAC command.
320 */
321 void AddMacCommand(Ptr<MacCommand> macCommand);
322
323 static constexpr uint16_t ADR_ACK_LIMIT = 64; //!< ADRACKCnt threshold for setting ADRACKReq
324 static constexpr uint16_t ADR_ACK_DELAY = 32; //!< ADRACKCnt threshold for ADR backoff action
325
326 protected:
327 /**
328 * Structure representing the parameters that will be used in the
329 * retransmission procedure.
330 */
332 {
333 Time firstAttempt; //!< Timestamp of the first transmission of the packet
334 Ptr<Packet> packet = nullptr; //!< A pointer to the packet being retransmitted
335 bool waitingAck = false; //!< Whether the packet requires explicit acknowledgment
336 uint8_t retxLeft; //!< Number of retransmission attempts left
337 };
338
339 uint8_t m_nbTrans; //!< Default number of unacknowledged redundant transmissions of each packet.
340 TracedValue<uint8_t> m_dataRate; //!< The data rate this device is using to transmit.
342 m_txPowerDbm; //!< The transmission ERP [dBm] this device is currently using.
343 uint8_t m_codingRate; //!< The coding rate used by this device.
344 bool m_headerDisabled; //!< Whether or not the LoRa PHY header is disabled for communications by
345 //!< this device.
346 LoraDeviceAddress m_address; //!< The address of this device.
347
348 /**
349 * Find the minimum wait time before the next possible transmission based
350 * on end device's Class Type.
351 *
352 * @param waitTime Currently known minimum wait time, possibly raised by this function.
353 * @return The updated minimum wait time in Time format.
354 */
355 virtual Time GetNextClassTransmissionDelay(Time waitTime);
356
357 /**
358 * Find a suitable channel for transmission. The channel is chosen randomly among the
359 * ones that are available in the end device, based on their duty cycle limitations.
360 *
361 * @return A pointer to the channel.
362 */
364
365 /**
366 * The duration of a receive window in number of symbols. This should be
367 * converted to time based or the reception parameter used.
368 *
369 * The downlink preamble transmitted by the gateways contains 8 symbols.
370 * The receiver requires 5 symbols to detect the preamble and synchronize.
371 * Therefore there must be a 5 symbols overlap between the receive window
372 * and the transmitted preamble.
373 * (Ref: Recommended SX1272/76 Settings for EU868 LoRaWAN Network Operation )
374 */
376
377 /**
378 * List of the MAC commands that need to be applied to the next UL packet.
379 */
380 std::list<Ptr<MacCommand>> m_macCommandList;
381
382 /**
383 * Structure containing the retransmission parameters for this device.
384 */
386
387 /**
388 * An uniform random variable, used to randomly pick from the channel list.
389 */
391
392 /**
393 * Used to record the last reception SNR measurement to be included in the DevStatusAns.
394 */
396
397 uint16_t m_adrAckCnt; //!< ADRACKCnt counter of the number of consecutive uplinks without
398 //!< downlink reply from the server. Reset upon reception of any Class A
399 //!< downlink destined to the device.
400
401 /////////////////
402 // Callbacks //
403 /////////////////
404
405 /**
406 * The trace source fired when the transmission procedure is finished.
407 */
409
410 private:
411 /**
412 * Get the set of active transmission channels compatible with the current device data rate and
413 * transmission power.
414 *
415 * @return A (possibly empty) vector of compatible transmission channels.
416 */
417 std::vector<Ptr<LogicalLoraChannel>> GetCompatibleTxChannels();
418
419 /**
420 * Find the base minimum wait time before the next possible transmission.
421 *
422 * @return The base minimum wait time.
423 */
425
426 /**
427 * Execute ADR backoff as in LoRaWAN specification, V1.0.4 (2020)
428 */
429 void ExecuteADRBackoff();
430
431 /**
432 * Check whether the size of the application payload is under the maximum allowed.
433 *
434 * From LoRaWAN L2 1.0.4 Specification (TS001-1.0.4), Section 4.3.2: "N is the number of octets
435 * of the application payload and SHALL be equal to or less than N ≤ M − 1 − (length of FHDR in
436 * octets), where M is the maximum MACPayload length. The valid ranges of both N and M are
437 * region-specific and defined in the “LoRaWAN Regional Parameters” [RP002] document."
438 *
439 * @param appPayloadSize Number of bytes of the application payload.
440 * @param dataRate Data rate to evaluate the max MACPayload for.
441 * @return Whether the payload size is valid.
442 */
443 bool IsPayloadSizeValid(uint32_t appPayloadSize, uint8_t dataRate);
444
445 bool m_adr; //!< Uplink ADR bit contained in the FCtrl field of the LoRaWAN FHDR.
446 //!< Controlled by the device, if set to false signals the network server
447 //!< that the device may not accept attempts to control the number of
448 //!< retransmissions, the data rate, or the TX power with downlink
449 //!< LinkADRReq commands. This also allows the device's local ADR backoff
450 //!< procedure to reset configurations in case of connectivity loss.
451
452 /**
453 * The event of retransmitting a packet in a consecutive moment if an ACK is not received.
454 *
455 * This Event is used to cancel the retransmission if the ACK is found in ParseCommand function
456 * and if a newer packet is delivered from the application to be sent.
457 */
459
460 /**
461 * The event of transmitting a packet in a consecutive moment, when the duty cycle let us
462 * transmit.
463 *
464 * This Event is used to cancel the transmission of this packet if a newer packet is delivered
465 * from the application to be sent.
466 */
468
469 /**
470 * The last known link margin in dB from the demodulation floor.
471 *
472 * This value is obtained (and updated) when a LinkCheckAns Mac command is
473 * received.
474 */
476
477 /**
478 * The last known gateway count (i.e., gateways that are in communication
479 * range with this end device).
480 *
481 * This value is obtained (and updated) when a LinkCheckAns Mac command is
482 * received.
483 */
485
486 /**
487 * The aggregated duty cycle this device needs to respect across all sub-bands.
488 */
490
491 /**
492 * The message type to apply to packets sent with the Send method.
493 */
495
496 /**
497 * current value of the device frame counter.
498 */
500
501 bool m_adrAckReq; //!< ADRACKReq bit, set to 1 after ADR_ACK_LIMIT consecutive uplinks without
502 //!< downlink messages received from the server. It requests the server to
503 //!< send a downlink for keepalive purposes.
504};
505
506} // namespace lorawan
507
508} // namespace ns3
509#endif /* END_DEVICE_LORAWAN_MAC_H */
An identifier for simulation events.
Definition event-id.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:67
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
Forward calls to a chain of Callback.
Trace classes with value semantics.
a unique identifier for an interface.
Definition type-id.h:49
LorawanMacHeader::MType m_mType
The message type to apply to packets sent with the Send method.
Ptr< LogicalLoraChannel > GetRandomChannelForTx()
Find a suitable channel for transmission.
virtual void ResetRetransmissionParameters()
Reset retransmission parameters contained in the structure LoraRetxParams.
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.
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.
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 ...
bool m_adrAckReq
ADRACKReq bit, set to 1 after ADR_ACK_LIMIT consecutive uplinks without downlink messages received fr...
Ptr< UniformRandomVariable > m_uniformRV
An uniform random variable, used to randomly pick from the channel list.
static constexpr uint16_t ADR_ACK_DELAY
ADRACKCnt threshold for ADR backoff action.
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.
uint16_t m_adrAckCnt
ADRACKCnt counter of the number of consecutive uplinks without downlink reply from the server.
std::vector< Ptr< LogicalLoraChannel > > GetCompatibleTxChannels()
Get the set of active transmission channels compatible with the current device data rate and transmis...
void ParseCommands(LoraFrameHeader frameHeader)
Parse and take action on the commands contained on this FrameHeader.
bool IsPayloadSizeValid(uint32_t appPayloadSize, uint8_t dataRate)
Check whether the size of the application payload is under the maximum allowed.
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.
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.
static constexpr uint16_t ADR_ACK_LIMIT
ADRACKCnt threshold for setting ADRACKReq.
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 ExecuteADRBackoff()
Execute ADR backoff as in LoRaWAN specification, V1.0.4 (2020).
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.
LorawanMac()
Default constructor.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Send()
Send a packet.
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.