os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0499.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-0499.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-0499.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 KBulkMaxINTransferSize = 1200;
    30 const TInt KBulkMaxOUTTransferSize = 150000;
    31 const TUint KHostNumWriteBytesPreHalt1 = 128000;
    32 const TUint KHostNumWriteBytesPreHalt2 = 128000;
    33 const TUint KHostNumWriteBytesPostHalt1 = 512;
    34 const TUint KHostNumWriteBytesPostHalt2 = 512;
    35 const TInt KDeviceNumWrittenBytesPreHalt = KHostNumWriteBytesPreHalt1+KHostNumWriteBytesPreHalt2;
    36 const TInt KDeviceNumReadBytesPostHalt = KHostNumWriteBytesPostHalt1+KHostNumWriteBytesPostHalt2;
    37 const TInt KDeviceNumReadBytesPreHalt = 128;
    38 const TInt KHostNumReadBytesPostHalt = KDeviceNumReadBytesPostHalt+1; //read cached data, allow 1 for ZLP
    39 
    40 
    41 //Make these single bit values ... 
    42 // ... so that their completion can be easily recorded in a bit mask!
    43 const TInt KBulkTransferInId0 = 1<<0;
    44 const TInt KBulkTransferOutId0 = 1<<3;
    45 const TInt KBulkTransferOutId1 = 1<<4;
    46 
    47 const TInt KUnexpectedTransferID = -101;
    48 const TInt KUndefinedStep	 = -102;
    49 
    50 
    51 _LIT(KTestCaseId,"PBASE-T_USBDI-0499");
    52 const TFunctorTestCase<CUT_PBASE_T_USBDI_0499,TBool> CUT_PBASE_T_USBDI_0499::iFunctor(KTestCaseId);	
    53 
    54 CUT_PBASE_T_USBDI_0499* CUT_PBASE_T_USBDI_0499::NewL(TBool aHostRole)
    55 	{
    56 	CUT_PBASE_T_USBDI_0499* self = new (ELeave) CUT_PBASE_T_USBDI_0499(aHostRole);
    57 	CleanupStack::PushL(self);
    58 	self->ConstructL();
    59 	CleanupStack::Pop(self);
    60 	return self;
    61 	}
    62 	
    63 
    64 CUT_PBASE_T_USBDI_0499::CUT_PBASE_T_USBDI_0499(TBool aHostRole)
    65 :	CBaseBulkTestCase(KTestCaseId,aHostRole),
    66 	iCaseStep(EInProgress)
    67 	{
    68 	} 
    69 
    70 
    71 void CUT_PBASE_T_USBDI_0499::ConstructL()
    72 	{
    73 	BaseBulkConstructL();
    74 	}
    75 
    76 
    77 CUT_PBASE_T_USBDI_0499::~CUT_PBASE_T_USBDI_0499()
    78 	{
    79 	LOG_FUNC
    80 	}
    81 	
    82 	
    83 void CUT_PBASE_T_USBDI_0499::Ep0TransferCompleteL(TInt aCompletionCode)
    84 	{
    85 	LOG_FUNC
    86 	
    87 	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
    88 	
    89 	if(aCompletionCode != KErrNone)
    90 		{
    91 		if(iCaseStep == EFailed)
    92 			{// ignore error, nad catch the TestFailed method called further down.
    93 			RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
    94 			}
    95 		else
    96 			{
    97 			TBuf<256> msg;
    98 			msg.Format(_L("<Error %d> Transfer to control endpoint 0 was not successful"),aCompletionCode);
    99 			RDebug::Print(msg);
   100 			iCaseStep = EFailed;
   101 			TTestCaseFailed request(aCompletionCode,msg);
   102 			iControlEp0->SendRequest(request,this);
   103 			return;
   104 			}
   105 		}
   106 
   107 	switch(iCaseStep)
   108 		{
   109 		// Test case passed
   110 		case EPassed:
   111 			TestPassed();
   112 			break;
   113 		
   114 		// Test case failed	
   115 		case EFailed:
   116 			TestFailed(KErrCompletion);
   117 			break;
   118 			
   119 		case ETransferOutHalt:
   120 			iOutTransfer[0]->TransferOut(KLiteralFrench4(), KHostNumWriteBytesPreHalt1, EFalse);
   121 			iOutTransfer[1]->TransferOut(KLiteralFrench4(), KHostNumWriteBytesPreHalt2, EFalse);
   122 			break;
   123 		
   124 		case EAwaitClearPreHalt:
   125 			{
   126 			RDebug::Printf("Client has been asked to clear endpoint buffer");
   127 			User::After(1000000); //Give client time to clear buffer
   128 			TEndpointCancelReadRequest request(1,1);
   129 			iControlEp0->SendRequest(request,this);
   130 			iCaseStep = EAwaitCancelRead;
   131 			}
   132 			break;
   133 
   134 		case EAwaitCancelRead:
   135 			{
   136 			TEndpointReadRequest request(1,1, KDeviceNumReadBytesPostHalt);// EP1 because 1st writter EP
   137 			iControlEp0->SendRequest(request,this);
   138 			iCaseStep = ETransferOut;
   139 			}
   140 			break;
   141 			
   142 		case ETransferOut:
   143 			RDebug::Printf("Try to send data (post halt)");
   144 			iOutTransfer[0]->TransferOut(KLiteralEnglish8().Mid(0, KHostNumWriteBytesPostHalt1), EFalse);
   145 			iOutTransfer[1]->TransferOut(KLiteralEnglish8().Mid(KHostNumWriteBytesPostHalt1, KHostNumWriteBytesPostHalt2), EFalse);
   146 			break;
   147 		
   148 		case ETransferIn:
   149 			RDebug::Printf("Try to receive data");
   150 			iInTransfer[0]->TransferIn(KHostNumReadBytesPostHalt);
   151 			break;
   152 	
   153 		default:
   154 			RDebug::Printf("<Error> Unknown test step");
   155 			TestFailed(KErrUnknown);
   156 			break;
   157 		}
   158 	}
   159 	
   160 void CUT_PBASE_T_USBDI_0499::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
   161 	{
   162 	LOG_FUNC
   163 	Cancel();
   164 	
   165 	TInt err(KErrNone);
   166 	TBuf<256> msg;
   167 	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d",aTransferId, aCompletionCode);
   168 
   169 	switch(iCaseStep)
   170 		{
   171 		case ETransferOutHalt:
   172 			{
   173 			if(aCompletionCode != KErrUsbStalled)
   174 				{
   175 				iOutTransfer[0]->Cancel();
   176 				iOutTransfer[1]->Cancel();
   177 				err = KErrCorrupt;
   178 				msg.Format(_L("<Error %d> The transfer completed with no errors but the endpoint should have halted"),aCompletionCode);
   179 				break; //switch(iCaseStep)
   180 				}
   181 	
   182 			switch(aTransferId)
   183 				{
   184 				case KBulkTransferOutId0:
   185 				case KBulkTransferOutId1:
   186 					iTransferComplete |= aTransferId;
   187 					RDebug::Printf("Transfer %d stalled", aTransferId);
   188 					break; //switch(aTransferId)
   189 				
   190 				default:
   191 					iTransferComplete = 0; //reset
   192 					err = KUnexpectedTransferID;
   193 					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
   194 							       err, KBulkTransferOutId0, KBulkTransferOutId1, aTransferId);
   195 					break; //switch(aTransferId)
   196 				}
   197 
   198 			if(err==KErrNone && iTransferComplete == (KBulkTransferOutId0 | KBulkTransferOutId1))
   199 				{
   200 				RDebug::Printf("Clear halt and try to send data again. Transfers Completed %d", iTransferComplete);
   201 				// Acknowledge the stall and clear				
   202 				err = iTestPipeInterface1BulkOut.ClearRemoteStall();
   203 				if(err != KErrNone)
   204 					{
   205 					msg.Format(_L("<Error %d> The remote stall cannot be cleared"),err);
   206 					break; //switch(iCaseStep)
   207 					}
   208 				iCaseStep = EAwaitClearPreHalt;
   209 				TEndpointReadRequest request(1,1, KDeviceNumWrittenBytesPreHalt);// EP1 because 1st writter EP
   210 				iControlEp0->SendRequest(request,this);	
   211 				iTransferComplete = 0; //reset
   212 				}
   213 			}
   214 			break; //switch(iCaseStep)
   215 
   216 		case ETransferOut:
   217 			{
   218 			if(aCompletionCode != KErrNone)
   219 				{
   220 				iOutTransfer[0]->Cancel();
   221 				iOutTransfer[1]->Cancel();
   222 				err = KErrCorrupt;
   223 				msg.Format(_L("<Error %d> No data sent on bulk OUT request"),aCompletionCode);
   224 				break; //switch(iCaseStep)
   225 				}
   226 	
   227 			switch(aTransferId)
   228 				{
   229 				case KBulkTransferOutId0:
   230 				case KBulkTransferOutId1:
   231 					iTransferComplete |= aTransferId;
   232 					RDebug::Printf("Transfer %d completed", aTransferId);
   233 					break; //switch(aTransferId)
   234 
   235 				default:
   236 					iTransferComplete = 0; //reset
   237 					err = KUnexpectedTransferID;
   238 					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
   239 							       err, KBulkTransferOutId0, KBulkTransferOutId1, aTransferId);
   240 					break; //switch(aTransferId)
   241 				}
   242 
   243 			if(err==KErrNone && iTransferComplete == (KBulkTransferOutId0 | KBulkTransferOutId1))
   244 				{
   245 				RDebug::Printf("Try to receive back sent data. Transfers Completed %d", iTransferComplete);
   246 				iCaseStep = ETransferIn;
   247 				TWriteSynchronousCachedReadDataRequest request(1,1,1);
   248 				iControlEp0->SendRequest(request,this);	
   249 				iTransferComplete = 0; //reset
   250 				}
   251 			break; //switch(iCaseStep)
   252 
   253 		case ETransferIn:
   254 			if(aCompletionCode != KErrNone)
   255 				{
   256 				err = KErrCorrupt;
   257 				msg.Format(_L("<Error %d> No data sent on bulk IN request"),aCompletionCode);
   258 				break; //switch(iCaseStep)
   259 				}
   260 	
   261 			if(aTransferId != KBulkTransferInId0)
   262 				{
   263 				iTransferComplete = 0; //reset
   264 				err = KUnexpectedTransferID;
   265 				msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d , got %d"),
   266 					       err, KBulkTransferInId0, aTransferId);
   267 				break; //switch(iCaseStep)
   268 				}
   269 				
   270 			// else ok, compare data rcvd now
   271 			TPtrC8 data1(iInTransfer[0]->DataPolled());		
   272 			if(ValidateData(data1, KLiteralEnglish8().Mid(0, KDeviceNumReadBytesPostHalt)) == EFalse)
   273 				{
   274 				err = KErrCompletion; //indicates data validation failure
   275 				msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
   276 				break; //switch(iCaseStep)
   277 				}
   278 			
   279 			// Comparison is a match
   280 			RDebug::Printf("Comparison for IN transfer is a match");
   281 			iCaseStep = EPassed;
   282 			TTestCasePassed request;
   283 			iControlEp0->SendRequest(request,this);
   284 			iTransferComplete = 0; //reset
   285 			}
   286 			break; //switch(iCaseStep)
   287 
   288 		default:
   289 			err = KUndefinedStep;
   290 			msg.Format(_L("<Error %d> Undefined case step %d reached"),KUndefinedStep, iCaseStep);
   291 			break; //switch(iCaseStep)
   292 		}
   293 	
   294 	if(err == KErrCompletion)
   295 		//indicates data validation failure
   296 		{
   297 		msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
   298 		}
   299 	
   300 	if(err!=KErrNone)
   301 		{	
   302 		RDebug::Print(msg);
   303 		iCaseStep = EFailed;
   304 		TTestCaseFailed request(err,msg);
   305 		return iControlEp0->SendRequest(request,this);
   306 		}	
   307 	}
   308 	
   309 void CUT_PBASE_T_USBDI_0499::DeviceInsertedL(TUint aDeviceHandle)
   310 	{
   311 	LOG_FUNC
   312 	
   313 	Cancel();
   314 	
   315 	if(BaseBulkDeviceInsertedL(aDeviceHandle) == EDeviceConfigurationError)
   316 		// Prepare for response from control transfer to client
   317 		{
   318 		iCaseStep = EFailed;
   319 		}
   320 	
   321 	// Create the bulk transfers	
   322 	iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxINTransferSize,*this,KBulkTransferInId0);
   323 	iOutTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxOUTTransferSize,*this,KBulkTransferOutId0);
   324 	iOutTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxOUTTransferSize,*this,KBulkTransferOutId1);
   325 	
   326 	// Initialise the descriptors for transfer		
   327 	RDebug::Printf("Initialising the transfer descriptors");
   328 	TInt err = iUsbInterface1.InitialiseTransferDescriptors();
   329 	if(err != KErrNone)
   330 		{
   331 		TBuf<256> msg;
   332 		msg.Format(_L("<Error %d> Unable to initialise transfer descriptors"),err);
   333 		RDebug::Print(msg);
   334 		iCaseStep = EFailed;
   335 		TTestCaseFailed request(err,msg);
   336 		iControlEp0->SendRequest(request,this);
   337 		return;
   338 		}
   339 
   340 	iCaseStep = ETransferOutHalt;	
   341 	TEndpointReadAndHaltRequest request(1,1,KDeviceNumReadBytesPreHalt);// EP1 means endpoint index 1 not the actual endpoint number
   342 	iControlEp0->SendRequest(request,this);
   343 	}
   344 	
   345 	} //end namespace