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