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