bluetoothengine/btnotif/btnotifsrv/src/btnotifoutgoingpairinghandler.cpp
changeset 47 9e2a905b887f
parent 42 b72428996822
child 57 5ebadcda06cb
equal deleted inserted replaced
45:b0aebde9b1fb 47:9e2a905b887f
    18 #include "btnotifoutgoingpairinghandler.h"
    18 #include "btnotifoutgoingpairinghandler.h"
    19 #include <btengconstants.h>
    19 #include <btengconstants.h>
    20 #include <btservices/btdevextension.h>
    20 #include <btservices/btdevextension.h>
    21 #include "btnotifsecuritymanager.h"
    21 #include "btnotifsecuritymanager.h"
    22 #include "bluetoothtrace.h"
    22 #include "bluetoothtrace.h"
       
    23 #include "bluetoothnotification.h"
       
    24 #include "btnotifserver.h"
       
    25 #include "btnotifconnectiontracker.h"
       
    26 #include "btnotificationmanager.h"
    23 
    27 
    24 /**  Length of the default PIN. */
    28 /**  Length of the default PIN. */
    25 const TInt KDefaultHeadsetPinLength = 4;
    29 const TInt KDefaultHeadsetPinLength = 4;
       
    30 
       
    31 /** Maximum repeated outgoing pairing attempt.
       
    32  *  if the pairing fails the UI specs says
       
    33  *  we can ask twice the user if he/she want 
       
    34  *  to retry pairing. 
       
    35  */
       
    36 const TInt KMaxRepeatedPairingAttempt = 2;
    26 
    37 
    27 enum TPairingStageId
    38 enum TPairingStageId
    28     {
    39     {
    29     /**
    40     /**
    30      * no pairing operation ongoing
    41      * no pairing operation ongoing
    82 //
    93 //
    83 void CBTNotifOutgoingPairingHandler::ConstructL()
    94 void CBTNotifOutgoingPairingHandler::ConstructL()
    84     {
    95     {
    85     BaseConstructL();
    96     BaseConstructL();
    86     User::LeaveIfError( iTimer.CreateLocal() );
    97     User::LeaveIfError( iTimer.CreateLocal() );
       
    98     iPairingAttempt = KMaxRepeatedPairingAttempt;
    87     }
    99     }
    88 
   100 
    89 // ---------------------------------------------------------------------------
   101 // ---------------------------------------------------------------------------
    90 // NewL
   102 // NewL
    91 // ---------------------------------------------------------------------------
   103 // ---------------------------------------------------------------------------
   111         iActive->Cancel();
   123         iActive->Cancel();
   112         }
   124         }
   113     iBondingSession.Close();
   125     iBondingSession.Close();
   114     iSocket.Close();
   126     iSocket.Close();
   115     iTimer.Close();
   127     iTimer.Close();
       
   128     if( iNotification )
       
   129         {
       
   130         // Clear the notification callback, we cannot receive them anymore.
       
   131         iNotification->RemoveObserver();
       
   132         iNotification->Close(); // Also dequeues the notification from the queue.
       
   133         iNotification = NULL;
       
   134         }
   116     }
   135     }
   117 
   136 
   118 // ---------------------------------------------------------------------------
   137 // ---------------------------------------------------------------------------
   119 // Simply deny the request as this is handing outgoing pairing
   138 // Simply deny the request as this is handing outgoing pairing
   120 // ---------------------------------------------------------------------------
   139 // ---------------------------------------------------------------------------
   121 //
   140 //
   122 TInt CBTNotifOutgoingPairingHandler::ObserveIncomingPair( const TBTDevAddr& /*aAddr*/ )
   141 TInt CBTNotifOutgoingPairingHandler::ObserveIncomingPair( const TBTDevAddr& aAddr )
   123     {
   142     {
       
   143     (void)aAddr;
   124     return KErrServerBusy;
   144     return KErrServerBusy;
   125     }
   145     }
   126 
   146 
   127 // ---------------------------------------------------------------------------
   147 // ---------------------------------------------------------------------------
   128 // Accept the request only this device is not busy with another pairing request.
   148 // Accept the request only this device is not busy with another pairing request.
   134     if ( iActive->IsActive() || aAddr != iAddr )
   154     if ( iActive->IsActive() || aAddr != iAddr )
   135         {
   155         {
   136         // we don't allow another pairing request.
   156         // we don't allow another pairing request.
   137         User::Leave( KErrServerBusy );
   157         User::Leave( KErrServerBusy );
   138         }
   158         }
   139     
       
   140     iAddr = aAddr;
   159     iAddr = aAddr;
   141     iCod = TBTDeviceClass( aCod );
   160     iCod = TBTDeviceClass( aCod );
   142     UnSetPairResult();
   161     UnSetPairResult();
   143     iParent.UnpairDevice( iAddr );
   162     iParent.UnpairDevice( iAddr );
   144     if ( CBtDevExtension::IsHeadset( iCod ) )
   163     if ( CBtDevExtension::IsHeadset( iCod ) )
   159 //
   178 //
   160 void CBTNotifOutgoingPairingHandler::CancelOutgoingPair()
   179 void CBTNotifOutgoingPairingHandler::CancelOutgoingPair()
   161     {
   180     {
   162     BOstraceFunctionEntry0( DUMMY_DEVLIST );
   181     BOstraceFunctionEntry0( DUMMY_DEVLIST );
   163     iParent.RenewPairingHandler( NULL );
   182     iParent.RenewPairingHandler( NULL );
       
   183     if( iNotification )
       
   184         {
       
   185         // Cancel the user query
       
   186         // This will also unregister us from the notification.
       
   187         TInt err = iNotification->Close();
       
   188         NOTIF_NOTHANDLED( !err )
       
   189         iNotification = NULL;
       
   190         }
   164     }
   191     }
   165 
   192 
   166 
   193 
   167 // ---------------------------------------------------------------------------
   194 // ---------------------------------------------------------------------------
   168 // when phone initiated a pairing request towards a headset,
   195 // when phone initiated a pairing request towards a headset,
   183         // 0000 will not be supplied as it does not mmet the security
   210         // 0000 will not be supplied as it does not mmet the security
   184         // requirements
   211         // requirements
   185         const TUint8 KZeroPinValue = '0';
   212         const TUint8 KZeroPinValue = '0';
   186         for (TInt i = 0; i < KDefaultHeadsetPinLength; ++i)
   213         for (TInt i = 0; i < KDefaultHeadsetPinLength; ++i)
   187             {
   214             {
   188             aPin().iPIN[i] = KZeroPinValue;
   215                 aPin().iPIN[i] = KZeroPinValue;
   189             }
   216             }
   190         aPin().iLength = KDefaultHeadsetPinLength;
   217         aPin().iLength = KDefaultHeadsetPinLength;
   191         }
   218         }
   192     }
   219     }
   193 
   220 
   200     BOstraceFunctionEntry0( DUMMY_DEVLIST );
   227     BOstraceFunctionEntry0( DUMMY_DEVLIST );
   201     if ( aAddr == iAddr )
   228     if ( aAddr == iAddr )
   202         {
   229         {
   203         iParent.OutgoingPairCompleted( KErrCancel );
   230         iParent.OutgoingPairCompleted( KErrCancel );
   204         iParent.RenewPairingHandler( NULL );
   231         iParent.RenewPairingHandler( NULL );
       
   232         if( iNotification )
       
   233             {
       
   234             // Cancel the user query
       
   235             // This will also unregister us from the notification.
       
   236             TInt err = iNotification->Close();
       
   237             NOTIF_NOTHANDLED( !err )
       
   238             iNotification = NULL;
       
   239             }
   205         }
   240         }
   206     }
   241     }
   207 
   242 
   208 // ---------------------------------------------------------------------------
   243 // ---------------------------------------------------------------------------
   209 // Pairing result will be received when pairing operation completes.
   244 // Pairing result will be received when pairing operation completes.
   294             {
   329             {
   295             SetPairResult( aStatus );
   330             SetPairResult( aStatus );
   296             }
   331             }
   297         if ( aStatus )
   332         if ( aStatus )
   298             {
   333             {
   299             iParent.OutgoingPairCompleted( aStatus );
   334             // retry pairing
   300             }
   335             if(aStatus && iPairingAttempt > 0)
       
   336                 {
       
   337                 if(aActive->RequestId() == EGeneralBondingRetry && iPairMode == EBTOutgoingHeadsetManualPairing)
       
   338                     {
       
   339                     // Headset pairing failed, reset and try again from auto pairing
       
   340                     iActive->SetRequestId(EGeneralBonding);
       
   341                     }
       
   342                 iPairingAttempt --;
       
   343                 ShowPairingRetryDialog();
       
   344                 }
       
   345             else
       
   346                 {
       
   347                 iPairingAttempt --;
       
   348                 ShowPairingFailureDialog();
       
   349                 }
       
   350              }
   301         }
   351         }
   302     }
   352     }
   303 
   353 
   304 // ---------------------------------------------------------------------------
   354 // ---------------------------------------------------------------------------
   305 // From class MBTEngActiveObserver.
   355 // From class MBTEngActiveObserver.
   342     BOstrace1(TRACE_DEBUG,DUMMY_DEVLIST,"error: %d", aError );
   392     BOstrace1(TRACE_DEBUG,DUMMY_DEVLIST,"error: %d", aError );
   343     // Our RunL can actually not leave, so we should never reach here.
   393     // Our RunL can actually not leave, so we should never reach here.
   344     (void) aActive;
   394     (void) aActive;
   345     iParent.OutgoingPairCompleted( aError );
   395     iParent.OutgoingPairCompleted( aError );
   346     iParent.RenewPairingHandler( NULL );
   396     iParent.RenewPairingHandler( NULL );
       
   397     if( iNotification )
       
   398         {
       
   399         // Cancel the user query
       
   400         // This will also unregister us from the notification.
       
   401         TInt err = iNotification->Close();
       
   402         NOTIF_NOTHANDLED( !err )
       
   403         iNotification = NULL;
       
   404         }
   347     }
   405     }
   348 
   406 
   349 // ---------------------------------------------------------------------------
   407 // ---------------------------------------------------------------------------
   350 // decide the next state and issue pair request
   408 // decide the next state and issue pair request
   351 // ---------------------------------------------------------------------------
   409 // ---------------------------------------------------------------------------
   388         }
   446         }
   389     iActive->GoActive();
   447     iActive->GoActive();
   390     BOstraceFunctionExit0( DUMMY_DEVLIST );
   448     BOstraceFunctionExit0( DUMMY_DEVLIST );
   391     }
   449     }
   392 
   450 
       
   451 // ---------------------------------------------------------------------------
       
   452 // From class MBTNotificationResult.
       
   453 // Handle a result from a user query.
       
   454 // ---------------------------------------------------------------------------
       
   455 //
       
   456 void CBTNotifOutgoingPairingHandler::MBRDataReceived( CHbSymbianVariantMap& aData )
       
   457     {
       
   458     BOstraceFunctionEntry0( DUMMY_DEVLIST );
       
   459     (void) aData;
       
   460     BOstraceFunctionExit0( DUMMY_DEVLIST );
       
   461     }
       
   462 
       
   463 // ---------------------------------------------------------------------------
       
   464 // From class MBTNotificationResult.
       
   465 // The notification is finished.
       
   466 // ---------------------------------------------------------------------------
       
   467 //
       
   468 void CBTNotifOutgoingPairingHandler::MBRNotificationClosed( TInt aError, const TDesC8& aData )
       
   469     {
       
   470     BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, aError );
       
   471     // First unregister from the notification, so we can already get the next one.
       
   472     iNotification->RemoveObserver();
       
   473     iNotification = NULL;
       
   474     TRAP_IGNORE( NotificationClosedL( aError, aData ) );
       
   475     BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   476     }
       
   477 
       
   478 // ---------------------------------------------------------------------------
       
   479 // Get and configure a notification.
       
   480 // ---------------------------------------------------------------------------
       
   481 //
       
   482 void CBTNotifOutgoingPairingHandler::PrepareNotificationL( TBluetoothDialogParams::TBTDialogType aType,
       
   483     TBTDialogResourceId aResourceId )
       
   484     {
       
   485     BOstraceFunctionEntry0( DUMMY_DEVLIST );
       
   486     iNotification = 
       
   487             iParent.ConnectionTracker().NotificationManager()->GetNotification();
       
   488     User::LeaveIfNull( iNotification ); // For OOM exception, leaves with KErrNoMemory
       
   489     iNotification->SetObserver( this );
       
   490     iNotification->SetNotificationType( aType, aResourceId );
       
   491     const CBtDevExtension* dev = iParent.BTDevRepository().Device(iAddr);
       
   492     if(dev)
       
   493         {
       
   494         User::LeaveIfError(iNotification->SetData( TBluetoothDeviceDialog::EDeviceName, dev->Alias()));
       
   495         }
       
   496     else
       
   497         {
       
   498         TBTDeviceName name;
       
   499         iAddr.GetReadable(name);
       
   500         User::LeaveIfError(iNotification->SetData( TBluetoothDeviceDialog::EDeviceName, name));
       
   501         NOTIF_NOTHANDLED( !err )            
       
   502         }
       
   503     iParent.ConnectionTracker().NotificationManager()->QueueNotificationL( iNotification);
       
   504     NOTIF_NOTHANDLED( !err )
       
   505     BOstraceFunctionExit0( DUMMY_DEVLIST );
       
   506     }
       
   507 
       
   508 // ---------------------------------------------------------------------------
       
   509 // The notification is finished, handle the result.
       
   510 // ---------------------------------------------------------------------------
       
   511 //
       
   512 void CBTNotifOutgoingPairingHandler::NotificationClosedL( TInt aError, const TDesC8& aData )
       
   513     {
       
   514     BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, aError );
       
   515     // Read the result.
       
   516     TPckgC<TBool> result( EFalse );
       
   517     result.Set( aData.Ptr(), result.Length() ); // Read the part containing the result
       
   518     // Set a pointer descriptor to capture the remaining data, if any.
       
   519     TPtrC8 dataPtr( aData.Mid( result.Length() ) );
       
   520 
       
   521     if(result() && iPairingAttempt >= 0)
       
   522         {
       
   523             HandleOutgoingPairL(iAddr,iCod.DeviceClass());
       
   524         }
       
   525     else
       
   526         {
       
   527             iPairingAttempt = KMaxRepeatedPairingAttempt; // reset the counter
       
   528             iParent.OutgoingPairCompleted( KErrCancel );
       
   529             iParent.RenewPairingHandler( NULL );
       
   530         }
       
   531     BOstraceFunctionExit1( DUMMY_DEVLIST, this );
       
   532     }
       
   533 
       
   534 // ---------------------------------------------------------------------------
       
   535 // Show a dialog to ask the user to retry the pairing
       
   536 // ---------------------------------------------------------------------------
       
   537 //
       
   538 void CBTNotifOutgoingPairingHandler::ShowPairingRetryDialog()
       
   539     {
       
   540     PrepareNotificationL( TBluetoothDialogParams::EQuery, EPairingFailureRetry);
       
   541     }
       
   542 
       
   543 // ---------------------------------------------------------------------------
       
   544 // Show a dialog to tell the user pairing retry attempt failed
       
   545 // ---------------------------------------------------------------------------
       
   546 //
       
   547 void CBTNotifOutgoingPairingHandler::ShowPairingFailureDialog()
       
   548     {
       
   549     PrepareNotificationL( TBluetoothDialogParams::EQuery, EPairingFailureOk );
       
   550     }
       
   551 
       
   552 
       
   553