Loading...
Searching...
No Matches
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
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("LorawanMac");
35
36NS_OBJECT_ENSURE_REGISTERED(LorawanMac);
37
38TypeId
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
68LorawanMac::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
81void
82LorawanMac::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
83{
84 Send(packet);
85}
86
87void
88LorawanMac::SetDevice(Ptr<NetDevice> device)
89{
90 m_device = device;
91}
92
93Ptr<NetDevice>
95{
96 return m_device;
97}
98
99Ptr<SatPhy>
101{
102 return m_phy;
103}
104
105void
106LorawanMac::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
120
121void
126
127uint8_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
141double
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
155double
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
168Time
169LorawanMac::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
212void
213LorawanMac::SetSfForDataRate(std::vector<uint8_t> sfForDataRate)
214{
215 m_sfForDataRate = sfForDataRate;
216}
217
218void
219LorawanMac::SetBandwidthForDataRate(std::vector<double> bandwidthForDataRate)
220{
221 m_bandwidthForDataRate = bandwidthForDataRate;
222}
223
224void
225LorawanMac::SetMaxAppPayloadForDataRate(std::vector<uint32_t> maxAppPayloadForDataRate)
226{
227 m_maxAppPayloadForDataRate = maxAppPayloadForDataRate;
228}
229
230void
231LorawanMac::SetTxDbmForTxPower(std::vector<double> txDbmForTxPower)
232{
233 m_txDbmForTxPower = txDbmForTxPower;
234}
235
236void
238{
239 m_nPreambleSymbols = nPreambleSymbols;
240}
241
242int
247
248void
249LorawanMac::setRegenerative(bool isRegenerative)
250{
251 NS_LOG_FUNCTION(this << isRegenerative);
252 m_isRegenerative = isRegenerative;
253}
254
255void
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...
Class representing the LoRaWAN MAC layer.
Definition lorawan-mac.h:49
TracedCallback< Ptr< const Packet > > m_receivedPacket
Trace source that is fired when a packet reaches the MAC layer.
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...
void SetReplyDataRateMatrix(ReplyDataRateMatrix replyDataRateMatrix)
Set the matrix to use when deciding with which DataRate to respond.
virtual ~LorawanMac()
std::vector< uint32_t > m_maxAppPayloadForDataRate
A vector holding the maximum app payload size that corresponds to a certain DataRate.
std::vector< double > m_bandwidthForDataRate
A vector holding the bandwidth each Data Rate corresponds to.
int m_nPreambleSymbols
The number of symbols to use in the PHY preamble.
int GetNPreambleSymbols(void)
Get the number of PHY preamble symbols this MAC is set to use.
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...
Time GetOnAirTime(Ptr< Packet > packet, LoraTxParameters txParams)
Compute the time that a packet with certain characteristics will take to be transmitted.
uint32_t m_beamId
ID of beam for UT.
Ptr< SatPhy > GetPhy(void)
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.
bool m_isRegenerative
Indicates if satellite is regenerative on the link where this layer is sending packets.
Ptr< SatPhy > m_phy
The PHY instance that sits under this MAC layer.
virtual void Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
Send a packet.
void SetBandwidthForDataRate(std::vector< double > bandwidthForDataRate)
Set the vector to use to check up correspondence between bandwidth and DataRate.
static TypeId GetTypeId(void)
TracedCallback< Ptr< const Packet > > m_sentNewPacket
Trace source that is fired when a new APP layer packet arrives at the MAC layer.
void SetSfForDataRate(std::vector< uint8_t > sfForDataRate)
Set the vector to use to check up correspondence between SF and DataRate.
void SetMaxAppPayloadForDataRate(std::vector< uint32_t > maxAppPayloadForDataRate)
Set the maximum App layer payload for a set DataRate.
uint8_t GetSfFromDataRate(uint8_t dataRate)
Get the SF corresponding to a data rate, based on this MAC's region.
Ptr< NetDevice > GetDevice(void)
Get the device this MAC layer is installed on.
Ptr< NetDevice > m_device
The device this MAC layer is installed on.
LoraLogicalChannelHelper GetLoraLogicalChannelHelper(void)
Get the logical lora channel helper associated with this MAC.
std::vector< double > m_txDbmForTxPower
A vector holding the power that corresponds to a certain TxPower value.
double GetDbmForTxPower(uint8_t txPower)
Get the transmission power in dBm that corresponds, in this region, to the encoded 8-bit txPower.
void SetNPreambleSymbols(int nPreambleSymbols)
Set the number of PHY preamble symbols this MAC is set to use.
void SetLoraLogicalChannelHelper(LoraLogicalChannelHelper helper)
Set the LoraLogicalChannelHelper this MAC instance will use.
void SetPhy(Ptr< SatPhy > phy)
Set the underlying PHY layer.
void SetDevice(Ptr< NetDevice > device)
Set the device this MAC layer is installed on.
LoraLogicalChannelHelper m_channelHelper
The LoraLogicalChannelHelper instance that is assigned to this MAC.
void setRegenerative(bool isRegenerative)
Indicates if the satellite is regenerative on the link this layer is sending packets.
std::vector< uint8_t > m_sfForDataRate
A vector holding the SF each Data Rate corresponds to.
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.
std::array< std::array< uint8_t, 6 >, 8 > ReplyDataRateMatrix
Definition lorawan-mac.h:57
SatMac()
Construct a SatMac.
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.