File: chargethresholdhelper_linux.cpp

package info (click to toggle)
powerdevil 4%3A6.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,680 kB
  • sloc: cpp: 13,284; xml: 1,911; python: 1,204; sh: 19; makefile: 10
file content (287 lines) | stat: -rw-r--r-- 10,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * SPDX-FileCopyrightText: 2020 Kai Uwe Broulik <kde@broulik.de>
 * SPDX-FileCopyrightText: 2023 Fabian Arndt <fabian.arndt@root-core.net>
 *
 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
 */

#include "chargethresholdhelper.h"

#include <powerdevil_debug.h>

#include <KAuth/HelperSupport>

#include <algorithm>

#include <QDir>
#include <QFile>

using namespace Qt::StringLiterals;

static const QString s_powerSupplySysFsPath = QStringLiteral("/sys/class/power_supply");
static const QString s_conservationModeSysFsPath = QStringLiteral("/sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode");

static const QString s_chargeStartThreshold = QStringLiteral("charge_control_start_threshold");
static const QString s_chargeEndThreshold = QStringLiteral("charge_control_end_threshold");
static const QString s_chargeTypesFilename = QStringLiteral("charge_types");

// Charge types supported by this helper, the device & kernel may support more than these. Docs:
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power - see "charge_type" and "charge_types"
static const QByteArray s_chargeTypeStandard = QByteArrayLiteral("Standard");
static const QByteArray s_chargeTypeCustom = QByteArrayLiteral("Custom");

ChargeThresholdHelper::ChargeThresholdHelper(QObject *parent)
    : QObject(parent)
{
}

static QStringList getBatteries()
{
    const QStringList power_supplies = QDir(s_powerSupplySysFsPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
    QStringList batteries;

    for (const QString &psu : power_supplies) {
        QDir psuDir(QString(s_powerSupplySysFsPath + u'/' + psu));
        QFile file(psuDir.filePath(u"type"_s));
        if (!file.open(QIODevice::ReadOnly)) {
            continue;
        }

        QString psu_type;
        QTextStream stream(&file);
        stream >> psu_type;

        if (psu_type.trimmed() != QLatin1String("Battery")) {
            continue; // Not a battery, skip
        }

        if (!psuDir.exists(s_chargeStartThreshold) && !psuDir.exists(s_chargeEndThreshold)) {
            continue; // No charge thresholds, skip
        }

        batteries.append(psu);
    }

    return batteries;
}

static int getThreshold(const QString &battery, const QString &which)
{
    QFile file(s_powerSupplySysFsPath + QLatin1Char('/') + battery + QLatin1Char('/') + which);

    // We don't need write access here, but if we can't write then it's better to mark this
    // threshold as unsupported so we won't try to write to it later. To the user, a read-only
    // value that's fixed by the firmware won't be very interesting anyway.
    if (!file.open(QIODevice::ReadWrite)) {
        return -1;
    }

    int threshold = -1;
    QTextStream stream(&file);
    stream >> threshold;

    if (threshold < 0 || threshold > 100) {
        qWarning() << file.fileName() << "contains invalid threshold" << threshold;
        return -1;
    }

    return threshold;
}

static QMap<QString, int> getThresholds(const QString &which)
{
    QMap<QString, int> thresholds;

    const QStringList batteries = getBatteries();
    for (const QString &battery : batteries) {
        if (int threshold = getThreshold(battery, which); threshold != -1) {
            thresholds.insert(battery, threshold);
        }
    }

    return thresholds;
}

static bool setThresholds(const QString &which, int threshold)
{
    const QStringList batteries = getBatteries();
    for (const QString &battery : batteries) {
        QFile file(s_powerSupplySysFsPath + QLatin1Char('/') + battery + QLatin1Char('/') + which);
        // TODO should we check the current value before writing the new one or is it clever
        // enough not to shred some chip by writing the same thing again?
        if (!file.open(QIODevice::WriteOnly)) {
            qWarning() << "Failed to open" << file.fileName() << "for writing";
            return false;
        }

        if (file.write(QByteArray::number(threshold)) == -1) {
            qWarning() << "Failed to write threshold into" << file.fileName();
            return false;
        }
    }

    return true;
}

static bool setChargeType(const QByteArray &chargeType)
{
    const QStringList batteries = getBatteries();
    for (const QString &battery : batteries) {
        QFile file(s_powerSupplySysFsPath + QLatin1Char('/') + battery + QLatin1Char('/') + s_chargeTypesFilename);
        if (!file.exists()) {
            // some drivers support start/end threshold without charge type selection - ignore those
            continue;
        }

        if (!file.open(QIODevice::WriteOnly)) {
            qWarning() << "Failed to open" << file.fileName() << "for writing";
            return false;
        }

        if (file.write(chargeType) == -1) {
            qWarning() << "Failed to charge type into" << file.fileName();
            return false;
        }
    }

    return true;
}

ActionReply ChargeThresholdHelper::getthreshold(const QVariantMap &args)
{
    Q_UNUSED(args);

    QMap<QString, int> stopThresholds = getThresholds(s_chargeEndThreshold);

    // In the rare case there are multiple batteries with varying charge thresholds, try to use something sensible
    const auto stopThresholdIt = std::ranges::min_element(std::as_const(stopThresholds));
    if (stopThresholdIt == stopThresholds.cend()) {
        auto reply = ActionReply::HelperErrorReply();
        reply.setErrorDescription(QStringLiteral("Charge thresholds are not supported by the kernel for this hardware"));
        return reply;
    }

    const int stopThreshold = *stopThresholdIt;
    const int startThreshold = getThreshold(stopThresholdIt.key(), s_chargeStartThreshold);

    ActionReply reply;
    reply.setData({
        {u"chargeStartThreshold"_s, startThreshold},
        {u"chargeStopThreshold"_s, stopThreshold},
    });
    return reply;
}

ActionReply ChargeThresholdHelper::setthreshold(const QVariantMap &args)
{
    bool hasStartThreshold;
    const int startThreshold = args.value(QStringLiteral("chargeStartThreshold"), -1).toInt(&hasStartThreshold);
    hasStartThreshold &= startThreshold != -1;

    bool hasStopThreshold;
    const int stopThreshold = args.value(QStringLiteral("chargeStopThreshold"), -1).toInt(&hasStopThreshold);
    hasStopThreshold &= stopThreshold != -1;

    QByteArray chargeType = args.value(QStringLiteral("chargeType"), QString()).toByteArray();
    bool requiresCustomThresholds = chargeType.isEmpty() || chargeType == s_chargeTypeCustom;
    bool hasCustomThresholds = hasStartThreshold || hasStopThreshold;

    if (requiresCustomThresholds && !hasCustomThresholds) {
        auto reply = ActionReply::HelperErrorReply(); // is there an "invalid arguments" error?
        reply.setErrorDescription(QStringLiteral("Thresholds required, but not provided"));
        return reply;
    }

    if (requiresCustomThresholds) {
        bool thresholdsInvalid = //
            (hasStartThreshold && (startThreshold < 0 || startThreshold > 100)) //
            || (hasStopThreshold && (stopThreshold < 0 || stopThreshold > 100)) //
            || (hasStartThreshold && hasStopThreshold && startThreshold > stopThreshold);

        if (thresholdsInvalid) {
            auto reply = ActionReply::HelperErrorReply(); // is there an "invalid arguments" error?
            reply.setErrorDescription(QStringLiteral("Invalid thresholds provided"));
            return reply;
        }

        if (hasStartThreshold && !setThresholds(s_chargeStartThreshold, startThreshold)) {
            auto reply = ActionReply::HelperErrorReply();
            reply.setErrorDescription(QStringLiteral("Failed to write start charge threshold"));
            return reply;
        }

        if (hasStopThreshold && !setThresholds(s_chargeEndThreshold, stopThreshold)) {
            auto reply = ActionReply::HelperErrorReply();
            reply.setErrorDescription(QStringLiteral("Failed to write stop charge threshold"));
            return reply;
        }
    }

    if (!chargeType.isEmpty()) {
        if (!setChargeType(chargeType)) {
            auto reply = ActionReply::HelperErrorReply();
            reply.setErrorDescription(QStringLiteral("Failed to write start charge type"));
            return reply;
        }
    } else if (requiresCustomThresholds) { // best effort automatic assignment, no errors
        if ((startThreshold == 0 || startThreshold == 100) && (stopThreshold == 0 || stopThreshold == 100)) {
            setChargeType(s_chargeTypeStandard);
        } else {
            setChargeType(s_chargeTypeCustom);
        }
    }

    return ActionReply();
}

// This handles Lenovo's "battery conservation mode" that limits the threshold to a fixed value
// This value differs from model to model (most 55-60%, others ~80%) and is unknown (last checked in Kernel 6.3)
ActionReply ChargeThresholdHelper::getconservationmode(const QVariantMap &args)
{
    Q_UNUSED(args);

    QFile file(s_conservationModeSysFsPath);
    if (!file.open(QIODevice::ReadOnly)) {
        auto reply = ActionReply::HelperErrorReply();
        reply.setErrorDescription(QStringLiteral("Battery conservation mode is not supported"));
        return reply;
    }

    int state = 0;
    QTextStream stream(&file);
    stream >> state;

    if (state < 0 || state > 1) {
        auto reply = ActionReply::HelperErrorReply();
        reply.setErrorDescription(QStringLiteral("Battery conservation mode is in an invalid state"));
        return reply;
    }

    ActionReply reply;
    reply.setData({{QStringLiteral("batteryConservationModeEnabled"), !!state}});
    return reply;
}

ActionReply ChargeThresholdHelper::setconservationmode(const QVariantMap &args)
{
    const bool enabled = args.value(QStringLiteral("batteryConservationModeEnabled"), 0).toBool();
    QFile file(s_conservationModeSysFsPath);

    if (!file.open(QIODevice::WriteOnly)) {
        auto reply = ActionReply::HelperErrorReply();
        reply.setErrorDescription(QStringLiteral("Failed to open battery conservation mode file"));
        return reply;
    }

    if (file.write(QByteArray::number(enabled)) == -1) {
        auto reply = ActionReply::HelperErrorReply();
        reply.setErrorDescription(QStringLiteral("Failed to set battery conservation mode"));
        return reply;
    }

    return ActionReply();
}

KAUTH_HELPER_MAIN("org.kde.powerdevil.chargethresholdhelper", ChargeThresholdHelper)

#include "moc_chargethresholdhelper.cpp"