bluetoothengine/bteng/src/btengpairman.cpp
changeset 0 f63038272f30
child 10 0707dd69d236
child 15 00f9ee97d895
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Pairing result receiver in Bluetooth engine subsystem
       
    15 *
       
    16 */
       
    17 
       
    18 #include "btengpairman.h"
       
    19 #include "btengserver.h"
       
    20 #include "btengsrvsession.h"
       
    21 #include "btengotgpair.h"
       
    22 #include "btengincpair.h"
       
    23 #include "btengclientserver.h"
       
    24 #include "debug.h"
       
    25 #include <e32property.h>
       
    26 
       
    27 /**  Identification for active object */
       
    28 enum TPairManActiveRequestId
       
    29     {
       
    30     ESimplePairingResult,
       
    31     EAuthenticationResult,
       
    32     ERegistryInitiatePairedDevicesView,
       
    33     ERegistryPairedDevicesNewView,
       
    34     ERegistryInitiatePairedDevicesList,
       
    35     ERegistryGetPairedDevices,
       
    36     };
       
    37 
       
    38 /**  The message argument which holds the Bluetooth address. */
       
    39 const TInt KBTEngAddrSlot = 0;
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // Tells if two TBTNamelessDevice instances are for the same remote device
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 TBool CompareDeviceByAddress( const TBTNamelessDevice& aDevA, const TBTNamelessDevice& aDevB )
       
    46     {
       
    47     return aDevA.Address() == aDevB.Address();
       
    48     }
       
    49 
       
    50 // ======== MEMBER FUNCTIONS ========
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // C++ default constructor
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CBTEngPairMan::CBTEngPairMan( CBTEngServer& aServer )
       
    57     : iServer( aServer )
       
    58     {
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // Symbian 2nd-phase constructor
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 void CBTEngPairMan::ConstructL()
       
    66     {
       
    67     TRACE_FUNC_ENTRY
       
    68         // Connect to pairing server for authentication & simple pairing 
       
    69         // results directly from the BT stack.
       
    70         // Pairing server doesn't exist if we run BT 2.0 stack:
       
    71     iPairingServ = new (ELeave) RBluetoothPairingServer;
       
    72     TInt err = iPairingServ->Connect();
       
    73     if ( err)
       
    74         {
       
    75         delete iPairingServ;
       
    76         iPairingServ = NULL;
       
    77         }
       
    78     else
       
    79         {
       
    80         User::LeaveIfError( iPairingResult.Open( *iPairingServ ) );
       
    81         User::LeaveIfError( iAuthenResult.Open( *iPairingServ ) );
       
    82         iSSPResultActive = CBTEngActive::NewL( *this, ESimplePairingResult, CActive::EPriorityStandard );
       
    83         iAuthenResultActive = CBTEngActive::NewL( *this, EAuthenticationResult, CActive::EPriorityStandard );        
       
    84         SubscribeSspPairingResult();
       
    85         SubscribeAuthenticateResult();
       
    86         }
       
    87 
       
    88     // connect to registry
       
    89     User::LeaveIfError( iBTRegistry.Open( BTRegServ() ) );    
       
    90     iRegistryActive = CBTEngActive::NewL( *this, ERegistryInitiatePairedDevicesView, CActive::EPriorityStandard );
       
    91     // Start to get the list of all paired devices.
       
    92     CreatePairedDevicesView( ERegistryInitiatePairedDevicesView );
       
    93     iPairedDevices = new (ELeave) RArray<TBTNamelessDevice>;
       
    94     TRACE_FUNC_EXIT
       
    95     }
       
    96 
       
    97 // ---------------------------------------------------------------------------
       
    98 // NewL
       
    99 // ---------------------------------------------------------------------------
       
   100 //
       
   101 CBTEngPairMan* CBTEngPairMan::NewL( CBTEngServer& aServer )
       
   102     {
       
   103     CBTEngPairMan* self = NULL;
       
   104     self = new  CBTEngPairMan( aServer );
       
   105     CleanupStack::PushL( self );
       
   106     self->ConstructL();
       
   107     CleanupStack::Pop( self );
       
   108     return self;
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Destructor
       
   113 // ---------------------------------------------------------------------------
       
   114 //
       
   115 CBTEngPairMan::~CBTEngPairMan()
       
   116     {
       
   117     TRACE_FUNC_ENTRY
       
   118     CancelSubscribe();
       
   119     delete iSSPResultActive;
       
   120     delete iAuthenResultActive;
       
   121     delete iRegistryActive;
       
   122     delete iPairedDevicesResp;
       
   123     delete iPairer;
       
   124     if ( iPairedDevices )
       
   125         {
       
   126         iPairedDevices->Close();
       
   127         delete iPairedDevices;
       
   128         }
       
   129     iBTRegistry.Close();
       
   130     iPairingResult.Close();
       
   131     iAuthenResult.Close();
       
   132     if ( iPairingServ )
       
   133         {
       
   134         iPairingServ->Close();
       
   135         delete iPairingServ;
       
   136         }
       
   137     TRACE_FUNC_EXIT
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // Handles pairing related commands from BTEng clients.
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 void CBTEngPairMan::ProcessCommandL( const RMessage2& aMessage )
       
   145     {
       
   146     TRACE_FUNC_ENTRY
       
   147     TInt opcode = aMessage.Function();
       
   148     TBTDevAddrPckgBuf addrPkg;
       
   149     switch( opcode )
       
   150         {
       
   151         case EBTEngSetPairingObserver:
       
   152             {
       
   153             aMessage.ReadL( KBTEngAddrSlot, addrPkg );
       
   154             SetPairObserver( addrPkg(), aMessage.Int1() );
       
   155             break;
       
   156             }
       
   157         case EBTEngPairDevice:
       
   158             {
       
   159             TBTDevAddrPckgBuf addrPkg;
       
   160             aMessage.ReadL( KBTEngAddrSlot, addrPkg );
       
   161             PairDeviceL( addrPkg(), aMessage.Int1() );
       
   162             break;
       
   163             }
       
   164         case EBTEngCancelPairDevice:
       
   165             {
       
   166             CancelCommand( opcode );
       
   167             break;
       
   168             }
       
   169         default:
       
   170             {
       
   171             TRACE_INFO( ( _L( "CBTEngPairMan ProcessCommandL: bad request (%d)" ), 
       
   172                            aMessage.Function() ) )
       
   173             User::Leave( KErrArgument );
       
   174             }
       
   175         }
       
   176     TRACE_FUNC_EXIT    
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // Cancels outgoing pairing requests
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 void CBTEngPairMan::CancelCommand( TInt aOpCode )
       
   184     {
       
   185     switch( aOpCode )
       
   186         {
       
   187         case EBTEngPairDevice:
       
   188             {
       
   189             TRACE_FUNC_ENTRY
       
   190             if ( iPairer )
       
   191                 {
       
   192                 iPairer->CancelOutgoingPair();
       
   193                 }
       
   194             TRACE_FUNC_EXIT
       
   195             break;
       
   196             }
       
   197         }
       
   198     }
       
   199 
       
   200 // ---------------------------------------------------------------------------
       
   201 // Handle a change in BTRegistry remote devices table.
       
   202 // ---------------------------------------------------------------------------
       
   203 //
       
   204 void CBTEngPairMan::RemoteRegistryChangeDetected()
       
   205     {
       
   206     if ( !iRegistryActive->IsActive() )
       
   207         {
       
   208         CreatePairedDevicesView( ERegistryPairedDevicesNewView );
       
   209         }
       
   210     else
       
   211         {
       
   212         iNotHandledRegEventCounter++;
       
   213         }
       
   214     }
       
   215 
       
   216 // ---------------------------------------------------------------------------
       
   217 // Returns the RBluetoothPairingServer instance.
       
   218 // ---------------------------------------------------------------------------
       
   219 //
       
   220 RBluetoothPairingServer* CBTEngPairMan::PairingServer()
       
   221     {
       
   222     return iPairingServ;
       
   223     }
       
   224 
       
   225 // ---------------------------------------------------------------------------
       
   226 // Access the reference of RSockServ
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 RSocketServ& CBTEngPairMan::SocketServ()
       
   230     {
       
   231     return iServer.SocketServ();
       
   232     }
       
   233 
       
   234 // ---------------------------------------------------------------------------
       
   235 // Access the reference of RBTRegSrv
       
   236 // ---------------------------------------------------------------------------
       
   237 //
       
   238 RBTRegServ& CBTEngPairMan::BTRegServ()
       
   239     {
       
   240     return iServer.BTRegServ();
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // Deletes the current pairing handler and transfer the responsibility
       
   245 // to the specified.
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 void CBTEngPairMan::RenewPairer( CBTEngPairBase* aPairer )
       
   249     {
       
   250     delete iPairer;
       
   251     iPairer = aPairer;
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // Find the session who requested this and completes its request.
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 void CBTEngPairMan::OutgoingPairCompleted( TInt aErr )
       
   259     {
       
   260     TRACE_FUNC_ENTRY
       
   261     // the meaning of KHCIErrorBase equals KErrNone. Hide this specific BT stack
       
   262 	// detail from clients:
       
   263     if ( aErr == KHCIErrorBase )
       
   264         {
       
   265         aErr = KErrNone;
       
   266         }
       
   267     // we must complete client's pairing request:
       
   268     iServer.iSessionIter.SetToLast();
       
   269     CBTEngSrvSession* session = (CBTEngSrvSession*) iServer.iSessionIter--;
       
   270     TInt ret( KErrNotFound );
       
   271     while( session && ret )
       
   272         {
       
   273         ret = session->CompletePairRequest( aErr );
       
   274         session = (CBTEngSrvSession*) iServer.iSessionIter--;
       
   275         }
       
   276     TRACE_FUNC_EXIT
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // Unpair the device from registry
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 void CBTEngPairMan::UnpairDevice( const TBTDevAddr& aAddr )
       
   284     {
       
   285     TRACE_FUNC_ENTRY
       
   286     TIdentityRelation<TBTNamelessDevice> addrComp( CompareDeviceByAddress );
       
   287     TBTNamelessDevice dev;
       
   288     dev.SetAddress( aAddr );
       
   289     // only do unpairing if the we have a link key with it.
       
   290     TInt index = iPairedDevices->Find( dev, addrComp );
       
   291     if ( index > KErrNotFound )
       
   292         {
       
   293         dev = (*iPairedDevices)[index];
       
   294         TBTDeviceSecurity security = dev.GlobalSecurity();
       
   295         // Clear trust setting so that correct icon will be shown in ui applications.
       
   296         security.SetNoAuthenticate(EFalse );
       
   297         security.SetNoAuthorise(EFalse );
       
   298         dev.SetGlobalSecurity(security);
       
   299         dev.DeleteLinkKey();
       
   300         if ( dev.IsValidUiCookie() && 
       
   301              ( dev.UiCookie() & EBTUiCookieJustWorksPaired ) )
       
   302             {
       
   303             // Remove the UI cookie bit for Just Works pairing.
       
   304             TInt32 cookie = dev.UiCookie() & ~EBTUiCookieJustWorksPaired;
       
   305             dev.SetUiCookie( cookie );
       
   306             TRACE_INFO( ( _L( "UI cookie %x cleared"), EBTUiCookieJustWorksPaired ) );
       
   307             }
       
   308         // modify the device in registry synchronously
       
   309         // status.Int() could be -1 if the device is not in registry 
       
   310         // which is totally fine for us.
       
   311         (void) UpdateRegDevice( dev );
       
   312         }
       
   313     TRACE_FUNC_EXIT
       
   314     }
       
   315 
       
   316 TInt CBTEngPairMan::AddUiCookieJustWorksPaired( const TBTNamelessDevice& aDev )
       
   317     {
       
   318     TInt err( KErrNone );
       
   319     // There might be UI cookies used by other applications,
       
   320     // we should not overwrite them. 
       
   321     TInt32 cookie = aDev.IsValidUiCookie() ? aDev.UiCookie() : EBTUiCookieUndefined;
       
   322     if ( !( cookie & EBTUiCookieJustWorksPaired ) )
       
   323         {
       
   324         // Only update the cookie if the wanted one is not in registry yet
       
   325         // to keep minimal operations with registry.
       
   326         TBTNamelessDevice dev = aDev;		
       
   327         cookie |= EBTUiCookieJustWorksPaired;
       
   328         dev.SetUiCookie( cookie );
       
   329         err = UpdateRegDevice( dev );
       
   330         TRACE_INFO( ( _L( "[BTENG] CBTEngOtgPair write Ui cookie ret %d"), err ) );
       
   331         }
       
   332     return err;
       
   333     }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // update a nameless device in registry
       
   337 // ---------------------------------------------------------------------------
       
   338 //
       
   339 TInt CBTEngPairMan::UpdateRegDevice( const TBTNamelessDevice& aDev )
       
   340     {
       
   341     TRequestStatus status( KRequestPending );
       
   342     // update the device in registry synchronously
       
   343     iBTRegistry.ModifyDevice( aDev, status );
       
   344     User::WaitForRequest( status );
       
   345     TRACE_INFO( ( _L( "UpdateRegDevice, ret %d"), status.Int() ) )
       
   346     return status.Int();
       
   347     }
       
   348 
       
   349 // ---------------------------------------------------------------------------
       
   350 // Ask server class the connection status of the specified device
       
   351 // ---------------------------------------------------------------------------
       
   352 //
       
   353 TBTEngConnectionStatus CBTEngPairMan::IsDeviceConnected( const TBTDevAddr& aAddr )
       
   354     {
       
   355     return iServer.IsDeviceConnected( aAddr );
       
   356     }
       
   357 
       
   358 // ---------------------------------------------------------------------------
       
   359 // From class MBTEngActiveObserver.
       
   360 // Checks if there is an authentication result.
       
   361 // ---------------------------------------------------------------------------
       
   362 //
       
   363 void CBTEngPairMan::RequestCompletedL( CBTEngActive* /*aActive*/, TInt aId, TInt aStatus )
       
   364     {
       
   365     TRACE_FUNC_ARG( ( _L( "aId: %d, aStatus: %d"), aId, aStatus ) )
       
   366         // Check which request completed.
       
   367     switch( aId )
       
   368         {
       
   369         case ESimplePairingResult:
       
   370             {
       
   371             TBTDevAddr tmpAddr = iSimplePairingRemote;
       
   372             if (aStatus != KErrServerTerminated)
       
   373                 {
       
   374                 SubscribeSspPairingResult();
       
   375                 }
       
   376             HandlePairingResultL( tmpAddr, aStatus );
       
   377             break;
       
   378             }
       
   379         case EAuthenticationResult:
       
   380             {
       
   381             TBTDevAddr tmpAddr = iAuthenticateRemote;
       
   382             if (aStatus != KErrServerTerminated)
       
   383                 {
       
   384                 SubscribeAuthenticateResult();
       
   385                 }
       
   386             HandlePairingResultL( tmpAddr, aStatus );
       
   387             break;
       
   388             }
       
   389         case ERegistryInitiatePairedDevicesView:
       
   390         case ERegistryPairedDevicesNewView:
       
   391             {
       
   392             HandleCreatePairedDevicesViewCompletedL( aStatus, aId );
       
   393             break;
       
   394             }
       
   395         case ERegistryInitiatePairedDevicesList:
       
   396         case ERegistryGetPairedDevices:    
       
   397             {
       
   398             HandleGetPairedDevicesCompletedL( aStatus, aId );
       
   399             break;
       
   400             }
       
   401         default:
       
   402                 // Should not be possible, but no need for handling.
       
   403             TRACE_INFO( (_L("[BTEng]: CBTEngPairMan::RequestCompletedL unhandled event!!") ) )
       
   404             break;
       
   405         }
       
   406     TRACE_FUNC_EXIT
       
   407     }
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // From class MBTEngActiveObserver.
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 void CBTEngPairMan::HandleError( CBTEngActive* aActive, TInt aId, TInt aError )
       
   414     {
       
   415     TRACE_FUNC_ARG( ( _L( "request id: %d, error: %d" ), aId, aError ) )
       
   416     (void) aActive;
       
   417     (void) aError;
       
   418     if ( aId == ERegistryInitiatePairedDevicesList || 
       
   419          aId == ERegistryGetPairedDevices )
       
   420         {// leave happened in registry operation, delete registry response:
       
   421         delete iPairedDevicesResp;
       
   422         iPairedDevicesResp = NULL;
       
   423         }
       
   424     }
       
   425 
       
   426 // ---------------------------------------------------------------------------
       
   427 // Activate or deactivate a pairing handler
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 TInt CBTEngPairMan::SetPairObserver(const TBTDevAddr& aAddr, TBool aActivate)
       
   431     {
       
   432     TRACE_FUNC_ARG( ( _L( "%d" ), aActivate ) )
       
   433     TRACE_BDADDR( aAddr )
       
   434     TInt err( KErrNone );
       
   435     if ( !aActivate )
       
   436         {
       
   437         if ( iPairer )
       
   438             {
       
   439             iPairer->StopPairHandling( aAddr );
       
   440             }
       
   441         return err;
       
   442         }
       
   443     
       
   444     if ( !iPairer)
       
   445         {
       
   446         // This is an incoming pair, unpair it from registry and 
       
   447         // create the handler:
       
   448         UnpairDevice( aAddr );
       
   449         TRAP( err, iPairer = CBTEngIncPair::NewL( *this, aAddr ));     
       
   450         }
       
   451     if ( iPairer)
       
   452         {
       
   453         // let the handler decide what to do:
       
   454         err = iPairer->ObserveIncomingPair( aAddr );        
       
   455         }    
       
   456     TRACE_FUNC_EXIT
       
   457     return err;
       
   458     }
       
   459 
       
   460 // ---------------------------------------------------------------------------
       
   461 // Delegates the request to current pair handler
       
   462 // ---------------------------------------------------------------------------
       
   463 //
       
   464 void CBTEngPairMan::PairDeviceL( const TBTDevAddr& aAddr, TUint32 aCod )
       
   465     {
       
   466     if ( !iPairer)
       
   467         {
       
   468         // no existing pair handling, create one:
       
   469         iPairer = CBTEngOtgPair::NewL( *this, aAddr );
       
   470         }
       
   471     // let pair handler decide what to do:
       
   472     iPairer->HandleOutgoingPairL( aAddr, aCod );
       
   473     }
       
   474 
       
   475 // ---------------------------------------------------------------------------
       
   476 // cancel Subscribings to simple pairing result and authentication result from
       
   477 // Pairing Server
       
   478 // ---------------------------------------------------------------------------
       
   479 //
       
   480 void CBTEngPairMan::CancelSubscribe()
       
   481     {
       
   482     TRACE_FUNC_ENTRY
       
   483     if( iSSPResultActive && iSSPResultActive->IsActive() )
       
   484         {
       
   485             // Cancel listening Simple pairing result
       
   486         iPairingResult.CancelSimplePairingResult();
       
   487         iSSPResultActive->Cancel();
       
   488         }
       
   489     if( iAuthenResultActive && iAuthenResultActive->IsActive() )
       
   490         {
       
   491             // Cancel listening authentication result
       
   492         iAuthenResult.CancelAuthenticationResult();
       
   493         iAuthenResultActive->Cancel();
       
   494         }
       
   495     TRACE_FUNC_EXIT
       
   496     }
       
   497 
       
   498 // ---------------------------------------------------------------------------
       
   499 // Subscribes to simple pairing result from Pairing Server
       
   500 // ---------------------------------------------------------------------------
       
   501 //
       
   502 void CBTEngPairMan::SubscribeSspPairingResult()
       
   503     {
       
   504     TRACE_FUNC_ENTRY
       
   505     iPairingResult.SimplePairingResult( iSimplePairingRemote, iSSPResultActive->RequestStatus() );
       
   506     iSSPResultActive->GoActive();
       
   507     TRACE_FUNC_EXIT
       
   508     }
       
   509 
       
   510 // ---------------------------------------------------------------------------
       
   511 // Subscribes to authentication result from Pairing Server
       
   512 // ---------------------------------------------------------------------------
       
   513 //
       
   514 void CBTEngPairMan::SubscribeAuthenticateResult()
       
   515     {
       
   516     TRACE_FUNC_ENTRY
       
   517     // Subscribe authentication result (which requires pairing for unpaired devices)
       
   518     iAuthenResult.AuthenticationResult( iAuthenticateRemote, iAuthenResultActive->RequestStatus() );
       
   519     iAuthenResultActive->GoActive();
       
   520     TRACE_FUNC_EXIT
       
   521     }
       
   522 
       
   523 // ---------------------------------------------------------------------------
       
   524 // Handle a pairing result from the pairing server.
       
   525 // ---------------------------------------------------------------------------
       
   526 //
       
   527 void CBTEngPairMan::HandlePairingResultL( const TBTDevAddr& aAddr, TInt aResult )
       
   528     {
       
   529     TRACE_FUNC_ARG( (_L("result %d"), aResult ) )
       
   530     TRACE_BDADDR( aAddr );
       
   531     if ( !iPairer && ( aResult == KErrNone || aResult == KHCIErrorBase ) )
       
   532         {
       
   533         // we only create new handler if incoming pairing succeeds.
       
   534         // Pairing failure could be caused by user local cancellation, as the  
       
   535         // result, the handler was destroyed by notifier. We shall not
       
   536         // instantiate the handler again.
       
   537         // If a pairing failed due to other reasons than user local cancelling,
       
   538         // it will be catched by the already started handler 
       
   539         // (except Just Works pairing - no handler for it at all until we receive
       
   540         // registry change event. Thus if incoming JWs pairing failed, no user
       
   541         // notification will be shown.)
       
   542         TBTNamelessDevice dev;
       
   543         dev.SetAddress( aAddr );
       
   544         TIdentityRelation<TBTNamelessDevice> addrComp( CompareDeviceByAddress );
       
   545         TInt index = iPairedDevices->Find( dev, addrComp );
       
   546         
       
   547         // If the device is not found in the old paired device list, it is a new
       
   548         // paired device:
       
   549         if ( index == KErrNotFound)
       
   550             {
       
   551             // No handler yet, create incoming pairing handler:
       
   552             iPairer = CBTEngIncPair::NewL( *this, aAddr );
       
   553             }
       
   554         }
       
   555     if ( iPairer )
       
   556         {
       
   557         iPairer->HandlePairServerResult( aAddr, aResult );
       
   558         }  
       
   559 
       
   560     TRACE_FUNC_EXIT
       
   561     }
       
   562 
       
   563 // ---------------------------------------------------------------------------
       
   564 // issue creating a bonded devices view
       
   565 // ---------------------------------------------------------------------------
       
   566 //
       
   567 void CBTEngPairMan::CreatePairedDevicesView( TInt aReqId )
       
   568     {
       
   569     TRACE_FUNC_ENTRY
       
   570     iNotHandledRegEventCounter = 0;
       
   571     TBTRegistrySearch searchPattern;
       
   572     searchPattern.FindBonded();
       
   573     iRegistryActive->SetRequestId( aReqId );
       
   574     iBTRegistry.CreateView( searchPattern, iRegistryActive->iStatus );
       
   575     iRegistryActive->GoActive();
       
   576     TRACE_FUNC_EXIT
       
   577     }
       
   578 
       
   579 // ---------------------------------------------------------------------------
       
   580 // gets the paired devices from the view created by CreatePairedDevicesView
       
   581 // ---------------------------------------------------------------------------
       
   582 //
       
   583 void CBTEngPairMan::GetPairedDevices( TInt aReqId )
       
   584     {
       
   585     TRACE_FUNC_ENTRY
       
   586     delete iPairedDevicesResp;
       
   587     iPairedDevicesResp = NULL;
       
   588     TRAP_IGNORE( iPairedDevicesResp = CBTRegistryResponse::NewL( iBTRegistry ) );
       
   589     if ( iPairedDevicesResp )
       
   590         {
       
   591         iRegistryActive->SetRequestId( aReqId );
       
   592         iPairedDevicesResp->Start( iRegistryActive->iStatus );
       
   593         iRegistryActive->GoActive();
       
   594         }
       
   595     TRACE_FUNC_EXIT
       
   596     }
       
   597 
       
   598 // ---------------------------------------------------------------------------
       
   599 // re-create a paired device view if registry was changed during the previous
       
   600 // operation. otherwise if the view is not empty, get the paired devices.
       
   601 // ---------------------------------------------------------------------------
       
   602 //
       
   603 void CBTEngPairMan::HandleCreatePairedDevicesViewCompletedL( TInt aStatus, TInt aReqId )
       
   604     {
       
   605     TRACE_FUNC_ENTRY
       
   606 
       
   607     if ( aReqId == ERegistryInitiatePairedDevicesView )
       
   608         {// Initialization phase, list paired devices if there are.
       
   609         if ( aStatus > KErrNone )
       
   610             {
       
   611             GetPairedDevices( ERegistryInitiatePairedDevicesList );
       
   612             }
       
   613         else
       
   614             {//no paired device, close the view.
       
   615             (void) iBTRegistry.CloseView();
       
   616             }
       
   617         }
       
   618     else
       
   619         {
       
   620         if (iNotHandledRegEventCounter)
       
   621             { // more registry change detected, create paired device view again:
       
   622             (void) iBTRegistry.CloseView();
       
   623             CreatePairedDevicesView( ERegistryPairedDevicesNewView );
       
   624             }          
       
   625         else if ( aStatus > KErrNone )
       
   626             { // paired device available, get them:
       
   627             GetPairedDevices( ERegistryGetPairedDevices );
       
   628             }
       
   629         else
       
   630             {
       
   631             // No paired devices in registry, empty local db:
       
   632             (void) iBTRegistry.CloseView();
       
   633             iPairedDevices->Reset();
       
   634             }
       
   635         }
       
   636     TRACE_FUNC_EXIT
       
   637     }
       
   638 
       
   639 // ---------------------------------------------------------------------------
       
   640 // update paired device list. if registry was changed, create a new view.
       
   641 // otherwise check for new pairing event.
       
   642 // ---------------------------------------------------------------------------
       
   643 //
       
   644 void CBTEngPairMan::HandleGetPairedDevicesCompletedL( TInt /*aStatus*/, TInt aReqId )
       
   645     {
       
   646     TRACE_FUNC_ENTRY
       
   647     (void) iBTRegistry.CloseView();
       
   648     if ( aReqId == ERegistryInitiatePairedDevicesList )
       
   649         {
       
   650         // We completed the initialization of paired device list, 
       
   651         // move all paired devices to the array:
       
   652         UpdatePairedDeviceListL();
       
   653         }
       
   654     
       
   655     if (iNotHandledRegEventCounter)
       
   656         { // more registry change detected, create paired device view again:
       
   657         CreatePairedDevicesView( ERegistryPairedDevicesNewView );
       
   658         }
       
   659     else if ( aReqId == ERegistryGetPairedDevices)
       
   660         {
       
   661         // no more registry change detected, find new pairings:
       
   662         CheckPairEventL();
       
   663         }
       
   664 
       
   665     TRACE_FUNC_EXIT
       
   666     }
       
   667 
       
   668 // ---------------------------------------------------------------------------
       
   669 // copy the nameless devices to local array
       
   670 // ---------------------------------------------------------------------------
       
   671 //
       
   672 void CBTEngPairMan::UpdatePairedDeviceListL()
       
   673     {
       
   674     TRACE_FUNC_ENTRY
       
   675     iPairedDevices->Reset();
       
   676     for ( TInt i = 0; i < iPairedDevicesResp->Results().Count(); i++ )
       
   677         {
       
   678         TRACE_BDADDR( iPairedDevicesResp->Results()[i]->BDAddr() );
       
   679         TRACE_INFO((_L("[BTENG]\t linkkeytype %d"), 
       
   680                 iPairedDevicesResp->Results()[i]->LinkKeyType()))
       
   681         iPairedDevices->AppendL( iPairedDevicesResp->Results()[i]->AsNamelessDevice() );
       
   682         }
       
   683     delete iPairedDevicesResp;
       
   684     iPairedDevicesResp = NULL;
       
   685     TRACE_FUNC_EXIT
       
   686     }
       
   687 
       
   688 // ---------------------------------------------------------------------------
       
   689 // find new paired devices. for each, delegate the information to
       
   690 // current pair handler.
       
   691 // ---------------------------------------------------------------------------
       
   692 //
       
   693 void CBTEngPairMan::CheckPairEventL()
       
   694     {
       
   695     TRACE_FUNC_ENTRY
       
   696     RArray<TBTNamelessDevice>* pairedDevicesOld;
       
   697     pairedDevicesOld = iPairedDevices;
       
   698     CleanupStack::PushL( pairedDevicesOld );
       
   699     CleanupClosePushL( *pairedDevicesOld );
       
   700     iPairedDevices = NULL;
       
   701     iPairedDevices = new (ELeave) RArray<TBTNamelessDevice>;
       
   702     UpdatePairedDeviceListL();
       
   703     
       
   704     TIdentityRelation<TBTNamelessDevice> addrComp( CompareDeviceByAddress );
       
   705     for ( TInt i = 0; i < iPairedDevices->Count(); i++ )
       
   706         {
       
   707         TBTNamelessDevice& dev = (*iPairedDevices)[i];        
       
   708         TInt index = pairedDevicesOld->Find( dev, addrComp );
       
   709  
       
   710         // If the device is not found in the old paired device list or
       
   711         // the link key type has been changed from 
       
   712         // ELinkKeyUnauthenticatedUpgradable, the device is a new 
       
   713         // paired device:
       
   714         TBool newPaired = dev.LinkKeyType() != ELinkKeyUnauthenticatedUpgradable && 
       
   715              ( index == KErrNotFound  || 
       
   716                  ( index > KErrNotFound &&
       
   717                  dev.LinkKeyType() != (*pairedDevicesOld)[index].LinkKeyType() ) );
       
   718         TRACE_BDADDR( dev.Address() );
       
   719         if ( newPaired && !iPairer)
       
   720             {
       
   721             iPairer = CBTEngIncPair::NewL( *this, dev.Address() );
       
   722             }
       
   723         if ( iPairer )
       
   724             {
       
   725             // Ask pair handler to decide what to do:
       
   726             iPairer->HandleRegistryNewPairedEvent( dev );
       
   727             }
       
   728         }
       
   729     // Free old paired device list resource:
       
   730     CleanupStack::PopAndDestroy( 2 );
       
   731     TRACE_FUNC_EXIT
       
   732     }
       
   733