// -----------------------------------------------------------------------------
// CMediatorServerCommandHandler::IssueCommandL
//  
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CMediatorServerCommandHandler::IssueCommandL( 
                                        TMediatorCategory aCategory, 
                                        MediatorService::TCommand aCommand,
                                        const TDesC8& aData,
                                        TCapabilitySet aCaps,
                                        MMediatorCommandResponseObserver* aObserver )
    {
    LOG(_L("[Mediator Server]\t CMediatorServerCommandHandler::IssueCommandL"));
    CCategory* category = iObjectHandler.CategoryL( aCategory );
    if ( category )
        {
        // Find the command from register list
        TInt ignore = 0;
        CCommand* commandPtr = category->FindCommand( aCommand.iCommandId, ignore );
        if ( !commandPtr )
            {
            ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::IssueCommandL: Command %d not found in category %d of domain %d\n"), aCommand.iCommandId, 
                                                                                                                                                   aCategory.iCategory.iUid,
                                                                                                                                                   aCategory.iDomain.iUid ) );
            
            User::Leave( KErrMediatorCommandNotFound );
            }     
        // Then check the capabilities && the version information
        // Capabilities are checked so that boolean ETrue is returned
        // when all parameter caps can be found from aCaps
        if ( !aCaps.HasCapabilities( commandPtr->Policy() ) )
            {
#ifdef _DEBUG
            for ( TInt index = 0; index < ECapability_Limit; index++ )
                {
                TCapabilitySet commandCaps = commandPtr->Policy();
                TBool command = commandCaps.HasCapability( (TCapability) index );
                TBool requestor = aCaps.HasCapability( (TCapability) index );
                if ( command && !requestor )
                    {
                    ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::IssueCommandL: capability %d missing\n"), index ));
                    ERROR_TRACE(Print(_L("[Mediator] Capability error when issuing command %d in category %d of domain %d\n"), aCommand.iCommandId, 
                                                                                                                                 aCategory.iCategory.iUid,
                                                                                                                                 aCategory.iDomain.iUid ) );
                    }
                }
#endif    
            User::Leave( KErrPermissionDenied );
            }
        // Check (major) version match
        if ( aCommand.iVersion.iMajor != commandPtr->Version().iMajor )
            {
            ERROR_TRACE(Print(_L("[Mediator] CMediatorServerCommandHandler::IssueCommandL: registered=%d, issued=%d\n"), 
                                                                                        commandPtr->Version().iMajor,
                                                                                        aCommand.iVersion.iMajor ));
            ERROR_TRACE(Print(_L("[Mediator] Version error when issuing command %d in category %d of domain %d\n"), aCommand.iCommandId, 
                                                                                                                      aCategory.iCategory.iUid,
                                                                                                                      aCategory.iDomain.iUid ) );
            // There's a major version mismatch
            User::Leave( KErrMediatorVersionMismatch );
            }
        
        // If ok, issue to command to client
        // Make the new command, set initiator and responder
        // We don't need to command data for the pending list
        CCommand* newCommand = CCommand::NewL( aCommand );
        CleanupStack::PushL( newCommand );
        
        newCommand->SetResponseObserver( aObserver );
        newCommand->SetObserver( commandPtr->Observer() );
        newCommand->SetDomain( aCategory.iDomain );
        newCommand->SetCategory( aCategory.iCategory );
        newCommand->SetTimeout( commandPtr->Timeout() );
        
        // Start command timing, if it is not an infinite command
        if ( commandPtr->Timeout() != KMediatorTimeoutInfinite )
            {
            // Start timeout timer ( request callback here )
            newCommand->StartTimer( this );    
            }
                
        iCommandPendingList.AppendL( newCommand );
        
        CleanupStack::Pop( newCommand );
        
        // Now send the command to correct responder --> we have it pending
        commandPtr->Observer()->MediatorCommandL( aCategory.iDomain,
                                                  aCategory.iCategory,
                                                  aCommand.iCommandId,
                                                  aCommand.iVersion,
                                                  aData );
        }
    }