resourcemgmt/hwresourcesmgr/server/src/HWRMPowerCommonData.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 <centralrepository.h>
       
    21 #include "HWRMtrace.h"
       
    22 #include "HWRMPowerCommonData.h"
       
    23 #include "HWRMClientServer.h"
       
    24 #include "HWRMPowerDomainCRKeys.h"
       
    25 #include "HWRMPrivateCRKeys.h"
       
    26 #include "HWRMConfiguration.h"
       
    27 #include "HWRMPowerService.h"
       
    28 #include "HWRMPower.h"
       
    29 #include "HWRMPowerService.h"
       
    30 
       
    31 // EXTERNAL DATA STRUCTURES
       
    32 // None
       
    33 
       
    34 // EXTERNAL FUNCTION PROTOTYPES  
       
    35 // None
       
    36 
       
    37 // CONSTANTS
       
    38 const TInt KMilliToMicroSeconds = 1000;
       
    39 
       
    40 // MACROS
       
    41 // None
       
    42 
       
    43 // LOCAL CONSTANTS AND MACROS
       
    44 // None
       
    45 
       
    46 // MODULE DATA STRUCTURES
       
    47 // None
       
    48 
       
    49 // LOCAL FUNCTION PROTOTYPES
       
    50 // None
       
    51 
       
    52 // FORWARD DECLARATIONS
       
    53 // None
       
    54 
       
    55 // ============================= LOCAL FUNCTIONS ===============================
       
    56 
       
    57 // ============================ MEMBER FUNCTIONS ===============================
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CHWRMPowerCommonData::CHWRMPowerCommonData
       
    61 // C++ constructor
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CHWRMPowerCommonData::CHWRMPowerCommonData()
       
    65 	:iBatteryInfoQue(_FOFF(TBatteryInfoData,iSglQueLink)),
       
    66 	iBatteryInfoIter(iBatteryInfoQue),
       
    67 	iBatteryVoltageInfoQue(_FOFF(TBatteryInfoData,iSglQueLink)),
       
    68 	iBatteryVoltageInfoIter(iBatteryVoltageInfoQue)
       
    69 #ifdef SYMBIAN_HWRM_EXTPOWERINFO 
       
    70 	,iChargerTypeInfoQue(_FOFF(TBatteryInfoData,iSglQueLink)),
       
    71 	iChargerTypeInfoIter(iChargerTypeInfoQue)
       
    72 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
    73     {
       
    74     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CHWRMPowerCommonData()" ) );
       
    75     
       
    76     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CHWRMPowerCommonData - return" ) );
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CHWRMPowerCommonData::ConstructL
       
    81 // Symbian 2nd phase constructor can leave.
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 void CHWRMPowerCommonData::ConstructL(CHWRMPluginHandler* aPluginHandler)
       
    85     {    
       
    86     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::ConstructL()" ) );
       
    87 
       
    88     // Get read only values from cenrep
       
    89     iRepository = CRepository::NewL(KCRUidPowerSettings);
       
    90 
       
    91     iMaxReportingPeriodNotifyHandler = CCenRepListener::NewL(*this, 
       
    92                                                 *iRepository, 
       
    93                                                 KPowerMaxReportingPeriod);
       
    94     iMaxReportingPeriodNotifyHandler->StartListeningL();
       
    95 
       
    96     TInt value(0);
       
    97     User::LeaveIfError(iRepository->Get(KPowerBaseTimeInterval, value));
       
    98     if ( value < 1 )
       
    99         {
       
   100         User::Leave(KErrArgument);
       
   101         }
       
   102     iBaseTimeInterval = value * 1000;  // milliseconds to microseconds 
       
   103 
       
   104    	COMPONENT_TRACE2(_L( "HWRM Server - CHWRMPowerCommonData::ConstructL - iBaseTimeInterval: %d" ), iBaseTimeInterval );
       
   105     
       
   106     User::LeaveIfError(iRepository->Get(KPowerMaxReportingPeriod, value));
       
   107     if ( value < 1 )
       
   108         {
       
   109         User::Leave(KErrArgument);
       
   110         }
       
   111     iMaximumReportingPeriod = value * KMilliToMicroSeconds;  // milliseconds to microseconds 
       
   112 
       
   113     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMPowerCommonData::ConstructL - iMaximumReportingPeriod: %d" ), iMaximumReportingPeriod );
       
   114 
       
   115     // Register for indications from HWRM plugin.
       
   116     aPluginHandler->RegisterForIndications(this);
       
   117     
       
   118     iPowerService = CHWRMPowerService::NewL(aPluginHandler, *this);
       
   119 
       
   120     iPowerReportMeasurementsTimer = CHWRMGenericTimer::NewL(*this, iMaximumReportingPeriod, EPowerReportClientsId);    
       
   121 
       
   122 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   123     iChargingCurrentMeasurementsTimer = CHWRMGenericTimer::NewL(*this, iMaximumReportingPeriod, EChargingCurrentClientsId);
       
   124     
       
   125     //Timer should not be started for charging time measurements. So pass KHWRMGenericTimerMaxTime.
       
   126     iChargingTimeMeasurementsTimer = CHWRMGenericTimer::NewL(*this, KHWRMGenericTimerMaxTime, EChargingTimeClientsId);
       
   127 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   128     
       
   129     iPowerReportClientsData = CClientsData::NewL(*iPowerReportMeasurementsTimer);
       
   130 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   131     iChargingCurrentClientsData = CClientsData::NewL(*iChargingCurrentMeasurementsTimer);
       
   132     iChargingTimeClientsData = CClientsData::NewL(*iChargingTimeMeasurementsTimer);
       
   133 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   134     		
       
   135     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::ConstructL - return" ) );
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CHWRMPowerCommonData::NewL
       
   140 // Two-phased constructor.
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 CHWRMPowerCommonData* CHWRMPowerCommonData::NewL(CHWRMPluginHandler* aPluginHandler)
       
   144     {
       
   145     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::NewL()" ) );
       
   146 
       
   147     CHWRMPowerCommonData* self = new( ELeave ) CHWRMPowerCommonData();
       
   148     
       
   149     CleanupStack::PushL( self );
       
   150     self->ConstructL(aPluginHandler);
       
   151     CleanupStack::Pop();
       
   152 
       
   153     COMPONENT_TRACE2(_L( "HWRM Server - CHWRMPowerCommonData::NewL - return 0x%x" ), self );
       
   154     
       
   155     return self;
       
   156     }
       
   157    
       
   158 // ---------------------------------------------------------
       
   159 // Destructor
       
   160 // ---------------------------------------------------------
       
   161 //
       
   162 CHWRMPowerCommonData::~CHWRMPowerCommonData()
       
   163     {
       
   164     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::~CHWRMPowerCommonData()" ) );
       
   165     
       
   166     delete iPowerService;
       
   167     iPowerService = NULL;
       
   168     
       
   169     iMaxReportingPeriodNotifyHandler->StopListening();
       
   170     delete iMaxReportingPeriodNotifyHandler;
       
   171     iMaxReportingPeriodNotifyHandler =  NULL;
       
   172 
       
   173     delete iRepository;  
       
   174     iRepository = NULL;
       
   175     
       
   176     delete iPowerReportClientsData;
       
   177     iPowerReportClientsData = NULL;
       
   178 
       
   179 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   180     
       
   181     delete iChargingCurrentClientsData;
       
   182     iChargingCurrentClientsData = NULL;
       
   183     
       
   184     delete iChargingTimeClientsData;
       
   185     iChargingTimeClientsData = NULL;
       
   186     
       
   187 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   188     
       
   189     if (iPowerReportMeasurementsTimer)
       
   190         {
       
   191         iPowerReportMeasurementsTimer->Cancel();
       
   192         delete iPowerReportMeasurementsTimer;
       
   193         iPowerReportMeasurementsTimer = NULL;
       
   194         }
       
   195     
       
   196 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   197 
       
   198     if(iChargingCurrentMeasurementsTimer)
       
   199         {
       
   200         iChargingCurrentMeasurementsTimer->Cancel();
       
   201         delete iChargingCurrentMeasurementsTimer;
       
   202         iChargingCurrentMeasurementsTimer = NULL;
       
   203         }
       
   204 
       
   205     if (iChargingTimeMeasurementsTimer)
       
   206         {
       
   207         iChargingTimeMeasurementsTimer->Cancel();
       
   208         delete iChargingTimeMeasurementsTimer;
       
   209         iChargingTimeMeasurementsTimer = NULL;
       
   210         }
       
   211     
       
   212 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   213     
       
   214 	TBatteryInfoData*  batteryInfoData;
       
   215 	
       
   216 	iBatteryInfoIter.SetToFirst();
       
   217 	
       
   218     while((batteryInfoData = iBatteryInfoIter++) != NULL)
       
   219         {
       
   220         iBatteryInfoQue.Remove(*batteryInfoData);
       
   221         delete batteryInfoData;
       
   222         }
       
   223 	
       
   224 	iBatteryVoltageInfoIter.SetToFirst();
       
   225 	
       
   226     while((batteryInfoData = iBatteryVoltageInfoIter++) != NULL)
       
   227         {
       
   228         iBatteryVoltageInfoQue.Remove(*batteryInfoData);
       
   229         delete batteryInfoData;
       
   230         }
       
   231     
       
   232 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   233     
       
   234     iChargerTypeInfoIter.SetToFirst();
       
   235     
       
   236     while((batteryInfoData = iChargerTypeInfoIter++) != NULL)
       
   237         {
       
   238         iChargerTypeInfoQue.Remove(*batteryInfoData);
       
   239         delete batteryInfoData;
       
   240         }
       
   241     
       
   242 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   243     
       
   244     
       
   245     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::~CHWRMPowerCommonData - return" ) );
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------
       
   249 // CHWRMPowerCommonData::ProcessIndicationL
       
   250 // Callback function From MHWRMIndicationHandler
       
   251 // Called by the plugin handler to indicate that a new 
       
   252 // measurement report is ready
       
   253 // ---------------------------------------------------------
       
   254 //
       
   255 void CHWRMPowerCommonData::ProcessIndicationL( const TUint32 aId, TDesC8& aData )
       
   256 	{
       
   257     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::ProcessIndicationL()" ) );
       
   258 
       
   259 	switch (aId)
       
   260 		{
       
   261         case HWRMPowerCommand::EAverageBatteryPowerIndicationId:
       
   262 			{
       
   263 			if (iPowerReportClientsData->IsMeasuringOn())
       
   264 				{
       
   265 				HWRMPowerCommand::TBatteryPowerReportPackage batteryPowerReportPckg;
       
   266 				batteryPowerReportPckg.Copy(aData);
       
   267 				HWRMPowerCommand::TBatteryPowerReport batteryPowerReport = batteryPowerReportPckg();
       
   268 				
       
   269 				if (batteryPowerReport.iErrorCode == KErrNone)
       
   270 					{					
       
   271 					iPowerReportClientsData->BatteryMeasurementIndicationL(batteryPowerReportPckg);
       
   272 					}
       
   273 				}
       
   274 			}
       
   275 		break;
       
   276 
       
   277 		#ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   278         
       
   279         case HWRMPowerCommand::EAverageBatteryChargingCurrentIndicationId:
       
   280 			{
       
   281 			if (iChargingCurrentClientsData->IsMeasuringOn())
       
   282 				{
       
   283 				HWRMPowerCommand::TBatteryChargingCurrentReportPackage batteryChargeCurrentReportPckg;
       
   284 				batteryChargeCurrentReportPckg.Copy(aData);
       
   285 				HWRMPowerCommand::TBatteryChargeCurrentReport batteryChargeCurrentReport= batteryChargeCurrentReportPckg();
       
   286 				
       
   287 				if (batteryChargeCurrentReport.iErrorCode == KErrNone)
       
   288 					{
       
   289 					iChargingCurrentClientsData->BatteryMeasurementIndicationL(batteryChargeCurrentReportPckg);
       
   290 					}
       
   291 				}
       
   292 			}
       
   293 		break;
       
   294 
       
   295         case HWRMPowerCommand::EBatteryFullChargingTimeIndicationId:
       
   296             {
       
   297 			if (iChargingTimeClientsData->IsMeasuringOn())
       
   298 				{
       
   299 				HWRMPowerCommand::TBatteryFullChargingTimeReportPackage batteryChargeTimePckg;
       
   300 				batteryChargeTimePckg.Copy(aData);
       
   301 				HWRMPowerCommand::TBatteryFullChargingTimeReport batteryChargeTimeReport= batteryChargeTimePckg();
       
   302 				
       
   303 				if (batteryChargeTimeReport.iErrorCode == KErrNone)
       
   304 					{
       
   305 					iChargingTimeClientsData->BatteryMeasurementIndicationL(batteryChargeTimePckg);
       
   306 					}
       
   307 				}
       
   308 			}
       
   309         break;
       
   310 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   311         
       
   312 		}	
       
   313     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::EventIndicationL - return" ) );
       
   314 	}
       
   315 
       
   316 // ---------------------------------------------------------
       
   317 // CHWRMPowerCommonData::GenericTimerFired
       
   318 // Callback function From MHWRMGenericTimerCallback
       
   319 // Called when the timer is fired. The PowerCommonData
       
   320 // hold two of them one for Voltage, one for Consumption 
       
   321 // Measurements. Informs clients, cleans up and stops the 
       
   322 // plugin.
       
   323 // ---------------------------------------------------------
       
   324 //
       
   325 void CHWRMPowerCommonData::GenericTimerFired(TInt aTimerId, TBool /*aCutOff*/)
       
   326 	{
       
   327 	TInt err(KErrNone);
       
   328 	
       
   329     COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::GenericTimerFired()" ) );
       
   330     
       
   331 	switch(aTimerId)
       
   332 		{
       
   333         case EPowerReportClientsId:
       
   334 			{
       
   335 			iPowerReportClientsData->MaxReportingPeriodReached();
       
   336 			TRAP(err, DoStopPowerMeasurementsL());
       
   337 			break;
       
   338 			}
       
   339        
       
   340 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   341         
       
   342         case EChargingCurrentClientsId:
       
   343             {
       
   344             iChargingCurrentClientsData->MaxReportingPeriodReached();
       
   345             TRAP(err,DoStopChargingCurrentMeasurementsL());
       
   346             break;
       
   347             }
       
   348         
       
   349 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   350 		}
       
   351 	
       
   352 	if (err != KErrNone)
       
   353 		{
       
   354 		// All the pending batch request have been completed now.
       
   355 		// Also plugin will not send any more measurements.
       
   356 		COMPONENT_TRACE3(_L( "HWRM Server - CHWRMPowerCommonData::GenericTimerFired(%d) - Stopping measurement failed: %d" ), aTimerId, err );
       
   357 		}
       
   358 	
       
   359 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::GenericTimerFired - return" ) );
       
   360 	}
       
   361 
       
   362 // ---------------------------------------------------------
       
   363 // CHWRMPowerCommonData::DoStopPowerMeasurementsL
       
   364 // Issues a plugin command to stop the Power measurements.
       
   365 // ---------------------------------------------------------
       
   366 //
       
   367 void CHWRMPowerCommonData::DoStopPowerMeasurementsL()
       
   368 	{
       
   369 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoStopPowerMeasurementsL()" ) );
       
   370 	
       
   371 	iPowerService->StopPowerMeasurementsL();
       
   372 	
       
   373 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoStopPowerMeasurementsL - return" ) );
       
   374 	}
       
   375 
       
   376 
       
   377 
       
   378 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   379 // ---------------------------------------------------------
       
   380 // CHWRMPowerCommonData::DoStopChargingCurrentMeasurementsL
       
   381 // Issues a plugin command to stop the Charging current measurements.
       
   382 // ---------------------------------------------------------
       
   383 //
       
   384 void CHWRMPowerCommonData::DoStopChargingCurrentMeasurementsL()
       
   385 	{
       
   386 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoStopChargingCurrentMeasurementsL()" ) );
       
   387 	
       
   388 	iPowerService->StopChargingCurrentMeasurementsL();
       
   389 	
       
   390 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoStopChargingCurrentMeasurementsL - return" ) );
       
   391 	}
       
   392 
       
   393 // ---------------------------------------------------------
       
   394 // CHWRMPowerCommonData::DoStopChargingTimeMeasurementsL
       
   395 // Issues a plugin command to stop BatteryFull charging time measurement.
       
   396 // ---------------------------------------------------------
       
   397 //
       
   398 void CHWRMPowerCommonData::DoStopChargingTimeMeasurementsL()
       
   399 	{
       
   400 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoStopChargingTimeMeasurementsL()" ) );
       
   401 	
       
   402 	iPowerService->StopChargingTimeMeasurementsL();
       
   403 	
       
   404 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoStopChargingTimeMeasurementsL - return" ) );
       
   405 	}
       
   406 
       
   407 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   408 
       
   409 // ---------------------------------------------------------
       
   410 // CHWRMPowerCommonData::DoMaxReportingValueChanged
       
   411 // Handles the max reporting period cenrep value changes for all measurements.
       
   412 // ---------------------------------------------------------
       
   413 void CHWRMPowerCommonData::MaxPeriodChangedL(TTimeIntervalMicroSeconds32& aNewMaxPeriod,
       
   414                            TTimeIntervalMicroSeconds32& aOldMaxPeriod)
       
   415     {
       
   416     
       
   417     DoMaxReportingValueChangedL(iPowerReportMeasurementsTimer, aNewMaxPeriod, aOldMaxPeriod);
       
   418 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   419     DoMaxReportingValueChangedL(iChargingCurrentMeasurementsTimer, aNewMaxPeriod, aOldMaxPeriod);
       
   420     DoMaxReportingValueChangedL(iChargingTimeMeasurementsTimer, aNewMaxPeriod, aOldMaxPeriod);
       
   421 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   422     }
       
   423 
       
   424 
       
   425 
       
   426 // ---------------------------------------------------------
       
   427 // CHWRMPowerCommonData::DoMaxReportingValueChangedL
       
   428 // Handles the max reporting period cenrep value changes.
       
   429 // ---------------------------------------------------------
       
   430 
       
   431 
       
   432 void CHWRMPowerCommonData::DoMaxReportingValueChangedL(CHWRMGenericTimer* aTimer, 
       
   433                                                       TTimeIntervalMicroSeconds32& aNewMaxPeriod,
       
   434                                                       TTimeIntervalMicroSeconds32& aOldMaxPeriod)
       
   435     {    
       
   436     /*
       
   437     * Timer Usage:
       
   438     * aTimer->IsActive()  : Checks if timer is running(active, which means measurement is going on.)
       
   439     * aTimer->Freeze()    : Stop the timer.
       
   440     * aTimer->GetTimerId(): Get the measurement(power,charging,..) corresponding to timer.
       
   441     * aTimer->RunL()      : Calls CHWRMPowerCommonData::GenericTimerFired, which stops the measurements.
       
   442     *
       
   443     * - If new max reporting period is negative, complete pending measurement with KErrGeneral and stop the measurements.
       
   444     * - If timer is not running, just set the timer.
       
   445     * - If new max reporting period is zero, stop the timer if running and measurements will go on for infinite duration.
       
   446     * - If new max reporting period is more than elapsed time, stop current time and start for remaining time(Max-elapsed).
       
   447     * - If new max reporting period is less than elapsed time, complete pending measurement with KErrTimedOut and stop the measurements.
       
   448     */
       
   449     #ifdef _DEBUG
       
   450     _LIT(KHWRMPowerCommonDataPanic,"CHWRMPowerCommonData");
       
   451     #endif      
       
   452     __ASSERT_DEBUG((aTimer)!=NULL, User::Panic(KHWRMPowerCommonDataPanic,KErrBadHandle));
       
   453    
       
   454     if(aNewMaxPeriod.Int() < 0)
       
   455         {
       
   456         if(aTimer->IsActive())
       
   457             {
       
   458             //Cenrep value is negative, Inform client that timer has expired.
       
   459             aTimer->Freeze();
       
   460             
       
   461             switch(aTimer->GetTimerId())
       
   462                 {
       
   463                 case EPowerReportClientsId:
       
   464             		{
       
   465             		iPowerReportClientsData->MaxReportingPeriodReached(KErrGeneral);
       
   466             		break;
       
   467             		}
       
   468                 
       
   469 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   470                 
       
   471                 case EChargingCurrentClientsId:
       
   472                     {
       
   473                     iChargingCurrentClientsData->MaxReportingPeriodReached(KErrGeneral);
       
   474                     break;
       
   475                     }
       
   476                 
       
   477 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   478 
       
   479                 }
       
   480             aTimer->RunL();
       
   481             }
       
   482         }
       
   483     else
       
   484         {
       
   485         iMaximumReportingPeriod = aNewMaxPeriod.Int();
       
   486         
       
   487         aTimer->SetMaximumTime(aNewMaxPeriod);
       
   488         
       
   489         if(aOldMaxPeriod.Int() == 0)
       
   490             {
       
   491             aTimer->Set(aNewMaxPeriod);
       
   492             }
       
   493     	else if(aTimer->IsActive())
       
   494     		{
       
   495     		if(aNewMaxPeriod.Int() == 0)
       
   496     			{
       
   497     			//Stop Timer and don't Start Timer
       
   498     			aTimer->Freeze();
       
   499     			}
       
   500     		else
       
   501     			{
       
   502     			//Ex: If aOldMaxPeriod=8sec, aNewMaxPeriod=10sec, 
       
   503     			// elapsedTime=4sec, start timer with 6sec.
       
   504     
       
   505                 //Freeze will stop the timer and return remaining time.
       
   506     			TTimeIntervalMicroSeconds32 remainingTime = aTimer->Freeze();
       
   507     			TTimeIntervalMicroSeconds32 elapsedTime;
       
   508     			elapsedTime = aOldMaxPeriod.Int() - remainingTime.Int();
       
   509     			if(aNewMaxPeriod > elapsedTime)
       
   510     				{
       
   511     				//Start a new Timer.
       
   512     				aTimer->Set((aNewMaxPeriod.Int() - elapsedTime.Int()));
       
   513     				}
       
   514                 else
       
   515                     {
       
   516                     //Inform Client that Timer has expired.
       
   517                     aTimer->Freeze();
       
   518                     aTimer->RunL();
       
   519                     }
       
   520     			}
       
   521             }
       
   522         }
       
   523     }
       
   524 
       
   525 // -----------------------------------------------------------------------------
       
   526 // CHWRMPowerCommonData::HandleNotifyInt
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 void CHWRMPowerCommonData::HandleNotifyInt(TUint32 aId, TInt aNewValue)
       
   530     {
       
   531     COMPONENT_TRACE3(_L("HWRM Server - CHWRMPowerCommonData::HandleNotifyInt(0x%x, 0x%x)"), aId, aNewValue) ; 
       
   532 
       
   533     switch(aId)
       
   534         {
       
   535         case KPowerMaxReportingPeriod:
       
   536             {
       
   537             TTimeIntervalMicroSeconds32 oldMaxPeriod = iMaximumReportingPeriod;
       
   538             TTimeIntervalMicroSeconds32 newMaxPeriod = aNewValue*KMilliToMicroSeconds;
       
   539             
       
   540             if(oldMaxPeriod.Int() != newMaxPeriod.Int())
       
   541                 {
       
   542                 TRAP_IGNORE(MaxPeriodChangedL(newMaxPeriod, oldMaxPeriod));
       
   543                 }
       
   544             break;
       
   545             }
       
   546        default:
       
   547             COMPONENT_TRACE2(_L("HWRM Server - CHWRMPowerCommonData::HandleNotifyInt Cannot handle(0x%x)"), aId);
       
   548             break;
       
   549 
       
   550         }
       
   551 
       
   552     COMPONENT_TRACE1(_L("HWRM Server - CHWRMPowerCommonData::HandleNotifyInt - return") ); 
       
   553     }
       
   554 
       
   555 // -----------------------------------------------------------------------------
       
   556 // CHWRMPowerCommonData::HandleNotifyError
       
   557 // -----------------------------------------------------------------------------
       
   558 //
       
   559 #if  defined(COMPONENT_TRACE_FLAG) && defined(_DEBUG)
       
   560 void CHWRMPowerCommonData::HandleNotifyError(TUint32 aId, TInt error, CCenRepListener* aHandler)
       
   561 #else
       
   562 void CHWRMPowerCommonData::HandleNotifyError(TUint32 /*aId*/, TInt /*error*/, CCenRepListener* /*aHandler*/)
       
   563 #endif
       
   564     {
       
   565     COMPONENT_TRACE4(_L("HWRM Server - CHWRMPowerCommonData::HandleNotifyError(0x%x, 0x%x, 0x%x)"), aId, error, aHandler); 
       
   566     
       
   567 
       
   568     COMPONENT_TRACE1(_L("HWRM Server - CHWRMPowerCommonData::HandleNotifyError - return") ); 
       
   569     }
       
   570 
       
   571 // ---------------------------------------------------------
       
   572 // CHWRMPowerCommonData::DoSetPluginIntervalMultipleL
       
   573 // Issues a plugin command to set the plugin interval. 
       
   574 // Used by charging current reporting.
       
   575 // ---------------------------------------------------------
       
   576 //
       
   577 void  CHWRMPowerCommonData::DoSetPluginIntervalMultipleL(HWRMPowerCommand::TPowerCmd aCommand,
       
   578 											  		   TUint aIntervalMultiple)
       
   579 	{
       
   580 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoSetPluginIntervalMultipleL()" ) );
       
   581 	
       
   582 	iPowerService->SetPluginIntervalMultipleL(aCommand, aIntervalMultiple);
       
   583 	
       
   584 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoSetPluginIntervalMultipleL - return" ) );
       
   585 	}
       
   586 
       
   587 
       
   588 // ---------------------------------------------------------
       
   589 // CHWRMPowerCommonData::DoGetBatteryInfoL
       
   590 // Issues a plugin command to get Battery Info. If there is
       
   591 // an outstanding plugin request there isn't a new
       
   592 // one issued but the appropriate data are
       
   593 // stored in a queue. This data are used later to complete
       
   594 // or cancel the client.
       
   595 // ---------------------------------------------------------
       
   596 //
       
   597 void CHWRMPowerCommonData::DoGetBatteryInfoL(const RMessage2& aMessage, const CHWRMPowerService& aPowerService)
       
   598 	{
       
   599 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoGetBatteryInfoL()" ) );
       
   600 
       
   601 	TBatteryInfoData* batteryInfoData = new(ELeave) TBatteryInfoData(aPowerService, aMessage);
       
   602 	
       
   603 	if (iBatteryInfoQue.IsEmpty())
       
   604 		{
       
   605 		CleanupStack::PushL(batteryInfoData);
       
   606 		
       
   607 		RMessage2 dummy;
       
   608 	    iPowerService->GetBatteryInfoL(dummy);
       
   609 	    
       
   610 		CleanupStack::Pop(batteryInfoData);
       
   611 		}
       
   612 	iBatteryInfoQue.AddFirst(*batteryInfoData);    
       
   613     
       
   614 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoGetBatteryInfoL - return" ) );
       
   615 	}
       
   616 
       
   617 // ---------------------------------------------------------
       
   618 // CHWRMPowerCommonData::DoGetBatteryVoltageInfoL
       
   619 // Issues a plugin command to get Battery Voltage Info. If there is
       
   620 // an outstanding plugin request there isn't a new
       
   621 // one issued but the appropriate data are
       
   622 // stored in a queue. This data are used later to complete
       
   623 // or cancel the client.
       
   624 // ---------------------------------------------------------
       
   625 //
       
   626 void CHWRMPowerCommonData::DoGetBatteryVoltageInfoL(const RMessage2& aMessage, const CHWRMPowerService& aPowerService)
       
   627 	{
       
   628 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoGetBatteryVoltageInfoL()" ) );
       
   629 
       
   630 	TBatteryInfoData* batteryVoltageInfoData = new(ELeave) TBatteryInfoData(aPowerService, aMessage);
       
   631 	
       
   632 	if (iBatteryVoltageInfoQue.IsEmpty())
       
   633 		{
       
   634 		CleanupStack::PushL(batteryVoltageInfoData);
       
   635 		
       
   636 		RMessage2 dummy;
       
   637 	    iPowerService->GetBatteryVoltageInfoL(dummy);
       
   638 	    
       
   639 		CleanupStack::Pop(batteryVoltageInfoData);
       
   640 		}
       
   641 	iBatteryVoltageInfoQue.AddFirst(*batteryVoltageInfoData);    
       
   642     
       
   643 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoGetBatteryVoltageInfoL - return" ) );
       
   644 	}
       
   645 
       
   646 // ---------------------------------------------------------
       
   647 // CHWRMPowerCommonData::CompletePendingGetBatteryInfo
       
   648 // Completes and removes from the respective list any pending 
       
   649 // GetBatteryInfo requests.
       
   650 // ---------------------------------------------------------
       
   651 //
       
   652 void CHWRMPowerCommonData::CompletePendingGetBatteryInfo(TInt aReason, TPckg<CHWRMPower::TBatteryConsumptionData> &aDataPckg)
       
   653 	{
       
   654 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CompletePendingGetBatteryInfo()" ) );
       
   655 	
       
   656 	TBatteryInfoData* batteryInfoData;
       
   657 	TInt error;
       
   658 	
       
   659 	//inform everybody
       
   660 	iBatteryInfoIter.SetToFirst();
       
   661 	while((batteryInfoData = iBatteryInfoIter++) != NULL)
       
   662 		{
       
   663 		error = aReason;
       
   664 		if (error == KErrNone)
       
   665 			{
       
   666 			error = batteryInfoData->iRequestedMessage.Write(0,aDataPckg);
       
   667 			}
       
   668 		batteryInfoData->iRequestedMessage.Complete(error);
       
   669 		iBatteryInfoQue.Remove(*batteryInfoData);
       
   670 		
       
   671 		delete batteryInfoData;
       
   672 		}             
       
   673 	
       
   674 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CompletePendingGetBatteryInfo - return" ) );
       
   675 	}
       
   676 
       
   677 // ---------------------------------------------------------
       
   678 // CHWRMPowerCommonData::CompletePendingGetBatteryVoltageInfo
       
   679 // Completes and removes from the respective list any pending 
       
   680 // GetBatteryVoltageInfo requests.
       
   681 // ---------------------------------------------------------
       
   682 //
       
   683 void CHWRMPowerCommonData::CompletePendingGetBatteryVoltageInfo(TInt aReason, TPckg<CHWRMPower::TBatteryVoltageData> &aDataPckg)
       
   684 	{
       
   685 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CompletePendingGetBatteryVoltageInfo()" ) );
       
   686 	
       
   687 	TBatteryInfoData* batteryVoltageInfoData;
       
   688 	TInt error;
       
   689 	
       
   690 	//inform everybody
       
   691 	iBatteryVoltageInfoIter.SetToFirst();
       
   692 	while((batteryVoltageInfoData = iBatteryVoltageInfoIter++) != NULL)
       
   693 		{
       
   694 		error = aReason;
       
   695 		if (error == KErrNone)
       
   696 			{
       
   697 			error = batteryVoltageInfoData->iRequestedMessage.Write(0,aDataPckg);
       
   698 			}
       
   699 		batteryVoltageInfoData->iRequestedMessage.Complete(error);
       
   700 		iBatteryVoltageInfoQue.Remove(*batteryVoltageInfoData);
       
   701 		
       
   702 		delete batteryVoltageInfoData;
       
   703 		}             
       
   704     
       
   705 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CompletePendingGetBatteryVoltageInfo - return" ) );
       
   706 	}
       
   707 
       
   708 // ---------------------------------------------------------
       
   709 // CHWRMPowerCommonData::CancelPendingGetBatteryInfo
       
   710 // Cancels and removes from the respective list any pending 
       
   711 // GetBatteryInfo requests.
       
   712 // ---------------------------------------------------------
       
   713 //
       
   714 void CHWRMPowerCommonData::CancelPendingGetBatteryInfo(CHWRMPowerService& aPowerService)
       
   715 	{
       
   716 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CancelPendingGetBatteryInfo()" ) );
       
   717 	
       
   718 	TBatteryInfoData* batteryInfoData;
       
   719 	
       
   720 	//inform everybody
       
   721 	iBatteryInfoIter.SetToFirst();
       
   722 	while((batteryInfoData = iBatteryInfoIter++) != NULL)
       
   723 		{
       
   724 		if ( &(batteryInfoData->iPowerClientService) == &aPowerService)
       
   725 			{
       
   726 			batteryInfoData->iRequestedMessage.Complete(KErrCancel);
       
   727 			iBatteryInfoQue.Remove(*batteryInfoData);
       
   728 			
       
   729 			delete batteryInfoData;
       
   730 			}
       
   731 		}             
       
   732     
       
   733 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CancelPendingGetBatteryInfo - return" ) );
       
   734 	}
       
   735 
       
   736 // ---------------------------------------------------------
       
   737 // CHWRMPowerCommonData::CancelPendingGetBatteryVoltageInfo
       
   738 // Cancels and removes from the respective list any pending 
       
   739 // GetBatteryVoltageInfo requests.
       
   740 // ---------------------------------------------------------
       
   741 //
       
   742 void CHWRMPowerCommonData::CancelPendingGetBatteryVoltageInfo(CHWRMPowerService& aPowerService)
       
   743 	{
       
   744 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CancelPendingGetBatteryVoltageInfo()" ) );
       
   745 	
       
   746 	TBatteryInfoData* batteryVoltageInfoData;
       
   747 	
       
   748 	//inform everybody
       
   749 	iBatteryVoltageInfoIter.SetToFirst();
       
   750 	while((batteryVoltageInfoData = iBatteryVoltageInfoIter++) != NULL)
       
   751 		{
       
   752 		if ( &(batteryVoltageInfoData->iPowerClientService) == &aPowerService)
       
   753 			{
       
   754 			batteryVoltageInfoData->iRequestedMessage.Complete(KErrCancel);
       
   755 			iBatteryVoltageInfoQue.Remove(*batteryVoltageInfoData);
       
   756 			
       
   757 			delete batteryVoltageInfoData;
       
   758 			}
       
   759 		}    
       
   760     
       
   761 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CancelPendingGetBatteryVoltageInfo - return" ) );
       
   762 	}
       
   763 #ifdef SYMBIAN_HWRM_EXTPOWERINFO
       
   764 // ---------------------------------------------------------
       
   765 // CHWRMPowerCommonData::DoGetChargerTypeL
       
   766 // Issues a plugin command to get charger type. If there is
       
   767 // an outstanding plugin request there isn't a new
       
   768 // one issued but the appropriate data are
       
   769 // stored in a queue. This data are used later to complete
       
   770 // the request.
       
   771 // ---------------------------------------------------------
       
   772 //
       
   773 void CHWRMPowerCommonData::DoGetChargerTypeL(const RMessage2& aMessage, const CHWRMPowerService& aPowerService)
       
   774 	{
       
   775 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoGetChargerTypeL()" ) );
       
   776 
       
   777 	TBatteryInfoData* batteryInfoData = new(ELeave) TBatteryInfoData(aPowerService, aMessage);
       
   778 	
       
   779 	if (iChargerTypeInfoQue.IsEmpty())
       
   780 		{
       
   781 		CleanupStack::PushL(batteryInfoData);
       
   782 		
       
   783 		RMessage2 dummy;
       
   784 	    iPowerService->GetChargerTypeL(dummy);
       
   785 	    
       
   786 		CleanupStack::Pop(batteryInfoData);
       
   787 		}
       
   788 	iChargerTypeInfoQue.AddFirst(*batteryInfoData);    
       
   789     
       
   790 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::DoGetChargerTypeL - return" ) );
       
   791 	}
       
   792 
       
   793 // ---------------------------------------------------------
       
   794 // CHWRMPowerCommonData::CompletePendingGetChargerType
       
   795 // Completes and removes from the respective list any pending 
       
   796 // GetChargerType requests.
       
   797 // ---------------------------------------------------------
       
   798 //
       
   799 void CHWRMPowerCommonData::CompletePendingGetChargerType(TInt aReason, TPckg<CHWRMPower::TBatteryChargerType> &aDataPckg)
       
   800 	{
       
   801 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CompletePendingGetChargerType()" ) );
       
   802 	
       
   803 	TBatteryInfoData* batteryInfoData;
       
   804 	TInt error;
       
   805 	
       
   806 	//inform everybody
       
   807 	iChargerTypeInfoIter.SetToFirst();
       
   808 	while((batteryInfoData = iChargerTypeInfoIter++) != NULL)
       
   809 		{
       
   810 		error = aReason;
       
   811 		if (error == KErrNone)
       
   812 			{
       
   813 			error = batteryInfoData->iRequestedMessage.Write(0,aDataPckg);
       
   814 			}
       
   815 		batteryInfoData->iRequestedMessage.Complete(error);
       
   816 		iChargerTypeInfoQue.Remove(*batteryInfoData);
       
   817 		
       
   818 		delete batteryInfoData;
       
   819 		}             
       
   820 	
       
   821 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CompletePendingGetChargerType - return" ) );
       
   822 	}
       
   823 
       
   824 // ---------------------------------------------------------
       
   825 // CHWRMPowerCommonData::CancelPendingGetChargerType
       
   826 // Cancels and removes from the respective list any pending 
       
   827 // GetChargerType requests.
       
   828 // ---------------------------------------------------------
       
   829 //
       
   830 void CHWRMPowerCommonData::CancelPendingGetChargerType(CHWRMPowerService& aPowerService)
       
   831 	{
       
   832 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CancelPendingGetChargerType()" ) );
       
   833 	
       
   834 	TBatteryInfoData* batteryInfoData;
       
   835 	
       
   836 	//inform everybody
       
   837 	iChargerTypeInfoIter.SetToFirst();
       
   838 	while((batteryInfoData = iChargerTypeInfoIter++) != NULL)
       
   839 		{
       
   840 		if ( &(batteryInfoData->iPowerClientService) == &aPowerService)
       
   841 			{
       
   842 			batteryInfoData->iRequestedMessage.Complete(KErrCancel);
       
   843 			iChargerTypeInfoQue.Remove(*batteryInfoData);
       
   844 			
       
   845 			delete batteryInfoData;
       
   846 			}
       
   847 		}             
       
   848     
       
   849 	COMPONENT_TRACE1(_L( "HWRM Server - CHWRMPowerCommonData::CancelPendingGetChargerType - return" ) );
       
   850 	}
       
   851 
       
   852 #endif //SYMBIAN_HWRM_EXTPOWERINFO
       
   853 
       
   854 // ---------------------------------------------------------
       
   855 // CClientsData::NewL
       
   856 // Two-phased constructor.
       
   857 // ---------------------------------------------------------
       
   858 //
       
   859 CClientsData* CClientsData::NewL(CHWRMGenericTimer& aMeasurementsTimer)
       
   860 	{
       
   861     COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::NewL(()" ) );
       
   862 
       
   863     CClientsData* self = new( ELeave ) CClientsData(aMeasurementsTimer);
       
   864     
       
   865     CleanupStack::PushL( self );
       
   866     self->ConstructL();
       
   867     CleanupStack::Pop();
       
   868 
       
   869     COMPONENT_TRACE2(_L( "HWRM Server - CClientsData::NewL( - return 0x%x" ), self );
       
   870     
       
   871     return self;
       
   872 	}
       
   873 
       
   874 // -----------------------------------------------------------------------------
       
   875 // CClientsData::CClientsData
       
   876 // C++ constructor
       
   877 // -----------------------------------------------------------------------------
       
   878 //
       
   879 CClientsData::CClientsData(CHWRMGenericTimer& aMeasurementsTimer)
       
   880 	:iMeasurementsTimer(aMeasurementsTimer),
       
   881 	iClientsQue(_FOFF(TClientData,iSglQueLink)),
       
   882 	iClientsQueIter(iClientsQue)
       
   883 	{
       
   884 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::CClientsData()" ) );
       
   885 	
       
   886 	iMeasuring = EFalse;
       
   887 	
       
   888 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::CClientsData - return" ) );
       
   889 	}
       
   890 
       
   891 // -----------------------------------------------------------------------------
       
   892 // CClientsData::ConstructL
       
   893 // Symbian 2nd phase constructor can leave.
       
   894 // -----------------------------------------------------------------------------
       
   895 //
       
   896 void CClientsData::ConstructL()
       
   897 	{    
       
   898     COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::ConstructL()" ) );
       
   899     
       
   900     COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::ConstructL - return" ) );
       
   901     }
       
   902 
       
   903 // -----------------------------------------------------------------------------
       
   904 // CClientsData::GenericTimerFired
       
   905 // Called when the generic timer expires or reaches the MaxPeriod.
       
   906 // Completes any pending client-sever messages and resets the 
       
   907 // data (Held by the service) of each client. The list is then emptied
       
   908 // -----------------------------------------------------------------------------
       
   909 //
       
   910 void CClientsData::MaxReportingPeriodReached(TInt aReason)
       
   911 	{
       
   912 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::MaxReportingPeriodReached()" ) );
       
   913 	
       
   914 	TClientData* clientData;
       
   915 	
       
   916 	//inform everybody
       
   917 	iClientsQueIter.SetToFirst();
       
   918 	while((clientData = iClientsQueIter++) != NULL)
       
   919 		{
       
   920 		clientData->iPowerMeasurementsClientData.CompleteGetMeasurementsMessage(aReason);				
       
   921 		clientData->iPowerMeasurementsClientData.Reset();
       
   922 		}
       
   923 	
       
   924 	//reset the measurements data
       
   925 	CleanUpMeasurementsData();
       
   926 	
       
   927 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::MaxReportingPeriodReached - return" ) );
       
   928 	}
       
   929 
       
   930 // ---------------------------------------------------------
       
   931 // Destructor
       
   932 // ---------------------------------------------------------
       
   933 //
       
   934 CClientsData::~CClientsData()
       
   935     {
       
   936     COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::~CClientsData()" ) );
       
   937     
       
   938     CleanUpMeasurementsData();
       
   939     
       
   940     COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::~CClientsData - return" ) );
       
   941     }
       
   942 
       
   943 // -----------------------------------------------------------------------------
       
   944 // CClientsData::EmptyClientsQue
       
   945 // Deletes all the items of the delts queue and empties it.
       
   946 // -----------------------------------------------------------------------------
       
   947 //
       
   948 void CClientsData::EmptyClientsQue()
       
   949 	{
       
   950 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::EmptyClientsQue()" ) );
       
   951 	
       
   952 	TClientData*  clientData;
       
   953 	
       
   954 	iClientsQueIter.SetToFirst();
       
   955 	
       
   956     while((clientData = iClientsQueIter++) != NULL)
       
   957         {
       
   958         iClientsQue.Remove(*clientData);
       
   959         delete clientData;
       
   960         }
       
   961     
       
   962     COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::EmptyClientsQue - return" ) );
       
   963 	}
       
   964 
       
   965 // -----------------------------------------------------------------------------
       
   966 // CClientsData::FindClient
       
   967 // Finds the clientthat correspond to the aPowerMeasurementsClientData.
       
   968 // -----------------------------------------------------------------------------
       
   969 //
       
   970 TClientData* CClientsData::FindClient(TPowerMeasurementsClientData* aPowerMeasurementsClientData)
       
   971 	{
       
   972 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::FindClient()" ) );
       
   973 	
       
   974     TBool found(EFalse);    
       
   975     TClientData* clientData;
       
   976 	
       
   977     iClientsQueIter.SetToFirst();
       
   978 
       
   979 	while( found == EFalse && ((clientData = iClientsQueIter++) != NULL))
       
   980         {
       
   981         if (&(clientData->iPowerMeasurementsClientData) == aPowerMeasurementsClientData)
       
   982         	{
       
   983         	found = ETrue;
       
   984         	}
       
   985         }
       
   986 	
       
   987 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::FindClient - return" ) );
       
   988 	
       
   989 	return clientData;
       
   990 	}
       
   991 
       
   992 // -----------------------------------------------------------------------------
       
   993 // CClientsData::RecalculatePluginInterval
       
   994 // Adjusts the interval used by the plugin when a new client requests 
       
   995 // measurement with a time interval that changes the GCD.
       
   996 // -----------------------------------------------------------------------------
       
   997 //
       
   998 void CClientsData::RecalculatePluginInterval(TUint &aIntervalMultiple)
       
   999 	{	
       
  1000 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::RecalculatePluginInterval()" ) );
       
  1001 	
       
  1002 	TClientData* clientData;
       
  1003 	
       
  1004 	iClientsQueIter.SetToFirst();
       
  1005 	
       
  1006 	clientData = iClientsQueIter++;
       
  1007 	TUint newGCD = clientData->iPowerMeasurementsClientData.iRequestedIntervalMultiple;
       
  1008 	
       
  1009 	while((clientData = iClientsQueIter++) != NULL)
       
  1010 		{
       
  1011 	    newGCD = CalculateGCD(clientData->iPowerMeasurementsClientData.iRequestedIntervalMultiple,newGCD);
       
  1012 		}
       
  1013 	aIntervalMultiple = newGCD;
       
  1014 	
       
  1015 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::RecalculatePluginInterval - return" ) );
       
  1016 	}		
       
  1017 
       
  1018 // -----------------------------------------------------------------------------
       
  1019 // CClientsData::GetPluginIntervalMultiple
       
  1020 // Getter for the plugin's interval multiple.
       
  1021 // -----------------------------------------------------------------------------
       
  1022 //
       
  1023 void CClientsData::GetPluginIntervalMultiple(TUint &aIntervalMultiple)
       
  1024 	{
       
  1025 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::GetPluginIntervalMultiple()" ) );
       
  1026 	
       
  1027 	aIntervalMultiple = iPluginIntervalMultiple;
       
  1028 	
       
  1029 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::GetPluginIntervalMultiple - return" ) );
       
  1030 	}
       
  1031 
       
  1032 // -----------------------------------------------------------------------------
       
  1033 // CClientsData::UpdatePluginIntervalMultiple
       
  1034 // Setter for the plugin's time interval.
       
  1035 // -----------------------------------------------------------------------------
       
  1036 //
       
  1037 void CClientsData::UpdatePluginIntervalMultiple(TUint aIntervalMultiple)
       
  1038 	{
       
  1039 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::UpdatePluginIntervalMultiple()" ) );
       
  1040 	
       
  1041 	iPluginIntervalMultiple = aIntervalMultiple;
       
  1042 	
       
  1043 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::UpdatePluginIntervalMultiple - return" ) );
       
  1044 	}
       
  1045  
       
  1046 // -----------------------------------------------------------------------------
       
  1047 // CClientsData::IsClientsQueEmpty
       
  1048 // Cheks if the Clients Queue is empty
       
  1049 // -----------------------------------------------------------------------------
       
  1050 // 
       
  1051 TBool CClientsData::IsClientsQueEmpty()
       
  1052 	{
       
  1053 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::IsClientsQueEmpty()" ) );
       
  1054 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::IsClientsQueEmpty - return" ) );
       
  1055 	
       
  1056 	return iClientsQue.IsEmpty();
       
  1057 	}
       
  1058 
       
  1059 // -----------------------------------------------------------------------------
       
  1060 // CClientsData::CalculateGCD
       
  1061 // Computes the Greatest Common Divisor of num1 and num2.
       
  1062 // -----------------------------------------------------------------------------
       
  1063 //
       
  1064 TUint CClientsData::CalculateGCD(TUint num1, TUint num2)
       
  1065 	{
       
  1066 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::CalculateGCD()" ) );
       
  1067 	
       
  1068     if (num2 == 0)
       
  1069     	{
       
  1070     	return num1;
       
  1071     	}
       
  1072     else 
       
  1073     	{
       
  1074     	return CalculateGCD(num2, num1 % num2);
       
  1075     	}
       
  1076  	}
       
  1077 
       
  1078 // -----------------------------------------------------------------------------
       
  1079 // CClientsData::AddMeasurementsClientL
       
  1080 // Creates and adds to the list a data structure containing all the 
       
  1081 // appropriate client data.
       
  1082 // -----------------------------------------------------------------------------
       
  1083 // 
       
  1084 void CClientsData::AddMeasurementsClientL(TPowerMeasurementsClientData& aPowerMeasurementsClientData)
       
  1085 	{ 	     
       
  1086 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::AddMeasurementsClientL()" ) );
       
  1087 	
       
  1088 	TClientData* clientData = new(ELeave) TClientData(aPowerMeasurementsClientData);
       
  1089 	
       
  1090 	iClientsQue.AddFirst(*clientData);
       
  1091 	
       
  1092 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::AddMeasurementsClientL - return" ) );
       
  1093  	}
       
  1094 
       
  1095 // -----------------------------------------------------------------------------
       
  1096 // CClientsData::RemoveMeasurementsClientData
       
  1097 // Deletes all the data relevant to the client whose PowerService holds
       
  1098 // aPowerMeasurementsClientData.
       
  1099 // -----------------------------------------------------------------------------
       
  1100 // 
       
  1101 void CClientsData::RemoveMeasurementsClientData(TPowerMeasurementsClientData* aPowerMeasurementsClientData)
       
  1102 	{	
       
  1103 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::RemoveMeasurementsClientData()" ) );
       
  1104 	
       
  1105     TClientData* clientData;
       
  1106     clientData = FindClient(aPowerMeasurementsClientData);
       
  1107      
       
  1108     if (clientData)
       
  1109     	{
       
  1110     	iClientsQue.Remove(*clientData);    	
       
  1111 	    delete clientData;
       
  1112 	    
       
  1113 	    if (iClientsQue.IsEmpty())
       
  1114 	    	{
       
  1115 	    	SetMeasuringOff();
       
  1116 	    	}
       
  1117     	}
       
  1118 	
       
  1119 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::RemoveMeasurementsClientData - return" ) );
       
  1120 	}
       
  1121 
       
  1122 // -----------------------------------------------------------------------------
       
  1123 // CClientsData::RemoveMeasurementsClientL
       
  1124 // Deletes all the data relevant to the client whose PowerService holds
       
  1125 // aPowerMeasurementsClientData. Leaves in the case the client is not found.
       
  1126 // -----------------------------------------------------------------------------
       
  1127 // 
       
  1128 void CClientsData::RemoveMeasurementsClientL(TPowerMeasurementsClientData* aPowerMeasurementsClientData)
       
  1129  	{
       
  1130 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::RemoveMeasurementsClientL()" ) );
       
  1131 	
       
  1132     TClientData* clientData;
       
  1133     clientData = FindClient(aPowerMeasurementsClientData);
       
  1134      
       
  1135     if (clientData == NULL)
       
  1136     	{
       
  1137     	User::Leave(KErrNotFound);
       
  1138     	}
       
  1139     
       
  1140     iClientsQue.Remove(*clientData);
       
  1141     delete clientData;
       
  1142     
       
  1143     if (iClientsQue.IsEmpty())
       
  1144     	{
       
  1145     	SetMeasuringOff();
       
  1146     	}
       
  1147 	
       
  1148 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::RemoveMeasurementsClientL - return" ) );
       
  1149 	}
       
  1150 
       
  1151 // -----------------------------------------------------------------------------
       
  1152 // CClientsData::NeedPluginIntervalAdjustment
       
  1153 // Checks if a adjustement is needed for the interval used by the plugin. 
       
  1154 // The Interval Multiple requested by the new client is passed
       
  1155 // -----------------------------------------------------------------------------
       
  1156 // 
       
  1157 
       
  1158 TBool CClientsData::NeedPluginIntervalAdjustment(TUint aIntervalMultiple)
       
  1159  	{
       
  1160 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::NeedPluginIntervalAdjustment()" ) );
       
  1161 	
       
  1162  	TBool needAdjustment; 	
       
  1163  	
       
  1164  	if (iClientsQue.IsEmpty())
       
  1165  		{
       
  1166  		needAdjustment =  EFalse; 		
       
  1167  		}
       
  1168  	else
       
  1169  		{
       
  1170 	 	TUint currentPluginIntervalMultiple;
       
  1171 	 	GetPluginIntervalMultiple(currentPluginIntervalMultiple);
       
  1172 	 	TUint currentGCD = currentPluginIntervalMultiple;
       
  1173 	 	TUint newInterval = aIntervalMultiple;
       
  1174 
       
  1175 	 	if( (newInterval % currentGCD) == 0)
       
  1176 	 		{
       
  1177 	 		needAdjustment = EFalse;
       
  1178 	 		}
       
  1179 	 	else
       
  1180 	 		{
       
  1181 	 		needAdjustment = ETrue;	
       
  1182 	 		}
       
  1183  		}
       
  1184  	
       
  1185 	COMPONENT_TRACE2(_L( "HWRM Server - CClientsData::NeedPluginIntervalAdjustment( - return 0x%x" ), needAdjustment );
       
  1186 	
       
  1187 	return needAdjustment;
       
  1188  	}
       
  1189 
       
  1190 // -----------------------------------------------------------------------------
       
  1191 // CClientsData::CleanUpMeasurementsData
       
  1192 // Deletes all the measurement related data. Used at destruction time.
       
  1193 // -----------------------------------------------------------------------------
       
  1194 // 
       
  1195 void CClientsData::CleanUpMeasurementsData()
       
  1196 	{
       
  1197 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::CleanUpMeasurementsData()" ) );
       
  1198 	
       
  1199 	EmptyClientsQue();
       
  1200 	
       
  1201 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::CleanUpMeasurementsData - return" ) );
       
  1202 	}
       
  1203 
       
  1204 // -----------------------------------------------------------------------------
       
  1205 // CClientsData::BatteryMeasurementIndicationL
       
  1206 // Handles a measurement callback indication from the plugin.
       
  1207 // -----------------------------------------------------------------------------
       
  1208 //
       
  1209 void CClientsData::BatteryMeasurementIndicationL(TDesC8& aValue)
       
  1210 	{		
       
  1211 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::BatteryMeasurementIndicationL()" ) );
       
  1212 	
       
  1213 	TClientData* clientData;
       
  1214 	
       
  1215     iClientsQueIter.SetToFirst();
       
  1216 
       
  1217 	while((clientData = iClientsQueIter++) != NULL)
       
  1218 		{
       
  1219 		clientData->iPowerMeasurementsClientData.NewBatteryMeasurementL(aValue, iPluginIntervalMultiple);
       
  1220 		}
       
  1221 	
       
  1222 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::BatteryMeasurementIndicationL - return" ) );
       
  1223 	}
       
  1224  
       
  1225 
       
  1226 // -----------------------------------------------------------------------------
       
  1227 // TPowerMeasurementsClientData::IsMeasuringOn
       
  1228 // Check if plugin measuring is on.
       
  1229 // -----------------------------------------------------------------------------
       
  1230 //
       
  1231 TBool CClientsData::IsMeasuringOn()
       
  1232 	{
       
  1233 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::IsMeasuringOn()" ) );
       
  1234 	
       
  1235 	COMPONENT_TRACE2(_L( "HWRM Server - CClientsData::IsMeasuringOn( - return 0x%x" ), iMeasuring );
       
  1236 	
       
  1237 	return iMeasuring;
       
  1238 	}
       
  1239 
       
  1240 // -----------------------------------------------------------------------------
       
  1241 // TPowerMeasurementsClientData::SetMeasuringOn
       
  1242 // Set the plugin measuring flag on.
       
  1243 // -----------------------------------------------------------------------------
       
  1244 //
       
  1245 void CClientsData::SetMeasuringOn()
       
  1246 	{
       
  1247 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::SetMeasuringOn()" ) );
       
  1248 	
       
  1249 	iMeasuring = ETrue;
       
  1250 	iMeasurementsTimer.Set(0);
       
  1251 	
       
  1252 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::SetMeasuringOn - return" ) );
       
  1253 	}
       
  1254 
       
  1255 // -----------------------------------------------------------------------------
       
  1256 // TPowerMeasurementsClientData::SetMeasuringOff
       
  1257 // Set the plugin measuring flag off.
       
  1258 // -----------------------------------------------------------------------------
       
  1259 //
       
  1260 void CClientsData::SetMeasuringOff()
       
  1261 	{
       
  1262 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::SetMeasuringOff()" ) );
       
  1263 	
       
  1264 	iMeasuring = EFalse;
       
  1265 	iMeasurementsTimer.Cancel();
       
  1266 	
       
  1267 	TClientData* clientData;
       
  1268 	iClientsQueIter.SetToFirst();
       
  1269 	while((clientData = iClientsQueIter++) != NULL)
       
  1270 		{
       
  1271 		clientData->iPowerMeasurementsClientData.SetMeasuringOff();
       
  1272 		}
       
  1273 	
       
  1274 	COMPONENT_TRACE1(_L( "HWRM Server - CClientsData::SetMeasuringOff - return" ) );
       
  1275 	}
       
  1276 
       
  1277 TClientData::TClientData(TPowerMeasurementsClientData& aPowerMeasurementsClientData)
       
  1278 	:iPowerMeasurementsClientData(aPowerMeasurementsClientData)
       
  1279 	{
       
  1280 	COMPONENT_TRACE1(_L( "HWRM Server - TClientData::TClientData()" ) );
       
  1281 	COMPONENT_TRACE1(_L( "HWRM Server - TClientData::TClientData - return" ) );
       
  1282 	}
       
  1283 	
       
  1284 TBatteryInfoData::TBatteryInfoData(const CHWRMPowerService& aPowerClientService, const RMessage2& aRequestedMessage)
       
  1285 	:iPowerClientService(aPowerClientService),
       
  1286 	iRequestedMessage(aRequestedMessage)
       
  1287 	{
       
  1288 	COMPONENT_TRACE1(_L( "HWRM Server - TBatteryInfoData::TBatteryInfoData()" ) );
       
  1289 	COMPONENT_TRACE1(_L( "HWRM Server - TBatteryInfoData::TBatteryInfoData - return" ) );
       
  1290 	}
       
  1291 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1292 
       
  1293 //  End of File