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