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 
35 TypeId
37 {
38  static TypeId tid = TypeId ("ns3::WifiRadioEnergyModel")
40  .AddConstructor<WifiRadioEnergyModel> ()
41  .AddAttribute ("IdleCurrentA",
42  "The default radio Idle current in Ampere.",
43  DoubleValue (0.000426), // idle mode = 426uA
44  MakeDoubleAccessor (&WifiRadioEnergyModel::SetIdleCurrentA,
46  MakeDoubleChecker<double> ())
47  .AddAttribute ("CcaBusyCurrentA",
48  "The default radio CCA Busy State current in Ampere.",
49  DoubleValue (0.000426), // default to be the same as idle mode
50  MakeDoubleAccessor (&WifiRadioEnergyModel::SetCcaBusyCurrentA,
52  MakeDoubleChecker<double> ())
53  .AddAttribute ("TxCurrentA",
54  "The radio Tx current in Ampere.",
55  DoubleValue (0.0174), // transmit at 0dBm = 17.4mA
56  MakeDoubleAccessor (&WifiRadioEnergyModel::SetTxCurrentA,
58  MakeDoubleChecker<double> ())
59  .AddAttribute ("RxCurrentA",
60  "The radio Rx current in Ampere.",
61  DoubleValue (0.0197), // receive mode = 19.7mA
62  MakeDoubleAccessor (&WifiRadioEnergyModel::SetRxCurrentA,
64  MakeDoubleChecker<double> ())
65  .AddAttribute ("SwitchingCurrentA",
66  "The default radio Channel Switch current in Ampere.",
67  DoubleValue (0.000426), // default to be the same as idle mode
68  MakeDoubleAccessor (&WifiRadioEnergyModel::SetSwitchingCurrentA,
70  MakeDoubleChecker<double> ())
71  .AddTraceSource ("TotalEnergyConsumption",
72  "Total energy consumption of the radio device.",
74  ;
75  return tid;
76 }
77 
79 {
80  NS_LOG_FUNCTION (this);
81  m_currentState = WifiPhy::IDLE; // initially IDLE
82  m_lastUpdateTime = Seconds (0.0);
84  m_source = NULL;
85  // set callback for WifiPhy listener
88 }
89 
91 {
92  NS_LOG_FUNCTION (this);
93  delete m_listener;
94 }
95 
96 void
98 {
99  NS_LOG_FUNCTION (this << source);
100  NS_ASSERT (source != NULL);
101  m_source = source;
102 }
103 
104 double
106 {
107  NS_LOG_FUNCTION (this);
109 }
110 
111 double
113 {
114  NS_LOG_FUNCTION (this);
115  return m_idleCurrentA;
116 }
117 
118 void
120 {
121  NS_LOG_FUNCTION (this << idleCurrentA);
122  m_idleCurrentA = idleCurrentA;
123 }
124 
125 double
127 {
128  NS_LOG_FUNCTION (this);
129  return m_ccaBusyCurrentA;
130 }
131 
132 void
134 {
135  NS_LOG_FUNCTION (this << CcaBusyCurrentA);
136  m_ccaBusyCurrentA = CcaBusyCurrentA;
137 }
138 
139 double
141 {
142  NS_LOG_FUNCTION (this);
143  return m_txCurrentA;
144 }
145 
146 void
148 {
149  NS_LOG_FUNCTION (this << txCurrentA);
150  m_txCurrentA = txCurrentA;
151 }
152 
153 double
155 {
156  NS_LOG_FUNCTION (this);
157  return m_rxCurrentA;
158 }
159 
160 void
162 {
163  NS_LOG_FUNCTION (this << rxCurrentA);
164  m_rxCurrentA = rxCurrentA;
165 }
166 
167 double
169 {
170  NS_LOG_FUNCTION (this);
171  return m_switchingCurrentA;
172 }
173 
174 void
176 {
177  NS_LOG_FUNCTION (this << switchingCurrentA);
178  m_switchingCurrentA = switchingCurrentA;
179 }
180 
181 
184 {
185  NS_LOG_FUNCTION (this);
186  return m_currentState;
187 }
188 
189 void
192 {
193  NS_LOG_FUNCTION (this);
194  if (callback.IsNull ())
195  {
196  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback!");
197  }
198  m_energyDepletionCallback = callback;
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION (this << newState);
205 
206  Time duration = Simulator::Now () - m_lastUpdateTime;
207  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
208 
209  // energy to decrease = current * voltage * time
210  double energyToDecrease = 0.0;
211  double supplyVoltage = m_source->GetSupplyVoltage ();
212  switch (m_currentState)
213  {
214  case WifiPhy::IDLE:
215  energyToDecrease = duration.GetSeconds () * m_idleCurrentA * supplyVoltage;
216  break;
217  case WifiPhy::CCA_BUSY:
218  energyToDecrease = duration.GetSeconds () * m_ccaBusyCurrentA * supplyVoltage;
219  break;
220  case WifiPhy::TX:
221  energyToDecrease = duration.GetSeconds () * m_txCurrentA * supplyVoltage;
222  break;
223  case WifiPhy::RX:
224  energyToDecrease = duration.GetSeconds () * m_rxCurrentA * supplyVoltage;
225  break;
226  case WifiPhy::SWITCHING:
227  energyToDecrease = duration.GetSeconds () * m_switchingCurrentA * supplyVoltage;
228  break;
229  default:
230  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state: " << m_currentState);
231  }
232 
233  // update total energy consumption
234  m_totalEnergyConsumption += energyToDecrease;
235 
236  // update last update time stamp
238 
239  // notify energy source
240  m_source->UpdateEnergySource ();
241 
242  // update current state & last update time stamp
243  SetWifiRadioState ((WifiPhy::State) newState);
244 
245  // some debug message
246  NS_LOG_DEBUG ("WifiRadioEnergyModel:Total energy consumption is " <<
247  m_totalEnergyConsumption << "J");
248 }
249 
250 void
252 {
253  NS_LOG_FUNCTION (this);
254  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is depleted!");
255  // invoke energy depletion callback, if set.
257  {
259  }
260 }
261 
264 {
265  NS_LOG_FUNCTION (this);
266  return m_listener;
267 }
268 
269 /*
270  * Private functions start here.
271  */
272 
273 void
275 {
276  NS_LOG_FUNCTION (this);
277  m_source = NULL;
279 }
280 
281 double
283 {
284  NS_LOG_FUNCTION (this);
285  switch (m_currentState)
286  {
287  case WifiPhy::IDLE:
288  return m_idleCurrentA;
289  case WifiPhy::CCA_BUSY:
290  return m_ccaBusyCurrentA;
291  case WifiPhy::TX:
292  return m_txCurrentA;
293  case WifiPhy::RX:
294  return m_rxCurrentA;
295  case WifiPhy::SWITCHING:
296  return m_switchingCurrentA;
297  default:
298  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state:" << m_currentState);
299  }
300 }
301 
302 void
304 {
305  NS_LOG_FUNCTION (this << state);
306  m_currentState = state;
307  std::string stateName;
308  switch (state)
309  {
310  case WifiPhy::IDLE:
311  stateName = "IDLE";
312  break;
313  case WifiPhy::CCA_BUSY:
314  stateName = "CCA_BUSY";
315  break;
316  case WifiPhy::TX:
317  stateName = "TX";
318  break;
319  case WifiPhy::RX:
320  stateName = "RX";
321  break;
322  case WifiPhy::SWITCHING:
323  stateName = "SWITCHING";
324  break;
325  }
326  NS_LOG_DEBUG ("WifiRadioEnergyModel:Switching to state: " << stateName <<
327  " at time = " << Simulator::Now ());
328 }
329 
330 // -------------------------------------------------------------------------- //
331 
333 {
334  NS_LOG_FUNCTION (this);
336 }
337 
339 {
340  NS_LOG_FUNCTION (this);
341 }
342 
343 void
345 {
346  NS_LOG_FUNCTION (this << &callback);
347  NS_ASSERT (!callback.IsNull ());
348  m_changeStateCallback = callback;
349 }
350 
351 void
353 {
354  NS_LOG_FUNCTION (this << duration);
356  {
357  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
358  }
361 }
362 
363 void
365 {
366  NS_LOG_FUNCTION (this);
368  {
369  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
370  }
372 }
373 
374 void
376 {
377  NS_LOG_FUNCTION (this);
379  {
380  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
381  }
383 }
384 
385 void
387 {
388  NS_LOG_FUNCTION (this << duration);
390  {
391  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
392  }
394  // schedule changing state back to IDLE after TX duration
397 }
398 
399 void
401 {
402  NS_LOG_FUNCTION (this << duration);
404  {
405  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
406  }
408  // schedule changing state back to IDLE after CCA_BUSY duration
411 }
412 
413 void
415 {
416  NS_LOG_FUNCTION (this << duration);
418  {
419  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
420  }
422  // schedule changing state back to IDLE after CCA_BUSY duration
425 }
426 
427 /*
428  * Private function state here.
429  */
430 
431 void
433 {
434  NS_LOG_FUNCTION (this);
436  {
437  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
438  }
440 }
441 
442 } // namespace ns3
void SetWifiRadioState(const WifiPhy::State state)
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
TracedValue< double > m_totalEnergyConsumption
Base class for device energy models.
State
The state of the PHY layer.
Definition: wifi-phy.h:123
NS_LOG_COMPONENT_DEFINE("WifiRadioEnergyModel")
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1014
#define NS_ASSERT(condition)
Definition: assert.h:64
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual void ChangeState(int newState)
Changes state of the WifiRadioEnergyMode.
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:824
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.
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
The PHY layer is switching to other channel.
Definition: wifi-phy.h:144
void SetCcaBusyCurrentA(double ccaBusyCurrentA)
double GetSeconds(void) const
Definition: nstime.h:274
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:1238
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:299
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)
Definition: log.h:289
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:1018
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:611
void SetIdleCurrentA(double idleCurrentA)
double GetSwitchingCurrentA(void) const
virtual void HandleEnergyDepletion(void)
Handles energy depletion.