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-frame-header.h"
12#include "lora-net-device.h"
13#include "lorawan-mac-header.h"
14
15#include "ns3/log.h"
16
17namespace ns3
18{
19namespace lorawan
20{
21
22NS_LOG_COMPONENT_DEFINE("GatewayLorawanMac");
23
24NS_OBJECT_ENSURE_REGISTERED(GatewayLorawanMac);
25
26TypeId
28{
29 static TypeId tid = TypeId("ns3::GatewayLorawanMac")
31 .AddConstructor<GatewayLorawanMac>()
32 .SetGroupName("lorawan");
33 return tid;
34}
35
40
45
46void
48{
49 NS_LOG_FUNCTION(this << packet);
50
51 // Get data rate to send this packet with
52 LoraTag tag;
53 packet->RemovePacketTag(tag);
54 uint8_t dataRate = tag.GetDataRate();
55 double frequency = tag.GetFrequency();
56 NS_LOG_DEBUG("DR: " << unsigned(dataRate));
57 NS_LOG_DEBUG("SF: " << unsigned(GetSfFromDataRate(dataRate)));
58 NS_LOG_DEBUG("BW: " << GetBandwidthFromDataRate(dataRate));
59 NS_LOG_DEBUG("Freq: " << frequency << " MHz");
60 packet->AddPacketTag(tag);
61
62 // Make sure we can transmit this packet
63 if (m_channelHelper->GetWaitingTime(CreateObject<LogicalLoraChannel>(frequency)) > Time(0))
64 {
65 // We cannot send now!
66 NS_LOG_WARN("Trying to send a packet but Duty Cycle won't allow it. Aborting.");
67 return;
68 }
69
70 LoraTxParameters params;
71 params.sf = GetSfFromDataRate(dataRate);
72 params.headerDisabled = false;
73 params.codingRate = 1;
74 params.bandwidthHz = GetBandwidthFromDataRate(dataRate);
75 params.nPreamble = 8;
76 params.crcEnabled = true;
77 params.lowDataRateOptimizationEnabled = LoraPhy::GetTSym(params) > MilliSeconds(16);
78
79 // Get the duration
80 Time duration = LoraPhy::GetOnAirTime(packet, params);
81
82 NS_LOG_DEBUG("Duration: " << duration.GetSeconds());
83
84 // Find the channel with the desired frequency
85 double sendingPower =
86 m_channelHelper->GetTxPowerForChannel(CreateObject<LogicalLoraChannel>(frequency));
87
88 // Add the event to the channelHelper to keep track of duty cycle
89 m_channelHelper->AddEvent(duration, CreateObject<LogicalLoraChannel>(frequency));
90
91 // Send the packet to the PHY layer to send it on the channel
92 m_phy->Send(packet, params, frequency, sendingPower);
93
94 m_sentNewPacket(packet);
95}
96
97bool
99{
100 return m_phy->IsTransmitting();
101}
102
103void
105{
106 NS_LOG_FUNCTION(this << packet);
107
108 // Make a copy of the packet to work on
109 Ptr<Packet> packetCopy = packet->Copy();
110
111 // Only forward the packet if it's uplink
112 LorawanMacHeader macHdr;
113 packetCopy->PeekHeader(macHdr);
114
115 if (macHdr.IsUplink())
116 {
117 DynamicCast<LoraNetDevice>(m_device)->Receive(packetCopy);
118
119 NS_LOG_DEBUG("Received packet: " << packet);
120
121 m_receivedPacket(packet);
122 }
123 else
124 {
125 NS_LOG_DEBUG("Not forwarding downlink message to NetDevice");
126 }
127}
128
129void
134
135void
140
141Time
143{
145
146 return m_channelHelper->GetWaitingTime(CreateObject<LogicalLoraChannel>(frequency));
147}
148} // namespace lorawan
149} // namespace ns3
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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.
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:156
static Time GetTSym(LoraTxParameters txParams)
Compute the symbol time from spreading factor and bandwidth.
Definition lora-phy.cc:150
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:131
double GetFrequency() const
Get the frequency of the packet.
Definition lora-tag.cc:125
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:37
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:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:250
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:1320
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:580
Structure to collect all parameters that are used to compute the duration of a packet (excluding payl...
Definition lora-phy.h:38