|
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". |
|
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" |
|
22 |
|
23 namespace NUnitTesting_USBDI |
|
24 { |
|
25 const TUint8 KNumPatternRepeatsNeededForValidation = 4; |
|
26 |
|
27 CEndpointReader::CEndpointReader(RDevUsbcClient& aClientDriver,TEndpointNumber aEndpoint) |
|
28 : CActive(EPriorityStandard), |
|
29 iClientDriver(aClientDriver), |
|
30 iEndpoint(aEndpoint), |
|
31 iDataPtr(NULL,0), |
|
32 iValidationPatternPtr(NULL,0) |
|
33 { |
|
34 CActiveScheduler::Add(this); |
|
35 } |
|
36 |
|
37 CEndpointReader::~CEndpointReader() |
|
38 { |
|
39 LOG_FUNC |
|
40 Cancel(); |
|
41 delete iDataBuffer; |
|
42 iDataBuffer = NULL; |
|
43 delete iValidationPatternBuffer; |
|
44 iValidationPatternBuffer = NULL; |
|
45 } |
|
46 |
|
47 TPtr8 CEndpointReader::Buffer() |
|
48 { |
|
49 return iDataPtr; |
|
50 } |
|
51 |
|
52 TBool CEndpointReader::IsValid() |
|
53 { |
|
54 return iIsValid; |
|
55 } |
|
56 |
|
57 TUint CEndpointReader::NumBytesReadSoFar() |
|
58 { |
|
59 return iNumBytesReadSoFar; |
|
60 } |
|
61 |
|
62 void CEndpointReader::ReadPacketL(MEndpointDataHandler* aHandler) |
|
63 { |
|
64 LOG_FUNC |
|
65 RDebug::Printf("Endpoint %d", iEndpoint); |
|
66 |
|
67 iHandler = aHandler; |
|
68 |
|
69 // Allocate buffer for reading a data packet |
|
70 if(iDataBuffer) |
|
71 { |
|
72 delete iDataBuffer; |
|
73 iDataBuffer = NULL; |
|
74 } |
|
75 iDataBuffer = HBufC8::NewL(KFullSpeedPacketSize); |
|
76 iDataPtr.Set(iDataBuffer->Des()); |
|
77 |
|
78 // Read from the endpoint |
|
79 // Read from the endpoint |
|
80 iClientDriver.ReadPacket(iStatus,iEndpoint,iDataPtr,KFullSpeedPacketSize); |
|
81 SetActive(); |
|
82 } |
|
83 |
|
84 |
|
85 void CEndpointReader::ReadL(TInt aByteCount) |
|
86 { |
|
87 LOG_FUNC |
|
88 RDebug::Printf("Endpoint %d", iEndpoint); |
|
89 |
|
90 // Allocate buffer for reading a data packet |
|
91 if(iDataBuffer) |
|
92 { |
|
93 delete iDataBuffer; |
|
94 iDataBuffer = NULL; |
|
95 } |
|
96 iDataBuffer = HBufC8::NewL(aByteCount); |
|
97 iDataPtr.Set(iDataBuffer->Des()); |
|
98 |
|
99 // Read from the endpoint |
|
100 iClientDriver.Read(iStatus,iEndpoint,iDataPtr,aByteCount); |
|
101 SetActive(); |
|
102 } |
|
103 |
|
104 void CEndpointReader::ReadUntilShortL(TInt aByteCount) |
|
105 { |
|
106 LOG_FUNC |
|
107 RDebug::Printf("Endpoint %d", iEndpoint); |
|
108 |
|
109 // Allocate buffer for reading a data packet |
|
110 if(iDataBuffer) |
|
111 { |
|
112 delete iDataBuffer; |
|
113 iDataBuffer = NULL; |
|
114 } |
|
115 iDataBuffer = HBufC8::NewL(aByteCount); |
|
116 iDataPtr.Set(iDataBuffer->Des()); |
|
117 |
|
118 // Read from the endpoint |
|
119 iClientDriver.ReadUntilShort(iStatus,iEndpoint,iDataPtr,aByteCount); |
|
120 SetActive(); |
|
121 } |
|
122 |
|
123 void CEndpointReader::ReadAndHaltL(TInt aByteCount) |
|
124 { |
|
125 LOG_FUNC |
|
126 iCompletionAction = EHaltEndpoint; |
|
127 ReadL(aByteCount); |
|
128 } |
|
129 |
|
130 void CEndpointReader::RepeatedReadAndValidateL(const TDesC8& aDataPattern, TUint aNumBytesPerRead, TUint aTotalNumBytes) |
|
131 { |
|
132 LOG_FUNC |
|
133 |
|
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++) |
|
149 { |
|
150 iValidationPatternPtr.Append(aDataPattern); |
|
151 } |
|
152 |
|
153 ReadUntilShortL(iRepeatedReadNumBytesPerRead); |
|
154 } |
|
155 |
|
156 |
|
157 TInt CEndpointReader::Acknowledge() |
|
158 { |
|
159 LOG_FUNC |
|
160 TInt err(iClientDriver.SendEp0StatusPacket()); |
|
161 if(err != KErrNone) |
|
162 { |
|
163 RDebug::Printf("<Error %d> Sending acknowledge packet",err); |
|
164 } |
|
165 return err; |
|
166 } |
|
167 |
|
168 |
|
169 void CEndpointReader::DoCancel() |
|
170 { |
|
171 LOG_FUNC |
|
172 |
|
173 // Cancel reading from the endpoint |
|
174 |
|
175 iClientDriver.ReadCancel(iEndpoint); |
|
176 } |
|
177 |
|
178 |
|
179 void CEndpointReader::RunL() |
|
180 { |
|
181 LOG_FUNC |
|
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 |
|
186 |
|
187 // The operation completion code |
|
188 TInt completionCode(iStatus.Int()); |
|
189 |
|
190 if(completionCode != KErrNone) |
|
191 { |
|
192 RDebug::Printf("<Error> void CEndpointReader::RunL()completed with ERROR %d", completionCode); |
|
193 |
|
194 // Nak the packet received |
|
195 iClientDriver.HaltEndpoint(iEndpoint); |
|
196 |
|
197 if(iHandler) |
|
198 { |
|
199 iHandler->EndpointReadError(iEndpoint,completionCode); |
|
200 } |
|
201 else |
|
202 { |
|
203 RDebug::Printf("No handler set"); |
|
204 } |
|
205 } |
|
206 else |
|
207 { |
|
208 // Some data has arrived but |
|
209 TInt ent = iDataBuffer->Length()/16; |
|
210 ent = ent==0?1:ent; |
|
211 for(TInt i=0; i<iDataBuffer->Length(); i+=ent) |
|
212 { |
|
213 RDebug::Printf("byte %d %02x %c",i,(*iDataBuffer)[i],(*iDataBuffer)[i]); |
|
214 } |
|
215 |
|
216 if(iHandler) |
|
217 { |
|
218 iHandler->DataReceivedFromEndpointL(iEndpoint,*iDataBuffer); |
|
219 } |
|
220 else |
|
221 { |
|
222 RDebug::Printf("No handler set"); |
|
223 } |
|
224 |
|
225 if(completionAction==EHaltEndpoint) |
|
226 { |
|
227 RDebug::Printf("Halting Endpoint"); |
|
228 iClientDriver.HaltEndpoint(iEndpoint); |
|
229 } |
|
230 |
|
231 if(completionAction==ERepeatedRead) |
|
232 { |
|
233 RDebug::Printf("Repeated Read"); |
|
234 iCompletionAction = ERepeatedRead; |
|
235 |
|
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); |
|
241 |
|
242 iNumBytesReadSoFar += iDataPtr.Length(); |
|
243 RDebug::Printf("Bytes read so far %d, Total bytes to read %d", iNumBytesReadSoFar, iRepeatedReadTotalNumBytes); |
|
244 |
|
245 if(err!=0) |
|
246 { |
|
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 |
|
254 } |
|
255 |
|
256 if(iNumBytesReadSoFar < iRepeatedReadTotalNumBytes) |
|
257 { |
|
258 ReadUntilShortL(iRepeatedReadNumBytesPerRead); |
|
259 } |
|
260 else |
|
261 { |
|
262 //reset |
|
263 iRepeatedReadTotalNumBytes = 0; |
|
264 iRepeatedReadNumBytesPerRead = 0; |
|
265 iNumBytesReadSoFar = 0; |
|
266 iDataPatternLength = 0; |
|
267 //do not reset iIsValid - this is required later |
|
268 } |
|
269 } |
|
270 } |
|
271 } |
|
272 |
|
273 TInt CEndpointReader::RunError(TInt aError) |
|
274 { |
|
275 LOG_FUNC |
|
276 |
|
277 RDebug::Printf("<Leaving Error> void CEndpointReader::RunError()called with ERROR %d", aError); |
|
278 |
|
279 // Nak the packet received |
|
280 iClientDriver.HaltEndpoint(iEndpoint); |
|
281 |
|
282 aError = KErrNone; |
|
283 return aError; |
|
284 } |
|
285 |
|
286 } |