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"
32 NS_OBJECT_ENSURE_REGISTERED (WifiRadioEnergyModel);
35 WifiRadioEnergyModel::GetTypeId (
void)
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),
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),
49 MakeDoubleAccessor (&WifiRadioEnergyModel::SetCcaBusyCurrentA,
50 &WifiRadioEnergyModel::GetCcaBusyCurrentA),
51 MakeDoubleChecker<double> ())
52 .AddAttribute (
"TxCurrentA",
53 "The radio Tx current in Ampere.",
55 MakeDoubleAccessor (&WifiRadioEnergyModel::SetTxCurrentA,
56 &WifiRadioEnergyModel::GetTxCurrentA),
57 MakeDoubleChecker<double> ())
58 .AddAttribute (
"RxCurrentA",
59 "The radio Rx current in Ampere.",
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),
67 MakeDoubleAccessor (&WifiRadioEnergyModel::SetSwitchingCurrentA,
68 &WifiRadioEnergyModel::GetSwitchingCurrentA),
69 MakeDoubleChecker<double> ())
70 .AddTraceSource (
"TotalEnergyConsumption",
71 "Total energy consumption of the radio device.",
77 WifiRadioEnergyModel::WifiRadioEnergyModel ()
81 m_lastUpdateTime =
Seconds (0.0);
82 m_energyDepletionCallback.Nullify ();
85 m_listener =
new WifiRadioEnergyModelPhyListener;
89 WifiRadioEnergyModel::~WifiRadioEnergyModel ()
107 return m_totalEnergyConsumption;
111 WifiRadioEnergyModel::GetIdleCurrentA (
void)
const
114 return m_idleCurrentA;
118 WifiRadioEnergyModel::SetIdleCurrentA (
double idleCurrentA)
121 m_idleCurrentA = idleCurrentA;
125 WifiRadioEnergyModel::GetCcaBusyCurrentA (
void)
const
128 return m_ccaBusyCurrentA;
132 WifiRadioEnergyModel::SetCcaBusyCurrentA (
double CcaBusyCurrentA)
135 m_ccaBusyCurrentA = CcaBusyCurrentA;
139 WifiRadioEnergyModel::GetTxCurrentA (
void)
const
146 WifiRadioEnergyModel::SetTxCurrentA (
double txCurrentA)
149 m_txCurrentA = txCurrentA;
153 WifiRadioEnergyModel::GetRxCurrentA (
void)
const
160 WifiRadioEnergyModel::SetRxCurrentA (
double rxCurrentA)
163 m_rxCurrentA = rxCurrentA;
167 WifiRadioEnergyModel::GetSwitchingCurrentA (
void)
const
170 return m_switchingCurrentA;
174 WifiRadioEnergyModel::SetSwitchingCurrentA (
double switchingCurrentA)
177 m_switchingCurrentA = switchingCurrentA;
185 return m_currentState;
193 if (callback.IsNull ())
195 NS_LOG_DEBUG (
"WifiRadioEnergyModel:Setting NULL energy depletion callback!");
197 m_energyDepletionCallback = callback;
209 double energyToDecrease = 0.0;
210 double supplyVoltage = m_source->GetSupplyVoltage ();
211 switch (m_currentState)
214 energyToDecrease = duration.
GetSeconds () * m_idleCurrentA * supplyVoltage;
217 energyToDecrease = duration.
GetSeconds () * m_ccaBusyCurrentA * supplyVoltage;
220 energyToDecrease = duration.
GetSeconds () * m_txCurrentA * supplyVoltage;
223 energyToDecrease = duration.
GetSeconds () * m_rxCurrentA * supplyVoltage;
226 energyToDecrease = duration.
GetSeconds () * m_switchingCurrentA * supplyVoltage;
229 NS_FATAL_ERROR (
"WifiRadioEnergyModel:Undefined radio state: " << m_currentState);
233 m_totalEnergyConsumption += energyToDecrease;
239 m_source->UpdateEnergySource ();
245 NS_LOG_DEBUG (
"WifiRadioEnergyModel:Total energy consumption is " <<
246 m_totalEnergyConsumption <<
"J");
253 NS_LOG_DEBUG (
"WifiRadioEnergyModel:Energy is depleted!");
255 if (!m_energyDepletionCallback.IsNull ())
257 m_energyDepletionCallback ();
277 m_energyDepletionCallback.Nullify ();
284 switch (m_currentState)
287 return m_idleCurrentA;
289 return m_ccaBusyCurrentA;
295 return m_switchingCurrentA;
297 NS_FATAL_ERROR (
"WifiRadioEnergyModel:Undefined radio state:" << m_currentState);
305 m_currentState = state;
306 std::string stateName;
313 stateName =
"CCA_BUSY";
322 stateName =
"SWITCHING";
325 stateName =
"SENSING";
328 NS_LOG_DEBUG (
"WifiRadioEnergyModel:Switching to state: " << stateName <<
334 WifiRadioEnergyModelPhyListener::WifiRadioEnergyModelPhyListener ()
340 WifiRadioEnergyModelPhyListener::~WifiRadioEnergyModelPhyListener ()
359 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
362 m_switchToIdleEvent.
Cancel ();
371 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
382 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
393 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
397 m_switchToIdleEvent.
Cancel ();
407 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
411 m_switchToIdleEvent.
Cancel ();
421 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
425 m_switchToIdleEvent.
Cancel ();
435 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
439 m_switchToIdleEvent.
Cancel ();
453 NS_FATAL_ERROR (
"WifiRadioEnergyModelPhyListener:Change state callback not set!");
virtual void NotifySwitchingStart(Time duration, uint16_t toChannel)
void SetWifiRadioState(const WifiPhy::State state)
smart pointer class similar to boost::intrusive_ptr
#define NS_LOG_FUNCTION(parameters)
#define NS_ASSERT(condition)
virtual void NotifySensingStart(Time duration)
#define NS_LOG_COMPONENT_DEFINE(name)
virtual void ChangeState(int newState)
Changes state of the WifiRadioEnergyMode.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
virtual double GetTotalEnergyConsumption(void) const
virtual void NotifyTxStart(Time duration)
Switches the WifiRadioEnergyModel to TX state and switches back to IDLE after TX duration.
WifiRadioEnergyModelPhyListener * GetPhyListener(void)
virtual void NotifyRxEndOk(void)
Switches the WifiRadioEnergyModel back to IDLE state.
#define NS_FATAL_ERROR(msg)
fatal error handling
double GetSeconds(void) const
void SetChangeStateCallback(DeviceEnergyModel::ChangeStateCallback callback)
Sets the change state callback. Used by helper class.
virtual void NotifyMaybeCcaBusyStart(Time duration)
virtual void NotifyRxEndError(void)
Switches the WifiRadioEnergyModel back to IDLE state.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
WifiPhy::State GetCurrentState(void) const
DeviceEnergyModel::ChangeStateCallback m_changeStateCallback
virtual void SetEnergySource(Ptr< EnergySource > source)
Sets pointer to EnergySouce installed on node.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
virtual void NotifyRxStart(Time duration)
Switches the WifiRadioEnergyModel to RX state.
int64_t GetNanoSeconds(void) const
void SetEnergyDepletionCallback(WifiRadioEnergyDepletionCallback callback)
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
#define NS_LOG_DEBUG(msg)
virtual void ChangeState(int newState)=0
virtual double DoGetCurrentA(void) const
virtual void HandleEnergyDepletion(void)
Handles energy depletion.