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 PBASE-T_USBDI-1230.cpp
19 #include "PBASE-T_USBDI-1230.h"
20 #include "testpolicy.h"
21 #include "modelleddevices.h"
22 #include "testliterals.h"
27 namespace NUnitTesting_USBDI
29 const TUint KTotalBytesToTransfer = 1024*64+511; //64kB + 511 bytes
30 const TUint KHostNumReadBytes = 1024*16;
31 const TInt KBulkMaxTransferSize = KHostNumReadBytes + 1000;
32 const TInt KDeviceNumWriteBytes = 1024;
35 //Make these single bit values ...
36 // ... so that their completion can be easily recorded in a bit mask!
37 const TUint32 KBulkTransferInId0 = 1<<0;
38 const TUint32 KBulkTransferInId1 = 1<<1;
39 const TUint32 KBulkTransferInId[KMaxNumInTransfers] = {KBulkTransferInId0, KBulkTransferInId1};
40 const TUint32 KBulkTransferIdMask = KBulkTransferInId[0] | KBulkTransferInId[1];
42 const TInt KUndefinedStep = -102;
43 const TInt KUnexpectedTransferID = -103;
44 const TInt KErrReturnedDeviceReadBytesTooVariable = -140;
45 const TInt KTransferSuccess = +100;
47 const TUint KRepeatedTimerInterval = 10000; //10ms
48 const TUint KBaseTimer = 0;
49 const TUint KTestTimer = 1;
50 const TUint KMaxTimeDiffPercentage = 60; //on inspection worst result was just under 70%
51 const TUint KMaxBytesWrittenDiffPercentage = 50;
56 _LIT(KTestCaseId,"PBASE-T_USBDI-1230");
57 const TFunctorTestCase<CUT_PBASE_T_USBDI_1230,TBool> CUT_PBASE_T_USBDI_1230::iFunctor(KTestCaseId);
59 CUT_PBASE_T_USBDI_1230* CUT_PBASE_T_USBDI_1230::NewL(TBool aHostRole)
61 CUT_PBASE_T_USBDI_1230* self = new (ELeave) CUT_PBASE_T_USBDI_1230(aHostRole);
62 CleanupStack::PushL(self);
64 CleanupStack::Pop(self);
69 CUT_PBASE_T_USBDI_1230::CUT_PBASE_T_USBDI_1230(TBool aHostRole)
70 : CBaseBulkTestCase(KTestCaseId,aHostRole),
71 iCaseStep(EInProgress)
76 void CUT_PBASE_T_USBDI_1230::ConstructL()
80 iInBuffer = HBufC8::NewL(KTestBufferLength);
82 iBulkTestTimer = CBulkTestTimer::NewL(*this);
84 //Create buffer to contain multiple lots of the payload pattern
85 //..so that we may grab cyclic chunks of said payload pattern
86 TInt repeats = KHostNumReadBytes / (KLiteralEnglish5().Length()) + 1 + 1; //1 extra to accommodate start point plus 1 to accomodate remainder in division
87 iValidateBuffer = HBufC8::NewL(KLiteralEnglish5().Length() * repeats);
88 iValidateBufferPtr.Set(iValidateBuffer->Des());
89 iValidateBufferPtr.Zero();
90 for(TInt i=0;i<repeats;i++)
92 iValidateBufferPtr.Append(KLiteralEnglish5());
95 RDebug::Printf("CUT_PBASE_T_USBDI_1230::ConstructL(): buffer created");
99 CUT_PBASE_T_USBDI_1230::~CUT_PBASE_T_USBDI_1230()
104 void CUT_PBASE_T_USBDI_1230::KillTransfers()
108 iInTransfer[0]->Cancel();
109 iInTransfer[1]->Cancel();
112 void CUT_PBASE_T_USBDI_1230::ExtractDeviceReadBytes()
116 iControlEp0->LastRequestCompletionTime( iEndTime[KTestTimer]);
117 iTimingError = iTimingError == KErrNone ? CheckTimes(KBaseTimer, KTestTimer, KMaxTimeDiffPercentage) : iTimingError;
118 ResetTimes(KTestTimer);
120 RDebug::Printf("Collect client's return of the number of bytes written on its bulk in endpoint ...");
121 TLex8 lex(iInBufferPtr.Left(KNumberStringLength));
122 TUint32 numBytes = 0;
123 User::LeaveIfError(lex.Val(numBytes, EDecimal));
124 RDebug::Printf("********************NUM*BYTES****************************");
125 RDebug::Printf(" NUM BYTES READ BY CLIENT ==== %d ==== ", numBytes);
126 RDebug::Printf("********************NUM*BYTES****************************");
127 RDebug::Printf("\n");
130 //Do not count this case - it may result from the remote resetting when all bulk transfers have completed
132 TUint numBytesSinceLast = numBytes - iDeviceNumBytesReadInTotal;
133 iDeviceNumBytesReadInTotal = numBytes;
134 iDeviceMinTimedNumBytesRead = numBytesSinceLast < iDeviceMinTimedNumBytesRead ? numBytesSinceLast : iDeviceMinTimedNumBytesRead ;
135 iDeviceMaxTimedNumBytesRead = numBytesSinceLast > iDeviceMaxTimedNumBytesRead ? numBytesSinceLast : iDeviceMaxTimedNumBytesRead ;;
140 void CUT_PBASE_T_USBDI_1230::PostTransferAction()
142 switch(iTransferResult)
148 case KTransferSuccess:
150 // Indicates success - comparison is a match
151 RDebug::Printf("Comparison for IN transfer is a match");
153 TTestCasePassed request;
154 iTransferComplete = 0; //reset
155 iControlEp0->SendRequest(request,this);
164 TTestCaseFailed request(iTransferResult,iMsg);
165 iControlEp0->SendRequest(request,this);
172 TInt CUT_PBASE_T_USBDI_1230::ValidatePreviousAndPerformNextTransfers(TInt aTransferId)
179 case KBulkTransferInId0:
183 case KBulkTransferInId1:
188 _LIT(lit, "TRANSFER ID OUT OF RANGE");
189 User::Panic(lit, KErrArgument);
190 return 0; //should never get here
193 RDebug::Printf("\n");
194 RDebug::Printf("Transfer[%d]", index);
197 if(iNumBytesExpected[index] != 0)
199 TPtrC8 data1(iInTransfer[index]->DataPolled());
200 if(ValidateData(data1, iValidateBufferPtr.Mid(iValidationStringStartPointTransfer[index], iNumBytesExpected[index])) == EFalse)
202 RDebug::Printf("=====VALIDATION FAILURE: Point of Validation String Entry %d, Newly Read Bytes %d=====",iValidationStringStartPointTransfer[index], iNumBytesExpected[index]);
205 iNumBytesExpected[index] = 0; //reset
207 if(iNumBytesRequestedSoFar >= KTotalBytesToTransfer)
208 //if we are near the end end the other transfer will mop up remaining bytes...
210 RDebug::Printf("****ALL DONE for Transfer[%d]****", index);
211 return KBulkTransferInId[index];
213 iValidationStringStartPointTransfer[index] = iNumBytesRequestedSoFar%(KLiteralEnglish5().Length()); //PRIOR TO THIS TRANSFER
214 TUint bytesLeftToRead = KTotalBytesToTransfer - iNumBytesRequestedSoFar;
215 iNumBytesExpected[index] = bytesLeftToRead < KHostNumReadBytes ? bytesLeftToRead : KHostNumReadBytes;
216 iNumBytesRequestedSoFar += iNumBytesExpected[index];
217 iInTransfer[index]->TransferIn(KHostNumReadBytes); //rely on ZLP to complete the last 'TransferIn'
218 iExpectedNextTransferNumber = 1 - iExpectedNextTransferNumber;
224 void CUT_PBASE_T_USBDI_1230::RequestNumBytesSent(TUint8 aTimerIndex)
226 iInBufferPtr.Set(iInBuffer->Des());
227 iInBufferPtr.Zero(); //reset
228 iInBufferPtr.SetLength(KNumberStringLength);
229 TInterfaceGetRecordedNumBytesReadInPayload request(1,1,iInBufferPtr);
230 iControlEp0->SendRequest(request,this);
231 iControlEp0->LastRequestStartTime( iStartTime[aTimerIndex]);
235 void CUT_PBASE_T_USBDI_1230::Ep0TransferCompleteL(TInt aCompletionCode)
239 RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d, test step = %d", aCompletionCode, iCaseStep);
241 if(aCompletionCode != KErrNone)
243 if(iCaseStep == EFailed)
244 {// ignore error, nad catch the TestFailed method called further down.
245 RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
251 _LIT(lit, "<Error %d> Transfer to control endpoint 0 was not successful");
252 msg.Format(lit,aCompletionCode);
255 TTestCaseFailed request(aCompletionCode,msg);
256 iControlEp0->SendRequest(request,this);
270 TestFailed(KErrCompletion);
275 iControlEp0->LastRequestCompletionTime( iEndTime[KBaseTimer]);
276 RDebug::Printf("Asking client for repeated 'Write'");
277 iCaseStep = ERequestRepeatedWrite;
278 TRepeatedWriteDataRequest request(1,1,KLiteralEnglish5(),KDeviceNumWriteBytes,KTotalBytesToTransfer);// EP2 means endpoint index 2 not the actual endpoint number, here the ep with 32 byte max packet size
279 iControlEp0->SendRequest(request,this);
283 case ERequestRepeatedWrite:
285 RDebug::Printf("Try to perform ALL transfers");
287 iCaseStep = ETransfer;
288 iIsValid = ETrue; //innocent until proved guilty
289 RDebug::Printf("\n");
290 ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[0]); //should not validate - just perform necessary transfers
291 ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[1]); //should not validate - just perform necessary transfers
292 RDebug::Printf("\n");
293 _LITDBG("Bulk test timer NOT instanciated");
294 __ASSERT_DEBUG(iBulkTestTimer, User::Panic(lit, KErrGeneral));
295 iBulkTestTimer->After(KRepeatedTimerInterval);
300 // we must be getting num bytes read
302 ExtractDeviceReadBytes();
305 _LITDBG("Bulk test timer NOT instanciated");
306 __ASSERT_DEBUG(iBulkTestTimer, User::Panic(lit, KErrGeneral));
307 iBulkTestTimer->After(KRepeatedTimerInterval);
311 case EDelayedTransferComplete:
313 PostTransferAction();
318 RDebug::Printf("<Error> Unknown test step");
319 TestFailed(KErrUnknown);
324 void CUT_PBASE_T_USBDI_1230::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
329 iTransferResult = KErrNone;
330 RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
336 if(aCompletionCode != KErrNone)
339 iTransferResult = KErrCorrupt;
340 _LIT(lit, "<Error %d> The transfer completed with an error.");
341 iMsg.Format(lit, aCompletionCode);
344 if(aTransferId != KBulkTransferInId[0] && aTransferId != KBulkTransferInId[1])
346 iTransferComplete = 0; //reset
347 iTransferResult = KUnexpectedTransferID;
348 _LIT(lit, "<Error %d> Unexpected transfer ID, wanted %d or %d, got %d");
349 iMsg.Format(lit, iTransferResult, KBulkTransferInId[0], KBulkTransferInId[1], aTransferId);
353 RDebug::Printf("Transfer IN %d completed - num bytes requested = %d", aTransferId, iNumBytesRequestedSoFar);
355 RDebug::Printf("\n");
356 iTransferComplete |= ValidatePreviousAndPerformNextTransfers(aTransferId);
357 RDebug::Printf("\n");
359 if(iTransferResult==KErrNone && (iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
362 Transfers all complete - now check validation
364 RDebug::Printf("All Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
365 iBulkTestTimer->Cancel(); //Cancel Timer
366 iTransferResult = KTransferSuccess;
369 iTransferResult = KErrCompletion; //indicates data validation failure
370 iIsValid = ETrue; //reset
373 if(iTimingError == KErrTooBig)
375 __PRINT_CONTROL_TRANSFER_TIMER_COMPARISON_WARNING
376 iTransferResult = KErrTooBig;
377 iTimingError = KErrNone; //reset
380 if(KMaxBytesWrittenDiffPercentage*iDeviceMaxTimedNumBytesRead > KPercent*iDeviceMinTimedNumBytesRead)
382 RDebug::Printf("Device APPARENTLY reading rate erratic:-");
383 RDebug::Printf("Min Timed Number of Bytes = %d", iDeviceMinTimedNumBytesRead);
384 RDebug::Printf("Max Timed Number of Bytes = %d", iDeviceMaxTimedNumBytesRead);
385 iTransferResult = KErrTooBig;
386 iDeviceMaxTimedNumBytesRead = 0;
387 iDeviceMinTimedNumBytesRead = KMaxTUint;
393 iTransferResult = KUndefinedStep;
394 _LIT(lit, "<Error %d> Undefined case step %d reached");
395 iMsg.Format(lit,KUndefinedStep, iCaseStep);
400 if(iTransferResult == KErrReturnedDeviceReadBytesTooVariable)
401 //indicates apparent device read rate validation failure
403 iMsg.Format(_L("<Error %d> Device APPEARS not to be reading bytes at a constant rate"), iTransferResult);
406 if(iTransferResult == KErrTooBig)
407 //indicates timing validation failure
409 iMsg.Format(_L("<Error %d> Timer comparison showed too great a difference in transfer times between the time taken by EP0 transfers with and without a bulk transfer"), iTransferResult);
412 if(iTransferResult == KErrCompletion)
413 //indicates data validation failure
415 _LIT(lit, "<Error %d> Client has posted an error discovered in validation");
416 iMsg.Format(lit, iTransferResult);
419 if(iTransferResult != KErrNone)
421 KillTransfers(); //harmless if tranfers are all done
422 if(!iControlEp0->IsActive())
424 PostTransferAction();
428 iCaseStep = EDelayedTransferComplete; //so that we move forward when the EP0 transfer has completed
433 void CUT_PBASE_T_USBDI_1230::DeviceInsertedL(TUint aDeviceHandle)
438 RDebug::Printf("this - %08x", this);
442 if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
443 // Prepare for response from control transfer to client
446 _LIT(lit, "Base class DeviceInsertedL failed");
451 if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
452 // Prepare for response from control transfer to client
455 _LIT(lit, "Base class SetUpInterfaceAndPipes for Interface 2 failed");
461 iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[0]);
462 iInTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[1]);
464 // Initialise the descriptors for transfer
465 RDebug::Printf("Initialising the transfer descriptors - interface 1");
466 err = iUsbInterface1.InitialiseTransferDescriptors();
469 _LIT(lit, "<Error %d> Unable to initialise transfer descriptors");
478 TTestCaseFailed request(err,msg);
479 iControlEp0->SendRequest(request,this);
483 iCaseStep = EGetTimerBase;
484 iDeviceMinTimedNumBytesRead = KMaxTUint;
485 iDeviceMaxTimedNumBytesRead = 0;
486 iDeviceNumBytesReadInTotal = 0;
487 RequestNumBytesSent(KBaseTimer);
491 void CUT_PBASE_T_USBDI_1230::HandleBulkTestTimerFired()
493 if(iCaseStep == ETransfer)
495 RequestNumBytesSent(KTestTimer);