Fixed thermal HAL, added thresholds.

Bug: 27424857
Change-Id: Ia9d0826abaae86ea35bf932e23dadb8a004e0a30
diff --git a/device.mk b/device.mk
index 123d357..3b58556 100644
--- a/device.mk
+++ b/device.mk
@@ -364,7 +364,7 @@
 
 PRODUCT_PACKAGES += \
     power.shamu \
-    hardware_properties.shamu
+    thermal.shamu
 
 # For android_filesystem_config.h
 PRODUCT_PACKAGES += \
diff --git a/hardware_properties/hardware_properties.c b/hardware_properties/hardware_properties.c
deleted file mode 100644
index 9ea2d79..0000000
--- a/hardware_properties/hardware_properties.c
+++ /dev/null
@@ -1,234 +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 <ctype.h>
-#include <errno.h>
-#include <inttypes.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define LOG_TAG "HardwarePropertiesHAL"
-#include <utils/Log.h>
-
-#include <hardware/hardware.h>
-#include <hardware/hardware_properties.h>
-
-#define MAX_LENGTH         50
-
-#define CPU_USAGE_FILE    "/proc/stat"
-#define CPU_NUM           4
-#define TEMP_FILE         "/sys/class/thermal/thermal_zone%d/temp"
-#define CPU0_TEMP_NUM     6
-#define CPU_LABEL         "CPU %d"
-#define BATTERY_TEMP_NUM  17
-#define BATTERY_LABEL     "BATTERY"
-#define GPU_TEMP_NUM      11
-#define GPU_LABEL         "GPU"
-
-static ssize_t get_cpu_temperatures(struct hardware_properties_module *module,
-                                    float **temps) {
-    FILE *file;
-    char file_name[MAX_LENGTH];
-    int cpu;
-    float temp;
-    size_t size = 0;
-
-    *temps = malloc(CPU_NUM * sizeof(float));
-
-    for (cpu = 0; cpu < CPU_NUM; cpu++) {
-        sprintf(file_name, TEMP_FILE, cpu + CPU0_TEMP_NUM);
-        file = fopen(file_name, "r");
-        if (file == NULL) {
-            ALOGE("%s: failed to open: %s", __func__, strerror(errno));
-            free(*temps);
-            *temps = NULL;
-            return -1;
-        }
-        if (1 != fscanf(file, "%f", &temp)) {
-            fclose(file);
-            free(*temps);
-            *temps = NULL;
-            ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
-            return -1;
-        }
-        // tsens_tz_sensor[5-8]: temperature in Celsius.
-        (*temps)[size] = temp;
-        size++;
-        fclose(file);
-    }
-    return size;
-}
-
-static ssize_t get_battery_temperatures(
-        struct hardware_properties_module *module, float **temps) {
-    FILE *file;
-    char file_name[MAX_LENGTH];
-    float temp;
-
-    *temps = malloc(sizeof(float));
-
-    sprintf(file_name, TEMP_FILE, BATTERY_TEMP_NUM);
-    file = fopen(file_name, "r");
-
-    if (file == NULL) {
-        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
-        free(*temps);
-        *temps = NULL;
-        return -1;
-    }
-
-    if (1 != fscanf(file, "%f", &temp)) {
-        fclose(file);
-        free(*temps);
-        *temps = NULL;
-        ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
-        return -1;
-    }
-
-    // hwmon sensor: battery: temperature in millidegrees Celsius.
-    (*temps)[0] = temp / 1000;
-    fclose(file);
-    return 1;
-}
-
-static ssize_t get_gpu_temperatures(struct hardware_properties_module *module,
-                                    float **temps) {
-    FILE *file;
-    char file_name[MAX_LENGTH];
-    float temp;
-
-    *temps = malloc(sizeof(float));
-
-    sprintf(file_name, TEMP_FILE, GPU_TEMP_NUM);
-    file = fopen(file_name, "r");
-
-    if (file == NULL) {
-        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
-        free(*temps);
-        *temps = NULL;
-        return -1;
-    }
-
-    if (1 != fscanf(file, "%f", &temp)) {
-        fclose(file);
-        free(*temps);
-        *temps = NULL;
-        ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
-        return -1;
-    }
-
-    // tsens_tz_sensor11: temperature in Celsius.
-    (*temps)[0] = temp;
-    fclose(file);
-    return 1;
-}
-
-static ssize_t get_cpu_usages(struct hardware_properties_module *module,
-                              int64_t **active_times,
-                              int64_t **total_times) {
-    int vals, cpu_num;
-    ssize_t read;
-    uint64_t user, nice, system, idle, active, total;
-    char *line = NULL;
-    size_t len = 0;
-    size_t size = 0;
-    FILE *file = fopen(CPU_USAGE_FILE, "r");
-
-    if (file == NULL) {
-        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
-        return -1;
-    }
-
-    *active_times = malloc(CPU_NUM * sizeof(int64_t));
-    *total_times = malloc(CPU_NUM * sizeof(int64_t));
-
-    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);
-
-        if (vals != 5 || size == CPU_NUM) {
-            if (vals != 5) {
-                ALOGE("%s: failed to read CPU information from file: %s",
-                      __func__, strerror(errno));
-            } else {
-                ALOGE("/proc/stat file has incorrect format.");
-            }
-            fclose(file);
-            free(line);
-            free(*active_times);
-            free(*total_times);
-            *active_times = NULL;
-            *total_times = NULL;
-            line = NULL;
-            len = 0;
-            return -1;
-        }
-
-        active = user + nice + system;
-        total = active + idle;
-        (*active_times)[size] = active;
-        (*total_times)[size] = total;
-
-        size++;
-
-        free(line);
-        line = NULL;
-        len = 0;
-    }
-
-    if (size != CPU_NUM) {
-        ALOGE("/proc/stat file has incorrect format.");
-        fclose(file);
-        free(*active_times);
-        *active_times = NULL;
-        free(*total_times);
-        *total_times = NULL;
-        return -1;
-    }
-
-    fclose(file);
-    return size;
-}
-
-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 = "Shamu Hardware Properties HAL",
-        .author = "The Android Open Source Project",
-        .methods = &hardware_properties_module_methods,
-    },
-    .getCpuTemperatures = get_cpu_temperatures,
-    .getGpuTemperatures = get_gpu_temperatures,
-    .getBatteryTemperatures = get_battery_temperatures,
-    .getCpuUsages = get_cpu_usages,
-};
diff --git a/hardware_properties/Android.mk b/thermal/Android.mk
similarity index 90%
rename from hardware_properties/Android.mk
rename to thermal/Android.mk
index 2e4605c..6ded026 100644
--- a/hardware_properties/Android.mk
+++ b/thermal/Android.mk
@@ -19,8 +19,8 @@
 include $(CLEAR_VARS)
 LOCAL_CFLAGS := -Wno-unused-parameter
 LOCAL_MODULE_RELATIVE_PATH := hw
-LOCAL_SRC_FILES := hardware_properties.c
+LOCAL_SRC_FILES := thermal.c
 LOCAL_SHARED_LIBRARIES := liblog libcutils
 LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE := hardware_properties.shamu
+LOCAL_MODULE := thermal.shamu
 include $(BUILD_SHARED_LIBRARY)
diff --git a/thermal/thermal.c b/thermal/thermal.c
new file mode 100644
index 0000000..f8cb8e9
--- /dev/null
+++ b/thermal/thermal.c
@@ -0,0 +1,273 @@
+/*
+ * 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 <ctype.h>
+#include <errno.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 MAX_LENGTH                  50
+
+#define CPU_USAGE_FILE              "/proc/stat"
+#define TEMPERATURE_FILE_FORMAT     "/sys/class/thermal/thermal_zone%d/temp"
+#define CPU_ONLINE_FILE_FORMAT      "/sys/devices/system/cpu/cpu%d/online"
+
+#define CPU0_SENSOR_NUM             6
+#define BATTERY_SENSOR_NUM          17
+#define GPU_SENSOR_NUM              11
+#define SKIN_SENSOR_NUM             14
+
+#define CPU_NUM                     4
+#define TEMPERATURE_NUM             7
+
+// qcom, therm-reset-temp
+#define CPU_SHUTDOWN_THRESHOLD      115
+// qcom, limit-temp
+#define CPU_THROTTLING_THRESHOLD    60
+
+#define BATTERY_SHUTDOWN_THRESHOLD  68
+// vendor/moto/shamu/proprietary/thermal-engine/thermal-engine-shamu.conf
+#define SKIN_THROTTLING_THRESHOLD   40
+
+#define GPU_LABEL                   "GPU"
+#define BATTERY_LABEL               "battery"
+#define SKIN_LABEL                  "skin"
+
+const char *CPU_LABEL[] = {"CPU0", "CPU1", "CPU2", "CPU3"};
+
+/**
+ * Reads device temperature.
+ *
+ * @param sensor_num Number of sensor file with temperature.
+ * @param type Device temperature type.
+ * @param name Device temperature name.
+ * @param mult Multiplier used to translate temperature to Celsius.
+ * @param throttling_threshold Throttling threshold for the temperature.
+ * @param shutdown_threshold Shutdown threshold for the temperature.
+ * @param out Pointer to temperature_t structure that will be filled with current values.
+ *
+ * @return 0 on success or negative value -errno on error.
+ */
+static ssize_t read_temperature(int sensor_num, int type, const char *name, float mult,
+        float throttling_threshold, float shutdown_threshold, temperature_t *out) {
+    FILE *file;
+    char file_name[MAX_LENGTH];
+    float temp;
+
+    sprintf(file_name, TEMPERATURE_FILE_FORMAT, sensor_num);
+    file = fopen(file_name, "r");
+    if (file == NULL) {
+        ALOGE("%s: failed to open: %s", __func__, strerror(errno));
+        return -errno;
+    }
+    if (1 != fscanf(file, "%f", &temp)) {
+        fclose(file);
+        ALOGE("%s: failed to read a float: %s", __func__, strerror(errno));
+        return errno ? -errno : -EIO;
+    }
+
+    fclose(file);
+
+    (*out) = (temperature_t) {
+        .type = type,
+        .name = name,
+        .current_value = temp * mult,
+        .throttling_threshold = throttling_threshold,
+        .shutdown_threshold = shutdown_threshold
+    };
+
+    return 0;
+}
+
+static ssize_t get_cpu_temperatures(temperature_t *list, size_t size) {
+    size_t cpu;
+
+    for (cpu = 0; cpu < CPU_NUM; cpu++) {
+        if (cpu >= size) {
+            break;
+        }
+        // tsens_tz_sensor[5-8]: temperature in Celsius.
+        ssize_t result = read_temperature(cpu + CPU0_SENSOR_NUM, DEVICE_TEMPERATURE_CPU,
+                CPU_LABEL[cpu], 1, CPU_THROTTLING_THRESHOLD, CPU_SHUTDOWN_THRESHOLD, &list[cpu]);
+        if (result != 0) {
+            return result;
+        }
+    }
+    return cpu;
+}
+
+static ssize_t get_temperatures(thermal_module_t *module, temperature_t *list, size_t size) {
+    ssize_t result = 0;
+    size_t current_index = 0;
+
+    if (list == NULL) {
+        return TEMPERATURE_NUM;
+    }
+
+    result = get_cpu_temperatures(list, size);
+    if (result < 0) {
+        return result;
+    }
+    current_index += result;
+
+    // GPU temperature.
+    if (current_index < size) {
+        // tsens_tz_sensor11: temperature in Celsius.
+        result = read_temperature(GPU_SENSOR_NUM, DEVICE_TEMPERATURE_GPU, GPU_LABEL,
+                1, UNKNOWN_TEMPERATURE, UNKNOWN_TEMPERATURE, &list[current_index]);
+        if (result < 0) {
+            return result;
+        }
+        current_index++;
+    }
+
+    // Battery temperature.
+    if (current_index < size) {
+        // hwmon sensor: battery: temperature in millidegrees Celsius.
+        result = read_temperature(BATTERY_SENSOR_NUM, DEVICE_TEMPERATURE_BATTERY, BATTERY_LABEL,
+                0.001, UNKNOWN_TEMPERATURE, BATTERY_SHUTDOWN_THRESHOLD, &list[current_index]);
+        if (result < 0) {
+            return result;
+        }
+        current_index++;
+    }
+
+    // Skin temperature.
+    if (current_index < size) {
+        // quiet_therm: temperature in Celsius.
+        result = read_temperature(SKIN_SENSOR_NUM, DEVICE_TEMPERATURE_SKIN, SKIN_LABEL, 1,
+                SKIN_THROTTLING_THRESHOLD, UNKNOWN_TEMPERATURE, &list[current_index]);
+        if (result < 0) {
+            return result;
+        }
+        current_index++;
+    }
+    return TEMPERATURE_NUM;
+}
+
+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 *file;
+    FILE *cpu_file;
+
+    if (list == NULL) {
+        return CPU_NUM;
+    }
+
+    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 || size == CPU_NUM) {
+            if (vals != 5) {
+                ALOGE("%s: failed to read CPU information from file: %s", __func__,
+                        strerror(errno));
+            } else {
+                ALOGE("/proc/stat file has incorrect format.");
+            }
+            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));
+            fclose(file);
+            return -errno;
+        }
+        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);
+
+        list[size] = (cpu_usage_t) {
+            .name = CPU_LABEL[size],
+            .active = active,
+            .total = total,
+            .is_online = online
+        };
+
+        size++;
+    }
+
+    fclose(file);
+
+    if (size != CPU_NUM) {
+        ALOGE("/proc/stat file has incorrect format.");
+        return -EIO;
+    }
+
+    return size;
+}
+
+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 = "Shamu Thermal HAL",
+        .author = "The Android Open Source Project",
+        .methods = &thermal_module_methods,
+    },
+    .getTemperatures = get_temperatures,
+    .getCpuUsages = get_cpu_usages,
+};