Index: src/wifi/model/wifi-radio-energy-model.cc |
=================================================================== |
--- a/src/wifi/model/wifi-radio-energy-model.cc |
+++ b/src/wifi/model/wifi-radio-energy-model.cc |
@@ -113,6 +113,9 @@ |
NS_LOG_FUNCTION (this << source); |
NS_ASSERT (source != NULL); |
m_source = source; |
+ m_switchToOffEvent.Cancel (); |
+ Time durationToOff = GetMaximumTimeInState (m_currentState); |
+ m_switchToOffEvent = Simulator::Schedule (durationToOff, &WifiRadioEnergyModel::ChangeState, this, WifiPhy::OFF); |
} |
double |
@@ -252,6 +255,38 @@ |
} |
} |
+Time |
+WifiRadioEnergyModel::GetMaximumTimeInState (int state) const |
+{ |
+ Time remainingTime; |
+ double remainingEnergy = m_source->GetRemainingEnergy(); |
+ double supplyVoltage = m_source->GetSupplyVoltage (); |
+ switch (state) |
+ { |
+ case WifiPhy::IDLE: |
+ remainingTime = NanoSeconds (1e9 * (remainingEnergy / (m_idleCurrentA * supplyVoltage))); |
+ break; |
+ case WifiPhy::CCA_BUSY: |
+ remainingTime = NanoSeconds (1e9 * (remainingEnergy / (m_ccaBusyCurrentA * supplyVoltage))); |
+ break; |
+ case WifiPhy::TX: |
+ remainingTime = NanoSeconds (1e9 * (remainingEnergy / (m_txCurrentA * supplyVoltage))); |
+ break; |
+ case WifiPhy::RX: |
+ remainingTime = NanoSeconds (1e9 * (remainingEnergy / (m_rxCurrentA * supplyVoltage))); |
+ break; |
+ case WifiPhy::SWITCHING: |
+ remainingTime = NanoSeconds (1e9 * (remainingEnergy / (m_switchingCurrentA * supplyVoltage))); |
+ break; |
+ case WifiPhy::SLEEP: |
+ remainingTime = NanoSeconds (1e9 * (remainingEnergy / (m_sleepCurrentA * supplyVoltage))); |
+ break; |
+ default: |
+ NS_FATAL_ERROR ("WifiRadioEnergyModel: undefined radio state " << state); |
Rediet
2018/01/11 08:34:34
It means that it'll fail if invoked in OFF mode?
S. Deronne
2018/01/13 16:03:01
Yes, we cannot return an infinite time.
|
+ } |
+ return remainingTime; |
+} |
+ |
void |
WifiRadioEnergyModel::ChangeState (int newState) |
{ |
@@ -259,6 +294,20 @@ |
m_nPendingChangeState++; |
+ if (m_nPendingChangeState > 1 && newState == WifiPhy::OFF) |
+ { |
+ SetWifiRadioState ((WifiPhy::State) newState); |
+ m_nPendingChangeState--; |
+ return; |
+ } |
+ |
+ if (newState != WifiPhy::OFF) |
+ { |
+ m_switchToOffEvent.Cancel (); |
+ Time durationToOff = GetMaximumTimeInState (newState); |
+ m_switchToOffEvent = Simulator::Schedule (durationToOff, &WifiRadioEnergyModel::ChangeState, this, WifiPhy::OFF); |
+ } |
+ |
Time duration = Simulator::Now () - m_lastUpdateTime; |
NS_ASSERT (duration.IsPositive ()); // check if duration is valid |
@@ -294,6 +343,7 @@ |
// update total energy consumption |
m_totalEnergyConsumption += energyToDecrease; |
+ NS_ASSERT (m_totalEnergyConsumption <= m_source->GetInitialEnergy ()); |
// update last update time stamp |
m_lastUpdateTime = Simulator::Now (); |
@@ -308,7 +358,7 @@ |
// by the previous instance is erroneously the final state stored in m_currentState. The check below |
// ensures that previous instances do not change m_currentState. |
- if (m_nPendingChangeState <= 1) |
+ if (m_nPendingChangeState <= 1 && m_currentState != WifiPhy::OFF) |
{ |
// update current state & last update time stamp |
SetWifiRadioState ((WifiPhy::State) newState); |
@@ -345,6 +395,19 @@ |
} |
} |
+void |
+WifiRadioEnergyModel::HandleEnergyChanged (void) |
+{ |
+ NS_LOG_FUNCTION (this); |
+ NS_LOG_DEBUG ("WifiRadioEnergyModel:Energy is changed!"); |
+ if (m_currentState != WifiPhy::OFF) |
+ { |
+ m_switchToOffEvent.Cancel (); |
+ Time durationToOff = GetMaximumTimeInState (m_currentState); |
+ m_switchToOffEvent = Simulator::Schedule (durationToOff, &WifiRadioEnergyModel::ChangeState, this, WifiPhy::OFF); |
+ } |
+} |
+ |
WifiRadioEnergyModelPhyListener * |
WifiRadioEnergyModel::GetPhyListener (void) |
{ |