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-1232.cpp sl@0: // @internalComponent sl@0: // sl@0: // sl@0: sl@0: #include "PBASE-T_USBDI-1232.h" sl@0: #include sl@0: #include "testdebug.h" sl@0: #include "modelleddevices.h" sl@0: #include "TestPolicy.h" sl@0: sl@0: namespace NUnitTesting_USBDI sl@0: { sl@0: sl@0: _LIT(KTestCaseId,"PBASE-T_USBDI-1232"); sl@0: // the name is very important sl@0: const TFunctorTestCase sl@0: CUT_PBASE_T_USBDI_1232::iFunctor(KTestCaseId); sl@0: sl@0: CUT_PBASE_T_USBDI_1232* CUT_PBASE_T_USBDI_1232::NewL(TBool aHostRole) sl@0: { sl@0: CUT_PBASE_T_USBDI_1232* self = new (ELeave) CUT_PBASE_T_USBDI_1232(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: CUT_PBASE_T_USBDI_1232::CUT_PBASE_T_USBDI_1232(TBool aHostRole) : sl@0: CBaseTestCase(KTestCaseId, aHostRole) sl@0: { sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::ConstructL() sl@0: { sl@0: RDebug::Printf("====> Constructor entry priority = %d", RThread().Priority()); sl@0: sl@0: // Collect existing thread priority (to reinstate later) sl@0: iPriority = RThread().Priority(); sl@0: sl@0: iTestDevice = new RUsbDeviceA(this); sl@0: BaseConstructL(); sl@0: } sl@0: sl@0: CUT_PBASE_T_USBDI_1232::~CUT_PBASE_T_USBDI_1232() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RDebug::Printf("====> Destructor entry priority = %d", RThread().Priority()); sl@0: sl@0: // Reinstate original priority sl@0: sl@0: RThread().SetPriority(iPriority); sl@0: sl@0: // Cancel any async operations sl@0: sl@0: Cancel(); // Cancel host timer sl@0: sl@0: // Destroy the watchers sl@0: // they still use opened interfaces to cancel the suspend if active sl@0: delete iInterface1Watcher; sl@0: delete iInterface0Watcher; sl@0: sl@0: // Close the 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_1232::ExecuteHostTestCaseL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RDebug::Printf("====> ExecuteHostTestCaseL entry priority = %d", sl@0: RThread().Priority()); sl@0: sl@0: // Bump thread priority for this test only sl@0: sl@0: RThread().SetPriority(EPriorityAbsoluteHigh); sl@0: RDebug::Printf("Thread priority raised %d->%d", iPriority, RThread().Priority()); sl@0: sl@0: iCaseStep = EInProcess; sl@0: iActorFDF = CActorFDF::NewL(*this); sl@0: iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0); sl@0: iInterface0Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface0,TCallBack(CUT_PBASE_T_USBDI_1232::Interface0ResumedL,this)); sl@0: iInterface1Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface1,TCallBack(CUT_PBASE_T_USBDI_1232::Interface1ResumedL,this)); sl@0: sl@0: // Monitor for device connections sl@0: iActorFDF->Monitor(); sl@0: sl@0: // Start the connection timeout sl@0: TimeoutIn(30); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::ExecuteDeviceTestCaseL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Construct the device for the test case sl@0: iTestDevice->OpenL(TestCaseId()); sl@0: iTestDevice->SubscribeToReports(iStatus); sl@0: SetActive(); sl@0: sl@0: // Connect the test device sl@0: iTestDevice->SoftwareConnect(); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::HostDoCancel() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RDebug::Printf("====> HostDoCancel entry priority = %d", RThread().Priority()); sl@0: sl@0: // Cancel the timeout timer sl@0: CancelTimeout(); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::DeviceDoCancel() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: // Cancel the device sl@0: iTestDevice->CancelSubscriptionToReports(); sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::DeviceInsertedL(TUint aDeviceHandle) sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RDebug::Printf("====> DeviceInsertedL entry priority = %d", RThread().Priority()); sl@0: sl@0: Cancel(); // Cancel the timer sl@0: TInt err(KErrNone); sl@0: iDeviceHandle = aDeviceHandle; sl@0: iActorFDF->Monitor(); 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( sl@0: " 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: // Check tree now sl@0: CHECK(CheckTreeAfterDeviceInsertion(testDevice, _L("RDeviceA")) == KErrNone); sl@0: sl@0: // Perform the correct test step sl@0: switch (iCaseStep) sl@0: { sl@0: case EInProcess: sl@0: { sl@0: TUint32 token1(0); sl@0: TUint32 token2(0); sl@0: sl@0: RDebug::Printf("Obtaining token for interface 0"); sl@0: err = testDevice.Device().GetTokenForInterface(0, token1); sl@0: if (err != KErrNone) sl@0: { sl@0: RDebug::Printf( sl@0: " Token for interface 0 could not be retrieved", sl@0: err); sl@0: return TestFailed(err); sl@0: } sl@0: RDebug::Printf("Token 1 (%d) retrieved", token1); sl@0: RDebug::Printf("Opening interface 0"); sl@0: err = iUsbInterface0.Open(token1); // Alternate interface setting 0 sl@0: if (err != KErrNone) sl@0: { sl@0: RDebug::Printf( sl@0: " Interface 0 could not be opened", err); sl@0: return TestFailed(err); sl@0: } sl@0: RDebug::Printf("Interface 0 opened"); sl@0: sl@0: RDebug::Printf("Obtaining token for interface 1"); sl@0: err = testDevice.Device().GetTokenForInterface(1, token2); sl@0: if (err != KErrNone) sl@0: { sl@0: RDebug::Printf( sl@0: " Token for interface 1 could not be retrieved", sl@0: err); sl@0: return TestFailed(err); sl@0: } sl@0: RDebug::Printf("Opening interface 1"); sl@0: err = iUsbInterface1.Open(token2); // Alternate interface setting 0 sl@0: if (err != KErrNone) sl@0: { sl@0: RDebug::Printf( sl@0: " Interface 1 could not be opened", err); sl@0: return TestFailed(err); sl@0: } sl@0: RDebug::Printf("Interface 1 opened"); sl@0: sl@0: // device go to suspend sl@0: // Suspend interface 0 sl@0: RDebug::Printf("Suspending interface 0"); sl@0: iInterface0Watcher->SuspendAndWatch(); sl@0: sl@0: // Suspend interface 1 sl@0: RDebug::Printf("Suspending interface 1"); sl@0: iInterface1Watcher->SuspendAndWatch(); sl@0: sl@0: iCaseStep = ESuspendWhenResuming; sl@0: sl@0: TimeoutIn(10); // Give 10 seconds for device to suspend sl@0: sl@0: sl@0: iUsbInterface0.CancelWaitForResume(); // a tricky way to close the watcher of interface sl@0: iUsbInterface1.CancelWaitForResume(); sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: TestFailed(KErrCorrupt); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: TInt CUT_PBASE_T_USBDI_1232::Interface0ResumedL(TAny* aPtr) sl@0: { sl@0: LOG_CFUNC sl@0: sl@0: RDebug::Printf("====> Interface0ResumedL entry priority = %d", RThread().Priority()); sl@0: sl@0: RDebug::Printf(" -Interface 0 resumed"); sl@0: CUT_PBASE_T_USBDI_1232* self = sl@0: reinterpret_cast(aPtr); sl@0: TInt completionCode = self->iInterface0Watcher->CompletionCode(); sl@0: RDebug::Printf(" -watcher 0 iStatus=%d",completionCode); sl@0: sl@0: switch (self->iCaseStep) sl@0: { sl@0: sl@0: case EPassed: sl@0: { sl@0: if (completionCode == KErrNone) sl@0: { sl@0: RDebug::Printf("Device resume successed,test passed!"); sl@0: self->SendEp0Request(); // stop client site sl@0: } sl@0: else sl@0: { sl@0: RDebug::Printf("Device resume failed, err = %d ",completionCode); sl@0: self->iCaseStep = EFailed; sl@0: self->SendEp0Request(); sl@0: } sl@0: } sl@0: break; sl@0: case EFailed: sl@0: self->SendEp0Request(); sl@0: break; sl@0: sl@0: default: sl@0: break; sl@0: }; sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: TInt CUT_PBASE_T_USBDI_1232::Interface1ResumedL(TAny* aPtr) sl@0: { sl@0: LOG_CFUNC sl@0: sl@0: RDebug::Printf("====> Interface1ResumedL entry priority = %d", RThread().Priority()); sl@0: sl@0: RDebug::Printf("Interface 1 resumed"); sl@0: CUT_PBASE_T_USBDI_1232* self = sl@0: reinterpret_cast(aPtr); sl@0: RDebug::Printf("watcher 1 iStatus=%d", self->iInterface1Watcher->CompletionCode()); sl@0: return KErrNone; sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::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: void CUT_PBASE_T_USBDI_1232::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: void CUT_PBASE_T_USBDI_1232::DeviceStateChangeL( sl@0: RUsbDevice::TDeviceState aPreviousState, sl@0: RUsbDevice::TDeviceState aNewState, TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: Cancel(); sl@0: sl@0: // test RInterface , the RUsbDevice notification logic not used . sl@0: RDebug::Printf(" -Device State change from err=%d",aCompletionCode); sl@0: sl@0: switch (iCaseStep) sl@0: { sl@0: case ESuspendWhenResuming: sl@0: if (aNewState == RUsbDevice::EDeviceSuspended) sl@0: { sl@0: RDebug::Printf("====> device has suspended!"); sl@0: sl@0: SuspendWhenResuming(); sl@0: } sl@0: break; sl@0: case EValidSuspendWhenResuming: sl@0: if (aPreviousState == RUsbDevice::EDeviceSuspended&&aNewState sl@0: == RUsbDevice::EDeviceSuspended) sl@0: { sl@0: RDebug::Printf("====> device suspended again,suspend while resuming succeed!"); sl@0: iCaseStep = EPassed; sl@0: iUsbInterface0.CancelPermitSuspend(); sl@0: } sl@0: else sl@0: { sl@0: iCaseStep = EFailed; sl@0: iUsbInterface0.CancelPermitSuspend(); sl@0: } sl@0: sl@0: break; sl@0: default: sl@0: break; sl@0: } sl@0: sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::Ep0TransferCompleteL(TInt aCompletionCode) sl@0: { sl@0: LOG_FUNC sl@0: RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d", sl@0: aCompletionCode); sl@0: switch (iCaseStep) sl@0: { sl@0: sl@0: default: sl@0: case EFailed: sl@0: TestFailed(KErrCompletion); sl@0: break; sl@0: sl@0: case EPassed: sl@0: TestPassed(); sl@0: break; sl@0: } sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::HostRunL() sl@0: { sl@0: LOG_FUNC sl@0: sl@0: RDebug::Printf("====> HostRunL entry priority = %d", RThread().Priority()); 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", sl@0: completionCode); sl@0: TestFailed(completionCode); sl@0: } sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::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: void CUT_PBASE_T_USBDI_1232::SuspendWhenResuming() sl@0: { sl@0: RDebug::Printf("====> SuspendWhenResuming entry priority = %d", sl@0: RThread().Priority()); sl@0: sl@0: // Cancel suspend-in-progress sl@0: RDebug::Printf("Cancel Suspend interface 0"); sl@0: sl@0: iUsbInterface0.CancelPermitSuspend(); sl@0: sl@0: // how to prove, see log? sl@0: // Suspend interface 0 sl@0: RDebug::Printf("Suspending interface 0"); sl@0: iInterface0Watcher->SuspendAndWatch(); sl@0: sl@0: // Suspend interface 1 sl@0: RDebug::Printf("Suspending interface 1"); sl@0: iInterface1Watcher->SuspendAndWatch(); sl@0: sl@0: iCaseStep = EValidSuspendWhenResuming; sl@0: sl@0: TimeoutIn(10); // Give 10 seconds for device to suspend sl@0: sl@0: } sl@0: sl@0: void CUT_PBASE_T_USBDI_1232::SendEp0Request() sl@0: { sl@0: TTestCasePassed request; sl@0: iControlEp0->SendRequest(request, this); sl@0: } sl@0: sl@0: }//end namespace sl@0: sl@0: