bluetoothengine/bteng/src/btengpairman.cpp
changeset 15 00f9ee97d895
parent 0 f63038272f30
equal deleted inserted replaced
2:0b192a3a05a4 15:00f9ee97d895
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    31     EAuthenticationResult,
    31     EAuthenticationResult,
    32     ERegistryInitiatePairedDevicesView,
    32     ERegistryInitiatePairedDevicesView,
    33     ERegistryPairedDevicesNewView,
    33     ERegistryPairedDevicesNewView,
    34     ERegistryInitiatePairedDevicesList,
    34     ERegistryInitiatePairedDevicesList,
    35     ERegistryGetPairedDevices,
    35     ERegistryGetPairedDevices,
       
    36     ERegistryGetLocalAddress,
    36     };
    37     };
    37 
    38 
    38 /**  The message argument which holds the Bluetooth address. */
    39 /**  The message argument which holds the Bluetooth address. */
    39 const TInt KBTEngAddrSlot = 0;
    40 const TInt KBTEngAddrSlot = 0;
    40 
    41 
    79         {
    80         {
    80         User::LeaveIfError( iPairingResult.Open( *iPairingServ ) );
    81         User::LeaveIfError( iPairingResult.Open( *iPairingServ ) );
    81         User::LeaveIfError( iAuthenResult.Open( *iPairingServ ) );
    82         User::LeaveIfError( iAuthenResult.Open( *iPairingServ ) );
    82         iSSPResultActive = CBTEngActive::NewL( *this, ESimplePairingResult, CActive::EPriorityStandard );
    83         iSSPResultActive = CBTEngActive::NewL( *this, ESimplePairingResult, CActive::EPriorityStandard );
    83         iAuthenResultActive = CBTEngActive::NewL( *this, EAuthenticationResult, CActive::EPriorityStandard );        
    84         iAuthenResultActive = CBTEngActive::NewL( *this, EAuthenticationResult, CActive::EPriorityStandard );        
    84         SubscribeSspPairingResult();
    85         }
    85         SubscribeAuthenticateResult();
    86 
    86         }
    87     // RProperty for accessing the local device address
       
    88     User::LeaveIfError( iPropertyLocalAddr.Attach(KPropertyUidBluetoothCategory, KPropertyKeyBluetoothGetLocalDeviceAddress) );
    87 
    89 
    88     // connect to registry
    90     // connect to registry
    89     User::LeaveIfError( iBTRegistry.Open( BTRegServ() ) );    
    91     User::LeaveIfError( iBTRegistry.Open( BTRegServ() ) );    
    90     iRegistryActive = CBTEngActive::NewL( *this, ERegistryInitiatePairedDevicesView, CActive::EPriorityStandard );
    92     iRegistryActive = CBTEngActive::NewL( *this, ERegistryInitiatePairedDevicesView, CActive::EPriorityStandard );
    91     // Start to get the list of all paired devices.
    93     iPairedDevices = new (ELeave) RArray<TBTNamelessDevice>; 
    92     CreatePairedDevicesView( ERegistryInitiatePairedDevicesView );
    94 
    93     iPairedDevices = new (ELeave) RArray<TBTNamelessDevice>;
    95     // Initialise paired devices list
       
    96     iLocalAddrActive = CBTEngActive::NewL( *this, ERegistryGetLocalAddress, CActive::EPriorityStandard );
       
    97     InitPairedDevicesList();
       
    98  
    94     TRACE_FUNC_EXIT
    99     TRACE_FUNC_EXIT
    95     }
   100     }
    96 
   101 
    97 // ---------------------------------------------------------------------------
   102 // ---------------------------------------------------------------------------
    98 // NewL
   103 // NewL
   132     if ( iPairingServ )
   137     if ( iPairingServ )
   133         {
   138         {
   134         iPairingServ->Close();
   139         iPairingServ->Close();
   135         delete iPairingServ;
   140         delete iPairingServ;
   136         }
   141         }
       
   142     if ( !iMessage.IsNull() )
       
   143         {
       
   144         iMessage.Complete( KErrCancel );
       
   145         }
       
   146     iPropertyLocalAddr.Cancel();
       
   147     iPropertyLocalAddr.Close();
       
   148     delete iLocalAddrActive;
       
   149     TRACE_FUNC_EXIT
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // Initialises the paired devices list.
       
   154 // If the local address is not available from the P&S key 
       
   155 // KPropertyKeyBluetoothGetLocalDeviceAddress, then the list may need to be 
       
   156 // updated once the H/W is switched on. This is so that any registry update 
       
   157 // from a restore operation can be included in the list, without mistaking the 
       
   158 // new devices for new pairings.
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CBTEngPairMan::InitPairedDevicesList()
       
   162     {
       
   163     TRACE_FUNC_ENTRY
       
   164 
       
   165     // Check that we have the Bluetooth local address. If we don't then initialise anyway, but subscribe for an update.
       
   166     // This allows us to refresh our paired devices list to include updates made to the remote devices table of the 
       
   167     // Bluetooth registry from a restore operation. We need to include these devices without mistaking them for new 
       
   168     // pairings. We look solely at the P&S key for the address to avoid the condition whereby the address has been
       
   169     // entered into the reigstry but the Bluetooth Manager server has not begun the restore process yet. The signalling
       
   170     // of the P&S key will cause Bluetooth Manager to update the registry with any restored devices before fulfilling
       
   171     // any further requests.
       
   172 
       
   173     // Subscribe to local address property in case we need an update.
       
   174     iPropertyLocalAddr.Subscribe( iLocalAddrActive->iStatus );
       
   175     iLocalAddrActive->SetRequestId( ERegistryGetLocalAddress );
       
   176     iLocalAddrActive->GoActive();
       
   177 
       
   178     // Attempt to read address from P&S key.
       
   179     TBuf8<KBTDevAddrSize> btAddrDes;
       
   180     TInt err = iPropertyLocalAddr.Get( btAddrDes );
       
   181 
       
   182     // Is the P&S key defined yet? (if not, stack not up yet)
       
   183     if ( err == KErrNone )
       
   184         {
       
   185         // P&S key defined, is local address set? (if not, H/W not initialised yet)
       
   186         if ( btAddrDes.Length() == KBTDevAddrSize )
       
   187             {
       
   188             TBTDevAddr btAddr = btAddrDes;
       
   189 
       
   190             if ( btAddr != TBTDevAddr() )
       
   191                 {
       
   192                 // Non-zero local address is available.
       
   193                 iPropertyLocalAddr.Cancel();
       
   194                 iLocalAddrActive->CancelRequest();
       
   195                 }
       
   196             }
       
   197         }
       
   198 
       
   199     // Perform initialisation of the paired devices list.
       
   200     DoInitPairedDevicesList();
       
   201 
       
   202     TRACE_FUNC_EXIT
       
   203     }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 // Initialises the paired devices list (second stage)
       
   207 // This method performs the actual initialisation, now that the local BT H/W
       
   208 // address had been made available.
       
   209 // ---------------------------------------------------------------------------
       
   210 //
       
   211 void CBTEngPairMan::DoInitPairedDevicesList()
       
   212     {
       
   213     TRACE_FUNC_ENTRY
       
   214 
       
   215     if ( !iRegistryActive->IsActive() )
       
   216         {
       
   217         // Start to get the list of all paired devices.
       
   218         CreatePairedDevicesView( ERegistryInitiatePairedDevicesView );
       
   219         }
       
   220     else
       
   221         {
       
   222         iNotHandledInitEventCounter++;
       
   223         }
       
   224 
   137     TRACE_FUNC_EXIT
   225     TRACE_FUNC_EXIT
   138     }
   226     }
   139 
   227 
   140 // ---------------------------------------------------------------------------
   228 // ---------------------------------------------------------------------------
   141 // Handles pairing related commands from BTEng clients.
   229 // Handles pairing related commands from BTEng clients.
   154             SetPairObserver( addrPkg(), aMessage.Int1() );
   242             SetPairObserver( addrPkg(), aMessage.Int1() );
   155             break;
   243             break;
   156             }
   244             }
   157         case EBTEngPairDevice:
   245         case EBTEngPairDevice:
   158             {
   246             {
       
   247             if ( !iMessage.IsNull() )
       
   248                 {
       
   249                 User::Leave( KErrServerBusy );
       
   250                 }
   159             TBTDevAddrPckgBuf addrPkg;
   251             TBTDevAddrPckgBuf addrPkg;
   160             aMessage.ReadL( KBTEngAddrSlot, addrPkg );
   252             aMessage.ReadL( KBTEngAddrSlot, addrPkg );
   161             PairDeviceL( addrPkg(), aMessage.Int1() );
   253             PairDeviceL( addrPkg(), aMessage.Int1() );
       
   254             iMessage = RMessage2( aMessage );
   162             break;
   255             break;
   163             }
   256             }
   164         case EBTEngCancelPairDevice:
   257         case EBTEngCancelPairDevice:
   165             {
   258             {
   166             CancelCommand( opcode );
   259             // Only the client who requested pairing can cancel it:
       
   260             if ( !iMessage.IsNull() && aMessage.Session() == iMessage.Session() )
       
   261                 {
       
   262                 iPairer->CancelOutgoingPair();
       
   263                 iMessage.Complete( KErrCancel );
       
   264                 }
   167             break;
   265             break;
   168             }
   266             }
   169         default:
   267         default:
   170             {
   268             {
   171             TRACE_INFO( ( _L( "CBTEngPairMan ProcessCommandL: bad request (%d)" ), 
   269             TRACE_INFO( ( _L( "CBTEngPairMan ProcessCommandL: bad request (%d)" ), 
   175         }
   273         }
   176     TRACE_FUNC_EXIT    
   274     TRACE_FUNC_EXIT    
   177     }
   275     }
   178 
   276 
   179 // ---------------------------------------------------------------------------
   277 // ---------------------------------------------------------------------------
   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.
   278 // Handle a change in BTRegistry remote devices table.
   202 // ---------------------------------------------------------------------------
   279 // ---------------------------------------------------------------------------
   203 //
   280 //
   204 void CBTEngPairMan::RemoteRegistryChangeDetected()
   281 void CBTEngPairMan::RemoteRegistryChangeDetected()
   205     {
   282     {
   226 // Access the reference of RSockServ
   303 // Access the reference of RSockServ
   227 // ---------------------------------------------------------------------------
   304 // ---------------------------------------------------------------------------
   228 //
   305 //
   229 RSocketServ& CBTEngPairMan::SocketServ()
   306 RSocketServ& CBTEngPairMan::SocketServ()
   230     {
   307     {
   231     return iServer.SocketServ();
   308     return iServer.SocketServer();
   232     }
   309     }
   233 
   310 
   234 // ---------------------------------------------------------------------------
   311 // ---------------------------------------------------------------------------
   235 // Access the reference of RBTRegSrv
   312 // Access the reference of RBTRegSrv
   236 // ---------------------------------------------------------------------------
   313 // ---------------------------------------------------------------------------
   237 //
   314 //
   238 RBTRegServ& CBTEngPairMan::BTRegServ()
   315 RBTRegServ& CBTEngPairMan::BTRegServ()
   239     {
   316     {
   240     return iServer.BTRegServ();
   317     return iServer.RegistrServer();
   241     }
   318     }
   242 
   319 
   243 // ---------------------------------------------------------------------------
   320 // ---------------------------------------------------------------------------
   244 // Deletes the current pairing handler and transfer the responsibility
   321 // Deletes the current pairing handler and transfer the responsibility
   245 // to the specified.
   322 // to the specified.
   263     if ( aErr == KHCIErrorBase )
   340     if ( aErr == KHCIErrorBase )
   264         {
   341         {
   265         aErr = KErrNone;
   342         aErr = KErrNone;
   266         }
   343         }
   267     // we must complete client's pairing request:
   344     // we must complete client's pairing request:
   268     iServer.iSessionIter.SetToLast();
   345     if ( !iMessage.IsNull()  )
   269     CBTEngSrvSession* session = (CBTEngSrvSession*) iServer.iSessionIter--;
   346         {
   270     TInt ret( KErrNotFound );
   347         iMessage.Complete( aErr );
   271     while( session && ret )
   348         }
   272         {
   349     TRACE_FUNC_EXIT
   273         ret = session->CompletePairRequest( aErr );
   350     }
   274         session = (CBTEngSrvSession*) iServer.iSessionIter--;
   351 
       
   352 // ---------------------------------------------------------------------------
       
   353 // A session will be ended, completes the pending request for this session.
       
   354 // ---------------------------------------------------------------------------
       
   355 //
       
   356 void CBTEngPairMan::SessionClosed( CSession2* aSession )
       
   357     {
       
   358     TRACE_FUNC_ARG( ( _L( " session %x"), aSession ) )
       
   359     if ( !iMessage.IsNull() && iMessage.Session() == aSession )
       
   360         {
       
   361         iMessage.Complete( KErrCancel );
   275         }
   362         }
   276     TRACE_FUNC_EXIT
   363     TRACE_FUNC_EXIT
   277     }
   364     }
   278 
   365 
   279 // ---------------------------------------------------------------------------
   366 // ---------------------------------------------------------------------------
   289     // only do unpairing if the we have a link key with it.
   376     // only do unpairing if the we have a link key with it.
   290     TInt index = iPairedDevices->Find( dev, addrComp );
   377     TInt index = iPairedDevices->Find( dev, addrComp );
   291     if ( index > KErrNotFound )
   378     if ( index > KErrNotFound )
   292         {
   379         {
   293         dev = (*iPairedDevices)[index];
   380         dev = (*iPairedDevices)[index];
   294         TBTDeviceSecurity security = dev.GlobalSecurity();
   381         
   295         // Clear trust setting so that correct icon will be shown in ui applications.
   382         TRequestStatus status( KRequestPending );
   296         security.SetNoAuthenticate(EFalse );
   383         // Unpair the device in registry (synchronously)
   297         security.SetNoAuthorise(EFalse );
   384         iBTRegistry.UnpairDevice( dev.Address(), status );
   298         dev.SetGlobalSecurity(security);
   385         User::WaitForRequest( status );
   299         dev.DeleteLinkKey();
   386         TRACE_INFO( ( _L( "Delete link key, res %d"), status.Int() ) )
   300         if ( dev.IsValidUiCookie() && 
   387         
   301              ( dev.UiCookie() & EBTUiCookieJustWorksPaired ) )
   388         if ( status == KErrNone )
   302             {
   389             {
   303             // Remove the UI cookie bit for Just Works pairing.
   390             TBTDeviceSecurity security = dev.GlobalSecurity();
   304             TInt32 cookie = dev.UiCookie() & ~EBTUiCookieJustWorksPaired;
   391             // Clear trust setting so that correct icon will be shown in ui applications.
   305             dev.SetUiCookie( cookie );
   392             security.SetNoAuthenticate(EFalse );
   306             TRACE_INFO( ( _L( "UI cookie %x cleared"), EBTUiCookieJustWorksPaired ) );
   393             security.SetNoAuthorise(EFalse );
   307             }
   394             dev.SetGlobalSecurity(security);
   308         // modify the device in registry synchronously
   395             dev.DeleteLinkKey();
   309         // status.Int() could be -1 if the device is not in registry 
   396             if ( dev.IsValidUiCookie() && 
   310         // which is totally fine for us.
   397                  ( dev.UiCookie() & EBTUiCookieJustWorksPaired ) )
   311         (void) UpdateRegDevice( dev );
   398                 {
       
   399                 // Remove the UI cookie bit for Just Works pairing.
       
   400                 TInt32 cookie = dev.UiCookie() & ~EBTUiCookieJustWorksPaired;
       
   401                 dev.SetUiCookie( cookie );
       
   402                 TRACE_INFO( ( _L( "UI cookie %x cleared"), EBTUiCookieJustWorksPaired ) );
       
   403                 }
       
   404             // modify the device in registry synchronously
       
   405             // status.Int() could be -1 if the device is not in registry 
       
   406             // which is totally fine for us.
       
   407             (void) UpdateRegDevice( dev );
       
   408             }
   312         }
   409         }
   313     TRACE_FUNC_EXIT
   410     TRACE_FUNC_EXIT
   314     }
   411     }
   315 
   412 
   316 TInt CBTEngPairMan::AddUiCookieJustWorksPaired( const TBTNamelessDevice& aDev )
   413 TInt CBTEngPairMan::AddUiCookieJustWorksPaired( const TBTNamelessDevice& aDev )
   317     {
   414     {
       
   415 	TRACE_FUNC_ENTRY
   318     TInt err( KErrNone );
   416     TInt err( KErrNone );
   319     // There might be UI cookies used by other applications,
   417     // There might be UI cookies used by other applications,
   320     // we should not overwrite them. 
   418     // we should not overwrite them. 
   321     TInt32 cookie = aDev.IsValidUiCookie() ? aDev.UiCookie() : EBTUiCookieUndefined;
   419     TInt32 cookie = aDev.IsValidUiCookie() ? aDev.UiCookie() : EBTUiCookieUndefined;
   322     if ( !( cookie & EBTUiCookieJustWorksPaired ) )
   420     if ( !( cookie & EBTUiCookieJustWorksPaired ) )
   327         cookie |= EBTUiCookieJustWorksPaired;
   425         cookie |= EBTUiCookieJustWorksPaired;
   328         dev.SetUiCookie( cookie );
   426         dev.SetUiCookie( cookie );
   329         err = UpdateRegDevice( dev );
   427         err = UpdateRegDevice( dev );
   330         TRACE_INFO( ( _L( "[BTENG] CBTEngOtgPair write Ui cookie ret %d"), err ) );
   428         TRACE_INFO( ( _L( "[BTENG] CBTEngOtgPair write Ui cookie ret %d"), err ) );
   331         }
   429         }
       
   430     TRACE_FUNC_EXIT
   332     return err;
   431     return err;
   333     }
   432     }
   334 
   433 
   335 // ---------------------------------------------------------------------------
   434 // ---------------------------------------------------------------------------
   336 // update a nameless device in registry
   435 // update a nameless device in registry
   391             {
   490             {
   392             HandleCreatePairedDevicesViewCompletedL( aStatus, aId );
   491             HandleCreatePairedDevicesViewCompletedL( aStatus, aId );
   393             break;
   492             break;
   394             }
   493             }
   395         case ERegistryInitiatePairedDevicesList:
   494         case ERegistryInitiatePairedDevicesList:
       
   495             {			
       
   496 			if (iSSPResultActive && iAuthenResultActive)
       
   497 				{
       
   498 				SubscribeSspPairingResult();
       
   499 				SubscribeAuthenticateResult();
       
   500 				}
       
   501             HandleGetPairedDevicesCompletedL( aStatus, aId );
       
   502             break;
       
   503             }
   396         case ERegistryGetPairedDevices:    
   504         case ERegistryGetPairedDevices:    
   397             {
   505             {
   398             HandleGetPairedDevicesCompletedL( aStatus, aId );
   506             HandleGetPairedDevicesCompletedL( aStatus, aId );
       
   507             break;
       
   508             }
       
   509         case ERegistryGetLocalAddress:
       
   510             {
       
   511             // Refresh paired devices list to include any restored devices.
       
   512             DoInitPairedDevicesList();
   399             break;
   513             break;
   400             }
   514             }
   401         default:
   515         default:
   402                 // Should not be possible, but no need for handling.
   516                 // Should not be possible, but no need for handling.
   403             TRACE_INFO( (_L("[BTEng]: CBTEngPairMan::RequestCompletedL unhandled event!!") ) )
   517             TRACE_INFO( (_L("[BTEng]: CBTEngPairMan::RequestCompletedL unhandled event!!") ) )
   429 //
   543 //
   430 TInt CBTEngPairMan::SetPairObserver(const TBTDevAddr& aAddr, TBool aActivate)
   544 TInt CBTEngPairMan::SetPairObserver(const TBTDevAddr& aAddr, TBool aActivate)
   431     {
   545     {
   432     TRACE_FUNC_ARG( ( _L( "%d" ), aActivate ) )
   546     TRACE_FUNC_ARG( ( _L( "%d" ), aActivate ) )
   433     TRACE_BDADDR( aAddr )
   547     TRACE_BDADDR( aAddr )
       
   548     iPairingOperationAttempted = ETrue;
   434     TInt err( KErrNone );
   549     TInt err( KErrNone );
   435     if ( !aActivate )
   550     if ( !aActivate )
   436         {
   551         {
   437         if ( iPairer )
   552         if ( iPairer )
   438             {
   553             {
   461 // Delegates the request to current pair handler
   576 // Delegates the request to current pair handler
   462 // ---------------------------------------------------------------------------
   577 // ---------------------------------------------------------------------------
   463 //
   578 //
   464 void CBTEngPairMan::PairDeviceL( const TBTDevAddr& aAddr, TUint32 aCod )
   579 void CBTEngPairMan::PairDeviceL( const TBTDevAddr& aAddr, TUint32 aCod )
   465     {
   580     {
       
   581     iPairingOperationAttempted = ETrue;
   466     if ( !iPairer)
   582     if ( !iPairer)
   467         {
   583         {
   468         // no existing pair handling, create one:
   584         // no existing pair handling, create one:
   469         iPairer = CBTEngOtgPair::NewL( *this, aAddr );
   585         iPairer = CBTEngOtgPair::NewL( *this, aAddr );
   470         }
   586         }
   494         }
   610         }
   495     TRACE_FUNC_EXIT
   611     TRACE_FUNC_EXIT
   496     }
   612     }
   497 
   613 
   498 // ---------------------------------------------------------------------------
   614 // ---------------------------------------------------------------------------
   499 // Subscribes to simple pairing result from Pairing Server
   615 // Subscribes to simple pairing result from Pairing Server (if not already 
       
   616 // subscribed).
   500 // ---------------------------------------------------------------------------
   617 // ---------------------------------------------------------------------------
   501 //
   618 //
   502 void CBTEngPairMan::SubscribeSspPairingResult()
   619 void CBTEngPairMan::SubscribeSspPairingResult()
   503     {
   620     {
   504     TRACE_FUNC_ENTRY
   621     TRACE_FUNC_ENTRY
   505     iPairingResult.SimplePairingResult( iSimplePairingRemote, iSSPResultActive->RequestStatus() );
   622     if ( !iSSPResultActive->IsActive() )
   506     iSSPResultActive->GoActive();
   623         {
   507     TRACE_FUNC_EXIT
   624         iPairingResult.SimplePairingResult( iSimplePairingRemote, iSSPResultActive->RequestStatus() );
   508     }
   625         iSSPResultActive->GoActive();
   509 
   626         }
   510 // ---------------------------------------------------------------------------
   627     TRACE_FUNC_EXIT
   511 // Subscribes to authentication result from Pairing Server
   628     }
       
   629 
       
   630 // ---------------------------------------------------------------------------
       
   631 // Subscribes to authentication result from Pairing Server (if not already
       
   632 // subscribed).
   512 // ---------------------------------------------------------------------------
   633 // ---------------------------------------------------------------------------
   513 //
   634 //
   514 void CBTEngPairMan::SubscribeAuthenticateResult()
   635 void CBTEngPairMan::SubscribeAuthenticateResult()
   515     {
   636     {
   516     TRACE_FUNC_ENTRY
   637     TRACE_FUNC_ENTRY
   517     // Subscribe authentication result (which requires pairing for unpaired devices)
   638     if ( !iAuthenResultActive->IsActive() )
   518     iAuthenResult.AuthenticationResult( iAuthenticateRemote, iAuthenResultActive->RequestStatus() );
   639         {
   519     iAuthenResultActive->GoActive();
   640         // Subscribe authentication result (which requires pairing for unpaired devices)
       
   641         iAuthenResult.AuthenticationResult( iAuthenticateRemote, iAuthenResultActive->RequestStatus() );
       
   642         iAuthenResultActive->GoActive();
       
   643         }
   520     TRACE_FUNC_EXIT
   644     TRACE_FUNC_EXIT
   521     }
   645     }
   522 
   646 
   523 // ---------------------------------------------------------------------------
   647 // ---------------------------------------------------------------------------
   524 // Handle a pairing result from the pairing server.
   648 // Handle a pairing result from the pairing server.
   565 // ---------------------------------------------------------------------------
   689 // ---------------------------------------------------------------------------
   566 //
   690 //
   567 void CBTEngPairMan::CreatePairedDevicesView( TInt aReqId )
   691 void CBTEngPairMan::CreatePairedDevicesView( TInt aReqId )
   568     {
   692     {
   569     TRACE_FUNC_ENTRY
   693     TRACE_FUNC_ENTRY
   570     iNotHandledRegEventCounter = 0;
   694     if ( aReqId == ERegistryInitiatePairedDevicesView )
       
   695         {
       
   696         iNotHandledInitEventCounter = 0;
       
   697         }
       
   698     else
       
   699         {
       
   700         iNotHandledRegEventCounter = 0;
       
   701         }
   571     TBTRegistrySearch searchPattern;
   702     TBTRegistrySearch searchPattern;
   572     searchPattern.FindBonded();
   703     searchPattern.FindBonded();
   573     iRegistryActive->SetRequestId( aReqId );
   704     iRegistryActive->SetRequestId( aReqId );
   574     iBTRegistry.CreateView( searchPattern, iRegistryActive->iStatus );
   705     iBTRegistry.CreateView( searchPattern, iRegistryActive->iStatus );
   575     iRegistryActive->GoActive();
   706     iRegistryActive->GoActive();
   603 void CBTEngPairMan::HandleCreatePairedDevicesViewCompletedL( TInt aStatus, TInt aReqId )
   734 void CBTEngPairMan::HandleCreatePairedDevicesViewCompletedL( TInt aStatus, TInt aReqId )
   604     {
   735     {
   605     TRACE_FUNC_ENTRY
   736     TRACE_FUNC_ENTRY
   606 
   737 
   607     if ( aReqId == ERegistryInitiatePairedDevicesView )
   738     if ( aReqId == ERegistryInitiatePairedDevicesView )
   608         {// Initialization phase, list paired devices if there are.
   739         {// Initialization phase, list paired devices if there are any.
   609         if ( aStatus > KErrNone )
   740         if ( iNotHandledInitEventCounter )
       
   741             {
       
   742             // Reinitialisaton detected, create paired device view again:
       
   743             (void) iBTRegistry.CloseView();
       
   744             CreatePairedDevicesView( ERegistryInitiatePairedDevicesView );
       
   745             }
       
   746         else if ( aStatus > KErrNone )
   610             {
   747             {
   611             GetPairedDevices( ERegistryInitiatePairedDevicesList );
   748             GetPairedDevices( ERegistryInitiatePairedDevicesList );
   612             }
   749             }
   613         else
   750         else
   614             {//no paired device, close the view.
   751             {//no paired device, close the view.
   615             (void) iBTRegistry.CloseView();
   752             (void) iBTRegistry.CloseView();
   616             }
   753             }
   617         }
   754         }
   618     else
   755     else
   619         {
   756         {
   620         if (iNotHandledRegEventCounter)
   757         if ( iNotHandledInitEventCounter )
       
   758             {
       
   759             // We need to reinitialise but we may be pairing.
       
   760             // This situation is not expected to arise, as reinitialisation means
       
   761             // that the H/W was only just switched on.
       
   762             // If we have ever started to take part in a pairing, then prioritise that
       
   763             // pairing.
       
   764             (void) iBTRegistry.CloseView();
       
   765             if ( iPairingOperationAttempted )
       
   766                 {
       
   767                 iNotHandledInitEventCounter = 0;
       
   768                 CreatePairedDevicesView( ERegistryPairedDevicesNewView );
       
   769                 }
       
   770             else
       
   771                 {
       
   772                 CreatePairedDevicesView( ERegistryInitiatePairedDevicesView );
       
   773                 }
       
   774             }
       
   775         else if (iNotHandledRegEventCounter)
   621             { // more registry change detected, create paired device view again:
   776             { // more registry change detected, create paired device view again:
   622             (void) iBTRegistry.CloseView();
   777             (void) iBTRegistry.CloseView();
   623             CreatePairedDevicesView( ERegistryPairedDevicesNewView );
   778             CreatePairedDevicesView( ERegistryPairedDevicesNewView );
   624             }          
   779             }          
   625         else if ( aStatus > KErrNone )
   780         else if ( aStatus > KErrNone )
   645     {
   800     {
   646     TRACE_FUNC_ENTRY
   801     TRACE_FUNC_ENTRY
   647     (void) iBTRegistry.CloseView();
   802     (void) iBTRegistry.CloseView();
   648     if ( aReqId == ERegistryInitiatePairedDevicesList )
   803     if ( aReqId == ERegistryInitiatePairedDevicesList )
   649         {
   804         {
   650         // We completed the initialization of paired device list, 
   805         if ( iNotHandledInitEventCounter )
   651         // move all paired devices to the array:
   806             {
   652         UpdatePairedDeviceListL();
   807             // Reinitialisation required, create paired device view again:
   653         }
   808             CreatePairedDevicesView( ERegistryInitiatePairedDevicesView );
   654     
   809             }
   655     if (iNotHandledRegEventCounter)
   810         else
   656         { // more registry change detected, create paired device view again:
   811             {
   657         CreatePairedDevicesView( ERegistryPairedDevicesNewView );
   812             // We completed the initialisation of paired device list, 
   658         }
   813             // move all paired devices to the array:
   659     else if ( aReqId == ERegistryGetPairedDevices)
   814             UpdatePairedDeviceListL();
   660         {
   815             }
   661         // no more registry change detected, find new pairings:
   816         }
   662         CheckPairEventL();
   817     else
       
   818         {
       
   819         if (iNotHandledInitEventCounter)
       
   820             {
       
   821             // We need to reinitialise but we may be pairing.
       
   822             // This situation is not expected to arise, as reinitialisation means
       
   823             // that the H/W was only just switched on.
       
   824             // If we have ever started to take part in a pairing, then prioritise that
       
   825             // pairing.
       
   826             if ( iPairingOperationAttempted )
       
   827                 {
       
   828                 iNotHandledInitEventCounter = 0;
       
   829                 CreatePairedDevicesView( ERegistryPairedDevicesNewView );
       
   830                 }
       
   831             else
       
   832                 {
       
   833                 CreatePairedDevicesView( ERegistryInitiatePairedDevicesView );
       
   834                 }		
       
   835             }     
       
   836         else if (iNotHandledRegEventCounter)
       
   837             { // more registry change detected, create paired device view again:
       
   838             CreatePairedDevicesView( ERegistryPairedDevicesNewView );
       
   839             }
       
   840         else if ( aReqId == ERegistryGetPairedDevices)
       
   841            {
       
   842             // no more registry change detected, find new pairings:
       
   843             CheckPairEventL();
       
   844            }
   663         }
   845         }
   664 
   846 
   665     TRACE_FUNC_EXIT
   847     TRACE_FUNC_EXIT
   666     }
   848     }
   667 
   849 
   716                  ( index > KErrNotFound &&
   898                  ( index > KErrNotFound &&
   717                  dev.LinkKeyType() != (*pairedDevicesOld)[index].LinkKeyType() ) );
   899                  dev.LinkKeyType() != (*pairedDevicesOld)[index].LinkKeyType() ) );
   718         TRACE_BDADDR( dev.Address() );
   900         TRACE_BDADDR( dev.Address() );
   719         if ( newPaired && !iPairer)
   901         if ( newPaired && !iPairer)
   720             {
   902             {
       
   903             iPairingOperationAttempted = ETrue;
   721             iPairer = CBTEngIncPair::NewL( *this, dev.Address() );
   904             iPairer = CBTEngIncPair::NewL( *this, dev.Address() );
   722             }
   905             }
   723         if ( iPairer )
   906         if ( newPaired && iPairer )
   724             {
   907             {
   725             // Ask pair handler to decide what to do:
   908             // Ask pair handler to decide what to do:
   726             iPairer->HandleRegistryNewPairedEvent( dev );
   909             iPairer->HandleRegistryNewPairedEvent( dev );
   727             }
   910             }
   728         }
   911         }