perfapps/perfmon/engine/src/perfmon_powerlistener.cpp
changeset 51 b048e15729d6
child 52 36d60d12b4af
equal deleted inserted replaced
44:5db69f4c3d06 51:b048e15729d6
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17  
       
    18 // INCLUDE FILES
       
    19 #include "perfmon_powerlistener.h"
       
    20 #include "../../symbian_version.hrh"
       
    21 
       
    22 #include <centralrepository.h>
       
    23 #include <hwrm/hwrmpowerdomaincrkeys.h>
       
    24 
       
    25 
       
    26 #if (SYMBIAN_VERSION_SUPPORT == SYMBIAN_3)
       
    27 
       
    28     CPerfMonPowerListener* CPerfMonPowerListener::NewL()
       
    29         {
       
    30         CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
       
    31         CleanupStack::PushL(self);
       
    32         self->ConstructL();
       
    33         CleanupStack::Pop();
       
    34         return self;
       
    35         }
       
    36 
       
    37     // --------------------------------------------------------------------------------------------
       
    38 
       
    39     CPerfMonPowerListener::CPerfMonPowerListener() :
       
    40         iHWRMPower(0),
       
    41         iLastPowerAvg(0),
       
    42         iMaxPower(0),
       
    43         iOriginalMaxReportingPeriod(0)
       
    44         {
       
    45         }
       
    46 
       
    47     // --------------------------------------------------------------------------------------------
       
    48 
       
    49     void CPerfMonPowerListener::ConstructL()
       
    50         {
       
    51         }
       
    52 
       
    53     // --------------------------------------------------------------------------------------------
       
    54 
       
    55     CPerfMonPowerListener::~CPerfMonPowerListener()
       
    56         {
       
    57         DeActivate();
       
    58         
       
    59         if (iHWRMPower != 0)
       
    60             {
       
    61             delete iHWRMPower;
       
    62             iHWRMPower = 0;
       
    63             }
       
    64         }
       
    65 
       
    66     // --------------------------------------------------------------------------------------------
       
    67 
       
    68     TBool CPerfMonPowerListener::IsSupported()
       
    69         {
       
    70         return ETrue;
       
    71         }
       
    72 
       
    73     // --------------------------------------------------------------------------------------------
       
    74 
       
    75     TInt CPerfMonPowerListener::Activate()
       
    76         {
       
    77         if (iHWRMPower == 0)
       
    78             {
       
    79             TRAPD(err, iHWRMPower = CHWRMPower::NewL());
       
    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             }
       
    94 
       
    95         // Set infinite reporting period
       
    96         TRAPD(err, SetReportingPeriodL(KReportingDuration));
       
    97         if (err != KErrNone)
       
    98             {
       
    99             return err;
       
   100             }
       
   101 
       
   102         TRequestStatus status(KRequestPending);
       
   103 
       
   104         // Start the power consumption notification
       
   105         iHWRMPower->StartAveragePowerReporting(status, KSampleIntervalMultiple);
       
   106         User::WaitForRequest(status);
       
   107 
       
   108         // Check if monitoring was succesfully started or already ongoing.
       
   109         if ((status.Int() != KErrNone) && (status.Int() != KErrAlreadyExists))
       
   110             {
       
   111             return status.Int();
       
   112             }
       
   113 
       
   114         return KErrNone;
       
   115         }
       
   116 
       
   117     // ---------------------------------------------------------------------------
       
   118 
       
   119     void CPerfMonPowerListener::DeActivate()
       
   120         {
       
   121         if (iHWRMPower != 0)
       
   122             {
       
   123             TRAP_IGNORE(iHWRMPower->StopAveragePowerReportingL());
       
   124             }
       
   125 
       
   126         // Restore original value to max sampling duration
       
   127         TRAP_IGNORE(SetReportingPeriodL(iOriginalMaxReportingPeriod));
       
   128 
       
   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 
       
   262 // End of File