blob: 6b126bdf3f5508e165e09a6135c8808a466c16dd [file] [log] [blame]
Tao Bao832c9cd2019-09-27 23:32:00 -07001/*
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#include "recovery_utils/battery_utils.h"
18
19#include <stdint.h>
20#include <unistd.h>
21
22#include <android-base/logging.h>
Yifan Hong67a8fd22021-11-30 16:39:00 -080023#include <android/binder_manager.h>
24#include <health-shim/shim.h>
Tao Bao832c9cd2019-09-27 23:32:00 -070025#include <healthhalutils/HealthHalUtils.h>
26
27BatteryInfo GetBatteryInfo() {
Tao Bao832c9cd2019-09-27 23:32:00 -070028 using android::hardware::health::V2_0::get_health_service;
Yifan Hong67a8fd22021-11-30 16:39:00 -080029 using HidlHealth = android::hardware::health::V2_0::IHealth;
30 using aidl::android::hardware::health::BatteryStatus;
31 using aidl::android::hardware::health::HealthShim;
32 using aidl::android::hardware::health::IHealth;
33 using aidl::android::hardware::health::toString;
34 using std::string_literals::operator""s;
Tao Bao832c9cd2019-09-27 23:32:00 -070035
Yifan Hong67a8fd22021-11-30 16:39:00 -080036 auto service_name = IHealth::descriptor + "/default"s;
37 std::shared_ptr<IHealth> health;
38 if (AServiceManager_isDeclared(service_name.c_str())) {
39 ndk::SpAIBinder binder(AServiceManager_waitForService(service_name.c_str()));
40 health = IHealth::fromBinder(binder);
41 }
42 if (health == nullptr) {
43 LOG(INFO) << "Unable to get AIDL health service, trying HIDL...";
44 android::sp<HidlHealth> hidl_health = get_health_service();
45 if (hidl_health != nullptr) {
46 health = ndk::SharedRefBase::make<HealthShim>(hidl_health);
47 }
48 }
49 if (health == nullptr) {
50 LOG(WARNING) << "No health implementation is found; assuming defaults";
51 }
Tao Bao832c9cd2019-09-27 23:32:00 -070052
53 int wait_second = 0;
54 while (true) {
55 auto charge_status = BatteryStatus::UNKNOWN;
Yifan Hong67a8fd22021-11-30 16:39:00 -080056 if (health != nullptr) {
57 auto res = health->getChargeStatus(&charge_status);
58 if (!res.isOk()) {
59 LOG(WARNING) << "Unable to call getChargeStatus: " << res.getDescription();
60 charge_status = BatteryStatus::UNKNOWN;
61 }
Tao Bao832c9cd2019-09-27 23:32:00 -070062 }
63
Yifan Hong67a8fd22021-11-30 16:39:00 -080064 // Treat unknown status as on charger. See hardware/interfaces/health/aidl/BatteryStatus.aidl
65 // for the meaning of the return values.
Tao Bao832c9cd2019-09-27 23:32:00 -070066 bool charging = (charge_status != BatteryStatus::DISCHARGING &&
67 charge_status != BatteryStatus::NOT_CHARGING);
68
Tao Bao832c9cd2019-09-27 23:32:00 -070069 int32_t capacity = INT32_MIN;
70 if (health != nullptr) {
Yifan Hong67a8fd22021-11-30 16:39:00 -080071 auto res = health->getCapacity(&capacity);
72 if (!res.isOk()) {
73 LOG(WARNING) << "Unable to call getCapacity: " << res.getDescription();
74 capacity = INT32_MIN;
75 }
Tao Bao832c9cd2019-09-27 23:32:00 -070076 }
77
78 LOG(INFO) << "charge_status " << toString(charge_status) << ", charging " << charging
Yifan Hong67a8fd22021-11-30 16:39:00 -080079 << ", capacity " << capacity;
Tao Bao832c9cd2019-09-27 23:32:00 -070080
81 constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10;
82 // At startup, the battery drivers in devices like N5X/N6P take some time to load
83 // the battery profile. Before the load finishes, it reports value 50 as a fake
84 // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected
85 // to finish loading the battery profile earlier than 10 seconds after kernel startup.
Yifan Hong67a8fd22021-11-30 16:39:00 -080086 if (capacity == 50) {
Tao Bao832c9cd2019-09-27 23:32:00 -070087 if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) {
Yifan Hong67a8fd22021-11-30 16:39:00 -080088 LOG(INFO) << "Battery capacity == 50, waiting "
89 << (BATTERY_READ_TIMEOUT_IN_SEC - wait_second)
90 << " seconds to ensure this is not a fake value...";
Tao Bao832c9cd2019-09-27 23:32:00 -070091 sleep(1);
92 wait_second++;
93 continue;
94 }
95 }
96 // If we can't read battery percentage, it may be a device without battery. In this
97 // situation, use 100 as a fake battery percentage.
Yifan Hong67a8fd22021-11-30 16:39:00 -080098 if (capacity == INT32_MIN) {
99 LOG(WARNING) << "Using fake battery capacity 100.";
Tao Bao832c9cd2019-09-27 23:32:00 -0700100 capacity = 100;
101 }
102
Yifan Hong67a8fd22021-11-30 16:39:00 -0800103 LOG(INFO) << "GetBatteryInfo() reporting charging " << charging << ", capacity " << capacity;
Tao Bao832c9cd2019-09-27 23:32:00 -0700104 return BatteryInfo{ charging, capacity };
105 }
106}