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"
29 
30 #include <ns3/log.h>
31 
32 namespace ns3
33 {
34 
35 NS_LOG_COMPONENT_DEFINE("LorawanMacGateway");
36 
37 NS_OBJECT_ENSURE_REGISTERED(LorawanMacGateway);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId("ns3::LorawanMacGateway")
43  .SetParent<LorawanMac>()
44  .AddConstructor<LorawanMacGateway>();
45  return tid;
46 }
47 
49 {
50  NS_FATAL_ERROR("Default constructor not in use");
51 }
52 
53 LorawanMacGateway::LorawanMacGateway(uint32_t satId, uint32_t beamId)
54  : LorawanMac(satId, beamId)
55 {
56  NS_LOG_FUNCTION(this);
57 }
58 
60 {
61  NS_LOG_FUNCTION(this);
62 }
63 
64 void
65 LorawanMacGateway::Send(Ptr<Packet> packet)
66 {
67  NS_LOG_FUNCTION(this << packet);
68 
69  // Get DataRate to send this packet with
70  LoraTag tag;
71  packet->RemovePacketTag(tag);
72  uint8_t modcod = tag.GetModcod();
73  uint8_t dataRate = tag.GetDataRate();
74  double frequency = tag.GetFrequency();
75  NS_LOG_DEBUG("DR: " << (uint32_t) unsigned(dataRate));
76  NS_LOG_DEBUG("SF: " << (uint32_t) unsigned(GetSfFromDataRate(dataRate)));
77  NS_LOG_DEBUG("BW: " << GetBandwidthFromDataRate(dataRate));
78  NS_LOG_DEBUG("Freq: " << frequency << " MHz");
79  packet->AddPacketTag(tag);
80 
82  {
83  packet->AddPacketTag(SatMacTimeTag(Simulator::Now()));
84 
85  // Add a SatAddressTag tag with this device's address as the source address.
86  packet->AddByteTag(SatAddressTag(m_nodeInfo->GetMacAddress()));
87 
88  // Add a SatDevTimeTag tag for packet delay computation at the receiver end.
89  SatDevTimeTag satDevTag;
90  packet->RemovePacketTag(satDevTag);
91  packet->AddPacketTag(SatDevTimeTag(Simulator::Now()));
92  }
93 
94  // Make sure we can transmit this packet
95  if (m_channelHelper.GetWaitingTime(CreateObject<LoraLogicalChannel>(frequency)) > Time(0))
96  {
97  // We cannot send now!
98  NS_LOG_WARN("Trying to send a packet but Duty Cycle won't allow it. Aborting.");
99  return;
100  }
101 
102  if (IsTransmitting())
103  {
104  // Gateway already transmitting!
105  NS_LOG_WARN("Gateway is already transmitting. Aborting.");
106  return;
107  }
108 
109  LoraTxParameters params;
110  params.sf = GetSfFromDataRate(dataRate);
111  params.headerDisabled = false;
113  params.bandwidthHz = GetBandwidthFromDataRate(dataRate);
114  params.nPreamble = 8;
115  params.crcEnabled = 1;
117 
121  txInfo.modCod = (SatEnums::SatModcod_t)modcod;
122  txInfo.sliceId = 0;
123  txInfo.waveformId = 0;
124 
125  // Get the duration
126  Time duration = GetOnAirTime(packet, params);
127 
128  NS_LOG_DEBUG("Duration: " << duration.GetSeconds());
129 
130  // Add the event to the channelHelper to keep track of duty cycle
131  m_channelHelper.AddEvent(duration, CreateObject<LoraLogicalChannel>(frequency));
132 
133  SatMacTag mTag;
134  mTag.SetDestAddress(Mac48Address::GetBroadcast());
135  mTag.SetSourceAddress(Mac48Address::ConvertFrom(m_device->GetAddress()));
136  packet->AddPacketTag(mTag);
137 
138  SatAddressE2ETag addressE2ETag;
139  addressE2ETag.SetE2EDestAddress(Mac48Address::GetBroadcast());
140  addressE2ETag.SetE2ESourceAddress(Mac48Address::ConvertFrom(m_device->GetAddress()));
141  packet->AddPacketTag(addressE2ETag);
142 
144  packets.push_back(packet);
145  uint32_t carrierId = 0;
146 
147  // Send the packet to the PHY layer to send it on the channel
148  m_phy->SendPdu(packets, carrierId, duration, txInfo);
149 
150  m_sentNewPacket(packet);
151 }
152 
153 bool
155 {
156  return DynamicCast<SatLoraPhyTx>(m_phy->GetPhyTx())->IsTransmitting();
157 }
158 
159 void
160 LorawanMacGateway::Receive(SatPhy::PacketContainer_t packets, Ptr<SatSignalParameters> /*rxParams*/)
161 {
162  NS_LOG_FUNCTION(this << packets);
163 
164  // Invoke the `Rx` and `RxDelay` trace sources.
165  RxTraces(packets);
166 
167  Ptr<Packet> packet;
168  for (SatPhy::PacketContainer_t::iterator i = packets.begin(); i != packets.end(); i++)
169  {
170  packet = *i;
171  // Make a copy of the packet to work on
172  Ptr<Packet> packetCopy = packet->Copy();
173 
174  SatMacTag mTag;
175  packetCopy->RemovePacketTag(mTag);
176 
177  // Only forward the packet if it's uplink
178  LorawanMacHeader macHdr;
179  packetCopy->PeekHeader(macHdr);
180 
181  if (macHdr.IsUplink())
182  {
183  m_device->GetObject<SatLorawanNetDevice>()->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:38
double GetFrequency(void)
Get the frequency of the packet.
Definition: lora-tag.cc:140
uint8_t GetDataRate(void)
Get the data rate for this packet.
Definition: lora-tag.cc:146
uint8_t GetModcod(void)
Get the modcod for this packet.
Definition: lora-tag.cc:158
virtual void Send(Ptr< Packet > packet)
Send a packet.
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...
virtual void Receive(SatPhy::PacketContainer_t packets, Ptr< SatSignalParameters >)
Receive a packet from the lower layer.
Time GetWaitingTime(double frequency)
Return the next time at which we will be able to transmit.
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:47
TracedCallback< Ptr< const Packet > > m_receivedPacket
Trace source that is fired when a packet reaches the MAC layer.
Definition: lorawan-mac.h:268
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 > m_phy
The PHY instance that sits under this MAC layer.
Definition: lorawan-mac.h:279
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
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 > m_device
The device this MAC layer is installed on.
Definition: lorawan-mac.h:284
LoraLogicalChannelHelper m_channelHelper
The LoraLogicalChannelHelper instance that is assigned to this MAC.
Definition: lorawan-mac.h:289
double GetBandwidthFromDataRate(uint8_t dataRate)
Get the BW corresponding to a data rate, based on this MAC's region.
Definition: lorawan-mac.cc:137
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 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:78
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.