summaryrefslogtreecommitdiff
path: root/health/aidl/default/include/health-impl/Health.h
blob: 6bd49465d6ee8cba5657ee9f769c110293a27f4b (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
110
111
112
113
114
/*
 * 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.
 */

#pragma once

#include <memory>
#include <optional>

#include <aidl/android/hardware/health/BnHealth.h>
#include <aidl/android/hardware/health/IHealthInfoCallback.h>
#include <android/binder_auto_utils.h>
#include <health-impl/HalHealthLoop.h>
#include <healthd/BatteryMonitor.h>
#include <healthd/healthd.h>

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

class LinkedCallback;

// AIDL version of android::hardware::health::V2_1::implementation::Health and BinderHealth.
// There's no need to separate the two in AIDL because AIDL does not support passthrough transport.
//
// Instead of inheriting from HalHealthLoop directly, implements the callback interface to
// HalHealthLoop instead.
//
// Sample implementation of health HAL.
class Health : public BnHealth, public HalHealthLoopCallback {
  public:
    // Initialize with |config|.
    // A subclass may modify |config| before passing it to the parent constructor.
    // See implementation of Health for code samples.
    Health(std::string_view instance_name, std::unique_ptr<struct healthd_config>&& config);
    virtual ~Health();

    ndk::ScopedAStatus registerCallback(
            const std::shared_ptr<IHealthInfoCallback>& callback) override;
    ndk::ScopedAStatus unregisterCallback(
            const std::shared_ptr<IHealthInfoCallback>& callback) override;
    ndk::ScopedAStatus update() override;

    // A subclass should not override this. Override UpdateHealthInfo instead.
    ndk::ScopedAStatus getHealthInfo(HealthInfo* out) override final;

    // A subclass is recommended to override the path in healthd_config in the constructor.
    // Only override these if there are no suitable kernel interfaces to read these values.
    ndk::ScopedAStatus getChargeCounterUah(int32_t* out) override;
    ndk::ScopedAStatus getCurrentNowMicroamps(int32_t* out) override;
    ndk::ScopedAStatus getCurrentAverageMicroamps(int32_t* out) override;
    ndk::ScopedAStatus getCapacity(int32_t* out) override;
    ndk::ScopedAStatus getChargeStatus(BatteryStatus* out) override;

    // A subclass may either override these or provide function pointers in
    // in healthd_config in the constructor.
    // Prefer overriding these for efficiency.
    ndk::ScopedAStatus getEnergyCounterNwh(int64_t* out) override;

    // A subclass may override these for a specific device.
    // The default implementations return nothing in |out|.
    ndk::ScopedAStatus getDiskStats(std::vector<DiskStats>* out) override;
    ndk::ScopedAStatus getStorageInfo(std::vector<StorageInfo>* out) override;

    // A subclass may override these to provide a different implementation.
    binder_status_t dump(int fd, const char** args, uint32_t num_args) override;

    // HalHealthLoopCallback implementation.
    void OnInit(HalHealthLoop* hal_health_loop, struct healthd_config* config) override;
    void OnHealthInfoChanged(const HealthInfo& health_info) override;

    // A subclass may override this if it wants to handle binder events differently.
    virtual void BinderEvent(uint32_t epevents);

    // A subclass may override this to determine whether screen should be kept on in charger mode.
    // Default is to invoke healthd_config->screen_on() on the BatteryProperties converted
    // from getHealthInfo.
    // Prefer overriding these to providing screen_on in healthd_config in the constructor
    // for efficiency.
    virtual std::optional<bool> ShouldKeepScreenOn();

  protected:
    // A subclass can override this to modify any health info object before
    // returning to clients. This is similar to healthd_board_battery_update().
    // By default, it does nothing.
    // See implementation of Health for code samples.
    virtual void UpdateHealthInfo(HealthInfo* health_info);

  private:
    friend LinkedCallback;  // for exposing death_recipient_

    bool unregisterCallbackInternal(std::shared_ptr<IHealthInfoCallback> callback);

    std::string instance_name_;
    ::android::BatteryMonitor battery_monitor_;
    std::unique_ptr<struct healthd_config> healthd_config_;

    ndk::ScopedAIBinder_DeathRecipient death_recipient_;
    int binder_fd_ = -1;
    std::mutex callbacks_lock_;
    std::vector<std::unique_ptr<LinkedCallback>> callbacks_;
};

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