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-0477.cpp sl@0: // @internalComponent sl@0: // sl@0: // sl@0: sl@0: #include "PBASE-T_USBDI-0477.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: _LIT(KTestCaseId,"PBASE-T_USBDI-0477"); sl@0: const TFunctorTestCase CUT_PBASE_T_USBDI_0477::iFunctor(KTestCaseId); sl@0: sl@0: CUT_PBASE_T_USBDI_0477* CUT_PBASE_T_USBDI_0477::NewL(TBool aHostRole) sl@0: { sl@0: CUT_PBASE_T_USBDI_0477* self = new (ELeave) CUT_PBASE_T_USBDI_0477(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_0477::CUT_PBASE_T_USBDI_0477(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_0477::ConstructL() sl@0: { sl@0: iTestDevice = new RUsbDeviceA(this); sl@0: BaseConstructL(); sl@0: } sl@0: sl@0: sl@0: CUT_PBASE_T_USBDI_0477::~CUT_PBASE_T_USBDI_0477() sl@0: { sl@0: LOG_FUNC sl@0: Cancel(); sl@0: sl@0: iTestPipe.Close(); sl@0: iUsbInterface1.Close(); sl@0: iUsbInterface0.Close(); sl@0: sl@0: delete iClientAction; 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: sl@0: void CUT_PBASE_T_USBDI_0477::ExecuteHostTestCaseL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: iActorFDF = CActorFDF::NewL(*this); sl@0: iClientAction = new (ELeave) CEp0Transfer(iUsbInterface0); sl@0: iActorFDF->Monitor(); sl@0: TimeoutIn(30); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::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: sl@0: void CUT_PBASE_T_USBDI_0477::HostDoCancel() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Cancel the test step timeout sl@0: sl@0: CancelTimeout(); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::DeviceDoCancel() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Cancel the device sl@0: sl@0: iTestDevice->CancelSubscriptionToReports(); sl@0: } sl@0: sl@0: TBool CUT_PBASE_T_USBDI_0477::CheckFirstInterfaceDescriptorDeviceA(TUsbInterfaceDescriptor& aIfDescriptor) sl@0: { sl@0: LOG_FUNC sl@0: /* Interface0 [setting0] sl@0: Interface1 [setting0] sl@0: [endpoint1] Bulk out sl@0: [endpoint2] Bulk in sl@0: [setting1] sl@0: [endpoint1] Interrupt in sl@0: [endpoint2] Bulk out sl@0: [endpoint3] Bulk in sl@0: */ sl@0: // alt. setting 0 sl@0: TUsbInterfaceDescriptor::Cast(&aIfDescriptor); // is it an interface? sl@0: CHECK_RET_BOOL(&aIfDescriptor != 0); sl@0: sl@0: // EP1 sl@0: TUsbGenericDescriptor* desc = aIfDescriptor.iFirstChild; sl@0: TUsbEndpointDescriptor::Cast(desc); sl@0: CHECK_RET_BOOL(desc != 0); sl@0: sl@0: // EP 2 sl@0: desc = desc->iNextPeer; sl@0: TUsbEndpointDescriptor::Cast(desc); sl@0: CHECK_RET_BOOL(desc != 0); sl@0: desc = desc->iNextPeer; // no peer sl@0: CHECK_RET_BOOL(desc == 0); sl@0: sl@0: // alt. setting 1 sl@0: desc = aIfDescriptor.iNextPeer; sl@0: TUsbInterfaceDescriptor::Cast(desc); sl@0: CHECK_RET_BOOL(desc != 0); sl@0: sl@0: // EP1 sl@0: desc = desc->iFirstChild; sl@0: TUsbEndpointDescriptor::Cast(desc); sl@0: CHECK_RET_BOOL(desc != 0); sl@0: sl@0: // EP 2 sl@0: desc = desc->iNextPeer; sl@0: TUsbEndpointDescriptor::Cast(desc); sl@0: CHECK_RET_BOOL(desc != 0); sl@0: sl@0: // EP 3 sl@0: desc = desc->iNextPeer; sl@0: TUsbEndpointDescriptor::Cast(desc); sl@0: CHECK_RET_BOOL(desc != 0); sl@0: desc = desc->iNextPeer; // no peer sl@0: CHECK_RET_BOOL(desc == 0); sl@0: sl@0: RDebug::Printf("CheckFirstInterfaceDescriptorDeviceA successfull!"); sl@0: return ETrue; sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::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: TimeoutIn(30); sl@0: return; sl@0: } sl@0: sl@0: TUint32 token0,token1; // Token for an interface sl@0: 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: sl@0: err = iUsbInterface0.Open(token0); 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: // Test case has failed sl@0: TBuf<256> msg; sl@0: _LIT(string, " Token for interface could not be retrieved"); sl@0: msg.Format(string,err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: iClientAction->SendRequest(request,this); sl@0: return; 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 using token %d"),err,token1); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: iClientAction->SendRequest(request,this); sl@0: return; sl@0: } sl@0: sl@0: sl@0: // check interface descriptor now sl@0: RDebug::Printf("check Interface descriptor now"); sl@0: TUsbInterfaceDescriptor ifDescriptor; sl@0: CHECK(iUsbInterface1.GetInterfaceDescriptor(ifDescriptor) == KErrNone); sl@0: CHECK(CheckFirstInterfaceDescriptorDeviceA(ifDescriptor)); sl@0: sl@0: // Enumerate Endpoints On Interface 1 alt. setting 0 sl@0: CHECK(iUsbInterface1.EnumerateEndpointsOnInterface(0) == 2); sl@0: sl@0: // get busId now sl@0: TUsbBusId busId; sl@0: CHECK(iUsbInterface1.GetBusId(busId) == KErrNone); sl@0: RDebug::Printf("busId(Interface) = %d",busId); sl@0: sl@0: // get device speed now sl@0: RUsbInterface::TDeviceSpeed deviceSpeed; sl@0: CHECK(iUsbInterface1.GetDeviceSpeed(deviceSpeed) == KErrNone); sl@0: RDebug::Printf("GetDeviceSpeed = %d", deviceSpeed); sl@0: CHECK(deviceSpeed == RUsbInterface::EFullSpeed); sl@0: sl@0: RDebug::Printf("Number of alternate interface settings available: %d",iUsbInterface1.GetAlternateInterfaceCount()); sl@0: gtest(iUsbInterface1.GetAlternateInterfaceCount() == 2); 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: iClientAction->SendRequest(request,this); sl@0: return; sl@0: } sl@0: sl@0: // Open a pipe for endpoint (Bulk out) sl@0: TInt endpointAddress; sl@0: err = GetEndpointAddress(iUsbInterface1,1,KTransferTypeBulk,KEpDirectionOut,endpointAddress); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<128> msg; sl@0: msg.Format(_L(" Could not get address for Bulk out endpoint"),err); sl@0: RDebug::Print(msg); sl@0: TTestCaseFailed request(err,msg); sl@0: iClientAction->SendRequest(request,this); sl@0: return; sl@0: } sl@0: sl@0: RDebug::Printf("Opening pipe for endpoint address %02x on interface 1 setting 1",endpointAddress); sl@0: err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<128> msg; sl@0: msg.Format(_L(" Unable to open pipe on interface 1 setting 0"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: iClientAction->SendRequest(request,this); sl@0: return; sl@0: } sl@0: sl@0: // get busId sl@0: TUsbBusId busIdPipe; sl@0: CHECK(iTestPipe.GetBusId(busIdPipe) == KErrNone); sl@0: RDebug::Printf("busId(Pipe) = %d",busIdPipe); sl@0: CHECK(busIdPipe == busId); sl@0: sl@0: TUsbEndpointId usbEpId; sl@0: CHECK(iTestPipe.GetEndpointId(usbEpId) == KErrNone); sl@0: RDebug::Printf("EndpointId = %d",usbEpId); sl@0: sl@0: // check ep descriptor now sl@0: RDebug::Printf("check ep descriptor now"); sl@0: TUsbEndpointDescriptor epDescriptor; sl@0: sl@0: sl@0: CHECK(iTestPipe.GetEndpointDescriptor(epDescriptor) == KErrNone); sl@0: TUsbEndpointDescriptor::Cast(&epDescriptor); sl@0: CHECK(&epDescriptor != 0); sl@0: CHECK(epDescriptor.iFirstChild == 0); // no children sl@0: CHECK(epDescriptor.iNextPeer != 0); // 1 peer sl@0: sl@0: sl@0: RDebug::Printf("Pipe established now closing"); sl@0: iTestPipe.Close(); sl@0: sl@0: // Select alternate interface 2, error expected sl@0: err = iUsbInterface1.SelectAlternateInterface(2); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Printf(" Selecting alternate interface 2",err); sl@0: RDebug::Printf("...Rolling Back...."); sl@0: sl@0: // Establish pipes on rollback sl@0: // Open a pipe for endpoint (Bulk out) sl@0: err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse); sl@0: if(err != KErrNone) sl@0: { sl@0: TBuf<128> msg; sl@0: msg.Format(_L(" Unable to open pipe on interface 1 setting 1 for rollback case"),err); sl@0: RDebug::Print(msg); sl@0: iCaseStep = EFailed; sl@0: TTestCaseFailed request(err,msg); sl@0: iClientAction->SendRequest(request,this); sl@0: return; sl@0: } sl@0: sl@0: RDebug::Printf("Pipe established on rollback now closing"); sl@0: iTestPipe.Close(); sl@0: } sl@0: sl@0: // Inform client device test case successful sl@0: iCaseStep = EPassed; sl@0: TTestCasePassed request; sl@0: iClientAction->SendRequest(request,this); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::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: sl@0: TestFailed(KErrDisconnected); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::BusErrorL(TInt aError) sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // This test case handles no failiures on the bus sl@0: sl@0: TestFailed(aError); sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState, sl@0: RUsbDevice::TDeviceState aNewState,TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: Cancel(); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::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(iCaseStep == EPassed) sl@0: { sl@0: TestPassed(); sl@0: } sl@0: sl@0: if(iCaseStep == EFailed) sl@0: { sl@0: TestFailed(KErrCompletion); sl@0: } sl@0: } sl@0: sl@0: sl@0: void CUT_PBASE_T_USBDI_0477::HostRunL() sl@0: { sl@0: LOG_FUNC sl@0: 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_0477::DeviceRunL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Disconnect the device sl@0: sl@0: iTestDevice->SoftwareDisconnect(); sl@0: sl@0: // Complete the test case request sl@0: sl@0: TestPolicy().SignalTestComplete(iStatus.Int()); sl@0: } sl@0: sl@0: sl@0: } sl@0: