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