usbengines/usbwatcher/src/cusbactivestate.cpp
changeset 35 9d8b04ca6939
child 38 218231f2b3b3
equal deleted inserted replaced
34:7858bc6ead78 35:9d8b04ca6939
       
     1 /*
       
     2 * Copyright (c) 2002-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:  This implements CUsbActiveState class.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <usbman.h>
       
    21 #include "debug.h"
       
    22 #include "cusbactivestate.h"
       
    23 #include "cusbwatcher.h"
       
    24 
       
    25 // CONSTANTS
       
    26 const TUint KUsbAllStates = 0xFFFFFFFF;
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ==============================
       
    29 
       
    30 // ----------------------------------------------------------------------------
       
    31 // C++ default constructor can NOT contain any code, that might leave.
       
    32 // ----------------------------------------------------------------------------
       
    33 //
       
    34 CUsbActiveState::CUsbActiveState( RUsb& aUsbMan, CUsbWatcher& aOwner )
       
    35     : CActive( EPriorityStandard )
       
    36     , iUsbMan( aUsbMan )
       
    37     , iOwner( aOwner )
       
    38     , iPreviousState( EUsbDeviceStateUndefined )
       
    39     {
       
    40     LOG_FUNC
       
    41 
       
    42     CActiveScheduler::Add( this );
       
    43     }
       
    44 
       
    45 // ----------------------------------------------------------------------------
       
    46 // Symbian 2nd phase constructor can leave.
       
    47 // ----------------------------------------------------------------------------
       
    48 //
       
    49 void CUsbActiveState::ConstructL()
       
    50     {
       
    51     LOG_FUNC
       
    52 
       
    53     LEAVEIFERROR( iUsbMan.GetDeviceState( iCurrentState ) );
       
    54     LOG1( "Intial UsbDeviceState = %d" , iCurrentState );
       
    55         
       
    56     // start USB if cable is pluged-in at bootup
       
    57     if( EUsbDeviceStateUndefined != iCurrentState )
       
    58         {
       
    59         iOwner.StateChangeNotify( iPreviousState, iCurrentState );
       
    60         iPreviousState = iCurrentState;
       
    61         }
       
    62     iUsbMan.DeviceStateNotification( KUsbAllStates, iCurrentState, iStatus );
       
    63     SetActive();
       
    64     }
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // Two-phased constructor.
       
    68 // ----------------------------------------------------------------------------
       
    69 //
       
    70 CUsbActiveState* CUsbActiveState::NewL( RUsb& aUsbMan, CUsbWatcher& aOwner )
       
    71     {
       
    72     LOG_FUNC
       
    73 
       
    74     CUsbActiveState* self = new ( ELeave ) CUsbActiveState( aUsbMan, aOwner );
       
    75     CleanupStack::PushL( self );
       
    76     self->ConstructL();
       
    77     CleanupStack::Pop(); // pop self
       
    78     return self;
       
    79     }
       
    80 
       
    81 // Destructor
       
    82 CUsbActiveState::~CUsbActiveState()
       
    83     {
       
    84     LOG_FUNC
       
    85 
       
    86     Cancel();
       
    87     }
       
    88 
       
    89 // ----------------------------------------------------------------------------
       
    90 // This function is called when device state is changed.
       
    91 // ----------------------------------------------------------------------------
       
    92 //
       
    93 void CUsbActiveState::RunL()
       
    94     {
       
    95     LOG_FUNC
       
    96 
       
    97     LEAVEIFERROR( iStatus.Int() ); // Close process if error happens here
       
    98 
       
    99     TUsbDeviceState newState = iCurrentState;
       
   100     iUsbMan.DeviceStateNotification( KUsbAllStates, iCurrentState,
       
   101             iStatus );
       
   102     SetActive();
       
   103     
       
   104     // Notify only if there is a change
       
   105     if ( newState != iPreviousState )
       
   106         {
       
   107         LOG2( "USB device state changed: %d ==> %d", iPreviousState,
       
   108             newState );
       
   109         iOwner.StateChangeNotify( iPreviousState, newState );
       
   110         iPreviousState = newState;
       
   111         }
       
   112      else
       
   113         {
       
   114         LOG2("USB device change ignored: %d -> %d", iPreviousState,
       
   115             newState ); 
       
   116         }
       
   117     }
       
   118  
       
   119 // ----------------------------------------------------------------------------
       
   120 // Standard active object cancellation function.
       
   121 // ----------------------------------------------------------------------------
       
   122 //
       
   123 void CUsbActiveState::DoCancel()
       
   124     {
       
   125     LOG_FUNC
       
   126 
       
   127     iUsbMan.DeviceStateNotificationCancel();
       
   128     }
       
   129 
       
   130 // ----------------------------------------------------------------------------
       
   131 // Get current device state.
       
   132 // ----------------------------------------------------------------------------
       
   133 //
       
   134 TUsbDeviceState CUsbActiveState::CurrentState()
       
   135     {
       
   136     LOG_FUNC
       
   137 
       
   138     return iCurrentState;
       
   139     }
       
   140 
       
   141 // ----------------------------------------------------------------------------
       
   142 // Get previous device state.
       
   143 // ----------------------------------------------------------------------------
       
   144 //
       
   145 TUsbDeviceState CUsbActiveState::PreviousState()
       
   146     {
       
   147     LOG_FUNC
       
   148 
       
   149     return iPreviousState;
       
   150     }
       
   151 	
       
   152 // End of file