os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/testcase0681.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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 // @internalComponent
    15 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include <e32std_private.h>
    20 #include <u32std.h> 	// unicode builds
    21 #include <e32base.h>
    22 #include <e32base_private.h>
    23 #include <e32Test.h>	// RTest headder
    24 #include "testcaseroot.h"
    25 #include "b2bwatchers.h"
    26 #include "testcase0681.h"
    27 
    28 #define _REPEATS (oOpenIterations*3)
    29 
    30 
    31 /* **************************************************************************************
    32  * the name below is used to add a pointer to our construction method to a pointer MAP in 
    33  * the class factory
    34  */
    35 _LIT(KTestCaseId,"PBASE-USB_OTGDI-0681");
    36 const TTestCaseFactoryReceipt<CTestCase0681> CTestCase0681::iFactoryReceipt(KTestCaseId);	
    37 
    38 CTestCase0681* CTestCase0681::NewL(TBool aHost)
    39 	{
    40 	LOG_FUNC
    41 	CTestCase0681* self = new (ELeave) CTestCase0681(aHost);
    42 	CleanupStack::PushL(self);
    43 	self->ConstructL();
    44 	CleanupStack::Pop(self);
    45 	return self;
    46 	}
    47 	
    48 
    49 CTestCase0681::CTestCase0681(TBool aHost)
    50 	: CTestCaseB2BRoot(KTestCaseId, aHost, iStatus) 
    51 	{
    52 	LOG_FUNC
    53 		
    54 	} 
    55 
    56 
    57 /**
    58  ConstructL
    59 */
    60 void CTestCase0681::ConstructL()
    61 	{
    62 	LOG_FUNC
    63 
    64 	iDualRoleCase = ETrue;	// another back-back
    65 		
    66 	BaseConstructL();
    67 	}
    68 
    69 
    70 CTestCase0681::~CTestCase0681()
    71 	{
    72 	LOG_FUNC
    73 	iCollector.DestroyObservers();
    74 	Cancel();
    75 	}
    76 
    77 
    78 void CTestCase0681::ExecuteTestCaseL()
    79 	{
    80 	LOG_FUNC
    81 	iCaseStep = EPreconditions;
    82 	iHNPCounter = 3;	//	To be decremented to govern the number of times we do HNP.
    83 	CActiveScheduler::Add(this);
    84 	SelfComplete();
    85 	}
    86 
    87 	
    88 void CTestCase0681::DoCancel()
    89 	{
    90 	LOG_FUNC
    91 	// cancel our timer
    92 	iTimer.Cancel();
    93 	}
    94 
    95 void CTestCase0681::StepB2BPreconditions()
    96 	{
    97 	// prompt to insert connector and activate A-end first...
    98 	if (gTestRoleMaster)
    99 		{ // "B" device
   100 		test.Printf(_L("***** Important note *****\n"));
   101 		test.Printf(_L("Before commencing test, please\n"));
   102 		test.Printf(_L("insert 'B'-cable end and activate\n"));
   103 		test.Printf(_L("the test on the 'A' device.\n"));
   104 		test.Printf(_L("Then, press any key to continue.\n"));
   105 		test.Printf(_L("**************************\n"));
   106 		}
   107 	else
   108 		{
   109 		test.Printf(KInsertACablePrompt);
   110 		test.Printf(KPressAnyKeyToContinue);
   111 		}
   112 
   113 	RequestCharacter();	
   114 	}
   115 
   116 // handle event completion	
   117 void CTestCase0681::RunStepL()
   118 	{
   119 	LOG_FUNC
   120 	// Obtain the completion code for this CActive obj.
   121 	TInt completionCode(iStatus.Int()); 
   122 	TBuf<MAX_DSTRLEN> aDescription;
   123 	TInt err(0);
   124 
   125 	if(completionCode != KErrNone)
   126 		{
   127 	
   128 		switch(completionCode)
   129 			{
   130 			case KTestCaseFailureEventReceived:
   131 				{
   132 				TestFailed(KErrAbort, _L("Failure Event Received"));
   133 				break;
   134 				}
   135 			case KTestCaseWatchdogTO:
   136 				{
   137 				TestFailed(KErrAbort, _L("Watchdog timer expired"));			
   138 				break;
   139 				}
   140 			default:
   141 				{
   142 				TestFailed(KErrAbort, _L("Test step completed with Error"));
   143 				break;
   144 				}
   145 			}
   146 		iCollector.DestroyObservers();
   147 		return;
   148 		}
   149 
   150 	switch(iCaseStep)
   151 		{
   152 		case EPreconditions:
   153 			{
   154 			LOG_STEPNAME(_L("EPreconditions"))
   155 			iCaseStep = ELoadLdd;
   156 			StepB2BPreconditions();				
   157 			break;
   158 			}
   159 			
   160 			// 1. Load the Client LDD 
   161 		case ELoadLdd:
   162 			{
   163 			LOG_STEPNAME(_L("ELoadLdd"))
   164 			if (!StepLoadClient(0xF681/*use default settings for SRP/HNP support*/))
   165 				{
   166 				return TestFailed(KErrAbort, _L("Client Load Failure"));
   167 				}
   168 			//  load OTG ldd and init.		
   169 			if (!StepLoadLDD())
   170 				{
   171 				return TestFailed(KErrAbort, _L("OTG Load Failure"));
   172 				}
   173 				
   174 			if(otgActivateFdfActor()!=KErrNone)
   175 				{
   176 				return TestFailed(KErrAbort, _L("Couldn't load FDF Actor"));
   177 				}
   178 			
   179 			// test that the right cable is in
   180 			CheckRoleConnections();
   181 					
   182 			// subscribe to OTG states,events and messages now that it has loaded OK
   183 			TRAPD(result, iCollector.CreateObserversL(*this));
   184 			if (KErrNone != result)
   185 				return(TestFailed(KErrNoMemory, _L("Unable to create observers")));
   186 			iCollector.ClearAllEvents();
   187 			iCaseStep = EPerformSrp;
   188 			SelfComplete();
   189 			break;
   190 			}
   191 			
   192 		case EPerformSrp:
   193 			{
   194 			test.Printf(_L("Into EPerformSrp step...\n"));
   195 
   196 			if (gTestRoleMaster)
   197 				{
   198 				// Trigger SRP
   199 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
   200 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpInitiated);
   201 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);	
   202 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBHost);
   203 				
   204 				//	We shouldn't get configured at any point however since ADevice will call 
   205 				//	BusRespondSrp which should bypass that stage on the way to enabling us to
   206 				//	role swap.
   207 				iCollector.AddFailureNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
   208 
   209 				err = otgBusRequest();	
   210 				if (KErrNone != err)
   211 					{
   212 					return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
   213 					}
   214 
   215 				iCaseStep = ESwappedRoles;
   216 								}
   217 			else
   218 				{ // slave "A"
   219 				// Wait for SRP
   220 				
   221 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpReceived);
   222 				
   223 				iCaseStep = EAReceivedSrp;
   224 				}
   225 			const TInt KTestCase0681Timeout = 30000;			//	30 seconds, should be plenty of time for 3 role swaps
   226 			iCollector.AddStepTimeout(KTestCase0681Timeout);
   227 			SetActive();
   228 			break;
   229 			}
   230 
   231 		case EAReceivedSrp:		//	A-Device step only!
   232 			{
   233 			test.Printf(_L("Into EAReceivedSrp step...\n"));
   234 
   235 			if (KTestCaseWatchdogTO == iStatus.Int())
   236 				{
   237 				iCollector.DestroyObservers();
   238 				return TestFailed(KErrAbort, _L("Timeout"));
   239 				}			
   240 
   241 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);
   242 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToDevice);
   243 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAPeripheral);
   244 			
   245 			//	We've received SRP. We use BusRespondSRP to raise VBus and cede the host role
   246 			err = otgBusRespondSRP();
   247 			if (KErrNone != err)
   248 				{
   249 				return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
   250 				}
   251 
   252 			iCaseStep = ESwappedRoles;
   253 			SetActive();
   254 			break;
   255 			}
   256 
   257 		case ESwappedRoles:
   258 			{
   259 			test.Printf(_L("Into ESwappedRoles step...\n"));
   260 			if (KTestCaseWatchdogTO == iStatus.Int())
   261 				{
   262 				iCollector.DestroyObservers();
   263 				return TestFailed(KErrAbort, _L("Timeout"));
   264 				}			
   265 
   266 			if (gTestRoleMaster)
   267 				{
   268 				//	B-Device should now wait until it is B-Peripheral
   269 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBPeripheral);
   270 				iCaseStep = EDefaultRoles;
   271 				}
   272 			else
   273 				{
   274 				//	A-Device should wait to become a configured A-Peripheral
   275 				iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
   276 				iCaseStep = EAConfigured;
   277 				}
   278 			SetActive();
   279 			break;
   280 			}
   281 			
   282 		case EAConfigured:	//	A-Device only step
   283 			{
   284 			test.Printf(_L("Into EAConfigured step...\n"));
   285 			if (KTestCaseWatchdogTO == iStatus.Int())
   286 				{
   287 				iCollector.DestroyObservers();
   288 				return TestFailed(KErrAbort, _L("Timeout"));
   289 				}			
   290 
   291 			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
   292 			iCaseStep = EASuspended;
   293 			SetActive();
   294 			break;
   295 			}
   296 		
   297 		case EASuspended:	//	A-Device only step
   298 			{
   299 			test.Printf(_L("Into EASuspended step...\n"));
   300 			if (KTestCaseWatchdogTO == iStatus.Int())
   301 				{
   302 				iCollector.DestroyObservers();
   303 				return TestFailed(KErrAbort, _L("Timeout"));
   304 				}			
   305 			
   306 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAHost);	//	Swapping back to default role
   307 
   308 			iCaseStep = EDefaultRoles;
   309 			SetActive();
   310 			break;
   311 			}
   312 			
   313 		case EDefaultRoles:
   314 			{
   315 			test.Printf(_L("Into EDefaultRoles step...\n"));
   316 			
   317 			if ( --iHNPCounter >= 0)
   318 				{
   319 				//	We want to do further role swapping
   320 				if (gTestRoleMaster)
   321 					{
   322 					//	B-Device should now wait until it is configured
   323 					iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
   324 					iCaseStep = EBConfigured;
   325 					}
   326 				else
   327 					{
   328 					//	A-Device should expect nothing more until it becomes A-Peripheral
   329 					iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAPeripheral);
   330 					iCaseStep = ESwappedRoles;
   331 					}
   332 				SetActive();
   333 				}
   334 			else
   335 				{
   336 				//	We've done 3 x HNP cycles back to default roles. 
   337 				//	Time to shut down VBus and stop the test case.
   338 				iCaseStep = EDropVBus;
   339 				SelfComplete();
   340 				}
   341 			break;
   342 			}
   343 
   344 			
   345 		case EBConfigured:	//	A B-Device only step!
   346 			{
   347 			test.Printf(_L("Into EBConfigured step...\n"));
   348 			if (KTestCaseWatchdogTO == iStatus.Int())
   349 				{
   350 				iCollector.DestroyObservers();
   351 				return TestFailed(KErrAbort, _L("Timeout"));
   352 				}
   353 			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
   354 			iCaseStep = EBSuspended;
   355 			SetActive();
   356 			break;
   357 			}
   358 			
   359 		case EBSuspended:	
   360 			{
   361 			test.Printf(_L("Into EBSuspended step...\n"));
   362 			if (KTestCaseWatchdogTO == iStatus.Int())
   363 				{
   364 				iCollector.DestroyObservers();
   365 				return TestFailed(KErrAbort, _L("Timeout"));
   366 				}			
   367 			
   368 			// issue HNP
   369 			test.Printf(_L("VBus present, attempting a swap.\n"));
   370 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBHost);
   371 
   372 			err = otgBusRequest();	//	Request the host role
   373 
   374 			if (KErrNone != err)
   375 				{
   376 				test.Printf(_L("BusRequest returned %d\n"),err);
   377 				return TestFailed(KErrAbort, _L("BusRequest() failed!"));
   378 				}
   379 		
   380 			iCaseStep = ESwappedRoles;
   381 			SetActive();
   382 			break;
   383 			}
   384 			
   385 
   386 
   387 		case EDropVBus:
   388 			LOG_STEPNAME(_L("EDropVBus"))
   389 
   390 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusDropped);
   391 			if ( gTestRoleMaster)
   392 				{
   393 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
   394 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToDevice);
   395 				}
   396 			else
   397 				{ // SLAVE "A"
   398 				otgBusDrop();
   399 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAIdle);
   400 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToHost);
   401 				}
   402 			iCaseStep = EVBusDropped;
   403 			SetActive();
   404 			break;
   405 			
   406 		case EVBusDropped:
   407 			LOG_STEPNAME(_L("ELoopVerifyDrop"))
   408 			if (KTestCaseWatchdogTO == iStatus.Int())
   409 				{
   410 				return TestFailed(KErrAbort, _L("Timeout"));
   411 				}
   412 				
   413 			if (otgVbusPresent())
   414 				{
   415 				return TestFailed(KErrAbort, _L("Vbus did not drop - FAILED!"));
   416 				}
   417 			iCaseStep = EUnloadLdd;
   418 			SelfComplete();
   419 			break;
   420 			
   421 		case EUnloadLdd:
   422 			LOG_STEPNAME(_L("EUnloadLdd"))
   423 			otgDeactivateFdfActor();
   424 			iCollector.DestroyObservers();
   425 			if (EFalse == StepUnloadLDD())
   426 				return TestFailed(KErrAbort,_L("unload Ldd failure"));	
   427 			if (!StepUnloadClient())
   428 				return TestFailed(KErrAbort,_L("Client Unload Failure"));	
   429 
   430 			iCaseStep = ELastStep;
   431 			SelfComplete();
   432 			break;
   433 			
   434 		case ELastStep:
   435 			LOG_STEPNAME(_L("ELastStep"))
   436 			TestPassed();
   437 			break;
   438 			
   439 		default:
   440 			test.Printf(_L("<Error> unknown test step"));
   441 			Cancel();
   442 			return (TestFailed(KErrCorrupt, _L("<Error> unknown test step")));
   443 		}
   444 	}