A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "energy-source.h"
27 
28 NS_LOG_COMPONENT_DEFINE ("WifiRadioEnergyModel");
29 
30 namespace ns3 {
31 
32 NS_OBJECT_ENSURE_REGISTERED (WifiRadioEnergyModel);
33 
34 TypeId
36 {
37  static TypeId tid = TypeId ("ns3::WifiRadioEnergyModel")
39  .AddConstructor<WifiRadioEnergyModel> ()
40  .AddAttribute ("IdleCurrentA",
41  "The default radio Idle current in Ampere.",
42  DoubleValue (0.000426), // idle mode = 426uA
43  MakeDoubleAccessor (&WifiRadioEnergyModel::SetIdleCurrentA,
45  MakeDoubleChecker<double> ())
46  .AddAttribute ("CcaBusyCurrentA",
47  "The default radio CCA Busy State current in Ampere.",
48  DoubleValue (0.000426), // default to be the same as idle mode
49  MakeDoubleAccessor (&WifiRadioEnergyModel::SetCcaBusyCurrentA,
51  MakeDoubleChecker<double> ())
52  .AddAttribute ("TxCurrentA",
53  "The radio Tx current in Ampere.",
54  DoubleValue (0.0174), // transmit at 0dBm = 17.4mA
55  MakeDoubleAccessor (&WifiRadioEnergyModel::SetTxCurrentA,
57  MakeDoubleChecker<double> ())
58  .AddAttribute ("RxCurrentA",
59  "The radio Rx current in Ampere.",
60  DoubleValue (0.0197), // receive mode = 19.7mA
61  MakeDoubleAccessor (&WifiRadioEnergyModel::SetRxCurrentA,
63  MakeDoubleChecker<double> ())
64  .AddAttribute ("SwitchingCurrentA",
65  "The default radio Channel Switch current in Ampere.",
66  DoubleValue (0.000426), // default to be the same as idle mode
67  MakeDoubleAccessor (&WifiRadioEnergyModel::SetSwitchingCurrentA,
69  MakeDoubleChecker<double> ())
70  .AddTraceSource ("TotalEnergyConsumption",
71  "Total energy consumption of the radio device.",
73  ;
74  return tid;
75 }
76 
78 {
79  NS_LOG_FUNCTION (this);
80  m_currentState = WifiPhy::IDLE; // initially IDLE
81  m_lastUpdateTime = Seconds (0.0);
83  m_source = NULL;
84  // set callback for WifiPhy listener
87 }
88 
90 {
91  NS_LOG_FUNCTION (this);
92  delete m_listener;
93 }
94 
95 void
97 {
98  NS_LOG_FUNCTION (this << source);
99  NS_ASSERT (source != NULL);
100  m_source = source;
101 }
102 
103 double
105 {
106  NS_LOG_FUNCTION (this);
108 }
109 
110 double
112 {
113  NS_LOG_FUNCTION (this);
114  return m_idleCurrentA;
115 }
116 
117 void
119 {
120  NS_LOG_FUNCTION (this << idleCurrentA);
121  m_idleCurrentA = idleCurrentA;
122 }
123 
124 double
126 {
127  NS_LOG_FUNCTION (this);
128  return m_ccaBusyCurrentA;
129 }
130 
131 void
133 {
134  NS_LOG_FUNCTION (this << CcaBusyCurrentA);
135  m_ccaBusyCurrentA = CcaBusyCurrentA;
136 }
137 
138 double
140 {
141  NS_LOG_FUNCTION (this);
142  return m_txCurrentA;
143 }
144 
145 void
147 {
148  NS_LOG_FUNCTION (this << txCurrentA);
149  m_txCurrentA = txCurrentA;
150 }
151 
152 double
154 {
155  NS_LOG_FUNCTION (this);
156  return m_rxCurrentA;
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION (this << rxCurrentA);
163  m_rxCurrentA = rxCurrentA;
164 }
165 
166 double
168 {
169  NS_LOG_FUNCTION (this);
170  return m_switchingCurrentA;
171 }
172 
173 void
175 {
176  NS_LOG_FUNCTION (this << switchingCurrentA);
177  m_switchingCurrentA = switchingCurrentA;
178 }
179 
180 
183 {
184  NS_LOG_FUNCTION (this);
185  return m_currentState;
186 }
187 
188 void
191 {
192  NS_LOG_FUNCTION (this);
193  if (callback.IsNull ())
194  {
195  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback!");
196  }
197  m_energyDepletionCallback = callback;
198 }
199 
200 void
202 {
203  NS_LOG_FUNCTION (this << newState);
204 
205  Time duration = Simulator::Now () - m_lastUpdateTime;
206  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
207 
208  // energy to decrease = current * voltage * time
209  double energyToDecrease = 0.0;
210  double supplyVoltage = m_source->GetSupplyVoltage ();
211  switch (m_currentState)
212  {
213  case WifiPhy::IDLE:
214  energyToDecrease = duration.GetSeconds () * m_idleCurrentA * supplyVoltage;
215  break;
216  case WifiPhy::CCA_BUSY:
217  energyToDecrease = duration.GetSeconds () * m_ccaBusyCurrentA * supplyVoltage;
218  break;
219  case WifiPhy::TX:
220  energyToDecrease = duration.GetSeconds () * m_txCurrentA * supplyVoltage;
221  break;
222  case WifiPhy::RX:
223  energyToDecrease = duration.GetSeconds () * m_rxCurrentA * supplyVoltage;
224  break;
225  case WifiPhy::SWITCHING:
226  energyToDecrease = duration.GetSeconds () * m_switchingCurrentA * supplyVoltage;
227  break;
228  default:
229  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state: " << m_currentState);
230  }
231 
232  // update total energy consumption
233  m_totalEnergyConsumption += energyToDecrease;
234 
235  // update last update time stamp
237 
238  // notify energy source
239  m_source->UpdateEnergySource ();
240 
241  // update current state & last update time stamp
242  SetWifiRadioState ((WifiPhy::State) newState);
243 
244  // some debug message
245  NS_LOG_DEBUG ("WifiRadioEnergyModel:Total energy consumption is " <<
246  m_totalEnergyConsumption << "J");
247 }
248 
249 void
251 {
252  NS_LOG_FUNCTION (this);
253  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is depleted!");
254  // invoke energy depletion callback, if set.
256  {
258  }
259 }
260 
263 {
264  NS_LOG_FUNCTION (this);
265  return m_listener;
266 }
267 
268 /*
269  * Private functions start here.
270  */
271 
272 void
274 {
275  NS_LOG_FUNCTION (this);
276  m_source = NULL;
278 }
279 
280 double
282 {
283  NS_LOG_FUNCTION (this);
284  switch (m_currentState)
285  {
286  case WifiPhy::IDLE:
287  return m_idleCurrentA;
288  case WifiPhy::CCA_BUSY:
289  return m_ccaBusyCurrentA;
290  case WifiPhy::TX:
291  return m_txCurrentA;
292  case WifiPhy::RX:
293  return m_rxCurrentA;
294  case WifiPhy::SWITCHING:
295  return m_switchingCurrentA;
296  default:
297  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state:" << m_currentState);
298  }
299 }
300 
301 void
303 {
304  NS_LOG_FUNCTION (this << state);
305  m_currentState = state;
306  std::string stateName;
307  switch (state)
308  {
309  case WifiPhy::IDLE:
310  stateName = "IDLE";
311  break;
312  case WifiPhy::CCA_BUSY:
313  stateName = "CCA_BUSY";
314  break;
315  case WifiPhy::TX:
316  stateName = "TX";
317  break;
318  case WifiPhy::RX:
319  stateName = "RX";
320  break;
321  case WifiPhy::SWITCHING:
322  stateName = "SWITCHING";
323  break;
324  }
325  NS_LOG_DEBUG ("WifiRadioEnergyModel:Switching to state: " << stateName <<
326  " at time = " << Simulator::Now ());
327 }
328 
329 // -------------------------------------------------------------------------- //
330 
332 {
333  NS_LOG_FUNCTION (this);
335 }
336 
338 {
339  NS_LOG_FUNCTION (this);
340 }
341 
342 void
344 {
345  NS_LOG_FUNCTION (this << &callback);
346  NS_ASSERT (!callback.IsNull ());
347  m_changeStateCallback = callback;
348 }
349 
350 void
352 {
353  NS_LOG_FUNCTION (this << duration);
355  {
356  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
357  }
360 }
361 
362 void
364 {
365  NS_LOG_FUNCTION (this);
367  {
368  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
369  }
371 }
372 
373 void
375 {
376  NS_LOG_FUNCTION (this);
378  {
379  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
380  }
382 }
383 
384 void
386 {
387  NS_LOG_FUNCTION (this << duration);
389  {
390  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
391  }
393  // schedule changing state back to IDLE after TX duration
396 }
397 
398 void
400 {
401  NS_LOG_FUNCTION (this << duration);
403  {
404  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
405  }
407  // schedule changing state back to IDLE after CCA_BUSY duration
410 }
411 
412 void
414 {
415  NS_LOG_FUNCTION (this << duration);
417  {
418  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
419  }
421  // schedule changing state back to IDLE after CCA_BUSY duration
424 }
425 
426 /*
427  * Private function state here.
428  */
429 
430 void
432 {
433  NS_LOG_FUNCTION (this);
435  {
436  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
437  }
439 }
440 
441 } // namespace ns3
void SetWifiRadioState(const WifiPhy::State state)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
TracedValue< double > m_totalEnergyConsumption
Base class for device energy models.
State
The state of the PHY layer.
Definition: wifi-phy.h:123
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1018
#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:170
virtual void ChangeState(int newState)
Changes state of the WifiRadioEnergyMode.
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
WifiRadioEnergyModelPhyListener * m_listener
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:825
virtual double GetTotalEnergyConsumption(void) const
virtual void NotifyTxStart(Time duration)
Switches the WifiRadioEnergyModel to TX state and switches back to IDLE after TX duration.
WifiRadioEnergyModelPhyListener * GetPhyListener(void)
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.
The PHY layer is switching to other channel.
Definition: wifi-phy.h:144
void SetCcaBusyCurrentA(double ccaBusyCurrentA)
double GetSeconds(void) const
Definition: nstime.h:272
double GetCcaBusyCurrentA(void) const
void SetChangeStateCallback(DeviceEnergyModel::ChangeStateCallback callback)
Sets the change state callback.
virtual void NotifySwitchingStart(Time duration)
virtual void NotifyMaybeCcaBusyStart(Time duration)
virtual void NotifyRxEndError(void)
Switches the WifiRadioEnergyModel back to IDLE state.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
WifiPhy::State GetCurrentState(void) const
DeviceEnergyModel::ChangeStateCallback m_changeStateCallback
Change state callback used to notify the WifiRadioEnergyModel of a state change.
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
The PHY layer is IDLE.
Definition: wifi-phy.h:128
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
A WifiPhy listener class for notifying the WifiRadioEnergyModel of Wifi radio state change...
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
void SetTxCurrentA(double txCurrentA)
virtual void NotifyRxStart(Time duration)
Switches the WifiRadioEnergyModel to RX state.
int64_t GetNanoSeconds(void) const
Definition: nstime.h:297
void SetEnergyDepletionCallback(WifiRadioEnergyDepletionCallback callback)
void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
WifiRadioEnergyDepletionCallback m_energyDepletionCallback
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::cancel method.
Definition: event-id.cc:47
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1022
virtual void ChangeState(int newState)=0
void SetSwitchingCurrentA(double switchingCurrentA)
virtual double DoGetCurrentA(void) const
The PHY layer is receiving a packet.
Definition: wifi-phy.h:140
Hold a floating point type.
Definition: double.h:41
The PHY layer has sense the medium busy through the CCA mechanism.
Definition: wifi-phy.h:132
a unique identifier for an interface.
Definition: type-id.h:49
The PHY layer is sending a packet.
Definition: wifi-phy.h:136
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
void SetIdleCurrentA(double idleCurrentA)
double GetSwitchingCurrentA(void) const
virtual void HandleEnergyDepletion(void)
Handles energy depletion.