1 /*
2  * Copyright (C) 2021 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 <health-impl/HalHealthLoop.h>
18 
19 #include <android-base/logging.h>
20 
21 #include <health-impl/Health.h>
22 #include "health-convert.h"
23 
24 namespace aidl::android::hardware::health {
25 
26 // Unlike the HIDL version android::hardware::health::V2_1::implementation::HalHealthLoop,
27 // do not define HalHealthLoop::Init because we no longer have Health::getHealthConfig.
28 // Let the Health class handle Init.
Init(struct healthd_config * config)29 void HalHealthLoop::Init(struct healthd_config* config) {
30     callback_->OnInit(this, config);
31 }
32 
Heartbeat()33 void HalHealthLoop::Heartbeat() {
34     callback_->OnHeartbeat();
35 }
36 
ScheduleBatteryUpdate()37 void HalHealthLoop::ScheduleBatteryUpdate() {
38     // ignore errors. impl may not be able to handle any callbacks, so
39     // update() may return errors.
40     if (auto res = service_->update(); !res.isOk()) {
41         LOG(WARNING) << "update() on the health HAL implementation failed with "
42                      << res.getDescription();
43     }
44 
45     HealthInfo health_info;
46     auto res = service_->getHealthInfo(&health_info);
47     CHECK(res.isOk()) << "getHealthInfo() on the health HAL implementation failed with "
48                       << res.getDescription();
49     OnHealthInfoChanged(health_info);
50 }
51 
PrepareToWait()52 int HalHealthLoop::PrepareToWait() {
53     return callback_->OnPrepareToWait();
54 }
55 
OnHealthInfoChanged(const HealthInfo & health_info)56 void HalHealthLoop::OnHealthInfoChanged(const HealthInfo& health_info) {
57     callback_->OnHealthInfoChanged(health_info);
58     set_charger_online(health_info);
59     AdjustWakealarmPeriods(charger_online());
60 }
61 
set_charger_online(const HealthInfo & health_info)62 void HalHealthLoop::set_charger_online(const HealthInfo& health_info) {
63     charger_online_ = health_info.chargerAcOnline || health_info.chargerUsbOnline ||
64                       health_info.chargerWirelessOnline || health_info.chargerDockOnline;
65 }
66 
67 }  // namespace aidl::android::hardware::health
68