summaryrefslogtreecommitdiff
path: root/health/aidl/default/ChargerUtils.cpp
blob: f8e208b1966312b663754189017089c59e39b411 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2021 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 <android-base/logging.h>
#include <health-impl/ChargerUtils.h>

namespace aidl::android::hardware::health::charger {

std::optional<bool> ChargerCallback::ChargerShouldKeepScreenOn() {
    return service_->ShouldKeepScreenOn();
}

bool ChargerCallback::ChargerIsOnline() {
    auto hal_health_loop_sp = hal_health_loop_.lock();
    if (hal_health_loop_sp == nullptr) {
        // Assume no charger
        return false;
    }
    return hal_health_loop_sp->charger_online();
}

void ChargerCallback::ChargerInitConfig(healthd_config* config) {
    auto hal_health_loop_sp = hal_health_loop_.lock();
    if (hal_health_loop_sp == nullptr) {
        return;
    }
    return service_->OnInit(hal_health_loop_sp.get(), config);
}

int ChargerCallback::ChargerRegisterEvent(int fd, BoundFunction func, EventWakeup wakeup) {
    auto hal_health_loop_sp = hal_health_loop_.lock();
    if (hal_health_loop_sp == nullptr) {
        return -1;
    }
    return hal_health_loop_sp->RegisterEvent(fd, func, wakeup);
}

void ChargerCallback::set_hal_health_loop(const std::weak_ptr<HalHealthLoop>& hal_health_loop) {
    hal_health_loop_ = std::move(hal_health_loop);
}

// Implements HalHealthLoopCallback for AIDL charger
// Adapter of (Charger, Health) ->  HalHealthLoopCallback
class LoopCallback : public HalHealthLoopCallback {
  public:
    LoopCallback(const std::shared_ptr<Health>& service, ChargerCallback* charger_callback)
        : service_(service), charger_(std::make_unique<::android::Charger>(charger_callback)) {}

    void OnHeartbeat() override {
        service_->OnHeartbeat();
        charger_->OnHeartbeat();
    }
    // Return the minimum timeout. Negative values are treated as no values.
    int OnPrepareToWait() override {
        int timeout1 = service_->OnPrepareToWait();
        int timeout2 = charger_->OnPrepareToWait();

        if (timeout1 < 0) return timeout2;
        if (timeout2 < 0) return timeout1;
        return std::min(timeout1, timeout2);
    }

    void OnInit(HalHealthLoop*, struct healthd_config* config) override {
        // Charger::OnInit calls ChargerInitConfig, which calls into the real Health::OnInit.
        charger_->OnInit(config);
    }

    void OnHealthInfoChanged(const HealthInfo& health_info) override {
        charger_->OnHealthInfoChanged(::android::ChargerHealthInfo{
                .battery_level = health_info.batteryLevel,
                .battery_status = health_info.batteryStatus,
        });
        service_->OnHealthInfoChanged(health_info);
    }

  private:
    std::shared_ptr<Health> service_;
    std::unique_ptr<::android::Charger> charger_;
};

int ChargerModeMain(const std::shared_ptr<Health>& binder,
                    const std::shared_ptr<ChargerCallback>& charger_callback) {
    LOG(INFO) << "Starting charger mode.";
    //   parent stack ==========================================
    //   current stack                                         ||
    //    ||                                                   ||
    //    V                                                    V
    // hal_health_loop => loop_callback => charger --(raw)--> charger_callback
    //    ^----------------(weak)---------------------------------'
    auto loop_callback = std::make_shared<LoopCallback>(binder, charger_callback.get());
    auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, std::move(loop_callback));
    charger_callback->set_hal_health_loop(hal_health_loop);
    return hal_health_loop->StartLoop();
}

}  // namespace aidl::android::hardware::health::charger