os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/testcase0682.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_otgdi/src/testcase0682.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,422 @@
     1.4 +// Copyright (c) 2007-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 +// @internalComponent
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +#include <e32std.h>
    1.22 +#include <e32std_private.h>
    1.23 +#include <u32std.h> 	// unicode builds
    1.24 +#include <e32base.h>
    1.25 +#include <e32base_private.h>
    1.26 +#include <e32Test.h>	// RTest headder
    1.27 +#include "testcaseroot.h"
    1.28 +#include "b2bwatchers.h"
    1.29 +#include "testcase0682.h"
    1.30 +
    1.31 +#define _REPEATS (oOpenIterations*3)
    1.32 +
    1.33 +/* **************************************************************************************
    1.34 + * the name below is used to add a pointer to our construction method to a pointer MAP in 
    1.35 + * the class factory
    1.36 + */
    1.37 +_LIT(KTestCaseId,"PBASE-USB_OTGDI-0682");
    1.38 +const TTestCaseFactoryReceipt<CTestCase0682> CTestCase0682::iFactoryReceipt(KTestCaseId);	
    1.39 +
    1.40 +CTestCase0682* CTestCase0682::NewL(TBool aHost)
    1.41 +	{
    1.42 +	LOG_FUNC
    1.43 +	CTestCase0682* self = new (ELeave) CTestCase0682(aHost);
    1.44 +	CleanupStack::PushL(self);
    1.45 +	self->ConstructL();
    1.46 +	CleanupStack::Pop(self);
    1.47 +	return self;
    1.48 +	}
    1.49 +	
    1.50 +
    1.51 +CTestCase0682::CTestCase0682(TBool aHost)
    1.52 +: 	CTestCaseB2BRoot(KTestCaseId, aHost, iStatus),
    1.53 +	iFirstRoleSwap(ETrue)
    1.54 +	{
    1.55 +	LOG_FUNC
    1.56 +		
    1.57 +	} 
    1.58 +
    1.59 +
    1.60 +/**
    1.61 + ConstructL
    1.62 +*/
    1.63 +void CTestCase0682::ConstructL()
    1.64 +	{
    1.65 +	LOG_FUNC
    1.66 +
    1.67 +	iDualRoleCase = ETrue;	// another back-back
    1.68 +		
    1.69 +	BaseConstructL();
    1.70 +	}
    1.71 +
    1.72 +
    1.73 +CTestCase0682::~CTestCase0682()
    1.74 +	{
    1.75 +	LOG_FUNC
    1.76 +	iCollector.DestroyObservers();
    1.77 +	Cancel();
    1.78 +	}
    1.79 +
    1.80 +
    1.81 +void CTestCase0682::ExecuteTestCaseL()
    1.82 +	{
    1.83 +	LOG_FUNC
    1.84 +	iCaseStep = EPreconditions;
    1.85 +	iHNPCounter = 3;	//	To be decremented to govern the number of times we do HNP.
    1.86 +	CActiveScheduler::Add(this);
    1.87 +	SelfComplete();
    1.88 +	}
    1.89 +
    1.90 +	
    1.91 +void CTestCase0682::DoCancel()
    1.92 +	{
    1.93 +	LOG_FUNC
    1.94 +	// cancel our timer
    1.95 +	iTimer.Cancel();
    1.96 +	}
    1.97 +
    1.98 +void CTestCase0682::StepB2BPreconditions()
    1.99 +	{
   1.100 +	// prompt to insert connector and activate A-end first...
   1.101 +	if (gTestRoleMaster)
   1.102 +		{ // "B" device
   1.103 +		test.Printf(_L("***** Important note *****\n"));
   1.104 +		test.Printf(_L("Before commencing test, please\n"));
   1.105 +		test.Printf(_L("insert 'B'-cable end and activate\n"));
   1.106 +		test.Printf(_L("the test on the 'A' device.\n"));
   1.107 +		test.Printf(_L("Then, press any key to continue.\n"));
   1.108 +		test.Printf(_L("**************************\n"));
   1.109 +		}
   1.110 +	else
   1.111 +		{
   1.112 +		test.Printf(KInsertACablePrompt);
   1.113 +		test.Printf(KPressAnyKeyToContinue);
   1.114 +		}
   1.115 +
   1.116 +	RequestCharacter();	
   1.117 +	}
   1.118 +
   1.119 +// handle event completion	
   1.120 +void CTestCase0682::RunStepL()
   1.121 +	{
   1.122 +	LOG_FUNC
   1.123 +	// Obtain the completion code for this CActive obj.
   1.124 +	TInt completionCode(iStatus.Int()); 
   1.125 +	TBuf<MAX_DSTRLEN> aDescription;
   1.126 +	TInt err(0);
   1.127 +
   1.128 +	switch(iCaseStep)
   1.129 +		{
   1.130 +		case EPreconditions:
   1.131 +			{
   1.132 +			LOG_STEPNAME(_L("EPreconditions"))
   1.133 +			iCaseStep = ELoadLdd;
   1.134 +			StepB2BPreconditions();				
   1.135 +			break;
   1.136 +			}
   1.137 +			
   1.138 +			// 1. Load the Client LDD 
   1.139 +		case ELoadLdd:
   1.140 +			{
   1.141 +			LOG_STEPNAME(_L("ELoadLdd"))
   1.142 +			if (!StepLoadClient(0xF682/*use default settings for SRP/HNP support*/))
   1.143 +				{
   1.144 +				return TestFailed(KErrAbort, _L("Client Load Failure"));
   1.145 +				}
   1.146 +			//  load OTG ldd and init.		
   1.147 +			if (!StepLoadLDD())
   1.148 +				{
   1.149 +				return TestFailed(KErrAbort, _L("OTG Load Failure"));
   1.150 +				}
   1.151 +				
   1.152 +			if(otgActivateFdfActor()!=KErrNone)
   1.153 +				{
   1.154 +				return TestFailed(KErrAbort, _L("Couldn't load FDF Actor"));
   1.155 +				}
   1.156 +			
   1.157 +			// test that the right cable is in
   1.158 +			CheckRoleConnections();
   1.159 +					
   1.160 +			// subscribe to OTG states,events and messages now that it has loaded OK
   1.161 +			TRAPD(result, iCollector.CreateObserversL(*this));
   1.162 +			if (KErrNone != result)
   1.163 +				return(TestFailed(KErrNoMemory, _L("Unable to create observers")));
   1.164 +			iCollector.ClearAllEvents();
   1.165 +			iCaseStep = EPerformSrp;
   1.166 +			SelfComplete();
   1.167 +			break;
   1.168 +			}
   1.169 +			
   1.170 +		case EPerformSrp:
   1.171 +			{
   1.172 +			test.Printf(_L("Into EPerformSrp step...\n"));
   1.173 +
   1.174 +			if (gTestRoleMaster)
   1.175 +				{
   1.176 +				// Trigger SRP
   1.177 +				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
   1.178 +				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpInitiated);
   1.179 +				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);	
   1.180 +				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBPeripheral);
   1.181 +
   1.182 +				err = otgBusRequest();	//	Should generate SRP
   1.183 +				if (KErrNone != err)
   1.184 +					{
   1.185 +					return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
   1.186 +					}
   1.187 +
   1.188 +				iCaseStep = EDefaultRoles;
   1.189 +				}
   1.190 +			else
   1.191 +				{ // slave "A"
   1.192 +				// Wait for SRP
   1.193 +				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpReceived);
   1.194 +				
   1.195 +				iCaseStep = EAReceivedSrp;
   1.196 +				}
   1.197 +			const TInt KTestCase0681Timeout = 30000;			//	30 seconds, should be plenty of time for 3 role swaps
   1.198 +			iCollector.AddStepTimeout(KTestCase0681Timeout);
   1.199 +			SetActive();
   1.200 +			break;
   1.201 +			}
   1.202 +
   1.203 +		case EAReceivedSrp:		//	A-Device step only!
   1.204 +			{
   1.205 +			test.Printf(_L("Into EAReceivedSrp step...\n"));
   1.206 +
   1.207 +			if (KTestCaseWatchdogTO == iStatus.Int())
   1.208 +				{
   1.209 +				iCollector.DestroyObservers();
   1.210 +				return TestFailed(KErrAbort, _L("Timeout"));
   1.211 +				}			
   1.212 +
   1.213 +			//	We've received SRP. In this test, we use BusRequest to raise VBus but not cede the host role
   1.214 +			//	before we've enumerated and configured the device.
   1.215 +
   1.216 +			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);
   1.217 +			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToHost);
   1.218 +			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAHost);
   1.219 +
   1.220 +			err = otgBusRequest();
   1.221 +			if (KErrNone != err)
   1.222 +				{
   1.223 +				return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
   1.224 +				}
   1.225 +			
   1.226 +			iCaseStep = EDefaultRoles;
   1.227 +			SetActive();
   1.228 +			break;
   1.229 +			}
   1.230 +			
   1.231 +		case EDefaultRoles:
   1.232 +			{
   1.233 +			test.Printf(_L("Into EDefaultRoles step...\n"));
   1.234 +			
   1.235 +			if ( --iHNPCounter >= 0)
   1.236 +				{
   1.237 +				//	We want to do further role swapping
   1.238 +				if (gTestRoleMaster)
   1.239 +					{
   1.240 +					//	B-Device should now wait until it is configured
   1.241 +					iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
   1.242 +					iCaseStep = EBConfigured;
   1.243 +					}
   1.244 +				else
   1.245 +					{
   1.246 +					//	A-Device should expect nothing more until it becomes A-Peripheral
   1.247 +					iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAPeripheral);
   1.248 +					iCaseStep = ESwappedRoles;
   1.249 +					}
   1.250 +				SetActive();
   1.251 +				}
   1.252 +			else
   1.253 +				{
   1.254 +				//	We've done 3 x HNP cycles back to default roles. 
   1.255 +				//	Time to shut down VBus and stop the test case.
   1.256 +				iCaseStep = EDropVBus;
   1.257 +				SelfComplete();
   1.258 +				}
   1.259 +			break;
   1.260 +			}
   1.261 +
   1.262 +			
   1.263 +		case EBConfigured:	//	A B-Device only step!
   1.264 +			{
   1.265 +			test.Printf(_L("Into EBConfigured step...\n"));
   1.266 +			if (KTestCaseWatchdogTO == iStatus.Int())
   1.267 +				{
   1.268 +				iCollector.DestroyObservers();
   1.269 +				return TestFailed(KErrAbort, _L("Timeout"));
   1.270 +				}
   1.271 +			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
   1.272 +			iCaseStep = EBSuspended;
   1.273 +			SetActive();
   1.274 +			break;
   1.275 +			}
   1.276 +			
   1.277 +		case EBSuspended:	
   1.278 +			{
   1.279 +			test.Printf(_L("Into EBSuspended step...\n"));
   1.280 +			if (KTestCaseWatchdogTO == iStatus.Int())
   1.281 +				{
   1.282 +				iCollector.DestroyObservers();
   1.283 +				return TestFailed(KErrAbort, _L("Timeout"));
   1.284 +				}			
   1.285 +			
   1.286 +			// issue HNP
   1.287 +			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBHost);
   1.288 +
   1.289 +			//	The first time we arrive here, it should be unnecessary for the test code
   1.290 +			//	to issue a further bus request - it should "remember" that it earlier 
   1.291 +			//	requested the host role, triggering SRP to be followed (hopefully now) by HNP
   1.292 +			if(!iFirstRoleSwap)
   1.293 +				{
   1.294 +				err = otgBusRequest();	//	Request the host role
   1.295 +				if (KErrNone != err)
   1.296 +					{
   1.297 +					test.Printf(_L("BusRequest returned %d\n"),err);
   1.298 +					return TestFailed(KErrAbort, _L("BusRequest() failed!"));
   1.299 +					}
   1.300 +				}
   1.301 +			else
   1.302 +				{
   1.303 +				//	So that we know next time around that we need to perform a bus request
   1.304 +				iFirstRoleSwap = EFalse;
   1.305 +				}
   1.306 +		
   1.307 +			iCaseStep = ESwappedRoles;
   1.308 +			SetActive();
   1.309 +			break;
   1.310 +			}
   1.311 +			
   1.312 +		case ESwappedRoles:
   1.313 +			{
   1.314 +			test.Printf(_L("Into ESwappedRoles step...\n"));
   1.315 +			if (KTestCaseWatchdogTO == iStatus.Int())
   1.316 +				{
   1.317 +				iCollector.DestroyObservers();
   1.318 +				return TestFailed(KErrAbort, _L("Timeout"));
   1.319 +				}			
   1.320 +
   1.321 +			if (gTestRoleMaster)
   1.322 +				{
   1.323 +				//	B-Device should now wait until it is back to B-Peripheral
   1.324 +				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBPeripheral);
   1.325 +				iCaseStep = EDefaultRoles;
   1.326 +				}
   1.327 +			else
   1.328 +				{
   1.329 +				//	A-Device should wait to become a configured A-Peripheral
   1.330 +				iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
   1.331 +				iCaseStep = EAConfigured;
   1.332 +				}
   1.333 +			SetActive();
   1.334 +			break;
   1.335 +			}
   1.336 +			
   1.337 +		case EAConfigured:	//	A-Device only step
   1.338 +			{
   1.339 +			test.Printf(_L("Into EWaitTillAConfigured step...\n"));
   1.340 +			if (KTestCaseWatchdogTO == iStatus.Int())
   1.341 +				{
   1.342 +				iCollector.DestroyObservers();
   1.343 +				return TestFailed(KErrAbort, _L("Timeout"));
   1.344 +				}			
   1.345 +
   1.346 +			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
   1.347 +			iCaseStep = EASuspended;
   1.348 +			SetActive();
   1.349 +			break;
   1.350 +			}
   1.351 +		
   1.352 +		case EASuspended:	//	A-Device only step
   1.353 +			{
   1.354 +			test.Printf(_L("Into EWaitTillASuspended step...\n"));
   1.355 +			if (KTestCaseWatchdogTO == iStatus.Int())
   1.356 +				{
   1.357 +				iCollector.DestroyObservers();
   1.358 +				return TestFailed(KErrAbort, _L("Timeout"));
   1.359 +				}			
   1.360 +			
   1.361 +			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAHost);	//	Swapping back to default role
   1.362 +
   1.363 +			iCaseStep = EDefaultRoles;
   1.364 +			SetActive();
   1.365 +			break;
   1.366 +			}
   1.367 +
   1.368 +		case EDropVBus:
   1.369 +			LOG_STEPNAME(_L("EDropVBus"))
   1.370 +
   1.371 +			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusDropped);
   1.372 +			if ( gTestRoleMaster)
   1.373 +				{
   1.374 +				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
   1.375 +				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToDevice);
   1.376 +				}
   1.377 +			else
   1.378 +				{ // SLAVE "A"
   1.379 +				otgBusDrop();
   1.380 +				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAIdle);
   1.381 +				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToHost);
   1.382 +				}
   1.383 +			iCaseStep = EVBusDropped;
   1.384 +			SetActive();
   1.385 +			break;
   1.386 +			
   1.387 +		case EVBusDropped:
   1.388 +			LOG_STEPNAME(_L("ELoopVerifyDrop"))
   1.389 +			if (KTestCaseWatchdogTO == iStatus.Int())
   1.390 +				{
   1.391 +				return TestFailed(KErrAbort, _L("Timeout"));
   1.392 +				}
   1.393 +				
   1.394 +			if (otgVbusPresent())
   1.395 +				{
   1.396 +				return TestFailed(KErrAbort, _L("Vbus did not drop - FAILED!"));
   1.397 +				}
   1.398 +			iCaseStep = EUnloadLdd;
   1.399 +			SelfComplete();
   1.400 +			break;
   1.401 +			
   1.402 +		case EUnloadLdd:
   1.403 +			LOG_STEPNAME(_L("EUnloadLdd"))
   1.404 +			otgDeactivateFdfActor();
   1.405 +			iCollector.DestroyObservers();
   1.406 +			if (EFalse == StepUnloadLDD())
   1.407 +				return TestFailed(KErrAbort,_L("unload Ldd failure"));	
   1.408 +			if (!StepUnloadClient())
   1.409 +				return TestFailed(KErrAbort,_L("Client Unload Failure"));	
   1.410 +
   1.411 +			iCaseStep = ELastStep;
   1.412 +			SelfComplete();
   1.413 +			break;
   1.414 +			
   1.415 +		case ELastStep:
   1.416 +			LOG_STEPNAME(_L("ELastStep"))
   1.417 +			TestPassed();
   1.418 +			break;
   1.419 +			
   1.420 +		default:
   1.421 +			test.Printf(_L("<Error> unknown test step"));
   1.422 +			Cancel();
   1.423 +			return (TestFailed(KErrCorrupt, _L("<Error> unknown test step")));
   1.424 +		}
   1.425 +	}