bluetoothengine/btserviceutil/src/btdevextension.cpp
changeset 19 43824b19ee35
child 31 a0ea99b6fa53
equal deleted inserted replaced
17:f05641c183ff 19:43824b19ee35
       
     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:  an extended BT device offering properties of
       
    15 * a Bluetooth device that may be needed by Bluetooth UIs
       
    16 *
       
    17 */
       
    18 
       
    19 #include <btservices/btdevextension.h>
       
    20 
       
    21 // ======== MEMBER FUNCTIONS ========
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 // C++ default constructor
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 CBtDevExtension::CBtDevExtension( TDefaultDevNameOption aNameOption ) 
       
    28 : iNameOption( aNameOption )
       
    29     {
       
    30     }
       
    31 
       
    32 // ---------------------------------------------------------------------------
       
    33 // Symbian 2nd-phase constructor
       
    34 // ---------------------------------------------------------------------------
       
    35 //
       
    36 void CBtDevExtension::ConstructL(  CBTDevice* aDev )
       
    37     {
       
    38     SetDeviceL( aDev );
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------------------------
       
    42 // NewLC
       
    43 // ---------------------------------------------------------------------------
       
    44 //
       
    45 EXPORT_C CBtDevExtension* CBtDevExtension::NewLC( 
       
    46         CBTDevice* aDev, TDefaultDevNameOption aNameOption )
       
    47     {
       
    48     CBtDevExtension* self = NULL;
       
    49     self = new (ELeave) CBtDevExtension( aNameOption );
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL( aDev );
       
    52     return self;
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // NewL
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 EXPORT_C CBtDevExtension* CBtDevExtension::NewLC( 
       
    60         const TInquirySockAddr& aAddr,
       
    61         const TDesC& aName,
       
    62         TDefaultDevNameOption aNameOption )
       
    63     {
       
    64     CBtDevExtension* self = new (ELeave) CBtDevExtension( aNameOption );
       
    65     CleanupStack::PushL( self );
       
    66     CBTDevice* dev = CBTDevice::NewLC( aAddr.BTAddr() );
       
    67     TBTDeviceClass cod( aAddr.MajorServiceClass(), 
       
    68             aAddr.MajorClassOfDevice(), aAddr.MinorClassOfDevice() );
       
    69     dev->SetDeviceClass( cod );
       
    70     if ( aName.Length() )
       
    71         {
       
    72         dev->SetDeviceNameL(  BTDeviceNameConverter::ToUTF8L( aName ) );
       
    73         }
       
    74     self->ConstructL( dev );
       
    75     CleanupStack::Pop( dev );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Destructor
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C CBtDevExtension::~CBtDevExtension()
       
    84     {
       
    85     iAlias.Close();
       
    86     delete iDev;
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // IsBonded
       
    91 // ---------------------------------------------------------------------------
       
    92 //
       
    93 TBool CBtDevExtension::IsBonded( const TBTNamelessDevice &dev )
       
    94     {
       
    95     // IsValidPaired tells if the paired bit of dev is valid
       
    96     // and IsPaired tells if the device is paired or not:
       
    97     return dev.IsValidPaired() && 
       
    98            dev.IsPaired() && 
       
    99            // Authentication due to OBEX cases e.g. file transfer, is not 
       
   100            // considered as bonded in Bluetooth UI:
       
   101            dev.LinkKeyType() != ELinkKeyUnauthenticatedUpgradable;
       
   102     }
       
   103 
       
   104 // ---------------------------------------------------------------------------
       
   105 // IsJustWorksBonded
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 TBool CBtDevExtension::IsJustWorksBonded( const TBTNamelessDevice &dev )
       
   109     {
       
   110     return IsBonded( dev ) && 
       
   111          dev.LinkKeyType() == ELinkKeyUnauthenticatedNonUpgradable;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // IsUserAwareBonded
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 TBool CBtDevExtension::IsUserAwareBonded( const TBTNamelessDevice &dev )
       
   119     {
       
   120     if ( IsJustWorksBonded( dev ) )
       
   121         {
       
   122         // Just Works bonded devices can happen without user awareness.
       
   123         // For example, authentication due to an incoming service connection request 
       
   124         // from a device without IO.
       
   125         // We use cookies to identify if this JW pairing is user aware or not:
       
   126         TInt32 cookie = dev.IsValidUiCookie() ? dev.UiCookie() : EBTUiCookieUndefined;
       
   127         return (cookie & EBTUiCookieJustWorksPaired );        
       
   128         }
       
   129     // Pairing in other mode than Just Works are always user-aware:
       
   130     return IsBonded( dev );
       
   131     }
       
   132 
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // 
       
   136 // ---------------------------------------------------------------------------
       
   137 //
       
   138 EXPORT_C const TDesC& CBtDevExtension::Alias() const
       
   139     {
       
   140     return iAlias;
       
   141     }
       
   142 
       
   143 // ---------------------------------------------------------------------------
       
   144 // Addr()
       
   145 // ---------------------------------------------------------------------------
       
   146 //
       
   147 EXPORT_C const TBTDevAddr& CBtDevExtension::Addr() const
       
   148     {
       
   149     return iDev->BDAddr();
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // Device
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 EXPORT_C const CBTDevice& CBtDevExtension::Device() const
       
   157     {
       
   158     return *iDev;
       
   159     }
       
   160 
       
   161 
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // IsUserAwareBonded
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 EXPORT_C TBool CBtDevExtension::IsUserAwareBonded() const
       
   168     {
       
   169     return IsUserAwareBonded( iDev->AsNamelessDevice() );
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // ServiceConnectionStatus()
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 TBTEngConnectionStatus CBtDevExtension::ServiceConnectionStatus() const
       
   177     {
       
   178     return iServiceStatus;
       
   179     }
       
   180 
       
   181 // ---------------------------------------------------------------------------
       
   182 // SetDeviceL
       
   183 // ---------------------------------------------------------------------------
       
   184 //
       
   185 EXPORT_C void CBtDevExtension::SetDeviceL( CBTDevice* aDev )
       
   186     {
       
   187     delete iDev;
       
   188     iDev = aDev;
       
   189     // It is possible that the client set a NULL object to us.
       
   190     if ( !iDev )
       
   191         {
       
   192         iDev = CBTDevice::NewL();
       
   193         }
       
   194     // No optimization here. If client sets an identical device instance
       
   195     // We will still execute these steps:
       
   196     UpdateNameL();
       
   197     //UpdateServiceStatusL();
       
   198     }
       
   199 
       
   200 EXPORT_C CBtDevExtension* CBtDevExtension::CopyL()
       
   201     {
       
   202     CBtDevExtension* newDev = CBtDevExtension::NewLC( NULL );
       
   203     CBTDevice* dev = iDev->CopyL();
       
   204     CleanupStack::PushL( dev );
       
   205     newDev->SetDeviceL( dev );
       
   206     CleanupStack::Pop( dev);
       
   207     newDev->SetServiceConnectionStatus( ServiceConnectionStatus() );
       
   208     CleanupStack::Pop( newDev );
       
   209     return newDev;
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // ServiceConnectionStatus()
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 void CBtDevExtension::SetServiceConnectionStatus(
       
   217         TBTEngConnectionStatus aStatus )
       
   218     {
       
   219     iServiceStatus = aStatus;
       
   220     }
       
   221 
       
   222 
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // UpdateL()
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 void CBtDevExtension::UpdateNameL()
       
   229     {
       
   230     // UI takes friendly name for displaying if it is available
       
   231     iAlias.Zero();
       
   232     if ( iDev->IsValidFriendlyName() && iDev->FriendlyName().Length() != 0 )
       
   233         {
       
   234         iAlias.CreateL( iDev->FriendlyName() );
       
   235         }
       
   236     // otherwise, device name, if it is available, will be displayed
       
   237     else if ( iDev->IsValidDeviceName() && iDev->DeviceName().Length() != 0 )
       
   238         {
       
   239         iAlias.CreateL( BTDeviceNameConverter::ToUnicodeL(iDev->DeviceName() ) );
       
   240         }
       
   241     if ( iAlias.Length() == 0 && 
       
   242         ( iNameOption == EColonSeperatedBDAddr || iNameOption == EPlainBDAddr ) )
       
   243         {
       
   244         // Name for display is still missing. We need to make one for user to 
       
   245         // identify it.
       
   246         FormatAddressAsNameL();
       
   247         }
       
   248     }
       
   249 
       
   250 // ---------------------------------------------------------------------------
       
   251 // FormatAddressAsNameL()
       
   252 // ---------------------------------------------------------------------------
       
   253 //
       
   254 void CBtDevExtension::FormatAddressAsNameL()
       
   255     {
       
   256     // readable format of BD_ADDR is double size of BD ADDR size,
       
   257     // and plus the seperators.
       
   258     iAlias.ReAllocL( KBTDevAddrSize * 3 );
       
   259     _LIT(KColon, ":");
       
   260     if ( iNameOption == EColonSeperatedBDAddr )
       
   261         {
       
   262         iDev->BDAddr().GetReadable( iAlias, KNullDesC, KColon, KNullDesC );         
       
   263         }
       
   264     else
       
   265         {
       
   266         iDev->BDAddr().GetReadable( iAlias );
       
   267         }
       
   268     }