LEFT | RIGHT |
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. | 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
4 * | 4 * |
5 * This program is free software; you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 2 as |
7 * published by the Free Software Foundation; | 7 * published by the Free Software Foundation; |
8 * | 8 * |
9 * This program is distributed in the hope that it will be useful, | 9 * This program is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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 | 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 | 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 * | 17 * |
18 * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> | 18 * Author: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> |
19 */ | 19 */ |
20 | 20 |
21 #include "ns3/log.h" | 21 #include "ns3/log.h" |
22 #include "ns3/double.h" | 22 #include "ns3/double.h" |
23 #include "ns3/simulator.h" | 23 #include "ns3/simulator.h" |
24 #include "ns3/trace-source-accessor.h" | 24 #include "ns3/trace-source-accessor.h" |
| 25 #include "energy-source.h" |
25 #include "wifi-radio-energy-model.h" | 26 #include "wifi-radio-energy-model.h" |
26 | 27 |
27 NS_LOG_COMPONENT_DEFINE ("WifiRadioEnergyModel"); | 28 NS_LOG_COMPONENT_DEFINE ("WifiRadioEnergyModel"); |
28 | 29 |
29 namespace ns3 { | 30 namespace ns3 { |
30 | 31 |
31 NS_OBJECT_ENSURE_REGISTERED (WifiRadioEnergyModel); | 32 NS_OBJECT_ENSURE_REGISTERED (WifiRadioEnergyModel); |
32 | 33 |
33 TypeId | 34 TypeId |
34 WifiRadioEnergyModel::GetTypeId (void) | 35 WifiRadioEnergyModel::GetTypeId (void) |
35 { | 36 { |
36 static TypeId tid = TypeId ("ns3::WifiRadioEnergyModel") | 37 static TypeId tid = TypeId ("ns3::WifiRadioEnergyModel") |
37 .SetParent<DeviceEnergyModel> () | 38 .SetParent<DeviceEnergyModel> () |
38 .AddConstructor<WifiRadioEnergyModel> () | 39 .AddConstructor<WifiRadioEnergyModel> () |
39 .AddAttribute ("TxPowerW", | 40 .AddAttribute ("IdleCurrentA", |
40 "The radio Tx power in Watts.", | 41 "The default radio Idle current in Ampere.", |
41 DoubleValue (0.0435), // typical CC2420 value | 42 DoubleValue (0.000426), // idle mode = 426uA |
42 MakeDoubleAccessor (&WifiRadioEnergyModel::SetTxPowerW, | 43 MakeDoubleAccessor (&WifiRadioEnergyModel::SetIdleCurrentA, |
43 &WifiRadioEnergyModel::GetTxPowerW), | 44 &WifiRadioEnergyModel::GetIdleCurrentA), |
44 MakeDoubleChecker<double> ()) | 45 MakeDoubleChecker<double> ()) |
45 .AddAttribute ("RxPowerW", | 46 .AddAttribute ("CcaBusyCurrentA", |
46 "The radio Rx power in Watts.", | 47 "The default radio CCA Busy State current in Ampere.", |
47 DoubleValue (0.047), // typical CC2420 value | 48 DoubleValue (0.000426), // default to be the same as idle mo
de |
48 MakeDoubleAccessor (&WifiRadioEnergyModel::SetRxPowerW, | 49 MakeDoubleAccessor (&WifiRadioEnergyModel::SetCcaBusyCurrentA
, |
49 &WifiRadioEnergyModel::GetRxPowerW), | 50 &WifiRadioEnergyModel::GetCcaBusyCurrentA
), |
50 MakeDoubleChecker<double> ()) | 51 MakeDoubleChecker<double> ()) |
51 .AddAttribute ("IdlePowerW", | 52 .AddAttribute ("TxCurrentA", |
52 "The default radio Idle power in Watts.", | 53 "The radio Tx current in Ampere.", |
53 DoubleValue (0.001065), // typical CC2420 value | 54 DoubleValue (0.0174), // transmit at 0dBm = 17.4mA |
54 MakeDoubleAccessor (&WifiRadioEnergyModel::SetIdlePowerW, | 55 MakeDoubleAccessor (&WifiRadioEnergyModel::SetTxCurrentA, |
55 &WifiRadioEnergyModel::GetIdlePowerW), | 56 &WifiRadioEnergyModel::GetTxCurrentA), |
56 MakeDoubleChecker<double> ()) | 57 MakeDoubleChecker<double> ()) |
57 .AddAttribute ("SleepPowerW", | 58 .AddAttribute ("RxCurrentA", |
58 "The default radio Sleep power in Watts.", | 59 "The radio Rx current in Ampere.", |
59 DoubleValue (0.00005), // typical CC2420 value | 60 DoubleValue (0.0197), // receive mode = 19.7mA |
60 MakeDoubleAccessor (&WifiRadioEnergyModel::SetSleepPowerW, | 61 MakeDoubleAccessor (&WifiRadioEnergyModel::SetRxCurrentA, |
61 &WifiRadioEnergyModel::GetSleepPowerW), | 62 &WifiRadioEnergyModel::GetRxCurrentA), |
62 MakeDoubleChecker<double> ()) | 63 MakeDoubleChecker<double> ()) |
63 .AddAttribute ("PeriodicEnergyUpdateInterval", | 64 .AddAttribute ("SwitchingCurrentA", |
64 "Time between two consecutive periodic energy updates.", | 65 "The default radio Channel Switch current in Ampere.", |
65 TimeValue (Seconds (1.0)), | 66 DoubleValue (0.000426), // default to be the same as idle mo
de |
66 MakeTimeAccessor (&WifiRadioEnergyModel::SetEnergyUpdateInter
val, | 67 MakeDoubleAccessor (&WifiRadioEnergyModel::SetSwitchingCurren
tA, |
67 &WifiRadioEnergyModel::GetEnergyUpdateInter
val), | 68 &WifiRadioEnergyModel::GetSwitchingCurren
tA), |
68 MakeTimeChecker ()) | 69 MakeDoubleChecker<double> ()) |
69 .AddTraceSource ("TotalEnergyConsumption", | 70 .AddTraceSource ("TotalEnergyConsumption", |
70 "Total energy consumption of the radio device.", | 71 "Total energy consumption of the radio device.", |
71 MakeTraceSourceAccessor (&WifiRadioEnergyModel::m_totalEner
gyConsumption)) | 72 MakeTraceSourceAccessor (&WifiRadioEnergyModel::m_totalEner
gyConsumption)) |
72 ;· | 73 ;· |
73 return tid; | 74 return tid; |
74 } | 75 } |
75 | 76 |
76 WifiRadioEnergyModel::WifiRadioEnergyModel () | 77 WifiRadioEnergyModel::WifiRadioEnergyModel () |
77 { | 78 { |
78 NS_LOG_FUNCTION (this); | 79 NS_LOG_FUNCTION (this); |
79 m_currentState = IDLE; | 80 m_currentState = WifiPhy::IDLE; // initially IDLE |
80 m_lastUpdateTime = Seconds (0.0); | 81 m_lastUpdateTime = Seconds (0.0); |
81 m_energyDepletionCallback.Nullify (); | 82 m_energyDepletionCallback.Nullify (); |
82 /* | 83 m_source = NULL; |
83 * Start scheduling the periodic energy update event. WifiRadioEnergyModel | 84 // set callback for WifiPhy listener |
84 * is *not* aggregated to the node. Hence we have to schedule this in the | 85 m_listener = new WifiRadioEnergyModelPhyListener; |
85 * constructor instead of calling it in DoStart. | 86 m_listener->SetChangeStateCallback (MakeCallback (&DeviceEnergyModel::ChangeSt
ate, this)); |
86 */ | |
87 m_periodicEnergyUpdateEvent = Simulator::Schedule (Seconds (0.0), | |
88 &WifiRadioEnergyModel::DoCh
angeState, | |
89 this, m_currentState); | |
90 } | 87 } |
91 | 88 |
92 WifiRadioEnergyModel::~WifiRadioEnergyModel () | 89 WifiRadioEnergyModel::~WifiRadioEnergyModel () |
93 { | 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; |
94 } | 100 } |
95 | 101 |
96 double | 102 double |
97 WifiRadioEnergyModel::GetTotalEnergyConsumption (void) const | 103 WifiRadioEnergyModel::GetTotalEnergyConsumption (void) const |
98 { | 104 { |
99 NS_LOG_FUNCTION (this); | |
100 return m_totalEnergyConsumption; | 105 return m_totalEnergyConsumption; |
101 } | 106 } |
102 | 107 |
103 double | 108 double |
104 WifiRadioEnergyModel::GetTxPowerW (void) const | 109 WifiRadioEnergyModel::GetIdleCurrentA (void) const |
105 { | 110 { |
106 NS_LOG_FUNCTION (this); | 111 return m_idleCurrentA; |
107 return m_txPower; | 112 } |
108 } | 113 |
109 | 114 void |
110 void | 115 WifiRadioEnergyModel::SetIdleCurrentA (double idleCurrentA) |
111 WifiRadioEnergyModel::SetTxPowerW (double txPowerW) | 116 { |
112 { | 117 NS_LOG_FUNCTION (this << idleCurrentA); |
113 NS_LOG_FUNCTION (this << txPowerW); | 118 m_idleCurrentA = idleCurrentA; |
114 m_txPower = txPowerW; | 119 } |
115 } | 120 |
116 | 121 double |
117 double | 122 WifiRadioEnergyModel::GetCcaBusyCurrentA (void) const |
118 WifiRadioEnergyModel::GetRxPowerW (void) const | 123 { |
119 { | 124 return m_ccaBusyCurrentA; |
120 NS_LOG_FUNCTION (this); | 125 } |
121 return m_rxPower; | 126 |
122 } | 127 void |
123 | 128 WifiRadioEnergyModel::SetCcaBusyCurrentA (double CcaBusyCurrentA) |
124 void | 129 { |
125 WifiRadioEnergyModel::SetRxPowerW (double rxPowerW) | 130 NS_LOG_FUNCTION (this << CcaBusyCurrentA); |
126 { | 131 m_ccaBusyCurrentA = CcaBusyCurrentA; |
127 NS_LOG_FUNCTION (this << rxPowerW); | 132 } |
128 m_rxPower = rxPowerW; | 133 |
129 } | 134 double |
130 | 135 WifiRadioEnergyModel::GetTxCurrentA (void) const |
131 double | 136 { |
132 WifiRadioEnergyModel::GetIdlePowerW (void) const | 137 return m_txCurrentA; |
133 { | 138 } |
134 NS_LOG_FUNCTION (this); | 139 |
135 return m_idlePower; | 140 void |
136 } | 141 WifiRadioEnergyModel::SetTxCurrentA (double txCurrentA) |
137 | 142 { |
138 void | 143 NS_LOG_FUNCTION (this << txCurrentA); |
139 WifiRadioEnergyModel::SetIdlePowerW (double idlePowerW) | 144 m_txCurrentA = txCurrentA; |
140 { | 145 } |
141 NS_LOG_FUNCTION (this << idlePowerW); | 146 |
142 m_idlePower = idlePowerW; | 147 double |
143 } | 148 WifiRadioEnergyModel::GetRxCurrentA (void) const |
144 | 149 { |
145 double | 150 return m_rxCurrentA; |
146 WifiRadioEnergyModel::GetSleepPowerW (void) const | 151 } |
147 { | 152 |
148 NS_LOG_FUNCTION (this); | 153 void |
149 return m_sleepPower; | 154 WifiRadioEnergyModel::SetRxCurrentA (double rxCurrentA) |
150 } | 155 { |
151 | 156 NS_LOG_FUNCTION (this << rxCurrentA); |
152 void | 157 m_rxCurrentA = rxCurrentA; |
153 WifiRadioEnergyModel::SetSleepPowerW (double sleepPowerW) | 158 } |
154 { | 159 |
155 NS_LOG_FUNCTION (this << sleepPowerW); | 160 double |
156 m_sleepPower = sleepPowerW; | 161 WifiRadioEnergyModel::GetSwitchingCurrentA (void) const |
157 } | 162 { |
158 | 163 return m_switchingCurrentA; |
159 WifiRadioEnergyModel::WifiRadioState | 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 |
160 WifiRadioEnergyModel::GetCurrentState (void) const | 175 WifiRadioEnergyModel::GetCurrentState (void) const |
161 { | 176 { |
162 NS_LOG_FUNCTION (this); | |
163 return m_currentState; | 177 return m_currentState; |
164 } | 178 } |
165 | 179 |
166 void | 180 void |
167 WifiRadioEnergyModel::SetEnergyUpdateInterval (const Time interval) | |
168 { | |
169 NS_LOG_FUNCTION (this); | |
170 NS_ASSERT (interval.GetSeconds () > 0); | |
171 m_energyUpdateInterval = interval; | |
172 } | |
173 | |
174 Time | |
175 WifiRadioEnergyModel::GetEnergyUpdateInterval (void) const | |
176 { | |
177 NS_LOG_FUNCTION (this); | |
178 return m_energyUpdateInterval; | |
179 } | |
180 | |
181 void | |
182 WifiRadioEnergyModel::SetEnergyDepletionCallback ( | 181 WifiRadioEnergyModel::SetEnergyDepletionCallback ( |
183 WifiRadioEnergyDepletionCallback callback) | 182 WifiRadioEnergyDepletionCallback callback) |
184 { | 183 { |
185 NS_LOG_FUNCTION (this); | 184 NS_LOG_FUNCTION (this); |
186 if (callback.IsNull ()) | 185 if (callback.IsNull ()) |
187 { | 186 { |
188 NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback
!"); | 187 NS_LOG_DEBUG ("WifiRadioEnergyModel:Setting NULL energy depletion callback
!"); |
189 } | 188 } |
190 m_energyDepletionCallback = callback; | 189 m_energyDepletionCallback = callback; |
191 } | 190 } |
192 | 191 |
193 bool | 192 void |
194 WifiRadioEnergyModel::IsStateTransitionValid (const WifiRadioState destState) | 193 WifiRadioEnergyModel::ChangeState (int newState) |
195 { | 194 { |
196 NS_LOG_FUNCTION (this << destState); | 195 NS_LOG_FUNCTION (this << newState); |
197 bool returnValue = true; | 196 |
198 | 197 Time duration = Simulator::Now () - m_lastUpdateTime; |
199 /* | 198 NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid |
200 * This is a placeholder function to specify if some of the radio state | 199 |
201 * transitions are prohibited. For example, if transition TX -> RX is not | 200 // energy to decrease = current * voltage * time |
202 * allowed, and must go through IDLE as TX -> IDLE -> RX, then this method | 201 double energyToDecrease = 0.0; |
203 * can be used to raise an ns3 error. Initial state is m_currentState. | 202 double supplyVoltage = m_source->GetSupplyVoltage (); |
204 */ | 203 switch (m_currentState) |
205 | 204 { |
206 return returnValue; | 205 case WifiPhy::IDLE: |
| 206 energyToDecrease = duration.GetSeconds () * m_idleCurrentA * supplyVoltage
; |
| 207 break; |
| 208 case WifiPhy::CCA_BUSY: |
| 209 energyToDecrease = duration.GetSeconds () * m_ccaBusyCurrentA * supplyVolt
age; |
| 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 * supplyVo
ltage; |
| 219 break; |
| 220 default: |
| 221 NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state: " << m_curren
tState); |
| 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; |
207 } | 256 } |
208 | 257 |
209 /* | 258 /* |
210 * Private functions start here. | 259 * Private functions start here. |
211 */ | 260 */ |
212 | 261 |
213 void | 262 void |
214 WifiRadioEnergyModel::DoDispose (void) | 263 WifiRadioEnergyModel::DoDispose (void) |
215 { | 264 { |
216 NS_LOG_FUNCTION (this); | 265 m_source = NULL; |
217 BreakSourceRefCycle (); // break reference cycle to energy source | |
218 m_energyDepletionCallback.Nullify (); | 266 m_energyDepletionCallback.Nullify (); |
219 } | 267 } |
220 | 268 |
221 void | 269 double |
222 WifiRadioEnergyModel::DoHandleEnergyDepletion (void) | 270 WifiRadioEnergyModel::DoGetCurrentA (void) const |
223 { | 271 { |
224 NS_LOG_FUNCTION (this); | |
225 NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is depleted at Node #" | |
226 << GetNode ()->GetId ()); | |
227 // invoke energy depletion callback, if set. | |
228 if (!m_energyDepletionCallback.IsNull ()) | |
229 { | |
230 m_energyDepletionCallback (); | |
231 } | |
232 } | |
233 | |
234 void | |
235 WifiRadioEnergyModel::SetCurrentState (const WifiRadioState currentState) | |
236 { | |
237 NS_LOG_FUNCTION (this); | |
238 if (currentState != m_currentState) | |
239 { | |
240 m_currentState = currentState; | |
241 std::string stateName; | |
242 switch (currentState) | |
243 { | |
244 case TX: | |
245 stateName = "TX"; | |
246 break; | |
247 case RX: | |
248 stateName = "RX"; | |
249 break; | |
250 case IDLE: | |
251 stateName = "IDLE"; | |
252 break; | |
253 case SLEEP: | |
254 stateName = "SLEEP"; | |
255 break; | |
256 } | |
257 NS_LOG_DEBUG ("WifiRadioEnergyModel:Switching to state: " | |
258 << stateName << ", at Node #" << GetNode ()->GetId () | |
259 << ", at time = " << Simulator::Now ()); | |
260 } | |
261 } | |
262 | |
263 void | |
264 WifiRadioEnergyModel::DoChangeState (int newState) | |
265 { | |
266 NS_LOG_FUNCTION (this << newState); | |
267 NS_ASSERT (IsStateTransitionValid ((WifiRadioState) newState)); | |
268 | |
269 // Cancel the currently scheduled periodic event, if any | |
270 m_periodicEnergyUpdateEvent.Cancel (); | |
271 | |
272 Time duration = Simulator::Now () - m_lastUpdateTime; | |
273 NS_ASSERT (duration.GetNanoSeconds () >= 0); // check if duration is valid | |
274 | |
275 // update remaining energy accordingly | |
276 double energyToDecrease = 0.0; | |
277 switch (m_currentState) | 272 switch (m_currentState) |
278 { | 273 { |
279 case TX: | 274 case WifiPhy::IDLE: |
280 energyToDecrease = duration.GetSeconds () * m_txPower; | 275 return m_idleCurrentA; |
281 DecreaseRemainingEnergy (energyToDecrease); | 276 case WifiPhy::CCA_BUSY: |
282 break; | 277 return m_ccaBusyCurrentA; |
283 case RX: | 278 case WifiPhy::TX: |
284 energyToDecrease = duration.GetSeconds () * m_rxPower; | 279 return m_txCurrentA; |
285 DecreaseRemainingEnergy (energyToDecrease); | 280 case WifiPhy::RX: |
286 break; | 281 return m_rxCurrentA; |
287 case IDLE: | 282 case WifiPhy::SWITCHING: |
288 energyToDecrease = duration.GetSeconds () * m_idlePower; | 283 return m_switchingCurrentA; |
289 DecreaseRemainingEnergy (energyToDecrease); | 284 default: |
290 break; | 285 NS_FATAL_ERROR ("WifiRadioEnergyModel:Undefined radio state:" << m_current
State); |
291 case SLEEP: | 286 } |
292 energyToDecrease = duration.GetSeconds () * m_sleepPower; | 287 } |
293 DecreaseRemainingEnergy (energyToDecrease); | 288 |
294 break; | 289 void |
295 } | 290 WifiRadioEnergyModel::SetWifiRadioState (const WifiPhy::State state) |
296 | 291 { |
297 // update total energy consumption | 292 NS_LOG_FUNCTION (this << state); |
298 m_totalEnergyConsumption += energyToDecrease; | 293 m_currentState = state; |
299 | 294 std::string stateName; |
300 // update last update time stamp | 295 switch (state) |
301 m_lastUpdateTime = Simulator::Now (); | 296 { |
302 | 297 case WifiPhy::IDLE: |
303 // update current state & last update time stamp | 298 stateName = "IDLE"; |
304 SetCurrentState ((WifiRadioState) newState); | 299 break; |
305 | 300 case WifiPhy::CCA_BUSY: |
306 // Schedule the next periodic energy update event | 301 stateName = "CCA_BUSY"; |
307 m_periodicEnergyUpdateEvent = Simulator::Schedule (m_energyUpdateInterval, | 302 break; |
308 &WifiRadioEnergyModel::DoCh
angeState, | 303 case WifiPhy::TX: |
309 this, m_currentState); | 304 stateName = "TX"; |
310 | 305 break; |
311 // some debug message | 306 case WifiPhy::RX: |
312 NS_LOG_DEBUG ("WifiRadioEnergyModel:Total energy consumption at node #" | 307 stateName = "RX"; |
313 << GetNode ()->GetId () << " is " << m_totalEnergyConsumption | 308 break; |
314 << "J"); | 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::Chan
geStateCallback 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, &WifiRadioEnergyModelPhyL
istener::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, &WifiRadioEnergyModelPhyL
istener::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, &WifiRadioEnergyModelPhyL
istener::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); |
315 } | 417 } |
316 | 418 |
317 } // namespace ns3 | 419 } // namespace ns3 |
LEFT | RIGHT |