perfapps/perfmon/engine/src/perfmon_powerlistener.cpp
changeset 52 36d60d12b4af
parent 51 b048e15729d6
equal deleted inserted replaced
51:b048e15729d6 52:36d60d12b4af
    20 #include "../../symbian_version.hrh"
    20 #include "../../symbian_version.hrh"
    21 
    21 
    22 #include <centralrepository.h>
    22 #include <centralrepository.h>
    23 #include <hwrm/hwrmpowerdomaincrkeys.h>
    23 #include <hwrm/hwrmpowerdomaincrkeys.h>
    24 
    24 
    25 
    25 TBool CPerfMonPowerListener::iIsSupported = ETrue;
    26 #if (SYMBIAN_VERSION_SUPPORT == SYMBIAN_3)
    26 
    27 
    27 /*
    28     CPerfMonPowerListener* CPerfMonPowerListener::NewL()
    28  *
    29         {
    29  * class CPerfMonPowerListener implementation
    30         CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
    30  * 
    31         CleanupStack::PushL(self);
    31  */
    32         self->ConstructL();
    32 
    33         CleanupStack::Pop();
    33 CPerfMonPowerListener* CPerfMonPowerListener::NewL()
    34         return self;
    34     {
    35         }
    35     CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
    36 
    36     CleanupStack::PushL(self);
    37     // --------------------------------------------------------------------------------------------
    37     self->ConstructL();
    38 
    38     CleanupStack::Pop();
    39     CPerfMonPowerListener::CPerfMonPowerListener() :
    39     return self;
    40         iHWRMPower(0),
    40     }
    41         iLastPowerAvg(0),
    41 
    42         iMaxPower(0),
    42 // --------------------------------------------------------------------------------------------
    43         iOriginalMaxReportingPeriod(0)
    43 
    44         {
    44 CPerfMonPowerListener::CPerfMonPowerListener() :
    45         }
    45     iPowerListenerImpl(NULL)
    46 
    46     {
    47     // --------------------------------------------------------------------------------------------
    47     }
    48 
    48 
    49     void CPerfMonPowerListener::ConstructL()
    49 // --------------------------------------------------------------------------------------------
    50         {
    50 
    51         }
    51 void CPerfMonPowerListener::ConstructL()
    52 
    52     {
    53     // --------------------------------------------------------------------------------------------
    53     TRAPD(err, iPowerListenerImpl = CPerfMonPowerListenerImpl::NewL());
    54 
    54     if (err != KErrNone)
    55     CPerfMonPowerListener::~CPerfMonPowerListener()
    55         {
    56         {
    56         iPowerListenerImpl = NULL;
    57         DeActivate();
    57 
    58         
    58         // Could not initialize power monitoring client. Feature support not available.
    59         if (iHWRMPower != 0)
    59         iIsSupported = EFalse;
       
    60         }
       
    61     }
       
    62 
       
    63 // --------------------------------------------------------------------------------------------
       
    64 
       
    65 CPerfMonPowerListener::~CPerfMonPowerListener()
       
    66     {
       
    67     DeActivate();
       
    68 
       
    69     if (iPowerListenerImpl)
       
    70         {
       
    71         delete iPowerListenerImpl;
       
    72         iPowerListenerImpl = 0;
       
    73         }
       
    74     }
       
    75 
       
    76 // --------------------------------------------------------------------------------------------
       
    77 
       
    78 TInt CPerfMonPowerListener::Activate()
       
    79     { 
       
    80     if (iPowerListenerImpl)
       
    81         {
       
    82         return iPowerListenerImpl->Activate();
       
    83         }
       
    84     else
       
    85         {
       
    86         return KErrNotSupported;
       
    87         }
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 
       
    92 void CPerfMonPowerListener::DeActivate()
       
    93     {
       
    94     if (iPowerListenerImpl)
       
    95         {
       
    96         iPowerListenerImpl->DeActivate();
       
    97         }
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------------------------
       
   101 
       
   102 TInt CPerfMonPowerListener::GetPower()
       
   103     {
       
   104     if (iPowerListenerImpl)
       
   105         {
       
   106         return iPowerListenerImpl->GetPower();
       
   107         }
       
   108     else
       
   109         {
       
   110         return 0;
       
   111         }
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 
       
   116 TInt CPerfMonPowerListener::GetMaxPower()
       
   117     {
       
   118     if (iPowerListenerImpl)
       
   119         {
       
   120         return iPowerListenerImpl->GetMaxPower();
       
   121         }
       
   122     else
       
   123         {
       
   124         return 0;
       
   125         }
       
   126     }
       
   127 
       
   128 /*
       
   129  *
       
   130  * class CPerfMonPowerListenerImpl implementation
       
   131  * 
       
   132  */
       
   133 
       
   134 CPerfMonPowerListenerImpl* CPerfMonPowerListenerImpl::NewL()
       
   135     {
       
   136     CPerfMonPowerListenerImpl* self = new (ELeave) CPerfMonPowerListenerImpl();
       
   137     CleanupStack::PushL(self);
       
   138     self->ConstructL();
       
   139     CleanupStack::Pop();
       
   140     return self;
       
   141     }
       
   142 
       
   143 // --------------------------------------------------------------------------------------------
       
   144 
       
   145 CPerfMonPowerListenerImpl::CPerfMonPowerListenerImpl() :
       
   146     iHWRMPower(NULL),
       
   147     iLastPowerAvg(0),
       
   148     iMaxPower(0),
       
   149     iOriginalMaxReportingPeriod(0)
       
   150     {
       
   151     }
       
   152 
       
   153 // --------------------------------------------------------------------------------------------
       
   154 
       
   155 void CPerfMonPowerListenerImpl::ConstructL()
       
   156     {
       
   157     iHWRMPower = CHWRMPower::NewL();
       
   158 
       
   159     // Callbacks to this object
       
   160     User::LeaveIfError(iHWRMPower->SetPowerReportObserver(this));
       
   161 
       
   162     iOriginalMaxReportingPeriod = GetReportingPeriodL();
       
   163     }
       
   164 
       
   165 // --------------------------------------------------------------------------------------------
       
   166 
       
   167 CPerfMonPowerListenerImpl::~CPerfMonPowerListenerImpl()
       
   168     {
       
   169     DeActivate();
       
   170 
       
   171     if (iHWRMPower)
       
   172         {
       
   173         delete iHWRMPower;
       
   174         iHWRMPower = 0;
       
   175         }
       
   176     }
       
   177 
       
   178 // --------------------------------------------------------------------------------------------
       
   179 
       
   180 TInt CPerfMonPowerListenerImpl::Activate()
       
   181     {
       
   182     // Set infinite reporting period
       
   183     TRAPD(err, SetReportingPeriodL(KHWRMReportingDuration));
       
   184     if (err != KErrNone)
       
   185         {
       
   186         return err;
       
   187         }
       
   188 
       
   189     TRequestStatus status(KRequestPending);
       
   190 
       
   191     // Start the power consumption notification
       
   192     iHWRMPower->StartAveragePowerReporting(status, KHWRMSampleIntervalMultiple);
       
   193     User::WaitForRequest(status);
       
   194 
       
   195     // Check if monitoring was succesfully started or already ongoing.
       
   196     if ((status.Int() != KErrNone) && (status.Int() != KErrAlreadyExists))
       
   197         {
       
   198         return status.Int();
       
   199         }
       
   200 
       
   201     return KErrNone;
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 
       
   206 void CPerfMonPowerListenerImpl::DeActivate()
       
   207     {
       
   208     if (iHWRMPower)
       
   209         {
       
   210         TRAP_IGNORE(iHWRMPower->StopAveragePowerReportingL());
       
   211         }
       
   212 
       
   213     // Restore original value to max sampling duration
       
   214     TRAP_IGNORE(SetReportingPeriodL(iOriginalMaxReportingPeriod));
       
   215 
       
   216     iPowerBuffer.Reset();
       
   217     iLastPowerAvg = 0;
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------------------------
       
   221 
       
   222 TInt CPerfMonPowerListenerImpl::GetPower()
       
   223     {
       
   224     TInt avgPower = 0;
       
   225     TInt newValueCount = iPowerBuffer.Count();
       
   226 
       
   227     if (newValueCount > 0)
       
   228         {
       
   229         // Read all new values from buffer and calculate average from them.
       
   230         for (int i = 0; i < newValueCount; i++)
    60             {
   231             {
    61             delete iHWRMPower;
   232             avgPower += iPowerBuffer[i];
    62             iHWRMPower = 0;
       
    63             }
   233             }
    64         }
   234         avgPower = avgPower / newValueCount;
    65 
   235 
    66     // --------------------------------------------------------------------------------------------
   236         iPowerBuffer.Reset();
    67 
   237         iLastPowerAvg = avgPower;
    68     TBool CPerfMonPowerListener::IsSupported()
   238         }
    69         {
   239     // Use previous value if no new data has been reported since previous call.
    70         return ETrue;
   240     // This may happen since perfmon sampling and power reporting are not synchronized.
    71         }
   241     else
    72 
   242         {
    73     // --------------------------------------------------------------------------------------------
   243         avgPower = iLastPowerAvg;
    74 
   244         }
    75     TInt CPerfMonPowerListener::Activate()
   245 
    76         {
   246     return avgPower;
    77         if (iHWRMPower == 0)
   247     }
       
   248 
       
   249 // ---------------------------------------------------------------------------
       
   250 
       
   251 void CPerfMonPowerListenerImpl::PowerMeasurement(TInt aErrCode, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)
       
   252     {
       
   253     if (aErrCode == KErrNone)
       
   254         {
       
   255         // Calculate power value
       
   256         TInt value = aMeasurement.iAverageVoltage * aMeasurement.iAverageCurrent;
       
   257 
       
   258         // If charger is connected, reported values may be negative.
       
   259         if (value < 0)
    78             {
   260             {
    79             TRAPD(err, iHWRMPower = CHWRMPower::NewL());
   261             value = 0;
    80             if (err != KErrNone)
       
    81                 {
       
    82                 return err;
       
    83                 }
       
    84 
       
    85             // Callbacks to this object
       
    86             err = iHWRMPower->SetPowerReportObserver(this);
       
    87             if (err != KErrNone)
       
    88                 {
       
    89                 return err;
       
    90                 }
       
    91             
       
    92             TRAP_IGNORE(GetReportingPeriodL());
       
    93             }
   262             }
    94 
   263 
    95         // Set infinite reporting period
   264         // Store new value to buffer to wait for reading
    96         TRAPD(err, SetReportingPeriodL(KReportingDuration));
   265         iPowerBuffer.Append(value);
    97         if (err != KErrNone)
   266 
       
   267         if ( value > iMaxPower )
    98             {
   268             {
    99             return err;
   269             iMaxPower = value;
   100             }
   270             }
   101 
   271         }
   102         TRequestStatus status(KRequestPending);
   272     // Ignore any reports containing error information
   103 
   273     }
   104         // Start the power consumption notification
   274 
   105         iHWRMPower->StartAveragePowerReporting(status, KSampleIntervalMultiple);
   275 // ---------------------------------------------------------------------------
   106         User::WaitForRequest(status);
   276 
   107 
   277 TInt CPerfMonPowerListenerImpl::GetReportingPeriodL()
   108         // Check if monitoring was succesfully started or already ongoing.
   278     {
   109         if ((status.Int() != KErrNone) && (status.Int() != KErrAlreadyExists))
   279     CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
   110             {
   280 
   111             return status.Int();
   281     CleanupStack::PushL(cenRep);
   112             }
   282     TInt reportingPeriod(0);
   113 
   283     User::LeaveIfError(cenRep->Get(KPowerMaxReportingPeriod, reportingPeriod));
   114         return KErrNone;
   284     CleanupStack::PopAndDestroy();
   115         }
   285 
   116 
   286     return reportingPeriod;
   117     // ---------------------------------------------------------------------------
   287     }
   118 
   288 
   119     void CPerfMonPowerListener::DeActivate()
   289 // ---------------------------------------------------------------------------
   120         {
   290 
   121         if (iHWRMPower != 0)
   291 void CPerfMonPowerListenerImpl::SetReportingPeriodL(TInt aDuration)
   122             {
   292     {
   123             TRAP_IGNORE(iHWRMPower->StopAveragePowerReportingL());
   293     CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
   124             }
   294 
   125 
   295     CleanupStack::PushL(cenRep);
   126         // Restore original value to max sampling duration
   296     User::LeaveIfError(cenRep->Set(KPowerMaxReportingPeriod, aDuration));
   127         TRAP_IGNORE(SetReportingPeriodL(iOriginalMaxReportingPeriod));
   297     CleanupStack::PopAndDestroy();
   128 
   298     }
   129         iPowerBuffer.Reset();
       
   130         iLastPowerAvg = 0;
       
   131         }
       
   132 
       
   133     // ---------------------------------------------------------------------------
       
   134 
       
   135     TInt CPerfMonPowerListener::GetPower()
       
   136         {
       
   137         TInt avgPower = 0;
       
   138         TInt newValueCount = iPowerBuffer.Count();
       
   139 
       
   140         if (newValueCount > 0)
       
   141             {
       
   142             // Read all new values from buffer and calculate average from them.
       
   143             for (int i = 0; i < newValueCount; i++)
       
   144                 {
       
   145                 avgPower += iPowerBuffer[i];
       
   146                 }
       
   147             avgPower = avgPower / newValueCount;
       
   148 
       
   149             iPowerBuffer.Reset();
       
   150             iLastPowerAvg = avgPower;
       
   151             }
       
   152         else
       
   153             {
       
   154             avgPower = iLastPowerAvg;
       
   155             }
       
   156 
       
   157         return avgPower;
       
   158         }
       
   159 
       
   160     // ---------------------------------------------------------------------------
       
   161 
       
   162     void CPerfMonPowerListener::PowerMeasurement(TInt aErrCode, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)
       
   163         {
       
   164         if (aErrCode == KErrNone)
       
   165             {
       
   166             // Store new value to buffer to wait for reading
       
   167             TInt value = aMeasurement.iAverageVoltage * aMeasurement.iAverageCurrent;
       
   168 
       
   169             // If charger is connected, reported values may be negative.
       
   170             if (value < 0)
       
   171                 {
       
   172                 value = 0;
       
   173                 }
       
   174 
       
   175             iPowerBuffer.Append(value);
       
   176 
       
   177             if ( value > iMaxPower )
       
   178                 {
       
   179                 iMaxPower = value;
       
   180                 }
       
   181             }
       
   182         // Ignore any errors
       
   183         }
       
   184 
       
   185     void CPerfMonPowerListener::GetReportingPeriodL()
       
   186         {
       
   187         CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
       
   188 
       
   189         CleanupStack::PushL(cenRep);
       
   190         User::LeaveIfError(cenRep->Get(KPowerMaxReportingPeriod, iOriginalMaxReportingPeriod));
       
   191         CleanupStack::Pop();
       
   192 
       
   193         delete cenRep;
       
   194         }
       
   195 
       
   196     void CPerfMonPowerListener::SetReportingPeriodL(TInt aDuration)
       
   197         {
       
   198         CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
       
   199 
       
   200         CleanupStack::PushL(cenRep);
       
   201         User::LeaveIfError(cenRep->Set(KPowerMaxReportingPeriod, aDuration));
       
   202         CleanupStack::Pop();
       
   203         
       
   204         delete cenRep;
       
   205         }
       
   206 
       
   207 // SYMBIAN_VERSION_SUPPORT < SYMBIAN_3
       
   208 #else
       
   209 
       
   210     // Stub implementation for older Symbian versions
       
   211 
       
   212     CPerfMonPowerListener* CPerfMonPowerListener::NewL()
       
   213         {
       
   214         CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
       
   215         CleanupStack::PushL(self);
       
   216         self->ConstructL();
       
   217         CleanupStack::Pop();
       
   218         return self;
       
   219         }
       
   220 
       
   221     CPerfMonPowerListener::CPerfMonPowerListener() :
       
   222         iHWRMPower(0),
       
   223         iLastPowerAvg(0),
       
   224         iMaxPower(0)
       
   225         {
       
   226         }
       
   227 
       
   228     void CPerfMonPowerListener::ConstructL()
       
   229         {
       
   230         }
       
   231 
       
   232     CPerfMonPowerListener::~CPerfMonPowerListener()
       
   233         {
       
   234         DeActivate();
       
   235         }
       
   236 
       
   237     TBool CPerfMonPowerListener::IsSupported()
       
   238         {
       
   239         return EFalse;
       
   240         }
       
   241 
       
   242     TInt CPerfMonPowerListener::Activate()
       
   243         {
       
   244         return KErrNotSupported;
       
   245         }
       
   246 
       
   247     void CPerfMonPowerListener::DeActivate()
       
   248         {
       
   249         }
       
   250 
       
   251     TInt CPerfMonPowerListener::GetPower()
       
   252         {
       
   253         return 0;
       
   254         }
       
   255 
       
   256     void CPerfMonPowerListener::PowerMeasurement(TInt aErrCode, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)
       
   257         {
       
   258         }
       
   259 
       
   260 #endif
       
   261 
   299 
   262 // End of File
   300 // End of File