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 auto sf = GetSfFromDataRate(dataRate);
70 auto bw = GetBandwidthFromDataRate(dataRate);
71 // see SX1272/73 Datasheet, Section 4.1.1.6, Rev. 4, Jan. 2019
72 auto ldro = bool((sf == 11 || sf == 12) && bw == 125'000);
73
74 LoraTxParameters params;
75 params.spreadingFactor = sf;
76 params.bandwidthHz = bw;
77 params.codingRate = CodingRate::CR_4_5;
78 params.lowDataRateOptimize = ldro;
79 params.preambleLenSymb = 8;
80 params.implicitHeader = false;
81 params.crcEnabled = true;
82
83 // Get the duration
84 Time duration = LoraPhy::GetTimeOnAir(packet->GetSize(), params);
85
86 NS_LOG_DEBUG("Duration: " << duration.As(Time::S));
87
88 // Find the channel with the desired frequency
89 double sendingPower = m_channelHelper->GetTxPowerForChannel(frequencyHz);
90
91 // Add the event to the channelHelper to keep track of duty cycle
92 m_channelHelper->AddEvent(duration, frequencyHz);
93
94 // Send the packet to the PHY layer to send it on the channel
95 m_phy->Send(packet, frequencyHz, IQPolarity::DOWN, params, sendingPower);
96
97 m_sentNewPacket(packet);
98}
99
100bool
102{
103 return m_phy->IsTransmitting();
104}
105
106Time
108{
110 return m_channelHelper->GetWaitTime(frequencyHz);
111}
112
113void
118
119void
121{
122 NS_LOG_FUNCTION(this << packet);
123
124 // Make a copy of the packet to work on
125 Ptr<Packet> packetCopy = packet->Copy();
126
127 // Only forward the packet if it's uplink
128 LorawanMacHeader macHdr;
129 packetCopy->PeekHeader(macHdr);
130
131 if (macHdr.IsUplink())
132 {
133 NS_LOG_DEBUG("Received packet: " << packet);
134 // Pass the packet up to the NetDevice
135 if (!m_receiveCallback.IsNull())
136 {
137 m_receiveCallback(packetCopy);
138 }
139 m_receivedPacket(packet);
140 }
141 else
142 {
143 NS_LOG_DEBUG("Not forwarding downlink message to NetDevice");
144 }
145}
146
147void
152
153} // namespace lorawan
154} // 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
Inform this layer that reception of a packet we were locked on failed.
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 GetTimeOnAir(uint32_t phyPayloadLen, const LoraTxParameters &txParams)
Compute the total transmission time for a physical packet based on modulation parameters.
Definition lora-phy.cc:110
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.
uint32_t GetBandwidthFromDataRate(uint8_t dataRate) const
Get the bandwidth corresponding to a data rate, based on this MAC's region.
uint8_t GetSfFromDataRate(uint8_t dataRate) const
Get the spreading factor corresponding to a data rate, based on this MAC's region.
Ptr< LogicalLoraChannelHelper > m_channelHelper
The LogicalLoraChannelHelper instance that is assigned to this MAC.
ReceiveCallback m_receiveCallback
! Callback to forward to upper layers
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.
#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:50
@ DOWN
Downlink / Downchirp / Inverted polarity.
Definition lora-phy.h:35
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
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:73