lorawan-mac-gateway.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-gateway.h"
24 
25 #include "lora-frame-header.h"
26 #include "lorawan-mac-header.h"
27 #include "satellite-bbframe-conf.h"
30 
31 #include <ns3/log.h>
32 
33 namespace ns3
34 {
35 
36 NS_LOG_COMPONENT_DEFINE("LorawanMacGateway");
37 
38 NS_OBJECT_ENSURE_REGISTERED(LorawanMacGateway);
39 
40 TypeId
42 {
43  static TypeId tid = TypeId("ns3::LorawanMacGateway").SetParent<LorawanMac>();
44  return tid;
45 }
46 
48 {
49  NS_FATAL_ERROR("Default constructor not in use");
50 }
51 
52 LorawanMacGateway::LorawanMacGateway(uint32_t satId, uint32_t beamId)
53  : LorawanMac(satId, beamId)
54 {
55  NS_LOG_FUNCTION(this);
56 }
57 
59 {
60  NS_LOG_FUNCTION(this);
61 }
62 
63 void
64 LorawanMacGateway::Send(Ptr<Packet> packet)
65 {
66  NS_LOG_FUNCTION(this << packet);
67 
68  // Get DataRate to send this packet with
69  LoraTag tag;
70  packet->RemovePacketTag(tag);
71  uint8_t modcod = tag.GetModcod();
72  uint8_t dataRate = tag.GetDataRate();
73  double frequency = tag.GetFrequency();
74  NS_LOG_DEBUG("DR: " << (uint32_t) unsigned(dataRate));
75  NS_LOG_DEBUG("SF: " << (uint32_t) unsigned(GetSfFromDataRate(dataRate)));
76  NS_LOG_DEBUG("BW: " << GetBandwidthFromDataRate(dataRate));
77  NS_LOG_DEBUG("Freq: " << frequency << " MHz");
78  packet->AddPacketTag(tag);
79 
81  {
82  packet->AddPacketTag(SatMacTimeTag(Simulator::Now()));
83 
84  // Add a SatAddressTag tag with this device's address as the source address.
85  packet->AddByteTag(SatAddressTag(m_nodeInfo->GetMacAddress()));
86 
87  // Add a SatDevTimeTag tag for packet delay computation at the receiver end.
88  SatDevTimeTag satDevTag;
89  packet->RemovePacketTag(satDevTag);
90  packet->AddPacketTag(SatDevTimeTag(Simulator::Now()));
91  }
92 
93  // Make sure we can transmit this packet
94  if (m_channelHelper.GetWaitingTime(CreateObject<LoraLogicalChannel>(frequency)) > Time(0))
95  {
96  // We cannot send now!
97  NS_LOG_WARN("Trying to send a packet but Duty Cycle won't allow it. Aborting.");
98  return;
99  }
100 
101  if (IsTransmitting())
102  {
103  // Gateway already transmitting!
104  NS_LOG_WARN("Gateway is already transmitting. Aborting.");
105  return;
106  }
107 
108  LoraTxParameters params;
109  params.sf = GetSfFromDataRate(dataRate);
110  params.headerDisabled = false;
112  params.bandwidthHz = GetBandwidthFromDataRate(dataRate);
113  params.nPreamble = 8;
114  params.crcEnabled = 1;
116 
120  txInfo.modCod = (SatEnums::SatModcod_t)modcod;
121  txInfo.sliceId = 0;
122  txInfo.waveformId = 0;
123 
124  // Get the duration
125  Time duration = GetOnAirTime(packet, params);
126 
127  NS_LOG_DEBUG("Duration: " << duration.GetSeconds());
128 
129  // Add the event to the channelHelper to keep track of duty cycle
130  m_channelHelper.AddEvent(duration, CreateObject<LoraLogicalChannel>(frequency));
131 
132  SatMacTag mTag;
133  mTag.SetDestAddress(Mac48Address::GetBroadcast());
134  mTag.SetSourceAddress(Mac48Address::ConvertFrom(m_device->GetAddress()));
135  packet->AddPacketTag(mTag);
136 
137  SatAddressE2ETag addressE2ETag;
138  addressE2ETag.SetE2EDestAddress(Mac48Address::GetBroadcast());
139  addressE2ETag.SetE2ESourceAddress(Mac48Address::ConvertFrom(m_device->GetAddress()));
140  packet->AddPacketTag(addressE2ETag);
141 
143  packets.push_back(packet);
144  uint32_t carrierId = 0;
145 
146  // Send the packet to the PHY layer to send it on the channel
147  m_phy->SendPdu(packets, carrierId, duration, txInfo);
148 
149  m_sentNewPacket(packet);
150 }
151 
152 bool
154 {
155  return DynamicCast<SatLoraPhyTx>(m_phy->GetPhyTx())->IsTransmitting();
156 }
157 
158 void
159 LorawanMacGateway::Receive(SatPhy::PacketContainer_t packets, Ptr<SatSignalParameters> /*rxParams*/)
160 {
161  NS_LOG_FUNCTION(this << packets);
162 
163  // Invoke the `Rx` and `RxDelay` trace sources.
164  RxTraces(packets);
165 
166  Ptr<Packet> packet;
167  for (SatPhy::PacketContainer_t::iterator i = packets.begin(); i != packets.end(); i++)
168  {
169  packet = *i;
170  // Make a copy of the packet to work on
171  Ptr<Packet> packetCopy = packet->Copy();
172 
173  SatMacTag mTag;
174  packetCopy->RemovePacketTag(mTag);
175 
176  // Only forward the packet if it's uplink
177  LorawanMacHeader macHdr;
178  packetCopy->PeekHeader(macHdr);
179 
180  if (macHdr.IsUplink())
181  {
182  Ptr<SatLorawanNetDevice> lorawanNetDevice = m_device->GetObject<SatLorawanNetDevice>();
183  lorawanNetDevice->Receive(packetCopy);
184 
185  NS_LOG_DEBUG("Received packet: " << packet);
186 
187  m_receivedPacket(packet);
188  }
189  else
190  {
191  NS_LOG_DEBUG("Not forwarding downlink message to NetDevice");
192  }
193  }
194 }
195 
196 void
197 LorawanMacGateway::FailedReception(Ptr<const Packet> packet)
198 {
199  NS_LOG_FUNCTION(this << packet);
200 }
201 
202 void
204 {
205  NS_LOG_FUNCTION_NOARGS();
206 }
207 
208 Time
210 {
211  NS_LOG_FUNCTION(this << frequency);
212 
213  return m_channelHelper.GetWaitingTime(CreateObject<LoraLogicalChannel>(frequency));
214 }
215 } // namespace ns3
Time GetWaitingTime(Ptr< LoraLogicalChannel > channel)
Get the time it is necessary to wait for before transmitting on a given channel.
void AddEvent(Time duration, Ptr< LoraLogicalChannel > channel)
Register the transmission of a packet.
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
Definition: lora-tag.h:41
double GetFrequency(void)
Get the frequency of the packet.
Definition: lora-tag.cc:142
uint8_t GetDataRate(void)
Get the data rate for this packet.
Definition: lora-tag.cc:148
uint8_t GetModcod(void)
Get the modcod for this packet.
Definition: lora-tag.cc:160
virtual void Receive(SatPhy::PacketContainer_t packets, Ptr< SatSignalParameters >)=0
Receive a packet from the lower layer.
virtual void FailedReception(Ptr< const Packet > packet)
Function called by lower layers to inform this layer that reception of a packet we were locked on fai...
Time GetWaitingTime(double frequency)
Return the next time at which we will be able to transmit.
virtual void Send(Ptr< Packet > packet)=0
Send a packet.
virtual void TxFinished()
Perform actions after sending a packet.
static TypeId GetTypeId(void)
This class represents the Mac header of a LoRaWAN packet.
bool IsUplink(void) const
Check whether this header is for an uplink message.
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.
Definition: lorawan-mac.h:270
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 > m_phy
The PHY instance that sits under this MAC layer.
Definition: lorawan-mac.h:281
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
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 > m_device
The device this MAC layer is installed on.
Definition: lorawan-mac.h:286
LoraLogicalChannelHelper m_channelHelper
The LoraLogicalChannelHelper instance that is assigned to this MAC.
Definition: lorawan-mac.h:291
double GetBandwidthFromDataRate(uint8_t dataRate)
Get the BW corresponding to a data rate, based on this MAC's region.
Definition: lorawan-mac.cc:142
This class implements a tag that carries the satellite MAC of GW and UT.
void SetE2ESourceAddress(Mac48Address e2eSourceAddress)
Set E2E source MAC address.
void SetE2EDestAddress(Mac48Address e2eDestAddress)
Set E2E destination MAC address.
This class implements a tag that carries the MAC address of the sender of the packet.
Time tag used to identify the time when packet is enqueued at device level.
SatModcod_t
Modulation scheme and coding rate for DVB-S2.
SatLorawanNetDevice to be utilized in the UT and GW nodes for IoT configuration.
void Receive(Ptr< const Packet > packet)
void RxTraces(SatPhy::PacketContainer_t packets)
Invoke the Rx trace source for each received packet.
bool m_isStatisticsTagsEnabled
EnableStatisticsTags attribute.
Ptr< SatNodeInfo > m_nodeInfo
Node info containing node related information, such as node type, node id and MAC address (of the Sat...
This class implements a tag that carries the satellite MAC specific information, such as source and d...
void SetDestAddress(Mac48Address dest)
Set destination MAC address.
void SetSourceAddress(Mac48Address source)
Set source MAC address.
Time tag used to identify the time when packet is enqueued at MAC level.
SatSignalParameters::PacketsInBurst_t PacketContainer_t
Define PacketContainer in SatPhy.
Definition: satellite-phy.h:79
static double GetCodingRate(SatEnums::SatModcod_t modcod)
Gets the coding rate of a certain MODCOD.
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.
Struct for storing the packet specific Tx information.