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-0496.cpp
19 #include "PBASE-T_USBDI-0496.h"
20 #include "testpolicy.h"
21 #include "modelleddevices.h"
22 #include "testliterals.h"
27 namespace NUnitTesting_USBDI
29 const TUint KTotalBytesToTransfer = 2*1024*1024+511; //2MB + 511 bytes
30 const TUint KHostNumReadBytes = 1024*256;
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;
46 _LIT(KTestCaseId,"PBASE-T_USBDI-0496");
47 const TFunctorTestCase<CUT_PBASE_T_USBDI_0496,TBool> CUT_PBASE_T_USBDI_0496::iFunctor(KTestCaseId);
49 CUT_PBASE_T_USBDI_0496* CUT_PBASE_T_USBDI_0496::NewL(TBool aHostRole)
51 CUT_PBASE_T_USBDI_0496* self = new (ELeave) CUT_PBASE_T_USBDI_0496(aHostRole);
52 CleanupStack::PushL(self);
54 CleanupStack::Pop(self);
59 CUT_PBASE_T_USBDI_0496::CUT_PBASE_T_USBDI_0496(TBool aHostRole)
60 : CBaseBulkTestCase(KTestCaseId,aHostRole),
61 iCaseStep(EInProgress)
66 void CUT_PBASE_T_USBDI_0496::ConstructL()
70 iInBuffer = HBufC8::NewL(KTestBufferLength);
72 //Create buffer to contain two lots of the payload pattern
73 //..so that we may grab cyclic chunks of said payload pattern
74 TInt repeats = KHostNumReadBytes / (KLiteralEnglish5().Length()) + 1 + 1; //1 extra to accommodate start point plus 1 to accomodate remainder in division
75 iValidateBuffer = HBufC8::NewL(KLiteralEnglish5().Length() * repeats);
76 iValidateBufferPtr.Set(iValidateBuffer->Des());
77 iValidateBufferPtr.Zero();
78 for(TInt i=0;i<repeats;i++)
80 iValidateBufferPtr.Append(KLiteralEnglish5());
83 RDebug::Printf("CUT_PBASE_T_USBDI_0496::ConstructL(): buffer created");
87 CUT_PBASE_T_USBDI_0496::~CUT_PBASE_T_USBDI_0496()
92 void CUT_PBASE_T_USBDI_0496::KillTransfers()
96 iInTransfer[0]->Cancel();
97 iInTransfer[1]->Cancel();
102 void CUT_PBASE_T_USBDI_0496::Ep0TransferCompleteL(TInt aCompletionCode)
106 RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d", aCompletionCode);
108 if(aCompletionCode != KErrNone)
110 if(iCaseStep == EFailed)
111 {// ignore error, nad catch the TestFailed method called further down.
112 RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
118 _LIT(lit, "<Error %d> Transfer to control endpoint 0 was not successful");
119 msg.Format(lit,aCompletionCode);
122 TTestCaseFailed request(aCompletionCode,msg);
123 iControlEp0->SendRequest(request,this);
137 TestFailed(KErrCompletion);
140 case ERequestRepeatedWrite:
142 RDebug::Printf("Try to perform ALL transfers");
144 iCaseStep = ETransfer;
145 iIsValid = ETrue; //innocent until proved guilty
146 RDebug::Printf("\n");
147 iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[0]); //should not validate - just perform necessary transfers
148 iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[1]); //should not validate - just perform necessary transfers
149 RDebug::Printf("\n");
150 if((iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
152 iTransferComplete = 0; //reset
153 _LIT(lit, "TEST FAILURE: No data to send!!");
157 TTestCaseFailed request(KErrAbort,msg);
158 return iControlEp0->SendRequest(request,this);
164 RDebug::Printf("<Error> Unknown test step");
165 TestFailed(KErrUnknown);
170 void CUT_PBASE_T_USBDI_0496::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
177 RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
184 if(aCompletionCode != KErrNone)
188 _LIT(lit, "<Error %d> The transfer completed with an error.");
189 msg.Format(lit, aCompletionCode);
192 if(aTransferId != KBulkTransferInId[0] && aTransferId != KBulkTransferInId[1])
194 iTransferComplete = 0; //reset
195 err = KUnexpectedTransferID;
196 _LIT(lit, "<Error %d> Unexpected transfer ID, wanted %d or %d, got %d");
197 msg.Format(lit, err, KBulkTransferInId[0], KBulkTransferInId[1], aTransferId);
201 RDebug::Printf("\n");
202 iTransferComplete |= ValidatePreviousAndPerformNextTransfers(aTransferId);
203 RDebug::Printf("\n");
205 if(err==KErrNone && (iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
208 Transfers all complete - check all were valid, and if so pass the test
210 RDebug::Printf("All Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
213 err = KErrCompletion; //indicates data validation failure
214 iIsValid = ETrue; //reset
217 // Comparison is a match
218 RDebug::Printf("Comparison for IN transfer is a match");
220 TTestCasePassed request;
221 iControlEp0->SendRequest(request,this);
222 iTransferComplete = 0; //reset
228 err = KUndefinedStep;
229 _LIT(lit, "<Error %d> Undefined case step %d reached");
230 msg.Format(lit,KUndefinedStep, iCaseStep);
235 if(err == KErrCompletion)
236 //indicates data validation failure
238 _LIT(lit, "<Error %d> Bulk transfer IN data received does not match the data expected");
239 msg.Format(lit, err);
247 TTestCaseFailed request(err,msg);
248 return iControlEp0->SendRequest(request,this);
252 void CUT_PBASE_T_USBDI_0496::DeviceInsertedL(TUint aDeviceHandle)
257 RDebug::Printf("this - %08x", this);
261 if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
262 // Prepare for response from control transfer to client
265 _LIT(lit, "Base class DeviceInsertedL failed");
270 if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
271 // Prepare for response from control transfer to client
274 _LIT(lit, "Base class SetUpInterfaceAndPipes for Interface 2 failed");
280 iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[0]);
281 iInTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[1]);
283 // Initialise the descriptors for transfer
284 RDebug::Printf("Initialising the transfer descriptors - interface 1");
285 err = iUsbInterface1.InitialiseTransferDescriptors();
288 _LIT(lit, "<Error %d> Unable to initialise transfer descriptors");
297 TTestCaseFailed request(err,msg);
298 iControlEp0->SendRequest(request,this);
302 RDebug::Printf("Asking client for 'Read' on interface 1");
303 iCaseStep = ERequestRepeatedWrite;
304 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
305 iControlEp0->SendRequest(request,this);
309 TUint8 CUT_PBASE_T_USBDI_0496::Index(TUint8 aTransferId)
313 case KBulkTransferInId0:
315 case KBulkTransferInId1:
318 _LIT(lit, "TRANSFER ID OUT OF RANGE");
319 User::Panic(lit, KErrArgument);
320 return 0; //should never get here
324 TInt CUT_PBASE_T_USBDI_0496::ValidatePreviousAndPerformNextTransfers(TInt aTransferId)
326 @param aTransferId - specifies trasnfer to use
327 @return zero UNLESS no more trasnfers have been queued for the specified ID in which case return the transfer ID
328 Note: This return value allows the caller to respond easily if no more transfers are required for the specified
334 TUint8 index = Index(aTransferId);
335 RDebug::Printf("\n");
336 RDebug::Printf("Transfer[%d]", index);
339 if(iNumBytesExpected[index] != 0)
341 TPtrC8 data1(iInTransfer[index]->DataPolled());
342 if(ValidateData(data1, iValidateBufferPtr.Mid(iValidationStringStartPointTransfer[index], iNumBytesExpected[index])) == EFalse)
344 RDebug::Printf("=====VALIDATION FAILURE: Point of Validation String Entry %d, Newly Read Bytes %d=====",iValidationStringStartPointTransfer[index], iNumBytesExpected[index]);
347 iNumBytesExpected[index] = 0; //reset
349 if(iNumBytesRequestedSoFar >= KTotalBytesToTransfer)
350 //if we are near the end the other transfer will mop up remaining bytes...
352 RDebug::Printf("****ALL DONE for Transfer[%d]****", index);
353 return aTransferId; //tell caller that all transfers are complete for this transfer ID
356 iValidationStringStartPointTransfer[index] = iNumBytesRequestedSoFar%(KLiteralEnglish5().Length()); //PRIOR TO THIS TRANSFER
357 TUint bytesLeftToRead = KTotalBytesToTransfer - iNumBytesRequestedSoFar;
358 iNumBytesExpected[index] = bytesLeftToRead < KHostNumReadBytes ? bytesLeftToRead : KHostNumReadBytes;
359 iNumBytesRequestedSoFar += iNumBytesExpected[index];
360 iInTransfer[index]->TransferIn(KHostNumReadBytes); //rely on ZLP to complete the last 'TransferIn'
361 iExpectedNextTransferNumber = 1 - iExpectedNextTransferNumber;
363 return 0; //tell caller there are still transfers to complete for the requested transfer ID