blob: e49f44c7e4dba8af85daa271ef1a4828ed8a1bb2 [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#pragma once
18
19#include <memory>
20#include <optional>
21
22#include <aidl/android/hardware/health/BnHealth.h>
23#include <aidl/android/hardware/health/IHealthInfoCallback.h>
24#include <android/binder_auto_utils.h>
25#include <health-impl/HalHealthLoop.h>
26#include <healthd/BatteryMonitor.h>
27#include <healthd/healthd.h>
28
29namespace aidl::android::hardware::health {
30
31class LinkedCallback;
32
33// AIDL version of android::hardware::health::V2_1::implementation::Health and BinderHealth.
34// There's no need to separate the two in AIDL because AIDL does not support passthrough transport.
35//
36// Instead of inheriting from HalHealthLoop directly, implements the callback interface to
37// HalHealthLoop instead.
38//
39// Sample implementation of health HAL.
40class Health : public BnHealth, public HalHealthLoopCallback {
41 public:
42 // Initialize with |config|.
43 // A subclass may modify |config| before passing it to the parent constructor.
44 // See implementation of Health for code samples.
45 Health(std::string_view instance_name, std::unique_ptr<struct healthd_config>&& config);
46
47 ndk::ScopedAStatus registerCallback(
48 const std::shared_ptr<IHealthInfoCallback>& callback) override;
49 ndk::ScopedAStatus unregisterCallback(
50 const std::shared_ptr<IHealthInfoCallback>& callback) override;
51 ndk::ScopedAStatus update() override;
52
53 // A subclass should not override this. Override UpdateHealthInfo instead.
54 ndk::ScopedAStatus getHealthInfo(HealthInfo* out) override final;
55
56 // A subclass is recommended to override the path in healthd_config in the constructor.
57 // Only override these if there are no suitable kernel interfaces to read these values.
58 ndk::ScopedAStatus getChargeCounterUah(int32_t* out) override;
59 ndk::ScopedAStatus getCurrentNowMicroamps(int32_t* out) override;
60 ndk::ScopedAStatus getCurrentAverageMicroamps(int32_t* out) override;
61 ndk::ScopedAStatus getCapacity(int32_t* out) override;
62 ndk::ScopedAStatus getChargeStatus(BatteryStatus* out) override;
63
64 // A subclass may either override these or provide function pointers in
65 // in healthd_config in the constructor.
66 // Prefer overriding these for efficiency.
67 ndk::ScopedAStatus getEnergyCounterNwh(int64_t* out) override;
68
69 // A subclass may override these for a specific device.
70 // The default implementations return nothing in |out|.
71 ndk::ScopedAStatus getDiskStats(std::vector<DiskStats>* out) override;
72 ndk::ScopedAStatus getStorageInfo(std::vector<StorageInfo>* out) override;
73
74 // A subclass may override these to provide a different implementation.
75 binder_status_t dump(int fd, const char** args, uint32_t num_args) override;
76
77 // HalHealthLoopCallback implementation.
78 void OnInit(HalHealthLoop* hal_health_loop, struct healthd_config* config) override;
79 void OnHealthInfoChanged(const HealthInfo& health_info) override;
80
81 // A subclass may override this if it wants to handle binder events differently.
82 virtual void BinderEvent(uint32_t epevents);
83
84 // A subclass may override this to determine whether screen should be kept on in charger mode.
85 // Default is to invoke healthd_config->screen_on() on the BatteryProperties converted
86 // from getHealthInfo.
87 // Prefer overriding these to providing screen_on in healthd_config in the constructor
88 // for efficiency.
89 virtual std::optional<bool> ShouldKeepScreenOn();
90
91 protected:
92 // A subclass can override this to modify any health info object before
93 // returning to clients. This is similar to healthd_board_battery_update().
94 // By default, it does nothing.
95 // See implementation of Health for code samples.
96 virtual void UpdateHealthInfo(HealthInfo* health_info);
97
98 private:
99 friend LinkedCallback; // for exposing death_recipient_
100
101 bool unregisterCallbackInternal(std::shared_ptr<IHealthInfoCallback> callback);
102
103 std::string instance_name_;
104 ::android::BatteryMonitor battery_monitor_;
105 std::unique_ptr<struct healthd_config> healthd_config_;
106
107 ndk::ScopedAIBinder_DeathRecipient death_recipient_;
108 int binder_fd_ = -1;
109 std::mutex callbacks_lock_;
110 std::vector<std::unique_ptr<LinkedCallback>> callbacks_;
111};
112
113} // namespace aidl::android::hardware::health