|
1 /* |
|
2 * Copyright (c) 2002-2007 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: Obex client implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <apgcli.h> // RApaLSession |
|
22 |
|
23 #include "BTServiceClient.h" |
|
24 #include "BTServiceUtils.h" |
|
25 #include "BTConnectionTimer.h" |
|
26 #include "BTSUDebug.h" |
|
27 |
|
28 const TUint16 KMtuSizeReceiv = 0xFFFF; // 64kB |
|
29 const TUint16 KMtuSizeTrans = 0x3000; // 12kB |
|
30 const TInt KBufferSize = 0x4000; // 16kB |
|
31 |
|
32 const TInt KBTConnectionTimeout = 20000000; // 20 seconds |
|
33 const TInt KBTAbortTimeout = 2000000; // 20 seconds |
|
34 |
|
35 // CONSTANTS |
|
36 |
|
37 // ============================ MEMBER FUNCTIONS =============================== |
|
38 |
|
39 // ----------------------------------------------------------------------------- |
|
40 // CBTServiceClient::CBTServiceClient |
|
41 // C++ default constructor can NOT contain any code, that |
|
42 // might leave. |
|
43 // ----------------------------------------------------------------------------- |
|
44 // |
|
45 CBTServiceClient::CBTServiceClient( MBTServiceClientObserver* aObserver ) |
|
46 : CActive( EPriorityStandard ), |
|
47 iClientState( EBTSCliIdle ), |
|
48 iObserver( aObserver ) |
|
49 { |
|
50 CActiveScheduler::Add( this ); |
|
51 } |
|
52 |
|
53 // ----------------------------------------------------------------------------- |
|
54 // CBTServiceClient::ConstructL |
|
55 // Symbian 2nd phase constructor can leave. |
|
56 // ----------------------------------------------------------------------------- |
|
57 // |
|
58 void CBTServiceClient::ConstructL( const TBTDevAddr& aRemoteDevice, |
|
59 const TUint aRemotePort, |
|
60 RArray<CObexHeader*> aHeaderList ) |
|
61 { |
|
62 FLOG(_L("[BTSU]\t CBTServiceClient::ConstructL()")); |
|
63 |
|
64 iTotalBytesSent = 0; |
|
65 // Create Obex Client |
|
66 // |
|
67 TObexBluetoothProtocolInfo info; |
|
68 info.iTransport = KBTSProtocol; |
|
69 info.iAddr.SetBTAddr( aRemoteDevice ); |
|
70 info.iAddr.SetPort( aRemotePort ); |
|
71 |
|
72 TObexProtocolPolicy obexProtocolPolicy; |
|
73 obexProtocolPolicy.SetReceiveMtu( KMtuSizeReceiv ); |
|
74 obexProtocolPolicy.SetTransmitMtu( KMtuSizeTrans ); |
|
75 |
|
76 iClient = CObexClient::NewL( info, obexProtocolPolicy ); |
|
77 iClient->SetCallBack( *this ); |
|
78 iPasskeyRequest = new (ELeave) CBTSUPasskeyRequest(); |
|
79 |
|
80 // Create Connect-object |
|
81 // |
|
82 iConnectObject = CObexNullObject::NewL(); |
|
83 |
|
84 if ( aHeaderList.Count() > 0 ) |
|
85 { |
|
86 for ( TInt index = 0; index < aHeaderList.Count(); index++ ) |
|
87 { |
|
88 iConnectObject->AddHeaderL( *aHeaderList[index] ); |
|
89 } |
|
90 } |
|
91 |
|
92 // Establish client connection |
|
93 // |
|
94 iClient->Connect( *iConnectObject, iStatus ); |
|
95 SetActive(); |
|
96 iClientState = EBTSCliConnecting; |
|
97 iConnectionTimer = CBTConnectionTimer::NewL(this); |
|
98 iConnectionTimer -> SetTimeOut ( TTimeIntervalMicroSeconds32( KBTConnectionTimeout ) ); |
|
99 iConnectionTimer -> Start(); |
|
100 |
|
101 FLOG(_L("[BTSU]\t CBTServiceClient::ConstructL() completed")); |
|
102 } |
|
103 |
|
104 // ----------------------------------------------------------------------------- |
|
105 // CBTServiceClient::NewL |
|
106 // Two-phased constructor. |
|
107 // ----------------------------------------------------------------------------- |
|
108 // |
|
109 CBTServiceClient* CBTServiceClient::NewL( MBTServiceClientObserver* aObserver, |
|
110 const TBTDevAddr& aRemoteDevice, |
|
111 const TUint aRemotePort, |
|
112 RArray<CObexHeader*> aHeaderList ) |
|
113 { |
|
114 CBTServiceClient* self = new( ELeave ) CBTServiceClient( aObserver ); |
|
115 CleanupStack::PushL( self ); |
|
116 self->ConstructL( aRemoteDevice, aRemotePort, aHeaderList ); |
|
117 CleanupStack::Pop(); |
|
118 return self; |
|
119 } |
|
120 |
|
121 |
|
122 // Destructor |
|
123 CBTServiceClient::~CBTServiceClient() |
|
124 { |
|
125 FLOG(_L("[BTSU]\t CBTServiceClient::~CBTServiceClient()")); |
|
126 |
|
127 if(iConnectionTimer) |
|
128 { |
|
129 iConnectionTimer->Cancel(); |
|
130 delete iConnectionTimer; |
|
131 iConnectionTimer=NULL; |
|
132 } |
|
133 Cancel(); |
|
134 if(iClient) |
|
135 { |
|
136 delete iClient; |
|
137 iClient = NULL; |
|
138 } |
|
139 delete iPasskeyRequest; |
|
140 iPasskeyRequest = NULL; |
|
141 |
|
142 if ( iConnectObject ) |
|
143 { |
|
144 iConnectObject->Reset(); |
|
145 delete iConnectObject; |
|
146 iConnectObject = NULL; |
|
147 } |
|
148 if ( iObjectBuffer ) |
|
149 { |
|
150 iObjectBuffer->Reset(); |
|
151 delete iObjectBuffer; |
|
152 iObjectBuffer = NULL; |
|
153 } |
|
154 if ( iGetObject ) |
|
155 { |
|
156 delete iGetObject; |
|
157 iGetObject = NULL; |
|
158 } |
|
159 if ( iPutObject ) |
|
160 { |
|
161 iPutObject->Reset(); |
|
162 delete iPutObject; |
|
163 iPutObject = NULL; |
|
164 } |
|
165 if ( iPutBufObject ) |
|
166 { |
|
167 iPutBufObject->Reset(); |
|
168 delete iPutBufObject; |
|
169 iPutBufObject = NULL; |
|
170 } |
|
171 |
|
172 if(iBuffer) |
|
173 { |
|
174 delete iBuffer; |
|
175 iBuffer = NULL; |
|
176 } |
|
177 |
|
178 FLOG(_L("[BTSU]\t CBTServiceClient::~CBTServiceClient() completed")); |
|
179 } |
|
180 |
|
181 // ----------------------------------------------------------------------------- |
|
182 // CBTServiceClient::GetObjectL |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 void CBTServiceClient::GetObjectL( RArray<CObexHeader*>& aHeaderList, |
|
186 const TDesC& aFileName ) |
|
187 { |
|
188 FLOG(_L("[BTSU]\t CBTServiceClient::GetObjectL()")); |
|
189 |
|
190 if ( iGetObject ) |
|
191 { |
|
192 iGetObject->Reset(); |
|
193 delete iGetObject; |
|
194 iGetObject = NULL; |
|
195 } |
|
196 |
|
197 // Create object |
|
198 // |
|
199 if ( aFileName == KNullDesC ) |
|
200 { |
|
201 iObjectBuffer = CBufFlat::NewL( KBTSUDataBufferExpandSize ); |
|
202 iGetObject = CObexBufObject::NewL( iObjectBuffer ); |
|
203 } |
|
204 else |
|
205 { |
|
206 iGetObject = CObexBufObject::NewL( NULL ); |
|
207 iGetObject->SetDataBufL( aFileName ); |
|
208 } |
|
209 |
|
210 // Set headers |
|
211 // |
|
212 if ( aHeaderList.Count() > 0 ) |
|
213 { |
|
214 for ( TInt index = 0; index < aHeaderList.Count(); index++ ) |
|
215 { |
|
216 iGetObject->AddHeaderL( *aHeaderList[index] ); |
|
217 } |
|
218 } |
|
219 |
|
220 // Send get request |
|
221 // |
|
222 iClient->Get( *iGetObject, iStatus ); |
|
223 SetActive(); |
|
224 iClientState = EBTSCliGetting; |
|
225 |
|
226 FLOG(_L("[BTSU]\t CBTServiceClient::GetObjectL() completed")); |
|
227 } |
|
228 |
|
229 |
|
230 // ----------------------------------------------------------------------------- |
|
231 // CBTServiceClient::PutObjectL |
|
232 // ----------------------------------------------------------------------------- |
|
233 // |
|
234 void CBTServiceClient::PutObjectL( RArray<CObexHeader*>& aHeaderList, |
|
235 const TDesC& aFileName ) |
|
236 { |
|
237 FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL()")); |
|
238 |
|
239 if ( iPutObject ) |
|
240 { |
|
241 iPutObject->Reset(); |
|
242 delete iPutObject; |
|
243 iPutObject = NULL; |
|
244 } |
|
245 |
|
246 // Create object |
|
247 // |
|
248 iPutObject = CObexFileObject::NewL(); |
|
249 |
|
250 // Set body |
|
251 // |
|
252 |
|
253 if ( aFileName != KNullDesC ) |
|
254 { |
|
255 iPutObject->InitFromFileL ( aFileName ); |
|
256 } |
|
257 |
|
258 // Set headers |
|
259 // |
|
260 if ( aHeaderList.Count() > 0 ) |
|
261 { |
|
262 for ( TInt index = 0; index < aHeaderList.Count(); index++ ) |
|
263 { |
|
264 iPutObject->AddHeaderL( *aHeaderList[index] ); |
|
265 } |
|
266 } |
|
267 |
|
268 // Send object |
|
269 // |
|
270 iClient->Put( *iPutObject, iStatus ); |
|
271 SetActive(); |
|
272 iClientState = EBTSCliPutting; |
|
273 |
|
274 FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL() completed")); |
|
275 } |
|
276 |
|
277 // ----------------------------------------------------------------------------- |
|
278 // CBTServiceClient::PutObjectL |
|
279 // ----------------------------------------------------------------------------- |
|
280 // |
|
281 void CBTServiceClient::PutObjectL( RArray<CObexHeader*>& aHeaderList, |
|
282 RFile& aFile ) |
|
283 { |
|
284 FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL()")); |
|
285 |
|
286 if ( iPutObject ) |
|
287 { |
|
288 iPutObject->Reset(); |
|
289 delete iPutObject; |
|
290 iPutObject = NULL; |
|
291 } |
|
292 |
|
293 if ( iPutBufObject ) |
|
294 { |
|
295 iPutBufObject->Reset(); |
|
296 delete iPutBufObject; |
|
297 iPutBufObject = NULL; |
|
298 } |
|
299 // Create object |
|
300 // |
|
301 iPutBufObject = CObexBufObject::NewL(NULL); |
|
302 |
|
303 // Resolve MIME type |
|
304 // |
|
305 |
|
306 RApaLsSession session; |
|
307 HBufC8* mimeType = NULL; |
|
308 TDataType type; |
|
309 |
|
310 TUid uid; |
|
311 // Set headers |
|
312 // |
|
313 if ( aHeaderList.Count() > 0 ) |
|
314 { |
|
315 for ( TInt index = 0; index < aHeaderList.Count(); index++ ) |
|
316 { |
|
317 iPutBufObject->AddHeaderL( *aHeaderList[index] ); |
|
318 } |
|
319 } |
|
320 |
|
321 TDesC8 typeheader=iPutBufObject->Type(); |
|
322 if ( typeheader == KNullDesC8 ) |
|
323 { |
|
324 User::LeaveIfError( session.Connect() ); |
|
325 CleanupClosePushL( session ); |
|
326 TInt error = session.AppForDocument( aFile, uid, type ); |
|
327 if ( error == KErrNone ) |
|
328 { |
|
329 mimeType = type.Des8().AllocLC(); |
|
330 iPutBufObject->SetTypeL(*mimeType); |
|
331 CleanupStack::PopAndDestroy(); |
|
332 } |
|
333 CleanupStack::Pop(); // session |
|
334 session.Close(); |
|
335 } |
|
336 |
|
337 //Set object information |
|
338 // |
|
339 TFileName filename; |
|
340 aFile.Name(filename); |
|
341 |
|
342 TInt size; |
|
343 aFile.Size(size); |
|
344 iPutBufObject->SetLengthL(size); |
|
345 iPutBufObject->SetNameL(filename); |
|
346 |
|
347 TTime time; |
|
348 if ( aFile.Modified(time) == KErrNone ) |
|
349 { |
|
350 iPutBufObject->SetTimeL(time); |
|
351 } |
|
352 |
|
353 RFile file; |
|
354 file.Duplicate(aFile); |
|
355 |
|
356 iBuffer = CBufFlat::NewL(KBufferSize); |
|
357 iBuffer ->ResizeL(KBufferSize); |
|
358 |
|
359 TObexRFileBackedBuffer bufferdetails(*iBuffer,file,CObexBufObject::ESingleBuffering); |
|
360 iPutBufObject->SetDataBufL(bufferdetails); |
|
361 |
|
362 // Send object |
|
363 // |
|
364 iClient->Put( *iPutBufObject, iStatus ); |
|
365 SetActive(); |
|
366 iClientState = EBTSCliPutting; |
|
367 |
|
368 FLOG(_L("[BTSU]\t CBTServiceClient::PutObjectL() completed")); |
|
369 } |
|
370 |
|
371 // ----------------------------------------------------------------------------- |
|
372 // CBTServiceClient::CloseClientConnection |
|
373 // ----------------------------------------------------------------------------- |
|
374 // |
|
375 void CBTServiceClient::CloseClientConnection() |
|
376 { |
|
377 FLOG(_L("[BTSU]\t CBTServiceClient::CloseClientConnection()")); |
|
378 |
|
379 iClient->Disconnect( iStatus ); |
|
380 SetActive(); |
|
381 iClientState = EBTSCliDisconnecting; |
|
382 |
|
383 FLOG(_L("[BTSU]\t CBTServiceClient::CloseClientConnection() completed")); |
|
384 } |
|
385 |
|
386 // ----------------------------------------------------------------------------- |
|
387 // CBTServiceClient::GetProgressStatus |
|
388 // ----------------------------------------------------------------------------- |
|
389 // |
|
390 TInt CBTServiceClient::GetProgressStatus() |
|
391 { |
|
392 FLOG(_L("[BTSU]\t CBTServiceClient::GetProgressStatus()")); |
|
393 |
|
394 TInt bytesSent = 0; |
|
395 if ( iPutBufObject ) |
|
396 { |
|
397 bytesSent = iPutBufObject->BytesSent(); |
|
398 } |
|
399 if ( iPutObject ) |
|
400 { |
|
401 bytesSent = iPutObject->BytesSent(); |
|
402 } |
|
403 |
|
404 FTRACE(FPrint(_L("[BTSU]\t CBTServiceClient::GetProgressStatus() completed, bytes sent %d"), iTotalBytesSent + bytesSent ) ); |
|
405 |
|
406 return iTotalBytesSent + bytesSent; |
|
407 } |
|
408 |
|
409 // ----------------------------------------------------------------------------- |
|
410 // CBTServiceClient::GetUserPasswordL |
|
411 // ----------------------------------------------------------------------------- |
|
412 // |
|
413 void CBTServiceClient::GetUserPasswordL( const TDesC& /*aRealm*/ ) |
|
414 { |
|
415 FLOG(_L("[BTSU]\t CBTServiceClient::GetUserPasswordL()")); |
|
416 |
|
417 iPasskeyRequest->StartPassKeyRequestL( iClient ); |
|
418 |
|
419 FLOG(_L("[BTSU]\t CBTServiceClient::GetUserPasswordL() completed")); |
|
420 } |
|
421 |
|
422 // ----------------------------------------------------------------------------- |
|
423 // CBTServiceClient::DoCancel |
|
424 // ----------------------------------------------------------------------------- |
|
425 // |
|
426 void CBTServiceClient::DoCancel() |
|
427 { |
|
428 FLOG(_L("[BTSU]\t CBTServiceClient::DoCancel()")); |
|
429 |
|
430 if ( iConnectionTimer ) |
|
431 { |
|
432 iConnectionTimer->Cancel(); |
|
433 delete iConnectionTimer; |
|
434 iConnectionTimer=NULL; |
|
435 } |
|
436 // Deleting obexclient is the only way to cancel active requests |
|
437 // |
|
438 if ( iClient ) |
|
439 { |
|
440 delete iClient; |
|
441 iClient = NULL; |
|
442 } |
|
443 |
|
444 FLOG(_L("[BTSU]\t CBTServiceClient::DoCancel() completed")); |
|
445 } |
|
446 |
|
447 // ----------------------------------------------------------------------------- |
|
448 // CBTServiceClient::RunL |
|
449 // ----------------------------------------------------------------------------- |
|
450 // |
|
451 void CBTServiceClient::RunL() |
|
452 { |
|
453 FTRACE(FPrint(_L("[BTSU]\t CBTServiceClient::RunL() status %d"), iStatus.Int() ) ); |
|
454 if ( !iObserver ) |
|
455 { |
|
456 return; |
|
457 } |
|
458 switch ( iClientState ) |
|
459 { |
|
460 case EBTSCliConnecting: |
|
461 { |
|
462 FLOG(_L("[BTSU]\t CBTServiceClient::RunL() EBTSCliConnecting")); |
|
463 iConnectObject->Reset(); |
|
464 if(iConnectionTimer) |
|
465 { |
|
466 iConnectionTimer->Cancel(); |
|
467 delete iConnectionTimer; |
|
468 iConnectionTimer=NULL; |
|
469 } |
|
470 iObserver->ConnectCompleted( iStatus.Int() ); |
|
471 break; |
|
472 } |
|
473 |
|
474 case EBTSCliPutting: |
|
475 { |
|
476 FLOG(_L("[BTSU]\t CBTServiceClient::RunL() EBTSCliPutting")); |
|
477 if(iPutBufObject) |
|
478 { |
|
479 iTotalBytesSent += iPutBufObject->BytesSent(); |
|
480 } |
|
481 else |
|
482 { |
|
483 iTotalBytesSent += iPutObject->BytesSent(); |
|
484 } |
|
485 |
|
486 const CObexHeaderSet& response=iClient->GetPutFinalResponseHeaders(); |
|
487 if ( iPutBufObject ) |
|
488 { |
|
489 iPutBufObject->Reset(); |
|
490 delete iPutBufObject; |
|
491 iPutBufObject = NULL; |
|
492 } |
|
493 |
|
494 if ( iPutObject ) |
|
495 { |
|
496 iPutObject->Reset(); |
|
497 delete iPutObject; |
|
498 iPutObject = NULL; |
|
499 } |
|
500 if(iBuffer) |
|
501 { |
|
502 delete iBuffer; |
|
503 iBuffer = NULL; |
|
504 } |
|
505 //put there call getEnv |
|
506 iObserver->PutCompleted( iStatus.Int(), &response); |
|
507 FLOG(_L("[BTSU]\t CBTServiceClient::RunL() EBTSCliPutting done")); |
|
508 break; |
|
509 } |
|
510 |
|
511 case EBTSCliGetting: |
|
512 { |
|
513 iObserver->GetCompleted( iStatus.Int(), iGetObject ); |
|
514 break; |
|
515 } |
|
516 |
|
517 case EBTSCliDisconnecting: |
|
518 { |
|
519 // Any errors are ignored |
|
520 // |
|
521 iObserver->ClientConnectionClosed(); |
|
522 break; |
|
523 } |
|
524 |
|
525 case EBTSCliIdle: |
|
526 default: |
|
527 { |
|
528 FLOG(_L("[BTSU]\t CBTServiceClient::RunL() ERROR, unhandled case")); |
|
529 break; |
|
530 } |
|
531 } |
|
532 |
|
533 FLOG(_L("[BTSU]\t CBTServiceClient::RunL() completed")); |
|
534 } |
|
535 |
|
536 // ----------------------------------------------------------------------------- |
|
537 // CBTServiceClient::ConnectionTimedOut |
|
538 // ----------------------------------------------------------------------------- |
|
539 // |
|
540 void CBTServiceClient::ConnectionTimedOut() |
|
541 { |
|
542 FLOG(_L("[BTSU]\t CBTServiceClient::ConnectionTimedOut")); |
|
543 switch ( iClientState ) |
|
544 { |
|
545 case EBTSCliConnecting: |
|
546 { |
|
547 iObserver->ConnectTimedOut(); |
|
548 break; |
|
549 } |
|
550 case EBTSCliGetting: |
|
551 { |
|
552 iObserver->GetCompleted( KErrAbort, iGetObject ); |
|
553 break; |
|
554 } |
|
555 case EBTSCliPutting: |
|
556 { |
|
557 const CObexHeaderSet& response=iClient->GetPutFinalResponseHeaders(); |
|
558 iObserver->PutCompleted( KErrAbort, &response ); |
|
559 break; |
|
560 } |
|
561 default: |
|
562 FLOG(_L("[BTSU]\t CBTServiceClient::ConnectionTimedOut unhandled client state ")); |
|
563 } |
|
564 |
|
565 FLOG(_L("[BTSU]\t CBTServiceClient::ConnectionTimedOut")); |
|
566 } |
|
567 // ----------------------------------------------------------------------------- |
|
568 // CBTServiceClient::Abort |
|
569 // ----------------------------------------------------------------------------- |
|
570 // |
|
571 void CBTServiceClient::Abort() |
|
572 { |
|
573 FLOG(_L("[BTSU]\t CBTServiceClient::Abort")); |
|
574 if ( iClient && ( iClientState == EBTSCliPutting || iClientState == EBTSCliGetting ) ) |
|
575 { |
|
576 if ( iConnectionTimer ) |
|
577 { |
|
578 delete iConnectionTimer; |
|
579 iConnectionTimer = NULL; |
|
580 } |
|
581 TRAPD(trapErr, iConnectionTimer = CBTConnectionTimer::NewL(this) ); |
|
582 if ( trapErr != KErrNone) |
|
583 { |
|
584 iObserver->ConnectCompleted( KErrAbort ); |
|
585 return; |
|
586 } |
|
587 iConnectionTimer -> SetTimeOut ( TTimeIntervalMicroSeconds32( KBTAbortTimeout ) ); |
|
588 iConnectionTimer -> Start(); |
|
589 iClient->Abort(); |
|
590 } |
|
591 else if ( iClient && iClientState == EBTSCliConnecting) |
|
592 { |
|
593 iObserver->ConnectCompleted( KErrAbort ); |
|
594 } |
|
595 FLOG(_L("[BTSU]\t CBTServiceClient::Abort")); |
|
596 |
|
597 } |
|
598 |
|
599 // End of File |