summaryrefslogtreecommitdiff
path: root/healthd/healthd_mode_charger_hidl.cpp
blob: 3a33c02c55e010aa526f17f355ce73fad735c277 (plain)
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
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "healthd_mode_charger_hidl.h"

#include <android/hardware/health/2.0/types.h>
#include <charger.sysprop.h>
#include <cutils/klog.h>

#include "charger_utils.h"

using android::hardware::health::GetHealthServiceOrDefault;
using android::hardware::health::V2_0::Result;

namespace android {

ChargerHidl::ChargerHidl(const sp<android::hardware::health::V2_1::IHealth>& service)
    : HalHealthLoop("charger", service), charger_(std::make_unique<Charger>(this)) {}

void ChargerHidl::OnHealthInfoChanged(const HealthInfo_2_1& health_info) {
    set_charger_online(health_info);

    charger_->OnHealthInfoChanged(ChargerHealthInfo{
            .battery_level = health_info.legacy.legacy.batteryLevel,
            .battery_status = static_cast<::aidl::android::hardware::health::BatteryStatus>(
                    health_info.legacy.legacy.batteryStatus),
    });

    AdjustWakealarmPeriods(charger_online());
}

std::optional<bool> ChargerHidl::ChargerShouldKeepScreenOn() {
    std::optional<bool> out_screen_on;
    service()->shouldKeepScreenOn([&](Result res, bool screen_on) {
        if (res == Result::SUCCESS) {
            *out_screen_on = screen_on;
        }
    });
    return out_screen_on;
}

bool ChargerHidl::ChargerEnableSuspend() {
    return android::sysprop::ChargerProperties::enable_suspend().value_or(false);
}

}  // namespace android

int healthd_charger_main(int argc, char** argv) {
    int ch;

    while ((ch = getopt(argc, argv, "cr")) != -1) {
        switch (ch) {
            case 'c':
                // -c is now a noop
                break;
            case 'r':
                // -r is now a noop
                break;
            case '?':
            default:
                KLOG_ERROR("charger", "Unrecognized charger option: %c\n", optopt);
                exit(1);
        }
    }

    android::ChargerHidl charger(GetHealthServiceOrDefault());
    return charger.StartLoop();
}