resourcemgmt/hwrmfmtxwatcherplugin/src/hwrmfmtxusbobserver.cpp
changeset 0 4e1aa6a622a0
child 10 1fc153c72b60
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:  Usb observer implementation for fmtx watcher plugin.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "hwrmfmtxusbobserver.h"
       
    20 #include "trace.h"
       
    21 
       
    22 // bitmask for requesting notification of every USB device state change
       
    23 const TUint KUsbAllStates = 0xFFFFFFFF;
       
    24 
       
    25 // ============================ LOCAL FUNCTIONS ===============================
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // UsbConnected()
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 static TBool UsbConnected( TUsbDeviceState aDeviceState )
       
    32     {
       
    33     INFO_LOG1( "UsbConnected - %d",  aDeviceState);
       
    34 
       
    35     if ( aDeviceState == EUsbDeviceStateAttached || aDeviceState == EUsbDeviceStatePowered ||
       
    36     		aDeviceState == EUsbDeviceStateUndefined)
       
    37     	{
       
    38     	RDebug::Print(_L("return false "));
       
    39     	return EFalse;
       
    40     	}
       
    41     else
       
    42     	{
       
    43     	RDebug::Print(_L("return true "));
       
    44     	return ETrue;
       
    45 		}
       
    46     }
       
    47 
       
    48 // ============================ MEMBER FUNCTIONS ===============================
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // CHWRMFmtxUsbObserver::NewL()
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CHWRMFmtxUsbObserver* CHWRMFmtxUsbObserver::NewL( MHWRMFmtxConnObserverCallback& aObserver )
       
    55     {
       
    56     FUNC_LOG;
       
    57     
       
    58     CHWRMFmtxUsbObserver* self = new( ELeave ) CHWRMFmtxUsbObserver( aObserver );
       
    59     CleanupStack::PushL( self );
       
    60     self->ConstructL();
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CHWRMFmtxUsbObserver::~CHWRMFmtxUsbObserver()
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CHWRMFmtxUsbObserver::~CHWRMFmtxUsbObserver()
       
    70     {
       
    71     FUNC_LOG;
       
    72     
       
    73     Cancel();
       
    74     iUsbMan.Close();
       
    75     }
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // CHWRMFmtxUsbObserver::GetStatusL
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 TBool CHWRMFmtxUsbObserver::GetStatusL()
       
    82     {
       
    83     FUNC_LOG;
       
    84     
       
    85     TUsbDeviceState deviceState;
       
    86     TInt err( iUsbMan.GetDeviceState( deviceState ) );
       
    87     
       
    88     LOG_IF_ERROR2( err, "CHWRMFmtxUsbObserver::StartObservingL - err %d, state %d", err, deviceState );
       
    89 
       
    90     User::LeaveIfError( err );    	
       
    91     
       
    92     return UsbConnected( deviceState );
       
    93     }
       
    94     
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CHWRMFmtxUsbObserver::StartObservingL
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CHWRMFmtxUsbObserver::StartObservingL()
       
   101     {
       
   102     FUNC_LOG;
       
   103     
       
   104     if( !IsActive() )
       
   105         {
       
   106         OrderUsbNotification();
       
   107         
       
   108         // check the initial status, so that only state change is notified
       
   109         iConnected = GetStatusL();
       
   110         }
       
   111     }
       
   112     
       
   113 // -----------------------------------------------------------------------------
       
   114 // CHWRMFmtxUsbObserver::StopObserving
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 void CHWRMFmtxUsbObserver::StopObserving()
       
   118     {
       
   119     FUNC_LOG;
       
   120     
       
   121     Cancel();
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CHWRMFmtxUsbObserver::CHWRMFmtxUsbObserver()
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 CHWRMFmtxUsbObserver::CHWRMFmtxUsbObserver( MHWRMFmtxConnObserverCallback& aObserver ) :
       
   129     CActive( EPriorityStandard ),
       
   130     iDeviceState( EUsbDeviceStateUndefined ),
       
   131     iCallback( aObserver ),
       
   132     iConnected( ETrue )
       
   133     {
       
   134     FUNC_LOG;
       
   135     
       
   136     CActiveScheduler::Add( this );
       
   137     }
       
   138             
       
   139 // -----------------------------------------------------------------------------
       
   140 // CHWRMFmtxUsbObserver::ConstructL
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 void CHWRMFmtxUsbObserver::ConstructL()
       
   144     {
       
   145     FUNC_LOG;
       
   146     
       
   147     // Connect to usbman
       
   148 	TInt err = iUsbMan.Connect();	
       
   149     
       
   150     LOG_IF_ERROR1( err, "CHWRMFmtxUsbObserver::ConstructL - err %d", err );
       
   151     
       
   152     User::LeaveIfError( err );
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CHWRMFmtxUsbObserver::OrderUsbNotification()
       
   157 // -----------------------------------------------------------------------------
       
   158 //
       
   159 void CHWRMFmtxUsbObserver::OrderUsbNotification()
       
   160     {
       
   161     FUNC_LOG;
       
   162     
       
   163     iUsbMan.DeviceStateNotification( KUsbAllStates, iDeviceState, iStatus );
       
   164     SetActive();
       
   165     }
       
   166 
       
   167 // -----------------------------------------------------------------------------
       
   168 // CHWRMFmtxUsbObserver::RunL()
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 void CHWRMFmtxUsbObserver::RunL( )
       
   172     {
       
   173     FUNC_LOG;
       
   174     
       
   175     TInt status(iStatus.Int());
       
   176     
       
   177     LOG_IF_ERROR1( status, "CHWRMFmtxUsbObserver::RunL error, status=%d", status );
       
   178 
       
   179     TBool connected(UsbConnected( iDeviceState )); // store state before subscribing again
       
   180     
       
   181     // prevent looping due to immediate completion
       
   182     if ( status != KErrCancel && status != KErrServerTerminated )
       
   183         {
       
   184         OrderUsbNotification();    
       
   185         }
       
   186     
       
   187     if ( status == KErrNone )
       
   188         {
       
   189         INFO_LOG2( "CHWRMFmtxUsbObserver::RunL: connected=%d, iConnected=%d", connected, iConnected );
       
   190         if ( connected != iConnected ) // notify only if state has changed
       
   191             {
       
   192             iConnected = connected;    
       
   193             iCallback.HandleConnectionChange( EFmtxWatcherObserverUsb, iConnected );
       
   194             }    
       
   195         }
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CHWRMFmtxUsbObserver::DoCancel
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 void CHWRMFmtxUsbObserver::DoCancel()
       
   203     {
       
   204     FUNC_LOG;
       
   205 
       
   206     iUsbMan.DeviceStateNotificationCancel();
       
   207     }
       
   208 
       
   209 //  End of File