os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0486.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-0486.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "PBASE-T_USBDI-0486.h"
sl@0
    20
#include "testpolicy.h"
sl@0
    21
#include "testdebug.h"
sl@0
    22
#include "modelleddevices.h"
sl@0
    23
sl@0
    24
namespace NUnitTesting_USBDI
sl@0
    25
	{
sl@0
    26
	
sl@0
    27
_LIT(KTestCaseId,"PBASE-T_USBDI-0486");
sl@0
    28
const TFunctorTestCase<CUT_PBASE_T_USBDI_0486,TBool> CUT_PBASE_T_USBDI_0486::iFunctor(KTestCaseId);	
sl@0
    29
const TInt KBulkTranferId = 2;
sl@0
    30
const TInt KExpectedDataSize = 26;
sl@0
    31
sl@0
    32
CUT_PBASE_T_USBDI_0486* CUT_PBASE_T_USBDI_0486::NewL(TBool aHostRole)
sl@0
    33
	{
sl@0
    34
	CUT_PBASE_T_USBDI_0486* self = new (ELeave) CUT_PBASE_T_USBDI_0486(aHostRole);
sl@0
    35
	CleanupStack::PushL(self);
sl@0
    36
	self->ConstructL();
sl@0
    37
	CleanupStack::Pop(self);
sl@0
    38
	return self;  
sl@0
    39
	} 
sl@0
    40
	
sl@0
    41
sl@0
    42
CUT_PBASE_T_USBDI_0486::CUT_PBASE_T_USBDI_0486(TBool aHostRole)
sl@0
    43
:	CBaseBulkTestCase(KTestCaseId,aHostRole),
sl@0
    44
	iCaseStep(EWaitForDeviceCConnection)
sl@0
    45
	{
sl@0
    46
	} 
sl@0
    47
sl@0
    48
sl@0
    49
void CUT_PBASE_T_USBDI_0486::ConstructL()
sl@0
    50
	{
sl@0
    51
	BaseBulkConstructL();
sl@0
    52
	iTestDeviceC = new RUsbDeviceC(this);  // TODO check tree for device C, once inserted
sl@0
    53
	}
sl@0
    54
sl@0
    55
CUT_PBASE_T_USBDI_0486::~CUT_PBASE_T_USBDI_0486()
sl@0
    56
	{
sl@0
    57
	LOG_FUNC
sl@0
    58
	
sl@0
    59
	delete iTestDeviceC;
sl@0
    60
	}
sl@0
    61
	
sl@0
    62
/**
sl@0
    63
Called when the device has reported any kind of error in its operation
sl@0
    64
or when the device has been informed by the host to report success
sl@0
    65
*/
sl@0
    66
void CUT_PBASE_T_USBDI_0486::DeviceRunL()
sl@0
    67
	{
sl@0
    68
	LOG_FUNC	
sl@0
    69
	// Complete the test case request	
sl@0
    70
	TestPolicy().SignalTestComplete(iStatus.Int());
sl@0
    71
	}
sl@0
    72
sl@0
    73
void CUT_PBASE_T_USBDI_0486::DeviceDoCancel()
sl@0
    74
	{
sl@0
    75
	LOG_FUNC
sl@0
    76
	iTestDeviceC->CancelSubscriptionToReports();
sl@0
    77
	}
sl@0
    78
	
sl@0
    79
TBool CUT_PBASE_T_USBDI_0486::CheckSN(const TDesC16& aSerialNumberGot, const TDesC& aExpectedSerialNumber)
sl@0
    80
	{
sl@0
    81
	TBool areSNsIdenticals = (aSerialNumberGot.Compare(aExpectedSerialNumber) == 0);	
sl@0
    82
	 
sl@0
    83
	if(!areSNsIdenticals)
sl@0
    84
		{
sl@0
    85
		// Incorrect device for this test case	
sl@0
    86
		RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
sl@0
    87
			KErrNotFound,&aSerialNumberGot, &aExpectedSerialNumber);
sl@0
    88
		}
sl@0
    89
	return areSNsIdenticals;
sl@0
    90
	}
sl@0
    91
	
sl@0
    92
	
sl@0
    93
void CUT_PBASE_T_USBDI_0486::Ep0TransferCompleteL(TInt aCompletionCode)
sl@0
    94
	{
sl@0
    95
	LOG_FUNC
sl@0
    96
sl@0
    97
	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
sl@0
    98
	RDebug::Printf("--->Ep0TransferCompleteL, case step = %d", iCaseStep);
sl@0
    99
sl@0
   100
	switch(iCaseStep)
sl@0
   101
		{
sl@0
   102
		case EDeviceCConnected:
sl@0
   103
			{
sl@0
   104
			if(aCompletionCode != KErrNone)
sl@0
   105
				{
sl@0
   106
				RDebug::Printf("<Error %d> aCompletionCode != KErrNone",aCompletionCode);
sl@0
   107
				return TestFailed(aCompletionCode);
sl@0
   108
				}
sl@0
   109
			// close interface 0			
sl@0
   110
			iUsbInterface0.Close();			
sl@0
   111
			iCaseStep = EWaitForDeviceCDisconnection;
sl@0
   112
			}
sl@0
   113
			break; 
sl@0
   114
			
sl@0
   115
		case EPassed:
sl@0
   116
			TestPassed();
sl@0
   117
			break;		
sl@0
   118
	
sl@0
   119
		default:
sl@0
   120
			TestFailed(KErrCompletion);
sl@0
   121
			break;
sl@0
   122
		}
sl@0
   123
	}
sl@0
   124
	
sl@0
   125
void CUT_PBASE_T_USBDI_0486::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
sl@0
   126
	{
sl@0
   127
	LOG_FUNC
sl@0
   128
	Cancel();
sl@0
   129
	RDebug::Printf("--->TransferCompleteL, case step = %d", iCaseStep);
sl@0
   130
sl@0
   131
	if(aTransferId == KBulkTranferId)
sl@0
   132
		{							
sl@0
   133
		if(aCompletionCode != KErrCancel && aCompletionCode != KErrUsbIOError)
sl@0
   134
			{
sl@0
   135
			TBuf<256> msg;
sl@0
   136
			msg.Format(_L("<Error %d> The transfer completed with no errors but should have done so"),aCompletionCode);
sl@0
   137
			RDebug::Print(msg);
sl@0
   138
			TTestCaseFailed request(KErrCorrupt,msg);
sl@0
   139
			return iControlEp0->SendRequest(request,this);
sl@0
   140
			} 					
sl@0
   141
		}
sl@0
   142
	else
sl@0
   143
		{
sl@0
   144
		RDebug::Printf("<Error> a transfer completed (id=%d) that was not expected",aTransferId);
sl@0
   145
		return TestFailed(KErrCorrupt);
sl@0
   146
		}	
sl@0
   147
	}	
sl@0
   148
	
sl@0
   149
	
sl@0
   150
void CUT_PBASE_T_USBDI_0486::DeviceInsertedL(TUint aDeviceHandle)
sl@0
   151
	{
sl@0
   152
	LOG_FUNC
sl@0
   153
	RDebug::Printf("--->DeviceInsertedL, case step = %d", iCaseStep);
sl@0
   154
sl@0
   155
	// Cancel the timeout timer	
sl@0
   156
	Cancel();	
sl@0
   157
	TInt err(KErrNone);
sl@0
   158
	
sl@0
   159
	// Validate that device is as expected	
sl@0
   160
	CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
sl@0
   161
sl@0
   162
	// Perform the correct test step		
sl@0
   163
	switch(iCaseStep)
sl@0
   164
		{
sl@0
   165
		case EWaitForDeviceCConnection:
sl@0
   166
			{
sl@0
   167
			if(!CheckSN(testDevice.SerialNumber(), KTestDeviceC_SN()))
sl@0
   168
				{
sl@0
   169
				return TestFailed(KErrNotFound);
sl@0
   170
				}
sl@0
   171
			iCaseStep = EDeviceCConnected;
sl@0
   172
			
sl@0
   173
			err = testDevice.Device().GetTokenForInterface(0,iToken0DeviceC);
sl@0
   174
			if(err != KErrNone)
sl@0
   175
				{
sl@0
   176
				RDebug::Printf("<Error %d> Unable to retrieve token for interface 0",err);
sl@0
   177
				TestFailed(err);
sl@0
   178
				} 
sl@0
   179
			// Open the interface	
sl@0
   180
			err = iUsbInterface0.Open(iToken0DeviceC);
sl@0
   181
			if(err != KErrNone)
sl@0
   182
				{
sl@0
   183
				RDebug::Printf("<Error %d> Unable to open interface 0");
sl@0
   184
				TestFailed(err);
sl@0
   185
				}
sl@0
   186
				
sl@0
   187
			TUint32 token1;
sl@0
   188
			err = testDevice.Device().GetTokenForInterface(1,token1);
sl@0
   189
			if(err != KErrNone)
sl@0
   190
				{
sl@0
   191
				TBuf<256> msg;
sl@0
   192
				msg.Format(_L("<Error %d> Token for interface 1 could not be retrieved"),err);
sl@0
   193
				RDebug::Print(msg);
sl@0
   194
				iCaseStep = EFailed;
sl@0
   195
				TTestCaseFailed request(err,msg);
sl@0
   196
				return iControlEp0->SendRequest(request,this);
sl@0
   197
				}
sl@0
   198
				
sl@0
   199
				
sl@0
   200
			err = iUsbInterface1.Open(token1); // Default interface setting 0
sl@0
   201
			if(err != KErrNone)
sl@0
   202
				{
sl@0
   203
				TBuf<256> msg;
sl@0
   204
				msg.Format(_L("<Error %d> Unable to open interface 1 using token %d"),err,token1);
sl@0
   205
				RDebug::Print(msg);
sl@0
   206
				iCaseStep = EFailed;
sl@0
   207
				TTestCaseFailed request(err,msg);
sl@0
   208
				return iControlEp0->SendRequest(request,this);
sl@0
   209
				}
sl@0
   210
				
sl@0
   211
			TInt endpointAddress;
sl@0
   212
			// Queue a bulk in transfer on endpoint 2	
sl@0
   213
			err = GetEndpointAddress(iUsbInterface1,0,KTransferTypeBulk,KEpDirectionIn,endpointAddress);
sl@0
   214
			if(err != KErrNone)
sl@0
   215
				{
sl@0
   216
				TBuf<256> msg;
sl@0
   217
				msg.Format(_L("<Error %d> Address for bulk in endpoint could not be obtained"),err);
sl@0
   218
				RDebug::Print(msg);
sl@0
   219
				iCaseStep = EFailed;
sl@0
   220
				TTestCaseFailed request(err,msg);
sl@0
   221
				return iControlEp0->SendRequest(request,this);
sl@0
   222
				}
sl@0
   223
			RDebug::Printf("Endpoint adress %08x",endpointAddress);
sl@0
   224
	
sl@0
   225
			err = iUsbInterface1.OpenPipeForEndpoint(iTestPipeInterface1BulkIn,endpointAddress,ETrue);
sl@0
   226
			if(err != KErrNone)
sl@0
   227
				{
sl@0
   228
				TBuf<256> msg;
sl@0
   229
				msg.Format(_L("<Error %d> Unable to open pipe for endpoint %08x"),err,endpointAddress);
sl@0
   230
				RDebug::Print(msg);
sl@0
   231
				iCaseStep = EFailed;
sl@0
   232
				TTestCaseFailed request(err,msg);
sl@0
   233
				return iControlEp0->SendRequest(request,this);
sl@0
   234
				}
sl@0
   235
			
sl@0
   236
			// Create the bulk transfers	
sl@0
   237
			iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,256,*this, KBulkTranferId);
sl@0
   238
		
sl@0
   239
			// Initialise the descriptors for transfer		
sl@0
   240
			RDebug::Printf("Initialising the transfer descriptors");
sl@0
   241
			err = iUsbInterface1.InitialiseTransferDescriptors();
sl@0
   242
			if(err != KErrNone)
sl@0
   243
				{
sl@0
   244
				TBuf<256> msg;
sl@0
   245
				msg.Format(_L("<Error %d> Unable to initialise transfer descriptors"),err);
sl@0
   246
				RDebug::Print(msg);
sl@0
   247
				iCaseStep = EFailed;
sl@0
   248
				TTestCaseFailed request(err,msg);
sl@0
   249
				return iControlEp0->SendRequest(request,this);
sl@0
   250
				}				
sl@0
   251
			iInTransfer[0]->TransferIn(KExpectedDataSize);
sl@0
   252
					
sl@0
   253
			// 	disconnect device C and connect device A								
sl@0
   254
			TDisconnectDeviceCThenConnectDeviceARequest request;
sl@0
   255
			iControlEp0->SendRequest(request,this);	
sl@0
   256
	        										
sl@0
   257
			// Monitor for the reconnection from the client		
sl@0
   258
			iActorFDF->Monitor();
sl@0
   259
			TimeoutIn(30);
sl@0
   260
						
sl@0
   261
			}
sl@0
   262
			break; 
sl@0
   263
					
sl@0
   264
		case EWaitForDeviceAConnection:
sl@0
   265
			{
sl@0
   266
			if(!CheckSN(testDevice.SerialNumber(), TestCaseId()))
sl@0
   267
				{
sl@0
   268
				return TestFailed(KErrNotFound);
sl@0
   269
			    }
sl@0
   270
			// try to open interface 0			
sl@0
   271
			err = iUsbInterface0.Open(iToken0DeviceC);
sl@0
   272
			if(err != KErrNotFound) // invalid token
sl@0
   273
				{
sl@0
   274
				RDebug::Printf("iUsbInterface0.Open(iToken0DeviceC) === %d", err);
sl@0
   275
				TestFailed(err);
sl@0
   276
				}
sl@0
   277
			
sl@0
   278
			// get token now 
sl@0
   279
			TUint32 token0DeviceA; 
sl@0
   280
			err = testDevice.Device().GetTokenForInterface(0,token0DeviceA);
sl@0
   281
			if(err != KErrNone)
sl@0
   282
				{
sl@0
   283
				RDebug::Printf("<Error %d> Unable to retrieve token(device A) for interface 0",err);
sl@0
   284
				TestFailed(err);
sl@0
   285
				} 
sl@0
   286
			// Open the interface	
sl@0
   287
			err = iUsbInterface0.Open(token0DeviceA);
sl@0
   288
			if(err != KErrNone)
sl@0
   289
				{
sl@0
   290
				RDebug::Printf("<Error %d> Unable to open interface 0(device A)");
sl@0
   291
				TestFailed(err);
sl@0
   292
				}
sl@0
   293
			
sl@0
   294
			// ok, send EPassed request			
sl@0
   295
			iCaseStep = EPassed;
sl@0
   296
			// Send test case passed request to client
sl@0
   297
			User::After(2000000);	
sl@0
   298
			TTestCasePassed request;
sl@0
   299
			iControlEp0->SendRequest(request,this);			
sl@0
   300
			} 
sl@0
   301
			break;
sl@0
   302
			
sl@0
   303
		default:
sl@0
   304
			{
sl@0
   305
			RDebug::Printf("<Error> Test case actions out of sync");
sl@0
   306
			TestFailed(KErrCorrupt);
sl@0
   307
			}	
sl@0
   308
			break;
sl@0
   309
		}	
sl@0
   310
	}
sl@0
   311
	
sl@0
   312
void CUT_PBASE_T_USBDI_0486::DeviceRemovedL(TUint aDeviceHandle)
sl@0
   313
	{
sl@0
   314
	LOG_FUNC
sl@0
   315
	Cancel();	
sl@0
   316
	RDebug::Printf("--->DeviceRemovedL, case step = %d", iCaseStep);
sl@0
   317
sl@0
   318
	switch(iCaseStep)
sl@0
   319
		{
sl@0
   320
		case EWaitForDeviceCDisconnection: // device C is disconnected now, interface 0 has been closed before
sl@0
   321
			{
sl@0
   322
			iCaseStep = EWaitForDeviceAConnection;
sl@0
   323
			iActorFDF->Monitor();
sl@0
   324
			}
sl@0
   325
			break;		
sl@0
   326
	
sl@0
   327
		default:
sl@0
   328
			RDebug::Printf("<Error> Test case actions out of sync");
sl@0
   329
			TestFailed(KErrCorrupt);
sl@0
   330
			break;
sl@0
   331
			}
sl@0
   332
	}
sl@0
   333
	
sl@0
   334
void CUT_PBASE_T_USBDI_0486::HandleDeviceDConnection()
sl@0
   335
	{
sl@0
   336
	LOG_FUNC
sl@0
   337
		
sl@0
   338
	iTestDevice->SubscribeToReports(iStatus);
sl@0
   339
	SetActive();
sl@0
   340
	}
sl@0
   341
sl@0
   342
void CUT_PBASE_T_USBDI_0486::ExecuteDeviceTestCaseL()	
sl@0
   343
	{ 
sl@0
   344
	LOG_FUNC
sl@0
   345
	
sl@0
   346
	// Create the test device	
sl@0
   347
	iTestDeviceC->OpenL(KTestDeviceC_SN());
sl@0
   348
	iTestDeviceC->SubscribeToReports(iStatus);
sl@0
   349
	SetActive();
sl@0
   350
	
sl@0
   351
	// Connect the device to the host	
sl@0
   352
	iTestDeviceC->SoftwareConnect();
sl@0
   353
	}
sl@0
   354
sl@0
   355
RUsbDeviceC* CUT_PBASE_T_USBDI_0486::TestDeviceC()
sl@0
   356
	{
sl@0
   357
	return iTestDeviceC;
sl@0
   358
	}
sl@0
   359
sl@0
   360
RUsbDeviceD* CUT_PBASE_T_USBDI_0486::TestDeviceD()
sl@0
   361
	{
sl@0
   362
	return iTestDevice; //from CBaseBulkTestCase
sl@0
   363
	}
sl@0
   364
	
sl@0
   365
	}