bluetoothengine/btnotif/src/BTNPinNotifier.cpp
changeset 0 f63038272f30
child 6 6a29d5ad0713
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Declares authentication (PIN query) notifier class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <StringLoader.h>    // Localisation stringloader
       
    21 #include <utf.h>             // Unicode character conversion utilities
       
    22 #include <avkon.mbg>
       
    23 #include <btengconnman.h>
       
    24 #include <btotgpairpub.inl>
       
    25 #include <btextnotifiers.h>
       
    26 #include <BTNotif.rsg>       // Own resources
       
    27 #include "btnotif.hrh"       // Own resource header
       
    28 #include "btnpinnotifier.h"  // Own class definition
       
    29 #include "btNotifDebug.h"    // Debugging macros
       
    30 #include "btnotifnameutils.h"
       
    31 
       
    32 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    33 #include <btextnotifiers.h>
       
    34 #else
       
    35 #include <btextnotifiers.h>
       
    36 #include <btextnotifierspartner.h>
       
    37 #endif
       
    38 
       
    39 #include <SecondaryDisplay/BTnotifSecondaryDisplayAPI.h>
       
    40 
       
    41 #ifdef __SERIES60_HELP
       
    42 #include <hlplch.h>
       
    43 #include <csxhelp/bt.hlp.hrh> // The bt hrh info is needed, for help launching
       
    44 #endif
       
    45 const TInt KDefaultPinCodeLength=4;
       
    46 const TUint8 KZeroPinValue = '0';
       
    47 
       
    48 // ================= MEMBER FUNCTIONS =======================
       
    49 
       
    50 // ----------------------------------------------------------
       
    51 // CBTPinNotifier::NewL
       
    52 // Two-phased constructor.
       
    53 // ----------------------------------------------------------
       
    54 //
       
    55 CBTPinNotifier* CBTPinNotifier::NewL()
       
    56     {
       
    57     CBTPinNotifier* self=new (ELeave) CBTPinNotifier();
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL();
       
    60     CleanupStack::Pop(self);
       
    61     return self;
       
    62     }
       
    63 
       
    64 // ----------------------------------------------------------
       
    65 // CBTPinNotifier::CBTPinNotifier
       
    66 // C++ default constructor can NOT contain any code, that
       
    67 // might leave.
       
    68 // ----------------------------------------------------------
       
    69 //
       
    70 CBTPinNotifier::CBTPinNotifier()
       
    71     {    
       
    72     }
       
    73 
       
    74 // ----------------------------------------------------------
       
    75 // Destructor
       
    76 // ----------------------------------------------------------
       
    77 //
       
    78 CBTPinNotifier::~CBTPinNotifier()
       
    79     {
       
    80     }
       
    81 
       
    82 // ----------------------------------------------------------
       
    83 // CBTPinNotifier::RegisterL
       
    84 // Register notifier.
       
    85 // ----------------------------------------------------------
       
    86 //
       
    87 CBTPinNotifier::TNotifierInfo CBTPinNotifier::RegisterL()
       
    88     {
       
    89     iInfo.iUid=KBTPinCodeEntryNotifierUid;
       
    90     iInfo.iChannel=KBTAuthenticationChannel;
       
    91     iInfo.iPriority=ENotifierPriorityVHigh;
       
    92     return iInfo;
       
    93     }
       
    94 
       
    95 // ----------------------------------------------------------
       
    96 // CBTPinNotifier::GetParamsL
       
    97 // Initialize parameters and check if device is already
       
    98 // in registry. Jump to RunL as soon as possible.
       
    99 // ----------------------------------------------------------
       
   100 //
       
   101 void CBTPinNotifier::GetParamsL(const TDesC8& aBuffer, TInt aReplySlot, const RMessagePtr2& aMessage)
       
   102     {
       
   103     FLOG(_L("[BTNOTIF]\t CBTPinNotifier::GetParamsL() >>"));
       
   104 
       
   105     CBTNPairNotifierBase::GetParamsL( aBuffer, aReplySlot, aMessage );
       
   106 
       
   107     TBTPinCodeEntryNotifierParams param;
       
   108  	TPckgC<TBTPinCodeEntryNotifierParams> pckg(param);
       
   109  	pckg.Set(aBuffer);
       
   110 
       
   111     iBTAddr = pckg().DeviceAddress();
       
   112     if ( OtherOutgoPairing(iBTAddr) )
       
   113         {
       
   114         // We won't allow incoming pairing during outgoing pairing:
       
   115         FLOG(_L("[BTNOTIF]\t CBTPinNotifier: outgoing pair in progress, reject incoming pair"));
       
   116         CompleteMessage(KErrCancel);
       
   117         return;
       
   118         }
       
   119 
       
   120  	iLocallyInitiated = pckg().LocallyInitiated();
       
   121     iPasskeyLength = pckg().PinCodeMinLength();
       
   122     iStrongPinRequired = pckg().StrongPinCodeRequired();
       
   123     ProcessParamsGetDeviceL( iBTAddr, pckg().DeviceName() );
       
   124     
       
   125     FLOG(_L("[BTNOTIF]\t CBTPinNotifier::GetParamsL() <<"));
       
   126     }
       
   127 
       
   128 // ----------------------------------------------------------
       
   129 // CBTPinNotifier::HandleGetDeviceCompletedL
       
   130 // Ask user response and return it to caller.
       
   131 // Store device into registry if user has accepted pairing.
       
   132 // ----------------------------------------------------------
       
   133 //
       
   134 void CBTPinNotifier::HandleGetDeviceCompletedL(const CBTDevice* aDev)
       
   135     {
       
   136     FLOG(_L("[BTNOTIF]\t CBTPinNotifier::HandleGetDeviceCompletedL() >>"));
       
   137     (void) aDev;
       
   138 
       
   139     // Check if blocked device  
       
   140     if( CheckBlockedDeviceL() )
       
   141         return;
       
   142 
       
   143     // Turn lights on and deactivate apps -key
       
   144     iNotifUiUtil->TurnLightsOn();
       
   145     
       
   146     if( CheckAndSetAutomatedPairing() )
       
   147         {
       
   148         CompleteMessage(iPinCode, KErrNone);
       
   149         return;
       
   150         }
       
   151 
       
   152     // For incoming pairing request
       
   153     if(!iLocallyInitiated)
       
   154     	{
       
   155     	TBool userAnswer = AuthoriseIncomingPairingL();
       
   156         if( !userAnswer )
       
   157             {
       
   158             //User rejects the incoming pairing, complete message in QueryBlockDevice.
       
   159             FLOG(_L("[BTNOTIF]\t CBTPinNotifier::HandleGetDeviceCompletedL() <<"));
       
   160             return;
       
   161             }
       
   162     	}
       
   163 
       
   164 	// Use a forever loop here to show query dialog again of ínput pin code does't fulfill the
       
   165 	// minimum length requirement 
       
   166 	//      
       
   167  	FOREVER
       
   168 	 	{
       
   169 	    TBuf<KBTPassKeyMaxLength> tempResultBuffer;
       
   170 	    
       
   171 	    HBufC* prompt = GenerateQueryPromoptLC(); // prompt still in stack.
       
   172         TInt keypress = iNotifUiUtil->ShowTextInputQueryL(tempResultBuffer, *prompt, R_BT_ENTER_PASSKEY_QUERY, 
       
   173                 ECmdShowBtOpenCoverNote, CAknQueryDialog::EConfirmationTone );
       
   174         CleanupStack::PopAndDestroy( prompt );
       
   175 
       
   176         if( keypress ) // OK pressed
       
   177         	{
       
   178        		// Convert data from buf to pin
       
   179         	TBuf8<KBTPassKeyMaxLength> buf8;
       
   180         	buf8.Zero();
       
   181         	CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8, tempResultBuffer);
       
   182         	
       
   183         	    //check if the length of pin less that minimum required pin code length
       
   184         	if ((iPasskeyLength == 0) || ( buf8.Length() >= iPasskeyLength))
       
   185         		{
       
   186 			    iPinCode().iLength = STATIC_CAST(TUint8, buf8.Length());
       
   187 			    for (TInt index=0; index<buf8.Length(); index++)
       
   188 			        {
       
   189 			        iPinCode().iPIN[index] = buf8[index];
       
   190 			        }
       
   191 			   	CompleteMessage(iPinCode, KErrNone);
       
   192         		break;
       
   193         		}
       
   194         	else 
       
   195         		{
       
   196         		//keep FOREVER loop runing, till the user input pin meet the requirement.
       
   197         		tempResultBuffer.Zero();
       
   198         		}
       
   199         	}
       
   200 	    else    // Cancel pressed
       
   201 	        {
       
   202             // If the outgoing pairing mode is set, then this is bonding initiated by the user.
       
   203             TBool incomingConn = ( OutgoPairingMode( iBTAddr ) == EBTOutgoingPairNone );
       
   204             if( !iMessage.IsNull() )
       
   205                 {
       
   206                 // No need to inform the user of the pairing result if the user cancelled it.
       
   207                 // If user doesn't answer anything but stack cancels, pairing result is needed 
       
   208                 // to show for outgoing pairing. In this case, don't stop pairng observer.
       
   209                 CBTEngConnMan::StopPairingObserver( iBTAddr );    
       
   210                 }
       
   211 
       
   212             // We may be asking for pairing (locally initiated), because a remote device is requesting
       
   213             // accesss to a service for which we require authentication.
       
   214             if( iLocallyInitiated && incomingConn )
       
   215                 {
       
   216                 CheckAndHandleQueryIntervalL();
       
   217                 }
       
   218             CompleteMessage(KErrCancel);
       
   219 	        return;
       
   220 	        }
       
   221 	 	}//END of FOREVER loop
       
   222 
       
   223     FLOG(_L("[BTNOTIF]\t CBTPinNotifier::HandleGetDeviceCompletedL() completed"));
       
   224     }
       
   225 
       
   226 // ----------------------------------------------------------
       
   227 // CBTPinNotifier::CheckAndSetAutomatedPairing
       
   228 // ---------------------------------------------------------- 
       
   229 TBool CBTPinNotifier::CheckAndSetAutomatedPairing()
       
   230     {
       
   231     FLOG(_L("[BTNOTIF]\t CBTPinNotifier::CheckAndSetAutomatedPairing()"));    
       
   232     TBool ret( EFalse );
       
   233     // check if device is headset and passkey is empty -> do automated pairing with 0000.
       
   234 	if ( iDevice && 
       
   235 	     OutgoPairingMode( iBTAddr ) == EBTOutgoingHeadsetAutoPairing && 
       
   236 	     iPasskeyLength <= KDefaultPinCodeLength )
       
   237 	    {
       
   238 	    FLOG( _L( "[BTNOTIF]\t CBTPinNotifier::CheckAndSetAutomatedPairing - automated pairing on" ) );
       
   239 	    FLOG(_L("[BTNOTIF]\t Setting default PIN code"));
       
   240 	    iPinCode().iLength = KDefaultPinCodeLength;
       
   241 	    for( TInt i=0; i < KDefaultPinCodeLength; ++i)
       
   242 	        {
       
   243 	        iPinCode().iPIN[i] = KZeroPinValue;
       
   244 	        }
       
   245 	    ret = ETrue;
       
   246 	    }
       
   247 	return ret;
       
   248     }
       
   249 
       
   250 // ----------------------------------------------------------
       
   251 // CBTPinNotifier::GenerateQueryPromoptLC
       
   252 // ---------------------------------------------------------- 
       
   253 HBufC* CBTPinNotifier::GenerateQueryPromoptLC()
       
   254     {
       
   255     FLOG(_L("[BTNOTIF]\t CBTPinNotifier::GenerateQueryPromoptLC() >>"));
       
   256     HBufC* prompt = NULL;
       
   257 
       
   258     TBTDeviceName devName;
       
   259     BtNotifNameUtils::GetDeviceDisplayName( devName, iDevice );
       
   260     if (iPasskeyLength > 0)  //Minimum length passkey is defined
       
   261         {
       
   262         CDesCArray* stringArray = new ( ELeave ) CDesCArrayFlat( 1 );
       
   263         CleanupStack::PushL( stringArray );
       
   264         CArrayFix<TInt>* indexArray = new(ELeave) CArrayFixFlat<TInt>(1);
       
   265         CleanupStack::PushL( indexArray ); 
       
   266 
       
   267         stringArray->AppendL( BTDeviceNameConverter::ToUnicodeL(iDevice->DeviceName()) );   
       
   268         indexArray->AppendL( iPasskeyLength);   
       
   269         prompt = StringLoader::LoadL( R_BT_MIN_PASSKEY_PROMPT, *stringArray, *indexArray );
       
   270 
       
   271         CleanupStack::PopAndDestroy( indexArray );
       
   272         CleanupStack::PopAndDestroy( stringArray );
       
   273         CleanupStack::PushL( prompt );
       
   274         }
       
   275     else
       
   276         {
       
   277         prompt = StringLoader::LoadLC( R_BT_PASSKEY_PROMPT, devName );
       
   278         }
       
   279     FLOG(_L("[BTNOTIF]\t CBTPinNotifier::GenerateQueryPromoptLC() <<"));
       
   280     return prompt;
       
   281     }
       
   282 // End of File