A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
gateway-lorawan-mac.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 University of Padova
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Davide Magrin <magrinda@dei.unipd.it>
18 */
19
20#include "gateway-lorawan-mac.h"
21
22#include "lora-frame-header.h"
23#include "lora-net-device.h"
24#include "lorawan-mac-header.h"
25
26#include "ns3/log.h"
27
28namespace ns3
29{
30namespace lorawan
31{
32
33NS_LOG_COMPONENT_DEFINE("GatewayLorawanMac");
34
35NS_OBJECT_ENSURE_REGISTERED(GatewayLorawanMac);
36
37TypeId
39{
40 static TypeId tid = TypeId("ns3::GatewayLorawanMac")
42 .AddConstructor<GatewayLorawanMac>()
43 .SetGroupName("lorawan");
44 return tid;
45}
46
48{
49 NS_LOG_FUNCTION(this);
50}
51
53{
54 NS_LOG_FUNCTION(this);
55}
56
57void
59{
60 NS_LOG_FUNCTION(this << packet);
61
62 // Get data rate to send this packet with
63 LoraTag tag;
64 packet->RemovePacketTag(tag);
65 uint8_t dataRate = tag.GetDataRate();
66 double frequency = tag.GetFrequency();
67 NS_LOG_DEBUG("DR: " << unsigned(dataRate));
68 NS_LOG_DEBUG("SF: " << unsigned(GetSfFromDataRate(dataRate)));
69 NS_LOG_DEBUG("BW: " << GetBandwidthFromDataRate(dataRate));
70 NS_LOG_DEBUG("Freq: " << frequency << " MHz");
71 packet->AddPacketTag(tag);
72
73 // Make sure we can transmit this packet
74 if (m_channelHelper.GetWaitingTime(CreateObject<LogicalLoraChannel>(frequency)) > Time(0))
75 {
76 // We cannot send now!
77 NS_LOG_WARN("Trying to send a packet but Duty Cycle won't allow it. Aborting.");
78 return;
79 }
80
81 LoraTxParameters params;
82 params.sf = GetSfFromDataRate(dataRate);
83 params.headerDisabled = false;
84 params.codingRate = 1;
85 params.bandwidthHz = GetBandwidthFromDataRate(dataRate);
86 params.nPreamble = 8;
87 params.crcEnabled = true;
88 params.lowDataRateOptimizationEnabled = LoraPhy::GetTSym(params) > MilliSeconds(16);
89
90 // Get the duration
91 Time duration = m_phy->GetOnAirTime(packet, params);
92
93 NS_LOG_DEBUG("Duration: " << duration.GetSeconds());
94
95 // Find the channel with the desired frequency
96 double sendingPower =
97 m_channelHelper.GetTxPowerForChannel(CreateObject<LogicalLoraChannel>(frequency));
98
99 // Add the event to the channelHelper to keep track of duty cycle
100 m_channelHelper.AddEvent(duration, CreateObject<LogicalLoraChannel>(frequency));
101
102 // Send the packet to the PHY layer to send it on the channel
103 m_phy->Send(packet, params, frequency, sendingPower);
104
105 m_sentNewPacket(packet);
106}
107
108bool
110{
111 return m_phy->IsTransmitting();
112}
113
114void
116{
117 NS_LOG_FUNCTION(this << packet);
118
119 // Make a copy of the packet to work on
120 Ptr<Packet> packetCopy = packet->Copy();
121
122 // Only forward the packet if it's uplink
123 LorawanMacHeader macHdr;
124 packetCopy->PeekHeader(macHdr);
125
126 if (macHdr.IsUplink())
127 {
128 m_device->GetObject<LoraNetDevice>()->Receive(packetCopy);
129
130 NS_LOG_DEBUG("Received packet: " << packet);
131
132 m_receivedPacket(packet);
133 }
134 else
135 {
136 NS_LOG_DEBUG("Not forwarding downlink message to NetDevice");
137 }
138}
139
140void
142{
143 NS_LOG_FUNCTION(this << packet);
144}
145
146void
148{
150}
151
152Time
154{
156
157 return m_channelHelper.GetWaitingTime(CreateObject<LogicalLoraChannel>(frequency));
158}
159} // namespace lorawan
160} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
bool IsTransmitting()
Check whether the underlying PHY layer of the gateway is currently transmitting.
~GatewayLorawanMac() override
Destructor.
static TypeId GetTypeId()
Register this type.
void FailedReception(Ptr< const Packet > packet) override
Function called by lower layers to inform this layer that reception of a packet we were locked on fai...
void Send(Ptr< Packet > packet) override
Send a packet.
void TxFinished(Ptr< const Packet > packet) override
Perform actions after sending a packet.
Time GetWaitingTime(double frequency)
Return the next time at which we will be able to transmit on the specified frequency.
void Receive(Ptr< const Packet > packet) override
Receive a packet from the lower layer.
GatewayLorawanMac()
Default constructor.
double GetTxPowerForChannel(Ptr< LogicalLoraChannel > logicalChannel)
Returns the maximum transmission power [dBm] that is allowed on a channel.
Time GetWaitingTime(Ptr< LogicalLoraChannel > channel)
Get the time it is necessary to wait for before transmitting on a given channel.
void AddEvent(Time duration, Ptr< LogicalLoraChannel > channel)
Register the transmission of a packet.
Hold together all LoRa related objects.
static Time GetTSym(LoraTxParameters txParams)
Compute the symbol time from spreading factor and bandwidth.
Definition: lora-phy.cc:161
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
Definition: lora-tag.h:37
uint8_t GetDataRate() const
Get the data rate for this packet.
Definition: lora-tag.cc:142
double GetFrequency() const
Get the frequency of the packet.
Definition: lora-tag.cc:136
This class represents the Mac header of a LoRaWAN packet.
bool IsUplink() const
Check whether this header is for an uplink message.
Class representing the LoRaWAN MAC layer.
Definition: lorawan-mac.h:48
TracedCallback< Ptr< const Packet > > m_receivedPacket
Trace source that is fired when a packet reaches the MAC layer.
Definition: lorawan-mac.h:236
uint8_t GetSfFromDataRate(uint8_t dataRate)
Get the spreading factor corresponding to a data rate, based on this MAC's region.
Definition: lorawan-mac.cc:111
double GetBandwidthFromDataRate(uint8_t dataRate)
Get the bandwidth corresponding to a data rate, based on this MAC's region.
Definition: lorawan-mac.cc:125
LogicalLoraChannelHelper m_channelHelper
The LogicalLoraChannelHelper instance that is assigned to this MAC.
Definition: lorawan-mac.h:257
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:242
Ptr< LoraPhy > m_phy
The PHY instance that sits under this MAC layer.
Definition: lorawan-mac.h:247
Ptr< NetDevice > m_device
The device this MAC layer is installed on.
Definition: lorawan-mac.h:252
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition: lora-phy.h:49