os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0477.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// @file PBASE-T_USBDI-0477.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "PBASE-T_USBDI-0477.h"
sl@0
    20
#include "testpolicy.h"
sl@0
    21
#include "modelleddevices.h"
sl@0
    22
sl@0
    23
sl@0
    24
namespace NUnitTesting_USBDI
sl@0
    25
	{
sl@0
    26
	
sl@0
    27
_LIT(KTestCaseId,"PBASE-T_USBDI-0477");
sl@0
    28
const TFunctorTestCase<CUT_PBASE_T_USBDI_0477,TBool> CUT_PBASE_T_USBDI_0477::iFunctor(KTestCaseId);	
sl@0
    29
sl@0
    30
CUT_PBASE_T_USBDI_0477* CUT_PBASE_T_USBDI_0477::NewL(TBool aHostRole)
sl@0
    31
	{
sl@0
    32
	CUT_PBASE_T_USBDI_0477* self = new (ELeave) CUT_PBASE_T_USBDI_0477(aHostRole);
sl@0
    33
	CleanupStack::PushL(self);
sl@0
    34
	self->ConstructL();
sl@0
    35
	CleanupStack::Pop(self);
sl@0
    36
	return self;
sl@0
    37
	}
sl@0
    38
	
sl@0
    39
sl@0
    40
CUT_PBASE_T_USBDI_0477::CUT_PBASE_T_USBDI_0477(TBool aHostRole)
sl@0
    41
:	CBaseTestCase(KTestCaseId,aHostRole),
sl@0
    42
	iCaseStep(EInProgress)
sl@0
    43
	{
sl@0
    44
	} 
sl@0
    45
sl@0
    46
 
sl@0
    47
void CUT_PBASE_T_USBDI_0477::ConstructL()
sl@0
    48
	{
sl@0
    49
	iTestDevice = new RUsbDeviceA(this);
sl@0
    50
	BaseConstructL();
sl@0
    51
	}
sl@0
    52
sl@0
    53
sl@0
    54
CUT_PBASE_T_USBDI_0477::~CUT_PBASE_T_USBDI_0477()
sl@0
    55
	{
sl@0
    56
	LOG_FUNC
sl@0
    57
	Cancel();
sl@0
    58
sl@0
    59
	iTestPipe.Close();
sl@0
    60
	iUsbInterface1.Close();
sl@0
    61
	iUsbInterface0.Close();
sl@0
    62
sl@0
    63
	delete iClientAction;
sl@0
    64
	delete iActorFDF;
sl@0
    65
	if(!IsHost() && iTestDevice)
sl@0
    66
		{
sl@0
    67
		iTestDevice->Close();
sl@0
    68
		}		
sl@0
    69
	delete iTestDevice;
sl@0
    70
	}
sl@0
    71
	
sl@0
    72
	
sl@0
    73
void CUT_PBASE_T_USBDI_0477::ExecuteHostTestCaseL()
sl@0
    74
	{
sl@0
    75
	LOG_FUNC
sl@0
    76
	
sl@0
    77
	iActorFDF = CActorFDF::NewL(*this);
sl@0
    78
	iClientAction = new (ELeave) CEp0Transfer(iUsbInterface0);
sl@0
    79
	iActorFDF->Monitor();
sl@0
    80
	TimeoutIn(30);
sl@0
    81
	}
sl@0
    82
sl@0
    83
	
sl@0
    84
void CUT_PBASE_T_USBDI_0477::ExecuteDeviceTestCaseL()
sl@0
    85
	{
sl@0
    86
	LOG_FUNC
sl@0
    87
	
sl@0
    88
	iTestDevice->OpenL(TestCaseId());
sl@0
    89
	iTestDevice->SubscribeToReports(iStatus);
sl@0
    90
	SetActive();
sl@0
    91
	iTestDevice->SoftwareConnect();
sl@0
    92
	}
sl@0
    93
	
sl@0
    94
	
sl@0
    95
void CUT_PBASE_T_USBDI_0477::HostDoCancel()
sl@0
    96
	{
sl@0
    97
	LOG_FUNC
sl@0
    98
	
sl@0
    99
	// Cancel the test step timeout
sl@0
   100
	
sl@0
   101
	CancelTimeout();
sl@0
   102
	}
sl@0
   103
sl@0
   104
sl@0
   105
void CUT_PBASE_T_USBDI_0477::DeviceDoCancel()
sl@0
   106
	{
sl@0
   107
	LOG_FUNC
sl@0
   108
	
sl@0
   109
	// Cancel the device
sl@0
   110
	
sl@0
   111
	iTestDevice->CancelSubscriptionToReports();
sl@0
   112
	}
sl@0
   113
	
sl@0
   114
TBool CUT_PBASE_T_USBDI_0477::CheckFirstInterfaceDescriptorDeviceA(TUsbInterfaceDescriptor& aIfDescriptor)
sl@0
   115
	{
sl@0
   116
	LOG_FUNC	
sl@0
   117
	/*	Interface0	[setting0]
sl@0
   118
		Interface1	[setting0]
sl@0
   119
						[endpoint1] Bulk out
sl@0
   120
						[endpoint2] Bulk in
sl@0
   121
					[setting1]			
sl@0
   122
						[endpoint1] Interrupt in
sl@0
   123
						[endpoint2] Bulk out
sl@0
   124
						[endpoint3] Bulk in
sl@0
   125
	*/					
sl@0
   126
		// alt. setting 0
sl@0
   127
		TUsbInterfaceDescriptor::Cast(&aIfDescriptor); // is it an interface?
sl@0
   128
		CHECK_RET_BOOL(&aIfDescriptor != 0);
sl@0
   129
		
sl@0
   130
		// EP1
sl@0
   131
		TUsbGenericDescriptor* desc = aIfDescriptor.iFirstChild;
sl@0
   132
		TUsbEndpointDescriptor::Cast(desc);
sl@0
   133
		CHECK_RET_BOOL(desc != 0);
sl@0
   134
						
sl@0
   135
		// EP 2
sl@0
   136
		desc = desc->iNextPeer;
sl@0
   137
		TUsbEndpointDescriptor::Cast(desc);
sl@0
   138
		CHECK_RET_BOOL(desc != 0);		
sl@0
   139
		desc = desc->iNextPeer; // no peer
sl@0
   140
		CHECK_RET_BOOL(desc == 0);
sl@0
   141
		
sl@0
   142
		// alt. setting 1
sl@0
   143
		desc = aIfDescriptor.iNextPeer;
sl@0
   144
		TUsbInterfaceDescriptor::Cast(desc);
sl@0
   145
		CHECK_RET_BOOL(desc != 0);
sl@0
   146
		
sl@0
   147
		// EP1
sl@0
   148
		desc = desc->iFirstChild;
sl@0
   149
		TUsbEndpointDescriptor::Cast(desc);
sl@0
   150
		CHECK_RET_BOOL(desc != 0);
sl@0
   151
		
sl@0
   152
		// EP 2
sl@0
   153
		desc = desc->iNextPeer;
sl@0
   154
		TUsbEndpointDescriptor::Cast(desc);
sl@0
   155
		CHECK_RET_BOOL(desc != 0);
sl@0
   156
		
sl@0
   157
		// EP 3 
sl@0
   158
		desc = desc->iNextPeer;
sl@0
   159
		TUsbEndpointDescriptor::Cast(desc);
sl@0
   160
		CHECK_RET_BOOL(desc != 0);			
sl@0
   161
		desc = desc->iNextPeer; // no peer
sl@0
   162
		CHECK_RET_BOOL(desc == 0); 
sl@0
   163
		
sl@0
   164
		RDebug::Printf("CheckFirstInterfaceDescriptorDeviceA successfull!");
sl@0
   165
		return ETrue;
sl@0
   166
	}
sl@0
   167
	
sl@0
   168
void CUT_PBASE_T_USBDI_0477::DeviceInsertedL(TUint aDeviceHandle)
sl@0
   169
	{
sl@0
   170
	LOG_FUNC
sl@0
   171
	Cancel();
sl@0
   172
	TInt err(KErrNone);
sl@0
   173
	
sl@0
   174
	// Validate that device is as expected
sl@0
   175
	CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
sl@0
   176
	if(testDevice.SerialNumber().Compare(TestCaseId()) != 0)
sl@0
   177
		{
sl@0
   178
		// Incorrect device for this test case	
sl@0
   179
sl@0
   180
		RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
sl@0
   181
			KErrNotFound,&testDevice.SerialNumber(),&TestCaseId());
sl@0
   182
sl@0
   183
		// Start the connection timeout again
sl@0
   184
		TimeoutIn(30);
sl@0
   185
		return;
sl@0
   186
		}
sl@0
   187
			
sl@0
   188
	TUint32 token0,token1; // Token for an interface
sl@0
   189
	
sl@0
   190
	err = testDevice.Device().GetTokenForInterface(0,token0);
sl@0
   191
	if(err != KErrNone)
sl@0
   192
		{
sl@0
   193
		RDebug::Printf("<Error %d> Token for interface 0 could not be retrieved",err);
sl@0
   194
		return TestFailed(err);
sl@0
   195
		}
sl@0
   196
		
sl@0
   197
	err = iUsbInterface0.Open(token0);
sl@0
   198
	if(err != KErrNone)
sl@0
   199
		{
sl@0
   200
		RDebug::Printf("<Error %d> Unable to open interface 0 using token %d",err,token0);
sl@0
   201
		return TestFailed(err);
sl@0
   202
		}
sl@0
   203
	
sl@0
   204
	err = testDevice.Device().GetTokenForInterface(1,token1);
sl@0
   205
	if(err != KErrNone)
sl@0
   206
		{
sl@0
   207
		// Test case has failed
sl@0
   208
		TBuf<256> msg;
sl@0
   209
		_LIT(string, "<Error %d> Token for interface could not be retrieved");
sl@0
   210
		msg.Format(string,err);
sl@0
   211
		RDebug::Print(msg);
sl@0
   212
		iCaseStep = EFailed;
sl@0
   213
		TTestCaseFailed request(err,msg);
sl@0
   214
		iClientAction->SendRequest(request,this);
sl@0
   215
		return;
sl@0
   216
		}
sl@0
   217
sl@0
   218
	err = iUsbInterface1.Open(token1); // Default interface setting 0
sl@0
   219
	if(err != KErrNone)
sl@0
   220
		{
sl@0
   221
		TBuf<256> msg;
sl@0
   222
		msg.Format(_L("<Error %d> Unable to open interface using token %d"),err,token1);
sl@0
   223
		RDebug::Print(msg);
sl@0
   224
		iCaseStep = EFailed;
sl@0
   225
		TTestCaseFailed request(err,msg);
sl@0
   226
		iClientAction->SendRequest(request,this);
sl@0
   227
		return;
sl@0
   228
		}
sl@0
   229
		
sl@0
   230
			
sl@0
   231
	// check interface descriptor now				 
sl@0
   232
	RDebug::Printf("check Interface descriptor now");
sl@0
   233
	TUsbInterfaceDescriptor ifDescriptor;
sl@0
   234
	CHECK(iUsbInterface1.GetInterfaceDescriptor(ifDescriptor) == KErrNone);	
sl@0
   235
	CHECK(CheckFirstInterfaceDescriptorDeviceA(ifDescriptor));
sl@0
   236
																				
sl@0
   237
	//  Enumerate Endpoints On Interface 1 alt. setting 0
sl@0
   238
	CHECK(iUsbInterface1.EnumerateEndpointsOnInterface(0) == 2);
sl@0
   239
	
sl@0
   240
	// get busId now				
sl@0
   241
	TUsbBusId busId;	 
sl@0
   242
	CHECK(iUsbInterface1.GetBusId(busId) == KErrNone);
sl@0
   243
	RDebug::Printf("busId(Interface) = %d",busId);
sl@0
   244
	
sl@0
   245
	// get device speed now
sl@0
   246
	RUsbInterface::TDeviceSpeed deviceSpeed;
sl@0
   247
	CHECK(iUsbInterface1.GetDeviceSpeed(deviceSpeed) == KErrNone);
sl@0
   248
	RDebug::Printf("GetDeviceSpeed = %d", deviceSpeed);
sl@0
   249
	CHECK(deviceSpeed == RUsbInterface::EFullSpeed);	
sl@0
   250
		
sl@0
   251
	RDebug::Printf("Number of alternate interface settings available: %d",iUsbInterface1.GetAlternateInterfaceCount());
sl@0
   252
	gtest(iUsbInterface1.GetAlternateInterfaceCount() == 2);
sl@0
   253
	
sl@0
   254
	// Select alternate interface setting 1	
sl@0
   255
	err = iUsbInterface1.SelectAlternateInterface(1);
sl@0
   256
	if(err != KErrNone)
sl@0
   257
		{
sl@0
   258
		TBuf<256> msg;
sl@0
   259
		msg.Format(_L("<Error %d> Selecting alternate interface setting 1 on interface 1"),err);
sl@0
   260
		RDebug::Print(msg);
sl@0
   261
		iCaseStep = EFailed;
sl@0
   262
		TTestCaseFailed request(err,msg);
sl@0
   263
		iClientAction->SendRequest(request,this);
sl@0
   264
		return;
sl@0
   265
		}	
sl@0
   266
	
sl@0
   267
	// Open a pipe for endpoint (Bulk out)	
sl@0
   268
	TInt endpointAddress;
sl@0
   269
	err = GetEndpointAddress(iUsbInterface1,1,KTransferTypeBulk,KEpDirectionOut,endpointAddress);
sl@0
   270
	if(err != KErrNone)
sl@0
   271
		{
sl@0
   272
		TBuf<128> msg;
sl@0
   273
		msg.Format(_L("<Error %d> Could not get address for Bulk out endpoint"),err);
sl@0
   274
		RDebug::Print(msg);
sl@0
   275
		TTestCaseFailed request(err,msg);
sl@0
   276
		iClientAction->SendRequest(request,this);
sl@0
   277
		return;
sl@0
   278
		}
sl@0
   279
		
sl@0
   280
	RDebug::Printf("Opening pipe for endpoint address %02x on interface 1 setting 1",endpointAddress);
sl@0
   281
	err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse);
sl@0
   282
	if(err != KErrNone)
sl@0
   283
		{
sl@0
   284
		TBuf<128> msg;
sl@0
   285
		msg.Format(_L("<Error %d> Unable to open pipe on interface 1 setting 0"),err);
sl@0
   286
		RDebug::Print(msg);
sl@0
   287
		iCaseStep = EFailed;
sl@0
   288
		TTestCaseFailed request(err,msg);
sl@0
   289
		iClientAction->SendRequest(request,this);
sl@0
   290
		return;
sl@0
   291
		}
sl@0
   292
		
sl@0
   293
	// get busId				
sl@0
   294
	TUsbBusId busIdPipe;	 
sl@0
   295
	CHECK(iTestPipe.GetBusId(busIdPipe) == KErrNone);
sl@0
   296
	RDebug::Printf("busId(Pipe) = %d",busIdPipe);
sl@0
   297
	CHECK(busIdPipe == busId);
sl@0
   298
	
sl@0
   299
	TUsbEndpointId usbEpId;
sl@0
   300
	CHECK(iTestPipe.GetEndpointId(usbEpId) == KErrNone);
sl@0
   301
	RDebug::Printf("EndpointId = %d",usbEpId); 
sl@0
   302
	
sl@0
   303
	// check ep descriptor now				 
sl@0
   304
	RDebug::Printf("check ep descriptor now");
sl@0
   305
	TUsbEndpointDescriptor epDescriptor;
sl@0
   306
	
sl@0
   307
sl@0
   308
	CHECK(iTestPipe.GetEndpointDescriptor(epDescriptor) == KErrNone);
sl@0
   309
	TUsbEndpointDescriptor::Cast(&epDescriptor);
sl@0
   310
	CHECK(&epDescriptor != 0);
sl@0
   311
	CHECK(epDescriptor.iFirstChild == 0); // no children	
sl@0
   312
	CHECK(epDescriptor.iNextPeer != 0);	// 1 peer
sl@0
   313
sl@0
   314
		
sl@0
   315
	RDebug::Printf("Pipe established now closing");
sl@0
   316
	iTestPipe.Close();
sl@0
   317
	
sl@0
   318
	// Select alternate interface 2, error expected
sl@0
   319
	err = iUsbInterface1.SelectAlternateInterface(2);
sl@0
   320
	if(err != KErrNone)
sl@0
   321
		{
sl@0
   322
		RDebug::Printf("<Warning %d> Selecting alternate interface 2",err);
sl@0
   323
		RDebug::Printf("...Rolling Back....");
sl@0
   324
		
sl@0
   325
		// Establish pipes on rollback
sl@0
   326
		// Open a pipe for endpoint (Bulk out)		
sl@0
   327
		err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse);
sl@0
   328
		if(err != KErrNone)
sl@0
   329
			{
sl@0
   330
			TBuf<128> msg;
sl@0
   331
			msg.Format(_L("<Error %d> Unable to open pipe on interface 1 setting 1 for rollback case"),err);
sl@0
   332
			RDebug::Print(msg);
sl@0
   333
			iCaseStep = EFailed;
sl@0
   334
			TTestCaseFailed request(err,msg);
sl@0
   335
			iClientAction->SendRequest(request,this);
sl@0
   336
			return;
sl@0
   337
			}
sl@0
   338
		
sl@0
   339
		RDebug::Printf("Pipe established on rollback now closing");
sl@0
   340
		iTestPipe.Close();
sl@0
   341
		}
sl@0
   342
	
sl@0
   343
	// Inform client device test case successful	
sl@0
   344
	iCaseStep = EPassed;
sl@0
   345
	TTestCasePassed request;
sl@0
   346
	iClientAction->SendRequest(request,this);
sl@0
   347
	}
sl@0
   348
	
sl@0
   349
	
sl@0
   350
void CUT_PBASE_T_USBDI_0477::DeviceRemovedL(TUint aDeviceHandle)
sl@0
   351
	{
sl@0
   352
	LOG_FUNC
sl@0
   353
sl@0
   354
	// The test device should not be removed until the test case has passed
sl@0
   355
	// so this test case has not completed, and state this event as an error
sl@0
   356
sl@0
   357
	TestFailed(KErrDisconnected);
sl@0
   358
	}
sl@0
   359
	
sl@0
   360
	
sl@0
   361
void CUT_PBASE_T_USBDI_0477::BusErrorL(TInt aError)
sl@0
   362
	{
sl@0
   363
	LOG_FUNC
sl@0
   364
sl@0
   365
	// This test case handles no failiures on the bus
sl@0
   366
	
sl@0
   367
	TestFailed(aError);
sl@0
   368
	}
sl@0
   369
	
sl@0
   370
	
sl@0
   371
void CUT_PBASE_T_USBDI_0477::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,
sl@0
   372
	RUsbDevice::TDeviceState aNewState,TInt aCompletionCode)
sl@0
   373
	{
sl@0
   374
	LOG_FUNC
sl@0
   375
	Cancel();
sl@0
   376
	}
sl@0
   377
sl@0
   378
void CUT_PBASE_T_USBDI_0477::Ep0TransferCompleteL(TInt aCompletionCode)
sl@0
   379
	{
sl@0
   380
	LOG_FUNC
sl@0
   381
	
sl@0
   382
	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
sl@0
   383
	
sl@0
   384
	if(iCaseStep == EPassed)
sl@0
   385
		{
sl@0
   386
		TestPassed();
sl@0
   387
		}
sl@0
   388
		
sl@0
   389
	if(iCaseStep == EFailed)
sl@0
   390
		{
sl@0
   391
		TestFailed(KErrCompletion);
sl@0
   392
		}
sl@0
   393
	}
sl@0
   394
	
sl@0
   395
	
sl@0
   396
void CUT_PBASE_T_USBDI_0477::HostRunL()
sl@0
   397
	{
sl@0
   398
	LOG_FUNC
sl@0
   399
sl@0
   400
	// Obtain the completion code
sl@0
   401
	TInt completionCode(iStatus.Int());
sl@0
   402
	
sl@0
   403
	if(completionCode == KErrNone)
sl@0
   404
		{
sl@0
   405
		// Action timeout
sl@0
   406
		RDebug::Printf("<Error> Action timeout");
sl@0
   407
		TestFailed(KErrTimedOut);
sl@0
   408
		}
sl@0
   409
	else
sl@0
   410
		{
sl@0
   411
		RDebug::Printf("<Error %d> Timeout timer could not complete",completionCode);
sl@0
   412
		TestFailed(completionCode);
sl@0
   413
		}
sl@0
   414
	}
sl@0
   415
sl@0
   416
void CUT_PBASE_T_USBDI_0477::DeviceRunL()
sl@0
   417
	{
sl@0
   418
	LOG_FUNC
sl@0
   419
	
sl@0
   420
	// Disconnect the device
sl@0
   421
	
sl@0
   422
	iTestDevice->SoftwareDisconnect();
sl@0
   423
	
sl@0
   424
	// Complete the test case request
sl@0
   425
	
sl@0
   426
	TestPolicy().SignalTestComplete(iStatus.Int());
sl@0
   427
	}
sl@0
   428
	
sl@0
   429
	
sl@0
   430
	}
sl@0
   431