--- a/perfapps/perfmon/engine/src/perfmon_powerlistener.cpp Fri Sep 17 08:29:47 2010 +0300
+++ b/perfapps/perfmon/engine/src/perfmon_powerlistener.cpp Mon Oct 04 00:18:03 2010 +0300
@@ -22,241 +22,279 @@
#include <centralrepository.h>
#include <hwrm/hwrmpowerdomaincrkeys.h>
-
-#if (SYMBIAN_VERSION_SUPPORT == SYMBIAN_3)
-
- CPerfMonPowerListener* CPerfMonPowerListener::NewL()
- {
- CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
- CleanupStack::PushL(self);
- self->ConstructL();
- CleanupStack::Pop();
- return self;
- }
-
- // --------------------------------------------------------------------------------------------
-
- CPerfMonPowerListener::CPerfMonPowerListener() :
- iHWRMPower(0),
- iLastPowerAvg(0),
- iMaxPower(0),
- iOriginalMaxReportingPeriod(0)
- {
- }
-
- // --------------------------------------------------------------------------------------------
+TBool CPerfMonPowerListener::iIsSupported = ETrue;
- void CPerfMonPowerListener::ConstructL()
- {
- }
-
- // --------------------------------------------------------------------------------------------
-
- CPerfMonPowerListener::~CPerfMonPowerListener()
- {
- DeActivate();
-
- if (iHWRMPower != 0)
- {
- delete iHWRMPower;
- iHWRMPower = 0;
- }
- }
-
- // --------------------------------------------------------------------------------------------
-
- TBool CPerfMonPowerListener::IsSupported()
- {
- return ETrue;
- }
-
- // --------------------------------------------------------------------------------------------
+/*
+ *
+ * class CPerfMonPowerListener implementation
+ *
+ */
- TInt CPerfMonPowerListener::Activate()
- {
- if (iHWRMPower == 0)
- {
- TRAPD(err, iHWRMPower = CHWRMPower::NewL());
- if (err != KErrNone)
- {
- return err;
- }
-
- // Callbacks to this object
- err = iHWRMPower->SetPowerReportObserver(this);
- if (err != KErrNone)
- {
- return err;
- }
-
- TRAP_IGNORE(GetReportingPeriodL());
- }
-
- // Set infinite reporting period
- TRAPD(err, SetReportingPeriodL(KReportingDuration));
- if (err != KErrNone)
- {
- return err;
- }
-
- TRequestStatus status(KRequestPending);
+CPerfMonPowerListener* CPerfMonPowerListener::NewL()
+ {
+ CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
+ CleanupStack::PushL(self);
+ self->ConstructL();
+ CleanupStack::Pop();
+ return self;
+ }
- // Start the power consumption notification
- iHWRMPower->StartAveragePowerReporting(status, KSampleIntervalMultiple);
- User::WaitForRequest(status);
-
- // Check if monitoring was succesfully started or already ongoing.
- if ((status.Int() != KErrNone) && (status.Int() != KErrAlreadyExists))
- {
- return status.Int();
- }
-
- return KErrNone;
- }
+// --------------------------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
+CPerfMonPowerListener::CPerfMonPowerListener() :
+ iPowerListenerImpl(NULL)
+ {
+ }
- void CPerfMonPowerListener::DeActivate()
- {
- if (iHWRMPower != 0)
- {
- TRAP_IGNORE(iHWRMPower->StopAveragePowerReportingL());
- }
-
- // Restore original value to max sampling duration
- TRAP_IGNORE(SetReportingPeriodL(iOriginalMaxReportingPeriod));
-
- iPowerBuffer.Reset();
- iLastPowerAvg = 0;
- }
+// --------------------------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
-
- TInt CPerfMonPowerListener::GetPower()
- {
- TInt avgPower = 0;
- TInt newValueCount = iPowerBuffer.Count();
-
- if (newValueCount > 0)
- {
- // Read all new values from buffer and calculate average from them.
- for (int i = 0; i < newValueCount; i++)
- {
- avgPower += iPowerBuffer[i];
- }
- avgPower = avgPower / newValueCount;
-
- iPowerBuffer.Reset();
- iLastPowerAvg = avgPower;
- }
- else
- {
- avgPower = iLastPowerAvg;
- }
-
- return avgPower;
- }
-
- // ---------------------------------------------------------------------------
-
- void CPerfMonPowerListener::PowerMeasurement(TInt aErrCode, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)
- {
- if (aErrCode == KErrNone)
- {
- // Store new value to buffer to wait for reading
- TInt value = aMeasurement.iAverageVoltage * aMeasurement.iAverageCurrent;
-
- // If charger is connected, reported values may be negative.
- if (value < 0)
- {
- value = 0;
- }
-
- iPowerBuffer.Append(value);
-
- if ( value > iMaxPower )
- {
- iMaxPower = value;
- }
- }
- // Ignore any errors
- }
-
- void CPerfMonPowerListener::GetReportingPeriodL()
+void CPerfMonPowerListener::ConstructL()
+ {
+ TRAPD(err, iPowerListenerImpl = CPerfMonPowerListenerImpl::NewL());
+ if (err != KErrNone)
{
- CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
-
- CleanupStack::PushL(cenRep);
- User::LeaveIfError(cenRep->Get(KPowerMaxReportingPeriod, iOriginalMaxReportingPeriod));
- CleanupStack::Pop();
+ iPowerListenerImpl = NULL;
- delete cenRep;
+ // Could not initialize power monitoring client. Feature support not available.
+ iIsSupported = EFalse;
}
-
- void CPerfMonPowerListener::SetReportingPeriodL(TInt aDuration)
- {
- CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
+ }
- CleanupStack::PushL(cenRep);
- User::LeaveIfError(cenRep->Set(KPowerMaxReportingPeriod, aDuration));
- CleanupStack::Pop();
-
- delete cenRep;
- }
+// --------------------------------------------------------------------------------------------
-// SYMBIAN_VERSION_SUPPORT < SYMBIAN_3
-#else
+CPerfMonPowerListener::~CPerfMonPowerListener()
+ {
+ DeActivate();
- // Stub implementation for older Symbian versions
-
- CPerfMonPowerListener* CPerfMonPowerListener::NewL()
+ if (iPowerListenerImpl)
{
- CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
- CleanupStack::PushL(self);
- self->ConstructL();
- CleanupStack::Pop();
- return self;
+ delete iPowerListenerImpl;
+ iPowerListenerImpl = 0;
}
+ }
- CPerfMonPowerListener::CPerfMonPowerListener() :
- iHWRMPower(0),
- iLastPowerAvg(0),
- iMaxPower(0)
- {
- }
+// --------------------------------------------------------------------------------------------
- void CPerfMonPowerListener::ConstructL()
+TInt CPerfMonPowerListener::Activate()
+ {
+ if (iPowerListenerImpl)
{
- }
-
- CPerfMonPowerListener::~CPerfMonPowerListener()
- {
- DeActivate();
+ return iPowerListenerImpl->Activate();
}
-
- TBool CPerfMonPowerListener::IsSupported()
- {
- return EFalse;
- }
-
- TInt CPerfMonPowerListener::Activate()
+ else
{
return KErrNotSupported;
}
+ }
- void CPerfMonPowerListener::DeActivate()
+// ---------------------------------------------------------------------------
+
+void CPerfMonPowerListener::DeActivate()
+ {
+ if (iPowerListenerImpl)
+ {
+ iPowerListenerImpl->DeActivate();
+ }
+ }
+
+// ---------------------------------------------------------------------------
+
+TInt CPerfMonPowerListener::GetPower()
+ {
+ if (iPowerListenerImpl)
{
+ return iPowerListenerImpl->GetPower();
}
+ else
+ {
+ return 0;
+ }
+ }
- TInt CPerfMonPowerListener::GetPower()
+// ---------------------------------------------------------------------------
+
+TInt CPerfMonPowerListener::GetMaxPower()
+ {
+ if (iPowerListenerImpl)
+ {
+ return iPowerListenerImpl->GetMaxPower();
+ }
+ else
{
return 0;
}
+ }
- void CPerfMonPowerListener::PowerMeasurement(TInt aErrCode, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)
+/*
+ *
+ * class CPerfMonPowerListenerImpl implementation
+ *
+ */
+
+CPerfMonPowerListenerImpl* CPerfMonPowerListenerImpl::NewL()
+ {
+ CPerfMonPowerListenerImpl* self = new (ELeave) CPerfMonPowerListenerImpl();
+ CleanupStack::PushL(self);
+ self->ConstructL();
+ CleanupStack::Pop();
+ return self;
+ }
+
+// --------------------------------------------------------------------------------------------
+
+CPerfMonPowerListenerImpl::CPerfMonPowerListenerImpl() :
+ iHWRMPower(NULL),
+ iLastPowerAvg(0),
+ iMaxPower(0),
+ iOriginalMaxReportingPeriod(0)
+ {
+ }
+
+// --------------------------------------------------------------------------------------------
+
+void CPerfMonPowerListenerImpl::ConstructL()
+ {
+ iHWRMPower = CHWRMPower::NewL();
+
+ // Callbacks to this object
+ User::LeaveIfError(iHWRMPower->SetPowerReportObserver(this));
+
+ iOriginalMaxReportingPeriod = GetReportingPeriodL();
+ }
+
+// --------------------------------------------------------------------------------------------
+
+CPerfMonPowerListenerImpl::~CPerfMonPowerListenerImpl()
+ {
+ DeActivate();
+
+ if (iHWRMPower)
{
+ delete iHWRMPower;
+ iHWRMPower = 0;
+ }
+ }
+
+// --------------------------------------------------------------------------------------------
+
+TInt CPerfMonPowerListenerImpl::Activate()
+ {
+ // Set infinite reporting period
+ TRAPD(err, SetReportingPeriodL(KHWRMReportingDuration));
+ if (err != KErrNone)
+ {
+ return err;
+ }
+
+ TRequestStatus status(KRequestPending);
+
+ // Start the power consumption notification
+ iHWRMPower->StartAveragePowerReporting(status, KHWRMSampleIntervalMultiple);
+ User::WaitForRequest(status);
+
+ // Check if monitoring was succesfully started or already ongoing.
+ if ((status.Int() != KErrNone) && (status.Int() != KErrAlreadyExists))
+ {
+ return status.Int();
+ }
+
+ return KErrNone;
+ }
+
+// ---------------------------------------------------------------------------
+
+void CPerfMonPowerListenerImpl::DeActivate()
+ {
+ if (iHWRMPower)
+ {
+ TRAP_IGNORE(iHWRMPower->StopAveragePowerReportingL());
}
-#endif
+ // Restore original value to max sampling duration
+ TRAP_IGNORE(SetReportingPeriodL(iOriginalMaxReportingPeriod));
+
+ iPowerBuffer.Reset();
+ iLastPowerAvg = 0;
+ }
+
+// ---------------------------------------------------------------------------
+
+TInt CPerfMonPowerListenerImpl::GetPower()
+ {
+ TInt avgPower = 0;
+ TInt newValueCount = iPowerBuffer.Count();
+
+ if (newValueCount > 0)
+ {
+ // Read all new values from buffer and calculate average from them.
+ for (int i = 0; i < newValueCount; i++)
+ {
+ avgPower += iPowerBuffer[i];
+ }
+ avgPower = avgPower / newValueCount;
+
+ iPowerBuffer.Reset();
+ iLastPowerAvg = avgPower;
+ }
+ // Use previous value if no new data has been reported since previous call.
+ // This may happen since perfmon sampling and power reporting are not synchronized.
+ else
+ {
+ avgPower = iLastPowerAvg;
+ }
+
+ return avgPower;
+ }
+
+// ---------------------------------------------------------------------------
+
+void CPerfMonPowerListenerImpl::PowerMeasurement(TInt aErrCode, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)
+ {
+ if (aErrCode == KErrNone)
+ {
+ // Calculate power value
+ TInt value = aMeasurement.iAverageVoltage * aMeasurement.iAverageCurrent;
+
+ // If charger is connected, reported values may be negative.
+ if (value < 0)
+ {
+ value = 0;
+ }
+
+ // Store new value to buffer to wait for reading
+ iPowerBuffer.Append(value);
+
+ if ( value > iMaxPower )
+ {
+ iMaxPower = value;
+ }
+ }
+ // Ignore any reports containing error information
+ }
+
+// ---------------------------------------------------------------------------
+
+TInt CPerfMonPowerListenerImpl::GetReportingPeriodL()
+ {
+ CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
+
+ CleanupStack::PushL(cenRep);
+ TInt reportingPeriod(0);
+ User::LeaveIfError(cenRep->Get(KPowerMaxReportingPeriod, reportingPeriod));
+ CleanupStack::PopAndDestroy();
+
+ return reportingPeriod;
+ }
+
+// ---------------------------------------------------------------------------
+
+void CPerfMonPowerListenerImpl::SetReportingPeriodL(TInt aDuration)
+ {
+ CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
+
+ CleanupStack::PushL(cenRep);
+ User::LeaveIfError(cenRep->Set(KPowerMaxReportingPeriod, aDuration));
+ CleanupStack::PopAndDestroy();
+ }
// End of File