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