blob: f8e208b1966312b663754189017089c59e39b411 [file] [log] [blame]
Yifan Honga8f55ca2021-10-20 23:05:45 -07001/*
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 <android-base/logging.h>
18#include <health-impl/ChargerUtils.h>
19
20namespace aidl::android::hardware::health::charger {
21
22std::optional<bool> ChargerCallback::ChargerShouldKeepScreenOn() {
23 return service_->ShouldKeepScreenOn();
24}
25
26bool ChargerCallback::ChargerIsOnline() {
27 auto hal_health_loop_sp = hal_health_loop_.lock();
28 if (hal_health_loop_sp == nullptr) {
29 // Assume no charger
30 return false;
31 }
32 return hal_health_loop_sp->charger_online();
33}
34
35void ChargerCallback::ChargerInitConfig(healthd_config* config) {
36 auto hal_health_loop_sp = hal_health_loop_.lock();
37 if (hal_health_loop_sp == nullptr) {
38 return;
39 }
40 return service_->OnInit(hal_health_loop_sp.get(), config);
41}
42
43int ChargerCallback::ChargerRegisterEvent(int fd, BoundFunction func, EventWakeup wakeup) {
44 auto hal_health_loop_sp = hal_health_loop_.lock();
45 if (hal_health_loop_sp == nullptr) {
46 return -1;
47 }
48 return hal_health_loop_sp->RegisterEvent(fd, func, wakeup);
49}
50
51void ChargerCallback::set_hal_health_loop(const std::weak_ptr<HalHealthLoop>& hal_health_loop) {
52 hal_health_loop_ = std::move(hal_health_loop);
53}
54
55// Implements HalHealthLoopCallback for AIDL charger
56// Adapter of (Charger, Health) -> HalHealthLoopCallback
57class LoopCallback : public HalHealthLoopCallback {
58 public:
59 LoopCallback(const std::shared_ptr<Health>& service, ChargerCallback* charger_callback)
60 : service_(service), charger_(std::make_unique<::android::Charger>(charger_callback)) {}
61
62 void OnHeartbeat() override {
63 service_->OnHeartbeat();
64 charger_->OnHeartbeat();
65 }
66 // Return the minimum timeout. Negative values are treated as no values.
67 int OnPrepareToWait() override {
68 int timeout1 = service_->OnPrepareToWait();
69 int timeout2 = charger_->OnPrepareToWait();
70
71 if (timeout1 < 0) return timeout2;
72 if (timeout2 < 0) return timeout1;
73 return std::min(timeout1, timeout2);
74 }
75
76 void OnInit(HalHealthLoop*, struct healthd_config* config) override {
77 // Charger::OnInit calls ChargerInitConfig, which calls into the real Health::OnInit.
78 charger_->OnInit(config);
79 }
80
81 void OnHealthInfoChanged(const HealthInfo& health_info) override {
82 charger_->OnHealthInfoChanged(::android::ChargerHealthInfo{
83 .battery_level = health_info.batteryLevel,
84 .battery_status = health_info.batteryStatus,
85 });
86 service_->OnHealthInfoChanged(health_info);
87 }
88
89 private:
90 std::shared_ptr<Health> service_;
91 std::unique_ptr<::android::Charger> charger_;
92};
93
94int ChargerModeMain(const std::shared_ptr<Health>& binder,
95 const std::shared_ptr<ChargerCallback>& charger_callback) {
96 LOG(INFO) << "Starting charger mode.";
97 // parent stack ==========================================
98 // current stack ||
99 // || ||
100 // V V
101 // hal_health_loop => loop_callback => charger --(raw)--> charger_callback
102 // ^----------------(weak)---------------------------------'
103 auto loop_callback = std::make_shared<LoopCallback>(binder, charger_callback.get());
104 auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, std::move(loop_callback));
105 charger_callback->set_hal_health_loop(hal_health_loop);
106 return hal_health_loop->StartLoop();
107}
108
109} // namespace aidl::android::hardware::health::charger