Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 71cb0c5b authored by Polina Bondarenko's avatar Polina Bondarenko
Browse files

thermal: add usage of IThermal.

HardwarePropertiesManagerService native impl uses IThermal instead of
thermal_module.

Bug:32022261
Test: CTS
Change-Id: I85e5c5bedb1cb6762207ccfa3e3a7dd4c00ee30a
parent 3f2e215c
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -55,9 +55,7 @@ public class HardwarePropertiesManager {
    public @interface TemperatureSource {}

    /**
     * Device temperature types. These must match the values in
     * frameworks/native/include/hardwareproperties/HardwarePropertiesManager.h
     * TODO(b/32022261) Remove this comment.
     * Device temperature types.
     */
    /** Temperature of CPUs in Celsius. */
    public static final int DEVICE_TEMPERATURE_CPU = Constants.TemperatureType.CPU;
+1 −0
Original line number Diff line number Diff line
@@ -73,3 +73,4 @@ LOCAL_SHARED_LIBRARIES += \
    android.hardware.vr@1.0 \
    android.hardware.audio.common@2.0 \
    android.hardware.tv.input@1.0 \
    android.hardware.thermal@1.0 \
+106 −110
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@

#include <stdlib.h>

#include <hardware/thermal.h>
#include <android/hardware/thermal/1.0/IThermal.h>
#include <utils/Log.h>
#include <utils/String8.h>

@@ -29,6 +29,15 @@

namespace android {

using hardware::hidl_vec;
using hardware::Status;
using hardware::thermal::V1_0::CoolingDevice;
using hardware::thermal::V1_0::CpuUsage;
using hardware::thermal::V1_0::IThermal;
using hardware::thermal::V1_0::Temperature;
using hardware::thermal::V1_0::ThermalStatus;
using hardware::thermal::V1_0::ThermalStatusCode;

// ---------------------------------------------------------------------------

// These values must be kept in sync with the temperature source constants in
@@ -45,137 +54,130 @@ static struct {
    jmethodID initMethod;
} gCpuUsageInfoClassInfo;

jfloat gUndefinedTemperature;

static struct thermal_module* gThermalModule;
static sp<IThermal> gThermalModule;

// ----------------------------------------------------------------------------

static void nativeInit(JNIEnv* env, jobject obj) {
    status_t err = hw_get_module(THERMAL_HARDWARE_MODULE_ID, (hw_module_t const**)&gThermalModule);
    if (err) {
        ALOGE("Couldn't load %s module (%s)", THERMAL_HARDWARE_MODULE_ID, strerror(-err));
    // TODO(b/31632518)
    if (gThermalModule == nullptr) {
        gThermalModule = IThermal::getService("thermal");
    }

    if (gThermalModule == nullptr) {
        ALOGE("Undable to get Thermal service.");
    }
}

static jfloatArray nativeGetFanSpeeds(JNIEnv *env, jclass /* clazz */) {
    if (gThermalModule && gThermalModule->getCoolingDevices) {
        ssize_t list_size = gThermalModule->getCoolingDevices(gThermalModule, nullptr, 0);

        if (list_size >= 0) {
            cooling_device_t *list = (cooling_device_t *)
                    malloc(list_size * sizeof(cooling_device_t));
            ssize_t size = gThermalModule->getCoolingDevices(gThermalModule, list, list_size);
            if (size >= 0) {
                if (list_size > size) {
                    list_size = size;
                }
                jfloat values[list_size];
                for (ssize_t i = 0; i < list_size; ++i) {
                    values[i] = list[i].current_value;
    if (gThermalModule == nullptr) {
        ALOGE("Couldn't get fan speeds because of HAL error.");
        return env->NewFloatArray(0);
    }

                jfloatArray fanSpeeds = env->NewFloatArray(list_size);
                env->SetFloatArrayRegion(fanSpeeds, 0, list_size, values);
                free(list);
                return fanSpeeds;
    hidl_vec<CoolingDevice> list;
    Status status = gThermalModule->getCoolingDevices(
            [&list](ThermalStatus status, hidl_vec<CoolingDevice> devices) {
                if (status.code == ThermalStatusCode::SUCCESS) {
                    list = std::move(devices);
                } else {
                    ALOGE("Couldn't get fan speeds because of HAL error: %s",
                          status.debugMessage.c_str());
                }
            }).getStatus();

            free(list);
    if (!status.isOk()) {
        ALOGE("getCoolingDevices failed status: %d", status.exceptionCode());
    }

        ALOGE("Cloudn't get fan speeds because of HAL error");
    float values[list.size()];
    for (size_t i = 0; i < list.size(); ++i) {
        values[i] = list[i].currentValue;
    }
    return env->NewFloatArray(0);
    jfloatArray fanSpeeds = env->NewFloatArray(list.size());
    env->SetFloatArrayRegion(fanSpeeds, 0, list.size(), values);
    return fanSpeeds;
}

static jfloatArray nativeGetDeviceTemperatures(JNIEnv *env, jclass /* clazz */, int type,
                                               int source) {
    if (gThermalModule && gThermalModule->getTemperatures) {
        ssize_t list_size = gThermalModule->getTemperatures(gThermalModule, nullptr, 0);
        if (list_size >= 0) {
            temperature_t *list = (temperature_t *) malloc(list_size * sizeof(temperature_t));
            ssize_t size = gThermalModule->getTemperatures(gThermalModule, list, list_size);
            if (size >= 0) {
                if (list_size > size) {
                    list_size = size;
    if (gThermalModule == nullptr) {
        ALOGE("Couldn't get device temperatures because of HAL error.");
        return env->NewFloatArray(0);
    }
    hidl_vec<Temperature> list;
    Status status = gThermalModule->getTemperatures(
            [&list](ThermalStatus status, hidl_vec<Temperature> temperatures) {
                if (status.code == ThermalStatusCode::SUCCESS) {
                    list = std::move(temperatures);
                } else {
                    ALOGE("Couldn't get temperatures because of HAL error: %s",
                          status.debugMessage.c_str());
                }
            }).getStatus();

                jfloat values[list_size];
                size_t length = 0;
    if (!status.isOk()) {
        ALOGE("getDeviceTemperatures failed status: %d", status.exceptionCode());
    }

                for (ssize_t i = 0; i < list_size; ++i) {
                    if (list[i].type == type) {
    jfloat values[list.size()];
    size_t length = 0;
    for (size_t i = 0; i < list.size(); ++i) {
        if (static_cast<int>(list[i].type) == type) {
            switch (source) {
                case TEMPERATURE_CURRENT:
                                if (list[i].current_value == UNKNOWN_TEMPERATURE) {
                                    values[length++] = gUndefinedTemperature;
                                } else {
                                    values[length++] = list[i].current_value;
                                }
                    values[length++] = list[i].currentValue;
                    break;
                case TEMPERATURE_THROTTLING:
                                if (list[i].throttling_threshold == UNKNOWN_TEMPERATURE) {
                                    values[length++] = gUndefinedTemperature;
                                } else {
                                    values[length++] = list[i].throttling_threshold;
                                }
                    values[length++] = list[i].throttlingThreshold;
                    break;
                case TEMPERATURE_SHUTDOWN:
                                if (list[i].shutdown_threshold == UNKNOWN_TEMPERATURE) {
                                    values[length++] = gUndefinedTemperature;
                                } else {
                                    values[length++] = list[i].shutdown_threshold;
                                }
                    values[length++] = list[i].shutdownThreshold;
                    break;
                case TEMPERATURE_THROTTLING_BELOW_VR_MIN:
                                if (list[i].vr_throttling_threshold == UNKNOWN_TEMPERATURE) {
                                    values[length++] = gUndefinedTemperature;
                                } else {
                                    values[length++] = list[i].vr_throttling_threshold;
                                }
                    values[length++] = list[i].vrThrottlingThreshold;
                    break;
            }
        }
    }
    jfloatArray deviceTemps = env->NewFloatArray(length);
    env->SetFloatArrayRegion(deviceTemps, 0, length, values);
                free(list);
    return deviceTemps;
}
            free(list);

static jobjectArray nativeGetCpuUsages(JNIEnv *env, jclass /* clazz */) {
    if (gThermalModule == nullptr || !gCpuUsageInfoClassInfo.initMethod) {
        ALOGE("Couldn't get CPU usages because of HAL error.");
        return env->NewObjectArray(0, gCpuUsageInfoClassInfo.clazz, nullptr);
    }
        ALOGE("Couldn't get device temperatures because of HAL error");
    hidl_vec<CpuUsage> list;
    Status status = gThermalModule->getCpuUsages(
            [&list](ThermalStatus status, hidl_vec<CpuUsage> cpuUsages) {
                if (status.code == ThermalStatusCode::SUCCESS) {
                    list = std::move(cpuUsages);
                } else {
                    ALOGE("Couldn't get CPU usages because of HAL error: %s",
                          status.debugMessage.c_str());
                }
    return env->NewFloatArray(0);
            }).getStatus();

    if (!status.isOk()) {
        ALOGE("getCpuUsages failed status: %d", status.exceptionCode());
    }

static jobjectArray nativeGetCpuUsages(JNIEnv *env, jclass /* clazz */) {
    if (gThermalModule && gThermalModule->getCpuUsages
            && gCpuUsageInfoClassInfo.initMethod) {
        ssize_t size = gThermalModule->getCpuUsages(gThermalModule, nullptr);
        if (size >= 0) {
            cpu_usage_t *list = (cpu_usage_t *) malloc(size * sizeof(cpu_usage_t));
            size = gThermalModule->getCpuUsages(gThermalModule, list);
            if (size >= 0) {
                jobjectArray cpuUsages = env->NewObjectArray(size, gCpuUsageInfoClassInfo.clazz,
    jobjectArray cpuUsages = env->NewObjectArray(list.size(), gCpuUsageInfoClassInfo.clazz,
                                                 nullptr);
                for (ssize_t i = 0; i < size; ++i) {
                    if (list[i].is_online) {
    for (size_t i = 0; i < list.size(); ++i) {
        if (list[i].isOnline) {
            jobject cpuUsage = env->NewObject(gCpuUsageInfoClassInfo.clazz,
                                gCpuUsageInfoClassInfo.initMethod, list[i].active, list[i].total);
                                              gCpuUsageInfoClassInfo.initMethod,
                                              list[i].active,
                                              list[i].total);
            env->SetObjectArrayElement(cpuUsages, i, cpuUsage);
        }
    }
                free(list);
    return cpuUsages;
}
            free(list);
        }
        ALOGE("Couldn't get CPU usages because of HAL error");
    }
    return env->NewObjectArray(0, gCpuUsageInfoClassInfo.clazz, nullptr);
}

// ----------------------------------------------------------------------------

@@ -200,12 +202,6 @@ int register_android_server_HardwarePropertiesManagerService(JNIEnv* env) {
    gCpuUsageInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
    gCpuUsageInfoClassInfo.initMethod = GetMethodIDOrDie(env, gCpuUsageInfoClassInfo.clazz,
                                                         "<init>", "(JJ)V");

    clazz = env->FindClass("android/os/HardwarePropertiesManager");
    jfieldID undefined_temperature_field = GetStaticFieldIDOrDie(env, clazz,
                                                                 "UNDEFINED_TEMPERATURE", "F");
    gUndefinedTemperature = env->GetStaticFloatField(clazz, undefined_temperature_field);

    return res;
}