multimediacommsengine/mmcesrv/mmceserver/src/mcetimermanager.cpp
changeset 0 1bce908db942
equal deleted inserted replaced
-1:000000000000 0:1bce908db942
       
     1 /*
       
     2 * Copyright (c) 2006 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 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "mcetimermanager.h"
       
    22 #include "mcesrvlogs.h"
       
    23 #include "mcesip.h"
       
    24 
       
    25 // Granularity for timer entry array iEntries
       
    26 const TInt KArrayGranularity( 8);
       
    27 
       
    28 // One millisecond as microseconds
       
    29 const TInt KOneMillisAsMicros( 1000);
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CMceTimerManager::NewL
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 
       
    36 CMceTimerManager* CMceTimerManager::NewL()
       
    37     {
       
    38     CMceTimerManager* self = CMceTimerManager::NewLC();
       
    39     CleanupStack::Pop(self);
       
    40     return self;
       
    41     }
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CMceTimerManager::NewLC
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CMceTimerManager* CMceTimerManager::NewLC()
       
    48     {
       
    49     CMceTimerManager* self = new( ELeave ) CMceTimerManager;
       
    50     CleanupStack::PushL(self);
       
    51     self->ConstructL();
       
    52     return self;
       
    53     }
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CMceTimerManager::~CMceTimerManager
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CMceTimerManager::~CMceTimerManager()
       
    60     {
       
    61     if(iTimerContainer)
       
    62     	{
       
    63     	iTimerContainer->Cancel();
       
    64     	delete iTimerContainer;
       
    65     	}
       
    66 	iEntries.ResetAndDestroy();
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CMceTimerManager::StartL
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 TMceTimerId CMceTimerManager::StartL(MMCEExpirationHandler* aObserver,
       
    74         TUint aMilliseconds)
       
    75     {
       
    76     MCESRV_DEBUG("2 CMceTimerManager::StartL, Entry");
       
    77     return StartL(aObserver, aMilliseconds, NULL);
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CMceTimerManager::StartL
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 TMceTimerId CMceTimerManager::StartL(MMCEExpirationHandler* aObserver,
       
    85         TUint aMilliseconds, TAny* aTimerParam)
       
    86     {
       
    87     MCESRV_DEBUG("CMceTimerManager::StartL, Entry");
       
    88     // Do not accept a NULL observer
       
    89     if ( !aObserver)
       
    90         {
       
    91         User::Leave(KErrArgument);
       
    92         }
       
    93 
       
    94     // Get new unique timer id
       
    95 
       
    96     TMceTimerId newTimerId = NewTimerId();
       
    97     MCESRV_DEBUG_DVALUES( "timer", newTimerId, "timeout", aMilliseconds );
       
    98 
       
    99     // Define a new entry with the new id
       
   100     TEntry *newEntry = new ( ELeave ) TEntry();
       
   101     CleanupStack::PushL( newEntry );
       
   102     
       
   103     newEntry->iId = newTimerId;
       
   104     newEntry->iObserver = aObserver;
       
   105     newEntry->iObserverParam = aTimerParam;
       
   106     newEntry->iCallBack = TCallBack( TimerExpiredCallBack, newEntry );
       
   107     newEntry->iEntry = TDeltaTimerEntry( newEntry->iCallBack );
       
   108     newEntry->iTimerManager = this;
       
   109 
       
   110     iEntries.InsertL(newEntry, 0);
       
   111     CleanupStack::Pop( newEntry );
       
   112     
       
   113     TTimeIntervalMicroSeconds interval( TInt64( aMilliseconds ) * KOneMillisAsMicros );
       
   114 
       
   115     if (KErrNone != iTimerContainer->QueueLong( interval, newEntry->iEntry ) )
       
   116         {
       
   117         MCESRV_DEBUG("CMceTimerManager:: CDeltaTimer::QueueLong Error!!!");
       
   118         return CMceTimerManager::KNoSuchTimer;
       
   119         }
       
   120 
       
   121     MCESRV_DEBUG("CMceTimerManager::StartL, Exit");
       
   122     return newEntry->iId;
       
   123 
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CMceTimerManager::Stop
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 TInt CMceTimerManager::Stop(TMceTimerId aTimerId)
       
   131     {
       
   132     MCESRV_DEBUG("CMceTimerManager::Stop, Entry");
       
   133     MCESRV_DEBUG_DVALUE("timer", aTimerId );
       
   134 
       
   135     // Find the timer entry to be stopped
       
   136     TInt entryIndex = FindEntry(aTimerId);
       
   137     if (entryIndex == KErrNotFound)
       
   138         {
       
   139         MCESRV_DEBUG("CMceTimerManager::Stop - Invalid TimerId, timer not found");
       
   140         return KErrNotFound;
       
   141         }
       
   142 
       
   143     // Remove timer information from container and delete entry
       
   144     TEntry* entry = iEntries[entryIndex];
       
   145     iTimerContainer->Remove(entry->iEntry);
       
   146     iEntries.Remove(entryIndex);
       
   147     delete entry;
       
   148     entry = NULL;
       
   149 
       
   150     MCESRV_DEBUG("CMceTimerManager::Stop, Exit");
       
   151     return KErrNone;
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CMceTimerManager::IsRunning
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 TBool CMceTimerManager::IsRunning(TMceTimerId aTimerId) const
       
   159     {
       
   160 
       
   161     TInt entryIndex = FindEntry(aTimerId);
       
   162     if (entryIndex != KErrNotFound)
       
   163         {
       
   164         return ETrue;
       
   165         }
       
   166     return EFalse;
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CMceTimerManager::NewTimerId
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 TMceTimerId CMceTimerManager::NewTimerId()
       
   174     {
       
   175     // Note that a rolling unsigned 32bit integer id counter
       
   176     // is not strictly unique - however, it is not possible
       
   177     // to have a timer with id 1 and id 2^32-1 at the same time
       
   178     // due to time and memory constraints.
       
   179     iTimerIdCounter++;
       
   180     if (iTimerIdCounter == KNoSuchTimer)
       
   181         {
       
   182         iTimerIdCounter++;
       
   183         }
       
   184     return iTimerIdCounter;
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // CMceTimerManager::FindEntry
       
   189 // -----------------------------------------------------------------------------
       
   190 //
       
   191 TInt CMceTimerManager::FindEntry(TMceTimerId aTimerId) const
       
   192     {
       
   193     // Search for an entry with a matching id
       
   194     TInt count = iEntries.Count();
       
   195     for (TInt i = 0; i < count; i++)
       
   196         {
       
   197 
       
   198         if (iEntries[ i ]->iId == aTimerId)
       
   199             {
       
   200 
       
   201             return i;
       
   202 
       
   203             }
       
   204         }
       
   205 
       
   206     return KErrNotFound;
       
   207     }
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CMceTimerManager::CMceTimerManager
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 CMceTimerManager::CMceTimerManager() :
       
   214     iEntries(KArrayGranularity), iTimerIdCounter(KNoSuchTimer),
       
   215             iTimerContainer(NULL)
       
   216 
       
   217     {
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CMceTimerManager::ConstructL
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CMceTimerManager::ConstructL()
       
   225     {
       
   226 
       
   227     iTimerContainer = CDeltaTimer::NewL(CActive::EPriorityStandard);
       
   228 
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CMceTimerManager::TimerExpiredCallBack
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 TInt CMceTimerManager::TimerExpiredCallBack(TAny *aPtr)
       
   236     {
       
   237 
       
   238     MCESRV_DEBUG("TimerExpiredCallBack, Entry");
       
   239     if (aPtr != NULL)
       
   240         {
       
   241 
       
   242         TEntry* entry = static_cast<TEntry*> (aPtr );
       
   243         MCESRV_DEBUG_DVALUE(
       
   244                 "CMceTimerManager:timer expired - calling observer.timer", entry->iId );
       
   245 
       
   246         // Call timer expired function
       
   247         TRAP_IGNORE( entry->iObserver->TimerExpiredL( entry->iId,
       
   248                         entry->iObserverParam ) );
       
   249 
       
   250         // Remove the TEntry pointer from iEntries
       
   251         TInt index = entry->iTimerManager->iEntries.Find(entry);
       
   252         if (index != KErrNotFound)
       
   253             {
       
   254             entry->iTimerManager->iEntries.Remove(index);
       
   255             }
       
   256 
       
   257         // Delete entry
       
   258         delete entry;
       
   259         return KErrNone;
       
   260 
       
   261         }
       
   262 
       
   263     MCESRV_DEBUG("TimerExpiredCallBack, Exit");
       
   264     return KErrArgument;
       
   265 
       
   266     }
       
   267 
       
   268 // -----------------------------------------------------------------------------
       
   269 // CMceTimerManager::TEntry::TEntry
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 CMceTimerManager::TEntry::TEntry() :
       
   273     iId(KNoSuchTimer), iObserver( NULL), iObserverParam( NULL), iTimerManager( NULL )
       
   274     {
       
   275     }
       
   276