bluetoothengine/btserviceutil/src/btdevextension.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:  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 // NewLC
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C CBtDevExtension* CBtDevExtension::NewLC( 
       
    84         const TBTDevAddr& aAddr,
       
    85         const TDesC& aName,
       
    86         TDefaultDevNameOption aNameOption )
       
    87     {
       
    88     CBtDevExtension* self = new (ELeave) CBtDevExtension( aNameOption );
       
    89     CleanupStack::PushL( self );
       
    90     CBTDevice* dev = CBTDevice::NewLC( aAddr );
       
    91     if ( aName.Length() )
       
    92         {
       
    93         dev->SetDeviceNameL(  BTDeviceNameConverter::ToUTF8L( aName ) );
       
    94         }
       
    95     self->ConstructL( dev );
       
    96     CleanupStack::Pop( dev );
       
    97     return self;
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 // Destructor
       
   102 // ---------------------------------------------------------------------------
       
   103 //
       
   104 EXPORT_C CBtDevExtension::~CBtDevExtension()
       
   105     {
       
   106     iAlias.Close();
       
   107     delete iDev;
       
   108     }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // IsBonded
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 EXPORT_C TBool CBtDevExtension::IsBonded( const TBTNamelessDevice &dev )
       
   115     {
       
   116     // IsValidPaired tells if the paired bit of dev is valid
       
   117     // and IsPaired tells if the device is paired or not:
       
   118     return dev.IsValidPaired() && 
       
   119            dev.IsPaired() && 
       
   120            // Authentication due to OBEX cases e.g. file transfer, is not 
       
   121            // considered as bonded in Bluetooth UI:
       
   122            dev.LinkKeyType() != ELinkKeyUnauthenticatedUpgradable;
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // IsJustWorksBonded
       
   127 // ---------------------------------------------------------------------------
       
   128 //
       
   129 EXPORT_C TBool CBtDevExtension::IsJustWorksBonded( const TBTNamelessDevice &dev )
       
   130     {
       
   131     return IsBonded( dev ) && 
       
   132          dev.LinkKeyType() == ELinkKeyUnauthenticatedNonUpgradable;
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // IsUserAwareBonded
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 EXPORT_C TBool CBtDevExtension::IsUserAwareBonded( const TBTNamelessDevice &dev )
       
   140     {
       
   141     if ( IsJustWorksBonded( dev ) )
       
   142         {
       
   143         // Just Works bonded devices can happen without user awareness.
       
   144         // For example, authentication due to an incoming service connection request 
       
   145         // from a device without IO.
       
   146         // We use cookies to identify if this JW pairing is user aware or not:
       
   147         TInt32 cookie = dev.IsValidUiCookie() ? dev.UiCookie() : EBTUiCookieUndefined;
       
   148         return (cookie & EBTUiCookieJustWorksPaired );        
       
   149         }
       
   150     // Pairing in other mode than Just Works are always user-aware:
       
   151     return IsBonded( dev );
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------------------------
       
   155 // IsHeadset
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 EXPORT_C TBool CBtDevExtension::IsHeadset( const TBTDeviceClass &aCod )
       
   159     {
       
   160     if ( ( aCod.MajorServiceClass() & EMajorServiceAudioService ) &&
       
   161          ( aCod.MajorDeviceClass() & EMajorDeviceAudioDevice ) )
       
   162         {
       
   163         if (aCod.MinorDeviceClass() == EMinorDeviceAVHandsfree ||
       
   164             aCod.MinorDeviceClass() == EMinorDeviceAVCarAudio)
       
   165             {
       
   166             // This is the typical CoD setting used by carkits:
       
   167             return false;
       
   168             }
       
   169         return true;
       
   170         }
       
   171     return false;
       
   172     }
       
   173 
       
   174 // ---------------------------------------------------------------------------
       
   175 // 
       
   176 // ---------------------------------------------------------------------------
       
   177 //
       
   178 EXPORT_C const TDesC& CBtDevExtension::Alias() const
       
   179     {
       
   180     return iAlias;
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------------------------
       
   184 // Addr()
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 EXPORT_C const TBTDevAddr& CBtDevExtension::Addr() const
       
   188     {
       
   189     return iDev->BDAddr();
       
   190     }
       
   191 
       
   192 // ---------------------------------------------------------------------------
       
   193 // Device
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 EXPORT_C const CBTDevice& CBtDevExtension::Device() const
       
   197     {
       
   198     return *iDev;
       
   199     }
       
   200 
       
   201 
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // IsUserAwareBonded
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 EXPORT_C TBool CBtDevExtension::IsUserAwareBonded() const
       
   208     {
       
   209     return IsUserAwareBonded( iDev->AsNamelessDevice() );
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // ServiceConnectionStatus()
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 EXPORT_C TBTEngConnectionStatus CBtDevExtension::ServiceConnectionStatus() const
       
   217     {
       
   218     return iServiceStatus;
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // SetDeviceL
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 EXPORT_C void CBtDevExtension::SetDeviceL( CBTDevice* aDev )
       
   226     {
       
   227     delete iDev;
       
   228     iDev = aDev;
       
   229     // It is possible that the client set a NULL object to us.
       
   230     if ( !iDev )
       
   231         {
       
   232         iDev = CBTDevice::NewL();
       
   233         }
       
   234     // No optimization here. If client sets an identical device instance
       
   235     // We will still execute these steps:
       
   236     UpdateNameL();
       
   237     //UpdateServiceStatusL();
       
   238     }
       
   239 
       
   240 EXPORT_C CBtDevExtension* CBtDevExtension::CopyL()
       
   241     {
       
   242     CBtDevExtension* newDev = CBtDevExtension::NewLC( NULL );
       
   243     CBTDevice* dev = iDev->CopyL();
       
   244     CleanupStack::PushL( dev );
       
   245     newDev->SetDeviceL( dev );
       
   246     CleanupStack::Pop( dev);
       
   247     newDev->SetServiceConnectionStatus( ServiceConnectionStatus() );
       
   248     CleanupStack::Pop( newDev );
       
   249     return newDev;
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // ServiceConnectionStatus()
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 void CBtDevExtension::SetServiceConnectionStatus(
       
   257         TBTEngConnectionStatus aStatus )
       
   258     {
       
   259     iServiceStatus = aStatus;
       
   260     }
       
   261 
       
   262 // ---------------------------------------------------------------------------
       
   263 // UpdateL()
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 void CBtDevExtension::UpdateNameL()
       
   267     {
       
   268     // UI takes friendly name for displaying if it is available
       
   269     iAlias.Zero();
       
   270     if ( iDev->IsValidFriendlyName() && iDev->FriendlyName().Length() != 0 )
       
   271         {
       
   272         iAlias.ReAllocL(iDev->FriendlyName().Length());
       
   273         iAlias.Append(iDev->FriendlyName());
       
   274         }
       
   275     // otherwise, device name, if it is available, will be displayed
       
   276     else if ( iDev->IsValidDeviceName() && iDev->DeviceName().Length() != 0 )
       
   277         {
       
   278         TBTDeviceName name = BTDeviceNameConverter::ToUnicodeL(iDev->DeviceName());
       
   279         iAlias.ReAllocL(name.Length());
       
   280         iAlias.Append(name);
       
   281         }
       
   282     if ( iAlias.Length() == 0 && 
       
   283         ( iNameOption == EColonSeperatedBDAddr || iNameOption == EPlainBDAddr ) )
       
   284         {
       
   285         // Name for display is still missing. We need to make one for user to 
       
   286         // identify it.
       
   287         FormatAddressAsNameL();
       
   288         }
       
   289     }
       
   290 
       
   291 // ---------------------------------------------------------------------------
       
   292 // FormatAddressAsNameL()
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 void CBtDevExtension::FormatAddressAsNameL()
       
   296     {
       
   297     // readable format of BD_ADDR is double size of BD ADDR size,
       
   298     // and plus the seperators.
       
   299     iAlias.ReAllocL( KBTDevAddrSize * 3 );
       
   300     _LIT(KColon, ":");
       
   301     if ( iNameOption == EColonSeperatedBDAddr )
       
   302         {
       
   303         iDev->BDAddr().GetReadable( iAlias, KNullDesC, KColon, KNullDesC );         
       
   304         }
       
   305     else
       
   306         {
       
   307         iDev->BDAddr().GetReadable( iAlias );
       
   308         }
       
   309     }