Debian Codegraph @

src/systeminfo/linux/qbatteryinfo_upower.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_upower_p.h"
42
43 #include <QtCore/qdir.h>
44 #include <QtCore/qfile.h>
45 #include <QtCore/qmetaobject.h>
46 #include <QtCore/qtimer.h>
47 #include <QtCore/qnumeric.h>
48 #include <QFile>
49 #include "qdevicekitservice_linux_p.h"
50
51
52 QT_BEGIN_NAMESPACE
53
54 QBatteryInfoPrivate::QBatteryInfoPrivate(QBatteryInfo *parent)
55 : QObject(parent),
56 cType(QBatteryInfo::UnknownCharger),
57 cState(QBatteryInfo::UnknownChargingState),
58 q_ptr(parent),
59 index(0)
60 {
61 initialize();
62 }
63
64 QBatteryInfoPrivate::QBatteryInfoPrivate(int batteryIndex, QBatteryInfo *parent)
65 : QObject(parent),
66 cType(QBatteryInfo::UnknownCharger),
67 cState(QBatteryInfo::UnknownChargingState),
68 q_ptr(parent),
69 index(batteryIndex)
70 {
71 initialize();
72 }
73
74 void QBatteryInfoPrivate::initialize()
75 {
76 watcher = new QDBusServiceWatcher(QStringLiteral("org.freedesktop.UPower"),QDBusConnection::systemBus(),
77 QDBusServiceWatcher::WatchForRegistration |
78 QDBusServiceWatcher::WatchForUnregistration, this);
79 connect(watcher, SIGNAL(serviceRegistered(QString)),
80 this, SLOT(connectToUpower()));
81 connect(watcher, SIGNAL(serviceUnregistered(QString)),
82 this, SLOT(disconnectFromUpower()));
83
84 bool uPowerAvailable = QDBusConnection::systemBus().interface()->isServiceRegistered(QStringLiteral("org.freedesktop.UPower"));
85
86 if (uPowerAvailable)
87 connectToUpower();
88 }
89
90 QBatteryInfoPrivate::~QBatteryInfoPrivate()
91 {
92 }
93
94 void QBatteryInfoPrivate::connectToUpower()
95 {
96 getBatteryStats();
97 }
98
99 void QBatteryInfoPrivate::disconnectFromUpower()
100 {
101 }
102
103 int QBatteryInfoPrivate::batteryCount()
104 {
105 return batteryMap.count();
106 }
107
108 int QBatteryInfoPrivate::batteryIndex() const
109 {
110 return index;
111 }
112
113 bool QBatteryInfoPrivate::isValid()
114 {
115 // valid if the index < total count.
116 return (index >= 0) && (index < batteryCount());
117 }
118
119 void QBatteryInfoPrivate::setBatteryIndex(int batteryIndex)
120 {
121 if (index != batteryIndex) {
122 bool validBefore = isValid();
123 int oldIndex = index;
124 index = batteryIndex;
125 bool validNow = isValid();
126 if (validBefore != validNow)
127 Q_EMIT validChanged(validNow);
128
129 if (validNow) {
130 if (validBefore) {
131 // valid now, valid before so we have to check individual values
132
133 // ignore chargerType - it won't change based on battery index
134 //emit chargerTypeChanged(newChargerType);
135
136 QBatteryInfo::ChargingState newChargingState = chargingState();
137 if (newChargingState != chargingState(oldIndex))
138 emit chargingStateChanged(newChargingState);
139
140 int newValue = level();
141 if (newValue != level(oldIndex))
142 emit levelChanged(newValue);
143
144 newValue = currentFlow();
145 if (newValue != currentFlow(oldIndex))
146 emit currentFlowChanged(newValue);
147
148 newValue = cycleCount();
149 if (newValue != cycleCount(oldIndex))
150 emit cycleCountChanged(newValue);
151
152 newValue = remainingCapacity();
153 if (newValue != remainingCapacity(oldIndex))
154 emit remainingCapacityChanged(newValue);
155
156 newValue = remainingChargingTime();
157 if (newValue != remainingChargingTime(oldIndex))
158 emit remainingChargingTimeChanged(newValue);
159
160 newValue = voltage();
161 if (newValue != voltage(oldIndex))
162 emit voltageChanged(newValue);
163
164 QBatteryInfo::LevelStatus newLevelStatus = levelStatus();
165 if (newLevelStatus != levelStatus(oldIndex))
166 emit levelStatusChanged(newLevelStatus);
167
168 QBatteryInfo::Health newHealth = health();
169 if (newHealth != health(oldIndex))
170 emit healthChanged(newHealth);
171
172 float newTemperature = temperature();
173 if (!qFuzzyCompare(newTemperature, temperature(oldIndex)))
174 emit temperatureChanged(newTemperature);
175 } else {
176 // it wasn't valid before so everything is changed
177
178 // ignore chargerType - it won't change based on battery index
179 //emit chargerTypeChanged(newChargerType);
180
181 emit chargingStateChanged(chargingState());
182 emit levelChanged(level());
183 emit currentFlowChanged(currentFlow());
184 emit cycleCountChanged(cycleCount());
185 emit remainingCapacityChanged(remainingCapacity());
186 emit remainingChargingTimeChanged(remainingChargingTime());
187 emit voltageChanged(voltage());
188 emit levelStatusChanged(levelStatus());
189 emit healthChanged(health());
190 emit temperatureChanged(temperature());
191 }
192 }
193
194 emit batteryIndexChanged(index);
195 }
196 }
197
198 int QBatteryInfoPrivate::level(int battery)
199 {
200 int maxCapacity = maximumCapacity(battery);
201 int remCapacity = remainingCapacity(battery);
202
203 if (maxCapacity == 0)
204 return -1;
205
206 return remCapacity * 100 / maxCapacity;
207 }
208
209 int QBatteryInfoPrivate::level()
210 {
211 return level(index);
212 }
213
214 int QBatteryInfoPrivate::currentFlow(int battery)
215 {
216 if (batteryMap.count() >= battery)
217 return (batteryMap.value(battery).value(QStringLiteral("EnergyRate")).toDouble()
218 / (batteryMap.value(battery).value(QStringLiteral("Voltage")).toDouble()) * 1000);
219 else
220 return 0;
221 }
222
223 int QBatteryInfoPrivate::currentFlow()
224 {
225 return currentFlow(index);
226 }
227
228 int QBatteryInfoPrivate::cycleCount(int battery)
229 {
230 Q_UNUSED(battery)
231
232 return -1;
233 }
234
235 int QBatteryInfoPrivate::cycleCount()
236 {
237 return cycleCount(index);
238 }
239
240 int QBatteryInfoPrivate::maximumCapacity(int battery)
241 {
242 if (batteryMap.count() >= battery)
243 return batteryMap.value(battery).value(QStringLiteral("EnergyFull")).toDouble() * 1000;
244 else
245 return 0;
246 }
247
248 int QBatteryInfoPrivate::maximumCapacity()
249 {
250 return maximumCapacity(index);
251 }
252
253 int QBatteryInfoPrivate::remainingCapacity(int battery)
254 {
255 if (batteryMap.count() >= battery)
256 return batteryMap.value(battery).value(QStringLiteral("Energy")).toDouble() * 1000;
257 else
258 return 0;
259 }
260
261 int QBatteryInfoPrivate::remainingCapacity()
262 {
263 return remainingCapacity(index);
264 }
265
266 int QBatteryInfoPrivate::remainingChargingTime(int battery)
267 {
268 if (batteryMap.count() >= battery)
269 return batteryMap.value(battery).value(QStringLiteral("TimeToFull")).toInt();
270 else
271 return 0;
272 }
273
274 int QBatteryInfoPrivate::remainingChargingTime()
275 {
276 return remainingChargingTime(index);
277 }
278
279 int QBatteryInfoPrivate::voltage(int battery)
280 {
281 if (batteryMap.count() >= battery)
282 return (batteryMap.value(battery).value(QStringLiteral("Voltage")).toDouble() * 1000);
283 else
284 return 0;
285 }
286
287 int QBatteryInfoPrivate::voltage()
288 {
289 return voltage(index);
290 }
291
292 QBatteryInfo::ChargerType QBatteryInfoPrivate::chargerType()
293 {
294 return cType;
295 }
296
297 QBatteryInfo::ChargingState QBatteryInfoPrivate::chargingState(int battery)
298 {
299 Q_UNUSED(battery)
300 return cState;
301 }
302
303 QBatteryInfo::ChargingState QBatteryInfoPrivate::chargingState()
304 {
305 return chargingState(index);
306 }
307
308 QBatteryInfo::LevelStatus QBatteryInfoPrivate::levelStatus(int battery)
309 {
310 QBatteryInfo::LevelStatus stat = QBatteryInfo::LevelUnknown;
311
312 if (batteryMap.count() >= battery) {
313 int level = batteryMap.value(battery).value(QStringLiteral("Percentage")).toInt();
314 if (level < 3)
315 stat = QBatteryInfo::LevelEmpty;
316 else if (level < 11)
317 stat = QBatteryInfo::LevelLow;
318 else if (level < 99)
319 stat = QBatteryInfo::LevelOk;
320 else
321 stat = QBatteryInfo::LevelFull;
322 }
323 return stat;
324 }
325
326 QBatteryInfo::LevelStatus QBatteryInfoPrivate::levelStatus()
327 {
328 return levelStatus(index);
329 }
330
331 QBatteryInfo::Health QBatteryInfoPrivate::health(int battery)
332 {
333 QBatteryInfo::Health health = QBatteryInfo::HealthUnknown;
334 if (batteryMap.count() >= battery) {
335 int percent = (batteryMap.value(battery).value(QStringLiteral("EnergyFull")).toInt() *100)
336 / (float)( batteryMap.value(battery).value(QStringLiteral("EnergyFullDesign")).toInt());
337 if (percent < 65)
338 health = QBatteryInfo::HealthBad;
339 else
340 health = QBatteryInfo::HealthOk;
341 }
342 return health;
343 }
344
345 QBatteryInfo::Health QBatteryInfoPrivate::health()
346 {
347 return health(index);
348 }
349
350 float QBatteryInfoPrivate::temperature(int battery)
351 {
352 Q_UNUSED(battery)
353
354 return qQNaN();
355 }
356
357 float QBatteryInfoPrivate::temperature()
358 {
359 return temperature(index);
360 }
361
362 void QBatteryInfoPrivate::uPowerBatteryPropertyChanged(const QString &prop, const QVariant &v)
363 {
364 QUPowerDeviceInterface *uPowerDevice = qobject_cast<QUPowerDeviceInterface*>(sender());
365 int foundBattery = 0;
366
367 if (uPowerDevice->type() == 2) {
368 QMapIterator<int, QVariantMap> i(batteryMap);
369 while (i.hasNext()) {
370 i.next();
371 if (i.value().value(QStringLiteral("NativePath")).toString() == uPowerDevice->nativePath()) {
372 foundBattery = i.key();
373 break;
374 }
375 }
376
377 QVariantMap foundMap = batteryMap.value(foundBattery);
378 foundMap.insert(prop,v);
379 batteryMap.insert(foundBattery,foundMap);
380 }
381
382 if (prop == QLatin1String("Energy")) {
383 if (foundBattery == index)
384 Q_EMIT remainingCapacityChanged(v.toDouble() * 1000);
385
386 } else if (prop == QLatin1String("EnergyRate")) {
387 if (foundBattery == index)
388 Q_EMIT currentFlowChanged(v.toDouble() / (uPowerDevice->voltage() * 1000));
389
390 } else if (prop == QLatin1String("Percentage")) {
391 int level = v.toInt();
392
393 QBatteryInfo::LevelStatus stat = QBatteryInfo::LevelUnknown;
394
395 if (level < 3)
396 stat = QBatteryInfo::LevelEmpty;
397 else if (level < 11)
398 stat = QBatteryInfo::LevelLow;
399 else if (level < 99)
400 stat = QBatteryInfo::LevelOk;
401 else
402 stat = QBatteryInfo::LevelFull;
403
404 if (foundBattery == index)
405 Q_EMIT levelStatusChanged(stat);
406
407 } else if (prop == QLatin1String("Voltage")) {
408 if (foundBattery == index)
409 Q_EMIT voltageChanged(v.toDouble() * 1000 );
410
411 } else if (prop == QLatin1String("State")) {
412
413 QBatteryInfo::ChargingState curChargeState = getCurrentChargingState(v.toInt());
414
415 if (curChargeState != cState) {
416 cState = curChargeState;
417 if (foundBattery == index)
418 Q_EMIT chargingStateChanged(curChargeState);
419 }
420 } else if (prop == QLatin1String("Capacity")) {
421 QBatteryInfo::Health newHealth = health(index);
422 if (newHealth != healthList.value(index)) {
423 healthList.insert(index,newHealth);
424 Q_EMIT healthChanged(newHealth);
425 }
426
427 } else if (prop == QLatin1String("TimeToFull")) {
428 if (foundBattery == index)
429 Q_EMIT remainingChargingTimeChanged(v.toInt());
430 } else if (prop == QLatin1String("Online")) {
431 QBatteryInfo::ChargerType curCharger = QBatteryInfo::UnknownCharger;
432 if (v.toBool()) {
433 curCharger = getChargerType(uPowerDevice->nativePath());
434 }
435 if (curCharger != cType) {
436 cType = curCharger;
437 Q_EMIT chargerTypeChanged(cType);
438 }
439 }
440 }
441
442 QBatteryInfo::ChargerType QBatteryInfoPrivate::getChargerType(const QString &path)
443 {
444 QBatteryInfo::ChargerType chargerType = QBatteryInfo::UnknownCharger;
445 QFile charger;
446 charger.setFileName(QStringLiteral("/sys/class/power_supply/") + path + QStringLiteral("/type"));
447 if (charger.open(QIODevice::ReadOnly)) {
448 QString line = QString::fromLocal8Bit(charger.readAll().simplified());
449 if (line == QStringLiteral("USB")) {
450 chargerType = QBatteryInfo::USBCharger;
451 } else if (line == QStringLiteral("Mains")) {
452 chargerType = QBatteryInfo::WallCharger;
453 }
454 }
455 charger.close();
456 return chargerType;
457 }
458
459 QBatteryInfo::ChargingState QBatteryInfoPrivate::getCurrentChargingState(int state)
460 {
461 QBatteryInfo::ChargingState curChargeState = QBatteryInfo::UnknownChargingState;
462 switch (state) {
463 case 1: // charging
464 {
465 curChargeState = QBatteryInfo::Charging;
466 }
467 break;
468 case 2: //discharging
469 case 3: //empty
470 curChargeState = QBatteryInfo::Discharging;
471 break;
472 case 4: //fully charged
473 curChargeState = QBatteryInfo::IdleChargingState;
474 break;
475 case 5: //pending charge
476 case 6: //pending discharge
477 break;
478 default:
479 curChargeState = QBatteryInfo::UnknownChargingState;
480 break;
481 };
482
483 return curChargeState;
484 }
485
486 void QBatteryInfoPrivate::getBatteryStats()
487 {
488 batteryMap.clear();
489 QUPowerInterface *power;
490 power = new QUPowerInterface(this);
491
492 connect(power,SIGNAL(deviceAdded(QString)),
493 this,SLOT(deviceAdded(QString)));
494 connect(power,SIGNAL(deviceRemoved(QString)),
495 this,SLOT(deviceRemoved(QString)));
496
497 foreach (const QDBusObjectPath &objpath, power->enumerateDevices()) {
498 QUPowerDeviceInterface *uPowerDevice;
499 uPowerDevice = new QUPowerDeviceInterface(objpath.path(),this);
500 connect(uPowerDevice,SIGNAL(propertyChanged(QString,QVariant)),
501 this,SLOT(uPowerBatteryPropertyChanged(QString,QVariant)));
502
503
504 QMapIterator<QString, QVariant> i(uPowerDevice->getProperties());
505 while (i.hasNext()) {
506 i.next();
507 QString prop = i.key();
508 QVariant v = i.value();
509
510 int foundBattery = 0;
511 if (uPowerDevice->type() == 2) {
512
513 QMapIterator<int, QVariantMap> i(batteryMap);
514 while (i.hasNext()) {
515 i.next();
516 if (i.value().value(QStringLiteral("NativePath")).toString() == uPowerDevice->nativePath()) {
517 foundBattery = i.key();
518 break;
519 }
520 }
521
522 QVariantMap foundMap = batteryMap.value(foundBattery);
523 foundMap.insert(prop,v);
524 batteryMap.insert(foundBattery,foundMap);
525 }
526
527 if (prop == QLatin1String("Energy")) {
528 if (foundBattery == index)
529 Q_EMIT remainingCapacityChanged(v.toDouble() * 1000);
530 } else if (prop == QLatin1String("EnergyFullDesign")) {
531
532 } else if (prop == QLatin1String("EnergyRate")) {
533 if (foundBattery == index)
534 Q_EMIT currentFlowChanged(v.toDouble() / (uPowerDevice->voltage() * 1000));
535
536 } else if (prop == QLatin1String("Percentage")) {
537 int level = v.toInt();
538
539 QBatteryInfo::LevelStatus stat = QBatteryInfo::LevelUnknown;
540
541 if (level < 3)
542 stat = QBatteryInfo::LevelEmpty;
543 else if (level < 11)
544 stat = QBatteryInfo::LevelLow;
545 else if (level < 99)
546 stat = QBatteryInfo::LevelOk;
547 else
548 stat = QBatteryInfo::LevelFull;
549
550 if (foundBattery == index)
551 Q_EMIT levelStatusChanged(stat);
552
553 } else if (prop == QLatin1String("Voltage")) {
554 if (foundBattery == index)
555 Q_EMIT voltageChanged(v.toDouble() * 1000 );
556
557 } else if (prop == QLatin1String("State")) {
558
559 QBatteryInfo::ChargingState curChargeState = getCurrentChargingState(v.toInt());
560
561 if (curChargeState != cState) {
562 cState = curChargeState;
563 if (foundBattery == index)
564 Q_EMIT chargingStateChanged(curChargeState);
565 }
566
567 } else if (prop == QLatin1String("Capacity")) {
568
569 QBatteryInfo::Health newHealth = health(index);
570 healthList.insert(index,newHealth);
571 Q_EMIT healthChanged(newHealth);
572
573 } else if (prop == QLatin1String("TimeToFull")) {
574 Q_EMIT remainingChargingTimeChanged(v.toInt());
575 } else if (prop == QLatin1String("Online")) {
576 QBatteryInfo::ChargerType curCharger = QBatteryInfo::UnknownCharger;
577 if (v.toBool()) {
578 curCharger = getChargerType(uPowerDevice->nativePath());
579 if (curCharger != cType) {
580 cType = curCharger;
581 Q_EMIT chargerTypeChanged(cType);
582 }
583 }
584 }
585 }
586 }
587 }
588
589 void QBatteryInfoPrivate::deviceAdded(const QString &path)
590 {
591 QUPowerDeviceInterface *battery;
592 battery = new QUPowerDeviceInterface(path,this);
593 int batteryNumber = batteryCount();
594 connect(battery,SIGNAL(propertyChanged(QString,QVariant)),
595 this,SLOT(uPowerBatteryPropertyChanged(QString,QVariant)));
596
597 if (battery->type() == 2) {
598 batteryMap.insert(++batteryNumber,battery->getProperties());
599 }
600 }
601
602 void QBatteryInfoPrivate::deviceRemoved(const QString &path)
603 {
604 QUPowerDeviceInterface *battery;
605 battery = new QUPowerDeviceInterface(path,this);
606
607 int foundBattery = 0;
608 QMapIterator<int, QVariantMap> i(batteryMap);
609 while (i.hasNext()) {
610 i.next();
611 if (i.value().value(QStringLiteral("NativePath")).toString()
612 == battery->nativePath()) {
613 foundBattery = i.key();
614 break;
615 }
616 }
617
618 bool validBefore = isValid();
619 disconnect(battery,SIGNAL(propertyChanged(QString,QVariant)),
620 this,SLOT(uPowerBatteryPropertyChanged(QString,QVariant)));
621 if (battery->type() == 2) {
622 batteryMap.remove(foundBattery);
623 }
624 bool validNow = isValid();
625 if (validBefore != validNow)
626 Q_EMIT validChanged(validNow);
627 }
628
629 QT_END_NAMESPACE