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 #pragma once
17 
18 #include <memory>
19 #include <mutex>
20 #include <vector>
21 
22 #include <android-base/unique_fd.h>
23 #include <android/hardware/health/2.1/IHealth.h>
24 #include <healthd/BatteryMonitor.h>
25 #include <hidl/Status.h>
26 
27 #include <health2impl/Callback.h>
28 
29 using ::android::sp;
30 using ::android::hardware::hidl_string;
31 using ::android::hardware::hidl_vec;
32 using ::android::hardware::Return;
33 using ::android::hardware::Void;
34 using ::android::hidl::base::V1_0::IBase;
35 
36 namespace android {
37 namespace hardware {
38 namespace health {
39 namespace V2_1 {
40 namespace implementation {
41 
42 class Health : public IHealth {
43   public:
44     Health(std::unique_ptr<healthd_config>&& config);
45 
46     // Methods from ::android::hardware::health::V2_0::IHealth follow.
47     Return<::android::hardware::health::V2_0::Result> registerCallback(
48             const sp<::android::hardware::health::V2_0::IHealthInfoCallback>& callback) override;
49     Return<::android::hardware::health::V2_0::Result> unregisterCallback(
50             const sp<::android::hardware::health::V2_0::IHealthInfoCallback>& callback) override;
51     Return<::android::hardware::health::V2_0::Result> update() override;
52     Return<void> getChargeCounter(getChargeCounter_cb _hidl_cb) override;
53     Return<void> getCurrentNow(getCurrentNow_cb _hidl_cb) override;
54     Return<void> getCurrentAverage(getCurrentAverage_cb _hidl_cb) override;
55     Return<void> getCapacity(getCapacity_cb _hidl_cb) override;
56     Return<void> getEnergyCounter(getEnergyCounter_cb _hidl_cb) override;
57     Return<void> getChargeStatus(getChargeStatus_cb _hidl_cb) override;
58     Return<void> getStorageInfo(getStorageInfo_cb _hidl_cb) override;
59     Return<void> getDiskStats(getDiskStats_cb _hidl_cb) override;
60     Return<void> getHealthInfo(getHealthInfo_cb _hidl_cb) override;
61 
62     // Methods from ::android::hardware::health::V2_1::IHealth follow.
63     Return<void> getHealthConfig(getHealthConfig_cb _hidl_cb) override;
64     Return<void> getHealthInfo_2_1(getHealthInfo_2_1_cb _hidl_cb) override;
65     Return<void> shouldKeepScreenOn(shouldKeepScreenOn_cb _hidl_cb) override;
66 
67     // Methods from ::android::hidl::base::V1_0::IBase follow.
68     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;
69 
70   protected:
71     // A subclass can override this to modify any health info object before
72     // returning to clients. This is similar to healthd_board_battery_update().
73     // By default, it does nothing.
74     virtual void UpdateHealthInfo(HealthInfo* health_info);
75 
76   private:
77     bool unregisterCallbackInternal(const sp<IBase>& callback);
78 
79     BatteryMonitor battery_monitor_;
80     std::unique_ptr<healthd_config> healthd_config_;
81 
82     std::mutex callbacks_lock_;
83     std::vector<std::unique_ptr<Callback>> callbacks_;
84 };
85 
86 }  // namespace implementation
87 }  // namespace V2_1
88 }  // namespace health
89 }  // namespace hardware
90 }  // namespace android
91