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 "android.hardware.health-service.cuttlefish"
17
18 #include <memory>
19 #include <string_view>
20
21 #include <android-base/logging.h>
22 #include <android/binder_interface_utils.h>
23 #include <health-impl/Health.h>
24 #include <health/utils.h>
25
26 using ::aidl::android::hardware::health::BatteryHealth;
27 using ::aidl::android::hardware::health::BatteryStatus;
28 using ::aidl::android::hardware::health::HalHealthLoop;
29 using ::aidl::android::hardware::health::Health;
30 using ::aidl::android::hardware::health::HealthInfo;
31 using ::aidl::android::hardware::health::IHealth;
32 using ::android::hardware::health::InitHealthdConfig;
33 using ::ndk::ScopedAStatus;
34 using ::ndk::SharedRefBase;
35 using namespace std::literals;
36
37 namespace aidl::android::hardware::health {
38
39 // Health HAL implementation for cuttlefish. Note that in this implementation,
40 // cuttlefish pretends to be a device with a battery being charged.
41 // Implementations on real devices should not insert these fake values. For
42 // example, a battery-less device should report batteryPresent = false and
43 // batteryStatus = UNKNOWN.
44
45 class HealthImpl : public Health {
46 public:
47 // Inherit constructor.
48 using Health::Health;
~HealthImpl()49 virtual ~HealthImpl() {}
50
51 ScopedAStatus getChargeCounterUah(int32_t* out) override;
52 ScopedAStatus getCurrentNowMicroamps(int32_t* out) override;
53 ScopedAStatus getCurrentAverageMicroamps(int32_t* out) override;
54 ScopedAStatus getCapacity(int32_t* out) override;
55 ScopedAStatus getChargeStatus(BatteryStatus* out) override;
56
57 protected:
58 void UpdateHealthInfo(HealthInfo* health_info) override;
59 };
60
UpdateHealthInfo(HealthInfo * health_info)61 void HealthImpl::UpdateHealthInfo(HealthInfo* health_info) {
62 health_info->chargerAcOnline = true;
63 health_info->chargerUsbOnline = true;
64 health_info->chargerWirelessOnline = false;
65 health_info->maxChargingCurrentMicroamps = 500000;
66 health_info->maxChargingVoltageMicrovolts = 5000000;
67 health_info->batteryStatus = BatteryStatus::CHARGING;
68 health_info->batteryHealth = BatteryHealth::GOOD;
69 health_info->batteryPresent = true;
70 health_info->batteryLevel = 85;
71 health_info->batteryVoltageMillivolts = 3600;
72 health_info->batteryTemperatureTenthsCelsius = 350;
73 health_info->batteryCurrentMicroamps = 400000;
74 health_info->batteryCycleCount = 32;
75 health_info->batteryFullChargeUah = 4000000;
76 health_info->batteryChargeCounterUah = 1900000;
77 health_info->batteryTechnology = "Li-ion";
78 }
79
getChargeCounterUah(int32_t * out)80 ScopedAStatus HealthImpl::getChargeCounterUah(int32_t* out) {
81 *out = 1900000;
82 return ScopedAStatus::ok();
83 }
84
getCurrentNowMicroamps(int32_t * out)85 ScopedAStatus HealthImpl::getCurrentNowMicroamps(int32_t* out) {
86 *out = 400000;
87 return ScopedAStatus::ok();
88 }
89
getCurrentAverageMicroamps(int32_t *)90 ScopedAStatus HealthImpl::getCurrentAverageMicroamps(int32_t*) {
91 return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
92 }
93
getCapacity(int32_t * out)94 ScopedAStatus HealthImpl::getCapacity(int32_t* out) {
95 *out = 85;
96 return ScopedAStatus::ok();
97 }
98
getChargeStatus(BatteryStatus * out)99 ScopedAStatus HealthImpl::getChargeStatus(BatteryStatus* out) {
100 *out = BatteryStatus::CHARGING;
101 return ScopedAStatus::ok();
102 }
103
104 } // namespace aidl::android::hardware::health
105
main(int,char ** argv)106 int main(int, [[maybe_unused]] char** argv) {
107 #ifdef __ANDROID_RECOVERY__
108 android::base::InitLogging(argv, android::base::KernelLogger);
109 #endif
110 // Cuttlefish does not support offline-charging mode, hence do not handle
111 // --charger option.
112 using aidl::android::hardware::health::HealthImpl;
113 LOG(INFO) << "Starting health HAL.";
114 auto config = std::make_unique<healthd_config>();
115 InitHealthdConfig(config.get());
116 auto binder = SharedRefBase::make<HealthImpl>("default", std::move(config));
117 auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, binder);
118 return hal_health_loop->StartLoop();
119 }
120