sl@0: // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of the License "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: // @internalComponent
sl@0: // 
sl@0: //
sl@0: 
sl@0: #include <e32std.h>
sl@0: #include <e32std_private.h>
sl@0: #include <u32std.h> 	// unicode builds
sl@0: #include <e32base.h>
sl@0: #include <e32base_private.h>
sl@0: #include <e32Test.h>	// RTest headder
sl@0: #include "testcaseroot.h"
sl@0: #include "b2bwatchers.h"
sl@0: #include "testcase0682.h"
sl@0: 
sl@0: #define _REPEATS (oOpenIterations*3)
sl@0: 
sl@0: /* **************************************************************************************
sl@0:  * the name below is used to add a pointer to our construction method to a pointer MAP in 
sl@0:  * the class factory
sl@0:  */
sl@0: _LIT(KTestCaseId,"PBASE-USB_OTGDI-0682");
sl@0: const TTestCaseFactoryReceipt<CTestCase0682> CTestCase0682::iFactoryReceipt(KTestCaseId);	
sl@0: 
sl@0: CTestCase0682* CTestCase0682::NewL(TBool aHost)
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	CTestCase0682* self = new (ELeave) CTestCase0682(aHost);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL();
sl@0: 	CleanupStack::Pop(self);
sl@0: 	return self;
sl@0: 	}
sl@0: 	
sl@0: 
sl@0: CTestCase0682::CTestCase0682(TBool aHost)
sl@0: : 	CTestCaseB2BRoot(KTestCaseId, aHost, iStatus),
sl@0: 	iFirstRoleSwap(ETrue)
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 		
sl@0: 	} 
sl@0: 
sl@0: 
sl@0: /**
sl@0:  ConstructL
sl@0: */
sl@0: void CTestCase0682::ConstructL()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 
sl@0: 	iDualRoleCase = ETrue;	// another back-back
sl@0: 		
sl@0: 	BaseConstructL();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: CTestCase0682::~CTestCase0682()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	iCollector.DestroyObservers();
sl@0: 	Cancel();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CTestCase0682::ExecuteTestCaseL()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	iCaseStep = EPreconditions;
sl@0: 	iHNPCounter = 3;	//	To be decremented to govern the number of times we do HNP.
sl@0: 	CActiveScheduler::Add(this);
sl@0: 	SelfComplete();
sl@0: 	}
sl@0: 
sl@0: 	
sl@0: void CTestCase0682::DoCancel()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	// cancel our timer
sl@0: 	iTimer.Cancel();
sl@0: 	}
sl@0: 
sl@0: void CTestCase0682::StepB2BPreconditions()
sl@0: 	{
sl@0: 	// prompt to insert connector and activate A-end first...
sl@0: 	if (gTestRoleMaster)
sl@0: 		{ // "B" device
sl@0: 		test.Printf(_L("***** Important note *****\n"));
sl@0: 		test.Printf(_L("Before commencing test, please\n"));
sl@0: 		test.Printf(_L("insert 'B'-cable end and activate\n"));
sl@0: 		test.Printf(_L("the test on the 'A' device.\n"));
sl@0: 		test.Printf(_L("Then, press any key to continue.\n"));
sl@0: 		test.Printf(_L("**************************\n"));
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		test.Printf(KInsertACablePrompt);
sl@0: 		test.Printf(KPressAnyKeyToContinue);
sl@0: 		}
sl@0: 
sl@0: 	RequestCharacter();	
sl@0: 	}
sl@0: 
sl@0: // handle event completion	
sl@0: void CTestCase0682::RunStepL()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	// Obtain the completion code for this CActive obj.
sl@0: 	TInt completionCode(iStatus.Int()); 
sl@0: 	TBuf<MAX_DSTRLEN> aDescription;
sl@0: 	TInt err(0);
sl@0: 
sl@0: 	switch(iCaseStep)
sl@0: 		{
sl@0: 		case EPreconditions:
sl@0: 			{
sl@0: 			LOG_STEPNAME(_L("EPreconditions"))
sl@0: 			iCaseStep = ELoadLdd;
sl@0: 			StepB2BPreconditions();				
sl@0: 			break;
sl@0: 			}
sl@0: 			
sl@0: 			// 1. Load the Client LDD 
sl@0: 		case ELoadLdd:
sl@0: 			{
sl@0: 			LOG_STEPNAME(_L("ELoadLdd"))
sl@0: 			if (!StepLoadClient(0xF682/*use default settings for SRP/HNP support*/))
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("Client Load Failure"));
sl@0: 				}
sl@0: 			//  load OTG ldd and init.		
sl@0: 			if (!StepLoadLDD())
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("OTG Load Failure"));
sl@0: 				}
sl@0: 				
sl@0: 			if(otgActivateFdfActor()!=KErrNone)
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("Couldn't load FDF Actor"));
sl@0: 				}
sl@0: 			
sl@0: 			// test that the right cable is in
sl@0: 			CheckRoleConnections();
sl@0: 					
sl@0: 			// subscribe to OTG states,events and messages now that it has loaded OK
sl@0: 			TRAPD(result, iCollector.CreateObserversL(*this));
sl@0: 			if (KErrNone != result)
sl@0: 				return(TestFailed(KErrNoMemory, _L("Unable to create observers")));
sl@0: 			iCollector.ClearAllEvents();
sl@0: 			iCaseStep = EPerformSrp;
sl@0: 			SelfComplete();
sl@0: 			break;
sl@0: 			}
sl@0: 			
sl@0: 		case EPerformSrp:
sl@0: 			{
sl@0: 			test.Printf(_L("Into EPerformSrp step...\n"));
sl@0: 
sl@0: 			if (gTestRoleMaster)
sl@0: 				{
sl@0: 				// Trigger SRP
sl@0: 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
sl@0: 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpInitiated);
sl@0: 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);	
sl@0: 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBPeripheral);
sl@0: 
sl@0: 				err = otgBusRequest();	//	Should generate SRP
sl@0: 				if (KErrNone != err)
sl@0: 					{
sl@0: 					return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
sl@0: 					}
sl@0: 
sl@0: 				iCaseStep = EDefaultRoles;
sl@0: 				}
sl@0: 			else
sl@0: 				{ // slave "A"
sl@0: 				// Wait for SRP
sl@0: 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpReceived);
sl@0: 				
sl@0: 				iCaseStep = EAReceivedSrp;
sl@0: 				}
sl@0: 			const TInt KTestCase0681Timeout = 30000;			//	30 seconds, should be plenty of time for 3 role swaps
sl@0: 			iCollector.AddStepTimeout(KTestCase0681Timeout);
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			}
sl@0: 
sl@0: 		case EAReceivedSrp:		//	A-Device step only!
sl@0: 			{
sl@0: 			test.Printf(_L("Into EAReceivedSrp step...\n"));
sl@0: 
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				iCollector.DestroyObservers();
sl@0: 				return TestFailed(KErrAbort, _L("Timeout"));
sl@0: 				}			
sl@0: 
sl@0: 			//	We've received SRP. In this test, we use BusRequest to raise VBus but not cede the host role
sl@0: 			//	before we've enumerated and configured the device.
sl@0: 
sl@0: 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);
sl@0: 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToHost);
sl@0: 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAHost);
sl@0: 
sl@0: 			err = otgBusRequest();
sl@0: 			if (KErrNone != err)
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
sl@0: 				}
sl@0: 			
sl@0: 			iCaseStep = EDefaultRoles;
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			}
sl@0: 			
sl@0: 		case EDefaultRoles:
sl@0: 			{
sl@0: 			test.Printf(_L("Into EDefaultRoles step...\n"));
sl@0: 			
sl@0: 			if ( --iHNPCounter >= 0)
sl@0: 				{
sl@0: 				//	We want to do further role swapping
sl@0: 				if (gTestRoleMaster)
sl@0: 					{
sl@0: 					//	B-Device should now wait until it is configured
sl@0: 					iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
sl@0: 					iCaseStep = EBConfigured;
sl@0: 					}
sl@0: 				else
sl@0: 					{
sl@0: 					//	A-Device should expect nothing more until it becomes A-Peripheral
sl@0: 					iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAPeripheral);
sl@0: 					iCaseStep = ESwappedRoles;
sl@0: 					}
sl@0: 				SetActive();
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				//	We've done 3 x HNP cycles back to default roles. 
sl@0: 				//	Time to shut down VBus and stop the test case.
sl@0: 				iCaseStep = EDropVBus;
sl@0: 				SelfComplete();
sl@0: 				}
sl@0: 			break;
sl@0: 			}
sl@0: 
sl@0: 			
sl@0: 		case EBConfigured:	//	A B-Device only step!
sl@0: 			{
sl@0: 			test.Printf(_L("Into EBConfigured step...\n"));
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				iCollector.DestroyObservers();
sl@0: 				return TestFailed(KErrAbort, _L("Timeout"));
sl@0: 				}
sl@0: 			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
sl@0: 			iCaseStep = EBSuspended;
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			}
sl@0: 			
sl@0: 		case EBSuspended:	
sl@0: 			{
sl@0: 			test.Printf(_L("Into EBSuspended step...\n"));
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				iCollector.DestroyObservers();
sl@0: 				return TestFailed(KErrAbort, _L("Timeout"));
sl@0: 				}			
sl@0: 			
sl@0: 			// issue HNP
sl@0: 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBHost);
sl@0: 
sl@0: 			//	The first time we arrive here, it should be unnecessary for the test code
sl@0: 			//	to issue a further bus request - it should "remember" that it earlier 
sl@0: 			//	requested the host role, triggering SRP to be followed (hopefully now) by HNP
sl@0: 			if(!iFirstRoleSwap)
sl@0: 				{
sl@0: 				err = otgBusRequest();	//	Request the host role
sl@0: 				if (KErrNone != err)
sl@0: 					{
sl@0: 					test.Printf(_L("BusRequest returned %d\n"),err);
sl@0: 					return TestFailed(KErrAbort, _L("BusRequest() failed!"));
sl@0: 					}
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				//	So that we know next time around that we need to perform a bus request
sl@0: 				iFirstRoleSwap = EFalse;
sl@0: 				}
sl@0: 		
sl@0: 			iCaseStep = ESwappedRoles;
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			}
sl@0: 			
sl@0: 		case ESwappedRoles:
sl@0: 			{
sl@0: 			test.Printf(_L("Into ESwappedRoles step...\n"));
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				iCollector.DestroyObservers();
sl@0: 				return TestFailed(KErrAbort, _L("Timeout"));
sl@0: 				}			
sl@0: 
sl@0: 			if (gTestRoleMaster)
sl@0: 				{
sl@0: 				//	B-Device should now wait until it is back to B-Peripheral
sl@0: 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBPeripheral);
sl@0: 				iCaseStep = EDefaultRoles;
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				//	A-Device should wait to become a configured A-Peripheral
sl@0: 				iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
sl@0: 				iCaseStep = EAConfigured;
sl@0: 				}
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			}
sl@0: 			
sl@0: 		case EAConfigured:	//	A-Device only step
sl@0: 			{
sl@0: 			test.Printf(_L("Into EWaitTillAConfigured step...\n"));
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				iCollector.DestroyObservers();
sl@0: 				return TestFailed(KErrAbort, _L("Timeout"));
sl@0: 				}			
sl@0: 
sl@0: 			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
sl@0: 			iCaseStep = EASuspended;
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			}
sl@0: 		
sl@0: 		case EASuspended:	//	A-Device only step
sl@0: 			{
sl@0: 			test.Printf(_L("Into EWaitTillASuspended step...\n"));
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				iCollector.DestroyObservers();
sl@0: 				return TestFailed(KErrAbort, _L("Timeout"));
sl@0: 				}			
sl@0: 			
sl@0: 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAHost);	//	Swapping back to default role
sl@0: 
sl@0: 			iCaseStep = EDefaultRoles;
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			}
sl@0: 
sl@0: 		case EDropVBus:
sl@0: 			LOG_STEPNAME(_L("EDropVBus"))
sl@0: 
sl@0: 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusDropped);
sl@0: 			if ( gTestRoleMaster)
sl@0: 				{
sl@0: 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
sl@0: 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToDevice);
sl@0: 				}
sl@0: 			else
sl@0: 				{ // SLAVE "A"
sl@0: 				otgBusDrop();
sl@0: 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAIdle);
sl@0: 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToHost);
sl@0: 				}
sl@0: 			iCaseStep = EVBusDropped;
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			
sl@0: 		case EVBusDropped:
sl@0: 			LOG_STEPNAME(_L("ELoopVerifyDrop"))
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("Timeout"));
sl@0: 				}
sl@0: 				
sl@0: 			if (otgVbusPresent())
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("Vbus did not drop - FAILED!"));
sl@0: 				}
sl@0: 			iCaseStep = EUnloadLdd;
sl@0: 			SelfComplete();
sl@0: 			break;
sl@0: 			
sl@0: 		case EUnloadLdd:
sl@0: 			LOG_STEPNAME(_L("EUnloadLdd"))
sl@0: 			otgDeactivateFdfActor();
sl@0: 			iCollector.DestroyObservers();
sl@0: 			if (EFalse == StepUnloadLDD())
sl@0: 				return TestFailed(KErrAbort,_L("unload Ldd failure"));	
sl@0: 			if (!StepUnloadClient())
sl@0: 				return TestFailed(KErrAbort,_L("Client Unload Failure"));	
sl@0: 
sl@0: 			iCaseStep = ELastStep;
sl@0: 			SelfComplete();
sl@0: 			break;
sl@0: 			
sl@0: 		case ELastStep:
sl@0: 			LOG_STEPNAME(_L("ELastStep"))
sl@0: 			TestPassed();
sl@0: 			break;
sl@0: 			
sl@0: 		default:
sl@0: 			test.Printf(_L("<Error> unknown test step"));
sl@0: 			Cancel();
sl@0: 			return (TestFailed(KErrCorrupt, _L("<Error> unknown test step")));
sl@0: 		}
sl@0: 	}