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