os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0495.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0495.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,369 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// @file PBASE-T_USBDI-0495.cpp
    1.18 +// @internalComponent
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include "PBASE-T_USBDI-0495.h"
    1.23 +#include "testpolicy.h"
    1.24 +#include "modelleddevices.h"
    1.25 +#include "testliterals.h"
    1.26 +
    1.27 +
    1.28 + 
    1.29 +
    1.30 +namespace NUnitTesting_USBDI
    1.31 +	{
    1.32 +const TUint KTotalBytesToTransfer = 2*1024*1024+511; //2MB + 511 bytes
    1.33 +const TUint KHostNumWriteBytes = 1024*256;
    1.34 +const TInt KBulkMaxTransferSize = KHostNumWriteBytes + 1000;
    1.35 +const TInt KDeviceNumReadBytes = 1024;
    1.36 +
    1.37 +
    1.38 +//Make these single bit values ... 
    1.39 +// ... so that their completion can be easily recorded in a bit mask!
    1.40 +const TUint32 KBulkTransferOutId[KMaxNumOutTransfers] = {1<<0, 1<<1};
    1.41 +const TUint32 KBulkTransferIdMask = KBulkTransferOutId[0] | KBulkTransferOutId[1];
    1.42 +
    1.43 +const TInt KUndefinedStep	 		= -102;
    1.44 +const TInt KUnexpectedTransferID 	= -103;
    1.45 +
    1.46 +
    1.47 +
    1.48 +_LIT(KTestCaseId,"PBASE-T_USBDI-0495");
    1.49 +const TFunctorTestCase<CUT_PBASE_T_USBDI_0495,TBool> CUT_PBASE_T_USBDI_0495::iFunctor(KTestCaseId);	
    1.50 +
    1.51 +CUT_PBASE_T_USBDI_0495* CUT_PBASE_T_USBDI_0495::NewL(TBool aHostRole)
    1.52 +	{
    1.53 +	CUT_PBASE_T_USBDI_0495* self = new (ELeave) CUT_PBASE_T_USBDI_0495(aHostRole);
    1.54 +	CleanupStack::PushL(self);
    1.55 +	self->ConstructL();
    1.56 +	CleanupStack::Pop(self);
    1.57 +	return self;
    1.58 +	}
    1.59 +	
    1.60 +
    1.61 +CUT_PBASE_T_USBDI_0495::CUT_PBASE_T_USBDI_0495(TBool aHostRole)
    1.62 +:	CBaseBulkTestCase(KTestCaseId,aHostRole),
    1.63 +	iCaseStep(EInProgress),
    1.64 +	iRequestValidationResultPtr(NULL,0)
    1.65 +	{
    1.66 +	} 
    1.67 +
    1.68 +
    1.69 +void CUT_PBASE_T_USBDI_0495::ConstructL()
    1.70 +	{
    1.71 +	BaseBulkConstructL();
    1.72 +
    1.73 +	iInBuffer = HBufC8::NewL(KTestBufferLength);
    1.74 +
    1.75 +	//Create buffer to contain sufficient repeats of the payload pattern
    1.76 +	//..so that we may grab cyclic chunks of said payload pattern for OUT transfers
    1.77 +	TInt repeats = KHostNumWriteBytes / (KLiteralEnglish5().Length()) + 1 + 1; //1 extra to accommodate start point plus 1 to accomodate remainder in division
    1.78 +	iOutBuffer = HBufC8::NewL(KLiteralEnglish5().Length() * repeats);
    1.79 +	iOutBufferPtr.Set(iOutBuffer->Des());
    1.80 +	iOutBufferPtr.Zero();
    1.81 +	for(TInt i=0;i<repeats;i++)
    1.82 +		{
    1.83 +		iOutBufferPtr.Append(KLiteralEnglish5());
    1.84 +		}
    1.85 +
    1.86 +	RDebug::Printf("CUT_PBASE_T_USBDI_0495::ConstructL(): buffer created");
    1.87 +	}
    1.88 +
    1.89 +
    1.90 +CUT_PBASE_T_USBDI_0495::~CUT_PBASE_T_USBDI_0495()
    1.91 +	{
    1.92 +	LOG_FUNC
    1.93 +	}
    1.94 +	
    1.95 +void CUT_PBASE_T_USBDI_0495::KillTransfers()
    1.96 +	{
    1.97 +	LOG_FUNC
    1.98 +	
    1.99 +	iOutTransfer[0]->Cancel();
   1.100 +	iOutTransfer[1]->Cancel();
   1.101 +	}
   1.102 +	
   1.103 +	
   1.104 +	
   1.105 +void CUT_PBASE_T_USBDI_0495::Ep0TransferCompleteL(TInt aCompletionCode)
   1.106 +	{
   1.107 +	LOG_FUNC
   1.108 +	
   1.109 +	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d", aCompletionCode);
   1.110 +	
   1.111 +	if(aCompletionCode != KErrNone)
   1.112 +		{
   1.113 +		if(iCaseStep == EFailed)
   1.114 +			{// ignore error, nad catch the TestFailed method called further down.
   1.115 +			RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
   1.116 +			}
   1.117 +		else
   1.118 +			{
   1.119 +			TBuf<256> msg;
   1.120 +			KillTransfers();
   1.121 +			_LIT(lit, "<Error %d> Transfer to control endpoint 0 was not successful");
   1.122 +			msg.Format(lit,aCompletionCode);
   1.123 +			RDebug::Print(msg);
   1.124 +			iCaseStep = EFailed;
   1.125 +			TTestCaseFailed request(aCompletionCode,msg);
   1.126 +			iControlEp0->SendRequest(request,this);
   1.127 +			return;
   1.128 +			}
   1.129 +		}
   1.130 +	
   1.131 +	switch(iCaseStep)
   1.132 +		{
   1.133 +		// Test case passed
   1.134 +		case EPassed:
   1.135 +			TestPassed();
   1.136 +			break;
   1.137 +		
   1.138 +		// Test case failed	
   1.139 +		case EFailed:
   1.140 +			TestFailed(KErrCompletion);
   1.141 +			break;
   1.142 +		
   1.143 +		case ERequestRepeatedReadAndValidate:
   1.144 +			{
   1.145 +			RDebug::Printf("Try to perform ALL transfers");
   1.146 +	
   1.147 +			iCaseStep = ETransfer;	
   1.148 +			
   1.149 +			iTransferComplete |= PerformNextTransfer(KBulkTransferOutId[0]); //should not validate - just perform necessary transfers
   1.150 +			iTransferComplete |= PerformNextTransfer(KBulkTransferOutId[1]); //should not validate - just perform necessary transfers
   1.151 +			if((iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
   1.152 +				{
   1.153 +				_LIT(lit, "TEST FAILURE: No data to send!!");
   1.154 +				TBuf<40> msg(lit);
   1.155 +				RDebug::Print(msg);
   1.156 +				iCaseStep = EFailed;
   1.157 +				TTestCaseFailed request(KErrAbort,msg);
   1.158 +				return iControlEp0->SendRequest(request,this);
   1.159 +				}
   1.160 +			}
   1.161 +			break;
   1.162 +			
   1.163 +		case ERequestPrepareEndpointValidationResult:
   1.164 +			{
   1.165 +			RDebug::Printf("Asking client to prepare the result of its continuous validation");
   1.166 +			iCaseStep = ERequestValidationResult;
   1.167 +			iRequestValidationResultPtr.Set( iInBuffer->Des());
   1.168 +			iRequestValidationResultPtr.Zero(); //reset
   1.169 +			iRequestValidationResultPtr.SetLength(KPassFailStringLength);
   1.170 +			TInterfaceGetPayloadRequest request(1,iRequestValidationResultPtr);
   1.171 +			iControlEp0->SendRequest(request,this);
   1.172 +			}
   1.173 +			break;
   1.174 +	
   1.175 +		case ERequestValidationResult:
   1.176 +			{
   1.177 +			RDebug::Printf("Collect client's return validation  result in a pass or fail string ...");
   1.178 +			RDebug::RawPrint(*iInBuffer);
   1.179 +			RDebug::Printf("\n");
   1.180 +			TPtr8 ptr(iInBuffer->Des());
   1.181 +			if(ptr.Compare(KClientPassString) == 0)
   1.182 +				{
   1.183 +				RDebug::Printf("Client Validation Result is a PASS");
   1.184 +				RDebug::Printf("This is the FINAL check - the whole test has a PASSED");
   1.185 +				iCaseStep = EPassed;
   1.186 +				TTestCasePassed request;
   1.187 +				iControlEp0->SendRequest(request,this);
   1.188 +				}
   1.189 +			else
   1.190 +				{
   1.191 +				TBuf<256> msg;
   1.192 +				_LIT(lit, "<Error> Bulk data VALIDATION check was NOT successful");
   1.193 +				msg.Format(lit);
   1.194 +				RDebug::Print(msg);
   1.195 +				iCaseStep = EFailed;
   1.196 +				TTestCaseFailed request(KErrCorrupt,msg);
   1.197 +				iControlEp0->SendRequest(request,this);
   1.198 +				}
   1.199 +			}
   1.200 +			break;
   1.201 +	
   1.202 +		default:
   1.203 +			RDebug::Printf("<Error> Unknown test step");
   1.204 +			TestFailed(KErrUnknown);
   1.205 +			break;
   1.206 +		}
   1.207 +	}
   1.208 +	
   1.209 +void CUT_PBASE_T_USBDI_0495::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
   1.210 +	{
   1.211 +	LOG_FUNC
   1.212 +	Cancel();
   1.213 +	
   1.214 +	TInt err(KErrNone);
   1.215 +	TBuf<256> msg;
   1.216 +	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
   1.217 +
   1.218 +
   1.219 +	switch(iCaseStep)
   1.220 +		{
   1.221 +		case ETransfer:
   1.222 +			{
   1.223 +			if(aCompletionCode != KErrNone)
   1.224 +				{
   1.225 +				KillTransfers();
   1.226 +				err = KErrCorrupt;
   1.227 +				_LIT(lit, "<Error %d> The transfer completed with an error.");
   1.228 +				msg.Format(lit, aCompletionCode);
   1.229 +				break;
   1.230 +				}
   1.231 +
   1.232 +			if(aTransferId != KBulkTransferOutId[0] && aTransferId != KBulkTransferOutId[1])
   1.233 +				{
   1.234 +				iTransferComplete = 0; //reset
   1.235 +				err = KUnexpectedTransferID;
   1.236 +				_LIT(lit, "<Error %d> Unexpected transfer ID, wanted %d or %d, got %d");
   1.237 +				msg.Format(lit, err, KBulkTransferOutId[0], KBulkTransferOutId[1], aTransferId);
   1.238 +				break;
   1.239 +				}
   1.240 +
   1.241 +			RDebug::Printf("Transfer OUT %d completed - num bytes sent = %d", aTransferId, iNumWriteBytesRequested);
   1.242 +			
   1.243 +			iTransferComplete |= PerformNextTransfer(aTransferId);
   1.244 +			
   1.245 +			if(err==KErrNone && (iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
   1.246 +				{
   1.247 +				/*
   1.248 +				Transfers all complete - now ask device to validate first interface's transfer OUT
   1.249 +				*/
   1.250 +				RDebug::Printf("All Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
   1.251 +				if(err==KErrNone)
   1.252 +					{
   1.253 +					RDebug::Printf("Asking client to post validation recorded on the endpoint on its interface - ready for collection");
   1.254 +					iCaseStep = ERequestPrepareEndpointValidationResult;
   1.255 +					TRecordedValidationResultRequest request(1,1);
   1.256 +					iControlEp0->SendRequest(request,this);
   1.257 +					}
   1.258 +				}
   1.259 +			}
   1.260 +			break;
   1.261 +		default:
   1.262 +			err = KUndefinedStep;
   1.263 +			_LIT(lit, "<Error %d> Undefined case step %d reached");
   1.264 +			msg.Format(lit,KUndefinedStep, iCaseStep);
   1.265 +			break;
   1.266 +		}
   1.267 +
   1.268 +
   1.269 +	if(err == KErrCompletion)
   1.270 +		//indicates data validation failure
   1.271 +		{
   1.272 +		_LIT(lit, "<Error %d> Client has posted an error discovered in validation");
   1.273 +		msg.Format(lit, err);
   1.274 +		}
   1.275 +
   1.276 +	if(err!=KErrNone)
   1.277 +		{	
   1.278 +		KillTransfers();
   1.279 +		RDebug::Print(msg);
   1.280 +		iCaseStep = EFailed;
   1.281 +		TTestCaseFailed request(err,msg);
   1.282 +		return iControlEp0->SendRequest(request,this);
   1.283 +		}	
   1.284 +	}
   1.285 +	
   1.286 +void CUT_PBASE_T_USBDI_0495::DeviceInsertedL(TUint aDeviceHandle)
   1.287 +	{
   1.288 +	LOG_FUNC
   1.289 +	
   1.290 +	Cancel();
   1.291 +	RDebug::Printf("this - %08x", this);
   1.292 +	
   1.293 +	TBuf<256> msg;
   1.294 +	TInt err = KErrNone;
   1.295 +	if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
   1.296 +		// Prepare for response from control transfer to client
   1.297 +		{
   1.298 +		err = KErrGeneral;
   1.299 +		_LIT(lit, "Base class DeviceInsertedL failed");
   1.300 +		msg.Format(lit);
   1.301 +		}
   1.302 +	else
   1.303 +		{
   1.304 +		if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
   1.305 +			// Prepare for response from control transfer to client
   1.306 +			{
   1.307 +			err = KErrGeneral;
   1.308 +			_LIT(lit, "Base class SetUpInterfaceAndPipes for Interface 2 failed");
   1.309 +			msg.Format(lit);
   1.310 +			}
   1.311 +		else
   1.312 +			{
   1.313 +	
   1.314 +			iOutTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferOutId[0]);
   1.315 +			iOutTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferOutId[1]);
   1.316 +			
   1.317 +			// Initialise the descriptors for transfer		
   1.318 +			RDebug::Printf("Initialising the transfer descriptors - interface 1");
   1.319 +			err = iUsbInterface1.InitialiseTransferDescriptors();
   1.320 +			if(err != KErrNone)
   1.321 +				{
   1.322 +				_LIT(lit, "<Error %d> Unable to initialise transfer descriptors");
   1.323 +				msg.Format(lit,err);
   1.324 +				}
   1.325 +			}
   1.326 +		}
   1.327 +	if(err != KErrNone)
   1.328 +		{
   1.329 +		RDebug::Print(msg);
   1.330 +		iCaseStep = EFailed;
   1.331 +		TTestCaseFailed request(err,msg);
   1.332 +		iControlEp0->SendRequest(request,this);
   1.333 +		}
   1.334 +	else
   1.335 +		{
   1.336 +		RDebug::Printf("Asking client for continuous 'Read' and 'Validate'");
   1.337 +		iCaseStep = ERequestRepeatedReadAndValidate;	
   1.338 +		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
   1.339 +		iControlEp0->SendRequest(request,this);
   1.340 +		}
   1.341 +	}
   1.342 +
   1.343 +TInt CUT_PBASE_T_USBDI_0495::PerformNextTransfer(TInt aTransferId)
   1.344 +/**
   1.345 +@param 	aTransferId - specifies trasnfer to use
   1.346 +@return	zero UNLESS no more trasnfers have been queued for the specified ID in which case return the transfer ID
   1.347 +Note:	This return value allows the caller to respond easily if no more transfers are required for the specified 
   1.348 +		transfer ID.
   1.349 +*/
   1.350 +	{
   1.351 +	LOG_FUNC
   1.352 +	
   1.353 +	if(iNumWriteBytesRequested >= KTotalBytesToTransfer)
   1.354 +		{
   1.355 +		RDebug::Printf("****ALL DONE for Transfer using ID %d****", aTransferId);
   1.356 +		RDebug::Printf("Num bytes actually written = %d, num bytes required to be written = %d", iNumWriteBytesRequested, KTotalBytesToTransfer);
   1.357 +		return aTransferId; //Not writing any more - signal to user that no more transfers are required on this transfer ID
   1.358 +		}
   1.359 +	TUint bytesToWrite = KTotalBytesToTransfer - iNumWriteBytesRequested;
   1.360 +	TUint numWriteBytes = bytesToWrite < KHostNumWriteBytes ? bytesToWrite : KHostNumWriteBytes;
   1.361 +
   1.362 +	_LITDBG("PerformNextTransfer: None existant transfer ID requested");
   1.363 +	__ASSERT_DEBUG(aTransferId==KBulkTransferOutId[0] || aTransferId==KBulkTransferOutId[1], User::Panic(lit, KErrArgument));
   1.364 +	CBulkTransfer& bulkTransfer = aTransferId==KBulkTransferOutId[0]?*iOutTransfer[0]:*iOutTransfer[1];
   1.365 +	bulkTransfer.TransferOut(iOutBufferPtr.Mid(iNumWriteBytesRequested%(KLiteralEnglish5().Length()), numWriteBytes), EFalse);
   1.366 +	iNumWriteBytesRequested += numWriteBytes;
   1.367 +
   1.368 +	return 0; //Signal to the user that another transfer is queued on the specified transfer ID
   1.369 +	}
   1.370 +
   1.371 +
   1.372 +	} //end namespace