blob: e21a3d7917d10ab351bc217736d544ab29e68908 [file] [log] [blame]
Polina Bondarenkof8754ac2016-02-12 20:38:23 +01001/*
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
17package com.android.server;
18
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070019import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY;
20import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU;
21import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU;
22import static android.os.HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN;
23import static android.os.HardwarePropertiesManager.TEMPERATURE_CURRENT;
24import static android.os.HardwarePropertiesManager.TEMPERATURE_SHUTDOWN;
25import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING;
26import static android.os.HardwarePropertiesManager.TEMPERATURE_THROTTLING_BELOW_VR_MIN;
27
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -080028import android.Manifest;
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070029import android.app.AppOpsManager;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010030import android.app.admin.DevicePolicyManager;
31import android.content.Context;
32import android.content.pm.PackageManager;
33import android.os.Binder;
34import android.os.CpuUsageInfo;
35import android.os.IHardwarePropertiesManager;
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -070036import android.os.UserHandle;
Rick Yiu84953332018-09-12 17:02:24 +080037
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070038import com.android.internal.util.DumpUtils;
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -070039import com.android.server.vr.VrManagerInternal;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010040
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070041import java.io.FileDescriptor;
42import java.io.PrintWriter;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010043import java.util.Arrays;
44
45/**
46 * Service for {@link HardwarePropertiesManager}
47 */
48public class HardwarePropertiesManagerService extends IHardwarePropertiesManager.Stub {
49
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070050 private static final String TAG = "HardwarePropertiesManagerService";
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010051 private static native void nativeInit();
52
53 private static native float[] nativeGetFanSpeeds();
Polina Bondarenkoeb845522016-03-01 14:39:48 +010054 private static native float[] nativeGetDeviceTemperatures(int type, int source);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010055 private static native CpuUsageInfo[] nativeGetCpuUsages();
56
57 private final Context mContext;
58 private final Object mLock = new Object();
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070059 private final AppOpsManager mAppOps;
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010060
61 public HardwarePropertiesManagerService(Context context) {
62 mContext = context;
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070063 mAppOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010064 synchronized (mLock) {
65 nativeInit();
66 }
67 }
68
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070069 // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
70 // cross user permission - b/63697518
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010071 @Override
Polina Bondarenkoeb845522016-03-01 14:39:48 +010072 public float[] getDeviceTemperatures(String callingPackage, int type, int source)
73 throws SecurityException {
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010074 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
75 synchronized (mLock) {
Polina Bondarenkoeb845522016-03-01 14:39:48 +010076 return nativeGetDeviceTemperatures(type, source);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010077 }
78 }
79
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070080 // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
81 // cross user permission - b/63697518
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010082 @Override
83 public CpuUsageInfo[] getCpuUsages(String callingPackage) throws SecurityException {
84 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
85 synchronized (mLock) {
86 return nativeGetCpuUsages();
87 }
88 }
89
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -070090 // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
91 // cross user permission - b/63697518
Polina Bondarenkof8754ac2016-02-12 20:38:23 +010092 @Override
93 public float[] getFanSpeeds(String callingPackage) throws SecurityException {
94 enforceHardwarePropertiesRetrievalAllowed(callingPackage);
95 synchronized (mLock) {
96 return nativeGetFanSpeeds();
97 }
98 }
99
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -0700100 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 Bondarenkof8754ac2016-02-12 20:38:23 +0100151 /**
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 Bondarenko8503fd52016-12-20 12:44:38 +0100157 * @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 Bondarenkof8754ac2016-02-12 20:38:23 +0100160 */
161 private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage)
162 throws SecurityException {
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -0700163 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
164 final int userId = UserHandle.getUserId(Binder.getCallingUid());
Ruben Brunk8f1d5cb2016-03-22 18:08:41 -0700165 final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
Polina Bondarenkof8754ac2016-02-12 20:38:23 +0100166 final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
Polina Bondarenko8503fd52016-12-20 12:44:38 +0100167 if (!dpm.isDeviceOwnerApp(callingPackage)
Andrew Sappersteinb7caf1d2016-12-14 15:39:20 -0800168 && mContext.checkCallingOrSelfPermission(Manifest.permission.DEVICE_POWER)
Rick Yiu84953332018-09-12 17:02:24 +0800169 != 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 Bondarenkof8754ac2016-02-12 20:38:23 +0100173 }
174 }
175}
Karthik Ravi Shankarc59f0b32017-07-13 10:34:00 -0700176
177