radioengine/utils/src/cradioaccessoryobserver.cpp
changeset 24 6df133bd92e1
child 28 075425b8d9a4
equal deleted inserted replaced
23:a2b50a479edf 24:6df133bd92e1
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *
       
    16 */
       
    17 
       
    18 // User includes
       
    19 #include "cradioaccessoryobserver.h"
       
    20 #include "mradioheadseteventobserver.h"
       
    21 #include "cradioenginelogger.h"
       
    22 
       
    23 // ---------------------------------------------------------------------------
       
    24 //
       
    25 // ---------------------------------------------------------------------------
       
    26 //
       
    27 CRadioAccessoryObserver::CRadioAccessoryObserver()
       
    28     : CActive( CActive::EPriorityStandard )
       
    29     {
       
    30     }
       
    31 
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 //
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 void CRadioAccessoryObserver::ConstructL()
       
    38     {
       
    39     LOG_METHOD_AUTO;
       
    40     User::LeaveIfError( iAccessoryServer.Connect() );
       
    41 
       
    42     // Creates a new sub-session within an existing session.
       
    43     User::LeaveIfError( iAccessoryConn.CreateSubSession( iAccessoryServer ) );
       
    44     User::LeaveIfError( iAccessoryConn.GetAccessoryConnectionStatus( iAccPolGenIdArr ) );
       
    45 
       
    46     TInt nroChangedAccessories( 0 );
       
    47     UpdateCurrentAccessoriesL( nroChangedAccessories );
       
    48     LOG_FORMAT( "CRadioAccessoryObserver::ConstructL() -- Found %d wired/BT accessories ( %d accessories in total ).",
       
    49               nroChangedAccessories, iAccPolGenIdArr.Count() );
       
    50     CActiveScheduler::Add( this );
       
    51     // Accessory is always listened
       
    52     iAccessoryConn.NotifyAccessoryConnectionStatusChanged( iStatus, iAccPolGenIdArr );
       
    53     SetActive();
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 CRadioAccessoryObserver* CRadioAccessoryObserver::NewL()
       
    61     {
       
    62     CRadioAccessoryObserver* self = new( ELeave ) CRadioAccessoryObserver;
       
    63     CleanupStack::PushL( self );
       
    64     self->ConstructL();
       
    65     CleanupStack::Pop( self );
       
    66     return self;
       
    67     }
       
    68 
       
    69 // ---------------------------------------------------------------------------
       
    70 //
       
    71 // ---------------------------------------------------------------------------
       
    72 //
       
    73 CRadioAccessoryObserver::~CRadioAccessoryObserver()
       
    74     {
       
    75     Cancel();
       
    76     iAccessoryConn.CloseSubSession();
       
    77     iAccessoryServer.Close();
       
    78     iSavedAccessories.Close();
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 TBool CRadioAccessoryObserver::FindAccessoryL( TBool aAcceptOnlyHeadset, TAccAudioOutPutType& aOutputType ) const
       
    86     {
       
    87 #ifdef __WINS__
       
    88     TBool accessoryFound = ETrue;
       
    89     aAcceptOnlyHeadset = ETrue;
       
    90     aOutputType = EAccAudioOutPutTypePrivate;
       
    91 #else
       
    92     TBool accessoryFound = EFalse;
       
    93 
       
    94     LOG_FORMAT( "CRadioAccessoryObserver::FindAccessoryL - Connected accessories count = %d", iAccPolGenIdArr.Count());
       
    95     for ( TInt i = 0; i < iAccPolGenIdArr.Count() && !accessoryFound; i++ )
       
    96         {
       
    97         TAccPolGenericID accPolGenId = iAccPolGenIdArr.GetGenericIDL( i );
       
    98 
       
    99         //Check if physical connection is of acceptable type
       
   100         TUint32 physicalConnectionCaps = accPolGenId.PhysicalConnectionCaps();
       
   101         if ( physicalConnectionCaps & KPCWired || physicalConnectionCaps & KPCBluetooth )
       
   102             {
       
   103             LOG_FORMAT( "CRadioAccessoryObserver::FindAccessoryL - is wired or BT ( physicalConnectionCaps = %d )", physicalConnectionCaps );
       
   104 
       
   105             if ( !aAcceptOnlyHeadset )
       
   106                 {
       
   107                 accessoryFound = ETrue;
       
   108                 }
       
   109             //Get device type ( check if is HeadSet )
       
   110             TUint32 deviceType = accPolGenId.DeviceTypeCaps();
       
   111             if ( deviceType & KDTHeadset )
       
   112                 {
       
   113                 LOG_FORMAT( "CRadioAccessoryObserver::FindAccessoryL - is HeadSet( deviceType = %d )", deviceType );
       
   114                 if ( !IsLineoutConnectedL( accPolGenId ) )
       
   115                     {
       
   116                     LOG( "CRadioAccessoryObserver::FindAccessoryL - Is a normal headSet" );
       
   117                     accessoryFound = ETrue;
       
   118                     }
       
   119                 }
       
   120             else if ( deviceType & KDTLoopset )
       
   121                 {
       
   122                 LOG_FORMAT( "CRadioAccessoryObserver::FindAccessoryL - is Loopset( deviceType = %d )", deviceType );
       
   123                 accessoryFound = ETrue;
       
   124                 }
       
   125             else
       
   126                 {
       
   127                 LOG_FORMAT( "CRadioAccessoryObserver::FindWiredAccessoryL - is NOT HeadSet or Loopset( deviceType = %d )", deviceType );
       
   128                 }
       
   129 
       
   130             if ( accessoryFound )
       
   131                 {
       
   132                 // get the public/private status
       
   133                 TAccPolNameRecord nameRecord;
       
   134                 nameRecord.SetNameL( KAccAudioOutputType );
       
   135 
       
   136                 TAccValueTypeTInt audioOutputTypeValue;
       
   137                 iAccessoryConn.GetValueL( accPolGenId, nameRecord, audioOutputTypeValue );
       
   138 
       
   139                 aOutputType = static_cast<TAccAudioOutPutType>( audioOutputTypeValue.iValue );
       
   140                 LOG_FORMAT( "CRadioAccessoryObserver::FindAccessoryL - AudioOutputType = %d", aOutputType );
       
   141                 }
       
   142             }
       
   143         else
       
   144             {
       
   145             LOG_FORMAT( "CRadioAccessoryObserver::FindAccessoryL - is NOT wired or BT ( physicalConnectionCaps = %d )", physicalConnectionCaps );
       
   146             }
       
   147         }
       
   148 #endif
       
   149     return accessoryFound;
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 //
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 TBool CRadioAccessoryObserver::IsHeadsetConnectedL() const
       
   157     {
       
   158     TAccAudioOutPutType audioOutputType( EAccAudioOutPutTypePrivate );
       
   159     return FindAccessoryL( ETrue , audioOutputType );
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 TBool CRadioAccessoryObserver::IsLineoutConnectedL( TAccPolGenericID& aAccPolGenId ) const
       
   167     {
       
   168     TBool isLineOut( EFalse );
       
   169 
       
   170     CAccPolSubblockNameArray* nameArray = CAccPolSubblockNameArray::NewL();
       
   171     CleanupStack::PushL( nameArray );
       
   172     iAccessoryConn.GetSubblockNameArrayL( aAccPolGenId, *nameArray );
       
   173 
       
   174     TBool isAudioOutValue = nameArray->HasName( KAccAudioOut );
       
   175     CleanupStack::PopAndDestroy( nameArray );
       
   176     if ( isAudioOutValue )
       
   177         {
       
   178         LOG_FORMAT( "CRadioAccessoryObserver::IsLineoutConnectedL - isAudioOutValue = ( %d )", isAudioOutValue );
       
   179 
       
   180         TAccPolNameRecord nameRecord;
       
   181         nameRecord.SetNameL( KAccAudioOut );
       
   182 
       
   183         //Check if connected accessory is LineOut
       
   184         TAccValueTypeTInt audioOutValue;
       
   185         iAccessoryConn.GetValueL( aAccPolGenId, nameRecord, audioOutValue );
       
   186 
       
   187         LOG( "CRadioAccessoryObserver::IsLineoutConnectedL - GetValueL() - OK" );
       
   188         if ( TAccAudioOutLineout == audioOutValue.iValue )
       
   189             {
       
   190             //is lineout
       
   191             LOG_FORMAT( "CRadioAccessoryObserver::IsLineoutConnectedL - Accessory is LineOut( audioOutValue = %d )", audioOutValue.iValue );
       
   192             isLineOut = ETrue;
       
   193             }
       
   194         }
       
   195     return isLineOut;
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CRadioAccessoryObserver::UpdateCurrentAccessoriesL( TInt& aNroChangedAccessories )
       
   203     {
       
   204     // first check, the removed accessories.
       
   205     TInt removedAccessories = CountRemovedAccessoriesL();
       
   206     LOG_FORMAT( "CRadioAccessoryObserver::UpdateCurrentAccessoriesL() -- %d accessories removed.", removedAccessories );
       
   207     aNroChangedAccessories += removedAccessories;
       
   208     TInt addedAccessories = CountAddedAccessoriesL();
       
   209     LOG_FORMAT( "CRadioAccessoryObserver::UpdateCurrentAccessoriesL() -- %d accessories added.", addedAccessories );
       
   210     aNroChangedAccessories += addedAccessories;
       
   211 
       
   212     iSavedAccessories.Reset();
       
   213 
       
   214     for ( TInt i = 0; i < iAccPolGenIdArr.Count(); i++ )
       
   215         {
       
   216         iSavedAccessories.AppendL( iAccPolGenIdArr.GetGenericIDL( i ) );
       
   217         LOG_FORMAT( "CRadioAccessoryObserver::UpdateCurrentAccessoriesL() -- accessory in slot %d: %d",
       
   218                   i, iAccPolGenIdArr.GetGenericIDL( i ).UniqueID() );
       
   219         }
       
   220     LOG_FORMAT( "CRadioAccessoryObserver::UpdateCurrentAccessoriesL() -- %d accessories in total.", iSavedAccessories.Count() );
       
   221     }
       
   222 
       
   223 // ---------------------------------------------------------------------------
       
   224 //
       
   225 // ---------------------------------------------------------------------------
       
   226 //
       
   227 TInt CRadioAccessoryObserver::CountRemovedAccessoriesL() const
       
   228     {
       
   229     TInt removedAccessories( 0 );
       
   230 
       
   231     for ( TInt i = 0; i < iSavedAccessories.Count(); i++ )
       
   232         {
       
   233         TAccPolGenericID savedAcc = iSavedAccessories[i];
       
   234 
       
   235         TUint32 physicalConnectionCaps = savedAcc.PhysicalConnectionCaps();
       
   236 
       
   237         if ( physicalConnectionCaps & KPCWired || physicalConnectionCaps & KPCBluetooth )
       
   238             {
       
   239             TBool found( EFalse );
       
   240 
       
   241             for ( TInt j = 0; j < iAccPolGenIdArr.Count() && !found; j++ )
       
   242                 {
       
   243                 TAccPolGenericID freshAcc = iAccPolGenIdArr.GetGenericIDL( j );
       
   244                 if ( freshAcc.UniqueID() == savedAcc.UniqueID() )
       
   245                     {
       
   246                     LOG_FORMAT( "CRadioAccessoryObserver::CountRemovedAccessoriesL() -- Accessory with id = %d found.", savedAcc.UniqueID() );
       
   247                     found = ETrue;
       
   248                     }
       
   249                 }
       
   250             if ( !found )
       
   251                 {
       
   252                 LOG_FORMAT( "CRadioAccessoryObserver::CountRemovedAccessoriesL() -- Accessory with id = %d removed.", savedAcc.UniqueID() );
       
   253                 removedAccessories++;
       
   254                 }
       
   255             }
       
   256         }
       
   257     return removedAccessories;
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 //
       
   262 // ---------------------------------------------------------------------------
       
   263 //
       
   264 TInt CRadioAccessoryObserver::CountAddedAccessoriesL() const
       
   265     {
       
   266     TInt addedAccessories( 0 );
       
   267 
       
   268     for ( TInt i = 0; i < iAccPolGenIdArr.Count(); i++ )
       
   269         {
       
   270         TAccPolGenericID freshAcc = iAccPolGenIdArr.GetGenericIDL( i );
       
   271 
       
   272         TUint32 physicalConnectionCaps = freshAcc.PhysicalConnectionCaps();
       
   273 
       
   274         if ( physicalConnectionCaps & KPCWired || physicalConnectionCaps & KPCBluetooth )
       
   275             {
       
   276             TBool found( EFalse );
       
   277 
       
   278             for ( TInt j = 0; j < iSavedAccessories.Count() && !found; j++ )
       
   279                 {
       
   280                 TAccPolGenericID savedAcc = iSavedAccessories[j];
       
   281                 if ( savedAcc.UniqueID() == freshAcc.UniqueID() )
       
   282                     {
       
   283                     LOG_FORMAT( "CRadioAccessoryObserver::CountAddedAccessoriesL() -- Accessory with id = %d found.", freshAcc.UniqueID() );
       
   284                     found = ETrue;
       
   285                     }
       
   286                 }
       
   287             if ( !found )
       
   288                 {
       
   289                 LOG_FORMAT( "CRadioAccessoryObserver::CountAddedAccessoriesL() -- Accessory with id = %d added.", freshAcc.UniqueID() );
       
   290                 addedAccessories++;
       
   291                 }
       
   292             }
       
   293         }
       
   294     return addedAccessories;
       
   295     }
       
   296 
       
   297 // ---------------------------------------------------------------------------
       
   298 //
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 void CRadioAccessoryObserver::SetObserver( MRadioHeadsetEventObserver* aObserver )
       
   302     {
       
   303     iObserver = aObserver;
       
   304     }
       
   305 
       
   306 // ---------------------------------------------------------------------------
       
   307 // From class CActive
       
   308 //
       
   309 // ---------------------------------------------------------------------------
       
   310 //
       
   311 void CRadioAccessoryObserver::RunL()
       
   312     {
       
   313     TRequestStatus status = iStatus;
       
   314     iAccessoryConn.NotifyAccessoryConnectionStatusChanged( iStatus, iAccPolGenIdArr );
       
   315 
       
   316     SetActive();
       
   317 
       
   318     TInt nroChangedAccessories( 0 );
       
   319     UpdateCurrentAccessoriesL( nroChangedAccessories );
       
   320 
       
   321     if ( status == KErrNone && nroChangedAccessories != 0 )
       
   322         {
       
   323         TAccAudioOutPutType audioOutputType( EAccAudioOutPutTypePrivate );
       
   324 
       
   325         if ( FindAccessoryL( EFalse, audioOutputType ) )
       
   326             {
       
   327             // Accessory mode may change when combined connection status changes
       
   328             // or when audio routing status changes.
       
   329             if ( audioOutputType == EAccAudioOutPutTypePrivate )
       
   330                 {
       
   331                 LOG( "CRadioAccessoryObserver::RunL - Headset connected" );
       
   332                 iObserver->HeadsetConnectedCallbackL();
       
   333                 }
       
   334             else
       
   335                 {
       
   336                 LOG( "CRadioAccessoryObserver::RunL - Public wired or BT accessory connected." );
       
   337                 iObserver->HeadsetDisconnectedCallbackL();
       
   338                 }
       
   339             }
       
   340         else
       
   341             {
       
   342             LOG( "CRadioAccessoryObserver::RunL - No wired or BT accessories available" );
       
   343             iObserver->HeadsetDisconnectedCallbackL();
       
   344             }
       
   345         }
       
   346     }
       
   347 
       
   348 
       
   349 // ---------------------------------------------------------------------------
       
   350 // From class CActive
       
   351 //
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 void CRadioAccessoryObserver::DoCancel()
       
   355     {
       
   356     iAccessoryConn.CancelNotifyAccessoryConnectionStatusChanged();
       
   357     }