OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2017 University of Padova |
| 4 * |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 * |
| 18 * Author: Davide Magrin <magrinda@dei.unipd.it> |
| 19 */ |
| 20 |
| 21 #include "ns3/logical-lora-channel-helper.h" |
| 22 #include "ns3/simulator.h" |
| 23 #include "ns3/log.h" |
| 24 |
| 25 namespace ns3 { |
| 26 |
| 27 NS_LOG_COMPONENT_DEFINE ("LogicalLoraChannelHelper"); |
| 28 |
| 29 NS_OBJECT_ENSURE_REGISTERED (LogicalLoraChannelHelper); |
| 30 |
| 31 TypeId |
| 32 LogicalLoraChannelHelper::GetTypeId (void) |
| 33 { |
| 34 static TypeId tid = TypeId ("ns3::LogicalLoraChannelHelper") |
| 35 .SetParent<Object> () |
| 36 .SetGroupName ("lorawan"); |
| 37 return tid; |
| 38 } |
| 39 |
| 40 LogicalLoraChannelHelper::LogicalLoraChannelHelper () : |
| 41 m_nextAggregatedTransmissionTime (Seconds (0)), |
| 42 m_aggregatedDutyCycle (1) |
| 43 { |
| 44 NS_LOG_FUNCTION (this); |
| 45 } |
| 46 |
| 47 LogicalLoraChannelHelper::~LogicalLoraChannelHelper () { |
| 48 NS_LOG_FUNCTION (this); |
| 49 } |
| 50 |
| 51 std::vector<Ptr <LogicalLoraChannel> > |
| 52 LogicalLoraChannelHelper::GetChannelList (void) |
| 53 { |
| 54 NS_LOG_FUNCTION (this); |
| 55 |
| 56 // Make a copy of the channel vector |
| 57 std::vector<Ptr<LogicalLoraChannel> > vector; |
| 58 vector.reserve (m_channelList.size ()); |
| 59 std::copy (m_channelList.begin (), m_channelList.end (), std::back_inserter |
| 60 (vector)); |
| 61 |
| 62 return vector; |
| 63 } |
| 64 |
| 65 Ptr<SubBand> |
| 66 LogicalLoraChannelHelper::GetSubBandFromChannel (Ptr<LogicalLoraChannel> |
| 67 channel) |
| 68 { |
| 69 return GetSubBandFromFrequency (channel->GetFrequency ()); |
| 70 } |
| 71 |
| 72 Ptr<SubBand> |
| 73 LogicalLoraChannelHelper::GetSubBandFromFrequency (double frequency) |
| 74 { |
| 75 // Get the SubBand this frequency belongs to |
| 76 std::list< Ptr< SubBand > >::iterator it; |
| 77 for (it = m_subBandList.begin (); it != m_subBandList.end (); it++) { |
| 78 if ((*it)->BelongsToSubBand (frequency)) |
| 79 { |
| 80 return *it; |
| 81 } |
| 82 } |
| 83 |
| 84 NS_LOG_ERROR ("Warning: frequency is outside any known SubBand."); |
| 85 |
| 86 return 0; // If no SubBand is found, return 0 |
| 87 } |
| 88 |
| 89 void |
| 90 LogicalLoraChannelHelper::AddChannel (double frequency) |
| 91 { |
| 92 NS_LOG_FUNCTION (this << frequency); |
| 93 |
| 94 // Create the new channel and increment the counter |
| 95 Ptr<LogicalLoraChannel> channel = Create<LogicalLoraChannel> (frequency); |
| 96 |
| 97 // Add it to the list |
| 98 m_channelList.push_back (channel); |
| 99 |
| 100 NS_LOG_DEBUG ("Added a channel. Current number of channels in list is " << |
| 101 m_channelList.size ()); |
| 102 } |
| 103 |
| 104 void |
| 105 LogicalLoraChannelHelper::AddChannel (Ptr<LogicalLoraChannel> logicalChannel) |
| 106 { |
| 107 NS_LOG_FUNCTION (this << logicalChannel); |
| 108 |
| 109 // Add it to the list |
| 110 m_channelList.push_back (logicalChannel); |
| 111 } |
| 112 |
| 113 void |
| 114 LogicalLoraChannelHelper::SetChannel (uint8_t chIndex, |
| 115 Ptr<LogicalLoraChannel> logicalChannel) |
| 116 |
| 117 { |
| 118 NS_LOG_FUNCTION (this << chIndex << logicalChannel); |
| 119 |
| 120 m_channelList.at (chIndex) = logicalChannel; |
| 121 } |
| 122 |
| 123 void |
| 124 LogicalLoraChannelHelper::AddSubBand (double firstFrequency, |
| 125 double lastFrequency, double dutyCycle, |
| 126 double maxTxPowerDbm) |
| 127 { |
| 128 NS_LOG_FUNCTION (this << firstFrequency << lastFrequency); |
| 129 |
| 130 Ptr<SubBand> subBand = Create<SubBand> (firstFrequency, lastFrequency, |
| 131 dutyCycle, maxTxPowerDbm); |
| 132 |
| 133 m_subBandList.push_back (subBand); |
| 134 } |
| 135 |
| 136 void |
| 137 LogicalLoraChannelHelper::AddSubBand (Ptr<SubBand> subBand) |
| 138 { |
| 139 NS_LOG_FUNCTION (this << subBand); |
| 140 |
| 141 m_subBandList.push_back (subBand); |
| 142 } |
| 143 |
| 144 void |
| 145 LogicalLoraChannelHelper::RemoveChannel (Ptr<LogicalLoraChannel> logicalChannel) |
| 146 { |
| 147 // Search and remove the channel from the list |
| 148 std::vector<Ptr<LogicalLoraChannel> >::iterator it; |
| 149 for (it = m_channelList.begin (); it != m_channelList.end (); it++) |
| 150 { |
| 151 Ptr<LogicalLoraChannel> currentChannel = *it; |
| 152 if (currentChannel == logicalChannel) |
| 153 { |
| 154 m_channelList.erase (it); |
| 155 return; |
| 156 } |
| 157 } |
| 158 } |
| 159 |
| 160 Time |
| 161 LogicalLoraChannelHelper::GetAggregatedWaitingTime (void) |
| 162 { |
| 163 // Aggregate waiting time |
| 164 Time aggregatedWaitingTime = m_nextAggregatedTransmissionTime - Simulator::Now
(); |
| 165 |
| 166 // Handle case in which waiting time is negative |
| 167 aggregatedWaitingTime = Seconds (std::max (aggregatedWaitingTime.GetSeconds ()
, |
| 168 double(0))); |
| 169 |
| 170 NS_LOG_DEBUG ("Aggregated waiting time: " << aggregatedWaitingTime.GetSeconds
()); |
| 171 |
| 172 return aggregatedWaitingTime; |
| 173 } |
| 174 |
| 175 Time |
| 176 LogicalLoraChannelHelper::GetWaitingTime (Ptr<LogicalLoraChannel> channel) |
| 177 { |
| 178 NS_LOG_FUNCTION (this << channel); |
| 179 |
| 180 // SubBand waiting time |
| 181 Time subBandWaitingTime = GetSubBandFromChannel (channel)-> |
| 182 GetNextTransmissionTime () - |
| 183 Simulator::Now (); |
| 184 |
| 185 // Handle case in which waiting time is negative |
| 186 subBandWaitingTime = Seconds (std::max (subBandWaitingTime.GetSeconds (), |
| 187 double(0))); |
| 188 |
| 189 NS_LOG_DEBUG ("Waiting time: " << subBandWaitingTime.GetSeconds ()); |
| 190 |
| 191 return subBandWaitingTime; |
| 192 } |
| 193 |
| 194 void |
| 195 LogicalLoraChannelHelper::AddEvent (Time duration, |
| 196 Ptr<LogicalLoraChannel> channel) |
| 197 { |
| 198 NS_LOG_FUNCTION (this << duration << channel); |
| 199 |
| 200 Ptr<SubBand> subBand = GetSubBandFromChannel (channel); |
| 201 |
| 202 double dutyCycle = subBand->GetDutyCycle (); |
| 203 double timeOnAir = duration.GetSeconds (); |
| 204 |
| 205 // Computation of necessary waiting time on this sub-band |
| 206 subBand->SetNextTransmissionTime (Simulator::Now () + Seconds |
| 207 (timeOnAir/dutyCycle - timeOnAir)); |
| 208 |
| 209 // Computation of necessary aggregate waiting time |
| 210 m_nextAggregatedTransmissionTime = Simulator::Now () + Seconds |
| 211 (timeOnAir/m_aggregatedDutyCycle - timeOnAir); |
| 212 |
| 213 NS_LOG_DEBUG ("Time on air: " << timeOnAir); |
| 214 NS_LOG_DEBUG ("m_aggregatedDutyCycle: " << m_aggregatedDutyCycle); |
| 215 NS_LOG_DEBUG ("Current time: " << Simulator::Now ().GetSeconds ()); |
| 216 NS_LOG_DEBUG ("Next transmission on this sub-band allowed at time: " << |
| 217 (subBand->GetNextTransmissionTime ()).GetSeconds ()); |
| 218 NS_LOG_DEBUG ("Next aggregated transmission allowed at time " << |
| 219 m_nextAggregatedTransmissionTime.GetSeconds ()); |
| 220 } |
| 221 |
| 222 double |
| 223 LogicalLoraChannelHelper::GetTxPowerForChannel (Ptr<LogicalLoraChannel> |
| 224 logicalChannel) |
| 225 { |
| 226 NS_LOG_FUNCTION_NOARGS (); |
| 227 |
| 228 // Get the maxTxPowerDbm from the SubBand this channel is in |
| 229 std::list< Ptr< SubBand > >::iterator it; |
| 230 for (it = m_subBandList.begin (); it != m_subBandList.end (); it++) |
| 231 { |
| 232 // Check whether this channel is in this SubBand |
| 233 if ((*it)->BelongsToSubBand (logicalChannel->GetFrequency ())) |
| 234 { |
| 235 return (*it)->GetMaxTxPowerDbm (); |
| 236 } |
| 237 } |
| 238 NS_ABORT_MSG ("Logical channel doesn't belong to a known SubBand"); |
| 239 |
| 240 return 0; |
| 241 } |
| 242 |
| 243 void |
| 244 LogicalLoraChannelHelper::DisableChannel (int index) |
| 245 { |
| 246 NS_LOG_FUNCTION (this << index); |
| 247 |
| 248 m_channelList.at (index)->DisableForUplink (); |
| 249 } |
| 250 } |
OLD | NEW |