resourcemgmt/hwresourcesmgr/extendedlight/src/HWRMExtendedLightStatusObserver.cpp
changeset 0 4e1aa6a622a0
child 20 1ddbe54d0645
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2006-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 
       
    19 // INCLUDE FILES
       
    20 #include "HWRMPrivatePSKeys.h"
       
    21 #include "HWRMtrace.h"
       
    22 #include "HWRMExtendedLight.h"
       
    23 #include "HWRMExtendedLightStatusObserver.h"
       
    24 #include "HWRMLightClientServer.h"  // for KHWRMLightMaxTargets
       
    25 
       
    26 // CONSTANTS
       
    27 
       
    28 // ============================ MEMBER FUNCTIONS ===============================
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CHWRMExtendedLightStatusObserver::CHWRMExtendedLightStatusObserver
       
    32 // C++ constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CHWRMExtendedLightStatusObserver::CHWRMExtendedLightStatusObserver(MHWRMExtendedLightObserver* aCallback,
       
    37                                                                    TInt aSupportedTargets)
       
    38     : CActive(EPriorityStandard), 
       
    39       iCallback(aCallback),
       
    40       iSupportedTargets(aSupportedTargets)  
       
    41     {
       
    42     COMPONENT_TRACE2(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::CHWRMExtendedLightStatusObserver(0x%x)" ), aCallback);
       
    43 
       
    44     CActiveScheduler::Add(this);
       
    45 
       
    46     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::CHWRMExtendedLightStatusObserver - return" ) );
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CHWRMExtendedLightStatusObserver::NewL
       
    51 // Two-phased constructor 
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CHWRMExtendedLightStatusObserver* CHWRMExtendedLightStatusObserver::NewL(MHWRMExtendedLightObserver* aCallback,
       
    55                                                                          TInt aSupportedTargets)
       
    56     {
       
    57 
       
    58     COMPONENT_TRACE2(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::NewL(0x%x)" ), aCallback);
       
    59 
       
    60 	CHWRMExtendedLightStatusObserver* newInstance = new (ELeave) CHWRMExtendedLightStatusObserver(aCallback, aSupportedTargets);
       
    61 
       
    62 	CleanupStack::PushL( newInstance );
       
    63 
       
    64 	newInstance->ConstructL();
       
    65 
       
    66 	CleanupStack::Pop();
       
    67 
       
    68     COMPONENT_TRACE2(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::NewL - return 0x%x" ), newInstance );
       
    69 
       
    70 	return newInstance;
       
    71     }
       
    72 
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // Destructor
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CHWRMExtendedLightStatusObserver::~CHWRMExtendedLightStatusObserver()
       
    79     {
       
    80     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::~CHWRMExtendedLightStatusObserver()" ));
       
    81 
       
    82     // Cancel property separately, as the following Cancel() call doesn't seem to 
       
    83     // cause DoCancel to fire.
       
    84     iProperty.Cancel();
       
    85     
       
    86     Cancel();
       
    87     
       
    88     iProperty.Close();
       
    89     
       
    90     iLastStatusValue.Close();
       
    91     iStatusUnknownList.Close();
       
    92     iCallback = NULL; // PCLint demands
       
    93 
       
    94     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::~CHWRMExtendedLightStatusObserver - return" ) );
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CHWRMExtendedLightStatusObserver::ConstructL
       
    99 // Symbian 2nd phase constructor can leave.
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CHWRMExtendedLightStatusObserver::ConstructL()
       
   103     {
       
   104     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::ConstructL()" ));
       
   105 
       
   106     User::LeaveIfError( InitializeStatusArray( iLastStatusValue ) );
       
   107     User::LeaveIfError( InitializeStatusArray( iStatusUnknownList ) );
       
   108     User::LeaveIfError(iProperty.Attach(KPSUidHWResourceNotification, KHWRMLightStatus));
       
   109     
       
   110     // Call RunL to order notification and do the initial notifying of the client
       
   111     RunL();
       
   112 
       
   113     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::ConstructL - return" ) );
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CHWRMExtendedLightStatusObserver::RunL
       
   118 // Handle notification from pubsub session.
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CHWRMExtendedLightStatusObserver::RunL()
       
   122     {
       
   123     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::RunL()" ));
       
   124 
       
   125     // Reorder notification
       
   126     OrderNotification();
       
   127 
       
   128     // get value from pubsub
       
   129     RLightStatusArray statusArray( KHWRMLightMaxTargets );
       
   130     CleanupClosePushL( statusArray );
       
   131     TInt err = InitializeStatusArray( statusArray );
       
   132     if( err == KErrNone )
       
   133         {
       
   134         TInt arraySize = sizeof( THWRMStatusInfo ) * KHWRMLightMaxTargets;
       
   135         TPtr8 arrayPtr( ( TUint8* )&statusArray[0], arraySize, arraySize );
       
   136         err = iProperty.Get( arrayPtr );
       
   137     if ( err == KErrNotFound || err == KErrNone )
       
   138         {
       
   139             NotifyCallback( statusArray );
       
   140             }
       
   141         }
       
   142     
       
   143     else
       
   144         {
       
   145         COMPONENT_TRACE2(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::RunL. Error (%d) in iProperty.Get()" ), err);
       
   146         NotifyCallback(iStatusUnknownList );
       
   147         }
       
   148         
       
   149     CleanupStack::PopAndDestroy( &statusArray );
       
   150     User::LeaveIfError( err );
       
   151 
       
   152     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::RunL - return" ) );
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // CHWRMExtendedLightStatusObserver::DoCancel
       
   157 // Handle cancel order on this active object.
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CHWRMExtendedLightStatusObserver::DoCancel()
       
   161     {
       
   162     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::DoCancel()" ));
       
   163 
       
   164     iProperty.Cancel();
       
   165 
       
   166     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::DoCancel - return" ) );
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CHWRMExtendedLightStatusObserver::OrderNotification
       
   171 // Order new notification from CenRep.
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void CHWRMExtendedLightStatusObserver::OrderNotification()
       
   175     {
       
   176     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::OrderNotification()" ));
       
   177 
       
   178     iProperty.Subscribe(iStatus);
       
   179     SetActive();
       
   180 
       
   181     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::OrderNotification - return" ) );
       
   182     }
       
   183 
       
   184 
       
   185 // -----------------------------------------------------------------------------
       
   186 // CHWRMExtendedLightStatusObserver::NotifyCallback
       
   187 // Order new notification from CenRep.
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 void CHWRMExtendedLightStatusObserver::NotifyCallback( RLightStatusArray& aStatusList )
       
   191     {
       
   192     COMPONENT_TRACE1( ( _L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::NotifyCallback" ) ) );
       
   193 
       
   194 
       
   195     // Loop through value and update client. Hold notifying client until end unless there are
       
   196     // multiple differently changed target statuses.
       
   197     TInt currentTarget = KHWRMLightFirstTarget;
       
   198     TInt currentStatus = 0;
       
   199     TInt lastChangedStatus = 0;
       
   200     TInt index = 0;
       
   201     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
   202         {
       
   203         // only notify for supported targets
       
   204         if( currentTarget & iSupportedTargets )
       
   205             {            
       
   206             index = aStatusList.FindInOrder( currentTarget, FindByTarget ); 
       
   207             if( index >= 0 &&  index < KHWRMLightMaxTargets )
       
   208                 {
       
   209                 currentStatus = aStatusList[index].iStatus;
       
   210                 index = iLastStatusValue.FindInOrder( currentTarget, FindByTarget ); 
       
   211                 if( index >= 0 && index < KHWRMLightMaxTargets )
       
   212                     {
       
   213                     lastChangedStatus = iLastStatusValue[index].iStatus;
       
   214                     // If last value for masked target is different than new value, prepare to notify about that target
       
   215                     if( lastChangedStatus != currentStatus )
       
   216                         {
       
   217                         // update last status value and notify client
       
   218                         iLastStatusValue[index].iStatus = currentStatus;
       
   219                         if( iCallback )
       
   220                             {
       
   221                             iCallback->LightStatusChanged( currentTarget,
       
   222                                 static_cast<CHWRMExtendedLight::TLightStatus>( currentStatus ) );    
       
   223                             }
       
   224                         }
       
   225                     }
       
   226                 }
       
   227             }
       
   228             
       
   229         // shift the target ready for next loop
       
   230         currentTarget <<= 1;
       
   231         }
       
   232 
       
   233     COMPONENT_TRACE1(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::NotifyCallback - return" ) );
       
   234     
       
   235     }
       
   236     
       
   237 // -----------------------------------------------------------------------------
       
   238 // CHWRMExtendedLightStatusObserver::CurrentStatus
       
   239 // Return Light status
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 CHWRMExtendedLight::TLightStatus CHWRMExtendedLightStatusObserver::CurrentStatus(TInt aTarget) const
       
   243     {
       
   244     COMPONENT_TRACE2(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::CurrentStatus(0x%x)" ), aTarget );
       
   245     
       
   246     CHWRMExtendedLight::TLightStatus status( CHWRMExtendedLight::ELightStatusUnknown );
       
   247     RLightStatusArray currentStatus( KHWRMLightMaxTargets );
       
   248     TInt err = InitializeStatusArray( currentStatus );
       
   249     if ( err == KErrNone )
       
   250         {
       
   251         TInt arraySize = sizeof( THWRMStatusInfo ) * KHWRMLightMaxTargets;
       
   252         TPtr8 arrayPtr( ( TUint8* )&currentStatus[0], arraySize, arraySize );
       
   253         err = RProperty::Get( KPSUidHWResourceNotification, KHWRMLightStatus, arrayPtr );
       
   254         if( err == KErrNone )
       
   255         {
       
   256             TInt index = currentStatus.FindInOrder( aTarget, FindByTarget );
       
   257             if( index >= 0 && index < KHWRMLightMaxTargets )
       
   258             {
       
   259                 status = static_cast<CHWRMExtendedLight::TLightStatus>(
       
   260                     currentStatus[index].iStatus );
       
   261                 }
       
   262             }
       
   263         }
       
   264         
       
   265     // Clean up
       
   266     currentStatus.Close();   
       
   267     
       
   268     COMPONENT_TRACE2(_L( "HWRM ExtendedLightClient - CHWRMExtendedLightStatusObserver::CurrentStatus - return 0x%x" ), status );
       
   269     
       
   270     return status;
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CHWRMLightExtendedStatusObserver::FindByTarget
       
   275 // Finds item from iStatusList by target.
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 TInt CHWRMExtendedLightStatusObserver::FindByTarget( const TInt* aTarget, 
       
   279     const THWRMStatusInfo& aItem )
       
   280     {
       
   281     if ( *aTarget < aItem.iTarget )
       
   282         {
       
   283         return -1;
       
   284         }
       
   285     else if ( *aTarget > aItem.iTarget )
       
   286         {
       
   287         return 1;
       
   288         }
       
   289 
       
   290     return 0;
       
   291     }
       
   292 // -----------------------------------------------------------------------------
       
   293 // CHWRMLightStatusObserver::InitializeStatusArray
       
   294 // Initializes RLightStatusArray.
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 TInt CHWRMExtendedLightStatusObserver::InitializeStatusArray(
       
   298     RLightStatusArray& aArray ) const
       
   299     {
       
   300     TInt err = KErrNone;
       
   301     TInt currentTarget( KHWRMLightFirstTarget );
       
   302     for( TInt i = 0; i < KHWRMLightMaxTargets; i++ )
       
   303         {
       
   304         THWRMStatusInfo info;
       
   305         info.iTarget = currentTarget;
       
   306         info.iStatus = CHWRMLight::ELightStatusUnknown;
       
   307         err = aArray.Append( info );
       
   308         if( err != KErrNone )
       
   309             {
       
   310             break;
       
   311             }
       
   312         
       
   313         // shift the mask and target ready for next loop
       
   314         currentTarget <<= 1;
       
   315         }
       
   316 
       
   317     return err;
       
   318     }
       
   319 
       
   320 // End of File