• 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     ndk::ScopedAStatus setChargingPolicy(BatteryChargingPolicy in_value) override;
76     ndk::ScopedAStatus getChargingPolicy(BatteryChargingPolicy* out) override;
77     ndk::ScopedAStatus getBatteryHealthData(BatteryHealthData* out) override;
78 
79     // A subclass may override these to provide a different implementation.
80     binder_status_t dump(int fd, const char** args, uint32_t num_args) override;
81 
82     // HalHealthLoopCallback implementation.
83     void OnInit(HalHealthLoop* hal_health_loop, struct healthd_config* config) override;
84     void OnHealthInfoChanged(const HealthInfo& health_info) override;
85 
86     // A subclass may override this if it wants to handle binder events differently.
87     virtual void BinderEvent(uint32_t epevents);
88 
89     // A subclass may override this to determine whether screen should be kept on in charger mode.
90     // Default is to invoke healthd_config->screen_on() on the BatteryProperties converted
91     // from getHealthInfo.
92     // Prefer overriding these to providing screen_on in healthd_config in the constructor
93     // for efficiency.
94     virtual std::optional<bool> ShouldKeepScreenOn();
95 
96   protected:
97     // A subclass can override this to modify any health info object before
98     // returning to clients. This is similar to healthd_board_battery_update().
99     // By default, it does nothing.
100     // See implementation of Health for code samples.
101     virtual void UpdateHealthInfo(HealthInfo* health_info);
102 
103   private:
104     friend LinkedCallback;  // for exposing death_recipient_
105 
106     bool unregisterCallbackInternal(std::shared_ptr<IHealthInfoCallback> callback);
107 
108     std::string instance_name_;
109     ::android::BatteryMonitor battery_monitor_;
110     std::unique_ptr<struct healthd_config> healthd_config_;
111 
112     ndk::ScopedAIBinder_DeathRecipient death_recipient_;
113     int binder_fd_ = -1;
114     std::mutex callbacks_lock_;
115     std::vector<std::unique_ptr<LinkedCallback>> callbacks_;
116 };
117 
118 }  // namespace aidl::android::hardware::health
119