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 "testcasewd.h"
sl@0: #include "testcase0677.h"
sl@0: 
sl@0: #define _REPEATS (oOpenIterations*3)
sl@0: 
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: _LIT(KTestCaseId,"PBASE-USB_OTGDI-0677");
sl@0: const TTestCaseFactoryReceipt<CTestCase0677> CTestCase0677::iFactoryReceipt(KTestCaseId);	
sl@0: 
sl@0: CTestCase0677* CTestCase0677::NewL(TBool aHost)
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	CTestCase0677* self = new (ELeave) CTestCase0677(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: CTestCase0677::CTestCase0677(TBool aHost)
sl@0: :	CTestCaseRoot(KTestCaseId, aHost)
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 		
sl@0: 	} 
sl@0: 
sl@0: 
sl@0: /**
sl@0:  ConstructL
sl@0: */
sl@0: void CTestCase0677::ConstructL()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	iWDTimer = CTestCaseWatchdog::NewL();
sl@0: 	iRepeats = OPEN_REPEATS;
sl@0: 		
sl@0: 	BaseConstructL();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: CTestCase0677::~CTestCase0677()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 
sl@0: 	Cancel();
sl@0: 	delete iWDTimer;
sl@0: 	
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CTestCase0677::ExecuteTestCaseL()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	iCaseStep = EPreconditions;
sl@0: 	
sl@0: 	iRepeats = KOperationRetriesMax;	// VBus event rise retries
sl@0: 	
sl@0: 	CActiveScheduler::Add(this);
sl@0: 	SelfComplete();
sl@0: 
sl@0: 	}
sl@0: 
sl@0: 	
sl@0: void CTestCase0677::DoCancel()
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 
sl@0: 	// cancel our timer
sl@0: 	iTimer.Cancel();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CTestCase0677::CancelKB(CTestCaseRoot *pThis)
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	CTestCase0677 * p = REINTERPRET_CAST(CTestCase0677 *,pThis);
sl@0: 	// cancel any pending call, and then complete our active obj with a cancel value
sl@0: 	p->iConsole->ReadCancel();
sl@0: 
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CTestCase0677::CancelNotify(CTestCaseRoot *pThis)
sl@0: 	{
sl@0: 	LOG_FUNC
sl@0: 	CTestCase0677 * p = REINTERPRET_CAST(CTestCase0677 *,pThis);
sl@0: 	// cancel any pending call, and then complete our active obj with a timeout value
sl@0: 	switch (p->iCancelWhat)
sl@0: 		{
sl@0: 			case ECancelEventNotify:
sl@0: 				p->otgCancelOtgEventRequest();
sl@0: 				break;			
sl@0: 			case ECancelMessageNotify: 
sl@0: 				p->otgCancelOtgMessageRequest();
sl@0: 				break;
sl@0: 			case ECancelVBusNotify:
sl@0: 				p->otgCancelOtgVbusNotification();
sl@0: 				break;	
sl@0: 			default:
sl@0: 				ASSERT(0);
sl@0: 				break;
sl@0: 		}
sl@0: 	p->SelfComplete(KTestCaseWatchdogTO);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: // This test result depends on all the ID detection tests and the VBus driving and dropping tests have not yet passed
sl@0: void CTestCase0677::DescribePreconditions()
sl@0: 	{
sl@0: 	test.Printf(_L("Using OET, connect oscilloscope chan.A to VBus\n"));
sl@0: 	test.Printf(_L("Connect oscilloscope chan.B to D+\n"));
sl@0: 	test.Printf(_L("Trigger once, 200mV, 100ms \n"));
sl@0: 	test.Printf(_L("Prepare to observe VBus, D+ pulse.\n\n"));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CTestCase0677::ContinueAfter(TTimeIntervalMicroSeconds32 aMicroSecs, TCaseSteps aStep)
sl@0: 	{
sl@0: 	LOG_VERBOSE2(_L("Wait %dms before drop VBus"), (TInt)(aMicroSecs.Int()/1000));
sl@0: 	iTimer.After(iStatus, aMicroSecs);
sl@0: 	iCaseStep = aStep;
sl@0: 	SetActive();
sl@0: 	}
sl@0: 
sl@0: 
sl@0: // handle event completion	
sl@0: void CTestCase0677::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: 
sl@0: 	switch(iCaseStep)
sl@0: 		{
sl@0: 		case EPreconditions:
sl@0: 			iCaseStep = ELoadLdd;
sl@0: 			if (iAutomated)
sl@0: 				{
sl@0: 				iCaseStep = ELoadLdd;
sl@0: 				SelfComplete();
sl@0: 				break;
sl@0: 				}
sl@0: 			// prompt to insert connector
sl@0: 			test.Printf(KAttachOETAsBDevice);
sl@0: 			test.Printf(KPressAnyKeyToContinue);
sl@0: 			RequestCharacter();			
sl@0: 			break;
sl@0: 			
sl@0: 			// 1. load the LDD and init.		
sl@0: 		case ELoadLdd:
sl@0: 			if (!StepLoadLDD())
sl@0: 				{
sl@0: 				break;
sl@0: 				}
sl@0: 			iCaseStep = EDetectBPlug;
sl@0: 			SelfComplete();
sl@0: 			break;
sl@0: 
sl@0: 			// 2. detect 'B' plug now
sl@0: 		case EDetectBPlug:
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("User response too slow - FAILED!"));
sl@0: 				}
sl@0: 
sl@0: 			// if doing this test in /AUTO mode, we would fail this now
sl@0: 			// however if we can control ID_PIN through an API in future, we turn in on now.
sl@0: 			if (otgIdPinPresent())
sl@0: 				{
sl@0: 				test.Printf(KRemoveAConnectorPrompt);
sl@0: 				test.Printf(KPressAnyKeyToContinue);
sl@0: 				RequestCharacter();
sl@0: 
sl@0: 				iCaseStep = EDetectBPlug;
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				iCaseStep = ERequestBus;
sl@0: 				SelfComplete();
sl@0: 				}
sl@0: 			break;
sl@0: 						
sl@0: 			// 5. Issue SRP (request VBUS)
sl@0: 		case ERequestBus:
sl@0: 			iWDTimer->Cancel();
sl@0: 
sl@0: 			test.Printf(KMsgWaitingForSRPInitiated);
sl@0: 			otgQueueOtgEventRequest( iOTGEvent, iStatus );
sl@0: 
sl@0: 			// turn on VBus (B-SRP)
sl@0: 			err = otgBusRequest();
sl@0: 			iTimeSRPStart.HomeTime();
sl@0: 			
sl@0: 			if (KErrNone != err)
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("Issue SRP - RUsbOtgDriver::BusRequest() FAILED!"));
sl@0: 				}
sl@0: 			iCaseStep = EWaitForSRPInitiated;
sl@0: 			iCancelWhat = ECancelEventNotify;
sl@0: 			iWDTimer->IssueRequest(32000*2 /*KSpec_TA_SRP_RSPNS +1000*/, this, &CancelNotify);
sl@0: 			SetActive();
sl@0: 			break;
sl@0: 			
sl@0: 			// 6. get SRP initiated event, this is a local indication
sl@0: 		case EWaitForSRPInitiated:			
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("SRP Active indication NOT fired in time - FAILED!"));
sl@0: 				}
sl@0: 			OtgEventString(iOTGEvent, aDescription);
sl@0: 			test.Printf(_L("Received event %d '%S' status(%d)\n"), iOTGEvent, &aDescription, completionCode);
sl@0: 			if (RUsbOtgDriver::EEventSrpInitiated == iOTGEvent)
sl@0: 				{
sl@0: 				// calc interval
sl@0: 				TTimeIntervalMicroSeconds aIntvlMicro;
sl@0: 				TTime aNowTime;
sl@0: 				aNowTime.HomeTime();
sl@0: 				aIntvlMicro = aNowTime.MicroSecondsFrom(iTimeSRPStart);
sl@0: 				LOG_VERBOSE2(_L("SRP active after %d ms\n"), (TInt)(aIntvlMicro.Int64()/1000));
sl@0: 				iCancelWhat = ECancelMessageNotify;
sl@0: 				otgQueueOtgMessageRequest( iOTGMessage, iStatus );
sl@0: 				iCaseStep = EWaitForSRPTimeout;
sl@0: 				test.Printf(KMsgWaitingForSRPTimeout);
sl@0: 				SetActive();
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				iCaseStep = EWaitForSRPInitiated;
sl@0: 				test.Printf(KMsgWaitingForSRPInitiated);
sl@0: 				iStatus = KRequestPending;
sl@0: 				otgQueueOtgEventRequest( iOTGEvent, iStatus );
sl@0: 				SetActive();
sl@0: 				}
sl@0: 			break;
sl@0: 				
sl@0: 			// 6. get SRP timeout event, this is a local indication
sl@0: 		case EWaitForSRPTimeout:			
sl@0: 			if (KTestCaseWatchdogTO == iStatus.Int())
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("SRP T/O NOT fired in time - FAILED!"));
sl@0: 				}
sl@0: 			OtgMessageString(iOTGMessage, aDescription);
sl@0: 			test.Printf(_L("Received message %d '%S' status(%d)\n"), iOTGMessage, &aDescription, completionCode);
sl@0: 			if (RUsbOtgDriver::EMessageSrpTimeout == iOTGMessage)
sl@0: 				{
sl@0: 				iWDTimer->Cancel();		//	Only cancel WD Timer here, when timed portion of test is over
sl@0: 				// calc interval
sl@0: 				TTimeIntervalMicroSeconds aIntvlMicro;
sl@0: 				TTime aNowTime;
sl@0: 				aNowTime.HomeTime();
sl@0: 				aIntvlMicro = aNowTime.MicroSecondsFrom(iTimeSRPStart);
sl@0: 				LOG_VERBOSE2(_L("SRP times out after %d ms\n"), (TInt)(aIntvlMicro.Int64()/1000));
sl@0: 				// the correct value is 32 seconds, not 4.9 seconds as per the spec.
sl@0: 
sl@0: 				iCaseStep = EIssueSRPObservedPrompt;
sl@0: 				SelfComplete();
sl@0: 				}
sl@0: 			else
sl@0: 				{
sl@0: 				iCaseStep = EWaitForSRPTimeout;
sl@0: 				test.Printf(KMsgWaitingForSRPTimeout);
sl@0: 				iStatus = KRequestPending;
sl@0: 				otgQueueOtgMessageRequest( iOTGMessage, iStatus );
sl@0: 				SetActive();
sl@0: 				}
sl@0: 			break;
sl@0: 				
sl@0: 		case EIssueSRPObservedPrompt:
sl@0: 			{
sl@0: 			test.Printf(_L("\nPress Y to verify that SRP was observed\n"));
sl@0: 			test.Printf(_L("or any other key to fail test.\n"));
sl@0: 			RequestCharacter();
sl@0: 			iCaseStep = ECheckSRPObservedUserInput;
sl@0: 			break;
sl@0: 			}
sl@0: 			
sl@0: 							
sl@0: 		case ECheckSRPObservedUserInput:
sl@0: 			{
sl@0: 			if (('y' != iKeyCodeInput) && ('Y' != iKeyCodeInput))
sl@0: 				{
sl@0: 				return TestFailed(KErrAbort, _L("SRP NOT observed - FAILED!"));
sl@0: 				}
sl@0: 			iCaseStep = EUnloadLdd;
sl@0: 			SelfComplete();
sl@0: 			break;
sl@0: 			}
sl@0: 							
sl@0: 			// 12. unload OTG
sl@0: 		case EUnloadLdd:
sl@0: 			if (EFalse == StepUnloadLDD())
sl@0: 				return TestFailed(KErrAbort,_L("Unload Ldd failure"));	
sl@0: 			
sl@0: 			iCaseStep = ELastStep;
sl@0: 			SelfComplete();
sl@0: 			break;
sl@0: 			
sl@0: 		case ELastStep:
sl@0: 			TestPassed();
sl@0: 			break;
sl@0: 			
sl@0: 		default:
sl@0: 			test.Printf(_L("<Error> unknown test step\n"));
sl@0: 			Cancel();
sl@0: 			return (TestFailed(KErrCorrupt, _L("<Error> unknown test step")));
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: