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