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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Davide Magrin <magrinda@dei.unipd.it>
7 */
8
10
11#include "lora-net-device.h"
12#include "lora-phy.h"
13#include "lora-tag.h"
14#include "lorawan-mac-header.h"
15
16namespace ns3
17{
18namespace lorawan
19{
20
21NS_LOG_COMPONENT_DEFINE("GatewayLorawanMac");
22
24
25TypeId
27{
28 static TypeId tid = TypeId("ns3::GatewayLorawanMac")
30 .AddConstructor<GatewayLorawanMac>()
31 .SetGroupName("lorawan");
32 return tid;
33}
34
39
44
45void
47{
48 NS_LOG_FUNCTION(this << packet);
49
50 // Get data rate to send this packet with
51 LoraTag tag;
52 packet->RemovePacketTag(tag);
53 uint8_t dataRate = tag.GetDataRate();
54 uint32_t frequencyHz = tag.GetFrequency();
55 NS_LOG_DEBUG("DR: " << unsigned(dataRate));
56 NS_LOG_DEBUG("SF: " << unsigned(GetSfFromDataRate(dataRate)));
57 NS_LOG_DEBUG("BW: " << GetBandwidthFromDataRate(dataRate));
58 NS_LOG_DEBUG("Freq: " << frequencyHz << " Hz");
59 packet->AddPacketTag(tag);
60
61 // Make sure we can transmit this packet
62 if (GetWaitTime(frequencyHz).IsStrictlyPositive())
63 {
64 // We cannot send now!
65 NS_LOG_WARN("Trying to send a packet but Duty Cycle won't allow it. Aborting.");
66 return;
67 }
68
69 LoraTxParameters params;
70 params.sf = GetSfFromDataRate(dataRate);
71 params.headerDisabled = false;
72 params.codingRate = CodingRate::CR_4_5;
73 params.bandwidthHz = GetBandwidthFromDataRate(dataRate);
74 params.nPreamble = 8;
75 params.crcEnabled = true;
76 params.lowDataRateOptimizationEnabled = LoraPhy::GetTSym(params) > MilliSeconds(16);
77
78 // Get the duration
79 Time duration = LoraPhy::GetOnAirTime(packet, params);
80
81 NS_LOG_DEBUG("Duration: " << duration.As(Time::S));
82
83 // Find the channel with the desired frequency
84 double sendingPower = m_channelHelper->GetTxPowerForChannel(frequencyHz);
85
86 // Add the event to the channelHelper to keep track of duty cycle
87 m_channelHelper->AddEvent(duration, frequencyHz);
88
89 // Send the packet to the PHY layer to send it on the channel
90 m_phy->Send(packet, params, frequencyHz, sendingPower);
91
92 m_sentNewPacket(packet);
93}
94
95bool
97{
98 return m_phy->IsTransmitting();
99}
100
101void
103{
104 NS_LOG_FUNCTION(this << packet);
105
106 // Make a copy of the packet to work on
107 Ptr<Packet> packetCopy = packet->Copy();
108
109 // Only forward the packet if it's uplink
110 LorawanMacHeader macHdr;
111 packetCopy->PeekHeader(macHdr);
112
113 if (macHdr.IsUplink())
114 {
115 DynamicCast<LoraNetDevice>(m_device)->Receive(packetCopy);
116
117 NS_LOG_DEBUG("Received packet: " << packet);
118
119 m_receivedPacket(packet);
120 }
121 else
122 {
123 NS_LOG_DEBUG("Not forwarding downlink message to NetDevice");
124 }
125}
126
127void
132
133void
138
139Time
141{
143 return m_channelHelper->GetWaitTime(frequencyHz);
144}
145
146} // namespace lorawan
147} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:408
@ S
second
Definition nstime.h:106
a unique identifier for an interface.
Definition type-id.h:50
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
Class representing the MAC layer of a LoRaWAN gateway.
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.
Time GetWaitTime(uint32_t frequencyHz)
Return the next time at which we will be able to transmit on the specified frequency.
void TxFinished(Ptr< const Packet > packet) override
Perform actions after sending a packet.
void Receive(Ptr< const Packet > packet) override
Receive a packet from the lower layer.
GatewayLorawanMac()
Default constructor.
static Time GetOnAirTime(Ptr< Packet > packet, LoraTxParameters txParams)
Compute the time that a packet with certain characteristics will take to be transmitted.
Definition lora-phy.cc:155
static Time GetTSym(LoraTxParameters txParams)
Compute the symbol time from spreading factor and bandwidth.
Definition lora-phy.cc:149
Tag used to save various data about a packet, like its Spreading Factor and data about interference.
Definition lora-tag.h:26
uint8_t GetDataRate() const
Get the data rate for this packet.
Definition lora-tag.cc:128
uint32_t GetFrequency() const
Get the frequency of the packet.
Definition lora-tag.cc:122
This class represents the Mac header of a LoRaWAN packet.
bool IsUplink() const
Check whether this header is for an uplink message.
LorawanMac()
Default constructor.
TracedCallback< Ptr< const Packet > > m_receivedPacket
Trace source that is fired when a packet reaches the MAC layer.
uint8_t GetSfFromDataRate(uint8_t dataRate)
Get the spreading factor corresponding to a data rate, based on this MAC's region.
double GetBandwidthFromDataRate(uint8_t dataRate)
Get the bandwidth corresponding to a data rate, based on this MAC's region.
Ptr< LogicalLoraChannelHelper > m_channelHelper
The LogicalLoraChannelHelper instance that is assigned to this MAC.
TracedCallback< Ptr< const Packet > > m_sentNewPacket
Trace source that is fired when a new APP layer packet arrives at the MAC layer.
Ptr< LoraPhy > m_phy
The PHY instance that sits under this MAC layer.
Ptr< NetDevice > m_device
The device this MAC layer is installed on.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#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:253
@ CR_4_5
Coding rate 4/5.
Definition lora-phy.h:31
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1290
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:605
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition lora-phy.h:54