• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
29 namespace aidl::android::hardware::health {
30 
31 class 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.
40 class 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     virtual ~Health();
47 
48     ndk::ScopedAStatus registerCallback(
49             const std::shared_ptr<IHealthInfoCallback>& callback) override;
50     ndk::ScopedAStatus unregisterCallback(
51             const std::shared_ptr<IHealthInfoCallback>& callback) override;
52     ndk::ScopedAStatus update() override;
53 
54     // A subclass should not override this. Override UpdateHealthInfo instead.
55     ndk::ScopedAStatus getHealthInfo(HealthInfo* out) override final;
56 
57     // A subclass is recommended to override the path in healthd_config in the constructor.
58     // Only override these if there are no suitable kernel interfaces to read these values.
59     ndk::ScopedAStatus getChargeCounterUah(int32_t* out) override;
60     ndk::ScopedAStatus getCurrentNowMicroamps(int32_t* out) override;
61     ndk::ScopedAStatus getCurrentAverageMicroamps(int32_t* out) override;
62     ndk::ScopedAStatus getCapacity(int32_t* out) override;
63     ndk::ScopedAStatus getChargeStatus(BatteryStatus* out) override;
64 
65     // A subclass may either override these or provide function pointers in
66     // in healthd_config in the constructor.
67     // Prefer overriding these for efficiency.
68     ndk::ScopedAStatus getEnergyCounterNwh(int64_t* out) override;
69 
70     // A subclass may override these for a specific device.
71     // The default implementations return nothing in |out|.
72     ndk::ScopedAStatus getDiskStats(std::vector<DiskStats>* out) override;
73     ndk::ScopedAStatus getStorageInfo(std::vector<StorageInfo>* out) override;
74 
75     // A subclass may override these to provide a different implementation.
76     binder_status_t dump(int fd, const char** args, uint32_t num_args) override;
77 
78     // HalHealthLoopCallback implementation.
79     void OnInit(HalHealthLoop* hal_health_loop, struct healthd_config* config) override;
80     void OnHealthInfoChanged(const HealthInfo& health_info) override;
81 
82     // A subclass may override this if it wants to handle binder events differently.
83     virtual void BinderEvent(uint32_t epevents);
84 
85     // A subclass may override this to determine whether screen should be kept on in charger mode.
86     // Default is to invoke healthd_config->screen_on() on the BatteryProperties converted
87     // from getHealthInfo.
88     // Prefer overriding these to providing screen_on in healthd_config in the constructor
89     // for efficiency.
90     virtual std::optional<bool> ShouldKeepScreenOn();
91 
92   protected:
93     // A subclass can override this to modify any health info object before
94     // returning to clients. This is similar to healthd_board_battery_update().
95     // By default, it does nothing.
96     // See implementation of Health for code samples.
97     virtual void UpdateHealthInfo(HealthInfo* health_info);
98 
99   private:
100     friend LinkedCallback;  // for exposing death_recipient_
101 
102     bool unregisterCallbackInternal(std::shared_ptr<IHealthInfoCallback> callback);
103 
104     std::string instance_name_;
105     ::android::BatteryMonitor battery_monitor_;
106     std::unique_ptr<struct healthd_config> healthd_config_;
107 
108     ndk::ScopedAIBinder_DeathRecipient death_recipient_;
109     int binder_fd_ = -1;
110     std::mutex callbacks_lock_;
111     std::vector<std::unique_ptr<LinkedCallback>> callbacks_;
112 };
113 
114 }  // namespace aidl::android::hardware::health
115