|
1 // Copyright (c) 2004-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 "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 // This file implements a loopback driver for use with the ETel Regression test harness. This |
|
15 // driver supports up to 100 virtual port connections, from port 501 to 600 |
|
16 // directory exists. The log file is named loopback. |
|
17 // |
|
18 // |
|
19 |
|
20 /** |
|
21 @file |
|
22 @note This driver will create a log file in the Logs\ETel directory if the Logs\ETel |
|
23 */ |
|
24 |
|
25 #include "LoopbackQueue.h" |
|
26 #include "LoopbackConfig.h" |
|
27 |
|
28 CTPtr8Queue* CTPtr8Queue::NewL(TInt aBufferLength, TInt aQueueLength) |
|
29 { |
|
30 CTPtr8Queue* me = new(ELeave) CTPtr8Queue(aQueueLength); |
|
31 CleanupStack::PushL(me); |
|
32 me->ConstructL(aBufferLength, aQueueLength); |
|
33 CleanupStack::Pop(me); |
|
34 return me; |
|
35 } |
|
36 |
|
37 CTPtr8Queue::CTPtr8Queue(TInt aQueueLength) : iFirstBuffer(0), iNextBuffer(0), iQueueFull(EFalse), |
|
38 iDescriptors(aQueueLength) |
|
39 {} |
|
40 |
|
41 void CTPtr8Queue::ConstructL(TInt aBufferLength, TInt aQueueLength) |
|
42 { |
|
43 HBufC8* buffer = HBufC8::NewL(aBufferLength*aQueueLength); |
|
44 // don't push onto the cleanup stack, as we are assigning to member variable here |
|
45 iBuffer = buffer; |
|
46 TPtr8 tmp = buffer->Des(); |
|
47 tmp.AppendFill('C', aBufferLength*aQueueLength); |
|
48 for (TInt i = 0; i < aQueueLength; i++) |
|
49 { |
|
50 TUint8& startChar = tmp[i*aBufferLength]; |
|
51 TPtr8 nextElement = TPtr8(&startChar, aBufferLength); |
|
52 iDescriptors.AppendL(nextElement); |
|
53 } |
|
54 } |
|
55 |
|
56 CTPtr8Queue::~CTPtr8Queue() |
|
57 { |
|
58 delete iBuffer; |
|
59 } |
|
60 |
|
61 void CTPtr8Queue::Clear() |
|
62 { |
|
63 iFirstBuffer = iNextBuffer = 0; |
|
64 iQueueFull = EFalse; |
|
65 } |
|
66 |
|
67 TBool CTPtr8Queue::IsEmpty() const |
|
68 { |
|
69 if (iQueueFull) |
|
70 return EFalse; |
|
71 else |
|
72 return iFirstBuffer == iNextBuffer; |
|
73 } |
|
74 |
|
75 TBool CTPtr8Queue::QueueLength() const |
|
76 { |
|
77 if (iQueueFull) |
|
78 return iDescriptors.Count(); |
|
79 else |
|
80 return Abs(iFirstBuffer - iNextBuffer) % iDescriptors.Count(); |
|
81 } |
|
82 |
|
83 TBool CTPtr8Queue::IsFull() const |
|
84 { |
|
85 if (iQueueFull) |
|
86 { |
|
87 ASSERT(iFirstBuffer == iNextBuffer); |
|
88 } |
|
89 return iQueueFull; |
|
90 } |
|
91 |
|
92 TPtr8& CTPtr8Queue::GetNewBuffer() |
|
93 { |
|
94 ASSERT(!IsFull()); |
|
95 TPtr8& ret = iDescriptors[iNextBuffer]; |
|
96 iNextBuffer++; |
|
97 if (iNextBuffer >= iDescriptors.Count()) |
|
98 iNextBuffer = 0; |
|
99 if (iNextBuffer == iFirstBuffer) |
|
100 iQueueFull = ETrue; |
|
101 return ret; |
|
102 } |
|
103 |
|
104 const TPtr8& CTPtr8Queue::PeekFirstBuffer() const |
|
105 { |
|
106 ASSERT(!IsEmpty()); |
|
107 return iDescriptors[iFirstBuffer]; |
|
108 } |
|
109 |
|
110 TPtr8& CTPtr8Queue::DequeueFirstBuffer() |
|
111 { |
|
112 ASSERT(!IsEmpty()); |
|
113 TPtr8& ret = iDescriptors[iFirstBuffer]; |
|
114 iFirstBuffer++; |
|
115 if (iFirstBuffer >= iDescriptors.Count()) |
|
116 iFirstBuffer = 0; |
|
117 iQueueFull = EFalse; |
|
118 return ret; |
|
119 } |
|
120 |
|
121 MLoopbackQueue::~MLoopbackQueue() |
|
122 {} |
|
123 |
|
124 CPacketBufferQueue* CPacketBufferQueue::NewL(TInt aBufferLength, TInt aQueueLength) |
|
125 { |
|
126 CPacketBufferQueue* me = new (ELeave)CPacketBufferQueue(aBufferLength); |
|
127 CleanupStack::PushL(me); |
|
128 me->ConstructL(aBufferLength, aQueueLength); |
|
129 CleanupStack::Pop(me); |
|
130 return me; |
|
131 } |
|
132 |
|
133 CPacketBufferQueue::CPacketBufferQueue(TInt aBufferLength) : iBufferLength(aBufferLength) |
|
134 {} |
|
135 |
|
136 void CPacketBufferQueue::ConstructL(TInt aBufferLength, TInt aQueueLength) |
|
137 { |
|
138 iReadQueue = CTPtr8Queue::NewL(aBufferLength, aQueueLength); |
|
139 iWriteQueue = CTPtr8Queue::NewL(aBufferLength, aQueueLength); |
|
140 } |
|
141 |
|
142 CPacketBufferQueue::~CPacketBufferQueue() |
|
143 { |
|
144 delete iReadQueue; |
|
145 delete iWriteQueue; |
|
146 } |
|
147 |
|
148 TPtrC8 CPacketBufferQueue::PeekNextReadBuffer(TBool& aCompleteIfBufferNotFull) const |
|
149 { |
|
150 aCompleteIfBufferNotFull = ETrue; |
|
151 return iReadQueue->PeekFirstBuffer(); |
|
152 } |
|
153 |
|
154 void CPacketBufferQueue::DiscardNextReadBuffer() |
|
155 { |
|
156 iReadQueue->DequeueFirstBuffer(); |
|
157 } |
|
158 |
|
159 void CPacketBufferQueue::AppendToReadBuffer(TDesC8& aSrcData) |
|
160 { |
|
161 TPtr8& destBuf = iReadQueue->GetNewBuffer(); |
|
162 destBuf.Copy(aSrcData); |
|
163 } |
|
164 |
|
165 TPtrC8 CPacketBufferQueue::PeekNextWriteBuffer() const |
|
166 { |
|
167 return iWriteQueue->PeekFirstBuffer(); |
|
168 } |
|
169 |
|
170 void CPacketBufferQueue::DiscardNextWriteBuffer() |
|
171 { |
|
172 iWriteQueue->DequeueFirstBuffer(); |
|
173 } |
|
174 |
|
175 TPtr8 CPacketBufferQueue::AppendToWriteBuffer(TInt aLength) |
|
176 { |
|
177 TPtr8& newBuf = iWriteQueue->GetNewBuffer(); |
|
178 newBuf.FillZ(aLength); |
|
179 newBuf.LeftTPtr(aLength); |
|
180 return newBuf; |
|
181 } |
|
182 |
|
183 TBool CPacketBufferQueue::IsReadBufferEmpty() const |
|
184 { |
|
185 return iReadQueue->IsEmpty(); |
|
186 } |
|
187 |
|
188 TBool CPacketBufferQueue::IsWriteBufferEmpty() const |
|
189 { |
|
190 return iWriteQueue->IsEmpty(); |
|
191 } |
|
192 |
|
193 TBool CPacketBufferQueue::IsReadBufferFull(TInt /*aLength*/) const |
|
194 { |
|
195 return iReadQueue->IsFull(); |
|
196 } |
|
197 |
|
198 TBool CPacketBufferQueue::IsWriteBufferFull(TInt /*aLength*/) const |
|
199 { |
|
200 return iWriteQueue->IsFull(); |
|
201 } |
|
202 |
|
203 TInt CPacketBufferQueue::BufferSize() const |
|
204 { |
|
205 return iBufferLength; |
|
206 } |
|
207 |
|
208 void CPacketBufferQueue::Clear() |
|
209 { |
|
210 iWriteQueue->Clear(); |
|
211 iReadQueue->Clear(); |
|
212 } |
|
213 |
|
214 CSerialBufferQueue* CSerialBufferQueue::NewL(TInt aBufferLength, TCommConfigV01* aConfig) |
|
215 { |
|
216 CSerialBufferQueue* me = new (ELeave)CSerialBufferQueue(aBufferLength, aConfig); |
|
217 CleanupStack::PushL(me); |
|
218 me->ConstructL(aBufferLength); |
|
219 CleanupStack::Pop(me); |
|
220 return me; |
|
221 } |
|
222 |
|
223 CSerialBufferQueue::CSerialBufferQueue(TInt aBufferLength, TCommConfigV01* aConfig) : |
|
224 iBufferLength(aBufferLength), iConfig(aConfig), iWriteData(NULL, 0), iReadData(NULL, 0) |
|
225 {} |
|
226 |
|
227 void CSerialBufferQueue::ConstructL(TInt aBufferLength) |
|
228 { |
|
229 iReadBuffer = HBufC8::NewL(aBufferLength); |
|
230 iWriteBuffer = HBufC8::NewL(aBufferLength); |
|
231 iReadData.Set(iReadBuffer->Des()); |
|
232 iWriteData.Set(iWriteBuffer->Des()); |
|
233 } |
|
234 |
|
235 CSerialBufferQueue::~CSerialBufferQueue() |
|
236 { |
|
237 delete iReadBuffer; |
|
238 delete iWriteBuffer; |
|
239 } |
|
240 |
|
241 TPtrC8 CSerialBufferQueue::NextBuffer(const TPtrC8& aBuffer, TBool& aTerminatorFound) const |
|
242 { |
|
243 // Search for terminator characters |
|
244 TInt len = 0; |
|
245 aTerminatorFound = EFalse; |
|
246 const TText8* terminators = iConfig->iTerminator; |
|
247 TPtrC8 dataToTerminator(aBuffer); |
|
248 for (TInt termNum = 0; termNum < iConfig->iTerminatorCount; ++termNum) |
|
249 { |
|
250 TInt termIndex = dataToTerminator.Locate(terminators[termNum]); |
|
251 if (termIndex >= 0) |
|
252 { |
|
253 // Found the terminator; reduce the length of ptr and search for the next one |
|
254 len = termIndex + 1; |
|
255 dataToTerminator.Set(dataToTerminator.Ptr(), len); |
|
256 aTerminatorFound = ETrue; |
|
257 } |
|
258 } |
|
259 return dataToTerminator; |
|
260 } |
|
261 |
|
262 TPtrC8 CSerialBufferQueue::PeekNextReadBuffer(TBool& aCompleteIfBufferNotFull) const |
|
263 { |
|
264 // if the terminator is found, we want the requrest completed even if the buffer is not full |
|
265 return NextBuffer(iReadData, aCompleteIfBufferNotFull); |
|
266 } |
|
267 |
|
268 void CSerialBufferQueue::DiscardNextReadBuffer() |
|
269 { |
|
270 TBool terminatorFound; |
|
271 TPtrC8 dataToTerminator = NextBuffer(iReadData, terminatorFound); |
|
272 |
|
273 if (terminatorFound) |
|
274 { |
|
275 TPtrC8 source = iReadData.Right(iReadData.Length()-dataToTerminator.Length()); |
|
276 iReadData.Copy(source); |
|
277 } |
|
278 else |
|
279 { |
|
280 iReadData.SetLength(0); |
|
281 } |
|
282 } |
|
283 |
|
284 void CSerialBufferQueue::AppendToReadBuffer(TDesC8& aSrcData) |
|
285 { |
|
286 ASSERT(aSrcData.Length() < iReadData.MaxLength()-iReadData.Length()); |
|
287 iReadData.Append(aSrcData); |
|
288 } |
|
289 |
|
290 TPtrC8 CSerialBufferQueue::PeekNextWriteBuffer() const |
|
291 { |
|
292 TBool dummy; |
|
293 return NextBuffer(iWriteData, dummy); |
|
294 } |
|
295 |
|
296 void CSerialBufferQueue::DiscardNextWriteBuffer() |
|
297 { |
|
298 TBool terminatorFound; |
|
299 TPtrC8 dataToTerminator = NextBuffer(iWriteData, terminatorFound); |
|
300 |
|
301 // Delete the write data from the buffer |
|
302 if (terminatorFound) |
|
303 { |
|
304 TPtrC8 source = iWriteData.Right(iWriteData.Length()-dataToTerminator.Length()); |
|
305 iWriteData.Copy(source); |
|
306 } |
|
307 else |
|
308 { |
|
309 iWriteData.SetLength(0); |
|
310 } |
|
311 } |
|
312 |
|
313 TPtr8 CSerialBufferQueue::AppendToWriteBuffer(TInt aLength) |
|
314 { |
|
315 TInt length = iWriteData.Length(); |
|
316 ASSERT(aLength < iWriteData.MaxLength()-length); |
|
317 iWriteData.AppendFill('C', aLength); |
|
318 TUint8 *start = &iWriteData[length]; |
|
319 return TPtr8(start, aLength, aLength); |
|
320 } |
|
321 |
|
322 TBool CSerialBufferQueue::IsReadBufferEmpty() const |
|
323 { |
|
324 return iReadData.Length() == 0; |
|
325 } |
|
326 |
|
327 TBool CSerialBufferQueue::IsWriteBufferEmpty() const |
|
328 { |
|
329 return iWriteData.Length() == 0; |
|
330 } |
|
331 |
|
332 TBool CSerialBufferQueue::IsReadBufferFull(TInt aLength) const |
|
333 { |
|
334 return ( aLength > (iReadData.MaxLength()-iReadData.Length()) ); |
|
335 } |
|
336 |
|
337 TBool CSerialBufferQueue::IsWriteBufferFull(TInt aLength) const |
|
338 { |
|
339 return ( aLength > (iWriteData.MaxLength()-iWriteData.Length()) ); |
|
340 } |
|
341 |
|
342 TInt CSerialBufferQueue::BufferSize() const |
|
343 { |
|
344 return iWriteData.MaxLength()-iWriteData.Length(); |
|
345 } |
|
346 |
|
347 void CSerialBufferQueue::Clear() |
|
348 { |
|
349 } |
|
350 |