1 /*
2  * Copyright (C) 2019 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 <android-base/unique_fd.h>
20 #include <android/hardware/health/2.1/IHealth.h>
21 #include <healthd/healthd.h>
22 
23 #include <health2impl/Callback.h>
24 #include <health2impl/HalHealthLoop.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace health {
29 namespace V2_1 {
30 namespace implementation {
31 
32 // binderized health HAL implementation.
33 class BinderHealth : public HalHealthLoop, public IHealth, public hidl_death_recipient {
34   public:
35     // |impl| should be the passthrough implementation.
36     BinderHealth(const std::string& name, const sp<IHealth>& impl);
37 
38     // Methods from ::android::hardware::health::V2_0::IHealth follow.
39     Return<::android::hardware::health::V2_0::Result> registerCallback(
40             const sp<::android::hardware::health::V2_0::IHealthInfoCallback>& callback) override;
41     Return<::android::hardware::health::V2_0::Result> unregisterCallback(
42             const sp<::android::hardware::health::V2_0::IHealthInfoCallback>& callback) override;
43     Return<::android::hardware::health::V2_0::Result> update() override;
getChargeCounter(getChargeCounter_cb _hidl_cb)44     Return<void> getChargeCounter(getChargeCounter_cb _hidl_cb) override {
45         return service()->getChargeCounter(_hidl_cb);
46     }
getCurrentNow(getCurrentNow_cb _hidl_cb)47     Return<void> getCurrentNow(getCurrentNow_cb _hidl_cb) override {
48         return service()->getCurrentNow(_hidl_cb);
49     }
getCurrentAverage(getCurrentAverage_cb _hidl_cb)50     Return<void> getCurrentAverage(getCurrentAverage_cb _hidl_cb) override {
51         return service()->getCurrentAverage(_hidl_cb);
52     }
getCapacity(getCapacity_cb _hidl_cb)53     Return<void> getCapacity(getCapacity_cb _hidl_cb) override {
54         return service()->getCapacity(_hidl_cb);
55     }
getEnergyCounter(getEnergyCounter_cb _hidl_cb)56     Return<void> getEnergyCounter(getEnergyCounter_cb _hidl_cb) override {
57         return service()->getEnergyCounter(_hidl_cb);
58     }
getChargeStatus(getChargeStatus_cb _hidl_cb)59     Return<void> getChargeStatus(getChargeStatus_cb _hidl_cb) override {
60         return service()->getChargeStatus(_hidl_cb);
61     }
getStorageInfo(getStorageInfo_cb _hidl_cb)62     Return<void> getStorageInfo(getStorageInfo_cb _hidl_cb) override {
63         return service()->getStorageInfo(_hidl_cb);
64     }
getDiskStats(getDiskStats_cb _hidl_cb)65     Return<void> getDiskStats(getDiskStats_cb _hidl_cb) override {
66         return service()->getDiskStats(_hidl_cb);
67     }
getHealthInfo(getHealthInfo_cb _hidl_cb)68     Return<void> getHealthInfo(getHealthInfo_cb _hidl_cb) override {
69         return service()->getHealthInfo(_hidl_cb);
70     }
71 
72     // Methods from ::android::hardware::health::V2_1::IHealth follow.
getHealthConfig(getHealthConfig_cb _hidl_cb)73     Return<void> getHealthConfig(getHealthConfig_cb _hidl_cb) override {
74         return service()->getHealthConfig(_hidl_cb);
75     }
getHealthInfo_2_1(getHealthInfo_2_1_cb _hidl_cb)76     Return<void> getHealthInfo_2_1(getHealthInfo_2_1_cb _hidl_cb) override {
77         return service()->getHealthInfo_2_1(_hidl_cb);
78     }
shouldKeepScreenOn(shouldKeepScreenOn_cb _hidl_cb)79     Return<void> shouldKeepScreenOn(shouldKeepScreenOn_cb _hidl_cb) override {
80         return service()->shouldKeepScreenOn(_hidl_cb);
81     }
82 
83     // Methods from ::android::hidl::base::V1_0::IBase follow.
debug(const hidl_handle & fd,const hidl_vec<hidl_string> & args)84     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override {
85         return service()->debug(fd, args);
86     }
87 
88     // hidl_death_recipient implementation.
89     void serviceDied(uint64_t cookie, const wp<IBase>& who) override;
90 
91     // Called by BinderHealthCallback.
92     void OnHealthInfoChanged(const HealthInfo& health_info) override;
93 
94   protected:
95     void Init(struct healthd_config* config) override;
96     int PrepareToWait() override;
97     // A subclass may override this if it wants to handle binder events differently.
98     virtual void BinderEvent(uint32_t epevents);
99 
100   private:
101     bool unregisterCallbackInternal(const sp<IBase>& callback);
102     int binder_fd_ = -1;
103     std::mutex callbacks_lock_;
104     std::vector<std::unique_ptr<Callback>> callbacks_;
105 };
106 
107 }  // namespace implementation
108 }  // namespace V2_1
109 }  // namespace health
110 }  // namespace hardware
111 }  // namespace android
112