| 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_linux_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 | |
| 49 | #if !defined(QT_NO_UDEV) |
| 50 | #include "qudevwrapper_p.h" |
| 51 | #endif // QT_NO_UDEV |
| 52 | |
| 53 | QT_BEGIN_NAMESPACE |
| 54 | |
| 55 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, AC_ONLINE_SYSFS_PATH, (QLatin1String("/sys/class/power_supply/AC/online"))) |
| 56 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, BATTERY_SYSFS_PATH, (QLatin1String("/sys/class/power_supply/BAT%1/"))) |
| 57 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, POWER_SUPPLY_SYSFS_PATH, (QLatin1String("/sys/class/power_supply/"))) |
| 58 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, USB_PRESENT_SYSFS_PATH, (QLatin1String("/sys/class/power_supply/usb/present"))) |
| 59 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, USB_TYPE_SYSFS_PATH, (QLatin1String("/sys/class/power_supply/usb/type"))) |
| 60 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, USB0_PRESENT_SYSFS_PATH, (QLatin1String("/sys/class/power_supply/USB0/present"))) |
| 61 | Q_GLOBAL_STATIC_WITH_ARGS(const QString, USB0_TYPE_SYSFS_PATH, (QLatin1String("/sys/class/power_supply/USB0/type"))) |
| 62 | |
| 63 | QBatteryInfoPrivate::QBatteryInfoPrivate(QBatteryInfo *parent) |
| 64 | : QObject(parent) |
| 65 | , q_ptr(parent) |
| 66 | , watchIsValid(false) |
| 67 | , forceWatchBatteryCount(false) |
| 68 | , watchBatteryCount(false) |
| 69 | , watchChargerType(false) |
| 70 | , watchChargingState(false) |
| 71 | , watchCurrentFlow(false) |
| 72 | , watchRemainingCapacity(false) |
| 73 | , watchRemainingChargingTime(false) |
| 74 | , watchVoltage(false) |
| 75 | , watchLevelStatus(false) |
| 76 | , batteryCounts(-1) |
| 77 | , index(0) |
| 78 | , currentChargerType(QBatteryInfo::UnknownCharger) |
| 79 | #if !defined(QT_NO_UDEV) |
| 80 | , uDevWrapper(0) |
| 81 | #else |
| 82 | , timer(0) |
| 83 | #endif // QT_NO_UDEV |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | QBatteryInfoPrivate::QBatteryInfoPrivate(int batteryIndex, QBatteryInfo *parent) |
| 88 | : QObject(parent) |
| 89 | , q_ptr(parent) |
| 90 | , watchIsValid(false) |
| 91 | , forceWatchBatteryCount(false) |
| 92 | , watchBatteryCount(false) |
| 93 | , watchChargerType(false) |
| 94 | , watchChargingState(false) |
| 95 | , watchCurrentFlow(false) |
| 96 | , watchRemainingCapacity(false) |
| 97 | , watchRemainingChargingTime(false) |
| 98 | , watchVoltage(false) |
| 99 | , watchLevelStatus(false) |
| 100 | , batteryCounts(-1) |
| 101 | , index(batteryIndex) |
| 102 | , currentChargerType(QBatteryInfo::UnknownCharger) |
| 103 | #if !defined(QT_NO_UDEV) |
| 104 | , uDevWrapper(0) |
| 105 | #else |
| 106 | , timer(0) |
| 107 | #endif // QT_NO_UDEV |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | QBatteryInfoPrivate::~QBatteryInfoPrivate() |
| 112 | { |
| 113 | #if defined(QT_NO_UDEV) |
| 114 | delete timer; |
| 115 | #endif // QT_NO_UDEV |
| 116 | } |
| 117 | |
| 118 | int QBatteryInfoPrivate::batteryCount() |
| 119 | { |
| 120 | if (!watchBatteryCount) |
| 121 | return getBatteryCount(); |
| 122 | |
| 123 | return batteryCounts; |
| 124 | } |
| 125 | |
| 126 | int QBatteryInfoPrivate::batteryIndex() const |
| 127 | { |
| 128 | return index; |
| 129 | } |
| 130 | |
| 131 | bool QBatteryInfoPrivate::isValid() |
| 132 | { |
| 133 | // valid if the index < total count. |
| 134 | return (index >= 0) && (index < batteryCount()); |
| 135 | } |
| 136 | |
| 137 | void QBatteryInfoPrivate::setBatteryIndex(int batteryIndex) |
| 138 | { |
| 139 | if (index != batteryIndex) { |
| 140 | bool validBefore = isValid(); |
| 141 | int oldIndex = index; |
| 142 | index = batteryIndex; |
| 143 | bool validNow = isValid(); |
| 144 | if (validBefore != validNow) |
| 145 | Q_EMIT validChanged(validNow); |
| 146 | |
| 147 | if (validNow) { |
| 148 | if (validBefore) { |
| 149 | // valid now, valid before so we have to check individual values |
| 150 | |
| 151 | // ignore chargerType - it won't change based on battery index |
| 152 | //emit chargerTypeChanged(newChargerType); |
| 153 | |
| 154 | QBatteryInfo::ChargingState newChargingState = chargingState(); |
| 155 | if (newChargingState != chargingState(oldIndex)) |
| 156 | emit chargingStateChanged(newChargingState); |
| 157 | |
| 158 | int newValue = level(); |
| 159 | if (newValue != level(oldIndex)) |
| 160 | emit levelChanged(newValue); |
| 161 | |
| 162 | newValue = currentFlow(); |
| 163 | if (newValue != currentFlow(oldIndex)) |
| 164 | emit currentFlowChanged(newValue); |
| 165 | |
| 166 | newValue = cycleCount(); |
| 167 | if (newValue != cycleCount(oldIndex)) |
| 168 | emit cycleCountChanged(newValue); |
| 169 | |
| 170 | newValue = remainingCapacity(); |
| 171 | if (newValue != remainingCapacity(oldIndex)) |
| 172 | emit remainingCapacityChanged(newValue); |
| 173 | |
| 174 | newValue = remainingChargingTime(); |
| 175 | if (newValue != remainingChargingTime(oldIndex)) |
| 176 | emit remainingChargingTimeChanged(newValue); |
| 177 | |
| 178 | newValue = voltage(); |
| 179 | if (newValue != voltage(oldIndex)) |
| 180 | emit voltageChanged(newValue); |
| 181 | |
| 182 | QBatteryInfo::LevelStatus newLevelStatus = levelStatus(); |
| 183 | if (newLevelStatus != levelStatus(oldIndex)) |
| 184 | emit levelStatusChanged(newLevelStatus); |
| 185 | |
| 186 | QBatteryInfo::Health newHealth = health(); |
| 187 | if (newHealth != health(oldIndex)) |
| 188 | emit healthChanged(newHealth); |
| 189 | |
| 190 | float newTemperature = temperature(); |
| 191 | if (!qFuzzyCompare(newTemperature, temperature(oldIndex))) |
| 192 | emit temperatureChanged(newTemperature); |
| 193 | } else { |
| 194 | // it wasn't valid before so everything is changed |
| 195 | |
| 196 | // ignore chargerType - it won't change based on battery index |
| 197 | //emit chargerTypeChanged(newChargerType); |
| 198 | |
| 199 | emit chargingStateChanged(chargingState()); |
| 200 | emit levelChanged(level()); |
| 201 | emit currentFlowChanged(currentFlow()); |
| 202 | emit cycleCountChanged(cycleCount()); |
| 203 | emit remainingCapacityChanged(remainingCapacity()); |
| 204 | emit remainingChargingTimeChanged(remainingChargingTime()); |
| 205 | emit voltageChanged(voltage()); |
| 206 | emit levelStatusChanged(levelStatus()); |
| 207 | emit healthChanged(health()); |
| 208 | emit temperatureChanged(temperature()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | emit batteryIndexChanged(index); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | int QBatteryInfoPrivate::level(int battery) |
| 217 | { |
| 218 | int maxCapacity = maximumCapacity(battery); |
| 219 | int remCapacity = remainingCapacity(battery); |
| 220 | |
| 221 | if (maxCapacity == 0) |
| 222 | return -1; |
| 223 | |
| 224 | return remCapacity * 100 / maxCapacity; |
| 225 | } |
| 226 | |
| 227 | int QBatteryInfoPrivate::level() |
| 228 | { |
| 229 | return level(index); |
| 230 | } |
| 231 | |
| 232 | int QBatteryInfoPrivate::currentFlow(int battery) |
| 233 | { |
| 234 | if (!watchCurrentFlow) |
| 235 | return getCurrentFlow(battery); |
| 236 | |
| 237 | return currentFlows.value(battery); |
| 238 | } |
| 239 | |
| 240 | int QBatteryInfoPrivate::currentFlow() |
| 241 | { |
| 242 | return currentFlow(index); |
| 243 | } |
| 244 | |
| 245 | int QBatteryInfoPrivate::cycleCount(int battery) |
| 246 | { |
| 247 | Q_UNUSED(battery) |
| 248 | |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | int QBatteryInfoPrivate::cycleCount() |
| 253 | { |
| 254 | return cycleCount(index); |
| 255 | } |
| 256 | |
| 257 | int QBatteryInfoPrivate::maximumCapacity(int battery) |
| 258 | { |
| 259 | if (maximumCapacities[battery] == 0) { |
| 260 | QFile maximum(BATTERY_SYSFS_PATH()->arg(battery) + QStringLiteral("charge_full")); |
| 261 | if (maximum.open(QIODevice::ReadOnly)) { |
| 262 | bool ok = false; |
| 263 | int capacity = maximum.readAll().simplified().toInt(&ok); |
| 264 | if (ok) |
| 265 | maximumCapacities[battery] = capacity / 1000; |
| 266 | else |
| 267 | maximumCapacities[battery] = -1; |
| 268 | } else { |
| 269 | maximumCapacities[battery] = -1; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return maximumCapacities[battery]; |
| 274 | } |
| 275 | |
| 276 | int QBatteryInfoPrivate::maximumCapacity() |
| 277 | { |
| 278 | return maximumCapacity(index); |
| 279 | } |
| 280 | |
| 281 | int QBatteryInfoPrivate::remainingCapacity(int battery) |
| 282 | { |
| 283 | if (!watchRemainingCapacity) |
| 284 | return getRemainingCapacity(battery); |
| 285 | |
| 286 | return remainingCapacities.value(battery); |
| 287 | } |
| 288 | |
| 289 | int QBatteryInfoPrivate::remainingCapacity() |
| 290 | { |
| 291 | return remainingCapacity(index); |
| 292 | } |
| 293 | |
| 294 | int QBatteryInfoPrivate::remainingChargingTime(int battery) |
| 295 | { |
| 296 | if (!watchRemainingChargingTime) |
| 297 | return getRemainingChargingTime(battery); |
| 298 | |
| 299 | return remainingChargingTimes.value(battery); |
| 300 | } |
| 301 | |
| 302 | int QBatteryInfoPrivate::remainingChargingTime() |
| 303 | { |
| 304 | return remainingChargingTime(index); |
| 305 | } |
| 306 | |
| 307 | int QBatteryInfoPrivate::voltage(int battery) |
| 308 | { |
| 309 | if (!watchVoltage) |
| 310 | return getVoltage(battery); |
| 311 | |
| 312 | return voltages.value(battery); |
| 313 | } |
| 314 | |
| 315 | int QBatteryInfoPrivate::voltage() |
| 316 | { |
| 317 | return voltage(index); |
| 318 | } |
| 319 | |
| 320 | QBatteryInfo::ChargerType QBatteryInfoPrivate::chargerType() |
| 321 | { |
| 322 | if (!watchChargerType) |
| 323 | return getChargerType(); |
| 324 | |
| 325 | return currentChargerType; |
| 326 | } |
| 327 | |
| 328 | QBatteryInfo::ChargingState QBatteryInfoPrivate::chargingState(int battery) |
| 329 | { |
| 330 | if (!watchChargingState) |
| 331 | return getChargingState(battery); |
| 332 | |
| 333 | return chargingStates.value(battery); |
| 334 | } |
| 335 | |
| 336 | QBatteryInfo::ChargingState QBatteryInfoPrivate::chargingState() |
| 337 | { |
| 338 | return chargingState(index); |
| 339 | } |
| 340 | |
| 341 | QBatteryInfo::LevelStatus QBatteryInfoPrivate::levelStatus(int battery) |
| 342 | { |
| 343 | if (!watchLevelStatus) |
| 344 | return getLevelStatus(battery); |
| 345 | |
| 346 | return levelStatuss.value(battery); |
| 347 | } |
| 348 | |
| 349 | QBatteryInfo::LevelStatus QBatteryInfoPrivate::levelStatus() |
| 350 | { |
| 351 | return levelStatus(index); |
| 352 | } |
| 353 | |
| 354 | QBatteryInfo::Health QBatteryInfoPrivate::health(int battery) |
| 355 | { |
| 356 | Q_UNUSED(battery) |
| 357 | |
| 358 | return QBatteryInfo::HealthUnknown; |
| 359 | } |
| 360 | |
| 361 | QBatteryInfo::Health QBatteryInfoPrivate::health() |
| 362 | { |
| 363 | return health(index); |
| 364 | } |
| 365 | |
| 366 | float QBatteryInfoPrivate::temperature(int battery) |
| 367 | { |
| 368 | Q_UNUSED(battery) |
| 369 | |
| 370 | return qQNaN(); |
| 371 | } |
| 372 | |
| 373 | float QBatteryInfoPrivate::temperature() |
| 374 | { |
| 375 | return temperature(index); |
| 376 | } |
| 377 | |
| 378 | void QBatteryInfoPrivate::connectNotify(const QMetaMethod &signal) |
| 379 | { |
| 380 | static const QMetaMethod batteryCountChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::batteryCountChanged); |
| 381 | static const QMetaMethod validChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::validChanged); |
| 382 | static const QMetaMethod chargerTypeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::chargerTypeChanged); |
| 383 | static const QMetaMethod chargingStateChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::chargingStateChanged); |
| 384 | static const QMetaMethod currentFlowChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::currentFlowChanged); |
| 385 | static const QMetaMethod remainingCapacityChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::remainingCapacityChanged); |
| 386 | static const QMetaMethod remainingChargingTimeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::remainingChargingTimeChanged); |
| 387 | static const QMetaMethod voltageChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::voltageChanged); |
| 388 | static const QMetaMethod levelStatusChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::levelStatusChanged); |
| 389 | |
| 390 | #if !defined(QT_NO_UDEV) |
| 391 | if (!uDevWrapper) |
| 392 | uDevWrapper = new QUDevWrapper(this); |
| 393 | if (!watchChargerType && signal == chargerTypeChangedSignal) { |
| 394 | connect(uDevWrapper, SIGNAL(chargerTypeChanged(QByteArray,bool)), this, SLOT(onChargerTypeChanged(QByteArray,bool))); |
| 395 | } else if (!watchIsValid && !watchCurrentFlow && !watchVoltage && !watchChargingState && !watchRemainingCapacity |
| 396 | && !watchRemainingChargingTime && !watchBatteryCount && !watchLevelStatus) { |
| 397 | connect(uDevWrapper, SIGNAL(batteryDataChanged(int,QByteArray,QByteArray)), this, SLOT(onBatteryDataChanged(int,QByteArray,QByteArray))); |
| 398 | } |
| 399 | #else |
| 400 | if (timer == 0) { |
| 401 | timer = new QTimer; |
| 402 | timer->setInterval(2000); |
| 403 | connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout())); |
| 404 | } |
| 405 | |
| 406 | if (!timer->isActive()) |
| 407 | timer->start(); |
| 408 | #endif // QT_NO_UDEV |
| 409 | |
| 410 | if (signal == validChangedSignal) { |
| 411 | if (!watchIsValid && !watchBatteryCount) |
| 412 | forceWatchBatteryCount = true; |
| 413 | |
| 414 | watchIsValid = true; |
| 415 | |
| 416 | // we have to watch battery count if someone is watching validChanged. |
| 417 | watchBatteryCount = true; |
| 418 | batteryCounts = getBatteryCount(); |
| 419 | } else if (signal == batteryCountChangedSignal) { |
| 420 | watchBatteryCount = true; |
| 421 | forceWatchBatteryCount = false; |
| 422 | batteryCounts = getBatteryCount(); |
| 423 | } else if (signal == currentFlowChangedSignal) { |
| 424 | watchCurrentFlow = true; |
| 425 | int count = batteryCount(); |
| 426 | for (int i = 0; i < count; ++i) |
| 427 | currentFlows[i] = getCurrentFlow(i); |
| 428 | } else if (signal == voltageChangedSignal) { |
| 429 | watchVoltage = true; |
| 430 | int count = batteryCount(); |
| 431 | for (int i = 0; i < count; ++i) |
| 432 | voltages[i] = getVoltage(i); |
| 433 | } else if (signal == remainingCapacityChangedSignal) { |
| 434 | watchRemainingCapacity = true; |
| 435 | int count = batteryCount(); |
| 436 | for (int i = 0; i < count; ++i) |
| 437 | remainingCapacities[i] = getRemainingCapacity(i); |
| 438 | } else if (signal == remainingChargingTimeChangedSignal) { |
| 439 | watchRemainingChargingTime = true; |
| 440 | int count = batteryCount(); |
| 441 | for (int i = 0; i < count; ++i) |
| 442 | remainingChargingTimes[i] = getRemainingChargingTime(i); |
| 443 | } else if (signal == chargerTypeChangedSignal) { |
| 444 | watchChargerType = true; |
| 445 | currentChargerType = getChargerType(); |
| 446 | } else if (signal == chargingStateChangedSignal) { |
| 447 | watchChargingState = true; |
| 448 | int count = batteryCount(); |
| 449 | for (int i = 0; i < count; ++i) |
| 450 | chargingStates[i] = getChargingState(i); |
| 451 | } else if (signal == levelStatusChangedSignal) { |
| 452 | watchLevelStatus = true; |
| 453 | int count = batteryCount(); |
| 454 | for (int i = 0; i < count; i++) |
| 455 | levelStatuss[i] = getLevelStatus(i); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | void QBatteryInfoPrivate::disconnectNotify(const QMetaMethod &signal) |
| 460 | { |
| 461 | static const QMetaMethod batteryCountChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::batteryCountChanged); |
| 462 | static const QMetaMethod validChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::validChanged); |
| 463 | static const QMetaMethod chargerTypeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::chargerTypeChanged); |
| 464 | static const QMetaMethod chargingStateChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::chargingStateChanged); |
| 465 | static const QMetaMethod currentFlowChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::currentFlowChanged); |
| 466 | static const QMetaMethod remainingCapacityChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::remainingCapacityChanged); |
| 467 | static const QMetaMethod remainingChargingTimeChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::remainingChargingTimeChanged); |
| 468 | static const QMetaMethod voltageChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::voltageChanged); |
| 469 | static const QMetaMethod levelStatusChangedSignal = QMetaMethod::fromSignal(&QBatteryInfoPrivate::levelStatusChanged); |
| 470 | |
| 471 | if (signal == validChangedSignal) { |
| 472 | watchIsValid = false; |
| 473 | if (forceWatchBatteryCount) { |
| 474 | watchBatteryCount = false; |
| 475 | batteryCounts = -1; |
| 476 | } |
| 477 | } else if (signal == batteryCountChangedSignal) { |
| 478 | if (!watchIsValid) { |
| 479 | watchBatteryCount = false; |
| 480 | batteryCounts = -1; |
| 481 | } else { |
| 482 | forceWatchBatteryCount = true; |
| 483 | } |
| 484 | } else if (signal == currentFlowChangedSignal) { |
| 485 | watchCurrentFlow = false; |
| 486 | currentFlows.clear(); |
| 487 | } else if (signal == voltageChangedSignal) { |
| 488 | watchVoltage = false; |
| 489 | voltages.clear(); |
| 490 | } else if (signal == remainingCapacityChangedSignal) { |
| 491 | watchRemainingCapacity = false; |
| 492 | remainingCapacities.clear(); |
| 493 | } else if (signal == remainingChargingTimeChangedSignal) { |
| 494 | watchRemainingChargingTime = false; |
| 495 | remainingChargingTimes.clear(); |
| 496 | } else if (signal == chargerTypeChangedSignal) { |
| 497 | watchChargerType = false; |
| 498 | currentChargerType = QBatteryInfo::UnknownCharger; |
| 499 | } else if (signal == chargingStateChangedSignal) { |
| 500 | watchChargingState = false; |
| 501 | chargingStates.clear(); |
| 502 | } else if (signal == levelStatusChangedSignal) { |
| 503 | watchLevelStatus = false; |
| 504 | levelStatuss.clear(); |
| 505 | } |
| 506 | |
| 507 | #if !defined(QT_NO_UDEV) |
| 508 | if (uDevWrapper && !watchChargerType && signal == chargerTypeChangedSignal) { |
| 509 | disconnect(uDevWrapper, SIGNAL(chargerTypeChanged(QByteArray,bool)), |
| 510 | this, SLOT(onChargerTypeChanged(QByteArray,bool))); |
| 511 | } else if (uDevWrapper && !watchCurrentFlow && !watchVoltage && !watchChargingState && !watchRemainingCapacity |
| 512 | && !watchRemainingChargingTime && !watchBatteryCount && !watchLevelStatus) { |
| 513 | disconnect(uDevWrapper, SIGNAL(batteryDataChanged(int,QByteArray,QByteArray)), |
| 514 | this, SLOT(onBatteryDataChanged(int,QByteArray,QByteArray))); |
| 515 | } |
| 516 | #endif |
| 517 | |
| 518 | if (!watchBatteryCount && !watchChargerType && !watchChargingState |
| 519 | && !watchCurrentFlow && !watchRemainingCapacity |
| 520 | && !watchRemainingChargingTime && !watchVoltage && !watchLevelStatus) { |
| 521 | #if !defined(QT_NO_UDEV) |
| 522 | if (uDevWrapper) { |
| 523 | delete uDevWrapper; |
| 524 | uDevWrapper = 0; |
| 525 | } |
| 526 | #else |
| 527 | timer->stop(); |
| 528 | #endif // QT_NO_UDEV |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | #if !defined(QT_NO_UDEV) |
| 533 | |
| 534 | void QBatteryInfoPrivate::onBatteryDataChanged(int battery, const QByteArray &attribute, const QByteArray &value) |
| 535 | { |
| 536 | if (watchBatteryCount) { |
| 537 | int count = getBatteryCount(); |
| 538 | if (batteryCounts != count) { |
| 539 | bool validBefore = isValid(); |
| 540 | batteryCounts = count; |
| 541 | bool validNow = isValid(); |
| 542 | if (validBefore != validNow) |
| 543 | Q_EMIT validChanged(validNow); |
| 544 | |
| 545 | // We do not have to worry about firing all changed signals here. |
| 546 | // Each individual value will receive an onBatteryDataChanged() call |
| 547 | // and will fire a signal at that time. |
| 548 | |
| 549 | emit batteryCountChanged(count); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | if (watchChargingState && attribute.contains("status")) { |
| 554 | QBatteryInfo::ChargingState state = QBatteryInfo::UnknownChargingState; |
| 555 | if (qstrcmp(value, "Charging") == 0) |
| 556 | state = QBatteryInfo::Charging; |
| 557 | else if (qstrcmp(value, "Not charging") == 0) |
| 558 | state = QBatteryInfo::IdleChargingState; |
| 559 | else if (qstrcmp(value, "Discharging") == 0) |
| 560 | state = QBatteryInfo::Discharging; |
| 561 | else if (qstrcmp(value, "Full") == 0) |
| 562 | state = QBatteryInfo::IdleChargingState; |
| 563 | if (chargingStates.value(battery) != state) { |
| 564 | chargingStates[battery] = state; |
| 565 | if (battery == index) |
| 566 | emit chargingStateChanged(state); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | if (watchRemainingCapacity && attribute.contains("charge_now")) { |
| 571 | if (!value.isEmpty()) { |
| 572 | int remainingCapacity = value.toInt() / 1000; |
| 573 | if (remainingCapacities.value(battery) != remainingCapacity) { |
| 574 | remainingCapacities[battery] = remainingCapacity; |
| 575 | if (battery == index) |
| 576 | emit remainingCapacityChanged(remainingCapacity); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | if (watchRemainingChargingTime && attribute.contains("time_to_full_avg")) { |
| 582 | if (!value.isEmpty()) { |
| 583 | int remainingChargingTime = value.toInt(); |
| 584 | if (remainingChargingTimes.value(battery) != remainingChargingTime) { |
| 585 | remainingChargingTimes[battery] = remainingChargingTime; |
| 586 | if (battery == index) |
| 587 | emit remainingChargingTimeChanged(remainingChargingTime); |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (watchVoltage && attribute.contains("voltage_now")) { |
| 593 | if (!value.isEmpty()) { |
| 594 | int voltage = value.toInt() / 1000; |
| 595 | if (voltages.value(battery) != voltage) { |
| 596 | voltages[battery] = voltage; |
| 597 | if (battery == index) |
| 598 | emit voltageChanged(voltage); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (watchCurrentFlow && attribute.contains("current_now")) { |
| 604 | if (!value.isEmpty()) { |
| 605 | int currentFlow = value.toInt() / -1000; |
| 606 | if (chargingStates.value(battery) == QBatteryInfo::Discharging && currentFlow < 0) |
| 607 | currentFlow = -currentFlow; |
| 608 | |
| 609 | if (currentFlows.value(battery) != currentFlow) { |
| 610 | currentFlows[battery] = currentFlow; |
| 611 | if (battery == index) |
| 612 | emit currentFlowChanged(currentFlow); |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (watchLevelStatus && attribute.contains("capacity_level")) { |
| 618 | QBatteryInfo::LevelStatus levelStatus = QBatteryInfo::LevelUnknown; |
| 619 | if (qstrcmp(value, "Critical") == 0) |
| 620 | levelStatus = QBatteryInfo::LevelEmpty; |
| 621 | else if (qstrcmp(value, "Low") == 0) |
| 622 | levelStatus = QBatteryInfo::LevelLow; |
| 623 | else if (qstrcmp(value, "Normal") == 0) |
| 624 | levelStatus = QBatteryInfo::LevelOk; |
| 625 | else if (qstrcmp(value, "Full") == 0) |
| 626 | levelStatus = QBatteryInfo::LevelFull; |
| 627 | if (levelStatuss.value(battery) != levelStatus) { |
| 628 | levelStatuss[battery] = levelStatus; |
| 629 | if (battery == index) |
| 630 | emit levelStatusChanged(levelStatus); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void QBatteryInfoPrivate::onChargerTypeChanged(const QByteArray &value, bool enabled) |
| 636 | { |
| 637 | if (watchChargerType) { |
| 638 | QBatteryInfo::ChargerType charger = QBatteryInfo::UnknownCharger; |
| 639 | if (enabled) { |
| 640 | if ((qstrcmp(value, "AC") == 0) || qstrcmp(value, "USB_DCP") == 0) |
| 641 | charger = QBatteryInfo::WallCharger; |
| 642 | else if (qstrcmp(value, "USB") == 0) |
| 643 | charger = QBatteryInfo::USBCharger; |
| 644 | else if (qstrcmp(value, "USB_CDP") == 0 || qstrcmp(value, "USB_SDP") == 0) |
| 645 | charger = QBatteryInfo::VariableCurrentCharger; |
| 646 | } |
| 647 | if (currentChargerType != charger) { |
| 648 | currentChargerType = charger; |
| 649 | emit chargerTypeChanged(charger); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | #else |
| 655 | |
| 656 | void QBatteryInfoPrivate::onTimeout() |
| 657 | { |
| 658 | int count = getBatteryCount(); |
| 659 | int value; |
| 660 | if (watchBatteryCount) { |
| 661 | value = getBatteryCount(); |
| 662 | if (batteryCounts != value) { |
| 663 | bool validBefore = isValid(); |
| 664 | batteryCounts = value; |
| 665 | bool validNow = isValid(); |
| 666 | if (validBefore != validNow) |
| 667 | Q_EMIT validChanged(validNow); |
| 668 | |
| 669 | // We do not have to worry about firing all changed signals here. |
| 670 | // Each individual value will (possibly) be updated below |
| 671 | // and will fire a signal at that time if it has changed. |
| 672 | |
| 673 | emit batteryCountChanged(value); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | for (int i = 0; i < count; ++i) { |
| 678 | if (watchCurrentFlow) { |
| 679 | value = getCurrentFlow(i); |
| 680 | if (currentFlows.value(i) != value) { |
| 681 | currentFlows[i] = value; |
| 682 | if (i == index) |
| 683 | emit currentFlowChanged(value); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (watchVoltage) { |
| 688 | value = getVoltage(i); |
| 689 | if (voltages.value(i) != value) { |
| 690 | voltages[i] = value; |
| 691 | if (i == index) |
| 692 | emit voltageChanged(value); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | if (watchRemainingCapacity) { |
| 697 | value = getRemainingCapacity(i); |
| 698 | if (remainingCapacities.value(i) != value) { |
| 699 | remainingCapacities[i] = value; |
| 700 | if (i == index) |
| 701 | emit remainingCapacityChanged(value); |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (watchRemainingChargingTime) { |
| 706 | value = getRemainingChargingTime(i); |
| 707 | if (remainingChargingTimes.value(i) != value) { |
| 708 | remainingChargingTimes[i] = value; |
| 709 | if (i == index) |
| 710 | emit remainingChargingTimeChanged(value); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (watchChargerType) { |
| 715 | QBatteryInfo::ChargerType charger = getChargerType(); |
| 716 | if (currentChargerType != charger) { |
| 717 | currentChargerType = charger; |
| 718 | emit chargerTypeChanged(charger); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | if (watchChargingState) { |
| 723 | QBatteryInfo::ChargingState state = getChargingState(i); |
| 724 | if (chargingStates.value(i) != state) { |
| 725 | chargingStates[i] = state; |
| 726 | if (i == index) |
| 727 | emit chargingStateChanged(state); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if (watchLevelStatus) { |
| 732 | QBatteryInfo::LevelStatus levelStatus = getLevelStatus(i); |
| 733 | if (levelStatuss.value(i) != levelStatus) { |
| 734 | levelStatuss[i] = levelStatus; |
| 735 | if (i == index) |
| 736 | emit levelStatusChanged(levelStatus); |
| 737 | } |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | #endif // QT_NO_UDEV |
| 743 | |
| 744 | int QBatteryInfoPrivate::getBatteryCount() |
| 745 | { |
| 746 | return QDir(*POWER_SUPPLY_SYSFS_PATH()).entryList(QStringList() << QStringLiteral("BAT*")).size(); |
| 747 | } |
| 748 | |
| 749 | int QBatteryInfoPrivate::getCurrentFlow(int battery) |
| 750 | { |
| 751 | QBatteryInfo::ChargingState state = chargingState(battery); |
| 752 | if (state == QBatteryInfo::UnknownChargingState) |
| 753 | return 0; |
| 754 | |
| 755 | QFile current(BATTERY_SYSFS_PATH()->arg(battery) + QStringLiteral("current_now")); |
| 756 | if (!current.open(QIODevice::ReadOnly)) |
| 757 | return 0; |
| 758 | |
| 759 | bool ok = false; |
| 760 | int flow = current.readAll().simplified().toInt(&ok); |
| 761 | if (ok) { |
| 762 | // We want discharging current to be positive and charging current to be negative. |
| 763 | if (state == QBatteryInfo::Charging) { |
| 764 | // In case some drivers make charging current negative already and others are opposite |
| 765 | return flow < 0 ? flow / 1000 : flow / -1000; |
| 766 | } else if (state == QBatteryInfo::Discharging) { |
| 767 | // In case some drivers make discharging current positive already and others are opposite |
| 768 | return flow > 0 ? flow / 1000 : flow / -1000; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | int QBatteryInfoPrivate::getRemainingCapacity(int battery) |
| 776 | { |
| 777 | QFile remaining(BATTERY_SYSFS_PATH()->arg(battery) + QStringLiteral("charge_now")); |
| 778 | if (!remaining.open(QIODevice::ReadOnly)) |
| 779 | return -1; |
| 780 | |
| 781 | bool ok = false; |
| 782 | int capacity = remaining.readAll().simplified().toInt(&ok); |
| 783 | if (ok) |
| 784 | return capacity / 1000; |
| 785 | return -1; |
| 786 | } |
| 787 | |
| 788 | int QBatteryInfoPrivate::getRemainingChargingTime(int battery) |
| 789 | { |
| 790 | QBatteryInfo::ChargingState state = chargingState(battery); |
| 791 | if (state == QBatteryInfo::UnknownChargingState) |
| 792 | return -1; |
| 793 | else if (state == QBatteryInfo::IdleChargingState || state == QBatteryInfo::Discharging) |
| 794 | return 0; |
| 795 | |
| 796 | int remaining = 0; |
| 797 | QFile timeToFull(BATTERY_SYSFS_PATH()->arg(battery) + QStringLiteral("time_to_full_avg")); |
| 798 | if (timeToFull.open(QIODevice::ReadOnly)) { |
| 799 | bool ok = false; |
| 800 | remaining = timeToFull.readAll().simplified().toInt(&ok); |
| 801 | if (ok) |
| 802 | return remaining; |
| 803 | return -1; |
| 804 | } |
| 805 | |
| 806 | int max = 0; |
| 807 | int current = 0; |
| 808 | if ((max = maximumCapacity(battery)) == -1 |
| 809 | || (remaining = remainingCapacity(battery)) == -1 |
| 810 | || (current = currentFlow(battery)) == 0) { |
| 811 | return -1; |
| 812 | } |
| 813 | return (max - remaining) * -3600 / current; |
| 814 | } |
| 815 | |
| 816 | int QBatteryInfoPrivate::getVoltage(int battery) |
| 817 | { |
| 818 | QFile current(BATTERY_SYSFS_PATH()->arg(battery) + QStringLiteral("voltage_now")); |
| 819 | if (!current.open(QIODevice::ReadOnly)) |
| 820 | return -1; |
| 821 | |
| 822 | bool ok = false; |
| 823 | int voltage = current.readAll().simplified().toInt(&ok); |
| 824 | if (ok) |
| 825 | return voltage / 1000; |
| 826 | return -1; |
| 827 | } |
| 828 | |
| 829 | QBatteryInfo::ChargerType QBatteryInfoPrivate::getChargerType() |
| 830 | { |
| 831 | QFile charger(*AC_ONLINE_SYSFS_PATH()); |
| 832 | if (charger.open(QIODevice::ReadOnly)) { |
| 833 | char online; |
| 834 | if (charger.read(&online, 1) == 1 && online == '1') |
| 835 | return QBatteryInfo::WallCharger; |
| 836 | charger.close(); |
| 837 | } |
| 838 | |
| 839 | QMap<QString, QString> chargerMap; |
| 840 | chargerMap.insert(*USB0_PRESENT_SYSFS_PATH(), *USB0_TYPE_SYSFS_PATH()); |
| 841 | chargerMap.insert(*USB_PRESENT_SYSFS_PATH(), *USB_TYPE_SYSFS_PATH()); |
| 842 | |
| 843 | QList<QString> presentPaths = chargerMap.keys(); |
| 844 | foreach (const QString &presentPath, presentPaths) { |
| 845 | charger.setFileName(presentPath); |
| 846 | if (charger.open(QIODevice::ReadOnly)) { |
| 847 | char present; |
| 848 | if (charger.read(&present, 1) == 1 && present == '1') { |
| 849 | charger.close(); |
| 850 | |
| 851 | charger.setFileName(chargerMap.value(presentPath)); |
| 852 | if (charger.open(QIODevice::ReadOnly)) { |
| 853 | if (charger.readAll().simplified() == "USB_DCP") |
| 854 | return QBatteryInfo::WallCharger; |
| 855 | return QBatteryInfo::USBCharger; |
| 856 | } |
| 857 | } |
| 858 | charger.close(); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | return QBatteryInfo::UnknownCharger; |
| 863 | } |
| 864 | |
| 865 | QBatteryInfo::ChargingState QBatteryInfoPrivate::getChargingState(int battery) |
| 866 | { |
| 867 | QFile state(BATTERY_SYSFS_PATH()->arg(battery) + QStringLiteral("status")); |
| 868 | if (!state.open(QIODevice::ReadOnly)) |
| 869 | return QBatteryInfo::UnknownChargingState; |
| 870 | |
| 871 | QByteArray status = state.readAll().simplified(); |
| 872 | if (status == "Charging") |
| 873 | return QBatteryInfo::Charging; |
| 874 | else if (status == "Not charging") |
| 875 | return QBatteryInfo::IdleChargingState; |
| 876 | else if (status == "Discharging") |
| 877 | return QBatteryInfo::Discharging; |
| 878 | else if (status == "Full") |
| 879 | return QBatteryInfo::IdleChargingState; |
| 880 | |
| 881 | return QBatteryInfo::UnknownChargingState; |
| 882 | } |
| 883 | |
| 884 | QBatteryInfo::LevelStatus QBatteryInfoPrivate::getLevelStatus(int battery) |
| 885 | { |
| 886 | QFile levelStatusFile(BATTERY_SYSFS_PATH()->arg(battery) + QStringLiteral("capacity_level")); |
| 887 | if (!levelStatusFile.open(QIODevice::ReadOnly)) |
| 888 | return QBatteryInfo::LevelUnknown; |
| 889 | |
| 890 | QByteArray levelStatus = levelStatusFile.readAll().simplified(); |
| 891 | if (qstrcmp(levelStatus, "Critical") == 0) |
| 892 | return QBatteryInfo::LevelEmpty; |
| 893 | else if (qstrcmp(levelStatus, "Low") == 0) |
| 894 | return QBatteryInfo::LevelLow; |
| 895 | else if (qstrcmp(levelStatus, "Normal") == 0) |
| 896 | return QBatteryInfo::LevelOk; |
| 897 | else if (qstrcmp(levelStatus, "Full") == 0) |
| 898 | return QBatteryInfo::LevelFull; |
| 899 | |
| 900 | return QBatteryInfo::LevelUnknown; |
| 901 | } |
| 902 | |
| 903 | QT_END_NAMESPACE |