resourcemgmt/hwresourcesmgr/test/plugins/fmtxwatcherplugin/src/hwrmfmtxaccobserver.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "hwrmfmtxaccobserver.h"
       
    20 #include "trace.h"
       
    21 
       
    22 // ============================ LOCAL FUNCTIONS ===============================
       
    23 // -----------------------------------------------------------------------------
       
    24 // AccessoryConnected()
       
    25 // -----------------------------------------------------------------------------
       
    26 //
       
    27 static TBool AccessoryConnected( TAccMode aAccMode )
       
    28     {
       
    29     return aAccMode != EAccModeHandPortable;
       
    30     }
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 // -----------------------------------------------------------------------------
       
    34 // CHWRMFmtxAccObserver::NewL()
       
    35 // Two-phased constructor.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CHWRMFmtxAccObserver* CHWRMFmtxAccObserver::NewL( MHWRMFmtxConnObserverCallback& aCallback )
       
    39     {
       
    40     FUNC_LOG;
       
    41     
       
    42     CHWRMFmtxAccObserver* self = new( ELeave ) CHWRMFmtxAccObserver( aCallback );
       
    43     CleanupStack::PushL( self );
       
    44     self->ConstructL();
       
    45     CleanupStack::Pop( self );
       
    46     return self;
       
    47     }
       
    48     
       
    49 // -----------------------------------------------------------------------------
       
    50 // CHWRMFmtxAccObserver::~CHWRMFmtxAccObserver()
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 CHWRMFmtxAccObserver::~CHWRMFmtxAccObserver()
       
    54     {
       
    55     FUNC_LOG;
       
    56     
       
    57     Cancel();
       
    58     iAccessoryMode.CloseSubSession();
       
    59     iServer.Close();
       
    60     }    
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CHWRMFmtxAccObserver::StartObservingL
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 TBool CHWRMFmtxAccObserver::GetStatusL()
       
    67     {
       
    68     FUNC_LOG;
       
    69       
       
    70     TAccPolAccessoryMode accMode;        
       
    71     TInt err( iAccessoryMode.GetAccessoryMode( accMode ) ); 
       
    72 
       
    73     LOG_IF_ERROR1( err, "CHWRMFmtxAccObserver::GetStatusL: err=%d", err );
       
    74     
       
    75     User::LeaveIfError( err );
       
    76     
       
    77     return AccessoryConnected( accMode.iAccessoryMode );
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CHWRMFmtxAccObserver::StartObservingL
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 void CHWRMFmtxAccObserver::StartObservingL()
       
    85     {
       
    86     FUNC_LOG;
       
    87     
       
    88     if( IsActive() )
       
    89         {
       
    90         ERROR_LOG( "CHWRMFmtxAccObserver::StartObservingL: already active" );
       
    91         }
       
    92     else
       
    93         {
       
    94         OrderAccessoryNotification();
       
    95         
       
    96         iConnected = GetStatusL(); // only connection change will be notified
       
    97         }        
       
    98     }
       
    99     
       
   100 // -----------------------------------------------------------------------------
       
   101 // CHWRMFmtxAccObserver::StopObserving
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CHWRMFmtxAccObserver::StopObserving()
       
   105     {
       
   106     FUNC_LOG;
       
   107     
       
   108     Cancel();
       
   109     }
       
   110 
       
   111 // -----------------------------------------------------------------------------
       
   112 // CHWRMFmtxAccObserver::CHWRMFmtxAccObserver()
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 CHWRMFmtxAccObserver::CHWRMFmtxAccObserver( MHWRMFmtxConnObserverCallback& aCallback ) :
       
   116     CActive( EPriorityStandard ),
       
   117     iCallback( aCallback )
       
   118     {
       
   119     FUNC_LOG;
       
   120     
       
   121     CActiveScheduler::Add( this );
       
   122     }
       
   123     
       
   124 // -----------------------------------------------------------------------------
       
   125 // CHWRMFmtxAccObserver::ConstructL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CHWRMFmtxAccObserver::ConstructL()
       
   129     {
       
   130     FUNC_LOG;
       
   131     
       
   132     // Connect to accessory server
       
   133     TInt err = iServer.Connect();
       
   134     
       
   135     LOG_IF_ERROR1( err, "CHWRMFmtxAccObserver::ConstructL: Connect err=%d", err );
       
   136 
       
   137     if ( err == KErrAlreadyExists )
       
   138         {
       
   139         err = KErrNone;
       
   140         }
       
   141     
       
   142     User::LeaveIfError( err );
       
   143     
       
   144     err = iAccessoryMode.CreateSubSession( iServer );
       
   145     
       
   146     LOG_IF_ERROR1( err, "CHWRMFmtxAccObserver::ConstructL: CreateSubSession err", err );
       
   147     
       
   148     User::LeaveIfError( err );
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CHWRMFmtxAccObserver::OrderAccessoryNotification()
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 void CHWRMFmtxAccObserver::OrderAccessoryNotification()
       
   156     {
       
   157     FUNC_LOG;
       
   158     
       
   159     iAccessoryMode.NotifyAccessoryModeChanged( iStatus, iAccMode );
       
   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     TBool connected( AccessoryConnected( iAccMode.iAccessoryMode ) );
       
   176     
       
   177     // prevent looping due to immediate completion
       
   178     if ( status != KErrCancel && status != KErrServerTerminated )
       
   179         {
       
   180         OrderAccessoryNotification();    
       
   181         }
       
   182     
       
   183     if ( status == KErrNone )
       
   184         {
       
   185         INFO_LOG2( "CHWRMFmtxAccObserver::RunL: connected=%d, iConnected=%d", connected, iConnected );
       
   186         
       
   187         if ( connected != iConnected )
       
   188             {
       
   189             iCallback.HandleConnectionChange( EFmtxWatcherObserverAccessory, connected );    
       
   190             iConnected = connected; // store the status to be used on next completion    
       
   191             }
       
   192         }
       
   193     }
       
   194     
       
   195 // -----------------------------------------------------------------------------
       
   196 // CHWRMFmtxAccObserver::DoCancel
       
   197 // ---------------------------------------------------------------------------
       
   198 //
       
   199 void CHWRMFmtxAccObserver::DoCancel()
       
   200     {
       
   201     FUNC_LOG;
       
   202 
       
   203     iAccessoryMode.CancelNotifyAccessoryModeChanged();
       
   204     }
       
   205 
       
   206 //  End of File