|
1 /* |
|
2 * ============================================================================ |
|
3 * Name : btnotifconnectiontracker.cpp |
|
4 * Part of : bluetoothengine / btnotif |
|
5 * Description : Bluetooth connection tracker and manager. |
|
6 * |
|
7 * Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
8 * All rights reserved. |
|
9 * This component and the accompanying materials are made available |
|
10 * under the terms of "Eclipse Public License v1.0" |
|
11 * which accompanies this distribution, and is available |
|
12 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
13 * |
|
14 * Initial Contributors: |
|
15 * Nokia Corporation - initial contribution. |
|
16 * |
|
17 * Contributors: |
|
18 * Nokia Corporation |
|
19 * ============================================================================ |
|
20 * Template version: 4.1 |
|
21 */ |
|
22 |
|
23 #include "btnotifconnectiontracker.h" |
|
24 #include <btextnotifiers.h> |
|
25 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
26 #include <btextnotifierspartner.h> |
|
27 #endif |
|
28 |
|
29 #include "btnotifconnection.h" |
|
30 #include "btnotifsession.h" |
|
31 #include "btnotifclientserver.h" |
|
32 #include "bluetoothtrace.h" |
|
33 |
|
34 /** Id for the link key watcher active object. */ |
|
35 const TInt KLinkCountWatcher = 30; |
|
36 /** Id for the pairing result watcher active object. */ |
|
37 const TInt KSspResultWatcher = 31; |
|
38 /** Id for the registry watcher active object (TEMP!). */ |
|
39 const TInt KRegistryWatcher = 41; |
|
40 /** Time window for determining if there are too many requests. */ |
|
41 #ifndef __WINS__ |
|
42 #define KDENYTHRESHOLD TTimeIntervalSeconds(3) |
|
43 #else //__WINS__ |
|
44 #define KDENYTHRESHOLD TTimeIntervalSeconds(5) |
|
45 #endif //__WINS__ |
|
46 |
|
47 |
|
48 // ======== LOCAL FUNCTIONS ======== |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // Checks if the notifier is one launched by the security manager of the |
|
52 // protocol stack. These notifiers need to be served unless really not possible. |
|
53 // --------------------------------------------------------------------------- |
|
54 // |
|
55 TBool IsStackSecmanNotifier( TInt aUid ) |
|
56 { |
|
57 TBool result = EFalse; |
|
58 if( aUid == KBTManAuthNotifierUid.iUid || aUid == KBTManPinNotifierUid.iUid || |
|
59 aUid == KBTPinCodeEntryNotifierUid.iUid || aUid == KBTNumericComparisonNotifierUid.iUid || |
|
60 aUid == KBTPasskeyDisplayNotifierUid.iUid ) |
|
61 { |
|
62 result = ETrue; |
|
63 } |
|
64 return result; |
|
65 } |
|
66 |
|
67 |
|
68 // ======== MEMBER FUNCTIONS ======== |
|
69 |
|
70 // --------------------------------------------------------------------------- |
|
71 // C++ default constructor |
|
72 // --------------------------------------------------------------------------- |
|
73 // |
|
74 CBTNotifConnectionTracker::CBTNotifConnectionTracker( CBTNotifServer* aServer ) |
|
75 : iServer( aServer ) |
|
76 { |
|
77 } |
|
78 |
|
79 |
|
80 // --------------------------------------------------------------------------- |
|
81 // Symbian 2nd-phase constructor |
|
82 // --------------------------------------------------------------------------- |
|
83 // |
|
84 void CBTNotifConnectionTracker::ConstructL() |
|
85 { |
|
86 BOstraceFunctionEntry0( DUMMY_DEVLIST ); |
|
87 // Start watching the number of baseband links. |
|
88 TInt err = iLinkCount.Attach( KPropertyUidBluetoothCategory, |
|
89 KPropertyKeyBluetoothGetPHYCount ); |
|
90 // There is not much point to continue if we can't attach to |
|
91 // the link count key. |
|
92 User::LeaveIfError( err ); |
|
93 iLinkCountActive = CBtSimpleActive::NewL( *this, KLinkCountWatcher ); |
|
94 iLinkCount.Subscribe( iLinkCountActive->RequestStatus() ); |
|
95 iLinkCountActive->GoActive(); |
|
96 // Open a handle to the registry server |
|
97 User::LeaveIfError( iBTRegistrySession.Connect() ); |
|
98 // Open a handle to the socket server |
|
99 User::LeaveIfError( iSockServ.Connect() ); |
|
100 iPairingServ = new( ELeave ) RBluetoothPairingServer(); |
|
101 if( iPairingServ->Connect() ) |
|
102 { |
|
103 // Delete in case of error - there is no good other way to keep track. |
|
104 delete iPairingServ; |
|
105 iPairingServ = NULL; |
|
106 } |
|
107 else |
|
108 { |
|
109 iSspResultActive = CBtSimpleActive::NewL( *this, KSspResultWatcher ); |
|
110 User::LeaveIfError( iSspResultSession.Open( *iPairingServ ) ); |
|
111 iSspResultSession.SimplePairingResult( iSspResultAddr, iSspResultActive->RequestStatus() ); |
|
112 iSspResultActive->GoActive(); |
|
113 } |
|
114 iConnMan = CBTEngConnMan::NewL( this ); |
|
115 iPhyLinks = CBluetoothPhysicalLinks::NewL( *this, iSockServ ); |
|
116 // ToDo: remove this when registry notifications API is available!! |
|
117 err = iRegistryChange.Attach( KPropertyUidBluetoothCategory, KPropertyKeyBluetoothRegistryTableChange ); |
|
118 User::LeaveIfError( err ); |
|
119 iRegistryActive = CBtSimpleActive::NewL( *this, KRegistryWatcher ); |
|
120 iRegistryChange.Subscribe( iRegistryActive->RequestStatus() ); |
|
121 iRegistryActive->GoActive(); |
|
122 // End ToDo |
|
123 BOstraceFunctionExit0( DUMMY_DEVLIST ); |
|
124 } |
|
125 |
|
126 |
|
127 // --------------------------------------------------------------------------- |
|
128 // NewL. |
|
129 // --------------------------------------------------------------------------- |
|
130 // |
|
131 CBTNotifConnectionTracker* CBTNotifConnectionTracker::NewL( CBTNotifServer* aServer ) |
|
132 { |
|
133 CBTNotifConnectionTracker* self = new( ELeave ) CBTNotifConnectionTracker( aServer ); |
|
134 CleanupStack::PushL( self ); |
|
135 self->ConstructL(); |
|
136 CleanupStack::Pop( self ); |
|
137 return self; |
|
138 } |
|
139 |
|
140 |
|
141 // --------------------------------------------------------------------------- |
|
142 // Destructor |
|
143 // --------------------------------------------------------------------------- |
|
144 // |
|
145 CBTNotifConnectionTracker::~CBTNotifConnectionTracker() |
|
146 { |
|
147 BOstraceFunctionEntry0( DUMMY_DEVLIST ); |
|
148 iConnArray.ResetAndDestroy(); |
|
149 iConnArray.Close(); |
|
150 iDeniedRequests.Close(); |
|
151 delete iLinkCountActive; |
|
152 iLinkCount.Close(); |
|
153 |
|
154 delete iConnMan; |
|
155 delete iPhyLinks; |
|
156 iSockServ.Close(); |
|
157 delete iSspResultActive; |
|
158 iSspResultSession.Close(); |
|
159 if( iPairingServ ) |
|
160 { |
|
161 iPairingServ->Close(); |
|
162 delete iPairingServ; |
|
163 } |
|
164 delete iRegistryActive; |
|
165 iRegistryChange.Close(); |
|
166 iBTRegistrySession.Close(); |
|
167 BOstraceFunctionExit0( DUMMY_DEVLIST ); |
|
168 } |
|
169 |
|
170 // --------------------------------------------------------------------------- |
|
171 // Process a client message related to notifiers. |
|
172 // --------------------------------------------------------------------------- |
|
173 // |
|
174 void CBTNotifConnectionTracker::DispatchNotifierMessageL( const RMessage2& aMessage ) |
|
175 { |
|
176 BOstraceFunctionEntryExt ( DUMMY_LIST, this, aMessage.Function() ); |
|
177 TInt opcode = aMessage.Function(); |
|
178 TInt uid = aMessage.Int0(); |
|
179 const RMessage2* message = &aMessage; |
|
180 // Use a pointer to the original message, so that we don't duplicate it. |
|
181 // Then we avoid any bookkeeping for keeping them in sync. |
|
182 if( opcode == EBTNotifCancelNotifier ) |
|
183 { |
|
184 // We only accept a cancel message from the same session as the original |
|
185 // request (this is enforced by the RNotifier backend). So we use the |
|
186 // session of the cancel request (if this would change, the same way as |
|
187 // for updates can be followed). |
|
188 // We need to find the original request to identify the handler of the |
|
189 // connection; the uid points to the original request. |
|
190 message = ( (CBTNotifSession *) aMessage.Session() )->FindMessageFromUid( uid ); |
|
191 } |
|
192 else if( opcode == EBTNotifUpdateNotifier ) |
|
193 { |
|
194 // We accept a update messages from any client, although in practice, |
|
195 // they will all come from the same session (through RNotifier). |
|
196 // We need to find the original request to identify the handler of the |
|
197 // connection (the uid points to the original request). Through the |
|
198 // server, we get it from any session. |
|
199 message = iServer->FindMessageFromUid( uid ); |
|
200 } |
|
201 if( !message ) |
|
202 { |
|
203 // It's hard to continue if we don't know where to route the message. |
|
204 User::Leave( KErrDisconnected ); |
|
205 } |
|
206 TBuf8<0x250> paramsBuf; // Size needs to be long enough to read all possible parameter sizes. |
|
207 CBTNotifConnection* connection = FindConnectionFromMessageL( opcode, *message, paramsBuf ); |
|
208 if( !connection ) |
|
209 { |
|
210 User::Leave( KErrDisconnected ); |
|
211 } |
|
212 switch( opcode ) |
|
213 { |
|
214 case EBTNotifStartSyncNotifier: |
|
215 case EBTNotifStartAsyncNotifier: |
|
216 connection->HandleNotifierRequestL( paramsBuf, aMessage ); |
|
217 break; |
|
218 case EBTNotifUpdateNotifier: |
|
219 connection->HandleNotifierUpdateL( paramsBuf, aMessage ); |
|
220 break; |
|
221 case EBTNotifCancelNotifier: |
|
222 // Complete the cancel message already here, so that the caller can |
|
223 // continue, and the next operation can close sessions with the caller. |
|
224 aMessage.Complete( KErrNone ); |
|
225 connection->CancelNotifierRequestL( *message ); |
|
226 break; |
|
227 default: |
|
228 break; |
|
229 } |
|
230 BOstraceFunctionExit1( DUMMY_DEVLIST, this ); |
|
231 } |
|
232 |
|
233 |
|
234 // --------------------------------------------------------------------------- |
|
235 // Handle a request related to pairing. |
|
236 // --------------------------------------------------------------------------- |
|
237 // |
|
238 void CBTNotifConnectionTracker::HandleBondingRequestL( const RMessage2& aMessage ) |
|
239 { |
|
240 BOstraceFunctionEntryExt ( DUMMY_LIST, this, aMessage.Function() ); |
|
241 // Bonding is an infrequently occurring operation, so we don't waste memory |
|
242 // to keep a copy of the parameters. Instead we read them again when needed. |
|
243 TPckgBuf<TBTDevAddr> addrBuf; |
|
244 TInt opcode = aMessage.Function(); |
|
245 if( opcode == EBTEngPairDevice ) |
|
246 { |
|
247 aMessage.ReadL( EBTNotifSrvParamSlot, addrBuf ); |
|
248 } |
|
249 else if( opcode == EBTEngCancelPairDevice ) |
|
250 { |
|
251 const RMessage2* message = |
|
252 ( (CBTNotifSession *) aMessage.Session() )->FindMessageFromUid( EBTEngPairDevice ); |
|
253 message->ReadL( EBTNotifSrvParamSlot, addrBuf ); |
|
254 } |
|
255 BtTraceBtAddr1( TRACE_DEBUG, DUMMY_LIST, "CBTNotifConnectionTracker::HandleBondingRequestL() addr=", addrBuf() ); |
|
256 TInt err = KErrNotFound; |
|
257 CBTNotifConnection* connection = FindConnectionHandler( addrBuf() ); |
|
258 if( opcode == EBTEngPairDevice ) |
|
259 { |
|
260 if( !connection ) |
|
261 { |
|
262 // Create a connection first, then tell it to bond. |
|
263 err = iPhyLinks->CreateConnection( addrBuf() ); |
|
264 connection = CBTNotifConnection::NewLC( addrBuf(), this ); |
|
265 iConnArray.AppendL( connection ); |
|
266 CleanupStack::Pop( connection ); |
|
267 } |
|
268 else |
|
269 { |
|
270 // There is an existing connection. Care must be taken, the connection |
|
271 // _should_ be disconnect first if this device is already paired, so that |
|
272 // we are sure that we don't mix up the state of the connection. |
|
273 RBTPhysicalLinkAdapter link; |
|
274 err = link.Open( iSockServ, addrBuf() ); |
|
275 TUint32 linkState = 0; |
|
276 if( !err ) |
|
277 { |
|
278 err = link.PhysicalLinkState( linkState ); |
|
279 } |
|
280 if( !err && linkState & ( ENotifyAuthenticationComplete | ENotifyEncryptionChangeOn ) ) |
|
281 { |
|
282 // For now, we just reject the request. |
|
283 err = KErrAlreadyExists; |
|
284 } |
|
285 link.Close(); |
|
286 } |
|
287 if( !err ) |
|
288 { |
|
289 // Start bonding immediately so that the connection object is in the right state. |
|
290 connection->StartBondingL( aMessage ); |
|
291 } |
|
292 } |
|
293 else if( opcode == EBTEngCancelPairDevice && connection ) |
|
294 { |
|
295 connection->CancelBondingL(); |
|
296 err = KErrNone; |
|
297 aMessage.Complete( err ); |
|
298 } |
|
299 // KErrNotFound is returned for a request to cancel pairing that has no connection. |
|
300 if( err ) |
|
301 { |
|
302 aMessage.Complete( err ); |
|
303 } |
|
304 BOstraceFunctionExit1( DUMMY_DEVLIST, this ); |
|
305 } |
|
306 |
|
307 |
|
308 // --------------------------------------------------------------------------- |
|
309 // Handle a change in the number of connections. |
|
310 // --------------------------------------------------------------------------- |
|
311 // |
|
312 void CBTNotifConnectionTracker::HandleLinkCountChangeL() |
|
313 { |
|
314 BOstraceFunctionEntry0( DUMMY_DEVLIST ); |
|
315 TInt linkCount = 0; |
|
316 User::LeaveIfError( iLinkCount.Get( linkCount ) ); |
|
317 if( linkCount ) |
|
318 { |
|
319 RBTDevAddrArray links; |
|
320 CleanupClosePushL( links ); |
|
321 User::LeaveIfError( iPhyLinks->Enumerate( links, 10 ) ); |
|
322 __ASSERT_ALWAYS( links.Count(), PanicServer( EBTNotifPanicBadState ) ); |
|
323 for( TInt i = iConnArray.Count() -1; i >= 0 ; i-- ) |
|
324 { |
|
325 // Loop backwards, as we may remove entries from the array. |
|
326 |
|
327 // First check the existing connections, and |
|
328 // remove disconnected links |
|
329 TBTDevAddr addr = iConnArray[i]->Address(); |
|
330 TInt pos = links.Find( addr ); |
|
331 if( pos > KErrNotFound ) |
|
332 { |
|
333 // The link we know is still connected, |
|
334 // remove the watceher from the array. |
|
335 links.Remove( pos ); |
|
336 // ToDo: see comment below! |
|
337 } |
|
338 else if( iConnArray[i]->CurrentOperation() == CBTNotifConnection::EIdle ) |
|
339 { |
|
340 // This link is no more connected and idle, remove. |
|
341 CBTNotifConnection* connection = iConnArray[i]; |
|
342 iConnArray.Remove( i ); // Does not delete the object. |
|
343 delete connection; |
|
344 } |
|
345 // else we wait for the link to complete its operations. |
|
346 } |
|
347 // Now we have an array with only the new connections. |
|
348 // Add new watchers. |
|
349 for( TInt i = 0; i < links.Count(); i++ ) |
|
350 { |
|
351 CBTNotifConnection* connection = CBTNotifConnection::NewLC( links[i], this ); |
|
352 iConnArray.AppendL( connection ); |
|
353 CleanupStack::Pop( connection ); |
|
354 } |
|
355 // Close the links RBTDevAddrArray, needed before going out of scope. |
|
356 CleanupStack::PopAndDestroy(); |
|
357 } |
|
358 else |
|
359 { |
|
360 for( TInt i = iConnArray.Count() -1; i >= 0 ; i-- ) |
|
361 { |
|
362 if( iConnArray[i]->CurrentOperation() == CBTNotifConnection::EIdle ) |
|
363 { |
|
364 // This link is now idle, so we can remove it safely. |
|
365 CBTNotifConnection* connection = iConnArray[i]; |
|
366 iConnArray.Remove( i ); // Does not delete the object. |
|
367 delete connection; |
|
368 } |
|
369 } |
|
370 if( !iConnArray.Count() ) |
|
371 { |
|
372 // The array is idle, clean up the array resources. |
|
373 iConnArray.Reset(); |
|
374 } |
|
375 } |
|
376 BOstraceFunctionExit0( DUMMY_DEVLIST ); |
|
377 } |
|
378 |
|
379 |
|
380 // --------------------------------------------------------------------------- |
|
381 // Check if this device has been denied a connection already before. |
|
382 // Also check if a previous connection attempt has just been rejected. |
|
383 // --------------------------------------------------------------------------- |
|
384 // |
|
385 TBool CBTNotifConnectionTracker::UpdateBlockingHistoryL( const CBTDevice* aDevice, |
|
386 TBool aAccepted ) |
|
387 { |
|
388 BOstraceFunctionEntry0( DUMMY_DEVLIST ); |
|
389 __ASSERT_ALWAYS( aDevice, PanicServer( EBTNotifPanicBadArgument ) ); |
|
390 // Check the time since the previous event. |
|
391 TBool result = RecordConnectionAttempts( aAccepted ); |
|
392 TInt pos = iDeniedRequests.Find( aDevice->BDAddr() ); |
|
393 if( !aAccepted ) |
|
394 { |
|
395 if( pos == KErrNotFound ) |
|
396 { |
|
397 // The user denied the request from a new device, record the device address. |
|
398 if( aDevice->IsValidPaired() && aDevice->IsPaired() ) |
|
399 //[MCL]: && iDevice->LinkKeyType() != ELinkKeyUnauthenticatedUpgradable ) |
|
400 { |
|
401 // Paired devices are allowed one time rejection without a prompt for blocking. |
|
402 result = EFalse; |
|
403 } |
|
404 iDeniedRequests.AppendL( aDevice->BDAddr() ); |
|
405 } |
|
406 // Nothing needed here if the address is already in the array. |
|
407 } |
|
408 else if( pos > KErrNotFound ) |
|
409 { |
|
410 // The user accepted a request, and it was from a device he/she |
|
411 // previously rejected. Clear the history for this device from the array. |
|
412 iDeniedRequests.Remove( pos ); |
|
413 } |
|
414 BOstraceFunctionExit0( DUMMY_DEVLIST ); |
|
415 return result; |
|
416 } |
|
417 |
|
418 |
|
419 // --------------------------------------------------------------------------- |
|
420 // From class MBluetoothPhysicalLinksNotifier. |
|
421 // Handle baseband connection completion. |
|
422 // --------------------------------------------------------------------------- |
|
423 // |
|
424 void CBTNotifConnectionTracker::HandleCreateConnectionCompleteL( TInt aErr ) |
|
425 { |
|
426 BOstraceFunctionEntryExt ( DUMMY_LIST, this, aErr ); |
|
427 // We only connect links for starting outgoing bonding. |
|
428 const RMessage2* message = iServer->FindMessageFromUid( (TInt) EBTEngPairDevice ); |
|
429 if( message ) |
|
430 { |
|
431 TPckgBuf<TBTDevAddr> addrBuf; |
|
432 message->ReadL( EBTNotifSrvParamSlot, addrBuf ); |
|
433 CBTNotifConnection* connection = FindConnectionHandler( addrBuf() ); |
|
434 __ASSERT_ALWAYS( connection, PanicServer( EBTNotifPanicBadState ) ); |
|
435 if( !aErr && connection->CurrentOperation() == CBTNotifConnection::EIdle ) |
|
436 { |
|
437 TRAP( aErr, connection->StartBondingL( *message ) ); |
|
438 } |
|
439 if( aErr && connection->CurrentOperation() == CBTNotifConnection::EBonding ) |
|
440 { |
|
441 connection->PairingResult( aErr ); // Launch error note |
|
442 } |
|
443 } |
|
444 BOstraceFunctionExit1( DUMMY_DEVLIST, this ); |
|
445 } |
|
446 |
|
447 |
|
448 // --------------------------------------------------------------------------- |
|
449 // From class MBluetoothPhysicalLinksNotifier. |
|
450 // Handle baseband disconnection. |
|
451 // --------------------------------------------------------------------------- |
|
452 // |
|
453 void CBTNotifConnectionTracker::HandleDisconnectCompleteL( TInt aErr ) |
|
454 { |
|
455 BOstraceFunctionEntry0( DUMMY_DEVLIST ); |
|
456 // We only disconnect links for starting outgoing bonding. |
|
457 const RMessage2* message = iServer->FindMessageFromUid( (TInt) EBTEngPairDevice ); |
|
458 if( message ) |
|
459 { |
|
460 TPckgBuf<TBTDevAddr> addrBuf; |
|
461 message->ReadL( EBTNotifSrvParamSlot, addrBuf ); |
|
462 if( !aErr ) |
|
463 { |
|
464 aErr = iPhyLinks->CreateConnection( addrBuf() ); |
|
465 } |
|
466 if( aErr ) |
|
467 { |
|
468 iServer->CompleteMessage( message->Handle(), aErr, KNullDesC8 ); |
|
469 CBTNotifConnection* connection = FindConnectionHandler( addrBuf() ); |
|
470 __ASSERT_ALWAYS( connection, PanicServer( EBTNotifPanicBadState ) ); |
|
471 connection->PairingResult( aErr ); // Launch error note |
|
472 } |
|
473 } |
|
474 BOstraceFunctionExit0( DUMMY_DEVLIST ); |
|
475 } |
|
476 |
|
477 |
|
478 // --------------------------------------------------------------------------- |
|
479 // From class MBluetoothPhysicalLinksNotifier. |
|
480 // Handle disconnection of all links. |
|
481 // --------------------------------------------------------------------------- |
|
482 // |
|
483 void CBTNotifConnectionTracker::HandleDisconnectAllCompleteL( TInt aErr ) |
|
484 { |
|
485 (void) aErr; |
|
486 } |
|
487 |
|
488 |
|
489 // --------------------------------------------------------------------------- |
|
490 // From class MBTEngConnObserver. |
|
491 // Handle service-level connection completion. |
|
492 // --------------------------------------------------------------------------- |
|
493 // |
|
494 void CBTNotifConnectionTracker::ConnectComplete( TBTDevAddr& aAddr, |
|
495 TInt aErr, RBTDevAddrArray* aConflicts ) |
|
496 { |
|
497 (void) aAddr; |
|
498 (void) aErr; |
|
499 (void) aConflicts; |
|
500 } |
|
501 |
|
502 |
|
503 // --------------------------------------------------------------------------- |
|
504 // From class MBTEngConnObserver. |
|
505 // Handle service-level disconnection. |
|
506 // --------------------------------------------------------------------------- |
|
507 // |
|
508 void CBTNotifConnectionTracker::DisconnectComplete( TBTDevAddr& aAddr, TInt aErr ) |
|
509 { |
|
510 (void) aAddr; |
|
511 (void) aErr; |
|
512 } |
|
513 |
|
514 |
|
515 // --------------------------------------------------------------------------- |
|
516 // From class MBtSimpleActiveObserver. |
|
517 // Handle the active object completion. |
|
518 // --------------------------------------------------------------------------- |
|
519 // |
|
520 void CBTNotifConnectionTracker::RequestCompletedL( CBtSimpleActive* aActive, |
|
521 TInt aStatus ) |
|
522 { |
|
523 BOstraceFunctionEntryExt ( DUMMY_LIST, this, aActive->RequestId() ); |
|
524 BOstraceExt2( TRACE_DEBUG, DUMMY_DEVLIST, |
|
525 "CBTNotifConnectionTracker::MBAORequestCompletedL() requestid=%d status=%d", |
|
526 aActive->RequestId(), aStatus); |
|
527 if( aActive->RequestId() == KLinkCountWatcher ) |
|
528 { |
|
529 iLinkCount.Subscribe( aActive->RequestStatus() ); |
|
530 aActive->GoActive(); |
|
531 if( !aStatus ) |
|
532 { |
|
533 // HandleLinkCountChangeL(); |
|
534 } |
|
535 } |
|
536 // ToDo: remove this when registry notifications API is available!! |
|
537 else if( aActive->RequestId() == KRegistryWatcher ) |
|
538 { |
|
539 // BTRegistry notifies of a change. Check which one. |
|
540 iRegistryChange.Subscribe( aActive->RequestStatus() ); |
|
541 aActive->GoActive(); |
|
542 TInt tableChanged = 0; |
|
543 if( !aStatus && !iRegistryChange.Get( tableChanged ) && |
|
544 tableChanged == KRegistryChangeRemoteTable ) |
|
545 { |
|
546 // A record for a remote device has changed. Tell all |
|
547 // connections to update their record. |
|
548 for( TInt i = 0; i < iConnArray.Count(); i++ ) |
|
549 { |
|
550 // Reuse the functionality in the connection |
|
551 if( iConnArray[i]->CurrentOperation() < CBTNotifConnection::EReadingRegistry ) |
|
552 { |
|
553 iConnArray[i]->RequestCompletedL( aActive, aStatus ); |
|
554 } |
|
555 } |
|
556 } |
|
557 } |
|
558 // End ToDo |
|
559 else if( aActive->RequestId() == KSspResultWatcher ) |
|
560 { |
|
561 iSspResultSession.SimplePairingResult( iSspResultAddr, iSspResultActive->RequestStatus() ); |
|
562 iSspResultActive->GoActive(); |
|
563 CBTNotifConnection* connection = FindConnectionHandler( iSspResultAddr ); |
|
564 // ToDo: how to handle a result of a link that already disconnected? |
|
565 if( connection ) |
|
566 { |
|
567 connection->PairingResult( aStatus ); |
|
568 } |
|
569 } |
|
570 BOstraceFunctionExit1( DUMMY_DEVLIST, this ); |
|
571 } |
|
572 |
|
573 |
|
574 // --------------------------------------------------------------------------- |
|
575 // From class MBtSimpleActiveObserver. |
|
576 // Cancel and clean up all requests related to the active object. |
|
577 // --------------------------------------------------------------------------- |
|
578 // |
|
579 void CBTNotifConnectionTracker::CancelRequest( TInt aRequestId ) |
|
580 { |
|
581 BOstraceFunctionEntry0( DUMMY_DEVLIST ); |
|
582 if( aRequestId == KLinkCountWatcher ) |
|
583 { |
|
584 iLinkCount.Cancel(); |
|
585 } |
|
586 else if( aRequestId == KSspResultWatcher ) |
|
587 { |
|
588 iSspResultSession.CancelSimplePairingResult(); |
|
589 } |
|
590 else if ( aRequestId == KRegistryWatcher ) |
|
591 { |
|
592 iRegistryChange.Cancel(); |
|
593 } |
|
594 BOstraceFunctionExit0( DUMMY_DEVLIST ); |
|
595 } |
|
596 |
|
597 // --------------------------------------------------------------------------- |
|
598 // From class MBtSimpleActiveObserver. |
|
599 // |
|
600 // --------------------------------------------------------------------------- |
|
601 // |
|
602 void CBTNotifConnectionTracker::HandleError( CBtSimpleActive* aActive, |
|
603 TInt aError ) |
|
604 { |
|
605 (void) aActive; |
|
606 (void) aError; |
|
607 } |
|
608 |
|
609 // --------------------------------------------------------------------------- |
|
610 // Parse the details from a client message and find the associated handler. |
|
611 // --------------------------------------------------------------------------- |
|
612 // |
|
613 CBTNotifConnection* CBTNotifConnectionTracker::FindConnectionFromMessageL( |
|
614 TInt aOpcode, const RMessage2& aMessage, TDes8& aBuffer ) |
|
615 { |
|
616 BOstraceFunctionEntry1( DUMMY_DEVLIST, this ); |
|
617 TInt uid = aMessage.Int0(); |
|
618 aMessage.ReadL( EBTNotifSrvParamSlot, aBuffer ); |
|
619 TBTDevAddr addr = ParseAddressL( uid, aBuffer ); |
|
620 // If this is a |
|
621 CBTNotifConnection* connection = FindConnectionHandler( addr ); |
|
622 if( !connection && IsStackSecmanNotifier( uid ) && |
|
623 ( aOpcode == EBTNotifStartAsyncNotifier || aOpcode == EBTNotifStartSyncNotifier ) ) |
|
624 { |
|
625 // A notifier from stack. This happens if e.g. the pairing |
|
626 // request comes in before the link count changes (like security |
|
627 // mode 3). Create the handler and queue the request. |
|
628 // And note that |
|
629 connection = CBTNotifConnection::NewLC( addr, this ); |
|
630 iConnArray.AppendL( connection ); |
|
631 CleanupStack::Pop( connection ); |
|
632 } |
|
633 BOstraceFunctionExitExt( DUMMY_DEVLIST, this, connection ); |
|
634 return connection; |
|
635 } |
|
636 |
|
637 |
|
638 // --------------------------------------------------------------------------- |
|
639 // read the address from a client message. |
|
640 // --------------------------------------------------------------------------- |
|
641 // |
|
642 TBTDevAddr CBTNotifConnectionTracker::ParseAddressL( TInt aUid, |
|
643 const TDesC8& aParamsBuf ) const |
|
644 { |
|
645 BOstraceFunctionEntry1( DUMMY_DEVLIST, this ); |
|
646 TBTDevAddr addr; |
|
647 if( IsStackSecmanNotifier( aUid ) ) |
|
648 { |
|
649 // For all these, the address is the first data member, |
|
650 // so can be read using the TBTNotifierParams data structure. |
|
651 TBTNotifierParams params; |
|
652 TPckgC<TBTNotifierParams> paramsPckg( params ); |
|
653 paramsPckg.Set( aParamsBuf ); |
|
654 addr = paramsPckg().iBDAddr; |
|
655 } |
|
656 //else if( ) other notifier types |
|
657 BOstraceFunctionExitExt( DUMMY_DEVLIST, this, &addr ); |
|
658 return addr; |
|
659 } |
|
660 |
|
661 |
|
662 // --------------------------------------------------------------------------- |
|
663 // Find a specific connection. |
|
664 // --------------------------------------------------------------------------- |
|
665 // |
|
666 CBTNotifConnection* CBTNotifConnectionTracker::FindConnectionHandler( |
|
667 const TBTDevAddr& aAddr ) const |
|
668 { |
|
669 BOstraceFunctionEntry1( DUMMY_DEVLIST, this ); |
|
670 CBTNotifConnection* conn = NULL; |
|
671 if( aAddr != TBTDevAddr() ) |
|
672 { |
|
673 // This may be replaced by RArray::Find with appropriate key |
|
674 for( TInt i = 0; i < iConnArray.Count(); i++ ) |
|
675 { |
|
676 if( iConnArray[i]->Address() == aAddr ) |
|
677 { |
|
678 conn = iConnArray[i]; |
|
679 break; |
|
680 } |
|
681 } |
|
682 } |
|
683 BOstraceFunctionExitExt( DUMMY_DEVLIST, this, conn ); |
|
684 return conn; |
|
685 } |
|
686 |
|
687 |
|
688 // --------------------------------------------------------------------------- |
|
689 // Record and check the time between connection attempts. |
|
690 // --------------------------------------------------------------------------- |
|
691 // |
|
692 TBool CBTNotifConnectionTracker::RecordConnectionAttempts( TBool aAccepted ) |
|
693 { |
|
694 BOstraceFunctionEntry1( DUMMY_DEVLIST, this ); |
|
695 TBool result = ETrue; |
|
696 TTime now( 0 ); |
|
697 if( !aAccepted ) |
|
698 { |
|
699 now.UniversalTime(); |
|
700 if( iLastReject ) |
|
701 { |
|
702 // Check the time between denied connections, that it does not go too fast. |
|
703 TTimeIntervalSeconds prev( 0 ); |
|
704 if( !now.SecondsFrom( TTime( iLastReject ), prev ) ) |
|
705 { |
|
706 if( prev <= KDENYTHRESHOLD ) |
|
707 { |
|
708 // We are getting the requests too fast. Present the user with |
|
709 // an option to turn BT off. |
|
710 //iServer->SettingsTracker()->SetPower( EFalse ); |
|
711 result = EFalse; |
|
712 } |
|
713 } |
|
714 } |
|
715 } |
|
716 // Record the current timestamp. |
|
717 // It is reset in case the user accepted the request. |
|
718 iLastReject = now.Int64(); |
|
719 BOstraceFunctionExitExt( DUMMY_DEVLIST, this, result ); |
|
720 return result; |
|
721 } |