bluetoothengine/btnotif/btnotifsrv/src/btnotifserviceauthorizer.cpp
branchRCL_3
changeset 56 9386f31cc85b
parent 55 613943a21004
child 61 269724087bed
equal deleted inserted replaced
55:613943a21004 56:9386f31cc85b
     1 /*
       
     2 * Copyright (c) 2010 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: class for prompting user to authorize a service
       
    15 *              connection request.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <hb/hbcore/hbsymbianvariant.h>
       
    20 #include <btengconnman.h>
       
    21 #include "btnotifserviceauthorizer.h"
       
    22 #include "btnotifsecuritymanager.h"
       
    23 #include "bluetoothtrace.h"
       
    24 #include "btnotifclientserver.h"
       
    25 #include "bluetoothnotification.h"
       
    26 #include "btnotifconnectiontracker.h"
       
    27 #include "btnotificationmanager.h"
       
    28 #include "btnotifserver.h"
       
    29 #include "btnotifutil.h"
       
    30 
       
    31 //
       
    32 // SDP UUID Constants - Short form
       
    33 // Taken from Bluetooth Profile specification v1.1
       
    34 // These are used when registering the service to
       
    35 // local SDP database and when searching the service
       
    36 // information from remote device.
       
    37 const TUint KBTSdpDun                   = 0x1103;
       
    38 const TUint KBTSdpGenericTelephony      = 0x1204;
       
    39 const TUint KBTSdpFax                   = 0x1111;
       
    40 const TUint KBTSdpObjectPush            = 0x1105;
       
    41 const TUint KBTSdpFileTransfer          = 0x1106;
       
    42 const TUint KBTSdpHeadSet               = 0x1108;
       
    43 const TUint KBTSdpGenericNetworking     = 0x1201;
       
    44 const TUint KBTSdpBasicImaging          = 0x111b;
       
    45 
       
    46 
       
    47 CBTNotifServiceAuthorizer* CBTNotifServiceAuthorizer::NewL(
       
    48                                         CBTNotifSecurityManager& aParent)
       
    49     {
       
    50     CBTNotifServiceAuthorizer* me = new (ELeave) CBTNotifServiceAuthorizer(aParent);
       
    51     CleanupStack::PushL(me);
       
    52     me->ConstructL();
       
    53     CleanupStack::Pop(me);
       
    54     return me;
       
    55     }
       
    56 
       
    57 CBTNotifServiceAuthorizer::~CBTNotifServiceAuthorizer()
       
    58     {
       
    59     iParams.Close();
       
    60     if( iNotification )
       
    61         {
       
    62         // Clear the notification callback, we cannot receive them anymore.
       
    63         iNotification->RemoveObserver();
       
    64         iNotification->Close(); // Also dequeues the notification from the queue.
       
    65         iNotification = NULL;
       
    66         }
       
    67     if ( !iNotifierMessage.IsNull() )
       
    68         {
       
    69         iNotifierMessage.Complete( KErrServerTerminated );
       
    70         }
       
    71     }
       
    72 
       
    73 CBTNotifServiceAuthorizer::CBTNotifServiceAuthorizer(
       
    74                                         CBTNotifSecurityManager& aParent)
       
    75 :iParent(aParent)
       
    76     {
       
    77     }
       
    78 
       
    79 void CBTNotifServiceAuthorizer::ConstructL()
       
    80     {
       
    81     }
       
    82 
       
    83 void CBTNotifServiceAuthorizer::StartNotifierL(const RMessage2& aMessage)
       
    84     {
       
    85     if (!iNotifierMessage.IsNull())
       
    86         {
       
    87         if(aMessage.Function() == EBTNotifCancelNotifier)
       
    88             {
       
    89             TInt err = iNotification->Close();
       
    90             iNotifierMessage.Complete(KErrCancel);
       
    91             aMessage.Complete(err);
       
    92             return;
       
    93             }
       
    94         BOstrace0(TRACE_DEBUG,DUMMY_DEVLIST,"[BTNotif]:We are busy");
       
    95         User::Leave(KErrServerBusy );
       
    96         }
       
    97 
       
    98     iParams.ReAllocL( aMessage.GetDesLengthL( EBTNotifSrvParamSlot ) );
       
    99     aMessage.ReadL( EBTNotifSrvParamSlot, iParams );
       
   100     
       
   101     TBTAuthorisationParams params;
       
   102     TPckgC<TBTAuthorisationParams> paramsPckg( params );
       
   103     paramsPckg.Set( iParams );
       
   104     
       
   105     iServiceId = paramsPckg().iUid.iUid;
       
   106     
       
   107     const CBtDevExtension* dev = NULL;
       
   108     dev = iParent.BTDevRepository().Device(paramsPckg().iBDAddr);
       
   109 
       
   110     if(dev && dev->Device().GlobalSecurity().Banned() )
       
   111         {
       
   112         // If the device is banned, service connection from
       
   113         // this device is not allowed:
       
   114         BOstrace0(TRACE_DEBUG,DUMMY_DEVLIST,"[BTNotif]:Device is banned");
       
   115         aMessage.Complete( KErrCancel);
       
   116         return;    
       
   117         }
       
   118 
       
   119     if(dev && dev->Device().GlobalSecurity().NoAuthorise())
       
   120         {
       
   121         // If the device is a trusted one, no need to pop up query messages.
       
   122         TPckgBuf<TBool> answer;
       
   123         answer() = ETrue;
       
   124         aMessage.Write(EBTNotifSrvReplySlot, answer);
       
   125         aMessage.Complete(KErrNone);
       
   126         return;
       
   127         }
       
   128     
       
   129     // User must namually authorize this request. 
       
   130     // Get needed info for the dialog:
       
   131     iPairedDevice = (dev == NULL ) ? EFalse : dev->IsUserAwareBonded();
       
   132     iDeviceClass = (dev == NULL ) ? 0 : dev->Device().DeviceClass().DeviceClass();
       
   133     TBTNotifUtil::GetDeviceUiNameL(iCurrentDeviceName, 
       
   134             dev, paramsPckg().iName, paramsPckg().iBDAddr );
       
   135 
       
   136     TBool autoAuthorize;
       
   137     PrepareNotificationL(autoAuthorize,
       
   138             TBluetoothDialogParams::EUserAuthorization, 
       
   139             EAuthorization, iPairedDevice);
       
   140     if ( autoAuthorize ) 
       
   141         {
       
   142         TPckgBuf<TBool> answer; 
       
   143         answer() = ETrue;
       
   144         aMessage.Write(EBTNotifSrvReplySlot, answer);
       
   145         aMessage.Complete(KErrNone);
       
   146         }
       
   147     else 
       
   148         {
       
   149         iNotification->ShowL();
       
   150         // we do not save the message until all leavable functions have executed successfully.
       
   151         // This makes sure the iNotifierMessage has a valid handle.
       
   152         iNotifierMessage = aMessage;
       
   153         }
       
   154     }
       
   155 
       
   156 void CBTNotifServiceAuthorizer::MBRDataReceived( CHbSymbianVariantMap& aData )
       
   157     {
       
   158     // "actionResult" will be true if the user clicks 'Yes' on the dialog and false, if he/she clicks 'No'
       
   159     // "iCheckBoxState" will be set to true of the checkbox is checked, else false.
       
   160     if(aData.Keys().MdcaPoint(0).Compare(_L("actionResult")) == 0)
       
   161         {
       
   162         TBTAuthorisationParams params;
       
   163         TPckgC<TBTAuthorisationParams> paramsPckg(params);
       
   164         paramsPckg.Set(iParams);
       
   165 
       
   166         TPckgBuf<TBool> answer;
       
   167         TInt val = *(static_cast<TInt*>(aData.Get(_L("actionResult"))->Data()));
       
   168         if(val)
       
   169             {
       
   170             answer() = ETrue;
       
   171             if(iCheckBoxState)
       
   172                 {
       
   173                 // Set the device "Trusted" property
       
   174                 iParent.TrustDevice(paramsPckg().iBDAddr);
       
   175                 }
       
   176              }
       
   177         else
       
   178             {
       
   179             answer() = EFalse;
       
   180             if(iCheckBoxState)
       
   181                 {
       
   182                 // If the device is paried, unpair it as well.
       
   183                 if(iPairedDevice)
       
   184                     {
       
   185                     iParent.UnpairDevice(paramsPckg().iBDAddr);
       
   186                     }
       
   187                 
       
   188                 //Set the device "Blocked" property
       
   189                 iParent.BlockDevice(paramsPckg().iBDAddr,ETrue);
       
   190                 }
       
   191             }
       
   192         if ( !iNotifierMessage.IsNull() )
       
   193             {
       
   194             iNotifierMessage.Write(EBTNotifSrvReplySlot, answer);
       
   195             iNotifierMessage.Complete(KErrNone);
       
   196             }
       
   197         }
       
   198     else if(aData.Keys().MdcaPoint(0).Compare(_L("checkBoxState")) == 0)
       
   199         {
       
   200         iCheckBoxState = *(static_cast<TInt*>(aData.Get(_L("checkBoxState"))->Data()));
       
   201         }
       
   202     }
       
   203 
       
   204 void CBTNotifServiceAuthorizer::MBRNotificationClosed( TInt aError, const TDesC8& aData )
       
   205     {
       
   206     (void) aError;
       
   207     (void) aData;
       
   208     iNotification->RemoveObserver();
       
   209     iNotification = NULL;
       
   210     }
       
   211 
       
   212 void CBTNotifServiceAuthorizer::PrepareNotificationL(TBool& aAutoAuthorize,
       
   213         TBluetoothDialogParams::TBTDialogType aType,
       
   214     TBTDialogResourceId aResourceId, TBool aPaired)
       
   215     {
       
   216     iNotification = iParent.ConnectionTracker().NotificationManager()->GetNotification();
       
   217     User::LeaveIfNull( iNotification ); // For OOM exception, leaves with KErrNoMemory
       
   218     iNotification->SetObserver( this );
       
   219     iNotification->SetNotificationType( aType, aResourceId );
       
   220     TInt err = KErrNone;
       
   221     aAutoAuthorize = EFalse;
       
   222     
       
   223     //Set the dialog title based on the service IDs
       
   224     switch(iServiceId)
       
   225         {
       
   226         case KBTSdpObjectPush:
       
   227         case KBTSdpBasicImaging:
       
   228             {
       
   229             if(aPaired)
       
   230                 {
       
   231                 err = iNotification->SetData( TBluetoothDialogParams::EDialogTitle, TBluetoothDialogParams::EReceiveFromPairedDevice);
       
   232                 // In case of receiving a msg from a paired deivce, the checkbox is checked by default.
       
   233                 iCheckBoxState = ETrue;
       
   234                 User::LeaveIfError(err);
       
   235                 }
       
   236             else
       
   237                 {
       
   238                 err = iNotification->SetData( TBluetoothDialogParams::EDialogTitle, TBluetoothDialogParams::EReceive);
       
   239                 iCheckBoxState = EFalse;
       
   240                 User::LeaveIfError(err);
       
   241                 }
       
   242             }
       
   243             break;
       
   244             
       
   245         case KBTSdpFax:
       
   246         case KBTSdpDun:
       
   247         case KBTSdpFileTransfer:
       
   248         case KBTSdpHeadSet:
       
   249         case KBTSdpGenericTelephony:
       
   250         case KBTSdpGenericNetworking:
       
   251             {
       
   252             err = iNotification->SetData( TBluetoothDialogParams::EDialogTitle, TBluetoothDialogParams::EConnect);
       
   253             // In case of an incoming connection, the checkbox is checked by default.
       
   254             iCheckBoxState = ETrue;
       
   255             User::LeaveIfError(err);
       
   256             }
       
   257             break;
       
   258             
       
   259         default:
       
   260             {
       
   261             TBTAuthorisationParams params;
       
   262             TPckgC<TBTAuthorisationParams> paramsPckg(params);
       
   263             paramsPckg.Set(iParams);
       
   264             
       
   265             // In this case, if there already exists a connection to an audio device, then we simply accept
       
   266             // the incoming connection without querying the user.
       
   267             // If there is no existing connection, then we pop up a query message.
       
   268             if(IsExistingConnectionToAudioL(paramsPckg().iBDAddr))
       
   269                 {
       
   270                 aAutoAuthorize = ETrue;
       
   271                 return;
       
   272                 }
       
   273             else
       
   274                 {
       
   275                 err = iNotification->SetData( TBluetoothDialogParams::EDialogTitle, TBluetoothDialogParams::EConnect);
       
   276                 // In case of an incoming connection, the checkbox is checked by default.
       
   277                 iCheckBoxState = ETrue;
       
   278                 User::LeaveIfError(err);
       
   279                 }
       
   280             }
       
   281             break;
       
   282         }
       
   283     
       
   284     //Add the device name 
       
   285     err = iNotification->SetData( TBluetoothDeviceDialog::EDeviceName, iCurrentDeviceName );
       
   286     User::LeaveIfError(err);
       
   287     //Add the device class
       
   288     err = iNotification->SetData( TBluetoothDeviceDialog::EDeviceClass, iDeviceClass );
       
   289     User::LeaveIfError(err);
       
   290     
       
   291     }
       
   292 
       
   293 TBool CBTNotifServiceAuthorizer::IsExistingConnectionToAudioL(const TBTDevAddr& aDevAddr)
       
   294     {
       
   295     CBTEngConnMan* connMan = CBTEngConnMan::NewL();
       
   296     TBTEngConnectionStatus conntatus(EBTEngNotConnected);
       
   297     (void) connMan->IsConnected(aDevAddr,conntatus);      
       
   298     delete connMan;
       
   299     return (conntatus==EBTEngConnected || conntatus==EBTEngConnecting);
       
   300     }
       
   301