Debian Codegraph @

src/systeminfo/windows/qbatteryinfo_win.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Copyright (C) 2018 BlackBerry Limited. All rights reserved.
5 ** Contact: http://www.qt-project.org/legal
6 **
7 ** This file is part of the QtSystems module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** GNU Lesser General Public License Usage
19 ** Alternatively, this file may be used under the terms of the GNU Lesser
20 ** General Public License version 3 as published by the Free Software
21 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
22 ** packaging of this file. Please review the following information to
23 ** ensure the GNU Lesser General Public License version 3 requirements
24 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25 **
26 ** GNU General Public License Usage
27 ** Alternatively, this file may be used under the terms of the GNU
28 ** General Public License version 2.0 or (at your option) the GNU General
29 ** Public license version 3 or any later version approved by the KDE Free
30 ** Qt Foundation. The licenses are as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32 ** included in the packaging of this file. Please review the following
33 ** information to ensure the GNU General Public License requirements will
34 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35 ** https://www.gnu.org/licenses/gpl-3.0.html.
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "qbatteryinfo_win_p.h"
42
43 #include <qt_windows.h>
44
45 #include <powrprof.h>
46 #include <setupapi.h>
47
48 #if !defined (Q_CC_MINGW) || defined(__MINGW64_VERSION_MAJOR)
49 # include <batclass.h>
50 #endif
51
52 #include <QtCore/qmetaobject.h>
53 #include <QtCore/QTimer>
54 #include <QtCore/QUuid>
55 #include <QtCore/qnumeric.h>
56
57 #ifdef Q_CC_MSVC
58 # pragma comment (lib, "Setupapi.lib")
59 #endif
60
61 QT_BEGIN_NAMESPACE
62
63 QBatteryInfoPrivate::QBatteryInfoPrivate(QBatteryInfo *parent)
64 : QObject(parent)
65 , q_ptr(parent)
66 , timeToFull(0)
67 , numberOfBatteries(0)
68 , index(0)
69 {
70 initialize();
71 }
72
73 QBatteryInfoPrivate::QBatteryInfoPrivate(int batteryIndex, QBatteryInfo *parent)
74 : QObject(parent)
75 , q_ptr(parent)
76 , timeToFull(0)
77 , numberOfBatteries(0)
78 , index(batteryIndex)
79 {
80 initialize();
81 }
82
83 void QBatteryInfoPrivate::initialize()
84 {
85 QTimer *timer = new QTimer(this);
86 connect(timer,SIGNAL(timeout()),this,SLOT(getBatteryStatus()));
87 timer->start(1000);
88 getBatteryStatus();
89 }
90
91 QBatteryInfoPrivate::~QBatteryInfoPrivate()
92 {
93 }
94
95 int QBatteryInfoPrivate::batteryCount()
96 {
97 return numberOfBatteries;
98 }
99
100 int QBatteryInfoPrivate::batteryIndex() const
101 {
102 return index;
103 }
104
105 bool QBatteryInfoPrivate::isValid()
106 {
107 // valid if the index < total count.
108 return (index >= 0) && (index < batteryCount());
109 }
110
111 void QBatteryInfoPrivate::setBatteryIndex(int batteryIndex)
112 {
113 if (index != batteryIndex) {
114 bool validBefore = isValid();
115 int oldIndex = index;
116 index = batteryIndex;
117 bool validNow = isValid();
118 if (validBefore != validNow)
119 Q_EMIT validChanged(validNow);
120
121 if (validNow) {
122 if (validBefore) {
123 // valid now, valid before so we have to check individual values
124
125 // ignore chargerType - it won't change based on battery index
126 //emit chargerTypeChanged(newChargerType);
127
128 QBatteryInfo::ChargingState newChargingState = chargingState();
129 if (newChargingState != chargingState(oldIndex))
130 emit chargingStateChanged(newChargingState);
131
132 int newValue = level();
133 if (newValue != level(oldIndex))
134 emit levelChanged(newValue);
135
136 newValue = currentFlow();
137 if (newValue != currentFlow(oldIndex))
138 emit currentFlowChanged(newValue);
139
140 newValue = cycleCount();
141 if (newValue != cycleCount(oldIndex))
142 emit cycleCountChanged(newValue);
143
144 newValue = remainingCapacity();
145 if (newValue != remainingCapacity(oldIndex))
146 emit remainingCapacityChanged(newValue);
147
148 newValue = remainingChargingTime();
149 if (newValue != remainingChargingTime(oldIndex))
150 emit remainingChargingTimeChanged(newValue);
151
152 newValue = voltage();
153 if (newValue != voltage(oldIndex))
154 emit voltageChanged(newValue);
155
156 QBatteryInfo::LevelStatus newLevelStatus = levelStatus();
157 if (newLevelStatus != levelStatus(oldIndex))
158 emit levelStatusChanged(newLevelStatus);
159
160 QBatteryInfo::Health newHealth = health();
161 if (newHealth != health(oldIndex))
162 emit healthChanged(newHealth);
163
164 float newTemperature = temperature();
165 if (!qFuzzyCompare(newTemperature, temperature(oldIndex)))
166 emit temperatureChanged(newTemperature);
167 } else {
168 // it wasn't valid before so everything is changed
169
170 // ignore chargerType - it won't change based on battery index
171 //emit chargerTypeChanged(newChargerType);
172
173 emit chargingStateChanged(chargingState());
174 emit levelChanged(level());
175 emit currentFlowChanged(currentFlow());
176 emit cycleCountChanged(cycleCount());
177 emit remainingCapacityChanged(remainingCapacity());
178 emit remainingChargingTimeChanged(remainingChargingTime());
179 emit voltageChanged(voltage());
180 emit levelStatusChanged(levelStatus());
181 emit healthChanged(health());
182 emit temperatureChanged(temperature());
183 }
184 }
185
186 emit batteryIndexChanged(index);
187 }
188 }
189
190 int QBatteryInfoPrivate::level(int battery)
191 {
192 int maxCapacity = maximumCapacity(battery);
193 int remCapacity = remainingCapacity(battery);
194
195 if (maxCapacity == 0)
196 return -1;
197
198 return remCapacity * 100 / maxCapacity;
199 }
200
201 int QBatteryInfoPrivate::level()
202 {
203 return level(index);
204 }
205
206 int QBatteryInfoPrivate::currentFlow(int battery)
207 {
208 return currentFlows[battery];
209 }
210
211 int QBatteryInfoPrivate::currentFlow()
212 {
213 return currentFlow(index);
214 }
215
216 int QBatteryInfoPrivate::cycleCount(int battery)
217 {
218 Q_UNUSED(battery)
219
220 return -1;
221 }
222
223 int QBatteryInfoPrivate::cycleCount()
224 {
225 return cycleCount(index);
226 }
227
228 int QBatteryInfoPrivate::maximumCapacity(int battery)
229 {
230 return maximumCapacities[battery];
231 }
232
233 int QBatteryInfoPrivate::maximumCapacity()
234 {
235 return maximumCapacity(index);
236 }
237
238 int QBatteryInfoPrivate::remainingCapacity(int battery)
239 {
240 return remainingCapacities[battery];
241 }
242
243 int QBatteryInfoPrivate::remainingCapacity()
244 {
245 return remainingCapacity(index);
246 }
247
248 int QBatteryInfoPrivate::remainingChargingTime(int battery)
249 {
250 Q_UNUSED(battery)
251 SYSTEM_BATTERY_STATE systemBatteryState;
252 CallNtPowerInformation(SystemBatteryState,NULL,0,&systemBatteryState,sizeof(systemBatteryState));
253
254 int cTime = systemBatteryState.EstimatedTime;
255 if (cTime != timeToFull) {
256 timeToFull = cTime;
257 emit remainingChargingTimeChanged(timeToFull);
258 }
259
260 return timeToFull;
261 }
262
263 int QBatteryInfoPrivate::remainingChargingTime()
264 {
265 return remainingChargingTime(index);
266 }
267
268 int QBatteryInfoPrivate::voltage(int battery)
269 {
270 return voltages[battery];
271 }
272
273 int QBatteryInfoPrivate::voltage()
274 {
275 return voltage(index);
276 }
277
278 QBatteryInfo::ChargerType QBatteryInfoPrivate::chargerType()
279 {
280 return currentChargerType;
281 }
282
283 QBatteryInfo::ChargingState QBatteryInfoPrivate::chargingState(int battery)
284 {
285 return chargingStates[battery];
286 }
287
288 QBatteryInfo::ChargingState QBatteryInfoPrivate::chargingState()
289 {
290 return chargingState(index);
291 }
292
293 QBatteryInfo::LevelStatus QBatteryInfoPrivate::levelStatus(int battery)
294 {
295 return levelStatuss[battery];
296 }
297
298 QBatteryInfo::LevelStatus QBatteryInfoPrivate::levelStatus()
299 {
300 return levelStatus(index);
301 }
302
303 QBatteryInfo::Health QBatteryInfoPrivate::health(int battery)
304 {
305 Q_UNUSED(battery)
306
307 return QBatteryInfo::HealthUnknown;
308 }
309
310 QBatteryInfo::Health QBatteryInfoPrivate::health()
311 {
312 return health(index);
313 }
314
315 float QBatteryInfoPrivate::temperature(int battery)
316 {
317 Q_UNUSED(battery)
318
319 return qQNaN();
320 }
321
322 float QBatteryInfoPrivate::temperature()
323 {
324 return temperature(index);
325 }
326
327 void QBatteryInfoPrivate::getBatteryStatus()
328 {
329 #if !defined (Q_CC_MINGW) || defined(__MINGW64_VERSION_MAJOR)
330 SYSTEM_BATTERY_STATE systemBatteryState;
331 CallNtPowerInformation(SystemBatteryState,NULL,0,&systemBatteryState,sizeof(systemBatteryState));
332
333 int cTime = systemBatteryState.EstimatedTime;
334 if (cTime != timeToFull) {
335 timeToFull = cTime;
336 emit remainingChargingTimeChanged(timeToFull);
337 }
338
339 int batteryNumber = 0;
340
341 QUuid guidDeviceBattery(0x72631e54L,0x78A4,0x11d0,0xbc,0xf7,0x00,0xaa,0x00,0xb7,0xb3,0x2a);
342 GUID GUID_DEVICE_BATTERY = static_cast<GUID>(guidDeviceBattery);
343
344 HDEVINFO hdevInfo = SetupDiGetClassDevs(&GUID_DEVICE_BATTERY,0,0,DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
345 if (INVALID_HANDLE_VALUE != hdevInfo) {
346 for (int i = 0; i < 100; i++) {
347 SP_DEVICE_INTERFACE_DATA deviceInterfaceData = {0};
348 deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
349
350 if (SetupDiEnumDeviceInterfaces(hdevInfo,0,&GUID_DEVICE_BATTERY,i,&deviceInterfaceData)){
351 DWORD cbRequired = 0;
352
353 SetupDiGetDeviceInterfaceDetail(hdevInfo, &deviceInterfaceData,0, 0, &cbRequired, 0);
354 if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) {
355 PSP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetail =
356 (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LPTR, cbRequired);
357 if (deviceInterfaceDetail){
358 deviceInterfaceDetail->cbSize = sizeof(*deviceInterfaceDetail);
359 if (SetupDiGetDeviceInterfaceDetail(hdevInfo, &deviceInterfaceData, deviceInterfaceDetail, cbRequired, &cbRequired, 0)) {
360
361 batteryNumber++; //new battery
362 HANDLE batteryHandle = CreateFile(deviceInterfaceDetail->DevicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
363 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
364 if (INVALID_HANDLE_VALUE != batteryHandle){
365
366 BATTERY_QUERY_INFORMATION batteryQueryInfo = {0};
367
368 DWORD inBuf = 0;
369 DWORD dwOut;
370
371 if (DeviceIoControl(batteryHandle,IOCTL_BATTERY_QUERY_TAG, &inBuf, sizeof(inBuf), &batteryQueryInfo.BatteryTag, sizeof(batteryQueryInfo.BatteryTag), &dwOut, NULL)
372 && batteryQueryInfo.BatteryTag) {
373
374 BATTERY_INFORMATION batteryInfo = {0};
375 batteryQueryInfo.InformationLevel = BatteryInformation;
376
377 if (DeviceIoControl(batteryHandle, IOCTL_BATTERY_QUERY_INFORMATION,
378 &batteryQueryInfo, sizeof(batteryQueryInfo),
379 &batteryInfo, sizeof(batteryInfo), &dwOut, NULL)) {
380
381 maximumCapacities.insert(batteryNumber, -1);
382
383 if (batteryInfo.Capabilities & BATTERY_SYSTEM_BATTERY) {
384 if (!(batteryInfo.Capabilities & BATTERY_IS_SHORT_TERM)) {
385 // dwResult |= HASBATTERY;
386
387 BATTERY_WAIT_STATUS batteryWaitStatus = {0};
388 batteryWaitStatus.BatteryTag = batteryQueryInfo.BatteryTag;
389
390 BATTERY_STATUS batteryStatus;
391 if (DeviceIoControl(batteryHandle, IOCTL_BATTERY_QUERY_STATUS,
392 &batteryWaitStatus, sizeof(batteryWaitStatus),
393 &batteryStatus, sizeof(batteryStatus), &dwOut, NULL)) {
394
395 QBatteryInfo::ChargerType chargerType = QBatteryInfo::UnknownCharger;
396
397 if (batteryStatus.PowerState & BATTERY_POWER_ON_LINE) {
398 chargerType = QBatteryInfo::WallCharger;
399 }
400 if (currentChargerType != chargerType) {
401 currentChargerType = chargerType;
402 Q_EMIT chargerTypeChanged(chargerType);
403 }
404
405 QBatteryInfo::ChargingState chargingState;
406 if (batteryStatus.PowerState & BATTERY_CHARGING)
407 chargingState = QBatteryInfo::Charging;
408 if (batteryStatus.PowerState & BATTERY_DISCHARGING)
409 chargingState = QBatteryInfo::Discharging;
410
411 if (chargingStates[batteryNumber] != chargingState) {
412 chargingStates.insert(batteryNumber, chargingState);
413 if (batteryNumber == index)
414 emit chargingStateChanged(chargingState);
415 }
416
417 if (voltages[batteryNumber] != batteryStatus.Voltage) {
418 voltages.insert(batteryNumber, batteryStatus.Voltage);
419 if (batteryNumber == index)
420 Q_EMIT voltageChanged(batteryStatus.Voltage);
421 }
422 if (currentFlows[batteryNumber] != batteryStatus.Rate) {
423 currentFlows.insert(batteryNumber,batteryStatus.Rate);
424 if (batteryNumber == index)
425 Q_EMIT currentFlowChanged(batteryStatus.Rate);
426 }
427 if (batteryStatus.Voltage == BATTERY_UNKNOWN_VOLTAGE) {
428 // If we don't have the voltage then we can't convert from mWh to mAh
429 batteryStatus.Capacity = -1;
430 maximumCapacities.insert(batteryNumber, -1);
431 } else {
432 // Convert from mWh to mAh
433 batteryStatus.Capacity = batteryStatus.Capacity / batteryStatus.Voltage;
434 maximumCapacities.insert(batteryNumber, batteryInfo.FullChargedCapacity / batteryStatus.Voltage);
435 }
436 if (remainingCapacities[batteryNumber] != batteryStatus.Capacity) {
437 remainingCapacities.insert(batteryNumber, batteryStatus.Capacity);
438 if (batteryNumber == index)
439 Q_EMIT remainingCapacityChanged(batteryStatus.Capacity);
440 }
441 ///
442 int level = batteryInfo.FullChargedCapacity / batteryStatus.Capacity;
443 QBatteryInfo::LevelStatus batStatus = QBatteryInfo::LevelUnknown;
444
445 if (batteryStatus.PowerState & BATTERY_CRITICAL) {
446 batStatus =QBatteryInfo::LevelEmpty ;
447 } else if (level < 67) {
448 batStatus = QBatteryInfo::LevelLow;
449 } else if (level > 66) {
450 batStatus = QBatteryInfo::LevelOk;
451 } else if (level == 100) {
452 batStatus = QBatteryInfo::LevelFull;
453 }
454
455 if (levelStatuss[batteryNumber] != batStatus) {
456 levelStatuss.insert(batteryNumber,batStatus);
457 if (batteryNumber == index)
458 Q_EMIT levelStatusChanged(batStatus);
459 }
460 }
461 }
462 }
463 } //end IOCTL_BATTERY_QUERY_INFORMATION
464 } // end BATTERY_INFORMATION
465
466 CloseHandle(batteryHandle);
467 }
468 }
469 LocalFree(deviceInterfaceDetail);
470 }
471 }
472 }
473 else if (ERROR_NO_MORE_ITEMS == GetLastError()) {
474 break;
475 }
476 }
477 SetupDiDestroyDeviceInfoList(hdevInfo);
478 }
479 bool validBefore = isValid();
480 numberOfBatteries = batteryNumber;
481 bool validNow = isValid();
482 if (validBefore != validNow)
483 Q_EMIT validChanged(validNow);
484
485 #else // !defined (Q_CC_MINGW) || defined(__MINGW64_VERSION_MAJOR)
486 numberOfBatteries = 0;
487 Q_UNIMPLEMENTED();
488 #endif
489 }
490
491 QT_END_NAMESPACE