Polina Bondarenko | 965ecbb | 2015-11-13 15:34:28 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "HardwarePropertiesManager-JNI" |
| 18 | |
| 19 | #include "JNIHelp.h" |
| 20 | #include "jni.h" |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | |
| 24 | #include <hardware/hardware_properties.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/String8.h> |
| 27 | |
| 28 | #include <hardware_properties/HardwarePropertiesManager.h> |
| 29 | |
| 30 | #include "core_jni_helpers.h" |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | static struct { |
| 37 | jclass clazz; |
| 38 | jmethodID initMethod; |
| 39 | } gCpuUsageInfoClassInfo; |
| 40 | |
| 41 | static struct hardware_properties_module* gHardwarePropertiesModule; |
| 42 | |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | static void nativeInit(JNIEnv* env, jobject obj) { |
| 46 | status_t err = hw_get_module(HARDWARE_PROPERTIES_HARDWARE_MODULE_ID, |
| 47 | (hw_module_t const**)&gHardwarePropertiesModule); |
| 48 | if (err) { |
| 49 | ALOGE("Couldn't load %s module (%s)", HARDWARE_PROPERTIES_HARDWARE_MODULE_ID, |
| 50 | strerror(-err)); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static jfloatArray nativeGetFanSpeeds(JNIEnv *env, jclass /* clazz */) { |
| 55 | if (gHardwarePropertiesModule && gHardwarePropertiesModule->getFanSpeeds) { |
| 56 | float *speeds = nullptr; |
| 57 | ssize_t size = gHardwarePropertiesModule->getFanSpeeds(gHardwarePropertiesModule, &speeds); |
| 58 | |
| 59 | if (speeds && size > 0) { |
| 60 | jfloatArray fanSpeeds = env->NewFloatArray(size); |
| 61 | env->SetFloatArrayRegion(fanSpeeds, 0, size, speeds); |
| 62 | free(speeds); |
| 63 | return fanSpeeds; |
| 64 | } |
| 65 | |
| 66 | if (size < 0) { |
| 67 | ALOGE("Cloudn't get fan speeds because of HAL error"); |
| 68 | } |
| 69 | } |
| 70 | return env->NewFloatArray(0); |
| 71 | } |
| 72 | |
| 73 | static jfloatArray nativeGetDeviceTemperatures(JNIEnv *env, jclass /* clazz */, int type) { |
| 74 | if (gHardwarePropertiesModule) { |
| 75 | ssize_t size = 0; |
| 76 | float *temps = nullptr; |
| 77 | switch (type) { |
| 78 | case DEVICE_TEMPERATURE_CPU: |
| 79 | if (gHardwarePropertiesModule->getCpuTemperatures) { |
| 80 | size = gHardwarePropertiesModule->getCpuTemperatures(gHardwarePropertiesModule, |
| 81 | &temps); |
| 82 | } |
| 83 | break; |
| 84 | case DEVICE_TEMPERATURE_GPU: |
| 85 | if (gHardwarePropertiesModule->getGpuTemperatures) { |
| 86 | size = gHardwarePropertiesModule->getGpuTemperatures(gHardwarePropertiesModule, |
| 87 | &temps); |
| 88 | } |
| 89 | break; |
| 90 | case DEVICE_TEMPERATURE_BATTERY: |
| 91 | if (gHardwarePropertiesModule->getBatteryTemperatures) { |
| 92 | size = gHardwarePropertiesModule->getBatteryTemperatures(gHardwarePropertiesModule, |
| 93 | &temps); |
| 94 | } |
| 95 | break; |
| 96 | } |
| 97 | if (temps && size > 0) { |
| 98 | jfloatArray deviceTemps = env->NewFloatArray(size); |
| 99 | env->SetFloatArrayRegion(deviceTemps, 0, size, temps); |
| 100 | free(temps); |
| 101 | return deviceTemps; |
| 102 | } |
| 103 | if (size < 0) { |
| 104 | ALOGE("Couldn't get device temperatures type=%d because of HAL error", type); |
| 105 | } |
| 106 | } |
| 107 | return env->NewFloatArray(0); |
| 108 | } |
| 109 | |
| 110 | static jobjectArray nativeGetCpuUsages(JNIEnv *env, jclass /* clazz */) { |
| 111 | if (gHardwarePropertiesModule && gHardwarePropertiesModule->getCpuUsages |
| 112 | && gCpuUsageInfoClassInfo.initMethod) { |
| 113 | int64_t *active_times = nullptr; |
| 114 | int64_t *total_times = nullptr; |
| 115 | ssize_t size = gHardwarePropertiesModule->getCpuUsages(gHardwarePropertiesModule, |
| 116 | &active_times, &total_times); |
| 117 | if (active_times && total_times && size > 0) { |
| 118 | jobjectArray cpuUsages = env->NewObjectArray(size, gCpuUsageInfoClassInfo.clazz, |
| 119 | nullptr); |
| 120 | for (ssize_t i = 0; i < size; ++i) { |
| 121 | jobject cpuUsage = env->NewObject(gCpuUsageInfoClassInfo.clazz, |
| 122 | gCpuUsageInfoClassInfo.initMethod, |
| 123 | active_times[i], total_times[i]); |
| 124 | env->SetObjectArrayElement(cpuUsages, i, cpuUsage); |
| 125 | } |
| 126 | free(active_times); |
| 127 | free(total_times); |
| 128 | return cpuUsages; |
| 129 | } |
| 130 | |
| 131 | if (size < 0) { |
| 132 | ALOGE("Couldn't get CPU usages because of HAL error"); |
| 133 | } |
| 134 | } |
| 135 | return env->NewObjectArray(0, gCpuUsageInfoClassInfo.clazz, nullptr); |
| 136 | } |
| 137 | |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | |
| 140 | static const JNINativeMethod gHardwarePropertiesManagerMethods[] = { |
| 141 | /* name, signature, funcPtr */ |
| 142 | { "nativeInit", "()V", |
| 143 | (void*) nativeInit }, |
| 144 | { "nativeGetFanSpeeds", "()[F", |
| 145 | (void*) nativeGetFanSpeeds }, |
| 146 | { "nativeGetDeviceTemperatures", "(I)[F", |
| 147 | (void*) nativeGetDeviceTemperatures }, |
| 148 | { "nativeGetCpuUsages", "()[Landroid/os/CpuUsageInfo;", |
| 149 | (void*) nativeGetCpuUsages } |
| 150 | }; |
| 151 | |
| 152 | int register_android_os_HardwarePropertiesManager(JNIEnv* env) { |
| 153 | gHardwarePropertiesModule = nullptr; |
| 154 | int res = jniRegisterNativeMethods(env, "android/os/HardwarePropertiesManager", |
| 155 | gHardwarePropertiesManagerMethods, |
| 156 | NELEM(gHardwarePropertiesManagerMethods)); |
| 157 | jclass clazz = env->FindClass("android/os/CpuUsageInfo"); |
| 158 | gCpuUsageInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); |
| 159 | gCpuUsageInfoClassInfo.initMethod = GetMethodIDOrDie(env, gCpuUsageInfoClassInfo.clazz, |
| 160 | "<init>", "(JJ)V"); |
| 161 | return res; |
| 162 | } |
| 163 | |
| 164 | } /* namespace android */ |