A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
"
26
#include "
wifi-radio-energy-model.h
"
27
28
NS_LOG_COMPONENT_DEFINE
(
"WifiRadioEnergyModel"
);
29
30
namespace
ns3 {
31
32
NS_OBJECT_ENSURE_REGISTERED
(WifiRadioEnergyModel);
33
34
TypeId
35
WifiRadioEnergyModel::GetTypeId
(
void
)
36
{
37
static
TypeId
tid =
TypeId
(
"ns3::WifiRadioEnergyModel"
)
38
.
SetParent
<
DeviceEnergyModel
> ()
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
,
44
&
WifiRadioEnergyModel::GetIdleCurrentA
),
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
,
50
&
WifiRadioEnergyModel::GetCcaBusyCurrentA
),
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
,
56
&
WifiRadioEnergyModel::GetTxCurrentA
),
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
,
62
&
WifiRadioEnergyModel::GetRxCurrentA
),
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
,
68
&
WifiRadioEnergyModel::GetSwitchingCurrentA
),
69
MakeDoubleChecker<double> ())
70
.AddTraceSource (
"TotalEnergyConsumption"
,
71
"Total energy consumption of the radio device."
,
72
MakeTraceSourceAccessor
(&
WifiRadioEnergyModel::m_totalEnergyConsumption
))
73
;
74
return
tid;
75
}
76
77
WifiRadioEnergyModel::WifiRadioEnergyModel
()
78
{
79
NS_LOG_FUNCTION
(
this
);
80
m_currentState
=
WifiPhy::IDLE
;
// initially IDLE
81
m_lastUpdateTime
=
Seconds
(0.0);
82
m_energyDepletionCallback
.
Nullify
();
83
m_source
= NULL;
84
// set callback for WifiPhy listener
85
m_listener
=
new
WifiRadioEnergyModelPhyListener
;
86
m_listener
->
SetChangeStateCallback
(
MakeCallback
(&
DeviceEnergyModel::ChangeState
,
this
));
87
}
88
89
WifiRadioEnergyModel::~WifiRadioEnergyModel
()
90
{
91
NS_LOG_FUNCTION
(
this
);
92
delete
m_listener
;
93
}
94
95
void
96
WifiRadioEnergyModel::SetEnergySource
(
Ptr<EnergySource>
source)
97
{
98
NS_LOG_FUNCTION
(
this
<< source);
99
NS_ASSERT
(source != NULL);
100
m_source
= source;
101
}
102
103
double
104
WifiRadioEnergyModel::GetTotalEnergyConsumption
(
void
)
const
105
{
106
NS_LOG_FUNCTION
(
this
);
107
return
m_totalEnergyConsumption
;
108
}
109
110
double
111
WifiRadioEnergyModel::GetIdleCurrentA
(
void
)
const
112
{
113
NS_LOG_FUNCTION
(
this
);
114
return
m_idleCurrentA
;
115
}
116
117
void
118
WifiRadioEnergyModel::SetIdleCurrentA
(
double
idleCurrentA)
119
{
120
NS_LOG_FUNCTION
(
this
<< idleCurrentA);
121
m_idleCurrentA
= idleCurrentA;
122
}
123
124
double
125
WifiRadioEnergyModel::GetCcaBusyCurrentA
(
void
)
const
126
{
127
NS_LOG_FUNCTION
(
this
);
128
return
m_ccaBusyCurrentA
;
129
}
130
131
void
132
WifiRadioEnergyModel::SetCcaBusyCurrentA
(
double
CcaBusyCurrentA)
133
{
134
NS_LOG_FUNCTION
(
this
<< CcaBusyCurrentA);
135
m_ccaBusyCurrentA
= CcaBusyCurrentA;
136
}
137
138
double
139
WifiRadioEnergyModel::GetTxCurrentA
(
void
)
const
140
{
141
NS_LOG_FUNCTION
(
this
);
142
return
m_txCurrentA
;
143
}
144
145
void
146
WifiRadioEnergyModel::SetTxCurrentA
(
double
txCurrentA)
147
{
148
NS_LOG_FUNCTION
(
this
<< txCurrentA);
149
m_txCurrentA
= txCurrentA;
150
}
151
152
double
153
WifiRadioEnergyModel::GetRxCurrentA
(
void
)
const
154
{
155
NS_LOG_FUNCTION
(
this
);
156
return
m_rxCurrentA
;
157
}
158
159
void
160
WifiRadioEnergyModel::SetRxCurrentA
(
double
rxCurrentA)
161
{
162
NS_LOG_FUNCTION
(
this
<< rxCurrentA);
163
m_rxCurrentA
= rxCurrentA;
164
}
165
166
double
167
WifiRadioEnergyModel::GetSwitchingCurrentA
(
void
)
const
168
{
169
NS_LOG_FUNCTION
(
this
);
170
return
m_switchingCurrentA
;
171
}
172
173
void
174
WifiRadioEnergyModel::SetSwitchingCurrentA
(
double
switchingCurrentA)
175
{
176
NS_LOG_FUNCTION
(
this
<< switchingCurrentA);
177
m_switchingCurrentA
= switchingCurrentA;
178
}
179
180
181
WifiPhy::State
182
WifiRadioEnergyModel::GetCurrentState
(
void
)
const
183
{
184
NS_LOG_FUNCTION
(
this
);
185
return
m_currentState
;
186
}
187
188
void
189
WifiRadioEnergyModel::SetEnergyDepletionCallback
(
190
WifiRadioEnergyDepletionCallback
callback)
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
201
WifiRadioEnergyModel::ChangeState
(
int
newState)
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
236
m_lastUpdateTime
=
Simulator::Now
();
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
250
WifiRadioEnergyModel::HandleEnergyDepletion
(
void
)
251
{
252
NS_LOG_FUNCTION
(
this
);
253
NS_LOG_DEBUG
(
"WifiRadioEnergyModel:Energy is depleted!"
);
254
// invoke energy depletion callback, if set.
255
if
(!
m_energyDepletionCallback
.
IsNull
())
256
{
257
m_energyDepletionCallback
();
258
}
259
}
260
261
WifiRadioEnergyModelPhyListener
*
262
WifiRadioEnergyModel::GetPhyListener
(
void
)
263
{
264
NS_LOG_FUNCTION
(
this
);
265
return
m_listener
;
266
}
267
268
/*
269
* Private functions start here.
270
*/
271
272
void
273
WifiRadioEnergyModel::DoDispose
(
void
)
274
{
275
NS_LOG_FUNCTION
(
this
);
276
m_source
= NULL;
277
m_energyDepletionCallback
.
Nullify
();
278
}
279
280
double
281
WifiRadioEnergyModel::DoGetCurrentA
(
void
)
const
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
302
WifiRadioEnergyModel::SetWifiRadioState
(
const
WifiPhy::State
state)
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
331
WifiRadioEnergyModelPhyListener::WifiRadioEnergyModelPhyListener
()
332
{
333
NS_LOG_FUNCTION
(
this
);
334
m_changeStateCallback
.
Nullify
();
335
}
336
337
WifiRadioEnergyModelPhyListener::~WifiRadioEnergyModelPhyListener
()
338
{
339
NS_LOG_FUNCTION
(
this
);
340
}
341
342
void
343
WifiRadioEnergyModelPhyListener::SetChangeStateCallback
(
DeviceEnergyModel::ChangeStateCallback
callback)
344
{
345
NS_LOG_FUNCTION
(
this
<< &callback);
346
NS_ASSERT
(!callback.
IsNull
());
347
m_changeStateCallback
= callback;
348
}
349
350
void
351
WifiRadioEnergyModelPhyListener::NotifyRxStart
(
Time
duration)
352
{
353
NS_LOG_FUNCTION
(
this
<< duration);
354
if
(
m_changeStateCallback
.
IsNull
())
355
{
356
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
357
}
358
m_changeStateCallback
(
WifiPhy::RX
);
359
m_switchToIdleEvent
.
Cancel
();
360
}
361
362
void
363
WifiRadioEnergyModelPhyListener::NotifyRxEndOk
(
void
)
364
{
365
NS_LOG_FUNCTION
(
this
);
366
if
(
m_changeStateCallback
.
IsNull
())
367
{
368
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
369
}
370
m_changeStateCallback
(
WifiPhy::IDLE
);
371
}
372
373
void
374
WifiRadioEnergyModelPhyListener::NotifyRxEndError
(
void
)
375
{
376
NS_LOG_FUNCTION
(
this
);
377
if
(
m_changeStateCallback
.
IsNull
())
378
{
379
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
380
}
381
m_changeStateCallback
(
WifiPhy::IDLE
);
382
}
383
384
void
385
WifiRadioEnergyModelPhyListener::NotifyTxStart
(
Time
duration)
386
{
387
NS_LOG_FUNCTION
(
this
<< duration);
388
if
(
m_changeStateCallback
.
IsNull
())
389
{
390
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
391
}
392
m_changeStateCallback
(
WifiPhy::TX
);
393
// schedule changing state back to IDLE after TX duration
394
m_switchToIdleEvent
.
Cancel
();
395
m_switchToIdleEvent
=
Simulator::Schedule
(duration, &
WifiRadioEnergyModelPhyListener::SwitchToIdle
,
this
);
396
}
397
398
void
399
WifiRadioEnergyModelPhyListener::NotifyMaybeCcaBusyStart
(
Time
duration)
400
{
401
NS_LOG_FUNCTION
(
this
<< duration);
402
if
(
m_changeStateCallback
.
IsNull
())
403
{
404
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
405
}
406
m_changeStateCallback
(
WifiPhy::CCA_BUSY
);
407
// schedule changing state back to IDLE after CCA_BUSY duration
408
m_switchToIdleEvent
.
Cancel
();
409
m_switchToIdleEvent
=
Simulator::Schedule
(duration, &
WifiRadioEnergyModelPhyListener::SwitchToIdle
,
this
);
410
}
411
412
void
413
WifiRadioEnergyModelPhyListener::NotifySwitchingStart
(
Time
duration)
414
{
415
NS_LOG_FUNCTION
(
this
<< duration);
416
if
(
m_changeStateCallback
.
IsNull
())
417
{
418
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
419
}
420
m_changeStateCallback
(
WifiPhy::SWITCHING
);
421
// schedule changing state back to IDLE after CCA_BUSY duration
422
m_switchToIdleEvent
.
Cancel
();
423
m_switchToIdleEvent
=
Simulator::Schedule
(duration, &
WifiRadioEnergyModelPhyListener::SwitchToIdle
,
this
);
424
}
425
426
/*
427
* Private function state here.
428
*/
429
430
void
431
WifiRadioEnergyModelPhyListener::SwitchToIdle
(
void
)
432
{
433
NS_LOG_FUNCTION
(
this
);
434
if
(
m_changeStateCallback
.
IsNull
())
435
{
436
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
437
}
438
m_changeStateCallback
(
WifiPhy::IDLE
);
439
}
440
441
}
// namespace ns3
src
energy
model
wifi-radio-energy-model.cc
Generated on Tue May 14 2013 11:08:20 for ns-3 by
1.8.1.2