os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0486.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-0486.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-0486.h"
    20 #include "testpolicy.h"
    21 #include "testdebug.h"
    22 #include "modelleddevices.h"
    23 
    24 namespace NUnitTesting_USBDI
    25 	{
    26 	
    27 _LIT(KTestCaseId,"PBASE-T_USBDI-0486");
    28 const TFunctorTestCase<CUT_PBASE_T_USBDI_0486,TBool> CUT_PBASE_T_USBDI_0486::iFunctor(KTestCaseId);	
    29 const TInt KBulkTranferId = 2;
    30 const TInt KExpectedDataSize = 26;
    31 
    32 CUT_PBASE_T_USBDI_0486* CUT_PBASE_T_USBDI_0486::NewL(TBool aHostRole)
    33 	{
    34 	CUT_PBASE_T_USBDI_0486* self = new (ELeave) CUT_PBASE_T_USBDI_0486(aHostRole);
    35 	CleanupStack::PushL(self);
    36 	self->ConstructL();
    37 	CleanupStack::Pop(self);
    38 	return self;  
    39 	} 
    40 	
    41 
    42 CUT_PBASE_T_USBDI_0486::CUT_PBASE_T_USBDI_0486(TBool aHostRole)
    43 :	CBaseBulkTestCase(KTestCaseId,aHostRole),
    44 	iCaseStep(EWaitForDeviceCConnection)
    45 	{
    46 	} 
    47 
    48 
    49 void CUT_PBASE_T_USBDI_0486::ConstructL()
    50 	{
    51 	BaseBulkConstructL();
    52 	iTestDeviceC = new RUsbDeviceC(this);  // TODO check tree for device C, once inserted
    53 	}
    54 
    55 CUT_PBASE_T_USBDI_0486::~CUT_PBASE_T_USBDI_0486()
    56 	{
    57 	LOG_FUNC
    58 	
    59 	delete iTestDeviceC;
    60 	}
    61 	
    62 /**
    63 Called when the device has reported any kind of error in its operation
    64 or when the device has been informed by the host to report success
    65 */
    66 void CUT_PBASE_T_USBDI_0486::DeviceRunL()
    67 	{
    68 	LOG_FUNC	
    69 	// Complete the test case request	
    70 	TestPolicy().SignalTestComplete(iStatus.Int());
    71 	}
    72 
    73 void CUT_PBASE_T_USBDI_0486::DeviceDoCancel()
    74 	{
    75 	LOG_FUNC
    76 	iTestDeviceC->CancelSubscriptionToReports();
    77 	}
    78 	
    79 TBool CUT_PBASE_T_USBDI_0486::CheckSN(const TDesC16& aSerialNumberGot, const TDesC& aExpectedSerialNumber)
    80 	{
    81 	TBool areSNsIdenticals = (aSerialNumberGot.Compare(aExpectedSerialNumber) == 0);	
    82 	 
    83 	if(!areSNsIdenticals)
    84 		{
    85 		// Incorrect device for this test case	
    86 		RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
    87 			KErrNotFound,&aSerialNumberGot, &aExpectedSerialNumber);
    88 		}
    89 	return areSNsIdenticals;
    90 	}
    91 	
    92 	
    93 void CUT_PBASE_T_USBDI_0486::Ep0TransferCompleteL(TInt aCompletionCode)
    94 	{
    95 	LOG_FUNC
    96 
    97 	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
    98 	RDebug::Printf("--->Ep0TransferCompleteL, case step = %d", iCaseStep);
    99 
   100 	switch(iCaseStep)
   101 		{
   102 		case EDeviceCConnected:
   103 			{
   104 			if(aCompletionCode != KErrNone)
   105 				{
   106 				RDebug::Printf("<Error %d> aCompletionCode != KErrNone",aCompletionCode);
   107 				return TestFailed(aCompletionCode);
   108 				}
   109 			// close interface 0			
   110 			iUsbInterface0.Close();			
   111 			iCaseStep = EWaitForDeviceCDisconnection;
   112 			}
   113 			break; 
   114 			
   115 		case EPassed:
   116 			TestPassed();
   117 			break;		
   118 	
   119 		default:
   120 			TestFailed(KErrCompletion);
   121 			break;
   122 		}
   123 	}
   124 	
   125 void CUT_PBASE_T_USBDI_0486::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
   126 	{
   127 	LOG_FUNC
   128 	Cancel();
   129 	RDebug::Printf("--->TransferCompleteL, case step = %d", iCaseStep);
   130 
   131 	if(aTransferId == KBulkTranferId)
   132 		{							
   133 		if(aCompletionCode != KErrCancel && aCompletionCode != KErrUsbIOError)
   134 			{
   135 			TBuf<256> msg;
   136 			msg.Format(_L("<Error %d> The transfer completed with no errors but should have done so"),aCompletionCode);
   137 			RDebug::Print(msg);
   138 			TTestCaseFailed request(KErrCorrupt,msg);
   139 			return iControlEp0->SendRequest(request,this);
   140 			} 					
   141 		}
   142 	else
   143 		{
   144 		RDebug::Printf("<Error> a transfer completed (id=%d) that was not expected",aTransferId);
   145 		return TestFailed(KErrCorrupt);
   146 		}	
   147 	}	
   148 	
   149 	
   150 void CUT_PBASE_T_USBDI_0486::DeviceInsertedL(TUint aDeviceHandle)
   151 	{
   152 	LOG_FUNC
   153 	RDebug::Printf("--->DeviceInsertedL, case step = %d", iCaseStep);
   154 
   155 	// Cancel the timeout timer	
   156 	Cancel();	
   157 	TInt err(KErrNone);
   158 	
   159 	// Validate that device is as expected	
   160 	CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
   161 
   162 	// Perform the correct test step		
   163 	switch(iCaseStep)
   164 		{
   165 		case EWaitForDeviceCConnection:
   166 			{
   167 			if(!CheckSN(testDevice.SerialNumber(), KTestDeviceC_SN()))
   168 				{
   169 				return TestFailed(KErrNotFound);
   170 				}
   171 			iCaseStep = EDeviceCConnected;
   172 			
   173 			err = testDevice.Device().GetTokenForInterface(0,iToken0DeviceC);
   174 			if(err != KErrNone)
   175 				{
   176 				RDebug::Printf("<Error %d> Unable to retrieve token for interface 0",err);
   177 				TestFailed(err);
   178 				} 
   179 			// Open the interface	
   180 			err = iUsbInterface0.Open(iToken0DeviceC);
   181 			if(err != KErrNone)
   182 				{
   183 				RDebug::Printf("<Error %d> Unable to open interface 0");
   184 				TestFailed(err);
   185 				}
   186 				
   187 			TUint32 token1;
   188 			err = testDevice.Device().GetTokenForInterface(1,token1);
   189 			if(err != KErrNone)
   190 				{
   191 				TBuf<256> msg;
   192 				msg.Format(_L("<Error %d> Token for interface 1 could not be retrieved"),err);
   193 				RDebug::Print(msg);
   194 				iCaseStep = EFailed;
   195 				TTestCaseFailed request(err,msg);
   196 				return iControlEp0->SendRequest(request,this);
   197 				}
   198 				
   199 				
   200 			err = iUsbInterface1.Open(token1); // Default interface setting 0
   201 			if(err != KErrNone)
   202 				{
   203 				TBuf<256> msg;
   204 				msg.Format(_L("<Error %d> Unable to open interface 1 using token %d"),err,token1);
   205 				RDebug::Print(msg);
   206 				iCaseStep = EFailed;
   207 				TTestCaseFailed request(err,msg);
   208 				return iControlEp0->SendRequest(request,this);
   209 				}
   210 				
   211 			TInt endpointAddress;
   212 			// Queue a bulk in transfer on endpoint 2	
   213 			err = GetEndpointAddress(iUsbInterface1,0,KTransferTypeBulk,KEpDirectionIn,endpointAddress);
   214 			if(err != KErrNone)
   215 				{
   216 				TBuf<256> msg;
   217 				msg.Format(_L("<Error %d> Address for bulk in endpoint could not be obtained"),err);
   218 				RDebug::Print(msg);
   219 				iCaseStep = EFailed;
   220 				TTestCaseFailed request(err,msg);
   221 				return iControlEp0->SendRequest(request,this);
   222 				}
   223 			RDebug::Printf("Endpoint adress %08x",endpointAddress);
   224 	
   225 			err = iUsbInterface1.OpenPipeForEndpoint(iTestPipeInterface1BulkIn,endpointAddress,ETrue);
   226 			if(err != KErrNone)
   227 				{
   228 				TBuf<256> msg;
   229 				msg.Format(_L("<Error %d> Unable to open pipe for endpoint %08x"),err,endpointAddress);
   230 				RDebug::Print(msg);
   231 				iCaseStep = EFailed;
   232 				TTestCaseFailed request(err,msg);
   233 				return iControlEp0->SendRequest(request,this);
   234 				}
   235 			
   236 			// Create the bulk transfers	
   237 			iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,256,*this, KBulkTranferId);
   238 		
   239 			// Initialise the descriptors for transfer		
   240 			RDebug::Printf("Initialising the transfer descriptors");
   241 			err = iUsbInterface1.InitialiseTransferDescriptors();
   242 			if(err != KErrNone)
   243 				{
   244 				TBuf<256> msg;
   245 				msg.Format(_L("<Error %d> Unable to initialise transfer descriptors"),err);
   246 				RDebug::Print(msg);
   247 				iCaseStep = EFailed;
   248 				TTestCaseFailed request(err,msg);
   249 				return iControlEp0->SendRequest(request,this);
   250 				}				
   251 			iInTransfer[0]->TransferIn(KExpectedDataSize);
   252 					
   253 			// 	disconnect device C and connect device A								
   254 			TDisconnectDeviceCThenConnectDeviceARequest request;
   255 			iControlEp0->SendRequest(request,this);	
   256 	        										
   257 			// Monitor for the reconnection from the client		
   258 			iActorFDF->Monitor();
   259 			TimeoutIn(30);
   260 						
   261 			}
   262 			break; 
   263 					
   264 		case EWaitForDeviceAConnection:
   265 			{
   266 			if(!CheckSN(testDevice.SerialNumber(), TestCaseId()))
   267 				{
   268 				return TestFailed(KErrNotFound);
   269 			    }
   270 			// try to open interface 0			
   271 			err = iUsbInterface0.Open(iToken0DeviceC);
   272 			if(err != KErrNotFound) // invalid token
   273 				{
   274 				RDebug::Printf("iUsbInterface0.Open(iToken0DeviceC) === %d", err);
   275 				TestFailed(err);
   276 				}
   277 			
   278 			// get token now 
   279 			TUint32 token0DeviceA; 
   280 			err = testDevice.Device().GetTokenForInterface(0,token0DeviceA);
   281 			if(err != KErrNone)
   282 				{
   283 				RDebug::Printf("<Error %d> Unable to retrieve token(device A) for interface 0",err);
   284 				TestFailed(err);
   285 				} 
   286 			// Open the interface	
   287 			err = iUsbInterface0.Open(token0DeviceA);
   288 			if(err != KErrNone)
   289 				{
   290 				RDebug::Printf("<Error %d> Unable to open interface 0(device A)");
   291 				TestFailed(err);
   292 				}
   293 			
   294 			// ok, send EPassed request			
   295 			iCaseStep = EPassed;
   296 			// Send test case passed request to client
   297 			User::After(2000000);	
   298 			TTestCasePassed request;
   299 			iControlEp0->SendRequest(request,this);			
   300 			} 
   301 			break;
   302 			
   303 		default:
   304 			{
   305 			RDebug::Printf("<Error> Test case actions out of sync");
   306 			TestFailed(KErrCorrupt);
   307 			}	
   308 			break;
   309 		}	
   310 	}
   311 	
   312 void CUT_PBASE_T_USBDI_0486::DeviceRemovedL(TUint aDeviceHandle)
   313 	{
   314 	LOG_FUNC
   315 	Cancel();	
   316 	RDebug::Printf("--->DeviceRemovedL, case step = %d", iCaseStep);
   317 
   318 	switch(iCaseStep)
   319 		{
   320 		case EWaitForDeviceCDisconnection: // device C is disconnected now, interface 0 has been closed before
   321 			{
   322 			iCaseStep = EWaitForDeviceAConnection;
   323 			iActorFDF->Monitor();
   324 			}
   325 			break;		
   326 	
   327 		default:
   328 			RDebug::Printf("<Error> Test case actions out of sync");
   329 			TestFailed(KErrCorrupt);
   330 			break;
   331 			}
   332 	}
   333 	
   334 void CUT_PBASE_T_USBDI_0486::HandleDeviceDConnection()
   335 	{
   336 	LOG_FUNC
   337 		
   338 	iTestDevice->SubscribeToReports(iStatus);
   339 	SetActive();
   340 	}
   341 
   342 void CUT_PBASE_T_USBDI_0486::ExecuteDeviceTestCaseL()	
   343 	{ 
   344 	LOG_FUNC
   345 	
   346 	// Create the test device	
   347 	iTestDeviceC->OpenL(KTestDeviceC_SN());
   348 	iTestDeviceC->SubscribeToReports(iStatus);
   349 	SetActive();
   350 	
   351 	// Connect the device to the host	
   352 	iTestDeviceC->SoftwareConnect();
   353 	}
   354 
   355 RUsbDeviceC* CUT_PBASE_T_USBDI_0486::TestDeviceC()
   356 	{
   357 	return iTestDeviceC;
   358 	}
   359 
   360 RUsbDeviceD* CUT_PBASE_T_USBDI_0486::TestDeviceD()
   361 	{
   362 	return iTestDevice; //from CBaseBulkTestCase
   363 	}
   364 	
   365 	}