bluetoothengine/btnotif/src/BTNotifier.cpp
changeset 0 f63038272f30
child 6 6a29d5ad0713
child 15 00f9ee97d895
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     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 
       
    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 	TBool isCoverUI = ( 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( isCoverUI );
       
    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     TRAPD(err, GetParamsL(aBuffer, aReplySlot, aMessage));
       
   149     if (err)
       
   150         {
       
   151         CompleteMessage(err);
       
   152         }
       
   153     }
       
   154 
       
   155 // ----------------------------------------------------------
       
   156 // CBTNotifierBase::Cancel
       
   157 // Cancelling method.
       
   158 // ----------------------------------------------------------
       
   159 //
       
   160 void CBTNotifierBase::Cancel()
       
   161     {
       
   162     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::Cancel()"));    
       
   163 
       
   164     delete iNotifUiUtil;
       
   165     iNotifUiUtil = NULL;
       
   166     
       
   167 	delete iBTEngSettings;
       
   168     iBTEngSettings = NULL;
       
   169 
       
   170     delete iDevMan;
       
   171     iDevMan = NULL;
       
   172     
       
   173     delete iDevice;
       
   174     iDevice = NULL;
       
   175     
       
   176     if (iDeviceArray)
       
   177         {
       
   178         iDeviceArray->ResetAndDestroy();
       
   179         delete iDeviceArray;
       
   180         iDeviceArray = NULL;
       
   181         }
       
   182         
       
   183 	CompleteMessage(KErrCancel);        
       
   184     }
       
   185 
       
   186 // ----------------------------------------------------------
       
   187 // CBTNotifierBase::UpdateL
       
   188 // Notifier update. Not supported.
       
   189 // ----------------------------------------------------------
       
   190 //
       
   191 TPtrC8 CBTNotifierBase::UpdateL(const TDesC8& /*aBuffer*/)
       
   192     {
       
   193     TPtrC8 ret(KNullDesC8);
       
   194     return (ret);
       
   195     }
       
   196 
       
   197 
       
   198 // ----------------------------------------------------------
       
   199 // CBTNotifierBase::AutoLockOnL
       
   200 // ----------------------------------------------------------
       
   201 //
       
   202 TBool CBTNotifierBase::AutoLockOnL()
       
   203     {
       
   204     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AutoLockOnL()"));
       
   205 
       
   206     TBool result = EFalse;
       
   207     TInt status = KBTNAutolockEnabled;
       
   208     
       
   209     // Connecting and initialization:
       
   210 	CRepository* repository = CRepository::NewL(KCRUidSecuritySettings);
       
   211 		
       
   212 	repository->Get(KSettingsAutolockStatus, status);
       
   213 		
       
   214 	// Closing connection:
       
   215 	delete repository;
       
   216 		
       
   217     if ( status == KBTNAutolockEnabled )
       
   218         {
       
   219         result = ETrue;
       
   220         }
       
   221 
       
   222     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::AutoLockOnL() completed with %d"), result ));
       
   223 
       
   224     return result;
       
   225     }
       
   226 
       
   227 // ----------------------------------------------------------
       
   228 // CBTNotifierBase::CheckAndSetPowerOnL
       
   229 // The note or query to be shown depends on two SharedData flags
       
   230 // ----------------------------------------------------------
       
   231 //
       
   232 TBool CBTNotifierBase::CheckAndSetPowerOnL()
       
   233     {
       
   234     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::CheckAndSetPowerOnL()"));
       
   235 
       
   236 	TBTPowerStateValue powerState( EBTPowerOff );
       
   237     // Get current power status
       
   238     //
       
   239     User::LeaveIfError( iBTEngSettings->GetPowerState( powerState ) );
       
   240 
       
   241     // If power is off, turn it on
       
   242     //
       
   243     if ( powerState == EBTPowerOff )
       
   244         {
       
   245         TInt offlineModeOff( 1 );    // possible values are 0 and 1
       
   246         TInt activationEnabled( 0 ); // possible values are 0 and 1
       
   247         
       
   248         // Offline mode on?
       
   249         CRepository* repository = CRepository::NewL(KCRUidCoreApplicationUIs);
       
   250         repository->Get(KCoreAppUIsNetworkConnectionAllowed, offlineModeOff);
       
   251         delete repository;
       
   252         
       
   253         // BT Activation enabled?
       
   254         repository = CRepository::NewL(KCRUidBluetoothEngine);
       
   255         repository->Get(KBTEnabledInOffline, activationEnabled);
       
   256         delete repository;
       
   257         
       
   258         // Choose user interaction
       
   259         //
       
   260         if ( !offlineModeOff && !activationEnabled )
       
   261             {
       
   262             // Show user that feature is disabled:
       
   263             iNotifUiUtil->ShowInfoNoteL( R_BT_OFFLINE_DISABLED, ECmdShowBtOfflineDisableNote );
       
   264             return EFalse; // Bluetooth can't be set on.
       
   265             }
       
   266         else
       
   267             {
       
   268             // Choose query
       
   269             //
       
   270             TInt keypress( 0 );	
       
   271             if ( offlineModeOff )
       
   272                 {
       
   273                 keypress = iNotifUiUtil->ShowQueryL( KErrNone, R_BT_POWER_IS_OFF_QUERY, ECmdShowBtIsOffDlg );
       
   274                 }
       
   275             else
       
   276                 {
       
   277                 keypress = iNotifUiUtil->ShowQueryL( KErrNone, R_BT_ACTIVATE_IN_OFFLINE_QUERY, 
       
   278                         ECmdShowBtActivateInOfflineDlg );
       
   279 				}
       
   280 
       
   281             if( keypress )  // User answered YES
       
   282                 { 
       
   283                 //Check if the local name has been set. If not ask user to set name
       
   284                 
       
   285 				TBool ok = IsLocalNameModifiedL();
       
   286     			if( !ok )
       
   287     				{
       
   288       				ok = AskLocalBTNameQueryL();
       
   289     				}
       
   290                if ( ok )
       
   291 			       {  
       
   292                 	ok = ( iBTEngSettings->SetPowerState( EBTPowerOn ) ) ? EFalse : ETrue;
       
   293                 	}
       
   294                 return ok;
       
   295                 }
       
   296             else // User has cancelled the dialog
       
   297                 {
       
   298                 FLOG(_L("[BTNOTIF]\t CBTNotifierBase::CheckAndSetPowerOnL(), dialog canceled"));
       
   299                 return EFalse;
       
   300                 }                    
       
   301             }
       
   302         }
       
   303     return ETrue;
       
   304     }
       
   305     
       
   306 // ----------------------------------------------------------
       
   307 // CBTNotifierBase::AskLocalBTNameQueryL()
       
   308 // ----------------------------------------------------------
       
   309 //
       
   310 TBool CBTNotifierBase::AskLocalBTNameQueryL()
       
   311     {
       
   312 	FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() >>"));
       
   313 	
       
   314 	TBTDeviceName tempNameBuffer;
       
   315     // Cut the excess part of the name away. The query can handle only 
       
   316     // KBTUIMaxNameLength characters.
       
   317     //
       
   318     if( tempNameBuffer.Length() > KBTUIMaxNameLength )
       
   319      {
       
   320      tempNameBuffer.SetLength( KBTUIMaxNameLength );
       
   321      }
       
   322 
       
   323     //Get default name if given by e.g. phone product
       
   324     (void) RProperty::Get( KPropertyUidBluetoothCategory, 
       
   325                            KPropertyKeyBluetoothGetDeviceName, tempNameBuffer );
       
   326     
       
   327     if( !tempNameBuffer.Length() )
       
   328         {
       
   329         (void) RProperty::Get( KPropertyUidBluetoothCategory, 
       
   330                 KPropertyKeyBluetoothGetDeviceName, tempNameBuffer );
       
   331         }
       
   332     
       
   333     if( !tempNameBuffer.Length() )
       
   334         {
       
   335         RBTRegServ btRegServ;
       
   336         RBTLocalDevice btReg;
       
   337         TBTLocalDevice localDev;
       
   338 
       
   339         TInt err = btRegServ.Connect();
       
   340         if( !err )
       
   341             {
       
   342             err = btReg.Open( btRegServ );
       
   343             } 
       
   344         if( !err )
       
   345             {
       
   346             // Read the BT local name from BT Registry.
       
   347             err = btReg.Get( localDev );
       
   348             }
       
   349         if( !err )
       
   350             {
       
   351             // The error can be > 0 if there are unconverted characters.
       
   352             err = CnvUtfConverter::ConvertToUnicodeFromUtf8( tempNameBuffer, localDev.DeviceName() );
       
   353             }
       
   354         btReg.Close();
       
   355         btRegServ.Close();
       
   356         }
       
   357 
       
   358     TInt keypress = iNotifUiUtil->ShowTextInputQueryL( tempNameBuffer, 
       
   359             R_BT_ENTER_LOCAL_NAME_QUERY, ECmdBTnotifUnavailable );
       
   360     
       
   361     if( keypress )  // User has accepted the dialog
       
   362         {
       
   363         AknTextUtils::StripCharacters(tempNameBuffer,  KAknStripListControlChars);
       
   364         tempNameBuffer.TrimAll();   // Remove extra spaces
       
   365         // If name was full of invalid chars, it becomes empty after above cleanup. 
       
   366         if( tempNameBuffer.Length() ) 
       
   367             {
       
   368             TInt err = iBTEngSettings->SetLocalName( tempNameBuffer );
       
   369             FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() <<"));
       
   370             return (err) ? EFalse : ETrue;    
       
   371             }
       
   372         else
       
   373             {
       
   374             FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() << failed"));
       
   375             return EFalse;
       
   376             }
       
   377         }
       
   378     else
       
   379         {
       
   380         FLOG(_L("[BTNOTIF]\t CBTNotifierBase::AskLocalBTNameQueryL() << cancelled"));
       
   381         return EFalse;
       
   382         }
       
   383     }
       
   384 
       
   385 // ---------------------------------------------------------------------------------
       
   386 // CBTNotifierBase::IsExistingConnectionToAudioL
       
   387 // Check if there is any existing connection to audio profiles from the same device
       
   388 // ---------------------------------------------------------------------------------
       
   389 TBool CBTNotifierBase::IsExistingConnectionToAudioL( const TBTDevAddr& aDevAddr )
       
   390     {
       
   391     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsExistingConnectionToAudioL()"));
       
   392     CBTEngConnMan* connMan = CBTEngConnMan::NewL();
       
   393     TBTEngConnectionStatus conntatus( EBTEngNotConnected );
       
   394     (void) connMan->IsConnected(aDevAddr,conntatus);      
       
   395     delete connMan;
       
   396     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsExistingConnectionToAudioL() complete"));
       
   397     return ( conntatus==EBTEngConnected || conntatus==EBTEngConnecting );
       
   398     }
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CBTNotifierBase::IsLocalNameModified
       
   402 // Checks from central repository whether the Bluetooth friendly name 
       
   403 // has been modified .
       
   404 // ---------------------------------------------------------------------------
       
   405 //
       
   406 TBool CBTNotifierBase::IsLocalNameModifiedL()
       
   407 	{
       
   408 	FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsLocalNameModified()"));   
       
   409     CRepository* cenRep = CRepository::NewL( KCRUidBTEngPrivateSettings );
       
   410     TInt val( EBTLocalNameDefault );
       
   411     (void) cenRep->Get( KBTLocalNameChanged, val );
       
   412     delete cenRep;
       
   413     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::IsLocalNameModified() complete"));
       
   414     return val == EBTLocalNameSet;
       
   415     }
       
   416 
       
   417 
       
   418 void CBTNotifierBase::GetDeviceFromRegL(const TBTDevAddr& aAddr)
       
   419     {
       
   420     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::GetDeviceFromRegL"));
       
   421     __ASSERT_DEBUG(iDevice, BTNOTIF_PANIC(EiDeviceNullWhenCallingGetDeviceFromRegL));
       
   422 
       
   423     TBTRegistrySearch mySearch;
       
   424     mySearch.FindAddress( aAddr );
       
   425     iDeviceArray = new (ELeave) CBTDeviceArray(1);
       
   426     TInt err = iDevMan->GetDevices( mySearch, iDeviceArray );
       
   427     if(err)
       
   428         {
       
   429         FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::GetDeviceFromRegL iDevMan->GetDevices error = %d"), err));
       
   430         DoHandleGetDevicesCompleteL(err, NULL);
       
   431         }
       
   432     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::GetDeviceFromRegL done"));
       
   433     }
       
   434 
       
   435 // ----------------------------------------------------------
       
   436 // CBTNotifierBase::QueryBlockDeviceL
       
   437 // Opens a query that returns wether or not user wants the device blocked
       
   438 // ----------------------------------------------------------
       
   439 void CBTNotifierBase::QueryBlockDeviceL()
       
   440     {
       
   441     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::QueryBlockDeviceL()"));
       
   442     __ASSERT_DEBUG( iNotifUiUtil->IsQueryReleased(), User::Panic( _L("CBTNotifierBase - iYesNoDlg not released!"), KErrAlreadyExists ) );
       
   443     
       
   444     TBTDeviceName bName;
       
   445     BtNotifNameUtils::GetDeviceDisplayName(bName, iDevice);
       
   446 
       
   447     HBufC* header= StringLoader::LoadLC( R_BT_BLOCK_DEVICE_HEADER );
       
   448 
       
   449     TInt resId = IsUserAwarePaired( iDevice->AsNamelessDevice() ) ? 
       
   450         R_BT_BLOCK_PAIRED_DEVICE_NOHELP : R_BT_BLOCK_DEVICE_NOHELP;
       
   451 	
       
   452 	HBufC* stringHolder = StringLoader::LoadLC( resId, bName );
       
   453 
       
   454 	TInt keypress = iNotifUiUtil->ShowMessageQueryL( *stringHolder, *header, 
       
   455 	        R_BT_GENERIC_MESSAGE_QUERY, CAknQueryDialog::EConfirmationTone );
       
   456     
       
   457     CleanupStack::PopAndDestroy(stringHolder);                   
       
   458     CleanupStack::PopAndDestroy(header);  
       
   459 
       
   460     if( keypress )// user replied "Yes"
       
   461         {
       
   462         DoBlockDevice();
       
   463         }
       
   464     else  // user replied "No" 
       
   465         {
       
   466         CompleteMessage(KErrCancel);        
       
   467         } 
       
   468     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::QueryBlockDeviceL() complete"));   
       
   469     }
       
   470 
       
   471 void CBTNotifierBase::DoBlockDevice()
       
   472     {
       
   473     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::DoBlockDevice()"));
       
   474     TBTDeviceSecurity deviceSecurity = iDevice->GlobalSecurity();
       
   475     deviceSecurity.SetBanned(ETrue);
       
   476     deviceSecurity.SetNoAuthenticate(EFalse);
       
   477     deviceSecurity.SetNoAuthorise(EFalse);
       
   478     iDevice->SetGlobalSecurity(deviceSecurity);
       
   479     iDevice->DeleteLinkKey();
       
   480  
       
   481     iBTRegistryQueryState = ESetDeviceBlocked;
       
   482     TInt devManErr = iDevMan->ModifyDevice( *iDevice );     
       
   483             
       
   484     // if error, complete message, otherwise waiting for devman callback
       
   485     //
       
   486     if(devManErr != KErrNone)
       
   487         CompleteMessage(devManErr); 
       
   488     }
       
   489 
       
   490 void CBTNotifierBase::ChangeAuthorizeState( TBool aTrust )
       
   491     {
       
   492     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::ChangeAuthorizeState()"));   
       
   493     TBTDeviceSecurity sec = iDevice->GlobalSecurity();
       
   494     sec.SetBanned(EFalse);
       
   495     sec.SetNoAuthorise( ( aTrust ) ? ETrue : EFalse ) ;
       
   496     iDevice->SetGlobalSecurity(sec);
       
   497  
       
   498     iBTRegistryQueryState = ESetDeviceAuthorizeState;
       
   499     TInt devManErr = iDevMan->ModifyDevice( *iDevice );     
       
   500             
       
   501     // if error, complete message, otherwise waiting for devman callback
       
   502     //
       
   503     if(devManErr != KErrNone)
       
   504         {
       
   505         CompleteMessage(devManErr); 
       
   506         TBTNotifLockPublish::DeleteNotifLocks( 
       
   507                 EBTNotiferLockPairedDeviceSetting, iDevice->BDAddr() );
       
   508         }
       
   509     }
       
   510 
       
   511 // ----------------------------------------------------------
       
   512 // CBTNotifierBase::CheckAndHandleQueryIntervalL
       
   513 // ----------------------------------------------------------
       
   514 //
       
   515 void CBTNotifierBase::CheckAndHandleQueryIntervalL()
       
   516     {
       
   517     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval start." ) );
       
   518     TBool res = CheckQueryInterval();
       
   519     if( res )
       
   520         {
       
   521         FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval check on/off query status." ) );
       
   522         // Check if we're already showing Turn BT off query
       
   523         TInt queryValue=EBTQueryOff;
       
   524         RProperty::Get( KPSUidBluetoothEnginePrivateCategory, KBTTurnBTOffQueryOn, queryValue );
       
   525         if( queryValue==EBTQueryOff )
       
   526             {
       
   527             FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval asking to turn BT off." ) );
       
   528             RProperty::Set( KPSUidBluetoothEnginePrivateCategory, KBTTurnBTOffQueryOn, EBTQueryOn );
       
   529             TInt keypress = iNotifUiUtil->ShowQueryL( KErrNone, R_BT_TURN_BT_OFF_NOTE, ECmdBTnotifUnavailable );
       
   530             RProperty::Set( KPSUidBluetoothEnginePrivateCategory, KBTTurnBTOffQueryOn, EBTQueryOff );
       
   531             if( keypress )  // User has accepted the dialog
       
   532                 {
       
   533                 FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval turning BT off." ) );
       
   534                 (void) iBTEngSettings->SetPowerState( EBTPowerOff );
       
   535                 CompleteMessage( KErrAccessDenied );
       
   536                 }
       
   537             }
       
   538         }
       
   539     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckAndHandleQueryIntervalL done." ) );
       
   540     }
       
   541 
       
   542 void CBTNotifierBase::DoHandleGetDevicesCompleteL( TInt aErr, CBTDeviceArray* aDeviceArray)
       
   543     {
       
   544     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::DoHandleGetDevicesCompleteL(%d)"), aErr ));
       
   545     TBTDeviceName devName;
       
   546     if (!aErr && aDeviceArray && aDeviceArray->Count())
       
   547         {
       
   548         // Reassign so that we won't lose the device name as it is
       
   549         // needed if registry doesn't have the name for this device yet:
       
   550         CBTDevice* temp = iDevice;
       
   551         CleanupStack::PushL(temp);
       
   552         // Taking the object returned by registry so that iDevice have
       
   553         // all the up-to-date information except device name.
       
   554         iDevice = aDeviceArray->At(0);
       
   555         aDeviceArray->Delete( 0 );
       
   556         // we show user the device name from registry if it is available;
       
   557         // Otherwise, the name passed in by the notifier request shall be 
       
   558         // used.
       
   559         if( ( !iDevice->IsValidDeviceName() || 
       
   560               !iDevice->DeviceName().Length() ) && 
       
   561             temp->IsValidDeviceName() )
       
   562             {
       
   563             // We are using a stored device name, which will
       
   564             // already have been processed
       
   565             iDevice->SetDeviceNameL( temp->DeviceName() );
       
   566             }
       
   567         CleanupStack::PopAndDestroy(temp);
       
   568         }
       
   569     BtNotifNameUtils::GetDeviceName(devName, iDevice);   
       
   570     
       
   571     // It is possible that iDevice hasn't got a name so far. Use the default BT name
       
   572     // got from GetDeviceName().
       
   573     if ( !iDevice->IsValidDeviceName() || !iDevice->DeviceName().Length() )
       
   574         {
       
   575         BtNotifNameUtils::SetDeviceNameL(devName, *iDevice);
       
   576         }
       
   577     HandleGetDeviceCompletedL( iDevice ); 
       
   578     }
       
   579 
       
   580 // ----------------------------------------------------------
       
   581 // CBTNotifierBase::CompleteMessage
       
   582 // ----------------------------------------------------------
       
   583 //	
       
   584 void CBTNotifierBase::CompleteMessage(TInt aErr)
       
   585     {	   
       
   586 	if( !iMessage.IsNull() )
       
   587 	    {
       
   588 	    FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::CompleteMessage(%d)"), aErr ));
       
   589 	    iMessage.Complete( aErr );
       
   590 	    }
       
   591 	}
       
   592 
       
   593 // ----------------------------------------------------------
       
   594 // CBTNotifierBase::CompleteMessage
       
   595 // ----------------------------------------------------------
       
   596 //	
       
   597 void CBTNotifierBase::CompleteMessage(TInt aValueToReplySlot, TInt aErr)
       
   598     {
       
   599     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::CompleteMessage(%d, %d)"), 
       
   600             aValueToReplySlot, aErr ) );
       
   601     CompleteMessage(TPckgBuf<TInt>( aValueToReplySlot ), aErr);
       
   602 	}
       
   603 
       
   604 // ----------------------------------------------------------
       
   605 // CBTNotifierBase::CompleteMessage
       
   606 // ----------------------------------------------------------
       
   607 //	
       
   608 void CBTNotifierBase::CompleteMessage(const TDesC8& aDesToReplySlot, TInt aErr)
       
   609     {	   
       
   610 	if( !iMessage.IsNull() )
       
   611 	    {
       
   612 	    FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::CompleteMessage(TDesC8&, %d)"), aErr ) );
       
   613 	    if ( !aErr )
       
   614 	        {
       
   615 	        aErr = iMessage.Write( iReplySlot, aDesToReplySlot );
       
   616 	        }
       
   617 	    iMessage.Complete( aErr );
       
   618 	    }
       
   619 	}
       
   620 
       
   621 #ifdef __SERIES60_HELP    
       
   622 //-------------------------------------------------------------
       
   623 // CBTNotifierBase::LaunchHelp
       
   624 //--------------------------------------------------------------
       
   625 TInt CBTNotifierBase::LaunchHelp(TAny * tCoeHelpContext )
       
   626 	{	
       
   627 	TCoeHelpContext hc;
       
   628 	
       
   629 	if( tCoeHelpContext==NULL)
       
   630 		hc = TCoeHelpContext(KUidBTUI,KBT_HLP_BLOCKED);
       
   631 	else
       
   632 		hc = *static_cast<TCoeHelpContext*>(tCoeHelpContext);
       
   633 		 
       
   634 		CArrayFix< TCoeHelpContext >* array = new CArrayFixFlat< TCoeHelpContext >(1);
       
   635 	TRAPD(err,
       
   636 		{
       
   637 		CleanupStack::PushL(array);
       
   638 		array->AppendL(hc);		
       
   639 		HlpLauncher::LaunchHelpApplicationL( CCoeEnv::Static()->WsSession() , array );
       
   640     	CleanupStack::Pop(array);
       
   641 		});
       
   642 
       
   643     // the array is not deleted, since deleting it will result
       
   644     // to E32User-CBase 3. It is assumed that HlpLancher is taking care of it.
       
   645     // however this has not been documented.
       
   646     return err;
       
   647 	}
       
   648 #endif
       
   649 
       
   650 void CBTNotifierBase::HandleGetDevicesComplete( TInt aErr, CBTDeviceArray* aDeviceArray)
       
   651     {
       
   652     TRAP_IGNORE(DoHandleGetDevicesCompleteL(aErr, aDeviceArray));
       
   653     }
       
   654 
       
   655 void CBTNotifierBase::HandleDevManComplete(TInt aErr)
       
   656     {
       
   657     FTRACE(FPrint(_L("[BTNOTIF]\t CBTNotifierBase::HandleDevManComplete()  aErr = %d"), aErr ));
       
   658     
       
   659     switch (iBTRegistryQueryState)
       
   660         {
       
   661         case ESetDeviceBlocked:
       
   662             {
       
   663             // Blocking device was demanded by user after the user
       
   664             // rejected incoming pairing or connect request. The message
       
   665             // to be completed here is the original pair or authorization request
       
   666             // which has been rejected by the user.
       
   667             CompleteMessage(KErrCancel);
       
   668             }
       
   669         case ESetDeviceAuthorizeState:
       
   670             {
       
   671             TBTNotifLockPublish::DeleteNotifLocks( 
       
   672                     EBTNotiferLockPairedDeviceSetting, iDevice->BDAddr() );
       
   673             CompleteMessage(aErr);
       
   674             break;
       
   675             }
       
   676         }
       
   677     FLOG(_L("[BTNOTIF]\t CBTNotifierBase::HandleDevManComplete() Complete"));           
       
   678     }
       
   679 
       
   680 void CBTNotifierBase::HandleGetDeviceCompletedL(const CBTDevice* /*aDev*/)
       
   681     {    
       
   682     }
       
   683 
       
   684 // ----------------------------------------------------------
       
   685 // CBTNotifierBase::CheckQueryInterval
       
   686 // ----------------------------------------------------------
       
   687 //
       
   688 TBool CBTNotifierBase::CheckQueryInterval()
       
   689     {
       
   690     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval()" ) );
       
   691     TBool result = EFalse;
       
   692     // Check if the user gets too many queries within a certain time interval from unpaired devices
       
   693     if( iDevice && !iDevice->IsValidLinkKey() )
       
   694         {
       
   695         TBuf8<32> buf;
       
   696         TTime now;
       
   697         now.UniversalTime();    // Get current time
       
   698         TInt64 writeTime = ( now + REJECT_ADD_TIME ).Int64();
       
   699 
       
   700         // get last connection time
       
   701         TInt err = RProperty::Get( KPSUidBluetoothEnginePrivateCategory, KBTConnectionTimeStamp, buf );
       
   702         if( !err )
       
   703             {
       
   704             FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval Checking query interval." ) );
       
   705             // Parse recorded time stamp
       
   706             TInt64 num(0);
       
   707             TLex8 lex;
       
   708             lex = buf;
       
   709             lex.Val( num );
       
   710             TTime lastTime( num );
       
   711 
       
   712             // detection of clock adjusment
       
   713             TInt timetravel = 2 * REJECT_ADD_TIME.Int();
       
   714             if( lastTime > now + TTimeIntervalSeconds(timetravel) )
       
   715                 {
       
   716                 FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval  time travel detection." ) );
       
   717                 lastTime = now;
       
   718                 }
       
   719             // new request too quickly, move booked time
       
   720             if( lastTime + CONNECT_ADD_TIME > now )
       
   721                 {
       
   722                 FLOG( _L( "[BTNOTIF]\t CBTAuthNotifier::IsDosAttack  CONNECT_ADD_TIME added." ) );
       
   723                 writeTime = ( lastTime + CONNECT_ADD_TIME ).Int64();
       
   724                 }
       
   725 
       
   726             FTRACE( FPrint( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval() lastTime = %d" ), lastTime.Int64() ) );
       
   727             FTRACE( FPrint( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval now + treshold: %d" ), (now + DENY_THRESHOLD).Int64() ) );
       
   728             // If threshold is exceeded, ask user to turn Bluetooth OFF
       
   729             if( lastTime > now + DENY_THRESHOLD )
       
   730                 {
       
   731                 FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval threshold exceeded!." ) );
       
   732                 result = ETrue;
       
   733                 }
       
   734             }
       
   735         else
       
   736             {
       
   737             FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval setting connection timestamp first time." ) );
       
   738             }
       
   739         // Write back the timestamp
       
   740         buf.Num( writeTime );
       
   741         FTRACE( FPrint( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval updating timestamp: %d." ), writeTime ) );
       
   742         RProperty::Set( KPSUidBluetoothEnginePrivateCategory,KBTConnectionTimeStamp, buf );
       
   743         } // end of query interval check
       
   744     FLOG( _L( "[BTNOTIF]\t CBTNotifierBase::CheckQueryInterval() complete" ) );
       
   745     return result;
       
   746     }
       
   747 // End of File