2013-06-12 22:25:59 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define LOG_TAG "healthd"
|
|
|
|
|
2013-08-08 00:25:14 +02:00
|
|
|
#include "healthd.h"
|
2013-06-12 22:25:59 +02:00
|
|
|
#include "BatteryMonitor.h"
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-07-23 18:22:50 +02:00
|
|
|
#include <sys/types.h>
|
2013-06-12 22:25:59 +02:00
|
|
|
#include <unistd.h>
|
2015-07-23 18:22:50 +02:00
|
|
|
|
2013-06-12 22:25:59 +02:00
|
|
|
#include <batteryservice/BatteryService.h>
|
|
|
|
#include <cutils/klog.h>
|
2014-05-22 01:28:13 +02:00
|
|
|
#include <cutils/properties.h>
|
2015-07-23 18:22:50 +02:00
|
|
|
#include <log/log_read.h>
|
2013-08-15 02:39:13 +02:00
|
|
|
#include <utils/Errors.h>
|
2013-06-12 22:25:59 +02:00
|
|
|
#include <utils/String8.h>
|
|
|
|
#include <utils/Vector.h>
|
|
|
|
|
|
|
|
#define POWER_SUPPLY_SUBSYSTEM "power_supply"
|
|
|
|
#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
|
2014-07-11 00:06:21 +02:00
|
|
|
#define FAKE_BATTERY_CAPACITY 42
|
|
|
|
#define FAKE_BATTERY_TEMPERATURE 424
|
2013-06-12 22:25:59 +02:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
struct sysfsStringEnumMap {
|
2014-05-16 00:00:59 +02:00
|
|
|
const char* s;
|
2013-06-12 22:25:59 +02:00
|
|
|
int val;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mapSysfsString(const char* str,
|
|
|
|
struct sysfsStringEnumMap map[]) {
|
|
|
|
for (int i = 0; map[i].s; i++)
|
|
|
|
if (!strcmp(str, map[i].s))
|
|
|
|
return map[i].val;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BatteryMonitor::getBatteryStatus(const char* status) {
|
|
|
|
int ret;
|
|
|
|
struct sysfsStringEnumMap batteryStatusMap[] = {
|
|
|
|
{ "Unknown", BATTERY_STATUS_UNKNOWN },
|
|
|
|
{ "Charging", BATTERY_STATUS_CHARGING },
|
|
|
|
{ "Discharging", BATTERY_STATUS_DISCHARGING },
|
|
|
|
{ "Not charging", BATTERY_STATUS_NOT_CHARGING },
|
|
|
|
{ "Full", BATTERY_STATUS_FULL },
|
|
|
|
{ NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = mapSysfsString(status, batteryStatusMap);
|
|
|
|
if (ret < 0) {
|
|
|
|
KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
|
|
|
|
ret = BATTERY_STATUS_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BatteryMonitor::getBatteryHealth(const char* status) {
|
|
|
|
int ret;
|
|
|
|
struct sysfsStringEnumMap batteryHealthMap[] = {
|
|
|
|
{ "Unknown", BATTERY_HEALTH_UNKNOWN },
|
|
|
|
{ "Good", BATTERY_HEALTH_GOOD },
|
|
|
|
{ "Overheat", BATTERY_HEALTH_OVERHEAT },
|
|
|
|
{ "Dead", BATTERY_HEALTH_DEAD },
|
|
|
|
{ "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
|
|
|
|
{ "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
|
|
|
|
{ "Cold", BATTERY_HEALTH_COLD },
|
|
|
|
{ NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = mapSysfsString(status, batteryHealthMap);
|
|
|
|
if (ret < 0) {
|
|
|
|
KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
|
|
|
|
ret = BATTERY_HEALTH_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
|
|
|
|
char *cp = NULL;
|
|
|
|
|
|
|
|
if (path.isEmpty())
|
|
|
|
return -1;
|
|
|
|
int fd = open(path.string(), O_RDONLY, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
|
|
|
|
if (count > 0)
|
|
|
|
cp = (char *)memrchr(buf, '\n', count);
|
|
|
|
|
|
|
|
if (cp)
|
|
|
|
*cp = '\0';
|
|
|
|
else
|
|
|
|
buf[0] = '\0';
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
|
|
|
|
const int SIZE = 128;
|
|
|
|
char buf[SIZE];
|
|
|
|
int length = readFromFile(path, buf, SIZE);
|
|
|
|
BatteryMonitor::PowerSupplyType ret;
|
|
|
|
struct sysfsStringEnumMap supplyTypeMap[] = {
|
|
|
|
{ "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
|
|
|
|
{ "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
|
|
|
|
{ "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
|
|
|
|
{ "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
|
|
|
|
{ "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
|
|
|
|
{ "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
|
|
|
|
{ "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
|
|
|
|
{ "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
|
2015-10-08 01:12:04 +02:00
|
|
|
{ "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
|
|
|
|
{ "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
|
|
|
|
{ "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
|
2013-06-12 22:25:59 +02:00
|
|
|
{ "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
|
|
|
|
{ NULL, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (length <= 0)
|
|
|
|
return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
|
|
|
|
|
|
|
|
ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
|
|
|
|
if (ret < 0)
|
|
|
|
ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BatteryMonitor::getBooleanField(const String8& path) {
|
|
|
|
const int SIZE = 16;
|
|
|
|
char buf[SIZE];
|
|
|
|
|
|
|
|
bool value = false;
|
|
|
|
if (readFromFile(path, buf, SIZE) > 0) {
|
|
|
|
if (buf[0] != '0') {
|
|
|
|
value = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BatteryMonitor::getIntField(const String8& path) {
|
|
|
|
const int SIZE = 128;
|
|
|
|
char buf[SIZE];
|
|
|
|
|
|
|
|
int value = 0;
|
|
|
|
if (readFromFile(path, buf, SIZE) > 0) {
|
|
|
|
value = strtol(buf, NULL, 0);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BatteryMonitor::update(void) {
|
2013-08-08 00:25:14 +02:00
|
|
|
bool logthis;
|
2013-06-12 22:25:59 +02:00
|
|
|
|
|
|
|
props.chargerAcOnline = false;
|
|
|
|
props.chargerUsbOnline = false;
|
|
|
|
props.chargerWirelessOnline = false;
|
|
|
|
props.batteryStatus = BATTERY_STATUS_UNKNOWN;
|
|
|
|
props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
|
2015-07-10 22:11:01 +02:00
|
|
|
props.maxChargingCurrent = 0;
|
2013-06-12 22:25:59 +02:00
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (!mHealthdConfig->batteryPresentPath.isEmpty())
|
|
|
|
props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
|
2013-06-12 22:25:59 +02:00
|
|
|
else
|
2013-10-22 05:26:25 +02:00
|
|
|
props.batteryPresent = mBatteryDevicePresent;
|
2013-06-12 22:25:59 +02:00
|
|
|
|
2014-05-22 01:28:13 +02:00
|
|
|
props.batteryLevel = mBatteryFixedCapacity ?
|
|
|
|
mBatteryFixedCapacity :
|
|
|
|
getIntField(mHealthdConfig->batteryCapacityPath);
|
2013-08-13 02:03:35 +02:00
|
|
|
props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
|
2013-07-31 03:57:16 +02:00
|
|
|
|
2015-08-24 22:01:16 +02:00
|
|
|
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
|
|
|
|
props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryFullChargePath.isEmpty())
|
|
|
|
props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
|
|
|
|
props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
|
|
|
|
|
2014-05-22 01:28:13 +02:00
|
|
|
props.batteryTemperature = mBatteryFixedTemperature ?
|
|
|
|
mBatteryFixedTemperature :
|
|
|
|
getIntField(mHealthdConfig->batteryTemperaturePath);
|
2013-06-12 22:25:59 +02:00
|
|
|
|
|
|
|
const int SIZE = 128;
|
|
|
|
char buf[SIZE];
|
|
|
|
String8 btech;
|
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
|
2013-06-12 22:25:59 +02:00
|
|
|
props.batteryStatus = getBatteryStatus(buf);
|
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
|
2013-06-12 22:25:59 +02:00
|
|
|
props.batteryHealth = getBatteryHealth(buf);
|
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
|
2013-06-12 22:25:59 +02:00
|
|
|
props.batteryTechnology = String8(buf);
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < mChargerNames.size(); i++) {
|
|
|
|
String8 path;
|
|
|
|
path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
mChargerNames[i].string());
|
|
|
|
|
|
|
|
if (readFromFile(path, buf, SIZE) > 0) {
|
|
|
|
if (buf[0] != '0') {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
mChargerNames[i].string());
|
|
|
|
switch(readPowerSupplyType(path)) {
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_AC:
|
|
|
|
props.chargerAcOnline = true;
|
|
|
|
break;
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_USB:
|
|
|
|
props.chargerUsbOnline = true;
|
|
|
|
break;
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
|
|
|
|
props.chargerWirelessOnline = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
|
|
|
|
mChargerNames[i].string());
|
|
|
|
}
|
2015-07-10 22:11:01 +02:00
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
mChargerNames[i].string());
|
|
|
|
if (access(path.string(), R_OK) == 0) {
|
|
|
|
int maxChargingCurrent = getIntField(path);
|
|
|
|
if (props.maxChargingCurrent < maxChargingCurrent) {
|
|
|
|
props.maxChargingCurrent = maxChargingCurrent;
|
|
|
|
}
|
|
|
|
}
|
2013-06-12 22:25:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-08 00:25:14 +02:00
|
|
|
logthis = !healthd_board_battery_update(&props);
|
2013-07-31 03:57:16 +02:00
|
|
|
|
2013-08-08 00:25:14 +02:00
|
|
|
if (logthis) {
|
|
|
|
char dmesgline[256];
|
2015-08-24 22:01:16 +02:00
|
|
|
size_t len;
|
2013-10-23 03:21:27 +02:00
|
|
|
if (props.batteryPresent) {
|
2013-10-22 05:26:25 +02:00
|
|
|
snprintf(dmesgline, sizeof(dmesgline),
|
2013-08-08 00:25:14 +02:00
|
|
|
"battery l=%d v=%d t=%s%d.%d h=%d st=%d",
|
|
|
|
props.batteryLevel, props.batteryVoltage,
|
|
|
|
props.batteryTemperature < 0 ? "-" : "",
|
|
|
|
abs(props.batteryTemperature / 10),
|
|
|
|
abs(props.batteryTemperature % 10), props.batteryHealth,
|
|
|
|
props.batteryStatus);
|
2013-07-31 03:57:16 +02:00
|
|
|
|
2015-08-24 22:01:16 +02:00
|
|
|
len = strlen(dmesgline);
|
2013-10-23 03:21:27 +02:00
|
|
|
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
|
2015-08-24 22:01:16 +02:00
|
|
|
len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
|
|
|
|
" c=%d", props.batteryCurrent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
|
|
|
|
len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
|
|
|
|
" fc=%d", props.batteryFullCharge);
|
|
|
|
}
|
2013-08-08 00:25:14 +02:00
|
|
|
|
2015-08-24 22:01:16 +02:00
|
|
|
if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
|
|
|
|
len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
|
|
|
|
" cc=%d", props.batteryCycleCount);
|
2013-10-23 03:21:27 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snprintf(dmesgline, sizeof(dmesgline),
|
|
|
|
"battery none");
|
2013-08-08 00:25:14 +02:00
|
|
|
}
|
2013-07-31 03:57:16 +02:00
|
|
|
|
2015-08-24 22:01:16 +02:00
|
|
|
len = strlen(dmesgline);
|
2015-07-23 18:22:50 +02:00
|
|
|
snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
|
|
|
|
props.chargerAcOnline ? "a" : "",
|
|
|
|
props.chargerUsbOnline ? "u" : "",
|
|
|
|
props.chargerWirelessOnline ? "w" : "");
|
|
|
|
|
|
|
|
log_time realtime(CLOCK_REALTIME);
|
|
|
|
time_t t = realtime.tv_sec;
|
|
|
|
struct tm *tmp = gmtime(&t);
|
|
|
|
if (tmp) {
|
|
|
|
static const char fmt[] = " %Y-%m-%d %H:%M:%S.XXXXXXXXX UTC";
|
|
|
|
len = strlen(dmesgline);
|
|
|
|
if ((len < (sizeof(dmesgline) - sizeof(fmt) - 8)) // margin
|
|
|
|
&& strftime(dmesgline + len, sizeof(dmesgline) - len,
|
|
|
|
fmt, tmp)) {
|
|
|
|
char *usec = strchr(dmesgline + len, 'X');
|
|
|
|
if (usec) {
|
|
|
|
len = usec - dmesgline;
|
|
|
|
snprintf(dmesgline + len, sizeof(dmesgline) - len,
|
|
|
|
"%09u", realtime.tv_nsec);
|
|
|
|
usec[9] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
|
2013-08-08 00:25:14 +02:00
|
|
|
}
|
2013-06-12 22:25:59 +02:00
|
|
|
|
2013-09-10 21:40:00 +02:00
|
|
|
healthd_mode_ops->battery_update(&props);
|
2013-06-12 22:25:59 +02:00
|
|
|
return props.chargerAcOnline | props.chargerUsbOnline |
|
|
|
|
props.chargerWirelessOnline;
|
|
|
|
}
|
|
|
|
|
2013-08-15 02:39:13 +02:00
|
|
|
status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
|
|
|
|
status_t ret = BAD_VALUE;
|
|
|
|
|
2014-05-09 02:15:45 +02:00
|
|
|
val->valueInt64 = LONG_MIN;
|
|
|
|
|
2013-08-15 02:39:13 +02:00
|
|
|
switch(id) {
|
|
|
|
case BATTERY_PROP_CHARGE_COUNTER:
|
|
|
|
if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
|
2014-05-09 02:15:45 +02:00
|
|
|
val->valueInt64 =
|
2013-08-15 02:39:13 +02:00
|
|
|
getIntField(mHealthdConfig->batteryChargeCounterPath);
|
|
|
|
ret = NO_ERROR;
|
|
|
|
} else {
|
|
|
|
ret = NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BATTERY_PROP_CURRENT_NOW:
|
|
|
|
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
|
2014-05-09 02:15:45 +02:00
|
|
|
val->valueInt64 =
|
2013-08-15 02:39:13 +02:00
|
|
|
getIntField(mHealthdConfig->batteryCurrentNowPath);
|
|
|
|
ret = NO_ERROR;
|
|
|
|
} else {
|
|
|
|
ret = NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-08-28 03:11:49 +02:00
|
|
|
case BATTERY_PROP_CURRENT_AVG:
|
|
|
|
if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
|
2014-05-09 02:15:45 +02:00
|
|
|
val->valueInt64 =
|
2013-08-28 03:11:49 +02:00
|
|
|
getIntField(mHealthdConfig->batteryCurrentAvgPath);
|
|
|
|
ret = NO_ERROR;
|
|
|
|
} else {
|
|
|
|
ret = NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-03-19 23:04:40 +01:00
|
|
|
case BATTERY_PROP_CAPACITY:
|
|
|
|
if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
|
2014-05-09 02:15:45 +02:00
|
|
|
val->valueInt64 =
|
2014-03-19 23:04:40 +01:00
|
|
|
getIntField(mHealthdConfig->batteryCapacityPath);
|
|
|
|
ret = NO_ERROR;
|
|
|
|
} else {
|
|
|
|
ret = NAME_NOT_FOUND;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-05-09 02:15:45 +02:00
|
|
|
case BATTERY_PROP_ENERGY_COUNTER:
|
2014-05-20 22:54:40 +02:00
|
|
|
if (mHealthdConfig->energyCounter) {
|
|
|
|
ret = mHealthdConfig->energyCounter(&val->valueInt64);
|
|
|
|
} else {
|
|
|
|
ret = NAME_NOT_FOUND;
|
|
|
|
}
|
2014-05-09 02:15:45 +02:00
|
|
|
break;
|
|
|
|
|
2013-08-15 02:39:13 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-19 05:09:33 +02:00
|
|
|
void BatteryMonitor::dumpState(int fd) {
|
|
|
|
int v;
|
|
|
|
char vs[128];
|
|
|
|
|
2015-07-10 22:11:01 +02:00
|
|
|
snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
|
2013-09-19 05:09:33 +02:00
|
|
|
props.chargerAcOnline, props.chargerUsbOnline,
|
2015-07-10 22:11:01 +02:00
|
|
|
props.chargerWirelessOnline, props.maxChargingCurrent);
|
2013-09-19 05:09:33 +02:00
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
|
|
|
|
props.batteryStatus, props.batteryHealth, props.batteryPresent);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
|
|
|
|
props.batteryLevel, props.batteryVoltage,
|
|
|
|
props.batteryTemperature);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
|
|
|
|
v = getIntField(mHealthdConfig->batteryCurrentNowPath);
|
|
|
|
snprintf(vs, sizeof(vs), "current now: %d\n", v);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
|
|
|
|
v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
|
|
|
|
snprintf(vs, sizeof(vs), "current avg: %d\n", v);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
|
|
|
|
v = getIntField(mHealthdConfig->batteryChargeCounterPath);
|
|
|
|
snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
}
|
2015-08-24 22:01:16 +02:00
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
|
|
|
|
snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
|
|
|
|
snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
|
|
|
|
snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
|
|
|
|
write(fd, vs, strlen(vs));
|
|
|
|
}
|
2013-09-19 05:09:33 +02:00
|
|
|
}
|
|
|
|
|
2013-09-10 21:40:00 +02:00
|
|
|
void BatteryMonitor::init(struct healthd_config *hc) {
|
2013-06-12 22:25:59 +02:00
|
|
|
String8 path;
|
2014-05-22 01:28:13 +02:00
|
|
|
char pval[PROPERTY_VALUE_MAX];
|
2013-06-12 22:25:59 +02:00
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
mHealthdConfig = hc;
|
2013-06-12 22:25:59 +02:00
|
|
|
DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
|
|
|
|
if (dir == NULL) {
|
|
|
|
KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
|
|
|
|
} else {
|
|
|
|
struct dirent* entry;
|
|
|
|
|
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
const char* name = entry->d_name;
|
|
|
|
|
|
|
|
if (!strcmp(name, ".") || !strcmp(name, ".."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Look for "type" file in each subdirectory
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
switch(readPowerSupplyType(path)) {
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_AC:
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_USB:
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path.string(), R_OK) == 0)
|
|
|
|
mChargerNames.add(String8(name));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
|
2013-10-22 05:26:25 +02:00
|
|
|
mBatteryDevicePresent = true;
|
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (mHealthdConfig->batteryStatusPath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryStatusPath = path;
|
|
|
|
}
|
2013-06-12 22:25:59 +02:00
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (mHealthdConfig->batteryHealthPath.isEmpty()) {
|
2013-06-12 22:25:59 +02:00
|
|
|
path.clear();
|
2013-08-13 02:03:35 +02:00
|
|
|
path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
name);
|
2013-06-12 22:25:59 +02:00
|
|
|
if (access(path, R_OK) == 0)
|
2013-08-13 02:03:35 +02:00
|
|
|
mHealthdConfig->batteryHealthPath = path;
|
2013-06-12 22:25:59 +02:00
|
|
|
}
|
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (mHealthdConfig->batteryPresentPath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryPresentPath = path;
|
|
|
|
}
|
2013-07-31 03:57:16 +02:00
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryCapacityPath = path;
|
|
|
|
}
|
2013-07-31 03:57:16 +02:00
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/voltage_now",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0) {
|
|
|
|
mHealthdConfig->batteryVoltagePath = path;
|
|
|
|
} else {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/batt_vol",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryVoltagePath = path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:01:16 +02:00
|
|
|
if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/charge_full",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryFullChargePath = path;
|
|
|
|
}
|
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
|
2013-06-12 22:25:59 +02:00
|
|
|
path.clear();
|
2013-08-13 02:03:35 +02:00
|
|
|
path.appendFormat("%s/%s/current_now",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
2013-06-12 22:25:59 +02:00
|
|
|
if (access(path, R_OK) == 0)
|
2013-08-13 02:03:35 +02:00
|
|
|
mHealthdConfig->batteryCurrentNowPath = path;
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:01:16 +02:00
|
|
|
if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/cycle_count",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryCycleCountPath = path;
|
|
|
|
}
|
|
|
|
|
2013-08-28 03:11:49 +02:00
|
|
|
if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/current_avg",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryCurrentAvgPath = path;
|
|
|
|
}
|
|
|
|
|
2013-08-13 02:03:35 +02:00
|
|
|
if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/charge_counter",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryChargeCounterPath = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
|
|
|
|
name);
|
|
|
|
if (access(path, R_OK) == 0) {
|
|
|
|
mHealthdConfig->batteryTemperaturePath = path;
|
|
|
|
} else {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/batt_temp",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryTemperaturePath = path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
|
|
|
|
path.clear();
|
|
|
|
path.appendFormat("%s/%s/technology",
|
|
|
|
POWER_SUPPLY_SYSFS_PATH, name);
|
|
|
|
if (access(path, R_OK) == 0)
|
|
|
|
mHealthdConfig->batteryTechnologyPath = path;
|
2013-06-12 22:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mChargerNames.size())
|
|
|
|
KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
|
2013-10-22 05:26:25 +02:00
|
|
|
if (!mBatteryDevicePresent) {
|
2014-09-23 23:54:24 +02:00
|
|
|
KLOG_WARNING(LOG_TAG, "No battery devices found\n");
|
2013-10-22 05:26:25 +02:00
|
|
|
hc->periodic_chores_interval_fast = -1;
|
|
|
|
hc->periodic_chores_interval_slow = -1;
|
|
|
|
} else {
|
|
|
|
if (mHealthdConfig->batteryStatusPath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
|
|
|
|
if (mHealthdConfig->batteryHealthPath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
|
|
|
|
if (mHealthdConfig->batteryPresentPath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
|
|
|
|
if (mHealthdConfig->batteryCapacityPath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
|
|
|
|
if (mHealthdConfig->batteryVoltagePath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
|
|
|
|
if (mHealthdConfig->batteryTemperaturePath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
|
|
|
|
if (mHealthdConfig->batteryTechnologyPath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
|
2015-08-24 22:01:16 +02:00
|
|
|
if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
|
|
|
|
if (mHealthdConfig->batteryFullChargePath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
|
|
|
|
if (mHealthdConfig->batteryCycleCountPath.isEmpty())
|
|
|
|
KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
|
2013-10-22 05:26:25 +02:00
|
|
|
}
|
2014-05-22 01:28:13 +02:00
|
|
|
|
2014-07-11 00:06:21 +02:00
|
|
|
if (property_get("ro.boot.fake_battery", pval, NULL) > 0
|
|
|
|
&& strtol(pval, NULL, 10) != 0) {
|
|
|
|
mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
|
|
|
|
mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
|
|
|
|
}
|
2013-06-12 22:25:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // namespace android
|