1 /*
2  * Copyright (C) 2024 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 #include <memory>
18 #include <string_view>
19 
20 #include <android/binder_interface_utils.h>
21 #include <health-impl/Health.h>
22 #include <health/utils.h>
23 
24 using ::aidl::android::hardware::health::BatteryHealth;
25 using ::aidl::android::hardware::health::BatteryStatus;
26 using ::aidl::android::hardware::health::HalHealthLoop;
27 using ::aidl::android::hardware::health::Health;
28 using ::aidl::android::hardware::health::HealthInfo;
29 using ::aidl::android::hardware::health::IHealth;
30 using ::android::hardware::health::InitHealthdConfig;
31 using ::ndk::ScopedAStatus;
32 using ::ndk::SharedRefBase;
33 using namespace std::literals;
34 
35 namespace aidl::android::hardware::health {
36 
37 class HealthImpl : public Health {
38   public:
39     // Use default constructor from IHealth
40     using Health::Health;
~HealthImpl()41     virtual ~HealthImpl() {}
42 
43     ScopedAStatus getChargeCounterUah(int32_t* out) override;
44     ScopedAStatus getCurrentNowMicroamps(int32_t* out) override;
45     ScopedAStatus getCurrentAverageMicroamps(int32_t* out) override;
46     ScopedAStatus getCapacity(int32_t* out) override;
47     ScopedAStatus getChargeStatus(BatteryStatus* out) override;
48     ScopedAStatus getEnergyCounterNwh(int64_t* out) override;
49 
50   protected:
51     void UpdateHealthInfo(HealthInfo* health_info) override;
52 };
53 
getChargeCounterUah(int32_t * out)54 ScopedAStatus HealthImpl::getChargeCounterUah(int32_t* out) {
55     *out = 10000;  // 10 Ah
56     return ScopedAStatus::ok();
57 }
58 
getCurrentNowMicroamps(int32_t * out)59 ScopedAStatus HealthImpl::getCurrentNowMicroamps(int32_t* out) {
60     *out = 900000;  // 0.9A
61     return ScopedAStatus::ok();
62 }
63 
getCurrentAverageMicroamps(int32_t * out)64 ScopedAStatus HealthImpl::getCurrentAverageMicroamps(int32_t* out) {
65     *out = 900000;  // 0.9A
66     return ScopedAStatus::ok();
67 }
68 
getCapacity(int32_t * out)69 ScopedAStatus HealthImpl::getCapacity(int32_t* out) {
70     *out = 100;
71     return ScopedAStatus::ok();
72 }
73 
getChargeStatus(BatteryStatus * out)74 ScopedAStatus HealthImpl::getChargeStatus(BatteryStatus* out) {
75     *out = BatteryStatus::CHARGING;
76     return ScopedAStatus::ok();
77 }
78 
getEnergyCounterNwh(int64_t * out)79 ScopedAStatus HealthImpl::getEnergyCounterNwh(int64_t* out) {
80     *out = LONG_MAX;
81     return ScopedAStatus::ok();
82 }
83 
UpdateHealthInfo(HealthInfo * health_info)84 void HealthImpl::UpdateHealthInfo(HealthInfo* health_info) {
85     health_info->chargerAcOnline = true;
86     health_info->batteryStatus = BatteryStatus::CHARGING;
87     health_info->batteryLevel = 100;
88     health_info->batteryChargeTimeToFullNowSeconds = 0;
89 }
90 }  // namespace aidl::android::hardware::health
91 
main(int,char **)92 int main(int /* argc */, char** /* argv */) {
93     // Automotive does not support offline-charging mode, hence do not handle
94     // --charger option.
95     using aidl::android::hardware::health::HealthImpl;
96 
97     auto config = std::make_unique<healthd_config>();
98     InitHealthdConfig(config.get());
99     auto binder = SharedRefBase::make<HealthImpl>("default", std::move(config));
100     auto hal_health_loop = std::make_shared<HalHealthLoop>(binder, binder);
101     return hal_health_loop->StartLoop();
102 }
103