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
delete
m_listener
;
92
}
93
94
void
95
WifiRadioEnergyModel::SetEnergySource
(
Ptr<EnergySource>
source)
96
{
97
NS_LOG_FUNCTION
(
this
<< source);
98
NS_ASSERT
(source != NULL);
99
m_source
= source;
100
}
101
102
double
103
WifiRadioEnergyModel::GetTotalEnergyConsumption
(
void
)
const
104
{
105
return
m_totalEnergyConsumption
;
106
}
107
108
double
109
WifiRadioEnergyModel::GetIdleCurrentA
(
void
)
const
110
{
111
return
m_idleCurrentA
;
112
}
113
114
void
115
WifiRadioEnergyModel::SetIdleCurrentA
(
double
idleCurrentA)
116
{
117
NS_LOG_FUNCTION
(
this
<< idleCurrentA);
118
m_idleCurrentA
= idleCurrentA;
119
}
120
121
double
122
WifiRadioEnergyModel::GetCcaBusyCurrentA
(
void
)
const
123
{
124
return
m_ccaBusyCurrentA
;
125
}
126
127
void
128
WifiRadioEnergyModel::SetCcaBusyCurrentA
(
double
CcaBusyCurrentA)
129
{
130
NS_LOG_FUNCTION
(
this
<< CcaBusyCurrentA);
131
m_ccaBusyCurrentA
= CcaBusyCurrentA;
132
}
133
134
double
135
WifiRadioEnergyModel::GetTxCurrentA
(
void
)
const
136
{
137
return
m_txCurrentA
;
138
}
139
140
void
141
WifiRadioEnergyModel::SetTxCurrentA
(
double
txCurrentA)
142
{
143
NS_LOG_FUNCTION
(
this
<< txCurrentA);
144
m_txCurrentA
= txCurrentA;
145
}
146
147
double
148
WifiRadioEnergyModel::GetRxCurrentA
(
void
)
const
149
{
150
return
m_rxCurrentA
;
151
}
152
153
void
154
WifiRadioEnergyModel::SetRxCurrentA
(
double
rxCurrentA)
155
{
156
NS_LOG_FUNCTION
(
this
<< rxCurrentA);
157
m_rxCurrentA
= rxCurrentA;
158
}
159
160
double
161
WifiRadioEnergyModel::GetSwitchingCurrentA
(
void
)
const
162
{
163
return
m_switchingCurrentA
;
164
}
165
166
void
167
WifiRadioEnergyModel::SetSwitchingCurrentA
(
double
switchingCurrentA)
168
{
169
NS_LOG_FUNCTION
(
this
<< switchingCurrentA);
170
m_switchingCurrentA
= switchingCurrentA;
171
}
172
173
174
WifiPhy::State
175
WifiRadioEnergyModel::GetCurrentState
(
void
)
const
176
{
177
return
m_currentState
;
178
}
179
180
void
181
WifiRadioEnergyModel::SetEnergyDepletionCallback
(
182
WifiRadioEnergyDepletionCallback
callback)
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
193
WifiRadioEnergyModel::ChangeState
(
int
newState)
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
228
m_lastUpdateTime
=
Simulator::Now
();
229
230
// notify energy source
231
m_source
->
UpdateEnergySource
();
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
242
WifiRadioEnergyModel::HandleEnergyDepletion
(
void
)
243
{
244
NS_LOG_DEBUG
(
"WifiRadioEnergyModel:Energy is depleted!"
);
245
// invoke energy depletion callback, if set.
246
if
(!
m_energyDepletionCallback
.
IsNull
())
247
{
248
m_energyDepletionCallback
();
249
}
250
}
251
252
WifiRadioEnergyModelPhyListener
*
253
WifiRadioEnergyModel::GetPhyListener
(
void
)
254
{
255
return
m_listener
;
256
}
257
258
/*
259
* Private functions start here.
260
*/
261
262
void
263
WifiRadioEnergyModel::DoDispose
(
void
)
264
{
265
m_source
= NULL;
266
m_energyDepletionCallback
.
Nullify
();
267
}
268
269
double
270
WifiRadioEnergyModel::DoGetCurrentA
(
void
)
const
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
290
WifiRadioEnergyModel::SetWifiRadioState
(
const
WifiPhy::State
state)
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
319
WifiRadioEnergyModelPhyListener::WifiRadioEnergyModelPhyListener
()
320
{
321
m_changeStateCallback
.
Nullify
();
322
}
323
324
WifiRadioEnergyModelPhyListener::~WifiRadioEnergyModelPhyListener
()
325
{
326
}
327
328
void
329
WifiRadioEnergyModelPhyListener::SetChangeStateCallback
(
DeviceEnergyModel::ChangeStateCallback
callback)
330
{
331
NS_ASSERT
(!callback.
IsNull
());
332
m_changeStateCallback
= callback;
333
}
334
335
void
336
WifiRadioEnergyModelPhyListener::NotifyRxStart
(
Time
duration)
337
{
338
if
(
m_changeStateCallback
.
IsNull
())
339
{
340
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
341
}
342
m_changeStateCallback
(
WifiPhy::RX
);
343
m_switchToIdleEvent
.
Cancel
();
344
}
345
346
void
347
WifiRadioEnergyModelPhyListener::NotifyRxEndOk
(
void
)
348
{
349
if
(
m_changeStateCallback
.
IsNull
())
350
{
351
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
352
}
353
m_changeStateCallback
(
WifiPhy::IDLE
);
354
}
355
356
void
357
WifiRadioEnergyModelPhyListener::NotifyRxEndError
(
void
)
358
{
359
if
(
m_changeStateCallback
.
IsNull
())
360
{
361
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
362
}
363
m_changeStateCallback
(
WifiPhy::IDLE
);
364
}
365
366
void
367
WifiRadioEnergyModelPhyListener::NotifyTxStart
(
Time
duration)
368
{
369
if
(
m_changeStateCallback
.
IsNull
())
370
{
371
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
372
}
373
m_changeStateCallback
(
WifiPhy::TX
);
374
// schedule changing state back to IDLE after TX duration
375
m_switchToIdleEvent
.
Cancel
();
376
m_switchToIdleEvent
=
Simulator::Schedule
(duration, &
WifiRadioEnergyModelPhyListener::SwitchToIdle
,
this
);
377
}
378
379
void
380
WifiRadioEnergyModelPhyListener::NotifyMaybeCcaBusyStart
(
Time
duration)
381
{
382
if
(
m_changeStateCallback
.
IsNull
())
383
{
384
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
385
}
386
m_changeStateCallback
(
WifiPhy::CCA_BUSY
);
387
// schedule changing state back to IDLE after CCA_BUSY duration
388
m_switchToIdleEvent
.
Cancel
();
389
m_switchToIdleEvent
=
Simulator::Schedule
(duration, &
WifiRadioEnergyModelPhyListener::SwitchToIdle
,
this
);
390
}
391
392
void
393
WifiRadioEnergyModelPhyListener::NotifySwitchingStart
(
Time
duration)
394
{
395
if
(
m_changeStateCallback
.
IsNull
())
396
{
397
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
398
}
399
m_changeStateCallback
(
WifiPhy::SWITCHING
);
400
// schedule changing state back to IDLE after CCA_BUSY duration
401
m_switchToIdleEvent
.
Cancel
();
402
m_switchToIdleEvent
=
Simulator::Schedule
(duration, &
WifiRadioEnergyModelPhyListener::SwitchToIdle
,
this
);
403
}
404
405
/*
406
* Private function state here.
407
*/
408
409
void
410
WifiRadioEnergyModelPhyListener::SwitchToIdle
(
void
)
411
{
412
if
(
m_changeStateCallback
.
IsNull
())
413
{
414
NS_FATAL_ERROR
(
"WifiRadioEnergyModelPhyListener:Change state callback not set!"
);
415
}
416
m_changeStateCallback
(
WifiPhy::IDLE
);
417
}
418
419
}
// namespace ns3
src
energy
model
wifi-radio-energy-model.cc
Generated on Tue Oct 9 2012 16:45:37 for ns-3 by
1.8.1.2