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