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