Update contrib.
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-0477.cpp
19 #include "PBASE-T_USBDI-0477.h"
20 #include "testpolicy.h"
21 #include "modelleddevices.h"
24 namespace NUnitTesting_USBDI
27 _LIT(KTestCaseId,"PBASE-T_USBDI-0477");
28 const TFunctorTestCase<CUT_PBASE_T_USBDI_0477,TBool> CUT_PBASE_T_USBDI_0477::iFunctor(KTestCaseId);
30 CUT_PBASE_T_USBDI_0477* CUT_PBASE_T_USBDI_0477::NewL(TBool aHostRole)
32 CUT_PBASE_T_USBDI_0477* self = new (ELeave) CUT_PBASE_T_USBDI_0477(aHostRole);
33 CleanupStack::PushL(self);
35 CleanupStack::Pop(self);
40 CUT_PBASE_T_USBDI_0477::CUT_PBASE_T_USBDI_0477(TBool aHostRole)
41 : CBaseTestCase(KTestCaseId,aHostRole),
42 iCaseStep(EInProgress)
47 void CUT_PBASE_T_USBDI_0477::ConstructL()
49 iTestDevice = new RUsbDeviceA(this);
54 CUT_PBASE_T_USBDI_0477::~CUT_PBASE_T_USBDI_0477()
60 iUsbInterface1.Close();
61 iUsbInterface0.Close();
65 if(!IsHost() && iTestDevice)
73 void CUT_PBASE_T_USBDI_0477::ExecuteHostTestCaseL()
77 iActorFDF = CActorFDF::NewL(*this);
78 iClientAction = new (ELeave) CEp0Transfer(iUsbInterface0);
84 void CUT_PBASE_T_USBDI_0477::ExecuteDeviceTestCaseL()
88 iTestDevice->OpenL(TestCaseId());
89 iTestDevice->SubscribeToReports(iStatus);
91 iTestDevice->SoftwareConnect();
95 void CUT_PBASE_T_USBDI_0477::HostDoCancel()
99 // Cancel the test step timeout
105 void CUT_PBASE_T_USBDI_0477::DeviceDoCancel()
111 iTestDevice->CancelSubscriptionToReports();
114 TBool CUT_PBASE_T_USBDI_0477::CheckFirstInterfaceDescriptorDeviceA(TUsbInterfaceDescriptor& aIfDescriptor)
117 /* Interface0 [setting0]
118 Interface1 [setting0]
122 [endpoint1] Interrupt in
127 TUsbInterfaceDescriptor::Cast(&aIfDescriptor); // is it an interface?
128 CHECK_RET_BOOL(&aIfDescriptor != 0);
131 TUsbGenericDescriptor* desc = aIfDescriptor.iFirstChild;
132 TUsbEndpointDescriptor::Cast(desc);
133 CHECK_RET_BOOL(desc != 0);
136 desc = desc->iNextPeer;
137 TUsbEndpointDescriptor::Cast(desc);
138 CHECK_RET_BOOL(desc != 0);
139 desc = desc->iNextPeer; // no peer
140 CHECK_RET_BOOL(desc == 0);
143 desc = aIfDescriptor.iNextPeer;
144 TUsbInterfaceDescriptor::Cast(desc);
145 CHECK_RET_BOOL(desc != 0);
148 desc = desc->iFirstChild;
149 TUsbEndpointDescriptor::Cast(desc);
150 CHECK_RET_BOOL(desc != 0);
153 desc = desc->iNextPeer;
154 TUsbEndpointDescriptor::Cast(desc);
155 CHECK_RET_BOOL(desc != 0);
158 desc = desc->iNextPeer;
159 TUsbEndpointDescriptor::Cast(desc);
160 CHECK_RET_BOOL(desc != 0);
161 desc = desc->iNextPeer; // no peer
162 CHECK_RET_BOOL(desc == 0);
164 RDebug::Printf("CheckFirstInterfaceDescriptorDeviceA successfull!");
168 void CUT_PBASE_T_USBDI_0477::DeviceInsertedL(TUint aDeviceHandle)
174 // Validate that device is as expected
175 CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
176 if(testDevice.SerialNumber().Compare(TestCaseId()) != 0)
178 // Incorrect device for this test case
180 RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
181 KErrNotFound,&testDevice.SerialNumber(),&TestCaseId());
183 // Start the connection timeout again
188 TUint32 token0,token1; // Token for an interface
190 err = testDevice.Device().GetTokenForInterface(0,token0);
193 RDebug::Printf("<Error %d> Token for interface 0 could not be retrieved",err);
194 return TestFailed(err);
197 err = iUsbInterface0.Open(token0);
200 RDebug::Printf("<Error %d> Unable to open interface 0 using token %d",err,token0);
201 return TestFailed(err);
204 err = testDevice.Device().GetTokenForInterface(1,token1);
207 // Test case has failed
209 _LIT(string, "<Error %d> Token for interface could not be retrieved");
210 msg.Format(string,err);
213 TTestCaseFailed request(err,msg);
214 iClientAction->SendRequest(request,this);
218 err = iUsbInterface1.Open(token1); // Default interface setting 0
222 msg.Format(_L("<Error %d> Unable to open interface using token %d"),err,token1);
225 TTestCaseFailed request(err,msg);
226 iClientAction->SendRequest(request,this);
231 // check interface descriptor now
232 RDebug::Printf("check Interface descriptor now");
233 TUsbInterfaceDescriptor ifDescriptor;
234 CHECK(iUsbInterface1.GetInterfaceDescriptor(ifDescriptor) == KErrNone);
235 CHECK(CheckFirstInterfaceDescriptorDeviceA(ifDescriptor));
237 // Enumerate Endpoints On Interface 1 alt. setting 0
238 CHECK(iUsbInterface1.EnumerateEndpointsOnInterface(0) == 2);
242 CHECK(iUsbInterface1.GetBusId(busId) == KErrNone);
243 RDebug::Printf("busId(Interface) = %d",busId);
245 // get device speed now
246 RUsbInterface::TDeviceSpeed deviceSpeed;
247 CHECK(iUsbInterface1.GetDeviceSpeed(deviceSpeed) == KErrNone);
248 RDebug::Printf("GetDeviceSpeed = %d", deviceSpeed);
249 CHECK(deviceSpeed == RUsbInterface::EFullSpeed);
251 RDebug::Printf("Number of alternate interface settings available: %d",iUsbInterface1.GetAlternateInterfaceCount());
252 gtest(iUsbInterface1.GetAlternateInterfaceCount() == 2);
254 // Select alternate interface setting 1
255 err = iUsbInterface1.SelectAlternateInterface(1);
259 msg.Format(_L("<Error %d> Selecting alternate interface setting 1 on interface 1"),err);
262 TTestCaseFailed request(err,msg);
263 iClientAction->SendRequest(request,this);
267 // Open a pipe for endpoint (Bulk out)
268 TInt endpointAddress;
269 err = GetEndpointAddress(iUsbInterface1,1,KTransferTypeBulk,KEpDirectionOut,endpointAddress);
273 msg.Format(_L("<Error %d> Could not get address for Bulk out endpoint"),err);
275 TTestCaseFailed request(err,msg);
276 iClientAction->SendRequest(request,this);
280 RDebug::Printf("Opening pipe for endpoint address %02x on interface 1 setting 1",endpointAddress);
281 err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse);
285 msg.Format(_L("<Error %d> Unable to open pipe on interface 1 setting 0"),err);
288 TTestCaseFailed request(err,msg);
289 iClientAction->SendRequest(request,this);
295 CHECK(iTestPipe.GetBusId(busIdPipe) == KErrNone);
296 RDebug::Printf("busId(Pipe) = %d",busIdPipe);
297 CHECK(busIdPipe == busId);
299 TUsbEndpointId usbEpId;
300 CHECK(iTestPipe.GetEndpointId(usbEpId) == KErrNone);
301 RDebug::Printf("EndpointId = %d",usbEpId);
303 // check ep descriptor now
304 RDebug::Printf("check ep descriptor now");
305 TUsbEndpointDescriptor epDescriptor;
308 CHECK(iTestPipe.GetEndpointDescriptor(epDescriptor) == KErrNone);
309 TUsbEndpointDescriptor::Cast(&epDescriptor);
310 CHECK(&epDescriptor != 0);
311 CHECK(epDescriptor.iFirstChild == 0); // no children
312 CHECK(epDescriptor.iNextPeer != 0); // 1 peer
315 RDebug::Printf("Pipe established now closing");
318 // Select alternate interface 2, error expected
319 err = iUsbInterface1.SelectAlternateInterface(2);
322 RDebug::Printf("<Warning %d> Selecting alternate interface 2",err);
323 RDebug::Printf("...Rolling Back....");
325 // Establish pipes on rollback
326 // Open a pipe for endpoint (Bulk out)
327 err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse);
331 msg.Format(_L("<Error %d> Unable to open pipe on interface 1 setting 1 for rollback case"),err);
334 TTestCaseFailed request(err,msg);
335 iClientAction->SendRequest(request,this);
339 RDebug::Printf("Pipe established on rollback now closing");
343 // Inform client device test case successful
345 TTestCasePassed request;
346 iClientAction->SendRequest(request,this);
350 void CUT_PBASE_T_USBDI_0477::DeviceRemovedL(TUint aDeviceHandle)
354 // The test device should not be removed until the test case has passed
355 // so this test case has not completed, and state this event as an error
357 TestFailed(KErrDisconnected);
361 void CUT_PBASE_T_USBDI_0477::BusErrorL(TInt aError)
365 // This test case handles no failiures on the bus
371 void CUT_PBASE_T_USBDI_0477::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,
372 RUsbDevice::TDeviceState aNewState,TInt aCompletionCode)
378 void CUT_PBASE_T_USBDI_0477::Ep0TransferCompleteL(TInt aCompletionCode)
382 RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
384 if(iCaseStep == EPassed)
389 if(iCaseStep == EFailed)
391 TestFailed(KErrCompletion);
396 void CUT_PBASE_T_USBDI_0477::HostRunL()
400 // Obtain the completion code
401 TInt completionCode(iStatus.Int());
403 if(completionCode == KErrNone)
406 RDebug::Printf("<Error> Action timeout");
407 TestFailed(KErrTimedOut);
411 RDebug::Printf("<Error %d> Timeout timer could not complete",completionCode);
412 TestFailed(completionCode);
416 void CUT_PBASE_T_USBDI_0477::DeviceRunL()
420 // Disconnect the device
422 iTestDevice->SoftwareDisconnect();
424 // Complete the test case request
426 TestPolicy().SignalTestComplete(iStatus.Int());