Home | History | Annotate | Download | only in server
      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 
     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 
     28 import android.Manifest;
     29 import android.app.AppOpsManager;
     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;
     36 import android.os.UserHandle;
     37 
     38 import com.android.internal.util.DumpUtils;
     39 import com.android.server.vr.VrManagerInternal;
     40 
     41 import java.io.FileDescriptor;
     42 import java.io.PrintWriter;
     43 import java.util.Arrays;
     44 
     45 /**
     46  * Service for {@link HardwarePropertiesManager}
     47  */
     48 public class HardwarePropertiesManagerService extends IHardwarePropertiesManager.Stub {
     49 
     50     private static final String TAG = "HardwarePropertiesManagerService";
     51     private static native void nativeInit();
     52 
     53     private static native float[] nativeGetFanSpeeds();
     54     private static native float[] nativeGetDeviceTemperatures(int type, int source);
     55     private static native CpuUsageInfo[] nativeGetCpuUsages();
     56 
     57     private final Context mContext;
     58     private final Object mLock = new Object();
     59     private final AppOpsManager mAppOps;
     60 
     61     public HardwarePropertiesManagerService(Context context) {
     62         mContext = context;
     63         mAppOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE);
     64         synchronized (mLock) {
     65             nativeInit();
     66         }
     67     }
     68 
     69     // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
     70     // cross user permission - b/63697518
     71     @Override
     72     public float[] getDeviceTemperatures(String callingPackage, int type, int source)
     73             throws SecurityException {
     74         enforceHardwarePropertiesRetrievalAllowed(callingPackage);
     75         synchronized (mLock) {
     76             return nativeGetDeviceTemperatures(type, source);
     77         }
     78     }
     79 
     80     // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
     81     // cross user permission - b/63697518
     82     @Override
     83     public CpuUsageInfo[] getCpuUsages(String callingPackage) throws SecurityException {
     84         enforceHardwarePropertiesRetrievalAllowed(callingPackage);
     85         synchronized (mLock) {
     86             return nativeGetCpuUsages();
     87         }
     88     }
     89 
     90     // TODO - Make HardwarePropertiesManager APIs require a userId to verifiy
     91     // cross user permission - b/63697518
     92     @Override
     93     public float[] getFanSpeeds(String callingPackage) throws SecurityException {
     94         enforceHardwarePropertiesRetrievalAllowed(callingPackage);
     95         synchronized (mLock) {
     96             return nativeGetFanSpeeds();
     97         }
     98     }
     99 
    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 
    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      *
    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.
    160      */
    161     private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage)
    162             throws SecurityException {
    163         mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
    164         final int userId = UserHandle.getUserId(Binder.getCallingUid());
    165         final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
    166         final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
    167         if (!dpm.isDeviceOwnerApp(callingPackage)
    168                 && mContext.checkCallingOrSelfPermission(Manifest.permission.DEVICE_POWER)
    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.");
    173         }
    174     }
    175 }
    176 
    177 
    178