Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (c) 2017 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 android.os;
     18 
     19 import android.annotation.IntDef;
     20 import android.annotation.NonNull;
     21 import android.hardware.thermal.V2_0.TemperatureType;
     22 import android.hardware.thermal.V2_0.ThrottlingSeverity;
     23 
     24 import com.android.internal.util.Preconditions;
     25 
     26 import java.lang.annotation.Retention;
     27 import java.lang.annotation.RetentionPolicy;
     28 
     29 /**
     30  * Temperature values used by IThermalService.
     31  *
     32  * @hide
     33  */
     34 public final class Temperature implements Parcelable {
     35     /** Temperature value */
     36     private final float mValue;
     37     /** A Temperature type from ThermalHAL */
     38     private final int mType;
     39     /** Name of this Temperature */
     40     private final String mName;
     41     /** The level of the sensor is currently in throttling */
     42     private final int mStatus;
     43 
     44     @IntDef(prefix = { "THROTTLING_" }, value = {
     45             THROTTLING_NONE,
     46             THROTTLING_LIGHT,
     47             THROTTLING_MODERATE,
     48             THROTTLING_SEVERE,
     49             THROTTLING_CRITICAL,
     50             THROTTLING_EMERGENCY,
     51             THROTTLING_SHUTDOWN,
     52     })
     53     @Retention(RetentionPolicy.SOURCE)
     54     public @interface ThrottlingStatus {}
     55 
     56     /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */
     57     public static final int THROTTLING_NONE = ThrottlingSeverity.NONE;
     58     public static final int THROTTLING_LIGHT = ThrottlingSeverity.LIGHT;
     59     public static final int THROTTLING_MODERATE = ThrottlingSeverity.MODERATE;
     60     public static final int THROTTLING_SEVERE = ThrottlingSeverity.SEVERE;
     61     public static final int THROTTLING_CRITICAL = ThrottlingSeverity.CRITICAL;
     62     public static final int THROTTLING_EMERGENCY = ThrottlingSeverity.EMERGENCY;
     63     public static final int THROTTLING_SHUTDOWN = ThrottlingSeverity.SHUTDOWN;
     64 
     65     @IntDef(prefix = { "TYPE_" }, value = {
     66             TYPE_UNKNOWN,
     67             TYPE_CPU,
     68             TYPE_GPU,
     69             TYPE_BATTERY,
     70             TYPE_SKIN,
     71             TYPE_USB_PORT,
     72             TYPE_POWER_AMPLIFIER,
     73             TYPE_BCL_VOLTAGE,
     74             TYPE_BCL_CURRENT,
     75             TYPE_BCL_PERCENTAGE,
     76             TYPE_NPU,
     77     })
     78     @Retention(RetentionPolicy.SOURCE)
     79     public @interface Type {}
     80 
     81     /** Keep in sync with hardware/interfaces/thermal/2.0/types.hal */
     82     public static final int TYPE_UNKNOWN = TemperatureType.UNKNOWN;
     83     public static final int TYPE_CPU = TemperatureType.CPU;
     84     public static final int TYPE_GPU = TemperatureType.GPU;
     85     public static final int TYPE_BATTERY = TemperatureType.BATTERY;
     86     public static final int TYPE_SKIN = TemperatureType.SKIN;
     87     public static final int TYPE_USB_PORT = TemperatureType.USB_PORT;
     88     public static final int TYPE_POWER_AMPLIFIER = TemperatureType.POWER_AMPLIFIER;
     89     public static final int TYPE_BCL_VOLTAGE = TemperatureType.BCL_VOLTAGE;
     90     public static final int TYPE_BCL_CURRENT = TemperatureType.BCL_CURRENT;
     91     public static final int TYPE_BCL_PERCENTAGE = TemperatureType.BCL_PERCENTAGE;
     92     public static final int TYPE_NPU = TemperatureType.NPU;
     93 
     94     /**
     95      * Verify a valid Temperature type.
     96      *
     97      * @return true if a Temperature type is valid otherwise false.
     98      */
     99     public static boolean isValidType(@Type int type) {
    100         return type >= TYPE_UNKNOWN && type <= TYPE_NPU;
    101     }
    102 
    103     /**
    104      * Verify a valid throttling status.
    105      *
    106      * @return true if a status is valid otherwise false.
    107      */
    108     public static boolean isValidStatus(@ThrottlingStatus int status) {
    109         return status >= THROTTLING_NONE && status <= THROTTLING_SHUTDOWN;
    110     }
    111 
    112     public Temperature(float value, @Type int type,
    113             @NonNull String name, @ThrottlingStatus int status) {
    114         Preconditions.checkArgument(isValidType(type), "Invalid Type");
    115         Preconditions.checkArgument(isValidStatus(status) , "Invalid Status");
    116         mValue = value;
    117         mType = type;
    118         mName = Preconditions.checkStringNotEmpty(name);
    119         mStatus = status;
    120     }
    121 
    122     /**
    123      * Return the Temperature value.
    124      *
    125      * @return a Temperature value in floating point could be NaN.
    126      */
    127     public float getValue() {
    128         return mValue;
    129     }
    130 
    131     /**
    132      * Return the Temperature type.
    133      *
    134      * @return a Temperature type: TYPE_*
    135      */
    136     public @Type int getType() {
    137         return mType;
    138     }
    139 
    140     /**
    141      * Return the Temperature name.
    142      *
    143      * @return a Temperature name as String.
    144      */
    145     public String getName() {
    146         return mName;
    147     }
    148 
    149     /**
    150      * Return the Temperature throttling status.
    151      *
    152      * @return a Temperature throttling status: THROTTLING_*
    153      */
    154     public @ThrottlingStatus int getStatus() {
    155         return mStatus;
    156     }
    157 
    158     @Override
    159     public String toString() {
    160         return "Temperature{mValue=" + mValue + ", mType=" + mType
    161                 + ", mName=" + mName + ", mStatus=" + mStatus + "}";
    162     }
    163 
    164     @Override
    165     public int hashCode() {
    166         int hash = mName.hashCode();
    167         hash = 31 * hash + Float.hashCode(mValue);
    168         hash = 31 * hash + mType;
    169         hash = 31 * hash + mStatus;
    170         return hash;
    171     }
    172 
    173     @Override
    174     public boolean equals(Object o) {
    175         if (!(o instanceof Temperature)) {
    176             return false;
    177         }
    178         Temperature other = (Temperature) o;
    179         return other.mValue == mValue && other.mType == mType
    180                 && other.mName.equals(mName) && other.mStatus == mStatus;
    181     }
    182 
    183     @Override
    184     public void writeToParcel(Parcel p, int flags) {
    185         p.writeFloat(mValue);
    186         p.writeInt(mType);
    187         p.writeString(mName);
    188         p.writeInt(mStatus);
    189     }
    190 
    191     public static final @android.annotation.NonNull Parcelable.Creator<Temperature> CREATOR =
    192             new Parcelable.Creator<Temperature>() {
    193                 @Override
    194                 public Temperature createFromParcel(Parcel p) {
    195                     float value = p.readFloat();
    196                     int type = p.readInt();
    197                     String name = p.readString();
    198                     int status = p.readInt();
    199                     return new Temperature(value, type, name, status);
    200                 }
    201 
    202                 @Override
    203                 public Temperature[] newArray(int size) {
    204                     return new Temperature[size];
    205                 }
    206 
    207             };
    208 
    209     @Override
    210     public int describeContents() {
    211         return 0;
    212     }
    213 }
    214