1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0496.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,366 @@
1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// @file PBASE-T_USBDI-0496.cpp
1.18 +// @internalComponent
1.19 +//
1.20 +//
1.21 +
1.22 +#include "PBASE-T_USBDI-0496.h"
1.23 +#include "testpolicy.h"
1.24 +#include "modelleddevices.h"
1.25 +#include "testliterals.h"
1.26 +
1.27 +
1.28 +
1.29 +
1.30 +namespace NUnitTesting_USBDI
1.31 + {
1.32 +const TUint KTotalBytesToTransfer = 2*1024*1024+511; //2MB + 511 bytes
1.33 +const TUint KHostNumReadBytes = 1024*256;
1.34 +const TInt KBulkMaxTransferSize = KHostNumReadBytes + 1000;
1.35 +const TInt KDeviceNumWriteBytes = 1024;
1.36 +
1.37 +
1.38 +//Make these single bit values ...
1.39 +// ... so that their completion can be easily recorded in a bit mask!
1.40 +const TUint32 KBulkTransferInId0 = 1<<0;
1.41 +const TUint32 KBulkTransferInId1 = 1<<1;
1.42 +const TUint32 KBulkTransferInId[KMaxNumInTransfers] = {KBulkTransferInId0, KBulkTransferInId1};
1.43 +const TUint32 KBulkTransferIdMask = KBulkTransferInId[0] | KBulkTransferInId[1];
1.44 +
1.45 +const TInt KUndefinedStep = -102;
1.46 +const TInt KUnexpectedTransferID = -103;
1.47 +
1.48 +
1.49 +_LIT(KTestCaseId,"PBASE-T_USBDI-0496");
1.50 +const TFunctorTestCase<CUT_PBASE_T_USBDI_0496,TBool> CUT_PBASE_T_USBDI_0496::iFunctor(KTestCaseId);
1.51 +
1.52 +CUT_PBASE_T_USBDI_0496* CUT_PBASE_T_USBDI_0496::NewL(TBool aHostRole)
1.53 + {
1.54 + CUT_PBASE_T_USBDI_0496* self = new (ELeave) CUT_PBASE_T_USBDI_0496(aHostRole);
1.55 + CleanupStack::PushL(self);
1.56 + self->ConstructL();
1.57 + CleanupStack::Pop(self);
1.58 + return self;
1.59 + }
1.60 +
1.61 +
1.62 +CUT_PBASE_T_USBDI_0496::CUT_PBASE_T_USBDI_0496(TBool aHostRole)
1.63 +: CBaseBulkTestCase(KTestCaseId,aHostRole),
1.64 + iCaseStep(EInProgress)
1.65 + {
1.66 + }
1.67 +
1.68 +
1.69 +void CUT_PBASE_T_USBDI_0496::ConstructL()
1.70 + {
1.71 + BaseBulkConstructL();
1.72 +
1.73 + iInBuffer = HBufC8::NewL(KTestBufferLength);
1.74 +
1.75 + //Create buffer to contain two lots of the payload pattern
1.76 + //..so that we may grab cyclic chunks of said payload pattern
1.77 + TInt repeats = KHostNumReadBytes / (KLiteralEnglish5().Length()) + 1 + 1; //1 extra to accommodate start point plus 1 to accomodate remainder in division
1.78 + iValidateBuffer = HBufC8::NewL(KLiteralEnglish5().Length() * repeats);
1.79 + iValidateBufferPtr.Set(iValidateBuffer->Des());
1.80 + iValidateBufferPtr.Zero();
1.81 + for(TInt i=0;i<repeats;i++)
1.82 + {
1.83 + iValidateBufferPtr.Append(KLiteralEnglish5());
1.84 + }
1.85 +
1.86 + RDebug::Printf("CUT_PBASE_T_USBDI_0496::ConstructL(): buffer created");
1.87 + }
1.88 +
1.89 +
1.90 +CUT_PBASE_T_USBDI_0496::~CUT_PBASE_T_USBDI_0496()
1.91 + {
1.92 + LOG_FUNC
1.93 + }
1.94 +
1.95 +void CUT_PBASE_T_USBDI_0496::KillTransfers()
1.96 + {
1.97 + LOG_FUNC
1.98 +
1.99 + iInTransfer[0]->Cancel();
1.100 + iInTransfer[1]->Cancel();
1.101 + }
1.102 +
1.103 +
1.104 +
1.105 +void CUT_PBASE_T_USBDI_0496::Ep0TransferCompleteL(TInt aCompletionCode)
1.106 + {
1.107 + LOG_FUNC
1.108 +
1.109 + RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d", aCompletionCode);
1.110 +
1.111 + if(aCompletionCode != KErrNone)
1.112 + {
1.113 + if(iCaseStep == EFailed)
1.114 + {// ignore error, nad catch the TestFailed method called further down.
1.115 + RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
1.116 + }
1.117 + else
1.118 + {
1.119 + KillTransfers();
1.120 + TBuf<256> msg;
1.121 + _LIT(lit, "<Error %d> Transfer to control endpoint 0 was not successful");
1.122 + msg.Format(lit,aCompletionCode);
1.123 + RDebug::Print(msg);
1.124 + iCaseStep = EFailed;
1.125 + TTestCaseFailed request(aCompletionCode,msg);
1.126 + iControlEp0->SendRequest(request,this);
1.127 + return;
1.128 + }
1.129 + }
1.130 +
1.131 + switch(iCaseStep)
1.132 + {
1.133 + // Test case passed
1.134 + case EPassed:
1.135 + TestPassed();
1.136 + break;
1.137 +
1.138 + // Test case failed
1.139 + case EFailed:
1.140 + TestFailed(KErrCompletion);
1.141 + break;
1.142 +
1.143 + case ERequestRepeatedWrite:
1.144 + {
1.145 + RDebug::Printf("Try to perform ALL transfers");
1.146 +
1.147 + iCaseStep = ETransfer;
1.148 + iIsValid = ETrue; //innocent until proved guilty
1.149 + RDebug::Printf("\n");
1.150 + iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[0]); //should not validate - just perform necessary transfers
1.151 + iTransferComplete |= ValidatePreviousAndPerformNextTransfers(KBulkTransferInId[1]); //should not validate - just perform necessary transfers
1.152 + RDebug::Printf("\n");
1.153 + if((iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
1.154 + {
1.155 + iTransferComplete = 0; //reset
1.156 + _LIT(lit, "TEST FAILURE: No data to send!!");
1.157 + TBuf<40> msg(lit);
1.158 + RDebug::Print(msg);
1.159 + iCaseStep = EFailed;
1.160 + TTestCaseFailed request(KErrAbort,msg);
1.161 + return iControlEp0->SendRequest(request,this);
1.162 + }
1.163 + }
1.164 + break;
1.165 +
1.166 + default:
1.167 + RDebug::Printf("<Error> Unknown test step");
1.168 + TestFailed(KErrUnknown);
1.169 + break;
1.170 + }
1.171 + }
1.172 +
1.173 +void CUT_PBASE_T_USBDI_0496::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
1.174 + {
1.175 + LOG_FUNC
1.176 + Cancel();
1.177 +
1.178 + TInt err(KErrNone);
1.179 + TBuf<256> msg;
1.180 + RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d, test step = %d",aTransferId, aCompletionCode, iCaseStep);
1.181 +
1.182 +
1.183 + switch(iCaseStep)
1.184 + {
1.185 + case ETransfer:
1.186 + {
1.187 + if(aCompletionCode != KErrNone)
1.188 + {
1.189 + KillTransfers();
1.190 + err = KErrCorrupt;
1.191 + _LIT(lit, "<Error %d> The transfer completed with an error.");
1.192 + msg.Format(lit, aCompletionCode);
1.193 + break;
1.194 + }
1.195 + if(aTransferId != KBulkTransferInId[0] && aTransferId != KBulkTransferInId[1])
1.196 + {
1.197 + iTransferComplete = 0; //reset
1.198 + err = KUnexpectedTransferID;
1.199 + _LIT(lit, "<Error %d> Unexpected transfer ID, wanted %d or %d, got %d");
1.200 + msg.Format(lit, err, KBulkTransferInId[0], KBulkTransferInId[1], aTransferId);
1.201 + break;
1.202 + }
1.203 +
1.204 + RDebug::Printf("\n");
1.205 + iTransferComplete |= ValidatePreviousAndPerformNextTransfers(aTransferId);
1.206 + RDebug::Printf("\n");
1.207 +
1.208 + if(err==KErrNone && (iTransferComplete & KBulkTransferIdMask) == KBulkTransferIdMask)
1.209 + {
1.210 + /*
1.211 + Transfers all complete - check all were valid, and if so pass the test
1.212 + */
1.213 + RDebug::Printf("All Transfers Completed Successfully: Transfer Completion Aggregation Mask 0x%x", iTransferComplete);
1.214 + if(!iIsValid)
1.215 + {
1.216 + err = KErrCompletion; //indicates data validation failure
1.217 + iIsValid = ETrue; //reset
1.218 + break;
1.219 + }
1.220 + // Comparison is a match
1.221 + RDebug::Printf("Comparison for IN transfer is a match");
1.222 + iCaseStep = EPassed;
1.223 + TTestCasePassed request;
1.224 + iControlEp0->SendRequest(request,this);
1.225 + iTransferComplete = 0; //reset
1.226 + }
1.227 + }
1.228 + break;
1.229 +
1.230 + default:
1.231 + err = KUndefinedStep;
1.232 + _LIT(lit, "<Error %d> Undefined case step %d reached");
1.233 + msg.Format(lit,KUndefinedStep, iCaseStep);
1.234 + break;
1.235 + }
1.236 +
1.237 +
1.238 + if(err == KErrCompletion)
1.239 + //indicates data validation failure
1.240 + {
1.241 + _LIT(lit, "<Error %d> Bulk transfer IN data received does not match the data expected");
1.242 + msg.Format(lit, err);
1.243 + }
1.244 +
1.245 + if(err!=KErrNone)
1.246 + {
1.247 + KillTransfers();
1.248 + RDebug::Print(msg);
1.249 + iCaseStep = EFailed;
1.250 + TTestCaseFailed request(err,msg);
1.251 + return iControlEp0->SendRequest(request,this);
1.252 + }
1.253 + }
1.254 +
1.255 +void CUT_PBASE_T_USBDI_0496::DeviceInsertedL(TUint aDeviceHandle)
1.256 + {
1.257 + LOG_FUNC
1.258 +
1.259 + Cancel();
1.260 + RDebug::Printf("this - %08x", this);
1.261 +
1.262 + TBuf<256> msg;
1.263 + TInt err = KErrNone;
1.264 + if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
1.265 + // Prepare for response from control transfer to client
1.266 + {
1.267 + err = KErrGeneral;
1.268 + _LIT(lit, "Base class DeviceInsertedL failed");
1.269 + msg.Format(lit);
1.270 + }
1.271 + else
1.272 + {
1.273 + if(SetUpInterfaceAndPipesL(aDeviceHandle, 2) == EDeviceConfigurationError)
1.274 + // Prepare for response from control transfer to client
1.275 + {
1.276 + err = KErrGeneral;
1.277 + _LIT(lit, "Base class SetUpInterfaceAndPipes for Interface 2 failed");
1.278 + msg.Format(lit);
1.279 + }
1.280 + else
1.281 + {
1.282 +
1.283 + iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[0]);
1.284 + iInTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxTransferSize,*this,KBulkTransferInId[1]);
1.285 +
1.286 + // Initialise the descriptors for transfer
1.287 + RDebug::Printf("Initialising the transfer descriptors - interface 1");
1.288 + err = iUsbInterface1.InitialiseTransferDescriptors();
1.289 + if(err != KErrNone)
1.290 + {
1.291 + _LIT(lit, "<Error %d> Unable to initialise transfer descriptors");
1.292 + msg.Format(lit,err);
1.293 + }
1.294 + }
1.295 + }
1.296 + if(err != KErrNone)
1.297 + {
1.298 + RDebug::Print(msg);
1.299 + iCaseStep = EFailed;
1.300 + TTestCaseFailed request(err,msg);
1.301 + iControlEp0->SendRequest(request,this);
1.302 + }
1.303 + else
1.304 + {
1.305 + RDebug::Printf("Asking client for 'Read' on interface 1");
1.306 + iCaseStep = ERequestRepeatedWrite;
1.307 + 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
1.308 + iControlEp0->SendRequest(request,this);
1.309 + }
1.310 + }
1.311 +
1.312 +TUint8 CUT_PBASE_T_USBDI_0496::Index(TUint8 aTransferId)
1.313 + {
1.314 + switch(aTransferId)
1.315 + {
1.316 + case KBulkTransferInId0:
1.317 + return 0;
1.318 + case KBulkTransferInId1:
1.319 + return 1;
1.320 + default:
1.321 + _LIT(lit, "TRANSFER ID OUT OF RANGE");
1.322 + User::Panic(lit, KErrArgument);
1.323 + return 0; //should never get here
1.324 + }
1.325 + }
1.326 +
1.327 +TInt CUT_PBASE_T_USBDI_0496::ValidatePreviousAndPerformNextTransfers(TInt aTransferId)
1.328 +/**
1.329 +@param aTransferId - specifies trasnfer to use
1.330 +@return zero UNLESS no more trasnfers have been queued for the specified ID in which case return the transfer ID
1.331 +Note: This return value allows the caller to respond easily if no more transfers are required for the specified
1.332 + transfer ID.
1.333 +*/
1.334 + {
1.335 + LOG_FUNC
1.336 +
1.337 + TUint8 index = Index(aTransferId);
1.338 + RDebug::Printf("\n");
1.339 + RDebug::Printf("Transfer[%d]", index);
1.340 +
1.341 +
1.342 + if(iNumBytesExpected[index] != 0)
1.343 + {
1.344 + TPtrC8 data1(iInTransfer[index]->DataPolled());
1.345 + if(ValidateData(data1, iValidateBufferPtr.Mid(iValidationStringStartPointTransfer[index], iNumBytesExpected[index])) == EFalse)
1.346 + {
1.347 + RDebug::Printf("=====VALIDATION FAILURE: Point of Validation String Entry %d, Newly Read Bytes %d=====",iValidationStringStartPointTransfer[index], iNumBytesExpected[index]);
1.348 + iIsValid = EFalse;
1.349 + }
1.350 + iNumBytesExpected[index] = 0; //reset
1.351 + }
1.352 + if(iNumBytesRequestedSoFar >= KTotalBytesToTransfer)
1.353 + //if we are near the end the other transfer will mop up remaining bytes...
1.354 + {
1.355 + RDebug::Printf("****ALL DONE for Transfer[%d]****", index);
1.356 + return aTransferId; //tell caller that all transfers are complete for this transfer ID
1.357 + }
1.358 +
1.359 + iValidationStringStartPointTransfer[index] = iNumBytesRequestedSoFar%(KLiteralEnglish5().Length()); //PRIOR TO THIS TRANSFER
1.360 + TUint bytesLeftToRead = KTotalBytesToTransfer - iNumBytesRequestedSoFar;
1.361 + iNumBytesExpected[index] = bytesLeftToRead < KHostNumReadBytes ? bytesLeftToRead : KHostNumReadBytes;
1.362 + iNumBytesRequestedSoFar += iNumBytesExpected[index];
1.363 + iInTransfer[index]->TransferIn(KHostNumReadBytes); //rely on ZLP to complete the last 'TransferIn'
1.364 + iExpectedNextTransferNumber = 1 - iExpectedNextTransferNumber;
1.365 +
1.366 + return 0; //tell caller there are still transfers to complete for the requested transfer ID
1.367 + }
1.368 +
1.369 + } //end namespace