Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 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 | |
| 17 | #include "recovery_utils/battery_utils.h" |
| 18 | |
| 19 | #include <stdint.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <android-base/logging.h> |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 23 | #include <android/binder_manager.h> |
| 24 | #include <health-shim/shim.h> |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 25 | #include <healthhalutils/HealthHalUtils.h> |
| 26 | |
| 27 | BatteryInfo GetBatteryInfo() { |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 28 | using android::hardware::health::V2_0::get_health_service; |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 29 | 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 Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 35 | |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 36 | 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 Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 52 | |
| 53 | int wait_second = 0; |
| 54 | while (true) { |
| 55 | auto charge_status = BatteryStatus::UNKNOWN; |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 56 | 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 Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 64 | // Treat unknown status as on charger. See hardware/interfaces/health/aidl/BatteryStatus.aidl |
| 65 | // for the meaning of the return values. |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 66 | bool charging = (charge_status != BatteryStatus::DISCHARGING && |
| 67 | charge_status != BatteryStatus::NOT_CHARGING); |
| 68 | |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 69 | int32_t capacity = INT32_MIN; |
| 70 | if (health != nullptr) { |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 71 | auto res = health->getCapacity(&capacity); |
| 72 | if (!res.isOk()) { |
| 73 | LOG(WARNING) << "Unable to call getCapacity: " << res.getDescription(); |
| 74 | capacity = INT32_MIN; |
| 75 | } |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | LOG(INFO) << "charge_status " << toString(charge_status) << ", charging " << charging |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 79 | << ", capacity " << capacity; |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 80 | |
| 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 Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 86 | if (capacity == 50) { |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 87 | if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) { |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 88 | 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 Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 91 | 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 Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 98 | if (capacity == INT32_MIN) { |
| 99 | LOG(WARNING) << "Using fake battery capacity 100."; |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 100 | capacity = 100; |
| 101 | } |
| 102 | |
Yifan Hong | 67a8fd2 | 2021-11-30 16:39:00 -0800 | [diff] [blame] | 103 | LOG(INFO) << "GetBatteryInfo() reporting charging " << charging << ", capacity " << capacity; |
Tao Bao | 832c9cd | 2019-09-27 23:32:00 -0700 | [diff] [blame] | 104 | return BatteryInfo{ charging, capacity }; |
| 105 | } |
| 106 | } |