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