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