Solid

iokitbattery.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Harald Fernengel <harry@kdevelop.org>
3 SPDX-FileCopyrightText: 2017 René J.V. Bertin <rjvbertin@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "iokitbattery.h"
9#include "iokitdevice.h"
10
11// TODO - emit the signals
12
13using namespace Solid::Backends::IOKit;
14
15Battery::Battery(IOKitDevice *device)
16 : DeviceInterface(device)
17{
18}
19
20Battery::~Battery()
21{
22}
23
24// properties: QMap(("AdapterInfo", QVariant(int, 0))
25// ("Amperage", QVariant(int, 0))
26// ("AvgTimeToEmpty", QVariant(int, 65535))
27// ("AvgTimeToFull", QVariant(int, 65535))
28// ("BatteryInstalled", QVariant(bool, true))
29// ("BatteryInvalidWakeSeconds", QVariant(int, 30))
30// ("BatterySerialNumber", QVariant(QString, "W01286PEED3BA"))
31// ("BootPathUpdated", QVariant(int, 1501532930))
32// ("CellVoltage", QVariant(QVariantList, (QVariant(int, 4136), QVariant(int, 4134), QVariant(int, 4134), QVariant(int, 0))))
33// ("CurrentCapacity", QVariant(int, 5552))
34// ("CycleCount", QVariant(int, 16))
35// ("DesignCapacity", QVariant(int, 5770))
36// ("DesignCycleCount", QVariant(int, 1000))
37// ("DeviceName", QVariant(QString, "bq20z451"))
38// ("ExternalChargeCapable", QVariant(bool, true))
39// ("ExternalConnected", QVariant(bool, true))
40// ("FirmwareSerialNumber", QVariant(int, 48))
41// ("FullPathUpdated", QVariant(int, 1502790621))
42// ("FullyCharged", QVariant(bool, true))
43// ("IOGeneralInterest", QVariant(QString, "IOCommand is not serializable"))
44// ("InstantAmperage", QVariant(int, 0))
45// ("InstantTimeToEmpty", QVariant(int, 65535))
46// ("IsCharging", QVariant(bool, false))
47// ("LegacyBatteryInfo", QVariant(QVariantMap, QMap(("Amperage", QVariant(int, 0))
48// ("Capacity", QVariant(int, 5814))
49// ("Current", QVariant(int, 5552))
50// ("Cycle Count", QVariant(int, 16))
51// ("Flags", QVariant(int, 5))
52// ("Voltage", QVariant(int, 12403)))))
53// ("Location", QVariant(int, 0))
54// ("ManufactureDate", QVariant(int, 16106))
55// ("Manufacturer", QVariant(QString, "SMP"))
56// ("ManufacturerData", QVariant(QByteArray, "\x00\x00\x00\x00\x02\x01\x00\n\x01X\x00\x00\x02K6c\x03""00A\x03""ATL\x00\x12\x00\x00"))
57// ("MaxCapacity", QVariant(int, 5814))
58// ("MaxErr", QVariant(int, 1))
59// ("OperationStatus", QVariant(int, 58435))
60// ("PackReserve", QVariant(int, 200))
61// ("PermanentFailureStatus", QVariant(int, 0))
62// ("PostChargeWaitSeconds", QVariant(int, 120))
63// ("PostDischargeWaitSeconds", QVariant(int, 120))
64// ("Temperature", QVariant(int, 2965))
65// ("TimeRemaining", QVariant(int, 0))
66// ("UserVisiblePathUpdated", QVariant(int, 1502790679))
67// ("Voltage", QVariant(int, 12403))
68// ("className", QVariant(QString, "AppleSmartBattery")))
69
70qlonglong Battery::timeToEmpty() const
71{
72 if (chargeState() != Solid::Battery::Charging) {
73 int t = m_device->property(QStringLiteral("AvgTimeToEmpty")).toInt();
74 return t == 65535 ? -1 : t * 60;
75 }
76 return -1;
77}
78
79qlonglong Battery::timeToFull() const
80{
81 if (chargeState() == Solid::Battery::Charging) {
82 int t = m_device->property(QStringLiteral("AvgTimeToFull")).toInt();
83 return t == 65535 ? -1 : t * 60;
84 }
85 return -1;
86}
87
88double Battery::voltage() const
89{
90 return m_device->property(QStringLiteral("Voltage")).toInt() / 1000.0;
91}
92
93double Battery::temperature() const
94{
95 return m_device->property(QStringLiteral("Temperature")).toInt() / 100.0;
96}
97
98QString Battery::serial() const
99{
100 return m_device->property(QStringLiteral("BatterySerialNumber")).toString();
101}
102
103bool Battery::isPresent() const
104{
105 return m_device->property(QStringLiteral("ExternalConnected")).toBool();
106}
107
108Solid::Battery::BatteryType Battery::type() const
109{
110 // TODO - how to figure that one out? Just presume we're
111 // only called with the main battery.
112 return Solid::Battery::PrimaryBattery;
113}
114
115int Battery::chargePercent() const
116{
117 // always calculate since FullyCharged remains true down to 92% or so.
118 int maxCapacity = m_device->property(QStringLiteral("MaxCapacity")).toInt();
119 if (maxCapacity == 0) {
120 return 0; // prevent divide by 0
121 }
122 return int(m_device->property(QStringLiteral("CurrentCapacity")).toInt() * 100.0 / maxCapacity + 0.5);
123}
124
125int Battery::capacity() const
126{
127 if (m_device->iOKitPropertyExists(QStringLiteral("PermanentFailureStatus")) //
128 && m_device->property(QStringLiteral("PermanentFailureStatus")).toInt()) {
129 return 0;
130 }
131 return 100;
132}
133
134int Battery::cycleCount() const
135{
136 return m_device->property(QStringLiteral("CycleCount")).toInt();
137}
138
139bool Battery::isRechargeable() const
140{
141 return m_device->property(QStringLiteral("CycleCount")).toInt() > 1;
142}
143
144bool Battery::isPowerSupply() const
145{
146 /* clang-format off */
147 return m_device->iOKitPropertyExists(QStringLiteral("BatteryInstalled"))
148 ? m_device->property(QStringLiteral("BatteryInstalled")).toBool()
149 : true;
150 /* clang-format on */
151}
152
153Solid::Battery::ChargeState Battery::chargeState() const
154{
155 if (m_device->property(QStringLiteral("IsCharging")).toBool()) {
156 return Solid::Battery::Charging;
157 }
158 if (m_device->property(QStringLiteral("FullyCharged")).toBool()) {
159 return Solid::Battery::FullyCharged;
160 }
161 return Solid::Battery::Discharging;
162}
163
164#include "moc_iokitbattery.cpp"
BatteryType
This enum type defines the type of the device holding the battery.
ChargeState
This enum type defines charge state of a battery.
bool toBool() const const
int toInt(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:01:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.