src/contrib/energy/model/basic-radio-energy-model.h
changeset 6429 5ee11b58989d
equal deleted inserted replaced
6428:a3afed368709 6429:5ee11b58989d
       
     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 BASIC_RADIO_ENERGY_MODEL_H
       
    22 #define BASIC_RADIO_ENERGY_MODEL_H
       
    23 
       
    24 #include "radio-energy-model.h"
       
    25 #include "ns3/nstime.h"
       
    26 #include "ns3/event-id.h"
       
    27 #include "ns3/traced-value.h"
       
    28 
       
    29 namespace ns3 {
       
    30 
       
    31 /**
       
    32  * \brief a basic radio energy model
       
    33  *
       
    34  * This is a child class of the RadioEnergyModel class.
       
    35  * 
       
    36  * 4 states are defined for the radio: TX, RX, IDLE, SLEEP. Default state is
       
    37  * IDLE.
       
    38  * The different types of transactions that are defined are: 
       
    39  *  1. Tx: State goes from IDLE to TX, radio is in TX state for TX_duration,
       
    40  *     then state goes from TX to IDLE.
       
    41  *  2. Rx: State goes from IDLE to RX, radio is in RX state for RX_duration,
       
    42  *     then state goes from RX to IDLE.
       
    43  *  3. Go_to_Sleep: State goes from IDLE to SLEEP.
       
    44  *  4. End_of_Sleep: State goes from SLEEP to IDLE.
       
    45  * The class keeps track of what state the radio is currently in.
       
    46  *
       
    47  * Energy calculation: For each transaction, we see how much time the radio
       
    48  * spent in each state and calculate energy consumption accordingly.
       
    49  *
       
    50  * Energy update: Remaining energy =
       
    51  *                Previous remaining energy - Energy_transaction.
       
    52  *
       
    53  * Default values for power consumption are based on CC2420 radio chip, with
       
    54  * supply voltage as 2.5V and currents as 17.4 mA (TX), 18.8 mA (RX), 20 uA
       
    55  * (sleep) and 426 uA (idle).
       
    56  *
       
    57  * TODO change default values to wifi radio chip numbers.
       
    58  */
       
    59 class BasicRadioEnergyModel : public RadioEnergyModel
       
    60 {
       
    61 public:
       
    62   /// Callback type for energy depletion handling.
       
    63   typedef Callback<void> BasicEnergyDepletionCallback;
       
    64 
       
    65 public:
       
    66   static TypeId GetTypeId (void);
       
    67   BasicRadioEnergyModel ();
       
    68   virtual ~BasicRadioEnergyModel ();
       
    69 
       
    70   /**
       
    71    * For current state, the getter is public, but the setter method should be
       
    72    * private.
       
    73    */
       
    74   RadioState GetCurrentState (void) const;
       
    75 
       
    76   /**
       
    77    * \param interval Energy update interval.
       
    78    *
       
    79    * Setter for the energy update interval - time between two consecutive energy
       
    80    * updates.
       
    81    */
       
    82   void SetEnergyUpdateInterval (const Time interval);
       
    83 
       
    84   /**
       
    85    * Getter for the energy update interval - time between two consecutive energy
       
    86    * updates.
       
    87    */
       
    88   Time GetEnergyUpdateInterval (void) const;
       
    89 
       
    90   /**
       
    91    * \param callback Callback function.
       
    92    *
       
    93    * Sets callback for energy depletion handling.
       
    94    */
       
    95   void SetEnergyDepletionCallback (BasicEnergyDepletionCallback callback);
       
    96 
       
    97   /**
       
    98    * \param destState Radio state to switch to.
       
    99    * \return True if the transition is allowed.
       
   100    *
       
   101    * This function checks if a given radio state transition is allowed.
       
   102    */
       
   103   bool IsStateTransitionValid (const RadioState destState);
       
   104 
       
   105 
       
   106 private:
       
   107   void DoDispose (void);
       
   108 
       
   109   /// Handles energy depletion.
       
   110   void DoHandleEnergyDepletion (void);
       
   111 
       
   112   /// Implements GetTotalEnergyConsumption.
       
   113   double DoGetTotalEnergyConsumption (void) const;
       
   114 
       
   115   double DoGetTxPowerW (void) const;
       
   116   void DoSetTxPowerW (double txPowerW);
       
   117   double DoGetRxPowerW (void) const;
       
   118   void DoSetRxPowerW (double rxPowerW);
       
   119   double DoGetIdlePowerW (void) const;
       
   120   void DoSetIdlePowerW (double idlePowerW);
       
   121   double DoGetSleepPowerW (void) const;
       
   122   void DoSetSleepPowerW (double sleepPowerW);
       
   123 
       
   124   void DoUpdateRemainingEnergy (const RadioState destState);
       
   125 
       
   126   /**
       
   127    * \param currentState New state the radio device is currently in.
       
   128    *
       
   129    * Sets current state. This function is private so that only the energy model
       
   130    * can change its own state.
       
   131    */
       
   132   void SetCurrentState (const RadioState currentState);
       
   133 
       
   134 private:
       
   135   /*
       
   136    * Member variables for the power consumption in different radio modes.
       
   137    * Power = (supply voltage * current draw)
       
   138    */
       
   139   double m_txPower;
       
   140   double m_rxPower;
       
   141   double m_idlePower;
       
   142   double m_sleepPower;
       
   143 
       
   144   // This variable keeps track of the total energy consumed by this particular model.
       
   145   TracedValue<double> m_totalEnergyConsumption;
       
   146 
       
   147   // State variables.
       
   148   RadioState m_currentState;  // current state the radio is in
       
   149   Time m_lastUpdateTime;      // time stamp of previous energy update
       
   150   EventId m_periodicEnergyUpdateEvent;  // scheduled event to update remaining energy periodically
       
   151   Time m_energyUpdateInterval;  // Time between consecutive periodic energy updates.
       
   152 
       
   153   // energy depletion callback
       
   154   BasicEnergyDepletionCallback m_energyDepletionCallback;
       
   155 };
       
   156 
       
   157 } // namespace ns3
       
   158 
       
   159 #endif /* BASIC_RADIO_ENERGY_MODEL_H */