hwrmhaptics/hapticspluginmanager/src/hwrmhapticsreservationhandler.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Haptics server reservation handler implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "hwrmhapticsreservationhandler.h"
       
    20 #include "hwrmhapticstrace.h"
       
    21 #include "hwrmhapticsclientserver.h"
       
    22 #include "hwrmhapticsservice.h"
       
    23 #include "hwrmhapticspolicy.h"
       
    24 
       
    25 const TInt KDefaultPriority = 0;
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // Two-phased constructor.
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 EXPORT_C CHWRMHapticsReservationHandler* CHWRMHapticsReservationHandler::NewL(
       
    32     const TDesC& aPolicyFilename )
       
    33     {
       
    34     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::NewL()" ) ) );
       
    35 
       
    36     CHWRMHapticsReservationHandler* self = 
       
    37         new ( ELeave ) CHWRMHapticsReservationHandler();
       
    38     
       
    39     CleanupStack::PushL( self );
       
    40     
       
    41     self->ConstructL( aPolicyFilename );
       
    42     
       
    43     CleanupStack::Pop( self );
       
    44 
       
    45     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::NewL - return 0x%x" ), self ) );
       
    46 
       
    47     return self;
       
    48     }
       
    49    
       
    50 // ---------------------------------------------------------------------------
       
    51 // Destructor.
       
    52 // ---------------------------------------------------------------------------
       
    53 //
       
    54 CHWRMHapticsReservationHandler::~CHWRMHapticsReservationHandler()
       
    55     {
       
    56     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::~CHWRMHapticsReservationHandler()" ) ) );
       
    57 
       
    58     delete iPolicy;
       
    59     iPolicy = NULL;
       
    60     
       
    61     // delete reservations
       
    62     while ( iReservation )
       
    63         {
       
    64         TReservationData* deleteData = iReservation;
       
    65         iReservation = iReservation->iSuspendedData;
       
    66 
       
    67         delete deleteData;
       
    68         }
       
    69 
       
    70     iPriorities.Close();
       
    71     
       
    72     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::~CHWRMHapticsReservationHandler - return" ) ) );
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Reserve's resource.
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 TBool CHWRMHapticsReservationHandler::ReserveL( 
       
    80                                     TSecureId aSid, 
       
    81                                     TBool aForceNoCCoeEnv, 
       
    82                                     CHWRMHapticsService* aServiceCallback )
       
    83     {
       
    84     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ReserveL(0x%x, 0x%x)" ),
       
    85                        aForceNoCCoeEnv, aServiceCallback ) );
       
    86 
       
    87     TBool trusted( EFalse );
       
    88     TInt priority = KDefaultPriority;
       
    89     
       
    90     // get priority and trusted/not-trusted value
       
    91     GetPriority( aSid, trusted, priority );
       
    92     
       
    93     // If no CCoeEnv is forced, check that client is trusted
       
    94     if ( aForceNoCCoeEnv && !trusted )
       
    95         {
       
    96         User::Leave( KErrAccessDenied );
       
    97         }
       
    98 
       
    99     // keeps track whether or not the made reservation was suspended
       
   100     TBool reserveSuspended = EFalse;
       
   101     
       
   102     // If caller is not yet reserving this subresource, reserve it.
       
   103     if ( !AlreadyReserved( aServiceCallback ) )
       
   104         {
       
   105         // if active reservation has higher priority, reservation
       
   106         // becomes suspended
       
   107         if ( iReservation && iReservation->iPriority >= priority )
       
   108             {
       
   109             reserveSuspended = ETrue;
       
   110             }
       
   111 
       
   112         // Create data objects at this point so that any OOM leaves 
       
   113         // will not cause reservation state to be uncertain
       
   114         TReservationData* newData = new ( ELeave ) TReservationData();
       
   115 
       
   116         // Now we have done the memory allocation we need to, 
       
   117         // so we can reserve haptics with no risk of OOM errors
       
   118         // and therefore be sure that reservations array is never left in 
       
   119         // undetermined state.
       
   120         newData->iCallback = aServiceCallback;
       
   121         newData->iPriority = priority;
       
   122 
       
   123         if ( reserveSuspended )
       
   124             {
       
   125             // Reserve suspended, i.e. go through list until next one has lower priority
       
   126             TReservationData* priorityCheckData = iReservation->iSuspendedData;
       
   127             TReservationData* priorityPreviousData = iReservation;
       
   128             
       
   129             while ( priorityCheckData && 
       
   130                     priorityCheckData->iPriority <= priority )
       
   131                 {
       
   132                 priorityPreviousData = priorityCheckData;
       
   133                 priorityCheckData = priorityCheckData->iSuspendedData;
       
   134                 }
       
   135                 
       
   136             // insert new data
       
   137             newData->iSuspendedData = priorityCheckData;
       
   138             priorityPreviousData->iSuspendedData = newData;
       
   139             
       
   140             // Notify callback of the reserver that it is suspended
       
   141             NotifySuspend( newData );
       
   142             }
       
   143         else 
       
   144             {
       
   145             // Reserve as active, i.e. first in list
       
   146             newData->iSuspendedData = iReservation;
       
   147             iReservation = newData;
       
   148             
       
   149             // Notify callback of current reserver
       
   150             NotifySuspend( newData->iSuspendedData );
       
   151             }
       
   152         }             
       
   153     
       
   154     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ReserveL - return" ) ) );
       
   155     
       
   156     return reserveSuspended;
       
   157     }
       
   158 
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // Releases reservation of a service.
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 TBool CHWRMHapticsReservationHandler::Release( 
       
   165                                     CHWRMHapticsService* aServiceCallback )
       
   166     {
       
   167     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::Release(0x%x)" ), aServiceCallback) );
       
   168 
       
   169     // find the reservation data from the list
       
   170     TReservationData* found = iReservation;
       
   171     TReservationData* prev = NULL;
       
   172 
       
   173     // continue searching while there are items in the list, or until 
       
   174     // the item is found
       
   175     while ( found && found->iCallback != aServiceCallback )
       
   176         {
       
   177         prev = found;
       
   178         found = found->iSuspendedData;
       
   179         }
       
   180 
       
   181     // remove reservation if it was found
       
   182     if ( found )
       
   183         {
       
   184         COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::Release - Releasing: 0x%x, suspendedData = 0x%x" ), found, found->iSuspendedData ) );
       
   185         
       
   186         if ( !prev )
       
   187             {
       
   188             // reservation is the first one in the list, activate next item
       
   189             iReservation = found->iSuspendedData;
       
   190             }
       
   191         else
       
   192             {
       
   193             // reservation is in the middle of the list, reset list's links
       
   194             prev->iSuspendedData = found->iSuspendedData;
       
   195             }
       
   196 
       
   197         // delete released service
       
   198         delete found;
       
   199         found = NULL;
       
   200 
       
   201         // Reactivate first service on list
       
   202         NotifyResume( iReservation );
       
   203         }
       
   204     else
       
   205         {
       
   206         // reservation not found
       
   207         COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::Release - Reservation not found, releasing nothing" ) ) );
       
   208         }
       
   209 
       
   210     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::Release - return" ) ) );
       
   211     
       
   212     return (iReservation ? ETrue : EFalse);
       
   213     }
       
   214 
       
   215 // ---------------------------------------------------------------------------
       
   216 // Returns whether or not haptics is reserved for some other service than
       
   217 // given one.
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 TBool CHWRMHapticsReservationHandler::IsReserved( 
       
   221                         const CHWRMHapticsService* aServiceCallback ) const
       
   222     {
       
   223     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::IsReserved(0x%x)" ), aServiceCallback ) );
       
   224 
       
   225     TBool retval = EFalse;
       
   226     
       
   227     // Check if haptics is reserved to somebody other than caller
       
   228     if ( iReservation && iReservation->iCallback != aServiceCallback )
       
   229         {
       
   230         retval = ETrue;
       
   231         }
       
   232 
       
   233     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::IsReserved - return: 0x%x" ), retval ) );
       
   234     
       
   235     return retval;
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------------------------
       
   239 // Checks whether or not haptics is reserved for the given client.
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 TBool CHWRMHapticsReservationHandler::ActiveReservation( 
       
   243                         const CHWRMHapticsService* aServiceCallback ) const
       
   244     {
       
   245     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ActiveReservation(0x%x)" ), aServiceCallback ) );
       
   246 
       
   247     TBool retval = EFalse;
       
   248     
       
   249     // Check if haptics is reserved for the given service
       
   250     if ( iReservation && iReservation->iCallback == aServiceCallback )
       
   251         {
       
   252         retval = ETrue;
       
   253         }
       
   254 
       
   255     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ActiveReservation - return: 0x%x" ), retval ) );
       
   256     
       
   257     return retval;
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // Checks whether current haptics client's priority is higher than new 
       
   262 // request making client's priority.
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 TBool CHWRMHapticsReservationHandler::ReservedPriorityHigher( 
       
   266                                                     TSecureId aSid ) const
       
   267     {
       
   268     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ReservedPriorityHigher" ) ) );
       
   269 
       
   270     TBool ret = EFalse;
       
   271 
       
   272     if( iReservation )
       
   273         {
       
   274         // active reservation is the first reservation data
       
   275         TInt activePriority = iReservation->iPriority;
       
   276         
       
   277         // get priority of the calling service
       
   278         TBool trusted = EFalse;
       
   279         TInt reqPriority = KDefaultPriority;
       
   280         GetPriority( aSid, trusted, reqPriority );
       
   281         
       
   282         // check priority order
       
   283         if ( activePriority >= reqPriority )
       
   284             {
       
   285             ret = ETrue;
       
   286             }
       
   287         }
       
   288     
       
   289     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ReservedPriorityHigher - ret %d" ), ret ) );
       
   290     
       
   291     return ret;
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------------------------
       
   295 // Sets the priority of a client (used when executing priority setting
       
   296 // requested by the client using SetDeviceProperty()).
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 void CHWRMHapticsReservationHandler::SetPriorityL( TSecureId aSid, 
       
   300                                                    TInt aPriority )
       
   301     {
       
   302     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::SetPriorityL" ) ) );
       
   303 
       
   304     // do not insert new priority, if already set; just update old value
       
   305     TInt exists = EFalse;
       
   306     for ( TInt i = 0; i < iPriorities.Count() && !exists; ++i )
       
   307         {
       
   308         if ( iPriorities[i].iSid == aSid )
       
   309             {
       
   310             iPriorities[i].iPriority = aPriority;
       
   311             exists = ETrue;
       
   312             }
       
   313         }
       
   314 
       
   315     // if not found, insert new data
       
   316     if ( !exists )
       
   317         {
       
   318         TPriority priority( aSid, aPriority );
       
   319         iPriorities.AppendL( priority );
       
   320         }
       
   321     
       
   322     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::SetPriorityL - return" ) ) );
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // Removes the priority of a client.
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 void CHWRMHapticsReservationHandler::RemovePriority( TSecureId aSid )
       
   330     {
       
   331     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::RemovePriority" ) ) );
       
   332 
       
   333     TInt removed = EFalse;
       
   334     for ( TInt i = 0; i < iPriorities.Count() && !removed; ++i )
       
   335         {
       
   336         if ( iPriorities[i].iSid == aSid )
       
   337             {
       
   338             iPriorities.Remove( i );
       
   339             removed = ETrue;
       
   340             }
       
   341         }
       
   342 
       
   343     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::RemovePriority - return" ) ) );
       
   344     }
       
   345 
       
   346 // ---------------------------------------------------------------------------
       
   347 // Fetches the priority of a client identified using the given secure id.
       
   348 // ---------------------------------------------------------------------------
       
   349 //
       
   350 void CHWRMHapticsReservationHandler::GetPriority( TSecureId aSid, 
       
   351                                                TBool& aTrusted, TInt& aPriority ) const
       
   352     {
       
   353     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::GetPriority" ) ) );
       
   354 
       
   355     // initialize return value with the default priority
       
   356     TInt priority = KDefaultPriority;
       
   357     
       
   358     // search from the priority array
       
   359     TInt found = EFalse;
       
   360     for ( TInt i = 0; i < iPriorities.Count() && !found; ++i )
       
   361         {
       
   362         if ( iPriorities[i].iSid == aSid )
       
   363             {
       
   364             priority = iPriorities[i].iPriority;
       
   365             found = ETrue;
       
   366             }
       
   367         }
       
   368 
       
   369     TInt policyPriority;
       
   370     // search priority from the policy file data
       
   371     iPolicy->GetPriority( aSid, aTrusted, policyPriority );
       
   372 
       
   373     // set priority from policy file, if not found from priority array
       
   374     // and found from the policy file (i.e. trusted client)
       
   375     if ( !found && aTrusted )
       
   376         {
       
   377         priority = policyPriority;
       
   378         }
       
   379     
       
   380     aPriority = priority;
       
   381     
       
   382     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::GetPriority - return %d" ), priority ) );
       
   383     }
       
   384 
       
   385 // ---------------------------------------------------------------------------
       
   386 // C++ constructor
       
   387 // ---------------------------------------------------------------------------
       
   388 //
       
   389 CHWRMHapticsReservationHandler::CHWRMHapticsReservationHandler()
       
   390     {
       
   391     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::CHWRMHapticsReservationHandler()" ) ) );
       
   392     }
       
   393 
       
   394 // ---------------------------------------------------------------------------
       
   395 // Symbian 2nd phase constructor.
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 void CHWRMHapticsReservationHandler::ConstructL( 
       
   399                                             const TDesC& aPolicyFilename )
       
   400     {
       
   401     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ConstructL()" ) ) );
       
   402 
       
   403     // Initialize policy object
       
   404     iPolicy = CHWRMHapticsPolicy::NewL( aPolicyFilename );
       
   405     
       
   406     COMPONENT_TRACE( ( _L( "CHWRMHapticsReservationHandler::ConstructL - return" ) ) );
       
   407     }
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // Returns whether or not a reservation exists for the given service.
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 TBool CHWRMHapticsReservationHandler::AlreadyReserved( 
       
   414                         const CHWRMHapticsService* aService ) const
       
   415     {
       
   416     TBool reserved = EFalse;
       
   417 
       
   418     // go through the reservations
       
   419     TReservationData* reservationData = iReservation;
       
   420     while ( reservationData && !reserved )
       
   421         {
       
   422         if ( reservationData->iCallback == aService )
       
   423             {
       
   424             // reservation found for the service
       
   425             reserved = ETrue;
       
   426             }
       
   427         
       
   428         reservationData = reservationData->iSuspendedData;
       
   429         }
       
   430     
       
   431     return reserved;
       
   432     }
       
   433 
       
   434 // ---------------------------------------------------------------------------
       
   435 // Notifies callback that its reservations has been suspended.
       
   436 // ---------------------------------------------------------------------------
       
   437 //
       
   438 void CHWRMHapticsReservationHandler::NotifySuspend( 
       
   439                                             TReservationData* aData ) const
       
   440     {
       
   441     if ( aData && aData->iCallback )
       
   442         {
       
   443         aData->iCallback->SuspendResource();
       
   444         }
       
   445     }
       
   446 
       
   447 // ---------------------------------------------------------------------------
       
   448 // Notifies callback that its reservations has been resumed.
       
   449 // ---------------------------------------------------------------------------
       
   450 //
       
   451 void CHWRMHapticsReservationHandler::NotifyResume( 
       
   452                                             TReservationData* aData ) const
       
   453     {
       
   454     if ( aData && aData->iCallback )
       
   455         {
       
   456         aData->iCallback->ResumeResource();
       
   457         }
       
   458     }
       
   459 
       
   460 //  End of File