|
1 /* |
|
2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "TrkFramingLayer.h" |
|
20 |
|
21 #ifdef __USE_NEW_DEBUG_API__ |
|
22 #include "trkdispatchlayer2.h" |
|
23 #else |
|
24 #include "TrkDispatchLayer.h" |
|
25 #endif |
|
26 |
|
27 #include "serframe.h" |
|
28 |
|
29 _LIT8(kPPP_FLAG, "~"); |
|
30 _LIT8(kPPP_ESCAPE, "}"); |
|
31 |
|
32 |
|
33 // |
|
34 // |
|
35 // CTrkFramingLayer implementation |
|
36 // |
|
37 // |
|
38 |
|
39 // |
|
40 // CTrkFramingLayer constructor |
|
41 // |
|
42 CTrkFramingLayer::CTrkFramingLayer(CTrkCommPort* aPort) |
|
43 : iPort(aPort), |
|
44 iFramingState(CTrkFramingLayer::eWaiting), |
|
45 iEscape(0), |
|
46 iFCS(0), |
|
47 iSequenceId(0), |
|
48 iOutSequenceId(1) |
|
49 { |
|
50 // check the target endianness |
|
51 TUint32 test; |
|
52 TUint8 *byte_alias = (TUint8 *)&test; |
|
53 |
|
54 // Write a specific 4-byte sequence and then read it as |
|
55 // a 32-bit word. Big-endian systems yield one value and |
|
56 // little-endian systems yield another. |
|
57 |
|
58 byte_alias[ 0 ] = 0x12; |
|
59 byte_alias[ 1 ] = 0x34; |
|
60 byte_alias[ 2 ] = 0x56; |
|
61 byte_alias[ 3 ] = 0x78; |
|
62 |
|
63 if (test == 0x12345678) |
|
64 iIsBigEndian = ETrue; |
|
65 else |
|
66 iIsBigEndian = EFalse; |
|
67 } |
|
68 |
|
69 // |
|
70 // CTrkFramingLayer destructor |
|
71 // |
|
72 CTrkFramingLayer::~CTrkFramingLayer() |
|
73 { |
|
74 if (iPort) |
|
75 { |
|
76 iPort->ClosePort(); |
|
77 SafeDelete(iPort); |
|
78 } |
|
79 |
|
80 SafeDelete(iLastReply); |
|
81 } |
|
82 |
|
83 // |
|
84 // CTrkFramingLayer::Listen |
|
85 // |
|
86 // Start listening for incoming messages from the host debugger |
|
87 // |
|
88 void CTrkFramingLayer::Listen(CTrkDispatchLayer *aDispatchLayer) |
|
89 { |
|
90 // Start listening asynchronously for incoming messages |
|
91 iDispatchLayer = aDispatchLayer; |
|
92 |
|
93 iPort->Listen(this); |
|
94 } |
|
95 |
|
96 // |
|
97 // CTrkFramingLayer::StopListening |
|
98 // |
|
99 // Stop listening for incoming messages from the host debugger |
|
100 // |
|
101 void CTrkFramingLayer::StopListening() |
|
102 { |
|
103 iDispatchLayer = 0; |
|
104 iPort->StopListening(); |
|
105 } |
|
106 |
|
107 // |
|
108 // CTrkFramingLayer::HandleByte |
|
109 // |
|
110 // Handle a single incoming byte with a state machine |
|
111 // |
|
112 void CTrkFramingLayer::HandleByte(TUint8 aByte) |
|
113 { |
|
114 // discard existing escape if needed |
|
115 if (iFramingState != eInFrame) |
|
116 iEscape = EFalse; |
|
117 |
|
118 // handle state machine |
|
119 switch (iFramingState) |
|
120 { |
|
121 case eWaiting: |
|
122 { |
|
123 // Start of a new packet |
|
124 if (PPP_FLAG == aByte) |
|
125 { |
|
126 iFCS = PPPINITFCS; |
|
127 iFramingState = eFound; // next state |
|
128 } |
|
129 break; |
|
130 } |
|
131 |
|
132 case eFound: |
|
133 { |
|
134 if (PPP_FLAG == aByte) // discard multiple flags |
|
135 break; |
|
136 |
|
137 iFramingState = eInFrame; |
|
138 |
|
139 // fall through to eInFrame, receive first char |
|
140 } |
|
141 |
|
142 case eInFrame: |
|
143 { |
|
144 if (PPP_FLAG == aByte) |
|
145 { |
|
146 if (iEscape) |
|
147 { |
|
148 // [..., escape, flag] is bad frame; drop it |
|
149 // send a NAK with the error. |
|
150 RespondErr(kDSReplyNAK, kDSReplyEscapeError); |
|
151 |
|
152 // clear the buffer |
|
153 DiscardFrame(); |
|
154 } |
|
155 else |
|
156 { |
|
157 // normal case, good packet |
|
158 if (ProcessFrame()) |
|
159 { |
|
160 iDispatchLayer->HandleMsg(iBuffer); |
|
161 } |
|
162 |
|
163 // now start all over again |
|
164 DiscardFrame(); |
|
165 } |
|
166 break; |
|
167 } |
|
168 else if (iEscape) |
|
169 { |
|
170 aByte ^= PPP_TRANS; // change character to new code |
|
171 iEscape = EFalse; // no longer escaped character |
|
172 } |
|
173 else if (PPP_ESCAPE == aByte) |
|
174 { |
|
175 iEscape = ETrue; // next character is escaped |
|
176 break; |
|
177 } |
|
178 |
|
179 // make sure our buffer is not full |
|
180 if (iBuffer.Length() == iBuffer.MaxLength()) |
|
181 { |
|
182 // overflow. send out a NAK immediately. go into an overflow state |
|
183 // which will wait for the end of the packet. |
|
184 RespondErr(kDSReplyNAK, kDSReplyOverflow); |
|
185 |
|
186 iFramingState = eFrameOverflow; |
|
187 break; |
|
188 } |
|
189 |
|
190 // append byte to buffer and compute running FCS (checksum) |
|
191 iBuffer.Append(aByte); |
|
192 |
|
193 iFCS = PPPFCS(iFCS, aByte); |
|
194 break; |
|
195 } |
|
196 |
|
197 case eFrameOverflow: |
|
198 { |
|
199 // overflow occurred. throw away all characters and wait for a flag |
|
200 if (PPP_FLAG == aByte) |
|
201 { |
|
202 DiscardFrame(); |
|
203 } |
|
204 break; |
|
205 } |
|
206 |
|
207 default: |
|
208 { |
|
209 User::Panic(_L("Invalid Framing State"), __LINE__); |
|
210 break; |
|
211 } |
|
212 } |
|
213 } |
|
214 |
|
215 // |
|
216 // CTrkFramingLayer::RespondOkL |
|
217 // |
|
218 // Send an ACK to the host debugger |
|
219 // |
|
220 void CTrkFramingLayer::RespondOkL(const TDesC8& aMsg) |
|
221 { |
|
222 // compose the standard ACK message |
|
223 TBuf8<3> reply; |
|
224 reply.Zero(); |
|
225 |
|
226 reply.Append(kDSReplyACK); |
|
227 reply.Append(iSequenceId); |
|
228 reply.Append(kDSReplyNoError); |
|
229 |
|
230 // now append the rest of the response if necessary |
|
231 HBufC8 *ack = HBufC8::NewLC(aMsg.Length() + 3); |
|
232 ack->Des().Copy(aMsg.Ptr(), aMsg.Length()); |
|
233 |
|
234 TPtr8 ptr(ack->Des()); |
|
235 ptr.Insert(0, reply); |
|
236 |
|
237 // send it out |
|
238 TUint ackTries = 3; |
|
239 TInt leaveCode = KErrGeneral; |
|
240 |
|
241 do |
|
242 { |
|
243 TRAP(leaveCode, SendMsgL(*ack)); |
|
244 ackTries--; |
|
245 } while ((leaveCode != KErrNone) && ackTries > 0); |
|
246 |
|
247 // now increment the sequence id |
|
248 IncrementSequenceId(); |
|
249 |
|
250 // save this in case we need to resend it |
|
251 SafeDelete(iLastReply); |
|
252 iLastReply = ack->Des().AllocL(); |
|
253 |
|
254 CleanupStack::PopAndDestroy(ack); |
|
255 } |
|
256 |
|
257 // |
|
258 // CTrkFramingLayer::RespondErr |
|
259 // |
|
260 // Send an NAK to the host debugger |
|
261 // |
|
262 void CTrkFramingLayer::RespondErr(TUint8 aType, TUint8 aErr) |
|
263 { |
|
264 // compose the standard NAK message |
|
265 TBuf8<3> ack; |
|
266 TUint ackTries = 3; |
|
267 TInt leaveCode = KErrGeneral; |
|
268 |
|
269 ack.Zero(); |
|
270 ack.Append(aType); |
|
271 ack.Append(iSequenceId); |
|
272 ack.Append(aErr); |
|
273 |
|
274 // send it out |
|
275 do |
|
276 { |
|
277 TRAP(leaveCode, SendMsgL(ack)); |
|
278 ackTries--; |
|
279 } while ((leaveCode != KErrNone) && ackTries > 0); |
|
280 |
|
281 if (kDSReplySequenceMissing != aErr) |
|
282 { |
|
283 // now increment the sequence id |
|
284 IncrementSequenceId(); |
|
285 } |
|
286 |
|
287 // save this in case we need to resend it |
|
288 SafeDelete(iLastReply); |
|
289 iLastReply = ack.Alloc(); |
|
290 |
|
291 if (!iLastReply) |
|
292 User::Panic(_L("Failed to allocate reply buffer"), __LINE__); |
|
293 } |
|
294 |
|
295 // |
|
296 // CTrkFramingLayer::InformEventL |
|
297 // |
|
298 // Queue up an event on the target |
|
299 // |
|
300 void CTrkFramingLayer::InformEventL(const TDesC8& aMsg) |
|
301 { |
|
302 // compose the message |
|
303 TBuf8<1> id; |
|
304 id.Zero(); |
|
305 id.Append(iOutSequenceId); |
|
306 IncrementOutSequenceId(); |
|
307 |
|
308 HBufC8 *msg = HBufC8::NewLC(aMsg.Length() + 1); |
|
309 msg->Des().Copy(aMsg.Ptr(), aMsg.Length()); |
|
310 |
|
311 TPtr8 ptr(msg->Des()); |
|
312 ptr.Insert(1, id); |
|
313 |
|
314 // now send it out |
|
315 TUint ackTries = 3; |
|
316 TInt leaveCode = KErrGeneral; |
|
317 |
|
318 do |
|
319 { |
|
320 TRAP(leaveCode, SendMsgL(*msg)); |
|
321 ackTries--; |
|
322 } while ((leaveCode != KErrNone) && ackTries > 0); |
|
323 |
|
324 CleanupStack::PopAndDestroy(msg); |
|
325 |
|
326 if (leaveCode != KErrNone) |
|
327 User::Leave(leaveCode); |
|
328 } |
|
329 |
|
330 // |
|
331 // CTrkFramingLayer::SendRawMsgL |
|
332 // |
|
333 // Frames a message and sends it back to the host debugger |
|
334 // |
|
335 void CTrkFramingLayer::SendRawMsgL(const TDesC8& aMsg) |
|
336 { |
|
337 iPort->SendDataL(aMsg); |
|
338 } |
|
339 |
|
340 |
|
341 // |
|
342 // CTrkFramingLayer::SendMsgL |
|
343 // |
|
344 // Frames a message and sends it back to the host debugger |
|
345 // |
|
346 void CTrkFramingLayer::SendMsgL(const TDesC8& aMsg) |
|
347 { |
|
348 FCSType fcs; |
|
349 FCSType acc; |
|
350 |
|
351 // allocate room for this message with ample space for escape sequences and framing |
|
352 HBufC8 *msg = HBufC8::NewLC(aMsg.Length() + 100); |
|
353 |
|
354 TPtr8 ptr(msg->Des()); |
|
355 |
|
356 // walk through buffer, computing frame check sequence |
|
357 // (checksum). |
|
358 |
|
359 fcs = PPPINITFCS; |
|
360 |
|
361 for (TInt i=0; i<aMsg.Length(); i++) |
|
362 fcs = PPPFCS(fcs, aMsg[i]); |
|
363 |
|
364 fcs = fcs ^ PPPCOMPFCS; // finish by complementing |
|
365 |
|
366 // send bytes out the comm channel: |
|
367 // [0x7E (frame flag)] [...data...] [FCS:8 or FCS:16 or FCS:32] [0x7E (frame flag)] |
|
368 |
|
369 // FLAG |
|
370 ptr.Append(kPPP_FLAG); |
|
371 |
|
372 // DATA |
|
373 TBuf8<1> byte; |
|
374 |
|
375 for (TInt i=0; i<aMsg.Length(); i++) |
|
376 { |
|
377 byte.Zero(); |
|
378 byte.Append(aMsg[i]); |
|
379 |
|
380 // escape if necessary |
|
381 if (byte[0] == PPP_FLAG || byte[0] == PPP_ESCAPE) |
|
382 { |
|
383 ptr.Append(kPPP_ESCAPE); |
|
384 byte[0] ^= PPP_TRANS; |
|
385 } |
|
386 |
|
387 ptr.Append(byte); |
|
388 } |
|
389 |
|
390 // FCS always goes out in little endian (LSB, ... , MSB) order |
|
391 acc = fcs; // accumulator |
|
392 |
|
393 TInt fcsSize = sizeof(FCSType); |
|
394 |
|
395 for (TInt i=0; i<fcsSize; i++) |
|
396 { |
|
397 byte.Zero(); |
|
398 byte.Append(acc & 0xFF); |
|
399 |
|
400 acc >>= 8; |
|
401 |
|
402 // escape if necessary |
|
403 if (byte[0] == PPP_FLAG || byte[0] == PPP_ESCAPE) |
|
404 { |
|
405 ptr.Append(kPPP_ESCAPE); |
|
406 byte[0] ^= PPP_TRANS; |
|
407 } |
|
408 |
|
409 ptr.Append(byte); |
|
410 } |
|
411 |
|
412 // FLAG |
|
413 ptr.Append(kPPP_FLAG); |
|
414 |
|
415 iPort->SendDataL(*msg); |
|
416 |
|
417 CleanupStack::PopAndDestroy(msg); |
|
418 } |
|
419 |
|
420 // |
|
421 // CTrkFramingLayer::DiscardFrame |
|
422 // |
|
423 // Trash the existing message and reset the state machine |
|
424 // |
|
425 void CTrkFramingLayer::DiscardFrame() |
|
426 { |
|
427 iBuffer.Zero(); |
|
428 iFramingState = eWaiting; |
|
429 } |
|
430 |
|
431 // |
|
432 // CTrkFramingLayer::ProcessFrame |
|
433 // |
|
434 // Checks a complete message for correct format |
|
435 // |
|
436 TBool CTrkFramingLayer::ProcessFrame() |
|
437 { |
|
438 // the buffer contains data in the following form: |
|
439 // [data] [FCS8] |
|
440 // or: |
|
441 // [data] [FCS16] |
|
442 // or: |
|
443 // [data] [FCS32] |
|
444 // |
|
445 // where data is arbitrary length and FCSxx is 1, 2 or 4 bytes |
|
446 |
|
447 TInt minSize = 2 + sizeof(FCSType); |
|
448 |
|
449 if (iBuffer.Length() < minSize) |
|
450 { |
|
451 // data received is too short |
|
452 RespondErr(kDSReplyNAK, kDSReplyPacketSizeError); |
|
453 DiscardFrame(); |
|
454 return EFalse; |
|
455 } |
|
456 |
|
457 // check for FCS |
|
458 if (PPPGOODFCS != iFCS) |
|
459 { |
|
460 // bad checksum (FCS) |
|
461 RespondErr(kDSReplyNAK, kDSReplyBadFCS); |
|
462 DiscardFrame(); |
|
463 return EFalse; |
|
464 } |
|
465 |
|
466 // make sure the sequence id is correct, then increment |
|
467 if (iBuffer[TRK_MSG_SID_INDEX] == 0) |
|
468 { |
|
469 // special case - resets both sequence ids |
|
470 iSequenceId = 0; |
|
471 iOutSequenceId = 1; |
|
472 } |
|
473 else |
|
474 { |
|
475 // see which id we're dealing with |
|
476 if ((iBuffer[0] != kDSReplyACK) && (iBuffer[0] != kDSReplyNAK)) |
|
477 { |
|
478 // a command from the host |
|
479 if (iBuffer[TRK_MSG_SID_INDEX] == iSequenceId) |
|
480 { |
|
481 // got the expected sequence id - do nothing |
|
482 } |
|
483 else if (iBuffer[TRK_MSG_SID_INDEX] == (iSequenceId - 1)) |
|
484 { |
|
485 // we need to resend the last reply |
|
486 TUint ackTries = 3; |
|
487 TInt leaveCode = KErrGeneral; |
|
488 |
|
489 do |
|
490 { |
|
491 TRAP(leaveCode, SendMsgL(*iLastReply)); |
|
492 ackTries--; |
|
493 } while ((leaveCode != KErrNone) && ackTries > 0); |
|
494 |
|
495 return EFalse; |
|
496 } |
|
497 else |
|
498 { |
|
499 // bad sequence id |
|
500 RespondErr(kDSReplyNAK, kDSReplySequenceMissing); |
|
501 DiscardFrame(); |
|
502 return EFalse; |
|
503 } |
|
504 } |
|
505 } |
|
506 |
|
507 // trim back packet, eliminating FCS |
|
508 iBuffer.Delete(iBuffer.Length()-1, 1); |
|
509 |
|
510 // remove sequence id |
|
511 iBuffer.Delete(1, 1); |
|
512 |
|
513 return ETrue; |
|
514 } |