bluetoothengine/bteng/src/btengsrvsettingsmgr.cpp
author hgs
Sat, 24 Apr 2010 00:30:17 +0300
changeset 26 b78e66e88238
parent 25 9c3798b88e30
child 28 7e2761e776bd
permissions -rw-r--r--
201015
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23
hgs
parents:
diff changeset
     1
/*
25
hgs
parents: 24
diff changeset
     2
* Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
23
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description: Class to manage Bluetooth hardware and stack settings.
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
hgs
parents:
diff changeset
    18
#include "btengsrvsettingsmgr.h"
hgs
parents:
diff changeset
    19
#include <bthci.h>
hgs
parents:
diff changeset
    20
#include <bt_subscribe_partner.h>
hgs
parents:
diff changeset
    21
#include <btnotif.h>
hgs
parents:
diff changeset
    22
#include <btengdomainpskeys.h>
hgs
parents:
diff changeset
    23
#include <centralrepository.h>
hgs
parents:
diff changeset
    24
#include <featmgr.h>
hgs
parents:
diff changeset
    25
#include <AknSmallIndicator.h>
hgs
parents:
diff changeset
    26
#include <avkon.hrh>
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
#include "btengserver.h"
hgs
parents:
diff changeset
    29
#include "btengsrvpluginmgr.h"
hgs
parents:
diff changeset
    30
#include "btengsrvbbconnectionmgr.h"
hgs
parents:
diff changeset
    31
#include "btengsrvstate.h"
hgs
parents:
diff changeset
    32
#include "debug.h"
hgs
parents:
diff changeset
    33
hgs
parents:
diff changeset
    34
/** ID of active object helper */
hgs
parents:
diff changeset
    35
const TInt KBTEngSettingsActive = 30;
hgs
parents:
diff changeset
    36
/** Constant for converting minutes to microseconds */
hgs
parents:
diff changeset
    37
//const TInt64 KMinutesInMicroSecs = 60000000;
hgs
parents:
diff changeset
    38
const TInt64 KMinutesInMicroSecs = MAKE_TINT64( 0, 60000000 );
hgs
parents:
diff changeset
    39
/**  Timeout for disabling Simple Pairing debug mode. The value is 30 minutes. */
hgs
parents:
diff changeset
    40
//const TInt KBTEngSspDebugModeTimeout = 1800000000;
hgs
parents:
diff changeset
    41
const TInt64 KBTEngSspDebugModeTimeout = MAKE_TINT64( 0, 1800000000 );
hgs
parents:
diff changeset
    42
/**  Timeout for turning BT off automatically. The value is 10.5 seconds. */
hgs
parents:
diff changeset
    43
const TInt KBTEngBtAutoOffTimeout = 10500000;
26
hgs
parents: 25
diff changeset
    44
 
23
hgs
parents:
diff changeset
    45
// ======== MEMBER FUNCTIONS ========
hgs
parents:
diff changeset
    46
hgs
parents:
diff changeset
    47
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    48
// C++ default constructor
hgs
parents:
diff changeset
    49
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    50
//
hgs
parents:
diff changeset
    51
CBTEngSrvSettingsMgr::CBTEngSrvSettingsMgr( CBTEngServer* aServer )
hgs
parents:
diff changeset
    52
:   iServer( aServer )
hgs
parents:
diff changeset
    53
    {
hgs
parents:
diff changeset
    54
    }
hgs
parents:
diff changeset
    55
hgs
parents:
diff changeset
    56
hgs
parents:
diff changeset
    57
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    58
// Symbian second-phase constructor
hgs
parents:
diff changeset
    59
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    60
//
hgs
parents:
diff changeset
    61
void CBTEngSrvSettingsMgr::ConstructL()
hgs
parents:
diff changeset
    62
    {
hgs
parents:
diff changeset
    63
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
    64
    iActive = CBTEngActive::NewL( *this, KBTEngSettingsActive );
hgs
parents:
diff changeset
    65
    LoadBTPowerManagerL();
hgs
parents:
diff changeset
    66
    iEnterpriseEnablementMode = BluetoothFeatures::EnterpriseEnablementL();
hgs
parents:
diff changeset
    67
    TRACE_INFO( ( _L( "iEnterpriseEnablementMode = %d" ), iEnterpriseEnablementMode) )
hgs
parents:
diff changeset
    68
    if ( iEnterpriseEnablementMode == BluetoothFeatures::EDisabled )
hgs
parents:
diff changeset
    69
        {
hgs
parents:
diff changeset
    70
        SetVisibilityModeL( EBTVisibilityModeNoScans, 0 );
hgs
parents:
diff changeset
    71
        }
hgs
parents:
diff changeset
    72
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
    73
    }
hgs
parents:
diff changeset
    74
hgs
parents:
diff changeset
    75
hgs
parents:
diff changeset
    76
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    77
// NewL
hgs
parents:
diff changeset
    78
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    79
//
hgs
parents:
diff changeset
    80
CBTEngSrvSettingsMgr* CBTEngSrvSettingsMgr::NewL( CBTEngServer* aServer )
hgs
parents:
diff changeset
    81
    {
hgs
parents:
diff changeset
    82
    CBTEngSrvSettingsMgr* self = new( ELeave ) CBTEngSrvSettingsMgr( aServer );
hgs
parents:
diff changeset
    83
    CleanupStack::PushL( self );
hgs
parents:
diff changeset
    84
    self->ConstructL();
hgs
parents:
diff changeset
    85
    CleanupStack::Pop( self );
hgs
parents:
diff changeset
    86
    return self;
hgs
parents:
diff changeset
    87
    }
hgs
parents:
diff changeset
    88
hgs
parents:
diff changeset
    89
hgs
parents:
diff changeset
    90
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    91
// Destructor
hgs
parents:
diff changeset
    92
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
    93
//
hgs
parents:
diff changeset
    94
CBTEngSrvSettingsMgr::~CBTEngSrvSettingsMgr()
hgs
parents:
diff changeset
    95
    {
hgs
parents:
diff changeset
    96
    if( iActive && iActive->IsActive() )
hgs
parents:
diff changeset
    97
        {
hgs
parents:
diff changeset
    98
        // Cancel the outstanding request.
hgs
parents:
diff changeset
    99
        iPowerMgr.Cancel();
hgs
parents:
diff changeset
   100
        }
hgs
parents:
diff changeset
   101
    delete iActive;  
hgs
parents:
diff changeset
   102
    iPowerMgr.Close();
hgs
parents:
diff changeset
   103
    }
hgs
parents:
diff changeset
   104
hgs
parents:
diff changeset
   105
hgs
parents:
diff changeset
   106
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   107
// Power Bluetooth hardware on or off.
hgs
parents:
diff changeset
   108
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   109
//
hgs
parents:
diff changeset
   110
TInt CBTEngSrvSettingsMgr::SetHwPowerState( TBTPowerState aState )
hgs
parents:
diff changeset
   111
    {
hgs
parents:
diff changeset
   112
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   113
    TInt err = KErrNone;
hgs
parents:
diff changeset
   114
#ifndef __WINS__
hgs
parents:
diff changeset
   115
    if( iActive->IsActive() )
hgs
parents:
diff changeset
   116
        {
hgs
parents:
diff changeset
   117
        // Cancel the outstanding request.
hgs
parents:
diff changeset
   118
        iPowerMgr.Cancel();
hgs
parents:
diff changeset
   119
        iActive->Cancel();
hgs
parents:
diff changeset
   120
        }
hgs
parents:
diff changeset
   121
    iPowerMgr.SetPower( aState, NULL, iActive->RequestStatus() );
hgs
parents:
diff changeset
   122
    iActive->GoActive();
hgs
parents:
diff changeset
   123
#else   //__WINS__
hgs
parents:
diff changeset
   124
    iPowerState = aState;
hgs
parents:
diff changeset
   125
#endif  //__WINS__
hgs
parents:
diff changeset
   126
hgs
parents:
diff changeset
   127
   if( !err && aState == EBTOn )
hgs
parents:
diff changeset
   128
        {
hgs
parents:
diff changeset
   129
        TInt dutMode = EBTDutOff;
hgs
parents:
diff changeset
   130
        err = RProperty::Get( KPSUidBluetoothTestingMode, KBTDutEnabled, dutMode );
hgs
parents:
diff changeset
   131
        if( !err && dutMode == EBTDutOn )
hgs
parents:
diff changeset
   132
            {
hgs
parents:
diff changeset
   133
            // Set the DUT mode key to OFF since DUT mode is disabled at this point
hgs
parents:
diff changeset
   134
            err = RProperty::Set( KPSUidBluetoothTestingMode, KBTDutEnabled, EBTDutOff );
hgs
parents:
diff changeset
   135
            }
hgs
parents:
diff changeset
   136
        }
hgs
parents:
diff changeset
   137
    if( err )
hgs
parents:
diff changeset
   138
        {
hgs
parents:
diff changeset
   139
        // Power off if an error occurred during power on sequence.
hgs
parents:
diff changeset
   140
#ifndef __WINS__
hgs
parents:
diff changeset
   141
            // This cannot happen in emulator environment.
hgs
parents:
diff changeset
   142
        iPowerMgr.Cancel();
hgs
parents:
diff changeset
   143
        iActive->Cancel();
hgs
parents:
diff changeset
   144
        iPowerMgr.SetPower( EBTOff, NULL, iActive->RequestStatus() );
hgs
parents:
diff changeset
   145
        User::WaitForRequest( iActive->RequestStatus() );
hgs
parents:
diff changeset
   146
        // Result will be communicated through the caller of this function (by leaving).
hgs
parents:
diff changeset
   147
#else   //__WINS__
hgs
parents:
diff changeset
   148
        iPowerState = EBTOff;
hgs
parents:
diff changeset
   149
#endif  //__WINS__
hgs
parents:
diff changeset
   150
        } 
26
hgs
parents: 25
diff changeset
   151
    
hgs
parents: 25
diff changeset
   152
#ifdef __WINS__
hgs
parents: 25
diff changeset
   153
    TRequestStatus* status = &(iActive->RequestStatus());
hgs
parents: 25
diff changeset
   154
    iActive->GoActive();
hgs
parents: 25
diff changeset
   155
    User::RequestComplete(status,KErrNone);
hgs
parents: 25
diff changeset
   156
#endif  //__WINS__
hgs
parents: 25
diff changeset
   157
    
23
hgs
parents:
diff changeset
   158
    TRACE_FUNC_RES( ( _L( "result: %d" ), err ) )
hgs
parents:
diff changeset
   159
    return err;
hgs
parents:
diff changeset
   160
    }
hgs
parents:
diff changeset
   161
hgs
parents:
diff changeset
   162
hgs
parents:
diff changeset
   163
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   164
// Turn BT on or off.
hgs
parents:
diff changeset
   165
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   166
//
hgs
parents:
diff changeset
   167
void CBTEngSrvSettingsMgr::SetPowerStateL( TBTPowerState aState, TBool aTemporary )
hgs
parents:
diff changeset
   168
    {
hgs
parents:
diff changeset
   169
    TRACE_FUNC_ARG( ( _L( "%d (temporary=%d" ), aState, aTemporary ) )
hgs
parents:
diff changeset
   170
    if ( aState == EBTOn && iEnterpriseEnablementMode == BluetoothFeatures::EDisabled )
hgs
parents:
diff changeset
   171
        {
hgs
parents:
diff changeset
   172
        TRACE_INFO( ( _L( "no we're not... Bluetooth is enterprise-IT-disabled" ) ) )
hgs
parents:
diff changeset
   173
        User::Leave(KErrNotSupported);
hgs
parents:
diff changeset
   174
        }
hgs
parents:
diff changeset
   175
    TBool idle = ( iServer->StateMachine()->CurrentOperation() == CBTEngSrvState::ESrvOpIdle );
hgs
parents:
diff changeset
   176
    TBTPowerState currentState = EBTOff;
hgs
parents:
diff changeset
   177
    CheckTemporaryPowerStateL( currentState, aState, aTemporary );
hgs
parents:
diff changeset
   178
hgs
parents:
diff changeset
   179
    if ( ( currentState == aState || ( aTemporary && aState == EBTOff ) ) && idle )
hgs
parents:
diff changeset
   180
        {
hgs
parents:
diff changeset
   181
        // The requested power state is already active, ignore silently.
hgs
parents:
diff changeset
   182
        // We don't return an error here, as there is no error situation.
hgs
parents:
diff changeset
   183
        TRACE_INFO( ( _L( "SetPowerStateL: nothing to do" ) ) )
hgs
parents:
diff changeset
   184
        if ( currentState == aState )
hgs
parents:
diff changeset
   185
            {
hgs
parents:
diff changeset
   186
            // Make sure that the CenRep key is in sync.
hgs
parents:
diff changeset
   187
            // During boot-up, the pwoer is set from the CenRep key, so we could 
hgs
parents:
diff changeset
   188
            // end up out-of-sync.
hgs
parents:
diff changeset
   189
            TRACE_INFO( ( _L( "SetPowerStateL: currentState == aState" ) ) )
hgs
parents:
diff changeset
   190
            UpdateCenRepPowerKeyL( aState );
hgs
parents:
diff changeset
   191
            } 
hgs
parents:
diff changeset
   192
        return;
hgs
parents:
diff changeset
   193
        }
hgs
parents:
diff changeset
   194
    if ( aState == EBTOn )
hgs
parents:
diff changeset
   195
        {
hgs
parents:
diff changeset
   196
        // Hardware power on is the first step.
hgs
parents:
diff changeset
   197
        User::LeaveIfError( SetHwPowerState( aState ) );
hgs
parents:
diff changeset
   198
        }
hgs
parents:
diff changeset
   199
    else
hgs
parents:
diff changeset
   200
        {
hgs
parents:
diff changeset
   201
        //Prevent BT visibility in the situation when we turn OFF BT Engine 
hgs
parents:
diff changeset
   202
        //but FM Radio is still alive
hgs
parents:
diff changeset
   203
        SetVisibilityModeL( EBTVisibilityModeNoScans, 0 );
hgs
parents:
diff changeset
   204
        // Hardware power off is the last step.
hgs
parents:
diff changeset
   205
        // First disconnect all plug-ins.
hgs
parents:
diff changeset
   206
        iServer->PluginManager()->DisconnectAllPlugins();
hgs
parents:
diff changeset
   207
        }
hgs
parents:
diff changeset
   208
    // We only signal that BT is on after everything has completed (through 
hgs
parents:
diff changeset
   209
    // the CenRep power state key), so that all services are initialized.
hgs
parents:
diff changeset
   210
    // We signal that BT is off immediately though, so that our clients will 
hgs
parents:
diff changeset
   211
    // not try to use BT during power down.
hgs
parents:
diff changeset
   212
    iServer->StateMachine()->StartStateMachineL( aState );
hgs
parents:
diff changeset
   213
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   214
    }
hgs
parents:
diff changeset
   215
hgs
parents:
diff changeset
   216
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   217
// Turn BT on or off.
hgs
parents:
diff changeset
   218
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   219
//
hgs
parents:
diff changeset
   220
void CBTEngSrvSettingsMgr::SetPowerStateL(const RMessage2 aMessage )
hgs
parents:
diff changeset
   221
    {
hgs
parents:
diff changeset
   222
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   223
    __ASSERT_ALWAYS( aMessage.Function() == EBTEngSetPowerState, 
hgs
parents:
diff changeset
   224
            PanicServer( EBTEngPanicExpectSetPowerOpcode ) );
hgs
parents:
diff changeset
   225
    if ( !iMessage.IsNull() )
hgs
parents:
diff changeset
   226
        {
hgs
parents:
diff changeset
   227
        // A power management request from a client is outstanding.
hgs
parents:
diff changeset
   228
        User::Leave( KErrAlreadyExists );
hgs
parents:
diff changeset
   229
        }
hgs
parents:
diff changeset
   230
 
hgs
parents:
diff changeset
   231
    TBTPowerStateValue arg = (TBTPowerStateValue) aMessage.Int0();
hgs
parents:
diff changeset
   232
    // TBTPowerState power state type is inverted from TBTPowerStateValue,
hgs
parents:
diff changeset
   233
    // which is used by the client to pass the parameter...
hgs
parents:
diff changeset
   234
    TBTPowerState reqedPowerState( EBTOff );
hgs
parents:
diff changeset
   235
    if ( arg == EBTPowerOn )
hgs
parents:
diff changeset
   236
        {
hgs
parents:
diff changeset
   237
        reqedPowerState = EBTOn;
hgs
parents:
diff changeset
   238
        }
hgs
parents:
diff changeset
   239
    TBool tempPowerOn = (TBool) aMessage.Int1();
hgs
parents:
diff changeset
   240
    
hgs
parents:
diff changeset
   241
    SetPowerStateL( reqedPowerState, tempPowerOn );
hgs
parents:
diff changeset
   242
    if ( iServer->StateMachine()->CurrentOperation() == CBTEngSrvState::ESrvOpIdle )
hgs
parents:
diff changeset
   243
        {
hgs
parents:
diff changeset
   244
        // The request is accepted but the state machine is not running. This means the
hgs
parents:
diff changeset
   245
        // requested power state is already active. Request is done.
hgs
parents:
diff changeset
   246
        aMessage.Complete( KErrNone );
hgs
parents:
diff changeset
   247
        }
hgs
parents:
diff changeset
   248
    else
hgs
parents:
diff changeset
   249
        {
hgs
parents:
diff changeset
   250
        iMessage = RMessage2( aMessage );
hgs
parents:
diff changeset
   251
        }
hgs
parents:
diff changeset
   252
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   253
    }
hgs
parents:
diff changeset
   254
hgs
parents:
diff changeset
   255
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   256
// Initialize Bluetooth stack settings.
hgs
parents:
diff changeset
   257
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   258
//
hgs
parents:
diff changeset
   259
void CBTEngSrvSettingsMgr::InitBTStackL()
hgs
parents:
diff changeset
   260
    {
hgs
parents:
diff changeset
   261
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   262
    iServer->BasebandConnectionManager()->Subscribe();
hgs
parents:
diff changeset
   263
    TBTVisibilityMode visibility = EBTVisibilityModeHidden;
hgs
parents:
diff changeset
   264
    CRepository* cenRep = CRepository::NewL( KCRUidBTEngPrivateSettings );
hgs
parents:
diff changeset
   265
    TInt err = cenRep->Get( KBTDiscoverable, (TInt&) visibility );
hgs
parents:
diff changeset
   266
    delete cenRep;
hgs
parents:
diff changeset
   267
    if (iRestoreVisibility == EFalse)
hgs
parents:
diff changeset
   268
        {
hgs
parents:
diff changeset
   269
        if( err || visibility == EBTVisibilityModeTemporary &&
hgs
parents:
diff changeset
   270
            !( iServer->IsTimerQueued( CBTEngServer::EScanModeTimer ) ) )
hgs
parents:
diff changeset
   271
            {
hgs
parents:
diff changeset
   272
            visibility = EBTVisibilityModeHidden;
hgs
parents:
diff changeset
   273
            }
hgs
parents:
diff changeset
   274
        SetVisibilityModeL( visibility, 0 );
hgs
parents:
diff changeset
   275
        }
hgs
parents:
diff changeset
   276
hgs
parents:
diff changeset
   277
    TBool sspDebugMode = EFalse;
hgs
parents:
diff changeset
   278
    (void) RProperty::Get( KPropertyUidBluetoothCategory,
hgs
parents:
diff changeset
   279
                KPropertyKeyBluetoothGetSimplePairingDebugMode, (TInt&) sspDebugMode );
hgs
parents:
diff changeset
   280
        // Only set debug mode to off if it is on, to prevent a loop notifications.
hgs
parents:
diff changeset
   281
    if( sspDebugMode )
hgs
parents:
diff changeset
   282
        {
hgs
parents:
diff changeset
   283
        sspDebugMode = EFalse;
hgs
parents:
diff changeset
   284
        // Add LeaveIfError if unsuccessful
hgs
parents:
diff changeset
   285
        (void) RProperty::Set(KPropertyUidBluetoothCategory,
hgs
parents:
diff changeset
   286
                    KPropertyKeyBluetoothSetSimplePairingDebugMode, (TInt) sspDebugMode );
hgs
parents:
diff changeset
   287
        }
hgs
parents:
diff changeset
   288
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   289
    }
hgs
parents:
diff changeset
   290
hgs
parents:
diff changeset
   291
hgs
parents:
diff changeset
   292
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   293
// Reset settings and disconnect all links.
hgs
parents:
diff changeset
   294
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   295
//
hgs
parents:
diff changeset
   296
void CBTEngSrvSettingsMgr::StopBTStackL()
hgs
parents:
diff changeset
   297
    {
hgs
parents:
diff changeset
   298
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   299
    TBTVisibilityMode visibility = EBTVisibilityModeHidden;
hgs
parents:
diff changeset
   300
    CRepository* cenRep = CRepository::NewL( KCRUidBTEngPrivateSettings );
hgs
parents:
diff changeset
   301
          // Ignore error here; if we can't read it, likely we can't set it either.
hgs
parents:
diff changeset
   302
    (void) cenRep->Get( KBTDiscoverable, (TInt&) visibility );
hgs
parents:
diff changeset
   303
    delete cenRep;
hgs
parents:
diff changeset
   304
    if( visibility == EBTVisibilityModeTemporary )
hgs
parents:
diff changeset
   305
        {
hgs
parents:
diff changeset
   306
        visibility = EBTVisibilityModeHidden;
hgs
parents:
diff changeset
   307
        SetVisibilityModeL( visibility, 0 );    // Also cancels scan mode timer.
hgs
parents:
diff changeset
   308
        }
hgs
parents:
diff changeset
   309
hgs
parents:
diff changeset
   310
        // Stop listening to events
hgs
parents:
diff changeset
   311
    iServer->BasebandConnectionManager()->Unsubscribe();
hgs
parents:
diff changeset
   312
        // Disconnect all links
hgs
parents:
diff changeset
   313
    TCallBack cb( CBTEngServer::DisconnectAllCallBack, iServer );
hgs
parents:
diff changeset
   314
    iServer->BasebandConnectionManager()->DisconnectAllLinksL( cb );
hgs
parents:
diff changeset
   315
        // Results in a callback (which is called directly when there are no links).
hgs
parents:
diff changeset
   316
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   317
    }
hgs
parents:
diff changeset
   318
hgs
parents:
diff changeset
   319
hgs
parents:
diff changeset
   320
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   321
// Update the power state CenRep key.
hgs
parents:
diff changeset
   322
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   323
//
hgs
parents:
diff changeset
   324
void CBTEngSrvSettingsMgr::UpdateCenRepPowerKeyL( TBTPowerState aState )
hgs
parents:
diff changeset
   325
    {
hgs
parents:
diff changeset
   326
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   327
    CRepository* cenrep = CRepository::NewLC( KCRUidBluetoothPowerState );
hgs
parents:
diff changeset
   328
    // TBTPowerState power state type is inverted from TBTPowerStateValue...
hgs
parents:
diff changeset
   329
    TBTPowerStateValue power = (TBTPowerStateValue) !aState;
hgs
parents:
diff changeset
   330
    User::LeaveIfError( cenrep->Set( KBTPowerState, (TInt) power ) );
hgs
parents:
diff changeset
   331
    CleanupStack::PopAndDestroy( cenrep );
hgs
parents:
diff changeset
   332
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   333
    }
hgs
parents:
diff changeset
   334
hgs
parents:
diff changeset
   335
hgs
parents:
diff changeset
   336
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   337
// ?implementation_description
hgs
parents:
diff changeset
   338
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   339
//
hgs
parents:
diff changeset
   340
void CBTEngSrvSettingsMgr::SetVisibilityModeL( TBTVisibilityMode aMode, TInt aTime )
hgs
parents:
diff changeset
   341
    {
hgs
parents:
diff changeset
   342
    TRACE_FUNC_ARG( ( _L( "[aMode: %d" ), aMode ) )
hgs
parents:
diff changeset
   343
    if ( aMode != EBTVisibilityModeNoScans && iEnterpriseEnablementMode == BluetoothFeatures::EDisabled )
hgs
parents:
diff changeset
   344
        {
hgs
parents:
diff changeset
   345
        TRACE_INFO( ( _L( "\tnot changing anything... Bluetooth is enterprise-IT-disabled" ) ) )
hgs
parents:
diff changeset
   346
        User::Leave( KErrNotSupported );
hgs
parents:
diff changeset
   347
        }
hgs
parents:
diff changeset
   348
hgs
parents:
diff changeset
   349
    TInt err = KErrNone;
hgs
parents:
diff changeset
   350
    iServer->RemoveTimer( CBTEngServer::EScanModeTimer );
hgs
parents:
diff changeset
   351
    if( aMode != EBTVisibilityModeNoScans )
hgs
parents:
diff changeset
   352
        {
hgs
parents:
diff changeset
   353
        CRepository* cenRep = CRepository::NewL( KCRUidBTEngPrivateSettings );
hgs
parents:
diff changeset
   354
        err = cenRep->Set( KBTDiscoverable, aMode );
hgs
parents:
diff changeset
   355
        delete cenRep;
hgs
parents:
diff changeset
   356
        }
hgs
parents:
diff changeset
   357
    if( !err && aMode == EBTVisibilityModeTemporary )
hgs
parents:
diff changeset
   358
        {
hgs
parents:
diff changeset
   359
            // We need TInt64 here, as the max. time in microseconds for the 
hgs
parents:
diff changeset
   360
            // max. value (1 hour) is larger than KMaxTInt32.
hgs
parents:
diff changeset
   361
        TInt64 timeMicroSec = MAKE_TINT64( 0, aTime );
hgs
parents:
diff changeset
   362
        timeMicroSec = timeMicroSec * KMinutesInMicroSecs;
hgs
parents:
diff changeset
   363
            // Queue callback to set the visibility back to hidden.
hgs
parents:
diff changeset
   364
        iServer->QueueTimer( CBTEngServer::EScanModeTimer, (TInt64) timeMicroSec );
hgs
parents:
diff changeset
   365
        aMode = EBTVisibilityModeGeneral;
hgs
parents:
diff changeset
   366
        }
hgs
parents:
diff changeset
   367
    else if( !err && iRestoreVisibility )
hgs
parents:
diff changeset
   368
        {
hgs
parents:
diff changeset
   369
            // The user overrides, do not restore visibility mode anymore.
hgs
parents:
diff changeset
   370
        iRestoreVisibility = EFalse;
hgs
parents:
diff changeset
   371
        }
hgs
parents:
diff changeset
   372
    if( !err )
hgs
parents:
diff changeset
   373
        {
hgs
parents:
diff changeset
   374
        err = RProperty::Set( KUidSystemCategory, 
hgs
parents:
diff changeset
   375
                               KPropertyKeyBluetoothSetScanningStatus, aMode );
hgs
parents:
diff changeset
   376
        }
hgs
parents:
diff changeset
   377
    TBool hiddenMode = ( aMode == EBTVisibilityModeHidden );
hgs
parents:
diff changeset
   378
    if( !err && aMode != EBTVisibilityModeNoScans )
hgs
parents:
diff changeset
   379
        {
hgs
parents:
diff changeset
   380
            // In hidden mode, we only accept connections from paired devices.
hgs
parents:
diff changeset
   381
        err = RProperty::Set( KUidSystemCategory, 
hgs
parents:
diff changeset
   382
                               KPropertyKeyBluetoothSetAcceptPairedOnlyMode, 
hgs
parents:
diff changeset
   383
                               hiddenMode );
hgs
parents:
diff changeset
   384
        }
hgs
parents:
diff changeset
   385
hgs
parents:
diff changeset
   386
    User::LeaveIfError( err );  // To communicate the result to the client.
hgs
parents:
diff changeset
   387
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   388
    }
hgs
parents:
diff changeset
   389
hgs
parents:
diff changeset
   390
hgs
parents:
diff changeset
   391
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   392
// Timed visible mode has expired.
hgs
parents:
diff changeset
   393
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   394
//
hgs
parents:
diff changeset
   395
void CBTEngSrvSettingsMgr::ScanModeTimerCompletedL()
hgs
parents:
diff changeset
   396
    {
hgs
parents:
diff changeset
   397
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   398
    SetVisibilityModeL( EBTVisibilityModeHidden, 0 );
hgs
parents:
diff changeset
   399
    TBTPowerState power = EBTOff;
hgs
parents:
diff changeset
   400
    TInt err = GetHwPowerState( power );
25
hgs
parents: 24
diff changeset
   401
    // See p2.1.2.1.2 of the UI spec
hgs
parents: 24
diff changeset
   402
    // NB:  Please distinguish between TBTPowerState::EBTOff (defined in hcitypes.h)
hgs
parents: 24
diff changeset
   403
    //      and TBTPowerStateValue::EBTPowerOff (defined in btserversdkcrkeys.h)
hgs
parents: 24
diff changeset
   404
    //      because they have the opposite values
hgs
parents: 24
diff changeset
   405
    if( !err && (power != EBTOff))
23
hgs
parents:
diff changeset
   406
        {
hgs
parents:
diff changeset
   407
        // Show a notification to the user
hgs
parents:
diff changeset
   408
        TBTGenericInfoNotiferParamsPckg pckg;
hgs
parents:
diff changeset
   409
        pckg().iMessageType = EBTVisibilityTimeout;
hgs
parents:
diff changeset
   410
        
hgs
parents:
diff changeset
   411
        RNotifier notifier;
hgs
parents:
diff changeset
   412
        TInt err = notifier.Connect();
hgs
parents:
diff changeset
   413
        if( !err )
hgs
parents:
diff changeset
   414
            {
hgs
parents:
diff changeset
   415
            err = notifier.StartNotifier( KBTGenericInfoNotifierUid, pckg );
hgs
parents:
diff changeset
   416
            notifier.Close();
hgs
parents:
diff changeset
   417
            }
hgs
parents:
diff changeset
   418
        }
hgs
parents:
diff changeset
   419
    TRACE_FUNC_RES( ( _L( "result: %d" ), err ) )
hgs
parents:
diff changeset
   420
    }
hgs
parents:
diff changeset
   421
hgs
parents:
diff changeset
   422
hgs
parents:
diff changeset
   423
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   424
// Check the secure simple pairing debug mode and turn it off if needed.
hgs
parents:
diff changeset
   425
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   426
//
hgs
parents:
diff changeset
   427
void CBTEngSrvSettingsMgr::CheckSspDebugModeL( TBool aDebugMode )
hgs
parents:
diff changeset
   428
    {
hgs
parents:
diff changeset
   429
    TRACE_FUNC_ARG( ( _L( "SSP debug mode state %d" ), (TInt) aDebugMode ) )
hgs
parents:
diff changeset
   430
    TBTPowerState pwr = EBTOff;
hgs
parents:
diff changeset
   431
    TBool currentMode = EFalse;
hgs
parents:
diff changeset
   432
    TInt err = RProperty::Get( KPropertyUidBluetoothCategory, 
hgs
parents:
diff changeset
   433
                                KPropertyKeyBluetoothGetSimplePairingDebugMode, 
hgs
parents:
diff changeset
   434
                                (TInt&) currentMode );
hgs
parents:
diff changeset
   435
#ifndef __WINS__
hgs
parents:
diff changeset
   436
    err = iPowerMgr.GetPower( pwr, NULL ); // Treat error as power off.
hgs
parents:
diff changeset
   437
#else   //__WINS__
hgs
parents:
diff changeset
   438
    pwr = iPowerState;
hgs
parents:
diff changeset
   439
#endif  //__WINS__
hgs
parents:
diff changeset
   440
    if( err || pwr == EBTOff )
hgs
parents:
diff changeset
   441
        {
hgs
parents:
diff changeset
   442
        iServer->RemoveTimer( CBTEngServer::ESspDebugModeTimer );
hgs
parents:
diff changeset
   443
            // Only set debug mode to off if it is on, to prevent a loop notifications.
hgs
parents:
diff changeset
   444
        if( currentMode )
hgs
parents:
diff changeset
   445
            {
hgs
parents:
diff changeset
   446
            (void) RProperty::Set( KPropertyUidBluetoothCategory, 
hgs
parents:
diff changeset
   447
                                    KPropertyKeyBluetoothSetSimplePairingDebugMode,
hgs
parents:
diff changeset
   448
                                    (TInt) aDebugMode );
hgs
parents:
diff changeset
   449
            }
hgs
parents:
diff changeset
   450
            // In case of an error in getting the power state, turn BT off. 
hgs
parents:
diff changeset
   451
            // If BT is already off, this call will be ignored.
hgs
parents:
diff changeset
   452
        SetPowerStateL( EBTOff, EFalse );
hgs
parents:
diff changeset
   453
        }
hgs
parents:
diff changeset
   454
    else if( aDebugMode )
hgs
parents:
diff changeset
   455
        {
hgs
parents:
diff changeset
   456
            // Ignore if there already is a timer queued.
hgs
parents:
diff changeset
   457
        if( !( iServer->IsTimerQueued( CBTEngServer::ESspDebugModeTimer ) ) )
hgs
parents:
diff changeset
   458
            {
hgs
parents:
diff changeset
   459
            iServer->QueueTimer( CBTEngServer::ESspDebugModeTimer, (TInt64) KBTEngSspDebugModeTimeout );
hgs
parents:
diff changeset
   460
            err =  RProperty::Set(KPropertyUidBluetoothCategory, 
hgs
parents:
diff changeset
   461
                                   KPropertyKeyBluetoothSetSimplePairingDebugMode, 
hgs
parents:
diff changeset
   462
                                   (TInt) aDebugMode );
hgs
parents:
diff changeset
   463
            }
hgs
parents:
diff changeset
   464
        }
hgs
parents:
diff changeset
   465
    else
hgs
parents:
diff changeset
   466
        {
hgs
parents:
diff changeset
   467
            // Power is on, and debug mode is set to off.
hgs
parents:
diff changeset
   468
        TInt linkCount = 0;
hgs
parents:
diff changeset
   469
        err = RProperty::Get( KPropertyUidBluetoothCategory, 
hgs
parents:
diff changeset
   470
                               KPropertyKeyBluetoothGetPHYCount, linkCount );
hgs
parents:
diff changeset
   471
        if( err || !linkCount )
hgs
parents:
diff changeset
   472
            {
hgs
parents:
diff changeset
   473
            (void) RProperty::Set(KPropertyUidBluetoothCategory, 
hgs
parents:
diff changeset
   474
                                   KPropertyKeyBluetoothSetSimplePairingDebugMode,
hgs
parents:
diff changeset
   475
                                   (TInt) aDebugMode );
hgs
parents:
diff changeset
   476
            SetPowerStateL( EBTOff, EFalse );
hgs
parents:
diff changeset
   477
            }
hgs
parents:
diff changeset
   478
        else
hgs
parents:
diff changeset
   479
            {
hgs
parents:
diff changeset
   480
            // There are still existing connections, queue the
hgs
parents:
diff changeset
   481
            // timer again for half the period.
hgs
parents:
diff changeset
   482
            TInt64 interval = KBTEngSspDebugModeTimeout / 2;
hgs
parents:
diff changeset
   483
            iServer->QueueTimer( CBTEngServer::ESspDebugModeTimer, interval );
hgs
parents:
diff changeset
   484
            }
hgs
parents:
diff changeset
   485
        }
hgs
parents:
diff changeset
   486
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   487
    }
hgs
parents:
diff changeset
   488
hgs
parents:
diff changeset
   489
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   490
// A session will be ended, completes the pending request for this session.
hgs
parents:
diff changeset
   491
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   492
//
hgs
parents:
diff changeset
   493
void CBTEngSrvSettingsMgr::SessionClosed( CSession2* aSession )
hgs
parents:
diff changeset
   494
    {
hgs
parents:
diff changeset
   495
    TRACE_FUNC_ARG( ( _L( " session %x"), aSession ) )
hgs
parents:
diff changeset
   496
    if ( !iMessage.IsNull() && iMessage.Session() == aSession )
hgs
parents:
diff changeset
   497
        {
hgs
parents:
diff changeset
   498
        iMessage.Complete( KErrCancel );
hgs
parents:
diff changeset
   499
        }
hgs
parents:
diff changeset
   500
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   501
    }
hgs
parents:
diff changeset
   502
hgs
parents:
diff changeset
   503
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   504
// Check the power state and if BT gets turned off automatically.
hgs
parents:
diff changeset
   505
// This method is invoked either when the timer has expired, or
hgs
parents:
diff changeset
   506
// if there are no more connections while the timer was running.
hgs
parents:
diff changeset
   507
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   508
//
hgs
parents:
diff changeset
   509
void CBTEngSrvSettingsMgr::CheckAutoPowerOffL()
hgs
parents:
diff changeset
   510
    {
hgs
parents:
diff changeset
   511
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   512
    if ( iAutoOffClients > 0 )
hgs
parents:
diff changeset
   513
        {
hgs
parents:
diff changeset
   514
        TRACE_INFO( ( _L( "[CBTEngServer]\t iAutoOffClients %d"), iAutoOffClients ) )
hgs
parents:
diff changeset
   515
        return;
hgs
parents:
diff changeset
   516
        }
hgs
parents:
diff changeset
   517
    TInt linkCount = 0;
hgs
parents:
diff changeset
   518
    TInt err = RProperty::Get( KPropertyUidBluetoothCategory, 
hgs
parents:
diff changeset
   519
                                KPropertyKeyBluetoothGetPHYCount, linkCount );
hgs
parents:
diff changeset
   520
    if( !err && !linkCount )
hgs
parents:
diff changeset
   521
        {
hgs
parents:
diff changeset
   522
        TRACE_INFO( ( _L( "[CBTEngServer]\t SetPowerStateL( EBTOff, EFalse );")))
hgs
parents:
diff changeset
   523
        SetPowerStateL( EBTOff, EFalse );
hgs
parents:
diff changeset
   524
        }
hgs
parents:
diff changeset
   525
    else
hgs
parents:
diff changeset
   526
        {
hgs
parents:
diff changeset
   527
        if( iRestoreVisibility )
hgs
parents:
diff changeset
   528
            {
hgs
parents:
diff changeset
   529
                // Set visibility mode back to the value selected by the user.
hgs
parents:
diff changeset
   530
            SetVisibilityModeL( EBTVisibilityModeGeneral, 0 );
hgs
parents:
diff changeset
   531
            iRestoreVisibility = EFalse;
hgs
parents:
diff changeset
   532
            }
hgs
parents:
diff changeset
   533
            // show note if non-audio connection exists
hgs
parents:
diff changeset
   534
        if ( !iServer->PluginManager()->CheckAudioConnectionsL() )
hgs
parents:
diff changeset
   535
            {
hgs
parents:
diff changeset
   536
            RNotifier notifier;
hgs
parents:
diff changeset
   537
            TInt err = notifier.Connect();
hgs
parents:
diff changeset
   538
            if( !err )
hgs
parents:
diff changeset
   539
                {
hgs
parents:
diff changeset
   540
                TRequestStatus status;
hgs
parents:
diff changeset
   541
                TBTGenericInfoNotiferParamsPckg pckg;
hgs
parents:
diff changeset
   542
                pckg().iMessageType = EBTStayPowerOn;
hgs
parents:
diff changeset
   543
                TBuf8<sizeof(TInt)> result;
hgs
parents:
diff changeset
   544
                //notifier.StartNotifier( KBTGenericInfoNotifierUid, pckg, result );
hgs
parents:
diff changeset
   545
                notifier.StartNotifierAndGetResponse( status, 
hgs
parents:
diff changeset
   546
                                                  KBTGenericInfoNotifierUid, 
hgs
parents:
diff changeset
   547
                                                  pckg, result );   // Reply buffer not used.
hgs
parents:
diff changeset
   548
                User::WaitForRequest( status );
hgs
parents:
diff changeset
   549
                notifier.Close();
hgs
parents:
diff changeset
   550
                }
hgs
parents:
diff changeset
   551
            }
hgs
parents:
diff changeset
   552
        iAutoOffClients = 0;
hgs
parents:
diff changeset
   553
        iAutoSwitchOff = EFalse;
hgs
parents:
diff changeset
   554
        TCallBack cb;
hgs
parents:
diff changeset
   555
        iServer->BasebandConnectionManager()->SetAutoSwitchOff( EFalse, cb );
hgs
parents:
diff changeset
   556
        }
hgs
parents:
diff changeset
   557
    TRACE_FUNC_EXIT 
hgs
parents:
diff changeset
   558
    }
hgs
parents:
diff changeset
   559
hgs
parents:
diff changeset
   560
hgs
parents:
diff changeset
   561
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   562
// The method is called when BT stack scanning mode P&S key is changed
hgs
parents:
diff changeset
   563
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   564
//
hgs
parents:
diff changeset
   565
void CBTEngSrvSettingsMgr::UpdateVisibilityModeL( TInt aStackScanMode )
hgs
parents:
diff changeset
   566
    {
hgs
parents:
diff changeset
   567
    TRACE_FUNC_ARG( ( _L( "[aStackScanMode: %d" ), aStackScanMode ) )
hgs
parents:
diff changeset
   568
    TBTVisibilityMode currentMode;
hgs
parents:
diff changeset
   569
hgs
parents:
diff changeset
   570
    CRepository* cenRep = CRepository::NewLC( KCRUidBTEngPrivateSettings );
hgs
parents:
diff changeset
   571
    User::LeaveIfError( cenRep->Get( KBTDiscoverable, (TInt&) currentMode ) );
hgs
parents:
diff changeset
   572
hgs
parents:
diff changeset
   573
        // In case we are in temp visibility mode, we cannot always know whether the BT stack
hgs
parents:
diff changeset
   574
        // scan mode key was set by some external party or by us in SetVisibilityModeL above.
hgs
parents:
diff changeset
   575
        // Therefore we cannot stop the timer in case aMode is EBTVisibilityModeGeneral and 
hgs
parents:
diff changeset
   576
        // currentmode is EBTVisibilityModeTemporary
hgs
parents:
diff changeset
   577
    if( !( currentMode == EBTVisibilityModeTemporary && aStackScanMode == EBTVisibilityModeGeneral ) )
hgs
parents:
diff changeset
   578
        {
hgs
parents:
diff changeset
   579
            // Cancel the timer and queue it again if needed.
hgs
parents:
diff changeset
   580
        iServer->RemoveTimer( CBTEngServer::EScanModeTimer );
hgs
parents:
diff changeset
   581
        if( currentMode != aStackScanMode )
hgs
parents:
diff changeset
   582
            {
hgs
parents:
diff changeset
   583
            if( aStackScanMode == EPageScanOnly || aStackScanMode == EInquiryAndPageScan )
hgs
parents:
diff changeset
   584
                {
hgs
parents:
diff changeset
   585
                User::LeaveIfError( cenRep->Set( KBTDiscoverable, aStackScanMode ) );
hgs
parents:
diff changeset
   586
                }
hgs
parents:
diff changeset
   587
            else if( aStackScanMode == EInquiryScanOnly )
hgs
parents:
diff changeset
   588
                {
hgs
parents:
diff changeset
   589
                    // We don't support ENoScansEnabled nor EInquiryScanOnly mode
hgs
parents:
diff changeset
   590
                    // -> Consider these as same as Hidden 
hgs
parents:
diff changeset
   591
                User::LeaveIfError( cenRep->Set( KBTDiscoverable, EBTVisibilityModeHidden ) );
hgs
parents:
diff changeset
   592
                }
hgs
parents:
diff changeset
   593
            else if( aStackScanMode == ENoScansEnabled )
hgs
parents:
diff changeset
   594
                {
hgs
parents:
diff changeset
   595
                //We don't change KBTDiscoverable here, because ENoScansEnabled
hgs
parents:
diff changeset
   596
                //indicates BT/SYSTEM shutdown is happening
hgs
parents:
diff changeset
   597
                }
hgs
parents:
diff changeset
   598
            }
hgs
parents:
diff changeset
   599
        }
hgs
parents:
diff changeset
   600
    SetUiIndicatorsL();
hgs
parents:
diff changeset
   601
    CleanupStack::PopAndDestroy( cenRep );
hgs
parents:
diff changeset
   602
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   603
    }
hgs
parents:
diff changeset
   604
hgs
parents:
diff changeset
   605
hgs
parents:
diff changeset
   606
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   607
// Set Device Under Test mode.
hgs
parents:
diff changeset
   608
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   609
//
hgs
parents:
diff changeset
   610
void CBTEngSrvSettingsMgr::SetDutMode( TInt aDutMode )
hgs
parents:
diff changeset
   611
    {
hgs
parents:
diff changeset
   612
    TRACE_FUNC_ARG( ( _L( "DUT mode %d" ), aDutMode ) )
hgs
parents:
diff changeset
   613
hgs
parents:
diff changeset
   614
    if (aDutMode == EBTDutOff)
hgs
parents:
diff changeset
   615
        {
hgs
parents:
diff changeset
   616
        return;
hgs
parents:
diff changeset
   617
        }
hgs
parents:
diff changeset
   618
hgs
parents:
diff changeset
   619
    TInt powerState = EBTOff;
hgs
parents:
diff changeset
   620
    CRepository* cenrep = NULL;
hgs
parents:
diff changeset
   621
    
hgs
parents:
diff changeset
   622
    TRAPD(err, cenrep = CRepository::NewL(KCRUidBluetoothPowerState));
hgs
parents:
diff changeset
   623
    
hgs
parents:
diff changeset
   624
    if (!err && cenrep)
hgs
parents:
diff changeset
   625
        {
hgs
parents:
diff changeset
   626
        cenrep->Get(KBTPowerState, powerState);
hgs
parents:
diff changeset
   627
        delete cenrep;
hgs
parents:
diff changeset
   628
        cenrep = NULL;
hgs
parents:
diff changeset
   629
        }
hgs
parents:
diff changeset
   630
    else
hgs
parents:
diff changeset
   631
        {
hgs
parents:
diff changeset
   632
        return;
hgs
parents:
diff changeset
   633
        }
hgs
parents:
diff changeset
   634
24
hgs
parents: 23
diff changeset
   635
    if (powerState == EBTPowerOn)
23
hgs
parents:
diff changeset
   636
        {
hgs
parents:
diff changeset
   637
hgs
parents:
diff changeset
   638
#ifndef __WINS__
hgs
parents:
diff changeset
   639
        RBluetoothDutMode dutMode;
hgs
parents:
diff changeset
   640
        TInt err = dutMode.Open();
hgs
parents:
diff changeset
   641
        TRACE_FUNC_ARG( ( _L( "Open DUT mode handle err %d" ), err) )
hgs
parents:
diff changeset
   642
        if(!err) 
hgs
parents:
diff changeset
   643
            {
hgs
parents:
diff changeset
   644
            dutMode.ActivateDutMode();
hgs
parents:
diff changeset
   645
            dutMode.Close();
hgs
parents:
diff changeset
   646
            }
hgs
parents:
diff changeset
   647
#endif  //__WINS__                        
hgs
parents:
diff changeset
   648
        }
hgs
parents:
diff changeset
   649
    }
hgs
parents:
diff changeset
   650
hgs
parents:
diff changeset
   651
hgs
parents:
diff changeset
   652
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   653
// From class MBTEngActiveObserver.
hgs
parents:
diff changeset
   654
// Callback to notify that an outstanding request has completed.
hgs
parents:
diff changeset
   655
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   656
//
hgs
parents:
diff changeset
   657
void CBTEngSrvSettingsMgr::RequestCompletedL( CBTEngActive* aActive, TInt aId, TInt aStatus )
hgs
parents:
diff changeset
   658
    {
hgs
parents:
diff changeset
   659
    __ASSERT_ALWAYS( aId == KBTEngSettingsActive, PanicServer( EBTEngPanicCorrupt ) );
hgs
parents:
diff changeset
   660
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   661
    (void) aActive;
hgs
parents:
diff changeset
   662
    if ( aStatus != KErrNone && aStatus != KErrAlreadyExists && aStatus != KErrCancel )
hgs
parents:
diff changeset
   663
        {
hgs
parents:
diff changeset
   664
        // Something went wrong, so we turn BT off again.
hgs
parents:
diff changeset
   665
        SetPowerStateL( EBTOff, EFalse );
hgs
parents:
diff changeset
   666
        }
26
hgs
parents: 25
diff changeset
   667
    
23
hgs
parents:
diff changeset
   668
    if ( !iMessage.IsNull())
hgs
parents:
diff changeset
   669
        {
hgs
parents:
diff changeset
   670
        iMessage.Complete( aStatus );
hgs
parents:
diff changeset
   671
        }
hgs
parents:
diff changeset
   672
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   673
    }
hgs
parents:
diff changeset
   674
hgs
parents:
diff changeset
   675
hgs
parents:
diff changeset
   676
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   677
// From class MBTEngActiveObserver.
hgs
parents:
diff changeset
   678
// Callback to notify that an error has occurred in RunL.
hgs
parents:
diff changeset
   679
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   680
//
hgs
parents:
diff changeset
   681
void CBTEngSrvSettingsMgr::HandleError( CBTEngActive* aActive, TInt aId, TInt aError )
hgs
parents:
diff changeset
   682
    {
hgs
parents:
diff changeset
   683
    (void) aActive;
hgs
parents:
diff changeset
   684
    (void) aId;
hgs
parents:
diff changeset
   685
    if ( !iMessage.IsNull())
hgs
parents:
diff changeset
   686
        {
hgs
parents:
diff changeset
   687
        iMessage.Complete( aError );
hgs
parents:
diff changeset
   688
        }
hgs
parents:
diff changeset
   689
    }
hgs
parents:
diff changeset
   690
hgs
parents:
diff changeset
   691
hgs
parents:
diff changeset
   692
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   693
// Loads the BT Power Manager; leaves if it cannot be loaded.
hgs
parents:
diff changeset
   694
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   695
//
hgs
parents:
diff changeset
   696
void CBTEngSrvSettingsMgr::LoadBTPowerManagerL()
hgs
parents:
diff changeset
   697
    {
hgs
parents:
diff changeset
   698
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   699
    TRACE_INFO( ( _L( "[CBTEngSrvSettingsMgr]\t Using HCI API v2 power manager" ) ) )
hgs
parents:
diff changeset
   700
    User::LeaveIfError( iPowerMgr.Open() );
hgs
parents:
diff changeset
   701
#ifndef __WINS__
hgs
parents:
diff changeset
   702
    iPowerMgr.SetPower( EBTOff, NULL, iActive->RequestStatus() );
hgs
parents:
diff changeset
   703
    User::WaitForRequest( iActive->RequestStatus() );
hgs
parents:
diff changeset
   704
    TInt status = ( iActive->RequestStatus().Int() == KErrAlreadyExists ? KErrNone : iActive->RequestStatus().Int() ); 
hgs
parents:
diff changeset
   705
    User::LeaveIfError( status );
hgs
parents:
diff changeset
   706
#else   //__WINS__
hgs
parents:
diff changeset
   707
    iPowerState = EBTOff;
hgs
parents:
diff changeset
   708
#endif  //__WINS__
hgs
parents:
diff changeset
   709
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   710
    }
hgs
parents:
diff changeset
   711
hgs
parents:
diff changeset
   712
hgs
parents:
diff changeset
   713
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   714
// ?implementation_description
hgs
parents:
diff changeset
   715
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   716
//
hgs
parents:
diff changeset
   717
void CBTEngSrvSettingsMgr::SetUiIndicatorsL()
hgs
parents:
diff changeset
   718
    {
hgs
parents:
diff changeset
   719
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   720
    TBTPowerStateValue powerState = EBTPowerOff;
hgs
parents:
diff changeset
   721
    TBTVisibilityMode visibilityMode = EBTVisibilityModeHidden;
hgs
parents:
diff changeset
   722
    CRepository* cenrep = NULL;
hgs
parents:
diff changeset
   723
    TInt phys = 0;
hgs
parents:
diff changeset
   724
    TInt connecting = 0;
hgs
parents:
diff changeset
   725
hgs
parents:
diff changeset
   726
    cenrep = CRepository::NewLC( KCRUidBluetoothPowerState );
hgs
parents:
diff changeset
   727
    User::LeaveIfError( cenrep->Get( KBTPowerState, (TInt&) powerState ) );
hgs
parents:
diff changeset
   728
    CleanupStack::PopAndDestroy( cenrep );
hgs
parents:
diff changeset
   729
    
hgs
parents:
diff changeset
   730
    if( powerState == EBTPowerOff )
hgs
parents:
diff changeset
   731
        {
hgs
parents:
diff changeset
   732
        SetIndicatorStateL( EAknIndicatorBluetoothModuleOn, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   733
        SetIndicatorStateL( EAknIndicatorBluetooth, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   734
        SetIndicatorStateL( EAknIndicatorBluetoothModuleOnVisible, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   735
        SetIndicatorStateL( EAknIndicatorBluetoothVisible, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   736
        }
hgs
parents:
diff changeset
   737
    else
hgs
parents:
diff changeset
   738
        {
hgs
parents:
diff changeset
   739
        // Power is on.
hgs
parents:
diff changeset
   740
        RProperty::Get( KPropertyUidBluetoothCategory, KPropertyKeyBluetoothPHYCount, phys );
hgs
parents:
diff changeset
   741
        RProperty::Get( KPropertyUidBluetoothCategory, KPropertyKeyBluetoothConnecting, connecting );
hgs
parents:
diff changeset
   742
        
hgs
parents:
diff changeset
   743
        cenrep = CRepository::NewLC( KCRUidBTEngPrivateSettings );
hgs
parents:
diff changeset
   744
        User::LeaveIfError( cenrep->Get( KBTDiscoverable, (TInt&) visibilityMode ) );
hgs
parents:
diff changeset
   745
        CleanupStack::PopAndDestroy( cenrep );
hgs
parents:
diff changeset
   746
        
hgs
parents:
diff changeset
   747
        if( visibilityMode == EBTVisibilityModeHidden )
hgs
parents:
diff changeset
   748
            {
hgs
parents:
diff changeset
   749
             if ( connecting ) // BT connecting and hidden
hgs
parents:
diff changeset
   750
                {
hgs
parents:
diff changeset
   751
                SetIndicatorStateL( EAknIndicatorBluetoothModuleOn, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   752
                SetIndicatorStateL( EAknIndicatorBluetooth, EAknIndicatorStateAnimate );
hgs
parents:
diff changeset
   753
                }
hgs
parents:
diff changeset
   754
            else if ( phys > 0 ) // BT connection active and hidden     
hgs
parents:
diff changeset
   755
                {
hgs
parents:
diff changeset
   756
                SetIndicatorStateL( EAknIndicatorBluetoothModuleOn, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   757
                SetIndicatorStateL( EAknIndicatorBluetooth, EAknIndicatorStateOn );
hgs
parents:
diff changeset
   758
                }
hgs
parents:
diff changeset
   759
            else  // BT connection not active and hidden
hgs
parents:
diff changeset
   760
                {
hgs
parents:
diff changeset
   761
                SetIndicatorStateL( EAknIndicatorBluetoothModuleOn, EAknIndicatorStateOn );
hgs
parents:
diff changeset
   762
                SetIndicatorStateL( EAknIndicatorBluetooth, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   763
                }
hgs
parents:
diff changeset
   764
            SetIndicatorStateL( EAknIndicatorBluetoothModuleOnVisible, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   765
            SetIndicatorStateL( EAknIndicatorBluetoothVisible, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   766
            }           
hgs
parents:
diff changeset
   767
        else if( visibilityMode == EBTVisibilityModeGeneral || visibilityMode == EBTVisibilityModeTemporary )
hgs
parents:
diff changeset
   768
            {     
hgs
parents:
diff changeset
   769
            if ( connecting ) // BT connecting and visible
hgs
parents:
diff changeset
   770
                {
hgs
parents:
diff changeset
   771
                SetIndicatorStateL( EAknIndicatorBluetoothModuleOnVisible, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   772
                SetIndicatorStateL( EAknIndicatorBluetoothVisible, EAknIndicatorStateAnimate );
hgs
parents:
diff changeset
   773
                }
hgs
parents:
diff changeset
   774
            else if ( phys > 0 ) // BT connection active and visible 
hgs
parents:
diff changeset
   775
                {
hgs
parents:
diff changeset
   776
                SetIndicatorStateL( EAknIndicatorBluetoothModuleOnVisible, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   777
                SetIndicatorStateL( EAknIndicatorBluetoothVisible, EAknIndicatorStateOn );
hgs
parents:
diff changeset
   778
                }
hgs
parents:
diff changeset
   779
            else  // BT connection not active and visible
hgs
parents:
diff changeset
   780
                {
hgs
parents:
diff changeset
   781
                SetIndicatorStateL( EAknIndicatorBluetoothModuleOnVisible, EAknIndicatorStateOn );
hgs
parents:
diff changeset
   782
                SetIndicatorStateL( EAknIndicatorBluetoothVisible, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   783
                }
hgs
parents:
diff changeset
   784
            SetIndicatorStateL( EAknIndicatorBluetoothModuleOn, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   785
            SetIndicatorStateL( EAknIndicatorBluetooth, EAknIndicatorStateOff );
hgs
parents:
diff changeset
   786
            }
hgs
parents:
diff changeset
   787
        }
hgs
parents:
diff changeset
   788
    TRACE_FUNC_EXIT
hgs
parents:
diff changeset
   789
    }
hgs
parents:
diff changeset
   790
hgs
parents:
diff changeset
   791
hgs
parents:
diff changeset
   792
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   793
// ?implementation_description
hgs
parents:
diff changeset
   794
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   795
//
hgs
parents:
diff changeset
   796
void CBTEngSrvSettingsMgr::SetIndicatorStateL( const TInt aIndicator, const TInt aState )
hgs
parents:
diff changeset
   797
    {
hgs
parents:
diff changeset
   798
    CAknSmallIndicator* indicator = CAknSmallIndicator::NewLC( TUid::Uid( aIndicator ) );
hgs
parents:
diff changeset
   799
    indicator->SetIndicatorStateL( aState );
hgs
parents:
diff changeset
   800
    CleanupStack::PopAndDestroy( indicator ); //indicator
hgs
parents:
diff changeset
   801
    }
hgs
parents:
diff changeset
   802
hgs
parents:
diff changeset
   803
hgs
parents:
diff changeset
   804
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   805
// Gets the current HW power state.
hgs
parents:
diff changeset
   806
// For now this is a separate method to isolate the different variations.
hgs
parents:
diff changeset
   807
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   808
//
hgs
parents:
diff changeset
   809
TInt CBTEngSrvSettingsMgr::GetHwPowerState( TBTPowerState& aState )
hgs
parents:
diff changeset
   810
    {
hgs
parents:
diff changeset
   811
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   812
    TInt err = KErrNone;
hgs
parents:
diff changeset
   813
    
hgs
parents:
diff changeset
   814
#ifndef __WINS__
hgs
parents:
diff changeset
   815
    err = iPowerMgr.GetPower( aState, NULL );
hgs
parents:
diff changeset
   816
#else   //__WINS__
hgs
parents:
diff changeset
   817
    aState = iPowerState;
hgs
parents:
diff changeset
   818
#endif  //__WINS__
hgs
parents:
diff changeset
   819
    TRACE_FUNC_ARG( ( _L( "Power state is %d, result %d" ), (TInt) aState, err ) )
hgs
parents:
diff changeset
   820
    return err;
hgs
parents:
diff changeset
   821
    }
hgs
parents:
diff changeset
   822
hgs
parents:
diff changeset
   823
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   824
// Check the power state and if BT gets turned off automatically.
hgs
parents:
diff changeset
   825
// ---------------------------------------------------------------------------
hgs
parents:
diff changeset
   826
//
hgs
parents:
diff changeset
   827
void CBTEngSrvSettingsMgr::CheckTemporaryPowerStateL( TBTPowerState& aCurrentState, 
hgs
parents:
diff changeset
   828
    TBTPowerState aNewState, TBool aTemporary )
hgs
parents:
diff changeset
   829
    {
hgs
parents:
diff changeset
   830
    TRACE_FUNC_ENTRY
hgs
parents:
diff changeset
   831
    User::LeaveIfError( GetHwPowerState( aCurrentState ) );
hgs
parents:
diff changeset
   832
    if( !aTemporary )
hgs
parents:
diff changeset
   833
        {
hgs
parents:
diff changeset
   834
            // Force the new power state, so clear all auto switch off flags.
hgs
parents:
diff changeset
   835
            // If power is off, this will anyway be ignored.
hgs
parents:
diff changeset
   836
        iAutoOffClients = 0;
hgs
parents:
diff changeset
   837
        iAutoSwitchOff = EFalse;
hgs
parents:
diff changeset
   838
        TCallBack cb;
hgs
parents:
diff changeset
   839
        iServer->BasebandConnectionManager()->SetAutoSwitchOff( EFalse, cb );
hgs
parents:
diff changeset
   840
        if( iRestoreVisibility && aCurrentState == EBTOn )
hgs
parents:
diff changeset
   841
            {
hgs
parents:
diff changeset
   842
                // Set visibility mode back to the value selected by the user.
hgs
parents:
diff changeset
   843
            SetVisibilityModeL( EBTVisibilityModeGeneral, 0 );
hgs
parents:
diff changeset
   844
            iRestoreVisibility = EFalse;
hgs
parents:
diff changeset
   845
            }
hgs
parents:
diff changeset
   846
        }
hgs
parents:
diff changeset
   847
    else
hgs
parents:
diff changeset
   848
        {
hgs
parents:
diff changeset
   849
        if( aCurrentState == aNewState )
hgs
parents:
diff changeset
   850
            {
hgs
parents:
diff changeset
   851
            if( iAutoSwitchOff && aNewState == EBTOn )
hgs
parents:
diff changeset
   852
                {
hgs
parents:
diff changeset
   853
                iAutoOffClients++;
hgs
parents:
diff changeset
   854
                iServer->RemoveTimer( CBTEngServer::EAutoPowerOffTimer );
hgs
parents:
diff changeset
   855
                }
hgs
parents:
diff changeset
   856
            }
hgs
parents:
diff changeset
   857
        else if( iAutoSwitchOff || aNewState == EBTOn )
hgs
parents:
diff changeset
   858
            {
hgs
parents:
diff changeset
   859
            aNewState == EBTOff ? iAutoOffClients-- : iAutoOffClients++;
hgs
parents:
diff changeset
   860
            iAutoSwitchOff = ETrue;
hgs
parents:
diff changeset
   861
            if( aNewState == EBTOff && iAutoOffClients <= 0 )
hgs
parents:
diff changeset
   862
                {
hgs
parents:
diff changeset
   863
                TCallBack powerOffCb( CBTEngServer::AutoPowerOffCallBack, iServer );
hgs
parents:
diff changeset
   864
                iServer->BasebandConnectionManager()->SetAutoSwitchOff( ETrue, powerOffCb );
hgs
parents:
diff changeset
   865
                TInt64 interval = KBTEngBtAutoOffTimeout;
hgs
parents:
diff changeset
   866
                iServer->QueueTimer( CBTEngServer::EAutoPowerOffTimer, interval );
hgs
parents:
diff changeset
   867
                }
hgs
parents:
diff changeset
   868
            else if( aNewState == EBTOn )
hgs
parents:
diff changeset
   869
                {
hgs
parents:
diff changeset
   870
                CRepository* cenRep = CRepository::NewLC( KCRUidBTEngPrivateSettings );
hgs
parents:
diff changeset
   871
                TBTVisibilityMode visibility = EBTVisibilityModeGeneral;
hgs
parents:
diff changeset
   872
                TInt err = cenRep->Get( KBTDiscoverable, (TInt&) visibility );
hgs
parents:
diff changeset
   873
                CleanupStack::PopAndDestroy( cenRep );
hgs
parents:
diff changeset
   874
                if( !err && visibility == EBTVisibilityModeGeneral )
hgs
parents:
diff changeset
   875
                    {
hgs
parents:
diff changeset
   876
                    SetVisibilityModeL( EBTVisibilityModeHidden, 0 );
hgs
parents:
diff changeset
   877
                    iRestoreVisibility = ETrue;
hgs
parents:
diff changeset
   878
                   }
hgs
parents:
diff changeset
   879
                iServer->RemoveTimer( CBTEngServer::EAutoPowerOffTimer );
hgs
parents:
diff changeset
   880
                }
hgs
parents:
diff changeset
   881
            }
hgs
parents:
diff changeset
   882
        }
hgs
parents:
diff changeset
   883
    if( iAutoOffClients < 0 )
hgs
parents:
diff changeset
   884
        {
hgs
parents:
diff changeset
   885
        iAutoOffClients = 0;
hgs
parents:
diff changeset
   886
        }
hgs
parents:
diff changeset
   887
    TRACE_FUNC_EXIT 
hgs
parents:
diff changeset
   888
    }