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 * 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
21
22#include "ns3/log.h"
23#include "ns3/simulator.h"
24
25namespace ns3
26{
27namespace lorawan
28{
29
30NS_LOG_COMPONENT_DEFINE("LogicalLoraChannelHelper");
31
32NS_OBJECT_ENSURE_REGISTERED(LogicalLoraChannelHelper);
33
34TypeId
36{
37 static TypeId tid =
38 TypeId("ns3::LogicalLoraChannelHelper").SetParent<Object>().SetGroupName("lorawan");
39 return tid;
40}
41
43 : m_nextAggregatedTransmissionTime(Seconds(0)),
44 m_aggregatedDutyCycle(1)
45{
46 NS_LOG_FUNCTION(this);
47}
48
50{
51 NS_LOG_FUNCTION(this);
52}
53
54std::vector<Ptr<LogicalLoraChannel>>
56{
57 NS_LOG_FUNCTION(this);
58
59 // Make a copy of the channel vector
60 std::vector<Ptr<LogicalLoraChannel>> vector;
61 vector.reserve(m_channelList.size());
62 std::copy(m_channelList.begin(), m_channelList.end(), std::back_inserter(vector));
63
64 return vector;
65}
66
67std::vector<Ptr<LogicalLoraChannel>>
69{
70 NS_LOG_FUNCTION(this);
71
72 // Make a copy of the channel vector
73 std::vector<Ptr<LogicalLoraChannel>> vector;
74 vector.reserve(m_channelList.size());
75 std::copy(m_channelList.begin(),
76 m_channelList.end(),
77 std::back_inserter(vector)); // Working on a copy
78
79 std::vector<Ptr<LogicalLoraChannel>> channels;
80 std::vector<Ptr<LogicalLoraChannel>>::iterator it;
81 for (it = vector.begin(); it != vector.end(); it++)
82 {
83 if ((*it)->IsEnabledForUplink())
84 {
85 channels.push_back(*it);
86 }
87 }
88
89 return channels;
90}
91
94{
95 return GetSubBandFromFrequency(channel->GetFrequency());
96}
97
100{
101 // Get the SubBand this frequency belongs to
102 std::list<Ptr<SubBand>>::iterator it;
103 for (it = m_subBandList.begin(); it != m_subBandList.end(); it++)
104 {
105 if ((*it)->BelongsToSubBand(frequency))
106 {
107 return *it;
108 }
109 }
110
111 NS_LOG_ERROR("Requested frequency: " << frequency);
112 NS_ABORT_MSG("Warning: frequency is outside any known SubBand.");
113
114 return nullptr; // If no SubBand is found, return 0
115}
116
117void
119{
120 NS_LOG_FUNCTION(this << frequency);
121
122 // Create the new channel and increment the counter
123 Ptr<LogicalLoraChannel> channel = Create<LogicalLoraChannel>(frequency);
124
125 // Add it to the list
126 m_channelList.push_back(channel);
127
128 NS_LOG_DEBUG("Added a channel. Current number of channels in list is " << m_channelList.size());
129}
130
131void
133{
134 NS_LOG_FUNCTION(this << logicalChannel);
135
136 // Add it to the list
137 m_channelList.push_back(logicalChannel);
138}
139
140void
142
143{
144 NS_LOG_FUNCTION(this << chIndex << logicalChannel);
145
146 m_channelList.at(chIndex) = logicalChannel;
147}
148
149void
151 double lastFrequency,
152 double dutyCycle,
153 double maxTxPowerDbm)
154{
155 NS_LOG_FUNCTION(this << firstFrequency << lastFrequency);
156
157 Ptr<SubBand> subBand = Create<SubBand>(firstFrequency, lastFrequency, dutyCycle, maxTxPowerDbm);
158
159 m_subBandList.push_back(subBand);
160}
161
162void
164{
165 NS_LOG_FUNCTION(this << subBand);
166
167 m_subBandList.push_back(subBand);
168}
169
170void
172{
173 // Search and remove the channel from the list
174 std::vector<Ptr<LogicalLoraChannel>>::iterator it;
175 for (it = m_channelList.begin(); it != m_channelList.end(); it++)
176 {
177 Ptr<LogicalLoraChannel> currentChannel = *it;
178 if (currentChannel == logicalChannel)
179 {
180 m_channelList.erase(it);
181 return;
182 }
183 }
184}
185
186Time
188{
189 // Aggregate waiting time
190 Time aggregatedWaitingTime = m_nextAggregatedTransmissionTime - Simulator::Now();
191
192 // Handle case in which waiting time is negative
193 aggregatedWaitingTime = Seconds(std::max(aggregatedWaitingTime.GetSeconds(), double(0)));
194
195 NS_LOG_DEBUG("Aggregated waiting time: " << aggregatedWaitingTime.GetSeconds());
196
197 return aggregatedWaitingTime;
198}
199
200Time
202{
203 NS_LOG_FUNCTION(this << channel);
204
205 // SubBand waiting time
206 Time subBandWaitingTime =
207 GetSubBandFromChannel(channel)->GetNextTransmissionTime() - Simulator::Now();
208
209 // Handle case in which waiting time is negative
210 subBandWaitingTime = Seconds(std::max(subBandWaitingTime.GetSeconds(), double(0)));
211
212 NS_LOG_DEBUG("Waiting time: " << subBandWaitingTime.GetSeconds());
213
214 return subBandWaitingTime;
215}
216
217void
219{
220 NS_LOG_FUNCTION(this << duration << channel);
221
222 Ptr<SubBand> subBand = GetSubBandFromChannel(channel);
223
224 double dutyCycle = subBand->GetDutyCycle();
225 double timeOnAir = duration.GetSeconds();
226
227 // Computation of necessary waiting time on this sub-band
228 subBand->SetNextTransmissionTime(Simulator::Now() + Seconds(timeOnAir / dutyCycle - timeOnAir));
229
230 // Computation of necessary aggregate waiting time
232 Simulator::Now() + Seconds(timeOnAir / m_aggregatedDutyCycle - timeOnAir);
233
234 NS_LOG_DEBUG("Time on air: " << timeOnAir);
235 NS_LOG_DEBUG("m_aggregatedDutyCycle: " << m_aggregatedDutyCycle);
236 NS_LOG_DEBUG("Current time: " << Simulator::Now().GetSeconds());
237 NS_LOG_DEBUG("Next transmission on this sub-band allowed at time: "
238 << (subBand->GetNextTransmissionTime()).GetSeconds());
239 NS_LOG_DEBUG("Next aggregated transmission allowed at time "
241}
242
243double
245{
247
248 // Get the maxTxPowerDbm from the SubBand this channel is in
249 std::list<Ptr<SubBand>>::iterator it;
250 for (it = m_subBandList.begin(); it != m_subBandList.end(); it++)
251 {
252 // Check whether this channel is in this SubBand
253 if ((*it)->BelongsToSubBand(logicalChannel->GetFrequency()))
254 {
255 return (*it)->GetMaxTxPowerDbm();
256 }
257 }
258 NS_ABORT_MSG("Logical channel doesn't belong to a known SubBand");
259
260 return 0;
261}
262
263void
265{
266 NS_LOG_FUNCTION(this << index);
267
268 m_channelList.at(index)->DisableForUplink();
269}
270} // namespace lorawan
271} // namespace ns3
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
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
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:49
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#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_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Every class exported by the ns3 library is enclosed in the ns3 namespace.