kerneltest/e32test/usbho/t_usbdi/src/endpointreader.cpp
changeset 259 57b9594f5772
parent 0 a41df078684a
child 257 3e88ff8f41d5
equal deleted inserted replaced
247:d8d70de2bd36 259:57b9594f5772
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    17 //
    17 //
    18 
    18 
    19 #include "endpointreader.h"
    19 #include "endpointreader.h"
    20 #include "controltransferrequests.h"
    20 #include "controltransferrequests.h"
    21 #include "testdebug.h"
    21 #include "testdebug.h"
       
    22 #include "OstTraceDefinitions.h"
       
    23 #ifdef OST_TRACE_COMPILER_IN_USE
       
    24 #include "endpointreaderTraces.h"
       
    25 #endif
    22 
    26 
    23 namespace NUnitTesting_USBDI
    27 namespace NUnitTesting_USBDI
    24 	{
    28 	{
    25 const TUint8 KNumPatternRepeatsNeededForValidation = 4;
    29 const TUint8 KNumPatternRepeatsNeededForValidation = 4;
    26 
    30 
    29 	iClientDriver(aClientDriver),
    33 	iClientDriver(aClientDriver),
    30 	iEndpoint(aEndpoint),
    34 	iEndpoint(aEndpoint),
    31 	iDataPtr(NULL,0),
    35 	iDataPtr(NULL,0),
    32 	iValidationPatternPtr(NULL,0)
    36 	iValidationPatternPtr(NULL,0)
    33 	{
    37 	{
       
    38 	OstTraceFunctionEntryExt( CENDPOINTREADER_CENDPOINTREADER_ENTRY, this );
    34 	CActiveScheduler::Add(this);
    39 	CActiveScheduler::Add(this);
       
    40 	OstTraceFunctionExit1( CENDPOINTREADER_CENDPOINTREADER_EXIT, this );
    35 	}
    41 	}
    36 	
    42 	
    37 CEndpointReader::~CEndpointReader()
    43 CEndpointReader::~CEndpointReader()
    38 	{
    44 	{
    39 	LOG_FUNC
    45 	OstTraceFunctionEntry1( CENDPOINTREADER_CENDPOINTREADER_ENTRY_DUP01, this );
    40 	Cancel();
    46 	Cancel();
    41 	delete iDataBuffer;
    47 	delete iDataBuffer;
    42 	iDataBuffer = NULL;
    48 	iDataBuffer = NULL;
    43 	delete iValidationPatternBuffer;
    49 	delete iValidationPatternBuffer;
    44 	iValidationPatternBuffer = NULL;
    50 	iValidationPatternBuffer = NULL;
       
    51 	OstTraceFunctionExit1( CENDPOINTREADER_CENDPOINTREADER_EXIT_DUP01, this );
    45 	}
    52 	}
    46 			
    53 			
    47 TPtr8 CEndpointReader::Buffer()
    54 TPtr8 CEndpointReader::Buffer()
    48 	{
    55 	{
       
    56 	OstTraceFunctionEntry1( CENDPOINTREADER_BUFFER_ENTRY, this );
       
    57 	OstTraceFunctionExitExt( CENDPOINTREADER_BUFFER_EXIT, this, ( TUint )&( iDataPtr ) );
    49 	return iDataPtr;
    58 	return iDataPtr;
    50 	}
    59 	}
    51 
    60 
    52 TBool CEndpointReader::IsValid()
    61 TBool CEndpointReader::IsValid()
    53 	{
    62 	{
       
    63 	OstTraceFunctionEntry1( CENDPOINTREADER_ISVALID_ENTRY, this );
       
    64 	OstTraceFunctionExitExt( CENDPOINTREADER_ISVALID_EXIT, this, iIsValid );
    54 	return iIsValid;
    65 	return iIsValid;
    55 	}
    66 	}
    56 
    67 
    57 TUint CEndpointReader::NumBytesReadSoFar()
    68 TUint CEndpointReader::NumBytesReadSoFar()
    58 	{
    69 	{
       
    70 	OstTraceFunctionEntry1( CENDPOINTREADER_NUMBYTESREADSOFAR_ENTRY, this );
       
    71 	OstTraceFunctionExitExt( CENDPOINTREADER_NUMBYTESREADSOFAR_EXIT, this, iNumBytesReadSoFar );
    59 	return iNumBytesReadSoFar;
    72 	return iNumBytesReadSoFar;
    60 	}
    73 	}
    61 
    74 
    62 void CEndpointReader::ReadPacketL(MEndpointDataHandler* aHandler)
    75 void CEndpointReader::ReadPacketL(MEndpointDataHandler* aHandler)
    63 	{
    76 	{
    64 	LOG_FUNC
    77 	OstTraceFunctionEntryExt( CENDPOINTREADER_READPACKETL_ENTRY, this );
    65 	RDebug::Printf("Endpoint %d", iEndpoint);
    78 	OstTrace1(TRACE_NORMAL, CENDPOINTREADER_READPACKETL, "Endpoint %d", iEndpoint);
    66 	
    79 	
    67 	iHandler = aHandler;
    80 	iHandler = aHandler;
    68 
    81 
    69 	// Allocate buffer for reading a data packet
    82 	// Allocate buffer for reading a data packet
    70 	if(iDataBuffer)
    83 	if(iDataBuffer)
    77 
    90 
    78 	// Read from the endpoint
    91 	// Read from the endpoint
    79 	// Read from the endpoint
    92 	// Read from the endpoint
    80 	iClientDriver.ReadPacket(iStatus,iEndpoint,iDataPtr,KFullSpeedPacketSize);
    93 	iClientDriver.ReadPacket(iStatus,iEndpoint,iDataPtr,KFullSpeedPacketSize);
    81 	SetActive();
    94 	SetActive();
       
    95 	OstTraceFunctionExit1( CENDPOINTREADER_READPACKETL_EXIT, this );
    82 	}
    96 	}
    83 
    97 
    84 
    98 
    85 void CEndpointReader::ReadL(TInt aByteCount)
    99 void CEndpointReader::ReadL(TInt aByteCount)
    86 	{
   100 	{
    87 	LOG_FUNC
   101 	OstTraceFunctionEntryExt( CENDPOINTREADER_READL_ENTRY, this );
    88 	RDebug::Printf("Endpoint %d", iEndpoint);
   102 	OstTrace1(TRACE_NORMAL, CENDPOINTREADER_READL, "Endpoint %d", iEndpoint);
    89 	
   103 	
    90 	// Allocate buffer for reading a data packet
   104 	// Allocate buffer for reading a data packet
    91 	if(iDataBuffer)
   105 	if(iDataBuffer)
    92 		{
   106 		{
    93 		delete iDataBuffer;
   107 		delete iDataBuffer;
    97 	iDataPtr.Set(iDataBuffer->Des());
   111 	iDataPtr.Set(iDataBuffer->Des());
    98 
   112 
    99 	// Read from the endpoint
   113 	// Read from the endpoint
   100 	iClientDriver.Read(iStatus,iEndpoint,iDataPtr,aByteCount);
   114 	iClientDriver.Read(iStatus,iEndpoint,iDataPtr,aByteCount);
   101 	SetActive();
   115 	SetActive();
       
   116 	OstTraceFunctionExit1( CENDPOINTREADER_READL_EXIT, this );
   102 	}
   117 	}
   103 
   118 
   104 void CEndpointReader::ReadUntilShortL(TInt aByteCount)
   119 void CEndpointReader::ReadUntilShortL(TInt aByteCount)
   105 	{
   120 	{
   106 	LOG_FUNC
   121 	OstTraceFunctionEntryExt( CENDPOINTREADER_READUNTILSHORTL_ENTRY, this );
   107 	RDebug::Printf("Endpoint %d", iEndpoint);
   122 	OstTrace1(TRACE_NORMAL, CENDPOINTREADER_READUNTILSHORTL, "Endpoint %d", iEndpoint);
   108 	
   123 	
   109 	// Allocate buffer for reading a data packet
   124 	// Allocate buffer for reading a data packet
   110 	if(iDataBuffer)
   125 	if(iDataBuffer)
   111 		{
   126 		{
   112 		delete iDataBuffer;
   127 		delete iDataBuffer;
   116 	iDataPtr.Set(iDataBuffer->Des());
   131 	iDataPtr.Set(iDataBuffer->Des());
   117 
   132 
   118 	// Read from the endpoint
   133 	// Read from the endpoint
   119 	iClientDriver.ReadUntilShort(iStatus,iEndpoint,iDataPtr,aByteCount);
   134 	iClientDriver.ReadUntilShort(iStatus,iEndpoint,iDataPtr,aByteCount);
   120 	SetActive();
   135 	SetActive();
       
   136 	OstTraceFunctionExit1( CENDPOINTREADER_READUNTILSHORTL_EXIT, this );
   121 	}
   137 	}
   122 
   138 
   123 void CEndpointReader::ReadAndHaltL(TInt aByteCount)
   139 void CEndpointReader::ReadAndHaltL(TInt aByteCount)
   124 	{
   140 	{
   125 	LOG_FUNC
   141 	OstTraceFunctionEntryExt( CENDPOINTREADER_READANDHALTL_ENTRY, this );
   126 	iCompletionAction = EHaltEndpoint;
   142 	iCompletionAction = EHaltEndpoint;
   127 	ReadL(aByteCount);
   143 	ReadL(aByteCount);
       
   144 	OstTraceFunctionExit1( CENDPOINTREADER_READANDHALTL_EXIT, this );
   128 	}
   145 	}
   129 
   146 
   130 void CEndpointReader::RepeatedReadAndValidateL(const TDesC8& aDataPattern, TUint aNumBytesPerRead, TUint aTotalNumBytes)
   147 void CEndpointReader::RepeatedReadAndValidateL(const TDesC8& aDataPattern, TUint aNumBytesPerRead, TUint aTotalNumBytes)
   131 	{
   148 	{
   132 	LOG_FUNC
   149 OstTraceFunctionEntryExt( CENDPOINTREADER_REPEATEDREADANDVALIDATEL_ENTRY, this );
   133 
   150 
   134 	iCompletionAction = ERepeatedRead;
   151 	iCompletionAction = ERepeatedRead;
   135 	iRepeatedReadTotalNumBytes = aTotalNumBytes;
   152 	iRepeatedReadTotalNumBytes = aTotalNumBytes;
   136 	iRepeatedReadNumBytesPerRead = aNumBytesPerRead;
   153 	iRepeatedReadNumBytesPerRead = aNumBytesPerRead;
   137 	iNumBytesReadSoFar = 0;
   154 	iNumBytesReadSoFar = 0;
   138 	iDataPatternLength = aDataPattern.Length();
   155 	iDataPatternLength = aDataPattern.Length();
   139 	iIsValid = ETrue; //until proven guilty!
   156 	iIsValid = ETrue; //until proven guilty!
   140 	RDebug::Printf("Total Bytes To Read: %d, Bytes Per Individual Read: %d", iRepeatedReadTotalNumBytes, iRepeatedReadNumBytesPerRead);
   157 	OstTraceExt2(TRACE_NORMAL, CENDPOINTREADER_REPEATEDREADANDVALIDATEL, "Total Bytes To Read: %u, Bytes Per Individual Read: %u", iRepeatedReadTotalNumBytes, iRepeatedReadNumBytesPerRead);
   141 	//Create buffer to contain two lots of the payload pattern
   158 	//Create buffer to contain two lots of the payload pattern
   142 	//..so that we may grab cyclic chunks of said payload pattern
   159 	//..so that we may grab cyclic chunks of said payload pattern
   143 	delete iValidationPatternBuffer;
   160 	delete iValidationPatternBuffer;
   144 	iValidationPatternBuffer = NULL;
   161 	iValidationPatternBuffer = NULL;
   145 	iValidationPatternBuffer = HBufC8::New(KNumPatternRepeatsNeededForValidation*aDataPattern.Length()); 
   162 	iValidationPatternBuffer = HBufC8::New(KNumPatternRepeatsNeededForValidation*aDataPattern.Length()); 
   149 		{
   166 		{
   150 		iValidationPatternPtr.Append(aDataPattern);
   167 		iValidationPatternPtr.Append(aDataPattern);
   151 		}
   168 		}
   152 
   169 
   153 	ReadUntilShortL(iRepeatedReadNumBytesPerRead);
   170 	ReadUntilShortL(iRepeatedReadNumBytesPerRead);
       
   171 	OstTraceFunctionExit1( CENDPOINTREADER_REPEATEDREADANDVALIDATEL_EXIT, this );
   154 	}
   172 	}
   155 
   173 
   156 
   174 
   157 TInt CEndpointReader::Acknowledge()
   175 TInt CEndpointReader::Acknowledge()
   158 	{
   176 	{
   159 	LOG_FUNC
   177 	OstTraceFunctionEntry1( CENDPOINTREADER_ACKNOWLEDGE_ENTRY, this );
   160 	TInt err(iClientDriver.SendEp0StatusPacket());
   178 	TInt err(iClientDriver.SendEp0StatusPacket());
   161 	if(err != KErrNone)
   179 	if(err != KErrNone)
   162 		{
   180 		{
   163 		RDebug::Printf("<Error %d> Sending acknowledge packet",err);
   181 		OstTrace1(TRACE_NORMAL, CENDPOINTREADER_ACKNOWLEDGE, "<Error %d> Sending acknowledge packet",err);
   164 		}
   182 		}
       
   183 	OstTraceFunctionExitExt( CENDPOINTREADER_ACKNOWLEDGE_EXIT, this, err );
   165 	return err;
   184 	return err;
   166 	}
   185 	}
   167 			
   186 			
   168 			
   187 			
   169 void CEndpointReader::DoCancel()
   188 void CEndpointReader::DoCancel()
   170 	{
   189 	{
   171 	LOG_FUNC
   190 	OstTraceFunctionEntry1( CENDPOINTREADER_DOCANCEL_ENTRY, this );
   172 	
   191 	
   173 	// Cancel reading from the endpoint
   192 	// Cancel reading from the endpoint
   174 	
   193 	
   175 	iClientDriver.ReadCancel(iEndpoint);
   194 	iClientDriver.ReadCancel(iEndpoint);
       
   195 	OstTraceFunctionExit1( CENDPOINTREADER_DOCANCEL_EXIT, this );
   176 	}
   196 	}
   177 	
   197 	
   178 	
   198 	
   179 void CEndpointReader::RunL()
   199 void CEndpointReader::RunL()
   180 	{
   200 	{
   181 	LOG_FUNC
   201 	OstTraceFunctionEntry1( CENDPOINTREADER_RUNL_ENTRY, this );
   182 	RDebug::Printf("Endpoint %d", iEndpoint);
   202 	OstTrace1(TRACE_NORMAL, CENDPOINTREADER_RUNL, "Endpoint %d", iEndpoint);
   183 	RDebug::Printf("Completion Action %d", iCompletionAction);
   203 	OstTrace1(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP01, "Completion Action %d", iCompletionAction);
   184 	TCompletionAction completionAction = iCompletionAction;
   204 	TCompletionAction completionAction = iCompletionAction;
   185 	iCompletionAction = ENone; //reset here in case of 'early' returns
   205 	iCompletionAction = ENone; //reset here in case of 'early' returns
   186 	
   206 	
   187 	// The operation completion code
   207 	// The operation completion code
   188 	TInt completionCode(iStatus.Int());
   208 	TInt completionCode(iStatus.Int());
   189 	
   209 	
   190 	if(completionCode != KErrNone)
   210 	if(completionCode != KErrNone)
   191 		{
   211 		{
   192 		RDebug::Printf("<Error> void CEndpointReader::RunL()completed with ERROR %d", completionCode);
   212 		OstTrace1(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP02, "<Error> void CEndpointReader::RunL()completed with ERROR %d", completionCode);
   193 		
   213 		
   194 		// Nak the packet received
   214 		// Nak the packet received
   195 		iClientDriver.HaltEndpoint(iEndpoint);
   215 		iClientDriver.HaltEndpoint(iEndpoint);
   196 		
   216 		
   197 		if(iHandler)
   217 		if(iHandler)
   198 			{
   218 			{
   199 			iHandler->EndpointReadError(iEndpoint,completionCode);
   219 			iHandler->EndpointReadError(iEndpoint,completionCode);
   200 			}
   220 			}
   201 		else
   221 		else
   202 			{
   222 			{
   203 			RDebug::Printf("No handler set");
   223 			OstTrace0(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP03, "No handler set");
   204 			}
   224 			}
   205 		}
   225 		}
   206 	else
   226 	else
   207 		{
   227 		{
   208 		// Some data has arrived but 
   228 		// Some data has arrived but 
   209 		TInt ent = iDataBuffer->Length()/16;
   229 		TInt ent = iDataBuffer->Length()/16;
   210 		ent = ent==0?1:ent;
   230 		ent = ent==0?1:ent;
   211 		for(TInt i=0; i<iDataBuffer->Length(); i+=ent)
   231 		for(TInt i=0; i<iDataBuffer->Length(); i+=ent)
   212 			{
   232 			{
   213 			RDebug::Printf("byte %d %02x %c",i,(*iDataBuffer)[i],(*iDataBuffer)[i]);
   233 			OstTraceExt3(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP04, "byte %d %02x %c",i,(*iDataBuffer)[i],(*iDataBuffer)[i]);
   214 			}
   234 			}
   215 
   235 
   216 		if(iHandler)
   236 		if(iHandler)
   217 			{
   237 			{
   218 			iHandler->DataReceivedFromEndpointL(iEndpoint,*iDataBuffer);
   238 			iHandler->DataReceivedFromEndpointL(iEndpoint,*iDataBuffer);
   219 			}
   239 			}
   220 		else
   240 		else
   221 			{
   241 			{
   222 			RDebug::Printf("No handler set");
   242 			OstTrace0(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP05, "No handler set");
   223 			}
   243 			}
   224 		
   244 		
   225 		if(completionAction==EHaltEndpoint)
   245 		if(completionAction==EHaltEndpoint)
   226 			{
   246 			{
   227 			RDebug::Printf("Halting Endpoint");
   247 			OstTrace0(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP06, "Halting Endpoint");
   228 			iClientDriver.HaltEndpoint(iEndpoint);
   248 			iClientDriver.HaltEndpoint(iEndpoint);
   229 			}
   249 			}
   230 
   250 
   231 		if(completionAction==ERepeatedRead)
   251 		if(completionAction==ERepeatedRead)
   232 			{
   252 			{
   233 			RDebug::Printf("Repeated Read");
   253 			OstTrace0(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP07, "Repeated Read");
   234 			iCompletionAction = ERepeatedRead;
   254 			iCompletionAction = ERepeatedRead;
   235 			
   255 			
   236 			//Prepare to validate
   256 			//Prepare to validate
   237 			TInt previousBytesToRead = iRepeatedReadTotalNumBytes - iNumBytesReadSoFar; //PRIOR TO THIS READ
   257 			TInt previousBytesToRead = iRepeatedReadTotalNumBytes - iNumBytesReadSoFar; //PRIOR TO THIS READ
   238 			TUint expectedNumJustReadBytes = previousBytesToRead < iRepeatedReadNumBytesPerRead ? previousBytesToRead : iRepeatedReadNumBytesPerRead;
   258 			TUint expectedNumJustReadBytes = previousBytesToRead < iRepeatedReadNumBytesPerRead ? previousBytesToRead : iRepeatedReadNumBytesPerRead;
   239 			TPtrC8 valDesc = iValidationPatternPtr.Mid(iNumBytesReadSoFar%iDataPatternLength, expectedNumJustReadBytes);
   259 			TPtrC8 valDesc = iValidationPatternPtr.Mid(iNumBytesReadSoFar%iDataPatternLength, expectedNumJustReadBytes);
   240 			TInt err = iDataPtr.Compare(valDesc);
   260 			TInt err = iDataPtr.Compare(valDesc);
   241 
   261 
   242 			iNumBytesReadSoFar += iDataPtr.Length();
   262 			iNumBytesReadSoFar += iDataPtr.Length();
   243 			RDebug::Printf("Bytes read so far %d, Total bytes to read %d", iNumBytesReadSoFar, iRepeatedReadTotalNumBytes);
   263 			OstTraceExt2(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP08, "Bytes read so far %u, Total bytes to read %u", iNumBytesReadSoFar, iRepeatedReadTotalNumBytes);
   244 
   264 
   245 			if(err!=0)
   265 			if(err!=0)
   246 				{
   266 				{
   247 				RDebug::Printf("Validation Result %d, Validation String Length %d, Bytes Actually Read %d", err, valDesc.Length(), iDataPtr.Length());
   267 				OstTraceExt3(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP09, "Validation Result %d, Validation String Length %d, Bytes Actually Read %d", err, valDesc.Length(), iDataPtr.Length());
   248 				RDebug::Printf("Expected string, followed by read string");
   268 				OstTrace0(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP10, "Expected string, followed by read string");
   249 				RDebug::RawPrint(valDesc);
   269                 OstTraceData(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP50, "", valDesc.Ptr(), valDesc.Length());
   250 				RDebug::Printf("\n");
   270 				OstTrace0(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP11, "\n");
   251 				RDebug::RawPrint(iDataPtr);
   271                 OstTraceData(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP51, "", iDataPtr.Ptr(), iDataPtr.Length());
   252 				RDebug::Printf("\n");
   272 				OstTrace0(TRACE_NORMAL, CENDPOINTREADER_RUNL_DUP12, "\n");
   253 				iIsValid = EFalse; //record validation error
   273 				iIsValid = EFalse; //record validation error
   254 				}
   274 				}
   255 			
   275 			
   256 			if(iNumBytesReadSoFar < iRepeatedReadTotalNumBytes)
   276 			if(iNumBytesReadSoFar < iRepeatedReadTotalNumBytes)
   257 				{
   277 				{
   266 				iDataPatternLength = 0;
   286 				iDataPatternLength = 0;
   267 				//do not reset iIsValid - this is required later
   287 				//do not reset iIsValid - this is required later
   268 				}
   288 				}
   269 			}
   289 			}
   270 		}
   290 		}
       
   291 	OstTraceFunctionExit1( CENDPOINTREADER_RUNL_EXIT, this );
   271 	}
   292 	}
   272 
   293 
   273 TInt CEndpointReader::RunError(TInt aError)
   294 TInt CEndpointReader::RunError(TInt aError)
   274 	{
   295 	{
   275 	LOG_FUNC
   296 	OstTraceFunctionEntryExt( CENDPOINTREADER_RUNERROR_ENTRY, this );
   276 	
   297 	
   277 	RDebug::Printf("<Leaving Error> void CEndpointReader::RunError()called with ERROR %d", aError);
   298 	OstTrace1(TRACE_NORMAL, CENDPOINTREADER_RUNERROR, "<Leaving Error> void CEndpointReader::RunError()called with ERROR %d", aError);
   278 
   299 
   279 	// Nak the packet received
   300 	// Nak the packet received
   280 	iClientDriver.HaltEndpoint(iEndpoint);
   301 	iClientDriver.HaltEndpoint(iEndpoint);
   281 
   302 
   282 	aError = KErrNone;	
   303 	aError = KErrNone;	
       
   304 	OstTraceFunctionExitExt( CENDPOINTREADER_RUNERROR_EXIT, this, aError );
   283 	return aError;
   305 	return aError;
   284 	}
   306 	}
   285 	
   307 	
   286 	}
   308 	}