usbengines/usbwatcher/src/cusbglobalsystemstateobserver.cpp
changeset 0 1e05558e2206
equal deleted inserted replaced
-1:000000000000 0:1e05558e2206
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Subscribe to global system state events
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <startupdomainpskeys.h> //for global system state
       
    20 #include "cusbglobalsystemstateobserver.h"
       
    21 #include "cusbwatcher.h"
       
    22 
       
    23 // ----------------------------------------------------------------------------
       
    24 // Two-phase constructor
       
    25 // ----------------------------------------------------------------------------
       
    26 //
       
    27 CUsbGlobalSystemStateObserver* CUsbGlobalSystemStateObserver::NewL(
       
    28         CUsbWatcher& aUsbWatcher )
       
    29     {
       
    30     LOG_FUNC
       
    31 
       
    32     CUsbGlobalSystemStateObserver* me = CUsbGlobalSystemStateObserver::NewLC(
       
    33             aUsbWatcher );
       
    34     CleanupStack::Pop();
       
    35     return me;
       
    36     }
       
    37 
       
    38 // ----------------------------------------------------------------------------
       
    39 // Two-phase constructor, instance put in cleanupstack
       
    40 // ----------------------------------------------------------------------------
       
    41 //
       
    42 CUsbGlobalSystemStateObserver* CUsbGlobalSystemStateObserver::NewLC(
       
    43         CUsbWatcher& aUsbWatcher )
       
    44     {
       
    45     LOG_FUNC
       
    46 
       
    47     CUsbGlobalSystemStateObserver* me = new ( ELeave )
       
    48             CUsbGlobalSystemStateObserver( aUsbWatcher );
       
    49     CleanupStack::PushL( me );
       
    50     me->ConstructL();
       
    51     return me;
       
    52     }
       
    53 
       
    54 // ----------------------------------------------------------------------------
       
    55 // 2nd Phase Construction
       
    56 // ----------------------------------------------------------------------------
       
    57 //
       
    58 void CUsbGlobalSystemStateObserver::ConstructL()
       
    59     {
       
    60     LOG_FUNC
       
    61 
       
    62     User::LeaveIfError( iProperty.Attach( KPSUidStartup,
       
    63             KPSGlobalSystemState ) );
       
    64     }
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // C++ Constructor
       
    68 // ----------------------------------------------------------------------------
       
    69 //
       
    70 CUsbGlobalSystemStateObserver::CUsbGlobalSystemStateObserver(
       
    71         CUsbWatcher& aUsbWatcher )
       
    72     : CActive( EPriorityNormal )
       
    73     , iUsbWatcher( aUsbWatcher )
       
    74     {
       
    75     LOG_FUNC
       
    76 
       
    77     CActiveScheduler::Add( this );
       
    78     }
       
    79 
       
    80 // ----------------------------------------------------------------------------
       
    81 // Destructor
       
    82 // ----------------------------------------------------------------------------
       
    83 //
       
    84 CUsbGlobalSystemStateObserver::~CUsbGlobalSystemStateObserver()
       
    85     {
       
    86     LOG_FUNC
       
    87 
       
    88     Cancel();
       
    89     iProperty.Close();
       
    90     }
       
    91 
       
    92 // ----------------------------------------------------------------------------
       
    93 // Return global system state
       
    94 // ----------------------------------------------------------------------------
       
    95 //
       
    96 TInt CUsbGlobalSystemStateObserver::GlobalSystemState()
       
    97     {
       
    98     LOG_FUNC
       
    99 
       
   100     TInt state;
       
   101     RProperty::Get( KPSUidStartup, KPSGlobalSystemState, state );
       
   102     return state;
       
   103     }
       
   104 
       
   105 // ----------------------------------------------------------------------------
       
   106 // Start observing the state P&S key
       
   107 // ----------------------------------------------------------------------------
       
   108 //
       
   109 void CUsbGlobalSystemStateObserver::Subscribe()
       
   110     {
       
   111     LOG_FUNC
       
   112 
       
   113     if( IsActive() )
       
   114         {
       
   115         LOG( "Already observing" );
       
   116         return; // already observing
       
   117         }
       
   118 
       
   119     iProperty.Subscribe( iStatus );
       
   120     SetActive();
       
   121     }
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // Stop observing the state P&S key
       
   125 // ----------------------------------------------------------------------------
       
   126 //
       
   127 void CUsbGlobalSystemStateObserver::DoCancel()
       
   128     {
       
   129     LOG_FUNC
       
   130 
       
   131     iProperty.Cancel();
       
   132     }
       
   133 
       
   134 // ----------------------------------------------------------------------------
       
   135 // Called when the state changes
       
   136 // ----------------------------------------------------------------------------
       
   137 //
       
   138 void CUsbGlobalSystemStateObserver::RunL()
       
   139 {
       
   140     LOG_FUNC
       
   141     LOG1( "iStatus = %d", iStatus.Int() );
       
   142 
       
   143     Subscribe();
       
   144     TInt systemState = 0;
       
   145     User::LeaveIfError( iProperty.Get( systemState ) );
       
   146     LOG1( "SystemState= %d", systemState );
       
   147 
       
   148     if (  ( systemState == ESwStateChargingToNormal )
       
   149        || ( systemState == ESwStateAlarmToNormal )  )
       
   150         {
       
   151         iUsbWatcher.StopPersonality();
       
   152         }
       
   153     else if ( systemState == ESwStateCharging )
       
   154         {
       
   155         //USBWatcher started before charging state
       
   156         iUsbWatcher.StartPersonality();
       
   157         }
       
   158     else if (
       
   159         (systemState == ESwStateNormalRfOn) ||
       
   160         (systemState == ESwStateNormalRfOff) ||
       
   161         (systemState == ESwStateNormalBTSap) )
       
   162         {
       
   163         //after ESwStateChargingToNormal
       
   164         Cancel();
       
   165         iUsbWatcher.StartPersonality();
       
   166         }
       
   167 }
       
   168 
       
   169 // ----------------------------------------------------------------------------
       
   170 // Handle RunL leaving
       
   171 // ----------------------------------------------------------------------------
       
   172 //
       
   173 TInt CUsbGlobalSystemStateObserver::RunError( TInt aError )
       
   174     {
       
   175     LOG_FUNC
       
   176 
       
   177     LOG1( "aError = %d", aError );
       
   178     return ( KErrNone );
       
   179     }
       
   180 
       
   181 // End of file