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