Update contrib.
1 // Copyright (c) 2008-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // @file BaseBulkTestCase.cpp
19 #include "BaseBulkTestCase.h"
20 #include "testpolicy.h"
21 #include "modelleddevices.h"
25 namespace NUnitTesting_USBDI
28 //*****************************************************************************************************
31 CBulkTestTimer* CBulkTestTimer::NewL(MBulkTestTimerObserver& aParent)
33 CBulkTestTimer* self = new (ELeave) CBulkTestTimer(aParent);
34 CleanupStack::PushL(self);
36 CleanupStack::Pop(self);
41 CBulkTestTimer::CBulkTestTimer(MBulkTestTimerObserver& aParent)
42 : CTimer(EPriorityStandard),
45 CActiveScheduler::Add(this);
49 CBulkTestTimer::~CBulkTestTimer()
54 void CBulkTestTimer::ConstructL()
61 void CBulkTestTimer::RunL()
65 iParent.HandleBulkTestTimerFired();
70 //*****************************************************************************************************
73 //Bulk Test Case Base Class
74 CBaseBulkTestCase::CBaseBulkTestCase(const TDesC& aTestCaseId,TBool aHostFlag, TBool aHostOnly)
75 : CBaseTestCase(aTestCaseId, aHostFlag, aHostOnly),
77 iOutBufferPtr(NULL,0),
78 iValidateBufferPtr(NULL,0)
84 void CBaseBulkTestCase::BaseBulkConstructL()
86 iTestDevice = new RUsbDeviceD(this);
91 CBaseBulkTestCase::~CBaseBulkTestCase()
97 //Do this before deleting the transfer objects
98 //NB this should do nothing if already called from a derived test class
99 CloseInterfaceAndPipes();
101 delete iValidateBuffer;
105 delete iBulkTestTimer;
108 for(count=0;count<KMaxNumOutTransfers;count++)
110 delete iOutTransfer[count];
112 for(count=0;count<KMaxNumInTransfers;count++)
114 delete iInTransfer[count];
119 if(!IsHost() && iTestDevice)
121 iTestDevice->Close();
126 void CBaseBulkTestCase::ExecuteHostTestCaseL()
130 iActorFDF = CActorFDF::NewL(*this);
131 iControlEp0 = new (ELeave) CEp0Transfer(iUsbInterface0);
132 iActorFDF->Monitor();
136 void CBaseBulkTestCase::HostDoCancel()
140 // Cancel the test step timeout timer
146 void CBaseBulkTestCase::ExecuteDeviceTestCaseL()
150 iTestDevice->OpenL(TestCaseId());
151 iTestDevice->SubscribeToReports(iStatus);
154 // Connect the device to the host
156 iTestDevice->SoftwareConnect();
159 void CBaseBulkTestCase::DeviceDoCancel()
163 // Cancel the test device error reports
165 iTestDevice->CancelSubscriptionToReports();
169 void CBaseBulkTestCase::DeviceStateChangeL(RUsbDevice::TDeviceState aPreviousState,
170 RUsbDevice::TDeviceState aNewState,TInt aCompletionCode)
177 TInt CBaseBulkTestCase::BaseBulkDeviceInsertedL(TUint aDeviceHandle)
180 RDebug::Printf("this - %08x", this);
181 return BaseBulkDeviceInsertedL(aDeviceHandle, EFalse);
184 void CBaseBulkTestCase::DeviceInsertedL(TUint aDeviceHandle)
186 //to be implemnted in individual test cases, possibly with the help of BaseBulkDeviceInsertedL
187 BaseBulkDeviceInsertedL(aDeviceHandle);
190 TInt CBaseBulkTestCase::BaseBulkDeviceInsertedL(TUint aDeviceHandle, TBool aUseTwoInterfaces)
195 // Validate connected device
196 CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
198 RDebug::Printf("device serial number (%S)",&testDevice.SerialNumber());
199 RDebug::Printf("Manufacturer (%S)",&testDevice.Manufacturer());
200 RDebug::Printf("Product (%S)",&testDevice.Product());
201 RDebug::Printf("ProductId (%d)",testDevice.ProductId());
202 RDebug::Printf("VendorId (%d)",testDevice.VendorId());
204 if(testDevice.SerialNumber().Compare(TestCaseId()) != 0)
206 // Incorrect device for this test case
208 RDebug::Printf("<Warning %d> Incorrect device serial number (%S) connected for this test case (%S)",
209 KErrNotFound,&testDevice.SerialNumber(),&TestCaseId());
211 // Start the connection timeout again
213 return EDeviceConfigurationError;
218 err = testDevice.Device().GetTokenForInterface(0,token0);
221 RDebug::Printf("<Error %d> Token for interface 0 could not be retrieved",err);
223 // Start the connection timeout again
225 return EDeviceConfigurationError;
227 err = iUsbInterface0.Open(token0); // Default interface setting 0
230 RDebug::Printf("<Error %d> Unable to open interface 0 using token %d",err,token0);
231 // Start the connection timeout again
233 return EDeviceConfigurationError;
236 err = SetUpInterfaceAndPipesL(aDeviceHandle, 1);
238 //msg already setup, and failure message sent
240 return EDeviceConfigurationError;
246 TInt CBaseBulkTestCase::SetUpInterfaceAndPipesL(TUint aDeviceHandle, TUint8 aInterfaceNum)
250 TInt endpointAddress;
251 RUsbInterface* pTestInterface = NULL;
252 RUsbPipe* pTestPipeBulkIn = NULL;
253 RUsbPipe* pTestPipeBulkOut1 = NULL;
254 RUsbPipe* pTestPipeBulkOut2 = NULL;
255 CUsbTestDevice& testDevice = iActorFDF->DeviceL(aDeviceHandle);
257 switch(aInterfaceNum)
260 pTestInterface = &iUsbInterface1;
261 pTestPipeBulkIn = &iTestPipeInterface1BulkIn;
262 pTestPipeBulkOut1 = &iTestPipeInterface1BulkOut;
263 pTestPipeBulkOut2 = NULL;
266 pTestInterface = &iUsbInterface2;
267 pTestPipeBulkIn = &iTestPipeInterface2BulkIn;
268 pTestPipeBulkOut1 = &iTestPipeInterface2BulkOut1;
269 pTestPipeBulkOut2 = &iTestPipeInterface2BulkOut2;
272 User::Panic(_L("Bulk Interface Number Out Of Range"), KErrArgument);
276 RDebug::Printf("this - %08x", this);
279 err = testDevice.Device().GetTokenForInterface(aInterfaceNum,token);
283 msg.Format(_L("<Error %d> Token for interface 1 could not be retrieved"),err);
285 TTestCaseFailed request(err,msg);
286 return EDeviceConfigurationError;
288 if(pTestInterface != NULL)
290 err = pTestInterface->Open(token); // Default interface setting 1
294 msg.Format(_L("<Error %d> Unable to open interface 1 using token %d"),err,token);
296 TTestCaseFailed request(err,msg);
297 return EDeviceConfigurationError;
301 if(pTestPipeBulkIn != NULL)
303 err = GetEndpointAddress(*pTestInterface,0,KTransferTypeBulk,KEpDirectionIn,endpointAddress);
307 msg.Format(_L("<Error %d> Address for bulk in endpoint could not be obtained"),err);
309 TTestCaseFailed request(err,msg);
310 return EDeviceConfigurationError;
313 RDebug::Printf("IN Endpoint address %08x",endpointAddress);
315 err = pTestInterface->OpenPipeForEndpoint(*pTestPipeBulkIn,endpointAddress,ETrue);
319 msg.Format(_L("<Error %d> Unable to open pipe for endpoint %08x"),err,endpointAddress);
321 TTestCaseFailed request(err,msg);
322 return EDeviceConfigurationError;
326 if(pTestPipeBulkOut1 != NULL)
328 err = GetEndpointAddress(*pTestInterface,0,KTransferTypeBulk,KEpDirectionOut,endpointAddress);
332 msg.Format(_L("<Error %d> Address for(first) bulk out endpoint could not be obtained"),err);
334 TTestCaseFailed request(err,msg);
335 return EDeviceConfigurationError;
338 RDebug::Printf("OUT Endpoint address %08x",endpointAddress);
340 err = pTestInterface->OpenPipeForEndpoint(*pTestPipeBulkOut1,endpointAddress,ETrue);
344 msg.Format(_L("<Error %d> Unable to open pipe for endpoint %08x"),err,endpointAddress);
346 TTestCaseFailed request(err,msg);
347 return EDeviceConfigurationError;
351 if(pTestPipeBulkOut2 != NULL)
353 err = GetEndpointAddress(*pTestInterface,0,KTransferTypeBulk,KEpDirectionOut,1,endpointAddress);
357 msg.Format(_L("<Error %d> Address for(second) bulk out endpoint could not be obtained"),err);
359 TTestCaseFailed request(err,msg);
360 return EDeviceConfigurationError;
363 RDebug::Printf("OUT Endpoint address %08x",endpointAddress);
365 err = pTestInterface->OpenPipeForEndpoint(*pTestPipeBulkOut2,endpointAddress,ETrue);
369 msg.Format(_L("<Error %d> Unable to open pipe for endpoint %08x"),err,endpointAddress);
371 TTestCaseFailed request(err,msg);
372 return EDeviceConfigurationError;
379 void CBaseBulkTestCase::CloseInterfaceAndPipes()
383 // Close the pipe(s) before interface(s)
384 iTestPipeInterface2BulkIn.Close();
385 iTestPipeInterface2BulkOut1.Close();
386 iTestPipeInterface2BulkOut2.Close();
387 iTestPipeInterface1BulkIn.Close();
388 iTestPipeInterface1BulkOut.Close();
390 iUsbInterface2.Close();
391 iUsbInterface1.Close();
392 iUsbInterface0.Close();
395 void CBaseBulkTestCase::DeviceRemovedL(TUint aDeviceHandle)
399 // The test device should not be removed until the test case has passed
400 // so this test case has not completed, and state this event as an error
402 TestFailed(KErrDisconnected);
406 void CBaseBulkTestCase::BusErrorL(TInt aError)
410 // This test case handles no failiures on the bus
412 TestFailed(KErrCompletion);
415 void CBaseBulkTestCase::HostRunL()
417 // Obtain the completion code
418 TInt completionCode(iStatus.Int());
420 if(completionCode == KErrNone)
423 RDebug::Printf("<Error> Action timeout");
424 TestFailed(KErrTimedOut);
428 RDebug::Printf("<Error %d> Timeout timer could not complete",completionCode);
429 TestFailed(completionCode);
433 void CBaseBulkTestCase::DeviceRunL()
437 // Disconnect the device
438 iTestDevice->SoftwareDisconnect();
440 // Complete the test case request
441 TestPolicy().SignalTestComplete(iStatus.Int());
444 TBool CBaseBulkTestCase::ValidateData (const TDesC8& aDataToValidate, const TDesC8& aDataPattern)
446 return ValidateData(aDataToValidate, aDataPattern, aDataPattern.Length());
449 TBool CBaseBulkTestCase::ValidateData (const TDesC8& aDataToValidate, const TDesC8& aDataPattern, const TUint aNumBytes)
451 return ValidateData(aDataToValidate, aDataPattern, 0, aNumBytes);
454 TBool CBaseBulkTestCase::ValidateData (const TDesC8& aDataToValidate, const TDesC8& aDataPattern, const TUint aStartPoint, const TUint aNumBytes)
458 __ASSERT_DEBUG(aDataPattern.Length()!=0, User::Panic(_L("Trying to validate with ZERO LENGTH STRING"), KErrArgument));
460 if(aDataToValidate.Length()!=aNumBytes)
462 RDebug::Printf("ROUND TRIP VALIDATION: Length Match Failure, Sent = %d, Returned = %d", aNumBytes, aDataToValidate.Length());
465 TUint startPoint = aStartPoint%aDataPattern.Length();
466 TUint numStartBytes = (aDataPattern.Length() - startPoint)%aDataPattern.Length();
467 numStartBytes = aNumBytes<numStartBytes?aNumBytes:numStartBytes; //never test for more than aNumBytes
468 TUint fullRepeats = (aNumBytes-numStartBytes)/aDataPattern.Length();
469 TUint startEndPoint = (fullRepeats*aDataPattern.Length()) + numStartBytes;
470 TUint numEndBytes = aNumBytes - startEndPoint;//fullRepeats*aDataPattern.Length() - numStartBytes;
473 if(aDataToValidate.Left(numStartBytes).Compare(aDataPattern.Mid(startPoint, numStartBytes)) != 0)
475 RDebug::Printf("ROUND TRIP VALIDATION: Start Bytes Match Failure");
476 RDebug::Printf("ROUND TRIP VALIDATION: numStartBytes = %d", numStartBytes);
477 RDebug::Printf("Start of EXPECTED data ...");
478 RDebug::RawPrint(aDataPattern.Mid(startPoint, numStartBytes));
479 RDebug::Printf("\n");
480 RDebug::Printf("Start of RETURNED data ...");
481 RDebug::RawPrint(aDataToValidate.Left(numStartBytes));
482 RDebug::Printf("\n");
488 if(aDataToValidate.Mid(startEndPoint,numEndBytes).Compare(aDataPattern.Left(numEndBytes)) != 0)
490 RDebug::Printf("ROUND TRIP VALIDATION: End Bytes Match Failure");
491 RDebug::Printf("ROUND TRIP VALIDATION: startEndPoint = %d, numEndBytes = %d", startEndPoint, numEndBytes);
492 RDebug::Printf("End of EXPECTED data ...");
493 RDebug::RawPrint(aDataPattern.Left(numEndBytes));
494 RDebug::Printf("\n");
495 RDebug::Printf("End of RETURNED data ...");
496 RDebug::RawPrint(aDataToValidate.Mid(startEndPoint,numEndBytes));
497 RDebug::Printf("\n");
501 for(TInt i=0; i<fullRepeats; i++)
503 if(aDataToValidate.Mid(numStartBytes + i*aDataPattern.Length(),aDataPattern.Length()).Compare(aDataPattern) != 0)
505 RDebug::Printf("ROUND TRIP VALIDATION: Repeated Bytes Match Failure, Repeat %d",i);
506 RDebug::Printf("Middle block of EXPECTED data ...");
507 RDebug::RawPrint(aDataPattern);
508 RDebug::Printf("\n");
509 RDebug::Printf("Middle block of RETURNED data ...");
510 RDebug::RawPrint(aDataToValidate.Mid(numStartBytes + i*aDataPattern.Length(),aDataPattern.Length()));
511 RDebug::Printf("\n");
512 return EFalse; //from 'for' loop
518 void CBaseBulkTestCase::RecordTime(const TUint8 aTimerIndex)
521 if(aTimerIndex >= KMaxNumTimers)
523 RDebug::Printf("Record Timer with index %d called - index OUT OF RANGE", aTimerIndex);
524 User::Panic(_L("BAD TIMER INDEX"), KErrArgument);
526 iEndTime[aTimerIndex].HomeTime();
527 iTimeElapsed[aTimerIndex] = iEndTime[aTimerIndex].MicroSecondsFrom(iStartTime[aTimerIndex]);
528 RDebug::Printf("Timer with index %d completed in %d uSec", aTimerIndex, (TInt)(iTimeElapsed[aTimerIndex].Int64()));
531 TInt CBaseBulkTestCase::CheckTimes(const TUint8 aFirstTimerIndex, const TUint8 aSecondTimerIndex, const TUint aPercentage)
534 if(aFirstTimerIndex >= KMaxNumTimers)
536 RDebug::Printf("First timer with index %d called - index OUT OF RANGE", aFirstTimerIndex);
537 User::Panic(_L("BAD TIMER INDEX"), KErrArgument);
539 if(aSecondTimerIndex >= KMaxNumTimers)
541 RDebug::Printf("Second timer with index %d called - index OUT OF RANGE", aSecondTimerIndex);
542 User::Panic(_L("BAD TIMER INDEX"), KErrArgument);
546 RDebug::Printf("Transfer %d completed in %d uSec\nTransfer %d completed in %d uSec", aFirstTimerIndex, (TInt)(iTimeElapsed[aFirstTimerIndex].Int64()), aSecondTimerIndex, (TInt)(iTimeElapsed[aSecondTimerIndex].Int64()));
547 if(aPercentage*iTimeElapsed[aFirstTimerIndex].Int64() > KPercent*iTimeElapsed[aSecondTimerIndex].Int64())
550 RDebug::Printf("Time %d too big", aFirstTimerIndex);
552 if(aPercentage*iTimeElapsed[aSecondTimerIndex].Int64() > KPercent*iTimeElapsed[aFirstTimerIndex].Int64())
555 RDebug::Printf("Time %d too big", aSecondTimerIndex);
561 void CBaseBulkTestCase::ResetTimes(const TUint8 aTimerIndex)
563 iStartTime[aTimerIndex] = 0;
564 iEndTime[aTimerIndex] = 0;
565 iTimeElapsed[aTimerIndex] = 0;
568 TInt CBaseBulkTestCase::CheckAndResetTimes(const TUint8 aFirstTimerIndex, const TUint8 aSecondTimerIndex, const TUint aPercentage)
572 TInt ret = CheckTimes(aFirstTimerIndex, aSecondTimerIndex, aPercentage);
573 ResetTimes(aFirstTimerIndex);
574 ResetTimes(aSecondTimerIndex);
579 void CBaseBulkTestCase::HandleBulkTestTimerFired()
581 //do nothing here - leave to derived class if required