summaryrefslogtreecommitdiff
path: root/health/utils/libhealth2impl/HalHealthLoop.cpp
blob: 3901a763c3d96be56d1f8e6be0a2b62cfcfa481b (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
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2019 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 <health2impl/HalHealthLoop.h>

#include <android-base/logging.h>
#include <hal_conversion.h>
#include <hidl/HidlTransportSupport.h>
#include <hwbinder/IPCThreadState.h>

#include <health2impl/Health.h>

using android::hardware::configureRpcThreadpool;
using android::hardware::handleTransportPoll;
using android::hardware::IPCThreadState;
using android::hardware::setupTransportPolling;

using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
using android::hardware::health::V2_0::Result;

namespace android {
namespace hardware {
namespace health {
namespace V2_1 {
namespace implementation {

void HalHealthLoop::Init(struct healthd_config* config) {
    // Retrieve healthd_config from the HAL.
    service_->getHealthConfig([config](auto res, const auto& health_config) {
        CHECK(res == Result::SUCCESS);

        convertFromHealthConfig(health_config.battery, config);
        config->boot_min_cap = health_config.bootMinCap;

        // Leave screen_on empty because it is handled in GetScreenOn below.

        // Leave ignorePowerSupplyNames empty because it isn't
        // used by clients of health HAL.
    });
}

void HalHealthLoop::Heartbeat(void) {
    // noop
}

void HalHealthLoop::ScheduleBatteryUpdate() {
    // ignore errors. impl may not be able to handle any callbacks, so
    // update() may return errors.
    Result res = service_->update();
    if (res != Result::SUCCESS) {
        LOG(WARNING) << "update() on the health HAL implementation failed with " << toString(res);
    }

    service_->getHealthInfo_2_1([this](auto res, const auto& health_info) {
        CHECK(res == Result::SUCCESS)
                << "getHealthInfo_2_1() on the health HAL implementation failed with "
                << toString(res);
        this->OnHealthInfoChanged(health_info);
    });
}

int HalHealthLoop::PrepareToWait() {
    return -1;
}

void HalHealthLoop::OnHealthInfoChanged(const HealthInfo& health_info) {
    set_charger_online(health_info);
    AdjustWakealarmPeriods(charger_online());
}

void HalHealthLoop::set_charger_online(const HealthInfo& health_info) {
    const auto& props = health_info.legacy.legacy;
    charger_online_ =
            props.chargerAcOnline || props.chargerUsbOnline || props.chargerWirelessOnline;
}

}  // namespace implementation
}  // namespace V2_1
}  // namespace health
}  // namespace hardware
}  // namespace android