lorawan-mac.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2017 University of Padova
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Davide Magrin <magrinda@dei.unipd.it>
19  *
20  * Modified by: Bastien Tauran <bastien.tauran@viveris.fr>
21  */
22 
23 #include "lorawan-mac.h"
24 
25 #include <ns3/log.h>
26 
27 #include <algorithm>
28 #include <cmath>
29 #include <vector>
30 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("LorawanMac");
35 
36 NS_OBJECT_ENSURE_REGISTERED(LorawanMac);
37 
38 TypeId
40 {
41  static TypeId tid =
42  TypeId("ns3::LorawanMac")
43  .SetParent<SatMac>()
44  .AddTraceSource("SentNewPacket",
45  "Trace source indicating a new packet "
46  "arrived at the MAC layer",
47  MakeTraceSourceAccessor(&LorawanMac::m_sentNewPacket),
48  "ns3::Packet::TracedCallback")
49  .AddTraceSource("ReceivedPacket",
50  "Trace source indicating a packet "
51  "was correctly received at the MAC layer",
52  MakeTraceSourceAccessor(&LorawanMac::m_receivedPacket),
53  "ns3::Packet::TracedCallback")
54  .AddTraceSource("CannotSendBecauseDutyCycle",
55  "Trace source indicating a packet "
56  "could not be sent immediately because of duty cycle limitations",
57  MakeTraceSourceAccessor(&LorawanMac::m_cannotSendBecauseDutyCycle),
58  "ns3::Packet::TracedCallback");
59  return tid;
60 }
61 
63  : m_isRegenerative(false)
64 {
65  NS_FATAL_ERROR("Default constructor not in use");
66 }
67 
68 LorawanMac::LorawanMac(uint32_t satId, uint32_t beamId)
69  : SatMac(satId, beamId),
70  m_beamId(beamId),
71  m_isRegenerative(false)
72 {
73  NS_LOG_FUNCTION(this);
74 }
75 
77 {
78  NS_LOG_FUNCTION(this);
79 }
80 
81 void
82 LorawanMac::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
83 {
84  Send(packet);
85 }
86 
87 void
88 LorawanMac::SetDevice(Ptr<NetDevice> device)
89 {
90  m_device = device;
91 }
92 
93 Ptr<NetDevice>
95 {
96  return m_device;
97 }
98 
99 Ptr<SatPhy>
101 {
102  return m_phy;
103 }
104 
105 void
106 LorawanMac::SetPhy(Ptr<SatPhy> phy)
107 {
108  // Set the phy
109  m_phy = phy;
110  Ptr<SatLoraPhyTx> phyLoraTx = DynamicCast<SatLoraPhyTx>(m_phy->GetPhyTx());
111  NS_ASSERT_MSG(phyLoraTx != nullptr, "Lorawan MAC does not have a SatLoraPhyTx instance");
112  phyLoraTx->SetTxFinishedCallback(MakeCallback(&LorawanMac::TxFinished, this));
113 }
114 
117 {
118  return m_channelHelper;
119 }
120 
121 void
123 {
124  m_channelHelper = helper;
125 }
126 
127 uint8_t
129 {
130  NS_LOG_FUNCTION(this << unsigned(dataRate));
131 
132  // Check we are in range
133  if (dataRate >= m_sfForDataRate.size())
134  {
135  return 0;
136  }
137 
138  return m_sfForDataRate.at(dataRate);
139 }
140 
141 double
143 {
144  NS_LOG_FUNCTION(this << unsigned(dataRate));
145 
146  // Check we are in range
147  if (dataRate > m_bandwidthForDataRate.size())
148  {
149  return 0;
150  }
151 
152  return m_bandwidthForDataRate.at(dataRate);
153 }
154 
155 double
157 {
158  NS_LOG_FUNCTION(this << unsigned(txPower));
159 
160  if (txPower > m_txDbmForTxPower.size())
161  {
162  return 0;
163  }
164 
165  return m_txDbmForTxPower.at(txPower);
166 }
167 
168 Time
169 LorawanMac::GetOnAirTime(Ptr<Packet> packet, LoraTxParameters txParams)
170 {
171  NS_LOG_FUNCTION(packet);
172 
173  // The contents of this function are based on [1].
174  // [1] SX1272 LoRa modem designer's guide.
175 
176  // Compute the symbol duration
177  // Bandwidth is in Hz
178  double tSym = pow(2, int(txParams.sf)) / (txParams.bandwidthHz);
179 
180  // Compute the preamble duration
181  double tPreamble = (double(txParams.nPreamble) + 4.25) * tSym;
182 
183  // Payload size
184  uint32_t pl = packet->GetSize(); // Size in bytes
185  NS_LOG_DEBUG("Packet of size " << pl << " bytes");
186 
187  // This step is needed since the formula deals with double values.
188  // de = 1 when the low data rate optimization is enabled, 0 otherwise
189  // h = 1 when header is implicit, 0 otherwise
190  double de = txParams.lowDataRateOptimizationEnabled ? 1 : 0;
191  double h = txParams.headerDisabled ? 1 : 0;
192  double crc = txParams.crcEnabled ? 1 : 0;
193 
194  // num and den refer to numerator and denominator of the time on air formula
195  double num = 8 * pl - 4 * txParams.sf + 28 + 16 * crc - 20 * h;
196  double den = (txParams.sf - 2 * de) * txParams.codingRate;
197  double payloadSymbNb = 8 + std::max(std::ceil(num / den), double(0));
198 
199  // Time to transmit the payload
200  double tPayload = payloadSymbNb * tSym;
201 
202  NS_LOG_DEBUG("Time computation: num = " << num << ", den = " << den << ", payloadSymbNb = "
203  << payloadSymbNb << ", tSym = " << tSym);
204  NS_LOG_DEBUG("tPreamble = " << tPreamble);
205  NS_LOG_DEBUG("tPayload = " << tPayload);
206  NS_LOG_DEBUG("Total time = " << tPreamble + tPayload);
207 
208  // Compute and return the total packet on-air time
209  return Seconds(tPreamble + tPayload);
210 }
211 
212 void
213 LorawanMac::SetSfForDataRate(std::vector<uint8_t> sfForDataRate)
214 {
215  m_sfForDataRate = sfForDataRate;
216 }
217 
218 void
219 LorawanMac::SetBandwidthForDataRate(std::vector<double> bandwidthForDataRate)
220 {
221  m_bandwidthForDataRate = bandwidthForDataRate;
222 }
223 
224 void
225 LorawanMac::SetMaxAppPayloadForDataRate(std::vector<uint32_t> maxAppPayloadForDataRate)
226 {
227  m_maxAppPayloadForDataRate = maxAppPayloadForDataRate;
228 }
229 
230 void
231 LorawanMac::SetTxDbmForTxPower(std::vector<double> txDbmForTxPower)
232 {
233  m_txDbmForTxPower = txDbmForTxPower;
234 }
235 
236 void
237 LorawanMac::SetNPreambleSymbols(int nPreambleSymbols)
238 {
239  m_nPreambleSymbols = nPreambleSymbols;
240 }
241 
242 int
244 {
245  return m_nPreambleSymbols;
246 }
247 
248 void
249 LorawanMac::setRegenerative(bool isRegenerative)
250 {
251  NS_LOG_FUNCTION(this << isRegenerative);
252  m_isRegenerative = isRegenerative;
253 }
254 
255 void
257 {
258  m_replyDataRateMatrix = replyDataRateMatrix;
259 }
260 } // namespace ns3
This class supports LorawanMac instances by managing a list of the logical channels that the device i...
TracedCallback< Ptr< const Packet > > m_receivedPacket
Trace source that is fired when a packet reaches the MAC layer.
Definition: lorawan-mac.h:270
ReplyDataRateMatrix m_replyDataRateMatrix
The matrix that decides the DR the GW will use in a reply based on the ED's sending DR and on the val...
Definition: lorawan-mac.h:323
void SetReplyDataRateMatrix(ReplyDataRateMatrix replyDataRateMatrix)
Set the matrix to use when deciding with which DataRate to respond.
Definition: lorawan-mac.cc:256
virtual ~LorawanMac()
Definition: lorawan-mac.cc:76
std::vector< uint32_t > m_maxAppPayloadForDataRate
A vector holding the maximum app payload size that corresponds to a certain DataRate.
Definition: lorawan-mac.h:307
std::vector< double > m_bandwidthForDataRate
A vector holding the bandwidth each Data Rate corresponds to.
Definition: lorawan-mac.h:301
int m_nPreambleSymbols
The number of symbols to use in the PHY preamble.
Definition: lorawan-mac.h:312
int GetNPreambleSymbols(void)
Get the number of PHY preamble symbols this MAC is set to use.
Definition: lorawan-mac.cc:243
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...
Definition: lorawan-mac.cc:231
Time GetOnAirTime(Ptr< Packet > packet, LoraTxParameters txParams)
Compute the time that a packet with certain characteristics will take to be transmitted.
Definition: lorawan-mac.cc:169
Ptr< SatPhy > GetPhy(void)
Get the underlying PHY layer.
Definition: lorawan-mac.cc:100
TracedCallback< Ptr< const Packet > > m_cannotSendBecauseDutyCycle
The trace source that is fired when a packet cannot be sent because of duty cycle limitations.
Definition: lorawan-mac.h:265
bool m_isRegenerative
Indicates if satellite is regenerative on the link where this layer is sending packets.
Definition: lorawan-mac.h:333
Ptr< SatPhy > m_phy
The PHY instance that sits under this MAC layer.
Definition: lorawan-mac.h:281
virtual void Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Send a packet.
Definition: lorawan-mac.cc:82
void SetBandwidthForDataRate(std::vector< double > bandwidthForDataRate)
Set the vector to use to check up correspondence between bandwidth and DataRate.
Definition: lorawan-mac.cc:219
static TypeId GetTypeId(void)
Definition: lorawan-mac.cc:39
TracedCallback< Ptr< const Packet > > m_sentNewPacket
Trace source that is fired when a new APP layer packet arrives at the MAC layer.
Definition: lorawan-mac.h:276
void SetSfForDataRate(std::vector< uint8_t > sfForDataRate)
Set the vector to use to check up correspondence between SF and DataRate.
Definition: lorawan-mac.cc:213
void SetMaxAppPayloadForDataRate(std::vector< uint32_t > maxAppPayloadForDataRate)
Set the maximum App layer payload for a set DataRate.
Definition: lorawan-mac.cc:225
uint8_t GetSfFromDataRate(uint8_t dataRate)
Get the SF corresponding to a data rate, based on this MAC's region.
Definition: lorawan-mac.cc:128
Ptr< NetDevice > GetDevice(void)
Get the device this MAC layer is installed on.
Definition: lorawan-mac.cc:94
Ptr< NetDevice > m_device
The device this MAC layer is installed on.
Definition: lorawan-mac.h:286
LoraLogicalChannelHelper GetLoraLogicalChannelHelper(void)
Get the logical lora channel helper associated with this MAC.
Definition: lorawan-mac.cc:116
std::vector< double > m_txDbmForTxPower
A vector holding the power that corresponds to a certain TxPower value.
Definition: lorawan-mac.h:317
double GetDbmForTxPower(uint8_t txPower)
Get the transmission power in dBm that corresponds, in this region, to the encoded 8-bit txPower.
Definition: lorawan-mac.cc:156
void SetNPreambleSymbols(int nPreambleSymbols)
Set the number of PHY preamble symbols this MAC is set to use.
Definition: lorawan-mac.cc:237
void SetLoraLogicalChannelHelper(LoraLogicalChannelHelper helper)
Set the LoraLogicalChannelHelper this MAC instance will use.
Definition: lorawan-mac.cc:122
void SetPhy(Ptr< SatPhy > phy)
Set the underlying PHY layer.
Definition: lorawan-mac.cc:106
void SetDevice(Ptr< NetDevice > device)
Set the device this MAC layer is installed on.
Definition: lorawan-mac.cc:88
LoraLogicalChannelHelper m_channelHelper
The LoraLogicalChannelHelper instance that is assigned to this MAC.
Definition: lorawan-mac.h:291
void setRegenerative(bool isRegenerative)
Indicates if the satellite is regenerative on the link this layer is sending packets.
Definition: lorawan-mac.cc:249
std::vector< uint8_t > m_sfForDataRate
A vector holding the SF each Data Rate corresponds to.
Definition: lorawan-mac.h:296
virtual void TxFinished()=0
Perform actions after sending a packet.
double GetBandwidthFromDataRate(uint8_t dataRate)
Get the BW corresponding to a data rate, based on this MAC's region.
Definition: lorawan-mac.cc:142
std::array< std::array< uint8_t, 6 >, 8 > ReplyDataRateMatrix
Definition: lorawan-mac.h:57
Base MAC class for SatNetDevices.
Definition: satellite-mac.h:57
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
bool lowDataRateOptimizationEnabled
Whether Low Data Rate Optimization is enabled.
uint32_t nPreamble
Number of preamble symbols.
bool headerDisabled
Whether to use implicit header mode.
uint8_t sf
Spreading Factor.
double bandwidthHz
Bandwidth in Hz.
bool crcEnabled
Whether Cyclic Redundancy Check is enabled.