bluetoothengine/btnotif/src/BTNotifier.cpp
branchRCL_3
changeset 56 9386f31cc85b
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Declares Bluetooth notifiers base class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <centralrepository.h> 
       
    21 #include <CoreApplicationUIsSDKCRKeys.h>
       
    22 
       
    23 #include <settingsinternalcrkeys.h>
       
    24 #include <e32property.h>
       
    25 
       
    26 #include <btengsettings.h>
       
    27 #include <btengprivatecrkeys.h>
       
    28 #include <btengconnman.h>
       
    29 #include <btengutil.h>
       
    30 #include <StringLoader.h>    // Localisation stringloader
       
    31 #include <BTNotif.rsg>       // Own resources
       
    32 #include <featmgr.h>            // Feature Manager API
       
    33 #include <utf.h>             // CnvUtfConverter
       
    34 #include <bluetoothuiutil.h>
       
    35 #include "btnotifier.h"      // Own class definition
       
    36 #include "btNotifDebug.h"    // Debugging macros
       
    37 #include "btnotiflock.h"
       
    38 #include "btnotif.hrh"       // define MaxNameLength
       
    39 #include "btnotifpanic.h"
       
    40 #include "btnotifnameutils.h"
       
    41 
       
    42 #ifdef __SERIES60_HELP
       
    43 #include <hlplch.h>
       
    44 #include <csxhelp/bt.hlp.hrh> // The bt hrh info is needed, for help launching
       
    45 #endif
       
    46 
       
    47 // CONSTANTS
       
    48 const TInt KBTNAutolockEnabled = 1;
       
    49 
       
    50 // The following definitions are used to implement checking of connection/pairing
       
    51 // request frequency
       
    52 
       
    53 // if a new connection is made quicker than CONNECT_ADD_TIME after the old one, 
       
    54 // then the saved time is added with CONNECT_ADD_TIME.
       
    55 #define CONNECT_ADD_TIME TTimeIntervalSeconds(10)
       
    56 
       
    57 // if the saved time is later than now+DENY_THRESHOLD, start rejecting incoming connections
       
    58 // from unpaired devices.
       
    59 #define DENY_THRESHOLD	 TTimeIntervalSeconds(30)
       
    60 
       
    61 // if the user denies incoming connection the saved time goes this much in the future.
       
    62 #define REJECT_ADD_TIME TTimeIntervalSeconds(31)
       
    63 
       
    64 
       
    65 // ================= MEMBER FUNCTIONS =======================
       
    66 
       
    67 // ----------------------------------------------------------
       
    68 // CBTNotifierBase::CBTNotifierBase
       
    69 // C++ default constructor can NOT contain any code, that
       
    70 // might leave. Sets the AOs priority and puts
       
    71 // itself to the active scheduler stack.
       
    72 // ----------------------------------------------------------
       
    73 //
       
    74 CBTNotifierBase::CBTNotifierBase() 
       
    75     {
       
    76     }
       
    77 
       
    78 // ----------------------------------------------------------
       
    79 // CBTNotifierBase::ConstructL
       
    80 // Symbian 2nd phase constructor can leave.
       
    81 // Create registry object and open resource file.
       
    82 // ----------------------------------------------------------
       
    83 //
       
    84 void CBTNotifierBase::ConstructL()
       
    85     {
       
    86 	// Sets up TLS, must be done before FeatureManager is used.
       
    87 	FeatureManager::InitializeLibL();
       
    88 	iIsCoverUI = ( FeatureManager::FeatureSupported( KFeatureIdCoverDisplay ) ) 
       
    89                     ? ETrue : EFalse; 	
       
    90 	// Frees the TLS. Must be done after FeatureManager is used.
       
    91     FeatureManager::UnInitializeLib(); 
       
    92 
       
    93     iBTEngSettings = CBTEngSettings::NewL();
       
    94     iDevMan = CBTEngDevMan::NewL( this );
       
    95     iNotifUiUtil = CBTNotifUIUtil::NewL( iIsCoverUI );
       
    96     }
       
    97 
       
    98 // ----------------------------------------------------------
       
    99 // Destructor.
       
   100 // ----------------------------------------------------------
       
   101 //
       
   102 CBTNotifierBase::~CBTNotifierBase()
       
   103     {
       
   104     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::~CBTNotifierBase()"));    
       
   105     Cancel();
       
   106     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::~CBTNotifierBase() -- Done"));    
       
   107     }
       
   108 
       
   109 // ----------------------------------------------------------
       
   110 // CBTNotifierBase::Release
       
   111 // Release itself. Call to destructor.
       
   112 // ----------------------------------------------------------
       
   113 //
       
   114 void CBTNotifierBase::Release()
       
   115     {
       
   116     delete this;
       
   117     }
       
   118 
       
   119 
       
   120 // ----------------------------------------------------------
       
   121 // CBTNotifierBase::Info
       
   122 // Return registered information.
       
   123 // ----------------------------------------------------------
       
   124 //
       
   125 CBTNotifierBase::TNotifierInfo CBTNotifierBase::Info() const
       
   126     {
       
   127     return iInfo;
       
   128     }
       
   129 
       
   130 // ----------------------------------------------------------
       
   131 // CBTNotifierBase::StartL
       
   132 // Synchronic notifier launch. Does nothing here.
       
   133 // ----------------------------------------------------------
       
   134 //
       
   135 TPtrC8 CBTNotifierBase::StartL(const TDesC8& /*aBuffer*/)
       
   136     {
       
   137     TPtrC8 ret(KNullDesC8);
       
   138     return (ret);
       
   139     }
       
   140 
       
   141 // ----------------------------------------------------------
       
   142 // CBTNotifierBase::StartL
       
   143 // Asynchronic notifier launch.
       
   144 // ----------------------------------------------------------
       
   145 //
       
   146 void CBTNotifierBase::StartL(const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   147     {
       
   148     if( !iNotifUiUtil )
       
   149         {
       
   150         iNotifUiUtil = CBTNotifUIUtil::NewL( iIsCoverUI );    
       
   151         }
       
   152     
       
   153     TRAPD(err, GetParamsL(aBuffer, aReplySlot, aMessage));
       
   154     if (err)
       
   155         {
       
   156         CompleteMessage(err);
       
   157         }
       
   158     }
       
   159 
       
   160 // ----------------------------------------------------------
       
   161 // CBTNotifierBase::Cancel
       
   162 // Cancelling method.
       
   163 // ----------------------------------------------------------
       
   164 //
       
   165 void CBTNotifierBase::Cancel()
       
   166     {
       
   167     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::Cancel()"));    
       
   168 
       
   169     delete iNotifUiUtil;
       
   170     iNotifUiUtil = NULL;
       
   171     
       
   172 	delete iBTEngSettings;
       
   173     iBTEngSettings = NULL;
       
   174 
       
   175     delete iDevMan;
       
   176     iDevMan = NULL;
       
   177     
       
   178     delete iDevice;
       
   179     iDevice = NULL;
       
   180     
       
   181     if (iDeviceArray)
       
   182         {
       
   183         iDeviceArray->ResetAndDestroy();
       
   184         delete iDeviceArray;
       
   185         iDeviceArray = NULL;
       
   186         }
       
   187         
       
   188 	CompleteMessage(KErrCancel);        
       
   189     }
       
   190 
       
   191 // ----------------------------------------------------------
       
   192 // CBTNotifierBase::UpdateL
       
   193 // Notifier update. Not supported.
       
   194 // ----------------------------------------------------------
       
   195 //
       
   196 TPtrC8 CBTNotifierBase::UpdateL(const TDesC8& /*aBuffer*/)
       
   197     {
       
   198     TPtrC8 ret(KNullDesC8);
       
   199     return (ret);
       
   200     }
       
   201 
       
   202 
       
   203 // ----------------------------------------------------------
       
   204 // CBTNotifierBase::AutoLockOnL
       
   205 // ----------------------------------------------------------
       
   206 //
       
   207 TBool CBTNotifierBase::AutoLockOnL()
       
   208     {
       
   209     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AutoLockOnL()"));
       
   210 
       
   211     TBool result = EFalse;
       
   212     TInt status = KBTNAutolockEnabled;
       
   213     
       
   214     // Connecting and initialization:
       
   215 	CRepository* repository = CRepository::NewL(KCRUidSecuritySettings);
       
   216 		
       
   217 	repository->Get(KSettingsAutolockStatus, status);
       
   218 		
       
   219 	// Closing connection:
       
   220 	delete repository;
       
   221 		
       
   222     if ( status == KBTNAutolockEnabled )
       
   223         {
       
   224         result = ETrue;
       
   225         }
       
   226 
       
   227     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::AutoLockOnL() completed with %d"), result ));
       
   228 
       
   229     return result;
       
   230     }
       
   231 
       
   232 // ----------------------------------------------------------
       
   233 // CBTNotifierBase::CheckAndSetPowerOnL
       
   234 // The note or query to be shown depends on two SharedData flags
       
   235 // ----------------------------------------------------------
       
   236 //
       
   237 TBool CBTNotifierBase::CheckAndSetPowerOnL()
       
   238     {
       
   239     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::CheckAndSetPowerOnL()"));
       
   240 
       
   241 	TBTPowerStateValue powerState( EBTPowerOff );
       
   242     // Get current power status
       
   243     //
       
   244     User::LeaveIfError( iBTEngSettings->GetPowerState( powerState ) );
       
   245 
       
   246     // If power is off, turn it on
       
   247     //
       
   248     if ( powerState == EBTPowerOff )
       
   249         {
       
   250         TInt offlineModeOff( 1 );    // possible values are 0 and 1
       
   251         TInt activationEnabled( 0 ); // possible values are 0 and 1
       
   252         
       
   253         // Offline mode on?
       
   254         CRepository* repository = CRepository::NewL(KCRUidCoreApplicationUIs);
       
   255         repository->Get(KCoreAppUIsNetworkConnectionAllowed, offlineModeOff);
       
   256         delete repository;
       
   257         
       
   258         // BT Activation enabled?
       
   259         repository = CRepository::NewL(KCRUidBluetoothEngine);
       
   260         repository->Get(KBTEnabledInOffline, activationEnabled);
       
   261         delete repository;
       
   262         
       
   263         // Choose user interaction
       
   264         //
       
   265         if ( !offlineModeOff && !activationEnabled )
       
   266             {
       
   267             // Show user that feature is disabled:
       
   268             iNotifUiUtil->ShowInfoNoteL( R_BT_OFFLINE_DISABLED, ECmdShowBtOfflineDisableNote );
       
   269             return EFalse; // Bluetooth can't be set on.
       
   270             }
       
   271         else
       
   272             {
       
   273             // Choose query
       
   274             //
       
   275             TInt keypress( 0 );	
       
   276             if ( offlineModeOff )
       
   277                 {
       
   278                 keypress = iNotifUiUtil->ShowQueryL( KErrNone, R_BT_POWER_IS_OFF_QUERY, ECmdShowBtIsOffDlg );
       
   279                 }
       
   280             else
       
   281                 {
       
   282                 keypress = iNotifUiUtil->ShowQueryL( KErrNone, R_BT_ACTIVATE_IN_OFFLINE_QUERY, 
       
   283                         ECmdShowBtActivateInOfflineDlg );
       
   284 				}
       
   285 
       
   286             if( keypress )  // User answered YES
       
   287                 { 
       
   288                 //Check if the local name has been set. If not ask user to set name
       
   289                 
       
   290 				TBool ok = IsLocalNameModifiedL();
       
   291     			if( !ok )
       
   292     				{
       
   293       				ok = AskLocalBTNameQueryL();
       
   294     				}
       
   295                if ( ok )
       
   296 			       {  
       
   297                 	ok = ( iBTEngSettings->SetPowerState( EBTPowerOn ) ) ? EFalse : ETrue;
       
   298                 	}
       
   299                 return ok;
       
   300                 }
       
   301             else // User has cancelled the dialog
       
   302                 {
       
   303                 FLOG(_L("[BTNOTIF]\t CBTNotifierBase::CheckAndSetPowerOnL(), dialog canceled"));
       
   304                 return EFalse;
       
   305                 }                    
       
   306             }
       
   307         }
       
   308     return ETrue;
       
   309     }
       
   310     
       
   311 // ----------------------------------------------------------
       
   312 // CBTNotifierBase::AskLocalBTNameQueryL()
       
   313 // ----------------------------------------------------------
       
   314 //
       
   315 TBool CBTNotifierBase::AskLocalBTNameQueryL()
       
   316     {
       
   317 	FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() >>"));
       
   318 	
       
   319 	TBTDeviceName tempNameBuffer;
       
   320     // Cut the excess part of the name away. The query can handle only 
       
   321     // KBTUIMaxNameLength characters.
       
   322     //
       
   323     if( tempNameBuffer.Length() > KBTUIMaxNameLength )
       
   324      {
       
   325      tempNameBuffer.SetLength( KBTUIMaxNameLength );
       
   326      }
       
   327 
       
   328     //Get default name if given by e.g. phone product
       
   329     (void) RProperty::Get( KPropertyUidBluetoothCategory, 
       
   330                            KPropertyKeyBluetoothGetDeviceName, tempNameBuffer );
       
   331     
       
   332     if( !tempNameBuffer.Length() )
       
   333         {
       
   334         (void) RProperty::Get( KPropertyUidBluetoothCategory, 
       
   335                 KPropertyKeyBluetoothGetDeviceName, tempNameBuffer );
       
   336         }
       
   337     
       
   338     if( !tempNameBuffer.Length() )
       
   339         {
       
   340         RBTRegServ btRegServ;
       
   341         RBTLocalDevice btReg;
       
   342         TBTLocalDevice localDev;
       
   343 
       
   344         TInt err = btRegServ.Connect();
       
   345         if( !err )
       
   346             {
       
   347             err = btReg.Open( btRegServ );
       
   348             } 
       
   349         if( !err )
       
   350             {
       
   351             // Read the BT local name from BT Registry.
       
   352             err = btReg.Get( localDev );
       
   353             }
       
   354         if( !err )
       
   355             {
       
   356             // The error can be > 0 if there are unconverted characters.
       
   357             err = CnvUtfConverter::ConvertToUnicodeFromUtf8( tempNameBuffer, localDev.DeviceName() );
       
   358             }
       
   359         btReg.Close();
       
   360         btRegServ.Close();
       
   361         }
       
   362 
       
   363     TInt keypress = iNotifUiUtil->ShowTextInputQueryL( tempNameBuffer, 
       
   364             R_BT_ENTER_LOCAL_NAME_QUERY, ECmdBTnotifUnavailable );
       
   365     
       
   366     if( keypress )  // User has accepted the dialog
       
   367         {
       
   368         AknTextUtils::StripCharacters(tempNameBuffer,  KAknStripListControlChars);
       
   369         tempNameBuffer.TrimAll();   // Remove extra spaces
       
   370         // If name was full of invalid chars, it becomes empty after above cleanup. 
       
   371         if( tempNameBuffer.Length() ) 
       
   372             {
       
   373             TInt err = iBTEngSettings->SetLocalName( tempNameBuffer );
       
   374             FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() <<"));
       
   375             return (err) ? EFalse : ETrue;    
       
   376             }
       
   377         else
       
   378             {
       
   379             FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() << failed"));
       
   380             return EFalse;
       
   381             }
       
   382         }
       
   383     else
       
   384         {
       
   385         FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() << cancelled"));
       
   386         return EFalse;
       
   387         }
       
   388     }
       
   389 
       
   390 // ---------------------------------------------------------------------------------
       
   391 // CBTNotifierBase::IsExistingConnectionToAudioL
       
   392 // Check if there is any existing connection to audio profiles from the same device
       
   393 // ---------------------------------------------------------------------------------
       
   394 TBool CBTNotifierBase::IsExistingConnectionToAudioL( const TBTDevAddr& aDevAddr )
       
   395     {
       
   396     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsExistingConnectionToAudioL()"));
       
   397     CBTEngConnMan* connMan = CBTEngConnMan::NewL();
       
   398     TBTEngConnectionStatus conntatus( EBTEngNotConnected );
       
   399     (void) connMan->IsConnected(aDevAddr,conntatus);      
       
   400     delete connMan;
       
   401     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsExistingConnectionToAudioL() complete"));
       
   402     return ( conntatus==EBTEngConnected || conntatus==EBTEngConnecting );
       
   403     }
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // CBTNotifierBase::IsLocalNameModified
       
   407 // Checks from central repository whether the Bluetooth friendly name 
       
   408 // has been modified .
       
   409 // ---------------------------------------------------------------------------
       
   410 //
       
   411 TBool CBTNotifierBase::IsLocalNameModifiedL()
       
   412 	{
       
   413 	FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsLocalNameModified()"));   
       
   414     CRepository* cenRep = CRepository::NewL( KCRUidBTEngPrivateSettings );
       
   415     TInt val( EBTLocalNameDefault );
       
   416     (void) cenRep->Get( KBTLocalNameChanged, val );
       
   417     delete cenRep;
       
   418     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsLocalNameModified() complete"));
       
   419     return val == EBTLocalNameSet;
       
   420     }
       
   421 
       
   422 
       
   423 void CBTNotifierBase::GetDeviceFromRegL(const TBTDevAddr& aAddr)
       
   424     {
       
   425     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::GetDeviceFromRegL"));
       
   426     __ASSERT_DEBUG(iDevice, BTNOTIF_PANIC(EiDeviceNullWhenCallingGetDeviceFromRegL));
       
   427 
       
   428     TBTRegistrySearch mySearch;
       
   429     mySearch.FindAddress( aAddr );
       
   430     iDeviceArray = new (ELeave) CBTDeviceArray(1);
       
   431     if( !iDevMan )
       
   432         {
       
   433         iDevMan = CBTEngDevMan::NewL( this );    
       
   434         }
       
   435     TInt err = iDevMan->GetDevices( mySearch, iDeviceArray );
       
   436     if(err)
       
   437         {
       
   438         FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::GetDeviceFromRegL iDevMan->GetDevices error = %d"), err));
       
   439         DoHandleGetDevicesCompleteL(err, NULL);
       
   440         }
       
   441     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::GetDeviceFromRegL done"));
       
   442     }
       
   443 
       
   444 // ----------------------------------------------------------
       
   445 // CBTNotifierBase::QueryBlockDeviceL
       
   446 // Opens a query that returns wether or not user wants the device blocked
       
   447 // ----------------------------------------------------------
       
   448 void CBTNotifierBase::QueryBlockDeviceL()
       
   449     {
       
   450     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::QueryBlockDeviceL()"));
       
   451     __ASSERT_DEBUG( iNotifUiUtil->IsQueryReleased(), User::Panic( _L("CBTNotifierBase - iYesNoDlg not released!"), KErrAlreadyExists ) );
       
   452     
       
   453     TBTDeviceName bName;
       
   454     BtNotifNameUtils::GetDeviceDisplayName(bName, iDevice);
       
   455 
       
   456     HBufC* header= StringLoader::LoadLC( R_BT_BLOCK_DEVICE_HEADER );
       
   457 
       
   458     TInt resId = IsUserAwarePaired( iDevice->AsNamelessDevice() ) ? 
       
   459         R_BT_BLOCK_PAIRED_DEVICE_NOHELP : R_BT_BLOCK_DEVICE_NOHELP;
       
   460 	
       
   461     RBuf stringholder;
       
   462     stringholder.CleanupClosePushL();
       
   463     BluetoothUiUtil::LoadResourceAndSubstringL( stringholder, resId, bName, 0 );
       
   464 
       
   465 	TInt keypress = iNotifUiUtil->ShowMessageQueryL( stringholder, *header, 
       
   466 	        R_BT_GENERIC_MESSAGE_QUERY, CAknQueryDialog::EConfirmationTone );
       
   467     
       
   468     CleanupStack::PopAndDestroy(&stringholder);                   
       
   469     CleanupStack::PopAndDestroy(header);  
       
   470 
       
   471     if( keypress )// user replied "Yes"
       
   472         {
       
   473         DoBlockDevice();
       
   474         }
       
   475     else  // user replied "No" 
       
   476         {
       
   477         CompleteMessage(KErrCancel);        
       
   478         } 
       
   479     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::QueryBlockDeviceL() complete"));   
       
   480     }
       
   481 
       
   482 void CBTNotifierBase::DoBlockDevice()
       
   483     {
       
   484     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::DoBlockDevice()"));
       
   485     TBTDeviceSecurity deviceSecurity = iDevice->GlobalSecurity();
       
   486     deviceSecurity.SetBanned(ETrue);
       
   487     deviceSecurity.SetNoAuthenticate(EFalse);
       
   488     deviceSecurity.SetNoAuthorise(EFalse);
       
   489     iDevice->SetGlobalSecurity(deviceSecurity);
       
   490     iDevice->DeleteLinkKey();
       
   491  
       
   492     iBTRegistryQueryState = ESetDeviceBlocked;
       
   493     TInt err = KErrNone;
       
   494     if( !iDevMan )
       
   495         {
       
   496         TRAP(err, iDevMan = CBTEngDevMan::NewL( this ));    
       
   497         }
       
   498     if( !err )
       
   499         {
       
   500         err = iDevMan->ModifyDevice( *iDevice );
       
   501         }
       
   502     if( err )
       
   503         {
       
   504         // if error, complete message, otherwise waiting for devman callback
       
   505         CompleteMessage(err); 
       
   506         }
       
   507     }
       
   508 
       
   509 void CBTNotifierBase::ChangeAuthorizeState( TBool aTrust )
       
   510     {
       
   511     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::ChangeAuthorizeState()"));   
       
   512     TBTDeviceSecurity sec = iDevice->GlobalSecurity();
       
   513     sec.SetBanned(EFalse);
       
   514     sec.SetNoAuthorise( ( aTrust ) ? ETrue : EFalse ) ;
       
   515     iDevice->SetGlobalSecurity(sec);
       
   516  
       
   517     iBTRegistryQueryState = ESetDeviceAuthorizeState;
       
   518     TInt err = KErrNone;
       
   519     if( !iDevMan )
       
   520         {    
       
   521         TRAP(err, iDevMan = CBTEngDevMan::NewL( this ));
       
   522         }
       
   523     if( !err )
       
   524         {
       
   525         err = iDevMan->ModifyDevice( *iDevice );
       
   526         }
       
   527     if( err )
       
   528         {
       
   529         // if error, complete message, otherwise waiting for devman callback        
       
   530         CompleteMessage(err); 
       
   531         TBTNotifLockPublish::DeleteNotifLocks( 
       
   532                 EBTNotiferLockPairedDeviceSetting, iDevice->BDAddr() );
       
   533         }
       
   534     }
       
   535 
       
   536 // ----------------------------------------------------------
       
   537 // CBTNotifierBase::CheckAndHandleQueryIntervalL
       
   538 // ----------------------------------------------------------
       
   539 //
       
   540 void CBTNotifierBase::CheckAndHandleQueryIntervalL()
       
   541     {
       
   542     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval start." ) );
       
   543     TBool res = CheckQueryInterval();
       
   544     if( res )
       
   545         {
       
   546         FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval check on/off query status." ) );
       
   547         // Check if we're already showing Turn BT off query
       
   548         TInt queryValue=EBTQueryOff;
       
   549         RProperty::Get( KPSUidBluetoothEnginePrivateCategory, KBTTurnBTOffQueryOn, queryValue );
       
   550         if( queryValue==EBTQueryOff )
       
   551             {
       
   552             FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval asking to turn BT off." ) );
       
   553             RProperty::Set( KPSUidBluetoothEnginePrivateCategory, KBTTurnBTOffQueryOn, EBTQueryOn );
       
   554             TInt keypress = iNotifUiUtil->ShowQueryL( KErrNone, R_BT_TURN_BT_OFF_NOTE, ECmdBTnotifUnavailable );
       
   555             RProperty::Set( KPSUidBluetoothEnginePrivateCategory, KBTTurnBTOffQueryOn, EBTQueryOff );
       
   556             if( keypress )  // User has accepted the dialog
       
   557                 {
       
   558                 FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval turning BT off." ) );
       
   559                 (void) iBTEngSettings->SetPowerState( EBTPowerOff );
       
   560                 CompleteMessage( KErrAccessDenied );
       
   561                 }
       
   562             }
       
   563         }
       
   564     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckAndHandleQueryIntervalL done." ) );
       
   565     }
       
   566 
       
   567 void CBTNotifierBase::DoHandleGetDevicesCompleteL( TInt aErr, CBTDeviceArray* aDeviceArray)
       
   568     {
       
   569     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::DoHandleGetDevicesCompleteL(%d)"), aErr ));
       
   570     TBTDeviceName devName;
       
   571     if (!aErr && aDeviceArray && aDeviceArray->Count())
       
   572         {
       
   573         // Reassign so that we won't lose the device name as it is
       
   574         // needed if registry doesn't have the name for this device yet:
       
   575         CBTDevice* temp = iDevice;
       
   576         CleanupStack::PushL(temp);
       
   577         // Taking the object returned by registry so that iDevice have
       
   578         // all the up-to-date information except device name.
       
   579         iDevice = aDeviceArray->At(0);
       
   580         aDeviceArray->Delete( 0 );
       
   581         // we show user the device name from registry if it is available;
       
   582         // Otherwise, the name passed in by the notifier request shall be 
       
   583         // used.
       
   584         if( ( !iDevice->IsValidDeviceName() || 
       
   585               !iDevice->DeviceName().Length() ) && 
       
   586             temp->IsValidDeviceName() )
       
   587             {
       
   588             // We are using a stored device name, which will
       
   589             // already have been processed
       
   590             iDevice->SetDeviceNameL( temp->DeviceName() );
       
   591             }
       
   592         CleanupStack::PopAndDestroy(temp);
       
   593         }
       
   594     BtNotifNameUtils::GetDeviceName(devName, iDevice);   
       
   595     
       
   596     // It is possible that iDevice hasn't got a name so far. Use the default BT name
       
   597     // got from GetDeviceName().
       
   598     if ( !iDevice->IsValidDeviceName() || !iDevice->DeviceName().Length() )
       
   599         {
       
   600         BtNotifNameUtils::SetDeviceNameL(devName, *iDevice);
       
   601         }
       
   602     HandleGetDeviceCompletedL( iDevice ); 
       
   603     }
       
   604 
       
   605 // ----------------------------------------------------------
       
   606 // CBTNotifierBase::CompleteMessage
       
   607 // ----------------------------------------------------------
       
   608 //	
       
   609 void CBTNotifierBase::CompleteMessage(TInt aErr)
       
   610     {	   
       
   611 	if( !iMessage.IsNull() )
       
   612 	    {
       
   613 	    FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::CompleteMessage(%d)"), aErr ));
       
   614 	    iMessage.Complete( aErr );
       
   615 	    }
       
   616 	}
       
   617 
       
   618 // ----------------------------------------------------------
       
   619 // CBTNotifierBase::CompleteMessage
       
   620 // ----------------------------------------------------------
       
   621 //	
       
   622 void CBTNotifierBase::CompleteMessage(TInt aValueToReplySlot, TInt aErr)
       
   623     {
       
   624     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::CompleteMessage(%d, %d)"), 
       
   625             aValueToReplySlot, aErr ) );
       
   626     CompleteMessage(TPckgBuf<TInt>( aValueToReplySlot ), aErr);
       
   627 	}
       
   628 
       
   629 // ----------------------------------------------------------
       
   630 // CBTNotifierBase::CompleteMessage
       
   631 // ----------------------------------------------------------
       
   632 //	
       
   633 void CBTNotifierBase::CompleteMessage(const TDesC8& aDesToReplySlot, TInt aErr)
       
   634     {	   
       
   635 	if( !iMessage.IsNull() )
       
   636 	    {
       
   637 	    FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::CompleteMessage(TDesC8&, %d)"), aErr ) );
       
   638 	    if ( !aErr )
       
   639 	        {
       
   640 	        aErr = iMessage.Write( iReplySlot, aDesToReplySlot );
       
   641 	        }
       
   642 	    iMessage.Complete( aErr );
       
   643 	    }
       
   644 	}
       
   645 
       
   646 #ifdef __SERIES60_HELP    
       
   647 //-------------------------------------------------------------
       
   648 // CBTNotifierBase::LaunchHelp
       
   649 //--------------------------------------------------------------
       
   650 TInt CBTNotifierBase::LaunchHelp(TAny * tCoeHelpContext )
       
   651 	{	
       
   652 	TCoeHelpContext hc;
       
   653 	
       
   654 	if( tCoeHelpContext==NULL)
       
   655 		hc = TCoeHelpContext(KUidBTUI,KBT_HLP_BLOCKED);
       
   656 	else
       
   657 		hc = *static_cast<TCoeHelpContext*>(tCoeHelpContext);
       
   658 		 
       
   659 		CArrayFix< TCoeHelpContext >* array = new CArrayFixFlat< TCoeHelpContext >(1);
       
   660 	TRAPD(err,
       
   661 		{
       
   662 		CleanupStack::PushL(array);
       
   663 		array->AppendL(hc);		
       
   664 		HlpLauncher::LaunchHelpApplicationL( CCoeEnv::Static()->WsSession() , array );
       
   665     	CleanupStack::Pop(array);
       
   666 		});
       
   667 
       
   668     // the array is not deleted, since deleting it will result
       
   669     // to E32User-CBase 3. It is assumed that HlpLancher is taking care of it.
       
   670     // however this has not been documented.
       
   671     return err;
       
   672 	}
       
   673 #endif
       
   674 
       
   675 void CBTNotifierBase::HandleGetDevicesComplete( TInt aErr, CBTDeviceArray* aDeviceArray)
       
   676     {
       
   677     TRAP_IGNORE(DoHandleGetDevicesCompleteL(aErr, aDeviceArray));
       
   678     }
       
   679 
       
   680 void CBTNotifierBase::HandleDevManComplete(TInt aErr)
       
   681     {
       
   682     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::HandleDevManComplete()  aErr = %d"), aErr ));
       
   683     
       
   684     switch (iBTRegistryQueryState)
       
   685         {
       
   686         case ESetDeviceBlocked:
       
   687             {
       
   688             // Blocking device was demanded by user after the user
       
   689             // rejected incoming pairing or connect request. The message
       
   690             // to be completed here is the original pair or authorization request
       
   691             // which has been rejected by the user.
       
   692             CompleteMessage(KErrCancel);
       
   693             }
       
   694         case ESetDeviceAuthorizeState:
       
   695             {
       
   696             TBTNotifLockPublish::DeleteNotifLocks( 
       
   697                     EBTNotiferLockPairedDeviceSetting, iDevice->BDAddr() );
       
   698             CompleteMessage(aErr);
       
   699             break;
       
   700             }
       
   701         }
       
   702     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::HandleDevManComplete() Complete"));           
       
   703     }
       
   704 
       
   705 void CBTNotifierBase::HandleGetDeviceCompletedL(const CBTDevice* /*aDev*/)
       
   706     {    
       
   707     }
       
   708 
       
   709 // ----------------------------------------------------------
       
   710 // CBTNotifierBase::CheckQueryInterval
       
   711 // ----------------------------------------------------------
       
   712 //
       
   713 TBool CBTNotifierBase::CheckQueryInterval()
       
   714     {
       
   715     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval()" ) );
       
   716     TBool result = EFalse;
       
   717     // Check if the user gets too many queries within a certain time interval from unpaired devices
       
   718     if( iDevice && !iDevice->IsValidLinkKey() )
       
   719         {
       
   720         TBuf8<32> buf;
       
   721         TTime now;
       
   722         now.UniversalTime();    // Get current time
       
   723         TInt64 writeTime = ( now + REJECT_ADD_TIME ).Int64();
       
   724 
       
   725         // get last connection time
       
   726         TInt err = RProperty::Get( KPSUidBluetoothEnginePrivateCategory, KBTConnectionTimeStamp, buf );
       
   727         if( !err )
       
   728             {
       
   729             FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval Checking query interval." ) );
       
   730             // Parse recorded time stamp
       
   731             TInt64 num(0);
       
   732             TLex8 lex;
       
   733             lex = buf;
       
   734             lex.Val( num );
       
   735             TTime lastTime( num );
       
   736 
       
   737             // detection of clock adjusment
       
   738             TInt timetravel = 2 * REJECT_ADD_TIME.Int();
       
   739             if( lastTime > now + TTimeIntervalSeconds(timetravel) )
       
   740                 {
       
   741                 FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval  time travel detection." ) );
       
   742                 lastTime = now;
       
   743                 }
       
   744             // new request too quickly, move booked time
       
   745             if( lastTime + CONNECT_ADD_TIME > now )
       
   746                 {
       
   747                 FLOG( _L( "[BTNOTIF]\t CBTAuthNotifier::IsDosAttack  CONNECT_ADD_TIME added." ) );
       
   748                 writeTime = ( lastTime + CONNECT_ADD_TIME ).Int64();
       
   749                 }
       
   750 
       
   751             FTRACE( FPrint( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval() lastTime = %d" ), lastTime.Int64() ) );
       
   752             FTRACE( FPrint( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval now + treshold: %d" ), (now + DENY_THRESHOLD).Int64() ) );
       
   753             // If threshold is exceeded, ask user to turn Bluetooth OFF
       
   754             if( lastTime > now + DENY_THRESHOLD )
       
   755                 {
       
   756                 FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval threshold exceeded!." ) );
       
   757                 result = ETrue;
       
   758                 }
       
   759             }
       
   760         else
       
   761             {
       
   762             FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval setting connection timestamp first time." ) );
       
   763             }
       
   764         // Write back the timestamp
       
   765         buf.Num( writeTime );
       
   766         FTRACE( FPrint( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval updating timestamp: %d." ), writeTime ) );
       
   767         RProperty::Set( KPSUidBluetoothEnginePrivateCategory,KBTConnectionTimeStamp, buf );
       
   768         } // end of query interval check
       
   769     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval() complete" ) );
       
   770     return result;
       
   771     }
       
   772 // End of File