os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0488.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-0488.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-0488.h"
    20 #include "testpolicy.h"
    21 #include "modelleddevices.h"
    22 #include <d32usbdi_errors.h>
    23 
    24 namespace NUnitTesting_USBDI
    25 	{
    26 
    27 _LIT(KTestCaseId,"PBASE-T_USBDI-0488");
    28 const TFunctorTestCase<CUT_PBASE_T_USBDI_0488,TBool> CUT_PBASE_T_USBDI_0488::iFunctor(KTestCaseId);
    29 
    30 CUT_PBASE_T_USBDI_0488* CUT_PBASE_T_USBDI_0488::NewL(TBool aHostRole)
    31 	{
    32 	CUT_PBASE_T_USBDI_0488* self = new (ELeave) CUT_PBASE_T_USBDI_0488(aHostRole);
    33 	CleanupStack::PushL(self);
    34 	self->ConstructL();
    35 	CleanupStack::Pop(self);
    36 	return self;
    37 	}
    38 	
    39 
    40 CUT_PBASE_T_USBDI_0488::CUT_PBASE_T_USBDI_0488(TBool aHostRole)
    41 :	CBaseTestCase(KTestCaseId,aHostRole),
    42 	iCaseStep(EInProgress)
    43 	{
    44 	} 
    45 
    46 
    47 void CUT_PBASE_T_USBDI_0488::ConstructL()
    48 	{
    49 	iTestDevice = new RUsbDeviceA(this);
    50 	BaseConstructL();
    51 	}
    52 
    53 
    54 CUT_PBASE_T_USBDI_0488::~CUT_PBASE_T_USBDI_0488()
    55 	{
    56 	LOG_FUNC
    57 	
    58 	Cancel();
    59 	
    60 	// Close the interface
    61 	iUsbInterface0.Close();
    62 	
    63 	delete iControlEp0;
    64 	delete iActorFDF;
    65 	if(!IsHost() && iTestDevice)
    66 		{
    67 		iTestDevice->Close();
    68 		}		
    69 	delete iTestDevice;
    70 	}
    71 	
    72 	
    73 void CUT_PBASE_T_USBDI_0488::ExecuteHostTestCaseL()	
    74 	{
    75 	LOG_FUNC
    76 		
    77 	iActorFDF = CActorFDF::NewL(*this);
    78 	iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
    79 	iActorFDF->Monitor();
    80 	
    81 	TimeoutIn(30);
    82 	}
    83 	
    84 /*static*/ TInt CUT_PBASE_T_USBDI_0488::TestSelectAlternateInterfaceThenPanic(TAny* aTest) 
    85 	{
    86 	LOG_CFUNC
    87 	
    88 	CTrapCleanup* trapHandler=CTrapCleanup::New();
    89 	if(!trapHandler)
    90 		{
    91 		return KErrNoMemory;
    92 		}		
    93 						
    94 	CUT_PBASE_T_USBDI_0488* pTest = reinterpret_cast<CUT_PBASE_T_USBDI_0488*>(aTest);
    95 	
    96 	// change interface setting whilst pipes are opened in another alt. setting, should panic
    97 	pTest->iUsbInterface1.SelectAlternateInterface(0); // panic expected
    98 	
    99 	// Should never get here.*/
   100 	return KErrNone;
   101 	}
   102 	
   103 	
   104 	
   105 void CUT_PBASE_T_USBDI_0488::HostDoCancel()
   106 	{
   107 	LOG_FUNC	
   108 	// Cancel the timeout timer	
   109 	CancelTimeout();
   110 	}
   111 	
   112 	
   113 void CUT_PBASE_T_USBDI_0488::ExecuteDeviceTestCaseL()	
   114 	{
   115 	LOG_FUNC	
   116 	iTestDevice->OpenL(TestCaseId());
   117 	iTestDevice->SubscribeToReports(iStatus);
   118 	SetActive();
   119 	iTestDevice->SoftwareConnect();
   120 	}
   121 	
   122 void CUT_PBASE_T_USBDI_0488::DeviceDoCancel()
   123 	{
   124 	LOG_FUNC	
   125 	// Cancel the device	
   126 	iTestDevice->CancelSubscriptionToReports();
   127 	}
   128 	
   129 	
   130 void CUT_PBASE_T_USBDI_0488::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,RUsbDevice::TDeviceState aNewState,
   131 		TInt aCompletionCode)
   132 	{
   133 	LOG_FUNC	
   134 	}
   135 
   136 
   137 void CUT_PBASE_T_USBDI_0488::DeviceInsertedL(TUint aDeviceHandle)
   138 	{
   139 	LOG_FUNC
   140 	Cancel();
   141 	TInt err(KErrNone);
   142 	
   143 	// Validate that device is as expected
   144 	CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
   145 	if(testDevice.SerialNumber().Compare(TestCaseId()) != 0)
   146 		{
   147 		// Incorrect device for this test case
   148 		RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
   149 			KErrNotFound,&testDevice.SerialNumber(),&TestCaseId());
   150 		// Start the connection timeout again
   151 		CancelTimeout();
   152 		iTimer.After(iStatus,30000000);
   153 		SetActive();
   154 		return;
   155 		}
   156 	
   157 	
   158 	TUint32 token0,token1;
   159 	err = testDevice.Device().GetTokenForInterface(0,token0);
   160 	if(err != KErrNone)
   161 		{
   162 		RDebug::Printf("<Error %d> Token for interface 0 could not be retrieved",err);
   163 		return TestFailed(err);
   164 		}
   165 	err = iUsbInterface0.Open(token0); // Default interface setting 0
   166 	if(err != KErrNone)
   167 		{
   168 		RDebug::Printf("<Error %d> Unable to open interface 0 using token %d",err,token0);
   169 		return TestFailed(err);
   170 		}
   171 		
   172 	__UHEAP_MARK;
   173 	err = testDevice.Device().GetTokenForInterface(1,token1);
   174 	if(err != KErrNone)
   175 		{
   176 		TBuf<256> msg;
   177 		msg.Format(_L("<Error %d> Token for interface 1 could not be retrieved"),err);
   178 		RDebug::Print(msg);
   179 		iCaseStep = EFailed;
   180 		TTestCaseFailed request(err,msg);
   181 		return iControlEp0->SendRequest(request,this);
   182 		}
   183 	err = iUsbInterface1.Open(token1); // Default interface setting 0
   184 	if(err != KErrNone)
   185 		{
   186 		TBuf<256> msg;
   187 		msg.Format(_L("<Error %d> Unable to open interface 1 using token %d"),err,token1);
   188 		RDebug::Print(msg);
   189 		iCaseStep = EFailed;
   190 		TTestCaseFailed request(err,msg);
   191 		return iControlEp0->SendRequest(request,this);
   192 		} 
   193 	
   194 	// Select alternate interface setting 1	
   195 	err = iUsbInterface1.SelectAlternateInterface(1);
   196 	if(err != KErrNone)
   197 		{
   198 		TBuf<256> msg;
   199 		msg.Format(_L("<Error %d> Selecting alternate interface setting 1 on interface 1"),err);
   200 		RDebug::Print(msg);
   201 		iCaseStep = EFailed;
   202 		TTestCaseFailed request(err,msg);
   203 		iControlEp0->SendRequest(request,this);
   204 		return;
   205 		}  
   206 	
   207 	// Open a pipe for endpoint (Int in)	
   208 	TInt endpointAddress;
   209 	err = GetEndpointAddress(iUsbInterface1,1,KTransferTypeInterrupt,KEpDirectionIn,endpointAddress);
   210 	if(err != KErrNone)
   211 		{
   212 		TBuf<256> msg;
   213 		msg.Format(_L("<Error %d> unable to get endpoint address for interrupt in endpoint"),err);
   214 		RDebug::Print(msg);
   215 		iCaseStep = EFailed;
   216 		TTestCaseFailed request(err,msg);
   217 		iControlEp0->SendRequest(request,this);
   218 		return;
   219 		}	
   220 	
   221 	err = iUsbInterface1.OpenPipeForEndpoint(iInPipe,endpointAddress,EFalse);
   222 	if(err != KErrNone)
   223 		{
   224 		TBuf<256> msg;
   225 		msg.Format(_L("<Error %d> Unable to open pipe for endpoint 1"),err);
   226 		RDebug::Print(msg);
   227 		iCaseStep = EFailed;
   228 		TTestCaseFailed request(err,msg);
   229 		return iControlEp0->SendRequest(request,this);		
   230 		}		
   231 
   232 	RThread rt;
   233 	TInt r = rt.Create(_L("PanicThread"), CUT_PBASE_T_USBDI_0488::TestSelectAlternateInterfaceThenPanic, KDefaultStackSize, 0x1000, 0x1000, reinterpret_cast<TAny*>(this));
   234 	gtest(r == KErrNone);
   235 	TRequestStatus status;
   236 	rt.Logon(status);
   237 	rt.Resume();
   238 	rt.Close();
   239 	User::WaitForRequest(status);
   240 	gtest(status.Int() == UsbdiPanics::EInterfaceSettingChangeWithPipeOpen);
   241 	
   242 	iUsbInterface1.Close();	
   243 	 	
   244 	__UHEAP_MARKEND;
   245 	
   246 	// ok now, inform client that the test has passed
   247 	iCaseStep = EPassed;
   248 	TTestCasePassed request;
   249 	return iControlEp0->SendRequest(request,this);
   250 	}
   251 
   252 
   253 void CUT_PBASE_T_USBDI_0488::Ep0TransferCompleteL(TInt aCompletionCode)
   254 	{
   255 	LOG_FUNC
   256 	
   257 	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
   258 	
   259 	if(aCompletionCode != KErrNone)
   260 		{	
   261 		TBuf<256> msg;
   262 		msg.Format(_L("<Error %d> Transfer to control endpoint 0 was not successful"),aCompletionCode);
   263 		RDebug::Print(msg);
   264 		}
   265 
   266 	if(iCaseStep == EPassed)
   267 		{	
   268 		if(aCompletionCode == KErrNone)
   269 			{
   270 			return TestPassed();
   271 			}
   272 		// else error
   273 	    iCaseStep = EFailed;
   274 		}
   275 	
   276 	if(iCaseStep == EFailed)
   277 		{
   278 		return TestFailed(KErrCompletion);
   279 		}	
   280 	}
   281 	
   282 
   283 void CUT_PBASE_T_USBDI_0488::DeviceRemovedL(TUint aDeviceHandle)
   284 	{
   285 	LOG_FUNC
   286 	
   287 	// The test device should not be removed until the test case has passed
   288 	// so this test case has not completed, and state this event as an error	
   289 	TestFailed(KErrDisconnected);
   290 	}
   291 	
   292 	
   293 void CUT_PBASE_T_USBDI_0488::BusErrorL(TInt aError)
   294 	{
   295 	LOG_FUNC
   296 	
   297 	// This test case handles no failiures on the bus	
   298 	TestFailed(KErrCompletion);
   299 	}
   300 
   301 void CUT_PBASE_T_USBDI_0488::HostRunL()
   302 	{
   303 	LOG_FUNC	
   304 	// Obtain the completion code
   305 	TInt completionCode(iStatus.Int());
   306 	
   307 	if(completionCode == KErrNone)
   308 		{
   309 		// Action timeout
   310 		RDebug::Printf("<Error> Action timeout");
   311 		TestFailed(KErrTimedOut);
   312 		}
   313 	else
   314 		{
   315 		RDebug::Printf("<Error %d> Timeout timer could not complete",completionCode);
   316 		TestFailed(completionCode);
   317 		}
   318 	}
   319 
   320 void CUT_PBASE_T_USBDI_0488::DeviceRunL()
   321 	{
   322 	LOG_FUNC
   323 	
   324 	// Disconnect the device	
   325 	iTestDevice->SoftwareDisconnect();
   326 	
   327 	// Complete the test case request	
   328 	TestPolicy().SignalTestComplete(iStatus.Int());
   329 	}
   330 
   331 	
   332 	}