realtimenetprots/sipfw/SIP/LightWeightTimer/src/singletimer.cpp
changeset 0 307788aac0a8
child 55 36ea1f90a3d8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2005-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 // Name          : singletimer.cpp
       
    15 // Part of       : LightWeightTimer
       
    16 // Version       : SIP/4.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 #include "SipAssert.h"
       
    22 #include "singletimer.h"
       
    23 #include "TimerManager.h"
       
    24 #include "mtimernotifier.h"
       
    25 #include "timerlog.h"
       
    26 
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CSingleTimer::NewL
       
    30 // -----------------------------------------------------------------------------
       
    31 //
       
    32 CSingleTimer* CSingleTimer::NewL(MTimerNotifier& aObserver)
       
    33 	{
       
    34 	CSingleTimer* self = new (ELeave) CSingleTimer(aObserver);
       
    35 	CleanupStack::PushL(self);
       
    36 	self->ConstructL();
       
    37 	CleanupStack::Pop(self);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 // -----------------------------------------------------------------------------
       
    42 // CSingleTimer::CSingleTimer
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 CSingleTimer::CSingleTimer(MTimerNotifier& aObserver) :
       
    46 	CActive(EPriorityStandard),
       
    47 	iObserver(aObserver),
       
    48 	iTimerId(MTimerManager::KNoSuchTimer)
       
    49 	{
       
    50 	CActiveScheduler::Add(this);
       
    51 	}
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CSingleTimer::ConstructL
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CSingleTimer::ConstructL()
       
    58 	{
       
    59 	User::LeaveIfError(iTimer.CreateLocal());
       
    60 	}
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CSingleTimer::~CSingleTimer
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CSingleTimer::~CSingleTimer()
       
    67 	{	
       
    68 	Cancel();
       
    69 	iTimer.Close();
       
    70 	}
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CSingleTimer::TimerId
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 TTimerId CSingleTimer::TimerId() const
       
    77 	{
       
    78 	return iTimerId;
       
    79 	}
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CSingleTimer::SetAfter
       
    83 // If the equipment is switched off, timer set with After expires late.
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CSingleTimer::SetAfter(TUint32 aDelay, TTimerId aTimerId)
       
    87 	{	
       
    88     __SIP_ASSERT_RETURN(!IsActive(), KErrNotReady);
       
    89     __SIP_ASSERT_RETURN(aDelay <= KMaxTimerAfterDuration, KErrOverflow);
       
    90     __SIP_ASSERT_RETURN(aTimerId != CTimerManager::KNoSuchTimer, KErrArgument);
       
    91 
       
    92 #if defined(WRITE_TIMER_LOG)
       
    93 	__SIP_INT_LOG2( "LwTimer SetAfter (id,delay)", aTimerId, aDelay )
       
    94 #endif
       
    95 
       
    96 	iTimerId = aTimerId;
       
    97 
       
    98 	//If the duration is very short, expire immediately for better performance.
       
    99 #ifdef __WINSCW__
       
   100 	if (aDelay < KEmulatorDelay)
       
   101 #else
       
   102 	if (aDelay < KTargetHwDelay)
       
   103 #endif
       
   104 		{
       
   105 		ExpireImmediately();
       
   106 		}
       
   107 	else
       
   108 		{
       
   109 		iTimer.After(iStatus, aDelay * 1000);
       
   110 		SetActive();
       
   111 		}
       
   112 	}
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CSingleTimer::DoCancel
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void CSingleTimer::DoCancel()
       
   119 	{
       
   120 	iTimer.Cancel();
       
   121 	iTimerId = MTimerManager::KNoSuchTimer;
       
   122 	}
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CSingleTimer::RunL
       
   126 // Even in case of error, continue normally.
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CSingleTimer::RunL()
       
   130 	{
       
   131 #if defined(WRITE_TIMER_LOG)
       
   132 	if (iStatus.Int() != KErrNone)
       
   133 		{
       
   134 		__SIP_INT_LOG2( "LwTimer SingleTimer:RunL (status,id)", 
       
   135 		                iStatus.Int(), iTimerId )
       
   136 		}
       
   137 #endif
       
   138 
       
   139 	iObserver.TimerExpiredL(iTimerId);	
       
   140 	}
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CSingleTimer::RunError
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 TInt CSingleTimer::RunError(TInt aError)
       
   147 	{
       
   148 #if defined(WRITE_TIMER_LOG)
       
   149 	__SIP_INT_LOG1( "LwTimer SingleTimer:RunError error", aError )
       
   150 #endif
       
   151 
       
   152 	TInt err = iObserver.LeaveOccurred();
       
   153 
       
   154 	if (aError == KErrNoMemory || err == KErrNoMemory)
       
   155 		{
       
   156 		return KErrNoMemory;
       
   157 		}
       
   158 
       
   159 	return KErrNone;
       
   160 	}
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CSingleTimer::ExpireImmediately
       
   164 // Callback is not used from here, as it may lead to recursion if the callback
       
   165 // sets another timer with delay = 0.
       
   166 // CSingleTimer doesn't have a flag like CTimerManager::iNowExpiring.
       
   167 // -----------------------------------------------------------------------------
       
   168 //
       
   169 void CSingleTimer::ExpireImmediately()
       
   170 	{
       
   171 	//Initiate request
       
   172 	iStatus = KRequestPending;
       
   173 	SetActive();
       
   174 
       
   175 	//Complete request
       
   176 	TRequestStatus* status = &iStatus;
       
   177 	User::RequestComplete(status, KErrNone);
       
   178 	}