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  delete m_listener;
92 }
93 
94 void
96 {
97  NS_LOG_FUNCTION (this << source);
98  NS_ASSERT (source != NULL);
99  m_source = source;
100 }
101 
102 double
104 {
106 }
107 
108 double
110 {
111  return m_idleCurrentA;
112 }
113 
114 void
116 {
117  NS_LOG_FUNCTION (this << idleCurrentA);
118  m_idleCurrentA = idleCurrentA;
119 }
120 
121 double
123 {
124  return m_ccaBusyCurrentA;
125 }
126 
127 void
129 {
130  NS_LOG_FUNCTION (this << CcaBusyCurrentA);
131  m_ccaBusyCurrentA = CcaBusyCurrentA;
132 }
133 
134 double
136 {
137  return m_txCurrentA;
138 }
139 
140 void
142 {
143  NS_LOG_FUNCTION (this << txCurrentA);
144  m_txCurrentA = txCurrentA;
145 }
146 
147 double
149 {
150  return m_rxCurrentA;
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this << rxCurrentA);
157  m_rxCurrentA = rxCurrentA;
158 }
159 
160 double
162 {
163  return m_switchingCurrentA;
164 }
165 
166 void
168 {
169  NS_LOG_FUNCTION (this << switchingCurrentA);
170  m_switchingCurrentA = switchingCurrentA;
171 }
172 
173 
176 {
177  return m_currentState;
178 }
179 
180 void
183 {
184  NS_LOG_FUNCTION (this);
185  if (callback.IsNull ())
186  {
187  NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback!");
188  }
189  m_energyDepletionCallback = callback;
190 }
191 
192 void
194 {
195  NS_LOG_FUNCTION (this << newState);
196 
197  Time duration = Simulator::Now () - m_lastUpdateTime;
198  NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid
199 
200  // energy to decrease = current * voltage * time
201  double energyToDecrease = 0.0;
202  double supplyVoltage = m_source->GetSupplyVoltage ();
203  switch (m_currentState)
204  {
205  case WifiPhy::IDLE:
206  energyToDecrease = duration.GetSeconds () * m_idleCurrentA * supplyVoltage;
207  break;
208  case WifiPhy::CCA_BUSY:
209  energyToDecrease = duration.GetSeconds () * m_ccaBusyCurrentA * supplyVoltage;
210  break;
211  case WifiPhy::TX:
212  energyToDecrease = duration.GetSeconds () * m_txCurrentA * supplyVoltage;
213  break;
214  case WifiPhy::RX:
215  energyToDecrease = duration.GetSeconds () * m_rxCurrentA * supplyVoltage;
216  break;
217  case WifiPhy::SWITCHING:
218  energyToDecrease = duration.GetSeconds () * m_switchingCurrentA * supplyVoltage;
219  break;
220  default:
221  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state: " << m_currentState);
222  }
223 
224  // update total energy consumption
225  m_totalEnergyConsumption += energyToDecrease;
226 
227  // update last update time stamp
229 
230  // notify energy source
232 
233  // update current state & last update time stamp
234  SetWifiRadioState ((WifiPhy::State) newState);
235 
236  // some debug message
237  NS_LOG_DEBUG ("WifiRadioEnergyModel:Total energy consumption is " <<
238  m_totalEnergyConsumption << "J");
239 }
240 
241 void
243 {
244  NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is depleted!");
245  // invoke energy depletion callback, if set.
247  {
249  }
250 }
251 
254 {
255  return m_listener;
256 }
257 
258 /*
259  * Private functions start here.
260  */
261 
262 void
264 {
265  m_source = NULL;
267 }
268 
269 double
271 {
272  switch (m_currentState)
273  {
274  case WifiPhy::IDLE:
275  return m_idleCurrentA;
276  case WifiPhy::CCA_BUSY:
277  return m_ccaBusyCurrentA;
278  case WifiPhy::TX:
279  return m_txCurrentA;
280  case WifiPhy::RX:
281  return m_rxCurrentA;
282  case WifiPhy::SWITCHING:
283  return m_switchingCurrentA;
284  default:
285  NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state:" << m_currentState);
286  }
287 }
288 
289 void
291 {
292  NS_LOG_FUNCTION (this << state);
293  m_currentState = state;
294  std::string stateName;
295  switch (state)
296  {
297  case WifiPhy::IDLE:
298  stateName = "IDLE";
299  break;
300  case WifiPhy::CCA_BUSY:
301  stateName = "CCA_BUSY";
302  break;
303  case WifiPhy::TX:
304  stateName = "TX";
305  break;
306  case WifiPhy::RX:
307  stateName = "RX";
308  break;
309  case WifiPhy::SWITCHING:
310  stateName = "SWITCHING";
311  break;
312  }
313  NS_LOG_DEBUG ("WifiRadioEnergyModel:Switching to state: " << stateName <<
314  " at time = " << Simulator::Now ());
315 }
316 
317 // -------------------------------------------------------------------------- //
318 
320 {
322 }
323 
325 {
326 }
327 
328 void
330 {
331  NS_ASSERT (!callback.IsNull ());
332  m_changeStateCallback = callback;
333 }
334 
335 void
337 {
339  {
340  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
341  }
344 }
345 
346 void
348 {
350  {
351  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
352  }
354 }
355 
356 void
358 {
360  {
361  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
362  }
364 }
365 
366 void
368 {
370  {
371  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
372  }
374  // schedule changing state back to IDLE after TX duration
377 }
378 
379 void
381 {
383  {
384  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
385  }
387  // schedule changing state back to IDLE after CCA_BUSY duration
390 }
391 
392 void
394 {
396  {
397  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
398  }
400  // schedule changing state back to IDLE after CCA_BUSY duration
403 }
404 
405 /*
406  * Private function state here.
407  */
408 
409 void
411 {
413  {
414  NS_FATAL_ERROR ("WifiRadioEnergyModelPhyListener:Change state callback not set!");
415  }
417 }
418 
419 } // namespace ns3