bluetoothengine/btnotif/btnotifsrv/src/btnotifconnectiontracker.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2010 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: Bluetooth connection tracker and manager.
       
    15 *
       
    16 */
       
    17 
       
    18 #include "btnotifconnectiontracker.h"
       
    19 #include <btextnotifiers.h>
       
    20 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    21 #include <btextnotifierspartner.h>
       
    22 #endif
       
    23 
       
    24 #include <btservices/btdevextension.h>
       
    25 #include <btservices/btdevrepository.h>
       
    26 
       
    27 #include "btnotifsession.h"
       
    28 #include "btnotifclientserver.h"
       
    29 #include "bluetoothtrace.h"
       
    30 #include "btnotifsecuritymanager.h"
       
    31 
       
    32 /**  Time window for determining if there are too many requests. */
       
    33 #ifndef __WINS__
       
    34 #define KDENYTHRESHOLD TTimeIntervalSeconds(3)
       
    35 #else   //__WINS__
       
    36 #define KDENYTHRESHOLD TTimeIntervalSeconds(5)
       
    37 #endif  //__WINS__
       
    38 
       
    39 
       
    40 // ======== LOCAL FUNCTIONS ========
       
    41 
       
    42 // ======== MEMBER FUNCTIONS ========
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // C++ default constructor
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 CBTNotifConnectionTracker::CBTNotifConnectionTracker( CBTNotifServer* aServer )
       
    49 :   iServer( aServer )
       
    50     {
       
    51     }
       
    52 
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // Symbian 2nd-phase constructor
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 void CBTNotifConnectionTracker::ConstructL()
       
    59     {
       
    60     BOstraceFunctionEntry0( DUMMY_DEVLIST );
       
    61     // Open a handle to the registry server
       
    62     User::LeaveIfError( iBTRegistrySession.Connect() );
       
    63     // Open a handle to the socket server
       
    64     User::LeaveIfError( iSockServ.Connect() );
       
    65     iPairingManager = CBTNotifSecurityManager::NewL(*this, iServer->DevRepository() );
       
    66     BOstraceFunctionExit0( DUMMY_DEVLIST );
       
    67     }
       
    68 
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // NewL.
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 CBTNotifConnectionTracker* CBTNotifConnectionTracker::NewL( CBTNotifServer* aServer )
       
    75     {
       
    76     CBTNotifConnectionTracker* self = new( ELeave ) CBTNotifConnectionTracker( aServer );
       
    77     CleanupStack::PushL( self );
       
    78     self->ConstructL();
       
    79     CleanupStack::Pop( self );
       
    80     return self;
       
    81     }
       
    82 
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // Destructor
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 CBTNotifConnectionTracker::~CBTNotifConnectionTracker()
       
    89     {
       
    90     BOstraceFunctionEntry0( DUMMY_DEVLIST );
       
    91     iDeniedRequests.Close();
       
    92     delete iPairingManager;
       
    93     iSockServ.Close();
       
    94     iBTRegistrySession.Close();
       
    95     BOstraceFunctionExit0( DUMMY_DEVLIST );
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // Process notifier message related to pairing notifiers.
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 void CBTNotifConnectionTracker::HandleNotifierRequestL( const RMessage2& aMessage )
       
   103     {
       
   104     BOstraceFunctionEntryExt ( DUMMY_LIST, this, aMessage.Function() );
       
   105     iPairingManager->HandleNotifierRequestL(aMessage);
       
   106     BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // Handle a request related to pairing.
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 void CBTNotifConnectionTracker::HandleBondingRequestL( const RMessage2& aMessage )
       
   114     {
       
   115     BOstraceFunctionEntryExt ( DUMMY_LIST, this, aMessage.Function() );
       
   116     iPairingManager->HandleBondingRequestL(aMessage);
       
   117     BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // Check if this device has been denied a connection already before.
       
   122 // Also check if a previous connection attempt has just been rejected.
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 TBool CBTNotifConnectionTracker::UpdateBlockingHistoryL( const CBTDevice* aDevice, 
       
   126     TBool aAccepted )
       
   127     {
       
   128     BOstraceFunctionEntry0( DUMMY_DEVLIST );
       
   129     __ASSERT_ALWAYS( aDevice, PanicServer( EBTNotifPanicBadArgument ) );
       
   130     // Check the time since the previous event.
       
   131     TBool result = RecordConnectionAttempts( aAccepted );
       
   132     TInt pos = iDeniedRequests.Find( aDevice->BDAddr() );
       
   133     if( !aAccepted )
       
   134         {
       
   135         if( pos == KErrNotFound )
       
   136             {
       
   137             // The user denied the request from a new device, record the device address.
       
   138             if( aDevice->IsValidPaired() && aDevice->IsPaired() )
       
   139                 //[MCL]: && iDevice->LinkKeyType() != ELinkKeyUnauthenticatedUpgradable )
       
   140                 {
       
   141                 // Paired devices are allowed one time rejection without a prompt for blocking.
       
   142                 result = EFalse;
       
   143                 }
       
   144             iDeniedRequests.AppendL( aDevice->BDAddr() );
       
   145             }
       
   146         // Nothing needed here if the address is already in the array.
       
   147         }
       
   148     else if( pos > KErrNotFound )
       
   149         {
       
   150         // The user accepted a request, and it was from a device he/she 
       
   151         // previously rejected. Clear the history for this device from the array.
       
   152         iDeniedRequests.Remove( pos );
       
   153         }
       
   154     BOstraceFunctionExit0( DUMMY_DEVLIST );
       
   155     return result;
       
   156     }
       
   157 
       
   158 // ---------------------------------------------------------------------------
       
   159 // Record and check the time between connection attempts.
       
   160 // ---------------------------------------------------------------------------
       
   161 //
       
   162 TBool CBTNotifConnectionTracker::RecordConnectionAttempts( TBool aAccepted )
       
   163     {
       
   164     BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
       
   165     TBool result = ETrue;
       
   166     TTime now( 0 );
       
   167     if( !aAccepted )
       
   168         {
       
   169         now.UniversalTime();
       
   170         if( iLastReject )
       
   171             {
       
   172             // Check the time between denied connections, that it does not go too fast.
       
   173             TTimeIntervalSeconds prev( 0 );
       
   174             if( !now.SecondsFrom( TTime( iLastReject ), prev ) )
       
   175                 {
       
   176                 if( prev <= KDENYTHRESHOLD )
       
   177                     {
       
   178                     // We are getting the requests too fast. Present the user with
       
   179                     // an option to turn BT off.
       
   180                     //iServer->SettingsTracker()->SetPower( EFalse );
       
   181                     result = EFalse;
       
   182                     }
       
   183                 }
       
   184             }
       
   185         }
       
   186     // Record the current timestamp.
       
   187     // It is reset in case the user accepted the request.
       
   188     iLastReject = now.Int64();
       
   189     BOstraceFunctionExitExt( DUMMY_DEVLIST, this, result );
       
   190     return result;
       
   191     }
       
   192