Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server; |
| 18 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 19 | import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY; |
| 20 | import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU; |
| 21 | import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU; |
| 22 | import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN; |
| 23 | import static android.os.HardwarePropertiesManager.TEMPERATURE_CURRENT; |
| 24 | import static android.os.HardwarePropertiesManager.TEMPERATURE_SHUTDOWN; |
| 25 | import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING; |
| 26 | import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING_BELOW_VR_MIN; |
| 27 | |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 28 | import android.Manifest; |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 29 | import android.app.AppOpsManager; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 30 | import android.app.admin.DevicePolicyManager; |
| 31 | import android.content.Context; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.os.Binder; |
| 34 | import android.os.CpuUsageInfo; |
| 35 | import android.os.IHardwarePropertiesManager; |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 36 | import android.os.UserHandle; |
Rick Yiu | a6a12cf | 2018-09-12 17:02:24 +0800 | [diff] [blame] | 37 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 38 | import com.android.internal.util.DumpUtils; |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 39 | import com.android.server.vr.VrManagerInternal; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 40 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 41 | import java.io.FileDescriptor; |
| 42 | import java.io.PrintWriter; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 43 | import java.util.Arrays; |
| 44 | |
| 45 | /** |
| 46 | * Service for {@link HardwarePropertiesManager} |
| 47 | */ |
| 48 | public class HardwarePropertiesManagerService extends IHardwarePropertiesManager.Stub { |
| 49 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 50 | private static final String TAG = "HardwarePropertiesManagerService"; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 51 | private static native void nativeInit(); |
| 52 | |
| 53 | private static native float[] nativeGetFanSpeeds(); |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 54 | private static native float[] nativeGetDeviceTemperatures(int type, int source); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 55 | private static native CpuUsageInfo[] nativeGetCpuUsages(); |
| 56 | |
| 57 | private final Context mContext; |
| 58 | private final Object mLock = new Object(); |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 59 | private final AppOpsManager mAppOps; |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 60 | |
| 61 | public HardwarePropertiesManagerService(Context context) { |
| 62 | mContext = context; |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 63 | mAppOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 64 | synchronized (mLock) { |
| 65 | nativeInit(); |
| 66 | } |
| 67 | } |
| 68 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 69 | // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy |
| 70 | // cross user permission - b/63697518 |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 71 | @Override |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 72 | public float[] getDeviceTemperatures(String callingPackage, int type, int source) |
| 73 | throws SecurityException { |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 74 | enforceHardwarePropertiesRetrievalAllowed(callingPackage); |
| 75 | synchronized (mLock) { |
Polina Bondarenko | eb84552 | 2016-03-01 14:39:48 +0100 | [diff] [blame] | 76 | return nativeGetDeviceTemperatures(type, source); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 80 | // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy |
| 81 | // cross user permission - b/63697518 |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 82 | @Override |
| 83 | public CpuUsageInfo[] getCpuUsages(String callingPackage) throws SecurityException { |
| 84 | enforceHardwarePropertiesRetrievalAllowed(callingPackage); |
| 85 | synchronized (mLock) { |
| 86 | return nativeGetCpuUsages(); |
| 87 | } |
| 88 | } |
| 89 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 90 | // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy |
| 91 | // cross user permission - b/63697518 |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 92 | @Override |
| 93 | public float[] getFanSpeeds(String callingPackage) throws SecurityException { |
| 94 | enforceHardwarePropertiesRetrievalAllowed(callingPackage); |
| 95 | synchronized (mLock) { |
| 96 | return nativeGetFanSpeeds(); |
| 97 | } |
| 98 | } |
| 99 | |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 100 | private String getCallingPackageName() { |
| 101 | final String[] packages = mContext.getPackageManager().getPackagesForUid( |
| 102 | Binder.getCallingUid()); |
| 103 | if (packages != null && packages.length > 0) { |
| 104 | return packages[0]; |
| 105 | } |
| 106 | return "unknown"; |
| 107 | } |
| 108 | |
| 109 | private void dumpTempValues(String pkg, PrintWriter pw, int type, |
| 110 | String typeLabel) { |
| 111 | dumpTempValues(pkg, pw, type, typeLabel, "temperatures: ", |
| 112 | TEMPERATURE_CURRENT); |
| 113 | dumpTempValues(pkg, pw, type, typeLabel, "throttling temperatures: ", |
| 114 | TEMPERATURE_THROTTLING); |
| 115 | dumpTempValues(pkg, pw, type, typeLabel, "shutdown temperatures: ", |
| 116 | TEMPERATURE_SHUTDOWN); |
| 117 | dumpTempValues(pkg, pw, type, typeLabel, "vr throttling temperatures: ", |
| 118 | TEMPERATURE_THROTTLING_BELOW_VR_MIN); |
| 119 | } |
| 120 | |
| 121 | private void dumpTempValues(String pkg, PrintWriter pw, int type, |
| 122 | String typeLabel, String subLabel, int valueType) { |
| 123 | pw.println(typeLabel + subLabel + Arrays.toString(getDeviceTemperatures( |
| 124 | pkg, type, valueType))); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 129 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 130 | pw.println("****** Dump of HardwarePropertiesManagerService ******"); |
| 131 | |
| 132 | final String PKG = getCallingPackageName(); |
| 133 | dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_CPU, "CPU "); |
| 134 | dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_GPU, "GPU "); |
| 135 | dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_BATTERY, "Battery "); |
| 136 | dumpTempValues(PKG, pw, DEVICE_TEMPERATURE_SKIN, "Skin "); |
| 137 | |
| 138 | float[] fanSpeeds = getFanSpeeds(PKG); |
| 139 | pw.println("Fan speed: " + Arrays.toString(fanSpeeds) + "\n"); |
| 140 | |
| 141 | CpuUsageInfo[] cpuUsageInfos = getCpuUsages(PKG); |
| 142 | int core = 0; |
| 143 | for (int i = 0; i < cpuUsageInfos.length; i++) { |
| 144 | pw.println("Cpu usage of core: " + i + |
| 145 | ", active = " + cpuUsageInfos[i].getActive() + |
| 146 | ", total = " + cpuUsageInfos[i].getTotal()); |
| 147 | } |
| 148 | pw.println("****** End of HardwarePropertiesManagerService dump ******"); |
| 149 | } |
| 150 | |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 151 | /** |
| 152 | * Throws SecurityException if the calling package is not allowed to retrieve information |
| 153 | * provided by the service. |
| 154 | * |
| 155 | * @param callingPackage The calling package name. |
| 156 | * |
Polina Bondarenko | 8503fd5 | 2016-12-20 12:44:38 +0100 | [diff] [blame] | 157 | * @throws SecurityException if something other than the device owner, the current VR service, |
| 158 | * or a caller holding the {@link Manifest.permission#DEVICE_POWER} permission tries to |
| 159 | * retrieve information provided by this service. |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 160 | */ |
| 161 | private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage) |
| 162 | throws SecurityException { |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 163 | mAppOps.checkPackage(Binder.getCallingUid(), callingPackage); |
| 164 | final int userId = UserHandle.getUserId(Binder.getCallingUid()); |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 165 | final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 166 | final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); |
Polina Bondarenko | 8503fd5 | 2016-12-20 12:44:38 +0100 | [diff] [blame] | 167 | if (!dpm.isDeviceOwnerApp(callingPackage) |
Andrew Sapperstein | b7caf1d | 2016-12-14 15:39:20 -0800 | [diff] [blame] | 168 | && mContext.checkCallingOrSelfPermission(Manifest.permission.DEVICE_POWER) |
Rick Yiu | a6a12cf | 2018-09-12 17:02:24 +0800 | [diff] [blame] | 169 | != PackageManager.PERMISSION_GRANTED |
| 170 | && (vrService == null || !vrService.isCurrentVrListener(callingPackage, userId))) { |
| 171 | throw new SecurityException("The caller is neither a device owner" |
| 172 | + ", nor holding the DEVICE_POWER permission, nor the current VrListener."); |
Polina Bondarenko | f8754ac | 2016-02-12 20:38:23 +0100 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
Karthik Ravi Shankar | c59f0b3 | 2017-07-13 10:34:00 -0700 | [diff] [blame] | 176 | |
| 177 | |