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/simulator.h"
23 #include "ns3/pointer.h"
24 #include "ns3/energy-source.h"
26 #include "wifi-tx-current-model.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("WifiRadioEnergyModel");
31 
32 NS_OBJECT_ENSURE_REGISTERED (WifiRadioEnergyModel);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::WifiRadioEnergyModel")
39  .SetGroupName ("Energy")
40  .AddConstructor<WifiRadioEnergyModel> ()
41  .AddAttribute ("IdleCurrentA",
42  "The default radio Idle current in Ampere.",
43  DoubleValue (0.273), // idle mode = 273mA
46  MakeDoubleChecker<double> ())
47  .AddAttribute ("CcaBusyCurrentA",
48  "The default radio CCA Busy State current in Ampere.",
49  DoubleValue (0.273), // default to be the same as idle mode
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("TxCurrentA",
54  "The radio Tx current in Ampere.",
55  DoubleValue (0.380), // transmit at 0dBm = 380mA
58  MakeDoubleChecker<double> ())
59  .AddAttribute ("RxCurrentA",
60  "The radio Rx current in Ampere.",
61  DoubleValue (0.313), // receive mode = 313mA
64  MakeDoubleChecker<double> ())
65  .AddAttribute ("SwitchingCurrentA",
66  "The default radio Channel Switch current in Ampere.",
67  DoubleValue (0.273), // default to be the same as idle mode
70  MakeDoubleChecker<double> ())
71  .AddAttribute ("SleepCurrentA",
72  "The radio Sleep current in Ampere.",
73  DoubleValue (0.033), // sleep mode = 33mA
76  MakeDoubleChecker<double> ())
77  .AddAttribute ("TxCurrentModel", "A pointer to the attached tx current model.",
78  PointerValue (),
80  MakePointerChecker<WifiTxCurrentModel> ())
81  .AddTraceSource ("TotalEnergyConsumption",
82  "Total energy consumption of the radio device.",
84  "ns3::TracedValueCallback::Double")
85  ;
86  return tid;
87 }
88 
90 {
91  NS_LOG_FUNCTION (this);
92  m_currentState = WifiPhy::IDLE; // initially IDLE
93  m_lastUpdateTime = Seconds (0.0);
97  m_source = NULL;
98  // set callback for WifiPhy listener
101  // set callback for updating the tx current
103 }
104 
106 {
107  NS_LOG_FUNCTION (this);
108  delete m_listener;
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION (this << source);
115  NS_ASSERT (source != NULL);
116  m_source = source;
117 }
118 
119 double
121 {
122  NS_LOG_FUNCTION (this);
124 }
125 
126 double
128 {
129  NS_LOG_FUNCTION (this);
130  return m_idleCurrentA;
131 }
132 
133 void
135 {
136  NS_LOG_FUNCTION (this << idleCurrentA);
137  m_idleCurrentA = idleCurrentA;
138 }
139 
140 double
142 {
143  NS_LOG_FUNCTION (this);
144  return m_ccaBusyCurrentA;
145 }
146 
147 void
149 {
150  NS_LOG_FUNCTION (this << CcaBusyCurrentA);
151  m_ccaBusyCurrentA = CcaBusyCurrentA;
152 }
153 
154 double
156 {
157  NS_LOG_FUNCTION (this);
158  return m_txCurrentA;
159 }
160 
161 void
163 {
164  NS_LOG_FUNCTION (this << txCurrentA);
165  m_txCurrentA = txCurrentA;
166 }
167 
168 double
170 {
171  NS_LOG_FUNCTION (this);
172  return m_rxCurrentA;
173 }
174 
175 void
177 {
178  NS_LOG_FUNCTION (this << rxCurrentA);
179  m_rxCurrentA = rxCurrentA;
180 }
181 
182 double
184 {
185  NS_LOG_FUNCTION (this);
186  return m_switchingCurrentA;
187 }
188 
189 void
191 {
192  NS_LOG_FUNCTION (this << switchingCurrentA);
193  m_switchingCurrentA = switchingCurrentA;
194 }
195 
196 double
198 {
199  NS_LOG_FUNCTION (this);
200  return m_sleepCurrentA;
201 }
202 
203 void
205 {
206  NS_LOG_FUNCTION (this << sleepCurrentA);
207  m_sleepCurrentA = sleepCurrentA;
208 }
209 
212 {
213  NS_LOG_FUNCTION (this);
214  return m_currentState;
215 }
216 
217 void
220 {
221  NS_LOG_FUNCTION (this);
222  if (callback.IsNull ())
223  {
224  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback!");
225  }
226  m_energyDepletionCallback = callback;
227 }
228 
229 void
232 {
233  NS_LOG_FUNCTION (this);
234  if (callback.IsNull ())
235  {
236  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy recharged callback!");
237  }
238  m_energyRechargedCallback = callback;
239 }
240 
241 void
243 {
244  m_txCurrentModel = model;
245 }
246 
247 void
249 {
250  if (m_txCurrentModel)
251  {
252  m_txCurrentA = m_txCurrentModel->CalcTxCurrent (txPowerDbm);
253  }
254 }
255 
256 void
258 {
259  NS_LOG_FUNCTION (this << newState);
260 
261  Time duration = Simulator::Now () - m_lastUpdateTime;
262  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
263 
264  // energy to decrease = current * voltage * time
265  double energyToDecrease = 0.0;
266  double supplyVoltage = m_source->GetSupplyVoltage ();
267  switch (m_currentState)
268  {
269  case WifiPhy::IDLE:
270  energyToDecrease = duration.GetSeconds () * m_idleCurrentA * supplyVoltage;
271  break;
272  case WifiPhy::CCA_BUSY:
273  energyToDecrease = duration.GetSeconds () * m_ccaBusyCurrentA * supplyVoltage;
274  break;
275  case WifiPhy::TX:
276  energyToDecrease = duration.GetSeconds () * m_txCurrentA * supplyVoltage;
277  break;
278  case WifiPhy::RX:
279  energyToDecrease = duration.GetSeconds () * m_rxCurrentA * supplyVoltage;
280  break;
281  case WifiPhy::SWITCHING:
282  energyToDecrease = duration.GetSeconds () * m_switchingCurrentA * supplyVoltage;
283  break;
284  case WifiPhy::SLEEP:
285  energyToDecrease = duration.GetSeconds () * m_sleepCurrentA * supplyVoltage;
286  break;
287  default:
288  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state: " << m_currentState);
289  }
290 
291  // update total energy consumption
292  m_totalEnergyConsumption += energyToDecrease;
293 
294  // update last update time stamp
296 
298 
299  // notify energy source
300  m_source->UpdateEnergySource ();
301 
302  // in case the energy source is found to be depleted during the last update, a callback might be
303  // invoked that might cause a change in the Wifi PHY state (e.g., the PHY is put into SLEEP mode).
304  // This in turn causes a new call to this member function, with the consequence that the previous
305  // instance is resumed after the termination of the new instance. In particular, the state set
306  // by the previous instance is erroneously the final state stored in m_currentState. The check below
307  // ensures that previous instances do not change m_currentState.
308 
310  {
311  // update current state & last update time stamp
312  SetWifiRadioState ((WifiPhy::State) newState);
313 
314  // some debug message
315  NS_LOG_DEBUG ("WifiRadioEnergyModel:Total energy consumption is " <<
316  m_totalEnergyConsumption << "J");
317  }
318 
320 
322 }
323 
324 void
326 {
327  NS_LOG_FUNCTION (this);
328  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is depleted!");
329  // invoke energy depletion callback, if set.
331  {
333  }
334 }
335 
336 void
338 {
339  NS_LOG_FUNCTION (this);
340  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is recharged!");
341  // invoke energy recharged callback, if set.
343  {
345  }
346 }
347 
350 {
351  NS_LOG_FUNCTION (this);
352  return m_listener;
353 }
354 
355 /*
356  * Private functions start here.
357  */
358 
359 void
361 {
362  NS_LOG_FUNCTION (this);
363  m_source = NULL;
365 }
366 
367 double
369 {
370  NS_LOG_FUNCTION (this);
371  switch (m_currentState)
372  {
373  case WifiPhy::IDLE:
374  return m_idleCurrentA;
375  case WifiPhy::CCA_BUSY:
376  return m_ccaBusyCurrentA;
377  case WifiPhy::TX:
378  return m_txCurrentA;
379  case WifiPhy::RX:
380  return m_rxCurrentA;
381  case WifiPhy::SWITCHING:
382  return m_switchingCurrentA;
383  case WifiPhy::SLEEP:
384  return m_sleepCurrentA;
385  default:
386  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state:" << m_currentState);
387  }
388 }
389 
390 void
392 {
393  NS_LOG_FUNCTION (this << state);
394  m_currentState = state;
395  std::string stateName;
396  switch (state)
397  {
398  case WifiPhy::IDLE:
399  stateName = "IDLE";
400  break;
401  case WifiPhy::CCA_BUSY:
402  stateName = "CCA_BUSY";
403  break;
404  case WifiPhy::TX:
405  stateName = "TX";
406  break;
407  case WifiPhy::RX:
408  stateName = "RX";
409  break;
410  case WifiPhy::SWITCHING:
411  stateName = "SWITCHING";
412  break;
413  case WifiPhy::SLEEP:
414  stateName = "SLEEP";
415  break;
416  }
417  NS_LOG_DEBUG ("WifiRadioEnergyModel:Switching to state: " << stateName <<
418  " at time = " << Simulator::Now ());
419 }
420 
421 // -------------------------------------------------------------------------- //
422 
424 {
425  NS_LOG_FUNCTION (this);
428 }
429 
431 {
432  NS_LOG_FUNCTION (this);
433 }
434 
435 void
437 {
438  NS_LOG_FUNCTION (this << &callback);
439  NS_ASSERT (!callback.IsNull ());
440  m_changeStateCallback = callback;
441 }
442 
443 void
445 {
446  NS_LOG_FUNCTION (this << &callback);
447  NS_ASSERT (!callback.IsNull ());
448  m_updateTxCurrentCallback = callback;
449 }
450 
451 void
453 {
454  NS_LOG_FUNCTION (this << duration);
456  {
457  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
458  }
461 }
462 
463 void
465 {
466  NS_LOG_FUNCTION (this);
468  {
469  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
470  }
472 }
473 
474 void
476 {
477  NS_LOG_FUNCTION (this);
479  {
480  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
481  }
483 }
484 
485 void
487 {
488  NS_LOG_FUNCTION (this << duration << txPowerDbm);
490  {
491  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Update tx current callback not set!");
492  }
493  m_updateTxCurrentCallback (txPowerDbm);
495  {
496  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
497  }
499  // schedule changing state back to IDLE after TX duration
502 }
503 
504 void
506 {
507  NS_LOG_FUNCTION (this << duration);
509  {
510  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
511  }
513  // schedule changing state back to IDLE after CCA_BUSY duration
516 }
517 
518 void
520 {
521  NS_LOG_FUNCTION (this << duration);
523  {
524  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
525  }
527  // schedule changing state back to IDLE after CCA_BUSY duration
530 }
531 
532 void
534 {
535  NS_LOG_FUNCTION (this);
537  {
538  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
539  }
542 }
543 
544 void
546 {
547  NS_LOG_FUNCTION (this);
549  {
550  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
551  }
553 }
554 
555 /*
556  * Private function state here.
557  */
558 
559 void
561 {
562  NS_LOG_FUNCTION (this);
564  {
565  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
566  }
568 }
569 
570 } // namespace ns3
void SetWifiRadioState(const WifiPhy::State state)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
static TypeId GetTypeId(void)
Get the type ID.
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 "...
void HandleEnergyRecharged(void)
Handles energy recharged.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
TracedValue< double > m_totalEnergyConsumption
This variable keeps track of the total energy consumed by this model.
Base class for device energy models.
double m_rxCurrentA
receive current
The PHY layer has sense the medium busy through the CCA mechanism.
Definition: wifi-phy.h:180
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
double m_ccaBusyCurrentA
CCA busy current.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
void ChangeState(int newState)
Changes state of the WifiRadioEnergyMode.
The PHY layer is sleeping.
Definition: wifi-phy.h:196
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
WifiRadioEnergyModelPhyListener * m_listener
WifiPhy listener.
double GetSleepCurrentA(void) const
Gets sleep current.
void SetSleepCurrentA(double sleepCurrentA)
Sets sleep current.
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)
Sets receive current.
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)
Sets CCA busy current.
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
Gets CCA busy current.
void SetChangeStateCallback(DeviceEnergyModel::ChangeStateCallback callback)
Sets the change state callback.
WifiPhy::State m_currentState
current state the radio is in
Ptr< WifiTxCurrentModel > m_txCurrentModel
current model
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
double m_sleepCurrentA
sleep current
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
Ptr< EnergySource > m_source
energy source
The PHY layer is IDLE.
Definition: wifi-phy.h:176
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)
void NotifyRxEndError(void)
Switches the WifiRadioEnergyModel back to IDLE state.
The PHY layer is receiving a packet.
Definition: wifi-phy.h:188
Time m_lastUpdateTime
time stamp of previous energy update
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:184
The PHY layer is switching to other channel.
Definition: wifi-phy.h:192
EventId m_switchToIdleEvent
switch to idle event
void NotifyTxStart(Time duration, double txPowerDbm)
Switches the WifiRadioEnergyModel to TX state and switches back to IDLE after TX duration.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
void NotifyWakeup(void)
Defined in ns3::WifiPhyListener.
double GetTxCurrentA(void) const
Gets transmit current.
double GetRxCurrentA(void) const
Gets receive current.
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:249
void SetTxCurrentA(double txCurrentA)
Sets transmit current.
void NotifySleep(void)
Defined in ns3::WifiPhyListener.
A WiFi radio energy model.
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.
uint8_t m_nPendingChangeState
pending state change
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
WifiRadioEnergyDepletionCallback m_energyDepletionCallback
Energy depletion callback.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
double GetIdleCurrentA(void) const
Gets idle current.
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1274
virtual void ChangeState(int newState)=0
void SetSwitchingCurrentA(double switchingCurrentA)
Sets switching current.
void SetEnergySource(const Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
double m_switchingCurrentA
switching current
bool m_isSupersededChangeState
superseded change state
void SetTxCurrentModel(const Ptr< WifiTxCurrentModel > model)
State
The state of the PHY layer.
Definition: wifi-phy.h:171
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:914
void SetIdleCurrentA(double idleCurrentA)
Sets idle current.
WifiRadioEnergyRechargedCallback m_energyRechargedCallback
Energy recharged callback.
double GetSwitchingCurrentA(void) const
Gets switching current.
void HandleEnergyDepletion(void)
Handles energy depletion.
double m_txCurrentA
transmit current