| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #define LOG_TAG "healthd" |
| 18 | |
| Yabin Cui | e98e177 | 2016-02-17 12:21:34 -0800 | [diff] [blame] | 19 | #include <healthd/healthd.h> |
| 20 | #include <healthd/BatteryMonitor.h> |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 21 | |
| 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| Thierry Strudel | f73de6f | 2019-01-11 17:09:20 -0800 | [diff] [blame] | 29 | |
| 30 | #include <algorithm> |
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 31 | #include <memory> |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 32 | #include <optional> |
| Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 33 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 34 | #include <aidl/android/hardware/health/HealthInfo.h> |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 35 | #include <android-base/file.h> |
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 36 | #include <android-base/parseint.h> |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 37 | #include <android-base/strings.h> |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 38 | #include <android/hardware/health/2.1/types.h> |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 39 | #include <android/hardware/health/translate-ndk.h> |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 40 | #include <batteryservice/BatteryService.h> |
| 41 | #include <cutils/klog.h> |
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 42 | #include <cutils/properties.h> |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 43 | #include <utils/Errors.h> |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 44 | #include <utils/String8.h> |
| 45 | #include <utils/Vector.h> |
| 46 | |
| 47 | #define POWER_SUPPLY_SUBSYSTEM "power_supply" |
| 48 | #define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM |
| Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 49 | #define FAKE_BATTERY_CAPACITY 42 |
| 50 | #define FAKE_BATTERY_TEMPERATURE 424 |
| Ruchi Kandoi | 5c09ec1 | 2016-02-25 16:19:30 -0800 | [diff] [blame] | 51 | #define MILLION 1.0e6 |
| Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 52 | #define DEFAULT_VBUS_VOLTAGE 5000000 |
| Stefan Berger | 06c6f50 | 2019-04-06 11:05:19 +0200 | [diff] [blame] | 53 | #ifdef HEALTHD_USE_BATTERY_INFO |
| 54 | #define SYSFS_BATTERY_CURRENT "/sys/class/power_supply/battery/current_now" |
| 55 | #define SYSFS_BATTERY_VOLTAGE "/sys/class/power_supply/battery/voltage_now" |
| 56 | #endif |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 57 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 58 | using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo; |
| 59 | using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo; |
| 60 | using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo; |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 61 | using aidl::android::hardware::health::BatteryCapacityLevel; |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 62 | using aidl::android::hardware::health::BatteryChargingPolicy; |
| 63 | using aidl::android::hardware::health::BatteryChargingState; |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 64 | using aidl::android::hardware::health::BatteryHealth; |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 65 | using aidl::android::hardware::health::BatteryHealthData; |
| David Anderson | d5ed26a | 2023-12-08 15:12:24 -0800 | [diff] [blame] | 66 | using aidl::android::hardware::health::BatteryPartStatus; |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 67 | using aidl::android::hardware::health::BatteryStatus; |
| 68 | using aidl::android::hardware::health::HealthInfo; |
| 69 | |
| 70 | namespace { |
| 71 | |
| 72 | // Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls. |
| 73 | // Skips storageInfo and diskStats. |
| 74 | void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in, |
| 75 | ::android::hardware::health::V1_0::HealthInfo* out) { |
| 76 | out->chargerAcOnline = in.chargerAcOnline; |
| 77 | out->chargerUsbOnline = in.chargerUsbOnline; |
| 78 | out->chargerWirelessOnline = in.chargerWirelessOnline; |
| 79 | out->maxChargingCurrent = in.maxChargingCurrentMicroamps; |
| 80 | out->maxChargingVoltage = in.maxChargingVoltageMicrovolts; |
| 81 | out->batteryStatus = |
| 82 | static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus); |
| 83 | out->batteryHealth = |
| 84 | static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth); |
| 85 | out->batteryPresent = in.batteryPresent; |
| 86 | out->batteryLevel = in.batteryLevel; |
| 87 | out->batteryVoltage = in.batteryVoltageMillivolts; |
| 88 | out->batteryTemperature = in.batteryTemperatureTenthsCelsius; |
| 89 | out->batteryCurrent = in.batteryCurrentMicroamps; |
| 90 | out->batteryCycleCount = in.batteryCycleCount; |
| 91 | out->batteryFullCharge = in.batteryFullChargeUah; |
| 92 | out->batteryChargeCounter = in.batteryChargeCounterUah; |
| 93 | out->batteryTechnology = in.batteryTechnology; |
| 94 | } |
| 95 | |
| 96 | void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in, |
| 97 | ::android::hardware::health::V2_0::HealthInfo* out) { |
| 98 | translateToHidl(in, &out->legacy); |
| 99 | out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps; |
| 100 | // Skip storageInfo and diskStats |
| 101 | } |
| 102 | |
| 103 | void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in, |
| 104 | ::android::hardware::health::V2_1::HealthInfo* out) { |
| 105 | translateToHidl(in, &out->legacy); |
| 106 | out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>( |
| 107 | in.batteryCapacityLevel); |
| 108 | out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds; |
| 109 | out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah; |
| 110 | } |
| 111 | |
| 112 | } // namespace |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 113 | |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 114 | namespace android { |
| 115 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 116 | template <typename T> |
| 117 | struct SysfsStringEnumMap { |
| Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 118 | const char* s; |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 119 | T val; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 122 | template <typename T> |
| 123 | static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 124 | for (int i = 0; map[i].s; i++) |
| 125 | if (!strcmp(str, map[i].s)) |
| 126 | return map[i].val; |
| 127 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 128 | return std::nullopt; |
| Yabin Cui | db04a49 | 2016-02-16 17:19:23 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 131 | static void initHealthInfo(HealthInfo* health_info) { |
| Bart Van Assche | 024e18f | 2022-02-24 21:22:07 +0000 | [diff] [blame] | 132 | *health_info = { |
| 133 | .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED, |
| 134 | .batteryChargeTimeToFullNowSeconds = |
| 135 | (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED, |
| 136 | .batteryStatus = BatteryStatus::UNKNOWN, |
| 137 | .batteryHealth = BatteryHealth::UNKNOWN, |
| Andrei Ciubotariu | 515f5b5 | 2025-02-12 21:31:23 -0800 | [diff] [blame] | 138 | .batteryHealthData = std::nullopt, |
| Bart Van Assche | 024e18f | 2022-02-24 21:22:07 +0000 | [diff] [blame] | 139 | }; |
| Yifan Hong | 6cabe9b | 2019-11-05 17:04:50 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| Todd Poynor | e030a10 | 2018-01-19 14:03:59 -0800 | [diff] [blame] | 142 | BatteryMonitor::BatteryMonitor() |
| 143 | : mHealthdConfig(nullptr), |
| 144 | mBatteryDevicePresent(false), |
| 145 | mBatteryFixedCapacity(0), |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 146 | mBatteryFixedTemperature(0), |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 147 | mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN), |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 148 | mHealthInfo(std::make_unique<HealthInfo>()) { |
| Yifan Hong | 6cabe9b | 2019-11-05 17:04:50 -0800 | [diff] [blame] | 149 | initHealthInfo(mHealthInfo.get()); |
| 150 | } |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 151 | |
| 152 | BatteryMonitor::~BatteryMonitor() {} |
| 153 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 154 | HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const { |
| 155 | HealthInfo_1_0 health_info_1_0; |
| 156 | translateToHidl(*mHealthInfo, &health_info_1_0); |
| 157 | return health_info_1_0; |
| Yabin Cui | db04a49 | 2016-02-16 17:19:23 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 160 | HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const { |
| 161 | HealthInfo_2_0 health_info_2_0; |
| 162 | translateToHidl(*mHealthInfo, &health_info_2_0); |
| 163 | return health_info_2_0; |
| Hridya Valsaraju | 7fa7225 | 2018-01-12 17:44:33 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 166 | HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const { |
| 167 | HealthInfo_2_1 health_info_2_1; |
| 168 | translateToHidl(*mHealthInfo, &health_info_2_1); |
| 169 | return health_info_2_1; |
| 170 | } |
| 171 | |
| 172 | const HealthInfo& BatteryMonitor::getHealthInfo() const { |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 173 | return *mHealthInfo; |
| 174 | } |
| 175 | |
| 176 | BatteryStatus getBatteryStatus(const char* status) { |
| 177 | static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = { |
| 178 | {"Unknown", BatteryStatus::UNKNOWN}, |
| 179 | {"Charging", BatteryStatus::CHARGING}, |
| 180 | {"Discharging", BatteryStatus::DISCHARGING}, |
| 181 | {"Not charging", BatteryStatus::NOT_CHARGING}, |
| 182 | {"Full", BatteryStatus::FULL}, |
| 183 | {NULL, BatteryStatus::UNKNOWN}, |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 186 | auto ret = mapSysfsString(status, batteryStatusMap); |
| 187 | if (!ret) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 188 | KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status); |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 189 | *ret = BatteryStatus::UNKNOWN; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 192 | return *ret; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 195 | BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) { |
| 196 | static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = { |
| 197 | {"Unknown", BatteryCapacityLevel::UNKNOWN}, |
| 198 | {"Critical", BatteryCapacityLevel::CRITICAL}, |
| 199 | {"Low", BatteryCapacityLevel::LOW}, |
| 200 | {"Normal", BatteryCapacityLevel::NORMAL}, |
| 201 | {"High", BatteryCapacityLevel::HIGH}, |
| 202 | {"Full", BatteryCapacityLevel::FULL}, |
| Stephane Lee | 0684604 | 2020-02-12 17:00:24 -0800 | [diff] [blame] | 203 | {NULL, BatteryCapacityLevel::UNSUPPORTED}, |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap); |
| 207 | if (!ret) { |
| Stephane Lee | 0684604 | 2020-02-12 17:00:24 -0800 | [diff] [blame] | 208 | KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel); |
| 209 | *ret = BatteryCapacityLevel::UNSUPPORTED; |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | return *ret; |
| 213 | } |
| 214 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 215 | BatteryHealth getBatteryHealth(const char* status) { |
| 216 | static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = { |
| 217 | {"Unknown", BatteryHealth::UNKNOWN}, |
| 218 | {"Good", BatteryHealth::GOOD}, |
| 219 | {"Overheat", BatteryHealth::OVERHEAT}, |
| 220 | {"Dead", BatteryHealth::DEAD}, |
| 221 | {"Over voltage", BatteryHealth::OVER_VOLTAGE}, |
| 222 | {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE}, |
| 223 | {"Cold", BatteryHealth::COLD}, |
| 224 | // battery health values from JEITA spec |
| 225 | {"Warm", BatteryHealth::GOOD}, |
| 226 | {"Cool", BatteryHealth::GOOD}, |
| 227 | {"Hot", BatteryHealth::OVERHEAT}, |
| David Anderson | 629a26b | 2023-12-11 15:25:13 -0800 | [diff] [blame] | 228 | {"Calibration required", BatteryHealth::INCONSISTENT}, |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 229 | {NULL, BatteryHealth::UNKNOWN}, |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 230 | }; |
| 231 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 232 | auto ret = mapSysfsString(status, batteryHealthMap); |
| 233 | if (!ret) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 234 | KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status); |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 235 | *ret = BatteryHealth::UNKNOWN; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 238 | return *ret; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 241 | BatteryHealth getBatteryHealthStatus(int status) { |
| 242 | BatteryHealth value; |
| 243 | |
| 244 | if (status == BatteryMonitor::BH_NOMINAL) |
| 245 | value = BatteryHealth::GOOD; |
| 246 | else if (status == BatteryMonitor::BH_MARGINAL) |
| 247 | value = BatteryHealth::FAIR; |
| 248 | else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT) |
| 249 | value = BatteryHealth::DEAD; |
| 250 | else if (status == BatteryMonitor::BH_FAILED) |
| 251 | value = BatteryHealth::UNSPECIFIED_FAILURE; |
| Jack Wu | cf996f3 | 2023-04-13 19:37:46 +0800 | [diff] [blame] | 252 | else if (status == BatteryMonitor::BH_NOT_AVAILABLE) |
| 253 | value = BatteryHealth::NOT_AVAILABLE; |
| Jack Wu | 8231c3f | 2023-05-19 14:31:53 +0800 | [diff] [blame] | 254 | else if (status == BatteryMonitor::BH_INCONSISTENT) |
| 255 | value = BatteryHealth::INCONSISTENT; |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 256 | else |
| 257 | value = BatteryHealth::UNKNOWN; |
| 258 | |
| 259 | return value; |
| 260 | } |
| 261 | |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 262 | BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) { |
| 263 | static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = { |
| 264 | {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT}, |
| 265 | {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE}, |
| 266 | {NULL, BatteryChargingPolicy::DEFAULT}, |
| 267 | }; |
| 268 | |
| 269 | auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap); |
| 270 | if (!ret) { |
| 271 | *ret = BatteryChargingPolicy::DEFAULT; |
| 272 | } |
| 273 | |
| 274 | return *ret; |
| 275 | } |
| 276 | |
| 277 | BatteryChargingState getBatteryChargingState(const char* chargingState) { |
| 278 | static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = { |
| 279 | {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL}, |
| 280 | {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT}, |
| 281 | {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE}, |
| 282 | {NULL, BatteryChargingState::NORMAL}, |
| 283 | }; |
| 284 | |
| 285 | auto ret = mapSysfsString(chargingState, batteryChargingStateMap); |
| 286 | if (!ret) { |
| 287 | *ret = BatteryChargingState::NORMAL; |
| 288 | } |
| 289 | |
| 290 | return *ret; |
| 291 | } |
| 292 | |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 293 | static int readFromFile(const String8& path, std::string* buf) { |
| Bart Van Assche | 5a7e508 | 2022-02-24 21:40:15 +0000 | [diff] [blame] | 294 | buf->clear(); |
| Steven Moreland | 2aac335 | 2017-03-10 22:31:08 -0800 | [diff] [blame] | 295 | if (android::base::ReadFileToString(path.c_str(), buf)) { |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 296 | *buf = android::base::Trim(*buf); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 297 | } |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 298 | return buf->length(); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 301 | static bool writeToFile(const String8& path, int32_t in_value) { |
| 302 | return android::base::WriteStringToFile(std::to_string(in_value), path.c_str()); |
| 303 | } |
| 304 | |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 305 | static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) { |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 306 | static SysfsStringEnumMap<int> supplyTypeMap[] = { |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 307 | {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN}, |
| 308 | {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY}, |
| 309 | {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 310 | {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 311 | {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB}, |
| 312 | {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 313 | {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| Abhijeet Dharmapurikar | ee19f9f | 2015-09-14 16:35:26 -0700 | [diff] [blame] | 314 | {"USB_HVDCP_3", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 315 | {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 316 | {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 317 | {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 318 | {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 319 | {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB}, |
| 320 | {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS}, |
| 321 | {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK}, |
| Abhijeet Dharmapurikar | ee19f9f | 2015-09-14 16:35:26 -0700 | [diff] [blame] | 322 | {"DASH", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC}, |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 323 | {NULL, 0}, |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 324 | }; |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 325 | std::string buf; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 326 | |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 327 | if (readFromFile(path, &buf) <= 0) { |
| 328 | return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
| 329 | } |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 330 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 331 | auto ret = mapSysfsString(buf.c_str(), supplyTypeMap); |
| Abhijeet Dharmapurikar | 6933248 | 2016-05-24 15:12:11 -0700 | [diff] [blame] | 332 | if (!ret) |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 333 | *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 334 | |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 335 | return static_cast<BatteryMonitor::PowerSupplyType>(*ret); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 338 | static bool getBooleanField(const String8& path) { |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 339 | std::string buf; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 340 | bool value = false; |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 341 | |
| 342 | if (readFromFile(path, &buf) > 0) |
| 343 | if (buf[0] != '0') |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 344 | value = true; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 345 | |
| 346 | return value; |
| 347 | } |
| 348 | |
| David Anderson | ebdd01e | 2025-03-19 02:46:37 +0000 | [diff] [blame] | 349 | template <typename T = int> |
| 350 | static T getIntField(const String8& path) { |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 351 | std::string buf; |
| David Anderson | ebdd01e | 2025-03-19 02:46:37 +0000 | [diff] [blame] | 352 | T value = 0; |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 353 | |
| 354 | if (readFromFile(path, &buf) > 0) |
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 355 | android::base::ParseInt(buf, &value); |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 356 | |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 357 | return value; |
| 358 | } |
| 359 | |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 360 | static bool isScopedPowerSupply(const char* name) { |
| Kazuhiro Inaba | 8e4d982 | 2019-06-12 13:46:08 +0900 | [diff] [blame] | 361 | constexpr char kScopeDevice[] = "Device"; |
| 362 | |
| 363 | String8 path; |
| 364 | path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name); |
| 365 | std::string scope; |
| 366 | return (readFromFile(path, &scope) > 0 && scope == kScopeDevice); |
| 367 | } |
| 368 | |
| Andrei Ciubotariu | 515f5b5 | 2025-02-12 21:31:23 -0800 | [diff] [blame] | 369 | static BatteryHealthData *ensureBatteryHealthData(HealthInfo *info) { |
| 370 | if (!info->batteryHealthData.has_value()) { |
| 371 | return &info->batteryHealthData.emplace(); |
| 372 | } |
| 373 | |
| 374 | return &info->batteryHealthData.value(); |
| 375 | } |
| 376 | |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 377 | void BatteryMonitor::updateValues(void) { |
| Yifan Hong | 6cabe9b | 2019-11-05 17:04:50 -0800 | [diff] [blame] | 378 | initHealthInfo(mHealthInfo.get()); |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 379 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 380 | if (!mHealthdConfig->batteryPresentPath.empty()) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 381 | mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 382 | else |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 383 | mHealthInfo->batteryPresent = mBatteryDevicePresent; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 384 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 385 | mHealthInfo->batteryLevel = mBatteryFixedCapacity |
| 386 | ? mBatteryFixedCapacity |
| 387 | : getIntField(mHealthdConfig->batteryCapacityPath); |
| 388 | mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000; |
| Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 389 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 390 | if (!mHealthdConfig->batteryCurrentNowPath.empty()) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 391 | mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath); |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 392 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 393 | if (!mHealthdConfig->batteryFullChargePath.empty()) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 394 | mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath); |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 395 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 396 | if (!mHealthdConfig->batteryCycleCountPath.empty()) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 397 | mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath); |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 398 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 399 | if (!mHealthdConfig->batteryChargeCounterPath.empty()) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 400 | mHealthInfo->batteryChargeCounterUah = |
| 401 | getIntField(mHealthdConfig->batteryChargeCounterPath); |
| Ruchi Kandoi | 3f9886b | 2016-04-07 12:34:40 -0700 | [diff] [blame] | 402 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 403 | if (!mHealthdConfig->batteryCurrentAvgPath.empty()) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 404 | mHealthInfo->batteryCurrentAverageMicroamps = |
| Yifan Hong | 35cb083 | 2019-10-07 13:58:29 -0700 | [diff] [blame] | 405 | getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 406 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 407 | if (!mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 408 | mHealthInfo->batteryChargeTimeToFullNowSeconds = |
| 409 | getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath); |
| 410 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 411 | if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) |
| Stephane Lee | 1c108ed | 2020-02-10 18:23:57 -0800 | [diff] [blame] | 412 | mHealthInfo->batteryFullChargeDesignCapacityUah = |
| 413 | getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath); |
| Yifan Hong | 35cb083 | 2019-10-07 13:58:29 -0700 | [diff] [blame] | 414 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 415 | if (!mHealthdConfig->batteryHealthStatusPath.empty()) |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 416 | mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath); |
| 417 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 418 | if (!mHealthdConfig->batteryStateOfHealthPath.empty()) |
| Andrei Ciubotariu | 515f5b5 | 2025-02-12 21:31:23 -0800 | [diff] [blame] | 419 | ensureBatteryHealthData(mHealthInfo.get())->batteryStateOfHealth = |
| AleX Pelosi | ff70892 | 2023-02-17 01:39:21 +0000 | [diff] [blame] | 420 | getIntField(mHealthdConfig->batteryStateOfHealthPath); |
| 421 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 422 | if (!mHealthdConfig->batteryManufacturingDatePath.empty()) |
| Andrei Ciubotariu | 515f5b5 | 2025-02-12 21:31:23 -0800 | [diff] [blame] | 423 | ensureBatteryHealthData(mHealthInfo.get())->batteryManufacturingDateSeconds = |
| David Anderson | ebdd01e | 2025-03-19 02:46:37 +0000 | [diff] [blame] | 424 | getIntField<int64_t>(mHealthdConfig->batteryManufacturingDatePath); |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 425 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 426 | if (!mHealthdConfig->batteryFirstUsageDatePath.empty()) |
| Andrei Ciubotariu | 515f5b5 | 2025-02-12 21:31:23 -0800 | [diff] [blame] | 427 | ensureBatteryHealthData(mHealthInfo.get())->batteryFirstUsageSeconds = |
| David Anderson | ebdd01e | 2025-03-19 02:46:37 +0000 | [diff] [blame] | 428 | getIntField<int64_t>(mHealthdConfig->batteryFirstUsageDatePath); |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 429 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 430 | mHealthInfo->batteryTemperatureTenthsCelsius = |
| 431 | mBatteryFixedTemperature ? mBatteryFixedTemperature |
| 432 | : getIntField(mHealthdConfig->batteryTemperaturePath); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 433 | |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 434 | std::string buf; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 435 | |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 436 | if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0) |
| 437 | mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str()); |
| 438 | |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 439 | if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 440 | mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str()); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 441 | |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 442 | // Backward compatible with android.hardware.health V1 |
| 443 | if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) { |
| 444 | if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0) |
| 445 | mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str()); |
| 446 | } else { |
| 447 | mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus); |
| 448 | } |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 449 | |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 450 | if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0) |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 451 | mHealthInfo->batteryTechnology = buf; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 452 | |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 453 | if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0) |
| 454 | mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str()); |
| 455 | |
| 456 | if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0) |
| 457 | mHealthInfo->chargingState = getBatteryChargingState(buf.c_str()); |
| 458 | |
| Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 459 | double MaxPower = 0; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 460 | |
| Abhijeet Dharmapurikar | 6933248 | 2016-05-24 15:12:11 -0700 | [diff] [blame] | 461 | // Rescan for the available charger types |
| 462 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir); |
| 463 | if (dir == NULL) { |
| 464 | KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); |
| 465 | } else { |
| 466 | struct dirent* entry; |
| 467 | String8 path; |
| 468 | |
| 469 | mChargerNames.clear(); |
| 470 | |
| 471 | while ((entry = readdir(dir.get()))) { |
| 472 | const char* name = entry->d_name; |
| 473 | |
| 474 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 475 | continue; |
| 476 | |
| 477 | // Look for "type" file in each subdirectory |
| 478 | path.clear(); |
| 479 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); |
| 480 | switch(readPowerSupplyType(path)) { |
| 481 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 482 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 483 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 484 | case ANDROID_POWER_SUPPLY_TYPE_DOCK: |
| 485 | path.clear(); |
| 486 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| Michael Bestas | 49e82d7 | 2024-03-10 18:47:36 +0200 | [diff] [blame] | 487 | if (access(path.c_str(), R_OK) == 0) |
| Abhijeet Dharmapurikar | 6933248 | 2016-05-24 15:12:11 -0700 | [diff] [blame] | 488 | mChargerNames.add(String8(name)); |
| 489 | break; |
| 490 | default: |
| 491 | break; |
| 492 | } |
| 493 | |
| 494 | // Look for "is_dock" file |
| 495 | path.clear(); |
| 496 | path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name); |
| Michael Bestas | 49e82d7 | 2024-03-10 18:47:36 +0200 | [diff] [blame] | 497 | if (access(path.c_str(), R_OK) == 0) { |
| Abhijeet Dharmapurikar | 6933248 | 2016-05-24 15:12:11 -0700 | [diff] [blame] | 498 | path.clear(); |
| 499 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| Michael Bestas | 49e82d7 | 2024-03-10 18:47:36 +0200 | [diff] [blame] | 500 | if (access(path.c_str(), R_OK) == 0) |
| Abhijeet Dharmapurikar | 6933248 | 2016-05-24 15:12:11 -0700 | [diff] [blame] | 501 | mChargerNames.add(String8(name)); |
| 502 | |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| ShevT | 9d98a6a | 2018-07-26 11:47:47 +0300 | [diff] [blame] | 507 | for (size_t i = 0; i < mChargerNames.size(); i++) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 508 | String8 path; |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 509 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str()); |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 510 | if (getIntField(path)) { |
| 511 | path.clear(); |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 512 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str()); |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 513 | switch(readPowerSupplyType(path)) { |
| 514 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 515 | mHealthInfo->chargerAcOnline = true; |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 516 | break; |
| 517 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 518 | mHealthInfo->chargerUsbOnline = true; |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 519 | break; |
| 520 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 521 | mHealthInfo->chargerWirelessOnline = true; |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 522 | break; |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 523 | case ANDROID_POWER_SUPPLY_TYPE_DOCK: |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 524 | mHealthInfo->chargerDockOnline = true; |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 525 | break; |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 526 | default: |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 527 | path.clear(); |
| 528 | path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 529 | mChargerNames[i].c_str()); |
| 530 | if (access(path.c_str(), R_OK) == 0) |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 531 | mHealthInfo->chargerDockOnline = true; |
| 532 | else |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 533 | KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 534 | mChargerNames[i].c_str()); |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 535 | } |
| Stefan Berger | 06c6f50 | 2019-04-06 11:05:19 +0200 | [diff] [blame] | 536 | |
| 537 | #ifdef HEALTHD_USE_BATTERY_INFO |
| 538 | int ChargingCurrent = (access(SYSFS_BATTERY_CURRENT, R_OK) == 0) ? |
| 539 | abs(getIntField(String8(SYSFS_BATTERY_CURRENT))) : 0; |
| 540 | |
| 541 | int ChargingVoltage = (access(SYSFS_BATTERY_VOLTAGE, R_OK) == 0) ? |
| 542 | getIntField(String8(SYSFS_BATTERY_VOLTAGE)) : DEFAULT_VBUS_VOLTAGE; |
| 543 | #else |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 544 | path.clear(); |
| 545 | path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH, |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 546 | mChargerNames[i].c_str()); |
| 547 | int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0; |
| Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 548 | |
| Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 549 | path.clear(); |
| 550 | path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH, |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 551 | mChargerNames[i].c_str()); |
| Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 552 | |
| Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 553 | int ChargingVoltage = |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 554 | (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE; |
| Stefan Berger | 06c6f50 | 2019-04-06 11:05:19 +0200 | [diff] [blame] | 555 | #endif |
| Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 556 | |
| Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 557 | double power = ((double)ChargingCurrent / MILLION) * |
| 558 | ((double)ChargingVoltage / MILLION); |
| 559 | if (MaxPower < power) { |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 560 | mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent; |
| 561 | mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage; |
| Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 562 | MaxPower = power; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 566 | } |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 567 | |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 568 | static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) { |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 569 | char dmesgline[256]; |
| 570 | size_t len; |
| 571 | if (props.batteryPresent) { |
| 572 | snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d", |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 573 | props.batteryLevel, props.batteryVoltageMillivolts, |
| 574 | props.batteryTemperatureTenthsCelsius < 0 ? "-" : "", |
| 575 | abs(props.batteryTemperatureTenthsCelsius / 10), |
| 576 | abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth, |
| 577 | props.batteryStatus); |
| Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 578 | |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 579 | len = strlen(dmesgline); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 580 | if (!healthd_config.batteryCurrentNowPath.empty()) { |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 581 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d", |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 582 | props.batteryCurrentMicroamps); |
| Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 585 | if (!healthd_config.batteryFullChargePath.empty()) { |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 586 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d", |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 587 | props.batteryFullChargeUah); |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 588 | } |
| Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 589 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 590 | if (!healthd_config.batteryCycleCountPath.empty()) { |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 591 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d", |
| 592 | props.batteryCycleCount); |
| 593 | } |
| 594 | } else { |
| 595 | len = snprintf(dmesgline, sizeof(dmesgline), "battery none"); |
| Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 598 | snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s", |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 599 | props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "", |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 600 | props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : ""); |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 601 | |
| AleX Pelosi | f08aede | 2024-02-15 18:42:11 +0000 | [diff] [blame] | 602 | KLOG_WARNING(LOG_TAG, "%s\n", dmesgline); |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| Bart Van Assche | 095c944 | 2022-03-02 17:36:34 +0000 | [diff] [blame] | 605 | void BatteryMonitor::logValues(const HealthInfo_2_1& health_info, |
| 606 | const struct healthd_config& healthd_config) { |
| 607 | HealthInfo aidl_health_info; |
| 608 | (void)android::h2a::translate(health_info, &aidl_health_info); |
| 609 | doLogValues(aidl_health_info, healthd_config); |
| 610 | } |
| 611 | |
| 612 | void BatteryMonitor::logValues(void) { |
| 613 | doLogValues(*mHealthInfo, *mHealthdConfig); |
| 614 | } |
| 615 | |
| Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 616 | bool BatteryMonitor::isChargerOnline() { |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 617 | const HealthInfo& props = *mHealthInfo; |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 618 | return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline | |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 619 | props.chargerDockOnline; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| Yabin Cui | aedf603 | 2016-02-19 18:03:23 -0800 | [diff] [blame] | 622 | int BatteryMonitor::getChargeStatus() { |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 623 | BatteryStatus result = BatteryStatus::UNKNOWN; |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 624 | if (!mHealthdConfig->batteryStatusPath.empty()) { |
| Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 625 | std::string buf; |
| 626 | if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) |
| 627 | result = getBatteryStatus(buf.c_str()); |
| Yabin Cui | aedf603 | 2016-02-19 18:03:23 -0800 | [diff] [blame] | 628 | } |
| Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 629 | return static_cast<int>(result); |
| Yabin Cui | aedf603 | 2016-02-19 18:03:23 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 632 | status_t BatteryMonitor::setChargingPolicy(int value) { |
| 633 | status_t ret = NAME_NOT_FOUND; |
| 634 | bool result; |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 635 | if (!mHealthdConfig->chargingPolicyPath.empty()) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 636 | result = writeToFile(mHealthdConfig->chargingPolicyPath, value); |
| 637 | if (!result) { |
| 638 | KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n"); |
| 639 | ret = BAD_VALUE; |
| 640 | } else { |
| 641 | ret = OK; |
| 642 | } |
| 643 | } |
| 644 | return ret; |
| 645 | } |
| 646 | |
| 647 | int BatteryMonitor::getChargingPolicy() { |
| 648 | BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT; |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 649 | if (!mHealthdConfig->chargingPolicyPath.empty()) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 650 | std::string buf; |
| 651 | if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0) |
| 652 | result = getBatteryChargingPolicy(buf.c_str()); |
| 653 | } |
| 654 | return static_cast<int>(result); |
| 655 | } |
| 656 | |
| 657 | int BatteryMonitor::getBatteryHealthData(int id) { |
| 658 | if (id == BATTERY_PROP_MANUFACTURING_DATE) { |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 659 | if (!mHealthdConfig->batteryManufacturingDatePath.empty()) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 660 | return getIntField(mHealthdConfig->batteryManufacturingDatePath); |
| 661 | } |
| 662 | if (id == BATTERY_PROP_FIRST_USAGE_DATE) { |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 663 | if (!mHealthdConfig->batteryFirstUsageDatePath.empty()) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 664 | return getIntField(mHealthdConfig->batteryFirstUsageDatePath); |
| 665 | } |
| AleX Pelosi | ff70892 | 2023-02-17 01:39:21 +0000 | [diff] [blame] | 666 | if (id == BATTERY_PROP_STATE_OF_HEALTH) { |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 667 | if (!mHealthdConfig->batteryStateOfHealthPath.empty()) |
| AleX Pelosi | ff70892 | 2023-02-17 01:39:21 +0000 | [diff] [blame] | 668 | return getIntField(mHealthdConfig->batteryStateOfHealthPath); |
| 669 | } |
| David Anderson | d5ed26a | 2023-12-08 15:12:24 -0800 | [diff] [blame] | 670 | if (id == BATTERY_PROP_PART_STATUS) { |
| 671 | return static_cast<int>(BatteryPartStatus::UNSUPPORTED); |
| 672 | } |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 676 | status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) { |
| 677 | status_t ret = BAD_VALUE; |
| Jin Qian | 72adf11 | 2017-02-02 17:31:13 -0800 | [diff] [blame] | 678 | std::string buf; |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 679 | |
| Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 680 | val->valueInt64 = LONG_MIN; |
| 681 | |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 682 | switch(id) { |
| 683 | case BATTERY_PROP_CHARGE_COUNTER: |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 684 | if (!mHealthdConfig->batteryChargeCounterPath.empty()) { |
| Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 685 | val->valueInt64 = |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 686 | getIntField(mHealthdConfig->batteryChargeCounterPath); |
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 687 | ret = OK; |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 688 | } else { |
| 689 | ret = NAME_NOT_FOUND; |
| 690 | } |
| 691 | break; |
| 692 | |
| 693 | case BATTERY_PROP_CURRENT_NOW: |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 694 | if (!mHealthdConfig->batteryCurrentNowPath.empty()) { |
| Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 695 | val->valueInt64 = |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 696 | getIntField(mHealthdConfig->batteryCurrentNowPath); |
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 697 | ret = OK; |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 698 | } else { |
| 699 | ret = NAME_NOT_FOUND; |
| 700 | } |
| 701 | break; |
| 702 | |
| Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 703 | case BATTERY_PROP_CURRENT_AVG: |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 704 | if (!mHealthdConfig->batteryCurrentAvgPath.empty()) { |
| Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 705 | val->valueInt64 = |
| Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 706 | getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 707 | ret = OK; |
| Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 708 | } else { |
| 709 | ret = NAME_NOT_FOUND; |
| 710 | } |
| 711 | break; |
| 712 | |
| Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 713 | case BATTERY_PROP_CAPACITY: |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 714 | if (!mHealthdConfig->batteryCapacityPath.empty()) { |
| Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 715 | val->valueInt64 = |
| Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 716 | getIntField(mHealthdConfig->batteryCapacityPath); |
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 717 | ret = OK; |
| Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 718 | } else { |
| 719 | ret = NAME_NOT_FOUND; |
| 720 | } |
| 721 | break; |
| 722 | |
| Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 723 | case BATTERY_PROP_ENERGY_COUNTER: |
| Todd Poynor | e14b37e | 2014-05-20 13:54:40 -0700 | [diff] [blame] | 724 | if (mHealthdConfig->energyCounter) { |
| 725 | ret = mHealthdConfig->energyCounter(&val->valueInt64); |
| 726 | } else { |
| 727 | ret = NAME_NOT_FOUND; |
| 728 | } |
| Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 729 | break; |
| 730 | |
| Jin Qian | 72adf11 | 2017-02-02 17:31:13 -0800 | [diff] [blame] | 731 | case BATTERY_PROP_BATTERY_STATUS: |
| Todd Poynor | e030a10 | 2018-01-19 14:03:59 -0800 | [diff] [blame] | 732 | val->valueInt64 = getChargeStatus(); |
| Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 733 | ret = OK; |
| Jin Qian | 72adf11 | 2017-02-02 17:31:13 -0800 | [diff] [blame] | 734 | break; |
| 735 | |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 736 | case BATTERY_PROP_CHARGING_POLICY: |
| 737 | val->valueInt64 = getChargingPolicy(); |
| 738 | ret = OK; |
| 739 | break; |
| 740 | |
| 741 | case BATTERY_PROP_MANUFACTURING_DATE: |
| 742 | val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE); |
| 743 | ret = OK; |
| 744 | break; |
| 745 | |
| 746 | case BATTERY_PROP_FIRST_USAGE_DATE: |
| 747 | val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE); |
| 748 | ret = OK; |
| 749 | break; |
| 750 | |
| AleX Pelosi | ff70892 | 2023-02-17 01:39:21 +0000 | [diff] [blame] | 751 | case BATTERY_PROP_STATE_OF_HEALTH: |
| 752 | val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH); |
| 753 | ret = OK; |
| 754 | break; |
| 755 | |
| David Anderson | d5ed26a | 2023-12-08 15:12:24 -0800 | [diff] [blame] | 756 | case BATTERY_PROP_PART_STATUS: |
| 757 | val->valueInt64 = getBatteryHealthData(BATTERY_PROP_PART_STATUS); |
| 758 | ret = OK; |
| 759 | break; |
| 760 | |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | |
| Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 765 | return ret; |
| 766 | } |
| 767 | |
| David Anderson | d5ed26a | 2023-12-08 15:12:24 -0800 | [diff] [blame] | 768 | status_t BatteryMonitor::getSerialNumber(std::optional<std::string>* out) { |
| 769 | *out = std::nullopt; |
| 770 | return OK; |
| 771 | } |
| 772 | |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 773 | void BatteryMonitor::dumpState(int fd) { |
| 774 | int v; |
| 775 | char vs[128]; |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 776 | const HealthInfo& props = *mHealthInfo; |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 777 | |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 778 | snprintf(vs, sizeof(vs), "Cached HealthInfo:\n"); |
| 779 | write(fd, vs, strlen(vs)); |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 780 | snprintf(vs, sizeof(vs), |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 781 | " ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n", |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 782 | props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline, |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 783 | props.chargerDockOnline, props.maxChargingCurrentMicroamps, |
| 784 | props.maxChargingVoltageMicrovolts); |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 785 | write(fd, vs, strlen(vs)); |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 786 | snprintf(vs, sizeof(vs), " status: %d health: %d present: %d\n", |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 787 | props.batteryStatus, props.batteryHealth, props.batteryPresent); |
| 788 | write(fd, vs, strlen(vs)); |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 789 | snprintf(vs, sizeof(vs), " level: %d voltage: %d temp: %d\n", props.batteryLevel, |
| Yifan Hong | b99d15c | 2022-03-01 12:12:34 -0800 | [diff] [blame] | 790 | props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius); |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 791 | write(fd, vs, strlen(vs)); |
| 792 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 793 | if (!mHealthdConfig->batteryCurrentNowPath.empty()) { |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 794 | snprintf(vs, sizeof(vs), " current now: %d\n", props.batteryCurrentMicroamps); |
| 795 | write(fd, vs, strlen(vs)); |
| 796 | } |
| 797 | |
| 798 | if (!mHealthdConfig->batteryCycleCountPath.empty()) { |
| 799 | snprintf(vs, sizeof(vs), " cycle count: %d\n", props.batteryCycleCount); |
| 800 | write(fd, vs, strlen(vs)); |
| 801 | } |
| 802 | |
| 803 | if (!mHealthdConfig->batteryFullChargePath.empty()) { |
| 804 | snprintf(vs, sizeof(vs), " Full charge: %d\n", props.batteryFullChargeUah); |
| 805 | write(fd, vs, strlen(vs)); |
| 806 | } |
| 807 | |
| 808 | snprintf(vs, sizeof(vs), "Real-time Values:\n"); |
| 809 | write(fd, vs, strlen(vs)); |
| 810 | |
| 811 | if (!mHealthdConfig->batteryCurrentNowPath.empty()) { |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 812 | v = getIntField(mHealthdConfig->batteryCurrentNowPath); |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 813 | snprintf(vs, sizeof(vs), " current now: %d\n", v); |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 814 | write(fd, vs, strlen(vs)); |
| 815 | } |
| 816 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 817 | if (!mHealthdConfig->batteryCurrentAvgPath.empty()) { |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 818 | v = getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 819 | snprintf(vs, sizeof(vs), " current avg: %d\n", v); |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 820 | write(fd, vs, strlen(vs)); |
| 821 | } |
| 822 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 823 | if (!mHealthdConfig->batteryChargeCounterPath.empty()) { |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 824 | v = getIntField(mHealthdConfig->batteryChargeCounterPath); |
| YiKai Peng | ae455db | 2025-03-10 23:41:17 -0700 | [diff] [blame] | 825 | snprintf(vs, sizeof(vs), " charge counter: %d\n", v); |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 826 | write(fd, vs, strlen(vs)); |
| 827 | } |
| Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 828 | } |
| 829 | |
| Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 830 | void BatteryMonitor::init(struct healthd_config *hc) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 831 | String8 path; |
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 832 | char pval[PROPERTY_VALUE_MAX]; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 833 | |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 834 | mHealthdConfig = hc; |
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 835 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 836 | if (dir == NULL) { |
| 837 | KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); |
| 838 | } else { |
| 839 | struct dirent* entry; |
| 840 | |
| James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 841 | while ((entry = readdir(dir.get()))) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 842 | const char* name = entry->d_name; |
| 843 | |
| 844 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 845 | continue; |
| 846 | |
| Bart Van Assche | 25b2a8d | 2022-02-24 21:51:34 +0000 | [diff] [blame] | 847 | std::vector<String8>::iterator itIgnoreName = |
| 848 | find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(), |
| 849 | String8(name)); |
| Thierry Strudel | f73de6f | 2019-01-11 17:09:20 -0800 | [diff] [blame] | 850 | if (itIgnoreName != hc->ignorePowerSupplyNames.end()) |
| 851 | continue; |
| 852 | |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 853 | // Look for "type" file in each subdirectory |
| 854 | path.clear(); |
| 855 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); |
| 856 | switch(readPowerSupplyType(path)) { |
| 857 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 858 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 859 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 860 | case ANDROID_POWER_SUPPLY_TYPE_DOCK: |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 861 | path.clear(); |
| 862 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 863 | if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name)); |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 864 | break; |
| 865 | |
| 866 | case ANDROID_POWER_SUPPLY_TYPE_BATTERY: |
| Kazuhiro Inaba | 8e4d982 | 2019-06-12 13:46:08 +0900 | [diff] [blame] | 867 | // Some devices expose the battery status of sub-component like |
| 868 | // stylus. Such a device-scoped battery info needs to be skipped |
| 869 | // in BatteryMonitor, which is intended to report the status of |
| 870 | // the battery supplying the power to the whole system. |
| 871 | if (isScopedPowerSupply(name)) continue; |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 872 | mBatteryDevicePresent = true; |
| 873 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 874 | if (mHealthdConfig->batteryStatusPath.empty()) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 875 | path.clear(); |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 876 | path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH, |
| 877 | name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 878 | if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryStatusPath = path; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 881 | if (mHealthdConfig->batteryHealthPath.empty()) { |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 882 | path.clear(); |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 883 | path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, |
| 884 | name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 885 | if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryHealthPath = path; |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 888 | if (mHealthdConfig->batteryPresentPath.empty()) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 889 | path.clear(); |
| 890 | path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH, |
| 891 | name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 892 | if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryPresentPath = path; |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 895 | if (mHealthdConfig->batteryCapacityPath.empty()) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 896 | path.clear(); |
| 897 | path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH, |
| 898 | name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 899 | if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryCapacityPath = path; |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 900 | } |
| 901 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 902 | if (mHealthdConfig->batteryVoltagePath.empty()) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 903 | path.clear(); |
| 904 | path.appendFormat("%s/%s/voltage_now", |
| 905 | POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 906 | if (access(path.c_str(), R_OK) == 0) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 907 | mHealthdConfig->batteryVoltagePath = path; |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 911 | if (mHealthdConfig->batteryFullChargePath.empty()) { |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 912 | path.clear(); |
| 913 | path.appendFormat("%s/%s/charge_full", |
| 914 | POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 915 | if (access(path.c_str(), R_OK) == 0) |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 916 | mHealthdConfig->batteryFullChargePath = path; |
| 917 | } |
| 918 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 919 | if (mHealthdConfig->batteryCurrentNowPath.empty()) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 920 | path.clear(); |
| 921 | path.appendFormat("%s/%s/current_now", |
| 922 | POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 923 | if (access(path.c_str(), R_OK) == 0) |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 924 | mHealthdConfig->batteryCurrentNowPath = path; |
| 925 | } |
| 926 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 927 | if (mHealthdConfig->batteryCycleCountPath.empty()) { |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 928 | path.clear(); |
| 929 | path.appendFormat("%s/%s/cycle_count", |
| 930 | POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 931 | if (access(path.c_str(), R_OK) == 0) |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 932 | mHealthdConfig->batteryCycleCountPath = path; |
| 933 | } |
| 934 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 935 | if (mHealthdConfig->batteryCapacityLevelPath.empty()) { |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 936 | path.clear(); |
| 937 | path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 938 | if (access(path.c_str(), R_OK) == 0) { |
| 939 | mHealthdConfig->batteryCapacityLevelPath = path; |
| 940 | } |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 941 | } |
| 942 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 943 | if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) { |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 944 | path.clear(); |
| 945 | path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 946 | if (access(path.c_str(), R_OK) == 0) |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 947 | mHealthdConfig->batteryChargeTimeToFullNowPath = path; |
| 948 | } |
| 949 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 950 | if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) { |
| Stephane Lee | 1c108ed | 2020-02-10 18:23:57 -0800 | [diff] [blame] | 951 | path.clear(); |
| 952 | path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 953 | if (access(path.c_str(), R_OK) == 0) |
| Stephane Lee | 1c108ed | 2020-02-10 18:23:57 -0800 | [diff] [blame] | 954 | mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path; |
| 955 | } |
| 956 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 957 | if (mHealthdConfig->batteryCurrentAvgPath.empty()) { |
| Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 958 | path.clear(); |
| 959 | path.appendFormat("%s/%s/current_avg", |
| 960 | POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 961 | if (access(path.c_str(), R_OK) == 0) |
| Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 962 | mHealthdConfig->batteryCurrentAvgPath = path; |
| 963 | } |
| 964 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 965 | if (mHealthdConfig->batteryChargeCounterPath.empty()) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 966 | path.clear(); |
| 967 | path.appendFormat("%s/%s/charge_counter", |
| 968 | POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 969 | if (access(path.c_str(), R_OK) == 0) |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 970 | mHealthdConfig->batteryChargeCounterPath = path; |
| 971 | } |
| 972 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 973 | if (mHealthdConfig->batteryTemperaturePath.empty()) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 974 | path.clear(); |
| 975 | path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH, |
| 976 | name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 977 | if (access(path.c_str(), R_OK) == 0) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 978 | mHealthdConfig->batteryTemperaturePath = path; |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 982 | if (mHealthdConfig->batteryTechnologyPath.empty()) { |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 983 | path.clear(); |
| 984 | path.appendFormat("%s/%s/technology", |
| 985 | POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 986 | if (access(path.c_str(), R_OK) == 0) |
| Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 987 | mHealthdConfig->batteryTechnologyPath = path; |
| 988 | } |
| 989 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 990 | if (mHealthdConfig->batteryStateOfHealthPath.empty()) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 991 | path.clear(); |
| 992 | path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 993 | if (access(path.c_str(), R_OK) == 0) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 994 | mHealthdConfig->batteryStateOfHealthPath = path; |
| 995 | } else { |
| 996 | path.clear(); |
| 997 | path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 998 | if (access(path.c_str(), R_OK) == 0) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 999 | mHealthdConfig->batteryStateOfHealthPath = path; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1003 | if (mHealthdConfig->batteryHealthStatusPath.empty()) { |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 1004 | path.clear(); |
| 1005 | path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 1006 | if (access(path.c_str(), R_OK) == 0) { |
| 1007 | mHealthdConfig->batteryHealthStatusPath = path; |
| 1008 | } |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 1009 | } |
| 1010 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1011 | if (mHealthdConfig->batteryManufacturingDatePath.empty()) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1012 | path.clear(); |
| 1013 | path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 1014 | if (access(path.c_str(), R_OK) == 0) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1015 | mHealthdConfig->batteryManufacturingDatePath = path; |
| 1016 | } |
| 1017 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1018 | if (mHealthdConfig->batteryFirstUsageDatePath.empty()) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1019 | path.clear(); |
| 1020 | path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 1021 | if (access(path.c_str(), R_OK) == 0) { |
| 1022 | mHealthdConfig->batteryFirstUsageDatePath = path; |
| 1023 | } |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1024 | } |
| 1025 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1026 | if (mHealthdConfig->chargingStatePath.empty()) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1027 | path.clear(); |
| 1028 | path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 1029 | if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingStatePath = path; |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1030 | } |
| 1031 | |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1032 | if (mHealthdConfig->chargingPolicyPath.empty()) { |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1033 | path.clear(); |
| 1034 | path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 2b1a059 | 2023-09-12 15:26:15 +0000 | [diff] [blame] | 1035 | if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingPolicyPath = path; |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1036 | } |
| 1037 | |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1038 | break; |
| 1039 | |
| 1040 | case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN: |
| 1041 | break; |
| 1042 | } |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 1043 | |
| 1044 | // Look for "is_dock" file |
| 1045 | path.clear(); |
| 1046 | path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 1047 | if (access(path.c_str(), R_OK) == 0) { |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 1048 | path.clear(); |
| 1049 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| Tomasz Wasilczyk | 18b7461 | 2023-08-10 23:29:50 +0000 | [diff] [blame] | 1050 | if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name)); |
| Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame] | 1051 | } |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1052 | } |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| Ian Pedowitz | 585ab65 | 2015-10-12 19:01:00 -0700 | [diff] [blame] | 1055 | // Typically the case for devices which do not have a battery and |
| 1056 | // and are always plugged into AC mains. |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1057 | if (!mBatteryDevicePresent) { |
| Todd Poynor | ebeb0c0 | 2014-09-23 14:54:24 -0700 | [diff] [blame] | 1058 | KLOG_WARNING(LOG_TAG, "No battery devices found\n"); |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1059 | hc->periodic_chores_interval_fast = -1; |
| 1060 | hc->periodic_chores_interval_slow = -1; |
| 1061 | } else { |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1062 | if (mHealthdConfig->batteryStatusPath.empty()) |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1063 | KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1064 | if (mHealthdConfig->batteryHealthPath.empty()) |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1065 | KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1066 | if (mHealthdConfig->batteryPresentPath.empty()) |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1067 | KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1068 | if (mHealthdConfig->batteryCapacityPath.empty()) |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1069 | KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1070 | if (mHealthdConfig->batteryVoltagePath.empty()) |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1071 | KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1072 | if (mHealthdConfig->batteryTemperaturePath.empty()) |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1073 | KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1074 | if (mHealthdConfig->batteryTechnologyPath.empty()) |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1075 | KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1076 | if (mHealthdConfig->batteryCurrentNowPath.empty()) |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 1077 | KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1078 | if (mHealthdConfig->batteryFullChargePath.empty()) |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 1079 | KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1080 | if (mHealthdConfig->batteryCycleCountPath.empty()) |
| Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 1081 | KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1082 | if (mHealthdConfig->batteryCapacityLevelPath.empty()) |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 1083 | KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1084 | if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) |
| Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 1085 | KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1086 | if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) |
| Stephane Lee | 1c108ed | 2020-02-10 18:23:57 -0800 | [diff] [blame] | 1087 | KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1088 | if (mHealthdConfig->batteryStateOfHealthPath.empty()) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1089 | KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1090 | if (mHealthdConfig->batteryHealthStatusPath.empty()) |
| Jack Wu | b57f68a | 2023-02-04 19:56:06 +0800 | [diff] [blame] | 1091 | KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1092 | if (mHealthdConfig->batteryManufacturingDatePath.empty()) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1093 | KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1094 | if (mHealthdConfig->batteryFirstUsageDatePath.empty()) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1095 | KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1096 | if (mHealthdConfig->chargingStatePath.empty()) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1097 | KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n"); |
| Tomasz Wasilczyk | f597129 | 2023-08-14 18:18:26 +0000 | [diff] [blame] | 1098 | if (mHealthdConfig->chargingPolicyPath.empty()) |
| Jack Wu | e561d03 | 2022-11-24 12:19:41 +0800 | [diff] [blame] | 1099 | KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n"); |
| Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 1100 | } |
| Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 1101 | |
| Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 1102 | if (property_get("ro.boot.fake_battery", pval, NULL) > 0 |
| 1103 | && strtol(pval, NULL, 10) != 0) { |
| 1104 | mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY; |
| 1105 | mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; |
| 1106 | } |
| Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | }; // namespace android |