Update contrib.
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // @file endpointreader.cpp
19 #include "endpointreader.h"
20 #include "controltransferrequests.h"
21 #include "testdebug.h"
23 namespace NUnitTesting_USBDI
25 const TUint8 KNumPatternRepeatsNeededForValidation = 4;
27 CEndpointReader::CEndpointReader(RDevUsbcClient& aClientDriver,TEndpointNumber aEndpoint)
28 : CActive(EPriorityStandard),
29 iClientDriver(aClientDriver),
32 iValidationPatternPtr(NULL,0)
34 CActiveScheduler::Add(this);
37 CEndpointReader::~CEndpointReader()
43 delete iValidationPatternBuffer;
44 iValidationPatternBuffer = NULL;
47 TPtr8 CEndpointReader::Buffer()
52 TBool CEndpointReader::IsValid()
57 TUint CEndpointReader::NumBytesReadSoFar()
59 return iNumBytesReadSoFar;
62 void CEndpointReader::ReadPacketL(MEndpointDataHandler* aHandler)
65 RDebug::Printf("Endpoint %d", iEndpoint);
69 // Allocate buffer for reading a data packet
75 iDataBuffer = HBufC8::NewL(KFullSpeedPacketSize);
76 iDataPtr.Set(iDataBuffer->Des());
78 // Read from the endpoint
79 // Read from the endpoint
80 iClientDriver.ReadPacket(iStatus,iEndpoint,iDataPtr,KFullSpeedPacketSize);
85 void CEndpointReader::ReadL(TInt aByteCount)
88 RDebug::Printf("Endpoint %d", iEndpoint);
90 // Allocate buffer for reading a data packet
96 iDataBuffer = HBufC8::NewL(aByteCount);
97 iDataPtr.Set(iDataBuffer->Des());
99 // Read from the endpoint
100 iClientDriver.Read(iStatus,iEndpoint,iDataPtr,aByteCount);
104 void CEndpointReader::ReadUntilShortL(TInt aByteCount)
107 RDebug::Printf("Endpoint %d", iEndpoint);
109 // Allocate buffer for reading a data packet
115 iDataBuffer = HBufC8::NewL(aByteCount);
116 iDataPtr.Set(iDataBuffer->Des());
118 // Read from the endpoint
119 iClientDriver.ReadUntilShort(iStatus,iEndpoint,iDataPtr,aByteCount);
123 void CEndpointReader::ReadAndHaltL(TInt aByteCount)
126 iCompletionAction = EHaltEndpoint;
130 void CEndpointReader::RepeatedReadAndValidateL(const TDesC8& aDataPattern, TUint aNumBytesPerRead, TUint aTotalNumBytes)
134 iCompletionAction = ERepeatedRead;
135 iRepeatedReadTotalNumBytes = aTotalNumBytes;
136 iRepeatedReadNumBytesPerRead = aNumBytesPerRead;
137 iNumBytesReadSoFar = 0;
138 iDataPatternLength = aDataPattern.Length();
139 iIsValid = ETrue; //until proven guilty!
140 RDebug::Printf("Total Bytes To Read: %d, Bytes Per Individual Read: %d", iRepeatedReadTotalNumBytes, iRepeatedReadNumBytesPerRead);
141 //Create buffer to contain two lots of the payload pattern
142 //..so that we may grab cyclic chunks of said payload pattern
143 delete iValidationPatternBuffer;
144 iValidationPatternBuffer = NULL;
145 iValidationPatternBuffer = HBufC8::New(KNumPatternRepeatsNeededForValidation*aDataPattern.Length());
146 iValidationPatternPtr.Set(iValidationPatternBuffer->Des());
147 iValidationPatternPtr.Zero();
148 for(TUint i=0;i<KNumPatternRepeatsNeededForValidation;i++)
150 iValidationPatternPtr.Append(aDataPattern);
153 ReadUntilShortL(iRepeatedReadNumBytesPerRead);
157 TInt CEndpointReader::Acknowledge()
160 TInt err(iClientDriver.SendEp0StatusPacket());
163 RDebug::Printf("<Error %d> Sending acknowledge packet",err);
169 void CEndpointReader::DoCancel()
173 // Cancel reading from the endpoint
175 iClientDriver.ReadCancel(iEndpoint);
179 void CEndpointReader::RunL()
182 RDebug::Printf("Endpoint %d", iEndpoint);
183 RDebug::Printf("Completion Action %d", iCompletionAction);
184 TCompletionAction completionAction = iCompletionAction;
185 iCompletionAction = ENone; //reset here in case of 'early' returns
187 // The operation completion code
188 TInt completionCode(iStatus.Int());
190 if(completionCode != KErrNone)
192 RDebug::Printf("<Error> void CEndpointReader::RunL()completed with ERROR %d", completionCode);
194 // Nak the packet received
195 iClientDriver.HaltEndpoint(iEndpoint);
199 iHandler->EndpointReadError(iEndpoint,completionCode);
203 RDebug::Printf("No handler set");
208 // Some data has arrived but
209 TInt ent = iDataBuffer->Length()/16;
211 for(TInt i=0; i<iDataBuffer->Length(); i+=ent)
213 RDebug::Printf("byte %d %02x %c",i,(*iDataBuffer)[i],(*iDataBuffer)[i]);
218 iHandler->DataReceivedFromEndpointL(iEndpoint,*iDataBuffer);
222 RDebug::Printf("No handler set");
225 if(completionAction==EHaltEndpoint)
227 RDebug::Printf("Halting Endpoint");
228 iClientDriver.HaltEndpoint(iEndpoint);
231 if(completionAction==ERepeatedRead)
233 RDebug::Printf("Repeated Read");
234 iCompletionAction = ERepeatedRead;
236 //Prepare to validate
237 TInt previousBytesToRead = iRepeatedReadTotalNumBytes - iNumBytesReadSoFar; //PRIOR TO THIS READ
238 TUint expectedNumJustReadBytes = previousBytesToRead < iRepeatedReadNumBytesPerRead ? previousBytesToRead : iRepeatedReadNumBytesPerRead;
239 TPtrC8 valDesc = iValidationPatternPtr.Mid(iNumBytesReadSoFar%iDataPatternLength, expectedNumJustReadBytes);
240 TInt err = iDataPtr.Compare(valDesc);
242 iNumBytesReadSoFar += iDataPtr.Length();
243 RDebug::Printf("Bytes read so far %d, Total bytes to read %d", iNumBytesReadSoFar, iRepeatedReadTotalNumBytes);
247 RDebug::Printf("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");
249 RDebug::RawPrint(valDesc);
250 RDebug::Printf("\n");
251 RDebug::RawPrint(iDataPtr);
252 RDebug::Printf("\n");
253 iIsValid = EFalse; //record validation error
256 if(iNumBytesReadSoFar < iRepeatedReadTotalNumBytes)
258 ReadUntilShortL(iRepeatedReadNumBytesPerRead);
263 iRepeatedReadTotalNumBytes = 0;
264 iRepeatedReadNumBytesPerRead = 0;
265 iNumBytesReadSoFar = 0;
266 iDataPatternLength = 0;
267 //do not reset iIsValid - this is required later
273 TInt CEndpointReader::RunError(TInt aError)
277 RDebug::Printf("<Leaving Error> void CEndpointReader::RunError()called with ERROR %d", aError);
279 // Nak the packet received
280 iClientDriver.HaltEndpoint(iEndpoint);