os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/PBASE-T_USBDI-0499.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// @file PBASE-T_USBDI-0499.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "PBASE-T_USBDI-0499.h"
sl@0
    20
#include "testpolicy.h"
sl@0
    21
#include "modelleddevices.h"
sl@0
    22
#include "testliterals.h"
sl@0
    23
sl@0
    24
sl@0
    25
 
sl@0
    26
sl@0
    27
namespace NUnitTesting_USBDI
sl@0
    28
	{
sl@0
    29
const TInt KBulkMaxINTransferSize = 1200;
sl@0
    30
const TInt KBulkMaxOUTTransferSize = 150000;
sl@0
    31
const TUint KHostNumWriteBytesPreHalt1 = 128000;
sl@0
    32
const TUint KHostNumWriteBytesPreHalt2 = 128000;
sl@0
    33
const TUint KHostNumWriteBytesPostHalt1 = 512;
sl@0
    34
const TUint KHostNumWriteBytesPostHalt2 = 512;
sl@0
    35
const TInt KDeviceNumWrittenBytesPreHalt = KHostNumWriteBytesPreHalt1+KHostNumWriteBytesPreHalt2;
sl@0
    36
const TInt KDeviceNumReadBytesPostHalt = KHostNumWriteBytesPostHalt1+KHostNumWriteBytesPostHalt2;
sl@0
    37
const TInt KDeviceNumReadBytesPreHalt = 128;
sl@0
    38
const TInt KHostNumReadBytesPostHalt = KDeviceNumReadBytesPostHalt+1; //read cached data, allow 1 for ZLP
sl@0
    39
sl@0
    40
sl@0
    41
//Make these single bit values ... 
sl@0
    42
// ... so that their completion can be easily recorded in a bit mask!
sl@0
    43
const TInt KBulkTransferInId0 = 1<<0;
sl@0
    44
const TInt KBulkTransferOutId0 = 1<<3;
sl@0
    45
const TInt KBulkTransferOutId1 = 1<<4;
sl@0
    46
sl@0
    47
const TInt KUnexpectedTransferID = -101;
sl@0
    48
const TInt KUndefinedStep	 = -102;
sl@0
    49
sl@0
    50
sl@0
    51
_LIT(KTestCaseId,"PBASE-T_USBDI-0499");
sl@0
    52
const TFunctorTestCase<CUT_PBASE_T_USBDI_0499,TBool> CUT_PBASE_T_USBDI_0499::iFunctor(KTestCaseId);	
sl@0
    53
sl@0
    54
CUT_PBASE_T_USBDI_0499* CUT_PBASE_T_USBDI_0499::NewL(TBool aHostRole)
sl@0
    55
	{
sl@0
    56
	CUT_PBASE_T_USBDI_0499* self = new (ELeave) CUT_PBASE_T_USBDI_0499(aHostRole);
sl@0
    57
	CleanupStack::PushL(self);
sl@0
    58
	self->ConstructL();
sl@0
    59
	CleanupStack::Pop(self);
sl@0
    60
	return self;
sl@0
    61
	}
sl@0
    62
	
sl@0
    63
sl@0
    64
CUT_PBASE_T_USBDI_0499::CUT_PBASE_T_USBDI_0499(TBool aHostRole)
sl@0
    65
:	CBaseBulkTestCase(KTestCaseId,aHostRole),
sl@0
    66
	iCaseStep(EInProgress)
sl@0
    67
	{
sl@0
    68
	} 
sl@0
    69
sl@0
    70
sl@0
    71
void CUT_PBASE_T_USBDI_0499::ConstructL()
sl@0
    72
	{
sl@0
    73
	BaseBulkConstructL();
sl@0
    74
	}
sl@0
    75
sl@0
    76
sl@0
    77
CUT_PBASE_T_USBDI_0499::~CUT_PBASE_T_USBDI_0499()
sl@0
    78
	{
sl@0
    79
	LOG_FUNC
sl@0
    80
	}
sl@0
    81
	
sl@0
    82
	
sl@0
    83
void CUT_PBASE_T_USBDI_0499::Ep0TransferCompleteL(TInt aCompletionCode)
sl@0
    84
	{
sl@0
    85
	LOG_FUNC
sl@0
    86
	
sl@0
    87
	RDebug::Printf("Ep0TransferCompleteL with aCompletionCode = %d",aCompletionCode);
sl@0
    88
	
sl@0
    89
	if(aCompletionCode != KErrNone)
sl@0
    90
		{
sl@0
    91
		if(iCaseStep == EFailed)
sl@0
    92
			{// ignore error, nad catch the TestFailed method called further down.
sl@0
    93
			RDebug::Printf("***Failure sending FAIL message to client on endpoint 0***");
sl@0
    94
			}
sl@0
    95
		else
sl@0
    96
			{
sl@0
    97
			TBuf<256> msg;
sl@0
    98
			msg.Format(_L("<Error %d> Transfer to control endpoint 0 was not successful"),aCompletionCode);
sl@0
    99
			RDebug::Print(msg);
sl@0
   100
			iCaseStep = EFailed;
sl@0
   101
			TTestCaseFailed request(aCompletionCode,msg);
sl@0
   102
			iControlEp0->SendRequest(request,this);
sl@0
   103
			return;
sl@0
   104
			}
sl@0
   105
		}
sl@0
   106
sl@0
   107
	switch(iCaseStep)
sl@0
   108
		{
sl@0
   109
		// Test case passed
sl@0
   110
		case EPassed:
sl@0
   111
			TestPassed();
sl@0
   112
			break;
sl@0
   113
		
sl@0
   114
		// Test case failed	
sl@0
   115
		case EFailed:
sl@0
   116
			TestFailed(KErrCompletion);
sl@0
   117
			break;
sl@0
   118
			
sl@0
   119
		case ETransferOutHalt:
sl@0
   120
			iOutTransfer[0]->TransferOut(KLiteralFrench4(), KHostNumWriteBytesPreHalt1, EFalse);
sl@0
   121
			iOutTransfer[1]->TransferOut(KLiteralFrench4(), KHostNumWriteBytesPreHalt2, EFalse);
sl@0
   122
			break;
sl@0
   123
		
sl@0
   124
		case EAwaitClearPreHalt:
sl@0
   125
			{
sl@0
   126
			RDebug::Printf("Client has been asked to clear endpoint buffer");
sl@0
   127
			User::After(1000000); //Give client time to clear buffer
sl@0
   128
			TEndpointCancelReadRequest request(1,1);
sl@0
   129
			iControlEp0->SendRequest(request,this);
sl@0
   130
			iCaseStep = EAwaitCancelRead;
sl@0
   131
			}
sl@0
   132
			break;
sl@0
   133
sl@0
   134
		case EAwaitCancelRead:
sl@0
   135
			{
sl@0
   136
			TEndpointReadRequest request(1,1, KDeviceNumReadBytesPostHalt);// EP1 because 1st writter EP
sl@0
   137
			iControlEp0->SendRequest(request,this);
sl@0
   138
			iCaseStep = ETransferOut;
sl@0
   139
			}
sl@0
   140
			break;
sl@0
   141
			
sl@0
   142
		case ETransferOut:
sl@0
   143
			RDebug::Printf("Try to send data (post halt)");
sl@0
   144
			iOutTransfer[0]->TransferOut(KLiteralEnglish8().Mid(0, KHostNumWriteBytesPostHalt1), EFalse);
sl@0
   145
			iOutTransfer[1]->TransferOut(KLiteralEnglish8().Mid(KHostNumWriteBytesPostHalt1, KHostNumWriteBytesPostHalt2), EFalse);
sl@0
   146
			break;
sl@0
   147
		
sl@0
   148
		case ETransferIn:
sl@0
   149
			RDebug::Printf("Try to receive data");
sl@0
   150
			iInTransfer[0]->TransferIn(KHostNumReadBytesPostHalt);
sl@0
   151
			break;
sl@0
   152
	
sl@0
   153
		default:
sl@0
   154
			RDebug::Printf("<Error> Unknown test step");
sl@0
   155
			TestFailed(KErrUnknown);
sl@0
   156
			break;
sl@0
   157
		}
sl@0
   158
	}
sl@0
   159
	
sl@0
   160
void CUT_PBASE_T_USBDI_0499::TransferCompleteL(TInt aTransferId,TInt aCompletionCode)
sl@0
   161
	{
sl@0
   162
	LOG_FUNC
sl@0
   163
	Cancel();
sl@0
   164
	
sl@0
   165
	TInt err(KErrNone);
sl@0
   166
	TBuf<256> msg;
sl@0
   167
	RDebug::Printf("Transfer completed (id=%d), aCompletionCode = %d",aTransferId, aCompletionCode);
sl@0
   168
sl@0
   169
	switch(iCaseStep)
sl@0
   170
		{
sl@0
   171
		case ETransferOutHalt:
sl@0
   172
			{
sl@0
   173
			if(aCompletionCode != KErrUsbStalled)
sl@0
   174
				{
sl@0
   175
				iOutTransfer[0]->Cancel();
sl@0
   176
				iOutTransfer[1]->Cancel();
sl@0
   177
				err = KErrCorrupt;
sl@0
   178
				msg.Format(_L("<Error %d> The transfer completed with no errors but the endpoint should have halted"),aCompletionCode);
sl@0
   179
				break; //switch(iCaseStep)
sl@0
   180
				}
sl@0
   181
	
sl@0
   182
			switch(aTransferId)
sl@0
   183
				{
sl@0
   184
				case KBulkTransferOutId0:
sl@0
   185
				case KBulkTransferOutId1:
sl@0
   186
					iTransferComplete |= aTransferId;
sl@0
   187
					RDebug::Printf("Transfer %d stalled", aTransferId);
sl@0
   188
					break; //switch(aTransferId)
sl@0
   189
				
sl@0
   190
				default:
sl@0
   191
					iTransferComplete = 0; //reset
sl@0
   192
					err = KUnexpectedTransferID;
sl@0
   193
					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
sl@0
   194
							       err, KBulkTransferOutId0, KBulkTransferOutId1, aTransferId);
sl@0
   195
					break; //switch(aTransferId)
sl@0
   196
				}
sl@0
   197
sl@0
   198
			if(err==KErrNone && iTransferComplete == (KBulkTransferOutId0 | KBulkTransferOutId1))
sl@0
   199
				{
sl@0
   200
				RDebug::Printf("Clear halt and try to send data again. Transfers Completed %d", iTransferComplete);
sl@0
   201
				// Acknowledge the stall and clear				
sl@0
   202
				err = iTestPipeInterface1BulkOut.ClearRemoteStall();
sl@0
   203
				if(err != KErrNone)
sl@0
   204
					{
sl@0
   205
					msg.Format(_L("<Error %d> The remote stall cannot be cleared"),err);
sl@0
   206
					break; //switch(iCaseStep)
sl@0
   207
					}
sl@0
   208
				iCaseStep = EAwaitClearPreHalt;
sl@0
   209
				TEndpointReadRequest request(1,1, KDeviceNumWrittenBytesPreHalt);// EP1 because 1st writter EP
sl@0
   210
				iControlEp0->SendRequest(request,this);	
sl@0
   211
				iTransferComplete = 0; //reset
sl@0
   212
				}
sl@0
   213
			}
sl@0
   214
			break; //switch(iCaseStep)
sl@0
   215
sl@0
   216
		case ETransferOut:
sl@0
   217
			{
sl@0
   218
			if(aCompletionCode != KErrNone)
sl@0
   219
				{
sl@0
   220
				iOutTransfer[0]->Cancel();
sl@0
   221
				iOutTransfer[1]->Cancel();
sl@0
   222
				err = KErrCorrupt;
sl@0
   223
				msg.Format(_L("<Error %d> No data sent on bulk OUT request"),aCompletionCode);
sl@0
   224
				break; //switch(iCaseStep)
sl@0
   225
				}
sl@0
   226
	
sl@0
   227
			switch(aTransferId)
sl@0
   228
				{
sl@0
   229
				case KBulkTransferOutId0:
sl@0
   230
				case KBulkTransferOutId1:
sl@0
   231
					iTransferComplete |= aTransferId;
sl@0
   232
					RDebug::Printf("Transfer %d completed", aTransferId);
sl@0
   233
					break; //switch(aTransferId)
sl@0
   234
sl@0
   235
				default:
sl@0
   236
					iTransferComplete = 0; //reset
sl@0
   237
					err = KUnexpectedTransferID;
sl@0
   238
					msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d or %d, got %d"),
sl@0
   239
							       err, KBulkTransferOutId0, KBulkTransferOutId1, aTransferId);
sl@0
   240
					break; //switch(aTransferId)
sl@0
   241
				}
sl@0
   242
sl@0
   243
			if(err==KErrNone && iTransferComplete == (KBulkTransferOutId0 | KBulkTransferOutId1))
sl@0
   244
				{
sl@0
   245
				RDebug::Printf("Try to receive back sent data. Transfers Completed %d", iTransferComplete);
sl@0
   246
				iCaseStep = ETransferIn;
sl@0
   247
				TWriteSynchronousCachedReadDataRequest request(1,1,1);
sl@0
   248
				iControlEp0->SendRequest(request,this);	
sl@0
   249
				iTransferComplete = 0; //reset
sl@0
   250
				}
sl@0
   251
			break; //switch(iCaseStep)
sl@0
   252
sl@0
   253
		case ETransferIn:
sl@0
   254
			if(aCompletionCode != KErrNone)
sl@0
   255
				{
sl@0
   256
				err = KErrCorrupt;
sl@0
   257
				msg.Format(_L("<Error %d> No data sent on bulk IN request"),aCompletionCode);
sl@0
   258
				break; //switch(iCaseStep)
sl@0
   259
				}
sl@0
   260
	
sl@0
   261
			if(aTransferId != KBulkTransferInId0)
sl@0
   262
				{
sl@0
   263
				iTransferComplete = 0; //reset
sl@0
   264
				err = KUnexpectedTransferID;
sl@0
   265
				msg.Format(_L("<Error %d> Unexpected transfer ID, wanted %d , got %d"),
sl@0
   266
					       err, KBulkTransferInId0, aTransferId);
sl@0
   267
				break; //switch(iCaseStep)
sl@0
   268
				}
sl@0
   269
				
sl@0
   270
			// else ok, compare data rcvd now
sl@0
   271
			TPtrC8 data1(iInTransfer[0]->DataPolled());		
sl@0
   272
			if(ValidateData(data1, KLiteralEnglish8().Mid(0, KDeviceNumReadBytesPostHalt)) == EFalse)
sl@0
   273
				{
sl@0
   274
				err = KErrCompletion; //indicates data validation failure
sl@0
   275
				msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
sl@0
   276
				break; //switch(iCaseStep)
sl@0
   277
				}
sl@0
   278
			
sl@0
   279
			// Comparison is a match
sl@0
   280
			RDebug::Printf("Comparison for IN transfer is a match");
sl@0
   281
			iCaseStep = EPassed;
sl@0
   282
			TTestCasePassed request;
sl@0
   283
			iControlEp0->SendRequest(request,this);
sl@0
   284
			iTransferComplete = 0; //reset
sl@0
   285
			}
sl@0
   286
			break; //switch(iCaseStep)
sl@0
   287
sl@0
   288
		default:
sl@0
   289
			err = KUndefinedStep;
sl@0
   290
			msg.Format(_L("<Error %d> Undefined case step %d reached"),KUndefinedStep, iCaseStep);
sl@0
   291
			break; //switch(iCaseStep)
sl@0
   292
		}
sl@0
   293
	
sl@0
   294
	if(err == KErrCompletion)
sl@0
   295
		//indicates data validation failure
sl@0
   296
		{
sl@0
   297
		msg.Format(_L("<Error %d> Bulk transfer IN data received does not match Bulk Transfer OUT data"), err);
sl@0
   298
		}
sl@0
   299
	
sl@0
   300
	if(err!=KErrNone)
sl@0
   301
		{	
sl@0
   302
		RDebug::Print(msg);
sl@0
   303
		iCaseStep = EFailed;
sl@0
   304
		TTestCaseFailed request(err,msg);
sl@0
   305
		return iControlEp0->SendRequest(request,this);
sl@0
   306
		}	
sl@0
   307
	}
sl@0
   308
	
sl@0
   309
void CUT_PBASE_T_USBDI_0499::DeviceInsertedL(TUint aDeviceHandle)
sl@0
   310
	{
sl@0
   311
	LOG_FUNC
sl@0
   312
	
sl@0
   313
	Cancel();
sl@0
   314
	
sl@0
   315
	if(BaseBulkDeviceInsertedL(aDeviceHandle) == EDeviceConfigurationError)
sl@0
   316
		// Prepare for response from control transfer to client
sl@0
   317
		{
sl@0
   318
		iCaseStep = EFailed;
sl@0
   319
		}
sl@0
   320
	
sl@0
   321
	// Create the bulk transfers	
sl@0
   322
	iInTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkIn,iUsbInterface1,KBulkMaxINTransferSize,*this,KBulkTransferInId0);
sl@0
   323
	iOutTransfer[0] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxOUTTransferSize,*this,KBulkTransferOutId0);
sl@0
   324
	iOutTransfer[1] = new (ELeave) CBulkTransfer(iTestPipeInterface1BulkOut,iUsbInterface1,KBulkMaxOUTTransferSize,*this,KBulkTransferOutId1);
sl@0
   325
	
sl@0
   326
	// Initialise the descriptors for transfer		
sl@0
   327
	RDebug::Printf("Initialising the transfer descriptors");
sl@0
   328
	TInt err = iUsbInterface1.InitialiseTransferDescriptors();
sl@0
   329
	if(err != KErrNone)
sl@0
   330
		{
sl@0
   331
		TBuf<256> msg;
sl@0
   332
		msg.Format(_L("<Error %d> Unable to initialise transfer descriptors"),err);
sl@0
   333
		RDebug::Print(msg);
sl@0
   334
		iCaseStep = EFailed;
sl@0
   335
		TTestCaseFailed request(err,msg);
sl@0
   336
		iControlEp0->SendRequest(request,this);
sl@0
   337
		return;
sl@0
   338
		}
sl@0
   339
sl@0
   340
	iCaseStep = ETransferOutHalt;	
sl@0
   341
	TEndpointReadAndHaltRequest request(1,1,KDeviceNumReadBytesPreHalt);// EP1 means endpoint index 1 not the actual endpoint number
sl@0
   342
	iControlEp0->SendRequest(request,this);
sl@0
   343
	}
sl@0
   344
	
sl@0
   345
	} //end namespace