resourcemgmt/hwresourcesmgr/server/src/HWRMGenericTimer.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include "HWRMGenericTimer.h"
       
    22 #include "HWRMtrace.h"
       
    23 
       
    24 // ============================ MEMBER FUNCTIONS ===============================
       
    25 
       
    26 // -----------------------------------------------------------------------------
       
    27 // CHWRMGenericTimer::NewL
       
    28 // Two-phased constructor.
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CHWRMGenericTimer* CHWRMGenericTimer::NewL(MHWRMGenericTimerCallback& aCallback,
       
    32                                        const TTimeIntervalMicroSeconds32& aMaxTime,
       
    33                                        TInt aTimerId)
       
    34     {
       
    35     COMPONENT_TRACE4(_L( "HWRM Server - CHWRMGenericTimer::NewL(0x%x, 0x%x, 0x%x)" ), &aCallback, aMaxTime.Int(), aTimerId );
       
    36 
       
    37     CHWRMGenericTimer* self = new (ELeave) CHWRMGenericTimer(aCallback, aMaxTime, aTimerId);
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop(self);
       
    41 
       
    42     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMGenericTimer::NewL - return 0x%x" ), self );
       
    43 
       
    44     return self;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CHWRMGenericTimer::CHWRMGenericTimer
       
    49 // Constructor with callback class as a parameter.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CHWRMGenericTimer::CHWRMGenericTimer(MHWRMGenericTimerCallback& aCallback, 
       
    53                                  const TTimeIntervalMicroSeconds32& aMaxTime,
       
    54                                  TInt aTimerId)
       
    55                             : CTimer(EPriorityHigh), 
       
    56                              iCallback(aCallback),
       
    57                              iMaximumTime(aMaxTime),
       
    58                              iCutOff(EFalse),
       
    59                              iTimerId(aTimerId)
       
    60     {
       
    61     COMPONENT_TRACE3(_L( "HWRM Server - CHWRMGenericTimer::CHWRMGenericTimer(0x%x, 0x%x)" ), &aCallback, aMaxTime.Int() );
       
    62 
       
    63     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMGenericTimer::CHWRMGenericTimer - return" ) );
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CHWRMGenericTimer::~CHWRMGenericTimer
       
    68 // Destructor.
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CHWRMGenericTimer::~CHWRMGenericTimer()
       
    72     {
       
    73     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMGenericTimer::~CHWRMGenericTimer()" ) );
       
    74     
       
    75     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMGenericTimer::~CHWRMGenericTimer - return" ) );
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CHWRMGenericTimer::Set
       
    80 // Start the timer to complete after the specified number of microseconds.
       
    81 // If the duration is zero, then timer is set to predefined maximum value.
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 void CHWRMGenericTimer::Set(const TTimeIntervalMicroSeconds32& aInterval)
       
    85     {
       
    86     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMGenericTimer::Set(%d)" ), aInterval.Int() );
       
    87 
       
    88     __ASSERT_ALWAYS(CActiveScheduler::Current()!= NULL, User::Invariant());
       
    89     
       
    90     if (!IsAdded())
       
    91         {
       
    92         CActiveScheduler::Add(this);
       
    93         }
       
    94     
       
    95     // If the timer is already running, cancel it... 
       
    96     if (IsActive())
       
    97         {
       
    98         Cancel();
       
    99         }
       
   100         
       
   101     // And set the new timer. If timer has specified maximum time, enforce that.
       
   102     TTime now;
       
   103     now.UniversalTime();
       
   104     
       
   105     if ( iMaximumTime.Int() != 0 && (0 == aInterval.Int() || aInterval > iMaximumTime) ) 
       
   106         {
       
   107         // Maximum time has been specified, and interval parameter is such that 
       
   108         // it should be enforced
       
   109         iActivationTime = now + iMaximumTime;
       
   110        
       
   111         After(iMaximumTime);
       
   112         
       
   113         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMGenericTimer::Set - Using Maximum time %d" ), iMaximumTime.Int() );
       
   114         iCutOff = ETrue;
       
   115         }
       
   116     else if ( iMaximumTime.Int() == 0 && aInterval.Int() == 0 )
       
   117         {
       
   118         // No maximum time specified and interval calls for infinite duration.
       
   119         // --> Do not set the timer at all.
       
   120         }
       
   121     else
       
   122         {    
       
   123         // Otherwise just set the time to specified interval.
       
   124         iActivationTime = now + aInterval;
       
   125         After(aInterval);
       
   126         iCutOff = EFalse;
       
   127         }
       
   128 
       
   129     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMGenericTimer::Set - return" ) );
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CHWRMGenericTimer::Freeze
       
   134 // Stop timer and returns the remaining time
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 TTimeIntervalMicroSeconds32 CHWRMGenericTimer::Freeze()
       
   138     {
       
   139     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMGenericTimer::Freeze()" ));
       
   140     
       
   141     // Must use 64 bit version as no way to get TTimeIntervalMicroSeconds32 from TTime.
       
   142     TTimeIntervalMicroSeconds frozenTime = 0LL;
       
   143     
       
   144     if ( IsAdded() && IsActive() )
       
   145         {
       
   146         Cancel();
       
   147 
       
   148         TTime now;
       
   149         now.UniversalTime();
       
   150         frozenTime = iActivationTime.MicroSecondsFrom(now);
       
   151         if ( frozenTime.Int64() <= 0 )
       
   152             {
       
   153             // Should have activated by now, set freeze timer to 1 for instant activation
       
   154             // after unfreeze.
       
   155             frozenTime = 1LL;
       
   156             }
       
   157         }
       
   158         
       
   159     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMGenericTimer::Freeze - return %d" ), frozenTime.Int64() );    
       
   160 
       
   161     // Loss of precision ok, as frozentime never exceeds 32 bits.    
       
   162     return frozenTime.Int64();
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CHWRMGenericTimer::RunL
       
   167 // RunL() function will be run after the specified system time expires,
       
   168 // i.e. time is set by After() method,
       
   169 // -----------------------------------------------------------------------------
       
   170 void CHWRMGenericTimer::RunL()
       
   171     {
       
   172     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMGenericTimer::RunL()" ));
       
   173 
       
   174     iCallback.GenericTimerFired(iTimerId, iCutOff);
       
   175 
       
   176     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMGenericTimer::RunL - return" ) );
       
   177     }
       
   178 
       
   179 void CHWRMGenericTimer::SetMaximumTime(TTimeIntervalMicroSeconds32& aMaximumTime)
       
   180 	{
       
   181 	iMaximumTime = aMaximumTime;
       
   182 	}
       
   183 
       
   184 
       
   185 //  End of File