os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0499.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0499.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,345 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// @file PBASE-T_USBDI-0499.cpp
    1.18 +// @internalComponent
    1.19 +// 
    1.20 +//
    1.21 +
    1.22 +#include "PBASE-T_USBDI-0499.h"
    1.23 +#include "testpolicy.h"
    1.24 +#include "modelleddevices.h"
    1.25 +#include "testliterals.h"
    1.26 +
    1.27 +
    1.28 + 
    1.29 +
    1.30 +namespace NUnitTesting_USBDI
    1.31 +	{
    1.32 +const TInt KBulkMaxINTransferSize = 1200;
    1.33 +const TInt KBulkMaxOUTTransferSize = 150000;
    1.34 +const TUint KHostNumWriteBytesPreHalt1 = 128000;
    1.35 +const TUint KHostNumWriteBytesPreHalt2 = 128000;
    1.36 +const TUint KHostNumWriteBytesPostHalt1 = 512;
    1.37 +const TUint KHostNumWriteBytesPostHalt2 = 512;
    1.38 +const TInt KDeviceNumWrittenBytesPreHalt = KHostNumWriteBytesPreHalt1+KHostNumWriteBytesPreHalt2;
    1.39 +const TInt KDeviceNumReadBytesPostHalt = KHostNumWriteBytesPostHalt1+KHostNumWriteBytesPostHalt2;
    1.40 +const TInt KDeviceNumReadBytesPreHalt = 128;
    1.41 +const TInt KHostNumReadBytesPostHalt = KDeviceNumReadBytesPostHalt+1; //read cached data, allow 1 for ZLP
    1.42 +
    1.43 +
    1.44 +//Make these single bit values ... 
    1.45 +// ... so that their completion can be easily recorded in a bit mask!
    1.46 +const TInt KBulkTransferInId0 = 1<<0;
    1.47 +const TInt KBulkTransferOutId0 = 1<<3;
    1.48 +const TInt KBulkTransferOutId1 = 1<<4;
    1.49 +
    1.50 +const TInt KUnexpectedTransferID = -101;
    1.51 +const TInt KUndefinedStep	 = -102;
    1.52 +
    1.53 +
    1.54 +_LIT(KTestCaseId,"PBASE-T_USBDI-0499");
    1.55 +const TFunctorTestCase<CUT_PBASE_T_USBDI_0499,TBool> CUT_PBASE_T_USBDI_0499::iFunctor(KTestCaseId);	
    1.56 +
    1.57 +CUT_PBASE_T_USBDI_0499* CUT_PBASE_T_USBDI_0499::NewL(TBool aHostRole)
    1.58 +	{
    1.59 +	CUT_PBASE_T_USBDI_0499* self = new (ELeave) CUT_PBASE_T_USBDI_0499(aHostRole);
    1.60 +	CleanupStack::PushL(self);
    1.61 +	self->ConstructL();
    1.62 +	CleanupStack::Pop(self);
    1.63 +	return self;
    1.64 +	}
    1.65 +	
    1.66 +
    1.67 +CUT_PBASE_T_USBDI_0499::CUT_PBASE_T_USBDI_0499(TBool aHostRole)
    1.68 +:	CBaseBulkTestCase(KTestCaseId,aHostRole),
    1.69 +	iCaseStep(EInProgress)
    1.70 +	{
    1.71 +	} 
    1.72 +
    1.73 +
    1.74 +void CUT_PBASE_T_USBDI_0499::ConstructL()
    1.75 +	{
    1.76 +	BaseBulkConstructL();
    1.77 +	}
    1.78 +
    1.79 +
    1.80 +CUT_PBASE_T_USBDI_0499::~CUT_PBASE_T_USBDI_0499()
    1.81 +	{
    1.82 +	LOG_FUNC
    1.83 +	}
    1.84 +	
    1.85 +	
    1.86 +void CUT_PBASE_T_USBDI_0499::Ep0TransferCompleteL(TInt aCompletionCode)
    1.87 +	{
    1.88 +	LOG_FUNC
    1.89 +	
    1.90 +	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
    1.91 +	
    1.92 +	if(aCompletionCode != KErrNone)
    1.93 +		{
    1.94 +		if(iCaseStep == EFailed)
    1.95 +			{// ignore error, nad catch the TestFailed method called further down.
    1.96 +			RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
    1.97 +			}
    1.98 +		else
    1.99 +			{
   1.100 +			TBuf<256> msg;
   1.101 +			msg.Format(_L("<Error %d> Transfer to control endpoint 0 was not successful"),aCompletionCode);
   1.102 +			RDebug::Print(msg);
   1.103 +			iCaseStep = EFailed;
   1.104 +			TTestCaseFailed request(aCompletionCode,msg);
   1.105 +			iControlEp0->SendRequest(request,this);
   1.106 +			return;
   1.107 +			}
   1.108 +		}
   1.109 +
   1.110 +	switch(iCaseStep)
   1.111 +		{
   1.112 +		// Test case passed
   1.113 +		case EPassed:
   1.114 +			TestPassed();
   1.115 +			break;
   1.116 +		
   1.117 +		// Test case failed	
   1.118 +		case EFailed:
   1.119 +			TestFailed(KErrCompletion);
   1.120 +			break;
   1.121 +			
   1.122 +		case ETransferOutHalt:
   1.123 +			iOutTransfer[0]->TransferOut(KLiteralFrench4(), KHostNumWriteBytesPreHalt1, EFalse);
   1.124 +			iOutTransfer[1]->TransferOut(KLiteralFrench4(), KHostNumWriteBytesPreHalt2, EFalse);
   1.125 +			break;
   1.126 +		
   1.127 +		case EAwaitClearPreHalt:
   1.128 +			{
   1.129 +			RDebug::Printf("Client has been asked to clear endpoint buffer");
   1.130 +			User::After(1000000); //Give client time to clear buffer
   1.131 +			TEndpointCancelReadRequest request(1,1);
   1.132 +			iControlEp0->SendRequest(request,this);
   1.133 +			iCaseStep = EAwaitCancelRead;
   1.134 +			}
   1.135 +			break;
   1.136 +
   1.137 +		case EAwaitCancelRead:
   1.138 +			{
   1.139 +			TEndpointReadRequest request(1,1, KDeviceNumReadBytesPostHalt);// EP1 because 1st writter EP
   1.140 +			iControlEp0->SendRequest(request,this);
   1.141 +			iCaseStep = ETransferOut;
   1.142 +			}
   1.143 +			break;
   1.144 +			
   1.145 +		case ETransferOut:
   1.146 +			RDebug::Printf("Try to send data (post halt)");
   1.147 +			iOutTransfer[0]->TransferOut(KLiteralEnglish8().Mid(0, KHostNumWriteBytesPostHalt1), EFalse);
   1.148 +			iOutTransfer[1]->TransferOut(KLiteralEnglish8().Mid(KHostNumWriteBytesPostHalt1, KHostNumWriteBytesPostHalt2), EFalse);
   1.149 +			break;
   1.150 +		
   1.151 +		case ETransferIn:
   1.152 +			RDebug::Printf("Try to receive data");
   1.153 +			iInTransfer[0]->TransferIn(KHostNumReadBytesPostHalt);
   1.154 +			break;
   1.155 +	
   1.156 +		default:
   1.157 +			RDebug::Printf("<Error> Unknown test step");
   1.158 +			TestFailed(KErrUnknown);
   1.159 +			break;
   1.160 +		}
   1.161 +	}
   1.162 +	
   1.163 +void CUT_PBASE_T_USBDI_0499::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
   1.164 +	{
   1.165 +	LOG_FUNC
   1.166 +	Cancel();
   1.167 +	
   1.168 +	TInt err(KErrNone);
   1.169 +	TBuf<256> msg;
   1.170 +	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d",aTransferId, aCompletionCode);
   1.171 +
   1.172 +	switch(iCaseStep)
   1.173 +		{
   1.174 +		case ETransferOutHalt:
   1.175 +			{
   1.176 +			if(aCompletionCode != KErrUsbStalled)
   1.177 +				{
   1.178 +				iOutTransfer[0]->Cancel();
   1.179 +				iOutTransfer[1]->Cancel();
   1.180 +				err = KErrCorrupt;
   1.181 +				msg.Format(_L("<Error %d> The transfer completed with no errors but the endpoint should have halted"),aCompletionCode);
   1.182 +				break; //switch(iCaseStep)
   1.183 +				}
   1.184 +	
   1.185 +			switch(aTransferId)
   1.186 +				{
   1.187 +				case KBulkTransferOutId0:
   1.188 +				case KBulkTransferOutId1:
   1.189 +					iTransferComplete |= aTransferId;
   1.190 +					RDebug::Printf("Transfer %d stalled", aTransferId);
   1.191 +					break; //switch(aTransferId)
   1.192 +				
   1.193 +				default:
   1.194 +					iTransferComplete = 0; //reset
   1.195 +					err = KUnexpectedTransferID;
   1.196 +					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
   1.197 +							       err, KBulkTransferOutId0, KBulkTransferOutId1, aTransferId);
   1.198 +					break; //switch(aTransferId)
   1.199 +				}
   1.200 +
   1.201 +			if(err==KErrNone && iTransferComplete == (KBulkTransferOutId0 | KBulkTransferOutId1))
   1.202 +				{
   1.203 +				RDebug::Printf("Clear halt and try to send data again. Transfers Completed %d", iTransferComplete);
   1.204 +				// Acknowledge the stall and clear				
   1.205 +				err = iTestPipeInterface1BulkOut.ClearRemoteStall();
   1.206 +				if(err != KErrNone)
   1.207 +					{
   1.208 +					msg.Format(_L("<Error %d> The remote stall cannot be cleared"),err);
   1.209 +					break; //switch(iCaseStep)
   1.210 +					}
   1.211 +				iCaseStep = EAwaitClearPreHalt;
   1.212 +				TEndpointReadRequest request(1,1, KDeviceNumWrittenBytesPreHalt);// EP1 because 1st writter EP
   1.213 +				iControlEp0->SendRequest(request,this);	
   1.214 +				iTransferComplete = 0; //reset
   1.215 +				}
   1.216 +			}
   1.217 +			break; //switch(iCaseStep)
   1.218 +
   1.219 +		case ETransferOut:
   1.220 +			{
   1.221 +			if(aCompletionCode != KErrNone)
   1.222 +				{
   1.223 +				iOutTransfer[0]->Cancel();
   1.224 +				iOutTransfer[1]->Cancel();
   1.225 +				err = KErrCorrupt;
   1.226 +				msg.Format(_L("<Error %d> No data sent on bulk OUT request"),aCompletionCode);
   1.227 +				break; //switch(iCaseStep)
   1.228 +				}
   1.229 +	
   1.230 +			switch(aTransferId)
   1.231 +				{
   1.232 +				case KBulkTransferOutId0:
   1.233 +				case KBulkTransferOutId1:
   1.234 +					iTransferComplete |= aTransferId;
   1.235 +					RDebug::Printf("Transfer %d completed", aTransferId);
   1.236 +					break; //switch(aTransferId)
   1.237 +
   1.238 +				default:
   1.239 +					iTransferComplete = 0; //reset
   1.240 +					err = KUnexpectedTransferID;
   1.241 +					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
   1.242 +							       err, KBulkTransferOutId0, KBulkTransferOutId1, aTransferId);
   1.243 +					break; //switch(aTransferId)
   1.244 +				}
   1.245 +
   1.246 +			if(err==KErrNone && iTransferComplete == (KBulkTransferOutId0 | KBulkTransferOutId1))
   1.247 +				{
   1.248 +				RDebug::Printf("Try to receive back sent data. Transfers Completed %d", iTransferComplete);
   1.249 +				iCaseStep = ETransferIn;
   1.250 +				TWriteSynchronousCachedReadDataRequest request(1,1,1);
   1.251 +				iControlEp0->SendRequest(request,this);	
   1.252 +				iTransferComplete = 0; //reset
   1.253 +				}
   1.254 +			break; //switch(iCaseStep)
   1.255 +
   1.256 +		case ETransferIn:
   1.257 +			if(aCompletionCode != KErrNone)
   1.258 +				{
   1.259 +				err = KErrCorrupt;
   1.260 +				msg.Format(_L("<Error %d> No data sent on bulk IN request"),aCompletionCode);
   1.261 +				break; //switch(iCaseStep)
   1.262 +				}
   1.263 +	
   1.264 +			if(aTransferId != KBulkTransferInId0)
   1.265 +				{
   1.266 +				iTransferComplete = 0; //reset
   1.267 +				err = KUnexpectedTransferID;
   1.268 +				msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d , got %d"),
   1.269 +					       err, KBulkTransferInId0, aTransferId);
   1.270 +				break; //switch(iCaseStep)
   1.271 +				}
   1.272 +				
   1.273 +			// else ok, compare data rcvd now
   1.274 +			TPtrC8 data1(iInTransfer[0]->DataPolled());		
   1.275 +			if(ValidateData(data1, KLiteralEnglish8().Mid(0, KDeviceNumReadBytesPostHalt)) == EFalse)
   1.276 +				{
   1.277 +				err = KErrCompletion; //indicates data validation failure
   1.278 +				msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
   1.279 +				break; //switch(iCaseStep)
   1.280 +				}
   1.281 +			
   1.282 +			// Comparison is a match
   1.283 +			RDebug::Printf("Comparison for IN transfer is a match");
   1.284 +			iCaseStep = EPassed;
   1.285 +			TTestCasePassed request;
   1.286 +			iControlEp0->SendRequest(request,this);
   1.287 +			iTransferComplete = 0; //reset
   1.288 +			}
   1.289 +			break; //switch(iCaseStep)
   1.290 +
   1.291 +		default:
   1.292 +			err = KUndefinedStep;
   1.293 +			msg.Format(_L("<Error %d> Undefined case step %d reached"),KUndefinedStep, iCaseStep);
   1.294 +			break; //switch(iCaseStep)
   1.295 +		}
   1.296 +	
   1.297 +	if(err == KErrCompletion)
   1.298 +		//indicates data validation failure
   1.299 +		{
   1.300 +		msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
   1.301 +		}
   1.302 +	
   1.303 +	if(err!=KErrNone)
   1.304 +		{	
   1.305 +		RDebug::Print(msg);
   1.306 +		iCaseStep = EFailed;
   1.307 +		TTestCaseFailed request(err,msg);
   1.308 +		return iControlEp0->SendRequest(request,this);
   1.309 +		}	
   1.310 +	}
   1.311 +	
   1.312 +void CUT_PBASE_T_USBDI_0499::DeviceInsertedL(TUint aDeviceHandle)
   1.313 +	{
   1.314 +	LOG_FUNC
   1.315 +	
   1.316 +	Cancel();
   1.317 +	
   1.318 +	if(BaseBulkDeviceInsertedL(aDeviceHandle) == EDeviceConfigurationError)
   1.319 +		// Prepare for response from control transfer to client
   1.320 +		{
   1.321 +		iCaseStep = EFailed;
   1.322 +		}
   1.323 +	
   1.324 +	// Create the bulk transfers	
   1.325 +	iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxINTransferSize,*this,KBulkTransferInId0);
   1.326 +	iOutTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxOUTTransferSize,*this,KBulkTransferOutId0);
   1.327 +	iOutTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxOUTTransferSize,*this,KBulkTransferOutId1);
   1.328 +	
   1.329 +	// Initialise the descriptors for transfer		
   1.330 +	RDebug::Printf("Initialising the transfer descriptors");
   1.331 +	TInt err = iUsbInterface1.InitialiseTransferDescriptors();
   1.332 +	if(err != KErrNone)
   1.333 +		{
   1.334 +		TBuf<256> msg;
   1.335 +		msg.Format(_L("<Error %d> Unable to initialise transfer descriptors"),err);
   1.336 +		RDebug::Print(msg);
   1.337 +		iCaseStep = EFailed;
   1.338 +		TTestCaseFailed request(err,msg);
   1.339 +		iControlEp0->SendRequest(request,this);
   1.340 +		return;
   1.341 +		}
   1.342 +
   1.343 +	iCaseStep = ETransferOutHalt;	
   1.344 +	TEndpointReadAndHaltRequest request(1,1,KDeviceNumReadBytesPreHalt);// EP1 means endpoint index 1 not the actual endpoint number
   1.345 +	iControlEp0->SendRequest(request,this);
   1.346 +	}
   1.347 +	
   1.348 +	} //end namespace