51
|
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 |
|
52
|
25 |
TBool CPerfMonPowerListener::iIsSupported = ETrue;
|
51
|
26 |
|
52
|
27 |
/*
|
|
28 |
*
|
|
29 |
* class CPerfMonPowerListener implementation
|
|
30 |
*
|
|
31 |
*/
|
51
|
32 |
|
52
|
33 |
CPerfMonPowerListener* CPerfMonPowerListener::NewL()
|
|
34 |
{
|
|
35 |
CPerfMonPowerListener* self = new (ELeave) CPerfMonPowerListener();
|
|
36 |
CleanupStack::PushL(self);
|
|
37 |
self->ConstructL();
|
|
38 |
CleanupStack::Pop();
|
|
39 |
return self;
|
|
40 |
}
|
51
|
41 |
|
52
|
42 |
// --------------------------------------------------------------------------------------------
|
51
|
43 |
|
52
|
44 |
CPerfMonPowerListener::CPerfMonPowerListener() :
|
|
45 |
iPowerListenerImpl(NULL)
|
|
46 |
{
|
|
47 |
}
|
51
|
48 |
|
52
|
49 |
// --------------------------------------------------------------------------------------------
|
51
|
50 |
|
52
|
51 |
void CPerfMonPowerListener::ConstructL()
|
|
52 |
{
|
|
53 |
TRAPD(err, iPowerListenerImpl = CPerfMonPowerListenerImpl::NewL());
|
|
54 |
if (err != KErrNone)
|
51
|
55 |
{
|
52
|
56 |
iPowerListenerImpl = NULL;
|
51
|
57 |
|
52
|
58 |
// Could not initialize power monitoring client. Feature support not available.
|
|
59 |
iIsSupported = EFalse;
|
51
|
60 |
}
|
52
|
61 |
}
|
51
|
62 |
|
52
|
63 |
// --------------------------------------------------------------------------------------------
|
51
|
64 |
|
52
|
65 |
CPerfMonPowerListener::~CPerfMonPowerListener()
|
|
66 |
{
|
|
67 |
DeActivate();
|
51
|
68 |
|
52
|
69 |
if (iPowerListenerImpl)
|
51
|
70 |
{
|
52
|
71 |
delete iPowerListenerImpl;
|
|
72 |
iPowerListenerImpl = 0;
|
51
|
73 |
}
|
52
|
74 |
}
|
51
|
75 |
|
52
|
76 |
// --------------------------------------------------------------------------------------------
|
51
|
77 |
|
52
|
78 |
TInt CPerfMonPowerListener::Activate()
|
|
79 |
{
|
|
80 |
if (iPowerListenerImpl)
|
51
|
81 |
{
|
52
|
82 |
return iPowerListenerImpl->Activate();
|
51
|
83 |
}
|
52
|
84 |
else
|
51
|
85 |
{
|
|
86 |
return KErrNotSupported;
|
|
87 |
}
|
52
|
88 |
}
|
51
|
89 |
|
52
|
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)
|
51
|
105 |
{
|
52
|
106 |
return iPowerListenerImpl->GetPower();
|
51
|
107 |
}
|
52
|
108 |
else
|
|
109 |
{
|
|
110 |
return 0;
|
|
111 |
}
|
|
112 |
}
|
51
|
113 |
|
52
|
114 |
// ---------------------------------------------------------------------------
|
|
115 |
|
|
116 |
TInt CPerfMonPowerListener::GetMaxPower()
|
|
117 |
{
|
|
118 |
if (iPowerListenerImpl)
|
|
119 |
{
|
|
120 |
return iPowerListenerImpl->GetMaxPower();
|
|
121 |
}
|
|
122 |
else
|
51
|
123 |
{
|
|
124 |
return 0;
|
|
125 |
}
|
52
|
126 |
}
|
51
|
127 |
|
52
|
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)
|
51
|
172 |
{
|
52
|
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());
|
51
|
211 |
}
|
|
212 |
|
52
|
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++)
|
|
231 |
{
|
|
232 |
avgPower += iPowerBuffer[i];
|
|
233 |
}
|
|
234 |
avgPower = avgPower / newValueCount;
|
|
235 |
|
|
236 |
iPowerBuffer.Reset();
|
|
237 |
iLastPowerAvg = avgPower;
|
|
238 |
}
|
|
239 |
// Use previous value if no new data has been reported since previous call.
|
|
240 |
// This may happen since perfmon sampling and power reporting are not synchronized.
|
|
241 |
else
|
|
242 |
{
|
|
243 |
avgPower = iLastPowerAvg;
|
|
244 |
}
|
|
245 |
|
|
246 |
return avgPower;
|
|
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)
|
|
260 |
{
|
|
261 |
value = 0;
|
|
262 |
}
|
|
263 |
|
|
264 |
// Store new value to buffer to wait for reading
|
|
265 |
iPowerBuffer.Append(value);
|
|
266 |
|
|
267 |
if ( value > iMaxPower )
|
|
268 |
{
|
|
269 |
iMaxPower = value;
|
|
270 |
}
|
|
271 |
}
|
|
272 |
// Ignore any reports containing error information
|
|
273 |
}
|
|
274 |
|
|
275 |
// ---------------------------------------------------------------------------
|
|
276 |
|
|
277 |
TInt CPerfMonPowerListenerImpl::GetReportingPeriodL()
|
|
278 |
{
|
|
279 |
CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
|
|
280 |
|
|
281 |
CleanupStack::PushL(cenRep);
|
|
282 |
TInt reportingPeriod(0);
|
|
283 |
User::LeaveIfError(cenRep->Get(KPowerMaxReportingPeriod, reportingPeriod));
|
|
284 |
CleanupStack::PopAndDestroy();
|
|
285 |
|
|
286 |
return reportingPeriod;
|
|
287 |
}
|
|
288 |
|
|
289 |
// ---------------------------------------------------------------------------
|
|
290 |
|
|
291 |
void CPerfMonPowerListenerImpl::SetReportingPeriodL(TInt aDuration)
|
|
292 |
{
|
|
293 |
CRepository* cenRep = CRepository::NewL(KCRUidPowerSettings);
|
|
294 |
|
|
295 |
CleanupStack::PushL(cenRep);
|
|
296 |
User::LeaveIfError(cenRep->Set(KPowerMaxReportingPeriod, aDuration));
|
|
297 |
CleanupStack::PopAndDestroy();
|
|
298 |
}
|
51
|
299 |
|
|
300 |
// End of File
|