resourcemgmt/hwrmfmtxwatcherplugin/src/hwrmfmtxaccobserver.cpp
changeset 0 4e1aa6a622a0
child 76 cb32bcc88bad
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Accessory observer implementation for fmtx watcher plugin.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <accessoryservicesinternalpskeys.h>
       
    21 #include "hwrmfmtxaccobserver.h"
       
    22 #include "trace.h"
       
    23 
       
    24 // ============================ LOCAL FUNCTIONS ===============================
       
    25 // -----------------------------------------------------------------------------
       
    26 // AccessoryConnected()
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 static TBool AccessoryConnected( TInt aWiredConnStatus )
       
    30     {
       
    31     TBool wiredConnected( ETrue );
       
    32     
       
    33     if( aWiredConnStatus == EAccSrvcsWiredConnUninitialized ||
       
    34         aWiredConnStatus == EAccSrvcsWiredConnDisconnected )
       
    35         {
       
    36         wiredConnected = EFalse;
       
    37         }
       
    38     
       
    39     return wiredConnected;
       
    40     }
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 // -----------------------------------------------------------------------------
       
    44 // CHWRMFmtxAccObserver::NewL()
       
    45 // Two-phased constructor.
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CHWRMFmtxAccObserver* CHWRMFmtxAccObserver::NewL( MHWRMFmtxConnObserverCallback& aCallback )
       
    49     {
       
    50     FUNC_LOG;
       
    51     
       
    52     CHWRMFmtxAccObserver* self = new( ELeave ) CHWRMFmtxAccObserver( aCallback );
       
    53     CleanupStack::PushL( self );
       
    54     self->ConstructL();
       
    55     CleanupStack::Pop( self );
       
    56     return self;
       
    57     }
       
    58     
       
    59 // -----------------------------------------------------------------------------
       
    60 // CHWRMFmtxAccObserver::~CHWRMFmtxAccObserver()
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 CHWRMFmtxAccObserver::~CHWRMFmtxAccObserver()
       
    64     {
       
    65     FUNC_LOG;
       
    66     iProperty.Cancel();
       
    67     Cancel();
       
    68     iProperty.Close();
       
    69     }    
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CHWRMFmtxAccObserver::StartObservingL
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 TBool CHWRMFmtxAccObserver::GetStatusL()
       
    76     {
       
    77     FUNC_LOG;
       
    78       
       
    79     TInt wiredConnStatus( EAccSrvcsWiredConnUninitialized );        
       
    80     TInt err( iProperty.Get( KPSUidAccessoryServices,
       
    81                              KAccSrvcsWiredConnection,
       
    82                              wiredConnStatus ) ); 
       
    83 
       
    84     LOG_IF_ERROR1( err, "CHWRMFmtxAccObserver::GetStatusL: err=%d", err );
       
    85     
       
    86     User::LeaveIfError( err );
       
    87     
       
    88     return AccessoryConnected( wiredConnStatus );
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CHWRMFmtxAccObserver::StartObservingL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 void CHWRMFmtxAccObserver::StartObservingL()
       
    96     {
       
    97     FUNC_LOG;
       
    98     
       
    99     if( IsActive() )
       
   100         {
       
   101         ERROR_LOG( "CHWRMFmtxAccObserver::StartObservingL: already active" );
       
   102         }
       
   103     else
       
   104         {
       
   105         OrderAccessoryNotification();
       
   106         
       
   107         iConnected = GetStatusL(); // only connection change will be notified
       
   108         }        
       
   109     }
       
   110     
       
   111 // -----------------------------------------------------------------------------
       
   112 // CHWRMFmtxAccObserver::StopObserving
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 void CHWRMFmtxAccObserver::StopObserving()
       
   116     {
       
   117     FUNC_LOG;
       
   118     
       
   119     Cancel();
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CHWRMFmtxAccObserver::CHWRMFmtxAccObserver()
       
   124 // -----------------------------------------------------------------------------
       
   125 //
       
   126 CHWRMFmtxAccObserver::CHWRMFmtxAccObserver( MHWRMFmtxConnObserverCallback& aCallback ) :
       
   127     CActive( EPriorityStandard ),
       
   128     iCallback( aCallback )
       
   129     {
       
   130     FUNC_LOG;
       
   131     
       
   132     CActiveScheduler::Add( this );
       
   133     }
       
   134     
       
   135 // -----------------------------------------------------------------------------
       
   136 // CHWRMFmtxAccObserver::ConstructL
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CHWRMFmtxAccObserver::ConstructL()
       
   140     {
       
   141     FUNC_LOG;
       
   142     
       
   143     // Connect to accessory server
       
   144     TInt err = iProperty.Attach( KPSUidAccessoryServices, KAccSrvcsWiredConnection );
       
   145     
       
   146     LOG_IF_ERROR1( err, "CHWRMFmtxAccObserver::ConstructL: RProperty::Attach err=%d", err );
       
   147 
       
   148     User::LeaveIfError( err );
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CHWRMFmtxAccObserver::OrderAccessoryNotification()
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CHWRMFmtxAccObserver::OrderAccessoryNotification()
       
   156     {
       
   157     FUNC_LOG;
       
   158     
       
   159     iProperty.Subscribe( iStatus );
       
   160     SetActive();
       
   161     }
       
   162     
       
   163 // -----------------------------------------------------------------------------
       
   164 // CHWRMFmtxAccObserver::RunL()
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 void CHWRMFmtxAccObserver::RunL()
       
   168     {
       
   169     FUNC_LOG;
       
   170     
       
   171     TInt status( iStatus.Int() );
       
   172     
       
   173     LOG_IF_ERROR1( status, "CHWRMFmtxAccObserver::RunL: error, status=%d", status );
       
   174     
       
   175     // prevent looping due to immediate completion
       
   176     if ( status != KErrCancel )
       
   177         {
       
   178         OrderAccessoryNotification();    
       
   179         }
       
   180     
       
   181     TInt wiredConnStatus( EAccSrvcsWiredConnUninitialized );
       
   182     TInt err = iProperty.Get( wiredConnStatus );
       
   183     TBool connected( AccessoryConnected( wiredConnStatus ) );
       
   184     
       
   185     INFO_LOG2( "CHWRMFmtxAccObserver::RunL: err=%d, wiredConnStatus=%d", err, wiredConnStatus );
       
   186     
       
   187     if ( err == KErrNotFound || err == KErrNone )
       
   188         {
       
   189         INFO_LOG2( "CHWRMFmtxAccObserver::RunL: connected=%d, iConnected=%d", connected, iConnected );
       
   190         
       
   191         if ( connected != iConnected )
       
   192             {
       
   193             iCallback.HandleConnectionChange( EFmtxWatcherObserverAccessory, connected );    
       
   194             iConnected = connected; // store the status to be used on next completion    
       
   195             }
       
   196         }
       
   197     }
       
   198     
       
   199 // -----------------------------------------------------------------------------
       
   200 // CHWRMFmtxAccObserver::DoCancel
       
   201 // ---------------------------------------------------------------------------
       
   202 //
       
   203 void CHWRMFmtxAccObserver::DoCancel()
       
   204     {
       
   205     FUNC_LOG;
       
   206 
       
   207     iProperty.Cancel();
       
   208     }
       
   209 
       
   210 //  End of File