os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0495.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) 2008-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-0495.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "PBASE-T_USBDI-0495.h"
sl@0
    20
#include "testpolicy.h"
sl@0
    21
#include "modelleddevices.h"
sl@0
    22
#include "testliterals.h"
sl@0
    23
sl@0
    24
sl@0
    25
 
sl@0
    26
sl@0
    27
namespace NUnitTesting_USBDI
sl@0
    28
	{
sl@0
    29
const TUint KTotalBytesToTransfer = 2*1024*1024+511; //2MB + 511 bytes
sl@0
    30
const TUint KHostNumWriteBytes = 1024*256;
sl@0
    31
const TInt KBulkMaxTransferSize = KHostNumWriteBytes + 1000;
sl@0
    32
const TInt KDeviceNumReadBytes = 1024;
sl@0
    33
sl@0
    34
sl@0
    35
//Make these single bit values ... 
sl@0
    36
// ... so that their completion can be easily recorded in a bit mask!
sl@0
    37
const TUint32 KBulkTransferOutId[KMaxNumOutTransfers] = {1<<0, 1<<1};
sl@0
    38
const TUint32 KBulkTransferIdMask = KBulkTransferOutId[0] | KBulkTransferOutId[1];
sl@0
    39
sl@0
    40
const TInt KUndefinedStep	 		= -102;
sl@0
    41
const TInt KUnexpectedTransferID 	= -103;
sl@0
    42
sl@0
    43
sl@0
    44
sl@0
    45
_LIT(KTestCaseId,"PBASE-T_USBDI-0495");
sl@0
    46
const TFunctorTestCase<CUT_PBASE_T_USBDI_0495,TBool> CUT_PBASE_T_USBDI_0495::iFunctor(KTestCaseId);	
sl@0
    47
sl@0
    48
CUT_PBASE_T_USBDI_0495* CUT_PBASE_T_USBDI_0495::NewL(TBool aHostRole)
sl@0
    49
	{
sl@0
    50
	CUT_PBASE_T_USBDI_0495* self = new (ELeave) CUT_PBASE_T_USBDI_0495(aHostRole);
sl@0
    51
	CleanupStack::PushL(self);
sl@0
    52
	self->ConstructL();
sl@0
    53
	CleanupStack::Pop(self);
sl@0
    54
	return self;
sl@0
    55
	}
sl@0
    56
	
sl@0
    57
sl@0
    58
CUT_PBASE_T_USBDI_0495::CUT_PBASE_T_USBDI_0495(TBool aHostRole)
sl@0
    59
:	CBaseBulkTestCase(KTestCaseId,aHostRole),
sl@0
    60
	iCaseStep(EInProgress),
sl@0
    61
	iRequestValidationResultPtr(NULL,0)
sl@0
    62
	{
sl@0
    63
	} 
sl@0
    64
sl@0
    65
sl@0
    66
void CUT_PBASE_T_USBDI_0495::ConstructL()
sl@0
    67
	{
sl@0
    68
	BaseBulkConstructL();
sl@0
    69
sl@0
    70
	iInBuffer = HBufC8::NewL(KTestBufferLength);
sl@0
    71
sl@0
    72
	//Create buffer to contain sufficient repeats of the payload pattern
sl@0
    73
	//..so that we may grab cyclic chunks of said payload pattern for OUT transfers
sl@0
    74
	TInt repeats = KHostNumWriteBytes / (KLiteralEnglish5().Length()) + 1 + 1; //1 extra to accommodate start point plus 1 to accomodate remainder in division
sl@0
    75
	iOutBuffer = HBufC8::NewL(KLiteralEnglish5().Length() * repeats);
sl@0
    76
	iOutBufferPtr.Set(iOutBuffer->Des());
sl@0
    77
	iOutBufferPtr.Zero();
sl@0
    78
	for(TInt i=0;i<repeats;i++)
sl@0
    79
		{
sl@0
    80
		iOutBufferPtr.Append(KLiteralEnglish5());
sl@0
    81
		}
sl@0
    82
sl@0
    83
	RDebug::Printf("CUT_PBASE_T_USBDI_0495::ConstructL(): buffer created");
sl@0
    84
	}
sl@0
    85
sl@0
    86
sl@0
    87
CUT_PBASE_T_USBDI_0495::~CUT_PBASE_T_USBDI_0495()
sl@0
    88
	{
sl@0
    89
	LOG_FUNC
sl@0
    90
	}
sl@0
    91
	
sl@0
    92
void CUT_PBASE_T_USBDI_0495::KillTransfers()
sl@0
    93
	{
sl@0
    94
	LOG_FUNC
sl@0
    95
	
sl@0
    96
	iOutTransfer[0]->Cancel();
sl@0
    97
	iOutTransfer[1]->Cancel();
sl@0
    98
	}
sl@0
    99
	
sl@0
   100
	
sl@0
   101
	
sl@0
   102
void CUT_PBASE_T_USBDI_0495::Ep0TransferCompleteL(TInt aCompletionCode)
sl@0
   103
	{
sl@0
   104
	LOG_FUNC
sl@0
   105
	
sl@0
   106
	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d", aCompletionCode);
sl@0
   107
	
sl@0
   108
	if(aCompletionCode != KErrNone)
sl@0
   109
		{
sl@0
   110
		if(iCaseStep == EFailed)
sl@0
   111
			{// ignore error, nad catch the TestFailed method called further down.
sl@0
   112
			RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
sl@0
   113
			}
sl@0
   114
		else
sl@0
   115
			{
sl@0
   116
			TBuf<256> msg;
sl@0
   117
			KillTransfers();
sl@0
   118
			_LIT(lit, "<Error %d> Transfer to control endpoint 0 was not successful");
sl@0
   119
			msg.Format(lit,aCompletionCode);
sl@0
   120
			RDebug::Print(msg);
sl@0
   121
			iCaseStep = EFailed;
sl@0
   122
			TTestCaseFailed request(aCompletionCode,msg);
sl@0
   123
			iControlEp0->SendRequest(request,this);
sl@0
   124
			return;
sl@0
   125
			}
sl@0
   126
		}
sl@0
   127
	
sl@0
   128
	switch(iCaseStep)
sl@0
   129
		{
sl@0
   130
		// Test case passed
sl@0
   131
		case EPassed:
sl@0
   132
			TestPassed();
sl@0
   133
			break;
sl@0
   134
		
sl@0
   135
		// Test case failed	
sl@0
   136
		case EFailed:
sl@0
   137
			TestFailed(KErrCompletion);
sl@0
   138
			break;
sl@0
   139
		
sl@0
   140
		case ERequestRepeatedReadAndValidate:
sl@0
   141
			{
sl@0
   142
			RDebug::Printf("Try to perform ALL transfers");
sl@0
   143
	
sl@0
   144
			iCaseStep = ETransfer;	
sl@0
   145
			
sl@0
   146
			iTransferComplete |= PerformNextTransfer(KBulkTransferOutId[0]); //should not validate - just perform necessary transfers
sl@0
   147
			iTransferComplete |= PerformNextTransfer(KBulkTransferOutId[1]); //should not validate - just perform necessary transfers
sl@0
   148
			if((iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
sl@0
   149
				{
sl@0
   150
				_LIT(lit, "TEST FAILURE: No data to send!!");
sl@0
   151
				TBuf<40> msg(lit);
sl@0
   152
				RDebug::Print(msg);
sl@0
   153
				iCaseStep = EFailed;
sl@0
   154
				TTestCaseFailed request(KErrAbort,msg);
sl@0
   155
				return iControlEp0->SendRequest(request,this);
sl@0
   156
				}
sl@0
   157
			}
sl@0
   158
			break;
sl@0
   159
			
sl@0
   160
		case ERequestPrepareEndpointValidationResult:
sl@0
   161
			{
sl@0
   162
			RDebug::Printf("Asking client to prepare the result of its continuous validation");
sl@0
   163
			iCaseStep = ERequestValidationResult;
sl@0
   164
			iRequestValidationResultPtr.Set( iInBuffer->Des());
sl@0
   165
			iRequestValidationResultPtr.Zero(); //reset
sl@0
   166
			iRequestValidationResultPtr.SetLength(KPassFailStringLength);
sl@0
   167
			TInterfaceGetPayloadRequest request(1,iRequestValidationResultPtr);
sl@0
   168
			iControlEp0->SendRequest(request,this);
sl@0
   169
			}
sl@0
   170
			break;
sl@0
   171
	
sl@0
   172
		case ERequestValidationResult:
sl@0
   173
			{
sl@0
   174
			RDebug::Printf("Collect client's return validation  result in a pass or fail string ...");
sl@0
   175
			RDebug::RawPrint(*iInBuffer);
sl@0
   176
			RDebug::Printf("\n");
sl@0
   177
			TPtr8 ptr(iInBuffer->Des());
sl@0
   178
			if(ptr.Compare(KClientPassString) == 0)
sl@0
   179
				{
sl@0
   180
				RDebug::Printf("Client Validation Result is a PASS");
sl@0
   181
				RDebug::Printf("This is the FINAL check - the whole test has a PASSED");
sl@0
   182
				iCaseStep = EPassed;
sl@0
   183
				TTestCasePassed request;
sl@0
   184
				iControlEp0->SendRequest(request,this);
sl@0
   185
				}
sl@0
   186
			else
sl@0
   187
				{
sl@0
   188
				TBuf<256> msg;
sl@0
   189
				_LIT(lit, "<Error> Bulk data VALIDATION check was NOT successful");
sl@0
   190
				msg.Format(lit);
sl@0
   191
				RDebug::Print(msg);
sl@0
   192
				iCaseStep = EFailed;
sl@0
   193
				TTestCaseFailed request(KErrCorrupt,msg);
sl@0
   194
				iControlEp0->SendRequest(request,this);
sl@0
   195
				}
sl@0
   196
			}
sl@0
   197
			break;
sl@0
   198
	
sl@0
   199
		default:
sl@0
   200
			RDebug::Printf("<Error> Unknown test step");
sl@0
   201
			TestFailed(KErrUnknown);
sl@0
   202
			break;
sl@0
   203
		}
sl@0
   204
	}
sl@0
   205
	
sl@0
   206
void CUT_PBASE_T_USBDI_0495::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
sl@0
   207
	{
sl@0
   208
	LOG_FUNC
sl@0
   209
	Cancel();
sl@0
   210
	
sl@0
   211
	TInt err(KErrNone);
sl@0
   212
	TBuf<256> msg;
sl@0
   213
	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
sl@0
   214
sl@0
   215
sl@0
   216
	switch(iCaseStep)
sl@0
   217
		{
sl@0
   218
		case ETransfer:
sl@0
   219
			{
sl@0
   220
			if(aCompletionCode != KErrNone)
sl@0
   221
				{
sl@0
   222
				KillTransfers();
sl@0
   223
				err = KErrCorrupt;
sl@0
   224
				_LIT(lit, "<Error %d> The transfer completed with an error.");
sl@0
   225
				msg.Format(lit, aCompletionCode);
sl@0
   226
				break;
sl@0
   227
				}
sl@0
   228
sl@0
   229
			if(aTransferId != KBulkTransferOutId[0] && aTransferId != KBulkTransferOutId[1])
sl@0
   230
				{
sl@0
   231
				iTransferComplete = 0; //reset
sl@0
   232
				err = KUnexpectedTransferID;
sl@0
   233
				_LIT(lit, "<Error %d> Unexpected transfer ID, wanted %d or %d, got %d");
sl@0
   234
				msg.Format(lit, err, KBulkTransferOutId[0], KBulkTransferOutId[1], aTransferId);
sl@0
   235
				break;
sl@0
   236
				}
sl@0
   237
sl@0
   238
			RDebug::Printf("Transfer OUT %d completed - num bytes sent = %d", aTransferId, iNumWriteBytesRequested);
sl@0
   239
			
sl@0
   240
			iTransferComplete |= PerformNextTransfer(aTransferId);
sl@0
   241
			
sl@0
   242
			if(err==KErrNone && (iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
sl@0
   243
				{
sl@0
   244
				/*
sl@0
   245
				Transfers all complete - now ask device to validate first interface's transfer OUT
sl@0
   246
				*/
sl@0
   247
				RDebug::Printf("All Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
sl@0
   248
				if(err==KErrNone)
sl@0
   249
					{
sl@0
   250
					RDebug::Printf("Asking client to post validation recorded on the endpoint on its interface - ready for collection");
sl@0
   251
					iCaseStep = ERequestPrepareEndpointValidationResult;
sl@0
   252
					TRecordedValidationResultRequest request(1,1);
sl@0
   253
					iControlEp0->SendRequest(request,this);
sl@0
   254
					}
sl@0
   255
				}
sl@0
   256
			}
sl@0
   257
			break;
sl@0
   258
		default:
sl@0
   259
			err = KUndefinedStep;
sl@0
   260
			_LIT(lit, "<Error %d> Undefined case step %d reached");
sl@0
   261
			msg.Format(lit,KUndefinedStep, iCaseStep);
sl@0
   262
			break;
sl@0
   263
		}
sl@0
   264
sl@0
   265
sl@0
   266
	if(err == KErrCompletion)
sl@0
   267
		//indicates data validation failure
sl@0
   268
		{
sl@0
   269
		_LIT(lit, "<Error %d> Client has posted an error discovered in validation");
sl@0
   270
		msg.Format(lit, err);
sl@0
   271
		}
sl@0
   272
sl@0
   273
	if(err!=KErrNone)
sl@0
   274
		{	
sl@0
   275
		KillTransfers();
sl@0
   276
		RDebug::Print(msg);
sl@0
   277
		iCaseStep = EFailed;
sl@0
   278
		TTestCaseFailed request(err,msg);
sl@0
   279
		return iControlEp0->SendRequest(request,this);
sl@0
   280
		}	
sl@0
   281
	}
sl@0
   282
	
sl@0
   283
void CUT_PBASE_T_USBDI_0495::DeviceInsertedL(TUint aDeviceHandle)
sl@0
   284
	{
sl@0
   285
	LOG_FUNC
sl@0
   286
	
sl@0
   287
	Cancel();
sl@0
   288
	RDebug::Printf("this - %08x", this);
sl@0
   289
	
sl@0
   290
	TBuf<256> msg;
sl@0
   291
	TInt err = KErrNone;
sl@0
   292
	if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
sl@0
   293
		// Prepare for response from control transfer to client
sl@0
   294
		{
sl@0
   295
		err = KErrGeneral;
sl@0
   296
		_LIT(lit, "Base class DeviceInsertedL failed");
sl@0
   297
		msg.Format(lit);
sl@0
   298
		}
sl@0
   299
	else
sl@0
   300
		{
sl@0
   301
		if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
sl@0
   302
			// Prepare for response from control transfer to client
sl@0
   303
			{
sl@0
   304
			err = KErrGeneral;
sl@0
   305
			_LIT(lit, "Base class SetUpInterfaceAndPipes for Interface 2 failed");
sl@0
   306
			msg.Format(lit);
sl@0
   307
			}
sl@0
   308
		else
sl@0
   309
			{
sl@0
   310
	
sl@0
   311
			iOutTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferOutId[0]);
sl@0
   312
			iOutTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferOutId[1]);
sl@0
   313
			
sl@0
   314
			// Initialise the descriptors for transfer		
sl@0
   315
			RDebug::Printf("Initialising the transfer descriptors - interface 1");
sl@0
   316
			err = iUsbInterface1.InitialiseTransferDescriptors();
sl@0
   317
			if(err != KErrNone)
sl@0
   318
				{
sl@0
   319
				_LIT(lit, "<Error %d> Unable to initialise transfer descriptors");
sl@0
   320
				msg.Format(lit,err);
sl@0
   321
				}
sl@0
   322
			}
sl@0
   323
		}
sl@0
   324
	if(err != KErrNone)
sl@0
   325
		{
sl@0
   326
		RDebug::Print(msg);
sl@0
   327
		iCaseStep = EFailed;
sl@0
   328
		TTestCaseFailed request(err,msg);
sl@0
   329
		iControlEp0->SendRequest(request,this);
sl@0
   330
		}
sl@0
   331
	else
sl@0
   332
		{
sl@0
   333
		RDebug::Printf("Asking client for continuous 'Read' and 'Validate'");
sl@0
   334
		iCaseStep = ERequestRepeatedReadAndValidate;	
sl@0
   335
		TRepeatedReadAndValidateDataRequest request(1,1,KLiteralEnglish5,KDeviceNumReadBytes,KTotalBytesToTransfer);// EP2 means endpoint index 2 not the actual endpoint number, here the ep with 32 byte max packet size
sl@0
   336
		iControlEp0->SendRequest(request,this);
sl@0
   337
		}
sl@0
   338
	}
sl@0
   339
sl@0
   340
TInt CUT_PBASE_T_USBDI_0495::PerformNextTransfer(TInt aTransferId)
sl@0
   341
/**
sl@0
   342
@param 	aTransferId - specifies trasnfer to use
sl@0
   343
@return	zero UNLESS no more trasnfers have been queued for the specified ID in which case return the transfer ID
sl@0
   344
Note:	This return value allows the caller to respond easily if no more transfers are required for the specified 
sl@0
   345
		transfer ID.
sl@0
   346
*/
sl@0
   347
	{
sl@0
   348
	LOG_FUNC
sl@0
   349
	
sl@0
   350
	if(iNumWriteBytesRequested >= KTotalBytesToTransfer)
sl@0
   351
		{
sl@0
   352
		RDebug::Printf("****ALL DONE for Transfer using ID %d****", aTransferId);
sl@0
   353
		RDebug::Printf("Num bytes actually written = %d, num bytes required to be written = %d", iNumWriteBytesRequested, KTotalBytesToTransfer);
sl@0
   354
		return aTransferId; //Not writing any more - signal to user that no more transfers are required on this transfer ID
sl@0
   355
		}
sl@0
   356
	TUint bytesToWrite = KTotalBytesToTransfer - iNumWriteBytesRequested;
sl@0
   357
	TUint numWriteBytes = bytesToWrite < KHostNumWriteBytes ? bytesToWrite : KHostNumWriteBytes;
sl@0
   358
sl@0
   359
	_LITDBG("PerformNextTransfer: None existant transfer ID requested");
sl@0
   360
	__ASSERT_DEBUG(aTransferId==KBulkTransferOutId[0] || aTransferId==KBulkTransferOutId[1], User::Panic(lit, KErrArgument));
sl@0
   361
	CBulkTransfer& bulkTransfer = aTransferId==KBulkTransferOutId[0]?*iOutTransfer[0]:*iOutTransfer[1];
sl@0
   362
	bulkTransfer.TransferOut(iOutBufferPtr.Mid(iNumWriteBytesRequested%(KLiteralEnglish5().Length()), numWriteBytes), EFalse);
sl@0
   363
	iNumWriteBytesRequested += numWriteBytes;
sl@0
   364
sl@0
   365
	return 0; //Signal to the user that another transfer is queued on the specified transfer ID
sl@0
   366
	}
sl@0
   367
sl@0
   368
sl@0
   369
	} //end namespace