blob: ec23c10bc0d4d557fa07ee5830d9a3b2fc27d8b1 [file] [log] [blame]
Yifan Hong830cdb12021-01-11 20:47:23 -08001/*
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
24namespace 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.
29void HalHealthLoop::Init(struct healthd_config* config) {
30 callback_->OnInit(this, config);
31}
32
33void HalHealthLoop::Heartbeat() {
34 callback_->OnHeartbeat();
35}
36
37void 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
52int HalHealthLoop::PrepareToWait() {
53 return callback_->OnPrepareToWait();
54}
55
56void HalHealthLoop::OnHealthInfoChanged(const HealthInfo& health_info) {
57 callback_->OnHealthInfoChanged(health_info);
58 set_charger_online(health_info);
59 AdjustWakealarmPeriods(charger_online());
60}
61
62void HalHealthLoop::set_charger_online(const HealthInfo& health_info) {
63 charger_online_ = health_info.chargerAcOnline || health_info.chargerUsbOnline ||
Jack Wu5b135122021-12-20 14:56:50 +080064 health_info.chargerWirelessOnline || health_info.chargerDockOnline;
Yifan Hong830cdb12021-01-11 20:47:23 -080065}
66
67} // namespace aidl::android::hardware::health