A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 */
8
9#ifndef LORAWAN_MAC_H
10#define LORAWAN_MAC_H
11
13
14#include "ns3/net-device.h"
15#include "ns3/object.h"
16#include "ns3/packet.h"
17#include "ns3/traced-callback.h"
18
19namespace ns3
20{
21namespace lorawan
22{
23
24class LoraPhy;
25
26/**
27 * @ingroup lorawan
28 *
29 * Class representing the LoRaWAN MAC layer.
30 *
31 * This class is meant to be extended differently based on whether the layer
32 * belongs to an end device or a gateway, while holding some functionality that
33 * is common to both.
34 */
35class LorawanMac : public Object
36{
37 public:
38 /**
39 * This type defines the callback of a higher layer that a LorawanMac(-derived) object invokes
40 * to pass a packet up the stack.
41 *
42 * @param packet the packet that has been received.
43 */
45
46 /**
47 * Matrix structure to store possible data rate value to be used by a LoRaWAN end device for
48 * listening during the RX1 receive window. It is a function of the uplink data rate and the
49 * RX1DROffset [0:5].
50 */
51 typedef std::array<std::array<uint8_t, 6>, 8> ReplyDataRateMatrix;
52
53 /**
54 * Register this type.
55 * @return The object TypeId.
56 */
57 static TypeId GetTypeId();
58
59 LorawanMac(); //!< Default constructor
60 ~LorawanMac() override; //!< Destructor
61
62 /**
63 * Set the underlying PHY layer.
64 *
65 * @param phy The phy layer.
66 */
67 void SetPhy(Ptr<LoraPhy> phy);
68
69 /**
70 * Get the underlying PHY layer.
71 *
72 * @return The PHY layer that this MAC is connected to.
73 */
74 Ptr<LoraPhy> GetPhy() const;
75
76 /**
77 * Send a packet.
78 *
79 * @param packet The packet to send.
80 */
81 virtual void Send(Ptr<Packet> packet) = 0;
82
83 /**
84 * Perform actions after sending a packet.
85 *
86 * This method is typically registered as a callback in the underlying PHY
87 * layer so that it's called when a packet transmission concludes.
88 *
89 * @param packet The packet that has just been sent.
90 */
91 virtual void TxFinished(Ptr<const Packet> packet) = 0;
92
93 /**
94 * Receive a packet from the lower layer.
95 *
96 * This method is typically registered as a callback in the underlying PHY
97 * layer so that it's called when a packet is going up the stack.
98 *
99 * @param packet The received packet.
100 */
101 virtual void Receive(Ptr<const Packet> packet) = 0;
102
103 /**
104 * Inform this layer that reception of a packet we were locked on failed.
105 *
106 * This method is typically registered as a callback in the underlying PHY
107 * layer so that it's called when a packet reception fails.
108 *
109 * @param packet The packet we failed to receive.
110 */
111 virtual void FailedReception(Ptr<const Packet> packet) = 0;
112
113 /**
114 * Set the callback to be used to notify higher layers when a packet has been
115 * received.
116 *
117 * @param cb callback to invoke whenever a packet has been received and must
118 * be forwarded to the higher layers.
119 */
121
122 /**
123 * Set the device this MAC layer is installed on.
124 *
125 * @param device The NetDevice this MAC layer will refer to.
126 */
127 void SetDevice(Ptr<NetDevice> device);
128
129 /**
130 * Get the device this MAC layer is installed on.
131 *
132 * @return The NetDevice this MAC layer will refer to.
133 */
135
136 /**
137 * Get the logical lora channel helper associated with this MAC.
138 *
139 * @return A Ptr to the instance of LogicalLoraChannelHelper that this MAC is using.
140 */
142
143 /**
144 * Set the LogicalLoraChannelHelper this MAC instance will use.
145 *
146 * @param helper A Ptr to the instance of the helper to use.
147 */
149
150 /**
151 * Get the spreading factor corresponding to a data rate, based on this MAC's region.
152 *
153 * @param dataRate The data rate we need to convert to a Spreading Factor
154 * value.
155 * @return The spreading factor that corresponds to a data rate in this MAC's region, or 0
156 * if the dataRate is not valid.
157 */
158 uint8_t GetSfFromDataRate(uint8_t dataRate) const;
159
160 /**
161 * Get the bandwidth corresponding to a data rate, based on this MAC's region.
162 *
163 * @param dataRate The data rate we need to convert to a bandwidth value.
164 * @return The bandwidth (Hz) that corresponds to the parameter data rate in this
165 * MAC's region, or 0 if the dataRate is not valid.
166 */
167 uint32_t GetBandwidthFromDataRate(uint8_t dataRate) const;
168
169 /**
170 * Get the transmission power in dBm that corresponds, in this region, to the
171 * encoded 8-bit txPower.
172 *
173 * @param txPower The 8-bit encoded txPower to convert.
174 *
175 * @return The corresponding transmission power in dBm ERP, or -1 if the encoded
176 * power was not recognized as valid.
177 */
178 double GetDbmForTxPower(uint8_t txPower) const;
179
180 /**
181 * Set the vector to use to check up correspondence between spreading factor and data rate.
182 *
183 * @param sfForDataRate A vector that contains at position i the spreading factor that
184 * should correspond to data rate i.
185 */
186 void SetSfForDataRate(std::vector<uint8_t> sfForDataRate);
187
188 /**
189 * Set the vector to use to check up correspondence between bandwidth and
190 * data rate.
191 *
192 * @param bandwidthForDataRate A vector that contains at position i the
193 * bandwidth (Hz) that should correspond to data rate i in this MAC's region.
194 */
195 void SetBandwidthForDataRate(std::vector<uint32_t> bandwidthForDataRate);
196
197 /**
198 * Set the maximum LoRaWAN MACPayload size for a set data rate.
199 *
200 * @param maxMacPayloadForDataRate A vector that contains at position i the
201 * maximum LoRaWAN MACPayload that should correspond to data rate i in this
202 * MAC's region. See, LoRaWAN RP002-1.0.4
203 */
204 void SetMaxMacPayloadForDataRate(std::vector<uint32_t> maxMacPayloadForDataRate);
205
206 /**
207 * Set the vector to use to check up which transmission power in Dbm
208 * corresponds to a certain TxPower value in this MAC's region.
209 *
210 * @param txDbmForTxPower A vector that contains at position i the
211 * transmission power in dBm that should correspond to a TXPOWER value of i in
212 * this MAC's region.
213 */
214 void SetTxDbmForTxPower(std::vector<double> txDbmForTxPower);
215
216 /**
217 * Set the matrix to use when deciding with which data rate to respond. Region
218 * based.
219 *
220 * @param replyDataRateMatrix A matrix containing the reply DataRates, based
221 * on the sending data rate and on the value of the RX1DROffset parameter.
222 */
223 void SetReplyDataRateMatrix(ReplyDataRateMatrix replyDataRateMatrix);
224
225 /**
226 * Set the number of PHY preamble symbols this MAC is set to use.
227 *
228 * @param nPreambleSymbols The number of preamble symbols to use (typically 8).
229 */
230 void SetNPreambleSymbols(int nPreambleSymbols);
231
232 /**
233 * Get the number of PHY preamble symbols this MAC is set to use.
234 *
235 * @return The number of preamble symbols to use (typically 8).
236 */
237 int GetNPreambleSymbols() const;
238
239 protected:
240 ReceiveCallback m_receiveCallback; ///<! Callback to forward to upper layers
241
242 /**
243 * The trace source that is fired when a packet cannot be sent because of duty
244 * cycle limitations.
245 */
247
248 /**
249 * Trace source that is fired when a packet reaches the MAC layer.
250 */
252
253 /**
254 * Trace source that is fired when a new APP layer packet arrives at the MAC
255 * layer.
256 */
258
259 /**
260 * The PHY instance that sits under this MAC layer.
261 */
263
264 /**
265 * The device this MAC layer is installed on.
266 */
268
269 /**
270 * The LogicalLoraChannelHelper instance that is assigned to this MAC.
271 */
273
274 /**
275 * A vector holding the spreading factor each data rate corresponds to.
276 */
277 std::vector<uint8_t> m_sfForDataRate;
278
279 /**
280 * A vector holding the bandwidth each data rate corresponds to.
281 */
282 std::vector<uint32_t> m_bandwidthForDataRate;
283
284 /**
285 * A vector holding the maximum MACPayload size that corresponds to a
286 * certain data rate. See, LoRaWAN RP002-1.0.4
287 */
288 std::vector<uint32_t> m_maxMacPayloadForDataRate;
289
290 /**
291 * The number of symbols to use in the PHY preamble.
292 */
294
295 /**
296 * A vector holding the power that corresponds to a certain TxPower value.
297 */
298 std::vector<double> m_txDbmForTxPower;
299
300 /**
301 * The matrix that decides the data rate the gateway will use in a reply based on the end
302 * device's sending data rate and on the value of the RX1DROffset parameter.
303 */
305};
306
307} // namespace lorawan
308} // namespace ns3
309
310#endif /* LORAWAN_MAC_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
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition type-id.h:50
Base class for PHY layers implementing the LoRa modulation scheme.
Definition lora-phy.h:102
std::vector< double > m_txDbmForTxPower
A vector holding the power that corresponds to a certain TxPower value.
Ptr< LoraPhy > GetPhy() const
Get the underlying PHY layer.
TracedCallback< Ptr< const Packet > > m_cannotSendBecauseDutyCycle
The trace source that is fired when a packet cannot be sent because of duty cycle limitations.
std::vector< uint8_t > m_sfForDataRate
A vector holding the spreading factor each data rate corresponds to.
void SetSfForDataRate(std::vector< uint8_t > sfForDataRate)
Set the vector to use to check up correspondence between spreading factor and data rate.
LorawanMac()
Default constructor.
TracedCallback< Ptr< const Packet > > m_receivedPacket
Trace source that is fired when a packet reaches the MAC layer.
void SetDevice(Ptr< NetDevice > device)
Set the device this MAC layer is installed on.
virtual void FailedReception(Ptr< const Packet > packet)=0
Inform this layer that reception of a packet we were locked on failed.
void SetMaxMacPayloadForDataRate(std::vector< uint32_t > maxMacPayloadForDataRate)
Set the maximum LoRaWAN MACPayload size for a set data rate.
~LorawanMac() override
Destructor.
void SetPhy(Ptr< LoraPhy > phy)
Set the underlying PHY layer.
void SetBandwidthForDataRate(std::vector< uint32_t > bandwidthForDataRate)
Set the vector to use to check up correspondence between bandwidth and data rate.
Callback< void, Ptr< Packet > > ReceiveCallback
This type defines the callback of a higher layer that a LorawanMac(-derived) object invokes to pass a...
Definition lorawan-mac.h:44
void SetTxDbmForTxPower(std::vector< double > txDbmForTxPower)
Set the vector to use to check up which transmission power in Dbm corresponds to a certain TxPower va...
virtual void Send(Ptr< Packet > packet)=0
Send a packet.
ReplyDataRateMatrix m_replyDataRateMatrix
The matrix that decides the data rate the gateway will use in a reply based on the end device's sendi...
std::vector< uint32_t > m_maxMacPayloadForDataRate
A vector holding the maximum MACPayload size that corresponds to a certain data rate.
static TypeId GetTypeId()
Register this type.
uint32_t GetBandwidthFromDataRate(uint8_t dataRate) const
Get the bandwidth corresponding to a data rate, based on this MAC's region.
virtual void Receive(Ptr< const Packet > packet)=0
Receive a packet from the lower layer.
void SetLogicalLoraChannelHelper(Ptr< LogicalLoraChannelHelper > helper)
Set the LogicalLoraChannelHelper this MAC instance will use.
uint8_t GetSfFromDataRate(uint8_t dataRate) const
Get the spreading factor corresponding to a data rate, based on this MAC's region.
Ptr< LogicalLoraChannelHelper > m_channelHelper
The LogicalLoraChannelHelper instance that is assigned to this MAC.
ReceiveCallback m_receiveCallback
! Callback to forward to upper layers
int GetNPreambleSymbols() const
Get the number of PHY preamble symbols this MAC is set to use.
Ptr< NetDevice > GetDevice() const
Get the device this MAC layer is installed on.
virtual void TxFinished(Ptr< const Packet > packet)=0
Perform actions after sending a packet.
Ptr< LogicalLoraChannelHelper > GetLogicalLoraChannelHelper() const
Get the logical lora channel helper associated with this MAC.
double GetDbmForTxPower(uint8_t txPower) const
Get the transmission power in dBm that corresponds, in this region, to the encoded 8-bit txPower.
std::vector< uint32_t > m_bandwidthForDataRate
A vector holding the bandwidth each data rate corresponds to.
TracedCallback< Ptr< const Packet > > m_sentNewPacket
Trace source that is fired when a new APP layer packet arrives at the MAC layer.
Ptr< LoraPhy > m_phy
The PHY instance that sits under this MAC layer.
void SetNPreambleSymbols(int nPreambleSymbols)
Set the number of PHY preamble symbols this MAC is set to use.
void SetReplyDataRateMatrix(ReplyDataRateMatrix replyDataRateMatrix)
Set the matrix to use when deciding with which data rate to respond.
void SetReceiveCallback(ReceiveCallback cb)
Set the callback to be used to notify higher layers when a packet has been received.
Ptr< NetDevice > m_device
The device this MAC layer is installed on.
int m_nPreambleSymbols
The number of symbols to use in the PHY preamble.
std::array< std::array< uint8_t, 6 >, 8 > ReplyDataRateMatrix
Matrix structure to store possible data rate value to be used by a LoRaWAN end device for listening d...
Definition lorawan-mac.h:51
Every class exported by the ns3 library is enclosed in the ns3 namespace.