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