1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <health2impl/HalHealthLoop.h>
18 
19 #include <android-base/logging.h>
20 #include <hal_conversion.h>
21 #include <hidl/HidlTransportSupport.h>
22 #include <hwbinder/IPCThreadState.h>
23 
24 #include <health2impl/Health.h>
25 
26 using android::hardware::configureRpcThreadpool;
27 using android::hardware::handleTransportPoll;
28 using android::hardware::IPCThreadState;
29 using android::hardware::setupTransportPolling;
30 
31 using android::hardware::health::V1_0::hal_conversion::convertFromHealthConfig;
32 using android::hardware::health::V2_0::Result;
33 
34 namespace android {
35 namespace hardware {
36 namespace health {
37 namespace V2_1 {
38 namespace implementation {
39 
Init(struct healthd_config * config)40 void HalHealthLoop::Init(struct healthd_config* config) {
41     // Retrieve healthd_config from the HAL.
42     service_->getHealthConfig([config](auto res, const auto& health_config) {
43         CHECK(res == Result::SUCCESS);
44 
45         convertFromHealthConfig(health_config.battery, config);
46         config->boot_min_cap = health_config.bootMinCap;
47 
48         // Leave screen_on empty because it is handled in GetScreenOn below.
49 
50         // Leave ignorePowerSupplyNames empty because it isn't
51         // used by clients of health HAL.
52     });
53 }
54 
Heartbeat(void)55 void HalHealthLoop::Heartbeat(void) {
56     // noop
57 }
58 
ScheduleBatteryUpdate()59 void HalHealthLoop::ScheduleBatteryUpdate() {
60     // ignore errors. impl may not be able to handle any callbacks, so
61     // update() may return errors.
62     Result res = service_->update();
63     if (res != Result::SUCCESS) {
64         LOG(WARNING) << "update() on the health HAL implementation failed with " << toString(res);
65     }
66 
67     service_->getHealthInfo_2_1([this](auto res, const auto& health_info) {
68         CHECK(res == Result::SUCCESS)
69                 << "getHealthInfo_2_1() on the health HAL implementation failed with "
70                 << toString(res);
71         this->OnHealthInfoChanged(health_info);
72     });
73 }
74 
PrepareToWait()75 int HalHealthLoop::PrepareToWait() {
76     return -1;
77 }
78 
OnHealthInfoChanged(const HealthInfo & health_info)79 void HalHealthLoop::OnHealthInfoChanged(const HealthInfo& health_info) {
80     set_charger_online(health_info);
81     AdjustWakealarmPeriods(charger_online());
82 }
83 
set_charger_online(const HealthInfo & health_info)84 void HalHealthLoop::set_charger_online(const HealthInfo& health_info) {
85     const auto& props = health_info.legacy.legacy;
86     charger_online_ =
87             props.chargerAcOnline || props.chargerUsbOnline || props.chargerWirelessOnline;
88 }
89 
90 }  // namespace implementation
91 }  // namespace V2_1
92 }  // namespace health
93 }  // namespace hardware
94 }  // namespace android
95