src/energy/model/wifi-radio-energy-model.h
changeset 6938 85d201f1c67f
parent 6699 d8909a1fd0ff
child 7238 85a7e87bb4cc
equal deleted inserted replaced
6937:99ee62eec34c 6938:85d201f1c67f
       
     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  * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu>
       
    19  */
       
    20 
       
    21 #ifndef WIFI_RADIO_ENERGY_MODEL_H
       
    22 #define WIFI_RADIO_ENERGY_MODEL_H
       
    23 
       
    24 #include "ns3/device-energy-model.h"
       
    25 #include "ns3/nstime.h"
       
    26 #include "ns3/event-id.h"
       
    27 #include "ns3/traced-value.h"
       
    28 #include "ns3/wifi-phy.h"
       
    29 
       
    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 // -------------------------------------------------------------------------- //
       
   119 
       
   120 /**
       
   121  * \brief A WiFi radio energy model.
       
   122  * 
       
   123  * 4 states are defined for the radio: TX, RX, IDLE, SLEEP. Default state is
       
   124  * IDLE.
       
   125  * The different types of transactions that are defined are: 
       
   126  *  1. Tx: State goes from IDLE to TX, radio is in TX state for TX_duration,
       
   127  *     then state goes from TX to IDLE.
       
   128  *  2. Rx: State goes from IDLE to RX, radio is in RX state for RX_duration,
       
   129  *     then state goes from RX to IDLE.
       
   130  *  3. Go_to_Sleep: State goes from IDLE to SLEEP.
       
   131  *  4. End_of_Sleep: State goes from SLEEP to IDLE.
       
   132  * The class keeps track of what state the radio is currently in.
       
   133  *
       
   134  * Energy calculation: For each transaction, this model notifies EnergySource
       
   135  * object. The EnergySource object will query this model for the total current.
       
   136  * Then the EnergySource object uses the total current to calculate energy.
       
   137  *
       
   138  * Default values for power consumption are based on CC2420 radio chip, with
       
   139  * supply voltage as 2.5V and currents as 17.4 mA (TX), 18.8 mA (RX), 20 uA
       
   140  * (sleep) and 426 uA (idle).
       
   141  */
       
   142 class WifiRadioEnergyModel : public DeviceEnergyModel
       
   143 {
       
   144 public:
       
   145   /**
       
   146    * Callback type for energy depletion handling.
       
   147    */
       
   148   typedef Callback<void> WifiRadioEnergyDepletionCallback;
       
   149 
       
   150 public:
       
   151   static TypeId GetTypeId (void);
       
   152   WifiRadioEnergyModel ();
       
   153   virtual ~WifiRadioEnergyModel ();
       
   154 
       
   155   /**
       
   156    * \brief Sets pointer to EnergySouce installed on node.
       
   157    *
       
   158    * \param source Pointer to EnergySource installed on node.
       
   159    *
       
   160    * Implements DeviceEnergyModel::SetEnergySource.
       
   161    */
       
   162   virtual void SetEnergySource (Ptr<EnergySource> source);
       
   163 
       
   164   /**
       
   165    * \returns Total energy consumption of the wifi device.
       
   166    *
       
   167    * Implements DeviceEnergyModel::GetTotalEnergyConsumption.
       
   168    */
       
   169   virtual double GetTotalEnergyConsumption (void) const;
       
   170 
       
   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);
       
   176   double GetTxCurrentA (void) const;
       
   177   void SetTxCurrentA (double txCurrentA);
       
   178   double GetRxCurrentA (void) const;
       
   179   void SetRxCurrentA (double rxCurrentA);
       
   180   double GetSwitchingCurrentA (void) const;
       
   181   void SetSwitchingCurrentA (double switchingCurrentA);
       
   182 
       
   183   /**
       
   184    * \returns Current state.
       
   185    */
       
   186   WifiPhy::State GetCurrentState (void) const;
       
   187 
       
   188   /**
       
   189    * \param callback Callback function.
       
   190    *
       
   191    * Sets callback for energy depletion handling.
       
   192    */
       
   193   void SetEnergyDepletionCallback (WifiRadioEnergyDepletionCallback callback);
       
   194 
       
   195   /**
       
   196    * \brief Changes state of the WifiRadioEnergyMode.
       
   197    *
       
   198    * \param newState New state the wifi radio is in.
       
   199    *
       
   200    * Implements DeviceEnergyModel::ChangeState.
       
   201    */
       
   202   virtual void ChangeState (int newState);
       
   203 
       
   204   /**
       
   205    * \brief Handles energy depletion.
       
   206    *
       
   207    * Implements DeviceEnergyModel::HandleEnergyDepletion
       
   208    */
       
   209   virtual void HandleEnergyDepletion (void);
       
   210 
       
   211   /**
       
   212    * \returns Pointer to the PHY listener.
       
   213    */
       
   214   WifiRadioEnergyModelPhyListener * GetPhyListener (void);
       
   215 
       
   216 
       
   217 private:
       
   218   void DoDispose (void);
       
   219 
       
   220   /**
       
   221    * \returns Current draw of device, at current state.
       
   222    *
       
   223    * Implements DeviceEnergyModel::GetCurrentA.
       
   224    */
       
   225   virtual double DoGetCurrentA (void) const;
       
   226 
       
   227   /**
       
   228    * \param currentState New state the radio device is currently in.
       
   229    *
       
   230    * Sets current state. This function is private so that only the energy model
       
   231    * can change its own state.
       
   232    */
       
   233   void SetWifiRadioState (const WifiPhy::State state);
       
   234 
       
   235 private:
       
   236   Ptr<EnergySource> m_source;
       
   237 
       
   238   // Member variables for current draw in different radio modes.
       
   239   double m_txCurrentA;
       
   240   double m_rxCurrentA;
       
   241   double m_idleCurrentA;
       
   242   double m_ccaBusyCurrentA;
       
   243   double m_switchingCurrentA;
       
   244 
       
   245   // This variable keeps track of the total energy consumed by this model.
       
   246   TracedValue<double> m_totalEnergyConsumption;
       
   247 
       
   248   // State variables.
       
   249   WifiPhy::State m_currentState;  // current state the radio is in
       
   250   Time m_lastUpdateTime;          // time stamp of previous energy update
       
   251 
       
   252   // Energy depletion callback
       
   253   WifiRadioEnergyDepletionCallback m_energyDepletionCallback;
       
   254 
       
   255   // WifiPhy listener
       
   256   WifiRadioEnergyModelPhyListener *m_listener;
       
   257 };
       
   258 
       
   259 } // namespace ns3
       
   260 
       
   261 #endif /* WIFI_RADIO_ENERGY_MODEL_H */