os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0496.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // @file PBASE-T_USBDI-0496.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-0496.h"
    20 #include "testpolicy.h"
    21 #include "modelleddevices.h"
    22 #include "testliterals.h"
    23 
    24 
    25  
    26 
    27 namespace NUnitTesting_USBDI
    28 	{
    29 const TUint KTotalBytesToTransfer = 2*1024*1024+511; //2MB + 511 bytes
    30 const TUint KHostNumReadBytes = 1024*256;
    31 const TInt KBulkMaxTransferSize = KHostNumReadBytes + 1000;
    32 const TInt KDeviceNumWriteBytes = 1024;
    33 
    34 
    35 //Make these single bit values ... 
    36 // ... so that their completion can be easily recorded in a bit mask!
    37 const TUint32 KBulkTransferInId0 = 1<<0;
    38 const TUint32 KBulkTransferInId1 = 1<<1;
    39 const TUint32 KBulkTransferInId[KMaxNumInTransfers] = {KBulkTransferInId0, KBulkTransferInId1};
    40 const TUint32 KBulkTransferIdMask = KBulkTransferInId[0] | KBulkTransferInId[1];
    41 
    42 const TInt KUndefinedStep	 		= -102;
    43 const TInt KUnexpectedTransferID 	= -103;
    44 
    45 
    46 _LIT(KTestCaseId,"PBASE-T_USBDI-0496");
    47 const TFunctorTestCase<CUT_PBASE_T_USBDI_0496,TBool> CUT_PBASE_T_USBDI_0496::iFunctor(KTestCaseId);	
    48 
    49 CUT_PBASE_T_USBDI_0496* CUT_PBASE_T_USBDI_0496::NewL(TBool aHostRole)
    50 	{
    51 	CUT_PBASE_T_USBDI_0496* self = new (ELeave) CUT_PBASE_T_USBDI_0496(aHostRole);
    52 	CleanupStack::PushL(self);
    53 	self->ConstructL();
    54 	CleanupStack::Pop(self);
    55 	return self;
    56 	}
    57 	
    58 
    59 CUT_PBASE_T_USBDI_0496::CUT_PBASE_T_USBDI_0496(TBool aHostRole)
    60 :	CBaseBulkTestCase(KTestCaseId,aHostRole),
    61 	iCaseStep(EInProgress)
    62 	{
    63 	} 
    64 
    65 
    66 void CUT_PBASE_T_USBDI_0496::ConstructL()
    67 	{
    68 	BaseBulkConstructL();
    69 
    70 	iInBuffer = HBufC8::NewL(KTestBufferLength);
    71 
    72 	//Create buffer to contain two lots of the payload pattern
    73 	//..so that we may grab cyclic chunks of said payload pattern
    74 	TInt repeats = KHostNumReadBytes / (KLiteralEnglish5().Length()) + 1 + 1; //1 extra to accommodate start point plus 1 to accomodate remainder in division
    75 	iValidateBuffer = HBufC8::NewL(KLiteralEnglish5().Length() * repeats);
    76 	iValidateBufferPtr.Set(iValidateBuffer->Des());
    77 	iValidateBufferPtr.Zero();
    78 	for(TInt i=0;i<repeats;i++)
    79 		{
    80 		iValidateBufferPtr.Append(KLiteralEnglish5());
    81 		}
    82 	
    83 	RDebug::Printf("CUT_PBASE_T_USBDI_0496::ConstructL(): buffer created");
    84 	}
    85 
    86 
    87 CUT_PBASE_T_USBDI_0496::~CUT_PBASE_T_USBDI_0496()
    88 	{
    89 	LOG_FUNC
    90 	}
    91 	
    92 void CUT_PBASE_T_USBDI_0496::KillTransfers()
    93 	{
    94 	LOG_FUNC
    95 	
    96 	iInTransfer[0]->Cancel();
    97 	iInTransfer[1]->Cancel();
    98 	}
    99 	
   100 	
   101 	
   102 void CUT_PBASE_T_USBDI_0496::Ep0TransferCompleteL(TInt aCompletionCode)
   103 	{
   104 	LOG_FUNC
   105 	
   106 	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d", aCompletionCode);
   107 	
   108 	if(aCompletionCode != KErrNone)
   109 		{
   110 		if(iCaseStep == EFailed)
   111 			{// ignore error, nad catch the TestFailed method called further down.
   112 			RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
   113 			}
   114 		else
   115 			{
   116 			KillTransfers();
   117 			TBuf<256> msg;
   118 			_LIT(lit, "<Error %d> Transfer to control endpoint 0 was not successful");
   119 			msg.Format(lit,aCompletionCode);
   120 			RDebug::Print(msg);
   121 			iCaseStep = EFailed;
   122 			TTestCaseFailed request(aCompletionCode,msg);
   123 			iControlEp0->SendRequest(request,this);
   124 			return;
   125 			}
   126 		}
   127 	
   128 	switch(iCaseStep)
   129 		{
   130 		// Test case passed
   131 		case EPassed:
   132 			TestPassed();
   133 			break;
   134 		
   135 		// Test case failed	
   136 		case EFailed:
   137 			TestFailed(KErrCompletion);
   138 			break;
   139 		
   140 		case ERequestRepeatedWrite:
   141 			{
   142 			RDebug::Printf("Try to perform ALL transfers");
   143 			
   144 			iCaseStep = ETransfer;	
   145 			iIsValid = ETrue; //innocent until proved guilty
   146 			RDebug::Printf("\n");
   147 			iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[0]); //should not validate - just perform necessary transfers
   148 			iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[1]); //should not validate - just perform necessary transfers
   149 			RDebug::Printf("\n");
   150 			if((iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
   151 				{
   152 				iTransferComplete = 0; //reset
   153 				_LIT(lit, "TEST FAILURE: No data to send!!");
   154 				TBuf<40> msg(lit);
   155 				RDebug::Print(msg);
   156 				iCaseStep = EFailed;
   157 				TTestCaseFailed request(KErrAbort,msg);
   158 				return iControlEp0->SendRequest(request,this);
   159 				}
   160 			}
   161 			break;
   162 
   163 		default:
   164 			RDebug::Printf("<Error> Unknown test step");
   165 			TestFailed(KErrUnknown);
   166 			break;
   167 		}
   168 	}
   169 	
   170 void CUT_PBASE_T_USBDI_0496::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
   171 	{
   172 	LOG_FUNC
   173 	Cancel();
   174 	
   175 	TInt err(KErrNone);
   176 	TBuf<256> msg;
   177 	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
   178 
   179 
   180 	switch(iCaseStep)
   181 		{
   182 		case ETransfer:
   183 			{
   184 			if(aCompletionCode != KErrNone)
   185 				{
   186 				KillTransfers();
   187 				err = KErrCorrupt;
   188 				_LIT(lit, "<Error %d> The transfer completed with an error.");
   189 				msg.Format(lit, aCompletionCode);
   190 				break;
   191 				}
   192 			if(aTransferId != KBulkTransferInId[0] && aTransferId != KBulkTransferInId[1])
   193 				{
   194 				iTransferComplete = 0; //reset
   195 				err = KUnexpectedTransferID;
   196 				_LIT(lit, "<Error %d> Unexpected transfer ID, wanted %d or %d, got %d");
   197 				msg.Format(lit, err, KBulkTransferInId[0], KBulkTransferInId[1], aTransferId);
   198 				break;
   199 				}
   200 	
   201 			RDebug::Printf("\n");
   202 			iTransferComplete |= ValidatePreviousAndPerformNextTransfers(aTransferId);
   203 			RDebug::Printf("\n");
   204 			
   205 			if(err==KErrNone && (iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
   206 				{
   207 				/*
   208 				Transfers all complete - check all were valid, and if so pass the test
   209 				*/
   210 				RDebug::Printf("All Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
   211 				if(!iIsValid)
   212 					{
   213 					err = KErrCompletion; //indicates data validation failure
   214 					iIsValid = ETrue; //reset
   215 					break;
   216 					}
   217 				// Comparison is a match
   218 				RDebug::Printf("Comparison for IN transfer is a match");
   219 				iCaseStep = EPassed;
   220 				TTestCasePassed request;
   221 				iControlEp0->SendRequest(request,this);
   222 				iTransferComplete = 0; //reset
   223 				}
   224 			}
   225 			break;
   226 
   227 		default:
   228 			err = KUndefinedStep;
   229 			_LIT(lit, "<Error %d> Undefined case step %d reached");
   230 			msg.Format(lit,KUndefinedStep, iCaseStep);
   231 			break;
   232 		}
   233 
   234 
   235 	if(err == KErrCompletion)
   236 		//indicates data validation failure
   237 		{
   238 		_LIT(lit, "<Error %d> Bulk transfer IN data received does not match the data expected");
   239 		msg.Format(lit, err);
   240 		}
   241 
   242 	if(err!=KErrNone)
   243 		{	
   244 		KillTransfers();
   245 		RDebug::Print(msg);
   246 		iCaseStep = EFailed;
   247 		TTestCaseFailed request(err,msg);
   248 		return iControlEp0->SendRequest(request,this);
   249 		}	
   250 	}
   251 	
   252 void CUT_PBASE_T_USBDI_0496::DeviceInsertedL(TUint aDeviceHandle)
   253 	{
   254 	LOG_FUNC
   255 	
   256 	Cancel();
   257 	RDebug::Printf("this - %08x", this);
   258 	
   259 	TBuf<256> msg;
   260 	TInt err = KErrNone;
   261 	if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
   262 		// Prepare for response from control transfer to client
   263 		{
   264 		err = KErrGeneral;
   265 		_LIT(lit, "Base class DeviceInsertedL failed");
   266 		msg.Format(lit);
   267 		}
   268 	else
   269 		{
   270 		if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
   271 			// Prepare for response from control transfer to client
   272 			{
   273 			err = KErrGeneral;
   274 			_LIT(lit, "Base class SetUpInterfaceAndPipes for Interface 2 failed");
   275 			msg.Format(lit);
   276 			}
   277 		else
   278 			{
   279 	
   280 			iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[0]);
   281 			iInTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[1]);
   282 			
   283 			// Initialise the descriptors for transfer		
   284 			RDebug::Printf("Initialising the transfer descriptors - interface 1");
   285 			err = iUsbInterface1.InitialiseTransferDescriptors();
   286 			if(err != KErrNone)
   287 				{
   288 				_LIT(lit, "<Error %d> Unable to initialise transfer descriptors");
   289 				msg.Format(lit,err);
   290 				}
   291 			}
   292 		}
   293 	if(err != KErrNone)
   294 		{
   295 		RDebug::Print(msg);
   296 		iCaseStep = EFailed;
   297 		TTestCaseFailed request(err,msg);
   298 		iControlEp0->SendRequest(request,this);
   299 		}
   300 	else
   301 		{
   302 		RDebug::Printf("Asking client for 'Read' on interface 1");
   303 		iCaseStep = ERequestRepeatedWrite;	
   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
   305 		iControlEp0->SendRequest(request,this);
   306 		}
   307 	}
   308 
   309 TUint8 CUT_PBASE_T_USBDI_0496::Index(TUint8 aTransferId)
   310 	{
   311 	switch(aTransferId)
   312 		{
   313 		case KBulkTransferInId0:
   314 			return 0;
   315 		case KBulkTransferInId1:
   316 			return 1;
   317 		default:
   318 			_LIT(lit, "TRANSFER ID OUT OF RANGE");
   319 			User::Panic(lit, KErrArgument);
   320 			return 0; //should never get here
   321 		}
   322 	}
   323 
   324 TInt CUT_PBASE_T_USBDI_0496::ValidatePreviousAndPerformNextTransfers(TInt aTransferId)
   325 /**
   326 @param 	aTransferId - specifies trasnfer to use
   327 @return	zero UNLESS no more trasnfers have been queued for the specified ID in which case return the transfer ID
   328 Note:	This return value allows the caller to respond easily if no more transfers are required for the specified 
   329 		transfer ID.
   330 */
   331 	{
   332 	LOG_FUNC
   333 	
   334 	TUint8 index = Index(aTransferId);
   335 	RDebug::Printf("\n");
   336 	RDebug::Printf("Transfer[%d]", index);
   337 	
   338 	
   339 	if(iNumBytesExpected[index] != 0)
   340 		{
   341 		TPtrC8 data1(iInTransfer[index]->DataPolled());
   342 		if(ValidateData(data1, iValidateBufferPtr.Mid(iValidationStringStartPointTransfer[index], iNumBytesExpected[index])) == EFalse)
   343 			{
   344 			RDebug::Printf("=====VALIDATION FAILURE: Point of Validation String Entry %d, Newly Read Bytes %d=====",iValidationStringStartPointTransfer[index], iNumBytesExpected[index]);
   345 			iIsValid = EFalse;
   346 			}
   347 		iNumBytesExpected[index] = 0; //reset
   348 		}
   349 	if(iNumBytesRequestedSoFar >= KTotalBytesToTransfer)
   350 		//if we are near the end the other transfer will mop up remaining bytes...
   351 		{
   352 		RDebug::Printf("****ALL DONE for Transfer[%d]****", index);
   353 		return aTransferId; //tell caller that all transfers are complete for this transfer ID
   354 		}
   355 
   356 	iValidationStringStartPointTransfer[index] = iNumBytesRequestedSoFar%(KLiteralEnglish5().Length()); //PRIOR TO THIS TRANSFER
   357 	TUint bytesLeftToRead = KTotalBytesToTransfer - iNumBytesRequestedSoFar;
   358 	iNumBytesExpected[index] = bytesLeftToRead < KHostNumReadBytes ? bytesLeftToRead : KHostNumReadBytes;
   359 	iNumBytesRequestedSoFar += iNumBytesExpected[index];
   360 	iInTransfer[index]->TransferIn(KHostNumReadBytes); //rely on ZLP to complete the last 'TransferIn'
   361 	iExpectedNextTransferNumber = 1 - iExpectedNextTransferNumber;
   362 
   363 	return 0; //tell caller there are still transfers to complete for the requested transfer ID
   364 	}
   365 
   366 	} //end namespace