LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
| 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 * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> |
| 19 */ |
| 20 · |
| 21 #include "constant-jammer.h" |
| 22 #include "ns3/simulator.h" |
| 23 #include "ns3/assert.h" |
| 24 #include "ns3/log.h" |
| 25 #include "ns3/double.h" |
| 26 #include "ns3/uinteger.h" |
| 27 |
| 28 NS_LOG_COMPONENT_DEFINE ("ConstantJammer"); |
| 29 |
| 30 /* |
| 31 * Constant Jammer |
| 32 */ |
| 33 namespace ns3 { |
| 34 ········ |
| 35 NS_OBJECT_ENSURE_REGISTERED (ConstantJammer); |
| 36 ········ |
| 37 TypeId |
| 38 ConstantJammer::GetTypeId (void) |
| 39 { |
| 40 |
| 41 static TypeId tid = TypeId ("ns3::ConstantJammer") |
| 42 .SetParent<Jammer> () |
| 43 .AddConstructor<ConstantJammer> () |
| 44 .AddAttribute ("ConstantJammerTxPower", |
| 45 "Power to send jamming signal for constant jammer, in Watts."
, |
| 46 DoubleValue (0.001), // 0dBm |
| 47 MakeDoubleAccessor (&ConstantJammer::SetTxPower, |
| 48 &ConstantJammer::GetTxPower), |
| 49 MakeDoubleChecker<double> ()) |
| 50 .AddAttribute ("ConstantJammerJammingDuration", |
| 51 "Jamming duration for constant jammer.", |
| 52 TimeValue (MilliSeconds (5.0)), |
| 53 MakeTimeAccessor (&ConstantJammer::SetJammingDuration, |
| 54 &ConstantJammer::GetJammingDuration), |
| 55 MakeTimeChecker ()) |
| 56 .AddAttribute ("ConstantJammerConstantInterval", |
| 57 "Constant jammer jamming interval.", |
| 58 TimeValue (MilliSeconds (0.0)), // Set to 0 for continuous j
amming |
| 59 MakeTimeAccessor (&ConstantJammer::SetConstantJammingInterval
, |
| 60 &ConstantJammer::GetConstantJammingInterval
), |
| 61 MakeTimeChecker ()) |
| 62 .AddAttribute ("ConstantJammerRxTimeout", |
| 63 "Constant jammer RX timeout.", |
| 64 TimeValue (Seconds (2.0)), |
| 65 MakeTimeAccessor (&ConstantJammer::SetRxTimeout, |
| 66 &ConstantJammer::GetRxTimeout), |
| 67 MakeTimeChecker ()) |
| 68 .AddAttribute ("ConstantJammerReactToMitigationFlag", |
| 69 "Constant jammer react to mitigation flag, set to enable chas
ing.", |
| 70 UintegerValue (false), // default to no chasing |
| 71 MakeUintegerAccessor (&ConstantJammer::SetReactToMitigation, |
| 72 &ConstantJammer::GetReactToMitigation), |
| 73 MakeUintegerChecker<bool> ()) |
| 74 ; |
| 75 return tid; |
| 76 } |
| 77 |
| 78 ConstantJammer::ConstantJammer () |
| 79 : m_reactToMitigation (false), |
| 80 m_reacting (false) |
| 81 { |
| 82 } |
| 83 |
| 84 ConstantJammer::~ConstantJammer () |
| 85 { |
| 86 } |
| 87 |
| 88 void |
| 89 ConstantJammer::SetUtility (Ptr<WirelessModuleUtility> utility) |
| 90 { |
| 91 NS_LOG_FUNCTION (this << utility); |
| 92 NS_ASSERT (utility != NULL); |
| 93 m_utility = utility; |
| 94 } |
| 95 |
| 96 void |
| 97 ConstantJammer::SetEnergySource (Ptr<EnergySource> source) |
| 98 { |
| 99 NS_LOG_FUNCTION (this << source); |
| 100 NS_ASSERT (source != NULL); |
| 101 m_source = source; |
| 102 } |
| 103 |
| 104 void |
| 105 ConstantJammer::SetTxPower (double power) |
| 106 { |
| 107 NS_LOG_FUNCTION (this << power); |
| 108 m_txPower = power; |
| 109 } |
| 110 |
| 111 double |
| 112 ConstantJammer::GetTxPower (void) const |
| 113 { |
| 114 NS_LOG_FUNCTION (this); |
| 115 return m_txPower; |
| 116 } |
| 117 |
| 118 void |
| 119 ConstantJammer::SetJammingDuration (Time duration) |
| 120 { |
| 121 NS_LOG_FUNCTION (this << duration); |
| 122 m_jammingDuration = duration; |
| 123 } |
| 124 |
| 125 Time |
| 126 ConstantJammer::GetJammingDuration (void) const |
| 127 { |
| 128 NS_LOG_FUNCTION (this); |
| 129 return m_jammingDuration; |
| 130 } |
| 131 |
| 132 void |
| 133 ConstantJammer::SetConstantJammingInterval (Time interval) |
| 134 { |
| 135 NS_LOG_FUNCTION (this << interval); |
| 136 NS_ASSERT (interval.GetSeconds () >= 0); |
| 137 m_constantJammingInterval = interval; |
| 138 } |
| 139 |
| 140 Time |
| 141 ConstantJammer::GetConstantJammingInterval (void) const |
| 142 { |
| 143 NS_LOG_FUNCTION (this); |
| 144 return m_constantJammingInterval; |
| 145 } |
| 146 |
| 147 void |
| 148 ConstantJammer::SetRxTimeout (Time rxTimeout) |
| 149 { |
| 150 NS_LOG_FUNCTION (this << rxTimeout); |
| 151 m_rxTimeout = rxTimeout; |
| 152 } |
| 153 |
| 154 Time |
| 155 ConstantJammer::GetRxTimeout (void) const |
| 156 { |
| 157 NS_LOG_FUNCTION (this); |
| 158 return m_rxTimeout; |
| 159 } |
| 160 |
| 161 void |
| 162 ConstantJammer::SetReactToMitigation (const bool flag) |
| 163 { |
| 164 NS_LOG_FUNCTION (this << flag); |
| 165 m_reactToMitigation = flag; |
| 166 } |
| 167 |
| 168 bool |
| 169 ConstantJammer::GetReactToMitigation (void) const |
| 170 { |
| 171 NS_LOG_FUNCTION (this); |
| 172 return m_reactToMitigation; |
| 173 } |
| 174 |
| 175 /* |
| 176 * Private functions start here. |
| 177 */ |
| 178 |
| 179 void |
| 180 ConstantJammer::DoDispose (void) |
| 181 { |
| 182 NS_LOG_FUNCTION (this); |
| 183 m_jammingEvent.Cancel (); |
| 184 } |
| 185 |
| 186 void |
| 187 ConstantJammer::DoStopJammer (void) |
| 188 { |
| 189 NS_LOG_FUNCTION (this); |
| 190 m_jammingEvent.Cancel (); |
| 191 } |
| 192 |
| 193 void |
| 194 ConstantJammer::DoJamming (void) |
| 195 { |
| 196 NS_LOG_FUNCTION (this); |
| 197 NS_ASSERT (m_utility != NULL); |
| 198 |
| 199 if (!IsJammerOn ()) // check if jammer is on |
| 200 { |
| 201 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << ", Jammer is OFF!"
); |
| 202 return; |
| 203 } |
| 204 |
| 205 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 206 ", Sending jamming signal with TX power = " << m_txPower << |
| 207 " W" << ", At " << Simulator::Now ().GetSeconds () << "s"); |
| 208 |
| 209 // send jamming signal |
| 210 double actualPower = m_utility->SendJammingSignal (m_txPower, m_jammingDuratio
n); |
| 211 if (actualPower != 0.0) |
| 212 { |
| 213 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 214 ", Jamming signal sent with power = " << actualPower << " W"
); |
| 215 } |
| 216 else |
| 217 { |
| 218 NS_LOG_ERROR ("ConstantJammer:At Node #" << GetId () << |
| 219 ", Failed to send jamming signal!"); |
| 220 } |
| 221 |
| 222 /* |
| 223 * Schedule *first* RX timeout if react to mitigation is enabled. We know if |
| 224 * react to jamming mitigation is enabled, there should always be a RX timeout |
| 225 * event scheduled. The RX timeout event can only "expire" if it's never been |
| 226 * scheduled. |
| 227 */ |
| 228 if (m_reactToMitigation && m_rxTimeoutEvent.IsExpired()) |
| 229 { |
| 230 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 231 ", After jammer starts, scheduling RX timeout!"); |
| 232 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 233 &ConstantJammer::RxTimeoutHandler, |
| 234 this); |
| 235 } |
| 236 |
| 237 m_reacting = false; // always reset reacting flag |
| 238 } |
| 239 |
| 240 bool |
| 241 ConstantJammer::DoStartRxHandler (Ptr<Packet> packet, double startRss) |
| 242 { |
| 243 NS_LOG_FUNCTION (this << packet << startRss); |
| 244 |
| 245 if (m_reactToMitigation) // check if react to mitigation is enabled |
| 246 { |
| 247 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 248 ", React to mitigation enabled! Rescheduling RX Timeout"); |
| 249 // cancel RX timeout event |
| 250 m_rxTimeoutEvent.Cancel (); |
| 251 // reschedule next RX timeout |
| 252 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 253 &ConstantJammer::RxTimeoutHandler, |
| 254 this); |
| 255 } |
| 256 else |
| 257 { |
| 258 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 259 ", React to mitigation disabled!"); |
| 260 } |
| 261 return false; |
| 262 } |
| 263 |
| 264 bool |
| 265 ConstantJammer::DoEndRxHandler (Ptr<Packet> packet, double averageRss) |
| 266 { |
| 267 NS_LOG_FUNCTION (this << packet); |
| 268 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 269 ", Ignoring incoming packet!"); |
| 270 return false; |
| 271 } |
| 272 |
| 273 void |
| 274 ConstantJammer::DoEndTxHandler (Ptr<Packet> packet, double txPower) |
| 275 { |
| 276 NS_LOG_FUNCTION (this << packet << txPower); |
| 277 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 278 ". Sent jamming burst with power = " << txPower); |
| 279 |
| 280 m_jammingEvent.Cancel (); // cancel previously scheduled event |
| 281 |
| 282 // check to see if we are waiting the jammer to finish reacting to mitigation |
| 283 if (m_reacting) |
| 284 { |
| 285 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 286 ", Not sending jamming signal, jammer reacting to mitigation
!"); |
| 287 // get channel switch delay |
| 288 Time delay = m_utility->GetPhyLayerInfo().channelSwitchDelay; |
| 289 // schedule next jamming burst after channel switch delay |
| 290 m_jammingEvent = Simulator::Schedule (delay, |
| 291 &ConstantJammer::DoJamming, |
| 292 this); |
| 293 return; |
| 294 } |
| 295 |
| 296 // schedule next jamming burst after jamming interval. |
| 297 m_jammingEvent = Simulator::Schedule (m_constantJammingInterval, |
| 298 &ConstantJammer::DoJamming, this); |
| 299 } |
| 300 |
| 301 void |
| 302 ConstantJammer::RxTimeoutHandler (void) |
| 303 { |
| 304 NS_LOG_FUNCTION (this); |
| 305 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << ", RX timeout at " << |
| 306 Simulator::Now ().GetSeconds () << "s"); |
| 307 NS_ASSERT (m_utility != NULL); |
| 308 |
| 309 if (!m_reactToMitigation) // check react to mitigation flag |
| 310 { |
| 311 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 312 ", React to mitigation is turned OFF!"); |
| 313 return; |
| 314 } |
| 315 |
| 316 // Calculate channel number. |
| 317 uint16_t currentChannel = m_utility->GetPhyLayerInfo ().currentChannel; |
| 318 uint16_t nextChannel = currentChannel + 1; |
| 319 if (nextChannel >= m_utility->GetPhyLayerInfo ().numOfChannels) |
| 320 { |
| 321 nextChannel = 1; // wrap around and start form 1 |
| 322 } |
| 323 |
| 324 NS_LOG_DEBUG ("ConstantJammer:At Node #" << GetId () << |
| 325 ", Switching from channel " << currentChannel << " >-> " << |
| 326 nextChannel << ", At " << Simulator::Now ().GetSeconds () << "s"
); |
| 327 |
| 328 // hop to next channel |
| 329 m_utility->SwitchChannel (nextChannel); |
| 330 |
| 331 /* |
| 332 * Set reacting flag to indicate jammer is reacting. When the flag is set, no |
| 333 * scheduling of new jamming event is allowed. |
| 334 */ |
| 335 m_reacting = true; |
| 336 |
| 337 // cancel previously scheduled RX timeout |
| 338 m_rxTimeoutEvent.Cancel (); |
| 339 |
| 340 // schedule next RX timeout |
| 341 m_rxTimeoutEvent = Simulator::Schedule (m_rxTimeout, |
| 342 &ConstantJammer::RxTimeoutHandler, |
| 343 this); |
| 344 } |
| 345 |
| 346 } // namespace ns3 |
| 347 ·· |
LEFT | RIGHT |