os/kernelhwsrv/kerneltest/e32test/usbho/t_usbdi/src/endpointreader.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2007-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 endpointreader.cpp
sl@0
    15
// @internalComponent
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include "endpointreader.h"
sl@0
    20
#include "controltransferrequests.h"
sl@0
    21
#include "testdebug.h"
sl@0
    22
sl@0
    23
namespace NUnitTesting_USBDI
sl@0
    24
	{
sl@0
    25
const TUint8 KNumPatternRepeatsNeededForValidation = 4;
sl@0
    26
sl@0
    27
CEndpointReader::CEndpointReader(RDevUsbcClient& aClientDriver,TEndpointNumber aEndpoint)
sl@0
    28
:	CActive(EPriorityStandard),
sl@0
    29
	iClientDriver(aClientDriver),
sl@0
    30
	iEndpoint(aEndpoint),
sl@0
    31
	iDataPtr(NULL,0),
sl@0
    32
	iValidationPatternPtr(NULL,0)
sl@0
    33
	{
sl@0
    34
	CActiveScheduler::Add(this);
sl@0
    35
	}
sl@0
    36
	
sl@0
    37
CEndpointReader::~CEndpointReader()
sl@0
    38
	{
sl@0
    39
	LOG_FUNC
sl@0
    40
	Cancel();
sl@0
    41
	delete iDataBuffer;
sl@0
    42
	iDataBuffer = NULL;
sl@0
    43
	delete iValidationPatternBuffer;
sl@0
    44
	iValidationPatternBuffer = NULL;
sl@0
    45
	}
sl@0
    46
			
sl@0
    47
TPtr8 CEndpointReader::Buffer()
sl@0
    48
	{
sl@0
    49
	return iDataPtr;
sl@0
    50
	}
sl@0
    51
sl@0
    52
TBool CEndpointReader::IsValid()
sl@0
    53
	{
sl@0
    54
	return iIsValid;
sl@0
    55
	}
sl@0
    56
sl@0
    57
TUint CEndpointReader::NumBytesReadSoFar()
sl@0
    58
	{
sl@0
    59
	return iNumBytesReadSoFar;
sl@0
    60
	}
sl@0
    61
sl@0
    62
void CEndpointReader::ReadPacketL(MEndpointDataHandler* aHandler)
sl@0
    63
	{
sl@0
    64
	LOG_FUNC
sl@0
    65
	RDebug::Printf("Endpoint %d", iEndpoint);
sl@0
    66
	
sl@0
    67
	iHandler = aHandler;
sl@0
    68
sl@0
    69
	// Allocate buffer for reading a data packet
sl@0
    70
	if(iDataBuffer)
sl@0
    71
		{
sl@0
    72
		delete iDataBuffer;
sl@0
    73
		iDataBuffer = NULL;
sl@0
    74
		}
sl@0
    75
	iDataBuffer = HBufC8::NewL(KFullSpeedPacketSize); 
sl@0
    76
	iDataPtr.Set(iDataBuffer->Des());
sl@0
    77
sl@0
    78
	// Read from the endpoint
sl@0
    79
	// Read from the endpoint
sl@0
    80
	iClientDriver.ReadPacket(iStatus,iEndpoint,iDataPtr,KFullSpeedPacketSize);
sl@0
    81
	SetActive();
sl@0
    82
	}
sl@0
    83
sl@0
    84
sl@0
    85
void CEndpointReader::ReadL(TInt aByteCount)
sl@0
    86
	{
sl@0
    87
	LOG_FUNC
sl@0
    88
	RDebug::Printf("Endpoint %d", iEndpoint);
sl@0
    89
	
sl@0
    90
	// Allocate buffer for reading a data packet
sl@0
    91
	if(iDataBuffer)
sl@0
    92
		{
sl@0
    93
		delete iDataBuffer;
sl@0
    94
		iDataBuffer = NULL;
sl@0
    95
		}
sl@0
    96
	iDataBuffer = HBufC8::NewL(aByteCount);
sl@0
    97
	iDataPtr.Set(iDataBuffer->Des());
sl@0
    98
sl@0
    99
	// Read from the endpoint
sl@0
   100
	iClientDriver.Read(iStatus,iEndpoint,iDataPtr,aByteCount);
sl@0
   101
	SetActive();
sl@0
   102
	}
sl@0
   103
sl@0
   104
void CEndpointReader::ReadUntilShortL(TInt aByteCount)
sl@0
   105
	{
sl@0
   106
	LOG_FUNC
sl@0
   107
	RDebug::Printf("Endpoint %d", iEndpoint);
sl@0
   108
	
sl@0
   109
	// Allocate buffer for reading a data packet
sl@0
   110
	if(iDataBuffer)
sl@0
   111
		{
sl@0
   112
		delete iDataBuffer;
sl@0
   113
		iDataBuffer = NULL;
sl@0
   114
		}
sl@0
   115
	iDataBuffer = HBufC8::NewL(aByteCount);
sl@0
   116
	iDataPtr.Set(iDataBuffer->Des());
sl@0
   117
sl@0
   118
	// Read from the endpoint
sl@0
   119
	iClientDriver.ReadUntilShort(iStatus,iEndpoint,iDataPtr,aByteCount);
sl@0
   120
	SetActive();
sl@0
   121
	}
sl@0
   122
sl@0
   123
void CEndpointReader::ReadAndHaltL(TInt aByteCount)
sl@0
   124
	{
sl@0
   125
	LOG_FUNC
sl@0
   126
	iCompletionAction = EHaltEndpoint;
sl@0
   127
	ReadL(aByteCount);
sl@0
   128
	}
sl@0
   129
sl@0
   130
void CEndpointReader::RepeatedReadAndValidateL(const TDesC8& aDataPattern, TUint aNumBytesPerRead, TUint aTotalNumBytes)
sl@0
   131
	{
sl@0
   132
	LOG_FUNC
sl@0
   133
sl@0
   134
	iCompletionAction = ERepeatedRead;
sl@0
   135
	iRepeatedReadTotalNumBytes = aTotalNumBytes;
sl@0
   136
	iRepeatedReadNumBytesPerRead = aNumBytesPerRead;
sl@0
   137
	iNumBytesReadSoFar = 0;
sl@0
   138
	iDataPatternLength = aDataPattern.Length();
sl@0
   139
	iIsValid = ETrue; //until proven guilty!
sl@0
   140
	RDebug::Printf("Total Bytes To Read: %d, Bytes Per Individual Read: %d", iRepeatedReadTotalNumBytes, iRepeatedReadNumBytesPerRead);
sl@0
   141
	//Create buffer to contain two lots of the payload pattern
sl@0
   142
	//..so that we may grab cyclic chunks of said payload pattern
sl@0
   143
	delete iValidationPatternBuffer;
sl@0
   144
	iValidationPatternBuffer = NULL;
sl@0
   145
	iValidationPatternBuffer = HBufC8::New(KNumPatternRepeatsNeededForValidation*aDataPattern.Length()); 
sl@0
   146
	iValidationPatternPtr.Set(iValidationPatternBuffer->Des());
sl@0
   147
	iValidationPatternPtr.Zero();
sl@0
   148
	for(TUint i=0;i<KNumPatternRepeatsNeededForValidation;i++)
sl@0
   149
		{
sl@0
   150
		iValidationPatternPtr.Append(aDataPattern);
sl@0
   151
		}
sl@0
   152
sl@0
   153
	ReadUntilShortL(iRepeatedReadNumBytesPerRead);
sl@0
   154
	}
sl@0
   155
sl@0
   156
sl@0
   157
TInt CEndpointReader::Acknowledge()
sl@0
   158
	{
sl@0
   159
	LOG_FUNC
sl@0
   160
	TInt err(iClientDriver.SendEp0StatusPacket());
sl@0
   161
	if(err != KErrNone)
sl@0
   162
		{
sl@0
   163
		RDebug::Printf("<Error %d> Sending acknowledge packet",err);
sl@0
   164
		}
sl@0
   165
	return err;
sl@0
   166
	}
sl@0
   167
			
sl@0
   168
			
sl@0
   169
void CEndpointReader::DoCancel()
sl@0
   170
	{
sl@0
   171
	LOG_FUNC
sl@0
   172
	
sl@0
   173
	// Cancel reading from the endpoint
sl@0
   174
	
sl@0
   175
	iClientDriver.ReadCancel(iEndpoint);
sl@0
   176
	}
sl@0
   177
	
sl@0
   178
	
sl@0
   179
void CEndpointReader::RunL()
sl@0
   180
	{
sl@0
   181
	LOG_FUNC
sl@0
   182
	RDebug::Printf("Endpoint %d", iEndpoint);
sl@0
   183
	RDebug::Printf("Completion Action %d", iCompletionAction);
sl@0
   184
	TCompletionAction completionAction = iCompletionAction;
sl@0
   185
	iCompletionAction = ENone; //reset here in case of 'early' returns
sl@0
   186
	
sl@0
   187
	// The operation completion code
sl@0
   188
	TInt completionCode(iStatus.Int());
sl@0
   189
	
sl@0
   190
	if(completionCode != KErrNone)
sl@0
   191
		{
sl@0
   192
		RDebug::Printf("<Error> void CEndpointReader::RunL()completed with ERROR %d", completionCode);
sl@0
   193
		
sl@0
   194
		// Nak the packet received
sl@0
   195
		iClientDriver.HaltEndpoint(iEndpoint);
sl@0
   196
		
sl@0
   197
		if(iHandler)
sl@0
   198
			{
sl@0
   199
			iHandler->EndpointReadError(iEndpoint,completionCode);
sl@0
   200
			}
sl@0
   201
		else
sl@0
   202
			{
sl@0
   203
			RDebug::Printf("No handler set");
sl@0
   204
			}
sl@0
   205
		}
sl@0
   206
	else
sl@0
   207
		{
sl@0
   208
		// Some data has arrived but 
sl@0
   209
		TInt ent = iDataBuffer->Length()/16;
sl@0
   210
		ent = ent==0?1:ent;
sl@0
   211
		for(TInt i=0; i<iDataBuffer->Length(); i+=ent)
sl@0
   212
			{
sl@0
   213
			RDebug::Printf("byte %d %02x %c",i,(*iDataBuffer)[i],(*iDataBuffer)[i]);
sl@0
   214
			}
sl@0
   215
sl@0
   216
		if(iHandler)
sl@0
   217
			{
sl@0
   218
			iHandler->DataReceivedFromEndpointL(iEndpoint,*iDataBuffer);
sl@0
   219
			}
sl@0
   220
		else
sl@0
   221
			{
sl@0
   222
			RDebug::Printf("No handler set");
sl@0
   223
			}
sl@0
   224
		
sl@0
   225
		if(completionAction==EHaltEndpoint)
sl@0
   226
			{
sl@0
   227
			RDebug::Printf("Halting Endpoint");
sl@0
   228
			iClientDriver.HaltEndpoint(iEndpoint);
sl@0
   229
			}
sl@0
   230
sl@0
   231
		if(completionAction==ERepeatedRead)
sl@0
   232
			{
sl@0
   233
			RDebug::Printf("Repeated Read");
sl@0
   234
			iCompletionAction = ERepeatedRead;
sl@0
   235
			
sl@0
   236
			//Prepare to validate
sl@0
   237
			TInt previousBytesToRead = iRepeatedReadTotalNumBytes - iNumBytesReadSoFar; //PRIOR TO THIS READ
sl@0
   238
			TUint expectedNumJustReadBytes = previousBytesToRead < iRepeatedReadNumBytesPerRead ? previousBytesToRead : iRepeatedReadNumBytesPerRead;
sl@0
   239
			TPtrC8 valDesc = iValidationPatternPtr.Mid(iNumBytesReadSoFar%iDataPatternLength, expectedNumJustReadBytes);
sl@0
   240
			TInt err = iDataPtr.Compare(valDesc);
sl@0
   241
sl@0
   242
			iNumBytesReadSoFar += iDataPtr.Length();
sl@0
   243
			RDebug::Printf("Bytes read so far %d, Total bytes to read %d", iNumBytesReadSoFar, iRepeatedReadTotalNumBytes);
sl@0
   244
sl@0
   245
			if(err!=0)
sl@0
   246
				{
sl@0
   247
				RDebug::Printf("Validation Result %d, Validation String Length %d, Bytes Actually Read %d", err, valDesc.Length(), iDataPtr.Length());
sl@0
   248
				RDebug::Printf("Expected string, followed by read string");
sl@0
   249
				RDebug::RawPrint(valDesc);
sl@0
   250
				RDebug::Printf("\n");
sl@0
   251
				RDebug::RawPrint(iDataPtr);
sl@0
   252
				RDebug::Printf("\n");
sl@0
   253
				iIsValid = EFalse; //record validation error
sl@0
   254
				}
sl@0
   255
			
sl@0
   256
			if(iNumBytesReadSoFar < iRepeatedReadTotalNumBytes)
sl@0
   257
				{
sl@0
   258
				ReadUntilShortL(iRepeatedReadNumBytesPerRead);
sl@0
   259
				}
sl@0
   260
			else
sl@0
   261
				{
sl@0
   262
				//reset
sl@0
   263
				iRepeatedReadTotalNumBytes = 0;
sl@0
   264
				iRepeatedReadNumBytesPerRead = 0;
sl@0
   265
				iNumBytesReadSoFar = 0;
sl@0
   266
				iDataPatternLength = 0;
sl@0
   267
				//do not reset iIsValid - this is required later
sl@0
   268
				}
sl@0
   269
			}
sl@0
   270
		}
sl@0
   271
	}
sl@0
   272
sl@0
   273
TInt CEndpointReader::RunError(TInt aError)
sl@0
   274
	{
sl@0
   275
	LOG_FUNC
sl@0
   276
	
sl@0
   277
	RDebug::Printf("<Leaving Error> void CEndpointReader::RunError()called with ERROR %d", aError);
sl@0
   278
sl@0
   279
	// Nak the packet received
sl@0
   280
	iClientDriver.HaltEndpoint(iEndpoint);
sl@0
   281
sl@0
   282
	aError = KErrNone;	
sl@0
   283
	return aError;
sl@0
   284
	}
sl@0
   285
	
sl@0
   286
	}