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 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("LorawanMac");
31 
32 NS_OBJECT_ENSURE_REGISTERED(LorawanMac);
33 
34 TypeId
36 {
37  static TypeId tid =
38  TypeId("ns3::LorawanMac")
39  .SetParent<SatMac>()
40  .AddTraceSource("SentNewPacket",
41  "Trace source indicating a new packet "
42  "arrived at the MAC layer",
43  MakeTraceSourceAccessor(&LorawanMac::m_sentNewPacket),
44  "ns3::Packet::TracedCallback")
45  .AddTraceSource("ReceivedPacket",
46  "Trace source indicating a packet "
47  "was correctly received at the MAC layer",
48  MakeTraceSourceAccessor(&LorawanMac::m_receivedPacket),
49  "ns3::Packet::TracedCallback")
50  .AddTraceSource("CannotSendBecauseDutyCycle",
51  "Trace source indicating a packet "
52  "could not be sent immediately because of duty cycle limitations",
53  MakeTraceSourceAccessor(&LorawanMac::m_cannotSendBecauseDutyCycle),
54  "ns3::Packet::TracedCallback");
55  return tid;
56 }
57 
59  : m_isRegenerative(false)
60 {
61  NS_FATAL_ERROR("Default constructor not in use");
62 }
63 
64 LorawanMac::LorawanMac(uint32_t satId, uint32_t beamId)
65  : SatMac(satId, beamId, SatEnums::TRANSPARENT, SatEnums::TRANSPARENT),
66  m_beamId(beamId),
67  m_isRegenerative(false)
68 {
69  NS_LOG_FUNCTION(this);
70 }
71 
73 {
74  NS_LOG_FUNCTION(this);
75 }
76 
77 void
78 LorawanMac::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
79 {
80  Send(packet);
81 }
82 
83 void
84 LorawanMac::SetDevice(Ptr<NetDevice> device)
85 {
86  m_device = device;
87 }
88 
89 Ptr<NetDevice>
91 {
92  return m_device;
93 }
94 
95 Ptr<SatPhy>
97 {
98  return m_phy;
99 }
100 
101 void
102 LorawanMac::SetPhy(Ptr<SatPhy> phy)
103 {
104  // Set the phy
105  m_phy = phy;
106  DynamicCast<SatLoraPhyTx>(m_phy->GetPhyTx())
107  ->SetTxFinishedCallback(MakeCallback(&LorawanMac::TxFinished, this));
108 }
109 
112 {
113  return m_channelHelper;
114 }
115 
116 void
118 {
119  m_channelHelper = helper;
120 }
121 
122 uint8_t
124 {
125  NS_LOG_FUNCTION(this << unsigned(dataRate));
126 
127  // Check we are in range
128  if (dataRate >= m_sfForDataRate.size())
129  {
130  return 0;
131  }
132 
133  return m_sfForDataRate.at(dataRate);
134 }
135 
136 double
138 {
139  NS_LOG_FUNCTION(this << unsigned(dataRate));
140 
141  // Check we are in range
142  if (dataRate > m_bandwidthForDataRate.size())
143  {
144  return 0;
145  }
146 
147  return m_bandwidthForDataRate.at(dataRate);
148 }
149 
150 double
152 {
153  NS_LOG_FUNCTION(this << unsigned(txPower));
154 
155  if (txPower > m_txDbmForTxPower.size())
156  {
157  return 0;
158  }
159 
160  return m_txDbmForTxPower.at(txPower);
161 }
162 
163 Time
164 LorawanMac::GetOnAirTime(Ptr<Packet> packet, LoraTxParameters txParams)
165 {
166  NS_LOG_FUNCTION(packet);
167 
168  // The contents of this function are based on [1].
169  // [1] SX1272 LoRa modem designer's guide.
170 
171  // Compute the symbol duration
172  // Bandwidth is in Hz
173  double tSym = pow(2, int(txParams.sf)) / (txParams.bandwidthHz);
174 
175  // Compute the preamble duration
176  double tPreamble = (double(txParams.nPreamble) + 4.25) * tSym;
177 
178  // Payload size
179  uint32_t pl = packet->GetSize(); // Size in bytes
180  NS_LOG_DEBUG("Packet of size " << pl << " bytes");
181 
182  // This step is needed since the formula deals with double values.
183  // de = 1 when the low data rate optimization is enabled, 0 otherwise
184  // h = 1 when header is implicit, 0 otherwise
185  double de = txParams.lowDataRateOptimizationEnabled ? 1 : 0;
186  double h = txParams.headerDisabled ? 1 : 0;
187  double crc = txParams.crcEnabled ? 1 : 0;
188 
189  // num and den refer to numerator and denominator of the time on air formula
190  double num = 8 * pl - 4 * txParams.sf + 28 + 16 * crc - 20 * h;
191  double den = (txParams.sf - 2 * de) * txParams.codingRate;
192  double payloadSymbNb = 8 + std::max(std::ceil(num / den), double(0));
193 
194  // Time to transmit the payload
195  double tPayload = payloadSymbNb * tSym;
196 
197  NS_LOG_DEBUG("Time computation: num = " << num << ", den = " << den << ", payloadSymbNb = "
198  << payloadSymbNb << ", tSym = " << tSym);
199  NS_LOG_DEBUG("tPreamble = " << tPreamble);
200  NS_LOG_DEBUG("tPayload = " << tPayload);
201  NS_LOG_DEBUG("Total time = " << tPreamble + tPayload);
202 
203  // Compute and return the total packet on-air time
204  return Seconds(tPreamble + tPayload);
205 }
206 
207 void
208 LorawanMac::SetSfForDataRate(std::vector<uint8_t> sfForDataRate)
209 {
210  m_sfForDataRate = sfForDataRate;
211 }
212 
213 void
214 LorawanMac::SetBandwidthForDataRate(std::vector<double> bandwidthForDataRate)
215 {
216  m_bandwidthForDataRate = bandwidthForDataRate;
217 }
218 
219 void
220 LorawanMac::SetMaxAppPayloadForDataRate(std::vector<uint32_t> maxAppPayloadForDataRate)
221 {
222  m_maxAppPayloadForDataRate = maxAppPayloadForDataRate;
223 }
224 
225 void
226 LorawanMac::SetTxDbmForTxPower(std::vector<double> txDbmForTxPower)
227 {
228  m_txDbmForTxPower = txDbmForTxPower;
229 }
230 
231 void
232 LorawanMac::SetNPreambleSymbols(int nPreambleSymbols)
233 {
234  m_nPreambleSymbols = nPreambleSymbols;
235 }
236 
237 int
239 {
240  return m_nPreambleSymbols;
241 }
242 
243 void
244 LorawanMac::setRegenerative(bool isRegenerative)
245 {
246  NS_LOG_FUNCTION(this << isRegenerative);
247  m_isRegenerative = isRegenerative;
248 }
249 
250 void
252 {
253  m_replyDataRateMatrix = replyDataRateMatrix;
254 }
255 } // 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:268
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:321
void SetReplyDataRateMatrix(ReplyDataRateMatrix replyDataRateMatrix)
Set the matrix to use when deciding with which DataRate to respond.
Definition: lorawan-mac.cc:251
virtual ~LorawanMac()
Definition: lorawan-mac.cc:72
std::vector< uint32_t > m_maxAppPayloadForDataRate
A vector holding the maximum app payload size that corresponds to a certain DataRate.
Definition: lorawan-mac.h:305
std::vector< double > m_bandwidthForDataRate
A vector holding the bandwidth each Data Rate corresponds to.
Definition: lorawan-mac.h:299
int m_nPreambleSymbols
The number of symbols to use in the PHY preamble.
Definition: lorawan-mac.h:310
int GetNPreambleSymbols(void)
Get the number of PHY preamble symbols this MAC is set to use.
Definition: lorawan-mac.cc:238
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:226
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:164
Ptr< SatPhy > GetPhy(void)
Get the underlying PHY layer.
Definition: lorawan-mac.cc:96
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:263
bool m_isRegenerative
Indicates if satellite is regenerative on the link where this layer is sending packets.
Definition: lorawan-mac.h:331
Ptr< SatPhy > m_phy
The PHY instance that sits under this MAC layer.
Definition: lorawan-mac.h:279
virtual void Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Send a packet.
Definition: lorawan-mac.cc:78
void SetBandwidthForDataRate(std::vector< double > bandwidthForDataRate)
Set the vector to use to check up correspondence between bandwidth and DataRate.
Definition: lorawan-mac.cc:214
static TypeId GetTypeId(void)
Definition: lorawan-mac.cc:35
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:274
void SetSfForDataRate(std::vector< uint8_t > sfForDataRate)
Set the vector to use to check up correspondence between SF and DataRate.
Definition: lorawan-mac.cc:208
void SetMaxAppPayloadForDataRate(std::vector< uint32_t > maxAppPayloadForDataRate)
Set the maximum App layer payload for a set DataRate.
Definition: lorawan-mac.cc:220
uint8_t GetSfFromDataRate(uint8_t dataRate)
Get the SF corresponding to a data rate, based on this MAC's region.
Definition: lorawan-mac.cc:123
Ptr< NetDevice > GetDevice(void)
Get the device this MAC layer is installed on.
Definition: lorawan-mac.cc:90
Ptr< NetDevice > m_device
The device this MAC layer is installed on.
Definition: lorawan-mac.h:284
LoraLogicalChannelHelper GetLoraLogicalChannelHelper(void)
Get the logical lora channel helper associated with this MAC.
Definition: lorawan-mac.cc:111
std::vector< double > m_txDbmForTxPower
A vector holding the power that corresponds to a certain TxPower value.
Definition: lorawan-mac.h:315
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:151
void SetNPreambleSymbols(int nPreambleSymbols)
Set the number of PHY preamble symbols this MAC is set to use.
Definition: lorawan-mac.cc:232
void SetLoraLogicalChannelHelper(LoraLogicalChannelHelper helper)
Set the LoraLogicalChannelHelper this MAC instance will use.
Definition: lorawan-mac.cc:117
void SetPhy(Ptr< SatPhy > phy)
Set the underlying PHY layer.
Definition: lorawan-mac.cc:102
void SetDevice(Ptr< NetDevice > device)
Set the device this MAC layer is installed on.
Definition: lorawan-mac.cc:84
LoraLogicalChannelHelper m_channelHelper
The LoraLogicalChannelHelper instance that is assigned to this MAC.
Definition: lorawan-mac.h:289
void setRegenerative(bool isRegenerative)
Indicates if the satellite is regenerative on the link this layer is sending packets.
Definition: lorawan-mac.cc:244
std::vector< uint8_t > m_sfForDataRate
A vector holding the SF each Data Rate corresponds to.
Definition: lorawan-mac.h:294
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:137
std::array< std::array< uint8_t, 6 >, 8 > ReplyDataRateMatrix
Definition: lorawan-mac.h:55
SatEnums class is for simplifying the use of enumerators in the satellite module.
Base MAC class for SatNetDevices.
Definition: satellite-mac.h:52
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.