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