Merge "Re-worked hardware_properties to thermal HAL." into nyc-dev
diff --git a/include/hardware/hardware_properties.h b/include/hardware/hardware_properties.h
deleted file mode 100644
index 7c9bd4e..0000000
--- a/include/hardware/hardware_properties.h
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (C) 2015 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 ANDROID_INCLUDE_HARDWARE_HARDWAREPROPERTIES_H
-#define ANDROID_INCLUDE_HARDWARE_HARDWAREPROPERTIES_H
-
-#include <stdint.h>
-#include <sys/cdefs.h>
-#include <sys/types.h>
-
-#include <hardware/hardware.h>
-
-__BEGIN_DECLS
-
-#define HARDWARE_PROPERTIES_HARDWARE_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
-
-/**
- * The id of this module
- */
-#define HARDWARE_PROPERTIES_HARDWARE_MODULE_ID "hardware_properties"
-
-/**
- * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
- * and the fields of this data structure must begin with hw_module_t
- * followed by module specific information.
- */
-typedef struct hardware_properties_module {
- struct hw_module_t common;
-
- /*
- * (*getCpuTemperatures) is called to get CPU temperatures in Celsius of
- * each core.
- *
- * Returns number of cores or negative value on error.
- *
- */
- ssize_t (*getCpuTemperatures)(struct hardware_properties_module *module, float **temps);
-
- /*
- * (*getGpuTemperatures) is called to get GPU temperatures in Celsius of
- * each GPU.
- *
- * Returns number of GPUs or negative value on error.
- *
- */
- ssize_t (*getGpuTemperatures)(struct hardware_properties_module *module, float **temps);
-
- /*
- * (*getBatteryTemperatures) is called to get battery temperatures in
- * Celsius.
- *
- * Returns number of battery temperatures or negative value on error.
- *
- */
- ssize_t (*getBatteryTemperatures)(struct hardware_properties_module *module, float **temps);
-
- /*
- * (*getCpuUsages) is called to get CPU usage information of each core:
- * active and total times in ms since first boot.
- *
- * Returns number of cores or negative value on error.
- *
- */
- ssize_t (*getCpuUsages)(struct hardware_properties_module *module,
- int64_t **active_times, int64_t **total_times);
-
- /*
- * (*getFanSpeeds) is called to get the fan speeds in RPM of each fan.
- *
- * Returns number of fans or negative value on error.
- *
- */
- ssize_t (*getFanSpeeds)(struct hardware_properties_module *module,
- float **fan_speeds);
-} hardware_properties_module_t;
-
-__END_DECLS
-
-#endif // ANDROID_INCLUDE_HARDWARE_HARDWAREPROPERTIES_H
diff --git a/include/hardware/thermal.h b/include/hardware/thermal.h
new file mode 100644
index 0000000..7fdf682
--- /dev/null
+++ b/include/hardware/thermal.h
@@ -0,0 +1,200 @@
+/*
+ * Copyright (C) 2016 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 ANDROID_INCLUDE_HARDWARE_THERMAL_H
+#define ANDROID_INCLUDE_HARDWARE_THERMAL_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+#define THERMAL_HARDWARE_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
+
+#define THERMAL_HARDWARE_MODULE_ID "thermal"
+
+// This value is returned if a desired temperature is not available.
+#define UNKNOWN_TEMPERATURE -FLT_MAX
+
+/** Device temperature types. Must be kept in sync with
+ * framework/base/core/java/android/os/HardwarePropertiesManager.java
+ */
+enum temperature_type {
+ DEVICE_TEMPERATURE_UNKNOWN = -1,
+ DEVICE_TEMPERATURE_CPU = 0,
+ DEVICE_TEMPERATURE_GPU = 1,
+ DEVICE_TEMPERATURE_BATTERY = 2,
+ DEVICE_TEMPERATURE_SKIN = 3
+};
+
+enum cooling_type {
+ /** Fan cooling device speed in RPM. */
+ FAN_RPM = 0,
+};
+
+typedef struct {
+ /**
+ * This temperature's type.
+ */
+ enum temperature_type type;
+
+ /**
+ * Name of this temperature.
+ * All temperatures of the same "type" must have a different "name".
+ */
+ const char *name;
+
+ /**
+ * Current temperature in Celsius. If not available set by HAL to
+ * UNKNOWN_TEMPERATURE.
+ * Current temperature can be in any units if
+ * type=DEVICE_TEMPERATURE_UNKNOWN.
+ */
+ float current_value;
+
+ /**
+ * Throttling temperature constant for this temperature.
+ * If not available, set by HAL to UNKNOWN_TEMPERATURE.
+ */
+ float throttling_threshold;
+
+ /**
+ * Shutdown temperature constant for this temperature.
+ * If not available, set by HAL to UNKNOWN_TEMPERATURE.
+ */
+ float shutdown_threshold;
+} temperature_t;
+
+typedef struct {
+ /**
+ * This cooling device type.
+ */
+ enum cooling_type type;
+
+ /**
+ * Name of this cooling device.
+ * All cooling devices of the same "type" must have a different "name".
+ */
+ const char *name;
+
+ /**
+ * Current cooling device value. Units depend on cooling device "type".
+ */
+ float current_value;
+} cooling_device_t;
+
+typedef struct {
+ /**
+ * Name of this CPU.
+ * All CPUs must have a different "name".
+ */
+ const char *name;
+
+ /**
+ * Active time since the last boot in ms.
+ */
+ uint64_t active;
+
+ /**
+ * Total time since the last boot in ms.
+ */
+ uint64_t total;
+
+ /**
+ * Is set to true when a core is online.
+ * If the core is offline, all other members except |name| should be ignored.
+ */
+ bool is_online;
+} cpu_usage_t;
+
+typedef struct thermal_module {
+ struct hw_module_t common;
+
+ /*
+ * (*getTemperatures) is called to get temperatures in Celsius.
+ *
+ * @param list If NULL, this method only returns number of temperatures
+ * and caller should allocate a temperature_t array with that number
+ * of elements.
+ * Caller is responsible for allocating temperature_t array |list| of
+ * large enough size (not less than returned number of temperatures).
+ * If |list| is not NULL and this method returns non-negative value,
+ * it's filled with the current temperatures. If the resulting
+ * temperature list is longer than |size| elements, the remaining
+ * temperatures are discarded and not stored, but counted for the value
+ * returned by this method.
+ * The order of temperatures of built-in devices (such as CPUs, GPUs and
+ * etc.) in the |list| is kept the same regardless the number of calls
+ * to this method even if they go offline, if these devices exist on
+ * boot. The method always returns and never removes such temperatures.
+ * @param size The capacity of |list|, in elements, if |list| is not NULL.
+ *
+ * @return number of temperatures or negative value -errno on error.
+ *
+ */
+ ssize_t (*getTemperatures)(struct thermal_module *module, temperature_t *list, size_t size);
+
+ /*
+ * (*getCpuUsages) is called to get CPU usage information of each core:
+ * active and total times in ms since first boot.
+ *
+ * @param list If NULL, this method only returns number of cores and caller
+ * should allocate a cpu_usage_t array with that number of elements.
+ * Caller is responsible for allocating cpu_usage_t array |list| of
+ * large enough size (not less than returned number of CPUs).
+ * If |list| is not NULL and this method returns non-negative value,
+ * it's filled with the current CPU usages.
+ * The order of CPUs in the |list| is kept the same regardless the
+ * number of calls to this method.
+ *
+ * @return constant number of CPUs or negative value -errno on error.
+ *
+ */
+ ssize_t (*getCpuUsages)(struct thermal_module *module, cpu_usage_t *list);
+
+ /*
+ * (*getCoolingDevices) is called to get the cooling devices information.
+ *
+ * @param list If NULL, this method only returns number of cooling devices
+ * and caller should allocate a cooling_device_t array with that number
+ * of elements.
+ * Caller is responsible for allocating cooling_device_t array |list| of
+ * large enough size (not less than returned number of cooling devices).
+ * If |list| is not NULL and this method returns non-negative value,
+ * it's filled with the current cooling device information. If the
+ * resulting cooling device list is longer than |size| elements, the
+ * remaining cooling device informations are discarded and not stored,
+ * but counted for the value returned by this method.
+ * The order of built-in coolling devices in the |list| is kept the same
+ * regardless the number of calls to this method even if they go
+ * offline, if these devices exist on boot. The method always returns
+ * and never removes from the list such coolling devices.
+ * @param size The capacity of |list|, in elements, if |list| is not NULL.
+ *
+ * @return number of cooling devices or negative value -errno on error.
+ *
+ */
+ ssize_t (*getCoolingDevices)(struct thermal_module *module, cooling_device_t *list,
+ size_t size);
+} thermal_module_t;
+
+__END_DECLS
+
+#endif // ANDROID_INCLUDE_HARDWARE_THERMAL_H
diff --git a/modules/Android.mk b/modules/Android.mk
index 3dc4ca6..3bd0943 100644
--- a/modules/Android.mk
+++ b/modules/Android.mk
@@ -1,4 +1,4 @@
hardware_modules := gralloc hwcomposer audio nfc nfc-nci local_time \
power usbaudio audio_remote_submix camera usbcamera consumerir sensors vibrator \
- tv_input fingerprint input vehicle hardware_properties vr
+ tv_input fingerprint input vehicle thermal vr
include $(call all-named-subdir-makefiles,$(hardware_modules))
diff --git a/modules/hardware_properties/hardware_properties.c b/modules/hardware_properties/hardware_properties.c
deleted file mode 100644
index 89ef423..0000000
--- a/modules/hardware_properties/hardware_properties.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (C) 2015 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 <errno.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define LOG_TAG "HardwarePropertiesHAL"
-#include <utils/Log.h>
-
-#include <hardware/hardware.h>
-#include <hardware/hardware_properties.h>
-
-static ssize_t get_device_temperatures(
- struct hardware_properties_module *module, float **temps) {
- *temps = NULL;
- errno = ENOSYS;
- ALOGE("getDeviceTemperatures: %s", strerror(errno));
- return -1;
-}
-
-static ssize_t get_cpu_usages(struct hardware_properties_module *module,
- int64_t **active_times, int64_t **total_times) {
- *active_times = NULL;
- *total_times = NULL;
- errno = ENOSYS;
- ALOGE("getCpuUsages: %s", strerror(errno));
- return -1;
-}
-
-static ssize_t get_fan_speeds(struct hardware_properties_module *module,
- float **fan_speeds) {
- *fan_speeds = NULL;
- errno = ENOSYS;
- ALOGE("getFanSpeeds: %s", strerror(errno));
- return -1;
-}
-
-static struct hw_module_methods_t hardware_properties_module_methods = {
- .open = NULL,
-};
-
-struct hardware_properties_module HAL_MODULE_INFO_SYM = {
- .common = {
- .tag = HARDWARE_MODULE_TAG,
- .module_api_version = HARDWARE_PROPERTIES_HARDWARE_MODULE_API_VERSION_0_1,
- .hal_api_version = HARDWARE_HAL_API_VERSION,
- .id = HARDWARE_PROPERTIES_HARDWARE_MODULE_ID,
- .name = "Default Hardware Properties HAL",
- .author = "The Android Open Source Project",
- .methods = &hardware_properties_module_methods,
- },
-
- .getCpuTemperatures = get_device_temperatures,
- .getGpuTemperatures = get_device_temperatures,
- .getBatteryTemperatures = get_device_temperatures,
- .getCpuUsages = get_cpu_usages,
- .getFanSpeeds = get_fan_speeds,
-};
diff --git a/modules/hardware_properties/Android.mk b/modules/thermal/Android.mk
similarity index 83%
rename from modules/hardware_properties/Android.mk
rename to modules/thermal/Android.mk
index 41c02ea..80ad78a 100644
--- a/modules/hardware_properties/Android.mk
+++ b/modules/thermal/Android.mk
@@ -16,10 +16,11 @@
include $(CLEAR_VARS)
-LOCAL_MODULE := hardware_properties.default
+LOCAL_MODULE := thermal.default
LOCAL_MODULE_RELATIVE_PATH := hw
-LOCAL_SRC_FILES := hardware_properties.c
-LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_SRC_FILES := thermal.c
+LOCAL_SHARED_LIBRARIES := liblog libcutils
LOCAL_MODULE_TAGS := optional
+LOCAL_CFLAGS := -Wno-unused-parameter
include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/thermal/thermal.c b/modules/thermal/thermal.c
new file mode 100644
index 0000000..c45d87a
--- /dev/null
+++ b/modules/thermal/thermal.c
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2016 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 <errno.h>
+#include <ctype.h>
+#include <dirent.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define LOG_TAG "ThermalHAL"
+#include <utils/Log.h>
+
+#include <hardware/hardware.h>
+#include <hardware/thermal.h>
+
+#define CPU_LABEL "CPU"
+#define MAX_LENGTH 50
+
+#define CPU_USAGE_FILE "/proc/stat"
+#define TEMPERATURE_DIR "/sys/class/thermal"
+#define THERMAL_DIR "thermal_zone"
+#define CPU_ONLINE_FILE_FORMAT "/sys/devices/system/cpu/cpu%d/online"
+#define UNKNOWN_LABEL "UNKNOWN"
+
+static ssize_t get_temperatures(thermal_module_t *module, temperature_t *list, size_t size) {
+ char file_name[MAX_LENGTH];
+ FILE *file;
+ float temp;
+ size_t idx = 0;
+ DIR *dir;
+ struct dirent *de;
+
+ /** Read all available temperatures from
+ * /sys/class/thermal/thermal_zone[0-9]+/temp files.
+ * Don't guarantee that all temperatures are in Celsius. */
+ dir = opendir(TEMPERATURE_DIR);
+ if (dir == 0) {
+ ALOGE("%s: failed to open directory %s: %s", __func__, TEMPERATURE_DIR, strerror(-errno));
+ return -errno;
+ }
+
+ while ((de = readdir(dir))) {
+ if (!strncmp(de->d_name, THERMAL_DIR, strlen(THERMAL_DIR))) {
+ snprintf(file_name, MAX_LENGTH, "%s/%s/temp", TEMPERATURE_DIR, de->d_name);
+ file = fopen(file_name, "r");
+ if (file == NULL) {
+ continue;
+ }
+ if (1 != fscanf(file, "%f", &temp)) {
+ fclose(file);
+ continue;
+ }
+
+ if (list != NULL && idx < size) {
+ list[idx] = (temperature_t) {
+ .name = UNKNOWN_LABEL,
+ .type = DEVICE_TEMPERATURE_UNKNOWN,
+ .current_value = temp,
+ .throttling_threshold = UNKNOWN_TEMPERATURE,
+ .shutdown_threshold = UNKNOWN_TEMPERATURE,
+ };
+ }
+ fclose(file);
+ idx++;
+ }
+ }
+ closedir(dir);
+ return idx;
+}
+
+static ssize_t get_cpu_usages(thermal_module_t *module, cpu_usage_t *list) {
+ int vals, cpu_num, online;
+ ssize_t read;
+ uint64_t user, nice, system, idle, active, total;
+ char *line = NULL;
+ size_t len = 0;
+ size_t size = 0;
+ char file_name[MAX_LENGTH];
+ FILE *cpu_file;
+ FILE *file = fopen(CPU_USAGE_FILE, "r");
+
+ if (file == NULL) {
+ ALOGE("%s: failed to open: %s", __func__, strerror(errno));
+ return -errno;
+ }
+
+ while ((read = getline(&line, &len, file)) != -1) {
+ // Skip non "cpu[0-9]" lines.
+ if (strnlen(line, read) < 4 || strncmp(line, "cpu", 3) != 0 || !isdigit(line[3])) {
+ free(line);
+ line = NULL;
+ len = 0;
+ continue;
+ }
+ vals = sscanf(line, "cpu%d %" SCNu64 " %" SCNu64 " %" SCNu64 " %" SCNu64, &cpu_num, &user,
+ &nice, &system, &idle);
+
+ free(line);
+ line = NULL;
+ len = 0;
+
+ if (vals != 5) {
+ ALOGE("%s: failed to read CPU information from file: %s", __func__, strerror(errno));
+ fclose(file);
+ return errno ? -errno : -EIO;
+ }
+
+ active = user + nice + system;
+ total = active + idle;
+
+ // Read online CPU information.
+ snprintf(file_name, MAX_LENGTH, CPU_ONLINE_FILE_FORMAT, cpu_num);
+ cpu_file = fopen(file_name, "r");
+ online = 0;
+ if (cpu_file == NULL) {
+ ALOGE("%s: failed to open file: %s (%s)", __func__, file_name, strerror(errno));
+ // /sys/devices/system/cpu/cpu0/online is missing on some systems, because cpu0 can't
+ // be offline.
+ online = cpu_num == 0;
+ } else if (1 != fscanf(cpu_file, "%d", &online)) {
+ ALOGE("%s: failed to read CPU online information from file: %s (%s)", __func__,
+ file_name, strerror(errno));
+ fclose(file);
+ fclose(cpu_file);
+ return errno ? -errno : -EIO;
+ }
+ fclose(cpu_file);
+
+ if (list != NULL) {
+ list[size] = (cpu_usage_t) {
+ .name = CPU_LABEL,
+ .active = active,
+ .total = total,
+ .is_online = online
+ };
+ }
+
+ size++;
+ }
+
+ fclose(file);
+ return size;
+}
+
+static ssize_t get_cooling_devices(thermal_module_t *module, cooling_device_t *list, size_t size) {
+ return 0;
+}
+
+static struct hw_module_methods_t thermal_module_methods = {
+ .open = NULL,
+};
+
+thermal_module_t HAL_MODULE_INFO_SYM = {
+ .common = {
+ .tag = HARDWARE_MODULE_TAG,
+ .module_api_version = THERMAL_HARDWARE_MODULE_API_VERSION_0_1,
+ .hal_api_version = HARDWARE_HAL_API_VERSION,
+ .id = THERMAL_HARDWARE_MODULE_ID,
+ .name = "Default Thermal HAL",
+ .author = "The Android Open Source Project",
+ .methods = &thermal_module_methods,
+ },
+
+ .getTemperatures = get_temperatures,
+ .getCpuUsages = get_cpu_usages,
+ .getCoolingDevices = get_cooling_devices,
+};