A Discrete-Event Network Simulator
API
wifi-radio-energy-model.cc
Go to the documentation of this file.
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  * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/double.h"
23 #include "ns3/simulator.h"
24 #include "ns3/trace-source-accessor.h"
25 #include "ns3/pointer.h"
26 #include "ns3/energy-source.h"
28 #include "wifi-tx-current-model.h"
29 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE ("WifiRadioEnergyModel");
33 
34 NS_OBJECT_ENSURE_REGISTERED (WifiRadioEnergyModel);
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::WifiRadioEnergyModel")
41  .SetGroupName ("Energy")
42  .AddConstructor<WifiRadioEnergyModel> ()
43  .AddAttribute ("IdleCurrentA",
44  "The default radio Idle current in Ampere.",
45  DoubleValue (0.273), // idle mode = 273mA
48  MakeDoubleChecker<double> ())
49  .AddAttribute ("CcaBusyCurrentA",
50  "The default radio CCA Busy State current in Ampere.",
51  DoubleValue (0.273), // default to be the same as idle mode
54  MakeDoubleChecker<double> ())
55  .AddAttribute ("TxCurrentA",
56  "The radio Tx current in Ampere.",
57  DoubleValue (0.380), // transmit at 0dBm = 380mA
60  MakeDoubleChecker<double> ())
61  .AddAttribute ("RxCurrentA",
62  "The radio Rx current in Ampere.",
63  DoubleValue (0.313), // receive mode = 313mA
66  MakeDoubleChecker<double> ())
67  .AddAttribute ("SwitchingCurrentA",
68  "The default radio Channel Switch current in Ampere.",
69  DoubleValue (0.273), // default to be the same as idle mode
72  MakeDoubleChecker<double> ())
73  .AddAttribute ("SleepCurrentA",
74  "The radio Sleep current in Ampere.",
75  DoubleValue (0.033), // sleep mode = 33mA
78  MakeDoubleChecker<double> ())
79  .AddAttribute ("TxCurrentModel", "A pointer to the attached tx current model.",
80  PointerValue (),
82  MakePointerChecker<WifiTxCurrentModel> ())
83  .AddTraceSource ("TotalEnergyConsumption",
84  "Total energy consumption of the radio device.",
86  "ns3::TracedValueCallback::Double")
87  ;
88  return tid;
89 }
90 
92 {
93  NS_LOG_FUNCTION (this);
94  m_currentState = WifiPhy::IDLE; // initially IDLE
95  m_lastUpdateTime = Seconds (0.0);
99  m_source = NULL;
100  // set callback for WifiPhy listener
103  // set callback for updating the tx current
105 }
106 
108 {
109  NS_LOG_FUNCTION (this);
110  delete m_listener;
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION (this << source);
117  NS_ASSERT (source != NULL);
118  m_source = source;
119 }
120 
121 double
123 {
124  NS_LOG_FUNCTION (this);
126 }
127 
128 double
130 {
131  NS_LOG_FUNCTION (this);
132  return m_idleCurrentA;
133 }
134 
135 void
137 {
138  NS_LOG_FUNCTION (this << idleCurrentA);
139  m_idleCurrentA = idleCurrentA;
140 }
141 
142 double
144 {
145  NS_LOG_FUNCTION (this);
146  return m_ccaBusyCurrentA;
147 }
148 
149 void
151 {
152  NS_LOG_FUNCTION (this << CcaBusyCurrentA);
153  m_ccaBusyCurrentA = CcaBusyCurrentA;
154 }
155 
156 double
158 {
159  NS_LOG_FUNCTION (this);
160  return m_txCurrentA;
161 }
162 
163 void
165 {
166  NS_LOG_FUNCTION (this << txCurrentA);
167  m_txCurrentA = txCurrentA;
168 }
169 
170 double
172 {
173  NS_LOG_FUNCTION (this);
174  return m_rxCurrentA;
175 }
176 
177 void
179 {
180  NS_LOG_FUNCTION (this << rxCurrentA);
181  m_rxCurrentA = rxCurrentA;
182 }
183 
184 double
186 {
187  NS_LOG_FUNCTION (this);
188  return m_switchingCurrentA;
189 }
190 
191 void
193 {
194  NS_LOG_FUNCTION (this << switchingCurrentA);
195  m_switchingCurrentA = switchingCurrentA;
196 }
197 
198 double
200 {
201  NS_LOG_FUNCTION (this);
202  return m_sleepCurrentA;
203 }
204 
205 void
207 {
208  NS_LOG_FUNCTION (this << sleepCurrentA);
209  m_sleepCurrentA = sleepCurrentA;
210 }
211 
214 {
215  NS_LOG_FUNCTION (this);
216  return m_currentState;
217 }
218 
219 void
222 {
223  NS_LOG_FUNCTION (this);
224  if (callback.IsNull ())
225  {
226  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback!");
227  }
228  m_energyDepletionCallback = callback;
229 }
230 
231 void
234 {
235  NS_LOG_FUNCTION (this);
236  if (callback.IsNull ())
237  {
238  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy recharged callback!");
239  }
240  m_energyRechargedCallback = callback;
241 }
242 
243 void
245 {
246  m_txCurrentModel = model;
247 }
248 
249 void
251 {
252  if (m_txCurrentModel)
253  {
254  m_txCurrentA = m_txCurrentModel->CalcTxCurrent (txPowerDbm);
255  }
256 }
257 
258 void
260 {
261  NS_LOG_FUNCTION (this << newState);
262 
263  Time duration = Simulator::Now () - m_lastUpdateTime;
264  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
265 
266  // energy to decrease = current * voltage * time
267  double energyToDecrease = 0.0;
268  double supplyVoltage = m_source->GetSupplyVoltage ();
269  switch (m_currentState)
270  {
271  case WifiPhy::IDLE:
272  energyToDecrease = duration.GetSeconds () * m_idleCurrentA * supplyVoltage;
273  break;
274  case WifiPhy::CCA_BUSY:
275  energyToDecrease = duration.GetSeconds () * m_ccaBusyCurrentA * supplyVoltage;
276  break;
277  case WifiPhy::TX:
278  energyToDecrease = duration.GetSeconds () * m_txCurrentA * supplyVoltage;
279  break;
280  case WifiPhy::RX:
281  energyToDecrease = duration.GetSeconds () * m_rxCurrentA * supplyVoltage;
282  break;
283  case WifiPhy::SWITCHING:
284  energyToDecrease = duration.GetSeconds () * m_switchingCurrentA * supplyVoltage;
285  break;
286  case WifiPhy::SLEEP:
287  energyToDecrease = duration.GetSeconds () * m_sleepCurrentA * supplyVoltage;
288  break;
289  default:
290  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state: " << m_currentState);
291  }
292 
293  // update total energy consumption
294  m_totalEnergyConsumption += energyToDecrease;
295 
296  // update last update time stamp
298 
300 
301  // notify energy source
302  m_source->UpdateEnergySource ();
303 
304  // in case the energy source is found to be depleted during the last update, a callback might be
305  // invoked that might cause a change in the Wifi PHY state (e.g., the PHY is put into SLEEP mode).
306  // This in turn causes a new call to this member function, with the consequence that the previous
307  // instance is resumed after the termination of the new instance. In particular, the state set
308  // by the previous instance is erroneously the final state stored in m_currentState. The check below
309  // ensures that previous instances do not change m_currentState.
310 
312  {
313  // update current state & last update time stamp
314  SetWifiRadioState ((WifiPhy::State) newState);
315 
316  // some debug message
317  NS_LOG_DEBUG ("WifiRadioEnergyModel:Total energy consumption is " <<
318  m_totalEnergyConsumption << "J");
319  }
320 
322 
324 }
325 
326 void
328 {
329  NS_LOG_FUNCTION (this);
330  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is depleted!");
331  // invoke energy depletion callback, if set.
333  {
335  }
336 }
337 
338 void
340 {
341  NS_LOG_FUNCTION (this);
342  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is recharged!");
343  // invoke energy recharged callback, if set.
345  {
347  }
348 }
349 
352 {
353  NS_LOG_FUNCTION (this);
354  return m_listener;
355 }
356 
357 /*
358  * Private functions start here.
359  */
360 
361 void
363 {
364  NS_LOG_FUNCTION (this);
365  m_source = NULL;
367 }
368 
369 double
371 {
372  NS_LOG_FUNCTION (this);
373  switch (m_currentState)
374  {
375  case WifiPhy::IDLE:
376  return m_idleCurrentA;
377  case WifiPhy::CCA_BUSY:
378  return m_ccaBusyCurrentA;
379  case WifiPhy::TX:
380  return m_txCurrentA;
381  case WifiPhy::RX:
382  return m_rxCurrentA;
383  case WifiPhy::SWITCHING:
384  return m_switchingCurrentA;
385  case WifiPhy::SLEEP:
386  return m_sleepCurrentA;
387  default:
388  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state:" << m_currentState);
389  }
390 }
391 
392 void
394 {
395  NS_LOG_FUNCTION (this << state);
396  m_currentState = state;
397  std::string stateName;
398  switch (state)
399  {
400  case WifiPhy::IDLE:
401  stateName = "IDLE";
402  break;
403  case WifiPhy::CCA_BUSY:
404  stateName = "CCA_BUSY";
405  break;
406  case WifiPhy::TX:
407  stateName = "TX";
408  break;
409  case WifiPhy::RX:
410  stateName = "RX";
411  break;
412  case WifiPhy::SWITCHING:
413  stateName = "SWITCHING";
414  break;
415  case WifiPhy::SLEEP:
416  stateName = "SLEEP";
417  break;
418  }
419  NS_LOG_DEBUG ("WifiRadioEnergyModel:Switching to state: " << stateName <<
420  " at time = " << Simulator::Now ());
421 }
422 
423 // -------------------------------------------------------------------------- //
424 
426 {
427  NS_LOG_FUNCTION (this);
430 }
431 
433 {
434  NS_LOG_FUNCTION (this);
435 }
436 
437 void
439 {
440  NS_LOG_FUNCTION (this << &callback);
441  NS_ASSERT (!callback.IsNull ());
442  m_changeStateCallback = callback;
443 }
444 
445 void
447 {
448  NS_LOG_FUNCTION (this << &callback);
449  NS_ASSERT (!callback.IsNull ());
450  m_updateTxCurrentCallback = callback;
451 }
452 
453 void
455 {
456  NS_LOG_FUNCTION (this << duration);
458  {
459  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
460  }
463 }
464 
465 void
467 {
468  NS_LOG_FUNCTION (this);
470  {
471  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
472  }
474 }
475 
476 void
478 {
479  NS_LOG_FUNCTION (this);
481  {
482  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
483  }
485 }
486 
487 void
489 {
490  NS_LOG_FUNCTION (this << duration << txPowerDbm);
492  {
493  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Update tx current callback not set!");
494  }
495  m_updateTxCurrentCallback (txPowerDbm);
497  {
498  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
499  }
501  // schedule changing state back to IDLE after TX duration
504 }
505 
506 void
508 {
509  NS_LOG_FUNCTION (this << duration);
511  {
512  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
513  }
515  // schedule changing state back to IDLE after CCA_BUSY duration
518 }
519 
520 void
522 {
523  NS_LOG_FUNCTION (this << duration);
525  {
526  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
527  }
529  // schedule changing state back to IDLE after CCA_BUSY duration
532 }
533 
534 void
536 {
537  NS_LOG_FUNCTION (this);
539  {
540  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
541  }
544 }
545 
546 void
548 {
549  NS_LOG_FUNCTION (this);
551  {
552  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
553  }
555 }
556 
557 /*
558  * Private function state here.
559  */
560 
561 void
563 {
564  NS_LOG_FUNCTION (this);
566  {
567  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
568  }
570 }
571 
572 } // namespace ns3
void SetWifiRadioState(const WifiPhy::State state)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void HandleEnergyRecharged(void)
Handles energy recharged.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
TracedValue< double > m_totalEnergyConsumption
Base class for device energy models.
The PHY layer has sense the medium busy through the CCA mechanism.
Definition: wifi-phy.h:177
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
UpdateTxCurrentCallback m_updateTxCurrentCallback
Callback used to update the tx current stored in WifiRadioEnergyModel based on the nominal tx power u...
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void ChangeState(int newState)
Changes state of the WifiRadioEnergyMode.
The PHY layer is sleeping.
Definition: wifi-phy.h:193
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
WifiRadioEnergyModelPhyListener * m_listener
void SetSleepCurrentA(double sleepCurrentA)
virtual double GetTotalEnergyConsumption(void) const
WifiRadioEnergyModelPhyListener * GetPhyListener(void)
void SetUpdateTxCurrentCallback(UpdateTxCurrentCallback callback)
Sets the update tx current callback.
void SwitchToIdle(void)
A helper function that makes scheduling m_changeStateCallback possible.
void SetRxCurrentA(double rxCurrentA)
virtual void NotifyRxEndOk(void)
Switches the WifiRadioEnergyModel back to IDLE state.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
void SetCcaBusyCurrentA(double ccaBusyCurrentA)
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
double GetCcaBusyCurrentA(void) const
void SetChangeStateCallback(DeviceEnergyModel::ChangeStateCallback callback)
Sets the change state callback.
Ptr< WifiTxCurrentModel > m_txCurrentModel
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: pointer.h:220
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
virtual void NotifySwitchingStart(Time duration)
virtual void NotifyMaybeCcaBusyStart(Time duration)
The PHY layer is IDLE.
Definition: wifi-phy.h:173
void SetTxCurrentFromModel(double txPowerDbm)
Calls the CalcTxCurrent method of the tx current model to compute the tx current based on such model...
void SetEnergyRechargedCallback(WifiRadioEnergyRechargedCallback callback)
virtual void NotifyRxEndError(void)
Switches the WifiRadioEnergyModel back to IDLE state.
The PHY layer is receiving a packet.
Definition: wifi-phy.h:185
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
WifiPhy::State GetCurrentState(void) const
DeviceEnergyModel::ChangeStateCallback m_changeStateCallback
Change state callback used to notify the WifiRadioEnergyModel of a state change.
The PHY layer is sending a packet.
Definition: wifi-phy.h:181
The PHY layer is switching to other channel.
Definition: wifi-phy.h:189
virtual void NotifyTxStart(Time duration, double txPowerDbm)
Switches the WifiRadioEnergyModel to TX state and switches back to IDLE after TX duration.
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
virtual void NotifyWakeup(void)
Defined in ns3::WifiPhyListener.
A WifiPhy listener class for notifying the WifiRadioEnergyModel of Wifi radio state change...
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
void SetTxCurrentA(double txCurrentA)
virtual void NotifySleep(void)
Defined in ns3::WifiPhyListener.
A WiFi radio energy model.
virtual void NotifyRxStart(Time duration)
Switches the WifiRadioEnergyModel to RX state.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: double.h:42
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:353
void SetEnergyDepletionCallback(WifiRadioEnergyDepletionCallback callback)
void DoDispose(void)
Destructor implementation.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
WifiRadioEnergyDepletionCallback m_energyDepletionCallback
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1274
virtual void ChangeState(int newState)=0
void SetSwitchingCurrentA(double switchingCurrentA)
virtual double DoGetCurrentA(void) const
State
The state of the PHY layer.
Definition: wifi-phy.h:168
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
void SetIdleCurrentA(double idleCurrentA)
WifiRadioEnergyRechargedCallback m_energyRechargedCallback
double GetSwitchingCurrentA(void) const
virtual void HandleEnergyDepletion(void)
Handles energy depletion.
void SetTxCurrentModel(Ptr< WifiTxCurrentModel > model)