|
1 /* |
|
2 * Copyright (c) 2009 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: CHtiCommAdapter implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "HtiCommAdapter.h" |
|
20 #include "HtiDispatcher.h" |
|
21 #include "HTICommPluginInterface.h" |
|
22 #include "HtiMessage.h" |
|
23 #include "HtiNotifier.h" |
|
24 |
|
25 #include "HTILogging.h" |
|
26 |
|
27 //default value for max message size for incoming messages |
|
28 //used if value in constructor is not valid (<0) |
|
29 const static TInt KMaxMessageSize = 10 * 1024; // 10 KB |
|
30 |
|
31 const static TInt KHtiCommPriority = 2; |
|
32 |
|
33 _LIT( KCommPanic, "HtiCommAdapter" ); |
|
34 |
|
35 CHtiCommAdapter* CHtiCommAdapter::NewLC( CHTICommPluginInterface* aCommPlugin, |
|
36 CHtiDispatcher* aDispatcher, TInt aMaxMsgSize ) |
|
37 { |
|
38 CHtiCommAdapter* obj = new (ELeave) CHtiCommAdapter( aDispatcher, |
|
39 aCommPlugin, aMaxMsgSize ); |
|
40 CleanupStack::PushL( obj ); |
|
41 obj->ConstructL(); |
|
42 return obj; |
|
43 } |
|
44 |
|
45 CHtiCommAdapter* CHtiCommAdapter::NewL( CHTICommPluginInterface* aCommPlugin, |
|
46 CHtiDispatcher* aDispatcher, TInt aMaxMsgSize ) |
|
47 { |
|
48 CHtiCommAdapter* obj = NewLC( aCommPlugin, aDispatcher, aMaxMsgSize ); |
|
49 CleanupStack::Pop(); |
|
50 return obj; |
|
51 } |
|
52 |
|
53 CHtiCommAdapter::CHtiCommAdapter( CHtiDispatcher* aDispatcher, |
|
54 CHTICommPluginInterface* aCommPlugin, |
|
55 TInt aMaxMsgSize ) |
|
56 :CActive( KHtiCommPriority ), |
|
57 iState( EIdle ), |
|
58 iDispatcher( aDispatcher ), |
|
59 iCommPlugin( aCommPlugin ), |
|
60 iBufferPtr( NULL, 0 ), |
|
61 iMsgToReceive( NULL ), |
|
62 iMsgToSend( NULL ), |
|
63 iMsgToSendPtr( NULL, 0 ), |
|
64 iMsgToSendOffset( 0 ), |
|
65 iSkipLength( 0 ), |
|
66 iMaxMessageSize( aMaxMsgSize > 0 ? aMaxMsgSize : KMaxMessageSize ) |
|
67 { |
|
68 HTI_LOG_FORMAT( "MaxMsgSize %d", iMaxMessageSize ); |
|
69 }; |
|
70 |
|
71 CHtiCommAdapter::~CHtiCommAdapter() |
|
72 { |
|
73 HTI_LOG_FUNC_IN( "CHtiCommAdapter::~CHtiCommAdapter" ); |
|
74 Cancel(); |
|
75 |
|
76 delete iBuffer; |
|
77 delete iLeftovers; |
|
78 |
|
79 delete iMsgToReceive; |
|
80 delete iMsgToSend; |
|
81 |
|
82 HTI_LOG_FUNC_OUT( "CHtiCommAdapter::~CHtiCommAdapter" ); |
|
83 } |
|
84 |
|
85 void CHtiCommAdapter::ConstructL() |
|
86 { |
|
87 HTI_LOG_FUNC_IN( "CHtiCommAdapter::ConstructL" ); |
|
88 |
|
89 //allocate recommended buffer plus some extra space |
|
90 //needed to handle msg parsing |
|
91 iBuffer = HBufC8::NewL( iCommPlugin->GetReceiveBufferSize() + |
|
92 CHtiMessage::MinHeaderSize() ); |
|
93 |
|
94 iBuffer->Des().SetLength( iCommPlugin->GetReceiveBufferSize() ); |
|
95 iBufferPtr.Set( iBuffer->Des().LeftTPtr( |
|
96 iCommPlugin->GetReceiveBufferSize() ) ); |
|
97 |
|
98 iLeftovers = HBufC8::NewL( CHtiMessage::MinHeaderSize() ); |
|
99 |
|
100 CActiveScheduler::Add( this ); |
|
101 HTI_LOG_FUNC_OUT( "CHtiCommAdapter::ConstructL" ); |
|
102 } |
|
103 |
|
104 void CHtiCommAdapter::ReceiveMessage() |
|
105 { |
|
106 ReceiveMessage( EFalse ); |
|
107 } |
|
108 |
|
109 void CHtiCommAdapter::ReceiveMessage( TBool aContinue ) |
|
110 { |
|
111 HTI_LOG_FUNC_IN( "CHtiCommAdapter::ReceiveMessage" ); |
|
112 |
|
113 iState = aContinue?EReceivingCont:EReceiving; |
|
114 iCommPlugin->Receive( iBufferPtr, iStatus ); |
|
115 SetActive(); |
|
116 |
|
117 HTI_LOG_FUNC_OUT( "CHtiCommAdapter::ReceiveMessage" ); |
|
118 } |
|
119 |
|
120 void CHtiCommAdapter::SendMessage( CHtiMessage* aMessage ) |
|
121 { |
|
122 HTI_LOG_FUNC_IN( "CCommAdapter::SendMessage" ); |
|
123 iState = ESending; |
|
124 if ( aMessage != NULL ) |
|
125 { |
|
126 HTI_LOG_TEXT( "send first packet" ); |
|
127 iMsgToSend = aMessage; |
|
128 iMsgToSendOffset = 0; |
|
129 |
|
130 //first send header |
|
131 iMsgToSendPtr.Set( iMsgToSend->Header() ); |
|
132 } |
|
133 else if ( iMsgToSend != NULL ) |
|
134 { |
|
135 //send next chunk |
|
136 if ( iMsgToSend->BodySize() <= |
|
137 ( iMsgToSendOffset + iCommPlugin->GetSendBufferSize() ) ) |
|
138 { |
|
139 HTI_LOG_TEXT( "send remainder" ); |
|
140 iMsgToSendPtr.Set( iMsgToSend->Body().Mid( iMsgToSendOffset ) ); |
|
141 iMsgToSendOffset = iMsgToSend->BodySize(); //last sending |
|
142 } |
|
143 else |
|
144 { |
|
145 HTI_LOG_TEXT( "send packet" ); |
|
146 iMsgToSendPtr.Set( iMsgToSend->Body().Mid( iMsgToSendOffset, |
|
147 iCommPlugin->GetSendBufferSize() ) ); |
|
148 iMsgToSendOffset += iCommPlugin->GetSendBufferSize(); |
|
149 } |
|
150 } |
|
151 else |
|
152 { |
|
153 User::Panic( KCommPanic, KErrGeneral ); |
|
154 } |
|
155 |
|
156 iCommPlugin->Send( iMsgToSendPtr, iStatus ); |
|
157 SetActive(); |
|
158 |
|
159 HTI_LOG_FUNC_OUT( "CCommAdapter::SendMessage" ); |
|
160 } |
|
161 |
|
162 void CHtiCommAdapter::Reset() |
|
163 { |
|
164 HTI_LOG_FUNC_IN( "CHtiCommAdapter::Reset" ); |
|
165 Cancel(); |
|
166 //receiving msg |
|
167 delete iMsgToReceive; |
|
168 iMsgToReceive = NULL; |
|
169 |
|
170 //send msg |
|
171 delete iMsgToSend; |
|
172 iMsgToSend = NULL; |
|
173 iMsgToSendOffset = 0; |
|
174 |
|
175 //empty buffers |
|
176 iLeftovers->Des().Zero(); |
|
177 iBuffer->Des().Zero(); |
|
178 iSkipLength = 0; |
|
179 |
|
180 //set idle |
|
181 iState = EIdle; |
|
182 |
|
183 HTI_LOG_FUNC_OUT( "CHtiCommAdapter::Reset" ); |
|
184 } |
|
185 |
|
186 void CHtiCommAdapter::RunL() |
|
187 { |
|
188 HTI_LOG_FUNC_IN( "CHtiCommAdapter::RunL" ); |
|
189 HTI_LOG_FORMAT( "iStatus %d", iStatus.Int() ); |
|
190 |
|
191 //handle reset code common for all states |
|
192 if ( iStatus == KErrComModuleReset ) |
|
193 { |
|
194 HTI_LOG_TEXT( "Reset received from comm module" ); |
|
195 //reset dispatcher |
|
196 iDispatcher->Reset(); |
|
197 //dispatcher will call Reset() for comm adapters and reissue requests |
|
198 return; |
|
199 } |
|
200 |
|
201 //check for critical (unrecoverable) communication errors |
|
202 else if ( iStatus == KErrServerTerminated ) |
|
203 { |
|
204 if ( iDispatcher->GetShowErrorDialogs() ) |
|
205 { |
|
206 User::Panic( KCommPanic, KErrServerTerminated ); |
|
207 } |
|
208 else |
|
209 { |
|
210 User::Exit( KErrServerTerminated ); |
|
211 } |
|
212 } |
|
213 // USB errors from d32usbc.h |
|
214 else if ( -6700 > iStatus.Int() && iStatus.Int() > -6712 ) |
|
215 { |
|
216 if ( iDispatcher->GetShowErrorDialogs() ) |
|
217 { |
|
218 TBuf<48> errorText; |
|
219 errorText.Append( _L( "USB connection lost (" ) ); |
|
220 errorText.AppendNum( iStatus.Int() ); |
|
221 errorText.Append( _L( "). HTI stopped." ) ); |
|
222 CHtiNotifier::ShowErrorL( errorText ); |
|
223 } |
|
224 User::Exit( KErrDisconnected ); |
|
225 } |
|
226 else if ( iStatus == KErrDisconnected ) |
|
227 { |
|
228 // This happens if Bluetooth connection is lost. |
|
229 if ( iDispatcher->GetShowErrorDialogs() ) |
|
230 { |
|
231 CHtiNotifier::ShowErrorL( |
|
232 _L( "Connection lost. HTI stopped." ), KErrDisconnected ); |
|
233 } |
|
234 User::Exit( KErrDisconnected ); |
|
235 } |
|
236 |
|
237 switch ( iState ) |
|
238 { |
|
239 case EReceiving: |
|
240 //process |
|
241 HTI_LOG_TEXT( "EReceiving" ); |
|
242 if ( iStatus == KErrNone ) |
|
243 { |
|
244 iBuffer->Des().SetLength( iBufferPtr.Length() ); |
|
245 TRAPD( err, HandleReceiveL() ); |
|
246 if ( err != KErrNone ) |
|
247 { |
|
248 HTI_LOG_FORMAT( "Error in HandleReceiveL %d", err ); |
|
249 |
|
250 delete iMsgToReceive; |
|
251 iMsgToReceive = NULL; |
|
252 |
|
253 iDispatcher->Notify( err ); |
|
254 |
|
255 ReceiveMessage(); |
|
256 } |
|
257 } |
|
258 else |
|
259 { |
|
260 HTI_LOG_FORMAT( "Error %d, reissue request", iStatus.Int() ); |
|
261 iDispatcher->Notify( iStatus.Int() ); |
|
262 ReceiveMessage(); |
|
263 } |
|
264 break; |
|
265 case EReceivingCont: |
|
266 //process |
|
267 HTI_LOG_TEXT( "EReceivingCont" ); |
|
268 if ( iStatus == KErrNone ) |
|
269 { |
|
270 iBuffer->Des().SetLength( iBufferPtr.Length() ); |
|
271 TRAPD( err, HandleReceiveContL() ); |
|
272 if ( err != KErrNone ) |
|
273 { |
|
274 HTI_LOG_FORMAT( "Error in HandleReceiveContL %d", err ); |
|
275 |
|
276 delete iMsgToReceive; |
|
277 iMsgToReceive = NULL; |
|
278 |
|
279 iDispatcher->Notify( err ); |
|
280 |
|
281 ReceiveMessage(); |
|
282 } |
|
283 } |
|
284 else |
|
285 { |
|
286 HTI_LOG_FORMAT( "Error %d, reissue request", iStatus.Int() ); |
|
287 HTI_LOG_TEXT( "and dismiss received HTI msg beginning" ); |
|
288 delete iMsgToReceive; |
|
289 iMsgToReceive = NULL; |
|
290 |
|
291 iDispatcher->Notify( iStatus.Int() ); |
|
292 |
|
293 ReceiveMessage(); |
|
294 } |
|
295 break; |
|
296 case ESending: |
|
297 HTI_LOG_TEXT( "ESending" ); |
|
298 if ( iStatus == KErrNone ) |
|
299 { |
|
300 HandleSend(); |
|
301 } |
|
302 else |
|
303 { |
|
304 HTI_LOG_FORMAT( "Error %d, stop sending, go to EIdle", iStatus.Int() ); |
|
305 |
|
306 delete iMsgToSend; |
|
307 iMsgToSend = NULL; |
|
308 iMsgToSendOffset = 0; |
|
309 iState = EIdle; |
|
310 |
|
311 iDispatcher->Notify( iStatus.Int() ); |
|
312 } |
|
313 break; |
|
314 case EIdle: |
|
315 //ERROR |
|
316 HTI_LOG_TEXT( "EIdle" ); |
|
317 User::Panic( KCommPanic, KErrGeneral ); |
|
318 break; |
|
319 default: |
|
320 //ERROR |
|
321 HTI_LOG_TEXT( "default" ); |
|
322 User::Panic( KCommPanic, KErrGeneral ); |
|
323 }; |
|
324 |
|
325 HTI_LOG_FUNC_OUT( "CHtiCommAdapter::RunL" ); |
|
326 } |
|
327 |
|
328 void CHtiCommAdapter::HandleReceiveL() |
|
329 { |
|
330 TBool toContinue = EFalse; //for reissuing state |
|
331 |
|
332 if ( iLeftovers->Length() > 0 ) |
|
333 { |
|
334 HTI_LOG_TEXT( "handle leftovers" ); |
|
335 //if something left from previous time |
|
336 //insert it to the buffer beggining |
|
337 //check that there is enough space (should be) |
|
338 if ( iBuffer->Length() + iLeftovers->Length() <= |
|
339 iBuffer->Des().MaxLength() ) |
|
340 { |
|
341 iBuffer->Des().Insert( 0, *iLeftovers ); |
|
342 } |
|
343 else |
|
344 { |
|
345 //error |
|
346 HTI_LOG_TEXT( "iMsgToReceive contain too much leftovers, drop them" ); |
|
347 } |
|
348 |
|
349 iLeftovers->Des().Zero(); |
|
350 } |
|
351 |
|
352 //use loop cause can be several HTI messages in iBuffer |
|
353 while ( iBuffer->Length() >= CHtiMessage::MinHeaderSize() ) |
|
354 { |
|
355 HTI_LOG_FORMAT( "iBuffer.Length() %d", iBuffer->Length() ); |
|
356 HTI_LOG_TEXT( "check header" ); |
|
357 |
|
358 if ( CHtiMessage::CheckValidHtiHeader( *iBuffer ) ) |
|
359 { |
|
360 HTI_LOG_TEXT( "valid header" ); |
|
361 TInt msgSize = CHtiMessage::Size( *iBuffer ); |
|
362 HTI_LOG_FORMAT( "msgSize %d", msgSize ); |
|
363 HTI_LOG_HEX( iBuffer->Des().Ptr(), 14 ); |
|
364 |
|
365 __ASSERT_ALWAYS( iMsgToReceive == NULL, |
|
366 User::Panic( KCommPanic, KErrGeneral ) ); |
|
367 |
|
368 //check msgSize is acceptable |
|
369 if ( msgSize > iMaxMessageSize ) |
|
370 { |
|
371 //send err message |
|
372 iDispatcher->DispatchOutgoingErrorMessage( EHtiErrBigMessage ); |
|
373 iSkipLength = msgSize; |
|
374 } |
|
375 else if ( msgSize > iDispatcher->GetFreeMemory() ) |
|
376 { |
|
377 //send err message |
|
378 iDispatcher->DispatchOutgoingErrorMessage( EHtiErrNoMemory ); |
|
379 iSkipLength = msgSize; |
|
380 } |
|
381 else |
|
382 { |
|
383 iMsgToReceive = CHtiMessage::NewL( *iBuffer ); |
|
384 } |
|
385 |
|
386 if ( iMsgToReceive == NULL ) |
|
387 { |
|
388 HTI_LOG_TEXT( "skip message" ); |
|
389 if ( iSkipLength > iBuffer->Length() ) |
|
390 { |
|
391 iSkipLength -= iBuffer->Length(); |
|
392 iBuffer->Des().Zero(); |
|
393 // and continue to receive HTI message |
|
394 //switch state to EReceiveCont |
|
395 toContinue = ETrue; |
|
396 } |
|
397 else |
|
398 { |
|
399 //should not be here, but just in case |
|
400 iBuffer->Des().Delete( 0, iSkipLength ); |
|
401 iSkipLength = 0; |
|
402 } |
|
403 } |
|
404 else if ( iMsgToReceive->IsBodyComplete() ) |
|
405 { |
|
406 HTI_LOG_TEXT( "handle small message" ); |
|
407 //iMsgToReceive is already contain whole HTI message |
|
408 iDispatcher->DispatchIncomingMessage( iMsgToReceive ); |
|
409 |
|
410 //delete sent msg from buffer (and procces the rest) |
|
411 iBuffer->Des().Delete( 0, iMsgToReceive->Size() ); |
|
412 iMsgToReceive = NULL; |
|
413 } |
|
414 else |
|
415 { |
|
416 HTI_LOG_TEXT( "start receive big message" ); |
|
417 //clear it to leave loop |
|
418 iBuffer->Des().Zero(); |
|
419 // and continue to receive HTI message |
|
420 //switch state to EReceiveCont |
|
421 toContinue = ETrue; |
|
422 } |
|
423 } |
|
424 else |
|
425 { |
|
426 HTI_LOG_TEXT( "invalid header, dismiss buffer and reissue request" ); |
|
427 HTI_LOG_HEX( iBuffer->Des().Ptr(), 14 ); |
|
428 // clear all buffer cause don't know where is valid header starts |
|
429 // and wait for the next packet |
|
430 iBuffer->Des().Zero(); //to leave loop |
|
431 } |
|
432 } |
|
433 |
|
434 if ( iBuffer->Length() > 0 ) |
|
435 { |
|
436 HTI_LOG_FORMAT( "copy leftovers %d", iBuffer->Length() ); |
|
437 //header beggining left in the buffer |
|
438 //copy it to iLeftovers |
|
439 ( *iLeftovers ) = ( *iBuffer ); |
|
440 } |
|
441 //reissue request |
|
442 ReceiveMessage( toContinue ); |
|
443 } |
|
444 |
|
445 void CHtiCommAdapter::HandleReceiveContL() |
|
446 { |
|
447 HTI_LOG_FORMAT( "In buffer: %d", iBuffer->Length() ); |
|
448 TBool toContinue = ETrue; |
|
449 |
|
450 if ( iMsgToReceive != NULL && iSkipLength == 0 ) |
|
451 { |
|
452 //add new packet to a message |
|
453 TInt copyLen = iMsgToReceive->AddToBody( *iBuffer ); |
|
454 |
|
455 //delete what we copied to the message |
|
456 iBuffer->Des().Delete( 0, copyLen ); |
|
457 |
|
458 if ( iMsgToReceive->IsBodyComplete() ) |
|
459 { |
|
460 HTI_LOG_FORMAT( "body complete: %d", iMsgToReceive->BodySize() ); |
|
461 |
|
462 iDispatcher->DispatchIncomingMessage( iMsgToReceive ); |
|
463 iMsgToReceive = NULL; |
|
464 |
|
465 //message complete, go to state EReceive |
|
466 toContinue = EFalse; |
|
467 } |
|
468 } |
|
469 else if ( iSkipLength > 0 ) |
|
470 { |
|
471 HTI_LOG_FORMAT( "continue skipping, remains %d", iSkipLength ); |
|
472 |
|
473 if ( iSkipLength > iBuffer->Length() ) |
|
474 { |
|
475 iSkipLength -= iBuffer->Length(); |
|
476 iBuffer->Des().Zero(); |
|
477 } |
|
478 else |
|
479 { |
|
480 //skip last bytes |
|
481 iBuffer->Des().Delete( 0, iSkipLength ); |
|
482 iSkipLength = 0; |
|
483 |
|
484 toContinue = EFalse; |
|
485 } |
|
486 } |
|
487 |
|
488 if ( iBuffer->Length() > 0 ) |
|
489 { |
|
490 //process remainder and reissue in HandleReceiveL() |
|
491 HandleReceiveL(); |
|
492 } |
|
493 else |
|
494 { |
|
495 //reissue |
|
496 ReceiveMessage(toContinue); |
|
497 } |
|
498 } |
|
499 |
|
500 void CHtiCommAdapter::HandleSend() |
|
501 { |
|
502 if ( iMsgToSendOffset < iMsgToSend->BodySize() ) |
|
503 { |
|
504 //reissuse sending |
|
505 SendMessage( NULL ); |
|
506 } |
|
507 else |
|
508 { |
|
509 HTI_LOG_TEXT( "finish send, go to idle" ); |
|
510 iMsgToSendOffset = 0; |
|
511 delete iMsgToSend; |
|
512 iMsgToSend = NULL; |
|
513 iState = EIdle; |
|
514 iDispatcher->Notify( KErrNone ); |
|
515 } |
|
516 } |
|
517 |
|
518 TInt CHtiCommAdapter::RunError( TInt aError ) |
|
519 { |
|
520 TInt err = aError; |
|
521 switch ( aError ) |
|
522 { |
|
523 case KErrNoMemory: |
|
524 break; |
|
525 case KErrNotFound: |
|
526 break; |
|
527 } |
|
528 return err; |
|
529 } |
|
530 |
|
531 void CHtiCommAdapter::DoCancel() |
|
532 { |
|
533 switch ( iState ) |
|
534 { |
|
535 case EReceiving: |
|
536 case EReceivingCont: |
|
537 iCommPlugin->CancelReceive(); |
|
538 break; |
|
539 case ESending: |
|
540 iCommPlugin->CancelSend(); |
|
541 break; |
|
542 default: |
|
543 break; |
|
544 } |
|
545 } |