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-0500.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,313 @@
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-0500.cpp
1.18 +// @internalComponent
1.19 +//
1.20 +//
1.21 +
1.22 +#include "PBASE-T_USBDI-0500.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 TInt KBulkMaxINTransferSize = 600;
1.33 +const TInt KDeviceNumWriteBytesPreHalt = 256;
1.34 +const TUint KHostNumReadBytesPreHalt1 = 512;
1.35 +const TUint KHostNumReadBytesPreHalt2 = 512;
1.36 +const TUint KHostNumReadBytesPostHalt1 = 256;
1.37 +const TUint KHostNumReadBytesPostHalt2 = 257;
1.38 +const TInt KDeviceNumWriteBytesPostHalt = 512;
1.39 +
1.40 +
1.41 +//Make these single bit values ...
1.42 +// ... so that their completion can be easily recorded in a bit mask!
1.43 +const TInt KBulkTransferInId0 = 1<<0;
1.44 +const TInt KBulkTransferInId1 = 1<<4;
1.45 +
1.46 +const TInt KUnexpectedTransferID = -101;
1.47 +const TInt KUndefinedStep = -102;
1.48 +
1.49 +
1.50 +_LIT(KTestCaseId,"PBASE-T_USBDI-0500");
1.51 +const TFunctorTestCase<CUT_PBASE_T_USBDI_0500,TBool> CUT_PBASE_T_USBDI_0500::iFunctor(KTestCaseId);
1.52 +
1.53 +CUT_PBASE_T_USBDI_0500* CUT_PBASE_T_USBDI_0500::NewL(TBool aHostRole)
1.54 + {
1.55 + CUT_PBASE_T_USBDI_0500* self = new (ELeave) CUT_PBASE_T_USBDI_0500(aHostRole);
1.56 + CleanupStack::PushL(self);
1.57 + self->ConstructL();
1.58 + CleanupStack::Pop(self);
1.59 + return self;
1.60 + }
1.61 +
1.62 +
1.63 +CUT_PBASE_T_USBDI_0500::CUT_PBASE_T_USBDI_0500(TBool aHostRole)
1.64 +: CBaseBulkTestCase(KTestCaseId,aHostRole),
1.65 + iCaseStep(EInProgress)
1.66 + {
1.67 + }
1.68 +
1.69 +
1.70 +void CUT_PBASE_T_USBDI_0500::ConstructL()
1.71 + {
1.72 + BaseBulkConstructL();
1.73 + }
1.74 +
1.75 +
1.76 +CUT_PBASE_T_USBDI_0500::~CUT_PBASE_T_USBDI_0500()
1.77 + {
1.78 + LOG_FUNC
1.79 + }
1.80 +
1.81 +
1.82 +void CUT_PBASE_T_USBDI_0500::Ep0TransferCompleteL(TInt aCompletionCode)
1.83 + {
1.84 + LOG_FUNC
1.85 +
1.86 + RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
1.87 +
1.88 + if(aCompletionCode != KErrNone)
1.89 + {
1.90 + if(iCaseStep == EFailed)
1.91 + {// ignore error, nad catch the TestFailed method called further down.
1.92 + RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
1.93 + }
1.94 + else
1.95 + {
1.96 + TBuf<256> msg;
1.97 + msg.Format(_L("<Error %d> Transfer to control endpoint 0 was not successful"),aCompletionCode);
1.98 + RDebug::Print(msg);
1.99 + iCaseStep = EFailed;
1.100 + TTestCaseFailed request(aCompletionCode,msg);
1.101 + iControlEp0->SendRequest(request,this);
1.102 + return;
1.103 + }
1.104 + }
1.105 +
1.106 + switch(iCaseStep)
1.107 + {
1.108 + // Test case passed
1.109 + case EPassed:
1.110 + TestPassed();
1.111 + break;
1.112 +
1.113 + // Test case failed
1.114 + case EFailed:
1.115 + TestFailed(KErrCompletion);
1.116 + break;
1.117 +
1.118 + case ETransferInHalt:
1.119 + RDebug::Printf("Try to receive data (pre halt)");
1.120 + iInTransfer[0]->TransferIn(KHostNumReadBytesPreHalt1);
1.121 + iInTransfer[1]->TransferIn(KHostNumReadBytesPreHalt2);
1.122 + break;
1.123 +
1.124 + case ETransferIn:
1.125 + RDebug::Printf("Try to receive data");
1.126 + iInTransfer[0]->TransferIn(KHostNumReadBytesPostHalt1);
1.127 + iInTransfer[1]->TransferIn(KHostNumReadBytesPostHalt2);
1.128 + break;
1.129 +
1.130 + default:
1.131 + RDebug::Printf("<Error> Unknown test step");
1.132 + TestFailed(KErrUnknown);
1.133 + break;
1.134 + }
1.135 + }
1.136 +
1.137 +void CUT_PBASE_T_USBDI_0500::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
1.138 + {
1.139 + LOG_FUNC
1.140 + Cancel();
1.141 +
1.142 + TInt err(KErrNone);
1.143 + TBuf<256> msg;
1.144 + RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d",aTransferId, aCompletionCode);
1.145 +
1.146 + switch(iCaseStep)
1.147 + {
1.148 + case ETransferInHalt:
1.149 + {
1.150 + if(aCompletionCode != KErrUsbStalled)
1.151 + {
1.152 + iInTransfer[0]->Cancel();
1.153 + iInTransfer[1]->Cancel();
1.154 + err = KErrCorrupt;
1.155 + msg.Format(_L("<Error %d> The transfer completed with no errors but the endpoint should have halted"),aCompletionCode);
1.156 + break; //switch(iCaseStep)
1.157 + }
1.158 +
1.159 + switch(aTransferId)
1.160 + {
1.161 + case KBulkTransferInId0:
1.162 + case KBulkTransferInId1:
1.163 + iTransferComplete |= aTransferId;
1.164 + RDebug::Printf("Transfer %d completed", aTransferId);
1.165 + break; //switch(aTransferId)
1.166 +
1.167 + default:
1.168 + iTransferComplete = 0; //reset
1.169 + err = KUnexpectedTransferID;
1.170 + msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
1.171 + err, KBulkTransferInId0, KBulkTransferInId1, aTransferId);
1.172 + break; //switch(aTransferId)
1.173 + }
1.174 +
1.175 + if(err==KErrNone && iTransferComplete == (KBulkTransferInId0 | KBulkTransferInId1))
1.176 + {
1.177 + RDebug::Printf("Clear halt and try to send data again. Transfers Completed %d", iTransferComplete);
1.178 + iTransferComplete = 0; //reset
1.179 + // Acknowledge the stall and clear
1.180 + err = iTestPipeInterface1BulkIn.ClearRemoteStall();
1.181 + if(err != KErrNone)
1.182 + {
1.183 + msg.Format(_L("<Error %d> The remote stall cannot be cleared"),err);
1.184 + break; //switch(iCaseStep)
1.185 + }
1.186 + iCaseStep = ETransferIn;
1.187 + TEndpointPatternSynchronousWriteRequest request(1,1, KLiteralEnglish8().Mid(0,KDeviceNumWriteBytesPostHalt), KDeviceNumWriteBytesPostHalt);// EP1 because 1st writter EP
1.188 + iControlEp0->SendRequest(request,this);
1.189 + }
1.190 + }
1.191 + break; //switch(iCaseStep)
1.192 +
1.193 + case ETransferIn:
1.194 + {
1.195 + if(aCompletionCode != KErrNone)
1.196 + {
1.197 + iInTransfer[0]->Cancel();
1.198 + iInTransfer[1]->Cancel();
1.199 + err = KErrCorrupt;
1.200 +
1.201 + msg.Format(_L("<Error %d> No data sent on bulk IN request"),aCompletionCode);
1.202 + break; //switch(iCaseStep)
1.203 + }
1.204 +
1.205 + switch(aTransferId)
1.206 + {
1.207 + case KBulkTransferInId0:
1.208 + case KBulkTransferInId1:
1.209 + iTransferComplete |= aTransferId;
1.210 + RDebug::Printf("Transfer %d completed", aTransferId);
1.211 + break; //switch(aTransferId)
1.212 +
1.213 + default:
1.214 + iTransferComplete = 0; //reset
1.215 + err = KUnexpectedTransferID;
1.216 + msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
1.217 + err, KBulkTransferInId0, KBulkTransferInId1, aTransferId);
1.218 + break; //switch(aTransferId)
1.219 + }
1.220 +
1.221 + if(err==KErrNone && iTransferComplete == (KBulkTransferInId0 | KBulkTransferInId1))
1.222 + {
1.223 + // ok, compare data rcvd now
1.224 + iTransferComplete = 0; //reset
1.225 + TPtrC8 data1(iInTransfer[0]->DataPolled());
1.226 + TPtrC8 data2(iInTransfer[1]->DataPolled());
1.227 + //Validate first transfer for number of bytes requested.
1.228 + if(ValidateData(data1, KLiteralEnglish8(), KHostNumReadBytesPostHalt1) == EFalse)
1.229 + {
1.230 + err = KErrCompletion; //indicates data validation failure
1.231 + break; //switch(iCaseStep)
1.232 + }
1.233 +
1.234 + //Validate second transfer for the remainder of bytes that the device should have written.
1.235 + //NB The number of bytes requested is more than this remainder to accommodate the ZLP.
1.236 + if(ValidateData(data2, KLiteralEnglish8(), KHostNumReadBytesPostHalt1, KDeviceNumWriteBytesPostHalt-KHostNumReadBytesPostHalt1) == EFalse)
1.237 + {
1.238 + err = KErrCompletion; //indicates data validation failure
1.239 + break; //switch(iCaseStep)
1.240 + }
1.241 +
1.242 + // Comparison is a match
1.243 + RDebug::Printf("Comparison for IN transfer is a match");
1.244 + iCaseStep = EPassed;
1.245 + TTestCasePassed request;
1.246 + iControlEp0->SendRequest(request,this);
1.247 + }
1.248 + }
1.249 + break; //switch(iCaseStep)
1.250 +
1.251 + default:
1.252 + err = KUndefinedStep;
1.253 + msg.Format(_L("<Error %d> Undefined case step %d reached"),KUndefinedStep, iCaseStep);
1.254 + break; //switch(iCaseStep)
1.255 + }
1.256 +
1.257 + if(err == KErrCompletion)
1.258 + //indicates data validation failure
1.259 + {
1.260 + msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
1.261 + }
1.262 +
1.263 + if(err!=KErrNone)
1.264 + {
1.265 + RDebug::Print(msg);
1.266 + iCaseStep = EFailed;
1.267 + TTestCaseFailed request(err,msg);
1.268 + return iControlEp0->SendRequest(request,this);
1.269 + }
1.270 + }
1.271 +
1.272 +void CUT_PBASE_T_USBDI_0500::DeviceInsertedL(TUint aDeviceHandle)
1.273 + {
1.274 + LOG_FUNC
1.275 +
1.276 + Cancel();
1.277 + RDebug::Printf("this - %08x", this);
1.278 +
1.279 + TBuf<256> msg;
1.280 + TInt err = KErrNone;
1.281 + if(BaseBulkDeviceInsertedL(aDeviceHandle, EFalse) == EDeviceConfigurationError)
1.282 + // Prepare for response from control transfer to client
1.283 + {
1.284 + err = KErrGeneral;
1.285 + msg.Format(_L("Base class DeviceInsertedL failed"));
1.286 + }
1.287 + else
1.288 + {
1.289 + iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxINTransferSize,*this,KBulkTransferInId0);
1.290 + iInTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxINTransferSize,*this,KBulkTransferInId1);
1.291 +
1.292 + // Initialise the descriptors for transfer
1.293 + RDebug::Printf("Initialising the transfer descriptors");
1.294 + err = iUsbInterface1.InitialiseTransferDescriptors();
1.295 + if(err != KErrNone)
1.296 + {
1.297 + msg.Format(_L("<Error %d> Unable to initialise transfer descriptors"),err);
1.298 + }
1.299 + }
1.300 + if(err != KErrNone)
1.301 + {
1.302 + RDebug::Print(msg);
1.303 + iCaseStep = EFailed;
1.304 + TTestCaseFailed request(err,msg);
1.305 + iControlEp0->SendRequest(request,this);
1.306 + }
1.307 + else
1.308 + {
1.309 + RDebug::Printf("Asking client for 'Write' and 'Halt'");
1.310 + iCaseStep = ETransferInHalt;
1.311 + TEndpointPatternSynchronousWriteAndHaltRequest request(1,1,KLiteralFrench4(),KDeviceNumWriteBytesPreHalt);// EP1 means endpoint index 1 not the actual endpoint number
1.312 + iControlEp0->SendRequest(request,this);
1.313 + }
1.314 + }
1.315 +
1.316 + } //end namespace