os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-1232.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-1232.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "PBASE-T_USBDI-1232.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-1232");
sl@0
    29
	// the name is very important 
sl@0
    30
	const TFunctorTestCase<CUT_PBASE_T_USBDI_1232,TBool>
sl@0
    31
			CUT_PBASE_T_USBDI_1232::iFunctor(KTestCaseId);
sl@0
    32
sl@0
    33
	CUT_PBASE_T_USBDI_1232* CUT_PBASE_T_USBDI_1232::NewL(TBool aHostRole)
sl@0
    34
		{
sl@0
    35
		CUT_PBASE_T_USBDI_1232* self = new (ELeave) CUT_PBASE_T_USBDI_1232(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_1232::CUT_PBASE_T_USBDI_1232(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_1232::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_1232::~CUT_PBASE_T_USBDI_1232()
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_1232::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_1232::Interface0ResumedL,this));
sl@0
   106
		iInterface1Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface1,TCallBack(CUT_PBASE_T_USBDI_1232::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_1232::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_1232::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_1232::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_1232::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
				// device go to suspend
sl@0
   222
				// Suspend interface 0
sl@0
   223
				RDebug::Printf("Suspending interface 0");
sl@0
   224
				iInterface0Watcher->SuspendAndWatch();
sl@0
   225
sl@0
   226
				// Suspend interface 1
sl@0
   227
				RDebug::Printf("Suspending interface 1");
sl@0
   228
				iInterface1Watcher->SuspendAndWatch();
sl@0
   229
sl@0
   230
				iCaseStep = ESuspendWhenResuming;
sl@0
   231
sl@0
   232
				TimeoutIn(10); // Give 10 seconds for device to suspend						
sl@0
   233
                
sl@0
   234
				
sl@0
   235
				iUsbInterface0.CancelWaitForResume(); // a tricky way to close the watcher of interface
sl@0
   236
				iUsbInterface1.CancelWaitForResume();
sl@0
   237
				}
sl@0
   238
				break;
sl@0
   239
sl@0
   240
			default:
sl@0
   241
				TestFailed(KErrCorrupt);
sl@0
   242
				break;
sl@0
   243
			}
sl@0
   244
		}
sl@0
   245
sl@0
   246
	TInt CUT_PBASE_T_USBDI_1232::Interface0ResumedL(TAny* aPtr)
sl@0
   247
		{
sl@0
   248
		LOG_CFUNC
sl@0
   249
sl@0
   250
		RDebug::Printf("====> Interface0ResumedL entry priority = %d", RThread().Priority());
sl@0
   251
sl@0
   252
		RDebug::Printf(" -Interface 0 resumed");
sl@0
   253
		CUT_PBASE_T_USBDI_1232* self =
sl@0
   254
				reinterpret_cast<CUT_PBASE_T_USBDI_1232*>(aPtr);
sl@0
   255
		TInt completionCode = self->iInterface0Watcher->CompletionCode();
sl@0
   256
		RDebug::Printf(" -watcher 0 iStatus=%d",completionCode);
sl@0
   257
sl@0
   258
		switch (self->iCaseStep)
sl@0
   259
			{	
sl@0
   260
                
sl@0
   261
			case EPassed:
sl@0
   262
				{
sl@0
   263
				if (completionCode == KErrNone)
sl@0
   264
					{
sl@0
   265
					RDebug::Printf("Device resume successed,test passed!");
sl@0
   266
					self->SendEp0Request(); // stop client site
sl@0
   267
					}
sl@0
   268
				else
sl@0
   269
					{
sl@0
   270
					RDebug::Printf("Device resume failed, err = %d ",completionCode);
sl@0
   271
					self->iCaseStep = EFailed;
sl@0
   272
					self->SendEp0Request();
sl@0
   273
					}
sl@0
   274
				}
sl@0
   275
				break;
sl@0
   276
			case EFailed:				
sl@0
   277
				self->SendEp0Request();
sl@0
   278
				break;
sl@0
   279
sl@0
   280
			default:
sl@0
   281
				break;
sl@0
   282
			};
sl@0
   283
sl@0
   284
		return KErrNone;
sl@0
   285
		}
sl@0
   286
sl@0
   287
	TInt CUT_PBASE_T_USBDI_1232::Interface1ResumedL(TAny* aPtr)
sl@0
   288
		{
sl@0
   289
		LOG_CFUNC
sl@0
   290
sl@0
   291
		RDebug::Printf("====> Interface1ResumedL entry priority = %d", RThread().Priority());
sl@0
   292
sl@0
   293
		RDebug::Printf("Interface 1 resumed");
sl@0
   294
		CUT_PBASE_T_USBDI_1232* self =
sl@0
   295
				reinterpret_cast<CUT_PBASE_T_USBDI_1232*>(aPtr);
sl@0
   296
		RDebug::Printf("watcher 1 iStatus=%d", self->iInterface1Watcher->CompletionCode());
sl@0
   297
		return KErrNone;
sl@0
   298
		}
sl@0
   299
sl@0
   300
	void CUT_PBASE_T_USBDI_1232::DeviceRemovedL(TUint aDeviceHandle)
sl@0
   301
		{
sl@0
   302
		LOG_FUNC
sl@0
   303
sl@0
   304
		// The test device should not be removed until the test case has passed
sl@0
   305
		// so this test case has not completed, and state this event as an error
sl@0
   306
sl@0
   307
		TestFailed(KErrDisconnected);
sl@0
   308
		}
sl@0
   309
sl@0
   310
	void CUT_PBASE_T_USBDI_1232::BusErrorL(TInt aError)
sl@0
   311
		{
sl@0
   312
		LOG_FUNC
sl@0
   313
sl@0
   314
		// This test case handles no failiures on the bus
sl@0
   315
sl@0
   316
		TestFailed(aError);
sl@0
   317
		}
sl@0
   318
sl@0
   319
	void CUT_PBASE_T_USBDI_1232::DeviceStateChangeL(
sl@0
   320
			RUsbDevice::TDeviceState aPreviousState,
sl@0
   321
			RUsbDevice::TDeviceState aNewState, TInt aCompletionCode)
sl@0
   322
		{
sl@0
   323
		LOG_FUNC
sl@0
   324
		Cancel();
sl@0
   325
sl@0
   326
		// test RInterface , the  RUsbDevice notification logic not used . 
sl@0
   327
		RDebug::Printf(" -Device State change from err=%d",aCompletionCode);
sl@0
   328
sl@0
   329
		switch (iCaseStep)
sl@0
   330
			{
sl@0
   331
			case ESuspendWhenResuming:
sl@0
   332
				if (aNewState == RUsbDevice::EDeviceSuspended)
sl@0
   333
					{
sl@0
   334
					RDebug::Printf("====> device has suspended!");
sl@0
   335
                    
sl@0
   336
					SuspendWhenResuming();					
sl@0
   337
					}
sl@0
   338
				break;
sl@0
   339
			case EValidSuspendWhenResuming:
sl@0
   340
				if (aPreviousState == RUsbDevice::EDeviceSuspended&&aNewState
sl@0
   341
						== RUsbDevice::EDeviceSuspended)
sl@0
   342
					{
sl@0
   343
					RDebug::Printf("====> device suspended again,suspend while resuming succeed!");
sl@0
   344
					iCaseStep = EPassed;
sl@0
   345
					iUsbInterface0.CancelPermitSuspend();
sl@0
   346
					}
sl@0
   347
				else
sl@0
   348
					{
sl@0
   349
					iCaseStep = EFailed;
sl@0
   350
					iUsbInterface0.CancelPermitSuspend();
sl@0
   351
			        }
sl@0
   352
		
sl@0
   353
				break;
sl@0
   354
			default:
sl@0
   355
				break;
sl@0
   356
			}
sl@0
   357
sl@0
   358
		}
sl@0
   359
sl@0
   360
	void CUT_PBASE_T_USBDI_1232::Ep0TransferCompleteL(TInt aCompletionCode)
sl@0
   361
		{
sl@0
   362
		LOG_FUNC
sl@0
   363
		RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",
sl@0
   364
				aCompletionCode);
sl@0
   365
		switch (iCaseStep)
sl@0
   366
			{
sl@0
   367
sl@0
   368
			default:
sl@0
   369
			case EFailed:
sl@0
   370
				TestFailed(KErrCompletion);
sl@0
   371
				break;
sl@0
   372
sl@0
   373
			case EPassed:
sl@0
   374
				TestPassed();
sl@0
   375
				break;
sl@0
   376
			}
sl@0
   377
		}
sl@0
   378
sl@0
   379
	void CUT_PBASE_T_USBDI_1232::HostRunL()
sl@0
   380
		{
sl@0
   381
		LOG_FUNC
sl@0
   382
sl@0
   383
		RDebug::Printf("====> HostRunL entry priority = %d", RThread().Priority());
sl@0
   384
sl@0
   385
		// Obtain the completion code
sl@0
   386
		TInt completionCode(iStatus.Int());
sl@0
   387
sl@0
   388
		if (completionCode == KErrNone)
sl@0
   389
			{
sl@0
   390
			// Action timeout
sl@0
   391
			RDebug::Printf("<Error> Action timeout");
sl@0
   392
			TestFailed(KErrTimedOut);
sl@0
   393
			}
sl@0
   394
		else
sl@0
   395
			{
sl@0
   396
			RDebug::Printf("<Error %d> Timeout timer could not complete",
sl@0
   397
					completionCode);
sl@0
   398
			TestFailed(completionCode);
sl@0
   399
			}
sl@0
   400
		}
sl@0
   401
sl@0
   402
	void CUT_PBASE_T_USBDI_1232::DeviceRunL()
sl@0
   403
		{
sl@0
   404
		LOG_FUNC
sl@0
   405
sl@0
   406
		// Disconnect the device
sl@0
   407
sl@0
   408
		iTestDevice->SoftwareDisconnect();
sl@0
   409
sl@0
   410
		// Complete the test case request
sl@0
   411
sl@0
   412
		TestPolicy().SignalTestComplete(iStatus.Int());
sl@0
   413
		}
sl@0
   414
sl@0
   415
	void CUT_PBASE_T_USBDI_1232::SuspendWhenResuming()
sl@0
   416
		{
sl@0
   417
		RDebug::Printf("====> SuspendWhenResuming entry priority = %d",
sl@0
   418
				RThread().Priority());
sl@0
   419
sl@0
   420
		// Cancel suspend-in-progress
sl@0
   421
		RDebug::Printf("Cancel Suspend interface 0");
sl@0
   422
				
sl@0
   423
		iUsbInterface0.CancelPermitSuspend();
sl@0
   424
		
sl@0
   425
		// how to prove, see log? 
sl@0
   426
		// Suspend interface 0
sl@0
   427
		RDebug::Printf("Suspending interface 0");
sl@0
   428
		iInterface0Watcher->SuspendAndWatch();			
sl@0
   429
sl@0
   430
		// Suspend interface 1
sl@0
   431
		RDebug::Printf("Suspending interface 1");
sl@0
   432
		iInterface1Watcher->SuspendAndWatch();
sl@0
   433
sl@0
   434
		iCaseStep = EValidSuspendWhenResuming;
sl@0
   435
sl@0
   436
		TimeoutIn(10); // Give 10 seconds for device to suspend	
sl@0
   437
sl@0
   438
		}
sl@0
   439
sl@0
   440
	void CUT_PBASE_T_USBDI_1232::SendEp0Request()
sl@0
   441
		{
sl@0
   442
		TTestCasePassed request;
sl@0
   443
		iControlEp0->SendRequest(request, this);
sl@0
   444
		}
sl@0
   445
sl@0
   446
	}//end namespace
sl@0
   447
sl@0
   448