sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // @file PBASE-T_USBDI-0487.cpp sl@0: // @internalComponent sl@0: // sl@0: // sl@0: sl@0: #include "PBASE-T_USBDI-0487.h" sl@0: #include "testpolicy.h" sl@0: #include "modelleddevices.h" sl@0: sl@0: sl@0: namespace NUnitTesting_USBDI sl@0: { sl@0: sl@0: _LIT8(KDataPayload1,"12345678911111112"); sl@0: // _LIT8(KDataPayload2,"12345opqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); unused sl@0: // _LIT8(KDataPayload3,"abcdefghijklmnopqrstuvwxyzopqrstuvwxyzabcdefghijklmnopqrstuvwxyz12345"); unused sl@0: sl@0: const TInt KInterruptTransferId1 = 0x01; sl@0: const TInt KInterruptTransferId2 = 0x02; sl@0: const TInt KInterruptTransferId3 = 0x03; sl@0: sl@0: _LIT(KTestCaseId,"PBASE-T_USBDI-0487"); sl@0: const TFunctorTestCase CUT_PBASE_T_USBDI_0487::iFunctor(KTestCaseId); sl@0: sl@0: CUT_PBASE_T_USBDI_0487* CUT_PBASE_T_USBDI_0487::NewL(TBool aHostRole) sl@0: { sl@0: CUT_PBASE_T_USBDI_0487* self = new (ELeave) CUT_PBASE_T_USBDI_0487(aHostRole); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: CUT_PBASE_T_USBDI_0487::CUT_PBASE_T_USBDI_0487(TBool aHostRole) sl@0: : CBaseTestCase(KTestCaseId,aHostRole), sl@0: iCaseStep(EInProgress) sl@0: { sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::ConstructL() sl@0: { sl@0: iTestDevice = new RUsbDeviceA(this); sl@0: BaseConstructL(); sl@0: } sl@0: sl@0: sl@0: CUT_PBASE_T_USBDI_0487::~CUT_PBASE_T_USBDI_0487() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: Cancel(); sl@0: delete iTransferIn; sl@0: delete iTransferIn2; sl@0: delete iTransferIn3; sl@0: sl@0: // Close pipe(s) before interface(s) sl@0: iInPipe.Close(); sl@0: sl@0: // Close interfaces sl@0: iUsbInterface1.Close(); sl@0: iUsbInterface0.Close(); sl@0: sl@0: delete iControlEp0; sl@0: delete iActorFDF; sl@0: if(!IsHost() && iTestDevice) sl@0: { sl@0: iTestDevice->Close(); sl@0: } sl@0: delete iTestDevice; sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::ExecuteHostTestCaseL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: iActorFDF = CActorFDF::NewL(*this); sl@0: iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0); sl@0: iActorFDF->Monitor(); sl@0: sl@0: TimeoutIn(30); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::HostDoCancel() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Cancel the timeout timer sl@0: CancelTimeout(); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::ExecuteDeviceTestCaseL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: iTestDevice->OpenL(TestCaseId()); sl@0: iTestDevice->SubscribeToReports(iStatus); sl@0: SetActive(); sl@0: iTestDevice->SoftwareConnect(); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::DeviceDoCancel() sl@0: { sl@0: LOG_FUNC sl@0: // Cancel the device sl@0: iTestDevice->CancelSubscriptionToReports(); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,RUsbDevice::TDeviceState aNewState, sl@0: TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::DeviceInsertedL(TUint aDeviceHandle) sl@0: { sl@0: LOG_FUNC sl@0: Cancel(); sl@0: TInt err(KErrNone); sl@0: sl@0: // Validate that device is as expected sl@0: CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle); sl@0: if(testDevice.SerialNumber().Compare(TestCaseId()) != 0) sl@0: { sl@0: // Incorrect device for this test case sl@0: sl@0: RDebug::Printf(" Incorrect device serial number (%S) connected for this test case (%S)", sl@0: KErrNotFound,&testDevice.SerialNumber(),&TestCaseId()); sl@0: sl@0: // Start the connection timeout again sl@0: sl@0: CancelTimeout(); sl@0: iTimer.After(iStatus,30000000); sl@0: SetActive(); sl@0: return; sl@0: } sl@0: sl@0: TUint32 token0,token1; sl@0: err = testDevice.Device().GetTokenForInterface(0,token0); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Token for interface 0 could not be retrieved",err); sl@0: return TestFailed(err); sl@0: } sl@0: err = iUsbInterface0.Open(token0); // Default interface setting 0 sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Unable to open interface 0 using token %d",err,token0); sl@0: return TestFailed(err); sl@0: } sl@0: sl@0: err = testDevice.Device().GetTokenForInterface(1,token1); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Token for interface 1 could not be retrieved"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: err = iUsbInterface1.Open(token1); // Default interface setting 0 sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Unable to open interface 1 using token %d"),err,token1); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: sl@0: // Select alternate interface setting 1 sl@0: err = iUsbInterface1.SelectAlternateInterface(1); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Selecting alternate interface setting 1 on interface 1"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: iControlEp0->SendRequest(request,this); sl@0: return; sl@0: } sl@0: sl@0: // Open a pipe for endpoint (Int in) sl@0: TInt endpointAddress; sl@0: err = GetEndpointAddress(iUsbInterface1,1,KTransferTypeInterrupt,KEpDirectionIn,endpointAddress); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" unable to get endpoint address for interrupt in endpoint"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: iControlEp0->SendRequest(request,this); sl@0: return; sl@0: } sl@0: sl@0: err = iUsbInterface1.OpenPipeForEndpoint(iInPipe,endpointAddress,EFalse); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Unable to open pipe for endpoint 1"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: RDebug::Printf("Opened pipe to endpoint address %08x for interrupt transfer to host",endpointAddress); sl@0: sl@0: sl@0: // create an interrupt transfer sl@0: iTransferIn = new (ELeave) CInterruptTransfer(iInPipe,iUsbInterface1,256,*this,KInterruptTransferId1); sl@0: iTransferIn->RegisterTransferDescriptor(); sl@0: sl@0: // create a 2nd interrupt transfer sl@0: iTransferIn2 = new (ELeave) CInterruptTransfer(iInPipe,iUsbInterface1,256,*this,KInterruptTransferId2); sl@0: iTransferIn2->RegisterTransferDescriptor(); sl@0: sl@0: // create a 3rd interrupt transfer sl@0: iTransferIn3 = new (ELeave) CInterruptTransfer(iInPipe,iUsbInterface1,256,*this,KInterruptTransferId3); sl@0: iTransferIn3->RegisterTransferDescriptor(); sl@0: sl@0: // Initialise the descriptors for transfer sl@0: RDebug::Printf("Initialising the transfer descriptors"); sl@0: err = iUsbInterface1.InitialiseTransferDescriptors(); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Unable to initialise transfer descriptors",err); sl@0: return; sl@0: } sl@0: sl@0: // que interrupt transfer sl@0: err = iTransferIn->TransferInL(KDataPayload1().Length()); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Unable to queue an interrupt transfer"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: sl@0: // que interrupt transfer sl@0: err = iTransferIn2->TransferInL(KDataPayload1().Length()); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Unable to queue an interrupt transfer"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: sl@0: // Que a 3rd interrupt transfer sl@0: err = iTransferIn3->TransferInL(KDataPayload1().Length()); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Unable to queue an interrupt transfer"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: sl@0: // Timeout the interrupt transfer in 7 seconds sl@0: TimeoutIn(7); sl@0: // Instruct the client device to write the following data through the valid endpoint sl@0: TEndpointWriteRequest request(1,1,KDataPayload1); sl@0: iControlEp0->SendRequest(request,this); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::Ep0TransferCompleteL(TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d"),aCompletionCode; sl@0: sl@0: if(aCompletionCode != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Transfer to control endpoint 0 was not successful"),aCompletionCode); sl@0: RDebug::Print(msg); sl@0: } sl@0: sl@0: if(iCaseStep == EPassed) sl@0: { sl@0: if(aCompletionCode == KErrNone) sl@0: { sl@0: return TestPassed(); sl@0: } sl@0: // else error sl@0: iCaseStep = EFailed; sl@0: } sl@0: sl@0: if(iCaseStep == EFailed) sl@0: { sl@0: return TestFailed(KErrCompletion); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::TransferCompleteL(TInt aTransferId,TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: Cancel(); sl@0: sl@0: RDebug::Printf("Transfer %d completed with %d",aTransferId,aCompletionCode); sl@0: sl@0: if(aCompletionCode != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Transfer %d did not complete successfully"),aCompletionCode,aTransferId); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(aCompletionCode,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: sl@0: if(aTransferId == KInterruptTransferId1) sl@0: { sl@0: TPtrC8 data(iTransferIn->DataPolled()); sl@0: sl@0: RDebug::Printf("data.Length 1()= %d", data.Length()); sl@0: sl@0: TBuf<256> msg; sl@0: for(int i = 0 ; i < data.Length(); i++) sl@0: { sl@0: msg.AppendFormat(_L("%02x"),data[i]); sl@0: } sl@0: RDebug::Print(msg); sl@0: // Compare the data to what is expected sl@0: if(data.Compare(KDataPayload1) != 0) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Interrupt data received does not match data sent"),KErrCompletion); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(KErrCompletion,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: User::After(3000000); sl@0: // Comparison is a match, continue sl@0: TEndpointWriteRequest request(1,1,KDataPayload1); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: else if(aTransferId == KInterruptTransferId2) sl@0: { sl@0: TPtrC8 data(iTransferIn2->DataPolled()); sl@0: RDebug::Printf("data.Length 2()= %d", data.Length()); sl@0: sl@0: TBuf<256> msg; sl@0: for(int i = 0 ; i < data.Length(); i++) sl@0: { sl@0: msg.AppendFormat(_L("%02x"),data[i]); sl@0: } sl@0: RDebug::Print(msg); sl@0: sl@0: // Compare the data to what is expected sl@0: if(data.Compare(KDataPayload1) != 0) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Interrupt data received does not match data sent"),KErrCompletion); sl@0: RDebug::Print(msg); sl@0: //TODO if test fails! Test fail code below WAS commented out sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(KErrCompletion,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: // Comparison is a match, continue sl@0: TEndpointWriteRequest request(1,1,KDataPayload1); sl@0: return iControlEp0->SendRequest(request,this); sl@0: sl@0: } sl@0: else if(aTransferId == KInterruptTransferId3) sl@0: { sl@0: TPtrC8 data(iTransferIn3->DataPolled()); sl@0: // Compare the data to what is expected sl@0: if(data.Compare(KDataPayload1) != 0) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Interrupt data received does not match data sent"),KErrCompletion); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(KErrCompletion,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: // Comparison is a match, test passed sl@0: iCaseStep = EPassed; sl@0: TTestCasePassed request; sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: else sl@0: { sl@0: RDebug::Printf(" Unknown transfer identity"); sl@0: TestFailed(KErrUnknown); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::DeviceRemovedL(TUint aDeviceHandle) sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // The test device should not be removed until the test case has passed sl@0: // so this test case has not completed, and state this event as an error sl@0: TestFailed(KErrDisconnected); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::BusErrorL(TInt aError) sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // This test case handles no failiures on the bus sl@0: TestFailed(KErrCompletion); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::HostRunL() sl@0: { sl@0: LOG_FUNC sl@0: // Obtain the completion code sl@0: TInt completionCode(iStatus.Int()); sl@0: sl@0: if(completionCode == KErrNone) sl@0: { sl@0: // Action timeout sl@0: RDebug::Printf(" Action timeout"); sl@0: TestFailed(KErrTimedOut); sl@0: } sl@0: else sl@0: { sl@0: RDebug::Printf(" Timeout timer could not complete",completionCode); sl@0: TestFailed(completionCode); sl@0: } sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0487::DeviceRunL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Disconnect the device sl@0: iTestDevice->SoftwareDisconnect(); sl@0: sl@0: // Complete the test case request sl@0: TestPolicy().SignalTestComplete(iStatus.Int()); sl@0: } sl@0: sl@0: sl@0: }