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 | |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 34 | #include <android-base/file.h> |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 35 | #include <android-base/parseint.h> |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 36 | #include <android-base/strings.h> |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 37 | #include <android/hardware/health/2.1/types.h> |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 38 | #include <batteryservice/BatteryService.h> |
| 39 | #include <cutils/klog.h> |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 40 | #include <cutils/properties.h> |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 41 | #include <utils/Errors.h> |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 42 | #include <utils/String8.h> |
| 43 | #include <utils/Vector.h> |
| 44 | |
| 45 | #define POWER_SUPPLY_SUBSYSTEM "power_supply" |
| 46 | #define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM |
Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 47 | #define FAKE_BATTERY_CAPACITY 42 |
| 48 | #define FAKE_BATTERY_TEMPERATURE 424 |
Ruchi Kandoi | 5c09ec1 | 2016-02-25 16:19:30 -0800 | [diff] [blame] | 49 | #define MILLION 1.0e6 |
Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 50 | #define DEFAULT_VBUS_VOLTAGE 5000000 |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 51 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 52 | using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo; |
| 53 | using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo; |
| 54 | using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo; |
| 55 | using android::hardware::health::V1_0::BatteryHealth; |
| 56 | using android::hardware::health::V1_0::BatteryStatus; |
Yifan Hong | 35cb083 | 2019-10-07 13:58:29 -0700 | [diff] [blame] | 57 | using android::hardware::health::V2_1::BatteryCapacityLevel; |
Stephane Lee | 0684604 | 2020-02-12 17:00:24 -0800 | [diff] [blame] | 58 | using android::hardware::health::V2_1::Constants; |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 59 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 60 | namespace android { |
| 61 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 62 | template <typename T> |
| 63 | struct SysfsStringEnumMap { |
Mark Salyzyn | 6f5b47f | 2014-05-15 15:00:59 -0700 | [diff] [blame] | 64 | const char* s; |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 65 | T val; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 68 | template <typename T> |
| 69 | static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 70 | for (int i = 0; map[i].s; i++) |
| 71 | if (!strcmp(str, map[i].s)) |
| 72 | return map[i].val; |
| 73 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 74 | return std::nullopt; |
Yabin Cui | db04a49 | 2016-02-16 17:19:23 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Yifan Hong | 6cabe9b | 2019-11-05 17:04:50 -0800 | [diff] [blame] | 77 | static void initHealthInfo(HealthInfo_2_1* health_info_2_1) { |
| 78 | *health_info_2_1 = HealthInfo_2_1{}; |
| 79 | |
| 80 | // HIDL enum values are zero initialized, so they need to be initialized |
| 81 | // properly. |
Stephane Lee | bab6877 | 2020-08-12 22:38:20 -0700 | [diff] [blame] | 82 | health_info_2_1->batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED; |
Stephane Lee | 0684604 | 2020-02-12 17:00:24 -0800 | [diff] [blame] | 83 | health_info_2_1->batteryChargeTimeToFullNowSeconds = |
| 84 | (int64_t)Constants::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED; |
Yifan Hong | 6cabe9b | 2019-11-05 17:04:50 -0800 | [diff] [blame] | 85 | auto* props = &health_info_2_1->legacy.legacy; |
| 86 | props->batteryStatus = BatteryStatus::UNKNOWN; |
| 87 | props->batteryHealth = BatteryHealth::UNKNOWN; |
| 88 | } |
| 89 | |
Todd Poynor | e030a10 | 2018-01-19 14:03:59 -0800 | [diff] [blame] | 90 | BatteryMonitor::BatteryMonitor() |
| 91 | : mHealthdConfig(nullptr), |
| 92 | mBatteryDevicePresent(false), |
| 93 | mBatteryFixedCapacity(0), |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 94 | mBatteryFixedTemperature(0), |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 95 | mChargerDockOnline(false), |
Yifan Hong | 6cabe9b | 2019-11-05 17:04:50 -0800 | [diff] [blame] | 96 | mHealthInfo(std::make_unique<HealthInfo_2_1>()) { |
| 97 | initHealthInfo(mHealthInfo.get()); |
| 98 | } |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 99 | |
| 100 | BatteryMonitor::~BatteryMonitor() {} |
| 101 | |
| 102 | const HealthInfo_1_0& BatteryMonitor::getHealthInfo_1_0() const { |
| 103 | return getHealthInfo_2_0().legacy; |
Yabin Cui | db04a49 | 2016-02-16 17:19:23 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 106 | const HealthInfo_2_0& BatteryMonitor::getHealthInfo_2_0() const { |
| 107 | return getHealthInfo_2_1().legacy; |
Hridya Valsaraju | 7fa7225 | 2018-01-12 17:44:33 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 110 | const HealthInfo_2_1& BatteryMonitor::getHealthInfo_2_1() const { |
| 111 | return *mHealthInfo; |
| 112 | } |
| 113 | |
| 114 | BatteryStatus getBatteryStatus(const char* status) { |
| 115 | static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = { |
| 116 | {"Unknown", BatteryStatus::UNKNOWN}, |
| 117 | {"Charging", BatteryStatus::CHARGING}, |
| 118 | {"Discharging", BatteryStatus::DISCHARGING}, |
| 119 | {"Not charging", BatteryStatus::NOT_CHARGING}, |
| 120 | {"Full", BatteryStatus::FULL}, |
| 121 | {NULL, BatteryStatus::UNKNOWN}, |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 124 | auto ret = mapSysfsString(status, batteryStatusMap); |
| 125 | if (!ret) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 126 | KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status); |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 127 | *ret = BatteryStatus::UNKNOWN; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 130 | return *ret; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 133 | BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) { |
| 134 | static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = { |
| 135 | {"Unknown", BatteryCapacityLevel::UNKNOWN}, |
| 136 | {"Critical", BatteryCapacityLevel::CRITICAL}, |
| 137 | {"Low", BatteryCapacityLevel::LOW}, |
| 138 | {"Normal", BatteryCapacityLevel::NORMAL}, |
| 139 | {"High", BatteryCapacityLevel::HIGH}, |
| 140 | {"Full", BatteryCapacityLevel::FULL}, |
Stephane Lee | 0684604 | 2020-02-12 17:00:24 -0800 | [diff] [blame] | 141 | {NULL, BatteryCapacityLevel::UNSUPPORTED}, |
Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap); |
| 145 | if (!ret) { |
Stephane Lee | 0684604 | 2020-02-12 17:00:24 -0800 | [diff] [blame] | 146 | KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel); |
| 147 | *ret = BatteryCapacityLevel::UNSUPPORTED; |
Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | return *ret; |
| 151 | } |
| 152 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 153 | BatteryHealth getBatteryHealth(const char* status) { |
| 154 | static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = { |
| 155 | {"Unknown", BatteryHealth::UNKNOWN}, |
| 156 | {"Good", BatteryHealth::GOOD}, |
| 157 | {"Overheat", BatteryHealth::OVERHEAT}, |
| 158 | {"Dead", BatteryHealth::DEAD}, |
| 159 | {"Over voltage", BatteryHealth::OVER_VOLTAGE}, |
| 160 | {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE}, |
| 161 | {"Cold", BatteryHealth::COLD}, |
| 162 | // battery health values from JEITA spec |
| 163 | {"Warm", BatteryHealth::GOOD}, |
| 164 | {"Cool", BatteryHealth::GOOD}, |
| 165 | {"Hot", BatteryHealth::OVERHEAT}, |
| 166 | {NULL, BatteryHealth::UNKNOWN}, |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 169 | auto ret = mapSysfsString(status, batteryHealthMap); |
| 170 | if (!ret) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 171 | KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status); |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 172 | *ret = BatteryHealth::UNKNOWN; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 175 | return *ret; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 178 | int BatteryMonitor::readFromFile(const String8& path, std::string* buf) { |
Steven Moreland | 2aac335 | 2017-03-10 22:31:08 -0800 | [diff] [blame] | 179 | if (android::base::ReadFileToString(path.c_str(), buf)) { |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 180 | *buf = android::base::Trim(*buf); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 181 | } |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 182 | return buf->length(); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) { |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 186 | static SysfsStringEnumMap<int> supplyTypeMap[] = { |
| 187 | {"Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN}, |
| 188 | {"Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY}, |
| 189 | {"UPS", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 190 | {"Mains", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 191 | {"USB", ANDROID_POWER_SUPPLY_TYPE_USB}, |
| 192 | {"USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 193 | {"USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 194 | {"USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 195 | {"USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 196 | {"USB_C", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 197 | {"USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC}, |
| 198 | {"USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB}, |
| 199 | {"Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS}, |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 200 | {"Dock", ANDROID_POWER_SUPPLY_TYPE_DOCK}, |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 201 | {NULL, 0}, |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 202 | }; |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 203 | std::string buf; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 204 | |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 205 | if (readFromFile(path, &buf) <= 0) |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 206 | return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
| 207 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 208 | auto ret = mapSysfsString(buf.c_str(), supplyTypeMap); |
John Stultz | 47a6bf0 | 2019-11-06 00:23:34 +0000 | [diff] [blame] | 209 | if (!ret) { |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 210 | KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str()); |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 211 | *ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; |
Johan Redestig | 3282861 | 2016-02-03 13:45:54 +0100 | [diff] [blame] | 212 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 213 | |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 214 | return static_cast<BatteryMonitor::PowerSupplyType>(*ret); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | bool BatteryMonitor::getBooleanField(const String8& path) { |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 218 | std::string buf; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 219 | bool value = false; |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 220 | |
| 221 | if (readFromFile(path, &buf) > 0) |
| 222 | if (buf[0] != '0') |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 223 | value = true; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 224 | |
| 225 | return value; |
| 226 | } |
| 227 | |
| 228 | int BatteryMonitor::getIntField(const String8& path) { |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 229 | std::string buf; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 230 | int value = 0; |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 231 | |
| 232 | if (readFromFile(path, &buf) > 0) |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 233 | android::base::ParseInt(buf, &value); |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 234 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 235 | return value; |
| 236 | } |
| 237 | |
Kazuhiro Inaba | 8e4d982 | 2019-06-12 13:46:08 +0900 | [diff] [blame] | 238 | bool BatteryMonitor::isScopedPowerSupply(const char* name) { |
| 239 | constexpr char kScopeDevice[] = "Device"; |
| 240 | |
| 241 | String8 path; |
| 242 | path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name); |
| 243 | std::string scope; |
| 244 | return (readFromFile(path, &scope) > 0 && scope == kScopeDevice); |
| 245 | } |
| 246 | |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 247 | void BatteryMonitor::updateValues(void) { |
Yifan Hong | 6cabe9b | 2019-11-05 17:04:50 -0800 | [diff] [blame] | 248 | initHealthInfo(mHealthInfo.get()); |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 249 | |
| 250 | HealthInfo_1_0& props = mHealthInfo->legacy.legacy; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 251 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 252 | if (!mHealthdConfig->batteryPresentPath.isEmpty()) |
| 253 | props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 254 | else |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 255 | props.batteryPresent = mBatteryDevicePresent; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 256 | |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 257 | props.batteryLevel = mBatteryFixedCapacity ? |
| 258 | mBatteryFixedCapacity : |
| 259 | getIntField(mHealthdConfig->batteryCapacityPath); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 260 | props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000; |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 261 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 262 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) |
Yifan Hong | e5bd5f9 | 2020-04-08 16:14:45 -0700 | [diff] [blame] | 263 | props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath); |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 264 | |
| 265 | if (!mHealthdConfig->batteryFullChargePath.isEmpty()) |
| 266 | props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath); |
| 267 | |
| 268 | if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) |
| 269 | props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath); |
| 270 | |
Ruchi Kandoi | 3f9886b | 2016-04-07 12:34:40 -0700 | [diff] [blame] | 271 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) |
| 272 | props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 273 | |
Yifan Hong | 35cb083 | 2019-10-07 13:58:29 -0700 | [diff] [blame] | 274 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) |
| 275 | mHealthInfo->legacy.batteryCurrentAverage = |
| 276 | getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 277 | |
Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 278 | if (!mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) |
| 279 | mHealthInfo->batteryChargeTimeToFullNowSeconds = |
| 280 | getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath); |
| 281 | |
Stephane Lee | 1c108ed | 2020-02-10 18:23:57 -0800 | [diff] [blame] | 282 | if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty()) |
| 283 | mHealthInfo->batteryFullChargeDesignCapacityUah = |
| 284 | getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath); |
Yifan Hong | 35cb083 | 2019-10-07 13:58:29 -0700 | [diff] [blame] | 285 | |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 286 | props.batteryTemperature = mBatteryFixedTemperature ? |
| 287 | mBatteryFixedTemperature : |
| 288 | getIntField(mHealthdConfig->batteryTemperaturePath); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 289 | |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 290 | std::string buf; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 291 | |
Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 292 | if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0) |
| 293 | mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str()); |
| 294 | |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 295 | if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) |
| 296 | props.batteryStatus = getBatteryStatus(buf.c_str()); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 297 | |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 298 | if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0) |
| 299 | props.batteryHealth = getBatteryHealth(buf.c_str()); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 300 | |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 301 | if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0) |
| 302 | props.batteryTechnology = String8(buf.c_str()); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 303 | |
Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 304 | double MaxPower = 0; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 305 | |
ShevT | 9d98a6a | 2018-07-26 11:47:47 +0300 | [diff] [blame] | 306 | for (size_t i = 0; i < mChargerNames.size(); i++) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 307 | String8 path; |
| 308 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, |
| 309 | mChargerNames[i].string()); |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 310 | if (getIntField(path)) { |
| 311 | path.clear(); |
| 312 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, |
| 313 | mChargerNames[i].string()); |
| 314 | switch(readPowerSupplyType(path)) { |
| 315 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 316 | props.chargerAcOnline = true; |
| 317 | break; |
| 318 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 319 | props.chargerUsbOnline = true; |
| 320 | break; |
| 321 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
| 322 | props.chargerWirelessOnline = true; |
| 323 | break; |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 324 | case ANDROID_POWER_SUPPLY_TYPE_DOCK: |
| 325 | mChargerDockOnline = true; |
| 326 | break; |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 327 | default: |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 328 | path.clear(); |
| 329 | path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, |
| 330 | mChargerNames[i].string()); |
| 331 | if (access(path.string(), R_OK) == 0) { |
| 332 | mChargerDockOnline = true; |
| 333 | KLOG_INFO(LOG_TAG, "%s: online\n", |
| 334 | mChargerNames[i].string()); |
| 335 | } else { |
| 336 | KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n", |
| 337 | mChargerNames[i].string()); |
| 338 | } |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 339 | } |
| 340 | path.clear(); |
| 341 | path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH, |
| 342 | mChargerNames[i].string()); |
Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 343 | int ChargingCurrent = |
Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 344 | (access(path.string(), R_OK) == 0) ? getIntField(path) : 0; |
| 345 | |
Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 346 | path.clear(); |
| 347 | path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH, |
| 348 | mChargerNames[i].string()); |
Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 349 | |
Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 350 | int ChargingVoltage = |
| 351 | (access(path.string(), R_OK) == 0) ? getIntField(path) : |
| 352 | DEFAULT_VBUS_VOLTAGE; |
Badhri Jagan Sridharan | 40e1df4 | 2015-10-27 10:43:53 -0700 | [diff] [blame] | 353 | |
Dmitry Shmidt | 9f6b80c | 2016-06-20 12:58:37 -0700 | [diff] [blame] | 354 | double power = ((double)ChargingCurrent / MILLION) * |
| 355 | ((double)ChargingVoltage / MILLION); |
| 356 | if (MaxPower < power) { |
| 357 | props.maxChargingCurrent = ChargingCurrent; |
| 358 | props.maxChargingVoltage = ChargingVoltage; |
| 359 | MaxPower = power; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 363 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 364 | |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 365 | void BatteryMonitor::logValues(void) { |
Yifan Hong | 605e7d2 | 2021-02-08 15:14:48 -0800 | [diff] [blame] | 366 | logValues(*mHealthInfo, *mHealthdConfig); |
| 367 | } |
| 368 | |
| 369 | void BatteryMonitor::logValues(const android::hardware::health::V2_1::HealthInfo& health_info, |
| 370 | const struct healthd_config& healthd_config) { |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 371 | char dmesgline[256]; |
| 372 | size_t len; |
Yifan Hong | 605e7d2 | 2021-02-08 15:14:48 -0800 | [diff] [blame] | 373 | const HealthInfo_1_0& props = health_info.legacy.legacy; |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 374 | if (props.batteryPresent) { |
| 375 | snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d", |
| 376 | props.batteryLevel, props.batteryVoltage, props.batteryTemperature < 0 ? "-" : "", |
| 377 | abs(props.batteryTemperature / 10), abs(props.batteryTemperature % 10), |
| 378 | props.batteryHealth, props.batteryStatus); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 379 | |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 380 | len = strlen(dmesgline); |
Yifan Hong | 605e7d2 | 2021-02-08 15:14:48 -0800 | [diff] [blame] | 381 | if (!healthd_config.batteryCurrentNowPath.isEmpty()) { |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 382 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d", |
| 383 | props.batteryCurrent); |
Todd Poynor | 10b235e | 2013-08-07 15:25:14 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Yifan Hong | 605e7d2 | 2021-02-08 15:14:48 -0800 | [diff] [blame] | 386 | if (!healthd_config.batteryFullChargePath.isEmpty()) { |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 387 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d", |
| 388 | props.batteryFullCharge); |
| 389 | } |
Mark Salyzyn | acb1ddf | 2015-07-23 09:22:50 -0700 | [diff] [blame] | 390 | |
Yifan Hong | 605e7d2 | 2021-02-08 15:14:48 -0800 | [diff] [blame] | 391 | if (!healthd_config.batteryCycleCountPath.isEmpty()) { |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 392 | len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d", |
| 393 | props.batteryCycleCount); |
| 394 | } |
| 395 | } else { |
| 396 | len = snprintf(dmesgline, sizeof(dmesgline), "battery none"); |
Todd Poynor | b45f1f5 | 2013-07-30 18:57:16 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Yifan Hong | 1353e70 | 2019-10-07 10:41:30 -0700 | [diff] [blame] | 399 | snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s", |
| 400 | props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "", |
| 401 | props.chargerWirelessOnline ? "w" : ""); |
| 402 | |
| 403 | KLOG_WARNING(LOG_TAG, "%s\n", dmesgline); |
| 404 | } |
| 405 | |
| 406 | bool BatteryMonitor::isChargerOnline() { |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 407 | const HealthInfo_1_0& props = mHealthInfo->legacy.legacy; |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 408 | return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline | |
| 409 | mChargerDockOnline; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Yabin Cui | aedf603 | 2016-02-19 18:03:23 -0800 | [diff] [blame] | 412 | int BatteryMonitor::getChargeStatus() { |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 413 | BatteryStatus result = BatteryStatus::UNKNOWN; |
Yabin Cui | aedf603 | 2016-02-19 18:03:23 -0800 | [diff] [blame] | 414 | if (!mHealthdConfig->batteryStatusPath.isEmpty()) { |
Michael Scott | 3217c5c | 2016-06-05 11:20:13 -0700 | [diff] [blame] | 415 | std::string buf; |
| 416 | if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) |
| 417 | result = getBatteryStatus(buf.c_str()); |
Yabin Cui | aedf603 | 2016-02-19 18:03:23 -0800 | [diff] [blame] | 418 | } |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 419 | return static_cast<int>(result); |
Yabin Cui | aedf603 | 2016-02-19 18:03:23 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 422 | status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) { |
| 423 | status_t ret = BAD_VALUE; |
Jin Qian | 72adf11 | 2017-02-02 17:31:13 -0800 | [diff] [blame] | 424 | std::string buf; |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 425 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 426 | val->valueInt64 = LONG_MIN; |
| 427 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 428 | switch(id) { |
| 429 | case BATTERY_PROP_CHARGE_COUNTER: |
| 430 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 431 | val->valueInt64 = |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 432 | getIntField(mHealthdConfig->batteryChargeCounterPath); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 433 | ret = OK; |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 434 | } else { |
| 435 | ret = NAME_NOT_FOUND; |
| 436 | } |
| 437 | break; |
| 438 | |
| 439 | case BATTERY_PROP_CURRENT_NOW: |
| 440 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 441 | val->valueInt64 = |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 442 | getIntField(mHealthdConfig->batteryCurrentNowPath); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 443 | ret = OK; |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 444 | } else { |
| 445 | ret = NAME_NOT_FOUND; |
| 446 | } |
| 447 | break; |
| 448 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 449 | case BATTERY_PROP_CURRENT_AVG: |
| 450 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 451 | val->valueInt64 = |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 452 | getIntField(mHealthdConfig->batteryCurrentAvgPath); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 453 | ret = OK; |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 454 | } else { |
| 455 | ret = NAME_NOT_FOUND; |
| 456 | } |
| 457 | break; |
| 458 | |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 459 | case BATTERY_PROP_CAPACITY: |
| 460 | if (!mHealthdConfig->batteryCapacityPath.isEmpty()) { |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 461 | val->valueInt64 = |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 462 | getIntField(mHealthdConfig->batteryCapacityPath); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 463 | ret = OK; |
Paul Lawrence | 347c8de | 2014-03-19 15:04:40 -0700 | [diff] [blame] | 464 | } else { |
| 465 | ret = NAME_NOT_FOUND; |
| 466 | } |
| 467 | break; |
| 468 | |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 469 | case BATTERY_PROP_ENERGY_COUNTER: |
Todd Poynor | e14b37e | 2014-05-20 13:54:40 -0700 | [diff] [blame] | 470 | if (mHealthdConfig->energyCounter) { |
| 471 | ret = mHealthdConfig->energyCounter(&val->valueInt64); |
| 472 | } else { |
| 473 | ret = NAME_NOT_FOUND; |
| 474 | } |
Todd Poynor | 8f132af | 2014-05-08 17:15:45 -0700 | [diff] [blame] | 475 | break; |
| 476 | |
Jin Qian | 72adf11 | 2017-02-02 17:31:13 -0800 | [diff] [blame] | 477 | case BATTERY_PROP_BATTERY_STATUS: |
Todd Poynor | e030a10 | 2018-01-19 14:03:59 -0800 | [diff] [blame] | 478 | val->valueInt64 = getChargeStatus(); |
Elliott Hughes | 643268f | 2018-10-08 11:10:11 -0700 | [diff] [blame] | 479 | ret = OK; |
Jin Qian | 72adf11 | 2017-02-02 17:31:13 -0800 | [diff] [blame] | 480 | break; |
| 481 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 482 | default: |
| 483 | break; |
| 484 | } |
| 485 | |
Todd Poynor | c133b71 | 2013-08-14 17:39:13 -0700 | [diff] [blame] | 486 | return ret; |
| 487 | } |
| 488 | |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 489 | void BatteryMonitor::dumpState(int fd) { |
| 490 | int v; |
| 491 | char vs[128]; |
Yifan Hong | 1d4368b | 2019-10-07 11:18:04 -0700 | [diff] [blame] | 492 | const HealthInfo_1_0& props = mHealthInfo->legacy.legacy; |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 493 | |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 494 | snprintf(vs, sizeof(vs), |
| 495 | "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n", |
| 496 | props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline, |
| 497 | mChargerDockOnline, props.maxChargingCurrent, props.maxChargingVoltage); |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 498 | write(fd, vs, strlen(vs)); |
| 499 | snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n", |
| 500 | props.batteryStatus, props.batteryHealth, props.batteryPresent); |
| 501 | write(fd, vs, strlen(vs)); |
| 502 | snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", |
| 503 | props.batteryLevel, props.batteryVoltage, |
| 504 | props.batteryTemperature); |
| 505 | write(fd, vs, strlen(vs)); |
| 506 | |
| 507 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 508 | v = getIntField(mHealthdConfig->batteryCurrentNowPath); |
| 509 | snprintf(vs, sizeof(vs), "current now: %d\n", v); |
| 510 | write(fd, vs, strlen(vs)); |
| 511 | } |
| 512 | |
| 513 | if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 514 | v = getIntField(mHealthdConfig->batteryCurrentAvgPath); |
| 515 | snprintf(vs, sizeof(vs), "current avg: %d\n", v); |
| 516 | write(fd, vs, strlen(vs)); |
| 517 | } |
| 518 | |
| 519 | if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 520 | v = getIntField(mHealthdConfig->batteryChargeCounterPath); |
| 521 | snprintf(vs, sizeof(vs), "charge counter: %d\n", v); |
| 522 | write(fd, vs, strlen(vs)); |
| 523 | } |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 524 | |
| 525 | if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 526 | snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent); |
| 527 | write(fd, vs, strlen(vs)); |
| 528 | } |
| 529 | |
| 530 | if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 531 | snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount); |
| 532 | write(fd, vs, strlen(vs)); |
| 533 | } |
| 534 | |
| 535 | if (!mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 536 | snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge); |
| 537 | write(fd, vs, strlen(vs)); |
| 538 | } |
Todd Poynor | 020369d | 2013-09-18 20:09:33 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Todd Poynor | c7464c9 | 2013-09-10 12:40:00 -0700 | [diff] [blame] | 541 | void BatteryMonitor::init(struct healthd_config *hc) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 542 | String8 path; |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 543 | char pval[PROPERTY_VALUE_MAX]; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 544 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 545 | mHealthdConfig = hc; |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 546 | 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] | 547 | if (dir == NULL) { |
| 548 | KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH); |
| 549 | } else { |
| 550 | struct dirent* entry; |
| 551 | |
James Hawkins | 588a2ca | 2016-02-18 14:52:46 -0800 | [diff] [blame] | 552 | while ((entry = readdir(dir.get()))) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 553 | const char* name = entry->d_name; |
Thierry Strudel | f73de6f | 2019-01-11 17:09:20 -0800 | [diff] [blame] | 554 | std::vector<String8>::iterator itIgnoreName; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 555 | |
| 556 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 557 | continue; |
| 558 | |
Thierry Strudel | f73de6f | 2019-01-11 17:09:20 -0800 | [diff] [blame] | 559 | itIgnoreName = find(hc->ignorePowerSupplyNames.begin(), |
| 560 | hc->ignorePowerSupplyNames.end(), String8(name)); |
| 561 | if (itIgnoreName != hc->ignorePowerSupplyNames.end()) |
| 562 | continue; |
| 563 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 564 | // Look for "type" file in each subdirectory |
| 565 | path.clear(); |
| 566 | path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name); |
| 567 | switch(readPowerSupplyType(path)) { |
| 568 | case ANDROID_POWER_SUPPLY_TYPE_AC: |
| 569 | case ANDROID_POWER_SUPPLY_TYPE_USB: |
| 570 | case ANDROID_POWER_SUPPLY_TYPE_WIRELESS: |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 571 | case ANDROID_POWER_SUPPLY_TYPE_DOCK: |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 572 | path.clear(); |
| 573 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| 574 | if (access(path.string(), R_OK) == 0) |
| 575 | mChargerNames.add(String8(name)); |
| 576 | break; |
| 577 | |
| 578 | case ANDROID_POWER_SUPPLY_TYPE_BATTERY: |
Kazuhiro Inaba | 8e4d982 | 2019-06-12 13:46:08 +0900 | [diff] [blame] | 579 | // Some devices expose the battery status of sub-component like |
| 580 | // stylus. Such a device-scoped battery info needs to be skipped |
| 581 | // in BatteryMonitor, which is intended to report the status of |
| 582 | // the battery supplying the power to the whole system. |
| 583 | if (isScopedPowerSupply(name)) continue; |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 584 | mBatteryDevicePresent = true; |
| 585 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 586 | if (mHealthdConfig->batteryStatusPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 587 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 588 | path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH, |
| 589 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 590 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 591 | mHealthdConfig->batteryStatusPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 594 | if (mHealthdConfig->batteryHealthPath.isEmpty()) { |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 595 | path.clear(); |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 596 | path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH, |
| 597 | name); |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 598 | if (access(path, R_OK) == 0) |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 599 | mHealthdConfig->batteryHealthPath = path; |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 602 | if (mHealthdConfig->batteryPresentPath.isEmpty()) { |
| 603 | path.clear(); |
| 604 | path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH, |
| 605 | name); |
| 606 | if (access(path, R_OK) == 0) |
| 607 | mHealthdConfig->batteryPresentPath = path; |
| 608 | } |
| 609 | |
| 610 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) { |
| 611 | path.clear(); |
| 612 | path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH, |
| 613 | name); |
| 614 | if (access(path, R_OK) == 0) |
| 615 | mHealthdConfig->batteryCapacityPath = path; |
| 616 | } |
| 617 | |
| 618 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) { |
| 619 | path.clear(); |
| 620 | path.appendFormat("%s/%s/voltage_now", |
| 621 | POWER_SUPPLY_SYSFS_PATH, name); |
| 622 | if (access(path, R_OK) == 0) { |
| 623 | mHealthdConfig->batteryVoltagePath = path; |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 627 | if (mHealthdConfig->batteryFullChargePath.isEmpty()) { |
| 628 | path.clear(); |
| 629 | path.appendFormat("%s/%s/charge_full", |
| 630 | POWER_SUPPLY_SYSFS_PATH, name); |
| 631 | if (access(path, R_OK) == 0) |
| 632 | mHealthdConfig->batteryFullChargePath = path; |
| 633 | } |
| 634 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 635 | if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) { |
| 636 | path.clear(); |
| 637 | path.appendFormat("%s/%s/current_now", |
| 638 | POWER_SUPPLY_SYSFS_PATH, name); |
| 639 | if (access(path, R_OK) == 0) |
| 640 | mHealthdConfig->batteryCurrentNowPath = path; |
| 641 | } |
| 642 | |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 643 | if (mHealthdConfig->batteryCycleCountPath.isEmpty()) { |
| 644 | path.clear(); |
| 645 | path.appendFormat("%s/%s/cycle_count", |
| 646 | POWER_SUPPLY_SYSFS_PATH, name); |
| 647 | if (access(path, R_OK) == 0) |
| 648 | mHealthdConfig->batteryCycleCountPath = path; |
| 649 | } |
| 650 | |
Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 651 | if (mHealthdConfig->batteryCapacityLevelPath.isEmpty()) { |
| 652 | path.clear(); |
| 653 | path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name); |
| 654 | if (access(path, R_OK) == 0) mHealthdConfig->batteryCapacityLevelPath = path; |
| 655 | } |
| 656 | |
| 657 | if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) { |
| 658 | path.clear(); |
| 659 | path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name); |
| 660 | if (access(path, R_OK) == 0) |
| 661 | mHealthdConfig->batteryChargeTimeToFullNowPath = path; |
| 662 | } |
| 663 | |
Stephane Lee | 1c108ed | 2020-02-10 18:23:57 -0800 | [diff] [blame] | 664 | if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty()) { |
| 665 | path.clear(); |
| 666 | path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name); |
| 667 | if (access(path, R_OK) == 0) |
| 668 | mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path; |
| 669 | } |
| 670 | |
Todd Poynor | bc10211 | 2013-08-27 18:11:49 -0700 | [diff] [blame] | 671 | if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) { |
| 672 | path.clear(); |
| 673 | path.appendFormat("%s/%s/current_avg", |
| 674 | POWER_SUPPLY_SYSFS_PATH, name); |
| 675 | if (access(path, R_OK) == 0) |
| 676 | mHealthdConfig->batteryCurrentAvgPath = path; |
| 677 | } |
| 678 | |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 679 | if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) { |
| 680 | path.clear(); |
| 681 | path.appendFormat("%s/%s/charge_counter", |
| 682 | POWER_SUPPLY_SYSFS_PATH, name); |
| 683 | if (access(path, R_OK) == 0) |
| 684 | mHealthdConfig->batteryChargeCounterPath = path; |
| 685 | } |
| 686 | |
| 687 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) { |
| 688 | path.clear(); |
| 689 | path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH, |
| 690 | name); |
| 691 | if (access(path, R_OK) == 0) { |
| 692 | mHealthdConfig->batteryTemperaturePath = path; |
Todd Poynor | f5d3012 | 2013-08-12 17:03:35 -0700 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) { |
| 697 | path.clear(); |
| 698 | path.appendFormat("%s/%s/technology", |
| 699 | POWER_SUPPLY_SYSFS_PATH, name); |
| 700 | if (access(path, R_OK) == 0) |
| 701 | mHealthdConfig->batteryTechnologyPath = path; |
| 702 | } |
| 703 | |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 704 | break; |
| 705 | |
| 706 | case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN: |
| 707 | break; |
| 708 | } |
Jack Wu | 06b9041 | 2021-12-15 20:40:21 +0800 | [diff] [blame^] | 709 | |
| 710 | // Look for "is_dock" file |
| 711 | path.clear(); |
| 712 | path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name); |
| 713 | if (access(path.string(), R_OK) == 0) { |
| 714 | path.clear(); |
| 715 | path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name); |
| 716 | if (access(path.string(), R_OK) == 0) |
| 717 | mChargerNames.add(String8(name)); |
| 718 | |
| 719 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 720 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Ian Pedowitz | 585ab65 | 2015-10-12 19:01:00 -0700 | [diff] [blame] | 723 | // Typically the case for devices which do not have a battery and |
| 724 | // and are always plugged into AC mains. |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 725 | if (!mBatteryDevicePresent) { |
Todd Poynor | ebeb0c0 | 2014-09-23 14:54:24 -0700 | [diff] [blame] | 726 | KLOG_WARNING(LOG_TAG, "No battery devices found\n"); |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 727 | hc->periodic_chores_interval_fast = -1; |
| 728 | hc->periodic_chores_interval_slow = -1; |
| 729 | } else { |
| 730 | if (mHealthdConfig->batteryStatusPath.isEmpty()) |
| 731 | KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n"); |
| 732 | if (mHealthdConfig->batteryHealthPath.isEmpty()) |
| 733 | KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n"); |
| 734 | if (mHealthdConfig->batteryPresentPath.isEmpty()) |
| 735 | KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n"); |
| 736 | if (mHealthdConfig->batteryCapacityPath.isEmpty()) |
| 737 | KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n"); |
| 738 | if (mHealthdConfig->batteryVoltagePath.isEmpty()) |
| 739 | KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n"); |
| 740 | if (mHealthdConfig->batteryTemperaturePath.isEmpty()) |
| 741 | KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n"); |
| 742 | if (mHealthdConfig->batteryTechnologyPath.isEmpty()) |
| 743 | KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n"); |
Ruchi Kandoi | f18ec9f | 2015-09-28 13:35:59 -0700 | [diff] [blame] | 744 | if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) |
Ruchi Kandoi | cc33880 | 2015-08-24 13:01:16 -0700 | [diff] [blame] | 745 | KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n"); |
| 746 | if (mHealthdConfig->batteryFullChargePath.isEmpty()) |
| 747 | KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n"); |
| 748 | if (mHealthdConfig->batteryCycleCountPath.isEmpty()) |
| 749 | KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n"); |
Stephane Lee | 86f9f6a | 2019-12-19 15:09:41 -0800 | [diff] [blame] | 750 | if (mHealthdConfig->batteryCapacityLevelPath.isEmpty()) |
| 751 | KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n"); |
| 752 | if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) |
| 753 | KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n"); |
Stephane Lee | 1c108ed | 2020-02-10 18:23:57 -0800 | [diff] [blame] | 754 | if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty()) |
| 755 | KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n"); |
Todd Poynor | 6dcc45e | 2013-10-21 20:26:25 -0700 | [diff] [blame] | 756 | } |
Todd Poynor | 3db03a5 | 2014-05-21 16:28:13 -0700 | [diff] [blame] | 757 | |
Ruchi Kandoi | a78fc23 | 2014-07-10 15:06:21 -0700 | [diff] [blame] | 758 | if (property_get("ro.boot.fake_battery", pval, NULL) > 0 |
| 759 | && strtol(pval, NULL, 10) != 0) { |
| 760 | mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY; |
| 761 | mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE; |
| 762 | } |
Todd Poynor | 752faf2 | 2013-06-12 13:25:59 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | }; // namespace android |