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 "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  .AddConstructor<WifiRadioEnergyModel> ()
42  .AddAttribute ("IdleCurrentA",
43  "The default radio Idle current in Ampere.",
44  DoubleValue (0.273), // idle mode = 273mA
47  MakeDoubleChecker<double> ())
48  .AddAttribute ("CcaBusyCurrentA",
49  "The default radio CCA Busy State current in Ampere.",
50  DoubleValue (0.273), // default to be the same as idle mode
53  MakeDoubleChecker<double> ())
54  .AddAttribute ("TxCurrentA",
55  "The radio Tx current in Ampere.",
56  DoubleValue (0.380), // transmit at 0dBm = 380mA
59  MakeDoubleChecker<double> ())
60  .AddAttribute ("RxCurrentA",
61  "The radio Rx current in Ampere.",
62  DoubleValue (0.313), // receive mode = 313mA
65  MakeDoubleChecker<double> ())
66  .AddAttribute ("SwitchingCurrentA",
67  "The default radio Channel Switch current in Ampere.",
68  DoubleValue (0.273), // default to be the same as idle mode
71  MakeDoubleChecker<double> ())
72  .AddAttribute ("SleepCurrentA",
73  "The radio Sleep current in Ampere.",
74  DoubleValue (0.033), // sleep mode = 33mA
77  MakeDoubleChecker<double> ())
78  .AddAttribute ("TxCurrentModel", "A pointer to the attached tx current model.",
79  PointerValue (),
81  MakePointerChecker<WifiTxCurrentModel> ())
82  .AddTraceSource ("TotalEnergyConsumption",
83  "Total energy consumption of the radio device.",
85  "ns3::TracedValue::DoubleCallback")
86  ;
87  return tid;
88 }
89 
91 {
92  NS_LOG_FUNCTION (this);
93  m_currentState = WifiPhy::IDLE; // initially IDLE
94  m_lastUpdateTime = Seconds (0.0);
98  m_source = NULL;
99  // set callback for WifiPhy listener
102  // set callback for updating the tx current
104 }
105 
107 {
108  NS_LOG_FUNCTION (this);
109  delete m_listener;
110 }
111 
112 void
114 {
115  NS_LOG_FUNCTION (this << source);
116  NS_ASSERT (source != NULL);
117  m_source = source;
118 }
119 
120 double
122 {
123  NS_LOG_FUNCTION (this);
125 }
126 
127 double
129 {
130  NS_LOG_FUNCTION (this);
131  return m_idleCurrentA;
132 }
133 
134 void
136 {
137  NS_LOG_FUNCTION (this << idleCurrentA);
138  m_idleCurrentA = idleCurrentA;
139 }
140 
141 double
143 {
144  NS_LOG_FUNCTION (this);
145  return m_ccaBusyCurrentA;
146 }
147 
148 void
150 {
151  NS_LOG_FUNCTION (this << CcaBusyCurrentA);
152  m_ccaBusyCurrentA = CcaBusyCurrentA;
153 }
154 
155 double
157 {
158  NS_LOG_FUNCTION (this);
159  return m_txCurrentA;
160 }
161 
162 void
164 {
165  NS_LOG_FUNCTION (this << txCurrentA);
166  m_txCurrentA = txCurrentA;
167 }
168 
169 double
171 {
172  NS_LOG_FUNCTION (this);
173  return m_rxCurrentA;
174 }
175 
176 void
178 {
179  NS_LOG_FUNCTION (this << rxCurrentA);
180  m_rxCurrentA = rxCurrentA;
181 }
182 
183 double
185 {
186  NS_LOG_FUNCTION (this);
187  return m_switchingCurrentA;
188 }
189 
190 void
192 {
193  NS_LOG_FUNCTION (this << switchingCurrentA);
194  m_switchingCurrentA = switchingCurrentA;
195 }
196 
197 double
199 {
200  NS_LOG_FUNCTION (this);
201  return m_sleepCurrentA;
202 }
203 
204 void
206 {
207  NS_LOG_FUNCTION (this << sleepCurrentA);
208  m_sleepCurrentA = sleepCurrentA;
209 }
210 
213 {
214  NS_LOG_FUNCTION (this);
215  return m_currentState;
216 }
217 
218 void
221 {
222  NS_LOG_FUNCTION (this);
223  if (callback.IsNull ())
224  {
225  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback!");
226  }
227  m_energyDepletionCallback = callback;
228 }
229 
230 void
233 {
234  NS_LOG_FUNCTION (this);
235  if (callback.IsNull ())
236  {
237  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy recharged callback!");
238  }
239  m_energyRechargedCallback = callback;
240 }
241 
242 void
244 {
245  m_txCurrentModel = model;
246 }
247 
248 void
250 {
251  if (m_txCurrentModel)
252  {
253  m_txCurrentA = m_txCurrentModel->CalcTxCurrent (txPowerDbm);
254  }
255 }
256 
257 void
259 {
260  NS_LOG_FUNCTION (this << newState);
261 
262  Time duration = Simulator::Now () - m_lastUpdateTime;
263  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
264 
265  // energy to decrease = current * voltage * time
266  double energyToDecrease = 0.0;
267  double supplyVoltage = m_source->GetSupplyVoltage ();
268  switch (m_currentState)
269  {
270  case WifiPhy::IDLE:
271  energyToDecrease = duration.GetSeconds () * m_idleCurrentA * supplyVoltage;
272  break;
273  case WifiPhy::CCA_BUSY:
274  energyToDecrease = duration.GetSeconds () * m_ccaBusyCurrentA * supplyVoltage;
275  break;
276  case WifiPhy::TX:
277  energyToDecrease = duration.GetSeconds () * m_txCurrentA * supplyVoltage;
278  break;
279  case WifiPhy::RX:
280  energyToDecrease = duration.GetSeconds () * m_rxCurrentA * supplyVoltage;
281  break;
282  case WifiPhy::SWITCHING:
283  energyToDecrease = duration.GetSeconds () * m_switchingCurrentA * supplyVoltage;
284  break;
285  case WifiPhy::SLEEP:
286  energyToDecrease = duration.GetSeconds () * m_sleepCurrentA * supplyVoltage;
287  break;
288  default:
289  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state: " << m_currentState);
290  }
291 
292  // update total energy consumption
293  m_totalEnergyConsumption += energyToDecrease;
294 
295  // update last update time stamp
297 
299 
300  // notify energy source
301  m_source->UpdateEnergySource ();
302 
303  // in case the energy source is found to be depleted during the last update, a callback might be
304  // invoked that might cause a change in the Wifi PHY state (e.g., the PHY is put into SLEEP mode).
305  // This in turn causes a new call to this member function, with the consequence that the previous
306  // instance is resumed after the termination of the new instance. In particular, the state set
307  // by the previous instance is erroneously the final state stored in m_currentState. The check below
308  // ensures that previous instances do not change m_currentState.
309 
311  {
312  // update current state & last update time stamp
313  SetWifiRadioState ((WifiPhy::State) newState);
314 
315  // some debug message
316  NS_LOG_DEBUG ("WifiRadioEnergyModel:Total energy consumption is " <<
317  m_totalEnergyConsumption << "J");
318  }
319 
321 
323 }
324 
325 void
327 {
328  NS_LOG_FUNCTION (this);
329  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is depleted!");
330  // invoke energy depletion callback, if set.
332  {
334  }
335 }
336 
337 void
339 {
340  NS_LOG_FUNCTION (this);
341  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is recharged!");
342  // invoke energy recharged callback, if set.
344  {
346  }
347 }
348 
351 {
352  NS_LOG_FUNCTION (this);
353  return m_listener;
354 }
355 
356 /*
357  * Private functions start here.
358  */
359 
360 void
362 {
363  NS_LOG_FUNCTION (this);
364  m_source = NULL;
366 }
367 
368 double
370 {
371  NS_LOG_FUNCTION (this);
372  switch (m_currentState)
373  {
374  case WifiPhy::IDLE:
375  return m_idleCurrentA;
376  case WifiPhy::CCA_BUSY:
377  return m_ccaBusyCurrentA;
378  case WifiPhy::TX:
379  return m_txCurrentA;
380  case WifiPhy::RX:
381  return m_rxCurrentA;
382  case WifiPhy::SWITCHING:
383  return m_switchingCurrentA;
384  case WifiPhy::SLEEP:
385  return m_sleepCurrentA;
386  default:
387  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state:" << m_currentState);
388  }
389 }
390 
391 void
393 {
394  NS_LOG_FUNCTION (this << state);
395  m_currentState = state;
396  std::string stateName;
397  switch (state)
398  {
399  case WifiPhy::IDLE:
400  stateName = "IDLE";
401  break;
402  case WifiPhy::CCA_BUSY:
403  stateName = "CCA_BUSY";
404  break;
405  case WifiPhy::TX:
406  stateName = "TX";
407  break;
408  case WifiPhy::RX:
409  stateName = "RX";
410  break;
411  case WifiPhy::SWITCHING:
412  stateName = "SWITCHING";
413  break;
414  case WifiPhy::SLEEP:
415  stateName = "SLEEP";
416  break;
417  }
418  NS_LOG_DEBUG ("WifiRadioEnergyModel:Switching to state: " << stateName <<
419  " at time = " << Simulator::Now ());
420 }
421 
422 // -------------------------------------------------------------------------- //
423 
425 {
426  NS_LOG_FUNCTION (this);
429 }
430 
432 {
433  NS_LOG_FUNCTION (this);
434 }
435 
436 void
438 {
439  NS_LOG_FUNCTION (this << &callback);
440  NS_ASSERT (!callback.IsNull ());
441  m_changeStateCallback = callback;
442 }
443 
444 void
446 {
447  NS_LOG_FUNCTION (this << &callback);
448  NS_ASSERT (!callback.IsNull ());
449  m_updateTxCurrentCallback = callback;
450 }
451 
452 void
454 {
455  NS_LOG_FUNCTION (this << duration);
457  {
458  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
459  }
462 }
463 
464 void
466 {
467  NS_LOG_FUNCTION (this);
469  {
470  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
471  }
473 }
474 
475 void
477 {
478  NS_LOG_FUNCTION (this);
480  {
481  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
482  }
484 }
485 
486 void
488 {
489  NS_LOG_FUNCTION (this << duration << txPowerDbm);
491  {
492  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Update tx current callback not set!");
493  }
494  m_updateTxCurrentCallback (txPowerDbm);
496  {
497  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
498  }
500  // schedule changing state back to IDLE after TX duration
503 }
504 
505 void
507 {
508  NS_LOG_FUNCTION (this << duration);
510  {
511  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
512  }
514  // schedule changing state back to IDLE after CCA_BUSY duration
517 }
518 
519 void
521 {
522  NS_LOG_FUNCTION (this << duration);
524  {
525  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
526  }
528  // schedule changing state back to IDLE after CCA_BUSY duration
531 }
532 
533 void
535 {
536  NS_LOG_FUNCTION (this);
538  {
539  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
540  }
543 }
544 
545 void
547 {
548  NS_LOG_FUNCTION (this);
550  {
551  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
552  }
554 }
555 
556 /*
557  * Private function state here.
558  */
559 
560 void
562 {
563  NS_LOG_FUNCTION (this);
565  {
566  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
567  }
569 }
570 
571 } // namespace ns3
void SetWifiRadioState(const WifiPhy::State state)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:95
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:141
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1072
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:61
#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:157
#define NS_FATAL_ERROR(msg)
Fatal error handling.
Definition: fatal-error.h:100
WifiRadioEnergyModelPhyListener * m_listener
void SetSleepCurrentA(double sleepCurrentA)
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
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:327
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:210
virtual void NotifySwitchingStart(Time duration)
virtual void NotifyMaybeCcaBusyStart(Time duration)
The PHY layer is IDLE.
Definition: wifi-phy.h:137
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:149
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1290
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:145
The PHY layer is switching to other channel.
Definition: wifi-phy.h:153
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:223
void SetTxCurrentA(double txCurrentA)
virtual void NotifySleep(void)
Defined in ns3::WifiPhyListener.
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:339
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:859
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:1076
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:132
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:51
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
void SetIdleCurrentA(double idleCurrentA)
WifiRadioEnergyRechargedCallback m_energyRechargedCallback
double GetSwitchingCurrentA(void) const
virtual void HandleEnergyDepletion(void)
Handles energy depletion.
void SetTxCurrentModel(Ptr< WifiTxCurrentModel > model)