src/contrib/energy/model/wifi-radio-energy-model.h
changeset 6699 d8909a1fd0ff
parent 6535 0a90ef2b29a9
equal deleted inserted replaced
6698:bc947fbfac80 6699:d8909a1fd0ff
    23 
    23 
    24 #include "ns3/device-energy-model.h"
    24 #include "ns3/device-energy-model.h"
    25 #include "ns3/nstime.h"
    25 #include "ns3/nstime.h"
    26 #include "ns3/event-id.h"
    26 #include "ns3/event-id.h"
    27 #include "ns3/traced-value.h"
    27 #include "ns3/traced-value.h"
       
    28 #include "ns3/wifi-phy.h"
    28 
    29 
    29 namespace ns3 {
    30 namespace ns3 {
       
    31 
       
    32 /**
       
    33  * A WifiPhy listener class for notifying the WifiRadioEnergyModel of Wifi radio
       
    34  * state change.
       
    35  */
       
    36 class WifiRadioEnergyModelPhyListener : public WifiPhyListener
       
    37 {
       
    38 public:
       
    39   WifiRadioEnergyModelPhyListener ();
       
    40   virtual ~WifiRadioEnergyModelPhyListener ();
       
    41 
       
    42   /**
       
    43    * \brief Sets the change state callback. Used by helper class.
       
    44    *
       
    45    * \param callback Change state callback.
       
    46    */
       
    47   void SetChangeStateCallback (DeviceEnergyModel::ChangeStateCallback callback);
       
    48 
       
    49   /**
       
    50    * \brief Switches the WifiRadioEnergyModel to RX state.
       
    51    *
       
    52    * \param duration the expected duration of the packet reception.
       
    53    *
       
    54    * Defined in ns3::WifiPhyListener
       
    55    */
       
    56   virtual void NotifyRxStart (Time duration);
       
    57 
       
    58   /**
       
    59    * \brief Switches the WifiRadioEnergyModel back to IDLE state.
       
    60    *
       
    61    * Defined in ns3::WifiPhyListener
       
    62    *
       
    63    * Note that for the WifiRadioEnergyModel, the behavior of the function is the
       
    64    * same as NotifyRxEndError.
       
    65    */
       
    66   virtual void NotifyRxEndOk (void);
       
    67 
       
    68   /**
       
    69    * \brief Switches the WifiRadioEnergyModel back to IDLE state.
       
    70    *
       
    71    * Defined in ns3::WifiPhyListener
       
    72    *
       
    73    * Note that for the WifiRadioEnergyModel, the behavior of the function is the
       
    74    * same as NotifyRxEndOk.
       
    75    */
       
    76   virtual void NotifyRxEndError (void);
       
    77 
       
    78   /**
       
    79    * \brief Switches the WifiRadioEnergyModel to TX state and switches back to
       
    80    * IDLE after TX duration.
       
    81    *
       
    82    * \param duration the expected transmission duration.
       
    83    *
       
    84    * Defined in ns3::WifiPhyListener
       
    85    */
       
    86   virtual void NotifyTxStart (Time duration);
       
    87 
       
    88   /**
       
    89    * \param duration the expected busy duration.
       
    90    *
       
    91    * Defined in ns3::WifiPhyListener
       
    92    */
       
    93   virtual void NotifyMaybeCcaBusyStart (Time duration);
       
    94 
       
    95   /**
       
    96    * \param duration the expected channel switching duration.
       
    97    *
       
    98    * Defined in ns3::WifiPhyListener
       
    99    */
       
   100   virtual void NotifySwitchingStart (Time duration);
       
   101 
       
   102 private:
       
   103   /**
       
   104    * A helper function that makes scheduling m_changeStateCallback possible.
       
   105    */
       
   106   void SwitchToIdle (void);
       
   107 
       
   108 private:
       
   109   /**
       
   110    * Change state callback used to notify the WifiRadioEnergyModel of a state
       
   111    * change.
       
   112    */
       
   113   DeviceEnergyModel::ChangeStateCallback m_changeStateCallback;
       
   114 
       
   115   EventId m_switchToIdleEvent;
       
   116 };
       
   117 
       
   118 // -------------------------------------------------------------------------- //
    30 
   119 
    31 /**
   120 /**
    32  * \brief A WiFi radio energy model.
   121  * \brief A WiFi radio energy model.
    33  * 
   122  * 
    34  * 4 states are defined for the radio: TX, RX, IDLE, SLEEP. Default state is
   123  * 4 states are defined for the radio: TX, RX, IDLE, SLEEP. Default state is
    52  */
   141  */
    53 class WifiRadioEnergyModel : public DeviceEnergyModel
   142 class WifiRadioEnergyModel : public DeviceEnergyModel
    54 {
   143 {
    55 public:
   144 public:
    56   /**
   145   /**
    57    * Wifi radio states.
       
    58    */
       
    59   enum WifiRadioState
       
    60   {
       
    61     /**
       
    62      * Radio is transmitting.
       
    63      */
       
    64     TX = 0,
       
    65     /**
       
    66      * Radio is receiving.
       
    67      */
       
    68     RX,
       
    69     /**
       
    70      * Radio is idling.
       
    71      */
       
    72     IDLE,
       
    73     /**
       
    74      * Radio is asleep.
       
    75      */
       
    76     SLEEP
       
    77   };
       
    78 
       
    79   /**
       
    80    * Callback type for energy depletion handling.
   146    * Callback type for energy depletion handling.
    81    */
   147    */
    82   typedef Callback<void> WifiRadioEnergyDepletionCallback;
   148   typedef Callback<void> WifiRadioEnergyDepletionCallback;
    83 
   149 
    84 public:
   150 public:
    85   static TypeId GetTypeId (void);
   151   static TypeId GetTypeId (void);
    86   WifiRadioEnergyModel ();
   152   WifiRadioEnergyModel ();
    87   virtual ~WifiRadioEnergyModel ();
   153   virtual ~WifiRadioEnergyModel ();
    88 
   154 
    89   /**
   155   /**
    90    * \brief Sets pointer to node.
       
    91    *
       
    92    * \param node Pointer to node.
       
    93    *
       
    94    * Implements DeviceEnergyModel::SetNode.
       
    95    */
       
    96   virtual void SetNode (Ptr<Node> node);
       
    97 
       
    98   /**
       
    99    * \brief Gets pointer to node.
       
   100    *
       
   101    * \returns Pointer to node.
       
   102    *
       
   103    * Implements DeviceEnergyModel::GetNode.
       
   104    */
       
   105   virtual Ptr<Node> GetNode (void) const;
       
   106 
       
   107   /**
       
   108    * \brief Sets pointer to EnergySouce installed on node.
   156    * \brief Sets pointer to EnergySouce installed on node.
   109    *
   157    *
   110    * \param source Pointer to EnergySource installed on node.
   158    * \param source Pointer to EnergySource installed on node.
   111    *
   159    *
   112    * Implements DeviceEnergyModel::SetEnergySource.
   160    * Implements DeviceEnergyModel::SetEnergySource.
   119    * Implements DeviceEnergyModel::GetTotalEnergyConsumption.
   167    * Implements DeviceEnergyModel::GetTotalEnergyConsumption.
   120    */
   168    */
   121   virtual double GetTotalEnergyConsumption (void) const;
   169   virtual double GetTotalEnergyConsumption (void) const;
   122 
   170 
   123   // Setter & getters for state power consumption.
   171   // Setter & getters for state power consumption.
       
   172   double GetIdleCurrentA (void) const;
       
   173   void SetIdleCurrentA (double idleCurrentA);
       
   174   double GetCcaBusyCurrentA (void) const;
       
   175   void SetCcaBusyCurrentA (double ccaBusyCurrentA);
   124   double GetTxCurrentA (void) const;
   176   double GetTxCurrentA (void) const;
   125   void SetTxCurrentA (double txCurrentA);
   177   void SetTxCurrentA (double txCurrentA);
   126   double GetRxCurrentA (void) const;
   178   double GetRxCurrentA (void) const;
   127   void SetRxCurrentA (double rxCurrentA);
   179   void SetRxCurrentA (double rxCurrentA);
   128   double GetIdleCurrentA (void) const;
   180   double GetSwitchingCurrentA (void) const;
   129   void SetIdleCurrentA (double idleCurrentA);
   181   void SetSwitchingCurrentA (double switchingCurrentA);
   130   double GetSleepCurrentA (void) const;
       
   131   void SetSleepCurrentA (double sleepCurrentA);
       
   132 
   182 
   133   /**
   183   /**
   134    * \returns Current state.
   184    * \returns Current state.
   135    */
   185    */
   136   WifiRadioState GetCurrentState (void) const;
   186   WifiPhy::State GetCurrentState (void) const;
   137 
   187 
   138   /**
   188   /**
   139    * \param callback Callback function.
   189    * \param callback Callback function.
   140    *
   190    *
   141    * Sets callback for energy depletion handling.
   191    * Sets callback for energy depletion handling.
   156    *
   206    *
   157    * Implements DeviceEnergyModel::HandleEnergyDepletion
   207    * Implements DeviceEnergyModel::HandleEnergyDepletion
   158    */
   208    */
   159   virtual void HandleEnergyDepletion (void);
   209   virtual void HandleEnergyDepletion (void);
   160 
   210 
       
   211   /**
       
   212    * \returns Pointer to the PHY listener.
       
   213    */
       
   214   WifiRadioEnergyModelPhyListener * GetPhyListener (void);
       
   215 
   161 
   216 
   162 private:
   217 private:
   163   void DoDispose (void);
   218   void DoDispose (void);
   164 
   219 
   165   /**
   220   /**
   166    * \returns Current draw of device, at current state.
   221    * \returns Current draw of device, at current state.
   167    *
   222    *
   168    * Implements DeviceEnergyModel::GetCurrentA.
   223    * Implements DeviceEnergyModel::GetCurrentA.
   169    */
   224    */
   170   virtual double DoGetCurrentA (void) const;
   225   virtual double DoGetCurrentA (void) const;
   171 
       
   172   /**
       
   173    * \param destState Radio state to switch to.
       
   174    * \return True if the transition is allowed.
       
   175    *
       
   176    * This function checks if a given radio state transition is allowed.
       
   177    */
       
   178   bool IsStateTransitionValid (const WifiRadioState destState);
       
   179 
   226 
   180   /**
   227   /**
   181    * \param currentState New state the radio device is currently in.
   228    * \param currentState New state the radio device is currently in.
   182    *
   229    *
   183    * Sets current state. This function is private so that only the energy model
   230    * Sets current state. This function is private so that only the energy model
   184    * can change its own state.
   231    * can change its own state.
   185    */
   232    */
   186   void SetWifiRadioState (const WifiRadioState state);
   233   void SetWifiRadioState (const WifiPhy::State state);
   187 
   234 
   188 private:
   235 private:
   189   Ptr<Node> m_node;
       
   190   Ptr<EnergySource> m_source;
   236   Ptr<EnergySource> m_source;
   191 
   237 
   192   // Member variables for current draw in different radio modes.
   238   // Member variables for current draw in different radio modes.
   193   double m_txCurrentA;
   239   double m_txCurrentA;
   194   double m_rxCurrentA;
   240   double m_rxCurrentA;
   195   double m_idleCurrentA;
   241   double m_idleCurrentA;
   196   double m_sleepCurrentA;
   242   double m_ccaBusyCurrentA;
       
   243   double m_switchingCurrentA;
   197 
   244 
   198   // This variable keeps track of the total energy consumed by this model.
   245   // This variable keeps track of the total energy consumed by this model.
   199   TracedValue<double> m_totalEnergyConsumption;
   246   TracedValue<double> m_totalEnergyConsumption;
   200 
   247 
   201   // State variables.
   248   // State variables.
   202   WifiRadioState m_currentState;  // current state the radio is in
   249   WifiPhy::State m_currentState;  // current state the radio is in
   203   Time m_lastUpdateTime;          // time stamp of previous energy update
   250   Time m_lastUpdateTime;          // time stamp of previous energy update
   204 
   251 
   205   // energy depletion callback
   252   // Energy depletion callback
   206   WifiRadioEnergyDepletionCallback m_energyDepletionCallback;
   253   WifiRadioEnergyDepletionCallback m_energyDepletionCallback;
       
   254 
       
   255   // WifiPhy listener
       
   256   WifiRadioEnergyModelPhyListener *m_listener;
   207 };
   257 };
   208 
   258 
   209 } // namespace ns3
   259 } // namespace ns3
   210 
   260 
   211 #endif /* WIFI_RADIO_ENERGY_MODEL_H */
   261 #endif /* WIFI_RADIO_ENERGY_MODEL_H */