1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "[email protected]"
17 
18 #include <memory>
19 #include <string_view>
20 
21 #include <android-base/logging.h>
22 #include <health/utils.h>
23 #include <health2impl/Health.h>
24 
25 using ::android::sp;
26 using ::android::hardware::Return;
27 using ::android::hardware::Void;
28 using ::android::hardware::health::InitHealthdConfig;
29 using ::android::hardware::health::V1_0::BatteryStatus;
30 using ::android::hardware::health::V2_0::Result;
31 using ::android::hardware::health::V2_1::IHealth;
32 using namespace std::literals;
33 
34 namespace android {
35 namespace hardware {
36 namespace health {
37 namespace V2_1 {
38 namespace implementation {
39 
40 // Health HAL implementation for cuttlefish. Note that in this implementation, cuttlefish
41 // pretends to be a device with a battery being charged. Implementations on real devices
42 // should not insert these fake values. For example, a battery-less device should report
43 // batteryPresent = false and batteryStatus = UNKNOWN.
44 
45 class HealthImpl : public Health {
46  public:
HealthImpl(std::unique_ptr<healthd_config> && config)47   HealthImpl(std::unique_ptr<healthd_config>&& config)
48     : Health(std::move(config)) {}
49     Return<void> getChargeCounter(getChargeCounter_cb _hidl_cb) override;
50     Return<void> getCurrentNow(getCurrentNow_cb _hidl_cb) override;
51     Return<void> getCapacity(getCapacity_cb _hidl_cb) override;
52     Return<void> getChargeStatus(getChargeStatus_cb _hidl_cb) override;
53  protected:
54   void UpdateHealthInfo(HealthInfo* health_info) override;
55 };
56 
UpdateHealthInfo(HealthInfo * health_info)57 void HealthImpl::UpdateHealthInfo(HealthInfo* health_info) {
58   auto* battery_props = &health_info->legacy.legacy;
59   battery_props->chargerAcOnline = true;
60   battery_props->chargerUsbOnline = true;
61   battery_props->chargerWirelessOnline = false;
62   battery_props->maxChargingCurrent = 500000;
63   battery_props->maxChargingVoltage = 5000000;
64   battery_props->batteryStatus = V1_0::BatteryStatus::CHARGING;
65   battery_props->batteryHealth = V1_0::BatteryHealth::GOOD;
66   battery_props->batteryPresent = true;
67   battery_props->batteryLevel = 85;
68   battery_props->batteryVoltage = 3600;
69   battery_props->batteryTemperature = 350;
70   battery_props->batteryCurrent = 400000;
71   battery_props->batteryCycleCount = 32;
72   battery_props->batteryFullCharge = 4000000;
73   battery_props->batteryChargeCounter = 1900000;
74   battery_props->batteryTechnology = "Li-ion";
75 }
76 
getChargeCounter(getChargeCounter_cb _hidl_cb)77 Return<void> HealthImpl::getChargeCounter(getChargeCounter_cb _hidl_cb) {
78   _hidl_cb(Result::SUCCESS, 1900000);
79   return Void();
80 }
81 
getCurrentNow(getCurrentNow_cb _hidl_cb)82 Return<void> HealthImpl::getCurrentNow(getCurrentNow_cb _hidl_cb) {
83   _hidl_cb(Result::SUCCESS, 400000);
84   return Void();
85 }
86 
getCapacity(getCapacity_cb _hidl_cb)87 Return<void> HealthImpl::getCapacity(getCapacity_cb _hidl_cb) {
88   _hidl_cb(Result::SUCCESS, 85);
89   return Void();
90 }
91 
getChargeStatus(getChargeStatus_cb _hidl_cb)92 Return<void> HealthImpl::getChargeStatus(getChargeStatus_cb _hidl_cb) {
93   _hidl_cb(Result::SUCCESS, BatteryStatus::CHARGING);
94   return Void();
95 }
96 
97 }  // namespace implementation
98 }  // namespace V2_1
99 }  // namespace health
100 }  // namespace hardware
101 }  // namespace android
102 
103 
HIDL_FETCH_IHealth(const char * instance)104 extern "C" IHealth* HIDL_FETCH_IHealth(const char* instance) {
105   using ::android::hardware::health::V2_1::implementation::HealthImpl;
106   if (instance != "default"sv) {
107       return nullptr;
108   }
109   auto config = std::make_unique<healthd_config>();
110   InitHealthdConfig(config.get());
111 
112   return new HealthImpl(std::move(config));
113 }
114