resourcemgmt/hwresourcesmgr/server/src/HWRMLightService.cpp
changeset 0 4e1aa6a622a0
child 83 11da52d4c847
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2006-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 //
       
    15 
       
    16 
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HWRMServer.h"
       
    21 #include "HWRMService.h"
       
    22 #include "HWRMLightService.h"
       
    23 #include "HWRMPluginHandler.h"
       
    24 #include "HWRMReservationHandler.h"
       
    25 #include "HWRMtrace.h"
       
    26 
       
    27 // EXTERNAL DATA STRUCTURES
       
    28 // None
       
    29 
       
    30 // EXTERNAL FUNCTION PROTOTYPES  
       
    31 // None
       
    32 
       
    33 // CONSTANTS
       
    34 _LIT( KPanicCategory, "HWRMLightService" );
       
    35 
       
    36 const TUint KHWRMDefaultColor = 0;
       
    37 // MACROS
       
    38 // None
       
    39 
       
    40 // LOCAL CONSTANTS AND MACROS
       
    41 // None
       
    42 
       
    43 // MODULE DATA STRUCTURES
       
    44 // None
       
    45 
       
    46 // LOCAL FUNCTION PROTOTYPES
       
    47 // None
       
    48 
       
    49 // FORWARD DECLARATIONS
       
    50 // None
       
    51 
       
    52 // ============================= LOCAL FUNCTIONS ===============================
       
    53 
       
    54 // ============================ MEMBER FUNCTIONS ===============================
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CHWRMLightService::CHWRMLightService
       
    58 // C++ constructor
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CHWRMLightService::CHWRMLightService(CHWRMLightCommonData& aLightCommonData)
       
    62     : iLightCommonData(aLightCommonData)
       
    63     {
       
    64     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::CHWRMLightService()" ));
       
    65     
       
    66     // Null the target timers to ensure proper cleanup if ConstructL leaves
       
    67     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
    68         {
       
    69         iTargets[i].iTimer = NULL;
       
    70         }
       
    71     
       
    72     iSupportedTargets = iLightCommonData.SupportedTargets();
       
    73     iFullSystemTarget = iLightCommonData.FullSystemTarget();
       
    74     
       
    75     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::CHWRMLightService - return" ));
       
    76     }
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 // CHWRMLightService::ConstructL
       
    80 // Symbian 2nd phase constructor can leave.
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 void CHWRMLightService::ConstructL(CHWRMPluginHandler* aPluginHandler,
       
    84                                    CHWRMReservationHandler* aReservationHandler)
       
    85     {
       
    86     COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::ConstructL(0x%x, 0x%x)" ), aPluginHandler, aReservationHandler);
       
    87 
       
    88     __ASSERT_ALWAYS(aPluginHandler, User::Panic(KPanicCategory, EPanicBadHandle));
       
    89     __ASSERT_ALWAYS(aReservationHandler, User::Panic(KPanicCategory, EPanicBadHandle));
       
    90     
       
    91     BaseConstructL(aPluginHandler, aReservationHandler);
       
    92     
       
    93     // initialize target timers (and the rest of the data, just in case)    
       
    94     TInt currentTarget(KHWRMLightFirstTarget);
       
    95     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
    96         {
       
    97         // only initialize supported target timers
       
    98         if ( currentTarget & iSupportedTargets )
       
    99             {
       
   100             iTargets[i].iTimer = CHWRMGenericTimer::NewL(*this, KHWRMGenericTimerMaxTime, i);	
       
   101             }
       
   102 
       
   103         iTargets[i].iTarget = currentTarget;
       
   104         iTargets[i].iSuspended = EFalse;	
       
   105         iTargets[i].iReserved = EFalse;
       
   106         
       
   107         iTargets[i].iBaseState.iIntensity = KHWRMDefaultIntensity;	
       
   108         iTargets[i].iBaseState.iState = CHWRMLight::ELightStatusUnknown;
       
   109         iTargets[i].iBaseState.iFade = EFalse;
       
   110         iTargets[i].iBaseState.iOffCycleDuration = KHWRMDefaultCycleTime;
       
   111         iTargets[i].iBaseState.iOnCycleDuration = KHWRMDefaultCycleTime;
       
   112         iTargets[i].iBaseState.iColor = KHWRMInvalidColorMask;
       
   113         
       
   114         
       
   115         iTargets[i].iLastState.iIntensity = KHWRMDefaultIntensity;	
       
   116         iTargets[i].iLastState.iState = CHWRMLight::ELightStatusUnknown;
       
   117         iTargets[i].iLastState.iFade = EFalse;
       
   118         iTargets[i].iLastState.iOffCycleDuration = KHWRMDefaultCycleTime;
       
   119         iTargets[i].iLastState.iOnCycleDuration = KHWRMDefaultCycleTime;
       
   120         iTargets[i].iLastState.iColor = KHWRMInvalidColorMask;
       
   121 
       
   122         iTargets[i].iFrozenState.iIntensity = KHWRMDefaultIntensity;	
       
   123         iTargets[i].iFrozenState.iState = CHWRMLight::ELightStatusUnknown;
       
   124         iTargets[i].iFrozenState.iFade = EFalse;
       
   125         iTargets[i].iFrozenState.iOffCycleDuration = KHWRMDefaultCycleTime;
       
   126         iTargets[i].iFrozenState.iOnCycleDuration = KHWRMDefaultCycleTime;
       
   127         iTargets[i].iFrozenState.iColor = KHWRMInvalidColorMask;
       
   128 
       
   129         // move to next target
       
   130         currentTarget <<= 1;
       
   131         }
       
   132 
       
   133     iDefaultState.iIntensity = KHWRMDefaultIntensity;	
       
   134     iDefaultState.iState = CHWRMLight::ELightStatusUnknown; // This is updated based on inactivity time
       
   135     iDefaultState.iFade = ETrue;
       
   136     iDefaultState.iOffCycleDuration = KHWRMDefaultCycleTime;
       
   137     iDefaultState.iOnCycleDuration = KHWRMDefaultCycleTime;
       
   138     
       
   139     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ConstructL - return " ) );
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CHWRMLightService::NewL
       
   144 // Two-phased constructor.
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 CHWRMLightService* CHWRMLightService::NewL(CHWRMPluginHandler* aPluginHandler,
       
   148                                            CHWRMReservationHandler* aReservationHandler,
       
   149                                            CHWRMLightCommonData& aLightCommonData)
       
   150     {
       
   151     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::NewL(0x%x)" ), aPluginHandler);
       
   152 
       
   153     CHWRMLightService* self = new( ELeave ) CHWRMLightService(aLightCommonData);
       
   154     
       
   155     CleanupStack::PushL( self );
       
   156     self->ConstructL(aPluginHandler, aReservationHandler);
       
   157     CleanupStack::Pop();
       
   158 
       
   159     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::NewL - return 0x%x" ), self );
       
   160 
       
   161     return self;
       
   162     }
       
   163    
       
   164 // ---------------------------------------------------------
       
   165 // Destructor
       
   166 // ---------------------------------------------------------
       
   167 //
       
   168 CHWRMLightService::~CHWRMLightService()
       
   169     {
       
   170     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::~CHWRMLightService()" ) );
       
   171     
       
   172     CleanupLights();
       
   173 
       
   174     // Delete timers
       
   175     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
   176         {
       
   177         delete iTargets[i].iTimer;
       
   178         }
       
   179 
       
   180     // Base class cleanup
       
   181     BaseDestruct();
       
   182     
       
   183     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::~CHWRMLightService - return" ) );
       
   184     }
       
   185 
       
   186 
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CHWRMLightService::CleanupLights()
       
   190 // Handles Light service cleanup
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 void CHWRMLightService::CleanupLights()
       
   194     {
       
   195     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::CleanupLights()" ) );
       
   196     if (!iCleaningUp)
       
   197         {
       
   198         iCleaningUp = ETrue;
       
   199         
       
   200         TInt unreserved(0x0);
       
   201         TInt mask(0x0); // mask of targets
       
   202         
       
   203         // If any targets are still reserved, release them
       
   204         for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
   205             {
       
   206             if ( iTargets[i].iReserved )
       
   207                 {
       
   208                 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMCleanupLights - Target index %d needs release." ), i );
       
   209                 mask |= iTargets[i].iTarget;
       
   210 
       
   211                 // Clear reserved and suspended flags
       
   212                 iTargets[i].iReserved = EFalse;
       
   213                 iTargets[i].iSuspended = EFalse;
       
   214                 }
       
   215             }
       
   216         
       
   217         if ( mask )
       
   218             {
       
   219             unreserved = iReservationHandler->Release(this, mask);	
       
   220             }
       
   221         else
       
   222             {
       
   223             unreserved = iReservationHandler->GetUnreservedTargets();
       
   224             }
       
   225             
       
   226         mask = 0x0; // reset mask
       
   227         
       
   228         // Restore unreserved targets that have been affected by this session to default state.
       
   229         // Also cancel all timers.
       
   230         for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
   231             {
       
   232             if ( iTargets[i].iTimer && iTargets[i].iTimer->IsActive() )
       
   233                 {
       
   234                 iTargets[i].iTimer->Cancel();
       
   235                 }
       
   236                 
       
   237             if ( (unreserved & iTargets[i].iTarget) == iTargets[i].iTarget 
       
   238                  && iTargets[i].iLastState.iState != CHWRMLight::ELightStatusUnknown )
       
   239                 {
       
   240                 mask |= iTargets[i].iTarget;
       
   241                 }
       
   242             }
       
   243 
       
   244         // Restore default states if needed. iTargets[0] is used as dummy.
       
   245         // The aMessage pointer is required for completion after transaction.
       
   246         if ( mask )
       
   247             {
       
   248             // Cannot restore both system targets and non-system targets with same command, so do them separately.
       
   249             TInt subMask(mask & iFullSystemTarget);
       
   250 
       
   251             if ( subMask )
       
   252                 {
       
   253                 RestoreState(iTargets[0], EStateChangeDefaultState, EFalse, subMask);
       
   254                 }
       
   255 
       
   256             subMask = mask & ~subMask; // rest of targets
       
   257             
       
   258             if ( subMask )
       
   259                 {
       
   260                 RestoreState(iTargets[0], EStateChangeDefaultState, EFalse, subMask);
       
   261                 }
       
   262             }
       
   263         }
       
   264     else
       
   265         {
       
   266         COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::CleanupLights - Already cleaned up" ) );
       
   267         }
       
   268     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::CleanupLights - return" ) );        
       
   269     }
       
   270 
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CHWRMLightService::ExecuteMessageL
       
   274 // Handles Light requests. 
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 TBool CHWRMLightService::ExecuteMessageL( const RMessage2& aMessage )
       
   278     {
       
   279     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL(0x%x)" ), aMessage.Function());
       
   280     
       
   281     // If cleaning up or dummy message, something has gone wrong, so leave.
       
   282     if ( aMessage.IsNull() || iCleaningUp )
       
   283         {
       
   284         User::Leave(KErrBadHandle);
       
   285         }
       
   286 
       
   287     // All messages are by default asynchronous because of the nature of the plugin API
       
   288     TBool completeMessage(EFalse);
       
   289 
       
   290     switch ( aMessage.Function() )
       
   291         {
       
   292     	case EHWRMLightsOn:
       
   293     	    {
       
   294             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMLightsOn" ) );
       
   295 
       
   296             THWRMLightsOnCmdPackage pckg;
       
   297             aMessage.ReadL( 0, pckg );
       
   298 
       
   299             THWRMLightsOnData* commandData = &(pckg());
       
   300             
       
   301     	    LightsOnL( commandData->iTarget, 
       
   302     	               commandData->iDuration, 
       
   303     	               commandData->iIntensity, 
       
   304     	               commandData->iFadeIn, 
       
   305     	               aMessage );
       
   306 
       
   307     		break;
       
   308     	    }
       
   309     	case EHWRMLightsOff:
       
   310     	    {
       
   311             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMLightsOff" ) );
       
   312 
       
   313             THWRMLightsOffCmdPackage pckg;
       
   314             aMessage.ReadL( 0, pckg );
       
   315             
       
   316             THWRMLightsOffData* commandData = &(pckg());
       
   317 
       
   318     	    LightsOffL( commandData->iTarget,
       
   319     	                commandData->iDuration, 
       
   320     	                commandData->iFadeOut, 
       
   321     	                aMessage );
       
   322 
       
   323     		break;
       
   324     	    }
       
   325     	case EHWRMLightsBlink:
       
   326     	    {
       
   327             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMLightsBlink" ) );
       
   328 
       
   329             THWRMLightsBlinkCmdPackage pckg;
       
   330             aMessage.ReadL( 0, pckg );
       
   331 
       
   332             THWRMLightsBlinkData* commandData = &(pckg());
       
   333 
       
   334     	    LightsBlinkL( commandData->iTarget,
       
   335     	                  commandData->iDuration, 
       
   336     	                  commandData->iIntensity, 
       
   337     	                  commandData->iOnCycleDuration, 
       
   338     	                  commandData->iOffCycleDuration, 
       
   339     	                  aMessage );
       
   340 
       
   341     		break;
       
   342     	    }
       
   343     	case EHWRMReserveLights:
       
   344     	    {
       
   345             COMPONENT_TRACE1(_L("HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMReserveLights") );
       
   346             
       
   347             ReserveLightsL(aMessage);
       
   348             
       
   349             completeMessage = ETrue; // Since using dummy messages for freeze state restores, need complete always.
       
   350 
       
   351     		break;
       
   352     	    }
       
   353     	case EHWRMReleaseLights:
       
   354     	    {
       
   355             COMPONENT_TRACE1(_L("HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMReleaseLights") );
       
   356 
       
   357             // Release specified targets
       
   358             TInt targetMask(aMessage.Int0());
       
   359 
       
   360             ReleaseLights(targetMask);
       
   361             
       
   362             
       
   363             completeMessage = ETrue; // Since using dummy messages for default state restores, need complete always.
       
   364 
       
   365     		break;
       
   366     	    }
       
   367         case EHWRMGetSupportedTargets:
       
   368             {
       
   369 	        // Get the supported targets and return them in parameter 0
       
   370             TPtrC8 value( reinterpret_cast<TUint8*>(&iSupportedTargets), sizeof( iSupportedTargets ) );
       
   371             aMessage.WriteL( 0, value );
       
   372             
       
   373             completeMessage = ETrue;
       
   374             
       
   375             break;
       
   376             }
       
   377     	case EHWRMCleanupLights:
       
   378     	    {
       
   379             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMCleanupLights" ) );
       
   380 
       
   381             CleanupLights();
       
   382 
       
   383             // Complete, as dummy messages are used in cleanup
       
   384             completeMessage = ETrue;
       
   385 
       
   386     		break;
       
   387     	    }
       
   388         
       
   389         case EHWRMSetLightColor:
       
   390             {
       
   391             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMSetLightColor" ) );
       
   392 
       
   393             THWRMSetColorCmdPackage pckg;
       
   394             aMessage.ReadL( 0, pckg );
       
   395 
       
   396             THWRMSetColorData* commandData = &(pckg());
       
   397             
       
   398     	    SetColorL( commandData->iTarget, 
       
   399     	               commandData->iColor, 
       
   400     	               aMessage );
       
   401 
       
   402     		break;
       
   403     	    }
       
   404 
       
   405         case EHWRMLightsOnWithColor:
       
   406             {
       
   407             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMLightsOnWithColor" ) );
       
   408 
       
   409             
       
   410             THWRMLightsOnCmdPackage pckg;
       
   411             aMessage.ReadL( 0, pckg );
       
   412 
       
   413             THWRMLightsOnData* commandData = &(pckg());
       
   414 
       
   415             //Data allocated here will be freed in SetColorL if target is suspended
       
   416             //else will be freed in CompleteRequest for SetColorL
       
   417             THWRMLightsOnData* lightOnData = new(ELeave) THWRMLightsOnData;
       
   418             lightOnData->iTarget    = commandData->iTarget;
       
   419             lightOnData->iFadeIn    = commandData->iFadeIn;
       
   420             lightOnData->iDuration  = commandData->iDuration;
       
   421             lightOnData->iIntensity = commandData->iIntensity;
       
   422 
       
   423             CleanupStack::PushL( lightOnData );
       
   424             
       
   425             // coverity[freed_arg]
       
   426             TBool pluginCommandCalled = SetColorL(commandData->iTarget, commandData->iColor, aMessage, 
       
   427                                                   EFalse, ETrue, HWRMLightCommand::ELightsOnCmdId, lightOnData);
       
   428 
       
   429             //SetColor will store the pointer(lightOnData) as splitcommand data.
       
   430             //This will freed after ProcessResponseL.
       
   431             CleanupStack::Pop( lightOnData );
       
   432             
       
   433             if(!pluginCommandCalled)
       
   434                 {
       
   435                 //Target is suspended, so call light on.
       
   436                 //Message is completed to client and last state is stored.
       
   437                 LightsOnL( commandData->iTarget, 
       
   438                                       commandData->iDuration, 
       
   439                                       commandData->iIntensity, 
       
   440                                       commandData->iFadeIn, 
       
   441                                       aMessage );
       
   442                 }
       
   443             
       
   444             
       
   445             
       
   446             break;
       
   447             }
       
   448         
       
   449         case EHWRMLightsBlinkWithColor:
       
   450             {
       
   451             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - EHWRMLightsBlinkWithColor" ) );
       
   452 
       
   453             
       
   454             THWRMLightsBlinkCmdPackage pckg;
       
   455             aMessage.ReadL( 0, pckg );
       
   456 
       
   457             THWRMLightsBlinkData* commandData = &(pckg());
       
   458 
       
   459             //Data allocated here will be freed in SetColorL if target is suspended
       
   460             //else will be freed in CompleteRequest for SetColorL
       
   461             THWRMLightsBlinkData* lightBlinkData = new(ELeave) THWRMLightsBlinkData;
       
   462             
       
   463             lightBlinkData->iTarget           = commandData->iTarget;
       
   464             lightBlinkData->iDuration         = commandData->iDuration; 
       
   465             lightBlinkData->iIntensity        = commandData->iIntensity; 
       
   466             lightBlinkData->iOnCycleDuration  = commandData->iOnCycleDuration; 
       
   467             lightBlinkData->iOffCycleDuration = commandData->iOffCycleDuration;
       
   468 
       
   469             CleanupStack::PushL( lightBlinkData );
       
   470 
       
   471             // coverity[freed_arg]
       
   472             TBool pluginCommandCalled = SetColorL(commandData->iTarget, commandData->iColor, aMessage, 
       
   473                                                   EFalse, ETrue, HWRMLightCommand::ELightsBlinkCmdId, lightBlinkData);
       
   474 
       
   475             //SetColor will store the pointer(lightBlinkData) as splitcommand data.
       
   476             //This will freed after ProcessResponseL.
       
   477             CleanupStack::Pop( lightBlinkData );
       
   478             
       
   479 
       
   480             if(!pluginCommandCalled)
       
   481                 {
       
   482                 //Target is suspended, so call light Blink.
       
   483                 //Message is completed to client and last state is stored.
       
   484                 LightsBlinkL( commandData->iTarget,
       
   485                               commandData->iDuration, 
       
   486                               commandData->iIntensity, 
       
   487                               commandData->iOnCycleDuration, 
       
   488                               commandData->iOffCycleDuration, 
       
   489                               aMessage );
       
   490                 }
       
   491 
       
   492             
       
   493             break;
       
   494             }
       
   495 
       
   496         // Cannot identify the message, panic the client
       
   497         default:
       
   498             {
       
   499             CHWRMServer::PanicClient(aMessage, EPanicIllegalFunction);
       
   500             break;
       
   501             }
       
   502         }
       
   503         
       
   504     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ExecuteMessageL - return 0x%x" ), completeMessage );
       
   505 
       
   506     return completeMessage;
       
   507     }
       
   508 
       
   509 
       
   510 // -----------------------------------------------------------------------------
       
   511 // CHWRMLightService::ReleaseLights
       
   512 // -----------------------------------------------------------------------------
       
   513 //
       
   514 void CHWRMLightService::ReleaseLights(TInt aTargetMask)
       
   515     {
       
   516     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ReleaseLights(0x%x)" ), aTargetMask );
       
   517 
       
   518     TInt targetMask = aTargetMask;
       
   519     
       
   520     // Change mask if releasing system target 
       
   521     if ( targetMask & CHWRMLight::ESystemTarget )
       
   522         {
       
   523         targetMask = iFullSystemTarget;
       
   524         }
       
   525     else
       
   526         {
       
   527         // Strip unsupported targets from mask since those cannot be reserved anyway.
       
   528         targetMask &= iSupportedTargets;
       
   529         }            
       
   530 
       
   531     TInt restoreMask(0x0);
       
   532     TInt unreserved(iReservationHandler->Release(this, targetMask));	
       
   533            
       
   534     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
   535         {
       
   536         // freeze only targets that were actually reserved to this session
       
   537         if ( ((targetMask & iTargets[i].iTarget) == iTargets[i].iTarget) && iTargets[i].iReserved )
       
   538             {
       
   539             // Clear reserved and suspended flags
       
   540             iTargets[i].iReserved = EFalse;
       
   541             iTargets[i].iSuspended = EFalse;
       
   542                     
       
   543             // Freeze state
       
   544             iTargets[i].iFrozenTime = (iTargets[i].iTimer->Freeze()).Int();
       
   545             iTargets[i].iFrozenState.iIntensity = iTargets[i].iLastState.iIntensity;	
       
   546             iTargets[i].iFrozenState.iState = iTargets[i].iLastState.iState;
       
   547             iTargets[i].iFrozenState.iFade = iTargets[i].iLastState.iFade;
       
   548             iTargets[i].iFrozenState.iOffCycleDuration = iTargets[i].iLastState.iOffCycleDuration;
       
   549             iTargets[i].iFrozenState.iOnCycleDuration = iTargets[i].iLastState.iOnCycleDuration;
       
   550             iTargets[i].iFrozenState.iColor = iTargets[i].iLastState.iColor;
       
   551 
       
   552             // Prepare to restore default state for target light if no more reservations for it exist.
       
   553             if ( (unreserved & iTargets[i].iTarget) == iTargets[i].iTarget )
       
   554                 {
       
   555                 restoreMask |= iTargets[i].iTarget;
       
   556                 }
       
   557             }
       
   558         }
       
   559                 
       
   560         // Restore default states if needed. iTargets[0] is used as dummy.
       
   561         // Note: Cannot restore both system targets and non-system targets with same command, 
       
   562         //       because they have possibly different default states, so do them separately.
       
   563         if ( restoreMask )
       
   564             {
       
   565             TInt subMask(restoreMask & iFullSystemTarget);
       
   566                 
       
   567             if ( subMask )
       
   568                 {
       
   569                 RestoreState(iTargets[0], EStateChangeDefaultState, EFalse, subMask );
       
   570                 }
       
   571                 
       
   572             subMask = restoreMask & ~subMask; // rest of targets
       
   573                 
       
   574             if ( subMask )
       
   575                 {
       
   576                 RestoreState(iTargets[0], EStateChangeDefaultState, EFalse, subMask );
       
   577                 }
       
   578             }
       
   579 
       
   580     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ReleaseLights - return" ) );
       
   581     }
       
   582 
       
   583 // -----------------------------------------------------------------------------
       
   584 // CHWRMLightService::ReserveLightsL
       
   585 // -----------------------------------------------------------------------------
       
   586 //
       
   587 void CHWRMLightService::ReserveLightsL(const RMessage2& aMessage)
       
   588     {
       
   589     // Reserve one or more targets
       
   590     TInt targetMask = aMessage.Int0();
       
   591 
       
   592     // Change mask if reserving system target
       
   593     if ( targetMask & CHWRMLight::ESystemTarget )
       
   594         {
       
   595         targetMask = iFullSystemTarget;
       
   596         }
       
   597     else
       
   598         {
       
   599         // check supplied mask validity
       
   600         CheckTargetParameterL( targetMask );           
       
   601         }
       
   602 
       
   603     TInt suspendedMask(iReservationHandler->ReserveL(aMessage, aMessage.Int2(), this, targetMask));	
       
   604             
       
   605     // Go through targets and update datas
       
   606     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
   607         {
       
   608         if ( (targetMask & iTargets[i].iTarget) == iTargets[i].iTarget )
       
   609             {
       
   610             iTargets[i].iReserved = ETrue;
       
   611                     
       
   612             if ( (suspendedMask & iTargets[i].iTarget) == iTargets[i].iTarget )
       
   613                 {
       
   614                 iTargets[i].iSuspended = ETrue;
       
   615                 }
       
   616                     
       
   617             // Restore previously stored state if restore was specified and
       
   618             // there is valid frozen state stored.
       
   619             if ( aMessage.Int1() && iTargets[i].iFrozenState.iState != CHWRMLight::ELightStatusUnknown)
       
   620                 {
       
   621                 RestoreState(iTargets[i], EStateChangeFrozenState, EFalse);
       
   622                 }
       
   623             else if (aMessage.Int1() && iTargets[i].iFrozenState.iColor != KHWRMInvalidColorMask)
       
   624                 {
       
   625                 RMessage2 message;
       
   626                 TRAP_IGNORE(SetColorL(targetMask, iTargets[i].iFrozenState.iColor, message));
       
   627                 }
       
   628             }
       
   629         }
       
   630     }
       
   631 
       
   632 // -----------------------------------------------------------------------------
       
   633 // CHWRMLightService::UpdateDefaultSensitivityL
       
   634 // -----------------------------------------------------------------------------
       
   635 //
       
   636 void CHWRMLightService::UpdateDefaultSensitivityL(TInt aSensitivity)
       
   637     {
       
   638     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::UpdateDefaultSensitivityL(%d)" ), aSensitivity );
       
   639     
       
   640     RMessage2 dummy;
       
   641     HWRMLightCommand::TLightsSensorSensitivityCmdPackage pckg(aSensitivity);
       
   642     ExecutePluginCommandL(iSupportedTargets, dummy, HWRMLightCommand::ESetLightsSensorSensitivityCmdId, pckg, EFalse);
       
   643 
       
   644     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::UpdateDefaultSensitivityL - return" ) );
       
   645     }
       
   646 
       
   647 // -----------------------------------------------------------------------------
       
   648 // CHWRMLightService::UpdateDefaultIntensityL
       
   649 // -----------------------------------------------------------------------------
       
   650 //
       
   651 void CHWRMLightService::UpdateDefaultIntensityL(TInt aTarget, TInt aIntensity)
       
   652     {
       
   653     COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::UpdateDefaultIntensityL(0x%x, %d)" ), aTarget, aIntensity );
       
   654     
       
   655     RMessage2 dummy;
       
   656 
       
   657     HWRMLightCommand::TLightsIntensityData intensityData;
       
   658     intensityData.iTarget = aTarget;
       
   659     intensityData.iIntensity = aIntensity;
       
   660     HWRMLightCommand::TLightsIntensityCmdPackage pckg(intensityData);
       
   661 
       
   662     ExecutePluginCommandL(iSupportedTargets, dummy, HWRMLightCommand::ESetLightsIntensityCmdId, pckg, EFalse);
       
   663 
       
   664     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::UpdateDefaultIntensityL - return" ) );
       
   665     }
       
   666 
       
   667 
       
   668 // -----------------------------------------------------------------------------
       
   669 // CHWRMLightService::LightsOnL
       
   670 // -----------------------------------------------------------------------------
       
   671 //
       
   672 void CHWRMLightService::LightsOnL(TInt aTarget,
       
   673 	                   TInt aDuration, 
       
   674 	                   TInt aIntensity,
       
   675 	                   TBool aFadeIn,
       
   676 	                   const RMessage2& aMessage,
       
   677 	                   TBool aTimerInitiated,
       
   678 	                   TBool aActivationInitiated)
       
   679     {
       
   680     COMPONENT_TRACE8(_L( "HWRM Server - CHWRMLightService::LightsOnL(0x%x ,%d, %d, 0x%x, 0x%x, %d, %d )" ), aTarget, 
       
   681                                                                                                               aDuration, 
       
   682                                                                                                               aIntensity, 
       
   683                                                                                                               aFadeIn, 
       
   684                                                                                                               aMessage.Handle(), 
       
   685                                                                                                               aTimerInitiated,
       
   686                                                                                                               aActivationInitiated);
       
   687 
       
   688     CheckCommonParametersL( aTarget, aDuration, aIntensity);
       
   689     
       
   690     TInt modTarget(aTarget);
       
   691     
       
   692     if ( !aActivationInitiated )
       
   693         {
       
   694         modTarget = ModifyTargetMaskL(aTarget, EFalse);
       
   695         }
       
   696 
       
   697     // If there is no target, only complete the message, if any.
       
   698     if ( modTarget )
       
   699         {
       
   700         // If intensity is KHWRMDefaultIntensity, get default intensity from common data or use sensor with sensitivity
       
   701         TInt intensensitivity = aIntensity;
       
   702         TBool useSensor = EFalse;
       
   703         if ( aIntensity == KHWRMDefaultIntensity )
       
   704             {
       
   705             if ( iLightCommonData.SensorEnabled() )
       
   706                 {
       
   707                 intensensitivity = iLightCommonData.DefaultSensitivity();
       
   708                 useSensor = ETrue;
       
   709                 }
       
   710             else
       
   711                 {
       
   712                 // Adaptation will be responsible for setting the correct intensity, do nothing
       
   713                 }
       
   714             }
       
   715             
       
   716         // Check suspended targets, only call plugin for unsuspended ones
       
   717         TInt unsuspendedTargets = GetAndCheckUnsuspendedTargetsL(modTarget);
       
   718             
       
   719         // Call the plugin if any unsuspended targets
       
   720         if ( unsuspendedTargets )
       
   721             {
       
   722             TInt sensorTargets(0);      
       
   723             TInt nonSensorTargets(0);
       
   724             TBool splitCommand(EFalse);
       
   725             
       
   726             if ( useSensor )
       
   727                 {
       
   728                 // If sensor is in use, command may need splitting to sensor and non-sensor commands,
       
   729                 // depending on targets.
       
   730                 sensorTargets = unsuspendedTargets & iLightCommonData.SensorTargets();
       
   731                 nonSensorTargets = unsuspendedTargets & ~sensorTargets;
       
   732                 
       
   733                 if (sensorTargets && nonSensorTargets )
       
   734                     {
       
   735                     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::LightsOnL - Splitting command required" ) );
       
   736                     splitCommand = ETrue;
       
   737                     
       
   738                     // NOTE about split commands:
       
   739                     // The ultimate completion code for messages related to split commands will be whatever
       
   740                     // completion code the split command handled last will have. I.e. if there is an error
       
   741                     // on first command but second command completes properly, whole message completes properly.
       
   742                     // So some errors may go unnoticed by client. However, it should be extremely rare that
       
   743                     // one split command will succeed and one completes with error, if environment is properly 
       
   744                     // configured. OOM case is the only possiblity that comes to mind.
       
   745                     }
       
   746                 }
       
   747             else
       
   748                 {
       
   749                 nonSensorTargets = unsuspendedTargets;
       
   750                 }
       
   751                 
       
   752             if ( sensorTargets )
       
   753                 {
       
   754                 // Only sensor targets, no splitting
       
   755                 HWRMLightCommand::TLightsOnSensorData pluginCommand;
       
   756                 pluginCommand.iTarget = sensorTargets;	
       
   757                 pluginCommand.iSensitivity = intensensitivity;
       
   758                 pluginCommand.iFadeIn = aFadeIn;	
       
   759                 HWRMLightCommand::TLightsOnSensorCmdPackage pckg(pluginCommand);
       
   760                 ExecutePluginCommandL(sensorTargets, aMessage, HWRMLightCommand::ELightsOnSensorCmdId, pckg, splitCommand);
       
   761                 }
       
   762 
       
   763             if ( nonSensorTargets )
       
   764                 {
       
   765                 HWRMLightCommand::TLightsOnData pluginCommand;
       
   766                 pluginCommand.iTarget = nonSensorTargets;	
       
   767                 
       
   768                 // If targeting non-sensor targets when sensor use is indicated, use default intensity always.
       
   769                 if ( useSensor )
       
   770                     {
       
   771                     pluginCommand.iIntensity = KHWRMDefaultIntensity;
       
   772                     }
       
   773                 else
       
   774                     {
       
   775                     pluginCommand.iIntensity = intensensitivity;
       
   776                     }
       
   777                     
       
   778                 pluginCommand.iFadeIn = aFadeIn;	
       
   779                 HWRMLightCommand::TLightsOnCmdPackage pckg(pluginCommand);
       
   780                 ExecutePluginCommandL(nonSensorTargets, aMessage, HWRMLightCommand::ELightsOnCmdId, pckg, splitCommand);
       
   781                 }
       
   782                 
       
   783             // Publish state already here if cleaning up, since no ProcessResponse will come
       
   784             if ( iCleaningUp )
       
   785                 {
       
   786                 iLightCommonData.PublishLightStatus(unsuspendedTargets, CHWRMLight::ELightOn);
       
   787                 }
       
   788             }
       
   789         else
       
   790             {
       
   791             // complete message if suspended, as there will not be ProcessResponseL
       
   792             if ( aMessage.Handle() )
       
   793                 {
       
   794                 aMessage.Complete(KErrNone);
       
   795                 }        
       
   796             }
       
   797         
       
   798         // Use unmodified target in state update as virtual state for inactive targets needs to be preserved.
       
   799         if ( !aActivationInitiated )
       
   800             {
       
   801             UpdateTimersAndStoreStates(aTimerInitiated, aDuration, aTarget, CHWRMLight::ELightOn, aIntensity, aFadeIn, 0, 0);
       
   802             }
       
   803         }
       
   804     else
       
   805         {
       
   806         COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::LightsOnL - Target empty, only complete message" ) );
       
   807 
       
   808         // complete message if one is pending, as there will not be ProcessResponseL
       
   809         if ( aMessage.Handle() )
       
   810             {
       
   811             aMessage.Complete(KErrNone);
       
   812             }        
       
   813         }
       
   814 
       
   815     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::LightsOnL - return" ) );
       
   816     }
       
   817 
       
   818   	
       
   819 // -----------------------------------------------------------------------------
       
   820 // CHWRMLightService::LightsOffL
       
   821 // -----------------------------------------------------------------------------
       
   822 //
       
   823 void CHWRMLightService::LightsOffL(TInt aTarget,
       
   824 	                    TInt aDuration, 
       
   825 	                    TBool aFadeOut,
       
   826 	                    const RMessage2& aMessage,
       
   827 	                    TBool aTimerInitiated,
       
   828 	                    TBool aActivationInitiated)
       
   829     {
       
   830     COMPONENT_TRACE7(_L( "HWRM Server - CHWRMLightService::LightsOffL(0x%x ,%d, %d, 0x%x, %d, %d)" ), aTarget, 
       
   831                                                                                                         aDuration, 
       
   832                                                                                                         aFadeOut, 
       
   833                                                                                                         aMessage.Handle(),
       
   834                                                                                                         aTimerInitiated,
       
   835                                                                                                         aActivationInitiated);
       
   836 
       
   837     CheckCommonParametersL( aTarget, aDuration, KHWRMDefaultIntensity);
       
   838 
       
   839     TInt modTarget(aTarget);
       
   840     
       
   841     if ( !aActivationInitiated )
       
   842         {
       
   843         modTarget = ModifyTargetMaskL(aTarget, ETrue);
       
   844         }
       
   845 
       
   846     // If there is no target, only complete the message, if any.
       
   847     if ( modTarget )
       
   848         {
       
   849         // Check suspended targets, only call plugin for unsuspended ones
       
   850         TInt unsuspendedTargets = GetAndCheckUnsuspendedTargetsL(modTarget);
       
   851 
       
   852         // Call the plugin if any unsuspended targets
       
   853         if ( unsuspendedTargets )
       
   854             {
       
   855             HWRMLightCommand::TLightsOffData pluginCommand;
       
   856             pluginCommand.iTarget = unsuspendedTargets;	
       
   857             pluginCommand.iFadeOut = aFadeOut;	
       
   858             HWRMLightCommand::TLightsOffCmdPackage pckg(pluginCommand);
       
   859             ExecutePluginCommandL(unsuspendedTargets, aMessage, HWRMLightCommand::ELightsOffCmdId, pckg, EFalse);
       
   860 
       
   861             // Publish state already here if cleaning up, since no ProcessResponse will come
       
   862             if ( iCleaningUp )
       
   863                 {
       
   864                 iLightCommonData.PublishLightStatus(unsuspendedTargets, CHWRMLight::ELightOff);
       
   865                 }
       
   866             }
       
   867         else
       
   868             {
       
   869             // complete message if suspended, as there will not be ProcessResponseL
       
   870             if ( aMessage.Handle() )
       
   871                 {
       
   872                 aMessage.Complete(KErrNone);
       
   873                 }        
       
   874             }
       
   875         
       
   876         // Use unmodified target in state update as virtual state for inactive targets needs to be preserved.        
       
   877         if ( !aActivationInitiated )
       
   878             {
       
   879             UpdateTimersAndStoreStates(aTimerInitiated, aDuration, aTarget, CHWRMLight::ELightOff, KHWRMDefaultIntensity, aFadeOut, 0, 0);
       
   880             }
       
   881         }
       
   882     else
       
   883         {
       
   884         COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::LightsOffL - Target empty, only complete message" ) );
       
   885 
       
   886         // complete message if one is pending, as there will not be ProcessResponseL
       
   887         if ( aMessage.Handle() )
       
   888             {
       
   889             aMessage.Complete(KErrNone);
       
   890             }        
       
   891         }
       
   892     
       
   893     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::LightsOffL - return" ) );    
       
   894     }
       
   895   	
       
   896 // -----------------------------------------------------------------------------
       
   897 // CHWRMLightService::LightsBlinkL
       
   898 // -----------------------------------------------------------------------------
       
   899 //
       
   900 void CHWRMLightService::LightsBlinkL(TInt aTarget,
       
   901 	                      TInt aDuration, 
       
   902 	                      TInt aIntensity,
       
   903 	                      TInt aOnCycleTime,
       
   904 	                      TInt aOffCycleTime,
       
   905 	                      const RMessage2& aMessage,
       
   906 	                      TBool aTimerInitiated,
       
   907 	                      TBool aActivationInitiated)
       
   908     {
       
   909     COMPONENT_TRACE9(_L( "HWRM Server - CHWRMLightService::LightsBlinkL(0x%x ,%d, %d, %d, %d, 0x%x, %d, %d)" ), aTarget, 
       
   910                                                                                                                   aDuration, 
       
   911                                                                                                                   aIntensity, 
       
   912                                                                                                                   aOnCycleTime, 
       
   913                                                                                                                   aOffCycleTime, 
       
   914                                                                                                                   aMessage.Handle(),
       
   915                                                                                                                   aTimerInitiated,
       
   916                                                                                                                   aActivationInitiated);
       
   917 
       
   918     CheckCommonParametersL( aTarget, aDuration, aIntensity);
       
   919     CheckDurationParameterL( aOnCycleTime );
       
   920     CheckDurationParameterL( aOffCycleTime );
       
   921 
       
   922     // If one of the cycle durations is default, both must be
       
   923     if ( aOnCycleTime == KHWRMDefaultCycleTime || aOffCycleTime == KHWRMDefaultCycleTime )
       
   924         {
       
   925         if ( aOnCycleTime != aOffCycleTime )
       
   926             {
       
   927             COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::LightsBlinkL - Default cycle duration mismatch (%d,%d)" ), aOnCycleTime, aOffCycleTime);
       
   928             User::Leave(KErrArgument);
       
   929             }
       
   930         }
       
   931 
       
   932     TInt modTarget(aTarget);
       
   933     
       
   934     if ( !aActivationInitiated )
       
   935         {
       
   936         modTarget = ModifyTargetMaskL(aTarget, EFalse);
       
   937         }
       
   938 
       
   939     // If there is no target, only complete the message, if any.
       
   940     if ( modTarget )
       
   941         {
       
   942         
       
   943         // Check suspended targets, only call plugin for unsuspended ones
       
   944         TInt unsuspendedTargets = GetAndCheckUnsuspendedTargetsL(modTarget);
       
   945             
       
   946         // Call the plugin if any unsuspended targets
       
   947         if ( unsuspendedTargets )
       
   948             {
       
   949             // Call the plugin    
       
   950             HWRMLightCommand::TLightsBlinkData pluginCommand;
       
   951             pluginCommand.iTarget = unsuspendedTargets;	
       
   952             pluginCommand.iIntensity = aIntensity;
       
   953             pluginCommand.iOnCycleDuration = aOnCycleTime;
       
   954             pluginCommand.iOffCycleDuration = aOffCycleTime;
       
   955             HWRMLightCommand::TLightsBlinkCmdPackage pckg(pluginCommand);
       
   956             ExecutePluginCommandL(unsuspendedTargets, aMessage, HWRMLightCommand::ELightsBlinkCmdId, pckg, EFalse);
       
   957 
       
   958             // Publish state already here if cleaning up, since no ProcessResponse will come
       
   959             if ( iCleaningUp )
       
   960                 {
       
   961                 iLightCommonData.PublishLightStatus(unsuspendedTargets, CHWRMLight::ELightBlink);
       
   962                 }
       
   963             }
       
   964         else
       
   965             {
       
   966             // complete message if suspended, as there will not be ProcessResponseL
       
   967             if ( aMessage.Handle() )
       
   968                 {
       
   969                 aMessage.Complete(KErrNone);
       
   970                 }        
       
   971             }
       
   972             
       
   973         // Use unmodified target in state update as virtual state for inactive targets needs to be preserved.    
       
   974         if ( !aActivationInitiated )
       
   975             {
       
   976             UpdateTimersAndStoreStates(aTimerInitiated, aDuration, aTarget, CHWRMLight::ELightBlink, aIntensity, EFalse, aOnCycleTime, aOffCycleTime);
       
   977             }
       
   978         }
       
   979     else
       
   980         {
       
   981         COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::LightsBlinkL - Target empty, only complete message" ) );
       
   982 
       
   983         // complete message if one is pending, as there will not be ProcessResponseL
       
   984         if ( aMessage.Handle() )
       
   985             {
       
   986             aMessage.Complete(KErrNone);
       
   987             }        
       
   988         }
       
   989 
       
   990     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::LightsBlinkL - return" ) );
       
   991     
       
   992     }
       
   993 
       
   994 // -----------------------------------------------------------------------------
       
   995 // CHWRMLightService::ProcessResponseL
       
   996 // Handles Light requests responses.
       
   997 // CommandId not required as all commands have same response value
       
   998 // -----------------------------------------------------------------------------
       
   999 //
       
  1000 void CHWRMLightService::ProcessResponseL( TInt aCommandId,  TUint8 aTransId, TDesC8& aData, TBool aTimeout )
       
  1001     {
       
  1002     COMPONENT_TRACE4(_L( "HWRM Server - CHWRMLightService::ProcessResponseL(0x%x ,0x%x, <data>, 0x%x)" ), aCommandId, aTransId, aTimeout );
       
  1003 
       
  1004     TInt pluginErr(KErrNone);  // Error came from plugin as data (or timeout). Used to complete RMessage.
       
  1005     TInt contextErr(KErrNone);  // Error in context, i.e. bad handle or descriptor. Used to leave.
       
  1006 
       
  1007     // Unpack the package. All light messages contain only possible error code
       
  1008     // in return package.
       
  1009     if ( !aTimeout && aData.Size() != sizeof(TInt) )
       
  1010         {
       
  1011         COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::ProcessResponseL - Data size mismatch, expected: 0x%x, got 0x%x" ), sizeof(TInt), aData.Size() );
       
  1012         contextErr = KErrBadDescriptor;
       
  1013         pluginErr = KErrBadDescriptor;
       
  1014         }      
       
  1015     else
       
  1016         {        
       
  1017         // If response was caused by timeout, set that as the error.
       
  1018         // Timeout handled this way instead of just returning KErrTimeout in aData because
       
  1019         // aData format is not known in plugin handler and in theoretically some new commands
       
  1020         // may also return different response than just error code in future.
       
  1021         if ( aTimeout )
       
  1022             {
       
  1023             pluginErr = KErrTimedOut;            
       
  1024             }
       
  1025         else
       
  1026             {
       
  1027             HWRMLightCommand::TErrorCodeResponsePackage errPckg;
       
  1028             errPckg.Copy(aData);
       
  1029             pluginErr = errPckg();
       
  1030             }
       
  1031 
       
  1032         if ( pluginErr != KErrNone )
       
  1033             {
       
  1034             COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ProcessResponseL - Error: %d" ), pluginErr );        
       
  1035             }
       
  1036             
       
  1037         // Get target of the request
       
  1038         THWRMLightPluginRequestData* data = static_cast<THWRMLightPluginRequestData*>(iTransactionList->FindTransaction(aTransId, EFalse));
       
  1039         
       
  1040         // Update light status PS state value
       
  1041         if ( data && (pluginErr == KErrNone) )
       
  1042             {   
       
  1043             // Do not publish statuses after cleanup has started, as those would incorrectly override statuses set by cleanup
       
  1044             if ( !iCleaningUp )
       
  1045                 {                
       
  1046                 switch ( aCommandId )
       
  1047                     {
       
  1048                     case HWRMLightCommand::ELightsOnCmdId:
       
  1049                         {
       
  1050                         iLightCommonData.PublishLightStatus(data->iTarget, CHWRMLight::ELightOn);	    
       
  1051                         break;
       
  1052                         }
       
  1053                     case HWRMLightCommand::ELightsOnSensorCmdId:
       
  1054                         {
       
  1055                         iLightCommonData.PublishLightStatus(data->iTarget, CHWRMLight::ELightOn);	    
       
  1056                         break;
       
  1057                         }
       
  1058                     case HWRMLightCommand::ELightsBlinkCmdId:
       
  1059                         {
       
  1060                         iLightCommonData.PublishLightStatus(data->iTarget, CHWRMLight::ELightBlink);	    
       
  1061                         break;
       
  1062                         }
       
  1063                     case HWRMLightCommand::ELightsOffCmdId:
       
  1064                         {
       
  1065                         iLightCommonData.PublishLightStatus(data->iTarget, CHWRMLight::ELightOff);	    
       
  1066                         break;
       
  1067                         }
       
  1068                     case HWRMLightCommand::ESetLightsIntensityCmdId:
       
  1069                         {
       
  1070                         // No need to publish status in this case
       
  1071                         break;
       
  1072                         }
       
  1073                     case HWRMLightCommand::ESetLightsSensorSensitivityCmdId:
       
  1074                         {
       
  1075                         // No need to publish status in this case
       
  1076                         break;
       
  1077                         }
       
  1078                     
       
  1079                     case HWRMLightCommand::ELightsSetColorCmdId:
       
  1080                         {
       
  1081                         COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::ProcessResponseL - ELightsSetColorCmdId iSplitCommand=0x%x iSplitCommandId=0x%x"), data->iSplitCommand, data->iSplitCommandId);
       
  1082                         if(data->iSplitCommand)
       
  1083                             {
       
  1084                             switch(data->iSplitCommandId)
       
  1085                                 {
       
  1086                                 case HWRMLightCommand::ELightsOnCmdId:
       
  1087                                     {
       
  1088                                     THWRMLightsOnData *lightsOnData = (THWRMLightsOnData*)data->iSplitCommandData;
       
  1089                                     if(lightsOnData)
       
  1090                                         {
       
  1091                                         LightsOnL( lightsOnData->iTarget, 
       
  1092                                 	               lightsOnData->iDuration, 
       
  1093                                 	               lightsOnData->iIntensity, 
       
  1094                                 	               lightsOnData->iFadeIn, 
       
  1095                                 	               data->iRequestMessage );
       
  1096                                         }
       
  1097                                     break;
       
  1098                                     }
       
  1099                                 case HWRMLightCommand::ELightsBlinkCmdId:
       
  1100                                     {
       
  1101                                     THWRMLightsBlinkData *lightsBlinkData = (THWRMLightsBlinkData*)data->iSplitCommandData;
       
  1102                                     if(lightsBlinkData)
       
  1103                                         {
       
  1104                                         LightsBlinkL( lightsBlinkData->iTarget,
       
  1105                                                       lightsBlinkData->iDuration, 
       
  1106                                                       lightsBlinkData->iIntensity, 
       
  1107                                                       lightsBlinkData->iOnCycleDuration, 
       
  1108                                                       lightsBlinkData->iOffCycleDuration, 
       
  1109                                                       data->iRequestMessage );
       
  1110                                         }
       
  1111                                     break;
       
  1112                                     }
       
  1113                                 default:
       
  1114                                     {
       
  1115                                     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ProcessResponseL - ELightsSetColorCmdId Invalid iSplitCommandId=0x%x"), data->iSplitCommandId);
       
  1116                                     break;
       
  1117                                     }
       
  1118                                 
       
  1119                                 }
       
  1120                             }
       
  1121                         break;
       
  1122                         }
       
  1123                     default:
       
  1124                         {
       
  1125                         iLightCommonData.PublishLightStatus(data->iTarget, CHWRMLight::ELightStatusUnknown);	    
       
  1126                         break;
       
  1127                         }
       
  1128                     }
       
  1129                 }
       
  1130             }
       
  1131         else
       
  1132             {
       
  1133             // No data found or error in plugin. 
       
  1134             TInt target = KHWRMAllTargets;
       
  1135             
       
  1136             if ( data )
       
  1137                 {
       
  1138                 target = data->iTarget;    
       
  1139                 }
       
  1140 
       
  1141             if(aCommandId != HWRMLightCommand::ELightsSetColorCmdId)
       
  1142                 {
       
  1143                 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ProcessResponseL - Set ELightStatusUnknown CommandId=%d"), aCommandId);
       
  1144                 iLightCommonData.PublishLightStatus(target, CHWRMLight::ELightStatusUnknown);	            
       
  1145                 }
       
  1146             }
       
  1147         }
       
  1148 
       
  1149     // Complete request and delete transaction
       
  1150     contextErr = CompleteRequest(aTransId, pluginErr);    
       
  1151 
       
  1152     // Leave if there is error in context    
       
  1153     User::LeaveIfError(contextErr);
       
  1154 
       
  1155 
       
  1156     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ProcessResponseL - return" ) );
       
  1157     }
       
  1158 
       
  1159    
       
  1160 // -----------------------------------------------------------------------------
       
  1161 // CHWRMLightService::UpdateTimersAndStoreStates
       
  1162 // Stores base states of target data objects as necessary.
       
  1163 // This is called from various lights setting methods as opposed to 
       
  1164 // ProcessResponseL so that base state will be correct even if timer fires
       
  1165 // before plugin call completes. If plugin subsequently calls ProcessResponseL
       
  1166 // with error condition, the stored base state can unfortunately be
       
  1167 // inconsistent with real state, but that should be no problem,
       
  1168 // as the stored state is the last state user has requested.
       
  1169 // -----------------------------------------------------------------------------
       
  1170 //
       
  1171 void CHWRMLightService::UpdateTimersAndStoreStates(TBool aTimerInitiated,
       
  1172     	                                TInt aDuration,
       
  1173     	                                TInt aTarget,
       
  1174     	                                CHWRMLight::TLightStatus aState,
       
  1175                                         TInt aIntensity,
       
  1176                                         TBool aFade,
       
  1177                                         TInt aOnCycleTime,
       
  1178                                         TInt aOffCycleTime)
       
  1179     {
       
  1180     COMPONENT_TRACE9(_L( "HWRM Server - CHWRMLightService::UpdateTimersAndStoreStates(0x%x, %d, 0x%x, 0x%x, %d, 0x%x, %d, %d)" ), aTimerInitiated, aDuration, aTarget, aState, aIntensity, aFade, aOnCycleTime, aOffCycleTime );
       
  1181 
       
  1182     // For system targets, use full system target
       
  1183     TInt updateTarget(aTarget);
       
  1184     if ( aTarget & CHWRMLight::ESystemTarget)
       
  1185         {
       
  1186         updateTarget = iFullSystemTarget;
       
  1187         }
       
  1188 
       
  1189 
       
  1190     // Start timer or update base state if call is not timer initiated.
       
  1191     if ( !aTimerInitiated )
       
  1192         {
       
  1193         if ( aDuration != KHWRMInfiniteDuration )
       
  1194             {
       
  1195             // Start timer(s)
       
  1196             for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
  1197                 {
       
  1198                 if ( (updateTarget & iTargets[i].iTarget) == iTargets[i].iTarget)
       
  1199                     {
       
  1200                     COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::UpdateTimersAndStoreStates - Starting new timer with duration %d for target index %d" ), aDuration, i );    
       
  1201                     iTargets[i].iTimer->Set(aDuration*1000);
       
  1202 
       
  1203                     if ( iTargets[i].iBaseState.iState == CHWRMLight::ELightStatusUnknown )
       
  1204                         {
       
  1205                         // If duration based call and base state unknown,
       
  1206                         // set base state to off to avoid endless lighting.
       
  1207                         iTargets[i].iBaseState.iState = CHWRMLight::ELightOff;
       
  1208                         }
       
  1209                     }
       
  1210                 }
       
  1211             }
       
  1212         else
       
  1213             {
       
  1214             // Since this is not timer related call, update base data and cancel related timers.
       
  1215             for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
  1216                 {
       
  1217                 if ( (updateTarget & iTargets[i].iTarget) == iTargets[i].iTarget )
       
  1218                     {
       
  1219                     COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::UpdateTimersAndStoreStates - Setting base state (0x%x) for target index %d" ), aState, i );    
       
  1220                     iTargets[i].iBaseState.iIntensity = aIntensity;	
       
  1221                     iTargets[i].iBaseState.iState = aState;
       
  1222                     iTargets[i].iBaseState.iFade = aFade;
       
  1223                     iTargets[i].iBaseState.iOnCycleDuration = aOnCycleTime;
       
  1224                     iTargets[i].iBaseState.iOffCycleDuration = aOffCycleTime;
       
  1225                     iTargets[i].iTimer->Cancel();
       
  1226                     }
       
  1227                 }
       
  1228             }
       
  1229         }
       
  1230     else
       
  1231         {
       
  1232         // no else, this is for timer related calls, so no need to cancel or set timers or base data
       
  1233         COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::UpdateTimersAndStoreStates - Timer/resume initiated call, base states not updated" ) );    
       
  1234         }
       
  1235         
       
  1236     // Store last states on every call
       
  1237     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
  1238         {
       
  1239         if ( (updateTarget & iTargets[i].iTarget) == iTargets[i].iTarget )
       
  1240             {
       
  1241             iTargets[i].iLastState.iIntensity = aIntensity;	
       
  1242             iTargets[i].iLastState.iState = aState;
       
  1243             iTargets[i].iLastState.iFade = aFade;
       
  1244             iTargets[i].iLastState.iOnCycleDuration = aOnCycleTime;
       
  1245             iTargets[i].iLastState.iOffCycleDuration = aOffCycleTime;
       
  1246             }
       
  1247         }
       
  1248     
       
  1249     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::UpdateTimersAndStoreStates - return" ) );    
       
  1250     }
       
  1251 
       
  1252 // -----------------------------------------------------------------------------
       
  1253 // CHWRMLightService::GenericTimerFired
       
  1254 // Handles Light cutoff timer firing. 
       
  1255 // aCutOff is not used with lights.
       
  1256 // -----------------------------------------------------------------------------
       
  1257 //
       
  1258 void CHWRMLightService::GenericTimerFired(TInt aTimerId, TBool /*aCutOff*/)
       
  1259     {
       
  1260     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::GenericTimerFired(0x%x)" ), aTimerId );
       
  1261 
       
  1262     // Return the base state for target
       
  1263     RestoreState(iTargets[aTimerId], EStateChangeBaseState, ETrue);
       
  1264 
       
  1265     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::GenericTimerFired - return" ) );    
       
  1266     }
       
  1267     
       
  1268 
       
  1269 // -----------------------------------------------------------------------------
       
  1270 // CHWRMLightService::RestoreState
       
  1271 // Restores target state. aTargetData is always single target data, except in case of 
       
  1272 // restoring default state. In that case aDefaultTargetMask must contain a mask and
       
  1273 // aTargetData is ignored. It must still be a valid reference.
       
  1274 // aDefaultTargetMask may not contain both system and non-system targets.
       
  1275 // Note: If restored state used default intensity, restored state could potentially
       
  1276 // have different intensity or use sensor, if user has changed the settings
       
  1277 // while timer was active. This is correct behaviour however.
       
  1278 // -----------------------------------------------------------------------------
       
  1279 //
       
  1280 void CHWRMLightService::RestoreState(const THWRMLightTargetData& aTargetData, 
       
  1281                                      TStateChangeType aStateType,
       
  1282                                      TBool aTimerInitiated,
       
  1283     	                             TInt aDefaultTargetMask,
       
  1284     	                             const RMessage2* aMessage,
       
  1285 	                                 TBool aActivationInitiated )
       
  1286     {
       
  1287     COMPONENT_TRACE7(_L( "HWRM Server - CHWRMLightService::RestoreState(0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x)" ), aTargetData.iTarget, 
       
  1288                                                                                                                   aStateType, 
       
  1289                                                                                                                   aTimerInitiated, 
       
  1290                                                                                                                   aDefaultTargetMask,
       
  1291                                                                                                                   aMessage ? aMessage->Handle() : 0,
       
  1292                                                                                                                   aActivationInitiated );
       
  1293 
       
  1294     TInt err(KErrNone);
       
  1295         
       
  1296     RMessage2 message;
       
  1297     
       
  1298     // If non-null message pointer is provided, use that. Otherwise empty dummy message is used.
       
  1299     if ( aMessage )
       
  1300         {
       
  1301         message = *aMessage;
       
  1302         }
       
  1303     
       
  1304     // Get correct state data
       
  1305     TInt duration(KHWRMInfiniteDuration);
       
  1306     TInt targetMask(aTargetData.iTarget);
       
  1307     const THWRHLightTargetStateData* targetStateData = NULL;
       
  1308     
       
  1309 
       
  1310     TUint color = KHWRMDefaultColor;
       
  1311     
       
  1312     switch ( aStateType )
       
  1313         {
       
  1314         case EStateChangeFrozenState: 
       
  1315             {
       
  1316             COMPONENT_TRACE1(_L("HWRM Server - CHWRMLightService::RestoreState - EStateChangeFrozenState"));
       
  1317             targetStateData = &(aTargetData.iFrozenState);
       
  1318             duration = aTargetData.iFrozenTime;
       
  1319             color = targetStateData->iColor;
       
  1320             break;
       
  1321             }
       
  1322         case EStateChangeBaseState: 
       
  1323             {
       
  1324             COMPONENT_TRACE1(_L("HWRM Server - CHWRMLightService::RestoreState - EStateChangeBaseState"));
       
  1325             targetStateData = &(aTargetData.iBaseState);
       
  1326             color = targetStateData->iColor;
       
  1327             COMPONENT_TRACE2(_L("HWRM Server - CHWRMLightService::RestoreState - EStateChangeBaseState color(0x%x)"), color);
       
  1328             break;
       
  1329             }
       
  1330         case EStateChangeLastState: 
       
  1331             {
       
  1332             COMPONENT_TRACE1(_L("HWRM Server - CHWRMLightService::RestoreState - EStateChangeLastState"));
       
  1333             targetStateData = &(aTargetData.iLastState);
       
  1334             color = targetStateData->iColor;
       
  1335             break;
       
  1336             }
       
  1337         case EStateChangeDefaultState: 
       
  1338             {
       
  1339             COMPONENT_TRACE1(_L("HWRM Server - CHWRMLightService::RestoreState - EStateChangeDefaultState"));
       
  1340             // Target mask may not contain both system and non-system targets
       
  1341             targetMask = aDefaultTargetMask;         
       
  1342             if (targetMask & iFullSystemTarget)
       
  1343                 {
       
  1344                 COMPONENT_TRACE1(_L("HWRM Server - CHWRMLightService::RestoreState - GetData"));
       
  1345                 iDefaultState.iState = iLightCommonData.DefaultState();
       
  1346                 }
       
  1347             else
       
  1348                 {
       
  1349                 iDefaultState.iState = CHWRMLight::ELightOff;
       
  1350                 }
       
  1351 
       
  1352             //Restore color only if light state is off.
       
  1353             if(iDefaultState.iState != CHWRMLight::ELightOff)
       
  1354                 {
       
  1355                 color = KHWRMInvalidColorMask;
       
  1356                 }
       
  1357             
       
  1358             COMPONENT_TRACE2(_L("HWRM Server - CHWRMLightService::RestoreState - DefaultState=0x%x"), iDefaultState.iState);
       
  1359             targetStateData = &iDefaultState;  
       
  1360             break;
       
  1361             }
       
  1362         default: 
       
  1363             {
       
  1364             // Cannot get here. Panic if it does for some reason.
       
  1365             COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::RestoreState - Invalid state: 0x%x - Panicing" ), aStateType );
       
  1366             User::Panic(KPanicCategory, EPanicBadHandle);
       
  1367             break;
       
  1368             }
       
  1369         }
       
  1370         
       
  1371     // If state to be restored is unknown, default state is restored instead.
       
  1372     // Basically this is issue only when duration based call is used prior to infinite calls.
       
  1373     //
       
  1374     // Note: This can result in different targets being restored to different default
       
  1375     //       states, if default state changes between target restorings. Should be
       
  1376     //       extremely rare though, considering that most light users will have set
       
  1377     //       some base state anyway. 
       
  1378     if ( targetStateData->iState == CHWRMLight::ELightStatusUnknown )
       
  1379         {
       
  1380         // Default state of non-system targets is always off
       
  1381         if (targetMask & iFullSystemTarget)
       
  1382             {
       
  1383             COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::RestoreState - State for state type (0x%x) unknown - Using default state" ), aStateType );
       
  1384             iDefaultState.iState = iLightCommonData.DefaultState();
       
  1385             targetStateData = &iDefaultState;
       
  1386             }
       
  1387         else
       
  1388             {
       
  1389             iDefaultState.iState = CHWRMLight::ELightOff;
       
  1390             targetStateData = &iDefaultState;
       
  1391             }
       
  1392         }
       
  1393     
       
  1394     if(color != KHWRMInvalidColorMask)
       
  1395         {
       
  1396         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::RestoreState - Restore color with 0x%x" ), color);
       
  1397         TRAP_IGNORE(SetColorL(targetMask, color, message, aActivationInitiated));
       
  1398         }
       
  1399     else
       
  1400         {
       
  1401         COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::RestoreState - Invalid Color Mask"));
       
  1402         }
       
  1403 
       
  1404     // Call appropriate method to restore target state
       
  1405     switch ( targetStateData->iState )
       
  1406         {
       
  1407         case CHWRMLight::ELightOn:
       
  1408             {
       
  1409             TRAP(err, LightsOnL(targetMask, duration, targetStateData->iIntensity, targetStateData->iFade, message, aTimerInitiated, aActivationInitiated));
       
  1410             break;
       
  1411             }
       
  1412         case CHWRMLight::ELightOff:
       
  1413             {
       
  1414             TRAP(err, LightsOffL(targetMask, duration, targetStateData->iFade, message, aTimerInitiated, aActivationInitiated));
       
  1415             break;
       
  1416             }
       
  1417         case CHWRMLight::ELightBlink:
       
  1418             {
       
  1419             TRAP(err, LightsBlinkL(targetMask, duration, targetStateData->iIntensity, targetStateData->iOnCycleDuration, targetStateData->iOffCycleDuration, message, aTimerInitiated, aActivationInitiated));
       
  1420             break;
       
  1421             }
       
  1422         default:
       
  1423             {
       
  1424             COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::RestoreState - Invalid target status: 0x%x" ), targetStateData->iState );
       
  1425             break;
       
  1426             }
       
  1427         }
       
  1428 
       
  1429     if ( err != KErrNone )
       
  1430         {
       
  1431         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::RestoreState - Error in restoring light state: %d" ), err );
       
  1432         
       
  1433         // complete message if one is pending, as there will not be ProcessResponseL
       
  1434 		if ( aMessage )
       
  1435 			{
       
  1436         	if ( aMessage->Handle() )
       
  1437             	{
       
  1438             	aMessage->Complete(KErrNone);
       
  1439             	}
       
  1440 			}
       
  1441         }
       
  1442         
       
  1443     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::RestoreState - return" ) );    
       
  1444     }
       
  1445 
       
  1446 // -----------------------------------------------------------------------------
       
  1447 // CHWRMLightService::CheckCommonParametersL
       
  1448 // -----------------------------------------------------------------------------
       
  1449 //
       
  1450 void CHWRMLightService::CheckCommonParametersL(TInt aTarget, 
       
  1451                                                TInt aDuration, 
       
  1452                                                TInt aIntensity) const
       
  1453     {
       
  1454     CheckTargetParameterL(aTarget);
       
  1455     
       
  1456     CheckDurationParameterL(aDuration);    
       
  1457 
       
  1458     if ( aIntensity != KHWRMDefaultIntensity && (aIntensity < KHWRMLightMinIntensity || aIntensity > KHWRMLightMaxIntensity) )
       
  1459         {
       
  1460         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::CheckCommonParametersL - Invalid Intensity: %d" ), aIntensity );    
       
  1461         User::Leave(KErrArgument);
       
  1462         }
       
  1463     }
       
  1464         
       
  1465 // -----------------------------------------------------------------------------
       
  1466 // CHWRMLightService::CheckDurationParameterL
       
  1467 // -----------------------------------------------------------------------------
       
  1468 //
       
  1469 void CHWRMLightService::CheckDurationParameterL(TInt aDuration) const
       
  1470     {
       
  1471     if ( aDuration < 0 || aDuration > KHWRMLightMaxDuration )
       
  1472         {
       
  1473         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::CheckCommonParametersL - Invalid Duration: %d" ), aDuration );    
       
  1474         User::Leave(KErrArgument);
       
  1475         }
       
  1476     }
       
  1477 
       
  1478 // -----------------------------------------------------------------------------
       
  1479 // CHWRMLightService::CheckTargetParameterL
       
  1480 // -----------------------------------------------------------------------------
       
  1481 //
       
  1482 void CHWRMLightService::CheckTargetParameterL(TInt aTarget) const
       
  1483     {
       
  1484     // All target combinations specified in supported targets mask + system target are legal
       
  1485     if ( (~(iSupportedTargets | CHWRMLight::ESystemTarget) & aTarget) || aTarget == CHWRMLight::ENoTarget  )
       
  1486         {
       
  1487         COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::CheckCommonParametersL - Invalid Target: 0x%x. (Supported: 0x%x)" ), aTarget, iSupportedTargets );    
       
  1488         User::Leave(KErrNotSupported);
       
  1489         }
       
  1490     }
       
  1491         
       
  1492 // -----------------------------------------------------------------------------
       
  1493 // CHWRMLightService::SuspendSubResource
       
  1494 // Suspends light target
       
  1495 // Suspension doesn't actually change light state, it is responsibility of the
       
  1496 // active reserver of the target to change the state to desirable one.
       
  1497 // -----------------------------------------------------------------------------
       
  1498 //
       
  1499 void CHWRMLightService::SuspendSubResource(TInt aSubResource)
       
  1500     {
       
  1501     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::SuspendSubResource(0x%x)" ), aSubResource );
       
  1502 
       
  1503     iTargets[aSubResource].iSuspended = ETrue;
       
  1504 
       
  1505     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::SuspendSubResource - return" ) );
       
  1506     }
       
  1507     
       
  1508 // -----------------------------------------------------------------------------
       
  1509 // CHWRMLightService::ResumeSubResource
       
  1510 // Resumes suspended light target 
       
  1511 // -----------------------------------------------------------------------------
       
  1512 //
       
  1513 void CHWRMLightService::ResumeSubResource(TInt aSubResource)
       
  1514     {
       
  1515     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ResumeSubResource(0x%x)" ), aSubResource );
       
  1516     
       
  1517     iTargets[aSubResource].iSuspended = EFalse;
       
  1518     
       
  1519     // Restore last state
       
  1520     // Set aTimerInitiated to ETrue on RestoreState call, so that base states and timers will 
       
  1521     // not be affected by this state change.
       
  1522     RestoreState(iTargets[aSubResource], EStateChangeLastState, ETrue);
       
  1523     
       
  1524     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ResumeSubResource - return" ) );
       
  1525     }
       
  1526     
       
  1527 // -----------------------------------------------------------------------------
       
  1528 // CHWRMLightService::ActivateSubResource
       
  1529 // If subresource is activated, restore its state. 
       
  1530 // If it is deactivated, turn it off
       
  1531 // -----------------------------------------------------------------------------
       
  1532 //
       
  1533 void CHWRMLightService::ActivateSubResource(TInt aSubResource, TBool aActivate)
       
  1534     {
       
  1535     COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::ActivateSubResource(0x%x, 0x%x)" ), aSubResource, aActivate );
       
  1536     
       
  1537     RMessage2 message; // dummy message
       
  1538 
       
  1539     // Set aActivationIntiated to ETrue on RestoreState/lights off calls, so that target is not recursively modified,
       
  1540     //  and states/timers will not be affected by this call.
       
  1541     
       
  1542     if (aActivate)
       
  1543         {
       
  1544         RestoreState(iTargets[aSubResource], EStateChangeLastState, ETrue, CHWRMLight::ENoTarget, &message, ETrue);
       
  1545         }
       
  1546     else
       
  1547         {
       
  1548         // Just turn target off 
       
  1549         TRAPD(err, LightsOffL(iTargets[aSubResource].iTarget, KHWRMInfiniteDuration, iDefaultState.iFade, message, ETrue, ETrue));
       
  1550         
       
  1551         if ( err != KErrNone )
       
  1552             {
       
  1553             COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ActivateSubResource - error turning lights off: %d" ), err );
       
  1554             }
       
  1555         }
       
  1556     }
       
  1557 
       
  1558 // -----------------------------------------------------------------------------
       
  1559 // CHWRMLightService::GetAndCheckUnsuspendedTargetsL
       
  1560 // Checks which targets of specified aTarget mask are unsuspended and returns
       
  1561 // a new mask of those targets.
       
  1562 // -----------------------------------------------------------------------------
       
  1563 //
       
  1564 TInt CHWRMLightService::GetAndCheckUnsuspendedTargetsL(TInt aTarget) const
       
  1565     {
       
  1566     TInt unsuspendedTargets(0);
       
  1567     
       
  1568     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
  1569         {
       
  1570         if ( !iTargets[i].iSuspended && (aTarget & iTargets[i].iTarget) )
       
  1571             {
       
  1572             unsuspendedTargets |= iTargets[i].iTarget;
       
  1573             }
       
  1574         }
       
  1575    
       
  1576     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::GetAndCheckUnsuspendedTargetsL: 0x%x" ), unsuspendedTargets );
       
  1577     
       
  1578     // If there are any unsuspended targets, check reservation status for those.
       
  1579     if ( unsuspendedTargets )
       
  1580         {
       
  1581         // Check reservation.
       
  1582         if ( iReservationHandler->IsReserved(this, unsuspendedTargets) )
       
  1583             {
       
  1584             User::Leave(KErrInUse);
       
  1585             }
       
  1586         }
       
  1587         
       
  1588     return unsuspendedTargets;
       
  1589     }
       
  1590 
       
  1591 // -----------------------------------------------------------------------------
       
  1592 // CHWRMLightService::ExecutePluginCommandL
       
  1593 // Executes a plugin command and handles related transaction.
       
  1594 // -----------------------------------------------------------------------------
       
  1595 //
       
  1596 void CHWRMLightService::ExecutePluginCommandL(TInt aTarget,
       
  1597                           const RMessage2& aMessage,
       
  1598                           TInt aCommandId,
       
  1599                           TDesC8& aPckg,
       
  1600                           TBool aSplitCommand,
       
  1601                           TInt aSplitCommandId , 
       
  1602                           TAny* aSplitCommandData  )
       
  1603     {
       
  1604     // Create new data (TransId is updated later)
       
  1605     THWRMPluginRequestData* data = new (ELeave) THWRMLightPluginRequestData(aTarget, aMessage, 0, aCommandId, aSplitCommand, aSplitCommandId, aSplitCommandData);
       
  1606     CleanupStack::PushL( data );
       
  1607     
       
  1608     // When cleaning up, do not set callback as ProcessResponse would result in panic
       
  1609     if ( iCleaningUp )
       
  1610         {
       
  1611         data->iTransId = iPluginHandler->ProcessCommandL(aCommandId, aPckg, NULL);
       
  1612         }
       
  1613     else
       
  1614         {        
       
  1615         data->iTransId = iPluginHandler->ProcessCommandL(aCommandId, aPckg, this);
       
  1616         }
       
  1617     
       
  1618     // data still needed, do not destroy, just pop
       
  1619     CleanupStack::Pop( data );
       
  1620 
       
  1621     // Add data to list
       
  1622     iTransactionList->AddTransaction( data );
       
  1623     }
       
  1624     
       
  1625 // -----------------------------------------------------------------------------
       
  1626 // CHWRMLightService::SystemLightsOn
       
  1627 // Turns all unreserved system lights on.
       
  1628 // Custom lights are assumed to be specialty lights and therefore not grouped
       
  1629 // for this common turn on of lights.
       
  1630 // -----------------------------------------------------------------------------
       
  1631 //
       
  1632 void CHWRMLightService::SystemLightsOn()    
       
  1633     {   
       
  1634     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::SystemLightsOn()" ) );
       
  1635 
       
  1636     TInt err(KErrNone);
       
  1637 
       
  1638     RMessage2 dummy;
       
  1639     
       
  1640     TRAP(err, LightsOnL(CHWRMLight::ESystemTarget, KHWRMInfiniteDuration, KHWRMDefaultIntensity, EFalse, dummy ));
       
  1641 
       
  1642     if ( err != KErrNone )
       
  1643         {
       
  1644         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::SystemLightsOn - Error setting lights on: %d" ), err );
       
  1645         }
       
  1646 
       
  1647     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::SystemLightsOn - return" ) );
       
  1648     }
       
  1649 
       
  1650 
       
  1651 // -----------------------------------------------------------------------------
       
  1652 // CHWRMLightService::ModifyTargetMaskL
       
  1653 // Modifies target mask depending on current device configuration
       
  1654 // Note: Recursive calls to this method caused by activation callbacks from
       
  1655 //       ReservationHandler must be avoided. Therefore target is only modified
       
  1656 //       initially and activation caused calls will not call this method.
       
  1657 // -----------------------------------------------------------------------------
       
  1658 //
       
  1659 TInt CHWRMLightService::ModifyTargetMaskL(TInt aTarget, TBool aLightsOff)
       
  1660     {
       
  1661     TInt modifiedMask(CHWRMLight::ENoTarget);
       
  1662     TInt target(aTarget);
       
  1663     
       
  1664     if ( aTarget & CHWRMLight::ESystemTarget )
       
  1665         {
       
  1666         target = iFullSystemTarget;
       
  1667         }
       
  1668 
       
  1669     // Non-system target modifications are passed directly to plugin
       
  1670     modifiedMask = iLightCommonData.ModifyTargetL( target );
       
  1671     
       
  1672     // Make sure modified mask doesn't contain any targets not in original mask,
       
  1673     // as this can cause problems with ReservationHandler and is therefore not supported.
       
  1674     modifiedMask &= target;
       
  1675 
       
  1676     // Update active mask to reservation handler
       
  1677     iReservationHandler->UpdateActiveMask(target, modifiedMask, this);    
       
  1678 
       
  1679     // If command is lights off, do not return modified mask but the original 
       
  1680     // or reservation corrected system mask, as lights off is ok for inactive targets.
       
  1681     if ( aLightsOff )
       
  1682         {
       
  1683         COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::ModifyTargetMaskL - Lights off, target modification only used for activation purposes" ) );
       
  1684         modifiedMask = target;
       
  1685         }
       
  1686         
       
  1687     if ( aTarget & CHWRMLight::ESystemTarget )
       
  1688         {
       
  1689         TInt reservations(CHWRMLight::ENoTarget);
       
  1690         
       
  1691         // Get targets reserved for this session
       
  1692         for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
  1693             {
       
  1694             if ( iTargets[i].iReserved )
       
  1695                 {
       
  1696                 reservations |= iTargets[i].iTarget;
       
  1697                 }
       
  1698             }
       
  1699             
       
  1700         // Get Unreserved targets
       
  1701         reservations |= iReservationHandler->GetUnreservedTargets();    
       
  1702                
       
  1703         modifiedMask &= reservations;
       
  1704 
       
  1705         COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ModifyTargetMaskL - final system mask: 0x%x" ), modifiedMask );
       
  1706         }
       
  1707     
       
  1708     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::ModifyTargetMaskL - returning: 0x%x" ), modifiedMask );
       
  1709             
       
  1710     return modifiedMask;
       
  1711     }
       
  1712 
       
  1713 TBool CHWRMLightService::SetColorL(TInt aTarget, TUint aColor, const RMessage2& aMessage, TBool aActivationInitiated, 
       
  1714                                   TBool aSplitCommand, TInt aSplitCommandId, TAny* aSplitCommandData)
       
  1715     {
       
  1716     COMPONENT_TRACE7(_L( "HWRM Server - CHWRMLightService::SetColorL(0x%x, 0x%x, %d, 0x%x, 0x%x, 0x%x)" ), 
       
  1717                         aTarget, aColor, aActivationInitiated, aSplitCommand, aSplitCommandId, aSplitCommandData);
       
  1718     
       
  1719     TBool pluginCommandCalled = EFalse;
       
  1720     
       
  1721     CheckTargetParameterL( aTarget);
       
  1722 
       
  1723     TInt modTarget(aTarget);
       
  1724     
       
  1725     if ( !aActivationInitiated )
       
  1726         {
       
  1727         modTarget = ModifyTargetMaskL(aTarget, EFalse);
       
  1728         }
       
  1729 
       
  1730     // Check suspended targets, only call plugin for unsuspended ones
       
  1731     TInt unsuspendedTargets = GetAndCheckUnsuspendedTargetsL(modTarget);
       
  1732 
       
  1733     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::SetColorL unSuspended=0x%x)" ), unsuspendedTargets);
       
  1734         
       
  1735     // Call the plugin if any unsuspended targets
       
  1736     if ( unsuspendedTargets )
       
  1737         {
       
  1738         HWRMLightCommand::TLightsColorData pluginCommand;
       
  1739         Mem::FillZ(&pluginCommand, sizeof(HWRMLightCommand::TLightsColorData));
       
  1740         
       
  1741         pluginCommand.iTarget = unsuspendedTargets;
       
  1742         
       
  1743         if(aColor & KHWRMSetColorMask)
       
  1744             {
       
  1745             
       
  1746             pluginCommand.iBlue   = ((aColor)                   & 0xFF);
       
  1747             pluginCommand.iGreen  = ((aColor>>KHWRMGreenShift)  & 0xFF);
       
  1748             pluginCommand.iRed    = ((aColor>>KHWRMRedShift)    & 0xFF);
       
  1749             
       
  1750             pluginCommand.iDefaultColor = EFalse; 
       
  1751             }
       
  1752         else
       
  1753             {
       
  1754             pluginCommand.iDefaultColor = ETrue;
       
  1755             }
       
  1756         
       
  1757         HWRMLightCommand::TLightsColorCmdPackage pckg(pluginCommand);
       
  1758 
       
  1759         ExecutePluginCommandL(unsuspendedTargets, aMessage, HWRMLightCommand::ELightsSetColorCmdId, pckg, aSplitCommand, aSplitCommandId, aSplitCommandData);
       
  1760 
       
  1761         pluginCommandCalled = ETrue;
       
  1762         }
       
  1763     else
       
  1764         {
       
  1765         // complete message if suspended, as there will not be ProcessResponseL
       
  1766         if ( aMessage.Handle() && (!aSplitCommand))
       
  1767             {
       
  1768             aMessage.Complete(KErrNone);
       
  1769             }  
       
  1770         
       
  1771         if (aSplitCommand && aSplitCommandData)
       
  1772             {
       
  1773             COMPONENT_TRACE1(_L( "HWRM Server - CHWRMLightService::SetColorL Deleting allocated aSplitCommandData"));
       
  1774             //Release the data that is allocated, as there will not be ProcessResponseL  
       
  1775             //coverity[freed_arg]
       
  1776             delete aSplitCommandData;
       
  1777             }
       
  1778         }
       
  1779 
       
  1780     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
  1781         {
       
  1782         if ( (aTarget & iTargets[i].iTarget) == iTargets[i].iTarget )
       
  1783             {            
       
  1784             if(aSplitCommand)
       
  1785                 {
       
  1786                 //Store the base state only if it is duration based call.
       
  1787                 TUint color = iTargets[i].iLastState.iColor;
       
  1788                 if(color == KHWRMInvalidColorMask)
       
  1789                     {
       
  1790                     color = KHWRMDefaultColor;
       
  1791                     }
       
  1792                 COMPONENT_TRACE3(_L( "HWRM Server - CHWRMLightService::SetColorL BaseStateColor(0x%x) for target(0x%x)"), color, i);
       
  1793                 iTargets[i].iBaseState.iColor = color;
       
  1794                 }
       
  1795             else
       
  1796                 {
       
  1797                 //iTargets[i].iBaseState.iColor = KHWRMInvalidColorMask;
       
  1798                 }
       
  1799             iTargets[i].iLastState.iColor = aColor;    
       
  1800             }
       
  1801         }
       
  1802 
       
  1803     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMLightService::SetColorL - return %d" ), pluginCommandCalled );
       
  1804 
       
  1805     return pluginCommandCalled;
       
  1806     }
       
  1807 
       
  1808 
       
  1809 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1810 
       
  1811 //  End of File