summaryrefslogtreecommitdiffstats
path: root/healthd
diff options
context:
space:
mode:
Diffstat (limited to 'healthd')
-rw-r--r--healthd/Android.mk30
-rw-r--r--healthd/BatteryMonitor.cpp424
-rw-r--r--healthd/BatteryMonitor.h61
-rw-r--r--healthd/BatteryPropertiesRegistrar.cpp81
-rw-r--r--healthd/BatteryPropertiesRegistrar.h52
-rw-r--r--healthd/healthd.cpp285
-rw-r--r--healthd/healthd.h92
-rw-r--r--healthd/healthd_board_default.cpp29
8 files changed, 1054 insertions, 0 deletions
diff --git a/healthd/Android.mk b/healthd/Android.mk
new file mode 100644
index 0000000..473d375
--- /dev/null
+++ b/healthd/Android.mk
@@ -0,0 +1,30 @@
+# Copyright 2013 The Android Open Source Project
+
+ifneq ($(BUILD_TINY_ANDROID),true)
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := healthd_board_default.cpp
+LOCAL_MODULE := libhealthd.default
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+ healthd.cpp \
+ BatteryMonitor.cpp \
+ BatteryPropertiesRegistrar.cpp
+
+LOCAL_MODULE := healthd
+LOCAL_MODULE_TAGS := optional
+LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT_SBIN)
+LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_SBIN_UNSTRIPPED)
+
+LOCAL_STATIC_LIBRARIES := libbatteryservice libbinder libz libutils libstdc++ libcutils liblog libm libc
+LOCAL_HAL_STATIC_LIBRARIES := libhealthd
+
+include $(BUILD_EXECUTABLE)
+
+endif
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
new file mode 100644
index 0000000..688c7ff
--- /dev/null
+++ b/healthd/BatteryMonitor.cpp
@@ -0,0 +1,424 @@
+/*
+ * 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"
+
+#include "healthd.h"
+#include "BatteryMonitor.h"
+#include "BatteryPropertiesRegistrar.h"
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <batteryservice/BatteryService.h>
+#include <cutils/klog.h>
+#include <utils/String8.h>
+#include <utils/Vector.h>
+
+#define POWER_SUPPLY_SUBSYSTEM "power_supply"
+#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
+
+namespace android {
+
+struct sysfsStringEnumMap {
+ char* s;
+ 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 },
+ { "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) {
+ struct BatteryProperties props;
+ bool logthis;
+
+ props.chargerAcOnline = false;
+ props.chargerUsbOnline = false;
+ props.chargerWirelessOnline = false;
+ props.batteryStatus = BATTERY_STATUS_UNKNOWN;
+ props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
+ props.batteryCurrentNow = INT_MIN;
+ props.batteryChargeCounter = INT_MIN;
+
+ if (!mHealthdConfig->batteryPresentPath.isEmpty())
+ props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
+ else
+ props.batteryPresent = true;
+
+ props.batteryLevel = getIntField(mHealthdConfig->batteryCapacityPath);
+ props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
+
+ if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
+ props.batteryCurrentNow = getIntField(mHealthdConfig->batteryCurrentNowPath);
+
+ if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
+ props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
+
+ props.batteryTemperature = getIntField(mHealthdConfig->batteryTemperaturePath);
+
+ const int SIZE = 128;
+ char buf[SIZE];
+ String8 btech;
+
+ if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
+ props.batteryStatus = getBatteryStatus(buf);
+
+ if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
+ props.batteryHealth = getBatteryHealth(buf);
+
+ if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
+ 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());
+ }
+ }
+ }
+ }
+
+ logthis = !healthd_board_battery_update(&props);
+
+ if (logthis) {
+ char dmesgline[256];
+ snprintf(dmesgline, sizeof(dmesgline),
+ "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);
+
+ if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
+ char b[20];
+
+ snprintf(b, sizeof(b), " c=%d", props.batteryCurrentNow / 1000);
+ strlcat(dmesgline, b, sizeof(dmesgline));
+ }
+
+ KLOG_INFO(LOG_TAG, "%s chg=%s%s%s\n", dmesgline,
+ props.chargerAcOnline ? "a" : "",
+ props.chargerUsbOnline ? "u" : "",
+ props.chargerWirelessOnline ? "w" : "");
+ }
+
+ if (mBatteryPropertiesRegistrar != NULL)
+ mBatteryPropertiesRegistrar->notifyListeners(props);
+
+ return props.chargerAcOnline | props.chargerUsbOnline |
+ props.chargerWirelessOnline;
+}
+
+void BatteryMonitor::init(struct healthd_config *hc, bool nosvcmgr) {
+ String8 path;
+
+ mHealthdConfig = hc;
+ 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;
+
+ char buf[20];
+ // 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:
+ 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;
+ }
+
+ if (mHealthdConfig->batteryHealthPath.isEmpty()) {
+ path.clear();
+ path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
+ name);
+ if (access(path, R_OK) == 0)
+ mHealthdConfig->batteryHealthPath = path;
+ }
+
+ 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;
+ }
+
+ 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;
+ }
+
+ 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;
+ }
+ }
+
+ if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
+ path.clear();
+ path.appendFormat("%s/%s/current_now",
+ POWER_SUPPLY_SYSFS_PATH, name);
+ if (access(path, R_OK) == 0)
+ mHealthdConfig->batteryCurrentNowPath = path;
+ }
+
+ 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;
+ }
+
+ break;
+
+ case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
+ break;
+ }
+ }
+ closedir(dir);
+ }
+
+ if (!mChargerNames.size())
+ KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
+ 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");
+
+ if (nosvcmgr == false) {
+ mBatteryPropertiesRegistrar = new BatteryPropertiesRegistrar(this);
+ mBatteryPropertiesRegistrar->publish();
+ }
+}
+
+}; // namespace android
diff --git a/healthd/BatteryMonitor.h b/healthd/BatteryMonitor.h
new file mode 100644
index 0000000..ba291af
--- /dev/null
+++ b/healthd/BatteryMonitor.h
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+
+#ifndef HEALTHD_BATTERYMONITOR_H
+#define HEALTHD_BATTERYMONITOR_H
+
+#include <binder/IInterface.h>
+#include <utils/String8.h>
+#include <utils/Vector.h>
+
+#include "healthd.h"
+#include "BatteryPropertiesRegistrar.h"
+
+namespace android {
+
+class BatteryPropertiesRegistrar;
+
+class BatteryMonitor {
+ public:
+
+ enum PowerSupplyType {
+ ANDROID_POWER_SUPPLY_TYPE_UNKNOWN = 0,
+ ANDROID_POWER_SUPPLY_TYPE_AC,
+ ANDROID_POWER_SUPPLY_TYPE_USB,
+ ANDROID_POWER_SUPPLY_TYPE_WIRELESS,
+ ANDROID_POWER_SUPPLY_TYPE_BATTERY
+ };
+
+ void init(struct healthd_config *hc, bool nosvcmgr);
+ bool update(void);
+
+ private:
+ struct healthd_config *mHealthdConfig;
+ Vector<String8> mChargerNames;
+
+ sp<BatteryPropertiesRegistrar> mBatteryPropertiesRegistrar;
+
+ int getBatteryStatus(const char* status);
+ int getBatteryHealth(const char* status);
+ int readFromFile(const String8& path, char* buf, size_t size);
+ PowerSupplyType readPowerSupplyType(const String8& path);
+ bool getBooleanField(const String8& path);
+ int getIntField(const String8& path);
+};
+
+}; // namespace android
+
+#endif // HEALTHD_BATTERY_MONTIOR_H
diff --git a/healthd/BatteryPropertiesRegistrar.cpp b/healthd/BatteryPropertiesRegistrar.cpp
new file mode 100644
index 0000000..6a33ad8
--- /dev/null
+++ b/healthd/BatteryPropertiesRegistrar.cpp
@@ -0,0 +1,81 @@
+/*
+ * 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.
+ */
+
+#include "BatteryPropertiesRegistrar.h"
+#include <batteryservice/BatteryService.h>
+#include <batteryservice/IBatteryPropertiesListener.h>
+#include <batteryservice/IBatteryPropertiesRegistrar.h>
+#include <binder/IServiceManager.h>
+#include <utils/Errors.h>
+#include <utils/Mutex.h>
+#include <utils/String16.h>
+
+namespace android {
+
+BatteryPropertiesRegistrar::BatteryPropertiesRegistrar(BatteryMonitor* monitor) {
+ mBatteryMonitor = monitor;
+}
+
+void BatteryPropertiesRegistrar::publish() {
+ defaultServiceManager()->addService(String16("batterypropreg"), this);
+}
+
+void BatteryPropertiesRegistrar::notifyListeners(struct BatteryProperties props) {
+ Mutex::Autolock _l(mRegistrationLock);
+ for (size_t i = 0; i < mListeners.size(); i++) {
+ mListeners[i]->batteryPropertiesChanged(props);
+ }
+}
+
+void BatteryPropertiesRegistrar::registerListener(const sp<IBatteryPropertiesListener>& listener) {
+ {
+ Mutex::Autolock _l(mRegistrationLock);
+ // check whether this is a duplicate
+ for (size_t i = 0; i < mListeners.size(); i++) {
+ if (mListeners[i]->asBinder() == listener->asBinder()) {
+ return;
+ }
+ }
+
+ mListeners.add(listener);
+ listener->asBinder()->linkToDeath(this);
+ }
+ mBatteryMonitor->update();
+}
+
+void BatteryPropertiesRegistrar::unregisterListener(const sp<IBatteryPropertiesListener>& listener) {
+ Mutex::Autolock _l(mRegistrationLock);
+ for (size_t i = 0; i < mListeners.size(); i++) {
+ if (mListeners[i]->asBinder() == listener->asBinder()) {
+ mListeners[i]->asBinder()->unlinkToDeath(this);
+ mListeners.removeAt(i);
+ break;
+ }
+ }
+}
+
+void BatteryPropertiesRegistrar::binderDied(const wp<IBinder>& who) {
+ Mutex::Autolock _l(mRegistrationLock);
+
+ for (size_t i = 0; i < mListeners.size(); i++) {
+ if (mListeners[i]->asBinder() == who) {
+ mListeners.removeAt(i);
+ break;
+ }
+ }
+}
+
+} // namespace android
diff --git a/healthd/BatteryPropertiesRegistrar.h b/healthd/BatteryPropertiesRegistrar.h
new file mode 100644
index 0000000..793ddad
--- /dev/null
+++ b/healthd/BatteryPropertiesRegistrar.h
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+#ifndef HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
+#define HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
+
+#include "BatteryMonitor.h"
+
+#include <binder/IBinder.h>
+#include <utils/Mutex.h>
+#include <utils/Vector.h>
+#include <batteryservice/BatteryService.h>
+#include <batteryservice/IBatteryPropertiesListener.h>
+#include <batteryservice/IBatteryPropertiesRegistrar.h>
+
+namespace android {
+
+class BatteryMonitor;
+
+class BatteryPropertiesRegistrar : public BnBatteryPropertiesRegistrar,
+ public IBinder::DeathRecipient {
+public:
+ BatteryPropertiesRegistrar(BatteryMonitor* monitor);
+ void publish();
+ void notifyListeners(struct BatteryProperties props);
+
+private:
+ BatteryMonitor* mBatteryMonitor;
+ Mutex mRegistrationLock;
+ Vector<sp<IBatteryPropertiesListener> > mListeners;
+
+ void registerListener(const sp<IBatteryPropertiesListener>& listener);
+ void unregisterListener(const sp<IBatteryPropertiesListener>& listener);
+ void binderDied(const wp<IBinder>& who);
+};
+
+}; // namespace android
+
+#endif // HEALTHD_BATTERYPROPERTIES_REGISTRAR_H
diff --git a/healthd/healthd.cpp b/healthd/healthd.cpp
new file mode 100644
index 0000000..9b84c3e
--- /dev/null
+++ b/healthd/healthd.cpp
@@ -0,0 +1,285 @@
+/*
+ * 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"
+#define KLOG_LEVEL 6
+
+#include "healthd.h"
+#include "BatteryMonitor.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <batteryservice/BatteryService.h>
+#include <binder/IPCThreadState.h>
+#include <binder/ProcessState.h>
+#include <cutils/klog.h>
+#include <cutils/uevent.h>
+#include <sys/epoll.h>
+#include <sys/timerfd.h>
+
+using namespace android;
+
+// Periodic chores intervals in seconds
+#define DEFAULT_PERIODIC_CHORES_INTERVAL_FAST (60 * 1)
+#define DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW (60 * 10)
+
+static struct healthd_config healthd_config = {
+ .periodic_chores_interval_fast = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST,
+ .periodic_chores_interval_slow = DEFAULT_PERIODIC_CHORES_INTERVAL_SLOW,
+ .batteryStatusPath = String8(String8::kEmptyString),
+ .batteryHealthPath = String8(String8::kEmptyString),
+ .batteryPresentPath = String8(String8::kEmptyString),
+ .batteryCapacityPath = String8(String8::kEmptyString),
+ .batteryVoltagePath = String8(String8::kEmptyString),
+ .batteryTemperaturePath = String8(String8::kEmptyString),
+ .batteryTechnologyPath = String8(String8::kEmptyString),
+ .batteryCurrentNowPath = String8(String8::kEmptyString),
+ .batteryChargeCounterPath = String8(String8::kEmptyString),
+};
+
+#define POWER_SUPPLY_SUBSYSTEM "power_supply"
+
+// epoll events: uevent, wakealarm, binder
+#define MAX_EPOLL_EVENTS 3
+static int uevent_fd;
+static int wakealarm_fd;
+static int binder_fd;
+
+// -1 for no epoll timeout
+static int awake_poll_interval = -1;
+
+static int wakealarm_wake_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_FAST;
+
+static BatteryMonitor* gBatteryMonitor;
+
+static bool nosvcmgr;
+
+static void wakealarm_set_interval(int interval) {
+ struct itimerspec itval;
+
+ if (wakealarm_fd == -1)
+ return;
+
+ wakealarm_wake_interval = interval;
+
+ if (interval == -1)
+ interval = 0;
+
+ itval.it_interval.tv_sec = interval;
+ itval.it_interval.tv_nsec = 0;
+ itval.it_value.tv_sec = interval;
+ itval.it_value.tv_nsec = 0;
+
+ if (timerfd_settime(wakealarm_fd, 0, &itval, NULL) == -1)
+ KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n");
+}
+
+static void battery_update(void) {
+ // Fast wake interval when on charger (watch for overheat);
+ // slow wake interval when on battery (watch for drained battery).
+
+ int new_wake_interval = gBatteryMonitor->update() ?
+ healthd_config.periodic_chores_interval_fast :
+ healthd_config.periodic_chores_interval_slow;
+
+ if (new_wake_interval != wakealarm_wake_interval)
+ wakealarm_set_interval(new_wake_interval);
+
+ // During awake periods poll at fast rate. If wake alarm is set at fast
+ // rate then just use the alarm; if wake alarm is set at slow rate then
+ // poll at fast rate while awake and let alarm wake up at slow rate when
+ // asleep.
+
+ if (healthd_config.periodic_chores_interval_fast == -1)
+ awake_poll_interval = -1;
+ else
+ awake_poll_interval =
+ new_wake_interval == healthd_config.periodic_chores_interval_fast ?
+ -1 : healthd_config.periodic_chores_interval_fast * 1000;
+}
+
+static void periodic_chores() {
+ battery_update();
+}
+
+static void uevent_init(void) {
+ uevent_fd = uevent_open_socket(64*1024, true);
+
+ if (uevent_fd >= 0)
+ fcntl(uevent_fd, F_SETFL, O_NONBLOCK);
+ else
+ KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n");
+}
+
+#define UEVENT_MSG_LEN 1024
+static void uevent_event(void) {
+ char msg[UEVENT_MSG_LEN+2];
+ char *cp;
+ int n;
+
+ n = uevent_kernel_multicast_recv(uevent_fd, msg, UEVENT_MSG_LEN);
+ if (n <= 0)
+ return;
+ if (n >= UEVENT_MSG_LEN) /* overflow -- discard */
+ return;
+
+ msg[n] = '\0';
+ msg[n+1] = '\0';
+ cp = msg;
+
+ while (*cp) {
+ if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) {
+ battery_update();
+ break;
+ }
+
+ /* advance to after the next \0 */
+ while (*cp++)
+ ;
+ }
+}
+
+static void wakealarm_init(void) {
+ wakealarm_fd = timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK);
+ if (wakealarm_fd == -1) {
+ KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n");
+ return;
+ }
+
+ wakealarm_set_interval(healthd_config.periodic_chores_interval_fast);
+}
+
+static void wakealarm_event(void) {
+ unsigned long long wakeups;
+
+ if (read(wakealarm_fd, &wakeups, sizeof(wakeups)) == -1) {
+ KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm_fd failed\n");
+ return;
+ }
+
+ periodic_chores();
+}
+
+static void binder_init(void) {
+ ProcessState::self()->setThreadPoolMaxThreadCount(0);
+ IPCThreadState::self()->disableBackgroundScheduling(true);
+ IPCThreadState::self()->setupPolling(&binder_fd);
+}
+
+static void binder_event(void) {
+ IPCThreadState::self()->handlePolledCommands();
+}
+
+static void healthd_mainloop(void) {
+ struct epoll_event ev;
+ int epollfd;
+ int maxevents = 0;
+
+ epollfd = epoll_create(MAX_EPOLL_EVENTS);
+ if (epollfd == -1) {
+ KLOG_ERROR(LOG_TAG,
+ "healthd_mainloop: epoll_create failed; errno=%d\n",
+ errno);
+ return;
+ }
+
+ if (uevent_fd >= 0) {
+ ev.events = EPOLLIN | EPOLLWAKEUP;
+ ev.data.ptr = (void *)uevent_event;
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, uevent_fd, &ev) == -1)
+ KLOG_ERROR(LOG_TAG,
+ "healthd_mainloop: epoll_ctl for uevent_fd failed; errno=%d\n",
+ errno);
+ else
+ maxevents++;
+ }
+
+ if (wakealarm_fd >= 0) {
+ ev.events = EPOLLIN | EPOLLWAKEUP;
+ ev.data.ptr = (void *)wakealarm_event;
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, wakealarm_fd, &ev) == -1)
+ KLOG_ERROR(LOG_TAG,
+ "healthd_mainloop: epoll_ctl for wakealarm_fd failed; errno=%d\n",
+ errno);
+ else
+ maxevents++;
+ }
+
+ if (binder_fd >= 0) {
+ ev.events = EPOLLIN | EPOLLWAKEUP;
+ ev.data.ptr= (void *)binder_event;
+ if (epoll_ctl(epollfd, EPOLL_CTL_ADD, binder_fd, &ev) == -1)
+ KLOG_ERROR(LOG_TAG,
+ "healthd_mainloop: epoll_ctl for binder_fd failed; errno=%d\n",
+ errno);
+ else
+ maxevents++;
+ }
+
+ while (1) {
+ struct epoll_event events[maxevents];
+ int nevents;
+
+ IPCThreadState::self()->flushCommands();
+ nevents = epoll_wait(epollfd, events, maxevents, awake_poll_interval);
+
+ if (nevents == -1) {
+ if (errno == EINTR)
+ continue;
+ KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n");
+ break;
+ }
+
+ for (int n = 0; n < nevents; ++n) {
+ if (events[n].data.ptr)
+ (*(void (*)())events[n].data.ptr)();
+ }
+
+ if (!nevents)
+ periodic_chores();
+ }
+
+ return;
+}
+
+int main(int argc, char **argv) {
+ int ch;
+
+ klog_set_level(KLOG_LEVEL);
+
+ while ((ch = getopt(argc, argv, "n")) != -1) {
+ switch (ch) {
+ case 'n':
+ nosvcmgr = true;
+ break;
+ case '?':
+ default:
+ KLOG_WARNING(LOG_TAG, "Unrecognized healthd option: %c\n", ch);
+ }
+ }
+
+ healthd_board_init(&healthd_config);
+ wakealarm_init();
+ uevent_init();
+ binder_init();
+ gBatteryMonitor = new BatteryMonitor();
+ gBatteryMonitor->init(&healthd_config, nosvcmgr);
+
+ healthd_mainloop();
+ return 0;
+}
diff --git a/healthd/healthd.h b/healthd/healthd.h
new file mode 100644
index 0000000..5374fb1
--- /dev/null
+++ b/healthd/healthd.h
@@ -0,0 +1,92 @@
+/*
+ * 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.
+ */
+
+#ifndef _HEALTHD_H_
+#define _HEALTHD_H_
+
+#include <batteryservice/BatteryService.h>
+#include <utils/String8.h>
+
+// periodic_chores_interval_fast, periodic_chores_interval_slow: intervals at
+// which healthd wakes up to poll health state and perform periodic chores,
+// in units of seconds:
+//
+// periodic_chores_interval_fast is used while the device is not in
+// suspend, or in suspend and connected to a charger (to watch for battery
+// overheat due to charging). The default value is 60 (1 minute). Value
+// -1 turns off periodic chores (and wakeups) in these conditions.
+//
+// periodic_chores_interval_slow is used when the device is in suspend and
+// not connected to a charger (to watch for a battery drained to zero
+// remaining capacity). The default value is 600 (10 minutes). Value -1
+// tuns off periodic chores (and wakeups) in these conditions.
+//
+// power_supply sysfs attribute file paths. Set these to specific paths
+// to use for the associated battery parameters. healthd will search for
+// appropriate power_supply attribute files to use for any paths left empty:
+//
+// batteryStatusPath: charging status (POWER_SUPPLY_PROP_STATUS)
+// batteryHealthPath: battery health (POWER_SUPPLY_PROP_HEALTH)
+// batteryPresentPath: battery present (POWER_SUPPLY_PROP_PRESENT)
+// batteryCapacityPath: remaining capacity (POWER_SUPPLY_PROP_CAPACITY)
+// batteryVoltagePath: battery voltage (POWER_SUPPLY_PROP_VOLTAGE_NOW)
+// batteryTemperaturePath: battery temperature (POWER_SUPPLY_PROP_TEMP)
+// batteryTechnologyPath: battery technology (POWER_SUPPLY_PROP_TECHNOLOGY)
+// batteryCurrentNowPath: battery current (POWER_SUPPLY_PROP_CURRENT_NOW)
+// batteryChargeCounterPath: battery accumulated charge
+// (POWER_SUPPLY_PROP_CHARGE_COUNTER)
+
+struct healthd_config {
+ int periodic_chores_interval_fast;
+ int periodic_chores_interval_slow;
+
+ android::String8 batteryStatusPath;
+ android::String8 batteryHealthPath;
+ android::String8 batteryPresentPath;
+ android::String8 batteryCapacityPath;
+ android::String8 batteryVoltagePath;
+ android::String8 batteryTemperaturePath;
+ android::String8 batteryTechnologyPath;
+ android::String8 batteryCurrentNowPath;
+ android::String8 batteryChargeCounterPath;
+};
+
+// The following are implemented in libhealthd_board to handle board-specific
+// behavior.
+//
+// healthd_board_init() is called at startup time to modify healthd's
+// configuration according to board-specific requirements. config
+// points to the healthd configuration values described above. To use default
+// values, this function can simply return without modifying the fields of the
+// config parameter.
+
+void healthd_board_init(struct healthd_config *config);
+
+// Process updated battery property values. This function is called when
+// the kernel sends updated battery status via a uevent from the power_supply
+// subsystem, or when updated values are polled by healthd, as for periodic
+// poll of battery state.
+//
+// props are the battery properties read from the kernel. These values may
+// be modified in this call, prior to sending the modified values to the
+// Android runtime.
+//
+// Return 0 to indicate the usual kernel log battery status heartbeat message
+// is to be logged, or non-zero to prevent logging this information.
+
+int healthd_board_battery_update(struct android::BatteryProperties *props);
+
+#endif /* _HEALTHD_H_ */
diff --git a/healthd/healthd_board_default.cpp b/healthd/healthd_board_default.cpp
new file mode 100644
index 0000000..b2bb516
--- /dev/null
+++ b/healthd/healthd_board_default.cpp
@@ -0,0 +1,29 @@
+/*
+ * 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.
+ */
+
+#include <healthd.h>
+
+void healthd_board_init(struct healthd_config *config)
+{
+ // use defaults
+}
+
+
+int healthd_board_battery_update(struct android::BatteryProperties *props)
+{
+ // return 0 to log periodic polled battery status to kernel log
+ return 0;
+}