//
// ----------------------------------------------------------------------------------
// CASYStubCmdHandlerBase::ProcessCommandL()
// ----------------------------------------------------------------------------------
// 
void CASYStubCmdHandlerBase::ProcessCommandL( const TProcessCmdId aCommand,
    const TASYCmdParams& aCmdParams )
    {

    COMPONENT_TRACE( ( _L( "ASYStub - CASYStubCmdHandlerBase::ProcessCommandL() - Enter" ) ) );

    CASYMainServiceBase* mainServiceBase = ASYMainServiceBase();
    COMPONENT_TRACE(
        ( _L( "AsyStub - CASYStubCmdHandlerBase::ProcessCommandL - mainServiceBase (0x%x)" ), &mainServiceBase ) );

    TASYCommandParamRecord cmdParams( aCmdParams() );//For debugging

    if( aCommand == ECmdGetObjectValue || aCommand == ECmdSetObjectValue )
        {
        ProcessObjectL( aCommand, aCmdParams );
        }
    else
        {
        CASYStubMainServiceBase* AsyStub =
            reinterpret_cast<CASYStubMainServiceBase*> ( mainServiceBase );

        COMPONENT_TRACE(
            ( _L( "AsyStub - CASYStubCmdHandlerBase::ProcessCommandL - AsyStub (0x%x)" ), &AsyStub ) );
        AsyStub->AsyStubMainServicePointerReceivedL();
        COMPONENT_TRACE( ( _L( "ASYStub - CASYStubCmdHandlerBase::ProcessCommandL() - Return" ) ) );

        TInt aTestCaseID = AsyStub->GetTestCaseID(); // Do something with this
        TTFCapability aCapabilityS = AsyStub->GetAndRemoveCapability();
        switch( aCommand )
            {
            case ECmdGetValueBool:
                {

                TUint32 name;
                aCmdParams().iNameRecord.GetName( name );
                TAccValueTypeTBool aCmdResponse;
                aCmdResponse.iValue = EFalse;

                if( name == aCapabilityS.iCapability )
                    {
                    if( 0 != aCapabilityS.iTimeMs )
                        {
                        //Serve asynchronously       	            
                        Start( aCapabilityS.iTimeMs,
                            aCapabilityS.iValue,
                            EAPVBool );
                        }
                    else
                        {
                        aCmdResponse.iValue = aCapabilityS.iValue;
                        ProcessResponseL( aCmdResponse );
                        }
                    }
                else
                    {
                    TRACE_ASSERT_ALWAYS;
                    ProcessResponseL( aCmdResponse, KErrArgument );
                    }
                }
                break;

            case ECmdGetValueTDes8:
                {

                // Worm is fired through the Accessory Server.
                TBuf8<80>
                    worm( _L8("Malicious Worm Attach with extra long data with extra long content" ) );

                ProcessResponseL( worm, KErrNone );
                }
                break;
            case ECmdSetValueBool:
                {
                TUint32 name;
                aCmdParams().iNameRecord.GetName( name );
                TAccValueTypeTBool aCmdResponse;
                aCmdResponse.iValue = EFalse;

                if( name == aCapabilityS.iCapability && aCmdParams().iCmdValue
                    == aCapabilityS.iValue )
                    {
                    if( aCapabilityS.iTimeMs )
                        {
                        //Serve asynchronously       	            
                        Start( aCapabilityS.iTimeMs,
                            aCapabilityS.iValue,
                            EAPVBool );
                        }
                    else
                        {
                        aCmdResponse.iValue = aCmdParams().iCmdValue;
                        ProcessResponseL( aCmdResponse );
                        }
                    }
                else
                    {
                    TRACE_ASSERT_ALWAYS;
                    ProcessResponseL( aCmdResponse, KErrArgument );
                    }
                }
                break;

            case ECmdProcessCommandInit:
                {
                TAccValueTypeTBool boolInitResponse;
                TBool everyThing( ETrue );

                // If everything is ok
                if( everyThing )
                    {
                    COMPONENT_TRACE(
                        ( _L( "ASYStub - CASYStubCmdHandlerBase::ProcessCommandL() - everything is ok" ) ) );
                    boolInitResponse.iValue = ETrue;
                    ProcessResponseL( boolInitResponse );
                    }

                // If everything is not ok
                else
                    {
                    COMPONENT_TRACE(
                        ( _L( "ASYStub - CASYStubCmdHandlerBase::ProcessCommandL() - everything is not ok" ) ) );
                    boolInitResponse.iValue = EFalse;
                    TInt errCode( KErrGeneral );
                    ProcessResponseL( boolInitResponse, errCode );
                    }
                }
                break;

                //for user selection....
            case ECmdAccessoryUpdated:
                {
                TAccValueTypeTBool boolInitResponse;
                TBool everyThing( ETrue );

                //get capabilities
                CAccPolSubblockNameArray* iNameArray =
                    CAccPolSubblockNameArray::NewL();

                RAccessoryServer server;
                server.Connect();

                RAccessorySingleConnection connectionBase;
                connectionBase.CreateSubSession( server );
                connectionBase.GetSubblockNameArrayL( cmdParams.iGenericID,
                    *iNameArray );

                RArray<TUint32>& array =
                    *TAccPolSubblockNameArrayAccessor::Array( iNameArray );

                for( TInt i( 0 ); i < array.Count(); i++ )
                    {
                    TUint32 name = array[i];
                    TAccPolNameRecord nameRecord;
                    nameRecord.SetNameL( name );
                    TAccValueTypeTInt value;
                    TRAPD( err, connectionBase.GetValueL( cmdParams.iGenericID, nameRecord, value));

                    if( err != KErrNone )
                        {
                        COMPONENT_TRACE(
                            ( _L( "ASYStub - CASYStubCmdHandlerBase::ProcessCommandL() - Capability 0x%x has no value!"), name ) );
                        }

                    name = 0;
                    }

                connectionBase.CloseSubSession();
                server.Close();

                delete iNameArray;

                // If everything is ok
                if( everyThing )
                    {
                    COMPONENT_TRACE(
                        ( _L( "ASYStub - CASYStubCmdHandlerBase::ProcessCommandL() - everything is ok" ) ) );
                    boolInitResponse.iValue = ETrue;
                    ProcessResponseL( boolInitResponse );
                    }

                // If everything is not ok
                else
                    {
                    COMPONENT_TRACE(
                        ( _L( "ASYStub - CASYStubCmdHandlerBase::ProcessCommandL() - everything is not ok" ) ) );
                    boolInitResponse.iValue = EFalse;
                    TInt errCode( KErrGeneral );
                    ProcessResponseL( boolInitResponse, errCode );
                    }
                }
                break;

            default:
                {
                COMPONENT_TRACE(
                    ( _L( "BTASYStub - CBTBTASYStubCmdHandlerBase::ProcessCommandL() - ERROR: unhandled command" ) ) );
                TRACE_ASSERT_ALWAYS;
                User::Panic( _L("ASY Stub"), KErrGeneral );
                }
                break;
            }
        }
    }
//
// ----------------------------------------------------------------------------------
// CASYStubCmdHandlerBase::ProcessCommandL()
// ----------------------------------------------------------------------------------
// 
void CASYStubCmdHandlerBase::ProcessCommandL( const TProcessCmdId aCommand, const TASYCmdParams& aCmdParams )
    {
    CASYMainServiceBase* mainServiceBase = ASYMainServiceBase();
    CASYStubMainServiceBase* AsyStub = reinterpret_cast<CASYStubMainServiceBase*>( mainServiceBase );

    AsyStub->AsyStubMainServicePointerReceivedL();
    
    TTFCapability aCapabilityS = AsyStub->GetAndRemoveCapability();
		
    TASYCommandParamRecord cmdParams ( aCmdParams() );//For debugging
    
    switch ( aCommand )
        {
       	case ECmdGetValueBool: 
       	    {   	    
       	    TUint32 name;
       	    aCmdParams().iNameRecord.GetName( name );
       	    TAccValueTypeTBool aCmdResponse = {EFalse};
       	    
       	    if( name == aCapabilityS.iCapability )
       	        {
       	        if( 0 != aCapabilityS.iTimeMs )
       	            {
       	            //Serve asynchronously       	            
       	            Start(aCapabilityS.iTimeMs, aCapabilityS.iValue, EAPVBool );
       	            }
       	        else
       	            {
       			    aCmdResponse.iValue	= aCapabilityS.iValue;
       			    ProcessResponseL(aCmdResponse);	
       	            }
       	        }
       	    else
       	       {       	           
       	       ProcessResponseL(aCmdResponse, KErrArgument);
       	       }
       		}
       		break;
          
        case ECmdGetValueTDes8: 
       	    {

            // Worm is fired through the Accessory Server.
       	    TBuf8<22> worm(_L8("Malicious Worm Attach" ));

       	    ProcessResponseL( worm, KErrNone );       	       
       		}
       		break;

        case ECmdSetValueBool: 
       	    {       	    
       	    TUint32 name;
       	    aCmdParams().iNameRecord.GetName( name );
       	    TAccValueTypeTBool aCmdResponse = { EFalse };
       	    
       	    if( name == aCapabilityS.iCapability 
       	     && aCmdParams().iCmdValue == aCapabilityS.iValue )
       	        {
       	        if( aCapabilityS.iTimeMs )
       	            {
       	            //Serve asynchronously       	            
       	            Start(aCapabilityS.iTimeMs, aCapabilityS.iValue, EAPVBool );
       	            }
       	        else
       	            {
       			    aCmdResponse.iValue	= aCmdParams().iCmdValue;
       			    ProcessResponseL(aCmdResponse);
       	            }
       	        }
       	    else
       	       {       	       
       	       ProcessResponseL(aCmdResponse, KErrArgument);
       	       }
       	    }
       	    break;
       	          
        case ECmdProcessCommandInit:
            {        		      
           	TAccValueTypeTBool boolInitResponse;
            TBool everyThing( ETrue );
        
            // If everything is ok
            if ( everyThing )
                {            
                boolInitResponse.iValue = ETrue;
                ProcessResponseL( boolInitResponse );            
                }
        
            // If everything is not ok
            else
                {
                boolInitResponse.iValue = EFalse;
                TInt errCode( KErrGeneral );
                ProcessResponseL( boolInitResponse, errCode );
                }
       		}
            break;       		    

		//for user selection....
        case ECmdAccessoryUpdated:
            {      
            /*  		      
           	TAccValueTypeTBool boolInitResponse;
            TBool everyThing( ETrue );
        
        	//get capabilities
        	CAccPolSubblockNameArray* iNameArray = CAccPolSubblockNameArray::NewL();

        	RAccessoryServer server;
        	server.Connect();
        	
        	RAccessorySingleConnection connectionBase;
        	connectionBase.CreateSubSession( server);
			connectionBase.GetSubblockNameArrayL( cmdParams.iGenericID, *iNameArray);


			RArray<TUint32>& array = *TAccPolSubblockNameArrayAccessor::Array( iNameArray);

			for ( TInt i(0); i < array.Count(); i++)
			{
				TUint32 name = array[ i];
				TAccPolNameRecord nameRecord;
				nameRecord.SetNameL( name);
				TAccValueTypeTInt value;
				
				TRAPD( err, connectionBase.GetValueL( cmdParams.iGenericID, nameRecord, value));
				
				if ( err != KErrNone)				
				{
				}
				
				name = 0;			
			}
			connectionBase.CloseSubSession();
			server.Close();
			delete iNameArray;
            // If everything is ok
            if ( everyThing )
                {            
                boolInitResponse.iValue = ETrue;
                ProcessResponseL( boolInitResponse );            
                }
        
            // If everything is not ok
            else
                {
                boolInitResponse.iValue = EFalse;
                TInt errCode( KErrGeneral );
                ProcessResponseL( boolInitResponse, errCode );
                }*/
       		}
            break;      
		default:
    	    {            
            User::Panic(_L("ASY Stub"), KErrGeneral );
    	    }
           	break;			
       	}	
	}