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