Loading...
Searching...
No Matches
lorawan-ground-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
24
25#include "lora-beam-tag.h"
26#include "lora-frame-header.h"
27#include "lorawan-mac-header.h"
31
32#include "ns3/log.h"
33
34namespace ns3
35{
36
37NS_LOG_COMPONENT_DEFINE("LorawanGroundMacGateway");
38
39NS_OBJECT_ENSURE_REGISTERED(LorawanGroundMacGateway);
40
41TypeId
43{
44 static TypeId tid = TypeId("ns3::LorawanGroundMacGateway")
45 .SetParent<LorawanMacGateway>()
46 .AddConstructor<LorawanGroundMacGateway>();
47 return tid;
48}
49
51{
52 NS_FATAL_ERROR("Default constructor not in use");
53}
54
56 : LorawanMacGateway(satId, beamId)
57{
58 NS_LOG_FUNCTION(this);
59}
60
62{
63 NS_LOG_FUNCTION(this);
64}
65
66void
68{
69 NS_LOG_FUNCTION(this << packet);
70
71 // Get DataRate to send this packet with
72 LoraTag tag;
73 packet->RemovePacketTag(tag);
74 uint8_t modcod = tag.GetModcod();
75 uint8_t dataRate = tag.GetDataRate();
76 double frequency = tag.GetFrequency();
77 NS_LOG_DEBUG("DR: " << (uint32_t)unsigned(dataRate));
78 NS_LOG_DEBUG("SF: " << (uint32_t)unsigned(GetSfFromDataRate(dataRate)));
79 NS_LOG_DEBUG("BW: " << GetBandwidthFromDataRate(dataRate));
80 NS_LOG_DEBUG("Freq: " << frequency << " MHz");
81 packet->AddPacketTag(tag);
82
84 {
85 packet->AddPacketTag(SatMacTimeTag(Simulator::Now()));
86
87 // Add a SatAddressTag tag with this device's address as the source address.
88 packet->AddByteTag(SatAddressTag(m_nodeInfo->GetMacAddress()));
89
90 // Add a SatDevTimeTag tag for packet delay computation at the receiver end.
91 SatDevTimeTag satDevTag;
92 packet->RemovePacketTag(satDevTag);
93 packet->AddPacketTag(SatDevTimeTag(Simulator::Now()));
94 }
95
96 // Make sure we can transmit this packet
97 if (m_channelHelper.GetWaitingTime(CreateObject<LoraLogicalChannel>(frequency)) > Time(0))
98 {
99 // We cannot send now!
100 NS_LOG_WARN("Trying to send a packet but Duty Cycle won't allow it. Aborting.");
101 return;
102 }
103
104 if (IsTransmitting())
105 {
106 // Gateway already transmitting!
107 NS_LOG_WARN("Gateway is already transmitting. Aborting.");
108 return;
109 }
110
111 LoraTxParameters params;
112 params.sf = GetSfFromDataRate(dataRate);
113 params.headerDisabled = false;
115 params.bandwidthHz = GetBandwidthFromDataRate(dataRate);
116 params.nPreamble = 8;
117 params.crcEnabled = 1;
119
123 txInfo.modCod = (SatEnums::SatModcod_t)modcod;
124 txInfo.sliceId = 0;
125 txInfo.waveformId = 0;
126
127 // Get the duration
128 Time duration = GetOnAirTime(packet, params);
129
130 NS_LOG_DEBUG("Duration: " << duration.GetSeconds());
131
132 // Add the event to the channelHelper to keep track of duty cycle
133 m_channelHelper.AddEvent(duration, CreateObject<LoraLogicalChannel>(frequency));
134
135 SatMacTag mTag;
136 mTag.SetDestAddress(Mac48Address::GetBroadcast());
137 mTag.SetSourceAddress(Mac48Address::ConvertFrom(m_device->GetAddress()));
138 packet->AddPacketTag(mTag);
139
140 SatAddressE2ETag addressE2ETag;
141 addressE2ETag.SetE2EDestAddress(Mac48Address::GetBroadcast());
142 addressE2ETag.SetE2ESourceAddress(Mac48Address::ConvertFrom(m_device->GetAddress()));
143 packet->AddPacketTag(addressE2ETag);
144
146 packets.push_back(packet);
147 uint32_t carrierId = 0;
148
149 // Send the packet to the PHY layer to send it on the channel
150 m_phy->SendPdu(packets, carrierId, duration, txInfo);
151
152 m_sentNewPacket(packet);
153}
154
155void
157 Ptr<SatSignalParameters> /*rxParams*/)
158{
159 NS_LOG_FUNCTION(this << packets);
160
161 // Invoke the `Rx` and `RxDelay` trace sources.
162 RxTraces(packets);
163
164 Ptr<Packet> packet;
165 for (SatPhy::PacketContainer_t::iterator i = packets.begin(); i != packets.end(); i++)
166 {
167 packet = *i;
168 // Make a copy of the packet to work on
169 Ptr<Packet> packetCopy = packet->Copy();
170
171 SatMacTag mTag;
172 packetCopy->RemovePacketTag(mTag);
173
174 // Only forward the packet if it's uplink
175 LorawanMacHeader macHdr;
176 packetCopy->PeekHeader(macHdr);
177
178 if (macHdr.IsUplink())
179 {
180 LoraBeamTag beamTag = LoraBeamTag(GetBeamId());
181 packetCopy->AddPacketTag(beamTag);
182
183 Ptr<SatLorawanNetDevice> lorawanNetDevice = m_device->GetObject<SatLorawanNetDevice>();
184 lorawanNetDevice->Receive(packetCopy);
185
186 NS_LOG_DEBUG("Received packet: " << packet);
187
188 m_receivedPacket(packet);
189 }
190 else
191 {
192 NS_LOG_DEBUG("Not forwarding downlink message to NetDevice");
193 }
194 }
195}
196} // namespace ns3
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
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 Send(Ptr< Packet > packet)
Send a packet.
virtual void Receive(SatPhy::PacketContainer_t packets, Ptr< SatSignalParameters >)
Receive a packet from the lower layer.
This class represents the Mac header of a LoRaWAN packet.
bool IsUplink(void) const
Check whether this header is for an uplink message.
TracedCallback< Ptr< const Packet > > m_receivedPacket
Trace source that is fired when a packet reaches the MAC layer.
Time GetOnAirTime(Ptr< Packet > packet, LoraTxParameters txParams)
Compute the time that a packet with certain characteristics will take to be transmitted.
Ptr< SatPhy > m_phy
The PHY instance that sits under this MAC layer.
TracedCallback< Ptr< const Packet > > m_sentNewPacket
Trace source that is fired when a new APP layer packet arrives at the MAC layer.
uint8_t GetSfFromDataRate(uint8_t dataRate)
Get the SF corresponding to a data rate, based on this MAC's region.
Ptr< NetDevice > m_device
The device this MAC layer is installed on.
LoraLogicalChannelHelper m_channelHelper
The LoraLogicalChannelHelper instance that is assigned to this MAC.
double GetBandwidthFromDataRate(uint8_t dataRate)
Get the BW corresponding to a data rate, based on this MAC's region.
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)
uint32_t GetBeamId() const
Get beam ID of the object.
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.
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.