os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0497.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.
     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-0497.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-0497.h"
    20 #include "testpolicy.h"
    21 #include "modelleddevices.h"
    22 #include "testliterals.h"
    23 
    24 
    25  
    26 
    27 namespace NUnitTesting_USBDI
    28 	{
    29 const TInt KBulkMaxTransferSize = 40000;
    30 const TUint KHostNumWriteBytes1 = 32000;
    31 const TUint KHostNumWriteBytes2 = 32000;
    32 const TInt KDeviceNumReadBytes = 64000;
    33 const TUint KHostNumReadBytes1 = 32000;
    34 const TUint KHostNumReadBytes2 = 32001; //allow or ZLP
    35 
    36 
    37 //Make these single bit values ... 
    38 // ... so that their completion can be easily recorded in a bit mask!
    39 const TUint32 KIfc1BulkTransferInId1  = 1<<0;
    40 const TUint32 KIfc1BulkTransferInId2  = 1<<1;
    41 const TUint32 KIfc2BulkTransferInId1  = 1<<2;
    42 const TUint32 KIfc2BulkTransferInId2  = 1<<3;
    43 const TUint32 KIfc1BulkTransferInIdMask = KIfc1BulkTransferInId1 | KIfc1BulkTransferInId2; 
    44 const TUint32 KIfc2BulkTransferInIdMask = KIfc2BulkTransferInId1 | KIfc2BulkTransferInId2;
    45 const TUint32 KBulkTransferInIdMask = KIfc1BulkTransferInIdMask | KIfc2BulkTransferInIdMask;
    46 const TUint32 KIfc1BulkTransferOutId1 = 1<<4;
    47 const TUint32 KIfc1BulkTransferOutId2 = 1<<5;
    48 const TUint32 KIfc2BulkTransferOutId1 = 1<<6;
    49 const TUint32 KIfc2BulkTransferOutId2 = 1<<7;
    50 const TUint32 KIfc1BulkTransferOutIdMask = KIfc1BulkTransferOutId1 | KIfc1BulkTransferOutId2; 
    51 const TUint32 KIfc2BulkTransferOutIdMask = KIfc2BulkTransferOutId1 | KIfc2BulkTransferOutId2;
    52 const TUint32 KBulkTransferOutIdMask = KIfc1BulkTransferOutIdMask | KIfc2BulkTransferOutIdMask;
    53 
    54 const TInt KUnexpectedTransferID = -101;
    55 const TInt KUndefinedStep	 = -102;
    56 
    57 const TInt KMaxTimeDiffPercentage = 60;
    58 
    59 
    60 
    61 _LIT(KTestCaseId,"PBASE-T_USBDI-0497");
    62 const TFunctorTestCase<CUT_PBASE_T_USBDI_0497,TBool> CUT_PBASE_T_USBDI_0497::iFunctor(KTestCaseId);	
    63 
    64 CUT_PBASE_T_USBDI_0497* CUT_PBASE_T_USBDI_0497::NewL(TBool aHostRole)
    65 	{
    66 	CUT_PBASE_T_USBDI_0497* self = new (ELeave) CUT_PBASE_T_USBDI_0497(aHostRole);
    67 	CleanupStack::PushL(self);
    68 	self->ConstructL();
    69 	CleanupStack::Pop(self);
    70 	return self;
    71 	}
    72 	
    73 
    74 CUT_PBASE_T_USBDI_0497::CUT_PBASE_T_USBDI_0497(TBool aHostRole)
    75 :	CBaseBulkTestCase(KTestCaseId,aHostRole),
    76 	iCaseStep(EInProgress)
    77 	{
    78 	} 
    79 
    80 
    81 void CUT_PBASE_T_USBDI_0497::ConstructL()
    82 	{
    83 	BaseBulkConstructL();
    84 	}
    85 
    86 
    87 CUT_PBASE_T_USBDI_0497::~CUT_PBASE_T_USBDI_0497()
    88 	/**
    89 	The transfer objects have test specific names - so perform transfer specific clean up here.
    90 	*/
    91 	{
    92 	LOG_FUNC
    93 	
    94 	Cancel();
    95 
    96 	//Do this before deleting the transfer objects
    97 	CloseInterfaceAndPipes();
    98 	
    99 	TUint8 count;
   100 	for(count=0;count<KNumOutTransfersPerInterface;count++)
   101 		{
   102 		delete iIfc1OutTransfer[count];
   103 		iIfc1OutTransfer[count] = NULL;
   104 		delete iIfc2OutTransfer[count];
   105 		iIfc2OutTransfer[count] = NULL;
   106 		}
   107 	for(count=0;count<KNumInTransfersPerInterface;count++)
   108 		{
   109 		delete iIfc1InTransfer[count];
   110 		iIfc1InTransfer[count] = NULL;
   111 		delete iIfc2InTransfer[count];
   112 		iIfc2InTransfer[count] = NULL;
   113 		}
   114 	}
   115 	
   116 	
   117 void CUT_PBASE_T_USBDI_0497::Ep0TransferCompleteL(TInt aCompletionCode)
   118 	{
   119 	LOG_FUNC
   120 	
   121 	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
   122 	
   123 	if(aCompletionCode != KErrNone)
   124 		{
   125 		if(iCaseStep == EFailed)
   126 			{// ignore error, nad catch the TestFailed method called further down.
   127 			RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
   128 			}
   129 		else
   130 			{
   131 			TBuf<256> msg;
   132 			msg.Format(_L("<Error %d> Transfer to control endpoint 0 was not successful"),aCompletionCode);
   133 			RDebug::Print(msg);
   134 			iCaseStep = EFailed;
   135 			TTestCaseFailed request(aCompletionCode,msg);
   136 			iControlEp0->SendRequest(request,this);
   137 			return;
   138 			}
   139 		}
   140 	
   141 	switch(iCaseStep)
   142 		{
   143 		// Test case passed
   144 		case EPassed:
   145 			TestPassed();
   146 			break;
   147 		
   148 		// Test case failed	
   149 		case EFailed:
   150 			TestFailed(KErrCompletion);
   151 			break;
   152 		
   153 		case ERequestDeviceRead:
   154 			{
   155 			RDebug::Printf("Asking client for 'Read' on interface 2");
   156 			iCaseStep = ETransferOut;	
   157 			TEndpointReadRequest request(2,1,KDeviceNumReadBytes);// EP1 means endpoint index 1 not the actual endpoint number
   158 			iControlEp0->SendRequest(request,this);
   159 			}
   160 			break;
   161 			
   162 		case ETransferOut:
   163 			RDebug::Printf("Try to write data on BOTH interfaces");
   164 	
   165 			iStartTime[0].HomeTime();
   166 			iStartTime[1].HomeTime();
   167 	
   168 			iIfc1OutTransfer[0]->TransferOut(KLiteralFrench4(), KHostNumWriteBytes1, EFalse);
   169 			iIfc1OutTransfer[1]->TransferOut(KLiteralFrench4(), KHostNumWriteBytes1, KHostNumWriteBytes2, EFalse);
   170 			iIfc2OutTransfer[0]->TransferOut(KLiteralEnglish8(), KHostNumWriteBytes1, EFalse);
   171 			iIfc2OutTransfer[1]->TransferOut(KLiteralEnglish8(), KHostNumWriteBytes1, KHostNumWriteBytes2, EFalse);
   172 			break;
   173 		
   174 		case ERequestDeviceWriteBack:
   175 			{
   176 			RDebug::Printf("Asking client for 'Write' back on interface 2");
   177 			iCaseStep = ETransferIn;	
   178 			TWriteCachedReadDataRequest request(2,1,1);// EP1 means endpoint index 1 not the actual endpoint number
   179 			iControlEp0->SendRequest(request,this);
   180 			}
   181 			break;
   182 			
   183 		case ETransferIn:
   184 			RDebug::Printf("Try to receive data on BOTH interfaces");
   185 	
   186 			iStartTime[0].HomeTime();
   187 			iStartTime[1].HomeTime();
   188 			
   189 			iIfc1InTransfer[0]->TransferIn(KHostNumReadBytes1);
   190 			iIfc1InTransfer[1]->TransferIn(KHostNumReadBytes2);
   191 			iIfc2InTransfer[0]->TransferIn(KHostNumReadBytes1);
   192 			iIfc2InTransfer[1]->TransferIn(KHostNumReadBytes2);
   193 			break;
   194 	
   195 		default:
   196 			RDebug::Printf("<Error> Unknown test step");
   197 			TestFailed(KErrUnknown);
   198 			break;
   199 		}
   200 	}
   201 	
   202 void CUT_PBASE_T_USBDI_0497::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
   203 	{
   204 	LOG_FUNC
   205 	Cancel();
   206 	
   207 	TInt err(KErrNone);
   208 	TBuf<256> msg;
   209 	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
   210 
   211 	switch(iCaseStep)
   212 		{
   213 		case ETransferOut:
   214 			if(aCompletionCode != KErrNone)
   215 				{
   216 				iIfc1OutTransfer[0]->Cancel();
   217 				iIfc1OutTransfer[1]->Cancel();
   218 				iIfc2OutTransfer[0]->Cancel();
   219 				iIfc2OutTransfer[1]->Cancel();
   220 				err = KErrCorrupt;
   221 				msg.Format(_L("<Error %d> The transfer completed with an error."), aCompletionCode);
   222 				break; //switch(iCaseStep)
   223 				}
   224 	
   225 			switch(aTransferId)
   226 				{
   227 				case KIfc1BulkTransferOutId1:
   228 				case KIfc1BulkTransferOutId2:
   229 				case KIfc2BulkTransferOutId1:
   230 				case KIfc2BulkTransferOutId2:
   231 					iTransferComplete |= aTransferId;
   232 					RDebug::Printf("Transfer OUT %d completed (Transfer Completion Aggregation Mask 0x%x)", aTransferId, iTransferComplete);
   233 					break; //switch(aTransferId)
   234 				default:
   235 					iTransferComplete = 0; //reset
   236 					err = KUnexpectedTransferID;
   237 					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
   238 							       err, KIfc1BulkTransferInId1, KIfc1BulkTransferInId2, aTransferId);
   239 					break; //switch(aTransferId)
   240 				}
   241 			
   242 			if(err==KErrNone && iTimeElapsed[0] == 0 && (iTransferComplete & KIfc1BulkTransferOutIdMask) == KIfc1BulkTransferOutIdMask) 
   243 				//Record time elapsed for Interface 1 if not yet recorded.
   244 				{
   245 				RecordTime(0);
   246 				}
   247 
   248 			if(err==KErrNone && iTimeElapsed[1] == 0 && (iTransferComplete & KIfc2BulkTransferOutIdMask) == KIfc2BulkTransferOutIdMask)
   249 				//Record time elapsed for Interface 2 if not yet recorded.
   250 				{
   251 				RecordTime(1);
   252 				}
   253 
   254 			if(err==KErrNone && (iTransferComplete & KBulkTransferOutIdMask) == KBulkTransferOutIdMask)
   255 				{
   256 				RDebug::Printf("All OUT Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
   257 				iTransferComplete = 0; //reset
   258 				err = CheckAndResetTimes(0, 1, KMaxTimeDiffPercentage);
   259 				if(err!=KErrNone)
   260 					{
   261 					break; //switch(iCaseStep)
   262 					}
   263 	
   264 				RDebug::Printf("Asking client for 'Write' back on interface 1");
   265 				iCaseStep = ERequestDeviceWriteBack;
   266 				TWriteCachedReadDataRequest request(1,1,1);// EP1 means endpoint index 1 not the actual endpoint number
   267 				iControlEp0->SendRequest(request,this);
   268 				}
   269 			break; //switch(iCaseStep)
   270 
   271 		case ETransferIn:
   272 			if(aCompletionCode != KErrNone)
   273 				{
   274 				iIfc1InTransfer[0]->Cancel();
   275 				iIfc1InTransfer[1]->Cancel();
   276 				iIfc2InTransfer[0]->Cancel();
   277 				iIfc2InTransfer[1]->Cancel();
   278 				err = KErrCorrupt;			
   279 				
   280 				msg.Format(_L("<Error %d> No data sent on bulk IN request"),aCompletionCode);
   281 				break; //switch(iCaseStep)
   282 				}
   283 	
   284 			switch(aTransferId)
   285 				{
   286 				case KIfc1BulkTransferInId1:
   287 				case KIfc1BulkTransferInId2:
   288 				case KIfc2BulkTransferInId1:
   289 				case KIfc2BulkTransferInId2:
   290 					iTransferComplete |= aTransferId;
   291 					RDebug::Printf("Transfer OUT %d completed (Transfer Completion Aggregation Mask 0x%x)", aTransferId, iTransferComplete);
   292 					break; //switch(aTransferId)
   293 	
   294 				default:
   295 					iTransferComplete = 0; //reset
   296 					err = KUnexpectedTransferID;
   297 					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
   298 							       err, KIfc1BulkTransferInId1, KIfc1BulkTransferInId2, aTransferId);
   299 					break; //switch(aTransferId)
   300 				}
   301 			         
   302 			if(err==KErrNone && iTimeElapsed[0] == 0 && (iTransferComplete & KIfc1BulkTransferInIdMask) == KIfc1BulkTransferInIdMask)
   303 				//Record time elapsed for Interface 1 if not yet recorded.
   304 				{
   305 				RecordTime(0);
   306 				}
   307 
   308 			if(err==KErrNone && iTimeElapsed[1] == 0 && (iTransferComplete & KIfc2BulkTransferInIdMask) == KIfc2BulkTransferInIdMask) 
   309 				//Record time elapsed for Interface 2 if not yet recorded.
   310 				{
   311 				RecordTime(1);
   312 				}
   313 
   314 			if(err==KErrNone && (iTransferComplete & KBulkTransferInIdMask) == KBulkTransferInIdMask)
   315 				{
   316 				// ok, compare data rcvd now
   317 				RDebug::Printf("All IN Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
   318 				iTransferComplete = 0; //reset
   319 				err = CheckAndResetTimes(0, 1, KMaxTimeDiffPercentage);
   320 				if(err!=KErrNone)
   321 					{
   322 					break; //switch(iCaseStep)
   323 					}
   324 	
   325 				TPtrC8 data1(iIfc1InTransfer[0]->DataPolled());	
   326 				TPtrC8 data2(iIfc1InTransfer[1]->DataPolled());		
   327 				TPtrC8 data3(iIfc2InTransfer[0]->DataPolled());		
   328 				TPtrC8 data4(iIfc2InTransfer[1]->DataPolled());		
   329 				//Validate first transfer on Interface 1 for number of bytes originally written.
   330 				if(ValidateData(data1, KLiteralFrench4(), KHostNumWriteBytes1) == EFalse)
   331 					{
   332 					RDebug::Printf("Validation failure 1st transfer, Interface 1");
   333 					err = KErrCompletion; //indicates data validation failure
   334 					break; //switch(iCaseStep)
   335 					}
   336 
   337 				//Validate second transfer on Interface 1 for number of bytes originally written.
   338 				if(ValidateData(data2, KLiteralFrench4(), KHostNumWriteBytes1, KHostNumWriteBytes2) == EFalse)
   339 					{
   340 					RDebug::Printf("Validation failure 2nd transfer, Interface 1");
   341 					err = KErrCompletion; //indicates data validation failure
   342 					break; //switch(iCaseStep)
   343 					}
   344 
   345 				//Validate first transfer on Interface 2 for number of bytes originally written.
   346 				if(ValidateData(data3, KLiteralEnglish8(), KHostNumWriteBytes1) == EFalse)
   347 					{
   348 					RDebug::Printf("Validation failure 1st transfer, Interface 2");
   349 					err = KErrCompletion; //indicates data validation failure
   350 					break; //switch(iCaseStep)
   351 					}
   352 
   353 				//Validate second transfer on Interface 2 for number of bytes originally written.
   354 				if(ValidateData(data4, KLiteralEnglish8(), KHostNumWriteBytes1, KHostNumWriteBytes2) == EFalse)
   355 					{
   356 					RDebug::Printf("Validation failure 2nd transfer, Interface 2");
   357 					err = KErrCompletion; //indicates data validation failure
   358 					break; //switch(iCaseStep)
   359 					}
   360 
   361 				// Comparison is a match
   362 				RDebug::Printf("Comparison for IN transfer is a match");
   363 				iCaseStep = EPassed;
   364 				TTestCasePassed request;
   365 				iControlEp0->SendRequest(request,this);
   366 				}
   367 			break; //switch(iCaseStep)
   368 
   369 		default:
   370 			err = KUndefinedStep;
   371 			msg.Format(_L("<Error %d> Undefined case step %d reached"),KUndefinedStep, iCaseStep);
   372 			break; //switch(iCaseStep)
   373 		}
   374 	
   375 	if(err == KErrCompletion)
   376 		//indicates data validation failure
   377 		{
   378 		msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
   379 		}
   380 
   381 	if(err == KErrTooBig)
   382 		//indicates timing validation failure
   383 		{
   384 		msg.Format(_L("<Error %d> Timer comparison showed too great a difference in transfer times between the two interfaces"), err);
   385 		}
   386 	
   387 	if(err!=KErrNone)
   388 		{	
   389 		RDebug::Print(msg);
   390 		iCaseStep = EFailed;
   391 		TTestCaseFailed request(err,msg);
   392 		return iControlEp0->SendRequest(request,this);
   393 		}	
   394 	}
   395 	
   396 void CUT_PBASE_T_USBDI_0497::DeviceInsertedL(TUint aDeviceHandle)
   397 	{
   398 	LOG_FUNC
   399 	
   400 	Cancel();
   401 	RDebug::Printf("this - %08x", this);
   402 	
   403 	TBuf<256> msg;
   404 	TInt err = KErrNone;
   405 	if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
   406 		// Prepare for response from control transfer to client
   407 		{
   408 		err = KErrGeneral;
   409 		msg.Format(_L("Base class DeviceInsertedL failed"));
   410 		}
   411 	else
   412 		{
   413 		if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
   414 			// Prepare for response from control transfer to client
   415 			{
   416 			err = KErrGeneral;
   417 			msg.Format(_L("Base class SetUpInterfaceAndPipes for Interface 2 failed"));
   418 			}
   419 		else
   420 			{
   421 	
   422 			iIfc1InTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KIfc1BulkTransferInId1);
   423 			iIfc1InTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KIfc1BulkTransferInId2);
   424 			iIfc1OutTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxTransferSize,*this,KIfc1BulkTransferOutId1);
   425 			iIfc1OutTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxTransferSize,*this,KIfc1BulkTransferOutId2);
   426 
   427 			iIfc2InTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface2BulkIn,iUsbInterface2,KBulkMaxTransferSize,*this,KIfc2BulkTransferInId1);
   428 			iIfc2InTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface2BulkIn,iUsbInterface2,KBulkMaxTransferSize,*this,KIfc2BulkTransferInId2);
   429 			iIfc2OutTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface2BulkOut1,iUsbInterface2,KBulkMaxTransferSize,*this,KIfc2BulkTransferOutId1);
   430 			iIfc2OutTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface2BulkOut1,iUsbInterface2,KBulkMaxTransferSize,*this,KIfc2BulkTransferOutId2);
   431 			
   432 			// Initialise the descriptors for transfer		
   433 			RDebug::Printf("Initialising the transfer descriptors - interface 1");
   434 			err = iUsbInterface1.InitialiseTransferDescriptors();
   435 			if(err != KErrNone)
   436 				{
   437 				msg.Format(_L("<Error %d> Unable to initialise transfer descriptors (Interface 1)"),err);
   438 				}
   439 			else
   440 				{
   441 				// Initialise the descriptors for transfer		
   442 				RDebug::Printf("Initialising the transfer descriptors (Interface 2)");
   443 				err = iUsbInterface2.InitialiseTransferDescriptors();
   444 				if(err != KErrNone)
   445 					{
   446 					msg.Format(_L("<Error %d> Unable to initialise transfer descriptors"),err);
   447 					}
   448 				}
   449 			}
   450 		}
   451 	if(err != KErrNone)
   452 		{
   453 		RDebug::Print(msg);
   454 		iCaseStep = EFailed;
   455 		TTestCaseFailed request(err,msg);
   456 		iControlEp0->SendRequest(request,this);
   457 		}
   458 	else
   459 		{
   460 		RDebug::Printf("Asking client for 'Read' on interface 1");
   461 		iCaseStep = ERequestDeviceRead;	
   462 		TEndpointReadRequest request(1,1,KDeviceNumReadBytes);// EP1 means endpoint index 1 not the actual endpoint number
   463 		iControlEp0->SendRequest(request,this);
   464 		}
   465 	}
   466 	
   467 	} //end namespace