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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 * Martina Capuzzo <capuzzom@dei.unipd.it>
19 *
20 * Modified by: Peggy Anderson <peggy.anderson@usask.ca>
21 */
22
23#ifndef END_DEVICE_LORAWAN_MAC_H
24#define END_DEVICE_LORAWAN_MAC_H
25
26#include "lora-device-address.h"
27#include "lora-frame-header.h"
28#include "lorawan-mac-header.h"
29#include "lorawan-mac.h"
30
31#include "ns3/random-variable-stream.h"
32#include "ns3/traced-value.h"
33
34namespace ns3
35{
36namespace lorawan
37{
38
39/**
40 * \ingroup lorawan
41 *
42 * Class representing the MAC layer of a LoRaWAN device.
43 */
45{
46 public:
47 /**
48 * Register this type.
49 * \return The object TypeId.
50 */
51 static TypeId GetTypeId();
52
53 EndDeviceLorawanMac(); //!< Default constructor
54 ~EndDeviceLorawanMac() override; //!< Destructor
55
56 /////////////////////
57 // Sending methods //
58 /////////////////////
59
60 /**
61 * Send a packet.
62 *
63 * The MAC layer of the end device will take care of using the right parameters.
64 *
65 * \param packet The packet to send.
66 */
67 void Send(Ptr<Packet> packet) override;
68
69 /**
70 * Checking if we are performing the transmission of a new packet or a retransmission, and call
71 * SendToPhy function.
72 *
73 * \param packet The packet to send.
74 */
75 virtual void DoSend(Ptr<Packet> packet);
76
77 /**
78 * Add headers and send a packet with the sending function of the physical layer.
79 *
80 * \param packet The packet to send.
81 */
82 virtual void SendToPhy(Ptr<Packet> packet);
83
84 /**
85 * Postpone transmission to the specified time and delete previously scheduled transmissions if
86 * present.
87 *
88 * \param nextTxDelay Delay at which the transmission will be performed.
89 * \param packet The packet to delay the transmission of.
90 */
91 virtual void postponeTransmission(Time nextTxDelay, Ptr<Packet> packet);
92
93 ///////////////////////
94 // Receiving methods //
95 ///////////////////////
96
97 /**
98 * Receive a packet.
99 *
100 * This method is typically registered as a callback in the underlying PHY
101 * layer so that it's called when a packet is going up the stack.
102 *
103 * \param packet The received packet.
104 */
105 void Receive(Ptr<const Packet> packet) override;
106
107 void FailedReception(Ptr<const Packet> packet) override;
108
109 /**
110 * Perform the actions that are required after a packet send.
111 *
112 * This function handles opening of the first receive window.
113 *
114 * \param packet The packet that has just been transmitted.
115 */
116 void TxFinished(Ptr<const Packet> packet) override;
117
118 /////////////////////////
119 // Getters and Setters //
120 /////////////////////////
121
122 /**
123 * Reset retransmission parameters contained in the structure LoraRetxParams.
124 */
125 virtual void resetRetransmissionParameters();
126
127 /**
128 * Enable data rate adaptation in the retransmitting procedure.
129 *
130 * \param adapt If the data rate adaptation is enabled or not.
131 */
132 void SetDataRateAdaptation(bool adapt);
133
134 /**
135 * Get if data rate adaptation is enabled or not.
136 *
137 * \return True if the data rate adaptation is enabled, false if disabled.
138 */
139 bool GetDataRateAdaptation() const;
140
141 /**
142 * Set the max number of unacknowledged redundant transmissions of each packet. If,
143 * after a transmission, any acknowledgement is received, no more are sent for that packet.
144 *
145 * \param maxNumbTx The number of transmissions.
146 */
147 void SetMaxNumberOfTransmissions(uint8_t maxNumbTx);
148
149 /**
150 * Get the max number of unacknowledged redundant transmissions of each packet. If,
151 * after a transmission, any acknowledgement is received, no more are sent for that packet.
152 *
153 * \return The number of transmissions as uint8_t.
154 */
156
157 /**
158 * Set the data rate this end device will use when transmitting. For End
159 * Devices, this value is assumed to be fixed, and can be modified via MAC
160 * commands issued by the gateway.
161 *
162 * \param dataRate The dataRate to use when transmitting.
163 */
164 void SetDataRate(uint8_t dataRate);
165
166 /**
167 * Get the data rate this end device is set to use.
168 *
169 * \return The data rate this device uses when transmitting.
170 */
171 uint8_t GetDataRate();
172
173 /**
174 * Get the transmission power this end device is set to use.
175 *
176 * \return The transmission power this device uses when transmitting.
177 */
178 virtual uint8_t GetTransmissionPower();
179
180 /**
181 * Set the network address of this device.
182 *
183 * \param address The address to set.
184 */
186
187 /**
188 * Get the network address of this device.
189 *
190 * \return This device's address.
191 */
193
194 ///**
195 // * Set a value for the RX1DROffset parameter.
196 // *
197 // * This value determines the offset to use when deciding the data rate of the
198 // * downlink transmission during the first receive window from the
199 // * replyDataRateMatrix.
200 // *
201 // * \param rx1DrOffset The value to set for the offset.
202 // */
203 // void SetRx1DrOffset (uint8_t rx1DrOffset);
204
205 ///**
206 // * Get the value of the RX1DROffset parameter.
207 // *
208 // * \return The value of the RX1DROffset parameter.
209 // */
210 // uint8_t GetRx1DrOffset ();
211
212 /**
213 * Get the aggregated duty cycle.
214 *
215 * \return A time instance containing the aggregated duty cycle in fractional.
216 * form.
217 */
218 double GetAggregatedDutyCycle();
219
220 /////////////////////////
221 // MAC command methods //
222 /////////////////////////
223
224 /**
225 * Add the necessary options and MAC commands to the LoraFrameHeader.
226 *
227 * \param frameHeader The frame header on which to apply the options.
228 */
229 void ApplyNecessaryOptions(LoraFrameHeader& frameHeader);
230
231 /**
232 * Add the necessary options and MAC commands to the LorawanMacHeader.
233 *
234 * \param macHeader The mac header on which to apply the options.
235 */
237
238 /**
239 * Set the message type to send when the Send method is called.
240 *
241 * \param mType The message type.
242 */
244
245 /**
246 * Get the message type to send when the Send method is called.
247 *
248 * \return The message type.
249 */
251
252 /**
253 * Parse and take action on the commands contained on this FrameHeader.
254 *
255 * \param frameHeader The frame header.
256 */
257 void ParseCommands(LoraFrameHeader frameHeader);
258
259 /**
260 * Perform the actions that need to be taken when receiving a LinkCheckAns command.
261 *
262 * \param margin The margin value of the command.
263 * \param gwCnt The gateway count value of the command.
264 */
265 void OnLinkCheckAns(uint8_t margin, uint8_t gwCnt);
266
267 /**
268 * Perform the actions that need to be taken when receiving a LinkAdrReq command.
269 *
270 * \param dataRate The data rate value of the command.
271 * \param txPower The transmission power value of the command.
272 * \param enabledChannels A list of the enabled channels.
273 * \param repetitions The number of repetitions prescribed by the command.
274 */
275 void OnLinkAdrReq(uint8_t dataRate,
276 uint8_t txPower,
277 std::list<int> enabledChannels,
278 int repetitions);
279
280 /**
281 * Perform the actions that need to be taken when receiving a DutyCycleReq command.
282 *
283 * \param dutyCycle The aggregate duty cycle prescribed by the command, in
284 * fraction form.
285 */
286 void OnDutyCycleReq(double dutyCycle);
287
288 /**
289 * Perform the actions that need to be taken when receiving a RxParamSetupReq command.
290 *
291 * \param rxParamSetupReq The Parameter Setup Request.
292 */
293 void OnRxParamSetupReq(Ptr<RxParamSetupReq> rxParamSetupReq);
294
295 /**
296 * Perform the actions that need to be taken when receiving a RxParamSetupReq
297 * command based on the Device's Class Type.
298 *
299 * \param rxParamSetupReq The Parameter Setup Request.
300 */
301 virtual void OnRxClassParamSetupReq(Ptr<RxParamSetupReq> rxParamSetupReq);
302
303 /**
304 * Perform the actions that need to be taken when receiving a DevStatusReq command.
305 */
306 void OnDevStatusReq();
307
308 /**
309 * Perform the actions that need to be taken when receiving a NewChannelReq command.
310 *
311 * \param chIndex The ChIndex field of the received NewChannelReq command.
312 * \param frequency The Frequency field of the received NewChannelReq command.
313 * \param minDataRate The MinDR field of the received NewChannelReq command.
314 * \param maxDataRate The MaxDR field of the received NewChannelReq command.
315 */
316 void OnNewChannelReq(uint8_t chIndex,
317 double frequency,
318 uint8_t minDataRate,
319 uint8_t maxDataRate);
320
321 ////////////////////////////////////
322 // Logical channel administration //
323 ////////////////////////////////////
324
325 /**
326 * Add a logical channel to the helper.
327 *
328 * \param frequency The channel's center frequency.
329 */
330 void AddLogicalChannel(double frequency);
331
332 /**
333 * Set a new logical channel in the helper.
334 *
335 * \param chIndex The channel's new index.
336 * \param frequency The channel's center frequency.
337 * \param minDataRate The minimum data rate allowed on the channel.
338 * \param maxDataRate The maximum data rate allowed on the channel.
339 */
340 void SetLogicalChannel(uint8_t chIndex,
341 double frequency,
342 uint8_t minDataRate,
343 uint8_t maxDataRate);
344
345 /**
346 * Add a logical channel to the helper.
347 *
348 * \param logicalChannel The logical channel to add.
349 */
350 void AddLogicalChannel(Ptr<LogicalLoraChannel> logicalChannel);
351
352 /**
353 * Add a subband to the logical channel helper.
354 *
355 * \param startFrequency The SubBand's lowest frequency.
356 * \param endFrequency The SubBand's highest frequency.
357 * \param dutyCycle The SubBand's duty cycle, in fraction form.
358 * \param maxTxPowerDbm The maximum transmission power allowed on the SubBand.
359 */
360 void AddSubBand(double startFrequency,
361 double endFrequency,
362 double dutyCycle,
363 double maxTxPowerDbm);
364
365 /**
366 * Add a MAC command to the list of those that will be sent out in the next
367 * packet.
368 *
369 * \param macCommand A pointer to the MAC command.
370 */
371 void AddMacCommand(Ptr<MacCommand> macCommand);
372
373 protected:
374 /**
375 * Structure representing the parameters that will be used in the
376 * retransmission procedure.
377 */
379 {
380 Time firstAttempt; //!< Timestamp of the first transmission of the packet
381 Ptr<Packet> packet = nullptr; //!< A pointer to the packet being retransmitted
382 bool waitingAck = false; //!< Whether the packet requires explicit acknowledgment
383 uint8_t retxLeft; //!< Number of retransmission attempts left
384 };
385
386 bool
387 m_enableDRAdapt; //!< Enable data rate adaptation (ADR) during the retransmission procedure.
388 uint8_t
389 m_maxNumbTx; //!< Default number of unacknowledged redundant transmissions of each packet.
390 TracedValue<uint8_t> m_dataRate; //!< The data rate this device is using to transmit.
391 TracedValue<double> m_txPower; //!< The transmission power this device is using to transmit.
392 uint8_t m_codingRate; //!< The coding rate used by this device.
393 bool m_headerDisabled; //!< Whether or not the LoRa PHY header is disabled for communications by
394 //!< this device.
395 LoraDeviceAddress m_address; //!< The address of this device.
396
397 /**
398 * Find the minimum waiting time before the next possible transmission based
399 * on end device's Class Type.
400 *
401 * \param waitingTime Currently known minimum waiting time, possibly raised by this function.
402 * \return The updated minimum waiting time in Time format.
403 */
404 virtual Time GetNextClassTransmissionDelay(Time waitingTime);
405
406 /**
407 * Find a suitable channel for transmission. The channel is chosen among the
408 * ones that are available in the end device, based on their duty
409 * cycle limitations.
410 *
411 * \return A pointer to the channel.
412 */
414
415 /**
416 * The duration of a receive window in number of symbols. This should be
417 * converted to time based or the reception parameter used.
418 *
419 * The downlink preamble transmitted by the gateways contains 8 symbols.
420 * The receiver requires 5 symbols to detect the preamble and synchronize.
421 * Therefore there must be a 5 symbols overlap between the receive window
422 * and the transmitted preamble.
423 * (Ref: Recommended SX1272/76 Settings for EU868 LoRaWAN Network Operation )
424 */
426
427 /**
428 * List of the MAC commands that need to be applied to the next UL packet.
429 */
430 std::list<Ptr<MacCommand>> m_macCommandList;
431
432 /**
433 * Structure containing the retransmission parameters for this device.
434 */
436
437 /**
438 * An uniform random variable, used by the Shuffle method to randomly reorder
439 * the channel list.
440 */
442
443 /////////////////
444 // Callbacks //
445 /////////////////
446
447 /**
448 * The trace source fired when the transmission procedure is finished.
449 */
451
452 private:
453 /**
454 * Randomly shuffle a Ptr<LogicalLoraChannel> vector.
455 *
456 * Used to pick a random channel on which to send the packet.
457 *
458 * \param vector The vector of pointers to logical LoRa channels.
459 * \return The shuffled vector.
460 */
461 std::vector<Ptr<LogicalLoraChannel>> Shuffle(std::vector<Ptr<LogicalLoraChannel>> vector);
462
463 /**
464 * Find the base minimum waiting time before the next possible transmission.
465 *
466 * \return The base minimum waiting time.
467 */
469
470 /**
471 * Whether this device's data rate should be controlled by the network server.
472 */
474
475 /**
476 * The event of retransmitting a packet in a consecutive moment if an ACK is not received.
477 *
478 * This Event is used to cancel the retransmission if the ACK is found in ParseCommand function
479 * and if a newer packet is delivered from the application to be sent.
480 */
482
483 /**
484 * The event of transmitting a packet in a consecutive moment, when the duty cycle let us
485 * transmit.
486 *
487 * This Event is used to cancel the transmission of this packet if a newer packet is delivered
488 * from the application to be sent.
489 */
491
492 /**
493 * The last known link margin.
494 *
495 * This value is obtained (and updated) when a LinkCheckAns Mac command is
496 * received.
497 */
499
500 /**
501 * The last known gateway count (i.e., gateways that are in communication
502 * range with this end device).
503 *
504 * This value is obtained (and updated) when a LinkCheckAns Mac command is
505 * received.
506 */
508
509 /**
510 * The aggregated duty cycle this device needs to respect across all sub-bands.
511 */
513
514 /**
515 * The message type to apply to packets sent with the Send method.
516 */
518
519 /**
520 * current value of the device frame counter.
521 */
523};
524
525} // namespace lorawan
526
527} // namespace ns3
528#endif /* END_DEVICE_LORAWAN_MAC_H */
An identifier for simulation events.
Definition: event-id.h:56
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
Trace classes with value semantics.
Definition: traced-value.h:116
a unique identifier for an interface.
Definition: type-id.h:59
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.
uint8_t m_maxNumbTx
Default number of unacknowledged redundant transmissions of each packet.
TracedValue< int > m_lastKnownGatewayCount
The last known gateway count (i.e., gateways that are in communication range with this end device).
bool GetDataRateAdaptation() const
Get if data rate adaptation is enabled or not.
void SetLogicalChannel(uint8_t chIndex, double frequency, uint8_t minDataRate, uint8_t maxDataRate)
Set a new logical channel in the helper.
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...
virtual void OnRxClassParamSetupReq(Ptr< RxParamSetupReq > rxParamSetupReq)
Perform the actions that need to be taken when receiving a RxParamSetupReq command based on the Devic...
void AddSubBand(double startFrequency, double endFrequency, double dutyCycle, double maxTxPowerDbm)
Add a subband to the logical channel helper.
bool m_headerDisabled
Whether or not the LoRa PHY header is disabled for communications by this device.
bool m_controlDataRate
Whether this device's data rate should be controlled by the network server.
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.
void OnDutyCycleReq(double dutyCycle)
Perform the actions that need to be taken when receiving a DutyCycleReq command.
TracedValue< double > m_txPower
The transmission power this device is using to transmit.
uint8_t m_codingRate
The coding rate used by this device.
bool m_enableDRAdapt
Enable data rate adaptation (ADR) during the retransmission procedure.
void TxFinished(Ptr< const Packet > packet) override
Perform the actions that are required after a packet send.
Ptr< UniformRandomVariable > m_uniformRV
An uniform random variable, used by the Shuffle method to randomly reorder the channel list.
void OnRxParamSetupReq(Ptr< RxParamSetupReq > rxParamSetupReq)
Perform the actions that need to be taken when receiving a RxParamSetupReq command.
static TypeId GetTypeId()
Register this type.
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.
void Receive(Ptr< const Packet > packet) override
Receive a packet.
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.
virtual Time GetNextClassTransmissionDelay(Time waitingTime)
Find the minimum waiting time before the next possible transmission based on end device's Class Type.
void ParseCommands(LoraFrameHeader frameHeader)
Parse and take action on the commands contained on this FrameHeader.
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.
virtual uint8_t GetTransmissionPower()
Get the transmission power this end device is set to use.
uint8_t GetMaxNumberOfTransmissions()
Get the max number of unacknowledged redundant transmissions of each packet.
void SetDataRateAdaptation(bool adapt)
Enable data rate adaptation in the retransmitting procedure.
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 OnLinkAdrReq(uint8_t dataRate, uint8_t txPower, std::list< int > enabledChannels, int repetitions)
Perform the actions that need to be taken when receiving a LinkAdrReq command.
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.
void Send(Ptr< Packet > packet) override
Send a packet.
uint16_t m_currentFCnt
current value of the device frame counter.
void AddMacCommand(Ptr< MacCommand > macCommand)
Add a MAC command to the list of those that will be sent out in the next packet.
void SetMaxNumberOfTransmissions(uint8_t maxNumbTx)
Set the max number of unacknowledged redundant transmissions of each packet.
TracedValue< double > m_lastKnownLinkMargin
The last known link margin.
void AddLogicalChannel(double frequency)
Add a logical channel to the helper.
Time GetNextTransmissionDelay()
Find the base minimum waiting time before the next possible transmission.
std::vector< Ptr< LogicalLoraChannel > > Shuffle(std::vector< Ptr< LogicalLoraChannel > > vector)
Randomly shuffle a Ptr<LogicalLoraChannel> vector.
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.
void OnNewChannelReq(uint8_t chIndex, double frequency, uint8_t minDataRate, uint8_t maxDataRate)
Perform the actions that need to be taken when receiving a NewChannelReq command.
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.
uint8_t GetDataRate()
Get the data rate this end device is set to use.
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:48
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.