os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-1231.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
// @file PBASE-T_USBDI-1231.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "PBASE-T_USBDI-1231.h"
sl@0
    20
#include <d32usbc.h>
sl@0
    21
#include "testdebug.h"
sl@0
    22
#include "modelleddevices.h"
sl@0
    23
#include "TestPolicy.h"
sl@0
    24
sl@0
    25
namespace NUnitTesting_USBDI
sl@0
    26
	{
sl@0
    27
sl@0
    28
	_LIT(KTestCaseId,"PBASE-T_USBDI-1231");
sl@0
    29
	// the name is very important 
sl@0
    30
	const TFunctorTestCase<CUT_PBASE_T_USBDI_1231,TBool>
sl@0
    31
			CUT_PBASE_T_USBDI_1231::iFunctor(KTestCaseId);
sl@0
    32
sl@0
    33
	CUT_PBASE_T_USBDI_1231* CUT_PBASE_T_USBDI_1231::NewL(TBool aHostRole)
sl@0
    34
		{
sl@0
    35
		CUT_PBASE_T_USBDI_1231* self = new (ELeave) CUT_PBASE_T_USBDI_1231(aHostRole);
sl@0
    36
		CleanupStack::PushL(self);
sl@0
    37
		self->ConstructL();
sl@0
    38
		CleanupStack::Pop(self);
sl@0
    39
		return self;
sl@0
    40
		}
sl@0
    41
sl@0
    42
	CUT_PBASE_T_USBDI_1231::CUT_PBASE_T_USBDI_1231(TBool aHostRole) :
sl@0
    43
		CBaseTestCase(KTestCaseId, aHostRole)
sl@0
    44
		{
sl@0
    45
		}
sl@0
    46
sl@0
    47
	void CUT_PBASE_T_USBDI_1231::ConstructL()
sl@0
    48
		{
sl@0
    49
		RDebug::Printf("====> Constructor entry priority = %d", RThread().Priority());
sl@0
    50
sl@0
    51
		// Collect existing thread priority (to reinstate later)
sl@0
    52
		iPriority = RThread().Priority();
sl@0
    53
sl@0
    54
		iTestDevice = new RUsbDeviceA(this);
sl@0
    55
		BaseConstructL();
sl@0
    56
		}
sl@0
    57
sl@0
    58
	CUT_PBASE_T_USBDI_1231::~CUT_PBASE_T_USBDI_1231()
sl@0
    59
		{
sl@0
    60
		LOG_FUNC
sl@0
    61
sl@0
    62
		RDebug::Printf("====> Destructor entry priority = %d", RThread().Priority());
sl@0
    63
sl@0
    64
		// Reinstate original priority
sl@0
    65
sl@0
    66
		RThread().SetPriority(iPriority);
sl@0
    67
sl@0
    68
		// Cancel any async operations
sl@0
    69
sl@0
    70
		Cancel(); // Cancel host timer
sl@0
    71
sl@0
    72
		// Destroy the watchers
sl@0
    73
		// they still use opened interfaces to cancel the suspend if active
sl@0
    74
		delete iInterface1Watcher;
sl@0
    75
		delete iInterface0Watcher;
sl@0
    76
sl@0
    77
		// Close the interfaces
sl@0
    78
		iUsbInterface1.Close();
sl@0
    79
		iUsbInterface0.Close();
sl@0
    80
sl@0
    81
		delete iControlEp0;
sl@0
    82
		delete iActorFDF;
sl@0
    83
		if (!IsHost() && iTestDevice)
sl@0
    84
			{
sl@0
    85
			iTestDevice->Close();
sl@0
    86
			}
sl@0
    87
		delete iTestDevice;
sl@0
    88
		}
sl@0
    89
sl@0
    90
	void CUT_PBASE_T_USBDI_1231::ExecuteHostTestCaseL()
sl@0
    91
		{
sl@0
    92
		LOG_FUNC
sl@0
    93
sl@0
    94
		RDebug::Printf("====> ExecuteHostTestCaseL entry priority = %d",
sl@0
    95
				RThread().Priority());
sl@0
    96
sl@0
    97
		// Bump thread priority for this test only
sl@0
    98
sl@0
    99
		RThread().SetPriority(EPriorityAbsoluteHigh);
sl@0
   100
		RDebug::Printf("Thread priority raised %d->%d", iPriority, RThread().Priority());
sl@0
   101
sl@0
   102
		iCaseStep = EInProcess;
sl@0
   103
		iActorFDF = CActorFDF::NewL(*this);
sl@0
   104
		iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
sl@0
   105
		iInterface0Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface0,TCallBack(CUT_PBASE_T_USBDI_1231::Interface0ResumedL,this));
sl@0
   106
		iInterface1Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface1,TCallBack(CUT_PBASE_T_USBDI_1231::Interface1ResumedL,this));
sl@0
   107
sl@0
   108
		// Monitor for device connections
sl@0
   109
		iActorFDF->Monitor();
sl@0
   110
sl@0
   111
		// Start the connection timeout	
sl@0
   112
		TimeoutIn(30);
sl@0
   113
		}
sl@0
   114
sl@0
   115
	void CUT_PBASE_T_USBDI_1231::ExecuteDeviceTestCaseL()
sl@0
   116
		{
sl@0
   117
		LOG_FUNC
sl@0
   118
sl@0
   119
		// Construct the device for the test case
sl@0
   120
		iTestDevice->OpenL(TestCaseId());
sl@0
   121
		iTestDevice->SubscribeToReports(iStatus);
sl@0
   122
		SetActive();
sl@0
   123
sl@0
   124
		// Connect the test device	
sl@0
   125
		iTestDevice->SoftwareConnect();
sl@0
   126
		}
sl@0
   127
sl@0
   128
	void CUT_PBASE_T_USBDI_1231::HostDoCancel()
sl@0
   129
		{
sl@0
   130
		LOG_FUNC
sl@0
   131
sl@0
   132
		RDebug::Printf("====> HostDoCancel entry priority = %d", RThread().Priority());
sl@0
   133
sl@0
   134
		// Cancel the timeout timer
sl@0
   135
		CancelTimeout();
sl@0
   136
		}
sl@0
   137
sl@0
   138
	void CUT_PBASE_T_USBDI_1231::DeviceDoCancel()
sl@0
   139
		{
sl@0
   140
		LOG_FUNC
sl@0
   141
sl@0
   142
		// Cancel the device	
sl@0
   143
		iTestDevice->CancelSubscriptionToReports();
sl@0
   144
		}
sl@0
   145
sl@0
   146
	void CUT_PBASE_T_USBDI_1231::DeviceInsertedL(TUint aDeviceHandle)
sl@0
   147
		{
sl@0
   148
		LOG_FUNC
sl@0
   149
sl@0
   150
		RDebug::Printf("====> DeviceInsertedL entry priority = %d", RThread().Priority());
sl@0
   151
sl@0
   152
		Cancel(); // Cancel the timer
sl@0
   153
		TInt err(KErrNone);
sl@0
   154
		iDeviceHandle = aDeviceHandle;
sl@0
   155
		iActorFDF->Monitor();
sl@0
   156
sl@0
   157
		// Validate that device is as expected
sl@0
   158
		CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
sl@0
   159
		if (testDevice.SerialNumber().Compare(TestCaseId()) != 0)
sl@0
   160
			{
sl@0
   161
			// Incorrect device for this test case
sl@0
   162
sl@0
   163
			RDebug::Printf(
sl@0
   164
					"<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
sl@0
   165
					KErrNotFound, &testDevice.SerialNumber(), &TestCaseId());
sl@0
   166
sl@0
   167
			// Start the connection timeout again
sl@0
   168
			TimeoutIn(30);
sl@0
   169
			return;
sl@0
   170
			}
sl@0
   171
		// Check tree now	
sl@0
   172
		CHECK(CheckTreeAfterDeviceInsertion(testDevice, _L("RDeviceA")) == KErrNone);
sl@0
   173
sl@0
   174
		// Perform the correct test step				
sl@0
   175
		switch (iCaseStep)
sl@0
   176
			{
sl@0
   177
			case EInProcess:
sl@0
   178
				{
sl@0
   179
				TUint32 token1(0);
sl@0
   180
				TUint32 token2(0);
sl@0
   181
sl@0
   182
				RDebug::Printf("Obtaining token for interface 0");
sl@0
   183
				err = testDevice.Device().GetTokenForInterface(0, token1);
sl@0
   184
				if (err != KErrNone)
sl@0
   185
					{
sl@0
   186
					RDebug::Printf(
sl@0
   187
							"<Error %d> Token for interface 0 could not be retrieved",
sl@0
   188
							err);
sl@0
   189
					return TestFailed(err);
sl@0
   190
					}
sl@0
   191
				RDebug::Printf("Token 1 (%d) retrieved", token1);
sl@0
   192
				RDebug::Printf("Opening interface 0");
sl@0
   193
				err = iUsbInterface0.Open(token1); // Alternate interface setting 0
sl@0
   194
				if (err != KErrNone)
sl@0
   195
					{
sl@0
   196
					RDebug::Printf(
sl@0
   197
							"<Error %d> Interface 0 could not be opened", err);
sl@0
   198
					return TestFailed(err);
sl@0
   199
					}
sl@0
   200
				RDebug::Printf("Interface 0 opened");
sl@0
   201
sl@0
   202
				RDebug::Printf("Obtaining token for interface 1");
sl@0
   203
				err = testDevice.Device().GetTokenForInterface(1, token2);
sl@0
   204
				if (err != KErrNone)
sl@0
   205
					{
sl@0
   206
					RDebug::Printf(
sl@0
   207
							"<Error %d> Token for interface 1 could not be retrieved",
sl@0
   208
							err);
sl@0
   209
					return TestFailed(err);
sl@0
   210
					}
sl@0
   211
				RDebug::Printf("Opening interface 1");
sl@0
   212
				err = iUsbInterface1.Open(token2); // Alternate interface setting 0
sl@0
   213
				if (err != KErrNone)
sl@0
   214
					{
sl@0
   215
					RDebug::Printf(
sl@0
   216
							"<Error %d> Interface 1 could not be opened", err);
sl@0
   217
					return TestFailed(err);
sl@0
   218
					}
sl@0
   219
				RDebug::Printf("Interface 1 opened");
sl@0
   220
sl@0
   221
				ResumeWhenSuspending();
sl@0
   222
sl@0
   223
				}
sl@0
   224
				break;
sl@0
   225
sl@0
   226
			default:
sl@0
   227
				TestFailed(KErrCorrupt);
sl@0
   228
				break;
sl@0
   229
			}
sl@0
   230
		}
sl@0
   231
sl@0
   232
	TInt CUT_PBASE_T_USBDI_1231::Interface0ResumedL(TAny* aPtr)
sl@0
   233
		{
sl@0
   234
		LOG_CFUNC
sl@0
   235
sl@0
   236
		RDebug::Printf("====> Interface0ResumedL entry priority = %d", RThread().Priority());
sl@0
   237
sl@0
   238
		RDebug::Printf("-Interface 0 resumed");
sl@0
   239
		CUT_PBASE_T_USBDI_1231* self =
sl@0
   240
				reinterpret_cast<CUT_PBASE_T_USBDI_1231*>(aPtr);
sl@0
   241
		
sl@0
   242
		TInt completionCode=self->iInterface0Watcher->CompletionCode();
sl@0
   243
		
sl@0
   244
		TInt testStep = self->iCaseStep;
sl@0
   245
		RDebug::Printf(" -watcher 0 iStatus = %d <teststep %d>",completionCode, testStep);
sl@0
   246
           
sl@0
   247
		
sl@0
   248
		switch (self->iCaseStep)
sl@0
   249
			{
sl@0
   250
sl@0
   251
			case EValidResumeWhenSuspending:
sl@0
   252
				{
sl@0
   253
				if (completionCode == KErrNone)
sl@0
   254
				 {
sl@0
   255
				 RDebug::Printf("Device resume while suspending succeed!");
sl@0
   256
				 self->TimeoutIn(10);
sl@0
   257
				 self->iCaseStep = EPassed;
sl@0
   258
				 self->SendEp0Request();
sl@0
   259
				 }
sl@0
   260
				else
sl@0
   261
					{
sl@0
   262
					RDebug::Printf(
sl@0
   263
							"Device resume while suspending failed,<err %d>",
sl@0
   264
							completionCode);
sl@0
   265
					self->iCaseStep = EFailed;
sl@0
   266
					self->SendEp0Request();
sl@0
   267
					}
sl@0
   268
				}
sl@0
   269
				break;
sl@0
   270
sl@0
   271
			default:
sl@0
   272
				break;
sl@0
   273
			};
sl@0
   274
sl@0
   275
		return KErrNone;
sl@0
   276
		}
sl@0
   277
sl@0
   278
	TInt CUT_PBASE_T_USBDI_1231::Interface1ResumedL(TAny* aPtr)
sl@0
   279
		{
sl@0
   280
		LOG_CFUNC
sl@0
   281
sl@0
   282
		RDebug::Printf("====> Interface1ResumedL entry priority = %d", RThread().Priority());
sl@0
   283
sl@0
   284
		RDebug::Printf("Interface 1 resumed");
sl@0
   285
		CUT_PBASE_T_USBDI_1231* self =
sl@0
   286
				reinterpret_cast<CUT_PBASE_T_USBDI_1231*>(aPtr);
sl@0
   287
		RDebug::Printf("watcher 1 iStatus=%d",
sl@0
   288
				self->iInterface1Watcher->CompletionCode());
sl@0
   289
		return KErrNone;
sl@0
   290
		}
sl@0
   291
sl@0
   292
	void CUT_PBASE_T_USBDI_1231::DeviceRemovedL(TUint aDeviceHandle)
sl@0
   293
		{
sl@0
   294
		LOG_FUNC
sl@0
   295
sl@0
   296
		// The test device should not be removed until the test case has passed
sl@0
   297
		// so this test case has not completed, and state this event as an error
sl@0
   298
sl@0
   299
		TestFailed(KErrDisconnected);
sl@0
   300
		}
sl@0
   301
sl@0
   302
	void CUT_PBASE_T_USBDI_1231::BusErrorL(TInt aError)
sl@0
   303
		{
sl@0
   304
		LOG_FUNC
sl@0
   305
sl@0
   306
		// This test case handles no failiures on the bus
sl@0
   307
sl@0
   308
		TestFailed(aError);
sl@0
   309
		}
sl@0
   310
sl@0
   311
	void CUT_PBASE_T_USBDI_1231::DeviceStateChangeL(
sl@0
   312
			RUsbDevice::TDeviceState aPreviousState,
sl@0
   313
			RUsbDevice::TDeviceState aNewState, TInt aCompletionCode)
sl@0
   314
		{
sl@0
   315
		LOG_FUNC
sl@0
   316
		Cancel();
sl@0
   317
sl@0
   318
		// test RInterface , the  RUsbDevice notification logic not used . 
sl@0
   319
		RDebug::Printf(" -Device State change from %d to %d err=%d",
sl@0
   320
				aPreviousState, aNewState, aCompletionCode);
sl@0
   321
sl@0
   322
		switch (iCaseStep)
sl@0
   323
			{
sl@0
   324
			case EValidDeviceSuspend:
sl@0
   325
				if (aNewState == RUsbDevice::EDeviceSuspended)
sl@0
   326
					{
sl@0
   327
					RDebug::Printf("Device suspend!");
sl@0
   328
					iCaseStep = EValidDeviceResume;
sl@0
   329
					}
sl@0
   330
				else
sl@0
   331
					{
sl@0
   332
					RDebug::Printf("Device suspend failed!");
sl@0
   333
					iCaseStep = EFailed;
sl@0
   334
					SendEp0Request();
sl@0
   335
					}
sl@0
   336
				break;
sl@0
   337
			case EValidDeviceResume:
sl@0
   338
sl@0
   339
				if (aNewState == RUsbDevice::EDeviceActive)
sl@0
   340
					{
sl@0
   341
					RDebug::Printf("Device resume!");
sl@0
   342
					iCaseStep = EValidResumeWhenSuspending;
sl@0
   343
					
sl@0
   344
					}
sl@0
   345
				else
sl@0
   346
					{					
sl@0
   347
					iCaseStep = EFailed;
sl@0
   348
					SendEp0Request();
sl@0
   349
					}
sl@0
   350
sl@0
   351
				break;
sl@0
   352
			default:
sl@0
   353
				break;
sl@0
   354
			}
sl@0
   355
sl@0
   356
		}
sl@0
   357
sl@0
   358
	void CUT_PBASE_T_USBDI_1231::Ep0TransferCompleteL(TInt aCompletionCode)
sl@0
   359
		{
sl@0
   360
		LOG_FUNC
sl@0
   361
		RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",
sl@0
   362
				aCompletionCode);
sl@0
   363
		switch (iCaseStep)
sl@0
   364
			{
sl@0
   365
sl@0
   366
			default:
sl@0
   367
			case EFailed:
sl@0
   368
				TestFailed(KErrCompletion);
sl@0
   369
				break;
sl@0
   370
sl@0
   371
			case EPassed:
sl@0
   372
				TestPassed();
sl@0
   373
				break;
sl@0
   374
			}
sl@0
   375
		}
sl@0
   376
sl@0
   377
	void CUT_PBASE_T_USBDI_1231::HostRunL()
sl@0
   378
		{
sl@0
   379
		LOG_FUNC
sl@0
   380
sl@0
   381
		RDebug::Printf("====> HostRunL entry priority = %d", RThread().Priority());
sl@0
   382
sl@0
   383
		// Obtain the completion code
sl@0
   384
		TInt completionCode(iStatus.Int());
sl@0
   385
sl@0
   386
		if (completionCode == KErrNone)
sl@0
   387
			{
sl@0
   388
			// Action timeout
sl@0
   389
			RDebug::Printf("<Error> Action timeout");
sl@0
   390
			TestFailed(KErrTimedOut);
sl@0
   391
			}
sl@0
   392
		else
sl@0
   393
			{
sl@0
   394
			RDebug::Printf("<Error %d> Timeout timer could not complete",
sl@0
   395
					completionCode);
sl@0
   396
			TestFailed(completionCode);
sl@0
   397
			}
sl@0
   398
		}
sl@0
   399
sl@0
   400
	void CUT_PBASE_T_USBDI_1231::DeviceRunL()
sl@0
   401
		{
sl@0
   402
		LOG_FUNC
sl@0
   403
sl@0
   404
		// Disconnect the device
sl@0
   405
sl@0
   406
		iTestDevice->SoftwareDisconnect();
sl@0
   407
sl@0
   408
		// Complete the test case request
sl@0
   409
sl@0
   410
		TestPolicy().SignalTestComplete(iStatus.Int());
sl@0
   411
		}
sl@0
   412
sl@0
   413
	void CUT_PBASE_T_USBDI_1231::ResumeWhenSuspending()
sl@0
   414
		{
sl@0
   415
		RDebug::Printf("====> ResumeWhenSuspending entry priority = %d",
sl@0
   416
				RThread().Priority());
sl@0
   417
sl@0
   418
		// Suspend interface 0
sl@0
   419
		RDebug::Printf("Suspending interface 0");
sl@0
   420
		iInterface0Watcher->SuspendAndWatch();
sl@0
   421
sl@0
   422
		// Suspend interface 1
sl@0
   423
		RDebug::Printf("Suspending interface 1");
sl@0
   424
		iInterface1Watcher->SuspendAndWatch();
sl@0
   425
sl@0
   426
		// Cancel suspend-in-progress
sl@0
   427
		RDebug::Printf("Cancel Suspend interface 0");
sl@0
   428
		iUsbInterface0.CancelPermitSuspend();
sl@0
   429
sl@0
   430
		iCaseStep = EValidDeviceSuspend;
sl@0
   431
sl@0
   432
		}
sl@0
   433
sl@0
   434
	void CUT_PBASE_T_USBDI_1231::SendEp0Request()
sl@0
   435
		{
sl@0
   436
		TTestCasePassed request;
sl@0
   437
		iControlEp0->SendRequest(request, this);
sl@0
   438
		}
sl@0
   439
sl@0
   440
	}//end namespace
sl@0
   441
sl@0
   442