os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-1235.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-1235.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-1235.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-1235");
    29 	// the name is very important 
    30 	const TFunctorTestCase<CUT_PBASE_T_USBDI_1235,TBool>
    31 			CUT_PBASE_T_USBDI_1235::iFunctor(KTestCaseId);
    32 
    33 	CUT_PBASE_T_USBDI_1235* CUT_PBASE_T_USBDI_1235::NewL(TBool aHostRole)
    34 		{
    35 		CUT_PBASE_T_USBDI_1235* self = new (ELeave) CUT_PBASE_T_USBDI_1235(aHostRole);
    36 		CleanupStack::PushL(self);
    37 		self->ConstructL();
    38 		CleanupStack::Pop(self);
    39 		return self;
    40 		}
    41 
    42 	CUT_PBASE_T_USBDI_1235::CUT_PBASE_T_USBDI_1235(TBool aHostRole) :
    43 		CBaseTestCase(KTestCaseId, aHostRole), iSuspendedI0(EFalse),
    44 				iSuspendedI1(EFalse)
    45 		{
    46 		}
    47 
    48 	void CUT_PBASE_T_USBDI_1235::ConstructL()
    49 		{
    50 		iTestDevice = new RUsbDeviceA(this);
    51 		BaseConstructL();
    52 		}
    53 
    54 	CUT_PBASE_T_USBDI_1235::~CUT_PBASE_T_USBDI_1235()
    55 		{
    56 		LOG_FUNC
    57 
    58 		// Cancel any async operations
    59 
    60 		Cancel(); // Cancel host timer
    61 
    62 		// Destroy the watchers
    63 		delete iInterface1Watcher;
    64 		delete iInterface0Watcher;
    65 
    66 		// Close the interfaces
    67 		iUsbInterface1.Close();
    68 		iUsbInterface0.Close();
    69 
    70 		delete iControlEp0;
    71 		delete iActorFDF;
    72 		if (!IsHost() && iTestDevice)
    73 			{
    74 			iTestDevice->Close();
    75 			}
    76 		delete iTestDevice;
    77 		}
    78 
    79 	void CUT_PBASE_T_USBDI_1235::ExecuteHostTestCaseL()
    80 		{
    81 		LOG_FUNC
    82 
    83 		iCaseStep = EInProcess;
    84 		iActorFDF = CActorFDF::NewL(*this);
    85 		iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
    86 		iInterface0Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface0,TCallBack(CUT_PBASE_T_USBDI_1235::Interface0ResumedL,this));
    87 		iInterface1Watcher = new (ELeave) CInterfaceWatcher(iUsbInterface1,TCallBack(CUT_PBASE_T_USBDI_1235::Interface1ResumedL,this));
    88 
    89 		// Monitor for device connections
    90 		iActorFDF->Monitor();
    91 
    92 		// Start the connection timeout	
    93 		TimeoutIn(30);
    94 		}
    95 
    96 	void CUT_PBASE_T_USBDI_1235::ExecuteDeviceTestCaseL()
    97 		{
    98 		LOG_FUNC
    99 
   100 		// Construct the device for the test case
   101 		iTestDevice->OpenL(TestCaseId());
   102 		iTestDevice->SubscribeToReports(iStatus);
   103 		SetActive();
   104 
   105 		// Connect the test device	
   106 		iTestDevice->SoftwareConnect();
   107 		}
   108 
   109 	void CUT_PBASE_T_USBDI_1235::HostDoCancel()
   110 		{
   111 		LOG_FUNC
   112 
   113 		// Cancel the timeout timer
   114 		CancelTimeout();
   115 		}
   116 
   117 	void CUT_PBASE_T_USBDI_1235::DeviceDoCancel()
   118 		{
   119 		LOG_FUNC
   120 
   121 		// Cancel the device	
   122 		iTestDevice->CancelSubscriptionToReports();
   123 		}
   124 
   125 	void CUT_PBASE_T_USBDI_1235::DeviceInsertedL(TUint aDeviceHandle)
   126 		{
   127 		LOG_FUNC
   128 
   129 		Cancel(); // Cancel the timer
   130 		TInt err(KErrNone);
   131 		iDeviceHandle = aDeviceHandle;
   132 		iActorFDF->Monitor();
   133 
   134 		// Validate that device is as expected
   135 		CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
   136 		if (testDevice.SerialNumber().Compare(TestCaseId()) != 0)
   137 			{
   138 			// Incorrect device for this test case
   139 
   140 			RDebug::Printf(
   141 					"<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
   142 					KErrNotFound, &testDevice.SerialNumber(), &TestCaseId());
   143 
   144 			// Start the connection timeout again
   145 			TimeoutIn(30);
   146 			return;
   147 			}
   148 		// Check tree now	
   149 		CHECK(CheckTreeAfterDeviceInsertion(testDevice, _L("RDeviceA")) == KErrNone);
   150 
   151 		// Perform the correct test step				
   152 		switch (iCaseStep)
   153 			{
   154 			case EInProcess:
   155 				{
   156 				TUint32 token1(0);
   157 				TUint32 token2(0);
   158 
   159 				RDebug::Printf("Obtaining token for interface 0");
   160 				err = testDevice.Device().GetTokenForInterface(0, token1);
   161 				if (err != KErrNone)
   162 					{
   163 					RDebug::Printf(
   164 							"<Error %d> Token for interface 0 could not be retrieved",
   165 							err);
   166 					return TestFailed(err);
   167 					}
   168 				RDebug::Printf("Token 1 (%d) retrieved", token1);
   169 				RDebug::Printf("Opening interface 0");
   170 				err = iUsbInterface0.Open(token1); // Alternate interface setting 0
   171 				if (err != KErrNone)
   172 					{
   173 					RDebug::Printf(
   174 							"<Error %d> Interface 0 could not be opened", err);
   175 					return TestFailed(err);
   176 					}
   177 				RDebug::Printf("Interface 0 opened");
   178 
   179 				RDebug::Printf("Obtaining token for interface 1");
   180 				err = testDevice.Device().GetTokenForInterface(1, token2);
   181 				if (err != KErrNone)
   182 					{
   183 					RDebug::Printf(
   184 							"<Error %d> Token for interface 1 could not be retrieved",
   185 							err);
   186 					return TestFailed(err);
   187 					}
   188 				RDebug::Printf("Opening interface 1");
   189 				err = iUsbInterface1.Open(token2); // Alternate interface setting 0
   190 				if (err != KErrNone)
   191 					{
   192 					RDebug::Printf(
   193 							"<Error %d> Interface 1 could not be opened", err);
   194 					return TestFailed(err);
   195 					}
   196 				RDebug::Printf("Interface 1 opened");
   197 
   198 				SuspendDeviceByInterfacesAndCancelWaitForResume();
   199 
   200 				iCaseStep = EValidCancelSuspendAfterInterfaceSuspend;
   201 				
   202 				}
   203 				break;
   204 
   205 			default:
   206 				TestFailed(KErrCorrupt);
   207 				break;
   208 			}
   209 		}
   210 
   211 	TInt CUT_PBASE_T_USBDI_1235::Interface0ResumedL(TAny* aPtr)
   212 		{
   213 		LOG_CFUNC
   214 		RDebug::Printf("Interface 0 resumed");
   215 		CUT_PBASE_T_USBDI_1235* self =
   216 				reinterpret_cast<CUT_PBASE_T_USBDI_1235*>(aPtr);
   217 		TInt completionCode=self->iInterface0Watcher->CompletionCode();
   218 		RDebug::Printf(
   219 				"watcher 0 iStatus=%d",
   220 				completionCode);
   221 		self->iSuspendedI0 = EFalse;
   222 
   223 		switch (self->iCaseStep)
   224 			{
   225 
   226 			case EValidCancelSuspendAfterInterfaceSuspend:
   227 				{
   228 				if (completionCode == KErrCancel)
   229 					{
   230 					RDebug::Printf("CancelWaitForResume request: Success <>!",completionCode);
   231 				    // do not care the device's status.
   232 					self->iCaseStep = EPassed;					
   233 					}
   234 				else
   235 					{
   236 					RDebug::Printf(
   237 							"CancelWaitForResume request: Failed, < err %d >",
   238 							completionCode);
   239 					self->iCaseStep = EFailed;
   240 					
   241 					}
   242 				}
   243 
   244 				break;
   245 
   246 			default:
   247 				break;
   248 			};
   249 
   250 		return KErrNone;
   251 		}
   252 
   253 	TInt CUT_PBASE_T_USBDI_1235::Interface1ResumedL(TAny* aPtr)
   254 		{
   255 		LOG_CFUNC
   256 		RDebug::Printf("Interface 1 resumed");
   257 		CUT_PBASE_T_USBDI_1235* self =
   258 				reinterpret_cast<CUT_PBASE_T_USBDI_1235*>(aPtr);
   259 		TInt completionCode = self->iInterface1Watcher->CompletionCode();		
   260 		RDebug::Printf("watcher 1 iStatus=%d",completionCode);
   261 		self->iSuspendedI1 = EFalse;
   262 		
   263 		if(self->iCaseStep == EPassed && completionCode == KErrNone )
   264 			{
   265 			RDebug::Printf("Device resumed,test passed!");
   266 			self->SendEp0Request(); // stop client;
   267 			}
   268 		
   269 		return KErrNone;
   270 		}
   271 
   272 	void CUT_PBASE_T_USBDI_1235::DeviceRemovedL(TUint aDeviceHandle)
   273 		{
   274 		LOG_FUNC
   275 
   276 		// The test device should not be removed until the test case has passed
   277 		// so this test case has not completed, and state this event as an error
   278 
   279 		TestFailed(KErrDisconnected);
   280 		}
   281 
   282 	void CUT_PBASE_T_USBDI_1235::BusErrorL(TInt aError)
   283 		{
   284 		LOG_FUNC
   285 
   286 		// This test case handles no failiures on the bus
   287 
   288 		TestFailed(aError);
   289 		}
   290 
   291 
   292 	void CUT_PBASE_T_USBDI_1235::DeviceStateChangeL(
   293 			RUsbDevice::TDeviceState aPreviousState,
   294 			RUsbDevice::TDeviceState aNewState, TInt aCompletionCode)
   295 		{
   296 		LOG_FUNC
   297 		Cancel();
   298 		
   299 		// test RInterface , the  RUsbDevice notification logic not used . 
   300 		RDebug::Printf(
   301 				"-Device State change from %d to %d err=%d",
   302 				aPreviousState, aNewState, aCompletionCode);
   303 
   304 		}
   305 
   306 	void CUT_PBASE_T_USBDI_1235::Ep0TransferCompleteL(TInt aCompletionCode)
   307 		{
   308 		LOG_FUNC
   309 		RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",
   310 				aCompletionCode);
   311 		switch (iCaseStep)
   312 			{
   313 
   314 			default:
   315 			case EFailed:
   316 				TestFailed(KErrCompletion);
   317 				break;			
   318 
   319 			case EPassed:
   320 				TestPassed();
   321 				break;
   322 			}
   323 		}
   324 
   325 	void CUT_PBASE_T_USBDI_1235::HostRunL()
   326 		{
   327 		LOG_FUNC
   328 
   329 		// Obtain the completion code
   330 		TInt completionCode(iStatus.Int());
   331 
   332 		if (completionCode == KErrNone)
   333 			{
   334 			// Action timeout
   335 			RDebug::Printf("<Error> Action timeout");
   336 			TestFailed(KErrTimedOut);
   337 			}
   338 		else
   339 			{
   340 			RDebug::Printf("<Error %d> Timeout timer could not complete",
   341 					completionCode);
   342 			TestFailed(completionCode);
   343 			}
   344 		}
   345 
   346 	void CUT_PBASE_T_USBDI_1235::DeviceRunL()
   347 		{
   348 		LOG_FUNC
   349 
   350 		// Disconnect the device
   351 
   352 		iTestDevice->SoftwareDisconnect();
   353 
   354 		// Complete the test case request
   355 
   356 		TestPolicy().SignalTestComplete(iStatus.Int());
   357 		}
   358 
   359 	void CUT_PBASE_T_USBDI_1235::SuspendDeviceByInterfacesAndCancelWaitForResume()
   360 		{
   361 		// Suspend interface 0
   362 		RDebug::Printf("Suspending interface 0");
   363 		iInterface0Watcher->SuspendAndWatch();
   364 		iSuspendedI0 = ETrue;
   365 		
   366 		// Suspend interface 1
   367 		RDebug::Printf("Suspending interface 1");
   368 		iInterface1Watcher->SuspendAndWatch();
   369 		iSuspendedI1 = ETrue;
   370 		
   371 		RDebug::Printf("CancelPermitSuspend interface 1");
   372 		// try to resume device,then host can notify peripheral test passed.
   373 		iUsbInterface1.CancelPermitSuspend();
   374 
   375 		// Only test the CancelWaitForResume() operation 
   376 		// If we let the devic to suspend ,the periperal site could not receive the test control transfer which test finish result.
   377         // select interface0 to test the function CancelWaitForResume	
   378 		RDebug::Printf("CacelWaitForResume interface 0");
   379 		iUsbInterface0.CancelWaitForResume();
   380 
   381 		}
   382 	
   383 	void CUT_PBASE_T_USBDI_1235::SendEp0Request()
   384 		{		
   385 		TTestCasePassed request;
   386 		iControlEp0->SendRequest(request, this);
   387 		}
   388 
   389 	}//end namespace
   390 
   391