os/kernelhwsrv/kerneltest/e32test/usbho/t_otgdi/src/testcase0682.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 "testcase0682.h"
    27 
    28 #define _REPEATS (oOpenIterations*3)
    29 
    30 /* **************************************************************************************
    31  * the name below is used to add a pointer to our construction method to a pointer MAP in 
    32  * the class factory
    33  */
    34 _LIT(KTestCaseId,"PBASE-USB_OTGDI-0682");
    35 const TTestCaseFactoryReceipt<CTestCase0682> CTestCase0682::iFactoryReceipt(KTestCaseId);	
    36 
    37 CTestCase0682* CTestCase0682::NewL(TBool aHost)
    38 	{
    39 	LOG_FUNC
    40 	CTestCase0682* self = new (ELeave) CTestCase0682(aHost);
    41 	CleanupStack::PushL(self);
    42 	self->ConstructL();
    43 	CleanupStack::Pop(self);
    44 	return self;
    45 	}
    46 	
    47 
    48 CTestCase0682::CTestCase0682(TBool aHost)
    49 : 	CTestCaseB2BRoot(KTestCaseId, aHost, iStatus),
    50 	iFirstRoleSwap(ETrue)
    51 	{
    52 	LOG_FUNC
    53 		
    54 	} 
    55 
    56 
    57 /**
    58  ConstructL
    59 */
    60 void CTestCase0682::ConstructL()
    61 	{
    62 	LOG_FUNC
    63 
    64 	iDualRoleCase = ETrue;	// another back-back
    65 		
    66 	BaseConstructL();
    67 	}
    68 
    69 
    70 CTestCase0682::~CTestCase0682()
    71 	{
    72 	LOG_FUNC
    73 	iCollector.DestroyObservers();
    74 	Cancel();
    75 	}
    76 
    77 
    78 void CTestCase0682::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 CTestCase0682::DoCancel()
    89 	{
    90 	LOG_FUNC
    91 	// cancel our timer
    92 	iTimer.Cancel();
    93 	}
    94 
    95 void CTestCase0682::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 CTestCase0682::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 	switch(iCaseStep)
   126 		{
   127 		case EPreconditions:
   128 			{
   129 			LOG_STEPNAME(_L("EPreconditions"))
   130 			iCaseStep = ELoadLdd;
   131 			StepB2BPreconditions();				
   132 			break;
   133 			}
   134 			
   135 			// 1. Load the Client LDD 
   136 		case ELoadLdd:
   137 			{
   138 			LOG_STEPNAME(_L("ELoadLdd"))
   139 			if (!StepLoadClient(0xF682/*use default settings for SRP/HNP support*/))
   140 				{
   141 				return TestFailed(KErrAbort, _L("Client Load Failure"));
   142 				}
   143 			//  load OTG ldd and init.		
   144 			if (!StepLoadLDD())
   145 				{
   146 				return TestFailed(KErrAbort, _L("OTG Load Failure"));
   147 				}
   148 				
   149 			if(otgActivateFdfActor()!=KErrNone)
   150 				{
   151 				return TestFailed(KErrAbort, _L("Couldn't load FDF Actor"));
   152 				}
   153 			
   154 			// test that the right cable is in
   155 			CheckRoleConnections();
   156 					
   157 			// subscribe to OTG states,events and messages now that it has loaded OK
   158 			TRAPD(result, iCollector.CreateObserversL(*this));
   159 			if (KErrNone != result)
   160 				return(TestFailed(KErrNoMemory, _L("Unable to create observers")));
   161 			iCollector.ClearAllEvents();
   162 			iCaseStep = EPerformSrp;
   163 			SelfComplete();
   164 			break;
   165 			}
   166 			
   167 		case EPerformSrp:
   168 			{
   169 			test.Printf(_L("Into EPerformSrp step...\n"));
   170 
   171 			if (gTestRoleMaster)
   172 				{
   173 				// Trigger SRP
   174 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
   175 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpInitiated);
   176 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);	
   177 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBPeripheral);
   178 
   179 				err = otgBusRequest();	//	Should generate SRP
   180 				if (KErrNone != err)
   181 					{
   182 					return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
   183 					}
   184 
   185 				iCaseStep = EDefaultRoles;
   186 				}
   187 			else
   188 				{ // slave "A"
   189 				// Wait for SRP
   190 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventSrpReceived);
   191 				
   192 				iCaseStep = EAReceivedSrp;
   193 				}
   194 			const TInt KTestCase0681Timeout = 30000;			//	30 seconds, should be plenty of time for 3 role swaps
   195 			iCollector.AddStepTimeout(KTestCase0681Timeout);
   196 			SetActive();
   197 			break;
   198 			}
   199 
   200 		case EAReceivedSrp:		//	A-Device step only!
   201 			{
   202 			test.Printf(_L("Into EAReceivedSrp step...\n"));
   203 
   204 			if (KTestCaseWatchdogTO == iStatus.Int())
   205 				{
   206 				iCollector.DestroyObservers();
   207 				return TestFailed(KErrAbort, _L("Timeout"));
   208 				}			
   209 
   210 			//	We've received SRP. In this test, we use BusRequest to raise VBus but not cede the host role
   211 			//	before we've enumerated and configured the device.
   212 
   213 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusRaised);
   214 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToHost);
   215 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAHost);
   216 
   217 			err = otgBusRequest();
   218 			if (KErrNone != err)
   219 				{
   220 				return TestFailed(KErrAbort, _L("Raise Vbus - RUsbOtgDriver::BusRequest() FAILED!"));
   221 				}
   222 			
   223 			iCaseStep = EDefaultRoles;
   224 			SetActive();
   225 			break;
   226 			}
   227 			
   228 		case EDefaultRoles:
   229 			{
   230 			test.Printf(_L("Into EDefaultRoles step...\n"));
   231 			
   232 			if ( --iHNPCounter >= 0)
   233 				{
   234 				//	We want to do further role swapping
   235 				if (gTestRoleMaster)
   236 					{
   237 					//	B-Device should now wait until it is configured
   238 					iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
   239 					iCaseStep = EBConfigured;
   240 					}
   241 				else
   242 					{
   243 					//	A-Device should expect nothing more until it becomes A-Peripheral
   244 					iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAPeripheral);
   245 					iCaseStep = ESwappedRoles;
   246 					}
   247 				SetActive();
   248 				}
   249 			else
   250 				{
   251 				//	We've done 3 x HNP cycles back to default roles. 
   252 				//	Time to shut down VBus and stop the test case.
   253 				iCaseStep = EDropVBus;
   254 				SelfComplete();
   255 				}
   256 			break;
   257 			}
   258 
   259 			
   260 		case EBConfigured:	//	A B-Device only step!
   261 			{
   262 			test.Printf(_L("Into EBConfigured step...\n"));
   263 			if (KTestCaseWatchdogTO == iStatus.Int())
   264 				{
   265 				iCollector.DestroyObservers();
   266 				return TestFailed(KErrAbort, _L("Timeout"));
   267 				}
   268 			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
   269 			iCaseStep = EBSuspended;
   270 			SetActive();
   271 			break;
   272 			}
   273 			
   274 		case EBSuspended:	
   275 			{
   276 			test.Printf(_L("Into EBSuspended step...\n"));
   277 			if (KTestCaseWatchdogTO == iStatus.Int())
   278 				{
   279 				iCollector.DestroyObservers();
   280 				return TestFailed(KErrAbort, _L("Timeout"));
   281 				}			
   282 			
   283 			// issue HNP
   284 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBHost);
   285 
   286 			//	The first time we arrive here, it should be unnecessary for the test code
   287 			//	to issue a further bus request - it should "remember" that it earlier 
   288 			//	requested the host role, triggering SRP to be followed (hopefully now) by HNP
   289 			if(!iFirstRoleSwap)
   290 				{
   291 				err = otgBusRequest();	//	Request the host role
   292 				if (KErrNone != err)
   293 					{
   294 					test.Printf(_L("BusRequest returned %d\n"),err);
   295 					return TestFailed(KErrAbort, _L("BusRequest() failed!"));
   296 					}
   297 				}
   298 			else
   299 				{
   300 				//	So that we know next time around that we need to perform a bus request
   301 				iFirstRoleSwap = EFalse;
   302 				}
   303 		
   304 			iCaseStep = ESwappedRoles;
   305 			SetActive();
   306 			break;
   307 			}
   308 			
   309 		case ESwappedRoles:
   310 			{
   311 			test.Printf(_L("Into ESwappedRoles step...\n"));
   312 			if (KTestCaseWatchdogTO == iStatus.Int())
   313 				{
   314 				iCollector.DestroyObservers();
   315 				return TestFailed(KErrAbort, _L("Timeout"));
   316 				}			
   317 
   318 			if (gTestRoleMaster)
   319 				{
   320 				//	B-Device should now wait until it is back to B-Peripheral
   321 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBPeripheral);
   322 				iCaseStep = EDefaultRoles;
   323 				}
   324 			else
   325 				{
   326 				//	A-Device should wait to become a configured A-Peripheral
   327 				iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateConfigured);
   328 				iCaseStep = EAConfigured;
   329 				}
   330 			SetActive();
   331 			break;
   332 			}
   333 			
   334 		case EAConfigured:	//	A-Device only step
   335 			{
   336 			test.Printf(_L("Into EWaitTillAConfigured step...\n"));
   337 			if (KTestCaseWatchdogTO == iStatus.Int())
   338 				{
   339 				iCollector.DestroyObservers();
   340 				return TestFailed(KErrAbort, _L("Timeout"));
   341 				}			
   342 
   343 			iCollector.AddRequiredNotification(EWatcherPeripheralState, EUsbcDeviceStateSuspended);
   344 			iCaseStep = EASuspended;
   345 			SetActive();
   346 			break;
   347 			}
   348 		
   349 		case EASuspended:	//	A-Device only step
   350 			{
   351 			test.Printf(_L("Into EWaitTillASuspended step...\n"));
   352 			if (KTestCaseWatchdogTO == iStatus.Int())
   353 				{
   354 				iCollector.DestroyObservers();
   355 				return TestFailed(KErrAbort, _L("Timeout"));
   356 				}			
   357 			
   358 			iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAHost);	//	Swapping back to default role
   359 
   360 			iCaseStep = EDefaultRoles;
   361 			SetActive();
   362 			break;
   363 			}
   364 
   365 		case EDropVBus:
   366 			LOG_STEPNAME(_L("EDropVBus"))
   367 
   368 			iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventVbusDropped);
   369 			if ( gTestRoleMaster)
   370 				{
   371 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateBIdle);
   372 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToDevice);
   373 				}
   374 			else
   375 				{ // SLAVE "A"
   376 				otgBusDrop();
   377 				iCollector.AddRequiredNotification(EWatcherState, RUsbOtgDriver::EStateAIdle);
   378 				iCollector.AddRequiredNotification(EWatcherEvent, RUsbOtgDriver::EEventRoleChangedToHost);
   379 				}
   380 			iCaseStep = EVBusDropped;
   381 			SetActive();
   382 			break;
   383 			
   384 		case EVBusDropped:
   385 			LOG_STEPNAME(_L("ELoopVerifyDrop"))
   386 			if (KTestCaseWatchdogTO == iStatus.Int())
   387 				{
   388 				return TestFailed(KErrAbort, _L("Timeout"));
   389 				}
   390 				
   391 			if (otgVbusPresent())
   392 				{
   393 				return TestFailed(KErrAbort, _L("Vbus did not drop - FAILED!"));
   394 				}
   395 			iCaseStep = EUnloadLdd;
   396 			SelfComplete();
   397 			break;
   398 			
   399 		case EUnloadLdd:
   400 			LOG_STEPNAME(_L("EUnloadLdd"))
   401 			otgDeactivateFdfActor();
   402 			iCollector.DestroyObservers();
   403 			if (EFalse == StepUnloadLDD())
   404 				return TestFailed(KErrAbort,_L("unload Ldd failure"));	
   405 			if (!StepUnloadClient())
   406 				return TestFailed(KErrAbort,_L("Client Unload Failure"));	
   407 
   408 			iCaseStep = ELastStep;
   409 			SelfComplete();
   410 			break;
   411 			
   412 		case ELastStep:
   413 			LOG_STEPNAME(_L("ELastStep"))
   414 			TestPassed();
   415 			break;
   416 			
   417 		default:
   418 			test.Printf(_L("<Error> unknown test step"));
   419 			Cancel();
   420 			return (TestFailed(KErrCorrupt, _L("<Error> unknown test step")));
   421 		}
   422 	}