First public contribution.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // @file PBASE-T_USBDI-1232.cpp
19 #include "PBASE-T_USBDI-1232.h"
21 #include "testdebug.h"
22 #include "modelleddevices.h"
23 #include "TestPolicy.h"
25 namespace NUnitTesting_USBDI
28 _LIT(KTestCaseId,"PBASE-T_USBDI-1232");
29 // the name is very important
30 const TFunctorTestCase<CUT_PBASE_T_USBDI_1232,TBool>
31 CUT_PBASE_T_USBDI_1232::iFunctor(KTestCaseId);
33 CUT_PBASE_T_USBDI_1232* CUT_PBASE_T_USBDI_1232::NewL(TBool aHostRole)
35 CUT_PBASE_T_USBDI_1232* self = new (ELeave) CUT_PBASE_T_USBDI_1232(aHostRole);
36 CleanupStack::PushL(self);
38 CleanupStack::Pop(self);
42 CUT_PBASE_T_USBDI_1232::CUT_PBASE_T_USBDI_1232(TBool aHostRole) :
43 CBaseTestCase(KTestCaseId, aHostRole)
47 void CUT_PBASE_T_USBDI_1232::ConstructL()
49 RDebug::Printf("====> Constructor entry priority = %d", RThread().Priority());
51 // Collect existing thread priority (to reinstate later)
52 iPriority = RThread().Priority();
54 iTestDevice = new RUsbDeviceA(this);
58 CUT_PBASE_T_USBDI_1232::~CUT_PBASE_T_USBDI_1232()
62 RDebug::Printf("====> Destructor entry priority = %d", RThread().Priority());
64 // Reinstate original priority
66 RThread().SetPriority(iPriority);
68 // Cancel any async operations
70 Cancel(); // Cancel host timer
72 // Destroy the watchers
73 // they still use opened interfaces to cancel the suspend if active
74 delete iInterface1Watcher;
75 delete iInterface0Watcher;
77 // Close the interfaces
78 iUsbInterface1.Close();
79 iUsbInterface0.Close();
83 if (!IsHost() && iTestDevice)
90 void CUT_PBASE_T_USBDI_1232::ExecuteHostTestCaseL()
94 RDebug::Printf("====> ExecuteHostTestCaseL entry priority = %d",
95 RThread().Priority());
97 // Bump thread priority for this test only
99 RThread().SetPriority(EPriorityAbsoluteHigh);
100 RDebug::Printf("Thread priority raised %d->%d", iPriority, RThread().Priority());
102 iCaseStep = EInProcess;
103 iActorFDF = CActorFDF::NewL(*this);
104 iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
105 iInterface0Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface0,TCallBack(CUT_PBASE_T_USBDI_1232::Interface0ResumedL,this));
106 iInterface1Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface1,TCallBack(CUT_PBASE_T_USBDI_1232::Interface1ResumedL,this));
108 // Monitor for device connections
109 iActorFDF->Monitor();
111 // Start the connection timeout
115 void CUT_PBASE_T_USBDI_1232::ExecuteDeviceTestCaseL()
119 // Construct the device for the test case
120 iTestDevice->OpenL(TestCaseId());
121 iTestDevice->SubscribeToReports(iStatus);
124 // Connect the test device
125 iTestDevice->SoftwareConnect();
128 void CUT_PBASE_T_USBDI_1232::HostDoCancel()
132 RDebug::Printf("====> HostDoCancel entry priority = %d", RThread().Priority());
134 // Cancel the timeout timer
138 void CUT_PBASE_T_USBDI_1232::DeviceDoCancel()
143 iTestDevice->CancelSubscriptionToReports();
146 void CUT_PBASE_T_USBDI_1232::DeviceInsertedL(TUint aDeviceHandle)
150 RDebug::Printf("====> DeviceInsertedL entry priority = %d", RThread().Priority());
152 Cancel(); // Cancel the timer
154 iDeviceHandle = aDeviceHandle;
155 iActorFDF->Monitor();
157 // Validate that device is as expected
158 CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
159 if (testDevice.SerialNumber().Compare(TestCaseId()) != 0)
161 // Incorrect device for this test case
164 "<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
165 KErrNotFound, &testDevice.SerialNumber(), &TestCaseId());
167 // Start the connection timeout again
172 CHECK(CheckTreeAfterDeviceInsertion(testDevice, _L("RDeviceA")) == KErrNone);
174 // Perform the correct test step
182 RDebug::Printf("Obtaining token for interface 0");
183 err = testDevice.Device().GetTokenForInterface(0, token1);
187 "<Error %d> Token for interface 0 could not be retrieved",
189 return TestFailed(err);
191 RDebug::Printf("Token 1 (%d) retrieved", token1);
192 RDebug::Printf("Opening interface 0");
193 err = iUsbInterface0.Open(token1); // Alternate interface setting 0
197 "<Error %d> Interface 0 could not be opened", err);
198 return TestFailed(err);
200 RDebug::Printf("Interface 0 opened");
202 RDebug::Printf("Obtaining token for interface 1");
203 err = testDevice.Device().GetTokenForInterface(1, token2);
207 "<Error %d> Token for interface 1 could not be retrieved",
209 return TestFailed(err);
211 RDebug::Printf("Opening interface 1");
212 err = iUsbInterface1.Open(token2); // Alternate interface setting 0
216 "<Error %d> Interface 1 could not be opened", err);
217 return TestFailed(err);
219 RDebug::Printf("Interface 1 opened");
221 // device go to suspend
222 // Suspend interface 0
223 RDebug::Printf("Suspending interface 0");
224 iInterface0Watcher->SuspendAndWatch();
226 // Suspend interface 1
227 RDebug::Printf("Suspending interface 1");
228 iInterface1Watcher->SuspendAndWatch();
230 iCaseStep = ESuspendWhenResuming;
232 TimeoutIn(10); // Give 10 seconds for device to suspend
235 iUsbInterface0.CancelWaitForResume(); // a tricky way to close the watcher of interface
236 iUsbInterface1.CancelWaitForResume();
241 TestFailed(KErrCorrupt);
246 TInt CUT_PBASE_T_USBDI_1232::Interface0ResumedL(TAny* aPtr)
250 RDebug::Printf("====> Interface0ResumedL entry priority = %d", RThread().Priority());
252 RDebug::Printf(" -Interface 0 resumed");
253 CUT_PBASE_T_USBDI_1232* self =
254 reinterpret_cast<CUT_PBASE_T_USBDI_1232*>(aPtr);
255 TInt completionCode = self->iInterface0Watcher->CompletionCode();
256 RDebug::Printf(" -watcher 0 iStatus=%d",completionCode);
258 switch (self->iCaseStep)
263 if (completionCode == KErrNone)
265 RDebug::Printf("Device resume successed,test passed!");
266 self->SendEp0Request(); // stop client site
270 RDebug::Printf("Device resume failed, err = %d ",completionCode);
271 self->iCaseStep = EFailed;
272 self->SendEp0Request();
277 self->SendEp0Request();
287 TInt CUT_PBASE_T_USBDI_1232::Interface1ResumedL(TAny* aPtr)
291 RDebug::Printf("====> Interface1ResumedL entry priority = %d", RThread().Priority());
293 RDebug::Printf("Interface 1 resumed");
294 CUT_PBASE_T_USBDI_1232* self =
295 reinterpret_cast<CUT_PBASE_T_USBDI_1232*>(aPtr);
296 RDebug::Printf("watcher 1 iStatus=%d", self->iInterface1Watcher->CompletionCode());
300 void CUT_PBASE_T_USBDI_1232::DeviceRemovedL(TUint aDeviceHandle)
304 // The test device should not be removed until the test case has passed
305 // so this test case has not completed, and state this event as an error
307 TestFailed(KErrDisconnected);
310 void CUT_PBASE_T_USBDI_1232::BusErrorL(TInt aError)
314 // This test case handles no failiures on the bus
319 void CUT_PBASE_T_USBDI_1232::DeviceStateChangeL(
320 RUsbDevice::TDeviceState aPreviousState,
321 RUsbDevice::TDeviceState aNewState, TInt aCompletionCode)
326 // test RInterface , the RUsbDevice notification logic not used .
327 RDebug::Printf(" -Device State change from err=%d",aCompletionCode);
331 case ESuspendWhenResuming:
332 if (aNewState == RUsbDevice::EDeviceSuspended)
334 RDebug::Printf("====> device has suspended!");
336 SuspendWhenResuming();
339 case EValidSuspendWhenResuming:
340 if (aPreviousState == RUsbDevice::EDeviceSuspended&&aNewState
341 == RUsbDevice::EDeviceSuspended)
343 RDebug::Printf("====> device suspended again,suspend while resuming succeed!");
345 iUsbInterface0.CancelPermitSuspend();
350 iUsbInterface0.CancelPermitSuspend();
360 void CUT_PBASE_T_USBDI_1232::Ep0TransferCompleteL(TInt aCompletionCode)
363 RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",
370 TestFailed(KErrCompletion);
379 void CUT_PBASE_T_USBDI_1232::HostRunL()
383 RDebug::Printf("====> HostRunL entry priority = %d", RThread().Priority());
385 // Obtain the completion code
386 TInt completionCode(iStatus.Int());
388 if (completionCode == KErrNone)
391 RDebug::Printf("<Error> Action timeout");
392 TestFailed(KErrTimedOut);
396 RDebug::Printf("<Error %d> Timeout timer could not complete",
398 TestFailed(completionCode);
402 void CUT_PBASE_T_USBDI_1232::DeviceRunL()
406 // Disconnect the device
408 iTestDevice->SoftwareDisconnect();
410 // Complete the test case request
412 TestPolicy().SignalTestComplete(iStatus.Int());
415 void CUT_PBASE_T_USBDI_1232::SuspendWhenResuming()
417 RDebug::Printf("====> SuspendWhenResuming entry priority = %d",
418 RThread().Priority());
420 // Cancel suspend-in-progress
421 RDebug::Printf("Cancel Suspend interface 0");
423 iUsbInterface0.CancelPermitSuspend();
425 // how to prove, see log?
426 // Suspend interface 0
427 RDebug::Printf("Suspending interface 0");
428 iInterface0Watcher->SuspendAndWatch();
430 // Suspend interface 1
431 RDebug::Printf("Suspending interface 1");
432 iInterface1Watcher->SuspendAndWatch();
434 iCaseStep = EValidSuspendWhenResuming;
436 TimeoutIn(10); // Give 10 seconds for device to suspend
440 void CUT_PBASE_T_USBDI_1232::SendEp0Request()
442 TTestCasePassed request;
443 iControlEp0->SendRequest(request, this);