os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0481.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-0481.cpp
    15 // @internalComponent
    16 // 
    17 //
    18 
    19 #include "PBASE-T_USBDI-0481.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-0481");
    28 const TFunctorTestCase<CUT_PBASE_T_USBDI_0481,TBool> CUT_PBASE_T_USBDI_0481::iFunctor(KTestCaseId);	
    29 
    30 CUT_PBASE_T_USBDI_0481* CUT_PBASE_T_USBDI_0481::NewL(TBool aHostRole)
    31 	{
    32 	CUT_PBASE_T_USBDI_0481* self = new (ELeave) CUT_PBASE_T_USBDI_0481(aHostRole);
    33 	CleanupStack::PushL(self);
    34 	self->ConstructL();
    35 	CleanupStack::Pop(self);
    36 	return self;
    37 	} 
    38 	
    39 
    40 CUT_PBASE_T_USBDI_0481::CUT_PBASE_T_USBDI_0481(TBool aHostRole)
    41 :	CBaseTestCase(KTestCaseId,aHostRole),
    42 	iCaseStep(EInProgress)
    43 	{
    44 	} 
    45 
    46 
    47 void CUT_PBASE_T_USBDI_0481::ConstructL()
    48 	{
    49 	iTestDevice = new RUsbDeviceA(this);
    50 	BaseConstructL();
    51 	}
    52 
    53 
    54 CUT_PBASE_T_USBDI_0481::~CUT_PBASE_T_USBDI_0481()
    55 	{
    56 	LOG_FUNC
    57 	
    58 	Cancel();
    59 	
    60 	// Close interfaces	
    61 	iUsbInterface0.Close();
    62 	iUsbInterface1.Close();
    63 	
    64 	// Free resources
    65 	delete iControlEp0;
    66 	delete iActorFDF;
    67 	if(!IsHost() && iTestDevice)
    68 		{
    69 		iTestDevice->Close();
    70 		}		
    71 	delete iTestDevice;
    72 	}
    73 	
    74 void CUT_PBASE_T_USBDI_0481::ExecuteHostTestCaseL()	
    75 	{
    76 	LOG_FUNC
    77 
    78 	// Create the actor for the Function Driver Framework 	
    79 	iActorFDF = CActorFDF::NewL(*this);
    80 
    81 	// Create the control transfer for requests
    82 	iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
    83 	
    84 	// Monitor for devices
    85 	iActorFDF->Monitor();
    86 	
    87 	// Start the connection timeout timer
    88 	TimeoutIn(30);
    89 	}
    90 
    91 
    92 void CUT_PBASE_T_USBDI_0481::ExecuteDeviceTestCaseL()	
    93 	{
    94 	LOG_FUNC
    95 	
    96 	// Create the test device	
    97 	iTestDevice->OpenL(TestCaseId());
    98 	iTestDevice->SubscribeToReports(iStatus);
    99 	SetActive();
   100 	
   101 	// Connect the device to the host	
   102 	iTestDevice->SoftwareConnect();
   103 	}
   104 
   105 
   106 void CUT_PBASE_T_USBDI_0481::HostDoCancel()
   107 	{
   108 	LOG_FUNC
   109 	
   110 	// Cancel the test step action timeout timer	
   111 	CancelTimeout();
   112 	}
   113 
   114 	
   115 void CUT_PBASE_T_USBDI_0481::DeviceDoCancel()
   116 	{
   117 	LOG_FUNC
   118 	
   119 	// Cancel the device (the activity timer and the error reporting)	
   120 	iTestDevice->CancelSubscriptionToReports();
   121 	}
   122 	
   123 	
   124 void CUT_PBASE_T_USBDI_0481::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,
   125 		RUsbDevice::TDeviceState aNewState,TInt aCompletionCode)
   126 	{
   127 	LOG_FUNC
   128 	
   129 	}
   130 	
   131 	
   132 void CUT_PBASE_T_USBDI_0481::DeviceInsertedL(TUint aDeviceHandle)
   133 	{
   134 	LOG_FUNC
   135 
   136 	// Cancel the timeout timer	
   137 	Cancel();
   138 	
   139 	TInt err(KErrNone);
   140 	
   141 	// Validate that device is as expected	
   142 	CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
   143 	if(testDevice.SerialNumber().Compare(TestCaseId()) != 0)
   144 		{
   145 		// Incorrect device for this test case
   146 		RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
   147 			KErrNotFound,&testDevice.SerialNumber(),&TestCaseId());
   148 
   149 		// Start the connection timeout again
   150 		TimeoutIn(30);
   151 		return;
   152 		}	
   153 	
   154 	// Get the token for interface 0	
   155 	err = testDevice.Device().GetTokenForInterface(0,iToken0);
   156 	if(err != KErrNone)
   157 		{
   158 		RDebug::Printf("<Error %d> Unable to retrieve token for interface 0",err);
   159 		TestFailed(err);
   160 		}
   161 	
   162 	// Open interface 0
   163 	err = iUsbInterface0.Open(iToken0);
   164 	if(err != KErrNone)
   165 		{
   166 		RDebug::Printf("<Error %d> Unable to open interface 0");
   167 		TestFailed(err);
   168 		}
   169 		
   170 		// Get the token for interface 1	
   171 	TUint32 token1(0);
   172 	err = testDevice.Device().GetTokenForInterface(1,token1);
   173 	if(err != KErrNone)
   174 		{
   175 		RDebug::Printf("<Error %d> token1 for interface 1",err);
   176 		return TestFailed(err);
   177 		}
   178 	// Open interface 1
   179 	err = iUsbInterface1.Open(token1); // Alternate interface setting 0
   180 	if(err != KErrNone)
   181 		{
   182 		RDebug::Printf("<Error %d> Unable to open interface 1");
   183 		return TestFailed(err);
   184 		}
   185 
   186 	// Get the token for interface 1 again, fails
   187 	TUint32 token1Bis(0);
   188 	err = testDevice.Device().GetTokenForInterface(1,token1Bis);
   189 	if(err != KErrInUse)
   190 		{
   191 		RDebug::Printf("GetTokenForInterface(1,token1Bis), err != KErrInUse");
   192 		return TestFailed(err);
   193 		}
   194 		
   195 	// Open interface 1 again, fails
   196 	RDebug::Printf("open it twice, catch error Code");
   197 	err = iUsbInterface1.Open(token1); // Alternate interface setting 0
   198 	if(err != KErrInUse)
   199 		{
   200 		RDebug::Printf("iUsbInterface1.Open(token1), err != KErrInUse");
   201 		return TestFailed(err);
   202 		}
   203 
   204 	// test ok
   205 	User::After(1000000);
   206 	iCaseStep = EPassed;
   207 	TTestCasePassed request;
   208 	iControlEp0->SendRequest(request,this);	
   209 	}
   210 	
   211 void CUT_PBASE_T_USBDI_0481::Ep0TransferCompleteL(TInt aCompletionCode)
   212 	{
   213 	LOG_FUNC
   214 	
   215 	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
   216 	
   217 	if(aCompletionCode != KErrNone)
   218 		{	
   219 		TBuf<256> msg;
   220 		msg.Format(_L("<Error %d> Transfer to control endpoint 0 was not successful"),aCompletionCode);
   221 		RDebug::Print(msg);
   222 		}
   223 
   224 	if(iCaseStep == EPassed)
   225 		{	
   226 		if(aCompletionCode == KErrNone)
   227 			{
   228 			return TestPassed();
   229 			}
   230 		// else error
   231 	    iCaseStep = EFailed;
   232 		}
   233 	
   234 	if(iCaseStep == EFailed)
   235 		{
   236 		return TestFailed(KErrCompletion);
   237 		}
   238 	}
   239 	
   240 void CUT_PBASE_T_USBDI_0481::DeviceRemovedL(TUint aDeviceHandle)
   241 	{
   242 	LOG_FUNC
   243 
   244 	// The test device should not be removed until the test case has passed
   245 	// so this test case has not completed, and state this event as an error
   246 	TestFailed(KErrDisconnected);
   247 	}
   248 	
   249 	
   250 void CUT_PBASE_T_USBDI_0481::BusErrorL(TInt aError)
   251 	{
   252 	LOG_FUNC
   253 
   254 	// This test case handles no failiures on the bus
   255 	TestFailed(aError);
   256 	}
   257 
   258 
   259 void CUT_PBASE_T_USBDI_0481::HostRunL()
   260 	{
   261 	LOG_FUNC
   262 	
   263 	// Obtain the completion code
   264 	TInt completionCode(iStatus.Int());
   265 	
   266 	if(completionCode == KErrNone)
   267 		{
   268 		// Action timeout
   269 		RDebug::Printf("<Error> Action timeout");
   270 		TestFailed(KErrTimedOut);
   271 		}
   272 	else
   273 		{
   274 		RDebug::Printf("<Error %d> Timeout timer could not complete",completionCode);
   275 		TestFailed(completionCode);
   276 		}
   277 	}
   278 
   279 /**
   280 Called when the device has reported any kind of error in its opertaion
   281 or when the device has been informed by the host to report success
   282 */
   283 void CUT_PBASE_T_USBDI_0481::DeviceRunL()
   284 	{
   285 	LOG_FUNC	
   286 	// Disconnect the device	
   287 	iTestDevice->SoftwareDisconnect();	
   288 	// Complete the test case request	
   289 	TestPolicy().SignalTestComplete(iStatus.Int());
   290 	}
   291 
   292 	
   293 	}