os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0496.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
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-0496.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "PBASE-T_USBDI-0496.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 KHostNumReadBytes = 1024*256;
sl@0
    31
const TInt KBulkMaxTransferSize = KHostNumReadBytes + 1000;
sl@0
    32
const TInt KDeviceNumWriteBytes = 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 KBulkTransferInId0 = 1<<0;
sl@0
    38
const TUint32 KBulkTransferInId1 = 1<<1;
sl@0
    39
const TUint32 KBulkTransferInId[KMaxNumInTransfers] = {KBulkTransferInId0, KBulkTransferInId1};
sl@0
    40
const TUint32 KBulkTransferIdMask = KBulkTransferInId[0] | KBulkTransferInId[1];
sl@0
    41
sl@0
    42
const TInt KUndefinedStep	 		= -102;
sl@0
    43
const TInt KUnexpectedTransferID 	= -103;
sl@0
    44
sl@0
    45
sl@0
    46
_LIT(KTestCaseId,"PBASE-T_USBDI-0496");
sl@0
    47
const TFunctorTestCase<CUT_PBASE_T_USBDI_0496,TBool> CUT_PBASE_T_USBDI_0496::iFunctor(KTestCaseId);	
sl@0
    48
sl@0
    49
CUT_PBASE_T_USBDI_0496* CUT_PBASE_T_USBDI_0496::NewL(TBool aHostRole)
sl@0
    50
	{
sl@0
    51
	CUT_PBASE_T_USBDI_0496* self = new (ELeave) CUT_PBASE_T_USBDI_0496(aHostRole);
sl@0
    52
	CleanupStack::PushL(self);
sl@0
    53
	self->ConstructL();
sl@0
    54
	CleanupStack::Pop(self);
sl@0
    55
	return self;
sl@0
    56
	}
sl@0
    57
	
sl@0
    58
sl@0
    59
CUT_PBASE_T_USBDI_0496::CUT_PBASE_T_USBDI_0496(TBool aHostRole)
sl@0
    60
:	CBaseBulkTestCase(KTestCaseId,aHostRole),
sl@0
    61
	iCaseStep(EInProgress)
sl@0
    62
	{
sl@0
    63
	} 
sl@0
    64
sl@0
    65
sl@0
    66
void CUT_PBASE_T_USBDI_0496::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 two lots of the payload pattern
sl@0
    73
	//..so that we may grab cyclic chunks of said payload pattern
sl@0
    74
	TInt repeats = KHostNumReadBytes / (KLiteralEnglish5().Length()) + 1 + 1; //1 extra to accommodate start point plus 1 to accomodate remainder in division
sl@0
    75
	iValidateBuffer = HBufC8::NewL(KLiteralEnglish5().Length() * repeats);
sl@0
    76
	iValidateBufferPtr.Set(iValidateBuffer->Des());
sl@0
    77
	iValidateBufferPtr.Zero();
sl@0
    78
	for(TInt i=0;i<repeats;i++)
sl@0
    79
		{
sl@0
    80
		iValidateBufferPtr.Append(KLiteralEnglish5());
sl@0
    81
		}
sl@0
    82
	
sl@0
    83
	RDebug::Printf("CUT_PBASE_T_USBDI_0496::ConstructL(): buffer created");
sl@0
    84
	}
sl@0
    85
sl@0
    86
sl@0
    87
CUT_PBASE_T_USBDI_0496::~CUT_PBASE_T_USBDI_0496()
sl@0
    88
	{
sl@0
    89
	LOG_FUNC
sl@0
    90
	}
sl@0
    91
	
sl@0
    92
void CUT_PBASE_T_USBDI_0496::KillTransfers()
sl@0
    93
	{
sl@0
    94
	LOG_FUNC
sl@0
    95
	
sl@0
    96
	iInTransfer[0]->Cancel();
sl@0
    97
	iInTransfer[1]->Cancel();
sl@0
    98
	}
sl@0
    99
	
sl@0
   100
	
sl@0
   101
	
sl@0
   102
void CUT_PBASE_T_USBDI_0496::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
			KillTransfers();
sl@0
   117
			TBuf<256> msg;
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 ERequestRepeatedWrite:
sl@0
   141
			{
sl@0
   142
			RDebug::Printf("Try to perform ALL transfers");
sl@0
   143
			
sl@0
   144
			iCaseStep = ETransfer;	
sl@0
   145
			iIsValid = ETrue; //innocent until proved guilty
sl@0
   146
			RDebug::Printf("\n");
sl@0
   147
			iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[0]); //should not validate - just perform necessary transfers
sl@0
   148
			iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[1]); //should not validate - just perform necessary transfers
sl@0
   149
			RDebug::Printf("\n");
sl@0
   150
			if((iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
sl@0
   151
				{
sl@0
   152
				iTransferComplete = 0; //reset
sl@0
   153
				_LIT(lit, "TEST FAILURE: No data to send!!");
sl@0
   154
				TBuf<40> msg(lit);
sl@0
   155
				RDebug::Print(msg);
sl@0
   156
				iCaseStep = EFailed;
sl@0
   157
				TTestCaseFailed request(KErrAbort,msg);
sl@0
   158
				return iControlEp0->SendRequest(request,this);
sl@0
   159
				}
sl@0
   160
			}
sl@0
   161
			break;
sl@0
   162
sl@0
   163
		default:
sl@0
   164
			RDebug::Printf("<Error> Unknown test step");
sl@0
   165
			TestFailed(KErrUnknown);
sl@0
   166
			break;
sl@0
   167
		}
sl@0
   168
	}
sl@0
   169
	
sl@0
   170
void CUT_PBASE_T_USBDI_0496::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
sl@0
   171
	{
sl@0
   172
	LOG_FUNC
sl@0
   173
	Cancel();
sl@0
   174
	
sl@0
   175
	TInt err(KErrNone);
sl@0
   176
	TBuf<256> msg;
sl@0
   177
	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
sl@0
   178
sl@0
   179
sl@0
   180
	switch(iCaseStep)
sl@0
   181
		{
sl@0
   182
		case ETransfer:
sl@0
   183
			{
sl@0
   184
			if(aCompletionCode != KErrNone)
sl@0
   185
				{
sl@0
   186
				KillTransfers();
sl@0
   187
				err = KErrCorrupt;
sl@0
   188
				_LIT(lit, "<Error %d> The transfer completed with an error.");
sl@0
   189
				msg.Format(lit, aCompletionCode);
sl@0
   190
				break;
sl@0
   191
				}
sl@0
   192
			if(aTransferId != KBulkTransferInId[0] && aTransferId != KBulkTransferInId[1])
sl@0
   193
				{
sl@0
   194
				iTransferComplete = 0; //reset
sl@0
   195
				err = KUnexpectedTransferID;
sl@0
   196
				_LIT(lit, "<Error %d> Unexpected transfer ID, wanted %d or %d, got %d");
sl@0
   197
				msg.Format(lit, err, KBulkTransferInId[0], KBulkTransferInId[1], aTransferId);
sl@0
   198
				break;
sl@0
   199
				}
sl@0
   200
	
sl@0
   201
			RDebug::Printf("\n");
sl@0
   202
			iTransferComplete |= ValidatePreviousAndPerformNextTransfers(aTransferId);
sl@0
   203
			RDebug::Printf("\n");
sl@0
   204
			
sl@0
   205
			if(err==KErrNone && (iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
sl@0
   206
				{
sl@0
   207
				/*
sl@0
   208
				Transfers all complete - check all were valid, and if so pass the test
sl@0
   209
				*/
sl@0
   210
				RDebug::Printf("All Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
sl@0
   211
				if(!iIsValid)
sl@0
   212
					{
sl@0
   213
					err = KErrCompletion; //indicates data validation failure
sl@0
   214
					iIsValid = ETrue; //reset
sl@0
   215
					break;
sl@0
   216
					}
sl@0
   217
				// Comparison is a match
sl@0
   218
				RDebug::Printf("Comparison for IN transfer is a match");
sl@0
   219
				iCaseStep = EPassed;
sl@0
   220
				TTestCasePassed request;
sl@0
   221
				iControlEp0->SendRequest(request,this);
sl@0
   222
				iTransferComplete = 0; //reset
sl@0
   223
				}
sl@0
   224
			}
sl@0
   225
			break;
sl@0
   226
sl@0
   227
		default:
sl@0
   228
			err = KUndefinedStep;
sl@0
   229
			_LIT(lit, "<Error %d> Undefined case step %d reached");
sl@0
   230
			msg.Format(lit,KUndefinedStep, iCaseStep);
sl@0
   231
			break;
sl@0
   232
		}
sl@0
   233
sl@0
   234
sl@0
   235
	if(err == KErrCompletion)
sl@0
   236
		//indicates data validation failure
sl@0
   237
		{
sl@0
   238
		_LIT(lit, "<Error %d> Bulk transfer IN data received does not match the data expected");
sl@0
   239
		msg.Format(lit, err);
sl@0
   240
		}
sl@0
   241
sl@0
   242
	if(err!=KErrNone)
sl@0
   243
		{	
sl@0
   244
		KillTransfers();
sl@0
   245
		RDebug::Print(msg);
sl@0
   246
		iCaseStep = EFailed;
sl@0
   247
		TTestCaseFailed request(err,msg);
sl@0
   248
		return iControlEp0->SendRequest(request,this);
sl@0
   249
		}	
sl@0
   250
	}
sl@0
   251
	
sl@0
   252
void CUT_PBASE_T_USBDI_0496::DeviceInsertedL(TUint aDeviceHandle)
sl@0
   253
	{
sl@0
   254
	LOG_FUNC
sl@0
   255
	
sl@0
   256
	Cancel();
sl@0
   257
	RDebug::Printf("this - %08x", this);
sl@0
   258
	
sl@0
   259
	TBuf<256> msg;
sl@0
   260
	TInt err = KErrNone;
sl@0
   261
	if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
sl@0
   262
		// Prepare for response from control transfer to client
sl@0
   263
		{
sl@0
   264
		err = KErrGeneral;
sl@0
   265
		_LIT(lit, "Base class DeviceInsertedL failed");
sl@0
   266
		msg.Format(lit);
sl@0
   267
		}
sl@0
   268
	else
sl@0
   269
		{
sl@0
   270
		if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
sl@0
   271
			// Prepare for response from control transfer to client
sl@0
   272
			{
sl@0
   273
			err = KErrGeneral;
sl@0
   274
			_LIT(lit, "Base class SetUpInterfaceAndPipes for Interface 2 failed");
sl@0
   275
			msg.Format(lit);
sl@0
   276
			}
sl@0
   277
		else
sl@0
   278
			{
sl@0
   279
	
sl@0
   280
			iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[0]);
sl@0
   281
			iInTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[1]);
sl@0
   282
			
sl@0
   283
			// Initialise the descriptors for transfer		
sl@0
   284
			RDebug::Printf("Initialising the transfer descriptors - interface 1");
sl@0
   285
			err = iUsbInterface1.InitialiseTransferDescriptors();
sl@0
   286
			if(err != KErrNone)
sl@0
   287
				{
sl@0
   288
				_LIT(lit, "<Error %d> Unable to initialise transfer descriptors");
sl@0
   289
				msg.Format(lit,err);
sl@0
   290
				}
sl@0
   291
			}
sl@0
   292
		}
sl@0
   293
	if(err != KErrNone)
sl@0
   294
		{
sl@0
   295
		RDebug::Print(msg);
sl@0
   296
		iCaseStep = EFailed;
sl@0
   297
		TTestCaseFailed request(err,msg);
sl@0
   298
		iControlEp0->SendRequest(request,this);
sl@0
   299
		}
sl@0
   300
	else
sl@0
   301
		{
sl@0
   302
		RDebug::Printf("Asking client for 'Read' on interface 1");
sl@0
   303
		iCaseStep = ERequestRepeatedWrite;	
sl@0
   304
		TRepeatedWriteDataRequest request(1,1,KLiteralEnglish5(),KDeviceNumWriteBytes,KTotalBytesToTransfer);// EP2 means endpoint index 2 not the actual endpoint number, here the ep with 32 byte max packet size
sl@0
   305
		iControlEp0->SendRequest(request,this);
sl@0
   306
		}
sl@0
   307
	}
sl@0
   308
sl@0
   309
TUint8 CUT_PBASE_T_USBDI_0496::Index(TUint8 aTransferId)
sl@0
   310
	{
sl@0
   311
	switch(aTransferId)
sl@0
   312
		{
sl@0
   313
		case KBulkTransferInId0:
sl@0
   314
			return 0;
sl@0
   315
		case KBulkTransferInId1:
sl@0
   316
			return 1;
sl@0
   317
		default:
sl@0
   318
			_LIT(lit, "TRANSFER ID OUT OF RANGE");
sl@0
   319
			User::Panic(lit, KErrArgument);
sl@0
   320
			return 0; //should never get here
sl@0
   321
		}
sl@0
   322
	}
sl@0
   323
sl@0
   324
TInt CUT_PBASE_T_USBDI_0496::ValidatePreviousAndPerformNextTransfers(TInt aTransferId)
sl@0
   325
/**
sl@0
   326
@param 	aTransferId - specifies trasnfer to use
sl@0
   327
@return	zero UNLESS no more trasnfers have been queued for the specified ID in which case return the transfer ID
sl@0
   328
Note:	This return value allows the caller to respond easily if no more transfers are required for the specified 
sl@0
   329
		transfer ID.
sl@0
   330
*/
sl@0
   331
	{
sl@0
   332
	LOG_FUNC
sl@0
   333
	
sl@0
   334
	TUint8 index = Index(aTransferId);
sl@0
   335
	RDebug::Printf("\n");
sl@0
   336
	RDebug::Printf("Transfer[%d]", index);
sl@0
   337
	
sl@0
   338
	
sl@0
   339
	if(iNumBytesExpected[index] != 0)
sl@0
   340
		{
sl@0
   341
		TPtrC8 data1(iInTransfer[index]->DataPolled());
sl@0
   342
		if(ValidateData(data1, iValidateBufferPtr.Mid(iValidationStringStartPointTransfer[index], iNumBytesExpected[index])) == EFalse)
sl@0
   343
			{
sl@0
   344
			RDebug::Printf("=====VALIDATION FAILURE: Point of Validation String Entry %d, Newly Read Bytes %d=====",iValidationStringStartPointTransfer[index], iNumBytesExpected[index]);
sl@0
   345
			iIsValid = EFalse;
sl@0
   346
			}
sl@0
   347
		iNumBytesExpected[index] = 0; //reset
sl@0
   348
		}
sl@0
   349
	if(iNumBytesRequestedSoFar >= KTotalBytesToTransfer)
sl@0
   350
		//if we are near the end the other transfer will mop up remaining bytes...
sl@0
   351
		{
sl@0
   352
		RDebug::Printf("****ALL DONE for Transfer[%d]****", index);
sl@0
   353
		return aTransferId; //tell caller that all transfers are complete for this transfer ID
sl@0
   354
		}
sl@0
   355
sl@0
   356
	iValidationStringStartPointTransfer[index] = iNumBytesRequestedSoFar%(KLiteralEnglish5().Length()); //PRIOR TO THIS TRANSFER
sl@0
   357
	TUint bytesLeftToRead = KTotalBytesToTransfer - iNumBytesRequestedSoFar;
sl@0
   358
	iNumBytesExpected[index] = bytesLeftToRead < KHostNumReadBytes ? bytesLeftToRead : KHostNumReadBytes;
sl@0
   359
	iNumBytesRequestedSoFar += iNumBytesExpected[index];
sl@0
   360
	iInTransfer[index]->TransferIn(KHostNumReadBytes); //rely on ZLP to complete the last 'TransferIn'
sl@0
   361
	iExpectedNextTransferNumber = 1 - iExpectedNextTransferNumber;
sl@0
   362
sl@0
   363
	return 0; //tell caller there are still transfers to complete for the requested transfer ID
sl@0
   364
	}
sl@0
   365
sl@0
   366
	} //end namespace