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-0486.cpp sl@0: // @internalComponent sl@0: // sl@0: // sl@0: sl@0: #include "PBASE-T_USBDI-0486.h" sl@0: #include "testpolicy.h" sl@0: #include "testdebug.h" sl@0: #include "modelleddevices.h" sl@0: sl@0: namespace NUnitTesting_USBDI sl@0: { sl@0: sl@0: _LIT(KTestCaseId,"PBASE-T_USBDI-0486"); sl@0: const TFunctorTestCase CUT_PBASE_T_USBDI_0486::iFunctor(KTestCaseId); sl@0: const TInt KBulkTranferId = 2; sl@0: const TInt KExpectedDataSize = 26; sl@0: sl@0: CUT_PBASE_T_USBDI_0486* CUT_PBASE_T_USBDI_0486::NewL(TBool aHostRole) sl@0: { sl@0: CUT_PBASE_T_USBDI_0486* self = new (ELeave) CUT_PBASE_T_USBDI_0486(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_0486::CUT_PBASE_T_USBDI_0486(TBool aHostRole) sl@0: : CBaseBulkTestCase(KTestCaseId,aHostRole), sl@0: iCaseStep(EWaitForDeviceCConnection) sl@0: { sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::ConstructL() sl@0: { sl@0: BaseBulkConstructL(); sl@0: iTestDeviceC = new RUsbDeviceC(this); // TODO check tree for device C, once inserted sl@0: } sl@0: sl@0: CUT_PBASE_T_USBDI_0486::~CUT_PBASE_T_USBDI_0486() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: delete iTestDeviceC; sl@0: } sl@0: sl@0: /** sl@0: Called when the device has reported any kind of error in its operation sl@0: or when the device has been informed by the host to report success sl@0: */ sl@0: void CUT_PBASE_T_USBDI_0486::DeviceRunL() sl@0: { sl@0: LOG_FUNC sl@0: // Complete the test case request sl@0: TestPolicy().SignalTestComplete(iStatus.Int()); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::DeviceDoCancel() sl@0: { sl@0: LOG_FUNC sl@0: iTestDeviceC->CancelSubscriptionToReports(); sl@0: } sl@0: sl@0: TBool CUT_PBASE_T_USBDI_0486::CheckSN(const TDesC16& aSerialNumberGot, const TDesC& aExpectedSerialNumber) sl@0: { sl@0: TBool areSNsIdenticals = (aSerialNumberGot.Compare(aExpectedSerialNumber) == 0); sl@0: sl@0: if(!areSNsIdenticals) sl@0: { sl@0: // Incorrect device for this test case sl@0: RDebug::Printf(" Incorrect device serial number (%S) connected for this test case (%S)", sl@0: KErrNotFound,&aSerialNumberGot, &aExpectedSerialNumber); sl@0: } sl@0: return areSNsIdenticals; sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::Ep0TransferCompleteL(TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode); sl@0: RDebug::Printf("--->Ep0TransferCompleteL, case step = %d", iCaseStep); sl@0: sl@0: switch(iCaseStep) sl@0: { sl@0: case EDeviceCConnected: sl@0: { sl@0: if(aCompletionCode != KErrNone) sl@0: { sl@0: RDebug::Printf(" aCompletionCode != KErrNone",aCompletionCode); sl@0: return TestFailed(aCompletionCode); sl@0: } sl@0: // close interface 0 sl@0: iUsbInterface0.Close(); sl@0: iCaseStep = EWaitForDeviceCDisconnection; sl@0: } sl@0: break; sl@0: sl@0: case EPassed: sl@0: TestPassed(); sl@0: break; sl@0: sl@0: default: sl@0: TestFailed(KErrCompletion); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::TransferCompleteL(TInt aTransferId,TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: Cancel(); sl@0: RDebug::Printf("--->TransferCompleteL, case step = %d", iCaseStep); sl@0: sl@0: if(aTransferId == KBulkTranferId) sl@0: { sl@0: if(aCompletionCode != KErrCancel && aCompletionCode != KErrUsbIOError) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" The transfer completed with no errors but should have done so"),aCompletionCode); sl@0: RDebug::Print(msg); sl@0: TTestCaseFailed request(KErrCorrupt,msg); sl@0: return iControlEp0->SendRequest(request,this); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: RDebug::Printf(" a transfer completed (id=%d) that was not expected",aTransferId); sl@0: return TestFailed(KErrCorrupt); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::DeviceInsertedL(TUint aDeviceHandle) sl@0: { sl@0: LOG_FUNC sl@0: RDebug::Printf("--->DeviceInsertedL, case step = %d", iCaseStep); sl@0: sl@0: // Cancel the timeout timer 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: sl@0: // Perform the correct test step sl@0: switch(iCaseStep) sl@0: { sl@0: case EWaitForDeviceCConnection: sl@0: { sl@0: if(!CheckSN(testDevice.SerialNumber(), KTestDeviceC_SN())) sl@0: { sl@0: return TestFailed(KErrNotFound); sl@0: } sl@0: iCaseStep = EDeviceCConnected; sl@0: sl@0: err = testDevice.Device().GetTokenForInterface(0,iToken0DeviceC); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Unable to retrieve token for interface 0",err); sl@0: TestFailed(err); sl@0: } sl@0: // Open the interface sl@0: err = iUsbInterface0.Open(iToken0DeviceC); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Unable to open interface 0"); sl@0: TestFailed(err); sl@0: } sl@0: sl@0: TUint32 token1; 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: 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: TInt endpointAddress; sl@0: // Queue a bulk in transfer on endpoint 2 sl@0: err = GetEndpointAddress(iUsbInterface1,0,KTransferTypeBulk,KEpDirectionIn,endpointAddress); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Address for bulk in endpoint could not be obtained"),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("Endpoint adress %08x",endpointAddress); sl@0: sl@0: err = iUsbInterface1.OpenPipeForEndpoint(iTestPipeInterface1BulkIn,endpointAddress,ETrue); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<256> msg; sl@0: msg.Format(_L(" Unable to open pipe for endpoint %08x"),err,endpointAddress); 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: // Create the bulk transfers sl@0: iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,256,*this, KBulkTranferId); 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: TBuf<256> msg; sl@0: msg.Format(_L(" Unable to initialise transfer descriptors"),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: iInTransfer[0]->TransferIn(KExpectedDataSize); sl@0: sl@0: // disconnect device C and connect device A sl@0: TDisconnectDeviceCThenConnectDeviceARequest request; sl@0: iControlEp0->SendRequest(request,this); sl@0: sl@0: // Monitor for the reconnection from the client sl@0: iActorFDF->Monitor(); sl@0: TimeoutIn(30); sl@0: sl@0: } sl@0: break; sl@0: sl@0: case EWaitForDeviceAConnection: sl@0: { sl@0: if(!CheckSN(testDevice.SerialNumber(), TestCaseId())) sl@0: { sl@0: return TestFailed(KErrNotFound); sl@0: } sl@0: // try to open interface 0 sl@0: err = iUsbInterface0.Open(iToken0DeviceC); sl@0: if(err != KErrNotFound) // invalid token sl@0: { sl@0: RDebug::Printf("iUsbInterface0.Open(iToken0DeviceC) === %d", err); sl@0: TestFailed(err); sl@0: } sl@0: sl@0: // get token now sl@0: TUint32 token0DeviceA; sl@0: err = testDevice.Device().GetTokenForInterface(0,token0DeviceA); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Unable to retrieve token(device A) for interface 0",err); sl@0: TestFailed(err); sl@0: } sl@0: // Open the interface sl@0: err = iUsbInterface0.Open(token0DeviceA); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Unable to open interface 0(device A)"); sl@0: TestFailed(err); sl@0: } sl@0: sl@0: // ok, send EPassed request sl@0: iCaseStep = EPassed; sl@0: // Send test case passed request to client sl@0: User::After(2000000); sl@0: TTestCasePassed request; sl@0: iControlEp0->SendRequest(request,this); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: { sl@0: RDebug::Printf(" Test case actions out of sync"); sl@0: TestFailed(KErrCorrupt); sl@0: } sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::DeviceRemovedL(TUint aDeviceHandle) sl@0: { sl@0: LOG_FUNC sl@0: Cancel(); sl@0: RDebug::Printf("--->DeviceRemovedL, case step = %d", iCaseStep); sl@0: sl@0: switch(iCaseStep) sl@0: { sl@0: case EWaitForDeviceCDisconnection: // device C is disconnected now, interface 0 has been closed before sl@0: { sl@0: iCaseStep = EWaitForDeviceAConnection; sl@0: iActorFDF->Monitor(); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: RDebug::Printf(" Test case actions out of sync"); sl@0: TestFailed(KErrCorrupt); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::HandleDeviceDConnection() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: iTestDevice->SubscribeToReports(iStatus); sl@0: SetActive(); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0486::ExecuteDeviceTestCaseL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Create the test device sl@0: iTestDeviceC->OpenL(KTestDeviceC_SN()); sl@0: iTestDeviceC->SubscribeToReports(iStatus); sl@0: SetActive(); sl@0: sl@0: // Connect the device to the host sl@0: iTestDeviceC->SoftwareConnect(); sl@0: } sl@0: sl@0: RUsbDeviceC* CUT_PBASE_T_USBDI_0486::TestDeviceC() sl@0: { sl@0: return iTestDeviceC; sl@0: } sl@0: sl@0: RUsbDeviceD* CUT_PBASE_T_USBDI_0486::TestDeviceD() sl@0: { sl@0: return iTestDevice; //from CBaseBulkTestCase sl@0: } sl@0: sl@0: }