A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
logical-lora-channel-helper.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 "ns3/log.h"
12#include "ns3/simulator.h"
13
14namespace ns3
15{
16namespace lorawan
17{
18
19NS_LOG_COMPONENT_DEFINE("LogicalLoraChannelHelper");
20
21NS_OBJECT_ENSURE_REGISTERED(LogicalLoraChannelHelper);
22
23TypeId
25{
26 static TypeId tid =
27 TypeId("ns3::LogicalLoraChannelHelper").SetParent<Object>().SetGroupName("lorawan");
28 return tid;
29}
30
32 : m_nextAggregatedTransmissionTime(Seconds(0)),
33 m_aggregatedDutyCycle(1)
34{
35 NS_LOG_FUNCTION(this);
36}
37
42
43std::vector<Ptr<LogicalLoraChannel>>
45{
46 NS_LOG_FUNCTION(this);
47
48 // Make a copy of the channel vector
49 std::vector<Ptr<LogicalLoraChannel>> vector;
50 vector.reserve(m_channelList.size());
51 std::copy(m_channelList.begin(), m_channelList.end(), std::back_inserter(vector));
52
53 return vector;
54}
55
56std::vector<Ptr<LogicalLoraChannel>>
58{
59 NS_LOG_FUNCTION(this);
60
61 // Make a copy of the channel vector
62 std::vector<Ptr<LogicalLoraChannel>> vector;
63 vector.reserve(m_channelList.size());
64 std::copy(m_channelList.begin(),
65 m_channelList.end(),
66 std::back_inserter(vector)); // Working on a copy
67
68 std::vector<Ptr<LogicalLoraChannel>> channels;
69 std::vector<Ptr<LogicalLoraChannel>>::iterator it;
70 for (it = vector.begin(); it != vector.end(); it++)
71 {
72 if ((*it)->IsEnabledForUplink())
73 {
74 channels.push_back(*it);
75 }
76 }
77
78 return channels;
79}
80
86
89{
90 // Get the SubBand this frequency belongs to
91 std::list<Ptr<SubBand>>::iterator it;
92 for (it = m_subBandList.begin(); it != m_subBandList.end(); it++)
93 {
94 if ((*it)->BelongsToSubBand(frequency))
95 {
96 return *it;
97 }
98 }
99
100 NS_LOG_ERROR("Requested frequency: " << frequency);
101 NS_ABORT_MSG("Warning: frequency is outside any known SubBand.");
102
103 return nullptr; // If no SubBand is found, return 0
104}
105
106void
108{
109 NS_LOG_FUNCTION(this << frequency);
110
111 // Create the new channel and increment the counter
113
114 // Add it to the list
115 m_channelList.push_back(channel);
116
117 NS_LOG_DEBUG("Added a channel. Current number of channels in list is " << m_channelList.size());
118}
119
120void
122{
123 NS_LOG_FUNCTION(this << logicalChannel);
124
125 // Add it to the list
126 m_channelList.push_back(logicalChannel);
127}
128
129void
131
132{
133 NS_LOG_FUNCTION(this << chIndex << logicalChannel);
134
135 m_channelList.at(chIndex) = logicalChannel;
136}
137
138void
140 double lastFrequency,
141 double dutyCycle,
142 double maxTxPowerDbm)
143{
144 NS_LOG_FUNCTION(this << firstFrequency << lastFrequency);
145
146 Ptr<SubBand> subBand = Create<SubBand>(firstFrequency, lastFrequency, dutyCycle, maxTxPowerDbm);
147
148 m_subBandList.push_back(subBand);
149}
150
151void
153{
154 NS_LOG_FUNCTION(this << subBand);
155
156 m_subBandList.push_back(subBand);
157}
158
159void
161{
162 // Search and remove the channel from the list
163 std::vector<Ptr<LogicalLoraChannel>>::iterator it;
164 for (it = m_channelList.begin(); it != m_channelList.end(); it++)
165 {
166 Ptr<LogicalLoraChannel> currentChannel = *it;
167 if (currentChannel == logicalChannel)
168 {
169 m_channelList.erase(it);
170 return;
171 }
172 }
173}
174
175Time
177{
178 // Aggregate waiting time
179 Time aggregatedWaitingTime = m_nextAggregatedTransmissionTime - Simulator::Now();
180
181 // Handle case in which waiting time is negative
182 aggregatedWaitingTime = Seconds(std::max(aggregatedWaitingTime.GetSeconds(), double(0)));
183
184 NS_LOG_DEBUG("Aggregated waiting time: " << aggregatedWaitingTime.GetSeconds());
185
186 return aggregatedWaitingTime;
187}
188
189Time
191{
192 NS_LOG_FUNCTION(this << channel);
193
194 // SubBand waiting time
195 Time subBandWaitingTime =
196 GetSubBandFromChannel(channel)->GetNextTransmissionTime() - Simulator::Now();
197
198 // Handle case in which waiting time is negative
199 subBandWaitingTime = Seconds(std::max(subBandWaitingTime.GetSeconds(), double(0)));
200
201 NS_LOG_DEBUG("Waiting time: " << subBandWaitingTime.GetSeconds());
202
203 return subBandWaitingTime;
204}
205
206void
208{
209 NS_LOG_FUNCTION(this << duration << channel);
210
211 Ptr<SubBand> subBand = GetSubBandFromChannel(channel);
212
213 double dutyCycle = subBand->GetDutyCycle();
214 double timeOnAir = duration.GetSeconds();
215
216 // Computation of necessary waiting time on this sub-band
217 subBand->SetNextTransmissionTime(Simulator::Now() + Seconds(timeOnAir / dutyCycle - timeOnAir));
218
219 // Computation of necessary aggregate waiting time
221 Simulator::Now() + Seconds(timeOnAir / m_aggregatedDutyCycle - timeOnAir);
222
223 NS_LOG_DEBUG("Time on air: " << timeOnAir);
224 NS_LOG_DEBUG("m_aggregatedDutyCycle: " << m_aggregatedDutyCycle);
225 NS_LOG_DEBUG("Current time: " << Simulator::Now().GetSeconds());
226 NS_LOG_DEBUG("Next transmission on this sub-band allowed at time: "
227 << (subBand->GetNextTransmissionTime()).GetSeconds());
228 NS_LOG_DEBUG("Next aggregated transmission allowed at time "
230}
231
232double
234{
236
237 // Get the maxTxPowerDbm from the SubBand this channel is in
238 std::list<Ptr<SubBand>>::iterator it;
239 for (it = m_subBandList.begin(); it != m_subBandList.end(); it++)
240 {
241 // Check whether this channel is in this SubBand
242 if ((*it)->BelongsToSubBand(logicalChannel->GetFrequency()))
243 {
244 return (*it)->GetMaxTxPowerDbm();
245 }
246 }
247 NS_ABORT_MSG("Logical channel doesn't belong to a known SubBand");
248
249 return 0;
250}
251
252void
254{
255 NS_LOG_FUNCTION(this << index);
256
257 m_channelList.at(index)->DisableForUplink();
258}
259} // namespace lorawan
260} // namespace ns3
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
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
void RemoveChannel(Ptr< LogicalLoraChannel > channel)
Remove a channel.
double GetTxPowerForChannel(Ptr< LogicalLoraChannel > logicalChannel)
Returns the maximum transmission power [dBm] that is allowed on a channel.
Time GetAggregatedWaitingTime()
Get the time it is necessary to wait before transmitting again, according to the aggregate duty cycle...
Ptr< SubBand > GetSubBandFromFrequency(double frequency)
Get the SubBand a frequency belongs to.
void SetChannel(uint8_t chIndex, Ptr< LogicalLoraChannel > logicalChannel)
Set a new channel at a fixed index.
void DisableChannel(int index)
Disable the channel at a specified index.
double m_aggregatedDutyCycle
transmission will be possible according to the aggregated transmission timer
std::vector< Ptr< LogicalLoraChannel > > GetChannelList()
Get the list of LogicalLoraChannels currently registered on this helper.
static TypeId GetTypeId()
Register this type.
std::vector< Ptr< LogicalLoraChannel > > m_channelList
A vector of the LogicalLoraChannels that are currently registered within this helper.
Time m_nextAggregatedTransmissionTime
The next time at which.
std::vector< Ptr< LogicalLoraChannel > > GetEnabledChannelList()
Get the list of LogicalLoraChannels currently registered on this helper that have been enabled for Up...
void AddSubBand(double firstFrequency, double lastFrequency, double dutyCycle, double maxTxPowerDbm)
Add a new SubBand to this helper.
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.
std::list< Ptr< SubBand > > m_subBandList
A list of the SubBands that are currently registered within this helper.
Ptr< SubBand > GetSubBandFromChannel(Ptr< LogicalLoraChannel > channel)
Get the SubBand a channel belongs to.
void AddChannel(double frequency)
Add a new channel to the list.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.