os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0477.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-0477.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-0477.h"
    20 #include "testpolicy.h"
    21 #include "modelleddevices.h"
    22 
    23 
    24 namespace NUnitTesting_USBDI
    25 	{
    26 	
    27 _LIT(KTestCaseId,"PBASE-T_USBDI-0477");
    28 const TFunctorTestCase<CUT_PBASE_T_USBDI_0477,TBool> CUT_PBASE_T_USBDI_0477::iFunctor(KTestCaseId);	
    29 
    30 CUT_PBASE_T_USBDI_0477* CUT_PBASE_T_USBDI_0477::NewL(TBool aHostRole)
    31 	{
    32 	CUT_PBASE_T_USBDI_0477* self = new (ELeave) CUT_PBASE_T_USBDI_0477(aHostRole);
    33 	CleanupStack::PushL(self);
    34 	self->ConstructL();
    35 	CleanupStack::Pop(self);
    36 	return self;
    37 	}
    38 	
    39 
    40 CUT_PBASE_T_USBDI_0477::CUT_PBASE_T_USBDI_0477(TBool aHostRole)
    41 :	CBaseTestCase(KTestCaseId,aHostRole),
    42 	iCaseStep(EInProgress)
    43 	{
    44 	} 
    45 
    46  
    47 void CUT_PBASE_T_USBDI_0477::ConstructL()
    48 	{
    49 	iTestDevice = new RUsbDeviceA(this);
    50 	BaseConstructL();
    51 	}
    52 
    53 
    54 CUT_PBASE_T_USBDI_0477::~CUT_PBASE_T_USBDI_0477()
    55 	{
    56 	LOG_FUNC
    57 	Cancel();
    58 
    59 	iTestPipe.Close();
    60 	iUsbInterface1.Close();
    61 	iUsbInterface0.Close();
    62 
    63 	delete iClientAction;
    64 	delete iActorFDF;
    65 	if(!IsHost() && iTestDevice)
    66 		{
    67 		iTestDevice->Close();
    68 		}		
    69 	delete iTestDevice;
    70 	}
    71 	
    72 	
    73 void CUT_PBASE_T_USBDI_0477::ExecuteHostTestCaseL()
    74 	{
    75 	LOG_FUNC
    76 	
    77 	iActorFDF = CActorFDF::NewL(*this);
    78 	iClientAction = new (ELeave) CEp0Transfer(iUsbInterface0);
    79 	iActorFDF->Monitor();
    80 	TimeoutIn(30);
    81 	}
    82 
    83 	
    84 void CUT_PBASE_T_USBDI_0477::ExecuteDeviceTestCaseL()
    85 	{
    86 	LOG_FUNC
    87 	
    88 	iTestDevice->OpenL(TestCaseId());
    89 	iTestDevice->SubscribeToReports(iStatus);
    90 	SetActive();
    91 	iTestDevice->SoftwareConnect();
    92 	}
    93 	
    94 	
    95 void CUT_PBASE_T_USBDI_0477::HostDoCancel()
    96 	{
    97 	LOG_FUNC
    98 	
    99 	// Cancel the test step timeout
   100 	
   101 	CancelTimeout();
   102 	}
   103 
   104 
   105 void CUT_PBASE_T_USBDI_0477::DeviceDoCancel()
   106 	{
   107 	LOG_FUNC
   108 	
   109 	// Cancel the device
   110 	
   111 	iTestDevice->CancelSubscriptionToReports();
   112 	}
   113 	
   114 TBool CUT_PBASE_T_USBDI_0477::CheckFirstInterfaceDescriptorDeviceA(TUsbInterfaceDescriptor& aIfDescriptor)
   115 	{
   116 	LOG_FUNC	
   117 	/*	Interface0	[setting0]
   118 		Interface1	[setting0]
   119 						[endpoint1] Bulk out
   120 						[endpoint2] Bulk in
   121 					[setting1]			
   122 						[endpoint1] Interrupt in
   123 						[endpoint2] Bulk out
   124 						[endpoint3] Bulk in
   125 	*/					
   126 		// alt. setting 0
   127 		TUsbInterfaceDescriptor::Cast(&aIfDescriptor); // is it an interface?
   128 		CHECK_RET_BOOL(&aIfDescriptor != 0);
   129 		
   130 		// EP1
   131 		TUsbGenericDescriptor* desc = aIfDescriptor.iFirstChild;
   132 		TUsbEndpointDescriptor::Cast(desc);
   133 		CHECK_RET_BOOL(desc != 0);
   134 						
   135 		// EP 2
   136 		desc = desc->iNextPeer;
   137 		TUsbEndpointDescriptor::Cast(desc);
   138 		CHECK_RET_BOOL(desc != 0);		
   139 		desc = desc->iNextPeer; // no peer
   140 		CHECK_RET_BOOL(desc == 0);
   141 		
   142 		// alt. setting 1
   143 		desc = aIfDescriptor.iNextPeer;
   144 		TUsbInterfaceDescriptor::Cast(desc);
   145 		CHECK_RET_BOOL(desc != 0);
   146 		
   147 		// EP1
   148 		desc = desc->iFirstChild;
   149 		TUsbEndpointDescriptor::Cast(desc);
   150 		CHECK_RET_BOOL(desc != 0);
   151 		
   152 		// EP 2
   153 		desc = desc->iNextPeer;
   154 		TUsbEndpointDescriptor::Cast(desc);
   155 		CHECK_RET_BOOL(desc != 0);
   156 		
   157 		// EP 3 
   158 		desc = desc->iNextPeer;
   159 		TUsbEndpointDescriptor::Cast(desc);
   160 		CHECK_RET_BOOL(desc != 0);			
   161 		desc = desc->iNextPeer; // no peer
   162 		CHECK_RET_BOOL(desc == 0); 
   163 		
   164 		RDebug::Printf("CheckFirstInterfaceDescriptorDeviceA successfull!");
   165 		return ETrue;
   166 	}
   167 	
   168 void CUT_PBASE_T_USBDI_0477::DeviceInsertedL(TUint aDeviceHandle)
   169 	{
   170 	LOG_FUNC
   171 	Cancel();
   172 	TInt err(KErrNone);
   173 	
   174 	// Validate that device is as expected
   175 	CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
   176 	if(testDevice.SerialNumber().Compare(TestCaseId()) != 0)
   177 		{
   178 		// Incorrect device for this test case	
   179 
   180 		RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
   181 			KErrNotFound,&testDevice.SerialNumber(),&TestCaseId());
   182 
   183 		// Start the connection timeout again
   184 		TimeoutIn(30);
   185 		return;
   186 		}
   187 			
   188 	TUint32 token0,token1; // Token for an interface
   189 	
   190 	err = testDevice.Device().GetTokenForInterface(0,token0);
   191 	if(err != KErrNone)
   192 		{
   193 		RDebug::Printf("<Error %d> Token for interface 0 could not be retrieved",err);
   194 		return TestFailed(err);
   195 		}
   196 		
   197 	err = iUsbInterface0.Open(token0);
   198 	if(err != KErrNone)
   199 		{
   200 		RDebug::Printf("<Error %d> Unable to open interface 0 using token %d",err,token0);
   201 		return TestFailed(err);
   202 		}
   203 	
   204 	err = testDevice.Device().GetTokenForInterface(1,token1);
   205 	if(err != KErrNone)
   206 		{
   207 		// Test case has failed
   208 		TBuf<256> msg;
   209 		_LIT(string, "<Error %d> Token for interface could not be retrieved");
   210 		msg.Format(string,err);
   211 		RDebug::Print(msg);
   212 		iCaseStep = EFailed;
   213 		TTestCaseFailed request(err,msg);
   214 		iClientAction->SendRequest(request,this);
   215 		return;
   216 		}
   217 
   218 	err = iUsbInterface1.Open(token1); // Default interface setting 0
   219 	if(err != KErrNone)
   220 		{
   221 		TBuf<256> msg;
   222 		msg.Format(_L("<Error %d> Unable to open interface using token %d"),err,token1);
   223 		RDebug::Print(msg);
   224 		iCaseStep = EFailed;
   225 		TTestCaseFailed request(err,msg);
   226 		iClientAction->SendRequest(request,this);
   227 		return;
   228 		}
   229 		
   230 			
   231 	// check interface descriptor now				 
   232 	RDebug::Printf("check Interface descriptor now");
   233 	TUsbInterfaceDescriptor ifDescriptor;
   234 	CHECK(iUsbInterface1.GetInterfaceDescriptor(ifDescriptor) == KErrNone);	
   235 	CHECK(CheckFirstInterfaceDescriptorDeviceA(ifDescriptor));
   236 																				
   237 	//  Enumerate Endpoints On Interface 1 alt. setting 0
   238 	CHECK(iUsbInterface1.EnumerateEndpointsOnInterface(0) == 2);
   239 	
   240 	// get busId now				
   241 	TUsbBusId busId;	 
   242 	CHECK(iUsbInterface1.GetBusId(busId) == KErrNone);
   243 	RDebug::Printf("busId(Interface) = %d",busId);
   244 	
   245 	// get device speed now
   246 	RUsbInterface::TDeviceSpeed deviceSpeed;
   247 	CHECK(iUsbInterface1.GetDeviceSpeed(deviceSpeed) == KErrNone);
   248 	RDebug::Printf("GetDeviceSpeed = %d", deviceSpeed);
   249 	CHECK(deviceSpeed == RUsbInterface::EFullSpeed);	
   250 		
   251 	RDebug::Printf("Number of alternate interface settings available: %d",iUsbInterface1.GetAlternateInterfaceCount());
   252 	gtest(iUsbInterface1.GetAlternateInterfaceCount() == 2);
   253 	
   254 	// Select alternate interface setting 1	
   255 	err = iUsbInterface1.SelectAlternateInterface(1);
   256 	if(err != KErrNone)
   257 		{
   258 		TBuf<256> msg;
   259 		msg.Format(_L("<Error %d> Selecting alternate interface setting 1 on interface 1"),err);
   260 		RDebug::Print(msg);
   261 		iCaseStep = EFailed;
   262 		TTestCaseFailed request(err,msg);
   263 		iClientAction->SendRequest(request,this);
   264 		return;
   265 		}	
   266 	
   267 	// Open a pipe for endpoint (Bulk out)	
   268 	TInt endpointAddress;
   269 	err = GetEndpointAddress(iUsbInterface1,1,KTransferTypeBulk,KEpDirectionOut,endpointAddress);
   270 	if(err != KErrNone)
   271 		{
   272 		TBuf<128> msg;
   273 		msg.Format(_L("<Error %d> Could not get address for Bulk out endpoint"),err);
   274 		RDebug::Print(msg);
   275 		TTestCaseFailed request(err,msg);
   276 		iClientAction->SendRequest(request,this);
   277 		return;
   278 		}
   279 		
   280 	RDebug::Printf("Opening pipe for endpoint address %02x on interface 1 setting 1",endpointAddress);
   281 	err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse);
   282 	if(err != KErrNone)
   283 		{
   284 		TBuf<128> msg;
   285 		msg.Format(_L("<Error %d> Unable to open pipe on interface 1 setting 0"),err);
   286 		RDebug::Print(msg);
   287 		iCaseStep = EFailed;
   288 		TTestCaseFailed request(err,msg);
   289 		iClientAction->SendRequest(request,this);
   290 		return;
   291 		}
   292 		
   293 	// get busId				
   294 	TUsbBusId busIdPipe;	 
   295 	CHECK(iTestPipe.GetBusId(busIdPipe) == KErrNone);
   296 	RDebug::Printf("busId(Pipe) = %d",busIdPipe);
   297 	CHECK(busIdPipe == busId);
   298 	
   299 	TUsbEndpointId usbEpId;
   300 	CHECK(iTestPipe.GetEndpointId(usbEpId) == KErrNone);
   301 	RDebug::Printf("EndpointId = %d",usbEpId); 
   302 	
   303 	// check ep descriptor now				 
   304 	RDebug::Printf("check ep descriptor now");
   305 	TUsbEndpointDescriptor epDescriptor;
   306 	
   307 
   308 	CHECK(iTestPipe.GetEndpointDescriptor(epDescriptor) == KErrNone);
   309 	TUsbEndpointDescriptor::Cast(&epDescriptor);
   310 	CHECK(&epDescriptor != 0);
   311 	CHECK(epDescriptor.iFirstChild == 0); // no children	
   312 	CHECK(epDescriptor.iNextPeer != 0);	// 1 peer
   313 
   314 		
   315 	RDebug::Printf("Pipe established now closing");
   316 	iTestPipe.Close();
   317 	
   318 	// Select alternate interface 2, error expected
   319 	err = iUsbInterface1.SelectAlternateInterface(2);
   320 	if(err != KErrNone)
   321 		{
   322 		RDebug::Printf("<Warning %d> Selecting alternate interface 2",err);
   323 		RDebug::Printf("...Rolling Back....");
   324 		
   325 		// Establish pipes on rollback
   326 		// Open a pipe for endpoint (Bulk out)		
   327 		err = iUsbInterface1.OpenPipeForEndpoint(iTestPipe,endpointAddress,EFalse);
   328 		if(err != KErrNone)
   329 			{
   330 			TBuf<128> msg;
   331 			msg.Format(_L("<Error %d> Unable to open pipe on interface 1 setting 1 for rollback case"),err);
   332 			RDebug::Print(msg);
   333 			iCaseStep = EFailed;
   334 			TTestCaseFailed request(err,msg);
   335 			iClientAction->SendRequest(request,this);
   336 			return;
   337 			}
   338 		
   339 		RDebug::Printf("Pipe established on rollback now closing");
   340 		iTestPipe.Close();
   341 		}
   342 	
   343 	// Inform client device test case successful	
   344 	iCaseStep = EPassed;
   345 	TTestCasePassed request;
   346 	iClientAction->SendRequest(request,this);
   347 	}
   348 	
   349 	
   350 void CUT_PBASE_T_USBDI_0477::DeviceRemovedL(TUint aDeviceHandle)
   351 	{
   352 	LOG_FUNC
   353 
   354 	// The test device should not be removed until the test case has passed
   355 	// so this test case has not completed, and state this event as an error
   356 
   357 	TestFailed(KErrDisconnected);
   358 	}
   359 	
   360 	
   361 void CUT_PBASE_T_USBDI_0477::BusErrorL(TInt aError)
   362 	{
   363 	LOG_FUNC
   364 
   365 	// This test case handles no failiures on the bus
   366 	
   367 	TestFailed(aError);
   368 	}
   369 	
   370 	
   371 void CUT_PBASE_T_USBDI_0477::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,
   372 	RUsbDevice::TDeviceState aNewState,TInt aCompletionCode)
   373 	{
   374 	LOG_FUNC
   375 	Cancel();
   376 	}
   377 
   378 void CUT_PBASE_T_USBDI_0477::Ep0TransferCompleteL(TInt aCompletionCode)
   379 	{
   380 	LOG_FUNC
   381 	
   382 	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
   383 	
   384 	if(iCaseStep == EPassed)
   385 		{
   386 		TestPassed();
   387 		}
   388 		
   389 	if(iCaseStep == EFailed)
   390 		{
   391 		TestFailed(KErrCompletion);
   392 		}
   393 	}
   394 	
   395 	
   396 void CUT_PBASE_T_USBDI_0477::HostRunL()
   397 	{
   398 	LOG_FUNC
   399 
   400 	// Obtain the completion code
   401 	TInt completionCode(iStatus.Int());
   402 	
   403 	if(completionCode == KErrNone)
   404 		{
   405 		// Action timeout
   406 		RDebug::Printf("<Error> Action timeout");
   407 		TestFailed(KErrTimedOut);
   408 		}
   409 	else
   410 		{
   411 		RDebug::Printf("<Error %d> Timeout timer could not complete",completionCode);
   412 		TestFailed(completionCode);
   413 		}
   414 	}
   415 
   416 void CUT_PBASE_T_USBDI_0477::DeviceRunL()
   417 	{
   418 	LOG_FUNC
   419 	
   420 	// Disconnect the device
   421 	
   422 	iTestDevice->SoftwareDisconnect();
   423 	
   424 	// Complete the test case request
   425 	
   426 	TestPolicy().SignalTestComplete(iStatus.Int());
   427 	}
   428 	
   429 	
   430 	}
   431