keepalive/flextimer/server/engine/src/flextimerengine.cpp
changeset 32 5c4486441ae6
equal deleted inserted replaced
31:c16e04725da3 32:5c4486441ae6
       
     1 /*
       
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - Initial contribution
       
    11  *
       
    12  * Description:
       
    13  * This class contains implementation of CFlexTimerEngine.
       
    14  *
       
    15  */
       
    16 
       
    17 // System include files
       
    18 // None
       
    19 
       
    20 // User include files
       
    21 #include "flextimerengine.h"
       
    22 #include "flextimercontainer.h"
       
    23 #include "flextimerwakeuptimer.h"
       
    24 #include "OstTraceDefinitions.h"
       
    25 #ifdef OST_TRACE_COMPILER_IN_USE
       
    26 #include "flextimerengineTraces.h"
       
    27 #endif
       
    28 
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // NewL
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 EXPORT_C CFlexTimerEngine* CFlexTimerEngine::NewL()
       
    35     {
       
    36     CFlexTimerEngine* self = new (ELeave) CFlexTimerEngine();
       
    37     CleanupStack::PushL( self );
       
    38     self->ConstructL();
       
    39     CleanupStack::Pop( self );
       
    40     return self;
       
    41     }
       
    42 // ---------------------------------------------------------------------------
       
    43 // Destructor
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CFlexTimerEngine::~CFlexTimerEngine()
       
    47     {
       
    48     OstTrace0( TRACE_INTERNAL,
       
    49         CFLEXTIMERENGINE_CFLEXTIMERENGINE,
       
    50         "CFlexTimerEngine::~CFlexTimerEngine" );
       
    51 
       
    52     delete iFlexTimerContainer;
       
    53     delete iWakeUpTimer;
       
    54     }
       
    55 // ---------------------------------------------------------------------------
       
    56 // Add new timer and trap leave. Return Leave code to session.
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C TInt CFlexTimerEngine::AddTimer(
       
    60     const TTimeIntervalMicroSeconds& aWinStartInterval,
       
    61     const TTimeIntervalMicroSeconds& aWinEndInterval,
       
    62     TBool aCancelAtSystemTimeChange,
       
    63     const MFlexTimerServiceCB* aFlexTimerServiceCB )
       
    64     {
       
    65     TRAPD( err,
       
    66         iFlexTimerContainer->AddTimerL( aWinStartInterval,
       
    67             aWinEndInterval,
       
    68             aCancelAtSystemTimeChange,
       
    69             aFlexTimerServiceCB ) );
       
    70 
       
    71     if ( KErrNone == err )
       
    72         {
       
    73         StartTimer();
       
    74         }
       
    75     return err;
       
    76     }
       
    77 // ---------------------------------------------------------------------------
       
    78 // Remove timer from container queue and start new wakeup watch timer
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 EXPORT_C TInt CFlexTimerEngine::CancelTimer(
       
    82     const MFlexTimerServiceCB* aFlexTimerServiceCB )
       
    83     {
       
    84     TInt error = iFlexTimerContainer->RemoveTimer( aFlexTimerServiceCB );
       
    85     StartTimer();
       
    86     
       
    87     return error;
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Wakeup timer expired, Fire timers according to some algorithm set and
       
    92 // restart wakeup timer.
       
    93 // ---------------------------------------------------------------------------
       
    94 //
       
    95 void CFlexTimerEngine::WakeUp()
       
    96     {
       
    97     OstTrace0( TRACE_INTERNAL,
       
    98         CFLEXTIMERENGINE_WAKEUP,
       
    99         "CFlexTimerEngine::WakeUp" );
       
   100     
       
   101     iFlexTimerContainer->FireTimers(
       
   102         CFlexTimerContainer::EFlexTimerAlgorithmLatestPossible );
       
   103 
       
   104     StartTimer();
       
   105     }
       
   106 // ---------------------------------------------------------------------------
       
   107 // System time is changed. Abort all timers that need to be aborted and
       
   108 // restart wakeup timer.
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 void CFlexTimerEngine::SystemTimeChanged()
       
   112     {
       
   113     OstTrace0( TRACE_INTERNAL,
       
   114         CFLEXTIMERENGINE_SYSTEMTIMECHANGED,
       
   115         "CFlexTimerEngine::SystemTimeChanged" );
       
   116     
       
   117     iFlexTimerContainer->AbortTimersDueToTimeChange( KErrAbort );
       
   118     StartTimer();
       
   119     }
       
   120 // ---------------------------------------------------------------------------
       
   121 // Private constructor
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 CFlexTimerEngine::CFlexTimerEngine() : iFlexTimerContainer( NULL ),
       
   125     iWakeUpTimer( NULL )
       
   126     {
       
   127     OstTrace0( TRACE_INTERNAL,
       
   128         DUP_CFLEXTIMERENGINE_CFLEXTIMERENGINE,
       
   129         "CFlexTimerEngine::CFlexTimerEngine" );
       
   130     //Nothing to do here
       
   131     }
       
   132 // ---------------------------------------------------------------------------
       
   133 // Private 2nd phase construction
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 void CFlexTimerEngine::ConstructL()
       
   137     {
       
   138     iFlexTimerContainer = CFlexTimerContainer::NewL();
       
   139     iWakeUpTimer = CFlexTimerWakeUpTimer::NewL( *this );
       
   140     }
       
   141 // ---------------------------------------------------------------------------
       
   142 // Stop timer just in case it is already running (e.g. after AddTimer())
       
   143 // and Start new wakeup timer according to current situation in container
       
   144 // queues If timeout window has passed fire timers right now.
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 void CFlexTimerEngine::StartTimer()
       
   148     {
       
   149     TTimeIntervalMicroSeconds nextTimeout;
       
   150 
       
   151     // Stop the timer, because, if there are no more pending timeouts, the
       
   152     // wake-up timer needs to be stopped. If timeouts are found, the timer is
       
   153     // is restarted below.
       
   154     iWakeUpTimer->StopTimer();
       
   155 
       
   156     if ( iFlexTimerContainer->GetNextTimeout( nextTimeout ) )
       
   157         {
       
   158         // If timeout is in the future, timer is started to wake up at that
       
   159         // moment.
       
   160         if ( nextTimeout > TTimeIntervalMicroSeconds( 0 ) )
       
   161             {
       
   162             iWakeUpTimer->StartTimer( nextTimeout );
       
   163             }
       
   164         // If timer is due now or in the past, Lets fire those right now.
       
   165         else
       
   166             {
       
   167             WakeUp();
       
   168             }
       
   169         }
       
   170     // Else no timers, so no need for timeouts. Server is propably about to be
       
   171     // deleted.
       
   172     }