telephonyserverplugins/simtsy/src/csimtimer.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 1997-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 // Rudimentary timer class that acts as an observer, which is used as a
       
    15 // way to reduce the time duration from outside the Sim.Tsy.
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 #include "csimtimer.h"
       
    24 #include "CSimPhone.h"
       
    25 
       
    26 
       
    27 const TInt KOneSecond=1000000;	// < Constant multipler to map integer duration to millisecond timer.
       
    28 
       
    29 /**
       
    30 Factory function used to create a timer object that can be signalled to
       
    31 reduce its time.
       
    32 
       
    33 @return A CSimTime object, which has the functionality to reduce the
       
    34         time duration being run.
       
    35 @param aPhone The object created by this function is an observer which
       
    36               will be regitered with this CSimPhone object's
       
    37               CSimReduceTimers subject.
       
    38 */
       
    39 CSimTimer* CSimTimer::NewL(CSimPhone* aPhone)
       
    40 	{
       
    41 	CSimTimer* self = new(ELeave) CSimTimer();
       
    42 	CleanupStack::PushL(self);
       
    43 	self->ConstructL(aPhone);
       
    44 	CleanupStack::Pop(self);
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 /**
       
    49 Second-phase construction of the timer.  Obtains the observer subject
       
    50 from the CSimPhone object to ensure updates are made relative to all
       
    51 timers.
       
    52 
       
    53 @param aPhone The new observer will be regitered with this CSimPhone
       
    54               object's CSimReduceTimers subject.
       
    55 */
       
    56 void CSimTimer::ConstructL(CSimPhone* aPhone)
       
    57 	{
       
    58 	iReduceTimersSubject = aPhone->GetReduceTimersSubject();
       
    59 	}
       
    60 
       
    61 /**
       
    62 Default, first-phase, constructor for the timer (and observer) object.
       
    63 */
       
    64 CSimTimer::CSimTimer()
       
    65 				: CActive(EPriorityStandard), iRegistered(EFalse)
       
    66 	{
       
    67 	CActiveScheduler::Add(this);
       
    68 	_LIT(KPanic,"CSimTimer::CSimTimer()");
       
    69 	__ASSERT_ALWAYS(iTimer.CreateLocal() == KErrNone, User::Panic(KPanic,KErrGeneral));
       
    70 	}
       
    71 
       
    72 /**
       
    73 Default destructor for CSimTimer.  Ensures the timer is unregistered as
       
    74 an observer from its subject and cancels any outstanding requests.
       
    75 */
       
    76 CSimTimer::~CSimTimer()
       
    77 	{
       
    78 	// Ensure that this observer is unregistered from its subject.
       
    79 	if (iRegistered)
       
    80 		{
       
    81 		TRAP_IGNORE(iReduceTimersSubject->DetachL(this));
       
    82 		}
       
    83 	Cancel();
       
    84 	iTimer.Close();
       
    85 	}
       
    86 
       
    87 /**
       
    88 Starts the timer.  Waits for aDuration time, passed as a variable to
       
    89 this function, before completing.
       
    90 
       
    91 @param aDuration The duration time this timer should wait before
       
    92                  completing.
       
    93 @param aCallback A pointer to the MTimerCallBack object which will be
       
    94                  notified when aDuration time passes.
       
    95 */
       
    96 void CSimTimer::Start(TInt aDuration, MTimerCallBack* aCallback)
       
    97 	{
       
    98 	Start(aDuration,aCallback,KErrNotFound);
       
    99 	}
       
   100 
       
   101 /**
       
   102 Starts the timer.  Waits for aDuration time, passed as a variable to
       
   103 this function, before completing.
       
   104 
       
   105 @param aDuration The duration time this timer should wait before
       
   106                  completing.
       
   107 @param aCallback A pointer to the MTimerCallBack object which will be
       
   108                  notified when aDuration time passes.
       
   109 @param aId An integer that will be passed to the aCallback object when
       
   110            the timer completes.  This can be used, by the component
       
   111            using this timer, to identify the purpose of the timer.
       
   112            This parameter is optional, thus, if not required
       
   113            Start(TInt, MTimerCallBack*) can be used.
       
   114 */
       
   115 void CSimTimer::Start(TInt aDuration, MTimerCallBack* aCallback,TInt aId)
       
   116 	{
       
   117 	iCallBackId=aId;
       
   118 	iCallback=aCallback;
       
   119 	iDuration = aDuration;
       
   120 	RegisterWithSubjectInCSimPhone();
       
   121 	iStartTime.UniversalTime();
       
   122 	iTimer.After(iStatus,aDuration*KOneSecond);
       
   123 	SetActive();
       
   124 	}
       
   125 
       
   126 /**
       
   127 Stops the timer events.
       
   128 */
       
   129 void CSimTimer::DoCancel()
       
   130 	{
       
   131 	iTimer.Cancel();
       
   132 	}
       
   133 
       
   134 /**
       
   135 Calls the MTimerCallBack::TimerCallBack function when the duration time
       
   136 given to Start() completes.
       
   137 */
       
   138 void CSimTimer::RunL()
       
   139 	{
       
   140 	iStartTime = 0;
       
   141 	UnRegisterFromSubjectInCSimPhone();
       
   142 	iCallback->TimerCallBack(iCallBackId);
       
   143 	}
       
   144 
       
   145 /**
       
   146 Indicates whether the timer is running or not.
       
   147 
       
   148 @return Etrue if the timer is running, Efalse otherwise.
       
   149 */
       
   150 TBool CSimTimer::Running() const
       
   151 	{
       
   152 	return IsActive();
       
   153 	}
       
   154 
       
   155 /**
       
   156 Function for obtaining the amount of time, in seconds, remaining on
       
   157 this timer.
       
   158 
       
   159 @return The number of remaining whole seconds on the duration of this
       
   160         timer.  This function can return error in very unlikely cases
       
   161         where time remaining is negative (KErrAbort), or if the number
       
   162         of elapsed seconds, from when the timer started, is too large
       
   163         for 32-bit integer (KErrOverflow).  KErrNotFound is returned if
       
   164         the timer is not running; e.g. if the timer has been cancelled.
       
   165 */
       
   166 TInt CSimTimer::GetRemainingSeconds() const
       
   167 	{
       
   168 	if (!IsActive())
       
   169 		{
       
   170 		return KErrNotFound;
       
   171 		}
       
   172 
       
   173 	TTime endAt;
       
   174 	endAt.UniversalTime();
       
   175 	TTimeIntervalSeconds secsInterval;
       
   176 
       
   177 	TInt err = endAt.SecondsFrom(iStartTime,secsInterval);
       
   178 	if (err != KErrNone)
       
   179 		{
       
   180 		return err;
       
   181 		}
       
   182 
       
   183 	TInt timeElapsed = secsInterval.Int();
       
   184 	if (timeElapsed < 0)
       
   185 		{
       
   186 		return KErrAbort;
       
   187 		}
       
   188 
       
   189 	return iDuration-timeElapsed;
       
   190 	}
       
   191 
       
   192 /**
       
   193 Reduces the remaining time duration, of this timer, by the specified
       
   194 number of seconds, which must be a number greater than or equal to the
       
   195 remaining time on this timer, otherwise deduction will not be made.
       
   196 
       
   197 Note: Although the CSimTimer object is registered on a call to Start,
       
   198 it may stay registered even if the timer isn't running.  This can
       
   199 happen when DoCancel() is called from outside this object.  In such a
       
   200 case, any further Update() calls made to this object are ignored, since
       
   201 Running() will be EFalse.
       
   202 
       
   203 @param aSecsOffTimer Number of seconds to deduct off the remaining
       
   204                      duration of this timer.  This must be a number of
       
   205                      seconds greater than that remaining on the timer,
       
   206                      otherwise this method has no effect.
       
   207 */
       
   208 void CSimTimer::Update(TInt aSecsOffTimer)
       
   209 	{
       
   210 	if (aSecsOffTimer < 0 || !IsActive())
       
   211 		{
       
   212 		return;
       
   213 		}
       
   214 
       
   215 	TInt remaining = GetRemainingSeconds();
       
   216 
       
   217 	// Ensure there is enough time remaining to deduct aSeconds.
       
   218 	// Also ensures that RemainingSeconds() does not return an error.
       
   219 	if (remaining >= aSecsOffTimer)
       
   220 		{
       
   221 		Cancel();
       
   222 		TInt newDelay = remaining - aSecsOffTimer;
       
   223 		iTimer.After(iStatus,newDelay*KOneSecond);
       
   224 		SetActive();
       
   225 		}
       
   226 	}
       
   227 	
       
   228 /**
       
   229 Function for obtaining information about the event assocated with the timer
       
   230 
       
   231 @return The timer event ID as defined in TTimerEventId in simtsy.h
       
   232 */
       
   233 TInt CSimTimer::GetTimerEventInfo() const
       
   234 	{
       
   235 	return iCallBackId;
       
   236 	}
       
   237 
       
   238 /**
       
   239 Registers observer to the subject obtained from the CSimPhone object,
       
   240 given during construction.
       
   241 */
       
   242 void CSimTimer::RegisterWithSubjectInCSimPhone()
       
   243 	{
       
   244 	if (!iRegistered)
       
   245 		{
       
   246 		TRAPD(err, iReduceTimersSubject->AttachL(this));
       
   247 		if (err == KErrNone)
       
   248 			{
       
   249 			iRegistered = ETrue;
       
   250 			}
       
   251 		}
       
   252 	}
       
   253 
       
   254 /**
       
   255 Unregisters observer from the subject obtained from the CSimPhone
       
   256 object, given during construction.
       
   257 */
       
   258 void CSimTimer::UnRegisterFromSubjectInCSimPhone()
       
   259 	{
       
   260 	if (iRegistered)
       
   261 		{
       
   262 		TRAPD(err, iReduceTimersSubject->DetachL(this));
       
   263 		if (err == KErrNone)
       
   264 			{
       
   265 			iRegistered = EFalse;
       
   266 			}
       
   267 		}
       
   268 	}